effects_rd.cpp 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  1. /*************************************************************************/
  2. /* effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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.ids.push_back(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_for_input(RID p_texture) {
  65. if (input_to_uniform_set_cache.has(p_texture)) {
  66. RID uniform_set = input_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_INPUT_ATTACHMENT;
  74. u.binding = 0;
  75. u.ids.push_back(p_texture);
  76. uniforms.push_back(u);
  77. // This is specific to our subpass shader
  78. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, TONEMAP_MODE_SUBPASS), 0);
  79. input_to_uniform_set_cache[p_texture] = uniform_set;
  80. return uniform_set;
  81. }
  82. RID EffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  83. if (texture_to_uniform_set_cache.has(p_texture)) {
  84. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  85. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  86. return uniform_set;
  87. }
  88. }
  89. Vector<RD::Uniform> uniforms;
  90. RD::Uniform u;
  91. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  92. u.binding = 0;
  93. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  94. u.ids.push_back(p_texture);
  95. uniforms.push_back(u);
  96. // anything with the same configuration (one texture in binding 0 for set 0), is good
  97. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 0);
  98. texture_to_uniform_set_cache[p_texture] = uniform_set;
  99. return uniform_set;
  100. }
  101. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  102. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  103. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  104. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  105. return uniform_set;
  106. }
  107. }
  108. Vector<RD::Uniform> uniforms;
  109. RD::Uniform u;
  110. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  111. u.binding = 0;
  112. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  113. u.ids.push_back(p_texture);
  114. uniforms.push_back(u);
  115. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  116. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  117. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  118. return uniform_set;
  119. }
  120. RID EffectsRD::_get_compute_uniform_set_from_texture_and_sampler(RID p_texture, RID p_sampler) {
  121. TextureSamplerPair tsp;
  122. tsp.texture = p_texture;
  123. tsp.sampler = p_sampler;
  124. if (texture_sampler_to_compute_uniform_set_cache.has(tsp)) {
  125. RID uniform_set = texture_sampler_to_compute_uniform_set_cache[tsp];
  126. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  127. return uniform_set;
  128. }
  129. }
  130. Vector<RD::Uniform> uniforms;
  131. RD::Uniform u;
  132. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  133. u.binding = 0;
  134. u.ids.push_back(p_sampler);
  135. u.ids.push_back(p_texture);
  136. uniforms.push_back(u);
  137. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  138. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0), 0);
  139. texture_sampler_to_compute_uniform_set_cache[tsp] = uniform_set;
  140. return uniform_set;
  141. }
  142. RID EffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  143. TexturePair tp;
  144. tp.texture1 = p_texture1;
  145. tp.texture2 = p_texture2;
  146. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  147. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  148. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  149. return uniform_set;
  150. }
  151. }
  152. Vector<RD::Uniform> uniforms;
  153. {
  154. RD::Uniform u;
  155. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  156. u.binding = 0;
  157. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  158. u.ids.push_back(p_texture1);
  159. uniforms.push_back(u);
  160. }
  161. {
  162. RD::Uniform u;
  163. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  164. u.binding = 1;
  165. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  166. u.ids.push_back(p_texture2);
  167. uniforms.push_back(u);
  168. }
  169. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  170. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  171. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  172. return uniform_set;
  173. }
  174. RID EffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  175. TexturePair tp;
  176. tp.texture1 = p_texture1;
  177. tp.texture2 = p_texture2;
  178. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  179. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  180. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  181. return uniform_set;
  182. }
  183. }
  184. Vector<RD::Uniform> uniforms;
  185. {
  186. RD::Uniform u;
  187. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  188. u.binding = 0;
  189. u.ids.push_back(p_texture1);
  190. uniforms.push_back(u);
  191. }
  192. {
  193. RD::Uniform u;
  194. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  195. u.binding = 1;
  196. u.ids.push_back(p_texture2);
  197. uniforms.push_back(u);
  198. }
  199. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  200. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  201. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  202. return uniform_set;
  203. }
  204. void EffectsRD::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) {
  205. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  206. copy_to_fb.push_constant.use_section = true;
  207. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  208. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  209. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  210. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  211. if (p_flip_y) {
  212. copy_to_fb.push_constant.flip_y = true;
  213. }
  214. RD::DrawListID draw_list = p_draw_list;
  215. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  216. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  217. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  218. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  219. RD::get_singleton()->draw_list_draw(draw_list, true);
  220. }
  221. void EffectsRD::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_alpha_to_zero, bool p_srgb, RID p_secondary) {
  222. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  223. if (p_flip_y) {
  224. copy_to_fb.push_constant.flip_y = true;
  225. }
  226. if (p_force_luminance) {
  227. copy_to_fb.push_constant.force_luminance = true;
  228. }
  229. if (p_alpha_to_zero) {
  230. copy_to_fb.push_constant.alpha_to_zero = true;
  231. }
  232. if (p_srgb) {
  233. copy_to_fb.push_constant.srgb = true;
  234. }
  235. 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_DISCARD, Vector<Color>(), 1.0, 0, p_rect);
  236. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  237. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  238. if (p_secondary.is_valid()) {
  239. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_secondary), 1);
  240. }
  241. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  242. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  243. RD::get_singleton()->draw_list_draw(draw_list, true);
  244. RD::get_singleton()->draw_list_end();
  245. }
  246. void EffectsRD::copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_all_source, bool p_8_bit_dst, bool p_alpha_to_one) {
  247. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  248. if (p_flip_y) {
  249. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  250. }
  251. if (p_force_luminance) {
  252. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  253. }
  254. if (p_all_source) {
  255. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  256. }
  257. if (p_alpha_to_one) {
  258. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  259. }
  260. copy.push_constant.section[0] = 0;
  261. copy.push_constant.section[1] = 0;
  262. copy.push_constant.section[2] = p_rect.size.width;
  263. copy.push_constant.section[3] = p_rect.size.height;
  264. copy.push_constant.target[0] = p_rect.position.x;
  265. copy.push_constant.target[1] = p_rect.position.y;
  266. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  267. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY]);
  268. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  269. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  270. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  271. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  272. RD::get_singleton()->compute_list_end();
  273. }
  274. void EffectsRD::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
  275. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  276. copy.push_constant.section[0] = 0;
  277. copy.push_constant.section[1] = 0;
  278. copy.push_constant.section[2] = p_panorama_size.width;
  279. copy.push_constant.section[3] = p_panorama_size.height;
  280. copy.push_constant.target[0] = 0;
  281. copy.push_constant.target[1] = 0;
  282. copy.push_constant.camera_z_far = p_lod;
  283. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  284. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA]);
  285. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cube), 0);
  286. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_panorama), 3);
  287. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  288. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  289. RD::get_singleton()->compute_list_end();
  290. }
  291. void EffectsRD::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) {
  292. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  293. if (p_flip_y) {
  294. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  295. }
  296. copy.push_constant.section[0] = 0;
  297. copy.push_constant.section[1] = 0;
  298. copy.push_constant.section[2] = p_rect.size.width;
  299. copy.push_constant.section[3] = p_rect.size.height;
  300. copy.push_constant.target[0] = p_rect.position.x;
  301. copy.push_constant.target[1] = p_rect.position.y;
  302. copy.push_constant.camera_z_far = p_z_far;
  303. copy.push_constant.camera_z_near = p_z_near;
  304. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  305. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  306. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  307. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  308. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  309. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  310. RD::get_singleton()->compute_list_end();
  311. }
  312. void EffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  313. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  314. if (p_flip_y) {
  315. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  316. }
  317. copy.push_constant.section[0] = 0;
  318. copy.push_constant.section[1] = 0;
  319. copy.push_constant.section[2] = p_rect.size.width;
  320. copy.push_constant.section[3] = p_rect.size.height;
  321. copy.push_constant.target[0] = p_rect.position.x;
  322. copy.push_constant.target[1] = p_rect.position.y;
  323. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  324. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_SIMPLY_COPY_DEPTH]);
  325. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  326. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  327. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  328. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  329. RD::get_singleton()->compute_list_end();
  330. }
  331. void EffectsRD::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  332. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  333. copy.push_constant.section[0] = 0;
  334. copy.push_constant.section[1] = 0;
  335. copy.push_constant.section[2] = p_region.size.width;
  336. copy.push_constant.section[3] = p_region.size.height;
  337. copy.push_constant.target[0] = p_region.position.x;
  338. copy.push_constant.target[1] = p_region.position.y;
  339. copy.push_constant.set_color[0] = p_color.r;
  340. copy.push_constant.set_color[1] = p_color.g;
  341. copy.push_constant.set_color[2] = p_color.b;
  342. copy.push_constant.set_color[3] = p_color.a;
  343. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  344. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR]);
  345. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  346. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  347. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  348. RD::get_singleton()->compute_list_end();
  349. }
  350. void EffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_texture, RID p_back_texture, const Rect2i &p_region, bool p_8bit_dst) {
  351. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
  352. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  353. uint32_t base_flags = 0;
  354. copy.push_constant.section[0] = p_region.position.x;
  355. copy.push_constant.section[1] = p_region.position.y;
  356. copy.push_constant.section[2] = p_region.size.width;
  357. copy.push_constant.section[3] = p_region.size.height;
  358. //HORIZONTAL
  359. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  360. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  361. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  362. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  363. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL;
  364. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  365. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  366. RD::get_singleton()->compute_list_add_barrier(compute_list);
  367. //VERTICAL
  368. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  369. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 3);
  370. copy.push_constant.flags = base_flags;
  371. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  372. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  373. RD::get_singleton()->compute_list_end();
  374. }
  375. void EffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_high_quality, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  376. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
  377. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  378. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  379. uint32_t base_flags = 0;
  380. copy.push_constant.section[2] = p_size.x;
  381. copy.push_constant.section[3] = p_size.y;
  382. copy.push_constant.glow_strength = p_strength;
  383. copy.push_constant.glow_bloom = p_bloom;
  384. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  385. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  386. copy.push_constant.glow_exposure = p_exposure;
  387. copy.push_constant.glow_white = 0; //actually unused
  388. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  389. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  390. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  391. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  392. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  393. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  394. if (p_auto_exposure.is_valid() && p_first_pass) {
  395. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  396. }
  397. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  398. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  399. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  400. RD::get_singleton()->compute_list_end();
  401. }
  402. void EffectsRD::gaussian_glow_raster(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength, bool p_high_quality, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  403. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
  404. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  405. BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  406. uint32_t base_flags = 0;
  407. blur_raster.push_constant.pixel_size[0] = p_pixel_size.x;
  408. blur_raster.push_constant.pixel_size[1] = p_pixel_size.y;
  409. blur_raster.push_constant.glow_strength = p_strength;
  410. blur_raster.push_constant.glow_bloom = p_bloom;
  411. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  412. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  413. blur_raster.push_constant.glow_exposure = p_exposure;
  414. blur_raster.push_constant.glow_white = 0; //actually unused
  415. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  416. blur_raster.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  417. //HORIZONTAL
  418. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer_half, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  419. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer_half)));
  420. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  421. if (p_auto_exposure.is_valid() && p_first_pass) {
  422. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_auto_exposure), 1);
  423. }
  424. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  425. blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  426. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  427. RD::get_singleton()->draw_list_draw(draw_list, true);
  428. RD::get_singleton()->draw_list_end();
  429. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  430. //VERTICAL
  431. 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_DISCARD);
  432. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  433. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  434. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  435. blur_raster.push_constant.flags = base_flags;
  436. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  437. RD::get_singleton()->draw_list_draw(draw_list, true);
  438. RD::get_singleton()->draw_list_end();
  439. }
  440. 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) {
  441. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  442. { //scale color and depth to half
  443. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  444. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  445. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  446. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  447. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  448. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  449. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  450. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  451. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  452. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  453. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  454. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  455. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  456. RD::get_singleton()->compute_list_add_barrier(compute_list);
  457. }
  458. {
  459. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  460. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  461. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  462. ssr.push_constant.screen_size[0] = p_screen_size.x;
  463. ssr.push_constant.screen_size[1] = p_screen_size.y;
  464. ssr.push_constant.curve_fade_in = p_fade_in;
  465. ssr.push_constant.distance_fade = p_fade_out;
  466. ssr.push_constant.num_steps = p_max_steps;
  467. ssr.push_constant.depth_tolerance = p_tolerance;
  468. ssr.push_constant.use_half_res = true;
  469. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  470. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  471. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  472. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  473. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  474. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  475. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  476. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  477. store_camera(p_camera, ssr.push_constant.projection);
  478. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.pipelines[(p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL]);
  479. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  480. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  481. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  482. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  483. } else {
  484. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  485. }
  486. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  487. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  488. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  489. }
  490. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  491. //blur
  492. RD::get_singleton()->compute_list_add_barrier(compute_list);
  493. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  494. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  495. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  496. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  497. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  498. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  499. ssr_filter.push_constant.vertical = 0;
  500. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  501. ssr_filter.push_constant.steps = p_max_steps / 3;
  502. ssr_filter.push_constant.increment = 3;
  503. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  504. ssr_filter.push_constant.steps = p_max_steps / 2;
  505. ssr_filter.push_constant.increment = 2;
  506. } else {
  507. ssr_filter.push_constant.steps = p_max_steps;
  508. ssr_filter.push_constant.increment = 1;
  509. }
  510. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  511. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  512. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  513. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  514. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  515. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  516. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  517. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  518. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  519. RD::get_singleton()->compute_list_add_barrier(compute_list);
  520. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  521. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  522. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  523. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  524. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  525. ssr_filter.push_constant.vertical = 1;
  526. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  527. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  528. }
  529. RD::get_singleton()->compute_list_end();
  530. }
  531. 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) {
  532. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  533. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  534. p.normal /= p.d;
  535. float unit_size = p.normal.x;
  536. { //scale color and depth to half
  537. sss.push_constant.camera_z_far = p_camera.get_z_far();
  538. sss.push_constant.camera_z_near = p_camera.get_z_near();
  539. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  540. sss.push_constant.unit_size = unit_size;
  541. sss.push_constant.screen_size[0] = p_screen_size.x;
  542. sss.push_constant.screen_size[1] = p_screen_size.y;
  543. sss.push_constant.vertical = false;
  544. sss.push_constant.scale = p_scale;
  545. sss.push_constant.depth_scale = p_depth_scale;
  546. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  547. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  548. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  549. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  550. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  551. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  552. RD::get_singleton()->compute_list_add_barrier(compute_list);
  553. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  554. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  555. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  556. sss.push_constant.vertical = true;
  557. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  558. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  559. RD::get_singleton()->compute_list_end();
  560. }
  561. }
  562. void EffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  563. 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_DISCARD, Vector<Color>());
  564. if (p_reflection.is_valid()) {
  565. if (p_base.is_valid()) {
  566. 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)));
  567. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  568. } else {
  569. 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)));
  570. }
  571. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  572. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  573. } else {
  574. if (p_base.is_valid()) {
  575. 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)));
  576. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  577. } else {
  578. 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)));
  579. }
  580. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  581. }
  582. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  583. RD::get_singleton()->draw_list_draw(draw_list, true);
  584. RD::get_singleton()->draw_list_end();
  585. }
  586. void EffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  587. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  588. copy.push_constant.section[0] = 0;
  589. copy.push_constant.section[1] = 0;
  590. copy.push_constant.section[2] = p_size.width;
  591. copy.push_constant.section[3] = p_size.height;
  592. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  593. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  594. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  595. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  596. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  597. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  598. RD::get_singleton()->compute_list_end();
  599. }
  600. void EffectsRD::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_framebuffer, const Size2i &p_size) {
  601. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
  602. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  603. BlurRasterMode mode = BLUR_MIPMAP;
  604. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  605. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  606. 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_DISCARD);
  607. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  608. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  609. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  610. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  611. RD::get_singleton()->draw_list_draw(draw_list, true);
  612. RD::get_singleton()->draw_list_end();
  613. }
  614. void EffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {
  615. CopyToDPPushConstant push_constant;
  616. push_constant.screen_rect[0] = p_rect.position.x;
  617. push_constant.screen_rect[1] = p_rect.position.y;
  618. push_constant.screen_rect[2] = p_rect.size.width;
  619. push_constant.screen_rect[3] = p_rect.size.height;
  620. push_constant.z_far = p_z_far;
  621. push_constant.z_near = p_z_near;
  622. push_constant.texel_size[0] = 1.0f / p_dst_size.x;
  623. push_constant.texel_size[1] = 1.0f / p_dst_size.y;
  624. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  625. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ);
  626. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_dp.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  627. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  628. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  629. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  630. RD::get_singleton()->draw_list_draw(draw_list, true);
  631. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_TRANSFER);
  632. }
  633. void EffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  634. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  635. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  636. tonemap.push_constant.bcs[0] = p_settings.brightness;
  637. tonemap.push_constant.bcs[1] = p_settings.contrast;
  638. tonemap.push_constant.bcs[2] = p_settings.saturation;
  639. tonemap.push_constant.use_glow = p_settings.use_glow;
  640. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  641. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  642. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  643. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  644. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  645. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  646. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  647. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  648. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  649. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  650. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  651. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  652. if (p_settings.use_1d_color_correction) {
  653. mode += 2;
  654. }
  655. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  656. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  657. tonemap.push_constant.exposure = p_settings.exposure;
  658. tonemap.push_constant.white = p_settings.white;
  659. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  660. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  661. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  662. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  663. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  664. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  665. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  666. if (p_settings.view_count > 1) {
  667. // Use MULTIVIEW versions
  668. mode += 6;
  669. }
  670. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD);
  671. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass()));
  672. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  673. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  674. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  675. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  676. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  677. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  678. RD::get_singleton()->draw_list_draw(draw_list, true);
  679. RD::get_singleton()->draw_list_end();
  680. }
  681. void EffectsRD::tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) {
  682. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  683. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  684. tonemap.push_constant.bcs[0] = p_settings.brightness;
  685. tonemap.push_constant.bcs[1] = p_settings.contrast;
  686. tonemap.push_constant.bcs[2] = p_settings.saturation;
  687. ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses.");
  688. tonemap.push_constant.use_glow = p_settings.use_glow;
  689. int mode = p_settings.use_1d_color_correction ? TONEMAP_MODE_SUBPASS_1D_LUT : TONEMAP_MODE_SUBPASS;
  690. if (p_settings.view_count > 1) {
  691. // Use MULTIVIEW versions
  692. mode += 6;
  693. }
  694. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  695. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  696. tonemap.push_constant.exposure = p_settings.exposure;
  697. tonemap.push_constant.white = p_settings.white;
  698. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  699. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  700. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  701. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  702. RD::get_singleton()->draw_list_bind_render_pipeline(p_subpass_draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_dst_format_id, false, RD::get_singleton()->draw_list_get_current_pass()));
  703. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_for_input(p_source_color), 0);
  704. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1); // should be set to a default texture, it's ignored
  705. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2); // should be set to a default texture, it's ignored
  706. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  707. RD::get_singleton()->draw_list_bind_index_array(p_subpass_draw_list, index_array);
  708. RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  709. RD::get_singleton()->draw_list_draw(p_subpass_draw_list, true);
  710. }
  711. 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) {
  712. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of luminance reduction with the mobile renderer.");
  713. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  714. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  715. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  716. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  717. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  718. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  719. for (int i = 0; i < p_reduce.size(); i++) {
  720. if (i == 0) {
  721. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  722. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  723. } else {
  724. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  725. if (i == p_reduce.size() - 1 && !p_set) {
  726. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  727. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  728. } else {
  729. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  730. }
  731. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  732. }
  733. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  734. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  735. RD::get_singleton()->compute_list_dispatch_threads(compute_list, luminance_reduce.push_constant.source_size[0], luminance_reduce.push_constant.source_size[1], 1);
  736. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  737. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  738. }
  739. RD::get_singleton()->compute_list_end();
  740. }
  741. 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) {
  742. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster version of luminance reduction with the clustered renderer.");
  743. ERR_FAIL_COND_MSG(p_reduce.size() != p_fb.size(), "Incorrect frame buffer account for luminance reduction.");
  744. luminance_reduce_raster.push_constant.max_luminance = p_max_luminance;
  745. luminance_reduce_raster.push_constant.min_luminance = p_min_luminance;
  746. luminance_reduce_raster.push_constant.exposure_adjust = p_adjust;
  747. for (int i = 0; i < p_reduce.size(); i++) {
  748. luminance_reduce_raster.push_constant.source_size[0] = i == 0 ? p_source_size.x : luminance_reduce_raster.push_constant.dest_size[0];
  749. luminance_reduce_raster.push_constant.source_size[1] = i == 0 ? p_source_size.y : luminance_reduce_raster.push_constant.dest_size[1];
  750. luminance_reduce_raster.push_constant.dest_size[0] = MAX(luminance_reduce_raster.push_constant.source_size[0] / 8, 1);
  751. luminance_reduce_raster.push_constant.dest_size[1] = MAX(luminance_reduce_raster.push_constant.source_size[1] / 8, 1);
  752. bool final = !p_set && (luminance_reduce_raster.push_constant.dest_size[0] == 1) && (luminance_reduce_raster.push_constant.dest_size[1] == 1);
  753. LuminanceReduceRasterMode mode = final ? LUMINANCE_REDUCE_FRAGMENT_FINAL : (i == 0 ? LUMINANCE_REDUCE_FRAGMENT_FIRST : LUMINANCE_REDUCE_FRAGMENT);
  754. 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);
  755. 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])));
  756. 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);
  757. if (final) {
  758. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_prev_luminance), 1);
  759. }
  760. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  761. RD::get_singleton()->draw_list_set_push_constant(draw_list, &luminance_reduce_raster.push_constant, sizeof(LuminanceReduceRasterPushConstant));
  762. RD::get_singleton()->draw_list_draw(draw_list, true);
  763. RD::get_singleton()->draw_list_end();
  764. }
  765. }
  766. void EffectsRD::bokeh_dof(const BokehBuffers &p_buffers, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_bokeh_size, RenderingServer::DOFBokehShape p_bokeh_shape, RS::DOFBlurQuality p_quality, bool p_use_jitter, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  767. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of BOKEH DOF with the mobile renderer.");
  768. bokeh.push_constant.blur_far_active = p_dof_far;
  769. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  770. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  771. bokeh.push_constant.blur_near_active = p_dof_near;
  772. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  773. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  774. bokeh.push_constant.use_jitter = p_use_jitter;
  775. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  776. bokeh.push_constant.z_near = p_cam_znear;
  777. bokeh.push_constant.z_far = p_cam_zfar;
  778. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  779. bokeh.push_constant.blur_size = p_bokeh_size;
  780. bokeh.push_constant.second_pass = false;
  781. bokeh.push_constant.half_size = false;
  782. bokeh.push_constant.blur_scale = 0.5;
  783. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  784. /* FIRST PASS */
  785. // The alpha channel of the source color texture is filled with the expected circle size
  786. // If used for DOF far, the size is positive, if used for near, its negative.
  787. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE]);
  788. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  789. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.depth_texture), 1);
  790. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  791. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  792. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  793. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  794. RD::get_singleton()->compute_list_add_barrier(compute_list);
  795. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  796. //second pass
  797. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL]);
  798. static const int quality_samples[4] = { 6, 12, 12, 24 };
  799. bokeh.push_constant.steps = quality_samples[p_quality];
  800. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  801. //box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
  802. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[0]), 0);
  803. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  804. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  805. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  806. bokeh.push_constant.half_size = true;
  807. bokeh.push_constant.blur_size *= 0.5;
  808. } else {
  809. //medium and high quality use full size
  810. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.secondary_texture), 0);
  811. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  812. }
  813. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  814. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  815. RD::get_singleton()->compute_list_add_barrier(compute_list);
  816. //third pass
  817. bokeh.push_constant.second_pass = true;
  818. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  819. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[1]), 0);
  820. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[0]), 1);
  821. } else {
  822. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  823. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.secondary_texture), 1);
  824. }
  825. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  826. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  827. RD::get_singleton()->compute_list_add_barrier(compute_list);
  828. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  829. //forth pass, upscale for low quality
  830. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  831. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  832. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[1]), 1);
  833. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  834. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  835. bokeh.push_constant.half_size = false;
  836. bokeh.push_constant.second_pass = false;
  837. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  838. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  839. }
  840. } else {
  841. //circle
  842. //second pass
  843. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  844. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  845. bokeh.push_constant.steps = 0;
  846. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  847. //circle always runs in half size, otherwise too expensive
  848. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[0]), 0);
  849. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  850. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  851. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  852. bokeh.push_constant.half_size = true;
  853. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  854. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  855. RD::get_singleton()->compute_list_add_barrier(compute_list);
  856. //circle is just one pass, then upscale
  857. // upscale
  858. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  859. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  860. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[0]), 1);
  861. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  862. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  863. bokeh.push_constant.half_size = false;
  864. bokeh.push_constant.second_pass = false;
  865. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  866. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  867. }
  868. RD::get_singleton()->compute_list_end();
  869. }
  870. void EffectsRD::bokeh_dof_raster(const BokehBuffers &p_buffers, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_dof_blur_amount, RenderingServer::DOFBokehShape p_bokeh_shape, RS::DOFBlurQuality p_quality, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  871. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use blur DOF with the clustered renderer.");
  872. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  873. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  874. bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;
  875. bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;
  876. bokeh.push_constant.z_far = p_cam_zfar;
  877. bokeh.push_constant.z_near = p_cam_znear;
  878. bokeh.push_constant.second_pass = false;
  879. bokeh.push_constant.half_size = false;
  880. bokeh.push_constant.blur_size = p_dof_blur_amount;
  881. if (p_dof_far || p_dof_near) {
  882. if (p_dof_far) {
  883. bokeh.push_constant.blur_far_active = true;
  884. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  885. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  886. }
  887. if (p_dof_near) {
  888. bokeh.push_constant.blur_near_active = true;
  889. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  890. bokeh.push_constant.blur_near_end = p_dof_near_begin - p_dof_near_size;
  891. }
  892. {
  893. // generate our depth data
  894. RID framebuffer = p_buffers.base_weight_fb;
  895. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  896. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[BOKEH_GEN_BLUR_SIZE].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  897. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.depth_texture), 0);
  898. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  899. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  900. RD::get_singleton()->draw_list_draw(draw_list, true);
  901. RD::get_singleton()->draw_list_end();
  902. }
  903. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  904. // double pass approach
  905. BokehMode mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  906. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  907. //box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
  908. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  909. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  910. bokeh.push_constant.half_size = true;
  911. bokeh.push_constant.blur_size *= 0.5;
  912. }
  913. static const int quality_samples[4] = { 6, 12, 12, 24 };
  914. bokeh.push_constant.blur_scale = 0.5;
  915. bokeh.push_constant.steps = quality_samples[p_quality];
  916. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  917. // Pass 1
  918. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  919. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  920. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.base_texture), 0);
  921. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 1);
  922. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  923. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  924. RD::get_singleton()->draw_list_draw(draw_list, true);
  925. RD::get_singleton()->draw_list_end();
  926. // Pass 2
  927. if (!bokeh.push_constant.half_size) {
  928. // do not output weight, we're writing back into our base buffer
  929. mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;
  930. }
  931. bokeh.push_constant.second_pass = true;
  932. framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;
  933. RID texture = bokeh.push_constant.half_size ? p_buffers.half_texture[0] : p_buffers.secondary_texture;
  934. RID weight = bokeh.push_constant.half_size ? p_buffers.weight_texture[2] : p_buffers.weight_texture[1];
  935. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  936. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  937. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(texture), 0);
  938. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(weight), 1);
  939. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  940. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  941. RD::get_singleton()->draw_list_draw(draw_list, true);
  942. RD::get_singleton()->draw_list_end();
  943. if (bokeh.push_constant.half_size) {
  944. // Compose pass
  945. mode = BOKEH_COMPOSITE;
  946. framebuffer = p_buffers.base_fb;
  947. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  948. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  949. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.half_texture[1]), 0);
  950. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[3]), 1);
  951. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 2);
  952. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  953. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  954. RD::get_singleton()->draw_list_draw(draw_list, true);
  955. RD::get_singleton()->draw_list_end();
  956. }
  957. } else {
  958. // circular is a single pass approach
  959. BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;
  960. {
  961. // circle always runs in half size, otherwise too expensive (though the code below does support making this optional)
  962. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  963. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  964. bokeh.push_constant.half_size = true;
  965. // bokeh.push_constant.blur_size *= 0.5;
  966. }
  967. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  968. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  969. bokeh.push_constant.steps = 0.0;
  970. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  971. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  972. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  973. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.base_texture), 0);
  974. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 1);
  975. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  976. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  977. RD::get_singleton()->draw_list_draw(draw_list, true);
  978. RD::get_singleton()->draw_list_end();
  979. if (bokeh.push_constant.half_size) {
  980. // Compose
  981. mode = BOKEH_COMPOSITE;
  982. framebuffer = p_buffers.base_fb;
  983. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  984. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  985. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.half_texture[0]), 0);
  986. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[2]), 1);
  987. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 2);
  988. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  989. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  990. RD::get_singleton()->draw_list_draw(draw_list, true);
  991. RD::get_singleton()->draw_list_end();
  992. } else {
  993. // Just copy it back (we use our blur raster shader here)..
  994. draw_list = RD::get_singleton()->draw_list_begin(p_buffers.base_fb, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  995. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[BLUR_MODE_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_buffers.base_fb)));
  996. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.secondary_texture), 0);
  997. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  998. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  999. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  1000. RD::get_singleton()->draw_list_draw(draw_list, true);
  1001. RD::get_singleton()->draw_list_end();
  1002. }
  1003. }
  1004. }
  1005. }
  1006. 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) {
  1007. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  1008. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  1009. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  1010. }
  1011. for (int i = 0; i < 4; i++) {
  1012. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1013. continue;
  1014. }
  1015. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  1016. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  1017. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  1018. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  1019. ssao.gather_push_constant.pass = i;
  1020. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 2);
  1021. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  1022. 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));
  1023. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  1024. }
  1025. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  1026. }
  1027. void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_depth_mipmaps_texture, const Vector<RID> &p_depth_mipmaps, 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_downsample_uniform_set, RID &r_gather_uniform_set, RID &r_importance_map_uniform_set) {
  1028. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1029. RD::get_singleton()->draw_command_begin_label("SSAO");
  1030. /* FIRST PASS */
  1031. // Downsample and deinterleave the depth buffer.
  1032. {
  1033. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  1034. if (p_invalidate_uniform_sets) {
  1035. Vector<RD::Uniform> uniforms;
  1036. {
  1037. RD::Uniform u;
  1038. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1039. u.binding = 0;
  1040. u.ids.push_back(p_depth_mipmaps[1]);
  1041. uniforms.push_back(u);
  1042. }
  1043. {
  1044. RD::Uniform u;
  1045. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1046. u.binding = 1;
  1047. u.ids.push_back(p_depth_mipmaps[2]);
  1048. uniforms.push_back(u);
  1049. }
  1050. {
  1051. RD::Uniform u;
  1052. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1053. u.binding = 2;
  1054. u.ids.push_back(p_depth_mipmaps[3]);
  1055. uniforms.push_back(u);
  1056. }
  1057. r_downsample_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.downsample_shader.version_get_shader(ssao.downsample_shader_version, 2), 2);
  1058. }
  1059. float depth_linearize_mul = -p_projection.matrix[3][2];
  1060. float depth_linearize_add = p_projection.matrix[2][2];
  1061. if (depth_linearize_mul * depth_linearize_add < 0) {
  1062. depth_linearize_add = -depth_linearize_add;
  1063. }
  1064. ssao.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  1065. ssao.downsample_push_constant.z_near = depth_linearize_mul;
  1066. ssao.downsample_push_constant.z_far = depth_linearize_add;
  1067. if (ssao.downsample_push_constant.orthogonal) {
  1068. ssao.downsample_push_constant.z_near = p_projection.get_z_near();
  1069. ssao.downsample_push_constant.z_far = p_projection.get_z_far();
  1070. }
  1071. ssao.downsample_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1072. ssao.downsample_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1073. ssao.downsample_push_constant.radius_sq = p_settings.radius * p_settings.radius;
  1074. int downsample_pipeline = SSAO_DOWNSAMPLE;
  1075. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1076. downsample_pipeline = SSAO_DOWNSAMPLE_HALF;
  1077. } else if (p_settings.quality > RS::ENV_SSAO_QUALITY_MEDIUM) {
  1078. downsample_pipeline = SSAO_DOWNSAMPLE_MIPMAP;
  1079. }
  1080. if (p_settings.half_size) {
  1081. downsample_pipeline++;
  1082. }
  1083. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[downsample_pipeline]);
  1084. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  1085. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_depth_mipmaps[0]), 1);
  1086. if (p_settings.quality > RS::ENV_SSAO_QUALITY_MEDIUM) {
  1087. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, r_downsample_uniform_set, 2);
  1088. }
  1089. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.downsample_push_constant, sizeof(SSAODownsamplePushConstant));
  1090. Size2i size(MAX(1, p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1)), MAX(1, p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1)));
  1091. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  1092. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1093. RD::get_singleton()->draw_command_end_label(); // Downsample SSAO
  1094. }
  1095. /* SECOND PASS */
  1096. // Sample SSAO
  1097. {
  1098. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  1099. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  1100. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  1101. ssao.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1102. ssao.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1103. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  1104. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  1105. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  1106. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  1107. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  1108. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  1109. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  1110. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  1111. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  1112. ssao.gather_push_constant.radius = p_settings.radius;
  1113. float radius_near_limit = (p_settings.radius * 1.2f);
  1114. if (p_settings.quality <= RS::ENV_SSAO_QUALITY_LOW) {
  1115. radius_near_limit *= 1.50f;
  1116. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1117. ssao.gather_push_constant.radius *= 0.8f;
  1118. }
  1119. }
  1120. radius_near_limit /= tan_half_fov_y;
  1121. ssao.gather_push_constant.intensity = p_settings.intensity;
  1122. ssao.gather_push_constant.shadow_power = p_settings.power;
  1123. ssao.gather_push_constant.shadow_clamp = 0.98;
  1124. ssao.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  1125. ssao.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  1126. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  1127. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  1128. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  1129. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  1130. ssao.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  1131. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  1132. ssao.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  1133. ssao.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  1134. if (p_invalidate_uniform_sets) {
  1135. Vector<RD::Uniform> uniforms;
  1136. {
  1137. RD::Uniform u;
  1138. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1139. u.binding = 0;
  1140. u.ids.push_back(ssao.mirror_sampler);
  1141. u.ids.push_back(p_depth_mipmaps_texture);
  1142. uniforms.push_back(u);
  1143. }
  1144. {
  1145. RD::Uniform u;
  1146. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1147. u.binding = 1;
  1148. u.ids.push_back(p_normal_buffer);
  1149. uniforms.push_back(u);
  1150. }
  1151. {
  1152. RD::Uniform u;
  1153. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1154. u.binding = 2;
  1155. u.ids.push_back(ssao.gather_constants_buffer);
  1156. uniforms.push_back(u);
  1157. }
  1158. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 0), 0);
  1159. }
  1160. if (p_invalidate_uniform_sets) {
  1161. Vector<RD::Uniform> uniforms;
  1162. {
  1163. RD::Uniform u;
  1164. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1165. u.binding = 0;
  1166. u.ids.push_back(p_ao_pong);
  1167. uniforms.push_back(u);
  1168. }
  1169. {
  1170. RD::Uniform u;
  1171. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1172. u.binding = 1;
  1173. u.ids.push_back(default_sampler);
  1174. u.ids.push_back(p_importance_map);
  1175. uniforms.push_back(u);
  1176. }
  1177. {
  1178. RD::Uniform u;
  1179. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1180. u.binding = 2;
  1181. u.ids.push_back(ssao.importance_map_load_counter);
  1182. uniforms.push_back(u);
  1183. }
  1184. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 2), 1);
  1185. }
  1186. if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  1187. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  1188. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1189. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1190. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  1191. ssao.importance_map_push_constant.power = p_settings.power;
  1192. //base pass
  1193. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  1194. gather_ssao(compute_list, p_ao_pong_slices, p_settings, true, r_gather_uniform_set, RID());
  1195. //generate importance map
  1196. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  1197. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 0);
  1198. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1199. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1200. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1201. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1202. //process importance map A
  1203. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  1204. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  1205. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  1206. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1207. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1208. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1209. //process Importance Map B
  1210. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  1211. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  1212. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1213. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  1214. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1215. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1216. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1217. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  1218. RD::get_singleton()->draw_command_end_label(); // Importance Map
  1219. } else {
  1220. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  1221. }
  1222. gather_ssao(compute_list, p_ao_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set);
  1223. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  1224. }
  1225. // /* THIRD PASS */
  1226. // // Blur
  1227. //
  1228. {
  1229. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  1230. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  1231. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1232. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1233. int blur_passes = p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  1234. for (int pass = 0; pass < blur_passes; pass++) {
  1235. int blur_pipeline = SSAO_BLUR_PASS;
  1236. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1237. if (pass < blur_passes - 2) {
  1238. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  1239. } else {
  1240. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1241. }
  1242. }
  1243. for (int i = 0; i < 4; i++) {
  1244. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1245. continue;
  1246. }
  1247. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  1248. if (pass % 2 == 0) {
  1249. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1250. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_slices[i]), 0);
  1251. } else {
  1252. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_slices[i], ssao.mirror_sampler), 0);
  1253. }
  1254. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_pong_slices[i]), 1);
  1255. } else {
  1256. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1257. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong_slices[i]), 0);
  1258. } else {
  1259. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_pong_slices[i], ssao.mirror_sampler), 0);
  1260. }
  1261. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 1);
  1262. }
  1263. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  1264. 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));
  1265. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  1266. }
  1267. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1268. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1269. }
  1270. }
  1271. RD::get_singleton()->draw_command_end_label(); // Blur
  1272. }
  1273. /* FOURTH PASS */
  1274. // Interleave buffers
  1275. // back to full size
  1276. {
  1277. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1278. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1279. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1280. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1281. ssao.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  1282. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1283. if (p_settings.quality == RS::ENV_SSAO_QUALITY_LOW) {
  1284. interleave_pipeline = SSAO_INTERLEAVE;
  1285. } else if (p_settings.quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1286. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1287. }
  1288. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  1289. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 0);
  1290. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  1291. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao), 1);
  1292. } else {
  1293. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 1);
  1294. }
  1295. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1296. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1297. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1298. RD::get_singleton()->draw_command_end_label(); // Interleave
  1299. }
  1300. RD::get_singleton()->draw_command_end_label(); //SSAO
  1301. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_TRANSFER); //wait for upcoming transfer
  1302. int zero[1] = { 0 };
  1303. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1304. }
  1305. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  1306. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  1307. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  1308. roughness_limiter.push_constant.curve = p_curve;
  1309. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1310. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  1311. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  1312. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  1313. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  1314. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  1315. RD::get_singleton()->compute_list_end();
  1316. }
  1317. void EffectsRD::cubemap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  1318. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
  1319. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  1320. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  1321. roughness.push_constant.roughness = p_roughness;
  1322. roughness.push_constant.sample_count = p_sample_count;
  1323. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1324. roughness.push_constant.face_size = p_size;
  1325. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1326. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
  1327. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  1328. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 1);
  1329. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  1330. int x_groups = (p_size - 1) / 8 + 1;
  1331. int y_groups = (p_size - 1) / 8 + 1;
  1332. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  1333. RD::get_singleton()->compute_list_end();
  1334. }
  1335. void EffectsRD::cubemap_roughness_raster(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  1336. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
  1337. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
  1338. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  1339. roughness.push_constant.face_id = p_face_id;
  1340. roughness.push_constant.roughness = p_roughness;
  1341. roughness.push_constant.sample_count = p_sample_count;
  1342. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1343. roughness.push_constant.face_size = p_size;
  1344. 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_DISCARD);
  1345. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  1346. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  1347. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1348. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  1349. RD::get_singleton()->draw_list_draw(draw_list, true);
  1350. RD::get_singleton()->draw_list_end();
  1351. }
  1352. void EffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  1353. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
  1354. cubemap_downsampler.push_constant.face_size = p_size.x;
  1355. cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
  1356. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1357. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
  1358. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  1359. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  1360. int x_groups = (p_size.x - 1) / 8 + 1;
  1361. int y_groups = (p_size.y - 1) / 8 + 1;
  1362. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  1363. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  1364. RD::get_singleton()->compute_list_end();
  1365. }
  1366. void EffectsRD::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
  1367. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
  1368. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
  1369. cubemap_downsampler.push_constant.face_size = p_size.x;
  1370. cubemap_downsampler.push_constant.face_id = p_face_id;
  1371. 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_DISCARD);
  1372. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cubemap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  1373. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_cubemap), 0);
  1374. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1375. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  1376. RD::get_singleton()->draw_list_draw(draw_list, true);
  1377. RD::get_singleton()->draw_list_end();
  1378. }
  1379. void EffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  1380. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
  1381. Vector<RD::Uniform> uniforms;
  1382. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  1383. RD::Uniform u;
  1384. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1385. u.binding = i;
  1386. u.ids.push_back(p_dest_cubemap[i]);
  1387. uniforms.push_back(u);
  1388. }
  1389. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1390. RD::get_singleton()->free(filter.image_uniform_set);
  1391. }
  1392. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  1393. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  1394. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  1395. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1396. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[pipeline]);
  1397. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  1398. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  1399. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  1400. int x_groups = p_use_array ? 1792 : 342; // (128 * 128 * 7) / 64 : (128*128 + 64*64 + 32*32 + 16*16 + 8*8 + 4*4 + 2*2) / 64
  1401. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  1402. RD::get_singleton()->compute_list_end();
  1403. }
  1404. void EffectsRD::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
  1405. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
  1406. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
  1407. // TODO implement!
  1408. CubemapFilterRasterPushConstant push_constant;
  1409. push_constant.mip_level = p_mip_level;
  1410. push_constant.face_id = p_face_id;
  1411. CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
  1412. 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_DISCARD);
  1413. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, filter.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  1414. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_cubemap), 0);
  1415. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  1416. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1417. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
  1418. RD::get_singleton()->draw_list_draw(draw_list, true);
  1419. RD::get_singleton()->draw_list_end();
  1420. }
  1421. void EffectsRD::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_voxel_gi, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_voxel_gi, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
  1422. ResolvePushConstant push_constant;
  1423. push_constant.screen_size[0] = p_screen_size.x;
  1424. push_constant.screen_size[1] = p_screen_size.y;
  1425. push_constant.samples = p_samples;
  1426. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1427. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[p_source_voxel_gi.is_valid() ? RESOLVE_MODE_GI_VOXEL_GI : RESOLVE_MODE_GI]);
  1428. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_source_depth, p_source_normal_roughness), 0);
  1429. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_dest_depth, p_dest_normal_roughness), 1);
  1430. if (p_source_voxel_gi.is_valid()) {
  1431. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_voxel_gi), 2);
  1432. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_voxel_gi), 3);
  1433. }
  1434. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  1435. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
  1436. RD::get_singleton()->compute_list_end(p_barrier);
  1437. }
  1438. void EffectsRD::resolve_depth(RID p_source_depth, RID p_dest_depth, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
  1439. ResolvePushConstant push_constant;
  1440. push_constant.screen_size[0] = p_screen_size.x;
  1441. push_constant.screen_size[1] = p_screen_size.y;
  1442. push_constant.samples = p_samples;
  1443. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1444. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[RESOLVE_MODE_DEPTH]);
  1445. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_depth), 0);
  1446. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_depth), 1);
  1447. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  1448. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
  1449. RD::get_singleton()->compute_list_end(p_barrier);
  1450. }
  1451. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  1452. Sort::PushConstant push_constant;
  1453. push_constant.total_elements = p_size;
  1454. bool done = true;
  1455. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1456. if (numThreadGroups > 1) {
  1457. done = false;
  1458. }
  1459. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1460. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1461. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1462. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1463. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1464. int presorted = 512;
  1465. while (!done) {
  1466. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1467. done = true;
  1468. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1469. numThreadGroups = 0;
  1470. if (p_size > presorted) {
  1471. if (p_size > presorted * 2) {
  1472. done = false;
  1473. }
  1474. int pow2 = presorted;
  1475. while (pow2 < p_size) {
  1476. pow2 *= 2;
  1477. }
  1478. numThreadGroups = pow2 >> 9;
  1479. }
  1480. unsigned int nMergeSize = presorted * 2;
  1481. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1482. push_constant.job_params[0] = nMergeSubSize;
  1483. if (nMergeSubSize == nMergeSize >> 1) {
  1484. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1485. push_constant.job_params[2] = -1;
  1486. } else {
  1487. push_constant.job_params[1] = nMergeSubSize;
  1488. push_constant.job_params[2] = 1;
  1489. }
  1490. push_constant.job_params[3] = 0;
  1491. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1492. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1493. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1494. }
  1495. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1496. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1497. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1498. presorted *= 2;
  1499. }
  1500. RD::get_singleton()->compute_list_end();
  1501. }
  1502. EffectsRD::EffectsRD(bool p_prefer_raster_effects) {
  1503. prefer_raster_effects = p_prefer_raster_effects;
  1504. if (prefer_raster_effects) {
  1505. // init blur shader (on compute use copy shader)
  1506. Vector<String> blur_modes;
  1507. blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP
  1508. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR
  1509. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW
  1510. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE
  1511. blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
  1512. blur_raster.shader.initialize(blur_modes);
  1513. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  1514. blur_raster.shader_version = blur_raster.shader.version_create();
  1515. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  1516. blur_raster.pipelines[i].setup(blur_raster.shader.version_get_shader(blur_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1517. }
  1518. } else {
  1519. // not used in clustered
  1520. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  1521. blur_raster.pipelines[i].clear();
  1522. }
  1523. }
  1524. if (!prefer_raster_effects) { // Initialize copy
  1525. Vector<String> copy_modes;
  1526. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  1527. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  1528. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  1529. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  1530. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  1531. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  1532. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  1533. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  1534. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  1535. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  1536. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  1537. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  1538. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  1539. copy.shader.initialize(copy_modes);
  1540. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  1541. if (prefer_raster_effects) {
  1542. // disable shaders we can't use
  1543. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_COPY, false);
  1544. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_COPY_8BIT, false);
  1545. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_GLOW, false);
  1546. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE, false);
  1547. }
  1548. copy.shader_version = copy.shader.version_create();
  1549. for (int i = 0; i < COPY_MODE_MAX; i++) {
  1550. if (copy.shader.is_variant_enabled(i)) {
  1551. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  1552. }
  1553. }
  1554. }
  1555. {
  1556. Vector<String> copy_modes;
  1557. copy_modes.push_back("\n");
  1558. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  1559. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  1560. copy_to_fb.shader.initialize(copy_modes);
  1561. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  1562. //use additive
  1563. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  1564. copy_to_fb.pipelines[i].setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1565. }
  1566. }
  1567. {
  1568. // Initialize roughness
  1569. Vector<String> cubemap_roughness_modes;
  1570. cubemap_roughness_modes.push_back("");
  1571. if (prefer_raster_effects) {
  1572. roughness.raster_shader.initialize(cubemap_roughness_modes);
  1573. roughness.shader_version = roughness.raster_shader.version_create();
  1574. roughness.raster_pipeline.setup(roughness.raster_shader.version_get_shader(roughness.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1575. } else {
  1576. roughness.compute_shader.initialize(cubemap_roughness_modes);
  1577. roughness.shader_version = roughness.compute_shader.version_create();
  1578. roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));
  1579. roughness.raster_pipeline.clear();
  1580. }
  1581. }
  1582. {
  1583. // Initialize tonemapper
  1584. Vector<String> tonemap_modes;
  1585. tonemap_modes.push_back("\n");
  1586. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  1587. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  1588. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1589. tonemap_modes.push_back("\n#define SUBPASS\n");
  1590. tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n");
  1591. // multiview versions of our shaders
  1592. tonemap_modes.push_back("\n#define MULTIVIEW\n");
  1593. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
  1594. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_1D_LUT\n");
  1595. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1596. tonemap_modes.push_back("\n#define MULTIVIEW\n#define SUBPASS\n");
  1597. tonemap_modes.push_back("\n#define MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n");
  1598. tonemap.shader.initialize(tonemap_modes);
  1599. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  1600. tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
  1601. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
  1602. tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
  1603. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
  1604. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_MULTIVIEW, false);
  1605. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW, false);
  1606. }
  1607. tonemap.shader_version = tonemap.shader.version_create();
  1608. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  1609. if (tonemap.shader.is_variant_enabled(i)) {
  1610. tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1611. } else {
  1612. tonemap.pipelines[i].clear();
  1613. }
  1614. }
  1615. }
  1616. if (prefer_raster_effects) {
  1617. Vector<String> luminance_reduce_modes;
  1618. luminance_reduce_modes.push_back("\n#define FIRST_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FIRST
  1619. luminance_reduce_modes.push_back("\n"); // LUMINANCE_REDUCE_FRAGMENT
  1620. luminance_reduce_modes.push_back("\n#define FINAL_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FINAL
  1621. luminance_reduce_raster.shader.initialize(luminance_reduce_modes);
  1622. memset(&luminance_reduce_raster.push_constant, 0, sizeof(LuminanceReduceRasterPushConstant));
  1623. luminance_reduce_raster.shader_version = luminance_reduce_raster.shader.version_create();
  1624. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1625. 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);
  1626. }
  1627. } else {
  1628. // Initialize luminance_reduce
  1629. Vector<String> luminance_reduce_modes;
  1630. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1631. luminance_reduce_modes.push_back("\n");
  1632. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1633. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1634. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1635. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1636. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1637. }
  1638. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1639. luminance_reduce_raster.pipelines[i].clear();
  1640. }
  1641. }
  1642. {
  1643. // Initialize copier
  1644. Vector<String> copy_modes;
  1645. copy_modes.push_back("\n");
  1646. cube_to_dp.shader.initialize(copy_modes);
  1647. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  1648. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  1649. RD::PipelineDepthStencilState dss;
  1650. dss.enable_depth_test = true;
  1651. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  1652. dss.enable_depth_write = true;
  1653. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  1654. }
  1655. // Initialize bokeh
  1656. Vector<String> bokeh_modes;
  1657. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  1658. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n#define OUTPUT_WEIGHT\n");
  1659. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  1660. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n#define OUTPUT_WEIGHT\n");
  1661. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  1662. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n#define OUTPUT_WEIGHT\n");
  1663. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  1664. if (prefer_raster_effects) {
  1665. bokeh.raster_shader.initialize(bokeh_modes);
  1666. bokeh.shader_version = bokeh.raster_shader.version_create();
  1667. const int att_count[BOKEH_MAX] = { 1, 2, 1, 2, 1, 2, 1 };
  1668. for (int i = 0; i < BOKEH_MAX; i++) {
  1669. RD::PipelineColorBlendState blend_state = (i == BOKEH_COMPOSITE) ? RD::PipelineColorBlendState::create_blend(att_count[i]) : RD::PipelineColorBlendState::create_disabled(att_count[i]);
  1670. bokeh.raster_pipelines[i].setup(bokeh.raster_shader.version_get_shader(bokeh.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  1671. }
  1672. } else {
  1673. bokeh.compute_shader.initialize(bokeh_modes);
  1674. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_BOX_NOWEIGHT, false);
  1675. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT, false);
  1676. bokeh.shader_version = bokeh.compute_shader.version_create();
  1677. for (int i = 0; i < BOKEH_MAX; i++) {
  1678. if (bokeh.compute_shader.is_variant_enabled(i)) {
  1679. bokeh.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.compute_shader.version_get_shader(bokeh.shader_version, i));
  1680. }
  1681. }
  1682. for (int i = 0; i < BOKEH_MAX; i++) {
  1683. bokeh.raster_pipelines[i].clear();
  1684. }
  1685. }
  1686. if (!prefer_raster_effects) {
  1687. // Initialize ssao
  1688. RD::SamplerState sampler;
  1689. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1690. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1691. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1692. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1693. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1694. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1695. sampler.max_lod = 4;
  1696. ssao.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  1697. uint32_t pipeline = 0;
  1698. {
  1699. Vector<String> ssao_modes;
  1700. ssao_modes.push_back("\n");
  1701. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  1702. ssao_modes.push_back("\n#define GENERATE_MIPS\n");
  1703. ssao_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE");
  1704. ssao_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  1705. ssao_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE");
  1706. ssao.downsample_shader.initialize(ssao_modes);
  1707. ssao.downsample_shader_version = ssao.downsample_shader.version_create();
  1708. for (int i = 0; i <= SSAO_DOWNSAMPLE_HALF_RES_HALF; i++) {
  1709. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.downsample_shader.version_get_shader(ssao.downsample_shader_version, i));
  1710. pipeline++;
  1711. }
  1712. }
  1713. {
  1714. Vector<String> ssao_modes;
  1715. ssao_modes.push_back("\n");
  1716. ssao_modes.push_back("\n#define SSAO_BASE\n");
  1717. ssao_modes.push_back("\n#define ADAPTIVE\n");
  1718. ssao.gather_shader.initialize(ssao_modes);
  1719. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1720. for (int i = SSAO_GATHER; i <= SSAO_GATHER_ADAPTIVE; i++) {
  1721. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER));
  1722. pipeline++;
  1723. }
  1724. ssao.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSAOGatherConstants));
  1725. SSAOGatherConstants gather_constants;
  1726. const int sub_pass_count = 5;
  1727. for (int pass = 0; pass < 4; pass++) {
  1728. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  1729. int a = pass;
  1730. int spmap[5]{ 0, 1, 4, 3, 2 };
  1731. int b = spmap[subPass];
  1732. float ca, sa;
  1733. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  1734. ca = Math::cos(angle0);
  1735. sa = Math::sin(angle0);
  1736. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  1737. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  1738. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  1739. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  1740. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  1741. }
  1742. }
  1743. RD::get_singleton()->buffer_update(ssao.gather_constants_buffer, 0, sizeof(SSAOGatherConstants), &gather_constants);
  1744. }
  1745. {
  1746. Vector<String> ssao_modes;
  1747. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  1748. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  1749. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  1750. ssao.importance_map_shader.initialize(ssao_modes);
  1751. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  1752. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  1753. 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));
  1754. pipeline++;
  1755. }
  1756. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  1757. int zero[1] = { 0 };
  1758. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1759. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  1760. Vector<RD::Uniform> uniforms;
  1761. {
  1762. RD::Uniform u;
  1763. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1764. u.binding = 0;
  1765. u.ids.push_back(ssao.importance_map_load_counter);
  1766. uniforms.push_back(u);
  1767. }
  1768. 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);
  1769. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  1770. }
  1771. {
  1772. Vector<String> ssao_modes;
  1773. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1774. ssao_modes.push_back("\n#define MODE_SMART\n");
  1775. ssao_modes.push_back("\n#define MODE_WIDE\n");
  1776. ssao.blur_shader.initialize(ssao_modes);
  1777. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1778. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  1779. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1780. pipeline++;
  1781. }
  1782. }
  1783. {
  1784. Vector<String> ssao_modes;
  1785. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1786. ssao_modes.push_back("\n#define MODE_SMART\n");
  1787. ssao_modes.push_back("\n#define MODE_HALF\n");
  1788. ssao.interleave_shader.initialize(ssao_modes);
  1789. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  1790. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  1791. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  1792. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  1793. pipeline++;
  1794. }
  1795. }
  1796. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1797. }
  1798. if (!prefer_raster_effects) {
  1799. // Initialize roughness limiter
  1800. Vector<String> shader_modes;
  1801. shader_modes.push_back("");
  1802. roughness_limiter.shader.initialize(shader_modes);
  1803. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1804. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1805. }
  1806. {
  1807. //Initialize cubemap downsampler
  1808. Vector<String> cubemap_downsampler_modes;
  1809. cubemap_downsampler_modes.push_back("");
  1810. if (prefer_raster_effects) {
  1811. cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);
  1812. cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();
  1813. cubemap_downsampler.raster_pipeline.setup(cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1814. } else {
  1815. cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);
  1816. cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();
  1817. cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  1818. cubemap_downsampler.raster_pipeline.clear();
  1819. }
  1820. }
  1821. {
  1822. // Initialize cubemap filter
  1823. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  1824. Vector<String> cubemap_filter_modes;
  1825. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  1826. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  1827. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1828. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1829. if (filter.use_high_quality) {
  1830. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  1831. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  1832. } else {
  1833. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  1834. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  1835. }
  1836. if (prefer_raster_effects) {
  1837. filter.raster_shader.initialize(cubemap_filter_modes);
  1838. // array variants are not supported in raster
  1839. filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);
  1840. filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);
  1841. filter.shader_version = filter.raster_shader.version_create();
  1842. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1843. if (filter.raster_shader.is_variant_enabled(i)) {
  1844. filter.raster_pipelines[i].setup(filter.raster_shader.version_get_shader(filter.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1845. } else {
  1846. filter.raster_pipelines[i].clear();
  1847. }
  1848. }
  1849. Vector<RD::Uniform> uniforms;
  1850. {
  1851. RD::Uniform u;
  1852. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1853. u.binding = 0;
  1854. u.ids.push_back(filter.coefficient_buffer);
  1855. uniforms.push_back(u);
  1856. }
  1857. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.raster_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  1858. } else {
  1859. filter.compute_shader.initialize(cubemap_filter_modes);
  1860. filter.shader_version = filter.compute_shader.version_create();
  1861. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1862. filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));
  1863. filter.raster_pipelines[i].clear();
  1864. }
  1865. Vector<RD::Uniform> uniforms;
  1866. {
  1867. RD::Uniform u;
  1868. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1869. u.binding = 0;
  1870. u.ids.push_back(filter.coefficient_buffer);
  1871. uniforms.push_back(u);
  1872. }
  1873. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  1874. }
  1875. }
  1876. if (!prefer_raster_effects) {
  1877. Vector<String> specular_modes;
  1878. specular_modes.push_back("\n#define MODE_MERGE\n");
  1879. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1880. specular_modes.push_back("\n");
  1881. specular_modes.push_back("\n#define MODE_SSR\n");
  1882. specular_merge.shader.initialize(specular_modes);
  1883. specular_merge.shader_version = specular_merge.shader.version_create();
  1884. //use additive
  1885. RD::PipelineColorBlendState::Attachment ba;
  1886. ba.enable_blend = true;
  1887. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1888. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1889. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1890. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1891. ba.color_blend_op = RD::BLEND_OP_ADD;
  1892. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1893. RD::PipelineColorBlendState blend_additive;
  1894. blend_additive.attachments.push_back(ba);
  1895. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1896. RD::PipelineColorBlendState blend_state;
  1897. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1898. blend_state = blend_additive;
  1899. } else {
  1900. blend_state = RD::PipelineColorBlendState::create_disabled();
  1901. }
  1902. 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);
  1903. }
  1904. }
  1905. if (!prefer_raster_effects) {
  1906. {
  1907. Vector<String> ssr_modes;
  1908. ssr_modes.push_back("\n");
  1909. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1910. ssr.shader.initialize(ssr_modes);
  1911. ssr.shader_version = ssr.shader.version_create();
  1912. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1913. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1914. }
  1915. }
  1916. {
  1917. Vector<String> ssr_filter_modes;
  1918. ssr_filter_modes.push_back("\n");
  1919. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1920. ssr_filter.shader.initialize(ssr_filter_modes);
  1921. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1922. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1923. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1924. }
  1925. }
  1926. {
  1927. Vector<String> ssr_scale_modes;
  1928. ssr_scale_modes.push_back("\n");
  1929. ssr_scale.shader.initialize(ssr_scale_modes);
  1930. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1931. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1932. }
  1933. {
  1934. Vector<String> sss_modes;
  1935. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1936. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1937. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1938. sss.shader.initialize(sss_modes);
  1939. sss.shader_version = sss.shader.version_create();
  1940. for (int i = 0; i < sss_modes.size(); i++) {
  1941. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1942. }
  1943. }
  1944. {
  1945. Vector<String> resolve_modes;
  1946. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
  1947. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define VOXEL_GI_RESOLVE\n");
  1948. resolve_modes.push_back("\n#define MODE_RESOLVE_DEPTH\n");
  1949. resolve.shader.initialize(resolve_modes);
  1950. resolve.shader_version = resolve.shader.version_create();
  1951. for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
  1952. resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
  1953. }
  1954. }
  1955. }
  1956. {
  1957. Vector<String> sort_modes;
  1958. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  1959. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  1960. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  1961. sort.shader.initialize(sort_modes);
  1962. sort.shader_version = sort.shader.version_create();
  1963. for (int i = 0; i < SORT_MODE_MAX; i++) {
  1964. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  1965. }
  1966. }
  1967. RD::SamplerState sampler;
  1968. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1969. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1970. sampler.max_lod = 0;
  1971. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1972. RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");
  1973. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1974. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1975. sampler.max_lod = 1e20;
  1976. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1977. RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");
  1978. { //create index array for copy shaders
  1979. Vector<uint8_t> pv;
  1980. pv.resize(6 * 4);
  1981. {
  1982. uint8_t *w = pv.ptrw();
  1983. int *p32 = (int *)w;
  1984. p32[0] = 0;
  1985. p32[1] = 1;
  1986. p32[2] = 2;
  1987. p32[3] = 0;
  1988. p32[4] = 2;
  1989. p32[5] = 3;
  1990. }
  1991. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1992. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1993. }
  1994. }
  1995. EffectsRD::~EffectsRD() {
  1996. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1997. RD::get_singleton()->free(filter.image_uniform_set);
  1998. }
  1999. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  2000. RD::get_singleton()->free(filter.uniform_set);
  2001. }
  2002. RD::get_singleton()->free(default_sampler);
  2003. RD::get_singleton()->free(default_mipmap_sampler);
  2004. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  2005. RD::get_singleton()->free(filter.coefficient_buffer);
  2006. if (prefer_raster_effects) {
  2007. blur_raster.shader.version_free(blur_raster.shader_version);
  2008. bokeh.raster_shader.version_free(blur_raster.shader_version);
  2009. luminance_reduce_raster.shader.version_free(luminance_reduce_raster.shader_version);
  2010. roughness.raster_shader.version_free(roughness.shader_version);
  2011. cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);
  2012. filter.raster_shader.version_free(filter.shader_version);
  2013. } else {
  2014. bokeh.compute_shader.version_free(bokeh.shader_version);
  2015. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  2016. roughness.compute_shader.version_free(roughness.shader_version);
  2017. cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);
  2018. filter.compute_shader.version_free(filter.shader_version);
  2019. }
  2020. if (!prefer_raster_effects) {
  2021. copy.shader.version_free(copy.shader_version);
  2022. resolve.shader.version_free(resolve.shader_version);
  2023. specular_merge.shader.version_free(specular_merge.shader_version);
  2024. ssao.blur_shader.version_free(ssao.blur_shader_version);
  2025. ssao.gather_shader.version_free(ssao.gather_shader_version);
  2026. ssao.downsample_shader.version_free(ssao.downsample_shader_version);
  2027. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  2028. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  2029. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  2030. ssr.shader.version_free(ssr.shader_version);
  2031. ssr_filter.shader.version_free(ssr_filter.shader_version);
  2032. ssr_scale.shader.version_free(ssr_scale.shader_version);
  2033. sss.shader.version_free(sss.shader_version);
  2034. RD::get_singleton()->free(ssao.mirror_sampler);
  2035. RD::get_singleton()->free(ssao.gather_constants_buffer);
  2036. RD::get_singleton()->free(ssao.importance_map_load_counter);
  2037. }
  2038. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  2039. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  2040. sort.shader.version_free(sort.shader_version);
  2041. tonemap.shader.version_free(tonemap.shader_version);
  2042. }