ss_effects.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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/effects/copy_effects.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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(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].create_compute_pipeline(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  215. pipeline++;
  216. }
  217. }
  218. ERR_FAIL_COND(pipeline != SSAO_MAX);
  219. ss_effects.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  220. }
  221. // Screen Space Reflections
  222. ssr_half_size = GLOBAL_GET("rendering/environment/screen_space_reflection/half_size");
  223. {
  224. {
  225. Vector<String> ssr_downsample_modes;
  226. ssr_downsample_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_DOWNSAMPLE_DEFAULT
  227. ssr_downsample_modes.push_back("\n#define MODE_ODD_WIDTH\n"); // SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH
  228. ssr_downsample_modes.push_back("\n#define MODE_ODD_HEIGHT\n"); // SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_HEIGHT
  229. ssr_downsample_modes.push_back("\n#define MODE_ODD_WIDTH\n#define MODE_ODD_HEIGHT\n"); // SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH_AND_HEIGHT
  230. ssr.downsample_shader.initialize(ssr_downsample_modes);
  231. ssr.downsample_shader_version = ssr.downsample_shader.version_create();
  232. for (uint32_t i = 0; i < SCREEN_SPACE_REFLECTION_DOWNSAMPLE_MAX; i++) {
  233. ssr.downsample_pipelines[i].create_compute_pipeline(ssr.downsample_shader.version_get_shader(ssr.downsample_shader_version, i));
  234. }
  235. }
  236. {
  237. Vector<String> ssr_hiz_modes;
  238. ssr_hiz_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_HIZ_DEFAULT
  239. ssr_hiz_modes.push_back("\n#define MODE_ODD_WIDTH\n"); // SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH
  240. ssr_hiz_modes.push_back("\n#define MODE_ODD_HEIGHT\n"); // SCREEN_SPACE_REFLECTION_HIZ_ODD_HEIGHT
  241. ssr_hiz_modes.push_back("\n#define MODE_ODD_WIDTH\n#define MODE_ODD_HEIGHT\n"); // SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH_AND_HEIGHT
  242. ssr.hiz_shader.initialize(ssr_hiz_modes);
  243. ssr.hiz_shader_version = ssr.hiz_shader.version_create();
  244. for (uint32_t i = 0; i < SCREEN_SPACE_REFLECTION_HIZ_MAX; i++) {
  245. ssr.hiz_pipelines[i].create_compute_pipeline(ssr.hiz_shader.version_get_shader(ssr.hiz_shader_version, i));
  246. }
  247. }
  248. {
  249. Vector<String> ssr_modes;
  250. ssr_modes.push_back("\n");
  251. ssr.ssr_shader.initialize(ssr_modes);
  252. ssr.ssr_shader_version = ssr.ssr_shader.version_create();
  253. ssr.ssr_pipeline.create_compute_pipeline(ssr.ssr_shader.version_get_shader(ssr.ssr_shader_version, 0));
  254. }
  255. {
  256. Vector<String> ssr_filter_modes;
  257. ssr_filter_modes.push_back("\n");
  258. ssr.filter_shader.initialize(ssr_filter_modes);
  259. ssr.filter_shader_version = ssr.filter_shader.version_create();
  260. ssr.filter_pipeline.create_compute_pipeline(ssr.filter_shader.version_get_shader(ssr.filter_shader_version, 0));
  261. }
  262. {
  263. Vector<String> ssr_resolve_modes;
  264. ssr_resolve_modes.push_back("\n");
  265. ssr.resolve_shader.initialize(ssr_resolve_modes);
  266. ssr.resolve_shader_version = ssr.resolve_shader.version_create();
  267. ssr.resolve_pipeline.create_compute_pipeline(ssr.resolve_shader.version_get_shader(ssr.resolve_shader_version, 0));
  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 < SUBSURFACE_SCATTERING_MODE_MAX; i++) {
  282. sss.pipelines[i].create_compute_pipeline(sss.shader.version_get_shader(sss.shader_version, i));
  283. }
  284. }
  285. }
  286. void SSEffects::allocate_last_frame_buffer(Ref<RenderSceneBuffersRD> p_render_buffers, bool p_use_ssil, bool p_use_ssr) {
  287. Size2i last_frame_size = p_render_buffers->get_internal_size();
  288. uint32_t mipmaps = 1;
  289. uint32_t view_count = p_render_buffers->get_view_count();
  290. if (!p_use_ssil && p_use_ssr && ssr_half_size) {
  291. last_frame_size /= 2;
  292. }
  293. if (p_use_ssil) {
  294. mipmaps = 6;
  295. }
  296. bool should_create = true;
  297. bool has_texture = p_render_buffers->has_texture(RB_SCOPE_SSLF, RB_LAST_FRAME);
  298. if (has_texture) {
  299. RID last_frame_texture = p_render_buffers->get_texture(RB_SCOPE_SSLF, RB_LAST_FRAME);
  300. RD::TextureFormat texture_format = RD::get_singleton()->texture_get_format(last_frame_texture);
  301. should_create = texture_format.width != (uint32_t)last_frame_size.width || texture_format.height != (uint32_t)last_frame_size.height || texture_format.mipmaps != mipmaps || texture_format.array_layers != view_count;
  302. }
  303. if (should_create) {
  304. if (has_texture) {
  305. p_render_buffers->clear_context(RB_SCOPE_SSLF);
  306. }
  307. RID last_frame_texture = p_render_buffers->create_texture(RB_SCOPE_SSLF, 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, last_frame_size, view_count, mipmaps);
  308. RD::get_singleton()->texture_clear(last_frame_texture, Color(0, 0, 0, 0), 0, mipmaps, 0, view_count);
  309. }
  310. }
  311. void SSEffects::copy_internal_texture_to_last_frame(Ref<RenderSceneBuffersRD> p_render_buffers, CopyEffects &p_copy_effects) {
  312. uint32_t mipmaps = p_render_buffers->get_texture_format(RB_SCOPE_SSLF, RB_LAST_FRAME).mipmaps;
  313. for (uint32_t v = 0; v < p_render_buffers->get_view_count(); v++) {
  314. for (uint32_t m = 0; m < mipmaps; m++) {
  315. RID source;
  316. if (m == 0) {
  317. source = p_render_buffers->get_internal_texture(v);
  318. } else {
  319. source = p_render_buffers->get_texture_slice(RB_SCOPE_SSLF, RB_LAST_FRAME, v, m - 1);
  320. }
  321. RID dest = p_render_buffers->get_texture_slice(RB_SCOPE_SSLF, RB_LAST_FRAME, v, m);
  322. Size2i source_size = RD::get_singleton()->texture_size(source);
  323. Size2i dest_size = RD::get_singleton()->texture_size(dest);
  324. if (m == 0 && source_size == dest_size) {
  325. p_copy_effects.copy_to_rect(source, dest, Rect2i(Vector2i(), source_size), false, false, false, false, false, true);
  326. } else {
  327. p_copy_effects.make_mipmap(source, dest, dest_size);
  328. }
  329. }
  330. }
  331. }
  332. SSEffects::~SSEffects() {
  333. {
  334. // Cleanup SS Reflections
  335. for (int i = 0; i < SCREEN_SPACE_REFLECTION_DOWNSAMPLE_MAX; i++) {
  336. ssr.downsample_pipelines[i].free();
  337. }
  338. for (int i = 0; i < SCREEN_SPACE_REFLECTION_HIZ_MAX; i++) {
  339. ssr.hiz_pipelines[i].free();
  340. }
  341. ssr.ssr_pipeline.free();
  342. ssr.filter_pipeline.free();
  343. ssr.resolve_pipeline.free();
  344. ssr.downsample_shader.version_free(ssr.downsample_shader_version);
  345. ssr.hiz_shader.version_free(ssr.hiz_shader_version);
  346. ssr.ssr_shader.version_free(ssr.ssr_shader_version);
  347. ssr.filter_shader.version_free(ssr.filter_shader_version);
  348. ssr.resolve_shader.version_free(ssr.resolve_shader_version);
  349. if (ssr.ubo.is_valid()) {
  350. RD::get_singleton()->free_rid(ssr.ubo);
  351. }
  352. }
  353. {
  354. // Cleanup SS downsampler
  355. for (int i = 0; i < SS_EFFECTS_MAX; i++) {
  356. ss_effects.pipelines[i].free();
  357. }
  358. ss_effects.downsample_shader.version_free(ss_effects.downsample_shader_version);
  359. RD::get_singleton()->free_rid(ss_effects.mirror_sampler);
  360. RD::get_singleton()->free_rid(ss_effects.gather_constants_buffer);
  361. }
  362. {
  363. // Cleanup SSIL
  364. for (int i = 0; i < SSIL_MAX; i++) {
  365. ssil.pipelines[i].free();
  366. }
  367. ssil.blur_shader.version_free(ssil.blur_shader_version);
  368. ssil.gather_shader.version_free(ssil.gather_shader_version);
  369. ssil.interleave_shader.version_free(ssil.interleave_shader_version);
  370. ssil.importance_map_shader.version_free(ssil.importance_map_shader_version);
  371. RD::get_singleton()->free_rid(ssil.importance_map_load_counter);
  372. RD::get_singleton()->free_rid(ssil.projection_uniform_buffer);
  373. }
  374. {
  375. // Cleanup SSAO
  376. for (int i = 0; i < SSAO_MAX; i++) {
  377. ssao.pipelines[i].free();
  378. }
  379. ssao.blur_shader.version_free(ssao.blur_shader_version);
  380. ssao.gather_shader.version_free(ssao.gather_shader_version);
  381. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  382. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  383. RD::get_singleton()->free_rid(ssao.importance_map_load_counter);
  384. }
  385. {
  386. // Cleanup Subsurface scattering
  387. for (int i = 0; i < SUBSURFACE_SCATTERING_MODE_MAX; i++) {
  388. sss.pipelines[i].free();
  389. }
  390. sss.shader.version_free(sss.shader_version);
  391. }
  392. singleton = nullptr;
  393. }
  394. /* SS Downsampler */
  395. void SSEffects::downsample_depth(Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_view, const Projection &p_projection) {
  396. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  397. ERR_FAIL_NULL(uniform_set_cache);
  398. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  399. ERR_FAIL_NULL(material_storage);
  400. uint32_t view_count = p_render_buffers->get_view_count();
  401. Size2i full_screen_size = p_render_buffers->get_internal_size();
  402. Size2i size((full_screen_size.x + 1) / 2, (full_screen_size.y + 1) / 2);
  403. // Make sure our buffers exist, buffers are automatically cleared if view count or size changes.
  404. if (!p_render_buffers->has_texture(RB_SCOPE_SSDS, RB_LINEAR_DEPTH)) {
  405. 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);
  406. }
  407. // Downsample and deinterleave the depth buffer for SSAO and SSIL
  408. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  409. int downsample_mode = SS_EFFECTS_DOWNSAMPLE;
  410. bool use_mips = ssao_quality > RS::ENV_SSAO_QUALITY_MEDIUM || ssil_quality > RS::ENV_SSIL_QUALITY_MEDIUM;
  411. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW && ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  412. downsample_mode = SS_EFFECTS_DOWNSAMPLE_HALF;
  413. } else if (use_mips) {
  414. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  415. }
  416. bool use_half_size = false;
  417. bool use_full_mips = false;
  418. if (ssao_half_size && ssil_half_size) {
  419. downsample_mode++;
  420. use_half_size = true;
  421. } else if (ssao_half_size != ssil_half_size) {
  422. if (use_mips) {
  423. downsample_mode = SS_EFFECTS_DOWNSAMPLE_FULL_MIPS;
  424. use_full_mips = true;
  425. } else {
  426. // Only need the first two mipmaps, but the cost to generate the next two is trivial
  427. // TODO investigate the benefit of a shader version to generate only 2 mips
  428. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  429. use_mips = true;
  430. }
  431. }
  432. RID shader = ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, downsample_mode);
  433. int depth_index = use_half_size ? 1 : 0;
  434. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  435. RID downsample_uniform_set;
  436. if (use_mips) {
  437. // Grab our downsample uniform set from cache, these are automatically cleaned up if the depth textures are cleared.
  438. // This also ensures we can switch between left eye and right eye uniform sets without recreating the uniform twice a frame.
  439. thread_local LocalVector<RD::Uniform> u_depths;
  440. u_depths.clear();
  441. // Note, use_full_mips is true if either SSAO or SSIL uses half size, but the other full size and we're using mips.
  442. // That means we're filling all 5 levels.
  443. // In this scenario `depth_index` will be 0.
  444. for (int i = 0; i < (use_full_mips ? 4 : 3); i++) {
  445. RID depth_mipmap = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, depth_index + i + 1, 4, 1);
  446. RD::Uniform u_depth;
  447. u_depth.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  448. u_depth.binding = i;
  449. u_depth.append_id(depth_mipmap);
  450. u_depths.push_back(u_depth);
  451. }
  452. // This before only used SS_EFFECTS_DOWNSAMPLE_MIPMAP or SS_EFFECTS_DOWNSAMPLE_FULL_MIPS
  453. downsample_uniform_set = uniform_set_cache->get_cache_vec(shader, 2, u_depths);
  454. }
  455. Projection correction;
  456. correction.set_depth_correction(false);
  457. Projection temp = correction * p_projection;
  458. float depth_linearize_mul = -temp.columns[3][2];
  459. float depth_linearize_add = temp.columns[2][2];
  460. if (depth_linearize_mul * depth_linearize_add < 0) {
  461. depth_linearize_add = -depth_linearize_add;
  462. }
  463. ss_effects.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  464. ss_effects.downsample_push_constant.z_near = depth_linearize_mul;
  465. ss_effects.downsample_push_constant.z_far = depth_linearize_add;
  466. if (ss_effects.downsample_push_constant.orthogonal) {
  467. ss_effects.downsample_push_constant.z_near = p_projection.get_z_near();
  468. ss_effects.downsample_push_constant.z_far = p_projection.get_z_far();
  469. }
  470. ss_effects.downsample_push_constant.pixel_size[0] = 1.0 / full_screen_size.x;
  471. ss_effects.downsample_push_constant.pixel_size[1] = 1.0 / full_screen_size.y;
  472. ss_effects.downsample_push_constant.radius_sq = 1.0;
  473. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  474. RID depth_texture = p_render_buffers->get_depth_texture(p_view);
  475. RID depth_mipmap = p_render_buffers->get_texture_slice(RB_SCOPE_SSDS, RB_LINEAR_DEPTH, p_view * 4, depth_index, 4, 1);
  476. RD::Uniform u_depth_buffer(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, depth_texture }));
  477. RD::Uniform u_depth_mipmap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ depth_mipmap }));
  478. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ss_effects.pipelines[downsample_mode].get_rid());
  479. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_depth_buffer), 0);
  480. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_mipmap), 1);
  481. if (use_mips) {
  482. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, downsample_uniform_set, 2);
  483. }
  484. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ss_effects.downsample_push_constant, sizeof(SSEffectsDownsamplePushConstant));
  485. if (use_half_size) {
  486. size = Size2i(size.x >> 1, size.y >> 1).maxi(1);
  487. }
  488. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  489. RD::get_singleton()->compute_list_add_barrier(compute_list);
  490. RD::get_singleton()->draw_command_end_label();
  491. RD::get_singleton()->compute_list_end();
  492. ss_effects.used_full_mips_last_frame = use_full_mips;
  493. ss_effects.used_half_size_last_frame = use_half_size;
  494. ss_effects.used_mips_last_frame = use_mips;
  495. }
  496. /* SSIL */
  497. 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) {
  498. ssil_quality = p_quality;
  499. ssil_half_size = p_half_size;
  500. ssil_adaptive_target = p_adaptive_target;
  501. ssil_blur_passes = p_blur_passes;
  502. ssil_fadeout_from = p_fadeout_from;
  503. ssil_fadeout_to = p_fadeout_to;
  504. }
  505. 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) {
  506. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  507. ERR_FAIL_NULL(uniform_set_cache);
  508. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  509. if ((ssil_quality == RS::ENV_SSIL_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  510. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  511. }
  512. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_projection_uniform_set, 3);
  513. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0);
  514. for (int i = 0; i < 4; i++) {
  515. if ((ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  516. continue;
  517. }
  518. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_slices[i] }));
  519. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_edges_slices[i] }));
  520. ssil.gather_push_constant.pass_coord_offset[0] = i % 2;
  521. ssil.gather_push_constant.pass_coord_offset[1] = i / 2;
  522. ssil.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  523. ssil.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  524. ssil.gather_push_constant.pass = i;
  525. 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);
  526. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssil.gather_push_constant, sizeof(SSILGatherPushConstant));
  527. Size2i size;
  528. // Calculate size same way as we created the buffer
  529. if (ssil_half_size) {
  530. size.x = (p_settings.full_screen_size.x + 3) / 4;
  531. size.y = (p_settings.full_screen_size.y + 3) / 4;
  532. } else {
  533. size.x = (p_settings.full_screen_size.x + 1) / 2;
  534. size.y = (p_settings.full_screen_size.y + 1) / 2;
  535. }
  536. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  537. }
  538. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  539. }
  540. void SSEffects::ssil_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings) {
  541. if (p_ssil_buffers.half_size != ssil_half_size) {
  542. p_render_buffers->clear_context(RB_SCOPE_SSIL);
  543. }
  544. p_ssil_buffers.half_size = ssil_half_size;
  545. if (p_ssil_buffers.half_size) {
  546. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  547. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  548. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  549. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  550. } else {
  551. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  552. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  553. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  554. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  555. }
  556. uint32_t view_count = p_render_buffers->get_view_count();
  557. Size2i full_size = Size2i(p_ssil_buffers.buffer_width, p_ssil_buffers.buffer_height);
  558. Size2i half_size = Size2i(p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height);
  559. // We create our intermediate and final results as render buffers.
  560. // These are automatically cached and cleaned up when our viewport resizes
  561. // or when our viewport gets destroyed.
  562. 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...
  563. 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);
  564. RD::get_singleton()->texture_clear(final, Color(0, 0, 0, 0), 0, 1, 0, view_count);
  565. }
  566. // As we're not clearing these, and render buffers will return the cached texture if it already exists,
  567. // we don't first check has_texture here
  568. 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);
  569. 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);
  570. 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);
  571. 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);
  572. 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);
  573. }
  574. 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) {
  575. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  576. ERR_FAIL_NULL(uniform_set_cache);
  577. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  578. ERR_FAIL_NULL(material_storage);
  579. RD::get_singleton()->draw_command_begin_label("Process Screen-Space Indirect Lighting");
  580. // Obtain our (cached) buffer slices for the view we are rendering.
  581. RID last_frame = p_render_buffers->get_texture_slice(RB_SCOPE_SSLF, RB_LAST_FRAME, p_view, 0, 1, 6);
  582. RID deinterleaved = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED, p_view * 4, 0, 4, 1);
  583. RID deinterleaved_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED_PONG, 4 * p_view, 0, 4, 1);
  584. RID edges = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_EDGES, 4 * p_view, 0, 4, 1);
  585. RID importance_map = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_IMPORTANCE_MAP, p_view, 0);
  586. RID importance_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_IMPORTANCE_PONG, p_view, 0);
  587. RID deinterleaved_slices[4];
  588. RID deinterleaved_pong_slices[4];
  589. RID edges_slices[4];
  590. for (uint32_t i = 0; i < 4; i++) {
  591. deinterleaved_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED, p_view * 4 + i, 0);
  592. deinterleaved_pong_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_DEINTERLEAVED_PONG, p_view * 4 + i, 0);
  593. edges_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_EDGES, p_view * 4 + i, 0);
  594. }
  595. //Store projection info before starting the compute list
  596. SSILProjectionUniforms projection_uniforms;
  597. store_camera(p_last_projection, projection_uniforms.inv_last_frame_projection_matrix);
  598. RD::get_singleton()->buffer_update(ssil.projection_uniform_buffer, 0, sizeof(SSILProjectionUniforms), &projection_uniforms);
  599. memset(&ssil.gather_push_constant, 0, sizeof(SSILGatherPushConstant));
  600. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, SSIL_GATHER);
  601. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  602. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  603. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  604. {
  605. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  606. ssil.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  607. ssil.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  608. ssil.gather_push_constant.half_screen_pixel_size[0] = 2.0 / p_settings.full_screen_size.x;
  609. ssil.gather_push_constant.half_screen_pixel_size[1] = 2.0 / p_settings.full_screen_size.y;
  610. if (ssil_half_size) {
  611. ssil.gather_push_constant.half_screen_pixel_size[0] *= 2.0;
  612. ssil.gather_push_constant.half_screen_pixel_size[1] *= 2.0;
  613. }
  614. ssil.gather_push_constant.half_screen_pixel_size_x025[0] = ssil.gather_push_constant.half_screen_pixel_size[0] * 0.75;
  615. ssil.gather_push_constant.half_screen_pixel_size_x025[1] = ssil.gather_push_constant.half_screen_pixel_size[1] * 0.75;
  616. float tan_half_fov_x = 1.0 / p_projection.columns[0][0];
  617. float tan_half_fov_y = 1.0 / p_projection.columns[1][1];
  618. ssil.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  619. ssil.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  620. ssil.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  621. ssil.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  622. ssil.gather_push_constant.z_near = p_projection.get_z_near();
  623. ssil.gather_push_constant.z_far = p_projection.get_z_far();
  624. ssil.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  625. ssil.gather_push_constant.radius = p_settings.radius;
  626. float radius_near_limit = (p_settings.radius * 1.2f);
  627. if (ssil_quality <= RS::ENV_SSIL_QUALITY_LOW) {
  628. radius_near_limit *= 1.50f;
  629. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  630. ssil.gather_push_constant.radius *= 0.8f;
  631. }
  632. }
  633. radius_near_limit /= tan_half_fov_y;
  634. ssil.gather_push_constant.intensity = p_settings.intensity * Math::PI;
  635. ssil.gather_push_constant.fade_out_mul = -1.0 / (ssil_fadeout_to - ssil_fadeout_from);
  636. ssil.gather_push_constant.fade_out_add = ssil_fadeout_from / (ssil_fadeout_to - ssil_fadeout_from) + 1.0;
  637. ssil.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  638. ssil.gather_push_constant.neg_inv_radius = -1.0 / ssil.gather_push_constant.radius;
  639. ssil.gather_push_constant.normal_rejection_amount = p_settings.normal_rejection;
  640. ssil.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssil_buffers.half_buffer_width) * (p_ssil_buffers.half_buffer_height) * 255);
  641. ssil.gather_push_constant.adaptive_sample_limit = ssil_adaptive_target;
  642. ssil.gather_push_constant.quality = MAX(0, ssil_quality - 1);
  643. ssil.gather_push_constant.size_multiplier = ssil_half_size ? 2 : 1;
  644. // We are using our uniform cache so our uniform sets are automatically freed when our textures are freed.
  645. // It also ensures that we're reusing the right cached entry in a multiview situation without us having to
  646. // remember each instance of the uniform set.
  647. RID projection_uniform_set;
  648. {
  649. RD::Uniform u_last_frame;
  650. u_last_frame.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  651. u_last_frame.binding = 0;
  652. u_last_frame.append_id(default_mipmap_sampler);
  653. u_last_frame.append_id(last_frame);
  654. RD::Uniform u_projection;
  655. u_projection.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  656. u_projection.binding = 1;
  657. u_projection.append_id(ssil.projection_uniform_buffer);
  658. projection_uniform_set = uniform_set_cache->get_cache(shader, 3, u_last_frame, u_projection);
  659. }
  660. RID gather_uniform_set;
  661. {
  662. 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);
  663. RD::Uniform u_depth_texture_view;
  664. u_depth_texture_view.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  665. u_depth_texture_view.binding = 0;
  666. u_depth_texture_view.append_id(ss_effects.mirror_sampler);
  667. u_depth_texture_view.append_id(depth_texture_view);
  668. RD::Uniform u_normal_buffer;
  669. u_normal_buffer.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  670. u_normal_buffer.binding = 1;
  671. u_normal_buffer.append_id(p_normal_buffer);
  672. RD::Uniform u_gather_constants_buffer;
  673. u_gather_constants_buffer.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  674. u_gather_constants_buffer.binding = 2;
  675. u_gather_constants_buffer.append_id(ss_effects.gather_constants_buffer);
  676. gather_uniform_set = uniform_set_cache->get_cache(shader, 0, u_depth_texture_view, u_normal_buffer, u_gather_constants_buffer);
  677. }
  678. RID importance_map_uniform_set;
  679. {
  680. RD::Uniform u_pong;
  681. u_pong.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  682. u_pong.binding = 0;
  683. u_pong.append_id(deinterleaved_pong);
  684. RD::Uniform u_importance_map;
  685. u_importance_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  686. u_importance_map.binding = 1;
  687. u_importance_map.append_id(default_sampler);
  688. u_importance_map.append_id(importance_map);
  689. RD::Uniform u_load_counter;
  690. u_load_counter.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  691. u_load_counter.binding = 2;
  692. u_load_counter.append_id(ssil.importance_map_load_counter);
  693. RID shader_adaptive = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, SSIL_GATHER_ADAPTIVE);
  694. importance_map_uniform_set = uniform_set_cache->get_cache(shader_adaptive, 1, u_pong, u_importance_map, u_load_counter);
  695. }
  696. if (ssil_quality == RS::ENV_SSIL_QUALITY_ULTRA) {
  697. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  698. ssil.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  699. ssil.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  700. ssil.importance_map_push_constant.intensity = p_settings.intensity * Math::PI;
  701. //base pass
  702. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_BASE].get_rid());
  703. gather_ssil(compute_list, deinterleaved_pong_slices, edges_slices, p_settings, true, gather_uniform_set, importance_map_uniform_set, projection_uniform_set);
  704. //generate importance map
  705. RID gen_imp_shader = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 0);
  706. RD::Uniform u_ssil_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong }));
  707. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_map }));
  708. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GENERATE_IMPORTANCE_MAP].get_rid());
  709. 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);
  710. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 1, u_importance_map), 1);
  711. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  712. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  713. RD::get_singleton()->compute_list_add_barrier(compute_list);
  714. // process Importance Map A
  715. RID proc_imp_shader_a = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 1);
  716. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_map }));
  717. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_pong }));
  718. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPA].get_rid());
  719. 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);
  720. 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);
  721. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  722. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  723. RD::get_singleton()->compute_list_add_barrier(compute_list);
  724. // process Importance Map B
  725. RID proc_imp_shader_b = ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 2);
  726. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_pong }));
  727. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPB].get_rid());
  728. 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);
  729. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 1, u_importance_map), 1);
  730. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssil.counter_uniform_set, 2);
  731. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  732. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  733. RD::get_singleton()->compute_list_add_barrier(compute_list);
  734. RD::get_singleton()->draw_command_end_label(); // Importance Map
  735. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_ADAPTIVE].get_rid());
  736. } else {
  737. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER].get_rid());
  738. }
  739. gather_ssil(compute_list, deinterleaved_slices, edges_slices, p_settings, false, gather_uniform_set, importance_map_uniform_set, projection_uniform_set);
  740. RD::get_singleton()->draw_command_end_label(); //Gather
  741. }
  742. {
  743. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  744. ssil.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  745. ssil.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  746. ssil.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  747. int blur_passes = ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW ? ssil_blur_passes : 1;
  748. shader = ssil.blur_shader.version_get_shader(ssil.blur_shader_version, 0);
  749. for (int pass = 0; pass < blur_passes; pass++) {
  750. int blur_pipeline = SSIL_BLUR_PASS;
  751. if (ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  752. blur_pipeline = SSIL_BLUR_PASS_SMART;
  753. if (pass < blur_passes - 2) {
  754. blur_pipeline = SSIL_BLUR_PASS_WIDE;
  755. }
  756. }
  757. RID blur_shader = ssil.blur_shader.version_get_shader(ssil.blur_shader_version, blur_pipeline - SSIL_BLUR_PASS);
  758. for (int i = 0; i < 4; i++) {
  759. if ((ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  760. continue;
  761. }
  762. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[blur_pipeline].get_rid());
  763. if (pass % 2 == 0) {
  764. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  765. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_slices[i] }));
  766. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_slice), 0);
  767. } else {
  768. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, deinterleaved_slices[i] }));
  769. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_slice), 0);
  770. }
  771. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ deinterleaved_pong_slices[i] }));
  772. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ssil_pong_slice), 1);
  773. } else {
  774. if (ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  775. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong_slices[i] }));
  776. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_pong_slice), 0);
  777. } else {
  778. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, deinterleaved_pong_slices[i] }));
  779. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ssil_pong_slice), 0);
  780. }
  781. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ deinterleaved_slices[i] }));
  782. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ssil_slice), 1);
  783. }
  784. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ edges_slices[i] }));
  785. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 2, u_edges_slice), 2);
  786. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.blur_push_constant, sizeof(SSILBlurPushConstant));
  787. // Use the size of the actual buffer we're processing here or we won't cover the entire image.
  788. int x_groups = p_ssil_buffers.buffer_width;
  789. int y_groups = p_ssil_buffers.buffer_height;
  790. RD::get_singleton()->compute_list_dispatch_threads(compute_list, x_groups, y_groups, 1);
  791. }
  792. RD::get_singleton()->compute_list_add_barrier(compute_list);
  793. }
  794. RD::get_singleton()->draw_command_end_label(); // Blur
  795. }
  796. {
  797. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  798. ssil.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  799. ssil.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  800. ssil.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  801. ssil.interleave_push_constant.size_modifier = uint32_t(ssil_half_size ? 4 : 2);
  802. int interleave_pipeline = SSIL_INTERLEAVE_HALF;
  803. if (ssil_quality == RS::ENV_SSIL_QUALITY_LOW) {
  804. interleave_pipeline = SSIL_INTERLEAVE;
  805. } else if (ssil_quality >= RS::ENV_SSIL_QUALITY_MEDIUM) {
  806. interleave_pipeline = SSIL_INTERLEAVE_SMART;
  807. }
  808. shader = ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, 0);
  809. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[interleave_pipeline].get_rid());
  810. RID final = p_render_buffers->get_texture_slice(RB_SCOPE_SSIL, RB_FINAL, p_view, 0);
  811. RD::Uniform u_destination(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ final }));
  812. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_destination), 0);
  813. if (ssil_quality > RS::ENV_SSIL_QUALITY_VERY_LOW && ssil_blur_passes % 2 == 0) {
  814. RD::Uniform u_ssil(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved }));
  815. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil), 1);
  816. } else {
  817. RD::Uniform u_ssil_pong(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, deinterleaved_pong }));
  818. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil_pong), 1);
  819. }
  820. RD::Uniform u_edges(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ edges }));
  821. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_edges), 2);
  822. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.interleave_push_constant, sizeof(SSILInterleavePushConstant));
  823. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  824. RD::get_singleton()->compute_list_add_barrier(compute_list);
  825. RD::get_singleton()->draw_command_end_label(); // Interleave
  826. }
  827. RD::get_singleton()->draw_command_end_label(); // SSIL
  828. RD::get_singleton()->compute_list_end();
  829. int zero[1] = { 0 };
  830. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  831. }
  832. /* SSAO */
  833. 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) {
  834. ssao_quality = p_quality;
  835. ssao_half_size = p_half_size;
  836. ssao_adaptive_target = p_adaptive_target;
  837. ssao_blur_passes = p_blur_passes;
  838. ssao_fadeout_from = p_fadeout_from;
  839. ssao_fadeout_to = p_fadeout_to;
  840. }
  841. 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) {
  842. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  843. ERR_FAIL_NULL(uniform_set_cache);
  844. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  845. if ((ssao_quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  846. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  847. }
  848. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 1); //
  849. for (int i = 0; i < 4; i++) {
  850. if ((ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  851. continue;
  852. }
  853. RD::Uniform u_ao_slice(RD::UNIFORM_TYPE_IMAGE, 0, p_ao_slices[i]);
  854. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  855. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  856. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  857. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  858. ssao.gather_push_constant.pass = i;
  859. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, uniform_set_cache->get_cache(shader, 2, u_ao_slice), 2);
  860. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  861. Size2i size;
  862. // Make sure we use the same size as with which our buffer was created
  863. if (ssao_half_size) {
  864. size.x = (p_settings.full_screen_size.x + 3) / 4;
  865. size.y = (p_settings.full_screen_size.y + 3) / 4;
  866. } else {
  867. size.x = (p_settings.full_screen_size.x + 1) / 2;
  868. size.y = (p_settings.full_screen_size.y + 1) / 2;
  869. }
  870. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  871. }
  872. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  873. }
  874. void SSEffects::ssao_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings) {
  875. if (p_ssao_buffers.half_size != ssao_half_size) {
  876. p_render_buffers->clear_context(RB_SCOPE_SSAO);
  877. }
  878. p_ssao_buffers.half_size = ssao_half_size;
  879. if (ssao_half_size) {
  880. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  881. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  882. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  883. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  884. } else {
  885. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  886. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  887. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  888. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  889. }
  890. uint32_t view_count = p_render_buffers->get_view_count();
  891. Size2i full_size = Size2i(p_ssao_buffers.buffer_width, p_ssao_buffers.buffer_height);
  892. Size2i half_size = Size2i(p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height);
  893. // As we're not clearing these, and render buffers will return the cached texture if it already exists,
  894. // we don't first check has_texture here
  895. 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);
  896. 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);
  897. 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);
  898. 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);
  899. 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);
  900. }
  901. 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) {
  902. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  903. ERR_FAIL_NULL(uniform_set_cache);
  904. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  905. ERR_FAIL_NULL(material_storage);
  906. // Obtain our (cached) buffer slices for the view we are rendering.
  907. RID ao_deinterleaved = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED, p_view * 4, 0, 4, 1);
  908. RID ao_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED_PONG, p_view * 4, 0, 4, 1);
  909. RID importance_map = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_IMPORTANCE_MAP, p_view, 0);
  910. RID importance_pong = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_IMPORTANCE_PONG, p_view, 0);
  911. RID ao_final = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_FINAL, p_view, 0);
  912. RID ao_deinterleaved_slices[4];
  913. RID ao_pong_slices[4];
  914. for (uint32_t i = 0; i < 4; i++) {
  915. ao_deinterleaved_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED, p_view * 4 + i, 0);
  916. ao_pong_slices[i] = p_render_buffers->get_texture_slice(RB_SCOPE_SSAO, RB_DEINTERLEAVED_PONG, p_view * 4 + i, 0);
  917. }
  918. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  919. memset(&ssao.gather_push_constant, 0, sizeof(SSAOGatherPushConstant));
  920. /* FIRST PASS */
  921. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, SSAO_GATHER);
  922. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  923. RD::get_singleton()->draw_command_begin_label("Process Screen-Space Ambient Occlusion");
  924. /* SECOND PASS */
  925. // Sample SSAO
  926. {
  927. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  928. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  929. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  930. ssao.gather_push_constant.half_screen_pixel_size[0] = 2.0 / p_settings.full_screen_size.x;
  931. ssao.gather_push_constant.half_screen_pixel_size[1] = 2.0 / p_settings.full_screen_size.y;
  932. if (ssao_half_size) {
  933. ssao.gather_push_constant.half_screen_pixel_size[0] *= 2.0;
  934. ssao.gather_push_constant.half_screen_pixel_size[1] *= 2.0;
  935. }
  936. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.75;
  937. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.75;
  938. float tan_half_fov_x = 1.0 / p_projection.columns[0][0];
  939. float tan_half_fov_y = 1.0 / p_projection.columns[1][1];
  940. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  941. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  942. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  943. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  944. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  945. ssao.gather_push_constant.radius = p_settings.radius;
  946. float radius_near_limit = (p_settings.radius * 1.2f);
  947. if (ssao_quality <= RS::ENV_SSAO_QUALITY_LOW) {
  948. radius_near_limit *= 1.50f;
  949. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  950. ssao.gather_push_constant.radius *= 0.8f;
  951. }
  952. }
  953. radius_near_limit /= tan_half_fov_y;
  954. ssao.gather_push_constant.intensity = p_settings.intensity;
  955. ssao.gather_push_constant.shadow_power = p_settings.power;
  956. ssao.gather_push_constant.shadow_clamp = 0.98;
  957. ssao.gather_push_constant.fade_out_mul = -1.0 / (ssao_fadeout_to - ssao_fadeout_from);
  958. ssao.gather_push_constant.fade_out_add = ssao_fadeout_from / (ssao_fadeout_to - ssao_fadeout_from) + 1.0;
  959. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  960. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  961. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  962. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssao_buffers.half_buffer_width) * (p_ssao_buffers.half_buffer_height) * 255);
  963. ssao.gather_push_constant.adaptive_sample_limit = ssao_adaptive_target;
  964. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  965. ssao.gather_push_constant.quality = MAX(0, ssao_quality - 1);
  966. ssao.gather_push_constant.size_multiplier = ssao_half_size ? 2 : 1;
  967. // We are using our uniform cache so our uniform sets are automatically freed when our textures are freed.
  968. // It also ensures that we're reusing the right cached entry in a multiview situation without us having to
  969. // remember each instance of the uniform set.
  970. RID gather_uniform_set;
  971. {
  972. 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);
  973. RD::Uniform u_depth_texture_view;
  974. u_depth_texture_view.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  975. u_depth_texture_view.binding = 0;
  976. u_depth_texture_view.append_id(ss_effects.mirror_sampler);
  977. u_depth_texture_view.append_id(depth_texture_view);
  978. RD::Uniform u_normal_buffer;
  979. u_normal_buffer.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  980. u_normal_buffer.binding = 1;
  981. u_normal_buffer.append_id(p_normal_buffer);
  982. RD::Uniform u_gather_constants_buffer;
  983. u_gather_constants_buffer.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  984. u_gather_constants_buffer.binding = 2;
  985. u_gather_constants_buffer.append_id(ss_effects.gather_constants_buffer);
  986. gather_uniform_set = uniform_set_cache->get_cache(shader, 0, u_depth_texture_view, u_normal_buffer, u_gather_constants_buffer);
  987. }
  988. RID importance_map_uniform_set;
  989. {
  990. RD::Uniform u_pong;
  991. u_pong.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  992. u_pong.binding = 0;
  993. u_pong.append_id(ao_pong);
  994. RD::Uniform u_importance_map;
  995. u_importance_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  996. u_importance_map.binding = 1;
  997. u_importance_map.append_id(default_sampler);
  998. u_importance_map.append_id(importance_map);
  999. RD::Uniform u_load_counter;
  1000. u_load_counter.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1001. u_load_counter.binding = 2;
  1002. u_load_counter.append_id(ssao.importance_map_load_counter);
  1003. RID shader_adaptive = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, SSAO_GATHER_ADAPTIVE);
  1004. importance_map_uniform_set = uniform_set_cache->get_cache(shader_adaptive, 1, u_pong, u_importance_map, u_load_counter);
  1005. }
  1006. if (ssao_quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  1007. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  1008. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  1009. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  1010. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  1011. ssao.importance_map_push_constant.power = p_settings.power;
  1012. //base pass
  1013. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE].get_rid());
  1014. gather_ssao(compute_list, ao_pong_slices, p_settings, true, gather_uniform_set, RID());
  1015. //generate importance map
  1016. RID gen_imp_shader = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 0);
  1017. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP].get_rid());
  1018. RD::Uniform u_ao_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong }));
  1019. 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);
  1020. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_map }));
  1021. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(gen_imp_shader, 1, u_importance_map), 1);
  1022. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1023. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1024. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1025. //process importance map A
  1026. RID proc_imp_shader_a = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 1);
  1027. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA].get_rid());
  1028. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_map }));
  1029. 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);
  1030. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ importance_pong }));
  1031. 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);
  1032. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1033. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1034. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1035. //process Importance Map B
  1036. RID proc_imp_shader_b = ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 2);
  1037. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB].get_rid());
  1038. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, importance_pong }));
  1039. 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);
  1040. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(proc_imp_shader_b, 1, u_importance_map), 1);
  1041. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  1042. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1043. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1044. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1045. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE].get_rid());
  1046. RD::get_singleton()->draw_command_end_label(); // Importance Map
  1047. } else {
  1048. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER].get_rid());
  1049. }
  1050. gather_ssao(compute_list, ao_deinterleaved_slices, p_settings, false, gather_uniform_set, importance_map_uniform_set);
  1051. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  1052. }
  1053. // /* THIRD PASS */
  1054. // // Blur
  1055. //
  1056. {
  1057. RD::get_singleton()->draw_command_begin_label("Edge-Aware Blur");
  1058. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  1059. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  1060. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  1061. int blur_passes = ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? ssao_blur_passes : 1;
  1062. shader = ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0);
  1063. for (int pass = 0; pass < blur_passes; pass++) {
  1064. int blur_pipeline = SSAO_BLUR_PASS;
  1065. if (ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1066. if (pass < blur_passes - 2) {
  1067. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  1068. } else {
  1069. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1070. }
  1071. }
  1072. for (int i = 0; i < 4; i++) {
  1073. if ((ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1074. continue;
  1075. }
  1076. RID blur_shader = ssao.blur_shader.version_get_shader(ssao.blur_shader_version, blur_pipeline - SSAO_BLUR_PASS);
  1077. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline].get_rid());
  1078. if (pass % 2 == 0) {
  1079. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1080. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_deinterleaved_slices[i] }));
  1081. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_slices_with_sampler), 0);
  1082. } else {
  1083. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, ao_deinterleaved_slices[i] }));
  1084. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 0, u_ao_slices_with_sampler), 0);
  1085. }
  1086. RD::Uniform u_ao_pong_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_pong_slices[i] }));
  1087. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ao_pong_slices), 1);
  1088. } else {
  1089. if (ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1090. RD::Uniform u_ao_pong_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong_slices[i] }));
  1091. 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);
  1092. } else {
  1093. 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] }));
  1094. 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);
  1095. }
  1096. RD::Uniform u_ao_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_deinterleaved_slices[i] }));
  1097. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(blur_shader, 1, u_ao_slices), 1);
  1098. }
  1099. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  1100. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.buffer_width, p_ssao_buffers.buffer_height, 1);
  1101. }
  1102. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1103. }
  1104. RD::get_singleton()->draw_command_end_label(); // Blur
  1105. }
  1106. /* FOURTH PASS */
  1107. // Interleave buffers
  1108. // back to full size
  1109. {
  1110. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1111. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1112. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1113. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1114. ssao.interleave_push_constant.size_modifier = uint32_t(ssao_half_size ? 4 : 2);
  1115. shader = ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, 0);
  1116. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1117. if (ssao_quality == RS::ENV_SSAO_QUALITY_LOW) {
  1118. interleave_pipeline = SSAO_INTERLEAVE;
  1119. } else if (ssao_quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1120. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1121. }
  1122. RID interleave_shader = ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, interleave_pipeline - SSAO_INTERLEAVE);
  1123. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline].get_rid());
  1124. RD::Uniform u_upscale_buffer(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ ao_final }));
  1125. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 0, u_upscale_buffer), 0);
  1126. if (ssao_quality > RS::ENV_SSAO_QUALITY_VERY_LOW && ssao_blur_passes % 2 == 0) {
  1127. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_deinterleaved }));
  1128. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 1, u_ao), 1);
  1129. } else {
  1130. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, ao_pong }));
  1131. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(interleave_shader, 1, u_ao), 1);
  1132. }
  1133. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1134. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1135. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1136. RD::get_singleton()->draw_command_end_label(); // Interleave
  1137. }
  1138. RD::get_singleton()->draw_command_end_label(); //SSAO
  1139. RD::get_singleton()->compute_list_end();
  1140. int zero[1] = { 0 };
  1141. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1142. }
  1143. /* Screen Space Reflection */
  1144. void SSEffects::ssr_set_half_size(bool p_half_size) {
  1145. ssr_half_size = p_half_size;
  1146. }
  1147. void SSEffects::ssr_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RD::DataFormat p_color_format) {
  1148. if (p_ssr_buffers.half_size != ssr_half_size) {
  1149. p_render_buffers->clear_context(RB_SCOPE_SSR);
  1150. }
  1151. Vector2i internal_size = p_render_buffers->get_internal_size();
  1152. p_ssr_buffers.size = ssr_half_size ? (internal_size / 2) : internal_size;
  1153. uint32_t cur_width = p_ssr_buffers.size.width;
  1154. uint32_t cur_height = p_ssr_buffers.size.height;
  1155. p_ssr_buffers.mipmaps = 1;
  1156. while (cur_width > 1 && cur_height > 1) {
  1157. if (cur_width > 1) {
  1158. cur_width /= 2;
  1159. }
  1160. if (cur_height > 1) {
  1161. cur_height /= 2;
  1162. }
  1163. ++p_ssr_buffers.mipmaps;
  1164. }
  1165. p_ssr_buffers.half_size = ssr_half_size;
  1166. uint32_t view_count = p_render_buffers->get_view_count();
  1167. if (ssr_half_size) {
  1168. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_NORMAL_ROUGHNESS, RD::DATA_FORMAT_R8G8B8A8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, view_count);
  1169. }
  1170. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_HIZ, RD::DATA_FORMAT_R32_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, view_count, p_ssr_buffers.mipmaps);
  1171. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_SSR, p_color_format, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, view_count, p_ssr_buffers.mipmaps);
  1172. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_MIP_LEVEL, RD::DATA_FORMAT_R8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, p_ssr_buffers.size, view_count);
  1173. if (ssr_half_size) {
  1174. p_render_buffers->create_texture(RB_SCOPE_SSR, RB_FINAL, p_color_format, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT, RD::TEXTURE_SAMPLES_1, internal_size, view_count);
  1175. }
  1176. }
  1177. void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RID *p_normal_roughness_slices, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const Projection *p_projections, const Projection *p_reprojections, const Vector3 *p_eye_offsets, RendererRD::CopyEffects &p_copy_effects) {
  1178. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1179. ERR_FAIL_NULL(uniform_set_cache);
  1180. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1181. ERR_FAIL_NULL(material_storage);
  1182. uint32_t view_count = p_render_buffers->get_view_count();
  1183. {
  1184. // Store some scene data in a UBO, in the near future we will use a UBO shared with other shaders
  1185. ScreenSpaceReflectionSceneData scene_data;
  1186. if (ssr.ubo.is_null()) {
  1187. ssr.ubo = RD::get_singleton()->uniform_buffer_create(sizeof(ScreenSpaceReflectionSceneData));
  1188. }
  1189. Projection correction;
  1190. correction.set_depth_correction(true);
  1191. for (uint32_t v = 0; v < view_count; v++) {
  1192. Projection projection = correction * p_projections[v];
  1193. store_camera(projection, scene_data.projection[v]);
  1194. store_camera(projection.inverse(), scene_data.inv_projection[v]);
  1195. store_camera(p_reprojections[v], scene_data.reprojection[v]);
  1196. scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
  1197. scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
  1198. scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
  1199. scene_data.eye_offset[v][3] = 0.0f;
  1200. }
  1201. RD::get_singleton()->buffer_update(ssr.ubo, 0, sizeof(ScreenSpaceReflectionSceneData), &scene_data);
  1202. }
  1203. RID linear_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1204. RID nearest_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1205. if (ssr_half_size) {
  1206. RD::get_singleton()->draw_command_begin_label("SSR Downsample");
  1207. for (uint32_t v = 0; v < view_count; v++) {
  1208. ScreenSpaceReflectionDownsamplePushConstant push_constant;
  1209. push_constant.screen_size[0] = p_ssr_buffers.size.width;
  1210. push_constant.screen_size[1] = p_ssr_buffers.size.height;
  1211. RID source_depth_texture = p_render_buffers->get_depth_texture(v);
  1212. RID dest_depth_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, 0);
  1213. RID dest_normal_roughness_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_NORMAL_ROUGHNESS, v, 0);
  1214. Size2i parent_size = RD::get_singleton()->texture_size(source_depth_texture);
  1215. bool is_width_odd = (parent_size.width % 2) != 0;
  1216. bool is_height_odd = (parent_size.height % 2) != 0;
  1217. int32_t downsample_mode;
  1218. if (is_width_odd && is_height_odd) {
  1219. downsample_mode = SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH_AND_HEIGHT;
  1220. } else if (is_width_odd) {
  1221. downsample_mode = SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH;
  1222. } else if (is_height_odd) {
  1223. downsample_mode = SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_HEIGHT;
  1224. } else {
  1225. downsample_mode = SCREEN_SPACE_REFLECTION_DOWNSAMPLE_DEFAULT;
  1226. }
  1227. RID downsample_shader = ssr.downsample_shader.version_get_shader(ssr.downsample_shader_version, downsample_mode);
  1228. RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>{ nearest_sampler, source_depth_texture });
  1229. RD::Uniform u_source_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>{ nearest_sampler, p_normal_roughness_slices[v] });
  1230. RD::Uniform u_dest_depth(RD::UNIFORM_TYPE_IMAGE, 2, dest_depth_texture);
  1231. RD::Uniform u_dest_normal_roughness(RD::UNIFORM_TYPE_IMAGE, 3, dest_normal_roughness_texture);
  1232. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1233. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.downsample_pipelines[downsample_mode].get_rid());
  1234. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(downsample_shader, 0, u_source_depth, u_source_normal_roughness, u_dest_depth, u_dest_normal_roughness), 0);
  1235. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(push_constant));
  1236. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.screen_size[0], push_constant.screen_size[1], 1);
  1237. RD::get_singleton()->compute_list_end();
  1238. }
  1239. RD::get_singleton()->draw_command_end_label();
  1240. } else {
  1241. RD::get_singleton()->draw_command_begin_label("SSR Copy Depth");
  1242. for (uint32_t v = 0; v < view_count; v++) {
  1243. RID src_texture = p_render_buffers->get_depth_texture(v);
  1244. RID dest_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, 0);
  1245. p_copy_effects.copy_depth_to_rect(src_texture, dest_texture, Rect2i(Vector2i(), p_ssr_buffers.size));
  1246. }
  1247. RD::get_singleton()->draw_command_end_label();
  1248. }
  1249. RD::get_singleton()->draw_command_begin_label("SSR HI-Z");
  1250. for (uint32_t v = 0; v < view_count; v++) {
  1251. for (uint32_t m = 1; m < p_ssr_buffers.mipmaps; m++) {
  1252. ScreenSpaceReflectionHizPushConstant push_constant;
  1253. push_constant.screen_size[0] = MAX(1, p_ssr_buffers.size.width >> m);
  1254. push_constant.screen_size[1] = MAX(1, p_ssr_buffers.size.height >> m);
  1255. RID source;
  1256. if (!ssr_half_size && m == 1) { // Reuse the depth texture to not create a dependency on the previous depth copy pass.
  1257. source = p_render_buffers->get_depth_texture(v);
  1258. } else {
  1259. source = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, m - 1);
  1260. }
  1261. RID dest = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, m);
  1262. Size2i parent_size = RD::get_singleton()->texture_size(source);
  1263. bool is_width_odd = (parent_size.width % 2) != 0;
  1264. bool is_height_odd = (parent_size.height % 2) != 0;
  1265. int32_t hiz_mode;
  1266. if (is_width_odd && is_height_odd) {
  1267. hiz_mode = SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH_AND_HEIGHT;
  1268. } else if (is_width_odd) {
  1269. hiz_mode = SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH;
  1270. } else if (is_height_odd) {
  1271. hiz_mode = SCREEN_SPACE_REFLECTION_HIZ_ODD_HEIGHT;
  1272. } else {
  1273. hiz_mode = SCREEN_SPACE_REFLECTION_HIZ_DEFAULT;
  1274. }
  1275. RID hiz_shader = ssr.hiz_shader.version_get_shader(ssr.hiz_shader_version, hiz_mode);
  1276. RD::Uniform u_source(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>{ nearest_sampler, source });
  1277. RD::Uniform u_dest(RD::UNIFORM_TYPE_IMAGE, 1, dest);
  1278. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1279. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.hiz_pipelines[hiz_mode].get_rid());
  1280. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(hiz_shader, 0, u_source, u_dest), 0);
  1281. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(push_constant));
  1282. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.screen_size[0], push_constant.screen_size[1], 1);
  1283. RD::get_singleton()->compute_list_end();
  1284. }
  1285. }
  1286. RD::get_singleton()->draw_command_end_label();
  1287. RD::get_singleton()->draw_command_begin_label("SSR Main");
  1288. RID ssr_shader = ssr.ssr_shader.version_get_shader(ssr.ssr_shader_version, 0);
  1289. for (uint32_t v = 0; v < view_count; v++) {
  1290. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1291. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.ssr_pipeline.get_rid());
  1292. ScreenSpaceReflectionPushConstant push_constant;
  1293. push_constant.screen_size[0] = p_ssr_buffers.size.width;
  1294. push_constant.screen_size[1] = p_ssr_buffers.size.height;
  1295. push_constant.mipmaps = p_ssr_buffers.mipmaps;
  1296. push_constant.num_steps = p_max_steps;
  1297. push_constant.curve_fade_in = p_fade_in;
  1298. push_constant.distance_fade = p_fade_out;
  1299. push_constant.depth_tolerance = p_tolerance;
  1300. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1301. push_constant.view_index = v;
  1302. RID last_frame_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSLF, RB_LAST_FRAME, v, 0);
  1303. if (ssr_half_size && RD::get_singleton()->texture_size(last_frame_texture) != p_ssr_buffers.size) {
  1304. // SSIL is likely also enabled. The texture we need is in the second mipmap in this case.
  1305. last_frame_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSLF, RB_LAST_FRAME, v, 1);
  1306. }
  1307. RID hiz_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, 0, 1, p_ssr_buffers.mipmaps);
  1308. RID normal_roughness_texture = ssr_half_size ? p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_NORMAL_ROUGHNESS, v, 0) : p_normal_roughness_slices[v];
  1309. RID ssr_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_SSR, v, 0);
  1310. RID mip_level_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_MIP_LEVEL, v, 0);
  1311. RD::Uniform u_last_frame(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>{ linear_sampler, last_frame_texture });
  1312. RD::Uniform u_hiz(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>{ nearest_sampler, hiz_texture });
  1313. RD::Uniform u_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 2, Vector<RID>{ nearest_sampler, normal_roughness_texture });
  1314. RD::Uniform u_ssr(RD::UNIFORM_TYPE_IMAGE, 3, ssr_texture);
  1315. RD::Uniform u_mip_level(RD::UNIFORM_TYPE_IMAGE, 4, mip_level_texture);
  1316. RD::Uniform u_scene_data(RD::UNIFORM_TYPE_UNIFORM_BUFFER, 5, ssr.ubo);
  1317. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(ssr_shader, 0, u_last_frame, u_hiz, u_normal_roughness, u_ssr, u_mip_level, u_scene_data), 0);
  1318. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(push_constant));
  1319. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssr_buffers.size.width, p_ssr_buffers.size.height, 1);
  1320. RD::get_singleton()->compute_list_end();
  1321. }
  1322. RD::get_singleton()->draw_command_end_label();
  1323. RD::get_singleton()->draw_command_begin_label("SSR Roughness Filter");
  1324. RID filter_shader = ssr.filter_shader.version_get_shader(ssr.filter_shader_version, 0);
  1325. for (uint32_t v = 0; v < view_count; v++) {
  1326. for (uint32_t m = 1; m < p_ssr_buffers.mipmaps; m++) {
  1327. ScreenSpaceReflectionFilterPushConstant push_constant;
  1328. push_constant.screen_size[0] = MAX(1, p_ssr_buffers.size.width >> m);
  1329. push_constant.screen_size[1] = MAX(1, p_ssr_buffers.size.height >> m);
  1330. push_constant.mip_level = m;
  1331. RID source = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_SSR, v, m - 1);
  1332. RID dest = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_SSR, v, m);
  1333. RD::Uniform u_source(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>{ linear_sampler, source });
  1334. RD::Uniform u_dest(RD::UNIFORM_TYPE_IMAGE, 1, dest);
  1335. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1336. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.filter_pipeline.get_rid());
  1337. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(filter_shader, 0, u_source, u_dest), 0);
  1338. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(push_constant));
  1339. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.screen_size[0], push_constant.screen_size[1], 1);
  1340. RD::get_singleton()->compute_list_end();
  1341. }
  1342. }
  1343. RD::get_singleton()->draw_command_end_label();
  1344. if (ssr_half_size) {
  1345. RD::get_singleton()->draw_command_begin_label("SSR Resolve");
  1346. RID resolve_shader = ssr.resolve_shader.version_get_shader(ssr.resolve_shader_version, 0);
  1347. for (uint32_t v = 0; v < view_count; v++) {
  1348. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1349. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.resolve_pipeline.get_rid());
  1350. Vector2i internal_size = p_render_buffers->get_internal_size();
  1351. ScreenSpaceReflectionResolvePushConstant push_constant;
  1352. push_constant.screen_size[0] = internal_size.x;
  1353. push_constant.screen_size[1] = internal_size.y;
  1354. RID depth_texture = p_render_buffers->get_depth_texture(v);
  1355. RID depth_half_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_HIZ, v, 0);
  1356. RID normal_roughness_half_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_NORMAL_ROUGHNESS, v, 0);
  1357. RID ssr_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_SSR, v, 0, 1, p_ssr_buffers.mipmaps);
  1358. RID mip_level_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_MIP_LEVEL, v, 0);
  1359. RID output_texture = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_FINAL, v, 0);
  1360. RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>{ nearest_sampler, depth_texture });
  1361. RD::Uniform u_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>{ nearest_sampler, p_normal_roughness_slices[v] });
  1362. RD::Uniform u_depth_half(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 2, Vector<RID>{ nearest_sampler, depth_half_texture });
  1363. RD::Uniform u_normal_roughness_half(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 3, Vector<RID>{ nearest_sampler, normal_roughness_half_texture });
  1364. RD::Uniform u_ssr(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 4, Vector<RID>{ linear_sampler, ssr_texture });
  1365. RD::Uniform u_mip_level(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 5, Vector<RID>{ nearest_sampler, mip_level_texture });
  1366. RD::Uniform u_output(RD::UNIFORM_TYPE_IMAGE, 6, output_texture);
  1367. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(resolve_shader, 0, u_depth, u_normal_roughness, u_depth_half, u_normal_roughness_half, u_ssr, u_mip_level, u_output), 0);
  1368. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(push_constant));
  1369. RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.width, internal_size.height, 1);
  1370. RD::get_singleton()->compute_list_end();
  1371. }
  1372. RD::get_singleton()->draw_command_end_label();
  1373. }
  1374. }
  1375. /* Subsurface scattering */
  1376. void SSEffects::sss_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  1377. sss_quality = p_quality;
  1378. }
  1379. RS::SubSurfaceScatteringQuality SSEffects::sss_get_quality() const {
  1380. return sss_quality;
  1381. }
  1382. void SSEffects::sss_set_scale(float p_scale, float p_depth_scale) {
  1383. sss_scale = p_scale;
  1384. sss_depth_scale = p_depth_scale;
  1385. }
  1386. 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) {
  1387. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1388. ERR_FAIL_NULL(uniform_set_cache);
  1389. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1390. ERR_FAIL_NULL(material_storage);
  1391. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1392. // Our intermediate buffer is only created if we haven't created it already.
  1393. RD::DataFormat format = p_render_buffers->get_base_data_format();
  1394. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1395. uint32_t layers = 1; // We only need one layer, we're handling one view at a time
  1396. uint32_t mipmaps = 1; // Image::get_image_required_mipmaps(p_screen_size.x, p_screen_size.y, Image::FORMAT_RGBAH);
  1397. RID intermediate = p_render_buffers->create_texture(SNAME("SSR"), SNAME("intermediate"), format, usage_bits, RD::TEXTURE_SAMPLES_1, p_screen_size, layers, mipmaps);
  1398. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  1399. p.normal /= p.d;
  1400. float unit_size = p.normal.x;
  1401. { //scale color and depth to half
  1402. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1403. sss.push_constant.camera_z_far = p_camera.get_z_far();
  1404. sss.push_constant.camera_z_near = p_camera.get_z_near();
  1405. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  1406. sss.push_constant.unit_size = unit_size;
  1407. sss.push_constant.screen_size[0] = p_screen_size.x;
  1408. sss.push_constant.screen_size[1] = p_screen_size.y;
  1409. sss.push_constant.vertical = false;
  1410. sss.push_constant.scale = sss_scale;
  1411. sss.push_constant.depth_scale = sss_depth_scale;
  1412. RID shader = sss.shader.version_get_shader(sss.shader_version, sss_quality - 1);
  1413. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[sss_quality - 1].get_rid());
  1414. RD::Uniform u_diffuse_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_diffuse }));
  1415. RD::Uniform u_diffuse(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_diffuse }));
  1416. RD::Uniform u_intermediate_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, intermediate }));
  1417. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ intermediate }));
  1418. RD::Uniform u_depth_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_depth }));
  1419. // horizontal
  1420. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_diffuse_with_sampler), 0);
  1421. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_intermediate), 1);
  1422. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_depth_with_sampler), 2);
  1423. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  1424. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1425. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1426. // vertical
  1427. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_intermediate_with_sampler), 0);
  1428. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_diffuse), 1);
  1429. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_depth_with_sampler), 2);
  1430. sss.push_constant.vertical = true;
  1431. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  1432. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1433. RD::get_singleton()->compute_list_end();
  1434. }
  1435. }