copy_effects.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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(BitField<RasterEffects> p_raster_effects) {
  42. singleton = this;
  43. raster_effects = p_raster_effects;
  44. if (raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR)) {
  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_GLOW_GATHER\n"); // BLUR_MODE_GAUSSIAN_GLOW_GATHER
  50. blur_modes.push_back("\n#define MODE_GLOW_DOWNSAMPLE\n"); // BLUR_MODE_GAUSSIAN_GLOW_DOWNSAMPLE
  51. blur_modes.push_back("\n#define MODE_GLOW_UPSAMPLE\n"); // BLUR_MODE_GAUSSIAN_GLOW_UPSAMPLE
  52. blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
  53. blur_modes.push_back("\n#define MODE_SET_COLOR\n"); // BLUR_MODE_SET_COLOR
  54. blur_raster.shader.initialize(blur_modes);
  55. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  56. blur_raster.shader_version = blur_raster.shader.version_create();
  57. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  58. 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);
  59. }
  60. RD::SamplerState sampler_state;
  61. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  62. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  63. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER;
  64. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER;
  65. sampler_state.border_color = RD::SAMPLER_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
  66. blur_raster.glow_sampler = RD::get_singleton()->sampler_create(sampler_state);
  67. } else {
  68. // not used in clustered
  69. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  70. blur_raster.pipelines[i].clear();
  71. }
  72. }
  73. {
  74. Vector<String> copy_modes;
  75. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  76. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  77. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");
  78. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  79. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  80. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  81. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  82. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  83. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  84. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  85. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  86. copy_modes.push_back("\n#define MODE_OCTMAP_TO_PANORAMA\n");
  87. copy_modes.push_back("\n#define MODE_OCTMAP_ARRAY_TO_PANORAMA\n");
  88. copy.shader.initialize(copy_modes);
  89. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  90. copy.shader_version = copy.shader.version_create();
  91. for (int i = 0; i < COPY_MODE_MAX; i++) {
  92. if (copy.shader.is_variant_enabled(i)) {
  93. copy.pipelines[i].create_compute_pipeline(copy.shader.version_get_shader(copy.shader_version, i));
  94. }
  95. }
  96. }
  97. {
  98. Vector<String> copy_modes;
  99. copy_modes.push_back("\n"); // COPY_TO_FB_COPY
  100. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n"); // COPY_TO_FB_COPY_PANORAMA_TO_DP
  101. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_COPY2
  102. copy_modes.push_back("\n#define MODE_SET_COLOR\n"); // COPY_TO_FB_SET_COLOR
  103. copy_modes.push_back("\n#define USE_MULTIVIEW\n"); // COPY_TO_FB_MULTIVIEW
  104. copy_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_MULTIVIEW_WITH_DEPTH
  105. copy_to_fb.shader.initialize(copy_modes);
  106. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  107. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW, false);
  108. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW_WITH_DEPTH, false);
  109. }
  110. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  111. //use additive
  112. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  113. if (copy_to_fb.shader.is_variant_enabled(i)) {
  114. 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);
  115. } else {
  116. copy_to_fb.pipelines[i].clear();
  117. }
  118. }
  119. }
  120. {
  121. // Initialize copier
  122. Vector<String> copy_modes;
  123. copy_modes.push_back("\n");
  124. cube_to_dp.shader.initialize(copy_modes);
  125. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  126. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  127. RD::PipelineDepthStencilState dss;
  128. dss.enable_depth_test = true;
  129. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  130. dss.enable_depth_write = true;
  131. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  132. }
  133. {
  134. // Initialize cubemap to octmap copier.
  135. cube_to_octmap.shader.initialize({ "" });
  136. cube_to_octmap.shader_version = cube_to_octmap.shader.version_create();
  137. RID shader = cube_to_octmap.shader.version_get_shader(cube_to_octmap.shader_version, 0);
  138. cube_to_octmap.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled());
  139. }
  140. {
  141. // Initialize octmap downsampler.
  142. if (raster_effects.has_flag(RASTER_EFFECT_OCTMAP)) {
  143. octmap_downsampler.raster_shader.initialize({ "" });
  144. octmap_downsampler.shader_version = octmap_downsampler.raster_shader.version_create();
  145. octmap_downsampler.raster_pipeline.setup(octmap_downsampler.raster_shader.version_get_shader(octmap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  146. } else {
  147. Vector<String> downsampler_modes;
  148. for (int i = 0; i < DOWNSAMPLER_MODE_COMPUTE_MAX; i++) {
  149. String mode;
  150. if (i & DOWNSAMPLER_MODE_FLAG_RGB10_A2) {
  151. mode += "\n#define OCTMAP_FORMAT rgb10_a2\n";
  152. } else {
  153. mode += "\n#define OCTMAP_FORMAT rgba16f\n";
  154. }
  155. downsampler_modes.push_back(mode);
  156. }
  157. octmap_downsampler.compute_shader.initialize(downsampler_modes);
  158. octmap_downsampler.shader_version = octmap_downsampler.compute_shader.version_create();
  159. for (int i = 0; i < DOWNSAMPLER_MODE_COMPUTE_MAX; i++) {
  160. octmap_downsampler.compute_pipelines[i].create_compute_pipeline(octmap_downsampler.compute_shader.version_get_shader(octmap_downsampler.shader_version, i));
  161. }
  162. }
  163. }
  164. {
  165. // Initialize cubemap filter
  166. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  167. if (filter.use_high_quality) {
  168. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  169. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  170. } else {
  171. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  172. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  173. }
  174. Vector<String> cubemap_filter_modes;
  175. if (raster_effects.has_flag(RASTER_EFFECT_OCTMAP)) {
  176. for (int i = 0; i < FILTER_MODE_RASTER_MAX; i++) {
  177. String mode;
  178. if (i & FILTER_MODE_FLAG_HIGH_QUALITY) {
  179. mode += "\n#define USE_HIGH_QUALITY\n";
  180. } else {
  181. mode += "\n#define USE_LOW_QUALITY\n";
  182. }
  183. cubemap_filter_modes.push_back(mode);
  184. }
  185. filter.raster_shader.initialize(cubemap_filter_modes);
  186. filter.shader_version = filter.raster_shader.version_create();
  187. for (int i = 0; i < FILTER_MODE_RASTER_MAX; i++) {
  188. 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);
  189. }
  190. Vector<RD::Uniform> uniforms;
  191. {
  192. RD::Uniform u;
  193. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  194. u.binding = 0;
  195. u.append_id(filter.coefficient_buffer);
  196. uniforms.push_back(u);
  197. }
  198. 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);
  199. } else {
  200. for (int i = 0; i < FILTER_MODE_COMPUTE_MAX; i++) {
  201. String mode;
  202. if (i & FILTER_MODE_FLAG_HIGH_QUALITY) {
  203. mode += "\n#define USE_HIGH_QUALITY\n";
  204. } else {
  205. mode += "\n#define USE_LOW_QUALITY\n";
  206. }
  207. if (i & FILTER_MODE_FLAG_ARRAY) {
  208. mode += "\n#define USE_TEXTURE_ARRAY\n";
  209. }
  210. if (i & FILTER_MODE_FLAG_RGB10_A2) {
  211. mode += "\n#define OCTMAP_FORMAT rgb10_a2\n";
  212. } else {
  213. mode += "\n#define OCTMAP_FORMAT rgba16f\n";
  214. }
  215. cubemap_filter_modes.push_back(mode);
  216. }
  217. filter.compute_shader.initialize(cubemap_filter_modes);
  218. filter.shader_version = filter.compute_shader.version_create();
  219. for (int i = 0; i < FILTER_MODE_COMPUTE_MAX; i++) {
  220. filter.compute_pipelines[i].create_compute_pipeline(filter.compute_shader.version_get_shader(filter.shader_version, i));
  221. }
  222. Vector<RD::Uniform> uniforms;
  223. {
  224. RD::Uniform u;
  225. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  226. u.binding = 0;
  227. u.append_id(filter.coefficient_buffer);
  228. uniforms.push_back(u);
  229. }
  230. 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);
  231. }
  232. }
  233. {
  234. // Initialize roughness
  235. Vector<String> cubemap_roughness_modes;
  236. if (raster_effects.has_flag(RASTER_EFFECT_OCTMAP)) {
  237. cubemap_roughness_modes.push_back("");
  238. roughness.raster_shader.initialize(cubemap_roughness_modes);
  239. roughness.shader_version = roughness.raster_shader.version_create();
  240. 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);
  241. } else {
  242. cubemap_roughness_modes.push_back("\n#define OCTMAP_FORMAT rgba16f\n");
  243. cubemap_roughness_modes.push_back("\n#define OCTMAP_FORMAT rgb10_a2\n");
  244. roughness.compute_shader.initialize(cubemap_roughness_modes);
  245. roughness.shader_version = roughness.compute_shader.version_create();
  246. for (int i = 0; i < ROUGHNESS_MODE_MAX; i++) {
  247. roughness.compute_pipelines[i].create_compute_pipeline(roughness.compute_shader.version_get_shader(roughness.shader_version, i));
  248. }
  249. }
  250. }
  251. {
  252. Vector<String> specular_modes;
  253. specular_modes.push_back("\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD
  254. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR
  255. specular_modes.push_back("\n"); // SPECULAR_MERGE_ADDITIVE_ADD
  256. specular_modes.push_back("\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR
  257. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD_MULTIVIEW
  258. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR_MULTIVIEW
  259. specular_modes.push_back("\n#define USE_MULTIVIEW\n"); // SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW
  260. specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW
  261. specular_merge.shader.initialize(specular_modes);
  262. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  263. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADD_MULTIVIEW, false);
  264. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_SSR_MULTIVIEW, false);
  265. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW, false);
  266. specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW, false);
  267. }
  268. specular_merge.shader_version = specular_merge.shader.version_create();
  269. //use additive
  270. RD::PipelineColorBlendState::Attachment ba;
  271. ba.enable_blend = true;
  272. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  273. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  274. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
  275. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
  276. ba.color_blend_op = RD::BLEND_OP_ADD;
  277. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  278. RD::PipelineColorBlendState blend_additive;
  279. blend_additive.attachments.push_back(ba);
  280. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  281. if (specular_merge.shader.is_variant_enabled(i)) {
  282. RD::PipelineColorBlendState blend_state;
  283. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR || i == SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW || i == SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW) {
  284. blend_state = blend_additive;
  285. } else {
  286. blend_state = RD::PipelineColorBlendState::create_disabled();
  287. }
  288. 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);
  289. }
  290. }
  291. }
  292. }
  293. CopyEffects::~CopyEffects() {
  294. for (int i = 0; i < COPY_MODE_MAX; i++) {
  295. copy.pipelines[i].free();
  296. }
  297. if (raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR)) {
  298. blur_raster.shader.version_free(blur_raster.shader_version);
  299. RD::get_singleton()->free_rid(blur_raster.glow_sampler);
  300. }
  301. if (raster_effects.has_flag(RASTER_EFFECT_OCTMAP)) {
  302. octmap_downsampler.raster_shader.version_free(octmap_downsampler.shader_version);
  303. filter.raster_shader.version_free(filter.shader_version);
  304. roughness.raster_shader.version_free(roughness.shader_version);
  305. } else {
  306. // PipelineDeferredRD always needs to be freed before its corresponding shader since the pipeline may not have finished compiling before the shader is freed. This
  307. // ensures that we wait on the pipeline compilation before we free it.
  308. for (int i = 0; i < DOWNSAMPLER_MODE_COMPUTE_MAX; i++) {
  309. octmap_downsampler.compute_pipelines[i].free();
  310. }
  311. octmap_downsampler.compute_shader.version_free(octmap_downsampler.shader_version);
  312. for (int i = 0; i < FILTER_MODE_COMPUTE_MAX; i++) {
  313. filter.compute_pipelines[i].free();
  314. }
  315. filter.compute_shader.version_free(filter.shader_version);
  316. for (int i = 0; i < ROUGHNESS_MODE_MAX; i++) {
  317. roughness.compute_pipelines[i].free();
  318. }
  319. roughness.compute_shader.version_free(roughness.shader_version);
  320. }
  321. copy.shader.version_free(copy.shader_version);
  322. specular_merge.shader.version_free(specular_merge.shader_version);
  323. RD::get_singleton()->free_rid(filter.coefficient_buffer);
  324. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  325. RD::get_singleton()->free_rid(filter.image_uniform_set);
  326. }
  327. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  328. RD::get_singleton()->free_rid(filter.uniform_set);
  329. }
  330. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  331. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  332. cube_to_octmap.shader.version_free(cube_to_octmap.shader_version);
  333. singleton = nullptr;
  334. }
  335. 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, bool p_sanitize_inf_nan) {
  336. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  337. ERR_FAIL_NULL(uniform_set_cache);
  338. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  339. ERR_FAIL_NULL(material_storage);
  340. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  341. if (p_flip_y) {
  342. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  343. }
  344. if (p_force_luminance) {
  345. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  346. }
  347. if (p_all_source) {
  348. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  349. }
  350. if (p_alpha_to_one) {
  351. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  352. }
  353. if (p_sanitize_inf_nan) {
  354. copy.push_constant.flags |= COPY_FLAG_SANITIZE_INF_NAN;
  355. }
  356. copy.push_constant.section[0] = p_rect.position.x;
  357. copy.push_constant.section[1] = p_rect.position.y;
  358. copy.push_constant.section[2] = p_rect.size.width;
  359. copy.push_constant.section[3] = p_rect.size.height;
  360. copy.push_constant.target[0] = p_rect.position.x;
  361. copy.push_constant.target[1] = p_rect.position.y;
  362. // setup our uniforms
  363. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  364. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  365. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  366. CopyMode mode = p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY;
  367. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  368. ERR_FAIL_COND(shader.is_null());
  369. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  370. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  371. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  372. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  373. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  374. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  375. RD::get_singleton()->compute_list_end();
  376. }
  377. void CopyEffects::copy_octmap_to_panorama(RID p_source_octmap, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array, const Size2 &p_source_octmap_border_size) {
  378. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  379. ERR_FAIL_NULL(uniform_set_cache);
  380. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  381. ERR_FAIL_NULL(material_storage);
  382. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  383. copy.push_constant.section[0] = 0;
  384. copy.push_constant.section[1] = 0;
  385. copy.push_constant.section[2] = p_panorama_size.width;
  386. copy.push_constant.section[3] = p_panorama_size.height;
  387. copy.push_constant.target[0] = 0;
  388. copy.push_constant.target[1] = 0;
  389. copy.push_constant.camera_z_far = p_lod;
  390. copy.push_constant.octmap_border_size[0] = p_source_octmap_border_size.x;
  391. copy.push_constant.octmap_border_size[1] = p_source_octmap_border_size.y;
  392. // TODO, if this is needed at the copy stage, then we need to pass in the multiplier.
  393. copy.push_constant.luminance_multiplier = raster_effects.has_flag(RASTER_EFFECT_COPY) ? 2.0 : 1.0;
  394. // setup our uniforms
  395. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  396. RD::Uniform u_source_octmap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_octmap }));
  397. RD::Uniform u_dest_panorama(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_panorama);
  398. CopyMode mode = p_is_array ? COPY_MODE_OCTMAP_ARRAY_TO_PANORAMA : COPY_MODE_OCTMAP_TO_PANORAMA;
  399. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  400. ERR_FAIL_COND(shader.is_null());
  401. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  402. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  403. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_octmap), 0);
  404. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_panorama), 3);
  405. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  406. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  407. RD::get_singleton()->compute_list_end();
  408. }
  409. void CopyEffects::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  410. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  411. ERR_FAIL_NULL(uniform_set_cache);
  412. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  413. ERR_FAIL_NULL(material_storage);
  414. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  415. if (p_flip_y) {
  416. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  417. }
  418. copy.push_constant.section[0] = 0;
  419. copy.push_constant.section[1] = 0;
  420. copy.push_constant.section[2] = p_rect.size.width;
  421. copy.push_constant.section[3] = p_rect.size.height;
  422. copy.push_constant.target[0] = p_rect.position.x;
  423. copy.push_constant.target[1] = p_rect.position.y;
  424. // setup our uniforms
  425. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  426. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  427. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  428. CopyMode mode = COPY_MODE_SIMPLY_COPY_DEPTH;
  429. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  430. ERR_FAIL_COND(shader.is_null());
  431. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  432. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  433. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  434. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  435. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  436. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  437. RD::get_singleton()->compute_list_end();
  438. }
  439. 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) {
  440. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  441. ERR_FAIL_NULL(uniform_set_cache);
  442. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  443. ERR_FAIL_NULL(material_storage);
  444. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  445. if (p_flip_y) {
  446. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  447. }
  448. copy.push_constant.section[0] = 0;
  449. copy.push_constant.section[1] = 0;
  450. copy.push_constant.section[2] = p_rect.size.width;
  451. copy.push_constant.section[3] = p_rect.size.height;
  452. copy.push_constant.target[0] = p_rect.position.x;
  453. copy.push_constant.target[1] = p_rect.position.y;
  454. copy.push_constant.camera_z_far = p_z_far;
  455. copy.push_constant.camera_z_near = p_z_near;
  456. // setup our uniforms
  457. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  458. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  459. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  460. CopyMode mode = COPY_MODE_LINEARIZE_DEPTH;
  461. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  462. ERR_FAIL_COND(shader.is_null());
  463. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  464. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  465. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  466. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  467. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  468. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  469. RD::get_singleton()->compute_list_end();
  470. }
  471. 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) {
  472. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  473. ERR_FAIL_NULL(uniform_set_cache);
  474. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  475. ERR_FAIL_NULL(material_storage);
  476. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  477. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SECTION;
  478. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  479. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  480. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  481. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  482. if (p_flip_y) {
  483. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
  484. }
  485. copy_to_fb.push_constant.luminance_multiplier = 1.0;
  486. // setup our uniforms
  487. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  488. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  489. CopyToFBMode mode = p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY;
  490. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  491. ERR_FAIL_COND(shader.is_null());
  492. RD::DrawListID draw_list = p_draw_list;
  493. 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)));
  494. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  495. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  496. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  497. RD::get_singleton()->draw_list_draw(draw_list, true);
  498. }
  499. void CopyEffects::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_alpha_to_zero, bool p_srgb, RID p_secondary, bool p_multiview, bool p_alpha_to_one, bool p_linear, bool p_normal, const Rect2 &p_src_rect, float p_linear_luminance_multiplier) {
  500. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  501. ERR_FAIL_NULL(uniform_set_cache);
  502. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  503. ERR_FAIL_NULL(material_storage);
  504. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  505. copy_to_fb.push_constant.luminance_multiplier = 1.0;
  506. if (p_flip_y) {
  507. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
  508. }
  509. if (p_force_luminance) {
  510. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FORCE_LUMINANCE;
  511. }
  512. if (p_alpha_to_zero) {
  513. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ZERO;
  514. }
  515. if (p_srgb) {
  516. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_SRGB;
  517. }
  518. if (p_alpha_to_one) {
  519. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ONE;
  520. }
  521. if (p_linear) {
  522. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;
  523. copy_to_fb.push_constant.luminance_multiplier = p_linear_luminance_multiplier;
  524. }
  525. if (p_normal) {
  526. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_NORMAL;
  527. }
  528. if (p_src_rect != Rect2()) {
  529. copy_to_fb.push_constant.section[0] = p_src_rect.position.x;
  530. copy_to_fb.push_constant.section[1] = p_src_rect.position.y;
  531. copy_to_fb.push_constant.section[2] = p_src_rect.size.x;
  532. copy_to_fb.push_constant.section[3] = p_src_rect.size.y;
  533. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SRC_SECTION;
  534. }
  535. // setup our uniforms
  536. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  537. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  538. CopyToFBMode mode;
  539. if (p_multiview) {
  540. mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;
  541. } else {
  542. mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;
  543. }
  544. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  545. ERR_FAIL_COND(shader.is_null());
  546. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_rect);
  547. 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)));
  548. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  549. if (p_secondary.is_valid()) {
  550. // TODO may need to do this differently when reading from depth buffer for multiview
  551. RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));
  552. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);
  553. }
  554. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  555. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  556. RD::get_singleton()->draw_list_draw(draw_list, true);
  557. RD::get_singleton()->draw_list_end();
  558. }
  559. void CopyEffects::copy_to_drawlist(RD::DrawListID p_draw_list, RD::FramebufferFormatID p_fb_format, RID p_source_rd_texture, bool p_linear, float p_linear_luminance_multiplier) {
  560. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  561. ERR_FAIL_NULL(uniform_set_cache);
  562. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  563. ERR_FAIL_NULL(material_storage);
  564. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  565. copy_to_fb.push_constant.luminance_multiplier = 1.0;
  566. if (p_linear) {
  567. copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;
  568. copy_to_fb.push_constant.luminance_multiplier = p_linear_luminance_multiplier;
  569. }
  570. // setup our uniforms
  571. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  572. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  573. // Multiview not supported here!
  574. CopyToFBMode mode = COPY_TO_FB_COPY;
  575. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  576. ERR_FAIL_COND(shader.is_null());
  577. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_fb_format));
  578. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  579. RD::get_singleton()->draw_list_bind_index_array(p_draw_list, material_storage->get_quad_index_array());
  580. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  581. RD::get_singleton()->draw_list_draw(p_draw_list, true);
  582. }
  583. void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {
  584. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_COPY), "Can't use the raster version of the copy.");
  585. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  586. ERR_FAIL_NULL(uniform_set_cache);
  587. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  588. ERR_FAIL_NULL(material_storage);
  589. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  590. // setup our uniforms
  591. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  592. RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));
  593. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);
  594. ERR_FAIL_COND(shader.is_null());
  595. // Just copy it back (we use our blur raster shader here)..
  596. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
  597. 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)));
  598. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);
  599. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  600. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  601. RD::get_singleton()->draw_list_end();
  602. }
  603. void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, const Size2i &p_size, bool p_8bit_dst) {
  604. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR), "Can't use the compute version of the gaussian blur.");
  605. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  606. ERR_FAIL_NULL(uniform_set_cache);
  607. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  608. ERR_FAIL_NULL(material_storage);
  609. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  610. copy.push_constant.section[0] = p_region.position.x;
  611. copy.push_constant.section[1] = p_region.position.y;
  612. copy.push_constant.target[0] = p_region.position.x;
  613. copy.push_constant.target[1] = p_region.position.y;
  614. copy.push_constant.section[2] = p_size.width;
  615. copy.push_constant.section[3] = p_size.height;
  616. // setup our uniforms
  617. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  618. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  619. RD::Uniform u_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_texture);
  620. CopyMode mode = p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY;
  621. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  622. ERR_FAIL_COND(shader.is_null());
  623. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  624. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  625. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  626. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_texture), 3);
  627. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  628. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  629. RD::get_singleton()->compute_list_end();
  630. }
  631. void CopyEffects::gaussian_blur_raster(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_region, const Size2i &p_size) {
  632. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR), "Can't use the raster version of the gaussian blur.");
  633. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  634. ERR_FAIL_NULL(uniform_set_cache);
  635. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  636. ERR_FAIL_NULL(material_storage);
  637. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  638. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  639. BlurRasterMode blur_mode = BLUR_MODE_GAUSSIAN_BLUR;
  640. blur_raster.push_constant.dest_pixel_size[0] = 1.0 / float(p_size.x);
  641. blur_raster.push_constant.dest_pixel_size[1] = 1.0 / float(p_size.y);
  642. // setup our uniforms
  643. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  644. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  645. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  646. ERR_FAIL_COND(shader.is_null());
  647. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
  648. 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)));
  649. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  650. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  651. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  652. RD::get_singleton()->draw_list_end();
  653. }
  654. 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) {
  655. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR), "Can't use the compute version of the gaussian glow.");
  656. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  657. ERR_FAIL_NULL(uniform_set_cache);
  658. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  659. ERR_FAIL_NULL(material_storage);
  660. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  661. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  662. uint32_t base_flags = 0;
  663. copy.push_constant.section[2] = p_size.x;
  664. copy.push_constant.section[3] = p_size.y;
  665. copy.push_constant.glow_strength = p_strength;
  666. copy.push_constant.glow_bloom = p_bloom;
  667. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  668. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  669. copy.push_constant.glow_exposure = p_exposure;
  670. copy.push_constant.glow_white = 0; //actually unused
  671. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  672. copy.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
  673. // setup our uniforms
  674. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  675. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  676. RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);
  677. RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);
  678. ERR_FAIL_COND(shader.is_null());
  679. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  680. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode].get_rid());
  681. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  682. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);
  683. if (p_auto_exposure.is_valid() && p_first_pass) {
  684. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  685. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  686. }
  687. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0);
  688. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  689. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  690. RD::get_singleton()->compute_list_end();
  691. }
  692. void CopyEffects::gaussian_glow_downsample_raster(RID p_source_rd_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) {
  693. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR), "Can't use the raster version of the gaussian glow.");
  694. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  695. ERR_FAIL_NULL(uniform_set_cache);
  696. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  697. ERR_FAIL_NULL(material_storage);
  698. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  699. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  700. BlurRasterMode blur_mode = p_first_pass ? BLUR_MODE_GAUSSIAN_GLOW_GATHER : BLUR_MODE_GAUSSIAN_GLOW_DOWNSAMPLE;
  701. blur_raster.push_constant.source_pixel_size[0] = 1.0 / float(p_size.x);
  702. blur_raster.push_constant.source_pixel_size[1] = 1.0 / float(p_size.y);
  703. blur_raster.push_constant.glow_strength = p_strength;
  704. blur_raster.push_constant.glow_bloom = p_bloom;
  705. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  706. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  707. blur_raster.push_constant.glow_exposure = p_exposure;
  708. blur_raster.push_constant.glow_white = 0; //actually unused
  709. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  710. blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
  711. // setup our uniforms
  712. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ blur_raster.glow_sampler, p_source_rd_texture }));
  713. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  714. ERR_FAIL_COND(shader.is_null());
  715. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
  716. 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)));
  717. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  718. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  719. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  720. RD::get_singleton()->draw_list_end();
  721. }
  722. void CopyEffects::gaussian_glow_upsample_raster(RID p_source_rd_texture, RID p_dest_texture, RID p_blend_texture, float p_luminance_multiplier, const Size2i &p_source_size, const Size2i &p_dest_size, float p_level, float p_base_strength, bool p_use_debanding) {
  723. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_GAUSSIAN_BLUR), "Can't use the raster version of the gaussian glow.");
  724. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  725. ERR_FAIL_NULL(uniform_set_cache);
  726. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  727. ERR_FAIL_NULL(material_storage);
  728. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  729. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  730. BlurRasterMode blur_mode = BLUR_MODE_GAUSSIAN_GLOW_UPSAMPLE;
  731. blur_raster.push_constant.source_pixel_size[0] = 1.0 / float(p_source_size.x);
  732. blur_raster.push_constant.source_pixel_size[1] = 1.0 / float(p_source_size.y);
  733. blur_raster.push_constant.dest_pixel_size[0] = 1.0 / float(p_dest_size.x);
  734. blur_raster.push_constant.dest_pixel_size[1] = 1.0 / float(p_dest_size.y);
  735. blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
  736. blur_raster.push_constant.level = p_level * 0.5;
  737. blur_raster.push_constant.glow_strength = p_base_strength;
  738. uint32_t spec_constant = p_use_debanding ? 1 : 0;
  739. spec_constant |= p_level > 0.01 ? 2 : 0;
  740. // setup our uniforms
  741. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  742. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  743. RD::Uniform u_blend_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_blend_texture }));
  744. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  745. ERR_FAIL_COND(shader.is_null());
  746. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
  747. 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), false, 0, spec_constant));
  748. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  749. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_blend_rd_texture), 1);
  750. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  751. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  752. RD::get_singleton()->draw_list_end();
  753. }
  754. void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  755. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_COPY), "Can't use the compute version of the make_mipmap shader.");
  756. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  757. ERR_FAIL_NULL(uniform_set_cache);
  758. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  759. ERR_FAIL_NULL(material_storage);
  760. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  761. copy.push_constant.section[0] = 0;
  762. copy.push_constant.section[1] = 0;
  763. copy.push_constant.section[2] = p_size.width;
  764. copy.push_constant.section[3] = p_size.height;
  765. // setup our uniforms
  766. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  767. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  768. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  769. CopyMode mode = COPY_MODE_MIPMAP;
  770. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  771. ERR_FAIL_COND(shader.is_null());
  772. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  773. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  774. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  775. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  776. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  777. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  778. RD::get_singleton()->compute_list_end();
  779. }
  780. void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  781. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_COPY), "Can't use the raster version of mipmap.");
  782. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  783. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  784. ERR_FAIL_NULL(uniform_set_cache);
  785. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  786. ERR_FAIL_NULL(material_storage);
  787. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  788. BlurRasterMode mode = BLUR_MIPMAP;
  789. blur_raster.push_constant.dest_pixel_size[0] = 1.0 / float(p_size.x);
  790. blur_raster.push_constant.dest_pixel_size[1] = 1.0 / float(p_size.y);
  791. // setup our uniforms
  792. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  793. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  794. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);
  795. ERR_FAIL_COND(shader.is_null());
  796. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
  797. 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)));
  798. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  799. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  800. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  801. RD::get_singleton()->draw_list_end();
  802. }
  803. void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  804. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_COPY), "Can't use the compute version of the set_color shader.");
  805. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  806. ERR_FAIL_NULL(uniform_set_cache);
  807. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  808. copy.push_constant.section[0] = 0;
  809. copy.push_constant.section[1] = 0;
  810. copy.push_constant.section[2] = p_region.size.width;
  811. copy.push_constant.section[3] = p_region.size.height;
  812. copy.push_constant.target[0] = p_region.position.x;
  813. copy.push_constant.target[1] = p_region.position.y;
  814. copy.push_constant.set_color[0] = p_color.r;
  815. copy.push_constant.set_color[1] = p_color.g;
  816. copy.push_constant.set_color[2] = p_color.b;
  817. copy.push_constant.set_color[3] = p_color.a;
  818. // setup our uniforms
  819. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  820. CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;
  821. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  822. ERR_FAIL_COND(shader.is_null());
  823. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  824. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode].get_rid());
  825. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  826. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  827. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  828. RD::get_singleton()->compute_list_end();
  829. }
  830. void CopyEffects::set_color_raster(RID p_dest_texture, const Color &p_color, const Rect2i &p_region) {
  831. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_COPY), "Can't use the raster version of the set_color shader.");
  832. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  833. ERR_FAIL_NULL(uniform_set_cache);
  834. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  835. ERR_FAIL_NULL(material_storage);
  836. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  837. copy_to_fb.push_constant.set_color[0] = p_color.r;
  838. copy_to_fb.push_constant.set_color[1] = p_color.g;
  839. copy_to_fb.push_constant.set_color[2] = p_color.b;
  840. copy_to_fb.push_constant.set_color[3] = p_color.a;
  841. RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
  842. CopyToFBMode mode = COPY_TO_FB_SET_COLOR;
  843. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  844. ERR_FAIL_COND(shader.is_null());
  845. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_region);
  846. 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)));
  847. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  848. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  849. RD::get_singleton()->draw_list_draw(draw_list, true);
  850. RD::get_singleton()->draw_list_end();
  851. }
  852. 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) {
  853. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  854. ERR_FAIL_NULL(uniform_set_cache);
  855. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  856. ERR_FAIL_NULL(material_storage);
  857. Rect2i screen_rect;
  858. float atlas_width = p_dst_size.width / p_rect.size.width;
  859. float atlas_height = p_dst_size.height / p_rect.size.height;
  860. screen_rect.position.x = (int32_t)(Math::round(p_rect.position.x * atlas_width));
  861. screen_rect.position.y = (int32_t)(Math::round(p_rect.position.y * atlas_height));
  862. screen_rect.size.width = (int32_t)(Math::round(p_dst_size.width));
  863. screen_rect.size.height = (int32_t)(Math::round(p_dst_size.height));
  864. CopyToDPPushConstant push_constant;
  865. push_constant.z_far = p_z_far;
  866. push_constant.z_near = p_z_near;
  867. push_constant.texel_size[0] = 1.0f / p_dst_size.width;
  868. push_constant.texel_size[1] = 1.0f / p_dst_size.height;
  869. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  870. // setup our uniforms
  871. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  872. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  873. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  874. ERR_FAIL_COND(shader.is_null());
  875. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, screen_rect);
  876. 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)));
  877. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  878. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  879. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  880. RD::get_singleton()->draw_list_draw(draw_list, true);
  881. RD::get_singleton()->draw_list_end();
  882. }
  883. void CopyEffects::copy_cubemap_to_octmap(RID p_source_rd_texture, RID p_dst_framebuffer, float p_border_size) {
  884. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  885. ERR_FAIL_NULL(uniform_set_cache);
  886. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  887. ERR_FAIL_NULL(material_storage);
  888. RID shader = cube_to_octmap.shader.version_get_shader(cube_to_octmap.shader_version, 0);
  889. ERR_FAIL_COND(shader.is_null());
  890. cube_to_octmap.push_constant.border_size = 1.0f - p_border_size * 2.0f;
  891. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  892. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  893. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
  894. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_octmap.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  895. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  896. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  897. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cube_to_octmap.push_constant, sizeof(CopyToOctmapPushConstant));
  898. RD::get_singleton()->draw_list_draw(draw_list, true);
  899. RD::get_singleton()->draw_list_end();
  900. }
  901. void CopyEffects::octmap_downsample(RID p_source_octmap, RID p_dest_octmap, const Size2i &p_size, float p_border_size) {
  902. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use compute based octmap downsample.");
  903. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  904. ERR_FAIL_NULL(uniform_set_cache);
  905. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  906. ERR_FAIL_NULL(material_storage);
  907. octmap_downsampler.push_constant.size = p_size.x;
  908. octmap_downsampler.push_constant.border_size = 1.0f - p_border_size * 2.0f;
  909. // setup our uniforms
  910. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  911. RD::Uniform u_source_octmap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_octmap }));
  912. RD::Uniform u_dest_octmap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_octmap }));
  913. int mode = 0;
  914. RD::TextureFormat texture_format = RD::get_singleton()->texture_get_format(p_dest_octmap);
  915. switch (texture_format.format) {
  916. case RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32: {
  917. mode |= DOWNSAMPLER_MODE_FLAG_RGB10_A2;
  918. } break;
  919. case RD::DATA_FORMAT_R16G16B16A16_SFLOAT: {
  920. // Absence of the flag indicates RGBA16F.
  921. } break;
  922. default: {
  923. ERR_FAIL_MSG("Unrecognized octmap format.");
  924. }
  925. }
  926. RID shader = octmap_downsampler.compute_shader.version_get_shader(octmap_downsampler.shader_version, mode);
  927. ERR_FAIL_COND(shader.is_null());
  928. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  929. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, octmap_downsampler.compute_pipelines[mode].get_rid());
  930. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_octmap), 0);
  931. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_octmap), 1);
  932. int x_groups = Math::division_round_up(p_size.x, 8);
  933. int y_groups = Math::division_round_up(p_size.y, 8);
  934. RD::get_singleton()->compute_list_set_push_constant(compute_list, &octmap_downsampler.push_constant, sizeof(OctmapDownsamplerPushConstant));
  935. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  936. RD::get_singleton()->compute_list_end();
  937. }
  938. void CopyEffects::octmap_downsample_raster(RID p_source_octmap, RID p_dest_framebuffer, const Size2i &p_size, float p_border_size) {
  939. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use raster based octmap downsample.");
  940. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  941. ERR_FAIL_NULL(uniform_set_cache);
  942. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  943. ERR_FAIL_NULL(material_storage);
  944. octmap_downsampler.push_constant.size = p_size.x;
  945. octmap_downsampler.push_constant.border_size = 1.0f - p_border_size * 2.0f;
  946. // setup our uniforms
  947. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  948. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_octmap }));
  949. RID shader = octmap_downsampler.raster_shader.version_get_shader(octmap_downsampler.shader_version, 0);
  950. ERR_FAIL_COND(shader.is_null());
  951. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_IGNORE_COLOR_ALL);
  952. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, octmap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  953. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  954. RD::get_singleton()->draw_list_set_push_constant(draw_list, &octmap_downsampler.push_constant, sizeof(OctmapDownsamplerPushConstant));
  955. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  956. RD::get_singleton()->draw_list_end();
  957. }
  958. static constexpr int _compute_dispatch_size(bool p_use_array) {
  959. constexpr int SIZE = 320;
  960. constexpr int GROUP = 64;
  961. constexpr int LEVELS = 6; // One less than Sky::REAL_TIME_ROUGHNESS_LAYERS.
  962. int size = 0;
  963. if (p_use_array) {
  964. size = SIZE * SIZE * LEVELS;
  965. } else {
  966. int dim = SIZE;
  967. for (int i = 0; i < LEVELS && dim >= 2; i++) {
  968. size += dim * dim;
  969. dim >>= 1;
  970. }
  971. }
  972. return (size + GROUP - 1) / GROUP;
  973. }
  974. void CopyEffects::octmap_filter(RID p_source_octmap, const Vector<RID> &p_dest_octmap, bool p_use_array, float p_border_size) {
  975. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use compute based octmap filter.");
  976. ERR_FAIL_COND(p_dest_octmap.is_empty());
  977. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  978. ERR_FAIL_NULL(uniform_set_cache);
  979. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  980. ERR_FAIL_NULL(material_storage);
  981. OctmapFilterPushConstant push_constant;
  982. push_constant.border_size[0] = p_border_size;
  983. push_constant.border_size[1] = 1.0f - p_border_size * 2.0f;
  984. push_constant.size = 320;
  985. Vector<RD::Uniform> uniforms;
  986. for (int i = 0; i < p_dest_octmap.size(); i++) {
  987. RD::Uniform u;
  988. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  989. u.binding = i;
  990. u.append_id(p_dest_octmap[i]);
  991. uniforms.push_back(u);
  992. }
  993. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  994. RD::get_singleton()->free_rid(filter.image_uniform_set);
  995. }
  996. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  997. // setup our uniforms
  998. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  999. RD::Uniform u_source_octmap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_octmap }));
  1000. int mode = 0;
  1001. if (p_use_array) {
  1002. mode |= FILTER_MODE_FLAG_ARRAY;
  1003. }
  1004. if (filter.use_high_quality) {
  1005. mode |= FILTER_MODE_FLAG_HIGH_QUALITY;
  1006. }
  1007. RD::TextureFormat texture_format = RD::get_singleton()->texture_get_format(p_dest_octmap[0]);
  1008. switch (texture_format.format) {
  1009. case RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32: {
  1010. mode |= FILTER_MODE_FLAG_RGB10_A2;
  1011. } break;
  1012. case RD::DATA_FORMAT_R16G16B16A16_SFLOAT: {
  1013. // Absence of the flag indicates RGBA16F.
  1014. } break;
  1015. default: {
  1016. ERR_FAIL_MSG("Unrecognized octmap format.");
  1017. }
  1018. }
  1019. RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);
  1020. ERR_FAIL_COND(shader.is_null());
  1021. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1022. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode].get_rid());
  1023. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_octmap), 0);
  1024. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  1025. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  1026. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(OctmapFilterPushConstant));
  1027. RD::get_singleton()->compute_list_dispatch(compute_list, _compute_dispatch_size(p_use_array), 1, 1);
  1028. RD::get_singleton()->compute_list_end();
  1029. }
  1030. void CopyEffects::octmap_filter_raster(RID p_source_octmap, RID p_dest_framebuffer, uint32_t p_mip_level, float p_border_size) {
  1031. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use raster based octmap filter.");
  1032. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1033. ERR_FAIL_NULL(uniform_set_cache);
  1034. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1035. ERR_FAIL_NULL(material_storage);
  1036. OctmapFilterRasterPushConstant push_constant;
  1037. push_constant.border_size[0] = p_border_size;
  1038. push_constant.border_size[1] = 1.0f - p_border_size * 2.0f;
  1039. push_constant.mip_level = p_mip_level;
  1040. // setup our uniforms
  1041. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1042. RD::Uniform u_source_octmap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_octmap }));
  1043. int mode = 0;
  1044. if (filter.use_high_quality) {
  1045. mode |= FILTER_MODE_FLAG_HIGH_QUALITY;
  1046. }
  1047. RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);
  1048. ERR_FAIL_COND(shader.is_null());
  1049. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_IGNORE_COLOR_ALL);
  1050. 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)));
  1051. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_octmap), 0);
  1052. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  1053. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(OctmapFilterRasterPushConstant));
  1054. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  1055. RD::get_singleton()->draw_list_end();
  1056. }
  1057. void CopyEffects::octmap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_sample_count, float p_roughness, uint32_t p_source_size, uint32_t p_dest_size, float p_border_size) {
  1058. ERR_FAIL_COND_MSG(raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use compute based octmap roughness.");
  1059. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1060. ERR_FAIL_NULL(uniform_set_cache);
  1061. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1062. ERR_FAIL_NULL(material_storage);
  1063. memset(&roughness.push_constant, 0, sizeof(OctmapRoughnessPushConstant));
  1064. // Remap to perceptual-roughness^2 to create more detail in lower mips and match the mapping of octmap_filter.
  1065. roughness.push_constant.roughness = p_roughness * p_roughness;
  1066. roughness.push_constant.sample_count = MIN(p_sample_count, 64u);
  1067. roughness.push_constant.source_size = p_source_size;
  1068. roughness.push_constant.dest_size = p_dest_size;
  1069. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1070. roughness.push_constant.border_size[0] = p_border_size;
  1071. roughness.push_constant.border_size[1] = 1.0f - p_border_size * 2.0;
  1072. // setup our uniforms
  1073. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1074. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));
  1075. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));
  1076. OctmapRoughnessMode mode;
  1077. RD::TextureFormat texture_format = RD::get_singleton()->texture_get_format(p_dest_texture);
  1078. switch (texture_format.format) {
  1079. case RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32: {
  1080. mode = ROUGHNESS_MODE_RGB10_A2;
  1081. } break;
  1082. case RD::DATA_FORMAT_R16G16B16A16_SFLOAT: {
  1083. mode = ROUGHNESS_MODE_RGBA16F;
  1084. } break;
  1085. default: {
  1086. ERR_FAIL_MSG("Unrecognized octmap format.");
  1087. }
  1088. }
  1089. RID shader = roughness.compute_shader.version_get_shader(roughness.shader_version, mode);
  1090. ERR_FAIL_COND(shader.is_null());
  1091. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1092. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipelines[mode].get_rid());
  1093. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  1094. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);
  1095. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(OctmapRoughnessPushConstant));
  1096. int x_groups = (p_dest_size + 7) / 8;
  1097. int y_groups = x_groups;
  1098. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  1099. RD::get_singleton()->compute_list_end();
  1100. }
  1101. void CopyEffects::octmap_roughness_raster(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_sample_count, float p_roughness, uint32_t p_source_size, uint32_t p_dest_size, float p_border_size) {
  1102. ERR_FAIL_COND_MSG(!raster_effects.has_flag(RASTER_EFFECT_OCTMAP), "Can't use raster based octmap roughness.");
  1103. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1104. ERR_FAIL_NULL(uniform_set_cache);
  1105. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1106. ERR_FAIL_NULL(material_storage);
  1107. memset(&roughness.push_constant, 0, sizeof(OctmapRoughnessPushConstant));
  1108. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  1109. roughness.push_constant.sample_count = MAX(uint32_t(float(p_sample_count * 4u) * roughness.push_constant.roughness), 4u);
  1110. roughness.push_constant.source_size = p_source_size;
  1111. roughness.push_constant.dest_size = p_dest_size;
  1112. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1113. roughness.push_constant.border_size[0] = p_border_size;
  1114. roughness.push_constant.border_size[1] = 1.0f - p_border_size * 2.0;
  1115. // Setup our uniforms.
  1116. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1117. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  1118. RID shader = roughness.raster_shader.version_get_shader(roughness.shader_version, 0);
  1119. ERR_FAIL_COND(shader.is_null());
  1120. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_IGNORE_COLOR_ALL);
  1121. 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)));
  1122. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  1123. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(OctmapRoughnessPushConstant));
  1124. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  1125. RD::get_singleton()->draw_list_end();
  1126. }
  1127. void CopyEffects::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection, uint32_t p_view_count) {
  1128. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1129. ERR_FAIL_NULL(uniform_set_cache);
  1130. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1131. ERR_FAIL_NULL(material_storage);
  1132. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1133. RD::get_singleton()->draw_command_begin_label("Merge Specular");
  1134. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
  1135. int mode;
  1136. if (p_reflection.is_valid()) {
  1137. if (p_base.is_valid()) {
  1138. mode = SPECULAR_MERGE_SSR;
  1139. } else {
  1140. mode = SPECULAR_MERGE_ADDITIVE_SSR;
  1141. }
  1142. } else {
  1143. if (p_base.is_valid()) {
  1144. mode = SPECULAR_MERGE_ADD;
  1145. } else {
  1146. mode = SPECULAR_MERGE_ADDITIVE_ADD;
  1147. }
  1148. }
  1149. if (p_view_count > 1) {
  1150. mode += SPECULAR_MERGE_ADD_MULTIVIEW;
  1151. }
  1152. RID shader = specular_merge.shader.version_get_shader(specular_merge.shader_version, mode);
  1153. 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)));
  1154. if (p_base.is_valid()) {
  1155. RD::Uniform u_base(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_base }));
  1156. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_base), 2);
  1157. }
  1158. RD::Uniform u_specular(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_specular }));
  1159. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_specular), 0);
  1160. if (p_reflection.is_valid()) {
  1161. RD::Uniform u_reflection(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_reflection }));
  1162. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_reflection), 1);
  1163. }
  1164. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  1165. RD::get_singleton()->draw_list_end();
  1166. RD::get_singleton()->draw_command_end_label();
  1167. }