copy_effects.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*************************************************************************/
  2. /* copy_effects.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "copy_effects.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  34. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  35. #include "thirdparty/misc/cubemap_coeffs.h"
  36. using namespace RendererRD;
  37. CopyEffects *CopyEffects::singleton = nullptr;
  38. CopyEffects *CopyEffects::get_singleton() {
  39. return singleton;
  40. }
  41. CopyEffects::CopyEffects(bool p_prefer_raster_effects) {
  42. singleton = this;
  43. prefer_raster_effects = p_prefer_raster_effects;
  44. if (prefer_raster_effects) {
  45. // init blur shader (on compute use copy shader)
  46. Vector<String> blur_modes;
  47. blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP
  48. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR
  49. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW
  50. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE
  51. blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
  52. blur_modes.push_back("\n#define MODE_SET_COLOR\n"); // BLUR_MODE_SET_COLOR
  53. blur_raster.shader.initialize(blur_modes);
  54. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  55. blur_raster.shader_version = blur_raster.shader.version_create();
  56. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  57. blur_raster.pipelines[i].setup(blur_raster.shader.version_get_shader(blur_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  58. }
  59. } else {
  60. // not used in clustered
  61. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  62. blur_raster.pipelines[i].clear();
  63. }
  64. Vector<String> copy_modes;
  65. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  66. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  67. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");
  68. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  69. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  70. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  71. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  72. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  73. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  74. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  75. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  76. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  77. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  78. copy.shader.initialize(copy_modes);
  79. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  80. copy.shader_version = copy.shader.version_create();
  81. for (int i = 0; i < COPY_MODE_MAX; i++) {
  82. if (copy.shader.is_variant_enabled(i)) {
  83. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  84. }
  85. }
  86. }
  87. {
  88. Vector<String> copy_modes;
  89. copy_modes.push_back("\n"); // COPY_TO_FB_COPY
  90. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n"); // COPY_TO_FB_COPY_PANORAMA_TO_DP
  91. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_COPY2
  92. copy_modes.push_back("\n#define MULTIVIEW\n"); // COPY_TO_FB_MULTIVIEW
  93. copy_modes.push_back("\n#define MULTIVIEW\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_MULTIVIEW_WITH_DEPTH
  94. copy_modes.push_back("\n#define MODE_SET_COLOR\n"); // COPY_TO_FB_SET_COLOR
  95. copy_to_fb.shader.initialize(copy_modes);
  96. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  97. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW, false);
  98. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW_WITH_DEPTH, false);
  99. }
  100. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  101. //use additive
  102. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  103. if (copy_to_fb.shader.is_variant_enabled(i)) {
  104. copy_to_fb.pipelines[i].setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  105. } else {
  106. copy_to_fb.pipelines[i].clear();
  107. }
  108. }
  109. }
  110. {
  111. // Initialize copier
  112. Vector<String> copy_modes;
  113. copy_modes.push_back("\n");
  114. cube_to_dp.shader.initialize(copy_modes);
  115. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  116. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  117. RD::PipelineDepthStencilState dss;
  118. dss.enable_depth_test = true;
  119. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  120. dss.enable_depth_write = true;
  121. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  122. }
  123. {
  124. //Initialize cubemap downsampler
  125. Vector<String> cubemap_downsampler_modes;
  126. cubemap_downsampler_modes.push_back("");
  127. if (prefer_raster_effects) {
  128. cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);
  129. cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();
  130. cubemap_downsampler.raster_pipeline.setup(cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  131. } else {
  132. cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);
  133. cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();
  134. cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  135. cubemap_downsampler.raster_pipeline.clear();
  136. }
  137. }
  138. {
  139. // Initialize cubemap filter
  140. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  141. Vector<String> cubemap_filter_modes;
  142. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  143. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  144. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  145. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  146. if (filter.use_high_quality) {
  147. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  148. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  149. } else {
  150. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  151. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  152. }
  153. if (prefer_raster_effects) {
  154. filter.raster_shader.initialize(cubemap_filter_modes);
  155. // array variants are not supported in raster
  156. filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);
  157. filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);
  158. filter.shader_version = filter.raster_shader.version_create();
  159. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  160. if (filter.raster_shader.is_variant_enabled(i)) {
  161. filter.raster_pipelines[i].setup(filter.raster_shader.version_get_shader(filter.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  162. } else {
  163. filter.raster_pipelines[i].clear();
  164. }
  165. }
  166. Vector<RD::Uniform> uniforms;
  167. {
  168. RD::Uniform u;
  169. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  170. u.binding = 0;
  171. u.append_id(filter.coefficient_buffer);
  172. uniforms.push_back(u);
  173. }
  174. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.raster_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  175. } else {
  176. filter.compute_shader.initialize(cubemap_filter_modes);
  177. filter.shader_version = filter.compute_shader.version_create();
  178. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  179. filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));
  180. filter.raster_pipelines[i].clear();
  181. }
  182. Vector<RD::Uniform> uniforms;
  183. {
  184. RD::Uniform u;
  185. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  186. u.binding = 0;
  187. u.append_id(filter.coefficient_buffer);
  188. uniforms.push_back(u);
  189. }
  190. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  191. }
  192. }
  193. {
  194. // Initialize roughness
  195. Vector<String> cubemap_roughness_modes;
  196. cubemap_roughness_modes.push_back("");
  197. if (prefer_raster_effects) {
  198. roughness.raster_shader.initialize(cubemap_roughness_modes);
  199. roughness.shader_version = roughness.raster_shader.version_create();
  200. roughness.raster_pipeline.setup(roughness.raster_shader.version_get_shader(roughness.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  201. } else {
  202. roughness.compute_shader.initialize(cubemap_roughness_modes);
  203. roughness.shader_version = roughness.compute_shader.version_create();
  204. roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));
  205. roughness.raster_pipeline.clear();
  206. }
  207. }
  208. {
  209. Vector<String> specular_modes;
  210. specular_modes.push_back("\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD
  211. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR
  212. specular_modes.push_back("\n"); // SPECULAR_MERGE_ADDITIVE_ADD
  213. specular_modes.push_back("\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR
  214. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD_MULTIVIEW
  215. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR_MULTIVIEW
  216. specular_modes.push_back("\n#define USE_MULTIVIEW\n"); // SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW
  217. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW
  218. specular_merge.shader.initialize(specular_modes);
  219. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  220. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADD_MULTIVIEW, false);
  221. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_SSR_MULTIVIEW, false);
  222. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW, false);
  223. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW, false);
  224. }
  225. specular_merge.shader_version = specular_merge.shader.version_create();
  226. //use additive
  227. RD::PipelineColorBlendState::Attachment ba;
  228. ba.enable_blend = true;
  229. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  230. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  231. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  232. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  233. ba.color_blend_op = RD::BLEND_OP_ADD;
  234. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  235. RD::PipelineColorBlendState blend_additive;
  236. blend_additive.attachments.push_back(ba);
  237. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  238. if (specular_merge.shader.is_variant_enabled(i)) {
  239. RD::PipelineColorBlendState blend_state;
  240. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR || i == SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW || i == SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW) {
  241. blend_state = blend_additive;
  242. } else {
  243. blend_state = RD::PipelineColorBlendState::create_disabled();
  244. }
  245. specular_merge.pipelines[i].setup(specular_merge.shader.version_get_shader(specular_merge.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  246. }
  247. }
  248. }
  249. }
  250. CopyEffects::~CopyEffects() {
  251. if (prefer_raster_effects) {
  252. blur_raster.shader.version_free(blur_raster.shader_version);
  253. cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);
  254. filter.raster_shader.version_free(filter.shader_version);
  255. roughness.raster_shader.version_free(roughness.shader_version);
  256. } else {
  257. copy.shader.version_free(copy.shader_version);
  258. cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);
  259. filter.compute_shader.version_free(filter.shader_version);
  260. roughness.compute_shader.version_free(roughness.shader_version);
  261. }
  262. specular_merge.shader.version_free(specular_merge.shader_version);
  263. RD::get_singleton()->free(filter.coefficient_buffer);
  264. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  265. RD::get_singleton()->free(filter.image_uniform_set);
  266. }
  267. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  268. RD::get_singleton()->free(filter.uniform_set);
  269. }
  270. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  271. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  272. singleton = nullptr;
  273. }
  274. void CopyEffects::copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_all_source, bool p_8_bit_dst, bool p_alpha_to_one) {
  275. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_to_rect shader with the mobile renderer.");
  276. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  277. ERR_FAIL_NULL(uniform_set_cache);
  278. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  279. ERR_FAIL_NULL(material_storage);
  280. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  281. if (p_flip_y) {
  282. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  283. }
  284. if (p_force_luminance) {
  285. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  286. }
  287. if (p_all_source) {
  288. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  289. }
  290. if (p_alpha_to_one) {
  291. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  292. }
  293. copy.push_constant.section[0] = p_rect.position.x;
  294. copy.push_constant.section[1] = p_rect.position.y;
  295. copy.push_constant.section[2] = p_rect.size.width;
  296. copy.push_constant.section[3] = p_rect.size.height;
  297. copy.push_constant.target[0] = p_rect.position.x;
  298. copy.push_constant.target[1] = p_rect.position.y;
  299. // setup our uniforms
  300. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  301. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  302. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  303. CopyMode mode = p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY;
  304. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  305. ERR_FAIL_COND(shader.is_null());
  306. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  307. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  308. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  309. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  310. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  311. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  312. RD::get_singleton()->compute_list_end();
  313. }
  314. void CopyEffects::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
  315. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_cubemap_to_panorama shader with the mobile renderer.");
  316. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  317. ERR_FAIL_NULL(uniform_set_cache);
  318. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  319. ERR_FAIL_NULL(material_storage);
  320. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  321. copy.push_constant.section[0] = 0;
  322. copy.push_constant.section[1] = 0;
  323. copy.push_constant.section[2] = p_panorama_size.width;
  324. copy.push_constant.section[3] = p_panorama_size.height;
  325. copy.push_constant.target[0] = 0;
  326. copy.push_constant.target[1] = 0;
  327. copy.push_constant.camera_z_far = p_lod;
  328. // setup our uniforms
  329. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  330. RD::Uniform u_source_cube(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cube }));
  331. RD::Uniform u_dest_panorama(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_panorama);
  332. CopyMode mode = p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA;
  333. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  334. ERR_FAIL_COND(shader.is_null());
  335. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  336. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  337. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cube), 0);
  338. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_panorama), 3);
  339. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  340. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  341. RD::get_singleton()->compute_list_end();
  342. }
  343. void CopyEffects::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  344. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_depth_to_rect shader with the mobile renderer.");
  345. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  346. ERR_FAIL_NULL(uniform_set_cache);
  347. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  348. ERR_FAIL_NULL(material_storage);
  349. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  350. if (p_flip_y) {
  351. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  352. }
  353. copy.push_constant.section[0] = 0;
  354. copy.push_constant.section[1] = 0;
  355. copy.push_constant.section[2] = p_rect.size.width;
  356. copy.push_constant.section[3] = p_rect.size.height;
  357. copy.push_constant.target[0] = p_rect.position.x;
  358. copy.push_constant.target[1] = p_rect.position.y;
  359. // setup our uniforms
  360. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  361. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  362. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  363. CopyMode mode = COPY_MODE_SIMPLY_COPY_DEPTH;
  364. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  365. ERR_FAIL_COND(shader.is_null());
  366. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  367. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  368. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  369. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  370. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  371. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  372. RD::get_singleton()->compute_list_end();
  373. }
  374. void CopyEffects::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) {
  375. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_depth_to_rect_and_linearize shader with the mobile renderer.");
  376. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  377. ERR_FAIL_NULL(uniform_set_cache);
  378. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  379. ERR_FAIL_NULL(material_storage);
  380. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  381. if (p_flip_y) {
  382. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  383. }
  384. copy.push_constant.section[0] = 0;
  385. copy.push_constant.section[1] = 0;
  386. copy.push_constant.section[2] = p_rect.size.width;
  387. copy.push_constant.section[3] = p_rect.size.height;
  388. copy.push_constant.target[0] = p_rect.position.x;
  389. copy.push_constant.target[1] = p_rect.position.y;
  390. copy.push_constant.camera_z_far = p_z_far;
  391. copy.push_constant.camera_z_near = p_z_near;
  392. // setup our uniforms
  393. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  394. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  395. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  396. CopyMode mode = COPY_MODE_LINEARIZE_DEPTH;
  397. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  398. ERR_FAIL_COND(shader.is_null());
  399. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  400. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  401. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  402. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  403. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  404. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  405. RD::get_singleton()->compute_list_end();
  406. }
  407. void CopyEffects::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) {
  408. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  409. ERR_FAIL_NULL(uniform_set_cache);
  410. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  411. ERR_FAIL_NULL(material_storage);
  412. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  413. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SECTION;
  414. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  415. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  416. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  417. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  418. if (p_flip_y) {
  419. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
  420. }
  421. copy_to_fb.push_constant.luminance_multiplier = 1.0;
  422. // setup our uniforms
  423. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  424. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  425. CopyToFBMode mode = p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY;
  426. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  427. ERR_FAIL_COND(shader.is_null());
  428. RD::DrawListID draw_list = p_draw_list;
  429. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  430. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  431. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  432. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  433. RD::get_singleton()->draw_list_draw(draw_list, true);
  434. }
  435. void CopyEffects::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_alpha_to_zero, bool p_srgb, RID p_secondary, bool p_multiview, bool p_alpha_to_one, bool p_linear) {
  436. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  437. ERR_FAIL_NULL(uniform_set_cache);
  438. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  439. ERR_FAIL_NULL(material_storage);
  440. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  441. copy_to_fb.push_constant.luminance_multiplier = 1.0;
  442. if (p_flip_y) {
  443. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
  444. }
  445. if (p_force_luminance) {
  446. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FORCE_LUMINANCE;
  447. }
  448. if (p_alpha_to_zero) {
  449. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ZERO;
  450. }
  451. if (p_srgb) {
  452. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_SRGB;
  453. }
  454. if (p_alpha_to_one) {
  455. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ONE;
  456. }
  457. if (p_linear) {
  458. // Used for copying to a linear buffer. In the mobile renderer we divide the contents of the linear buffer
  459. // to allow for a wider effective range.
  460. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;
  461. copy_to_fb.push_constant.luminance_multiplier = prefer_raster_effects ? 2.0 : 1.0;
  462. }
  463. // setup our uniforms
  464. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  465. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  466. CopyToFBMode mode;
  467. if (p_multiview) {
  468. mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;
  469. } else {
  470. mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;
  471. }
  472. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  473. ERR_FAIL_COND(shader.is_null());
  474. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, p_rect);
  475. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  476. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  477. if (p_secondary.is_valid()) {
  478. // TODO may need to do this differently when reading from depth buffer for multiview
  479. RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));
  480. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);
  481. }
  482. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  483. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  484. RD::get_singleton()->draw_list_draw(draw_list, true);
  485. RD::get_singleton()->draw_list_end();
  486. }
  487. void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {
  488. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the copy with the clustered renderer.");
  489. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  490. ERR_FAIL_NULL(uniform_set_cache);
  491. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  492. ERR_FAIL_NULL(material_storage);
  493. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  494. // setup our uniforms
  495. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  496. RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));
  497. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);
  498. ERR_FAIL_COND(shader.is_null());
  499. // Just copy it back (we use our blur raster shader here)..
  500. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  501. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[BLUR_MODE_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  502. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);
  503. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  504. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  505. RD::get_singleton()->draw_list_draw(draw_list, true);
  506. RD::get_singleton()->draw_list_end();
  507. }
  508. void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, bool p_8bit_dst) {
  509. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
  510. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  511. ERR_FAIL_NULL(uniform_set_cache);
  512. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  513. ERR_FAIL_NULL(material_storage);
  514. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  515. copy.push_constant.section[0] = p_region.position.x;
  516. copy.push_constant.section[1] = p_region.position.y;
  517. copy.push_constant.section[2] = p_region.size.width;
  518. copy.push_constant.section[3] = p_region.size.height;
  519. // setup our uniforms
  520. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  521. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  522. RD::Uniform u_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_texture);
  523. CopyMode mode = p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY;
  524. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  525. ERR_FAIL_COND(shader.is_null());
  526. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  527. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  528. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  529. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_texture), 3);
  530. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  531. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  532. RD::get_singleton()->compute_list_end();
  533. }
  534. void CopyEffects::gaussian_blur_raster(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_region, const Size2i &p_size) {
  535. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian blur with the clustered renderer.");
  536. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  537. ERR_FAIL_NULL(uniform_set_cache);
  538. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  539. ERR_FAIL_NULL(material_storage);
  540. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  541. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  542. BlurRasterMode blur_mode = BLUR_MODE_GAUSSIAN_BLUR;
  543. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  544. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  545. // setup our uniforms
  546. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  547. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  548. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  549. ERR_FAIL_COND(shader.is_null());
  550. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  551. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
  552. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  553. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  554. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  555. RD::get_singleton()->draw_list_draw(draw_list, true);
  556. RD::get_singleton()->draw_list_end();
  557. }
  558. void CopyEffects::gaussian_glow(RID p_source_rd_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_high_quality, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {
  559. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
  560. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  561. ERR_FAIL_NULL(uniform_set_cache);
  562. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  563. ERR_FAIL_NULL(material_storage);
  564. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  565. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  566. uint32_t base_flags = 0;
  567. copy.push_constant.section[2] = p_size.x;
  568. copy.push_constant.section[3] = p_size.y;
  569. copy.push_constant.glow_strength = p_strength;
  570. copy.push_constant.glow_bloom = p_bloom;
  571. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  572. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  573. copy.push_constant.glow_exposure = p_exposure;
  574. copy.push_constant.glow_white = 0; //actually unused
  575. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  576. copy.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
  577. // setup our uniforms
  578. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  579. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  580. RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);
  581. RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);
  582. ERR_FAIL_COND(shader.is_null());
  583. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  584. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  585. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  586. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);
  587. if (p_auto_exposure.is_valid() && p_first_pass) {
  588. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  589. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  590. }
  591. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  592. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  593. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  594. RD::get_singleton()->compute_list_end();
  595. }
  596. void CopyEffects::gaussian_glow_raster(RID p_source_rd_texture, RID p_half_texture, RID p_dest_texture, float p_luminance_multiplier, const Size2i &p_size, float p_strength, bool p_high_quality, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {
  597. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
  598. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  599. ERR_FAIL_NULL(uniform_set_cache);
  600. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  601. ERR_FAIL_NULL(material_storage);
  602. RID half_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_half_texture);
  603. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  604. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  605. BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  606. uint32_t base_flags = 0;
  607. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  608. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  609. blur_raster.push_constant.glow_strength = p_strength;
  610. blur_raster.push_constant.glow_bloom = p_bloom;
  611. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  612. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  613. blur_raster.push_constant.glow_exposure = p_exposure;
  614. blur_raster.push_constant.glow_white = 0; //actually unused
  615. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  616. blur_raster.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
  617. blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
  618. // setup our uniforms
  619. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  620. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  621. RD::Uniform u_half_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_half_texture }));
  622. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  623. ERR_FAIL_COND(shader.is_null());
  624. //HORIZONTAL
  625. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(half_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  626. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(half_framebuffer)));
  627. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  628. if (p_auto_exposure.is_valid() && p_first_pass) {
  629. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  630. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  631. }
  632. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  633. blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  634. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  635. RD::get_singleton()->draw_list_draw(draw_list, true);
  636. RD::get_singleton()->draw_list_end();
  637. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  638. shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  639. ERR_FAIL_COND(shader.is_null());
  640. //VERTICAL
  641. draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  642. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
  643. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture), 0);
  644. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  645. blur_raster.push_constant.flags = base_flags;
  646. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  647. RD::get_singleton()->draw_list_draw(draw_list, true);
  648. RD::get_singleton()->draw_list_end();
  649. }
  650. void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  651. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the make_mipmap shader with the mobile renderer.");
  652. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  653. ERR_FAIL_NULL(uniform_set_cache);
  654. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  655. ERR_FAIL_NULL(material_storage);
  656. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  657. copy.push_constant.section[0] = 0;
  658. copy.push_constant.section[1] = 0;
  659. copy.push_constant.section[2] = p_size.width;
  660. copy.push_constant.section[3] = p_size.height;
  661. // setup our uniforms
  662. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  663. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  664. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  665. CopyMode mode = COPY_MODE_MIPMAP;
  666. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  667. ERR_FAIL_COND(shader.is_null());
  668. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  669. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  670. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  671. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  672. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  673. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  674. RD::get_singleton()->compute_list_end();
  675. }
  676. void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  677. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
  678. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  679. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  680. ERR_FAIL_NULL(uniform_set_cache);
  681. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  682. ERR_FAIL_NULL(material_storage);
  683. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  684. BlurRasterMode mode = BLUR_MIPMAP;
  685. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  686. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  687. // setup our uniforms
  688. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  689. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  690. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);
  691. ERR_FAIL_COND(shader.is_null());
  692. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  693. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
  694. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  695. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  696. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  697. RD::get_singleton()->draw_list_draw(draw_list, true);
  698. RD::get_singleton()->draw_list_end();
  699. }
  700. void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  701. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the set_color shader with the mobile renderer.");
  702. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  703. ERR_FAIL_NULL(uniform_set_cache);
  704. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  705. copy.push_constant.section[0] = 0;
  706. copy.push_constant.section[1] = 0;
  707. copy.push_constant.section[2] = p_region.size.width;
  708. copy.push_constant.section[3] = p_region.size.height;
  709. copy.push_constant.target[0] = p_region.position.x;
  710. copy.push_constant.target[1] = p_region.position.y;
  711. copy.push_constant.set_color[0] = p_color.r;
  712. copy.push_constant.set_color[1] = p_color.g;
  713. copy.push_constant.set_color[2] = p_color.b;
  714. copy.push_constant.set_color[3] = p_color.a;
  715. // setup our uniforms
  716. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  717. CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;
  718. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  719. ERR_FAIL_COND(shader.is_null());
  720. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  721. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  722. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  723. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  724. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  725. RD::get_singleton()->compute_list_end();
  726. }
  727. void CopyEffects::set_color_raster(RID p_dest_texture, const Color &p_color, const Rect2i &p_region) {
  728. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the set_color shader with the clustered renderer.");
  729. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  730. ERR_FAIL_NULL(uniform_set_cache);
  731. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  732. ERR_FAIL_NULL(material_storage);
  733. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  734. copy_to_fb.push_constant.set_color[0] = p_color.r;
  735. copy_to_fb.push_constant.set_color[1] = p_color.g;
  736. copy_to_fb.push_constant.set_color[2] = p_color.b;
  737. copy_to_fb.push_constant.set_color[3] = p_color.a;
  738. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  739. CopyToFBMode mode = COPY_TO_FB_SET_COLOR;
  740. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  741. ERR_FAIL_COND(shader.is_null());
  742. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, p_region);
  743. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
  744. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  745. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  746. RD::get_singleton()->draw_list_draw(draw_list, true);
  747. RD::get_singleton()->draw_list_end();
  748. }
  749. void CopyEffects::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {
  750. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  751. ERR_FAIL_NULL(uniform_set_cache);
  752. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  753. ERR_FAIL_NULL(material_storage);
  754. CopyToDPPushConstant push_constant;
  755. push_constant.screen_rect[0] = p_rect.position.x;
  756. push_constant.screen_rect[1] = p_rect.position.y;
  757. push_constant.screen_rect[2] = p_rect.size.width;
  758. push_constant.screen_rect[3] = p_rect.size.height;
  759. push_constant.z_far = p_z_far;
  760. push_constant.z_near = p_z_near;
  761. push_constant.texel_size[0] = 1.0f / p_dst_size.x;
  762. push_constant.texel_size[1] = 1.0f / p_dst_size.y;
  763. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  764. // setup our uniforms
  765. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  766. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  767. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  768. ERR_FAIL_COND(shader.is_null());
  769. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ);
  770. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_dp.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  771. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  772. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  773. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  774. RD::get_singleton()->draw_list_draw(draw_list, true);
  775. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_TRANSFER);
  776. }
  777. void CopyEffects::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  778. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
  779. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  780. ERR_FAIL_NULL(uniform_set_cache);
  781. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  782. ERR_FAIL_NULL(material_storage);
  783. cubemap_downsampler.push_constant.face_size = p_size.x;
  784. cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
  785. // setup our uniforms
  786. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  787. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  788. RD::Uniform u_dest_cubemap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_cubemap }));
  789. RID shader = cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  790. ERR_FAIL_COND(shader.is_null());
  791. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  792. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
  793. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  794. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_cubemap), 1);
  795. int x_groups = (p_size.x - 1) / 8 + 1;
  796. int y_groups = (p_size.y - 1) / 8 + 1;
  797. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  798. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  799. RD::get_singleton()->compute_list_end();
  800. }
  801. void CopyEffects::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
  802. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
  803. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
  804. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  805. ERR_FAIL_NULL(uniform_set_cache);
  806. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  807. ERR_FAIL_NULL(material_storage);
  808. cubemap_downsampler.push_constant.face_size = p_size.x;
  809. cubemap_downsampler.push_constant.face_id = p_face_id;
  810. // setup our uniforms
  811. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  812. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  813. RID shader = cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  814. ERR_FAIL_COND(shader.is_null());
  815. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  816. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cubemap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  817. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  818. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  819. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  820. RD::get_singleton()->draw_list_draw(draw_list, true);
  821. RD::get_singleton()->draw_list_end();
  822. }
  823. void CopyEffects::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  824. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
  825. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  826. ERR_FAIL_NULL(uniform_set_cache);
  827. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  828. ERR_FAIL_NULL(material_storage);
  829. Vector<RD::Uniform> uniforms;
  830. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  831. RD::Uniform u;
  832. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  833. u.binding = i;
  834. u.append_id(p_dest_cubemap[i]);
  835. uniforms.push_back(u);
  836. }
  837. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  838. RD::get_singleton()->free(filter.image_uniform_set);
  839. }
  840. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  841. // setup our uniforms
  842. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  843. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  844. int mode = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  845. mode = filter.use_high_quality ? mode : mode + 1;
  846. RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);
  847. ERR_FAIL_COND(shader.is_null());
  848. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  849. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode]);
  850. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  851. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  852. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  853. int x_groups = p_use_array ? 1792 : 342; // (128 * 128 * 7) / 64 : (128*128 + 64*64 + 32*32 + 16*16 + 8*8 + 4*4 + 2*2) / 64
  854. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  855. RD::get_singleton()->compute_list_end();
  856. }
  857. void CopyEffects::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
  858. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
  859. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
  860. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  861. ERR_FAIL_NULL(uniform_set_cache);
  862. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  863. ERR_FAIL_NULL(material_storage);
  864. // TODO implement!
  865. CubemapFilterRasterPushConstant push_constant;
  866. push_constant.mip_level = p_mip_level;
  867. push_constant.face_id = p_face_id;
  868. // setup our uniforms
  869. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  870. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  871. CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
  872. RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);
  873. ERR_FAIL_COND(shader.is_null());
  874. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  875. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, filter.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  876. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  877. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  878. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  879. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
  880. RD::get_singleton()->draw_list_draw(draw_list, true);
  881. RD::get_singleton()->draw_list_end();
  882. }
  883. void CopyEffects::cubemap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  884. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
  885. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  886. ERR_FAIL_NULL(uniform_set_cache);
  887. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  888. ERR_FAIL_NULL(material_storage);
  889. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  890. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  891. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  892. roughness.push_constant.sample_count = p_sample_count;
  893. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  894. roughness.push_constant.face_size = p_size;
  895. // setup our uniforms
  896. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  897. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));
  898. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));
  899. RID shader = roughness.compute_shader.version_get_shader(roughness.shader_version, 0);
  900. ERR_FAIL_COND(shader.is_null());
  901. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  902. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
  903. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  904. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);
  905. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  906. int x_groups = (p_size - 1) / 8 + 1;
  907. int y_groups = (p_size - 1) / 8 + 1;
  908. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  909. RD::get_singleton()->compute_list_end();
  910. }
  911. void CopyEffects::cubemap_roughness_raster(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  912. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
  913. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
  914. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  915. ERR_FAIL_NULL(uniform_set_cache);
  916. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  917. ERR_FAIL_NULL(material_storage);
  918. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  919. roughness.push_constant.face_id = p_face_id;
  920. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  921. roughness.push_constant.sample_count = p_sample_count;
  922. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  923. roughness.push_constant.face_size = p_size;
  924. // Setup our uniforms.
  925. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  926. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  927. RID shader = roughness.raster_shader.version_get_shader(roughness.shader_version, 0);
  928. ERR_FAIL_COND(shader.is_null());
  929. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  930. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  931. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  932. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  933. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  934. RD::get_singleton()->draw_list_draw(draw_list, true);
  935. RD::get_singleton()->draw_list_end();
  936. }
  937. void CopyEffects::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection, uint32_t p_view_count) {
  938. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  939. ERR_FAIL_NULL(uniform_set_cache);
  940. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  941. ERR_FAIL_NULL(material_storage);
  942. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  943. RD::get_singleton()->draw_command_begin_label("Merge specular");
  944. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, Vector<Color>());
  945. int mode;
  946. if (p_reflection.is_valid()) {
  947. if (p_base.is_valid()) {
  948. mode = SPECULAR_MERGE_SSR;
  949. } else {
  950. mode = SPECULAR_MERGE_ADDITIVE_SSR;
  951. }
  952. } else {
  953. if (p_base.is_valid()) {
  954. mode = SPECULAR_MERGE_ADD;
  955. } else {
  956. mode = SPECULAR_MERGE_ADDITIVE_ADD;
  957. }
  958. }
  959. if (p_view_count > 1) {
  960. mode += SPECULAR_MERGE_ADD_MULTIVIEW;
  961. }
  962. RID shader = specular_merge.shader.version_get_shader(specular_merge.shader_version, mode);
  963. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  964. if (p_base.is_valid()) {
  965. RD::Uniform u_base(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_base }));
  966. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_base), 2);
  967. }
  968. RD::Uniform u_specular(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_specular }));
  969. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_specular), 0);
  970. if (p_reflection.is_valid()) {
  971. RD::Uniform u_reflection(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_reflection }));
  972. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_reflection), 1);
  973. }
  974. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  975. RD::get_singleton()->draw_list_draw(draw_list, true);
  976. RD::get_singleton()->draw_list_end();
  977. RD::get_singleton()->draw_command_end_label();
  978. }