effects_rd.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /*************************************************************************/
  2. /* effects_rd.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 "effects_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/os.h"
  34. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  35. #include "thirdparty/misc/cubemap_coeffs.h"
  36. bool EffectsRD::get_prefer_raster_effects() {
  37. return prefer_raster_effects;
  38. }
  39. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  40. for (int i = 0; i < 4; i++) {
  41. for (int j = 0; j < 4; j++) {
  42. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  43. }
  44. }
  45. }
  46. RID EffectsRD::_get_uniform_set_from_image(RID p_image) {
  47. if (image_to_uniform_set_cache.has(p_image)) {
  48. RID uniform_set = image_to_uniform_set_cache[p_image];
  49. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  50. return uniform_set;
  51. }
  52. }
  53. Vector<RD::Uniform> uniforms;
  54. RD::Uniform u;
  55. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  56. u.binding = 0;
  57. u.append_id(p_image);
  58. uniforms.push_back(u);
  59. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  60. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  61. image_to_uniform_set_cache[p_image] = uniform_set;
  62. return uniform_set;
  63. }
  64. RID EffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  65. if (texture_to_uniform_set_cache.has(p_texture)) {
  66. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  67. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  68. return uniform_set;
  69. }
  70. }
  71. Vector<RD::Uniform> uniforms;
  72. RD::Uniform u;
  73. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  74. u.binding = 0;
  75. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  76. u.append_id(p_texture);
  77. uniforms.push_back(u);
  78. // anything with the same configuration (one texture in binding 0 for set 0), is good
  79. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, specular_merge.shader.version_get_shader(specular_merge.shader_version, 0), 0);
  80. texture_to_uniform_set_cache[p_texture] = uniform_set;
  81. return uniform_set;
  82. }
  83. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  84. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  85. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  86. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  87. return uniform_set;
  88. }
  89. }
  90. Vector<RD::Uniform> uniforms;
  91. RD::Uniform u;
  92. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  93. u.binding = 0;
  94. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  95. u.append_id(p_texture);
  96. uniforms.push_back(u);
  97. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  98. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  99. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  100. return uniform_set;
  101. }
  102. RID EffectsRD::_get_compute_uniform_set_from_texture_and_sampler(RID p_texture, RID p_sampler) {
  103. TextureSamplerPair tsp;
  104. tsp.texture = p_texture;
  105. tsp.sampler = p_sampler;
  106. if (texture_sampler_to_compute_uniform_set_cache.has(tsp)) {
  107. RID uniform_set = texture_sampler_to_compute_uniform_set_cache[tsp];
  108. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  109. return uniform_set;
  110. }
  111. }
  112. Vector<RD::Uniform> uniforms;
  113. RD::Uniform u;
  114. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  115. u.binding = 0;
  116. u.append_id(p_sampler);
  117. u.append_id(p_texture);
  118. uniforms.push_back(u);
  119. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  120. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0), 0);
  121. texture_sampler_to_compute_uniform_set_cache[tsp] = uniform_set;
  122. return uniform_set;
  123. }
  124. RID EffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  125. TexturePair tp;
  126. tp.texture1 = p_texture1;
  127. tp.texture2 = p_texture2;
  128. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  129. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  130. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  131. return uniform_set;
  132. }
  133. }
  134. Vector<RD::Uniform> uniforms;
  135. {
  136. RD::Uniform u;
  137. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  138. u.binding = 0;
  139. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  140. u.append_id(p_texture1);
  141. uniforms.push_back(u);
  142. }
  143. {
  144. RD::Uniform u;
  145. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  146. u.binding = 1;
  147. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  148. u.append_id(p_texture2);
  149. uniforms.push_back(u);
  150. }
  151. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  152. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  153. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  154. return uniform_set;
  155. }
  156. RID EffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  157. TexturePair tp;
  158. tp.texture1 = p_texture1;
  159. tp.texture2 = p_texture2;
  160. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  161. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  162. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  163. return uniform_set;
  164. }
  165. }
  166. Vector<RD::Uniform> uniforms;
  167. {
  168. RD::Uniform u;
  169. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  170. u.binding = 0;
  171. u.append_id(p_texture1);
  172. uniforms.push_back(u);
  173. }
  174. {
  175. RD::Uniform u;
  176. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  177. u.binding = 1;
  178. u.append_id(p_texture2);
  179. uniforms.push_back(u);
  180. }
  181. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  182. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  183. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  184. return uniform_set;
  185. }
  186. void EffectsRD::fsr_upscale(RID p_source_rd_texture, RID p_secondary_texture, RID p_destination_texture, const Size2i &p_internal_size, const Size2i &p_size, float p_fsr_upscale_sharpness) {
  187. memset(&FSR_upscale.push_constant, 0, sizeof(FSRUpscalePushConstant));
  188. int dispatch_x = (p_size.x + 15) / 16;
  189. int dispatch_y = (p_size.y + 15) / 16;
  190. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  191. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, FSR_upscale.pipeline);
  192. FSR_upscale.push_constant.resolution_width = p_internal_size.width;
  193. FSR_upscale.push_constant.resolution_height = p_internal_size.height;
  194. FSR_upscale.push_constant.upscaled_width = p_size.width;
  195. FSR_upscale.push_constant.upscaled_height = p_size.height;
  196. FSR_upscale.push_constant.sharpness = p_fsr_upscale_sharpness;
  197. //FSR Easc
  198. FSR_upscale.push_constant.pass = FSR_UPSCALE_PASS_EASU;
  199. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  200. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 1);
  201. RD::get_singleton()->compute_list_set_push_constant(compute_list, &FSR_upscale.push_constant, sizeof(FSRUpscalePushConstant));
  202. RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);
  203. RD::get_singleton()->compute_list_add_barrier(compute_list);
  204. //FSR Rcas
  205. FSR_upscale.push_constant.pass = FSR_UPSCALE_PASS_RCAS;
  206. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 0);
  207. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_destination_texture), 1);
  208. RD::get_singleton()->compute_list_set_push_constant(compute_list, &FSR_upscale.push_constant, sizeof(FSRUpscalePushConstant));
  209. RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);
  210. RD::get_singleton()->compute_list_end(compute_list);
  211. }
  212. void EffectsRD::taa_resolve(RID p_frame, RID p_temp, RID p_depth, RID p_velocity, RID p_prev_velocity, RID p_history, Size2 p_resolution, float p_z_near, float p_z_far) {
  213. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  214. ERR_FAIL_NULL(uniform_set_cache);
  215. RID shader = TAA_resolve.shader.version_get_shader(TAA_resolve.shader_version, 0);
  216. ERR_FAIL_COND(shader.is_null());
  217. memset(&TAA_resolve.push_constant, 0, sizeof(TAAResolvePushConstant));
  218. TAA_resolve.push_constant.resolution_width = p_resolution.width;
  219. TAA_resolve.push_constant.resolution_height = p_resolution.height;
  220. TAA_resolve.push_constant.disocclusion_threshold = 0.025f;
  221. TAA_resolve.push_constant.disocclusion_scale = 10.0f;
  222. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  223. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, TAA_resolve.pipeline);
  224. RD::Uniform u_frame_source(RD::UNIFORM_TYPE_IMAGE, 0, { p_frame });
  225. RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, { default_sampler, p_depth });
  226. RD::Uniform u_velocity(RD::UNIFORM_TYPE_IMAGE, 2, { p_velocity });
  227. RD::Uniform u_prev_velocity(RD::UNIFORM_TYPE_IMAGE, 3, { p_prev_velocity });
  228. RD::Uniform u_history(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 4, { default_sampler, p_history });
  229. RD::Uniform u_frame_dest(RD::UNIFORM_TYPE_IMAGE, 5, { p_temp });
  230. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_frame_source, u_depth, u_velocity, u_prev_velocity, u_history, u_frame_dest), 0);
  231. RD::get_singleton()->compute_list_set_push_constant(compute_list, &TAA_resolve.push_constant, sizeof(TAAResolvePushConstant));
  232. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_resolution.width, p_resolution.height, 1);
  233. RD::get_singleton()->compute_list_end();
  234. }
  235. void EffectsRD::screen_space_reflection(RID p_diffuse, RID p_normal_roughness, RenderingServer::EnvironmentSSRRoughnessQuality p_roughness_quality, RID p_blur_radius, RID p_blur_radius2, RID p_metallic, const Color &p_metallic_mask, RID p_depth, RID p_scale_depth, RID p_scale_normal, RID p_output, RID p_output_blur, const Size2i &p_screen_size, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const CameraMatrix &p_camera) {
  236. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  237. { //scale color and depth to half
  238. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  239. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  240. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  241. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  242. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  243. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  244. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  245. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  246. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  247. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  248. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  249. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  250. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  251. RD::get_singleton()->compute_list_add_barrier(compute_list);
  252. }
  253. {
  254. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  255. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  256. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  257. ssr.push_constant.screen_size[0] = p_screen_size.x;
  258. ssr.push_constant.screen_size[1] = p_screen_size.y;
  259. ssr.push_constant.curve_fade_in = p_fade_in;
  260. ssr.push_constant.distance_fade = p_fade_out;
  261. ssr.push_constant.num_steps = p_max_steps;
  262. ssr.push_constant.depth_tolerance = p_tolerance;
  263. ssr.push_constant.use_half_res = true;
  264. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  265. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  266. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  267. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  268. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  269. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  270. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  271. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  272. store_camera(p_camera, ssr.push_constant.projection);
  273. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.pipelines[(p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL]);
  274. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  275. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  276. if (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  277. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  278. } else {
  279. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  280. }
  281. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  282. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  283. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  284. }
  285. if (p_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) {
  286. //blur
  287. RD::get_singleton()->compute_list_add_barrier(compute_list);
  288. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  289. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  290. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  291. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  292. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  293. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  294. ssr_filter.push_constant.vertical = 0;
  295. if (p_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_LOW) {
  296. ssr_filter.push_constant.steps = p_max_steps / 3;
  297. ssr_filter.push_constant.increment = 3;
  298. } else if (p_roughness_quality == RS::ENV_SSR_ROUGHNESS_QUALITY_MEDIUM) {
  299. ssr_filter.push_constant.steps = p_max_steps / 2;
  300. ssr_filter.push_constant.increment = 2;
  301. } else {
  302. ssr_filter.push_constant.steps = p_max_steps;
  303. ssr_filter.push_constant.increment = 1;
  304. }
  305. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  306. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  307. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  308. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  309. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  310. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  311. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  312. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  313. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  314. RD::get_singleton()->compute_list_add_barrier(compute_list);
  315. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  316. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  317. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  318. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  319. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  320. ssr_filter.push_constant.vertical = 1;
  321. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  322. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  323. }
  324. RD::get_singleton()->compute_list_end();
  325. }
  326. void EffectsRD::sub_surface_scattering(RID p_diffuse, RID p_diffuse2, RID p_depth, const CameraMatrix &p_camera, const Size2i &p_screen_size, float p_scale, float p_depth_scale, RenderingServer::SubSurfaceScatteringQuality p_quality) {
  327. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  328. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  329. p.normal /= p.d;
  330. float unit_size = p.normal.x;
  331. { //scale color and depth to half
  332. sss.push_constant.camera_z_far = p_camera.get_z_far();
  333. sss.push_constant.camera_z_near = p_camera.get_z_near();
  334. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  335. sss.push_constant.unit_size = unit_size;
  336. sss.push_constant.screen_size[0] = p_screen_size.x;
  337. sss.push_constant.screen_size[1] = p_screen_size.y;
  338. sss.push_constant.vertical = false;
  339. sss.push_constant.scale = p_scale;
  340. sss.push_constant.depth_scale = p_depth_scale;
  341. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  342. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  343. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  344. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  345. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  346. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  347. RD::get_singleton()->compute_list_add_barrier(compute_list);
  348. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  349. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  350. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  351. sss.push_constant.vertical = true;
  352. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  353. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  354. RD::get_singleton()->compute_list_end();
  355. }
  356. }
  357. void EffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  358. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, Vector<Color>());
  359. if (p_reflection.is_valid()) {
  360. if (p_base.is_valid()) {
  361. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_SSR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  362. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  363. } else {
  364. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADDITIVE_SSR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  365. }
  366. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  367. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  368. } else {
  369. if (p_base.is_valid()) {
  370. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADD].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  371. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  372. } else {
  373. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADDITIVE_ADD].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  374. }
  375. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  376. }
  377. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  378. RD::get_singleton()->draw_list_draw(draw_list, true);
  379. RD::get_singleton()->draw_list_end();
  380. }
  381. void EffectsRD::luminance_reduction(RID p_source_texture, const Size2i p_source_size, const Vector<RID> p_reduce, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set) {
  382. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of luminance reduction with the mobile renderer.");
  383. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  384. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  385. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  386. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  387. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  388. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  389. for (int i = 0; i < p_reduce.size(); i++) {
  390. if (i == 0) {
  391. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  392. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  393. } else {
  394. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  395. if (i == p_reduce.size() - 1 && !p_set) {
  396. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  397. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  398. } else {
  399. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  400. }
  401. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  402. }
  403. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  404. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  405. RD::get_singleton()->compute_list_dispatch_threads(compute_list, luminance_reduce.push_constant.source_size[0], luminance_reduce.push_constant.source_size[1], 1);
  406. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  407. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  408. }
  409. RD::get_singleton()->compute_list_end();
  410. }
  411. void EffectsRD::luminance_reduction_raster(RID p_source_texture, const Size2i p_source_size, const Vector<RID> p_reduce, Vector<RID> p_fb, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set) {
  412. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster version of luminance reduction with the clustered renderer.");
  413. ERR_FAIL_COND_MSG(p_reduce.size() != p_fb.size(), "Incorrect frame buffer account for luminance reduction.");
  414. luminance_reduce_raster.push_constant.max_luminance = p_max_luminance;
  415. luminance_reduce_raster.push_constant.min_luminance = p_min_luminance;
  416. luminance_reduce_raster.push_constant.exposure_adjust = p_adjust;
  417. for (int i = 0; i < p_reduce.size(); i++) {
  418. luminance_reduce_raster.push_constant.source_size[0] = i == 0 ? p_source_size.x : luminance_reduce_raster.push_constant.dest_size[0];
  419. luminance_reduce_raster.push_constant.source_size[1] = i == 0 ? p_source_size.y : luminance_reduce_raster.push_constant.dest_size[1];
  420. luminance_reduce_raster.push_constant.dest_size[0] = MAX(luminance_reduce_raster.push_constant.source_size[0] / 8, 1);
  421. luminance_reduce_raster.push_constant.dest_size[1] = MAX(luminance_reduce_raster.push_constant.source_size[1] / 8, 1);
  422. bool final = !p_set && (luminance_reduce_raster.push_constant.dest_size[0] == 1) && (luminance_reduce_raster.push_constant.dest_size[1] == 1);
  423. LuminanceReduceRasterMode mode = final ? LUMINANCE_REDUCE_FRAGMENT_FINAL : (i == 0 ? LUMINANCE_REDUCE_FRAGMENT_FIRST : LUMINANCE_REDUCE_FRAGMENT);
  424. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_fb[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  425. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, luminance_reduce_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_fb[i])));
  426. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(i == 0 ? p_source_texture : p_reduce[i - 1]), 0);
  427. if (final) {
  428. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_prev_luminance), 1);
  429. }
  430. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  431. RD::get_singleton()->draw_list_set_push_constant(draw_list, &luminance_reduce_raster.push_constant, sizeof(LuminanceReduceRasterPushConstant));
  432. RD::get_singleton()->draw_list_draw(draw_list, true);
  433. RD::get_singleton()->draw_list_end();
  434. }
  435. }
  436. void EffectsRD::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) {
  437. // Downsample and deinterleave the depth buffer for SSAO and SSIL
  438. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  439. int downsample_pipeline = SS_EFFECTS_DOWNSAMPLE;
  440. bool use_mips = p_ssao_quality > RS::ENV_SSAO_QUALITY_MEDIUM || p_ssil_quality > RS::ENV_SSIL_QUALITY_MEDIUM;
  441. if (p_ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW && p_ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  442. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_HALF;
  443. } else if (use_mips) {
  444. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  445. }
  446. bool use_half_size = false;
  447. bool use_full_mips = false;
  448. if (p_ssao_half_size && p_ssil_half_size) {
  449. downsample_pipeline++;
  450. use_half_size = true;
  451. } else if (p_ssao_half_size != p_ssil_half_size) {
  452. if (use_mips) {
  453. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_FULL_MIPS;
  454. use_full_mips = true;
  455. } else {
  456. // Only need the first two mipmaps, but the cost to generate the next two is trivial
  457. // TODO investigate the benefit of a shader version to generate only 2 mips
  458. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  459. use_mips = true;
  460. }
  461. }
  462. int depth_index = use_half_size ? 1 : 0;
  463. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  464. 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) {
  465. Vector<RD::Uniform> uniforms;
  466. {
  467. RD::Uniform u;
  468. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  469. u.binding = 0;
  470. u.append_id(p_depth_mipmaps[depth_index + 1]);
  471. uniforms.push_back(u);
  472. }
  473. {
  474. RD::Uniform u;
  475. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  476. u.binding = 1;
  477. u.append_id(p_depth_mipmaps[depth_index + 2]);
  478. uniforms.push_back(u);
  479. }
  480. {
  481. RD::Uniform u;
  482. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  483. u.binding = 2;
  484. u.append_id(p_depth_mipmaps[depth_index + 3]);
  485. uniforms.push_back(u);
  486. }
  487. if (use_full_mips) {
  488. RD::Uniform u;
  489. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  490. u.binding = 3;
  491. u.append_id(p_depth_mipmaps[4]);
  492. uniforms.push_back(u);
  493. }
  494. 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);
  495. }
  496. float depth_linearize_mul = -p_projection.matrix[3][2];
  497. float depth_linearize_add = p_projection.matrix[2][2];
  498. if (depth_linearize_mul * depth_linearize_add < 0) {
  499. depth_linearize_add = -depth_linearize_add;
  500. }
  501. ss_effects.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  502. ss_effects.downsample_push_constant.z_near = depth_linearize_mul;
  503. ss_effects.downsample_push_constant.z_far = depth_linearize_add;
  504. if (ss_effects.downsample_push_constant.orthogonal) {
  505. ss_effects.downsample_push_constant.z_near = p_projection.get_z_near();
  506. ss_effects.downsample_push_constant.z_far = p_projection.get_z_far();
  507. }
  508. ss_effects.downsample_push_constant.pixel_size[0] = 1.0 / p_full_screen_size.x;
  509. ss_effects.downsample_push_constant.pixel_size[1] = 1.0 / p_full_screen_size.y;
  510. ss_effects.downsample_push_constant.radius_sq = 1.0;
  511. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ss_effects.pipelines[downsample_pipeline]);
  512. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  513. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_depth_mipmaps[depth_index + 0]), 1);
  514. if (use_mips) {
  515. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ss_effects.downsample_uniform_set, 2);
  516. }
  517. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ss_effects.downsample_push_constant, sizeof(SSEffectsDownsamplePushConstant));
  518. 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)));
  519. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  520. RD::get_singleton()->compute_list_add_barrier(compute_list);
  521. RD::get_singleton()->draw_command_end_label();
  522. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_COMPUTE);
  523. ss_effects.used_full_mips_last_frame = use_full_mips;
  524. ss_effects.used_half_size_last_frame = use_half_size;
  525. }
  526. void EffectsRD::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) {
  527. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  528. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  529. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  530. }
  531. for (int i = 0; i < 4; i++) {
  532. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  533. continue;
  534. }
  535. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  536. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  537. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  538. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  539. ssao.gather_push_constant.pass = i;
  540. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 2);
  541. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  542. 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));
  543. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  544. }
  545. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  546. }
  547. void EffectsRD::generate_ssao(RID p_normal_buffer, RID p_depth_mipmaps_texture, RID p_ao, const Vector<RID> p_ao_slices, RID p_ao_pong, const Vector<RID> p_ao_pong_slices, RID p_upscale_buffer, RID p_importance_map, RID p_importance_map_pong, const CameraMatrix &p_projection, const SSAOSettings &p_settings, bool p_invalidate_uniform_sets, RID &r_gather_uniform_set, RID &r_importance_map_uniform_set) {
  548. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  549. memset(&ssao.gather_push_constant, 0, sizeof(SSAOGatherPushConstant));
  550. /* FIRST PASS */
  551. RD::get_singleton()->draw_command_begin_label("Process Screen Space Ambient Occlusion");
  552. /* SECOND PASS */
  553. // Sample SSAO
  554. {
  555. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  556. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  557. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  558. ssao.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  559. ssao.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  560. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  561. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  562. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  563. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  564. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  565. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  566. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  567. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  568. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  569. ssao.gather_push_constant.radius = p_settings.radius;
  570. float radius_near_limit = (p_settings.radius * 1.2f);
  571. if (p_settings.quality <= RS::ENV_SSAO_QUALITY_LOW) {
  572. radius_near_limit *= 1.50f;
  573. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  574. ssao.gather_push_constant.radius *= 0.8f;
  575. }
  576. }
  577. radius_near_limit /= tan_half_fov_y;
  578. ssao.gather_push_constant.intensity = p_settings.intensity;
  579. ssao.gather_push_constant.shadow_power = p_settings.power;
  580. ssao.gather_push_constant.shadow_clamp = 0.98;
  581. ssao.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  582. ssao.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  583. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  584. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  585. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  586. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  587. ssao.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  588. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  589. ssao.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  590. ssao.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  591. if (p_invalidate_uniform_sets) {
  592. Vector<RD::Uniform> uniforms;
  593. {
  594. RD::Uniform u;
  595. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  596. u.binding = 0;
  597. u.append_id(default_sampler);
  598. u.append_id(p_depth_mipmaps_texture);
  599. uniforms.push_back(u);
  600. }
  601. {
  602. RD::Uniform u;
  603. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  604. u.binding = 1;
  605. u.append_id(p_normal_buffer);
  606. uniforms.push_back(u);
  607. }
  608. {
  609. RD::Uniform u;
  610. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  611. u.binding = 2;
  612. u.append_id(ss_effects.gather_constants_buffer);
  613. uniforms.push_back(u);
  614. }
  615. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 0), 0);
  616. }
  617. if (p_invalidate_uniform_sets) {
  618. Vector<RD::Uniform> uniforms;
  619. {
  620. RD::Uniform u;
  621. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  622. u.binding = 0;
  623. u.append_id(p_ao_pong);
  624. uniforms.push_back(u);
  625. }
  626. {
  627. RD::Uniform u;
  628. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  629. u.binding = 1;
  630. u.append_id(default_sampler);
  631. u.append_id(p_importance_map);
  632. uniforms.push_back(u);
  633. }
  634. {
  635. RD::Uniform u;
  636. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  637. u.binding = 2;
  638. u.append_id(ssao.importance_map_load_counter);
  639. uniforms.push_back(u);
  640. }
  641. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 2), 1);
  642. }
  643. if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  644. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  645. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  646. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  647. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  648. ssao.importance_map_push_constant.power = p_settings.power;
  649. //base pass
  650. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  651. gather_ssao(compute_list, p_ao_pong_slices, p_settings, true, r_gather_uniform_set, RID());
  652. //generate importance map
  653. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  654. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 0);
  655. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  656. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  657. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  658. RD::get_singleton()->compute_list_add_barrier(compute_list);
  659. //process importance map A
  660. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  661. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  662. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  663. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  664. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  665. RD::get_singleton()->compute_list_add_barrier(compute_list);
  666. //process Importance Map B
  667. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  668. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  669. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  670. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  671. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  672. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  673. RD::get_singleton()->compute_list_add_barrier(compute_list);
  674. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  675. RD::get_singleton()->draw_command_end_label(); // Importance Map
  676. } else {
  677. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  678. }
  679. gather_ssao(compute_list, p_ao_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set);
  680. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  681. }
  682. // /* THIRD PASS */
  683. // // Blur
  684. //
  685. {
  686. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  687. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  688. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  689. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  690. int blur_passes = p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  691. for (int pass = 0; pass < blur_passes; pass++) {
  692. int blur_pipeline = SSAO_BLUR_PASS;
  693. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  694. blur_pipeline = SSAO_BLUR_PASS_SMART;
  695. if (pass < blur_passes - 2) {
  696. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  697. } else {
  698. blur_pipeline = SSAO_BLUR_PASS_SMART;
  699. }
  700. }
  701. for (int i = 0; i < 4; i++) {
  702. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  703. continue;
  704. }
  705. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  706. if (pass % 2 == 0) {
  707. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  708. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_slices[i]), 0);
  709. } else {
  710. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_slices[i], ss_effects.mirror_sampler), 0);
  711. }
  712. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_pong_slices[i]), 1);
  713. } else {
  714. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  715. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong_slices[i]), 0);
  716. } else {
  717. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_pong_slices[i], ss_effects.mirror_sampler), 0);
  718. }
  719. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 1);
  720. }
  721. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  722. 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));
  723. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  724. }
  725. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  726. RD::get_singleton()->compute_list_add_barrier(compute_list);
  727. }
  728. }
  729. RD::get_singleton()->draw_command_end_label(); // Blur
  730. }
  731. /* FOURTH PASS */
  732. // Interleave buffers
  733. // back to full size
  734. {
  735. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  736. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  737. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  738. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  739. ssao.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  740. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  741. if (p_settings.quality == RS::ENV_SSAO_QUALITY_LOW) {
  742. interleave_pipeline = SSAO_INTERLEAVE;
  743. } else if (p_settings.quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  744. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  745. }
  746. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  747. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 0);
  748. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  749. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao), 1);
  750. } else {
  751. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 1);
  752. }
  753. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  754. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  755. RD::get_singleton()->compute_list_add_barrier(compute_list);
  756. RD::get_singleton()->draw_command_end_label(); // Interleave
  757. }
  758. RD::get_singleton()->draw_command_end_label(); //SSAO
  759. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER); //wait for upcoming transfer
  760. int zero[1] = { 0 };
  761. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  762. }
  763. void EffectsRD::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) {
  764. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  765. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  766. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  767. }
  768. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_projection_uniform_set, 3);
  769. for (int i = 0; i < 4; i++) {
  770. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  771. continue;
  772. }
  773. ssil.gather_push_constant.pass_coord_offset[0] = i % 2;
  774. ssil.gather_push_constant.pass_coord_offset[1] = i / 2;
  775. ssil.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  776. ssil.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  777. ssil.gather_push_constant.pass = i;
  778. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_compute_uniform_set_from_image_pair(p_ssil_slices[i], p_edges_slices[i]), 2);
  779. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssil.gather_push_constant, sizeof(SSILGatherPushConstant));
  780. 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));
  781. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  782. }
  783. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  784. }
  785. void EffectsRD::screen_space_indirect_lighting(RID p_diffuse, RID p_destination, RID p_normal_buffer, RID p_depth_mipmaps_texture, RID p_ssil, const Vector<RID> p_ssil_slices, RID p_ssil_pong, const Vector<RID> p_ssil_pong_slices, RID p_importance_map, RID p_importance_map_pong, RID p_edges, const Vector<RID> p_edges_slices, const CameraMatrix &p_projection, const CameraMatrix &p_last_projection, const SSILSettings &p_settings, bool p_invalidate_uniform_sets, RID &r_gather_uniform_set, RID &r_importance_map_uniform_set, RID &r_projection_uniform_set) {
  786. RD::get_singleton()->draw_command_begin_label("Process Screen Space Indirect Lighting");
  787. //Store projection info before starting the compute list
  788. SSILProjectionUniforms projection_uniforms;
  789. store_camera(p_last_projection, projection_uniforms.inv_last_frame_projection_matrix);
  790. RD::get_singleton()->buffer_update(ssil.projection_uniform_buffer, 0, sizeof(SSILProjectionUniforms), &projection_uniforms);
  791. memset(&ssil.gather_push_constant, 0, sizeof(SSILGatherPushConstant));
  792. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  793. {
  794. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  795. ssil.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  796. ssil.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  797. ssil.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  798. ssil.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  799. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  800. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  801. ssil.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  802. ssil.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  803. ssil.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  804. ssil.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  805. ssil.gather_push_constant.z_near = p_projection.get_z_near();
  806. ssil.gather_push_constant.z_far = p_projection.get_z_far();
  807. ssil.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  808. ssil.gather_push_constant.half_screen_pixel_size_x025[0] = ssil.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  809. ssil.gather_push_constant.half_screen_pixel_size_x025[1] = ssil.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  810. ssil.gather_push_constant.radius = p_settings.radius;
  811. float radius_near_limit = (p_settings.radius * 1.2f);
  812. if (p_settings.quality <= RS::ENV_SSIL_QUALITY_LOW) {
  813. radius_near_limit *= 1.50f;
  814. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  815. ssil.gather_push_constant.radius *= 0.8f;
  816. }
  817. }
  818. radius_near_limit /= tan_half_fov_y;
  819. ssil.gather_push_constant.intensity = p_settings.intensity * Math_PI;
  820. ssil.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  821. ssil.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  822. ssil.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  823. ssil.gather_push_constant.neg_inv_radius = -1.0 / ssil.gather_push_constant.radius;
  824. ssil.gather_push_constant.normal_rejection_amount = p_settings.normal_rejection;
  825. ssil.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  826. ssil.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  827. ssil.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  828. ssil.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  829. if (p_invalidate_uniform_sets) {
  830. Vector<RD::Uniform> uniforms;
  831. {
  832. RD::Uniform u;
  833. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  834. u.binding = 0;
  835. u.append_id(default_mipmap_sampler);
  836. u.append_id(p_diffuse);
  837. uniforms.push_back(u);
  838. }
  839. {
  840. RD::Uniform u;
  841. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  842. u.binding = 1;
  843. u.append_id(ssil.projection_uniform_buffer);
  844. uniforms.push_back(u);
  845. }
  846. r_projection_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 3);
  847. }
  848. if (p_invalidate_uniform_sets) {
  849. Vector<RD::Uniform> uniforms;
  850. {
  851. RD::Uniform u;
  852. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  853. u.binding = 0;
  854. u.append_id(default_sampler);
  855. u.append_id(p_depth_mipmaps_texture);
  856. uniforms.push_back(u);
  857. }
  858. {
  859. RD::Uniform u;
  860. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  861. u.binding = 1;
  862. u.append_id(p_normal_buffer);
  863. uniforms.push_back(u);
  864. }
  865. {
  866. RD::Uniform u;
  867. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  868. u.binding = 2;
  869. u.append_id(ss_effects.gather_constants_buffer);
  870. uniforms.push_back(u);
  871. }
  872. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 0);
  873. }
  874. if (p_invalidate_uniform_sets) {
  875. Vector<RD::Uniform> uniforms;
  876. {
  877. RD::Uniform u;
  878. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  879. u.binding = 0;
  880. u.append_id(p_ssil_pong);
  881. uniforms.push_back(u);
  882. }
  883. {
  884. RD::Uniform u;
  885. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  886. u.binding = 1;
  887. u.append_id(default_sampler);
  888. u.append_id(p_importance_map);
  889. uniforms.push_back(u);
  890. }
  891. {
  892. RD::Uniform u;
  893. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  894. u.binding = 2;
  895. u.append_id(ssil.importance_map_load_counter);
  896. uniforms.push_back(u);
  897. }
  898. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 2), 1);
  899. }
  900. if (p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) {
  901. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  902. ssil.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  903. ssil.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  904. ssil.importance_map_push_constant.intensity = p_settings.intensity * Math_PI;
  905. //base pass
  906. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_BASE]);
  907. gather_ssil(compute_list, p_ssil_pong_slices, p_edges_slices, p_settings, true, r_gather_uniform_set, r_importance_map_uniform_set, r_projection_uniform_set);
  908. //generate importance map
  909. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GENERATE_IMPORTANCE_MAP]);
  910. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong), 0);
  911. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  912. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  913. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  914. RD::get_singleton()->compute_list_add_barrier(compute_list);
  915. // process Importance Map A
  916. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPA]);
  917. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  918. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  919. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  920. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  921. RD::get_singleton()->compute_list_add_barrier(compute_list);
  922. // process Importance Map B
  923. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPB]);
  924. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  925. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  926. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssil.counter_uniform_set, 2);
  927. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  928. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  929. RD::get_singleton()->compute_list_add_barrier(compute_list);
  930. RD::get_singleton()->draw_command_end_label(); // Importance Map
  931. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_ADAPTIVE]);
  932. } else {
  933. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER]);
  934. }
  935. gather_ssil(compute_list, p_ssil_slices, p_edges_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set, r_projection_uniform_set);
  936. RD::get_singleton()->draw_command_end_label(); //Gather
  937. }
  938. {
  939. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  940. ssil.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  941. ssil.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  942. ssil.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  943. int blur_passes = p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  944. for (int pass = 0; pass < blur_passes; pass++) {
  945. int blur_pipeline = SSIL_BLUR_PASS;
  946. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  947. blur_pipeline = SSIL_BLUR_PASS_SMART;
  948. if (pass < blur_passes - 2) {
  949. blur_pipeline = SSIL_BLUR_PASS_WIDE;
  950. }
  951. }
  952. for (int i = 0; i < 4; i++) {
  953. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  954. continue;
  955. }
  956. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[blur_pipeline]);
  957. if (pass % 2 == 0) {
  958. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  959. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_slices[i]), 0);
  960. } else {
  961. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ssil_slices[i], ss_effects.mirror_sampler), 0);
  962. }
  963. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ssil_pong_slices[i]), 1);
  964. } else {
  965. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  966. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong_slices[i]), 0);
  967. } else {
  968. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ssil_pong_slices[i], ss_effects.mirror_sampler), 0);
  969. }
  970. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ssil_slices[i]), 1);
  971. }
  972. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_edges_slices[i]), 2);
  973. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.blur_push_constant, sizeof(SSILBlurPushConstant));
  974. int x_groups = (p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1));
  975. int y_groups = (p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  976. RD::get_singleton()->compute_list_dispatch_threads(compute_list, x_groups, y_groups, 1);
  977. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  978. RD::get_singleton()->compute_list_add_barrier(compute_list);
  979. }
  980. }
  981. }
  982. RD::get_singleton()->draw_command_end_label(); // Blur
  983. }
  984. {
  985. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  986. ssil.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  987. ssil.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  988. ssil.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  989. ssil.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  990. int interleave_pipeline = SSIL_INTERLEAVE_HALF;
  991. if (p_settings.quality == RS::ENV_SSIL_QUALITY_LOW) {
  992. interleave_pipeline = SSIL_INTERLEAVE;
  993. } else if (p_settings.quality >= RS::ENV_SSIL_QUALITY_MEDIUM) {
  994. interleave_pipeline = SSIL_INTERLEAVE_SMART;
  995. }
  996. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[interleave_pipeline]);
  997. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_destination), 0);
  998. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  999. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil), 1);
  1000. } else {
  1001. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong), 1);
  1002. }
  1003. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_edges), 2);
  1004. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.interleave_push_constant, sizeof(SSILInterleavePushConstant));
  1005. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1006. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1007. RD::get_singleton()->draw_command_end_label(); // Interleave
  1008. }
  1009. RD::get_singleton()->draw_command_end_label(); // SSIL
  1010. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER);
  1011. int zero[1] = { 0 };
  1012. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1013. }
  1014. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  1015. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  1016. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  1017. roughness_limiter.push_constant.curve = p_curve;
  1018. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1019. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  1020. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  1021. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  1022. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  1023. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  1024. RD::get_singleton()->compute_list_end();
  1025. }
  1026. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  1027. Sort::PushConstant push_constant;
  1028. push_constant.total_elements = p_size;
  1029. bool done = true;
  1030. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1031. if (numThreadGroups > 1) {
  1032. done = false;
  1033. }
  1034. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1035. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1036. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1037. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1038. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1039. int presorted = 512;
  1040. while (!done) {
  1041. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1042. done = true;
  1043. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1044. numThreadGroups = 0;
  1045. if (p_size > presorted) {
  1046. if (p_size > presorted * 2) {
  1047. done = false;
  1048. }
  1049. int pow2 = presorted;
  1050. while (pow2 < p_size) {
  1051. pow2 *= 2;
  1052. }
  1053. numThreadGroups = pow2 >> 9;
  1054. }
  1055. unsigned int nMergeSize = presorted * 2;
  1056. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1057. push_constant.job_params[0] = nMergeSubSize;
  1058. if (nMergeSubSize == nMergeSize >> 1) {
  1059. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1060. push_constant.job_params[2] = -1;
  1061. } else {
  1062. push_constant.job_params[1] = nMergeSubSize;
  1063. push_constant.job_params[2] = 1;
  1064. }
  1065. push_constant.job_params[3] = 0;
  1066. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1067. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1068. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1069. }
  1070. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1071. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1072. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1073. presorted *= 2;
  1074. }
  1075. RD::get_singleton()->compute_list_end();
  1076. }
  1077. EffectsRD::EffectsRD(bool p_prefer_raster_effects) {
  1078. {
  1079. Vector<String> FSR_upscale_modes;
  1080. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  1081. // MoltenVK does not support some of the operations used by the normal mode of FSR. Fallback works just fine though.
  1082. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n");
  1083. #else
  1084. // Everyone else can use normal mode when available.
  1085. if (RD::get_singleton()->get_device_capabilities()->supports_fsr_half_float) {
  1086. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_NORMAL\n");
  1087. } else {
  1088. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n");
  1089. }
  1090. #endif
  1091. FSR_upscale.shader.initialize(FSR_upscale_modes);
  1092. FSR_upscale.shader_version = FSR_upscale.shader.version_create();
  1093. FSR_upscale.pipeline = RD::get_singleton()->compute_pipeline_create(FSR_upscale.shader.version_get_shader(FSR_upscale.shader_version, 0));
  1094. }
  1095. prefer_raster_effects = p_prefer_raster_effects;
  1096. if (prefer_raster_effects) {
  1097. Vector<String> luminance_reduce_modes;
  1098. luminance_reduce_modes.push_back("\n#define FIRST_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FIRST
  1099. luminance_reduce_modes.push_back("\n"); // LUMINANCE_REDUCE_FRAGMENT
  1100. luminance_reduce_modes.push_back("\n#define FINAL_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FINAL
  1101. luminance_reduce_raster.shader.initialize(luminance_reduce_modes);
  1102. memset(&luminance_reduce_raster.push_constant, 0, sizeof(LuminanceReduceRasterPushConstant));
  1103. luminance_reduce_raster.shader_version = luminance_reduce_raster.shader.version_create();
  1104. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1105. luminance_reduce_raster.pipelines[i].setup(luminance_reduce_raster.shader.version_get_shader(luminance_reduce_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1106. }
  1107. } else {
  1108. // Initialize luminance_reduce
  1109. Vector<String> luminance_reduce_modes;
  1110. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1111. luminance_reduce_modes.push_back("\n");
  1112. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1113. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1114. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1115. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1116. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1117. }
  1118. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1119. luminance_reduce_raster.pipelines[i].clear();
  1120. }
  1121. }
  1122. if (!prefer_raster_effects) {
  1123. {
  1124. // Initialize depth buffer for screen space effects
  1125. Vector<String> downsampler_modes;
  1126. downsampler_modes.push_back("\n");
  1127. downsampler_modes.push_back("\n#define USE_HALF_SIZE\n");
  1128. downsampler_modes.push_back("\n#define GENERATE_MIPS\n");
  1129. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE\n");
  1130. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  1131. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE\n");
  1132. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define GENERATE_FULL_MIPS");
  1133. ss_effects.downsample_shader.initialize(downsampler_modes);
  1134. ss_effects.downsample_shader_version = ss_effects.downsample_shader.version_create();
  1135. for (int i = 0; i < SS_EFFECTS_MAX; i++) {
  1136. ss_effects.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, i));
  1137. }
  1138. ss_effects.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSEffectsGatherConstants));
  1139. SSEffectsGatherConstants gather_constants;
  1140. const int sub_pass_count = 5;
  1141. for (int pass = 0; pass < 4; pass++) {
  1142. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  1143. int a = pass;
  1144. int b = subPass;
  1145. int spmap[5]{ 0, 1, 4, 3, 2 };
  1146. b = spmap[subPass];
  1147. float ca, sa;
  1148. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  1149. ca = Math::cos(angle0);
  1150. sa = Math::sin(angle0);
  1151. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  1152. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  1153. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  1154. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  1155. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  1156. }
  1157. }
  1158. RD::get_singleton()->buffer_update(ss_effects.gather_constants_buffer, 0, sizeof(SSEffectsGatherConstants), &gather_constants);
  1159. }
  1160. {
  1161. // Initialize ssao
  1162. RD::SamplerState sampler;
  1163. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1164. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1165. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1166. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1167. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1168. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1169. sampler.max_lod = 4;
  1170. ss_effects.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  1171. uint32_t pipeline = 0;
  1172. {
  1173. Vector<String> ssao_modes;
  1174. ssao_modes.push_back("\n");
  1175. ssao_modes.push_back("\n#define SSAO_BASE\n");
  1176. ssao_modes.push_back("\n#define ADAPTIVE\n");
  1177. ssao.gather_shader.initialize(ssao_modes);
  1178. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1179. for (int i = 0; i <= SSAO_GATHER_ADAPTIVE; i++) {
  1180. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i));
  1181. pipeline++;
  1182. }
  1183. }
  1184. {
  1185. Vector<String> ssao_modes;
  1186. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  1187. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  1188. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  1189. ssao.importance_map_shader.initialize(ssao_modes);
  1190. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  1191. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  1192. 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));
  1193. pipeline++;
  1194. }
  1195. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  1196. int zero[1] = { 0 };
  1197. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1198. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  1199. Vector<RD::Uniform> uniforms;
  1200. {
  1201. RD::Uniform u;
  1202. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1203. u.binding = 0;
  1204. u.append_id(ssao.importance_map_load_counter);
  1205. uniforms.push_back(u);
  1206. }
  1207. 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);
  1208. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  1209. }
  1210. {
  1211. Vector<String> ssao_modes;
  1212. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1213. ssao_modes.push_back("\n#define MODE_SMART\n");
  1214. ssao_modes.push_back("\n#define MODE_WIDE\n");
  1215. ssao.blur_shader.initialize(ssao_modes);
  1216. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1217. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  1218. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1219. pipeline++;
  1220. }
  1221. }
  1222. {
  1223. Vector<String> ssao_modes;
  1224. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1225. ssao_modes.push_back("\n#define MODE_SMART\n");
  1226. ssao_modes.push_back("\n#define MODE_HALF\n");
  1227. ssao.interleave_shader.initialize(ssao_modes);
  1228. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  1229. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  1230. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  1231. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  1232. pipeline++;
  1233. }
  1234. }
  1235. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1236. }
  1237. }
  1238. if (!prefer_raster_effects) {
  1239. // Initialize roughness limiter
  1240. Vector<String> shader_modes;
  1241. shader_modes.push_back("");
  1242. roughness_limiter.shader.initialize(shader_modes);
  1243. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1244. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1245. }
  1246. if (!prefer_raster_effects) {
  1247. Vector<String> specular_modes;
  1248. specular_modes.push_back("\n#define MODE_MERGE\n");
  1249. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1250. specular_modes.push_back("\n");
  1251. specular_modes.push_back("\n#define MODE_SSR\n");
  1252. specular_merge.shader.initialize(specular_modes);
  1253. specular_merge.shader_version = specular_merge.shader.version_create();
  1254. //use additive
  1255. RD::PipelineColorBlendState::Attachment ba;
  1256. ba.enable_blend = true;
  1257. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1258. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1259. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1260. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1261. ba.color_blend_op = RD::BLEND_OP_ADD;
  1262. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1263. RD::PipelineColorBlendState blend_additive;
  1264. blend_additive.attachments.push_back(ba);
  1265. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1266. RD::PipelineColorBlendState blend_state;
  1267. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1268. blend_state = blend_additive;
  1269. } else {
  1270. blend_state = RD::PipelineColorBlendState::create_disabled();
  1271. }
  1272. specular_merge.pipelines[i].setup(specular_merge.shader.version_get_shader(specular_merge.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  1273. }
  1274. }
  1275. if (!prefer_raster_effects) {
  1276. {
  1277. Vector<String> ssr_modes;
  1278. ssr_modes.push_back("\n");
  1279. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1280. ssr.shader.initialize(ssr_modes);
  1281. ssr.shader_version = ssr.shader.version_create();
  1282. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1283. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1284. }
  1285. }
  1286. {
  1287. Vector<String> ssr_filter_modes;
  1288. ssr_filter_modes.push_back("\n");
  1289. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1290. ssr_filter.shader.initialize(ssr_filter_modes);
  1291. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1292. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1293. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1294. }
  1295. }
  1296. {
  1297. Vector<String> ssr_scale_modes;
  1298. ssr_scale_modes.push_back("\n");
  1299. ssr_scale.shader.initialize(ssr_scale_modes);
  1300. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1301. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1302. }
  1303. {
  1304. Vector<String> sss_modes;
  1305. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1306. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1307. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1308. sss.shader.initialize(sss_modes);
  1309. sss.shader_version = sss.shader.version_create();
  1310. for (int i = 0; i < sss_modes.size(); i++) {
  1311. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1312. }
  1313. }
  1314. {
  1315. Vector<String> ssil_modes;
  1316. ssil_modes.push_back("\n");
  1317. ssil_modes.push_back("\n#define SSIL_BASE\n");
  1318. ssil_modes.push_back("\n#define ADAPTIVE\n");
  1319. ssil.gather_shader.initialize(ssil_modes);
  1320. ssil.gather_shader_version = ssil.gather_shader.version_create();
  1321. for (int i = SSIL_GATHER; i <= SSIL_GATHER_ADAPTIVE; i++) {
  1322. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.gather_shader.version_get_shader(ssil.gather_shader_version, i));
  1323. }
  1324. ssil.projection_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSILProjectionUniforms));
  1325. }
  1326. {
  1327. Vector<String> ssil_modes;
  1328. ssil_modes.push_back("\n#define GENERATE_MAP\n");
  1329. ssil_modes.push_back("\n#define PROCESS_MAPA\n");
  1330. ssil_modes.push_back("\n#define PROCESS_MAPB\n");
  1331. ssil.importance_map_shader.initialize(ssil_modes);
  1332. ssil.importance_map_shader_version = ssil.importance_map_shader.version_create();
  1333. for (int i = SSIL_GENERATE_IMPORTANCE_MAP; i <= SSIL_PROCESS_IMPORTANCE_MAPB; i++) {
  1334. 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));
  1335. }
  1336. ssil.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  1337. int zero[1] = { 0 };
  1338. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1339. RD::get_singleton()->set_resource_name(ssil.importance_map_load_counter, "Importance Map Load Counter");
  1340. Vector<RD::Uniform> uniforms;
  1341. {
  1342. RD::Uniform u;
  1343. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1344. u.binding = 0;
  1345. u.append_id(ssil.importance_map_load_counter);
  1346. uniforms.push_back(u);
  1347. }
  1348. 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);
  1349. RD::get_singleton()->set_resource_name(ssil.counter_uniform_set, "Load Counter Uniform Set");
  1350. }
  1351. {
  1352. Vector<String> ssil_modes;
  1353. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  1354. ssil_modes.push_back("\n#define MODE_SMART\n");
  1355. ssil_modes.push_back("\n#define MODE_WIDE\n");
  1356. ssil.blur_shader.initialize(ssil_modes);
  1357. ssil.blur_shader_version = ssil.blur_shader.version_create();
  1358. for (int i = SSIL_BLUR_PASS; i <= SSIL_BLUR_PASS_WIDE; i++) {
  1359. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.blur_shader.version_get_shader(ssil.blur_shader_version, i - SSIL_BLUR_PASS));
  1360. }
  1361. }
  1362. {
  1363. Vector<String> ssil_modes;
  1364. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  1365. ssil_modes.push_back("\n#define MODE_SMART\n");
  1366. ssil_modes.push_back("\n#define MODE_HALF\n");
  1367. ssil.interleave_shader.initialize(ssil_modes);
  1368. ssil.interleave_shader_version = ssil.interleave_shader.version_create();
  1369. for (int i = SSIL_INTERLEAVE; i <= SSIL_INTERLEAVE_HALF; i++) {
  1370. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, i - SSIL_INTERLEAVE));
  1371. }
  1372. }
  1373. }
  1374. {
  1375. Vector<String> sort_modes;
  1376. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  1377. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  1378. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  1379. sort.shader.initialize(sort_modes);
  1380. sort.shader_version = sort.shader.version_create();
  1381. for (int i = 0; i < SORT_MODE_MAX; i++) {
  1382. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  1383. }
  1384. }
  1385. {
  1386. Vector<String> taa_modes;
  1387. taa_modes.push_back("\n#define MODE_TAA_RESOLVE");
  1388. TAA_resolve.shader.initialize(taa_modes);
  1389. TAA_resolve.shader_version = TAA_resolve.shader.version_create();
  1390. TAA_resolve.pipeline = RD::get_singleton()->compute_pipeline_create(TAA_resolve.shader.version_get_shader(TAA_resolve.shader_version, 0));
  1391. }
  1392. RD::SamplerState sampler;
  1393. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1394. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1395. sampler.max_lod = 0;
  1396. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1397. RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");
  1398. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1399. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1400. sampler.max_lod = 1e20;
  1401. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1402. RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");
  1403. { //create index array for copy shaders
  1404. Vector<uint8_t> pv;
  1405. pv.resize(6 * 4);
  1406. {
  1407. uint8_t *w = pv.ptrw();
  1408. int *p32 = (int *)w;
  1409. p32[0] = 0;
  1410. p32[1] = 1;
  1411. p32[2] = 2;
  1412. p32[3] = 0;
  1413. p32[4] = 2;
  1414. p32[5] = 3;
  1415. }
  1416. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1417. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1418. }
  1419. }
  1420. EffectsRD::~EffectsRD() {
  1421. RD::get_singleton()->free(default_sampler);
  1422. RD::get_singleton()->free(default_mipmap_sampler);
  1423. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  1424. FSR_upscale.shader.version_free(FSR_upscale.shader_version);
  1425. TAA_resolve.shader.version_free(TAA_resolve.shader_version);
  1426. if (prefer_raster_effects) {
  1427. luminance_reduce_raster.shader.version_free(luminance_reduce_raster.shader_version);
  1428. } else {
  1429. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  1430. }
  1431. if (!prefer_raster_effects) {
  1432. specular_merge.shader.version_free(specular_merge.shader_version);
  1433. ss_effects.downsample_shader.version_free(ss_effects.downsample_shader_version);
  1434. ssao.blur_shader.version_free(ssao.blur_shader_version);
  1435. ssao.gather_shader.version_free(ssao.gather_shader_version);
  1436. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  1437. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  1438. ssil.blur_shader.version_free(ssil.blur_shader_version);
  1439. ssil.gather_shader.version_free(ssil.gather_shader_version);
  1440. ssil.interleave_shader.version_free(ssil.interleave_shader_version);
  1441. ssil.importance_map_shader.version_free(ssil.importance_map_shader_version);
  1442. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  1443. ssr.shader.version_free(ssr.shader_version);
  1444. ssr_filter.shader.version_free(ssr_filter.shader_version);
  1445. ssr_scale.shader.version_free(ssr_scale.shader_version);
  1446. sss.shader.version_free(sss.shader_version);
  1447. RD::get_singleton()->free(ss_effects.mirror_sampler);
  1448. RD::get_singleton()->free(ss_effects.gather_constants_buffer);
  1449. RD::get_singleton()->free(ssao.importance_map_load_counter);
  1450. RD::get_singleton()->free(ssil.importance_map_load_counter);
  1451. RD::get_singleton()->free(ssil.projection_uniform_buffer);
  1452. }
  1453. sort.shader.version_free(sort.shader_version);
  1454. }