copy_effects.cpp 67 KB

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