ss_effects.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /**************************************************************************/
  2. /* ss_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 "ss_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/storage_rd/render_scene_buffers_rd.h"
  35. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  36. using namespace RendererRD;
  37. SSEffects *SSEffects::singleton = nullptr;
  38. static _FORCE_INLINE_ void store_camera(const Projection &p_mtx, float *p_array) {
  39. for (int i = 0; i < 4; i++) {
  40. for (int j = 0; j < 4; j++) {
  41. p_array[i * 4 + j] = p_mtx.columns[i][j];
  42. }
  43. }
  44. }
  45. SSEffects::SSEffects() {
  46. singleton = this;
  47. // Initialize depth buffer for screen space effects
  48. {
  49. Vector<String> downsampler_modes;
  50. downsampler_modes.push_back("\n");
  51. downsampler_modes.push_back("\n#define USE_HALF_SIZE\n");
  52. downsampler_modes.push_back("\n#define GENERATE_MIPS\n");
  53. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE\n");
  54. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  55. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE\n");
  56. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define GENERATE_FULL_MIPS");
  57. ss_effects.downsample_shader.initialize(downsampler_modes);
  58. ss_effects.downsample_shader_version = ss_effects.downsample_shader.version_create();
  59. for (int i = 0; i < SS_EFFECTS_MAX; i++) {
  60. ss_effects.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, i));
  61. }
  62. ss_effects.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSEffectsGatherConstants));
  63. SSEffectsGatherConstants gather_constants;
  64. const int sub_pass_count = 5;
  65. for (int pass = 0; pass < 4; pass++) {
  66. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  67. int a = pass;
  68. int spmap[5]{ 0, 1, 4, 3, 2 };
  69. int b = spmap[subPass];
  70. float ca, sa;
  71. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  72. ca = Math::cos(angle0);
  73. sa = Math::sin(angle0);
  74. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  75. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  76. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  77. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  78. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  79. }
  80. }
  81. RD::get_singleton()->buffer_update(ss_effects.gather_constants_buffer, 0, sizeof(SSEffectsGatherConstants), &gather_constants);
  82. }
  83. // Initialize Screen Space Indirect Lighting (SSIL)
  84. ssil_set_quality(RS::EnvironmentSSILQuality(int(GLOBAL_GET("rendering/environment/ssil/quality"))), GLOBAL_GET("rendering/environment/ssil/half_size"), GLOBAL_GET("rendering/environment/ssil/adaptive_target"), GLOBAL_GET("rendering/environment/ssil/blur_passes"), GLOBAL_GET("rendering/environment/ssil/fadeout_from"), GLOBAL_GET("rendering/environment/ssil/fadeout_to"));
  85. {
  86. Vector<String> ssil_modes;
  87. ssil_modes.push_back("\n");
  88. ssil_modes.push_back("\n#define SSIL_BASE\n");
  89. ssil_modes.push_back("\n#define ADAPTIVE\n");
  90. ssil.gather_shader.initialize(ssil_modes);
  91. ssil.gather_shader_version = ssil.gather_shader.version_create();
  92. for (int i = SSIL_GATHER; i <= SSIL_GATHER_ADAPTIVE; i++) {
  93. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.gather_shader.version_get_shader(ssil.gather_shader_version, i));
  94. }
  95. ssil.projection_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSILProjectionUniforms));
  96. }
  97. {
  98. Vector<String> ssil_modes;
  99. ssil_modes.push_back("\n#define GENERATE_MAP\n");
  100. ssil_modes.push_back("\n#define PROCESS_MAPA\n");
  101. ssil_modes.push_back("\n#define PROCESS_MAPB\n");
  102. ssil.importance_map_shader.initialize(ssil_modes);
  103. ssil.importance_map_shader_version = ssil.importance_map_shader.version_create();
  104. for (int i = SSIL_GENERATE_IMPORTANCE_MAP; i <= SSIL_PROCESS_IMPORTANCE_MAPB; i++) {
  105. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, i - SSIL_GENERATE_IMPORTANCE_MAP));
  106. }
  107. ssil.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  108. int zero[1] = { 0 };
  109. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  110. RD::get_singleton()->set_resource_name(ssil.importance_map_load_counter, "Importance Map Load Counter");
  111. Vector<RD::Uniform> uniforms;
  112. {
  113. RD::Uniform u;
  114. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  115. u.binding = 0;
  116. u.append_id(ssil.importance_map_load_counter);
  117. uniforms.push_back(u);
  118. }
  119. ssil.counter_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 2), 2);
  120. RD::get_singleton()->set_resource_name(ssil.counter_uniform_set, "Load Counter Uniform Set");
  121. }
  122. {
  123. Vector<String> ssil_modes;
  124. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  125. ssil_modes.push_back("\n#define MODE_SMART\n");
  126. ssil_modes.push_back("\n#define MODE_WIDE\n");
  127. ssil.blur_shader.initialize(ssil_modes);
  128. ssil.blur_shader_version = ssil.blur_shader.version_create();
  129. for (int i = SSIL_BLUR_PASS; i <= SSIL_BLUR_PASS_WIDE; i++) {
  130. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.blur_shader.version_get_shader(ssil.blur_shader_version, i - SSIL_BLUR_PASS));
  131. }
  132. }
  133. {
  134. Vector<String> ssil_modes;
  135. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  136. ssil_modes.push_back("\n#define MODE_SMART\n");
  137. ssil_modes.push_back("\n#define MODE_HALF\n");
  138. ssil.interleave_shader.initialize(ssil_modes);
  139. ssil.interleave_shader_version = ssil.interleave_shader.version_create();
  140. for (int i = SSIL_INTERLEAVE; i <= SSIL_INTERLEAVE_HALF; i++) {
  141. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, i - SSIL_INTERLEAVE));
  142. }
  143. }
  144. // Initialize Screen Space Ambient Occlusion (SSAO)
  145. ssao_set_quality(RS::EnvironmentSSAOQuality(int(GLOBAL_GET("rendering/environment/ssao/quality"))), GLOBAL_GET("rendering/environment/ssao/half_size"), GLOBAL_GET("rendering/environment/ssao/adaptive_target"), GLOBAL_GET("rendering/environment/ssao/blur_passes"), GLOBAL_GET("rendering/environment/ssao/fadeout_from"), GLOBAL_GET("rendering/environment/ssao/fadeout_to"));
  146. {
  147. RD::SamplerState sampler;
  148. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  149. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  150. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  151. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  152. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  153. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  154. sampler.max_lod = 4;
  155. uint32_t pipeline = 0;
  156. {
  157. Vector<String> ssao_modes;
  158. ssao_modes.push_back("\n");
  159. ssao_modes.push_back("\n#define SSAO_BASE\n");
  160. ssao_modes.push_back("\n#define ADAPTIVE\n");
  161. ssao.gather_shader.initialize(ssao_modes);
  162. ssao.gather_shader_version = ssao.gather_shader.version_create();
  163. for (int i = 0; i <= SSAO_GATHER_ADAPTIVE; i++) {
  164. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i));
  165. pipeline++;
  166. }
  167. }
  168. {
  169. Vector<String> ssao_modes;
  170. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  171. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  172. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  173. ssao.importance_map_shader.initialize(ssao_modes);
  174. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  175. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  176. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, i - SSAO_GENERATE_IMPORTANCE_MAP));
  177. pipeline++;
  178. }
  179. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  180. int zero[1] = { 0 };
  181. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  182. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  183. Vector<RD::Uniform> uniforms;
  184. {
  185. RD::Uniform u;
  186. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  187. u.binding = 0;
  188. u.append_id(ssao.importance_map_load_counter);
  189. uniforms.push_back(u);
  190. }
  191. ssao.counter_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 2), 2);
  192. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  193. }
  194. {
  195. Vector<String> ssao_modes;
  196. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  197. ssao_modes.push_back("\n#define MODE_SMART\n");
  198. ssao_modes.push_back("\n#define MODE_WIDE\n");
  199. ssao.blur_shader.initialize(ssao_modes);
  200. ssao.blur_shader_version = ssao.blur_shader.version_create();
  201. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  202. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  203. pipeline++;
  204. }
  205. }
  206. {
  207. Vector<String> ssao_modes;
  208. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  209. ssao_modes.push_back("\n#define MODE_SMART\n");
  210. ssao_modes.push_back("\n#define MODE_HALF\n");
  211. ssao.interleave_shader.initialize(ssao_modes);
  212. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  213. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  214. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  215. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  216. pipeline++;
  217. }
  218. }
  219. ERR_FAIL_COND(pipeline != SSAO_MAX);
  220. ss_effects.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  221. }
  222. // Screen Space Reflections
  223. ssr_roughness_quality = RS::EnvironmentSSRRoughnessQuality(int(GLOBAL_GET("rendering/environment/screen_space_reflection/roughness_quality")));
  224. {
  225. Vector<RD::PipelineSpecializationConstant> specialization_constants;
  226. {
  227. RD::PipelineSpecializationConstant sc;
  228. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  229. sc.constant_id = 0; // SSR_USE_FULL_PROJECTION_MATRIX
  230. sc.bool_value = false;
  231. specialization_constants.push_back(sc);
  232. }
  233. {
  234. Vector<String> ssr_scale_modes;
  235. ssr_scale_modes.push_back("\n");
  236. ssr_scale.shader.initialize(ssr_scale_modes);
  237. ssr_scale.shader_version = ssr_scale.shader.version_create();
  238. for (int v = 0; v < SSR_VARIATIONS; v++) {
  239. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  240. ssr_scale.pipelines[v] = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), specialization_constants);
  241. }
  242. }
  243. {
  244. Vector<String> ssr_modes;
  245. ssr_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_NORMAL
  246. ssr_modes.push_back("\n#define MODE_ROUGH\n"); // SCREEN_SPACE_REFLECTION_ROUGH
  247. ssr.shader.initialize(ssr_modes);
  248. ssr.shader_version = ssr.shader.version_create();
  249. for (int v = 0; v < SSR_VARIATIONS; v++) {
  250. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  251. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  252. ssr.pipelines[v][i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i), specialization_constants);
  253. }
  254. }
  255. }
  256. {
  257. Vector<String> ssr_filter_modes;
  258. ssr_filter_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL
  259. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n"); // SCREEN_SPACE_REFLECTION_FILTER_VERTICAL
  260. ssr_filter.shader.initialize(ssr_filter_modes);
  261. ssr_filter.shader_version = ssr_filter.shader.version_create();
  262. for (int v = 0; v < SSR_VARIATIONS; v++) {
  263. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  264. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  265. ssr_filter.pipelines[v][i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i), specialization_constants);
  266. }
  267. }
  268. }
  269. }
  270. // Subsurface scattering
  271. sss_quality = RS::SubSurfaceScatteringQuality(int(GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_quality")));
  272. sss_scale = GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_scale");
  273. sss_depth_scale = GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_depth_scale");
  274. {
  275. Vector<String> sss_modes;
  276. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  277. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  278. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  279. sss.shader.initialize(sss_modes);
  280. sss.shader_version = sss.shader.version_create();
  281. for (int i = 0; i < sss_modes.size(); i++) {
  282. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  283. }
  284. }
  285. }
  286. SSEffects::~SSEffects() {
  287. {
  288. // Cleanup SS Reflections
  289. ssr.shader.version_free(ssr.shader_version);
  290. ssr_filter.shader.version_free(ssr_filter.shader_version);
  291. ssr_scale.shader.version_free(ssr_scale.shader_version);
  292. if (ssr.ubo.is_valid()) {
  293. RD::get_singleton()->free(ssr.ubo);
  294. }
  295. }
  296. {
  297. // Cleanup SS downsampler
  298. ss_effects.downsample_shader.version_free(ss_effects.downsample_shader_version);
  299. RD::get_singleton()->free(ss_effects.mirror_sampler);
  300. RD::get_singleton()->free(ss_effects.gather_constants_buffer);
  301. }
  302. {
  303. // Cleanup SSIL
  304. ssil.blur_shader.version_free(ssil.blur_shader_version);
  305. ssil.gather_shader.version_free(ssil.gather_shader_version);
  306. ssil.interleave_shader.version_free(ssil.interleave_shader_version);
  307. ssil.importance_map_shader.version_free(ssil.importance_map_shader_version);
  308. RD::get_singleton()->free(ssil.importance_map_load_counter);
  309. RD::get_singleton()->free(ssil.projection_uniform_buffer);
  310. }
  311. {
  312. // Cleanup SSAO
  313. ssao.blur_shader.version_free(ssao.blur_shader_version);
  314. ssao.gather_shader.version_free(ssao.gather_shader_version);
  315. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  316. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  317. RD::get_singleton()->free(ssao.importance_map_load_counter);
  318. }
  319. {
  320. // Cleanup Subsurface scattering
  321. sss.shader.version_free(sss.shader_version);
  322. }
  323. singleton = nullptr;
  324. }
  325. /* SS Downsampler */
  326. void SSEffects::downsample_depth(Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_view, const Projection &p_projection) {
  327. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  328. ERR_FAIL_NULL(uniform_set_cache);
  329. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  330. ERR_FAIL_NULL(material_storage);
  331. uint32_t view_count = p_render_buffers->get_view_count();
  332. Size2i full_screen_size = p_render_buffers->get_internal_size();
  333. Size2i size((full_screen_size.x + 1) / 2, (full_screen_size.y + 1) / 2);
  334. // Make sure our buffers exist, buffers are automatically cleared if view count or size changes.
  335. if (!p_render_buffers->has_texture(RB_SCOPE_SSDS, RB_LINEAR_DEPTH)) {
  336. p_render_buffers->create_texture(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, RD::DATA_FORMAT_R16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, size, view_count * 4, 5);
  337. }
  338. // Downsample and deinterleave the depth buffer for SSAO and SSIL
  339. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  340. int downsample_mode = SS_EFFECTS_DOWNSAMPLE;
  341. bool use_mips = ssao_quality > RS::ENV_SSAO_QUALITY_MEDIUM || ssil_quality > RS::ENV_SSIL_QUALITY_MEDIUM;
  342. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW && ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  343. downsample_mode = SS_EFFECTS_DOWNSAMPLE_HALF;
  344. } else if (use_mips) {
  345. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  346. }
  347. bool use_half_size = false;
  348. bool use_full_mips = false;
  349. if (ssao_half_size && ssil_half_size) {
  350. downsample_mode++;
  351. use_half_size = true;
  352. } else if (ssao_half_size != ssil_half_size) {
  353. if (use_mips) {
  354. downsample_mode = SS_EFFECTS_DOWNSAMPLE_FULL_MIPS;
  355. use_full_mips = true;
  356. } else {
  357. // Only need the first two mipmaps, but the cost to generate the next two is trivial
  358. // TODO investigate the benefit of a shader version to generate only 2 mips
  359. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  360. use_mips = true;
  361. }
  362. }
  363. RID shader = ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, downsample_mode);
  364. int depth_index = use_half_size ? 1 : 0;
  365. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  366. RID downsample_uniform_set;
  367. if (use_mips) {
  368. // Grab our downsample uniform set from cache, these are automatically cleaned up if the depth textures are cleared.
  369. // This also ensures we can switch between left eye and right eye uniform sets without recreating the uniform twice a frame.
  370. thread_local LocalVector<RD::Uniform> u_depths;
  371. u_depths.clear();
  372. // Note, use_full_mips is true if either SSAO or SSIL uses half size, but the other full size and we're using mips.
  373. // That means we're filling all 5 levels.
  374. // In this scenario `depth_index` will be 0.
  375. for (int i = 0; i < (use_full_mips ? 4 : 3); i++) {
  376. RID depth_mipmap = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, depth_index + i + 1, 4, 1);
  377. RD::Uniform u_depth;
  378. u_depth.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  379. u_depth.binding = i;
  380. u_depth.append_id(depth_mipmap);
  381. u_depths.push_back(u_depth);
  382. }
  383. // This before only used SS_EFFECTS_DOWNSAMPLE_MIPMAP or SS_EFFECTS_DOWNSAMPLE_FULL_MIPS
  384. downsample_uniform_set = uniform_set_cache->get_cache_vec(shader, 2, u_depths);
  385. }
  386. Projection correction;
  387. correction.set_depth_correction(false);
  388. Projection temp = correction * p_projection;
  389. float depth_linearize_mul = -temp.columns[3][2];
  390. float depth_linearize_add = temp.columns[2][2];
  391. if (depth_linearize_mul * depth_linearize_add < 0) {
  392. depth_linearize_add = -depth_linearize_add;
  393. }
  394. ss_effects.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  395. ss_effects.downsample_push_constant.z_near = depth_linearize_mul;
  396. ss_effects.downsample_push_constant.z_far = depth_linearize_add;
  397. if (ss_effects.downsample_push_constant.orthogonal) {
  398. ss_effects.downsample_push_constant.z_near = p_projection.get_z_near();
  399. ss_effects.downsample_push_constant.z_far = p_projection.get_z_far();
  400. }
  401. ss_effects.downsample_push_constant.pixel_size[0] = 1.0 / full_screen_size.x;
  402. ss_effects.downsample_push_constant.pixel_size[1] = 1.0 / full_screen_size.y;
  403. ss_effects.downsample_push_constant.radius_sq = 1.0;
  404. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  405. RID depth_texture = p_render_buffers->get_depth_texture(p_view);
  406. RID depth_mipmap = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, depth_index, 4, 1);
  407. RD::Uniform u_depth_buffer(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, depth_texture }));
  408. RD::Uniform u_depth_mipmap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ depth_mipmap }));
  409. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ss_effects.pipelines[downsample_mode]);
  410. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_depth_buffer), 0);
  411. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_mipmap), 1);
  412. if (use_mips) {
  413. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, downsample_uniform_set, 2);
  414. }
  415. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ss_effects.downsample_push_constant, sizeof(SSEffectsDownsamplePushConstant));
  416. if (use_half_size) {
  417. size = Size2i(size.x >> 1, size.y >> 1).maxi(1);
  418. }
  419. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  420. RD::get_singleton()->compute_list_add_barrier(compute_list);
  421. RD::get_singleton()->draw_command_end_label();
  422. RD::get_singleton()->compute_list_end();
  423. ss_effects.used_full_mips_last_frame = use_full_mips;
  424. ss_effects.used_half_size_last_frame = use_half_size;
  425. ss_effects.used_mips_last_frame = use_mips;
  426. }
  427. /* SSIL */
  428. void SSEffects::ssil_set_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  429. ssil_quality = p_quality;
  430. ssil_half_size = p_half_size;
  431. ssil_adaptive_target = p_adaptive_target;
  432. ssil_blur_passes = p_blur_passes;
  433. ssil_fadeout_from = p_fadeout_from;
  434. ssil_fadeout_to = p_fadeout_to;
  435. }
  436. void SSEffects::gather_ssil(RD::ComputeListID p_compute_list, const RID *p_ssil_slices, const RID *p_edges_slices, const SSILSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set, RID p_projection_uniform_set) {
  437. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  438. ERR_FAIL_NULL(uniform_set_cache);
  439. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  440. if ((ssil_quality == RS::ENV_SSIL_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  441. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  442. }
  443. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_projection_uniform_set, 3);
  444. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0);
  445. for (int i = 0; i < 4; i++) {
  446. if ((ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  447. continue;
  448. }
  449. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_slices[i] }));
  450. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_edges_slices[i] }));
  451. ssil.gather_push_constant.pass_coord_offset[0] = i % 2;
  452. ssil.gather_push_constant.pass_coord_offset[1] = i / 2;
  453. ssil.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  454. ssil.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  455. ssil.gather_push_constant.pass = i;
  456. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, uniform_set_cache->get_cache(shader, 2, u_ssil_slice, u_edges_slice), 2);
  457. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssil.gather_push_constant, sizeof(SSILGatherPushConstant));
  458. Size2i size;
  459. // Calculate size same way as we created the buffer
  460. if (ssil_half_size) {
  461. size.x = (p_settings.full_screen_size.x + 3) / 4;
  462. size.y = (p_settings.full_screen_size.y + 3) / 4;
  463. } else {
  464. size.x = (p_settings.full_screen_size.x + 1) / 2;
  465. size.y = (p_settings.full_screen_size.y + 1) / 2;
  466. }
  467. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  468. }
  469. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  470. }
  471. void SSEffects::ssil_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings) {
  472. if (p_ssil_buffers.half_size != ssil_half_size) {
  473. p_render_buffers->clear_context(RB_SCOPE_SSIL);
  474. }
  475. p_ssil_buffers.half_size = ssil_half_size;
  476. if (p_ssil_buffers.half_size) {
  477. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  478. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  479. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  480. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  481. } else {
  482. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  483. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  484. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  485. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  486. }
  487. uint32_t view_count = p_render_buffers->get_view_count();
  488. Size2i full_size = Size2i(p_ssil_buffers.buffer_width, p_ssil_buffers.buffer_height);
  489. Size2i half_size = Size2i(p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height);
  490. // We create our intermediate and final results as render buffers.
  491. // These are automatically cached and cleaned up when our viewport resizes
  492. // or when our viewport gets destroyed.
  493. if (!p_render_buffers->has_texture(RB_SCOPE_SSIL, RB_FINAL)) { // We don't strictly have to check if it exists but we only want to clear it when we create it...
  494. RID final = p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_FINAL, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT);
  495. RD::get_singleton()->texture_clear(final, Color(0, 0, 0, 0), 0, 1, 0, view_count);
  496. }
  497. if (!p_render_buffers->has_texture(RB_SCOPE_SSIL, RB_LAST_FRAME)) {
  498. RID last_frame = p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_LAST_FRAME, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT, RD::TEXTURE_SAMPLES_1, p_settings.full_screen_size, 0, 6);
  499. RD::get_singleton()->texture_clear(last_frame, Color(0, 0, 0, 0), 0, 6, 0, view_count);
  500. }
  501. // As we're not clearing these, and render buffers will return the cached texture if it already exists,
  502. // we don't first check has_texture here
  503. p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_DEINTERLEAVED, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, full_size, 4 * view_count);
  504. p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_DEINTERLEAVED_PONG, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, full_size, 4 * view_count);
  505. p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_EDGES, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, full_size, 4 * view_count);
  506. p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_IMPORTANCE_MAP, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, half_size);
  507. p_render_buffers->create_texture(RB_SCOPE_SSIL, RB_IMPORTANCE_PONG, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, half_size);
  508. }
  509. void SSEffects::screen_space_indirect_lighting(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const Projection &p_last_projection, const SSILSettings &p_settings) {
  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. RD::get_singleton()->draw_command_begin_label("Process Screen Space Indirect Lighting");
  515. // Obtain our (cached) buffer slices for the view we are rendering.
  516. RID last_frame = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_LAST_FRAME, p_view, 0, 1, 6);
  517. RID deinterleaved = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED, p_view * 4, 0, 4, 1);
  518. RID deinterleaved_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED_PONG, 4 * p_view, 0, 4, 1);
  519. RID edges = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_EDGES, 4 * p_view, 0, 4, 1);
  520. RID importance_map = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_IMPORTANCE_MAP, p_view, 0);
  521. RID importance_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_IMPORTANCE_PONG, p_view, 0);
  522. RID deinterleaved_slices[4];
  523. RID deinterleaved_pong_slices[4];
  524. RID edges_slices[4];
  525. for (uint32_t i = 0; i < 4; i++) {
  526. deinterleaved_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED, p_view * 4 + i, 0);
  527. deinterleaved_pong_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED_PONG, p_view * 4 + i, 0);
  528. edges_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_EDGES, p_view * 4 + i, 0);
  529. }
  530. //Store projection info before starting the compute list
  531. SSILProjectionUniforms projection_uniforms;
  532. store_camera(p_last_projection, projection_uniforms.inv_last_frame_projection_matrix);
  533. RD::get_singleton()->buffer_update(ssil.projection_uniform_buffer, 0, sizeof(SSILProjectionUniforms), &projection_uniforms);
  534. memset(&ssil.gather_push_constant, 0, sizeof(SSILGatherPushConstant));
  535. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, SSIL_GATHER);
  536. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  537. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  538. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  539. {
  540. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  541. ssil.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  542. ssil.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  543. ssil.gather_push_constant.half_screen_pixel_size[0] = 2.0 / p_settings.full_screen_size.x;
  544. ssil.gather_push_constant.half_screen_pixel_size[1] = 2.0 / p_settings.full_screen_size.y;
  545. if (ssil_half_size) {
  546. ssil.gather_push_constant.half_screen_pixel_size[0] *= 2.0;
  547. ssil.gather_push_constant.half_screen_pixel_size[1] *= 2.0;
  548. }
  549. ssil.gather_push_constant.half_screen_pixel_size_x025[0] = ssil.gather_push_constant.half_screen_pixel_size[0] * 0.75;
  550. ssil.gather_push_constant.half_screen_pixel_size_x025[1] = ssil.gather_push_constant.half_screen_pixel_size[1] * 0.75;
  551. float tan_half_fov_x = 1.0 / p_projection.columns[0][0];
  552. float tan_half_fov_y = 1.0 / p_projection.columns[1][1];
  553. ssil.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  554. ssil.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  555. ssil.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  556. ssil.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  557. ssil.gather_push_constant.z_near = p_projection.get_z_near();
  558. ssil.gather_push_constant.z_far = p_projection.get_z_far();
  559. ssil.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  560. ssil.gather_push_constant.radius = p_settings.radius;
  561. float radius_near_limit = (p_settings.radius * 1.2f);
  562. if (ssil_quality <= RS::ENV_SSIL_QUALITY_LOW) {
  563. radius_near_limit *= 1.50f;
  564. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  565. ssil.gather_push_constant.radius *= 0.8f;
  566. }
  567. }
  568. radius_near_limit /= tan_half_fov_y;
  569. ssil.gather_push_constant.intensity = p_settings.intensity * Math_PI;
  570. ssil.gather_push_constant.fade_out_mul = -1.0 / (ssil_fadeout_to - ssil_fadeout_from);
  571. ssil.gather_push_constant.fade_out_add = ssil_fadeout_from / (ssil_fadeout_to - ssil_fadeout_from) + 1.0;
  572. ssil.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  573. ssil.gather_push_constant.neg_inv_radius = -1.0 / ssil.gather_push_constant.radius;
  574. ssil.gather_push_constant.normal_rejection_amount = p_settings.normal_rejection;
  575. ssil.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssil_buffers.half_buffer_width) * (p_ssil_buffers.half_buffer_height) * 255);
  576. ssil.gather_push_constant.adaptive_sample_limit = ssil_adaptive_target;
  577. ssil.gather_push_constant.quality = MAX(0, ssil_quality - 1);
  578. ssil.gather_push_constant.size_multiplier = ssil_half_size ? 2 : 1;
  579. // We are using our uniform cache so our uniform sets are automatically freed when our textures are freed.
  580. // It also ensures that we're reusing the right cached entry in a multiview situation without us having to
  581. // remember each instance of the uniform set.
  582. RID projection_uniform_set;
  583. {
  584. RD::Uniform u_last_frame;
  585. u_last_frame.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  586. u_last_frame.binding = 0;
  587. u_last_frame.append_id(default_mipmap_sampler);
  588. u_last_frame.append_id(last_frame);
  589. RD::Uniform u_projection;
  590. u_projection.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  591. u_projection.binding = 1;
  592. u_projection.append_id(ssil.projection_uniform_buffer);
  593. projection_uniform_set = uniform_set_cache->get_cache(shader, 3, u_last_frame, u_projection);
  594. }
  595. RID gather_uniform_set;
  596. {
  597. RID depth_texture_view = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, ssil_half_size ? 1 : 0, 4, 4);
  598. RD::Uniform u_depth_texture_view;
  599. u_depth_texture_view.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  600. u_depth_texture_view.binding = 0;
  601. u_depth_texture_view.append_id(ss_effects.mirror_sampler);
  602. u_depth_texture_view.append_id(depth_texture_view);
  603. RD::Uniform u_normal_buffer;
  604. u_normal_buffer.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  605. u_normal_buffer.binding = 1;
  606. u_normal_buffer.append_id(p_normal_buffer);
  607. RD::Uniform u_gather_constants_buffer;
  608. u_gather_constants_buffer.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  609. u_gather_constants_buffer.binding = 2;
  610. u_gather_constants_buffer.append_id(ss_effects.gather_constants_buffer);
  611. gather_uniform_set = uniform_set_cache->get_cache(shader, 0, u_depth_texture_view, u_normal_buffer, u_gather_constants_buffer);
  612. }
  613. RID importance_map_uniform_set;
  614. {
  615. RD::Uniform u_pong;
  616. u_pong.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  617. u_pong.binding = 0;
  618. u_pong.append_id(deinterleaved_pong);
  619. RD::Uniform u_importance_map;
  620. u_importance_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  621. u_importance_map.binding = 1;
  622. u_importance_map.append_id(default_sampler);
  623. u_importance_map.append_id(importance_map);
  624. RD::Uniform u_load_counter;
  625. u_load_counter.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  626. u_load_counter.binding = 2;
  627. u_load_counter.append_id(ssil.importance_map_load_counter);
  628. RID shader_adaptive = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, SSIL_GATHER_ADAPTIVE);
  629. importance_map_uniform_set = uniform_set_cache->get_cache(shader_adaptive, 1, u_pong, u_importance_map, u_load_counter);
  630. }
  631. if (ssil_quality == RS::ENV_SSIL_QUALITY_ULTRA) {
  632. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  633. ssil.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  634. ssil.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  635. ssil.importance_map_push_constant.intensity = p_settings.intensity * Math_PI;
  636. //base pass
  637. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_BASE]);
  638. gather_ssil(compute_list, deinterleaved_pong_slices, edges_slices, p_settings, true, gather_uniform_set, importance_map_uniform_set, projection_uniform_set);
  639. //generate importance map
  640. RID gen_imp_shader = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 0);
  641. RD::Uniform u_ssil_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong }));
  642. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_map }));
  643. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GENERATE_IMPORTANCE_MAP]);
  644. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 0, u_ssil_pong_with_sampler), 0);
  645. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 1, u_importance_map), 1);
  646. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  647. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  648. RD::get_singleton()->compute_list_add_barrier(compute_list);
  649. // process Importance Map A
  650. RID proc_imp_shader_a = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 1);
  651. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_map }));
  652. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_pong }));
  653. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPA]);
  654. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_a, 0, u_importance_map_with_sampler), 0);
  655. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_a, 1, u_importance_map_pong), 1);
  656. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  657. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  658. RD::get_singleton()->compute_list_add_barrier(compute_list);
  659. // process Importance Map B
  660. RID proc_imp_shader_b = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 2);
  661. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_pong }));
  662. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPB]);
  663. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 0, u_importance_map_pong_with_sampler), 0);
  664. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 1, u_importance_map), 1);
  665. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssil.counter_uniform_set, 2);
  666. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  667. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  668. RD::get_singleton()->compute_list_add_barrier(compute_list);
  669. RD::get_singleton()->draw_command_end_label(); // Importance Map
  670. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_ADAPTIVE]);
  671. } else {
  672. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER]);
  673. }
  674. gather_ssil(compute_list, deinterleaved_slices, edges_slices, p_settings, false, gather_uniform_set, importance_map_uniform_set, projection_uniform_set);
  675. RD::get_singleton()->draw_command_end_label(); //Gather
  676. }
  677. {
  678. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  679. ssil.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  680. ssil.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  681. ssil.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  682. int blur_passes = ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW ? ssil_blur_passes : 1;
  683. shader = ssil.blur_shader.version_get_shader(ssil.blur_shader_version, 0);
  684. for (int pass = 0; pass < blur_passes; pass++) {
  685. int blur_pipeline = SSIL_BLUR_PASS;
  686. if (ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  687. blur_pipeline = SSIL_BLUR_PASS_SMART;
  688. if (pass < blur_passes - 2) {
  689. blur_pipeline = SSIL_BLUR_PASS_WIDE;
  690. }
  691. }
  692. RID blur_shader = ssil.blur_shader.version_get_shader(ssil.blur_shader_version, blur_pipeline - SSIL_BLUR_PASS);
  693. for (int i = 0; i < 4; i++) {
  694. if ((ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  695. continue;
  696. }
  697. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[blur_pipeline]);
  698. if (pass % 2 == 0) {
  699. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  700. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_slices[i] }));
  701. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_slice), 0);
  702. } else {
  703. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, deinterleaved_slices[i] }));
  704. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_slice), 0);
  705. }
  706. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ deinterleaved_pong_slices[i] }));
  707. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ssil_pong_slice), 1);
  708. } else {
  709. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  710. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong_slices[i] }));
  711. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_pong_slice), 0);
  712. } else {
  713. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, deinterleaved_pong_slices[i] }));
  714. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_pong_slice), 0);
  715. }
  716. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ deinterleaved_slices[i] }));
  717. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ssil_slice), 1);
  718. }
  719. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ edges_slices[i] }));
  720. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 2, u_edges_slice), 2);
  721. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.blur_push_constant, sizeof(SSILBlurPushConstant));
  722. // Use the size of the actual buffer we're processing here or we won't cover the entire image.
  723. int x_groups = p_ssil_buffers.buffer_width;
  724. int y_groups = p_ssil_buffers.buffer_height;
  725. RD::get_singleton()->compute_list_dispatch_threads(compute_list, x_groups, y_groups, 1);
  726. }
  727. RD::get_singleton()->compute_list_add_barrier(compute_list);
  728. }
  729. RD::get_singleton()->draw_command_end_label(); // Blur
  730. }
  731. {
  732. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  733. ssil.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  734. ssil.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  735. ssil.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  736. ssil.interleave_push_constant.size_modifier = uint32_t(ssil_half_size ? 4 : 2);
  737. int interleave_pipeline = SSIL_INTERLEAVE_HALF;
  738. if (ssil_quality == RS::ENV_SSIL_QUALITY_LOW) {
  739. interleave_pipeline = SSIL_INTERLEAVE;
  740. } else if (ssil_quality >= RS::ENV_SSIL_QUALITY_MEDIUM) {
  741. interleave_pipeline = SSIL_INTERLEAVE_SMART;
  742. }
  743. shader = ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, 0);
  744. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[interleave_pipeline]);
  745. RID final = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_FINAL, p_view, 0);
  746. RD::Uniform u_destination(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ final }));
  747. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_destination), 0);
  748. if (ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW && ssil_blur_passes % 2 == 0) {
  749. RD::Uniform u_ssil(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved }));
  750. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil), 1);
  751. } else {
  752. RD::Uniform u_ssil_pong(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong }));
  753. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil_pong), 1);
  754. }
  755. RD::Uniform u_edges(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ edges }));
  756. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_edges), 2);
  757. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.interleave_push_constant, sizeof(SSILInterleavePushConstant));
  758. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  759. RD::get_singleton()->compute_list_add_barrier(compute_list);
  760. RD::get_singleton()->draw_command_end_label(); // Interleave
  761. }
  762. RD::get_singleton()->draw_command_end_label(); // SSIL
  763. RD::get_singleton()->compute_list_end();
  764. int zero[1] = { 0 };
  765. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  766. }
  767. /* SSAO */
  768. void SSEffects::ssao_set_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  769. ssao_quality = p_quality;
  770. ssao_half_size = p_half_size;
  771. ssao_adaptive_target = p_adaptive_target;
  772. ssao_blur_passes = p_blur_passes;
  773. ssao_fadeout_from = p_fadeout_from;
  774. ssao_fadeout_to = p_fadeout_to;
  775. }
  776. void SSEffects::gather_ssao(RD::ComputeListID p_compute_list, const RID *p_ao_slices, const SSAOSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set) {
  777. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  778. ERR_FAIL_NULL(uniform_set_cache);
  779. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  780. if ((ssao_quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  781. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  782. }
  783. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 1); //
  784. for (int i = 0; i < 4; i++) {
  785. if ((ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  786. continue;
  787. }
  788. RD::Uniform u_ao_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ao_slices[i] }));
  789. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  790. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  791. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  792. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  793. ssao.gather_push_constant.pass = i;
  794. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, uniform_set_cache->get_cache(shader, 2, u_ao_slice), 2);
  795. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  796. Size2i size;
  797. // Make sure we use the same size as with which our buffer was created
  798. if (ssao_half_size) {
  799. size.x = (p_settings.full_screen_size.x + 3) / 4;
  800. size.y = (p_settings.full_screen_size.y + 3) / 4;
  801. } else {
  802. size.x = (p_settings.full_screen_size.x + 1) / 2;
  803. size.y = (p_settings.full_screen_size.y + 1) / 2;
  804. }
  805. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  806. }
  807. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  808. }
  809. void SSEffects::ssao_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings) {
  810. if (p_ssao_buffers.half_size != ssao_half_size) {
  811. p_render_buffers->clear_context(RB_SCOPE_SSAO);
  812. }
  813. p_ssao_buffers.half_size = ssao_half_size;
  814. if (ssao_half_size) {
  815. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  816. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  817. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  818. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  819. } else {
  820. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  821. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  822. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  823. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  824. }
  825. uint32_t view_count = p_render_buffers->get_view_count();
  826. Size2i full_size = Size2i(p_ssao_buffers.buffer_width, p_ssao_buffers.buffer_height);
  827. Size2i half_size = Size2i(p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height);
  828. // As we're not clearing these, and render buffers will return the cached texture if it already exists,
  829. // we don't first check has_texture here
  830. p_render_buffers->create_texture(RB_SCOPE_SSAO, RB_DEINTERLEAVED, RD::DATA_FORMAT_R8G8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, full_size, 4 * view_count);
  831. p_render_buffers->create_texture(RB_SCOPE_SSAO, RB_DEINTERLEAVED_PONG, RD::DATA_FORMAT_R8G8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, full_size, 4 * view_count);
  832. p_render_buffers->create_texture(RB_SCOPE_SSAO, RB_IMPORTANCE_MAP, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, half_size);
  833. p_render_buffers->create_texture(RB_SCOPE_SSAO, RB_IMPORTANCE_PONG, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, half_size);
  834. p_render_buffers->create_texture(RB_SCOPE_SSAO, RB_FINAL, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1);
  835. }
  836. void SSEffects::generate_ssao(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const SSAOSettings &p_settings) {
  837. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  838. ERR_FAIL_NULL(uniform_set_cache);
  839. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  840. ERR_FAIL_NULL(material_storage);
  841. // Obtain our (cached) buffer slices for the view we are rendering.
  842. RID ao_deinterleaved = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED, p_view * 4, 0, 4, 1);
  843. RID ao_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED_PONG, p_view * 4, 0, 4, 1);
  844. RID importance_map = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_IMPORTANCE_MAP, p_view, 0);
  845. RID importance_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_IMPORTANCE_PONG, p_view, 0);
  846. RID ao_final = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_FINAL, p_view, 0);
  847. RID ao_deinterleaved_slices[4];
  848. RID ao_pong_slices[4];
  849. for (uint32_t i = 0; i < 4; i++) {
  850. ao_deinterleaved_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED, p_view * 4 + i, 0);
  851. ao_pong_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED_PONG, p_view * 4 + i, 0);
  852. }
  853. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  854. memset(&ssao.gather_push_constant, 0, sizeof(SSAOGatherPushConstant));
  855. /* FIRST PASS */
  856. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, SSAO_GATHER);
  857. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  858. RD::get_singleton()->draw_command_begin_label("Process Screen Space Ambient Occlusion");
  859. /* SECOND PASS */
  860. // Sample SSAO
  861. {
  862. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  863. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  864. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  865. ssao.gather_push_constant.half_screen_pixel_size[0] = 2.0 / p_settings.full_screen_size.x;
  866. ssao.gather_push_constant.half_screen_pixel_size[1] = 2.0 / p_settings.full_screen_size.y;
  867. if (ssao_half_size) {
  868. ssao.gather_push_constant.half_screen_pixel_size[0] *= 2.0;
  869. ssao.gather_push_constant.half_screen_pixel_size[1] *= 2.0;
  870. }
  871. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.75;
  872. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.75;
  873. float tan_half_fov_x = 1.0 / p_projection.columns[0][0];
  874. float tan_half_fov_y = 1.0 / p_projection.columns[1][1];
  875. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  876. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  877. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  878. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  879. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  880. ssao.gather_push_constant.radius = p_settings.radius;
  881. float radius_near_limit = (p_settings.radius * 1.2f);
  882. if (ssao_quality <= RS::ENV_SSAO_QUALITY_LOW) {
  883. radius_near_limit *= 1.50f;
  884. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  885. ssao.gather_push_constant.radius *= 0.8f;
  886. }
  887. }
  888. radius_near_limit /= tan_half_fov_y;
  889. ssao.gather_push_constant.intensity = p_settings.intensity;
  890. ssao.gather_push_constant.shadow_power = p_settings.power;
  891. ssao.gather_push_constant.shadow_clamp = 0.98;
  892. ssao.gather_push_constant.fade_out_mul = -1.0 / (ssao_fadeout_to - ssao_fadeout_from);
  893. ssao.gather_push_constant.fade_out_add = ssao_fadeout_from / (ssao_fadeout_to - ssao_fadeout_from) + 1.0;
  894. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  895. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  896. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  897. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssao_buffers.half_buffer_width) * (p_ssao_buffers.half_buffer_height) * 255);
  898. ssao.gather_push_constant.adaptive_sample_limit = ssao_adaptive_target;
  899. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  900. ssao.gather_push_constant.quality = MAX(0, ssao_quality - 1);
  901. ssao.gather_push_constant.size_multiplier = ssao_half_size ? 2 : 1;
  902. // We are using our uniform cache so our uniform sets are automatically freed when our textures are freed.
  903. // It also ensures that we're reusing the right cached entry in a multiview situation without us having to
  904. // remember each instance of the uniform set.
  905. RID gather_uniform_set;
  906. {
  907. RID depth_texture_view = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, ssao_half_size ? 1 : 0, 4, 4);
  908. RD::Uniform u_depth_texture_view;
  909. u_depth_texture_view.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  910. u_depth_texture_view.binding = 0;
  911. u_depth_texture_view.append_id(ss_effects.mirror_sampler);
  912. u_depth_texture_view.append_id(depth_texture_view);
  913. RD::Uniform u_normal_buffer;
  914. u_normal_buffer.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  915. u_normal_buffer.binding = 1;
  916. u_normal_buffer.append_id(p_normal_buffer);
  917. RD::Uniform u_gather_constants_buffer;
  918. u_gather_constants_buffer.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  919. u_gather_constants_buffer.binding = 2;
  920. u_gather_constants_buffer.append_id(ss_effects.gather_constants_buffer);
  921. gather_uniform_set = uniform_set_cache->get_cache(shader, 0, u_depth_texture_view, u_normal_buffer, u_gather_constants_buffer);
  922. }
  923. RID importance_map_uniform_set;
  924. {
  925. RD::Uniform u_pong;
  926. u_pong.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  927. u_pong.binding = 0;
  928. u_pong.append_id(ao_pong);
  929. RD::Uniform u_importance_map;
  930. u_importance_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  931. u_importance_map.binding = 1;
  932. u_importance_map.append_id(default_sampler);
  933. u_importance_map.append_id(importance_map);
  934. RD::Uniform u_load_counter;
  935. u_load_counter.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  936. u_load_counter.binding = 2;
  937. u_load_counter.append_id(ssao.importance_map_load_counter);
  938. RID shader_adaptive = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, SSAO_GATHER_ADAPTIVE);
  939. importance_map_uniform_set = uniform_set_cache->get_cache(shader_adaptive, 1, u_pong, u_importance_map, u_load_counter);
  940. }
  941. if (ssao_quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  942. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  943. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  944. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  945. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  946. ssao.importance_map_push_constant.power = p_settings.power;
  947. //base pass
  948. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  949. gather_ssao(compute_list, ao_pong_slices, p_settings, true, gather_uniform_set, RID());
  950. //generate importance map
  951. RID gen_imp_shader = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 0);
  952. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  953. RD::Uniform u_ao_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong }));
  954. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 0, u_ao_pong_with_sampler), 0);
  955. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_map }));
  956. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 1, u_importance_map), 1);
  957. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  958. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  959. RD::get_singleton()->compute_list_add_barrier(compute_list);
  960. //process importance map A
  961. RID proc_imp_shader_a = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 1);
  962. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  963. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_map }));
  964. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_a, 0, u_importance_map_with_sampler), 0);
  965. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_pong }));
  966. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_a, 1, u_importance_map_pong), 1);
  967. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  968. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  969. RD::get_singleton()->compute_list_add_barrier(compute_list);
  970. //process Importance Map B
  971. RID proc_imp_shader_b = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 2);
  972. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  973. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_pong }));
  974. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 0, u_importance_map_pong_with_sampler), 0);
  975. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 1, u_importance_map), 1);
  976. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  977. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  978. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  979. RD::get_singleton()->compute_list_add_barrier(compute_list);
  980. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  981. RD::get_singleton()->draw_command_end_label(); // Importance Map
  982. } else {
  983. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  984. }
  985. gather_ssao(compute_list, ao_deinterleaved_slices, p_settings, false, gather_uniform_set, importance_map_uniform_set);
  986. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  987. }
  988. // /* THIRD PASS */
  989. // // Blur
  990. //
  991. {
  992. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  993. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  994. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  995. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  996. int blur_passes = ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? ssao_blur_passes : 1;
  997. shader = ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0);
  998. for (int pass = 0; pass < blur_passes; pass++) {
  999. int blur_pipeline = SSAO_BLUR_PASS;
  1000. if (ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1001. if (pass < blur_passes - 2) {
  1002. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  1003. } else {
  1004. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1005. }
  1006. }
  1007. for (int i = 0; i < 4; i++) {
  1008. if ((ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1009. continue;
  1010. }
  1011. RID blur_shader = ssao.blur_shader.version_get_shader(ssao.blur_shader_version, blur_pipeline - SSAO_BLUR_PASS);
  1012. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  1013. if (pass % 2 == 0) {
  1014. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1015. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_deinterleaved_slices[i] }));
  1016. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_slices_with_sampler), 0);
  1017. } else {
  1018. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, ao_deinterleaved_slices[i] }));
  1019. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_slices_with_sampler), 0);
  1020. }
  1021. RD::Uniform u_ao_pong_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_pong_slices[i] }));
  1022. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ao_pong_slices), 1);
  1023. } else {
  1024. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1025. RD::Uniform u_ao_pong_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong_slices[i] }));
  1026. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_pong_slices_with_sampler), 0);
  1027. } else {
  1028. RD::Uniform u_ao_pong_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, ao_pong_slices[i] }));
  1029. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_pong_slices_with_sampler), 0);
  1030. }
  1031. RD::Uniform u_ao_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_deinterleaved_slices[i] }));
  1032. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ao_slices), 1);
  1033. }
  1034. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  1035. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.buffer_width, p_ssao_buffers.buffer_height, 1);
  1036. }
  1037. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1038. }
  1039. RD::get_singleton()->draw_command_end_label(); // Blur
  1040. }
  1041. /* FOURTH PASS */
  1042. // Interleave buffers
  1043. // back to full size
  1044. {
  1045. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1046. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1047. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1048. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1049. ssao.interleave_push_constant.size_modifier = uint32_t(ssao_half_size ? 4 : 2);
  1050. shader = ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, 0);
  1051. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1052. if (ssao_quality == RS::ENV_SSAO_QUALITY_LOW) {
  1053. interleave_pipeline = SSAO_INTERLEAVE;
  1054. } else if (ssao_quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1055. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1056. }
  1057. RID interleave_shader = ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, interleave_pipeline - SSAO_INTERLEAVE);
  1058. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  1059. RD::Uniform u_upscale_buffer(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_final }));
  1060. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 0, u_upscale_buffer), 0);
  1061. if (ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW && ssao_blur_passes % 2 == 0) {
  1062. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_deinterleaved }));
  1063. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 1, u_ao), 1);
  1064. } else {
  1065. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong }));
  1066. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 1, u_ao), 1);
  1067. }
  1068. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1069. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1070. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1071. RD::get_singleton()->draw_command_end_label(); // Interleave
  1072. }
  1073. RD::get_singleton()->draw_command_end_label(); //SSAO
  1074. RD::get_singleton()->compute_list_end();
  1075. int zero[1] = { 0 };
  1076. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1077. }
  1078. /* Screen Space Reflection */
  1079. void SSEffects::ssr_set_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
  1080. ssr_roughness_quality = p_quality;
  1081. }
  1082. void SSEffects::ssr_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RenderingDevice::DataFormat p_color_format) {
  1083. if (p_ssr_buffers.roughness_quality != ssr_roughness_quality) {
  1084. // Buffers will already be cleared if view count or viewport size has changed, also cleared them if we change roughness.
  1085. p_render_buffers->clear_context(RB_SCOPE_SSR);
  1086. }
  1087. Size2i internal_size = p_render_buffers->get_internal_size();
  1088. p_ssr_buffers.size = Size2i(internal_size.x / 2, internal_size.y / 2);
  1089. p_ssr_buffers.roughness_quality = ssr_roughness_quality;
  1090. // We are using barriers so we do not need to allocate textures for both views on anything but output...
  1091. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_DEPTH_SCALED, RD::DATA_FORMAT_R32_SFLOAT, RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, 1);
  1092. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_NORMAL_SCALED, RD::DATA_FORMAT_R8G8B8A8_UNORM, RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, 1);
  1093. if (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED && !p_render_buffers->has_texture(RB_SCOPE_SSR, RB_BLUR_RADIUS)) {
  1094. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_BLUR_RADIUS, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, 2); // 2 layers, for our two blur stages
  1095. }
  1096. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_INTERMEDIATE, p_color_format, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, 1);
  1097. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_OUTPUT, p_color_format, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size);
  1098. }
  1099. void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RID *p_normal_roughness_slices, const RID *p_metallic_slices, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const Projection *p_projections, const Vector3 *p_eye_offsets) {
  1100. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1101. ERR_FAIL_NULL(uniform_set_cache);
  1102. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1103. ERR_FAIL_NULL(material_storage);
  1104. uint32_t view_count = p_render_buffers->get_view_count();
  1105. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1106. {
  1107. // Store some scene data in a UBO, in the near future we will use a UBO shared with other shaders
  1108. ScreenSpaceReflectionSceneData scene_data;
  1109. if (ssr.ubo.is_null()) {
  1110. ssr.ubo = RD::get_singleton()->uniform_buffer_create(sizeof(ScreenSpaceReflectionSceneData));
  1111. }
  1112. for (uint32_t v = 0; v < view_count; v++) {
  1113. store_camera(p_projections[v], scene_data.projection[v]);
  1114. store_camera(p_projections[v].inverse(), scene_data.inv_projection[v]);
  1115. scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
  1116. scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
  1117. scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
  1118. scene_data.eye_offset[v][3] = 0.0;
  1119. }
  1120. RD::get_singleton()->buffer_update(ssr.ubo, 0, sizeof(ScreenSpaceReflectionSceneData), &scene_data);
  1121. }
  1122. uint32_t pipeline_specialization = 0;
  1123. if (view_count > 1) {
  1124. pipeline_specialization |= SSR_MULTIVIEW;
  1125. }
  1126. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1127. for (uint32_t v = 0; v < view_count; v++) {
  1128. // get buffers we need to use for this view
  1129. RID diffuse_slice = p_render_buffers->get_internal_texture(v);
  1130. RID depth_slice = p_render_buffers->get_depth_texture(v);
  1131. RID depth_scaled = p_render_buffers->get_texture(RB_SCOPE_SSR, RB_DEPTH_SCALED);
  1132. RID normal_scaled = p_render_buffers->get_texture(RB_SCOPE_SSR, RB_NORMAL_SCALED);
  1133. RID intermediate = p_render_buffers->get_texture(RB_SCOPE_SSR, RB_INTERMEDIATE);
  1134. RID output = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_OUTPUT, v, 0);
  1135. RID blur_radius[2];
  1136. if (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  1137. blur_radius[0] = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_BLUR_RADIUS, 0, 0);
  1138. blur_radius[1] = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_BLUR_RADIUS, 1, 0);
  1139. }
  1140. RD::get_singleton()->draw_command_begin_label(String("SSR View ") + itos(v));
  1141. { //scale color and depth to half
  1142. RD::get_singleton()->draw_command_begin_label("SSR Scale");
  1143. ScreenSpaceReflectionScalePushConstant push_constant;
  1144. push_constant.view_index = v;
  1145. push_constant.camera_z_far = p_projections[v].get_z_far();
  1146. push_constant.camera_z_near = p_projections[v].get_z_near();
  1147. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1148. push_constant.filter = false; // Enabling causes artifacts.
  1149. push_constant.screen_size[0] = p_ssr_buffers.size.x;
  1150. push_constant.screen_size[1] = p_ssr_buffers.size.y;
  1151. RID shader = ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0);
  1152. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipelines[pipeline_specialization]);
  1153. RD::Uniform u_diffuse(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, diffuse_slice }));
  1154. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_diffuse), 0);
  1155. RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, depth_slice }));
  1156. RD::Uniform u_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_normal_roughness_slices[v] }));
  1157. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth, u_normal_roughness), 1);
  1158. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ intermediate }));
  1159. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_intermediate), 2);
  1160. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ depth_scaled }));
  1161. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ normal_scaled }));
  1162. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth, u_scale_normal), 3);
  1163. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  1164. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssr_buffers.size.width, p_ssr_buffers.size.height, 1);
  1165. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1166. RD::get_singleton()->draw_command_end_label();
  1167. }
  1168. {
  1169. RD::get_singleton()->draw_command_begin_label("SSR main");
  1170. ScreenSpaceReflectionPushConstant push_constant;
  1171. push_constant.view_index = v;
  1172. push_constant.camera_z_far = p_projections[v].get_z_far();
  1173. push_constant.camera_z_near = p_projections[v].get_z_near();
  1174. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1175. push_constant.screen_size[0] = p_ssr_buffers.size.x;
  1176. push_constant.screen_size[1] = p_ssr_buffers.size.y;
  1177. push_constant.curve_fade_in = p_fade_in;
  1178. push_constant.distance_fade = p_fade_out;
  1179. push_constant.num_steps = p_max_steps;
  1180. push_constant.depth_tolerance = p_tolerance;
  1181. push_constant.use_half_res = true;
  1182. push_constant.proj_info[0] = -2.0f / (p_ssr_buffers.size.width * p_projections[v].columns[0][0]);
  1183. push_constant.proj_info[1] = -2.0f / (p_ssr_buffers.size.height * p_projections[v].columns[1][1]);
  1184. push_constant.proj_info[2] = (1.0f - p_projections[v].columns[0][2]) / p_projections[v].columns[0][0];
  1185. push_constant.proj_info[3] = (1.0f + p_projections[v].columns[1][2]) / p_projections[v].columns[1][1];
  1186. ScreenSpaceReflectionMode mode = (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL;
  1187. RID shader = ssr.shader.version_get_shader(ssr.shader_version, mode);
  1188. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.pipelines[pipeline_specialization][mode]);
  1189. RD::Uniform u_scene_data(RD::UNIFORM_TYPE_UNIFORM_BUFFER, 0, ssr.ubo);
  1190. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1191. // read from intermediate
  1192. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ intermediate }));
  1193. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ depth_scaled }));
  1194. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_intermediate, u_scale_depth), 0);
  1195. if (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  1196. // write to output and blur radius
  1197. RD::Uniform u_output(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ output }));
  1198. RD::Uniform u_blur_radius(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ blur_radius[0] }));
  1199. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_output, u_blur_radius), 1);
  1200. } else {
  1201. // We are only writing output
  1202. RD::Uniform u_output(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ output }));
  1203. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_output), 1);
  1204. }
  1205. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ normal_scaled }));
  1206. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_scale_normal), 2);
  1207. RD::Uniform u_metallic(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_metallic_slices[v] }));
  1208. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_metallic), 3);
  1209. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  1210. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssr_buffers.size.width, p_ssr_buffers.size.height, 1);
  1211. RD::get_singleton()->draw_command_end_label();
  1212. }
  1213. if (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  1214. RD::get_singleton()->draw_command_begin_label("SSR filter");
  1215. //blur
  1216. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1217. ScreenSpaceReflectionFilterPushConstant push_constant;
  1218. push_constant.view_index = v;
  1219. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1220. push_constant.edge_tolerance = Math::sin(Math::deg_to_rad(15.0));
  1221. push_constant.proj_info[0] = -2.0f / (p_ssr_buffers.size.width * p_projections[v].columns[0][0]);
  1222. push_constant.proj_info[1] = -2.0f / (p_ssr_buffers.size.height * p_projections[v].columns[1][1]);
  1223. push_constant.proj_info[2] = (1.0f - p_projections[v].columns[0][2]) / p_projections[v].columns[0][0];
  1224. push_constant.proj_info[3] = (1.0f + p_projections[v].columns[1][2]) / p_projections[v].columns[1][1];
  1225. push_constant.vertical = 0;
  1226. if (ssr_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_LOW) {
  1227. push_constant.steps = p_max_steps / 3;
  1228. push_constant.increment = 3;
  1229. } else if (ssr_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_MEDIUM) {
  1230. push_constant.steps = p_max_steps / 2;
  1231. push_constant.increment = 2;
  1232. } else {
  1233. push_constant.steps = p_max_steps;
  1234. push_constant.increment = 1;
  1235. }
  1236. push_constant.screen_size[0] = p_ssr_buffers.size.width;
  1237. push_constant.screen_size[1] = p_ssr_buffers.size.height;
  1238. // Horizontal pass
  1239. SSRReflectionMode mode = SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL;
  1240. RID shader = ssr_filter.shader.version_get_shader(ssr_filter.shader_version, mode);
  1241. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[pipeline_specialization][mode]);
  1242. RD::Uniform u_output(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ output }));
  1243. RD::Uniform u_blur_radius(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ blur_radius[0] }));
  1244. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_output, u_blur_radius), 0);
  1245. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ normal_scaled }));
  1246. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_scale_normal), 1);
  1247. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ intermediate }));
  1248. RD::Uniform u_blur_radius2(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ blur_radius[1] }));
  1249. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_intermediate, u_blur_radius2), 2);
  1250. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ depth_scaled }));
  1251. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth), 3);
  1252. RD::Uniform u_scene_data(RD::UNIFORM_TYPE_UNIFORM_BUFFER, 0, ssr.ubo);
  1253. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1254. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  1255. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssr_buffers.size.width, p_ssr_buffers.size.height, 1);
  1256. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1257. // Vertical pass
  1258. mode = SCREEN_SPACE_REFLECTION_FILTER_VERTICAL;
  1259. shader = ssr_filter.shader.version_get_shader(ssr_filter.shader_version, mode);
  1260. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[pipeline_specialization][mode]);
  1261. push_constant.vertical = 1;
  1262. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_intermediate, u_blur_radius2), 0);
  1263. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_scale_normal), 1);
  1264. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_output), 2);
  1265. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth), 3);
  1266. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1267. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  1268. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssr_buffers.size.width, p_ssr_buffers.size.height, 1);
  1269. if (v != view_count - 1) {
  1270. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1271. }
  1272. RD::get_singleton()->draw_command_end_label();
  1273. }
  1274. RD::get_singleton()->draw_command_end_label();
  1275. }
  1276. RD::get_singleton()->compute_list_end();
  1277. }
  1278. /* Subsurface scattering */
  1279. void SSEffects::sss_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  1280. sss_quality = p_quality;
  1281. }
  1282. RS::SubSurfaceScatteringQuality SSEffects::sss_get_quality() const {
  1283. return sss_quality;
  1284. }
  1285. void SSEffects::sss_set_scale(float p_scale, float p_depth_scale) {
  1286. sss_scale = p_scale;
  1287. sss_depth_scale = p_depth_scale;
  1288. }
  1289. void SSEffects::sub_surface_scattering(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_diffuse, RID p_depth, const Projection &p_camera, const Size2i &p_screen_size) {
  1290. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1291. ERR_FAIL_NULL(uniform_set_cache);
  1292. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1293. ERR_FAIL_NULL(material_storage);
  1294. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1295. // Our intermediate buffer is only created if we haven't created it already.
  1296. RD::DataFormat format = p_render_buffers->get_base_data_format();
  1297. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1298. uint32_t layers = 1; // We only need one layer, we're handling one view at a time
  1299. uint32_t mipmaps = 1; // Image::get_image_required_mipmaps(p_screen_size.x, p_screen_size.y, Image::FORMAT_RGBAH);
  1300. RID intermediate = p_render_buffers->create_texture(SNAME("SSR"), SNAME("intermediate"), format, usage_bits, RD::TEXTURE_SAMPLES_1, p_screen_size, layers, mipmaps);
  1301. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  1302. p.normal /= p.d;
  1303. float unit_size = p.normal.x;
  1304. { //scale color and depth to half
  1305. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1306. sss.push_constant.camera_z_far = p_camera.get_z_far();
  1307. sss.push_constant.camera_z_near = p_camera.get_z_near();
  1308. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  1309. sss.push_constant.unit_size = unit_size;
  1310. sss.push_constant.screen_size[0] = p_screen_size.x;
  1311. sss.push_constant.screen_size[1] = p_screen_size.y;
  1312. sss.push_constant.vertical = false;
  1313. sss.push_constant.scale = sss_scale;
  1314. sss.push_constant.depth_scale = sss_depth_scale;
  1315. RID shader = sss.shader.version_get_shader(sss.shader_version, sss_quality - 1);
  1316. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[sss_quality - 1]);
  1317. RD::Uniform u_diffuse_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_diffuse }));
  1318. RD::Uniform u_diffuse(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_diffuse }));
  1319. RD::Uniform u_intermediate_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, intermediate }));
  1320. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ intermediate }));
  1321. RD::Uniform u_depth_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_depth }));
  1322. // horizontal
  1323. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_diffuse_with_sampler), 0);
  1324. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_intermediate), 1);
  1325. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_depth_with_sampler), 2);
  1326. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  1327. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1328. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1329. // vertical
  1330. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_intermediate_with_sampler), 0);
  1331. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_diffuse), 1);
  1332. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_depth_with_sampler), 2);
  1333. sss.push_constant.vertical = true;
  1334. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  1335. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1336. RD::get_singleton()->compute_list_end();
  1337. }
  1338. }