copy_effects.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 MULTIVIEW\n"); // COPY_TO_FB_MULTIVIEW
  96. copy_modes.push_back("\n#define 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) {
  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. // setup our uniforms
  462. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  463. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  464. CopyToFBMode mode;
  465. if (p_multiview) {
  466. mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;
  467. } else {
  468. mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;
  469. }
  470. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  471. ERR_FAIL_COND(shader.is_null());
  472. 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);
  473. 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)));
  474. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  475. if (p_secondary.is_valid()) {
  476. // TODO may need to do this differently when reading from depth buffer for multiview
  477. RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));
  478. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);
  479. }
  480. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  481. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  482. RD::get_singleton()->draw_list_draw(draw_list, true);
  483. RD::get_singleton()->draw_list_end();
  484. }
  485. void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {
  486. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the copy with the clustered renderer.");
  487. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  488. ERR_FAIL_NULL(uniform_set_cache);
  489. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  490. ERR_FAIL_NULL(material_storage);
  491. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  492. // setup our uniforms
  493. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  494. RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));
  495. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);
  496. ERR_FAIL_COND(shader.is_null());
  497. // Just copy it back (we use our blur raster shader here)..
  498. 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);
  499. 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)));
  500. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);
  501. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  502. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  503. RD::get_singleton()->draw_list_draw(draw_list, true);
  504. RD::get_singleton()->draw_list_end();
  505. }
  506. void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, const Size2i &p_size, bool p_8bit_dst) {
  507. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
  508. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  509. ERR_FAIL_NULL(uniform_set_cache);
  510. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  511. ERR_FAIL_NULL(material_storage);
  512. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  513. copy.push_constant.section[0] = p_region.position.x;
  514. copy.push_constant.section[1] = p_region.position.y;
  515. copy.push_constant.target[0] = p_region.position.x;
  516. copy.push_constant.target[1] = p_region.position.y;
  517. copy.push_constant.section[2] = p_size.width;
  518. copy.push_constant.section[3] = p_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_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  554. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  555. RD::get_singleton()->draw_list_end();
  556. }
  557. 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) {
  558. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
  559. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  560. ERR_FAIL_NULL(uniform_set_cache);
  561. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  562. ERR_FAIL_NULL(material_storage);
  563. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  564. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  565. uint32_t base_flags = 0;
  566. copy.push_constant.section[2] = p_size.x;
  567. copy.push_constant.section[3] = p_size.y;
  568. copy.push_constant.glow_strength = p_strength;
  569. copy.push_constant.glow_bloom = p_bloom;
  570. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  571. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  572. copy.push_constant.glow_exposure = p_exposure;
  573. copy.push_constant.glow_white = 0; //actually unused
  574. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  575. copy.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
  576. // setup our uniforms
  577. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  578. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  579. RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);
  580. RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);
  581. ERR_FAIL_COND(shader.is_null());
  582. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  583. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  584. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  585. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);
  586. if (p_auto_exposure.is_valid() && p_first_pass) {
  587. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  588. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  589. }
  590. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0);
  591. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  592. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  593. RD::get_singleton()->compute_list_end();
  594. }
  595. 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) {
  596. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
  597. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  598. ERR_FAIL_NULL(uniform_set_cache);
  599. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  600. ERR_FAIL_NULL(material_storage);
  601. RID half_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_half_texture);
  602. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  603. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  604. BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  605. uint32_t base_flags = 0;
  606. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  607. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  608. blur_raster.push_constant.glow_strength = p_strength;
  609. blur_raster.push_constant.glow_bloom = p_bloom;
  610. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  611. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  612. blur_raster.push_constant.glow_exposure = p_exposure;
  613. blur_raster.push_constant.glow_white = 0; //actually unused
  614. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  615. blur_raster.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
  616. blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
  617. // setup our uniforms
  618. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  619. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  620. RD::Uniform u_half_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_half_texture }));
  621. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  622. ERR_FAIL_COND(shader.is_null());
  623. //HORIZONTAL
  624. 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);
  625. 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)));
  626. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  627. if (p_auto_exposure.is_valid() && p_first_pass) {
  628. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  629. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  630. }
  631. blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  632. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  633. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  634. RD::get_singleton()->draw_list_end();
  635. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  636. shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  637. ERR_FAIL_COND(shader.is_null());
  638. //VERTICAL
  639. 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);
  640. 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)));
  641. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture), 0);
  642. blur_raster.push_constant.flags = base_flags;
  643. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  644. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  645. RD::get_singleton()->draw_list_end();
  646. }
  647. void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  648. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the make_mipmap shader with the mobile renderer.");
  649. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  650. ERR_FAIL_NULL(uniform_set_cache);
  651. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  652. ERR_FAIL_NULL(material_storage);
  653. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  654. copy.push_constant.section[0] = 0;
  655. copy.push_constant.section[1] = 0;
  656. copy.push_constant.section[2] = p_size.width;
  657. copy.push_constant.section[3] = p_size.height;
  658. // setup our uniforms
  659. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  660. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  661. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  662. CopyMode mode = COPY_MODE_MIPMAP;
  663. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  664. ERR_FAIL_COND(shader.is_null());
  665. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  666. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  667. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  668. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  669. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  670. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  671. RD::get_singleton()->compute_list_end();
  672. }
  673. void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  674. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
  675. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  676. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  677. ERR_FAIL_NULL(uniform_set_cache);
  678. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  679. ERR_FAIL_NULL(material_storage);
  680. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  681. BlurRasterMode mode = BLUR_MIPMAP;
  682. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  683. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  684. // setup our uniforms
  685. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  686. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  687. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);
  688. ERR_FAIL_COND(shader.is_null());
  689. 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);
  690. 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)));
  691. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  692. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  693. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  694. RD::get_singleton()->draw_list_draw(draw_list, true);
  695. RD::get_singleton()->draw_list_end();
  696. }
  697. void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  698. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the set_color shader with the mobile renderer.");
  699. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  700. ERR_FAIL_NULL(uniform_set_cache);
  701. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  702. copy.push_constant.section[0] = 0;
  703. copy.push_constant.section[1] = 0;
  704. copy.push_constant.section[2] = p_region.size.width;
  705. copy.push_constant.section[3] = p_region.size.height;
  706. copy.push_constant.target[0] = p_region.position.x;
  707. copy.push_constant.target[1] = p_region.position.y;
  708. copy.push_constant.set_color[0] = p_color.r;
  709. copy.push_constant.set_color[1] = p_color.g;
  710. copy.push_constant.set_color[2] = p_color.b;
  711. copy.push_constant.set_color[3] = p_color.a;
  712. // setup our uniforms
  713. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  714. CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;
  715. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  716. ERR_FAIL_COND(shader.is_null());
  717. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  718. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  719. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  720. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  721. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  722. RD::get_singleton()->compute_list_end();
  723. }
  724. void CopyEffects::set_color_raster(RID p_dest_texture, const Color &p_color, const Rect2i &p_region) {
  725. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the set_color shader with the clustered renderer.");
  726. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  727. ERR_FAIL_NULL(uniform_set_cache);
  728. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  729. ERR_FAIL_NULL(material_storage);
  730. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  731. copy_to_fb.push_constant.set_color[0] = p_color.r;
  732. copy_to_fb.push_constant.set_color[1] = p_color.g;
  733. copy_to_fb.push_constant.set_color[2] = p_color.b;
  734. copy_to_fb.push_constant.set_color[3] = p_color.a;
  735. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  736. CopyToFBMode mode = COPY_TO_FB_SET_COLOR;
  737. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  738. ERR_FAIL_COND(shader.is_null());
  739. 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);
  740. 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)));
  741. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  742. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  743. RD::get_singleton()->draw_list_draw(draw_list, true);
  744. RD::get_singleton()->draw_list_end();
  745. }
  746. 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, BitField<RD::BarrierMask> p_post_barrier) {
  747. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  748. ERR_FAIL_NULL(uniform_set_cache);
  749. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  750. ERR_FAIL_NULL(material_storage);
  751. CopyToDPPushConstant push_constant;
  752. push_constant.screen_rect[0] = p_rect.position.x;
  753. push_constant.screen_rect[1] = p_rect.position.y;
  754. push_constant.screen_rect[2] = p_rect.size.width;
  755. push_constant.screen_rect[3] = p_rect.size.height;
  756. push_constant.z_far = p_z_far;
  757. push_constant.z_near = p_z_near;
  758. push_constant.texel_size[0] = 1.0f / p_dst_size.x;
  759. push_constant.texel_size[1] = 1.0f / p_dst_size.y;
  760. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  761. // setup our uniforms
  762. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  763. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  764. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  765. ERR_FAIL_COND(shader.is_null());
  766. 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);
  767. 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)));
  768. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  769. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  770. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  771. RD::get_singleton()->draw_list_draw(draw_list, true);
  772. RD::get_singleton()->draw_list_end(p_post_barrier);
  773. }
  774. void CopyEffects::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  775. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
  776. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  777. ERR_FAIL_NULL(uniform_set_cache);
  778. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  779. ERR_FAIL_NULL(material_storage);
  780. cubemap_downsampler.push_constant.face_size = p_size.x;
  781. cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
  782. // setup our uniforms
  783. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  784. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  785. RD::Uniform u_dest_cubemap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_cubemap }));
  786. RID shader = cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  787. ERR_FAIL_COND(shader.is_null());
  788. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  789. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
  790. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  791. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_cubemap), 1);
  792. int x_groups = (p_size.x - 1) / 8 + 1;
  793. int y_groups = (p_size.y - 1) / 8 + 1;
  794. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  795. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  796. RD::get_singleton()->compute_list_end();
  797. }
  798. void CopyEffects::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
  799. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
  800. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
  801. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  802. ERR_FAIL_NULL(uniform_set_cache);
  803. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  804. ERR_FAIL_NULL(material_storage);
  805. cubemap_downsampler.push_constant.face_size = p_size.x;
  806. cubemap_downsampler.push_constant.face_id = p_face_id;
  807. // setup our uniforms
  808. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  809. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  810. RID shader = cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  811. ERR_FAIL_COND(shader.is_null());
  812. 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);
  813. 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)));
  814. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  815. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  816. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  817. RD::get_singleton()->draw_list_end();
  818. }
  819. void CopyEffects::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  820. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
  821. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  822. ERR_FAIL_NULL(uniform_set_cache);
  823. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  824. ERR_FAIL_NULL(material_storage);
  825. Vector<RD::Uniform> uniforms;
  826. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  827. RD::Uniform u;
  828. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  829. u.binding = i;
  830. u.append_id(p_dest_cubemap[i]);
  831. uniforms.push_back(u);
  832. }
  833. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  834. RD::get_singleton()->free(filter.image_uniform_set);
  835. }
  836. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  837. // setup our uniforms
  838. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  839. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  840. int mode = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  841. mode = filter.use_high_quality ? mode : mode + 1;
  842. RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);
  843. ERR_FAIL_COND(shader.is_null());
  844. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  845. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode]);
  846. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  847. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  848. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  849. 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
  850. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  851. RD::get_singleton()->compute_list_end();
  852. }
  853. void CopyEffects::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
  854. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
  855. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
  856. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  857. ERR_FAIL_NULL(uniform_set_cache);
  858. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  859. ERR_FAIL_NULL(material_storage);
  860. // TODO implement!
  861. CubemapFilterRasterPushConstant push_constant;
  862. push_constant.mip_level = p_mip_level;
  863. push_constant.face_id = p_face_id;
  864. // setup our uniforms
  865. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  866. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  867. CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
  868. RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);
  869. ERR_FAIL_COND(shader.is_null());
  870. 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);
  871. 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)));
  872. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  873. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  874. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
  875. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  876. RD::get_singleton()->draw_list_end();
  877. }
  878. 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) {
  879. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
  880. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  881. ERR_FAIL_NULL(uniform_set_cache);
  882. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  883. ERR_FAIL_NULL(material_storage);
  884. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  885. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  886. // Remap to perceptual-roughness^2 to create more detail in lower mips and match the mapping of cubemap_filter.
  887. roughness.push_constant.roughness = p_roughness * p_roughness;
  888. roughness.push_constant.sample_count = p_sample_count;
  889. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  890. roughness.push_constant.face_size = p_size;
  891. // setup our uniforms
  892. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  893. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));
  894. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));
  895. RID shader = roughness.compute_shader.version_get_shader(roughness.shader_version, 0);
  896. ERR_FAIL_COND(shader.is_null());
  897. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  898. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
  899. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  900. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);
  901. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  902. int x_groups = (p_size - 1) / 8 + 1;
  903. int y_groups = (p_size - 1) / 8 + 1;
  904. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  905. RD::get_singleton()->compute_list_end();
  906. }
  907. 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) {
  908. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
  909. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
  910. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  911. ERR_FAIL_NULL(uniform_set_cache);
  912. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  913. ERR_FAIL_NULL(material_storage);
  914. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  915. roughness.push_constant.face_id = p_face_id;
  916. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  917. roughness.push_constant.sample_count = p_sample_count;
  918. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  919. roughness.push_constant.face_size = p_size;
  920. // Setup our uniforms.
  921. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  922. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  923. RID shader = roughness.raster_shader.version_get_shader(roughness.shader_version, 0);
  924. ERR_FAIL_COND(shader.is_null());
  925. 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);
  926. 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)));
  927. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  928. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  929. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  930. RD::get_singleton()->draw_list_end();
  931. }
  932. void CopyEffects::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection, uint32_t p_view_count) {
  933. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  934. ERR_FAIL_NULL(uniform_set_cache);
  935. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  936. ERR_FAIL_NULL(material_storage);
  937. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  938. RD::get_singleton()->draw_command_begin_label("Merge specular");
  939. 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>());
  940. int mode;
  941. if (p_reflection.is_valid()) {
  942. if (p_base.is_valid()) {
  943. mode = SPECULAR_MERGE_SSR;
  944. } else {
  945. mode = SPECULAR_MERGE_ADDITIVE_SSR;
  946. }
  947. } else {
  948. if (p_base.is_valid()) {
  949. mode = SPECULAR_MERGE_ADD;
  950. } else {
  951. mode = SPECULAR_MERGE_ADDITIVE_ADD;
  952. }
  953. }
  954. if (p_view_count > 1) {
  955. mode += SPECULAR_MERGE_ADD_MULTIVIEW;
  956. }
  957. RID shader = specular_merge.shader.version_get_shader(specular_merge.shader_version, mode);
  958. 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)));
  959. if (p_base.is_valid()) {
  960. RD::Uniform u_base(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_base }));
  961. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_base), 2);
  962. }
  963. RD::Uniform u_specular(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_specular }));
  964. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_specular), 0);
  965. if (p_reflection.is_valid()) {
  966. RD::Uniform u_reflection(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_reflection }));
  967. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_reflection), 1);
  968. }
  969. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  970. RD::get_singleton()->draw_list_end();
  971. RD::get_singleton()->draw_command_end_label();
  972. }