ss_effects.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*************************************************************************/
  2. /* ss_effects.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "ss_effects.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  34. using namespace RendererRD;
  35. SSEffects *SSEffects::singleton = nullptr;
  36. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  37. for (int i = 0; i < 4; i++) {
  38. for (int j = 0; j < 4; j++) {
  39. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  40. }
  41. }
  42. }
  43. SSEffects::SSEffects() {
  44. singleton = this;
  45. {
  46. // Initialize depth buffer for screen space effects
  47. Vector<String> downsampler_modes;
  48. downsampler_modes.push_back("\n");
  49. downsampler_modes.push_back("\n#define USE_HALF_SIZE\n");
  50. downsampler_modes.push_back("\n#define GENERATE_MIPS\n");
  51. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE\n");
  52. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  53. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE\n");
  54. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define GENERATE_FULL_MIPS");
  55. ss_effects.downsample_shader.initialize(downsampler_modes);
  56. ss_effects.downsample_shader_version = ss_effects.downsample_shader.version_create();
  57. for (int i = 0; i < SS_EFFECTS_MAX; i++) {
  58. ss_effects.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, i));
  59. }
  60. ss_effects.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSEffectsGatherConstants));
  61. SSEffectsGatherConstants gather_constants;
  62. const int sub_pass_count = 5;
  63. for (int pass = 0; pass < 4; pass++) {
  64. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  65. int a = pass;
  66. int b = subPass;
  67. int spmap[5]{ 0, 1, 4, 3, 2 };
  68. b = spmap[subPass];
  69. float ca, sa;
  70. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  71. ca = Math::cos(angle0);
  72. sa = Math::sin(angle0);
  73. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  74. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  75. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  76. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  77. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  78. }
  79. }
  80. RD::get_singleton()->buffer_update(ss_effects.gather_constants_buffer, 0, sizeof(SSEffectsGatherConstants), &gather_constants);
  81. }
  82. // Initialize Screen Space Indirect Lighting (SSIL)
  83. {
  84. Vector<String> ssil_modes;
  85. ssil_modes.push_back("\n");
  86. ssil_modes.push_back("\n#define SSIL_BASE\n");
  87. ssil_modes.push_back("\n#define ADAPTIVE\n");
  88. ssil.gather_shader.initialize(ssil_modes);
  89. ssil.gather_shader_version = ssil.gather_shader.version_create();
  90. for (int i = SSIL_GATHER; i <= SSIL_GATHER_ADAPTIVE; i++) {
  91. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.gather_shader.version_get_shader(ssil.gather_shader_version, i));
  92. }
  93. ssil.projection_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSILProjectionUniforms));
  94. }
  95. {
  96. Vector<String> ssil_modes;
  97. ssil_modes.push_back("\n#define GENERATE_MAP\n");
  98. ssil_modes.push_back("\n#define PROCESS_MAPA\n");
  99. ssil_modes.push_back("\n#define PROCESS_MAPB\n");
  100. ssil.importance_map_shader.initialize(ssil_modes);
  101. ssil.importance_map_shader_version = ssil.importance_map_shader.version_create();
  102. for (int i = SSIL_GENERATE_IMPORTANCE_MAP; i <= SSIL_PROCESS_IMPORTANCE_MAPB; i++) {
  103. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, i - SSIL_GENERATE_IMPORTANCE_MAP));
  104. }
  105. ssil.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  106. int zero[1] = { 0 };
  107. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  108. RD::get_singleton()->set_resource_name(ssil.importance_map_load_counter, "Importance Map Load Counter");
  109. Vector<RD::Uniform> uniforms;
  110. {
  111. RD::Uniform u;
  112. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  113. u.binding = 0;
  114. u.append_id(ssil.importance_map_load_counter);
  115. uniforms.push_back(u);
  116. }
  117. 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);
  118. RD::get_singleton()->set_resource_name(ssil.counter_uniform_set, "Load Counter Uniform Set");
  119. }
  120. {
  121. Vector<String> ssil_modes;
  122. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  123. ssil_modes.push_back("\n#define MODE_SMART\n");
  124. ssil_modes.push_back("\n#define MODE_WIDE\n");
  125. ssil.blur_shader.initialize(ssil_modes);
  126. ssil.blur_shader_version = ssil.blur_shader.version_create();
  127. for (int i = SSIL_BLUR_PASS; i <= SSIL_BLUR_PASS_WIDE; i++) {
  128. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.blur_shader.version_get_shader(ssil.blur_shader_version, i - SSIL_BLUR_PASS));
  129. }
  130. }
  131. {
  132. Vector<String> ssil_modes;
  133. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  134. ssil_modes.push_back("\n#define MODE_SMART\n");
  135. ssil_modes.push_back("\n#define MODE_HALF\n");
  136. ssil.interleave_shader.initialize(ssil_modes);
  137. ssil.interleave_shader_version = ssil.interleave_shader.version_create();
  138. for (int i = SSIL_INTERLEAVE; i <= SSIL_INTERLEAVE_HALF; i++) {
  139. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, i - SSIL_INTERLEAVE));
  140. }
  141. }
  142. {
  143. // Initialize Screen Space Ambient Occlusion (SSAO)
  144. RD::SamplerState sampler;
  145. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  146. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  147. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  148. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  149. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  150. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  151. sampler.max_lod = 4;
  152. uint32_t pipeline = 0;
  153. {
  154. Vector<String> ssao_modes;
  155. ssao_modes.push_back("\n");
  156. ssao_modes.push_back("\n#define SSAO_BASE\n");
  157. ssao_modes.push_back("\n#define ADAPTIVE\n");
  158. ssao.gather_shader.initialize(ssao_modes);
  159. ssao.gather_shader_version = ssao.gather_shader.version_create();
  160. for (int i = 0; i <= SSAO_GATHER_ADAPTIVE; i++) {
  161. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i));
  162. pipeline++;
  163. }
  164. }
  165. {
  166. Vector<String> ssao_modes;
  167. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  168. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  169. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  170. ssao.importance_map_shader.initialize(ssao_modes);
  171. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  172. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  173. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, i - SSAO_GENERATE_IMPORTANCE_MAP));
  174. pipeline++;
  175. }
  176. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  177. int zero[1] = { 0 };
  178. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  179. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  180. Vector<RD::Uniform> uniforms;
  181. {
  182. RD::Uniform u;
  183. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  184. u.binding = 0;
  185. u.append_id(ssao.importance_map_load_counter);
  186. uniforms.push_back(u);
  187. }
  188. 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);
  189. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  190. }
  191. {
  192. Vector<String> ssao_modes;
  193. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  194. ssao_modes.push_back("\n#define MODE_SMART\n");
  195. ssao_modes.push_back("\n#define MODE_WIDE\n");
  196. ssao.blur_shader.initialize(ssao_modes);
  197. ssao.blur_shader_version = ssao.blur_shader.version_create();
  198. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  199. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  200. pipeline++;
  201. }
  202. }
  203. {
  204. Vector<String> ssao_modes;
  205. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  206. ssao_modes.push_back("\n#define MODE_SMART\n");
  207. ssao_modes.push_back("\n#define MODE_HALF\n");
  208. ssao.interleave_shader.initialize(ssao_modes);
  209. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  210. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  211. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  212. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  213. pipeline++;
  214. }
  215. }
  216. ERR_FAIL_COND(pipeline != SSAO_MAX);
  217. ss_effects.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  218. }
  219. {
  220. // Screen Space Reflections
  221. Vector<RD::PipelineSpecializationConstant> specialization_constants;
  222. {
  223. RD::PipelineSpecializationConstant sc;
  224. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  225. sc.constant_id = 0; // SSR_USE_FULL_PROJECTION_MATRIX
  226. sc.bool_value = false;
  227. specialization_constants.push_back(sc);
  228. }
  229. {
  230. Vector<String> ssr_scale_modes;
  231. ssr_scale_modes.push_back("\n");
  232. ssr_scale.shader.initialize(ssr_scale_modes);
  233. ssr_scale.shader_version = ssr_scale.shader.version_create();
  234. for (int v = 0; v < SSR_VARIATIONS; v++) {
  235. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  236. ssr_scale.pipelines[v] = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), specialization_constants);
  237. }
  238. }
  239. {
  240. Vector<String> ssr_modes;
  241. ssr_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_NORMAL
  242. ssr_modes.push_back("\n#define MODE_ROUGH\n"); // SCREEN_SPACE_REFLECTION_ROUGH
  243. ssr.shader.initialize(ssr_modes);
  244. ssr.shader_version = ssr.shader.version_create();
  245. for (int v = 0; v < SSR_VARIATIONS; v++) {
  246. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  247. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  248. ssr.pipelines[v][i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i), specialization_constants);
  249. }
  250. }
  251. }
  252. {
  253. Vector<String> ssr_filter_modes;
  254. ssr_filter_modes.push_back("\n"); // SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL
  255. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n"); // SCREEN_SPACE_REFLECTION_FILTER_VERTICAL
  256. ssr_filter.shader.initialize(ssr_filter_modes);
  257. ssr_filter.shader_version = ssr_filter.shader.version_create();
  258. for (int v = 0; v < SSR_VARIATIONS; v++) {
  259. specialization_constants.ptrw()[0].bool_value = (v & SSR_MULTIVIEW) ? true : false;
  260. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  261. ssr_filter.pipelines[v][i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i), specialization_constants);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. SSEffects::~SSEffects() {
  268. {
  269. // Cleanup SS Reflections
  270. ssr.shader.version_free(ssr.shader_version);
  271. ssr_filter.shader.version_free(ssr_filter.shader_version);
  272. ssr_scale.shader.version_free(ssr_scale.shader_version);
  273. if (ssr.ubo.is_valid()) {
  274. RD::get_singleton()->free(ssr.ubo);
  275. }
  276. }
  277. {
  278. // Cleanup SS downsampler
  279. ss_effects.downsample_shader.version_free(ss_effects.downsample_shader_version);
  280. RD::get_singleton()->free(ss_effects.mirror_sampler);
  281. RD::get_singleton()->free(ss_effects.gather_constants_buffer);
  282. }
  283. {
  284. // Cleanup SSIL
  285. ssil.blur_shader.version_free(ssil.blur_shader_version);
  286. ssil.gather_shader.version_free(ssil.gather_shader_version);
  287. ssil.interleave_shader.version_free(ssil.interleave_shader_version);
  288. ssil.importance_map_shader.version_free(ssil.importance_map_shader_version);
  289. RD::get_singleton()->free(ssil.importance_map_load_counter);
  290. RD::get_singleton()->free(ssil.projection_uniform_buffer);
  291. }
  292. {
  293. // Cleanup SSAO
  294. ssao.blur_shader.version_free(ssao.blur_shader_version);
  295. ssao.gather_shader.version_free(ssao.gather_shader_version);
  296. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  297. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  298. RD::get_singleton()->free(ssao.importance_map_load_counter);
  299. }
  300. singleton = nullptr;
  301. }
  302. /* SS Downsampler */
  303. void SSEffects::downsample_depth(RID p_depth_buffer, const Vector<RID> &p_depth_mipmaps, RS::EnvironmentSSAOQuality p_ssao_quality, RS::EnvironmentSSILQuality p_ssil_quality, bool p_invalidate_uniform_set, bool p_ssao_half_size, bool p_ssil_half_size, Size2i p_full_screen_size, const CameraMatrix &p_projection) {
  304. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  305. ERR_FAIL_NULL(uniform_set_cache);
  306. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  307. ERR_FAIL_NULL(material_storage);
  308. // Downsample and deinterleave the depth buffer for SSAO and SSIL
  309. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  310. int downsample_mode = SS_EFFECTS_DOWNSAMPLE;
  311. bool use_mips = p_ssao_quality > RS::ENV_SSAO_QUALITY_MEDIUM || p_ssil_quality > RS::ENV_SSIL_QUALITY_MEDIUM;
  312. if (p_ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW && p_ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  313. downsample_mode = SS_EFFECTS_DOWNSAMPLE_HALF;
  314. } else if (use_mips) {
  315. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  316. }
  317. bool use_half_size = false;
  318. bool use_full_mips = false;
  319. if (p_ssao_half_size && p_ssil_half_size) {
  320. downsample_mode++;
  321. use_half_size = true;
  322. } else if (p_ssao_half_size != p_ssil_half_size) {
  323. if (use_mips) {
  324. downsample_mode = SS_EFFECTS_DOWNSAMPLE_FULL_MIPS;
  325. use_full_mips = true;
  326. } else {
  327. // Only need the first two mipmaps, but the cost to generate the next two is trivial
  328. // TODO investigate the benefit of a shader version to generate only 2 mips
  329. downsample_mode = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  330. use_mips = true;
  331. }
  332. }
  333. int depth_index = use_half_size ? 1 : 0;
  334. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  335. if (p_invalidate_uniform_set || use_full_mips != ss_effects.used_full_mips_last_frame || use_half_size != ss_effects.used_half_size_last_frame || use_mips != ss_effects.used_mips_last_frame) {
  336. Vector<RD::Uniform> uniforms;
  337. {
  338. RD::Uniform u;
  339. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  340. u.binding = 0;
  341. u.append_id(p_depth_mipmaps[depth_index + 1]);
  342. uniforms.push_back(u);
  343. }
  344. {
  345. RD::Uniform u;
  346. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  347. u.binding = 1;
  348. u.append_id(p_depth_mipmaps[depth_index + 2]);
  349. uniforms.push_back(u);
  350. }
  351. {
  352. RD::Uniform u;
  353. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  354. u.binding = 2;
  355. u.append_id(p_depth_mipmaps[depth_index + 3]);
  356. uniforms.push_back(u);
  357. }
  358. if (use_full_mips) {
  359. RD::Uniform u;
  360. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  361. u.binding = 3;
  362. u.append_id(p_depth_mipmaps[4]);
  363. uniforms.push_back(u);
  364. }
  365. ss_effects.downsample_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, use_full_mips ? 6 : 2), 2);
  366. }
  367. float depth_linearize_mul = -p_projection.matrix[3][2];
  368. float depth_linearize_add = p_projection.matrix[2][2];
  369. if (depth_linearize_mul * depth_linearize_add < 0) {
  370. depth_linearize_add = -depth_linearize_add;
  371. }
  372. ss_effects.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  373. ss_effects.downsample_push_constant.z_near = depth_linearize_mul;
  374. ss_effects.downsample_push_constant.z_far = depth_linearize_add;
  375. if (ss_effects.downsample_push_constant.orthogonal) {
  376. ss_effects.downsample_push_constant.z_near = p_projection.get_z_near();
  377. ss_effects.downsample_push_constant.z_far = p_projection.get_z_far();
  378. }
  379. ss_effects.downsample_push_constant.pixel_size[0] = 1.0 / p_full_screen_size.x;
  380. ss_effects.downsample_push_constant.pixel_size[1] = 1.0 / p_full_screen_size.y;
  381. ss_effects.downsample_push_constant.radius_sq = 1.0;
  382. RID shader = ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, downsample_mode);
  383. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  384. RD::Uniform u_depth_buffer(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_depth_buffer }));
  385. RD::Uniform u_depth_mipmaps(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_depth_mipmaps[depth_index + 0] }));
  386. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ss_effects.pipelines[downsample_mode]);
  387. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_depth_buffer), 0);
  388. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_mipmaps), 1);
  389. if (use_mips) {
  390. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ss_effects.downsample_uniform_set, 2);
  391. }
  392. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ss_effects.downsample_push_constant, sizeof(SSEffectsDownsamplePushConstant));
  393. Size2i size(MAX(1, p_full_screen_size.x >> (use_half_size ? 2 : 1)), MAX(1, p_full_screen_size.y >> (use_half_size ? 2 : 1)));
  394. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  395. RD::get_singleton()->compute_list_add_barrier(compute_list);
  396. RD::get_singleton()->draw_command_end_label();
  397. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_COMPUTE);
  398. ss_effects.used_full_mips_last_frame = use_full_mips;
  399. ss_effects.used_half_size_last_frame = use_half_size;
  400. }
  401. /* SSIL */
  402. void SSEffects::gather_ssil(RD::ComputeListID p_compute_list, const Vector<RID> p_ssil_slices, const Vector<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) {
  403. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  404. ERR_FAIL_NULL(uniform_set_cache);
  405. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  406. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  407. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  408. }
  409. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_projection_uniform_set, 3);
  410. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0);
  411. for (int i = 0; i < 4; i++) {
  412. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  413. continue;
  414. }
  415. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_slices[i] }));
  416. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_edges_slices[i] }));
  417. ssil.gather_push_constant.pass_coord_offset[0] = i % 2;
  418. ssil.gather_push_constant.pass_coord_offset[1] = i / 2;
  419. ssil.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  420. ssil.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  421. ssil.gather_push_constant.pass = i;
  422. 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);
  423. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssil.gather_push_constant, sizeof(SSILGatherPushConstant));
  424. Size2i size = Size2i(p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1), p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  425. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  426. }
  427. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  428. }
  429. void SSEffects::ssil_allocate_buffers(SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings, RID p_linear_depth) {
  430. if (p_ssil_buffers.half_size != p_settings.half_size) {
  431. ssil_free(p_ssil_buffers);
  432. }
  433. if (p_settings.half_size) {
  434. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  435. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  436. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  437. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  438. } else {
  439. p_ssil_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  440. p_ssil_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  441. p_ssil_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  442. p_ssil_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  443. }
  444. if (p_ssil_buffers.ssil_final.is_null()) {
  445. {
  446. p_ssil_buffers.depth_texture_view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_linear_depth, 0, p_settings.half_size ? 1 : 0, 4, RD::TEXTURE_SLICE_2D_ARRAY);
  447. }
  448. {
  449. RD::TextureFormat tf;
  450. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  451. tf.width = p_settings.full_screen_size.x;
  452. tf.height = p_settings.full_screen_size.y;
  453. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  454. p_ssil_buffers.ssil_final = RD::get_singleton()->texture_create(tf, RD::TextureView());
  455. RD::get_singleton()->set_resource_name(p_ssil_buffers.ssil_final, "SSIL texture");
  456. RD::get_singleton()->texture_clear(p_ssil_buffers.ssil_final, Color(0, 0, 0, 0), 0, 1, 0, 1);
  457. if (p_ssil_buffers.last_frame.is_null()) {
  458. tf.mipmaps = 6;
  459. p_ssil_buffers.last_frame = RD::get_singleton()->texture_create(tf, RD::TextureView());
  460. RD::get_singleton()->set_resource_name(p_ssil_buffers.last_frame, "Last Frame Radiance");
  461. RD::get_singleton()->texture_clear(p_ssil_buffers.last_frame, Color(0, 0, 0, 0), 0, tf.mipmaps, 0, 1);
  462. for (uint32_t i = 0; i < 6; i++) {
  463. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssil_buffers.last_frame, 0, i);
  464. p_ssil_buffers.last_frame_slices.push_back(slice);
  465. RD::get_singleton()->set_resource_name(slice, "Last Frame Radiance Mip " + itos(i) + " ");
  466. }
  467. }
  468. }
  469. {
  470. RD::TextureFormat tf;
  471. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  472. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  473. tf.width = p_ssil_buffers.buffer_width;
  474. tf.height = p_ssil_buffers.buffer_height;
  475. tf.array_layers = 4;
  476. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  477. p_ssil_buffers.deinterleaved = RD::get_singleton()->texture_create(tf, RD::TextureView());
  478. RD::get_singleton()->set_resource_name(p_ssil_buffers.deinterleaved, "SSIL deinterleaved buffer");
  479. for (uint32_t i = 0; i < 4; i++) {
  480. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssil_buffers.deinterleaved, i, 0);
  481. p_ssil_buffers.deinterleaved_slices.push_back(slice);
  482. RD::get_singleton()->set_resource_name(slice, "SSIL deinterleaved buffer array " + itos(i) + " ");
  483. }
  484. }
  485. {
  486. RD::TextureFormat tf;
  487. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  488. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  489. tf.width = p_ssil_buffers.buffer_width;
  490. tf.height = p_ssil_buffers.buffer_height;
  491. tf.array_layers = 4;
  492. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  493. p_ssil_buffers.pong = RD::get_singleton()->texture_create(tf, RD::TextureView());
  494. RD::get_singleton()->set_resource_name(p_ssil_buffers.pong, "SSIL deinterleaved pong buffer");
  495. for (uint32_t i = 0; i < 4; i++) {
  496. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssil_buffers.pong, i, 0);
  497. p_ssil_buffers.pong_slices.push_back(slice);
  498. RD::get_singleton()->set_resource_name(slice, "SSIL deinterleaved buffer pong array " + itos(i) + " ");
  499. }
  500. }
  501. {
  502. RD::TextureFormat tf;
  503. tf.format = RD::DATA_FORMAT_R8_UNORM;
  504. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  505. tf.width = p_ssil_buffers.buffer_width;
  506. tf.height = p_ssil_buffers.buffer_height;
  507. tf.array_layers = 4;
  508. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  509. p_ssil_buffers.edges = RD::get_singleton()->texture_create(tf, RD::TextureView());
  510. RD::get_singleton()->set_resource_name(p_ssil_buffers.edges, "SSIL edges buffer");
  511. for (uint32_t i = 0; i < 4; i++) {
  512. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssil_buffers.edges, i, 0);
  513. p_ssil_buffers.edges_slices.push_back(slice);
  514. RD::get_singleton()->set_resource_name(slice, "SSIL edges buffer slice " + itos(i) + " ");
  515. }
  516. }
  517. {
  518. RD::TextureFormat tf;
  519. tf.format = RD::DATA_FORMAT_R8_UNORM;
  520. tf.width = p_ssil_buffers.half_buffer_width;
  521. tf.height = p_ssil_buffers.half_buffer_height;
  522. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  523. p_ssil_buffers.importance_map[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  524. RD::get_singleton()->set_resource_name(p_ssil_buffers.importance_map[0], "SSIL Importance Map");
  525. p_ssil_buffers.importance_map[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  526. RD::get_singleton()->set_resource_name(p_ssil_buffers.importance_map[1], "SSIL Importance Map Pong");
  527. }
  528. p_ssil_buffers.half_size = p_settings.half_size;
  529. }
  530. }
  531. void SSEffects::screen_space_indirect_lighting(SSILRenderBuffers &p_ssil_buffers, RID p_normal_buffer, const CameraMatrix &p_projection, const CameraMatrix &p_last_projection, const SSILSettings &p_settings) {
  532. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  533. ERR_FAIL_NULL(uniform_set_cache);
  534. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  535. ERR_FAIL_NULL(material_storage);
  536. RD::get_singleton()->draw_command_begin_label("Process Screen Space Indirect Lighting");
  537. //Store projection info before starting the compute list
  538. SSILProjectionUniforms projection_uniforms;
  539. store_camera(p_last_projection, projection_uniforms.inv_last_frame_projection_matrix);
  540. RD::get_singleton()->buffer_update(ssil.projection_uniform_buffer, 0, sizeof(SSILProjectionUniforms), &projection_uniforms);
  541. memset(&ssil.gather_push_constant, 0, sizeof(SSILGatherPushConstant));
  542. RID shader = ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0);
  543. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  544. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  545. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  546. {
  547. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  548. ssil.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  549. ssil.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  550. ssil.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  551. ssil.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  552. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  553. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  554. ssil.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  555. ssil.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  556. ssil.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  557. ssil.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  558. ssil.gather_push_constant.z_near = p_projection.get_z_near();
  559. ssil.gather_push_constant.z_far = p_projection.get_z_far();
  560. ssil.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  561. ssil.gather_push_constant.half_screen_pixel_size_x025[0] = ssil.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  562. ssil.gather_push_constant.half_screen_pixel_size_x025[1] = ssil.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  563. ssil.gather_push_constant.radius = p_settings.radius;
  564. float radius_near_limit = (p_settings.radius * 1.2f);
  565. if (p_settings.quality <= RS::ENV_SSIL_QUALITY_LOW) {
  566. radius_near_limit *= 1.50f;
  567. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  568. ssil.gather_push_constant.radius *= 0.8f;
  569. }
  570. }
  571. radius_near_limit /= tan_half_fov_y;
  572. ssil.gather_push_constant.intensity = p_settings.intensity * Math_PI;
  573. ssil.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  574. ssil.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  575. ssil.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  576. ssil.gather_push_constant.neg_inv_radius = -1.0 / ssil.gather_push_constant.radius;
  577. ssil.gather_push_constant.normal_rejection_amount = p_settings.normal_rejection;
  578. ssil.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssil_buffers.half_buffer_width) * (p_ssil_buffers.half_buffer_height) * 255);
  579. ssil.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  580. ssil.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  581. ssil.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  582. if (p_ssil_buffers.projection_uniform_set.is_null()) {
  583. Vector<RD::Uniform> uniforms;
  584. {
  585. RD::Uniform u;
  586. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  587. u.binding = 0;
  588. u.append_id(default_mipmap_sampler);
  589. u.append_id(p_ssil_buffers.last_frame);
  590. uniforms.push_back(u);
  591. }
  592. {
  593. RD::Uniform u;
  594. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  595. u.binding = 1;
  596. u.append_id(ssil.projection_uniform_buffer);
  597. uniforms.push_back(u);
  598. }
  599. p_ssil_buffers.projection_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 3);
  600. }
  601. if (p_ssil_buffers.gather_uniform_set.is_null()) {
  602. Vector<RD::Uniform> uniforms;
  603. {
  604. RD::Uniform u;
  605. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  606. u.binding = 0;
  607. u.append_id(default_sampler);
  608. u.append_id(p_ssil_buffers.depth_texture_view);
  609. uniforms.push_back(u);
  610. }
  611. {
  612. RD::Uniform u;
  613. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  614. u.binding = 1;
  615. u.append_id(p_normal_buffer);
  616. uniforms.push_back(u);
  617. }
  618. {
  619. RD::Uniform u;
  620. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  621. u.binding = 2;
  622. u.append_id(ss_effects.gather_constants_buffer);
  623. uniforms.push_back(u);
  624. }
  625. p_ssil_buffers.gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 0);
  626. }
  627. if (p_ssil_buffers.importance_map_uniform_set.is_null()) {
  628. Vector<RD::Uniform> uniforms;
  629. {
  630. RD::Uniform u;
  631. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  632. u.binding = 0;
  633. u.append_id(p_ssil_buffers.pong);
  634. uniforms.push_back(u);
  635. }
  636. {
  637. RD::Uniform u;
  638. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  639. u.binding = 1;
  640. u.append_id(default_sampler);
  641. u.append_id(p_ssil_buffers.importance_map[0]);
  642. uniforms.push_back(u);
  643. }
  644. {
  645. RD::Uniform u;
  646. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  647. u.binding = 2;
  648. u.append_id(ssil.importance_map_load_counter);
  649. uniforms.push_back(u);
  650. }
  651. p_ssil_buffers.importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 2), 1);
  652. }
  653. if (p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) {
  654. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  655. ssil.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  656. ssil.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  657. ssil.importance_map_push_constant.intensity = p_settings.intensity * Math_PI;
  658. //base pass
  659. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_BASE]);
  660. gather_ssil(compute_list, p_ssil_buffers.pong_slices, p_ssil_buffers.edges_slices, p_settings, true, p_ssil_buffers.gather_uniform_set, p_ssil_buffers.importance_map_uniform_set, p_ssil_buffers.projection_uniform_set);
  661. //generate importance map
  662. RD::Uniform u_ssil_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.pong }));
  663. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.importance_map[0] }));
  664. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GENERATE_IMPORTANCE_MAP]);
  665. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ssil_pong_with_sampler), 0);
  666. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map), 1);
  667. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  668. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  669. RD::get_singleton()->compute_list_add_barrier(compute_list);
  670. // process Importance Map A
  671. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.importance_map[0] }));
  672. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.importance_map[1] }));
  673. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPA]);
  674. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_importance_map_with_sampler), 0);
  675. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map_pong), 1);
  676. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  677. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  678. RD::get_singleton()->compute_list_add_barrier(compute_list);
  679. // process Importance Map B
  680. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.importance_map[1] }));
  681. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPB]);
  682. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_importance_map_pong_with_sampler), 0);
  683. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map), 1);
  684. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssil.counter_uniform_set, 2);
  685. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  686. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssil_buffers.half_buffer_width, p_ssil_buffers.half_buffer_height, 1);
  687. RD::get_singleton()->compute_list_add_barrier(compute_list);
  688. RD::get_singleton()->draw_command_end_label(); // Importance Map
  689. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_ADAPTIVE]);
  690. } else {
  691. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER]);
  692. }
  693. gather_ssil(compute_list, p_ssil_buffers.deinterleaved_slices, p_ssil_buffers.edges_slices, p_settings, false, p_ssil_buffers.gather_uniform_set, p_ssil_buffers.importance_map_uniform_set, p_ssil_buffers.projection_uniform_set);
  694. RD::get_singleton()->draw_command_end_label(); //Gather
  695. }
  696. {
  697. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  698. ssil.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  699. ssil.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssil_buffers.buffer_width;
  700. ssil.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssil_buffers.buffer_height;
  701. int blur_passes = p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  702. shader = ssil.blur_shader.version_get_shader(ssil.blur_shader_version, 0);
  703. for (int pass = 0; pass < blur_passes; pass++) {
  704. int blur_pipeline = SSIL_BLUR_PASS;
  705. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  706. blur_pipeline = SSIL_BLUR_PASS_SMART;
  707. if (pass < blur_passes - 2) {
  708. blur_pipeline = SSIL_BLUR_PASS_WIDE;
  709. }
  710. }
  711. for (int i = 0; i < 4; i++) {
  712. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  713. continue;
  714. }
  715. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[blur_pipeline]);
  716. if (pass % 2 == 0) {
  717. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  718. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.deinterleaved_slices[i] }));
  719. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ssil_slice), 0);
  720. } else {
  721. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, p_ssil_buffers.deinterleaved_slices[i] }));
  722. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ssil_slice), 0);
  723. }
  724. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.pong_slices[i] }));
  725. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil_pong_slice), 1);
  726. } else {
  727. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  728. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.pong_slices[i] }));
  729. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ssil_pong_slice), 0);
  730. } else {
  731. RD::Uniform u_ssil_pong_slice(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, p_ssil_buffers.pong_slices[i] }));
  732. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ssil_pong_slice), 0);
  733. }
  734. RD::Uniform u_ssil_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.deinterleaved_slices[i] }));
  735. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil_slice), 1);
  736. }
  737. RD::Uniform u_edges_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.edges_slices[i] }));
  738. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_edges_slice), 2);
  739. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.blur_push_constant, sizeof(SSILBlurPushConstant));
  740. int x_groups = (p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1));
  741. int y_groups = (p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  742. RD::get_singleton()->compute_list_dispatch_threads(compute_list, x_groups, y_groups, 1);
  743. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  744. RD::get_singleton()->compute_list_add_barrier(compute_list);
  745. }
  746. }
  747. }
  748. RD::get_singleton()->draw_command_end_label(); // Blur
  749. }
  750. {
  751. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  752. ssil.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  753. ssil.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  754. ssil.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  755. ssil.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  756. int interleave_pipeline = SSIL_INTERLEAVE_HALF;
  757. if (p_settings.quality == RS::ENV_SSIL_QUALITY_LOW) {
  758. interleave_pipeline = SSIL_INTERLEAVE;
  759. } else if (p_settings.quality >= RS::ENV_SSIL_QUALITY_MEDIUM) {
  760. interleave_pipeline = SSIL_INTERLEAVE_SMART;
  761. }
  762. shader = ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, 0);
  763. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[interleave_pipeline]);
  764. RD::Uniform u_destination(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.ssil_final }));
  765. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_destination), 0);
  766. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  767. RD::Uniform u_ssil(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.deinterleaved }));
  768. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil), 1);
  769. } else {
  770. RD::Uniform u_ssil_pong(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssil_buffers.pong }));
  771. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ssil_pong), 1);
  772. }
  773. RD::Uniform u_edges(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssil_buffers.edges }));
  774. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_edges), 2);
  775. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.interleave_push_constant, sizeof(SSILInterleavePushConstant));
  776. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  777. RD::get_singleton()->compute_list_add_barrier(compute_list);
  778. RD::get_singleton()->draw_command_end_label(); // Interleave
  779. }
  780. RD::get_singleton()->draw_command_end_label(); // SSIL
  781. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER);
  782. int zero[1] = { 0 };
  783. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  784. }
  785. void SSEffects::ssil_free(SSILRenderBuffers &p_ssil_buffers) {
  786. if (p_ssil_buffers.ssil_final.is_valid()) {
  787. RD::get_singleton()->free(p_ssil_buffers.ssil_final);
  788. RD::get_singleton()->free(p_ssil_buffers.deinterleaved);
  789. RD::get_singleton()->free(p_ssil_buffers.pong);
  790. RD::get_singleton()->free(p_ssil_buffers.edges);
  791. RD::get_singleton()->free(p_ssil_buffers.importance_map[0]);
  792. RD::get_singleton()->free(p_ssil_buffers.importance_map[1]);
  793. RD::get_singleton()->free(p_ssil_buffers.last_frame);
  794. p_ssil_buffers.ssil_final = RID();
  795. p_ssil_buffers.deinterleaved = RID();
  796. p_ssil_buffers.pong = RID();
  797. p_ssil_buffers.edges = RID();
  798. p_ssil_buffers.deinterleaved_slices.clear();
  799. p_ssil_buffers.pong_slices.clear();
  800. p_ssil_buffers.edges_slices.clear();
  801. p_ssil_buffers.importance_map[0] = RID();
  802. p_ssil_buffers.importance_map[1] = RID();
  803. p_ssil_buffers.last_frame = RID();
  804. p_ssil_buffers.last_frame_slices.clear();
  805. p_ssil_buffers.gather_uniform_set = RID();
  806. p_ssil_buffers.importance_map_uniform_set = RID();
  807. p_ssil_buffers.projection_uniform_set = RID();
  808. }
  809. }
  810. /* SSAO */
  811. void SSEffects::gather_ssao(RD::ComputeListID p_compute_list, const Vector<RID> p_ao_slices, const SSAOSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set) {
  812. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  813. ERR_FAIL_NULL(uniform_set_cache);
  814. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  815. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  816. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 0);
  817. }
  818. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 1); //
  819. for (int i = 0; i < 4; i++) {
  820. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  821. continue;
  822. }
  823. RD::Uniform u_ao_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ao_slices[i] }));
  824. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  825. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  826. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  827. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  828. ssao.gather_push_constant.pass = i;
  829. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, uniform_set_cache->get_cache(shader, 2, u_ao_slice), 2);
  830. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  831. Size2i size = Size2i(p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1), p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  832. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  833. }
  834. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  835. }
  836. void SSEffects::ssao_allocate_buffers(SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings, RID p_linear_depth) {
  837. if (p_ssao_buffers.half_size != p_settings.half_size) {
  838. ssao_free(p_ssao_buffers);
  839. }
  840. if (p_settings.half_size) {
  841. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  842. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  843. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 7) / 8;
  844. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 7) / 8;
  845. } else {
  846. p_ssao_buffers.buffer_width = (p_settings.full_screen_size.x + 1) / 2;
  847. p_ssao_buffers.buffer_height = (p_settings.full_screen_size.y + 1) / 2;
  848. p_ssao_buffers.half_buffer_width = (p_settings.full_screen_size.x + 3) / 4;
  849. p_ssao_buffers.half_buffer_height = (p_settings.full_screen_size.y + 3) / 4;
  850. }
  851. if (p_ssao_buffers.ao_deinterleaved.is_null()) {
  852. {
  853. p_ssao_buffers.depth_texture_view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_linear_depth, 0, p_settings.half_size ? 1 : 0, 4, RD::TEXTURE_SLICE_2D_ARRAY);
  854. }
  855. {
  856. RD::TextureFormat tf;
  857. tf.format = RD::DATA_FORMAT_R8G8_UNORM;
  858. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  859. tf.width = p_ssao_buffers.buffer_width;
  860. tf.height = p_ssao_buffers.buffer_height;
  861. tf.array_layers = 4;
  862. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  863. p_ssao_buffers.ao_deinterleaved = RD::get_singleton()->texture_create(tf, RD::TextureView());
  864. RD::get_singleton()->set_resource_name(p_ssao_buffers.ao_deinterleaved, "SSAO De-interleaved Array");
  865. for (uint32_t i = 0; i < 4; i++) {
  866. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssao_buffers.ao_deinterleaved, i, 0);
  867. p_ssao_buffers.ao_deinterleaved_slices.push_back(slice);
  868. RD::get_singleton()->set_resource_name(slice, "SSAO De-interleaved Array Layer " + itos(i) + " ");
  869. }
  870. }
  871. {
  872. RD::TextureFormat tf;
  873. tf.format = RD::DATA_FORMAT_R8G8_UNORM;
  874. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  875. tf.width = p_ssao_buffers.buffer_width;
  876. tf.height = p_ssao_buffers.buffer_height;
  877. tf.array_layers = 4;
  878. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  879. p_ssao_buffers.ao_pong = RD::get_singleton()->texture_create(tf, RD::TextureView());
  880. RD::get_singleton()->set_resource_name(p_ssao_buffers.ao_pong, "SSAO De-interleaved Array Pong");
  881. for (uint32_t i = 0; i < 4; i++) {
  882. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssao_buffers.ao_pong, i, 0);
  883. p_ssao_buffers.ao_pong_slices.push_back(slice);
  884. RD::get_singleton()->set_resource_name(slice, "SSAO De-interleaved Array Layer " + itos(i) + " Pong");
  885. }
  886. }
  887. {
  888. RD::TextureFormat tf;
  889. tf.format = RD::DATA_FORMAT_R8_UNORM;
  890. tf.width = p_ssao_buffers.buffer_width;
  891. tf.height = p_ssao_buffers.buffer_height;
  892. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  893. p_ssao_buffers.importance_map[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  894. RD::get_singleton()->set_resource_name(p_ssao_buffers.importance_map[0], "SSAO Importance Map");
  895. p_ssao_buffers.importance_map[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  896. RD::get_singleton()->set_resource_name(p_ssao_buffers.importance_map[1], "SSAO Importance Map Pong");
  897. }
  898. {
  899. RD::TextureFormat tf;
  900. tf.format = RD::DATA_FORMAT_R8_UNORM;
  901. tf.width = p_settings.full_screen_size.x;
  902. tf.height = p_settings.full_screen_size.y;
  903. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  904. p_ssao_buffers.ao_final = RD::get_singleton()->texture_create(tf, RD::TextureView());
  905. RD::get_singleton()->set_resource_name(p_ssao_buffers.ao_final, "SSAO Final");
  906. }
  907. p_ssao_buffers.half_size = p_settings.half_size;
  908. }
  909. }
  910. void SSEffects::generate_ssao(SSAORenderBuffers &p_ssao_buffers, RID p_normal_buffer, const CameraMatrix &p_projection, const SSAOSettings &p_settings) {
  911. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  912. ERR_FAIL_NULL(uniform_set_cache);
  913. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  914. ERR_FAIL_NULL(material_storage);
  915. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  916. memset(&ssao.gather_push_constant, 0, sizeof(SSAOGatherPushConstant));
  917. /* FIRST PASS */
  918. RID shader = ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 0);
  919. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  920. RD::get_singleton()->draw_command_begin_label("Process Screen Space Ambient Occlusion");
  921. /* SECOND PASS */
  922. // Sample SSAO
  923. {
  924. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  925. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  926. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  927. ssao.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  928. ssao.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  929. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  930. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  931. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  932. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  933. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  934. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  935. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  936. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  937. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  938. ssao.gather_push_constant.radius = p_settings.radius;
  939. float radius_near_limit = (p_settings.radius * 1.2f);
  940. if (p_settings.quality <= RS::ENV_SSAO_QUALITY_LOW) {
  941. radius_near_limit *= 1.50f;
  942. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  943. ssao.gather_push_constant.radius *= 0.8f;
  944. }
  945. }
  946. radius_near_limit /= tan_half_fov_y;
  947. ssao.gather_push_constant.intensity = p_settings.intensity;
  948. ssao.gather_push_constant.shadow_power = p_settings.power;
  949. ssao.gather_push_constant.shadow_clamp = 0.98;
  950. ssao.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  951. ssao.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  952. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  953. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  954. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  955. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_ssao_buffers.half_buffer_width) * (p_ssao_buffers.half_buffer_height) * 255);
  956. ssao.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  957. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  958. ssao.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  959. ssao.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  960. if (p_ssao_buffers.gather_uniform_set.is_null()) {
  961. Vector<RD::Uniform> uniforms;
  962. {
  963. RD::Uniform u;
  964. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  965. u.binding = 0;
  966. u.append_id(default_sampler);
  967. u.append_id(p_ssao_buffers.depth_texture_view);
  968. uniforms.push_back(u);
  969. }
  970. {
  971. RD::Uniform u;
  972. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  973. u.binding = 1;
  974. u.append_id(p_normal_buffer);
  975. uniforms.push_back(u);
  976. }
  977. {
  978. RD::Uniform u;
  979. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  980. u.binding = 2;
  981. u.append_id(ss_effects.gather_constants_buffer);
  982. uniforms.push_back(u);
  983. }
  984. p_ssao_buffers.gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader, 0);
  985. RD::get_singleton()->set_resource_name(p_ssao_buffers.gather_uniform_set, "SSAO Gather Uniform Set");
  986. }
  987. if (p_ssao_buffers.importance_map_uniform_set.is_null()) {
  988. Vector<RD::Uniform> uniforms;
  989. {
  990. RD::Uniform u;
  991. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  992. u.binding = 0;
  993. u.append_id(p_ssao_buffers.ao_pong);
  994. uniforms.push_back(u);
  995. }
  996. {
  997. RD::Uniform u;
  998. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  999. u.binding = 1;
  1000. u.append_id(default_sampler);
  1001. u.append_id(p_ssao_buffers.importance_map[0]);
  1002. uniforms.push_back(u);
  1003. }
  1004. {
  1005. RD::Uniform u;
  1006. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1007. u.binding = 2;
  1008. u.append_id(ssao.importance_map_load_counter);
  1009. uniforms.push_back(u);
  1010. }
  1011. p_ssao_buffers.importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 2), 1);
  1012. RD::get_singleton()->set_resource_name(p_ssao_buffers.importance_map_uniform_set, "SSAO Importance Map Uniform Set");
  1013. }
  1014. if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  1015. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  1016. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  1017. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  1018. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  1019. ssao.importance_map_push_constant.power = p_settings.power;
  1020. //base pass
  1021. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  1022. gather_ssao(compute_list, p_ssao_buffers.ao_pong_slices, p_settings, true, p_ssao_buffers.gather_uniform_set, RID());
  1023. //generate importance map
  1024. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  1025. RD::Uniform u_ao_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.ao_pong }));
  1026. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ao_pong_with_sampler), 0);
  1027. RD::Uniform u_importance_map(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssao_buffers.importance_map[0] }));
  1028. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map), 1);
  1029. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1030. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1031. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1032. //process importance map A
  1033. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  1034. RD::Uniform u_importance_map_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.importance_map[0] }));
  1035. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_importance_map_with_sampler), 0);
  1036. RD::Uniform u_importance_map_pong(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssao_buffers.importance_map[1] }));
  1037. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map_pong), 1);
  1038. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1039. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1040. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1041. //process Importance Map B
  1042. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  1043. RD::Uniform u_importance_map_pong_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.importance_map[1] }));
  1044. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_importance_map_pong_with_sampler), 0);
  1045. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_importance_map), 1);
  1046. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  1047. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1048. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_ssao_buffers.half_buffer_width, p_ssao_buffers.half_buffer_height, 1);
  1049. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1050. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  1051. RD::get_singleton()->draw_command_end_label(); // Importance Map
  1052. } else {
  1053. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  1054. }
  1055. gather_ssao(compute_list, p_ssao_buffers.ao_deinterleaved_slices, p_settings, false, p_ssao_buffers.gather_uniform_set, p_ssao_buffers.importance_map_uniform_set);
  1056. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  1057. }
  1058. // /* THIRD PASS */
  1059. // // Blur
  1060. //
  1061. {
  1062. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  1063. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  1064. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_ssao_buffers.buffer_width;
  1065. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_ssao_buffers.buffer_height;
  1066. int blur_passes = p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  1067. shader = ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0);
  1068. for (int pass = 0; pass < blur_passes; pass++) {
  1069. int blur_pipeline = SSAO_BLUR_PASS;
  1070. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1071. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1072. if (pass < blur_passes - 2) {
  1073. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  1074. } else {
  1075. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1076. }
  1077. }
  1078. for (int i = 0; i < 4; i++) {
  1079. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1080. continue;
  1081. }
  1082. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  1083. if (pass % 2 == 0) {
  1084. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1085. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.ao_deinterleaved_slices[i] }));
  1086. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ao_slices_with_sampler), 0);
  1087. } else {
  1088. RD::Uniform u_ao_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, p_ssao_buffers.ao_deinterleaved_slices[i] }));
  1089. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ao_slices_with_sampler), 0);
  1090. }
  1091. RD::Uniform u_ao_pong_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssao_buffers.ao_pong_slices[i] }));
  1092. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ao_pong_slices), 1);
  1093. } else {
  1094. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1095. RD::Uniform u_ao_pong_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.ao_pong_slices[i] }));
  1096. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ao_pong_slices_with_sampler), 0);
  1097. } else {
  1098. RD::Uniform u_ao_pong_slices_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ ss_effects.mirror_sampler, p_ssao_buffers.ao_pong_slices[i] }));
  1099. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_ao_pong_slices_with_sampler), 0);
  1100. }
  1101. RD::Uniform u_ao_slices(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssao_buffers.ao_deinterleaved_slices[i] }));
  1102. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ao_slices), 1);
  1103. }
  1104. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  1105. Size2i size(p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1), p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  1106. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  1107. }
  1108. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1109. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1110. }
  1111. }
  1112. RD::get_singleton()->draw_command_end_label(); // Blur
  1113. }
  1114. /* FOURTH PASS */
  1115. // Interleave buffers
  1116. // back to full size
  1117. {
  1118. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1119. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1120. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1121. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1122. ssao.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  1123. shader = ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, 0);
  1124. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1125. if (p_settings.quality == RS::ENV_SSAO_QUALITY_LOW) {
  1126. interleave_pipeline = SSAO_INTERLEAVE;
  1127. } else if (p_settings.quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1128. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1129. }
  1130. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  1131. RD::Uniform u_upscale_buffer(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssao_buffers.ao_final }));
  1132. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_upscale_buffer), 0);
  1133. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  1134. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.ao_deinterleaved }));
  1135. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ao), 1);
  1136. } else {
  1137. RD::Uniform u_ao(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_ssao_buffers.ao_pong }));
  1138. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_ao), 1);
  1139. }
  1140. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1141. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1142. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1143. RD::get_singleton()->draw_command_end_label(); // Interleave
  1144. }
  1145. RD::get_singleton()->draw_command_end_label(); //SSAO
  1146. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER); //wait for upcoming transfer
  1147. int zero[1] = { 0 };
  1148. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1149. }
  1150. void SSEffects::ssao_free(SSAORenderBuffers &p_ssao_buffers) {
  1151. if (p_ssao_buffers.ao_final.is_valid()) {
  1152. RD::get_singleton()->free(p_ssao_buffers.ao_deinterleaved);
  1153. RD::get_singleton()->free(p_ssao_buffers.ao_pong);
  1154. RD::get_singleton()->free(p_ssao_buffers.ao_final);
  1155. RD::get_singleton()->free(p_ssao_buffers.importance_map[0]);
  1156. RD::get_singleton()->free(p_ssao_buffers.importance_map[1]);
  1157. p_ssao_buffers.ao_deinterleaved = RID();
  1158. p_ssao_buffers.ao_pong = RID();
  1159. p_ssao_buffers.ao_final = RID();
  1160. p_ssao_buffers.importance_map[0] = RID();
  1161. p_ssao_buffers.importance_map[1] = RID();
  1162. p_ssao_buffers.ao_deinterleaved_slices.clear();
  1163. p_ssao_buffers.ao_pong_slices.clear();
  1164. p_ssao_buffers.gather_uniform_set = RID();
  1165. p_ssao_buffers.importance_map_uniform_set = RID();
  1166. }
  1167. }
  1168. /* Screen Space Reflection */
  1169. void SSEffects::ssr_allocate_buffers(SSRRenderBuffers &p_ssr_buffers, const RenderingDevice::DataFormat p_color_format, RenderingServer::EnvironmentSSRRoughnessQuality p_roughness_quality, const Size2i &p_screen_size, const uint32_t p_view_count) {
  1170. // As we are processing one view at a time, we can reuse buffers, only our output needs to have layers for each view.
  1171. if (p_ssr_buffers.depth_scaled.is_null()) {
  1172. RD::TextureFormat tf;
  1173. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1174. tf.width = p_screen_size.x;
  1175. tf.height = p_screen_size.y;
  1176. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1177. tf.array_layers = 1;
  1178. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1179. p_ssr_buffers.depth_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1180. RD::get_singleton()->set_resource_name(p_ssr_buffers.depth_scaled, "SSR Depth Scaled");
  1181. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1182. p_ssr_buffers.normal_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1183. RD::get_singleton()->set_resource_name(p_ssr_buffers.normal_scaled, "SSR Normal Scaled");
  1184. }
  1185. if (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED && !p_ssr_buffers.blur_radius[0].is_valid()) {
  1186. RD::TextureFormat tf;
  1187. tf.format = RD::DATA_FORMAT_R8_UNORM;
  1188. tf.width = p_screen_size.x;
  1189. tf.height = p_screen_size.y;
  1190. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1191. tf.array_layers = 1;
  1192. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1193. p_ssr_buffers.blur_radius[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1194. RD::get_singleton()->set_resource_name(p_ssr_buffers.blur_radius[0], "SSR Blur Radius 0");
  1195. p_ssr_buffers.blur_radius[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1196. RD::get_singleton()->set_resource_name(p_ssr_buffers.blur_radius[1], "SSR Blur Radius 1");
  1197. }
  1198. if (p_ssr_buffers.intermediate.is_null()) {
  1199. RD::TextureFormat tf;
  1200. tf.format = p_color_format;
  1201. tf.width = p_screen_size.x;
  1202. tf.height = p_screen_size.y;
  1203. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1204. tf.array_layers = 1;
  1205. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1206. p_ssr_buffers.intermediate = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1207. RD::get_singleton()->set_resource_name(p_ssr_buffers.intermediate, "SSR Intermediate");
  1208. if (p_view_count > 1) {
  1209. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  1210. tf.array_layers = p_view_count;
  1211. } else {
  1212. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1213. tf.array_layers = 1;
  1214. }
  1215. p_ssr_buffers.output = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1216. RD::get_singleton()->set_resource_name(p_ssr_buffers.output, "SSR Output");
  1217. for (uint32_t v = 0; v < p_view_count; v++) {
  1218. p_ssr_buffers.output_slices[v] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_ssr_buffers.output, v, 0);
  1219. }
  1220. }
  1221. }
  1222. void SSEffects::screen_space_reflection(SSRRenderBuffers &p_ssr_buffers, const RID *p_diffuse_slices, const RID *p_normal_roughness_slices, RenderingServer::EnvironmentSSRRoughnessQuality p_roughness_quality, const RID *p_metallic_slices, const Color &p_metallic_mask, const RID *p_depth_slices, const Size2i &p_screen_size, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const uint32_t p_view_count, const CameraMatrix *p_projections, const Vector3 *p_eye_offsets) {
  1223. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  1224. ERR_FAIL_NULL(uniform_set_cache);
  1225. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1226. ERR_FAIL_NULL(material_storage);
  1227. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1228. {
  1229. // Store some scene data in a UBO, in the near future we will use a UBO shared with other shaders
  1230. ScreenSpaceReflectionSceneData scene_data;
  1231. if (ssr.ubo.is_null()) {
  1232. ssr.ubo = RD::get_singleton()->uniform_buffer_create(sizeof(ScreenSpaceReflectionSceneData));
  1233. }
  1234. for (uint32_t v = 0; v < p_view_count; v++) {
  1235. store_camera(p_projections[v], scene_data.projection[v]);
  1236. store_camera(p_projections[v].inverse(), scene_data.inv_projection[v]);
  1237. scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
  1238. scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
  1239. scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
  1240. scene_data.eye_offset[v][3] = 0.0;
  1241. }
  1242. RD::get_singleton()->buffer_update(ssr.ubo, 0, sizeof(ScreenSpaceReflectionSceneData), &scene_data, RD::BARRIER_MASK_COMPUTE);
  1243. }
  1244. uint32_t pipeline_specialization = 0;
  1245. if (p_view_count > 1) {
  1246. pipeline_specialization |= SSR_MULTIVIEW;
  1247. }
  1248. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1249. for (uint32_t v = 0; v < p_view_count; v++) {
  1250. RD::get_singleton()->draw_command_begin_label(String("SSR View ") + itos(v));
  1251. { //scale color and depth to half
  1252. RD::get_singleton()->draw_command_begin_label("SSR Scale");
  1253. ScreenSpaceReflectionScalePushConstant push_constant;
  1254. push_constant.view_index = v;
  1255. push_constant.camera_z_far = p_projections[v].get_z_far();
  1256. push_constant.camera_z_near = p_projections[v].get_z_near();
  1257. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1258. push_constant.filter = false; //enabling causes arctifacts
  1259. push_constant.screen_size[0] = p_screen_size.x;
  1260. push_constant.screen_size[1] = p_screen_size.y;
  1261. RID shader = ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0);
  1262. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipelines[pipeline_specialization]);
  1263. RD::Uniform u_diffuse(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_diffuse_slices[v] }));
  1264. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_diffuse), 0);
  1265. RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_depth_slices[v] }));
  1266. RD::Uniform u_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_normal_roughness_slices[v] }));
  1267. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth, u_normal_roughness), 1);
  1268. RD::Uniform u_output_blur(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.output_slices[v] }));
  1269. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_output_blur), 2);
  1270. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.depth_scaled }));
  1271. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_ssr_buffers.normal_scaled }));
  1272. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth, u_scale_normal), 3);
  1273. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  1274. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1275. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1276. RD::get_singleton()->draw_command_end_label();
  1277. }
  1278. {
  1279. RD::get_singleton()->draw_command_begin_label("SSR main");
  1280. ScreenSpaceReflectionPushConstant push_constant;
  1281. push_constant.view_index = v;
  1282. push_constant.camera_z_far = p_projections[v].get_z_far();
  1283. push_constant.camera_z_near = p_projections[v].get_z_near();
  1284. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1285. push_constant.screen_size[0] = p_screen_size.x;
  1286. push_constant.screen_size[1] = p_screen_size.y;
  1287. push_constant.curve_fade_in = p_fade_in;
  1288. push_constant.distance_fade = p_fade_out;
  1289. push_constant.num_steps = p_max_steps;
  1290. push_constant.depth_tolerance = p_tolerance;
  1291. push_constant.use_half_res = true;
  1292. push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_projections[v].matrix[0][0]);
  1293. push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_projections[v].matrix[1][1]);
  1294. push_constant.proj_info[2] = (1.0f - p_projections[v].matrix[0][2]) / p_projections[v].matrix[0][0];
  1295. push_constant.proj_info[3] = (1.0f + p_projections[v].matrix[1][2]) / p_projections[v].matrix[1][1];
  1296. push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  1297. push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  1298. push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  1299. push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  1300. ScreenSpaceReflectionMode mode = (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL;
  1301. RID shader = ssr.shader.version_get_shader(ssr.shader_version, mode);
  1302. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.pipelines[pipeline_specialization][mode]);
  1303. RD::Uniform u_scene_data(RD::UNIFORM_TYPE_UNIFORM_BUFFER, 0, ssr.ubo);
  1304. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1305. RD::Uniform u_output_blur(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.output_slices[v] }));
  1306. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_ssr_buffers.depth_scaled }));
  1307. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_output_blur, u_scale_depth), 0);
  1308. if (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  1309. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.intermediate }));
  1310. RD::Uniform u_blur_radius(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_ssr_buffers.blur_radius[0] }));
  1311. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_intermediate, u_blur_radius), 1);
  1312. } else {
  1313. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.intermediate }));
  1314. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_intermediate), 1);
  1315. }
  1316. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.normal_scaled }));
  1317. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_scale_normal), 2);
  1318. RD::Uniform u_metallic(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_metallic_slices[v] }));
  1319. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_metallic), 3);
  1320. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  1321. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1322. RD::get_singleton()->draw_command_end_label();
  1323. }
  1324. if (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  1325. RD::get_singleton()->draw_command_begin_label("SSR filter");
  1326. //blur
  1327. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1328. ScreenSpaceReflectionFilterPushConstant push_constant;
  1329. push_constant.view_index = v;
  1330. push_constant.orthogonal = p_projections[v].is_orthogonal();
  1331. push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  1332. push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_projections[v].matrix[0][0]);
  1333. push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_projections[v].matrix[1][1]);
  1334. push_constant.proj_info[2] = (1.0f - p_projections[v].matrix[0][2]) / p_projections[v].matrix[0][0];
  1335. push_constant.proj_info[3] = (1.0f + p_projections[v].matrix[1][2]) / p_projections[v].matrix[1][1];
  1336. push_constant.vertical = 0;
  1337. if (p_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_LOW) {
  1338. push_constant.steps = p_max_steps / 3;
  1339. push_constant.increment = 3;
  1340. } else if (p_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_MEDIUM) {
  1341. push_constant.steps = p_max_steps / 2;
  1342. push_constant.increment = 2;
  1343. } else {
  1344. push_constant.steps = p_max_steps;
  1345. push_constant.increment = 1;
  1346. }
  1347. push_constant.screen_size[0] = p_screen_size.width;
  1348. push_constant.screen_size[1] = p_screen_size.height;
  1349. // Horizontal pass
  1350. SSRReflectionMode mode = SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL;
  1351. RID shader = ssr_filter.shader.version_get_shader(ssr_filter.shader_version, mode);
  1352. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[pipeline_specialization][mode]);
  1353. RD::Uniform u_scene_data(RD::UNIFORM_TYPE_UNIFORM_BUFFER, 0, ssr.ubo);
  1354. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1355. RD::Uniform u_intermediate(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.intermediate }));
  1356. RD::Uniform u_blur_radius(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_ssr_buffers.blur_radius[0] }));
  1357. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_intermediate, u_blur_radius), 0);
  1358. RD::Uniform u_scale_normal(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.normal_scaled }));
  1359. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_scale_normal), 1);
  1360. RD::Uniform u_output_blur(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.output_slices[v] }));
  1361. RD::Uniform u_blur_radius2(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_ssr_buffers.blur_radius[1] }));
  1362. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_output_blur, u_blur_radius2), 2);
  1363. RD::Uniform u_scale_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ssr_buffers.depth_scaled }));
  1364. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth), 3);
  1365. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  1366. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1367. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1368. // Vertical pass
  1369. mode = SCREEN_SPACE_REFLECTION_FILTER_VERTICAL;
  1370. shader = ssr_filter.shader.version_get_shader(ssr_filter.shader_version, mode);
  1371. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[pipeline_specialization][mode]);
  1372. push_constant.vertical = 1;
  1373. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_output_blur, u_blur_radius2), 0);
  1374. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_scale_normal), 1);
  1375. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_intermediate), 2);
  1376. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_scale_depth), 3);
  1377. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 4, u_scene_data), 4);
  1378. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  1379. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  1380. if (v != p_view_count - 1) {
  1381. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1382. }
  1383. RD::get_singleton()->draw_command_end_label();
  1384. }
  1385. RD::get_singleton()->draw_command_end_label();
  1386. }
  1387. RD::get_singleton()->compute_list_end();
  1388. }
  1389. void SSEffects::ssr_free(SSRRenderBuffers &p_ssr_buffers) {
  1390. for (uint32_t v = 0; v < RendererSceneRender::MAX_RENDER_VIEWS; v++) {
  1391. p_ssr_buffers.output_slices[v] = RID();
  1392. }
  1393. if (p_ssr_buffers.output.is_valid()) {
  1394. RD::get_singleton()->free(p_ssr_buffers.output);
  1395. p_ssr_buffers.output = RID();
  1396. }
  1397. if (p_ssr_buffers.intermediate.is_valid()) {
  1398. RD::get_singleton()->free(p_ssr_buffers.intermediate);
  1399. p_ssr_buffers.intermediate = RID();
  1400. }
  1401. if (p_ssr_buffers.blur_radius[0].is_valid()) {
  1402. RD::get_singleton()->free(p_ssr_buffers.blur_radius[0]);
  1403. RD::get_singleton()->free(p_ssr_buffers.blur_radius[1]);
  1404. p_ssr_buffers.blur_radius[0] = RID();
  1405. p_ssr_buffers.blur_radius[1] = RID();
  1406. }
  1407. if (p_ssr_buffers.depth_scaled.is_valid()) {
  1408. RD::get_singleton()->free(p_ssr_buffers.depth_scaled);
  1409. p_ssr_buffers.depth_scaled = RID();
  1410. RD::get_singleton()->free(p_ssr_buffers.normal_scaled);
  1411. p_ssr_buffers.normal_scaled = RID();
  1412. }
  1413. }