copy_effects.cpp 62 KB

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