2
0

copy_effects.cpp 68 KB

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