2
0

renderer_scene_gi_rd.cpp 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403
  1. /*************************************************************************/
  2. /* renderer_scene_gi_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "renderer_scene_gi_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
  34. #include "servers/rendering/rendering_server_default.h"
  35. const Vector3i RendererSceneGIRD::SDFGI::Cascade::DIRTY_ALL = Vector3i(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF);
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // SDFGI
  38. void RendererSceneGIRD::SDFGI::create(RendererSceneEnvironmentRD *p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size, RendererSceneGIRD *p_gi) {
  39. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  40. storage = p_gi->storage;
  41. gi = p_gi;
  42. num_cascades = p_env->sdfgi_cascades;
  43. min_cell_size = p_env->sdfgi_min_cell_size;
  44. uses_occlusion = p_env->sdfgi_use_occlusion;
  45. y_scale_mode = p_env->sdfgi_y_scale;
  46. static const float y_scale[3] = { 2.0, 1.5, 1.0 };
  47. y_mult = y_scale[y_scale_mode];
  48. cascades.resize(num_cascades);
  49. probe_axis_count = SDFGI::PROBE_DIVISOR + 1;
  50. solid_cell_ratio = gi->sdfgi_solid_cell_ratio;
  51. solid_cell_count = uint32_t(float(cascade_size * cascade_size * cascade_size) * solid_cell_ratio);
  52. float base_cell_size = min_cell_size;
  53. RD::TextureFormat tf_sdf;
  54. tf_sdf.format = RD::DATA_FORMAT_R8_UNORM;
  55. tf_sdf.width = cascade_size; // Always 64x64
  56. tf_sdf.height = cascade_size;
  57. tf_sdf.depth = cascade_size;
  58. tf_sdf.texture_type = RD::TEXTURE_TYPE_3D;
  59. tf_sdf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  60. {
  61. RD::TextureFormat tf_render = tf_sdf;
  62. tf_render.format = RD::DATA_FORMAT_R16_UINT;
  63. render_albedo = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  64. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  65. render_emission = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  66. render_emission_aniso = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  67. tf_render.format = RD::DATA_FORMAT_R8_UNORM; //at least its easy to visualize
  68. for (int i = 0; i < 8; i++) {
  69. render_occlusion[i] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  70. }
  71. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  72. render_geom_facing = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  73. tf_render.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  74. render_sdf[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  75. render_sdf[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  76. tf_render.width /= 2;
  77. tf_render.height /= 2;
  78. tf_render.depth /= 2;
  79. render_sdf_half[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  80. render_sdf_half[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  81. }
  82. RD::TextureFormat tf_occlusion = tf_sdf;
  83. tf_occlusion.format = RD::DATA_FORMAT_R16_UINT;
  84. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
  85. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
  86. tf_occlusion.depth *= cascades.size(); //use depth for occlusion slices
  87. tf_occlusion.width *= 2; //use width for the other half
  88. RD::TextureFormat tf_light = tf_sdf;
  89. tf_light.format = RD::DATA_FORMAT_R32_UINT;
  90. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  91. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  92. RD::TextureFormat tf_aniso0 = tf_sdf;
  93. tf_aniso0.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  94. RD::TextureFormat tf_aniso1 = tf_sdf;
  95. tf_aniso1.format = RD::DATA_FORMAT_R8G8_UNORM;
  96. int passes = nearest_shift(cascade_size) - 1;
  97. //store lightprobe SH
  98. RD::TextureFormat tf_probes;
  99. tf_probes.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  100. tf_probes.width = probe_axis_count * probe_axis_count;
  101. tf_probes.height = probe_axis_count * SDFGI::SH_SIZE;
  102. tf_probes.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  103. tf_probes.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  104. history_size = p_requested_history_size;
  105. RD::TextureFormat tf_probe_history = tf_probes;
  106. tf_probe_history.format = RD::DATA_FORMAT_R16G16B16A16_SINT; //signed integer because SH are signed
  107. tf_probe_history.array_layers = history_size;
  108. RD::TextureFormat tf_probe_average = tf_probes;
  109. tf_probe_average.format = RD::DATA_FORMAT_R32G32B32A32_SINT; //signed integer because SH are signed
  110. tf_probe_average.texture_type = RD::TEXTURE_TYPE_2D;
  111. lightprobe_history_scroll = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  112. lightprobe_average_scroll = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  113. {
  114. //octahedral lightprobes
  115. RD::TextureFormat tf_octprobes = tf_probes;
  116. tf_octprobes.array_layers = cascades.size() * 2;
  117. tf_octprobes.format = RD::DATA_FORMAT_R32_UINT; //pack well with RGBE
  118. tf_octprobes.width = probe_axis_count * probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  119. tf_octprobes.height = probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  120. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  121. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  122. //lightprobe texture is an octahedral texture
  123. lightprobe_data = RD::get_singleton()->texture_create(tf_octprobes, RD::TextureView());
  124. RD::TextureView tv;
  125. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  126. lightprobe_texture = RD::get_singleton()->texture_create_shared(tv, lightprobe_data);
  127. //texture handling ambient data, to integrate with volumetric foc
  128. RD::TextureFormat tf_ambient = tf_probes;
  129. tf_ambient.array_layers = cascades.size();
  130. tf_ambient.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; //pack well with RGBE
  131. tf_ambient.width = probe_axis_count * probe_axis_count;
  132. tf_ambient.height = probe_axis_count;
  133. tf_ambient.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  134. //lightprobe texture is an octahedral texture
  135. ambient_texture = RD::get_singleton()->texture_create(tf_ambient, RD::TextureView());
  136. }
  137. cascades_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES);
  138. occlusion_data = RD::get_singleton()->texture_create(tf_occlusion, RD::TextureView());
  139. {
  140. RD::TextureView tv;
  141. tv.format_override = RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16;
  142. occlusion_texture = RD::get_singleton()->texture_create_shared(tv, occlusion_data);
  143. }
  144. for (uint32_t i = 0; i < cascades.size(); i++) {
  145. SDFGI::Cascade &cascade = cascades[i];
  146. /* 3D Textures */
  147. cascade.sdf_tex = RD::get_singleton()->texture_create(tf_sdf, RD::TextureView());
  148. cascade.light_data = RD::get_singleton()->texture_create(tf_light, RD::TextureView());
  149. cascade.light_aniso_0_tex = RD::get_singleton()->texture_create(tf_aniso0, RD::TextureView());
  150. cascade.light_aniso_1_tex = RD::get_singleton()->texture_create(tf_aniso1, RD::TextureView());
  151. {
  152. RD::TextureView tv;
  153. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  154. cascade.light_tex = RD::get_singleton()->texture_create_shared(tv, cascade.light_data);
  155. RD::get_singleton()->texture_clear(cascade.light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  156. RD::get_singleton()->texture_clear(cascade.light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  157. RD::get_singleton()->texture_clear(cascade.light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  158. }
  159. cascade.cell_size = base_cell_size;
  160. Vector3 world_position = p_world_position;
  161. world_position.y *= y_mult;
  162. int32_t probe_cells = cascade_size / SDFGI::PROBE_DIVISOR;
  163. Vector3 probe_size = Vector3(1, 1, 1) * cascade.cell_size * probe_cells;
  164. Vector3i probe_pos = Vector3i((world_position / probe_size + Vector3(0.5, 0.5, 0.5)).floor());
  165. cascade.position = probe_pos * probe_cells;
  166. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  167. base_cell_size *= 2.0;
  168. /* Probe History */
  169. cascade.lightprobe_history_tex = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  170. RD::get_singleton()->texture_clear(cascade.lightprobe_history_tex, Color(0, 0, 0, 0), 0, 1, 0, tf_probe_history.array_layers); //needs to be cleared for average to work
  171. cascade.lightprobe_average_tex = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  172. RD::get_singleton()->texture_clear(cascade.lightprobe_average_tex, Color(0, 0, 0, 0), 0, 1, 0, 1); //needs to be cleared for average to work
  173. /* Buffers */
  174. cascade.solid_cell_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGI::Cascade::SolidCell) * solid_cell_count);
  175. cascade.solid_cell_dispatch_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4, Vector<uint8_t>(), RD::STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
  176. cascade.lights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGIShader::Light) * MAX(SDFGI::MAX_STATIC_LIGHTS, SDFGI::MAX_DYNAMIC_LIGHTS));
  177. {
  178. Vector<RD::Uniform> uniforms;
  179. {
  180. RD::Uniform u;
  181. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  182. u.binding = 1;
  183. u.append_id(render_sdf[(passes & 1) ? 1 : 0]); //if passes are even, we read from buffer 0, else we read from buffer 1
  184. uniforms.push_back(u);
  185. }
  186. {
  187. RD::Uniform u;
  188. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  189. u.binding = 2;
  190. u.append_id(render_albedo);
  191. uniforms.push_back(u);
  192. }
  193. {
  194. RD::Uniform u;
  195. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  196. u.binding = 3;
  197. for (int j = 0; j < 8; j++) {
  198. u.append_id(render_occlusion[j]);
  199. }
  200. uniforms.push_back(u);
  201. }
  202. {
  203. RD::Uniform u;
  204. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  205. u.binding = 4;
  206. u.append_id(render_emission);
  207. uniforms.push_back(u);
  208. }
  209. {
  210. RD::Uniform u;
  211. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  212. u.binding = 5;
  213. u.append_id(render_emission_aniso);
  214. uniforms.push_back(u);
  215. }
  216. {
  217. RD::Uniform u;
  218. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  219. u.binding = 6;
  220. u.append_id(render_geom_facing);
  221. uniforms.push_back(u);
  222. }
  223. {
  224. RD::Uniform u;
  225. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  226. u.binding = 7;
  227. u.append_id(cascade.sdf_tex);
  228. uniforms.push_back(u);
  229. }
  230. {
  231. RD::Uniform u;
  232. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  233. u.binding = 8;
  234. u.append_id(occlusion_data);
  235. uniforms.push_back(u);
  236. }
  237. {
  238. RD::Uniform u;
  239. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  240. u.binding = 10;
  241. u.append_id(cascade.solid_cell_dispatch_buffer);
  242. uniforms.push_back(u);
  243. }
  244. {
  245. RD::Uniform u;
  246. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  247. u.binding = 11;
  248. u.append_id(cascade.solid_cell_buffer);
  249. uniforms.push_back(u);
  250. }
  251. cascade.sdf_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_STORE), 0);
  252. }
  253. {
  254. Vector<RD::Uniform> uniforms;
  255. {
  256. RD::Uniform u;
  257. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  258. u.binding = 1;
  259. u.append_id(render_albedo);
  260. uniforms.push_back(u);
  261. }
  262. {
  263. RD::Uniform u;
  264. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  265. u.binding = 2;
  266. u.append_id(render_geom_facing);
  267. uniforms.push_back(u);
  268. }
  269. {
  270. RD::Uniform u;
  271. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  272. u.binding = 3;
  273. u.append_id(render_emission);
  274. uniforms.push_back(u);
  275. }
  276. {
  277. RD::Uniform u;
  278. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  279. u.binding = 4;
  280. u.append_id(render_emission_aniso);
  281. uniforms.push_back(u);
  282. }
  283. {
  284. RD::Uniform u;
  285. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  286. u.binding = 5;
  287. u.append_id(cascade.solid_cell_dispatch_buffer);
  288. uniforms.push_back(u);
  289. }
  290. {
  291. RD::Uniform u;
  292. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  293. u.binding = 6;
  294. u.append_id(cascade.solid_cell_buffer);
  295. uniforms.push_back(u);
  296. }
  297. cascade.scroll_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_SCROLL), 0);
  298. }
  299. {
  300. Vector<RD::Uniform> uniforms;
  301. {
  302. RD::Uniform u;
  303. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  304. u.binding = 1;
  305. for (int j = 0; j < 8; j++) {
  306. u.append_id(render_occlusion[j]);
  307. }
  308. uniforms.push_back(u);
  309. }
  310. {
  311. RD::Uniform u;
  312. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  313. u.binding = 2;
  314. u.append_id(occlusion_data);
  315. uniforms.push_back(u);
  316. }
  317. cascade.scroll_occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_SCROLL_OCCLUSION), 0);
  318. }
  319. }
  320. //direct light
  321. for (uint32_t i = 0; i < cascades.size(); i++) {
  322. SDFGI::Cascade &cascade = cascades[i];
  323. Vector<RD::Uniform> uniforms;
  324. {
  325. RD::Uniform u;
  326. u.binding = 1;
  327. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  328. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  329. if (j < cascades.size()) {
  330. u.append_id(cascades[j].sdf_tex);
  331. } else {
  332. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  333. }
  334. }
  335. uniforms.push_back(u);
  336. }
  337. {
  338. RD::Uniform u;
  339. u.binding = 2;
  340. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  341. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  342. uniforms.push_back(u);
  343. }
  344. {
  345. RD::Uniform u;
  346. u.binding = 3;
  347. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  348. u.append_id(cascade.solid_cell_dispatch_buffer);
  349. uniforms.push_back(u);
  350. }
  351. {
  352. RD::Uniform u;
  353. u.binding = 4;
  354. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  355. u.append_id(cascade.solid_cell_buffer);
  356. uniforms.push_back(u);
  357. }
  358. {
  359. RD::Uniform u;
  360. u.binding = 5;
  361. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  362. u.append_id(cascade.light_data);
  363. uniforms.push_back(u);
  364. }
  365. {
  366. RD::Uniform u;
  367. u.binding = 6;
  368. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  369. u.append_id(cascade.light_aniso_0_tex);
  370. uniforms.push_back(u);
  371. }
  372. {
  373. RD::Uniform u;
  374. u.binding = 7;
  375. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  376. u.append_id(cascade.light_aniso_1_tex);
  377. uniforms.push_back(u);
  378. }
  379. {
  380. RD::Uniform u;
  381. u.binding = 8;
  382. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  383. u.append_id(cascades_ubo);
  384. uniforms.push_back(u);
  385. }
  386. {
  387. RD::Uniform u;
  388. u.binding = 9;
  389. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  390. u.append_id(cascade.lights_buffer);
  391. uniforms.push_back(u);
  392. }
  393. {
  394. RD::Uniform u;
  395. u.binding = 10;
  396. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  397. u.append_id(lightprobe_texture);
  398. uniforms.push_back(u);
  399. }
  400. {
  401. RD::Uniform u;
  402. u.binding = 11;
  403. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  404. u.append_id(occlusion_texture);
  405. uniforms.push_back(u);
  406. }
  407. cascade.sdf_direct_light_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.direct_light.version_get_shader(gi->sdfgi_shader.direct_light_shader, 0), 0);
  408. }
  409. //preprocess initialize uniform set
  410. {
  411. Vector<RD::Uniform> uniforms;
  412. {
  413. RD::Uniform u;
  414. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  415. u.binding = 1;
  416. u.append_id(render_albedo);
  417. uniforms.push_back(u);
  418. }
  419. {
  420. RD::Uniform u;
  421. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  422. u.binding = 2;
  423. u.append_id(render_sdf[0]);
  424. uniforms.push_back(u);
  425. }
  426. sdf_initialize_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE), 0);
  427. }
  428. {
  429. Vector<RD::Uniform> uniforms;
  430. {
  431. RD::Uniform u;
  432. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  433. u.binding = 1;
  434. u.append_id(render_albedo);
  435. uniforms.push_back(u);
  436. }
  437. {
  438. RD::Uniform u;
  439. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  440. u.binding = 2;
  441. u.append_id(render_sdf_half[0]);
  442. uniforms.push_back(u);
  443. }
  444. sdf_initialize_half_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF), 0);
  445. }
  446. //jump flood uniform set
  447. {
  448. Vector<RD::Uniform> uniforms;
  449. {
  450. RD::Uniform u;
  451. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  452. u.binding = 1;
  453. u.append_id(render_sdf[0]);
  454. uniforms.push_back(u);
  455. }
  456. {
  457. RD::Uniform u;
  458. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  459. u.binding = 2;
  460. u.append_id(render_sdf[1]);
  461. uniforms.push_back(u);
  462. }
  463. jump_flood_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  464. RID aux0 = uniforms.write[0].get_id(0);
  465. RID aux1 = uniforms.write[1].get_id(0);
  466. uniforms.write[0].set_id(0, aux1);
  467. uniforms.write[1].set_id(0, aux0);
  468. jump_flood_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  469. }
  470. //jump flood half uniform set
  471. {
  472. Vector<RD::Uniform> uniforms;
  473. {
  474. RD::Uniform u;
  475. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  476. u.binding = 1;
  477. u.append_id(render_sdf_half[0]);
  478. uniforms.push_back(u);
  479. }
  480. {
  481. RD::Uniform u;
  482. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  483. u.binding = 2;
  484. u.append_id(render_sdf_half[1]);
  485. uniforms.push_back(u);
  486. }
  487. jump_flood_half_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  488. RID aux0 = uniforms.write[0].get_id(0);
  489. RID aux1 = uniforms.write[1].get_id(0);
  490. uniforms.write[0].set_id(0, aux1);
  491. uniforms.write[1].set_id(0, aux0);
  492. jump_flood_half_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  493. }
  494. //upscale half size sdf
  495. {
  496. Vector<RD::Uniform> uniforms;
  497. {
  498. RD::Uniform u;
  499. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  500. u.binding = 1;
  501. u.append_id(render_albedo);
  502. uniforms.push_back(u);
  503. }
  504. {
  505. RD::Uniform u;
  506. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  507. u.binding = 2;
  508. u.append_id(render_sdf_half[(passes & 1) ? 0 : 1]); //reverse pass order because half size
  509. uniforms.push_back(u);
  510. }
  511. {
  512. RD::Uniform u;
  513. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  514. u.binding = 3;
  515. u.append_id(render_sdf[(passes & 1) ? 0 : 1]); //reverse pass order because it needs an extra JFA pass
  516. uniforms.push_back(u);
  517. }
  518. upscale_jfa_uniform_set_index = (passes & 1) ? 0 : 1;
  519. sdf_upscale_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE), 0);
  520. }
  521. //occlusion uniform set
  522. {
  523. Vector<RD::Uniform> uniforms;
  524. {
  525. RD::Uniform u;
  526. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  527. u.binding = 1;
  528. u.append_id(render_albedo);
  529. uniforms.push_back(u);
  530. }
  531. {
  532. RD::Uniform u;
  533. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  534. u.binding = 2;
  535. for (int i = 0; i < 8; i++) {
  536. u.append_id(render_occlusion[i]);
  537. }
  538. uniforms.push_back(u);
  539. }
  540. {
  541. RD::Uniform u;
  542. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  543. u.binding = 3;
  544. u.append_id(render_geom_facing);
  545. uniforms.push_back(u);
  546. }
  547. occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.preprocess.version_get_shader(gi->sdfgi_shader.preprocess_shader, SDFGIShader::PRE_PROCESS_OCCLUSION), 0);
  548. }
  549. for (uint32_t i = 0; i < cascades.size(); i++) {
  550. //integrate uniform
  551. Vector<RD::Uniform> uniforms;
  552. {
  553. RD::Uniform u;
  554. u.binding = 1;
  555. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  556. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  557. if (j < cascades.size()) {
  558. u.append_id(cascades[j].sdf_tex);
  559. } else {
  560. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  561. }
  562. }
  563. uniforms.push_back(u);
  564. }
  565. {
  566. RD::Uniform u;
  567. u.binding = 2;
  568. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  569. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  570. if (j < cascades.size()) {
  571. u.append_id(cascades[j].light_tex);
  572. } else {
  573. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  574. }
  575. }
  576. uniforms.push_back(u);
  577. }
  578. {
  579. RD::Uniform u;
  580. u.binding = 3;
  581. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  582. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  583. if (j < cascades.size()) {
  584. u.append_id(cascades[j].light_aniso_0_tex);
  585. } else {
  586. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  587. }
  588. }
  589. uniforms.push_back(u);
  590. }
  591. {
  592. RD::Uniform u;
  593. u.binding = 4;
  594. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  595. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  596. if (j < cascades.size()) {
  597. u.append_id(cascades[j].light_aniso_1_tex);
  598. } else {
  599. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  600. }
  601. }
  602. uniforms.push_back(u);
  603. }
  604. {
  605. RD::Uniform u;
  606. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  607. u.binding = 6;
  608. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  609. uniforms.push_back(u);
  610. }
  611. {
  612. RD::Uniform u;
  613. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  614. u.binding = 7;
  615. u.append_id(cascades_ubo);
  616. uniforms.push_back(u);
  617. }
  618. {
  619. RD::Uniform u;
  620. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  621. u.binding = 8;
  622. u.append_id(lightprobe_data);
  623. uniforms.push_back(u);
  624. }
  625. {
  626. RD::Uniform u;
  627. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  628. u.binding = 9;
  629. u.append_id(cascades[i].lightprobe_history_tex);
  630. uniforms.push_back(u);
  631. }
  632. {
  633. RD::Uniform u;
  634. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  635. u.binding = 10;
  636. u.append_id(cascades[i].lightprobe_average_tex);
  637. uniforms.push_back(u);
  638. }
  639. {
  640. RD::Uniform u;
  641. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  642. u.binding = 11;
  643. u.append_id(lightprobe_history_scroll);
  644. uniforms.push_back(u);
  645. }
  646. {
  647. RD::Uniform u;
  648. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  649. u.binding = 12;
  650. u.append_id(lightprobe_average_scroll);
  651. uniforms.push_back(u);
  652. }
  653. {
  654. RD::Uniform u;
  655. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  656. u.binding = 13;
  657. RID parent_average;
  658. if (cascades.size() == 1) {
  659. // If there is only one SDFGI cascade, we can't use the previous cascade for blending.
  660. parent_average = cascades[i].lightprobe_average_tex;
  661. } else if (i < cascades.size() - 1) {
  662. parent_average = cascades[i + 1].lightprobe_average_tex;
  663. } else {
  664. parent_average = cascades[i - 1].lightprobe_average_tex; //to use something, but it won't be used
  665. }
  666. u.append_id(parent_average);
  667. uniforms.push_back(u);
  668. }
  669. {
  670. RD::Uniform u;
  671. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  672. u.binding = 14;
  673. u.append_id(ambient_texture);
  674. uniforms.push_back(u);
  675. }
  676. cascades[i].integrate_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.integrate.version_get_shader(gi->sdfgi_shader.integrate_shader, 0), 0);
  677. }
  678. bounce_feedback = p_env->sdfgi_bounce_feedback;
  679. energy = p_env->sdfgi_energy;
  680. normal_bias = p_env->sdfgi_normal_bias;
  681. probe_bias = p_env->sdfgi_probe_bias;
  682. reads_sky = p_env->sdfgi_read_sky_light;
  683. }
  684. void RendererSceneGIRD::SDFGI::erase() {
  685. for (uint32_t i = 0; i < cascades.size(); i++) {
  686. const SDFGI::Cascade &c = cascades[i];
  687. RD::get_singleton()->free(c.light_data);
  688. RD::get_singleton()->free(c.light_aniso_0_tex);
  689. RD::get_singleton()->free(c.light_aniso_1_tex);
  690. RD::get_singleton()->free(c.sdf_tex);
  691. RD::get_singleton()->free(c.solid_cell_dispatch_buffer);
  692. RD::get_singleton()->free(c.solid_cell_buffer);
  693. RD::get_singleton()->free(c.lightprobe_history_tex);
  694. RD::get_singleton()->free(c.lightprobe_average_tex);
  695. RD::get_singleton()->free(c.lights_buffer);
  696. }
  697. RD::get_singleton()->free(render_albedo);
  698. RD::get_singleton()->free(render_emission);
  699. RD::get_singleton()->free(render_emission_aniso);
  700. RD::get_singleton()->free(render_sdf[0]);
  701. RD::get_singleton()->free(render_sdf[1]);
  702. RD::get_singleton()->free(render_sdf_half[0]);
  703. RD::get_singleton()->free(render_sdf_half[1]);
  704. for (int i = 0; i < 8; i++) {
  705. RD::get_singleton()->free(render_occlusion[i]);
  706. }
  707. RD::get_singleton()->free(render_geom_facing);
  708. RD::get_singleton()->free(lightprobe_data);
  709. RD::get_singleton()->free(lightprobe_history_scroll);
  710. RD::get_singleton()->free(occlusion_data);
  711. RD::get_singleton()->free(ambient_texture);
  712. RD::get_singleton()->free(cascades_ubo);
  713. }
  714. void RendererSceneGIRD::SDFGI::update(RendererSceneEnvironmentRD *p_env, const Vector3 &p_world_position) {
  715. bounce_feedback = p_env->sdfgi_bounce_feedback;
  716. energy = p_env->sdfgi_energy;
  717. normal_bias = p_env->sdfgi_normal_bias;
  718. probe_bias = p_env->sdfgi_probe_bias;
  719. reads_sky = p_env->sdfgi_read_sky_light;
  720. int32_t drag_margin = (cascade_size / SDFGI::PROBE_DIVISOR) / 2;
  721. for (uint32_t i = 0; i < cascades.size(); i++) {
  722. SDFGI::Cascade &cascade = cascades[i];
  723. cascade.dirty_regions = Vector3i();
  724. Vector3 probe_half_size = Vector3(1, 1, 1) * cascade.cell_size * float(cascade_size / SDFGI::PROBE_DIVISOR) * 0.5;
  725. probe_half_size = Vector3(0, 0, 0);
  726. Vector3 world_position = p_world_position;
  727. world_position.y *= y_mult;
  728. Vector3i pos_in_cascade = Vector3i((world_position + probe_half_size) / cascade.cell_size);
  729. for (int j = 0; j < 3; j++) {
  730. if (pos_in_cascade[j] < cascade.position[j]) {
  731. while (pos_in_cascade[j] < (cascade.position[j] - drag_margin)) {
  732. cascade.position[j] -= drag_margin * 2;
  733. cascade.dirty_regions[j] += drag_margin * 2;
  734. }
  735. } else if (pos_in_cascade[j] > cascade.position[j]) {
  736. while (pos_in_cascade[j] > (cascade.position[j] + drag_margin)) {
  737. cascade.position[j] += drag_margin * 2;
  738. cascade.dirty_regions[j] -= drag_margin * 2;
  739. }
  740. }
  741. if (cascade.dirty_regions[j] == 0) {
  742. continue; // not dirty
  743. } else if (uint32_t(ABS(cascade.dirty_regions[j])) >= cascade_size) {
  744. //moved too much, just redraw everything (make all dirty)
  745. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  746. break;
  747. }
  748. }
  749. if (cascade.dirty_regions != Vector3i() && cascade.dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  750. //see how much the total dirty volume represents from the total volume
  751. uint32_t total_volume = cascade_size * cascade_size * cascade_size;
  752. uint32_t safe_volume = 1;
  753. for (int j = 0; j < 3; j++) {
  754. safe_volume *= cascade_size - ABS(cascade.dirty_regions[j]);
  755. }
  756. uint32_t dirty_volume = total_volume - safe_volume;
  757. if (dirty_volume > (safe_volume / 2)) {
  758. //more than half the volume is dirty, make all dirty so its only rendered once
  759. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  760. }
  761. }
  762. }
  763. }
  764. void RendererSceneGIRD::SDFGI::update_light() {
  765. RD::get_singleton()->draw_command_begin_label("SDFGI Update dynamic Light");
  766. /* Update dynamic light */
  767. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  768. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.direct_light_pipeline[SDFGIShader::DIRECT_LIGHT_MODE_DYNAMIC]);
  769. SDFGIShader::DirectLightPushConstant push_constant;
  770. push_constant.grid_size[0] = cascade_size;
  771. push_constant.grid_size[1] = cascade_size;
  772. push_constant.grid_size[2] = cascade_size;
  773. push_constant.max_cascades = cascades.size();
  774. push_constant.probe_axis_size = probe_axis_count;
  775. push_constant.bounce_feedback = bounce_feedback;
  776. push_constant.y_mult = y_mult;
  777. push_constant.use_occlusion = uses_occlusion;
  778. for (uint32_t i = 0; i < cascades.size(); i++) {
  779. SDFGI::Cascade &cascade = cascades[i];
  780. push_constant.light_count = cascade_dynamic_light_count[i];
  781. push_constant.cascade = i;
  782. if (cascades[i].all_dynamic_lights_dirty || gi->sdfgi_frames_to_update_light == RS::ENV_SDFGI_UPDATE_LIGHT_IN_1_FRAME) {
  783. push_constant.process_offset = 0;
  784. push_constant.process_increment = 1;
  785. } else {
  786. static uint32_t frames_to_update_table[RS::ENV_SDFGI_UPDATE_LIGHT_MAX] = {
  787. 1, 2, 4, 8, 16
  788. };
  789. uint32_t frames_to_update = frames_to_update_table[gi->sdfgi_frames_to_update_light];
  790. push_constant.process_offset = RSG::rasterizer->get_frame_number() % frames_to_update;
  791. push_constant.process_increment = frames_to_update;
  792. }
  793. cascades[i].all_dynamic_lights_dirty = false;
  794. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascade.sdf_direct_light_uniform_set, 0);
  795. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::DirectLightPushConstant));
  796. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascade.solid_cell_dispatch_buffer, 0);
  797. }
  798. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_COMPUTE);
  799. RD::get_singleton()->draw_command_end_label();
  800. }
  801. void RendererSceneGIRD::SDFGI::update_probes(RendererSceneEnvironmentRD *p_env, RendererSceneSkyRD::Sky *p_sky) {
  802. RD::get_singleton()->draw_command_begin_label("SDFGI Update Probes");
  803. SDFGIShader::IntegratePushConstant push_constant;
  804. push_constant.grid_size[1] = cascade_size;
  805. push_constant.grid_size[2] = cascade_size;
  806. push_constant.grid_size[0] = cascade_size;
  807. push_constant.max_cascades = cascades.size();
  808. push_constant.probe_axis_size = probe_axis_count;
  809. push_constant.history_index = render_pass % history_size;
  810. push_constant.history_size = history_size;
  811. static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 4, 8, 16, 32, 64, 96, 128 };
  812. push_constant.ray_count = ray_count[gi->sdfgi_ray_count];
  813. push_constant.ray_bias = probe_bias;
  814. push_constant.image_size[0] = probe_axis_count * probe_axis_count;
  815. push_constant.image_size[1] = probe_axis_count;
  816. push_constant.store_ambient_texture = p_env->volumetric_fog_enabled;
  817. RID sky_uniform_set = gi->sdfgi_shader.integrate_default_sky_uniform_set;
  818. push_constant.sky_mode = SDFGIShader::IntegratePushConstant::SKY_MODE_DISABLED;
  819. push_constant.y_mult = y_mult;
  820. if (reads_sky && p_env) {
  821. push_constant.sky_energy = p_env->bg_energy;
  822. if (p_env->background == RS::ENV_BG_CLEAR_COLOR) {
  823. push_constant.sky_mode = SDFGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  824. Color c = storage->get_default_clear_color().srgb_to_linear();
  825. push_constant.sky_color[0] = c.r;
  826. push_constant.sky_color[1] = c.g;
  827. push_constant.sky_color[2] = c.b;
  828. } else if (p_env->background == RS::ENV_BG_COLOR) {
  829. push_constant.sky_mode = SDFGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  830. Color c = p_env->bg_color;
  831. push_constant.sky_color[0] = c.r;
  832. push_constant.sky_color[1] = c.g;
  833. push_constant.sky_color[2] = c.b;
  834. } else if (p_env->background == RS::ENV_BG_SKY) {
  835. if (p_sky && p_sky->radiance.is_valid()) {
  836. if (integrate_sky_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(integrate_sky_uniform_set)) {
  837. Vector<RD::Uniform> uniforms;
  838. {
  839. RD::Uniform u;
  840. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  841. u.binding = 0;
  842. u.append_id(p_sky->radiance);
  843. uniforms.push_back(u);
  844. }
  845. {
  846. RD::Uniform u;
  847. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  848. u.binding = 1;
  849. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  850. uniforms.push_back(u);
  851. }
  852. integrate_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.integrate.version_get_shader(gi->sdfgi_shader.integrate_shader, 0), 1);
  853. }
  854. sky_uniform_set = integrate_sky_uniform_set;
  855. push_constant.sky_mode = SDFGIShader::IntegratePushConstant::SKY_MODE_SKY;
  856. }
  857. }
  858. }
  859. render_pass++;
  860. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(true);
  861. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_PROCESS]);
  862. int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
  863. for (uint32_t i = 0; i < cascades.size(); i++) {
  864. push_constant.cascade = i;
  865. push_constant.world_offset[0] = cascades[i].position.x / probe_divisor;
  866. push_constant.world_offset[1] = cascades[i].position.y / probe_divisor;
  867. push_constant.world_offset[2] = cascades[i].position.z / probe_divisor;
  868. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
  869. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sky_uniform_set, 1);
  870. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
  871. RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
  872. }
  873. //end later after raster to avoid barriering on layout changes
  874. //RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER);
  875. RD::get_singleton()->draw_command_end_label();
  876. }
  877. void RendererSceneGIRD::SDFGI::store_probes() {
  878. RD::get_singleton()->barrier(RD::BARRIER_MASK_COMPUTE, RD::BARRIER_MASK_COMPUTE);
  879. RD::get_singleton()->draw_command_begin_label("SDFGI Store Probes");
  880. SDFGIShader::IntegratePushConstant push_constant;
  881. push_constant.grid_size[1] = cascade_size;
  882. push_constant.grid_size[2] = cascade_size;
  883. push_constant.grid_size[0] = cascade_size;
  884. push_constant.max_cascades = cascades.size();
  885. push_constant.probe_axis_size = probe_axis_count;
  886. push_constant.history_index = render_pass % history_size;
  887. push_constant.history_size = history_size;
  888. static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 4, 8, 16, 32, 64, 96, 128 };
  889. push_constant.ray_count = ray_count[gi->sdfgi_ray_count];
  890. push_constant.ray_bias = probe_bias;
  891. push_constant.image_size[0] = probe_axis_count * probe_axis_count;
  892. push_constant.image_size[1] = probe_axis_count;
  893. push_constant.store_ambient_texture = false;
  894. push_constant.sky_mode = 0;
  895. push_constant.y_mult = y_mult;
  896. // Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
  897. RENDER_TIMESTAMP("Average SDFGI Probes");
  898. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  899. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE]);
  900. //convert to octahedral to store
  901. push_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  902. push_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  903. for (uint32_t i = 0; i < cascades.size(); i++) {
  904. push_constant.cascade = i;
  905. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[i].integrate_uniform_set, 0);
  906. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
  907. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::IntegratePushConstant));
  908. RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, 1);
  909. }
  910. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_COMPUTE);
  911. RD::get_singleton()->draw_command_end_label();
  912. }
  913. int RendererSceneGIRD::SDFGI::get_pending_region_data(int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const {
  914. int dirty_count = 0;
  915. for (uint32_t i = 0; i < cascades.size(); i++) {
  916. const SDFGI::Cascade &c = cascades[i];
  917. if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
  918. if (dirty_count == p_region) {
  919. r_local_offset = Vector3i();
  920. r_local_size = Vector3i(1, 1, 1) * cascade_size;
  921. r_bounds.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + c.position)) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
  922. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
  923. return i;
  924. }
  925. dirty_count++;
  926. } else {
  927. for (int j = 0; j < 3; j++) {
  928. if (c.dirty_regions[j] != 0) {
  929. if (dirty_count == p_region) {
  930. Vector3i from = Vector3i(0, 0, 0);
  931. Vector3i to = Vector3i(1, 1, 1) * cascade_size;
  932. if (c.dirty_regions[j] > 0) {
  933. //fill from the beginning
  934. to[j] = c.dirty_regions[j];
  935. } else {
  936. //fill from the end
  937. from[j] = to[j] + c.dirty_regions[j];
  938. }
  939. for (int k = 0; k < j; k++) {
  940. // "chip" away previous regions to avoid re-voxelizing the same thing
  941. if (c.dirty_regions[k] > 0) {
  942. from[k] += c.dirty_regions[k];
  943. } else if (c.dirty_regions[k] < 0) {
  944. to[k] += c.dirty_regions[k];
  945. }
  946. }
  947. r_local_offset = from;
  948. r_local_size = to - from;
  949. r_bounds.position = Vector3(from + Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + c.position) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
  950. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / y_mult, 1);
  951. return i;
  952. }
  953. dirty_count++;
  954. }
  955. }
  956. }
  957. }
  958. return -1;
  959. }
  960. void RendererSceneGIRD::SDFGI::update_cascades() {
  961. //update cascades
  962. SDFGI::Cascade::UBO cascade_data[SDFGI::MAX_CASCADES];
  963. int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
  964. for (uint32_t i = 0; i < cascades.size(); i++) {
  965. Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
  966. cascade_data[i].offset[0] = pos.x;
  967. cascade_data[i].offset[1] = pos.y;
  968. cascade_data[i].offset[2] = pos.z;
  969. cascade_data[i].to_cell = 1.0 / cascades[i].cell_size;
  970. cascade_data[i].probe_offset[0] = cascades[i].position.x / probe_divisor;
  971. cascade_data[i].probe_offset[1] = cascades[i].position.y / probe_divisor;
  972. cascade_data[i].probe_offset[2] = cascades[i].position.z / probe_divisor;
  973. cascade_data[i].pad = 0;
  974. }
  975. RD::get_singleton()->buffer_update(cascades_ubo, 0, sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES, cascade_data, RD::BARRIER_MASK_COMPUTE);
  976. }
  977. void RendererSceneGIRD::SDFGI::debug_draw(const CameraMatrix &p_projection, const Transform3D &p_transform, int p_width, int p_height, RID p_render_target, RID p_texture) {
  978. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  979. if (!debug_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_uniform_set)) {
  980. Vector<RD::Uniform> uniforms;
  981. {
  982. RD::Uniform u;
  983. u.binding = 1;
  984. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  985. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  986. if (i < cascades.size()) {
  987. u.append_id(cascades[i].sdf_tex);
  988. } else {
  989. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  990. }
  991. }
  992. uniforms.push_back(u);
  993. }
  994. {
  995. RD::Uniform u;
  996. u.binding = 2;
  997. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  998. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  999. if (i < cascades.size()) {
  1000. u.append_id(cascades[i].light_tex);
  1001. } else {
  1002. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1003. }
  1004. }
  1005. uniforms.push_back(u);
  1006. }
  1007. {
  1008. RD::Uniform u;
  1009. u.binding = 3;
  1010. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1011. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  1012. if (i < cascades.size()) {
  1013. u.append_id(cascades[i].light_aniso_0_tex);
  1014. } else {
  1015. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1016. }
  1017. }
  1018. uniforms.push_back(u);
  1019. }
  1020. {
  1021. RD::Uniform u;
  1022. u.binding = 4;
  1023. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1024. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  1025. if (i < cascades.size()) {
  1026. u.append_id(cascades[i].light_aniso_1_tex);
  1027. } else {
  1028. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1029. }
  1030. }
  1031. uniforms.push_back(u);
  1032. }
  1033. {
  1034. RD::Uniform u;
  1035. u.binding = 5;
  1036. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1037. u.append_id(occlusion_texture);
  1038. uniforms.push_back(u);
  1039. }
  1040. {
  1041. RD::Uniform u;
  1042. u.binding = 8;
  1043. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1044. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1045. uniforms.push_back(u);
  1046. }
  1047. {
  1048. RD::Uniform u;
  1049. u.binding = 9;
  1050. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1051. u.append_id(cascades_ubo);
  1052. uniforms.push_back(u);
  1053. }
  1054. {
  1055. RD::Uniform u;
  1056. u.binding = 10;
  1057. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1058. u.append_id(p_texture);
  1059. uniforms.push_back(u);
  1060. }
  1061. {
  1062. RD::Uniform u;
  1063. u.binding = 11;
  1064. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1065. u.append_id(lightprobe_texture);
  1066. uniforms.push_back(u);
  1067. }
  1068. debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.debug_shader_version, 0);
  1069. }
  1070. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1071. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.debug_pipeline);
  1072. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set, 0);
  1073. SDFGIShader::DebugPushConstant push_constant;
  1074. push_constant.grid_size[0] = cascade_size;
  1075. push_constant.grid_size[1] = cascade_size;
  1076. push_constant.grid_size[2] = cascade_size;
  1077. push_constant.max_cascades = cascades.size();
  1078. push_constant.screen_size[0] = p_width;
  1079. push_constant.screen_size[1] = p_height;
  1080. push_constant.probe_axis_size = probe_axis_count;
  1081. push_constant.use_occlusion = uses_occlusion;
  1082. push_constant.y_mult = y_mult;
  1083. Vector2 vp_half = p_projection.get_viewport_half_extents();
  1084. push_constant.cam_extent[0] = vp_half.x;
  1085. push_constant.cam_extent[1] = vp_half.y;
  1086. push_constant.cam_extent[2] = -p_projection.get_z_near();
  1087. push_constant.cam_transform[0] = p_transform.basis.elements[0][0];
  1088. push_constant.cam_transform[1] = p_transform.basis.elements[1][0];
  1089. push_constant.cam_transform[2] = p_transform.basis.elements[2][0];
  1090. push_constant.cam_transform[3] = 0;
  1091. push_constant.cam_transform[4] = p_transform.basis.elements[0][1];
  1092. push_constant.cam_transform[5] = p_transform.basis.elements[1][1];
  1093. push_constant.cam_transform[6] = p_transform.basis.elements[2][1];
  1094. push_constant.cam_transform[7] = 0;
  1095. push_constant.cam_transform[8] = p_transform.basis.elements[0][2];
  1096. push_constant.cam_transform[9] = p_transform.basis.elements[1][2];
  1097. push_constant.cam_transform[10] = p_transform.basis.elements[2][2];
  1098. push_constant.cam_transform[11] = 0;
  1099. push_constant.cam_transform[12] = p_transform.origin.x;
  1100. push_constant.cam_transform[13] = p_transform.origin.y;
  1101. push_constant.cam_transform[14] = p_transform.origin.z;
  1102. push_constant.cam_transform[15] = 1;
  1103. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::DebugPushConstant));
  1104. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_width, p_height, 1);
  1105. RD::get_singleton()->compute_list_end();
  1106. Size2 rtsize = texture_storage->render_target_get_size(p_render_target);
  1107. storage->get_effects()->copy_to_fb_rect(p_texture, texture_storage->render_target_get_rd_framebuffer(p_render_target), Rect2(Vector2(), rtsize), true);
  1108. }
  1109. void RendererSceneGIRD::SDFGI::debug_probes(RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform) {
  1110. SDFGIShader::DebugProbesPushConstant push_constant;
  1111. for (int i = 0; i < 4; i++) {
  1112. for (int j = 0; j < 4; j++) {
  1113. push_constant.projection[i * 4 + j] = p_camera_with_transform.matrix[i][j];
  1114. }
  1115. }
  1116. //gen spheres from strips
  1117. uint32_t band_points = 16;
  1118. push_constant.band_power = 4;
  1119. push_constant.sections_in_band = ((band_points / 2) - 1);
  1120. push_constant.band_mask = band_points - 2;
  1121. push_constant.section_arc = Math_TAU / float(push_constant.sections_in_band);
  1122. push_constant.y_mult = y_mult;
  1123. uint32_t total_points = push_constant.sections_in_band * band_points;
  1124. uint32_t total_probes = probe_axis_count * probe_axis_count * probe_axis_count;
  1125. push_constant.grid_size[0] = cascade_size;
  1126. push_constant.grid_size[1] = cascade_size;
  1127. push_constant.grid_size[2] = cascade_size;
  1128. push_constant.cascade = 0;
  1129. push_constant.probe_axis_size = probe_axis_count;
  1130. if (!debug_probes_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(debug_probes_uniform_set)) {
  1131. Vector<RD::Uniform> uniforms;
  1132. {
  1133. RD::Uniform u;
  1134. u.binding = 1;
  1135. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1136. u.append_id(cascades_ubo);
  1137. uniforms.push_back(u);
  1138. }
  1139. {
  1140. RD::Uniform u;
  1141. u.binding = 2;
  1142. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1143. u.append_id(lightprobe_texture);
  1144. uniforms.push_back(u);
  1145. }
  1146. {
  1147. RD::Uniform u;
  1148. u.binding = 3;
  1149. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1150. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1151. uniforms.push_back(u);
  1152. }
  1153. {
  1154. RD::Uniform u;
  1155. u.binding = 4;
  1156. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1157. u.append_id(occlusion_texture);
  1158. uniforms.push_back(u);
  1159. }
  1160. debug_probes_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->sdfgi_shader.debug_probes.version_get_shader(gi->sdfgi_shader.debug_probes_shader, 0), 0);
  1161. }
  1162. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, gi->sdfgi_shader.debug_probes_pipeline[SDFGIShader::PROBE_DEBUG_PROBES].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  1163. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, debug_probes_uniform_set, 0);
  1164. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
  1165. RD::get_singleton()->draw_list_draw(p_draw_list, false, total_probes, total_points);
  1166. if (gi->sdfgi_debug_probe_dir != Vector3()) {
  1167. uint32_t cascade = 0;
  1168. Vector3 offset = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[cascade].position)) * cascades[cascade].cell_size * Vector3(1.0, 1.0 / y_mult, 1.0);
  1169. Vector3 probe_size = cascades[cascade].cell_size * (cascade_size / SDFGI::PROBE_DIVISOR) * Vector3(1.0, 1.0 / y_mult, 1.0);
  1170. Vector3 ray_from = gi->sdfgi_debug_probe_pos;
  1171. Vector3 ray_to = gi->sdfgi_debug_probe_pos + gi->sdfgi_debug_probe_dir * cascades[cascade].cell_size * Math::sqrt(3.0) * cascade_size;
  1172. float sphere_radius = 0.2;
  1173. float closest_dist = 1e20;
  1174. gi->sdfgi_debug_probe_enabled = false;
  1175. Vector3i probe_from = cascades[cascade].position / (cascade_size / SDFGI::PROBE_DIVISOR);
  1176. for (int i = 0; i < (SDFGI::PROBE_DIVISOR + 1); i++) {
  1177. for (int j = 0; j < (SDFGI::PROBE_DIVISOR + 1); j++) {
  1178. for (int k = 0; k < (SDFGI::PROBE_DIVISOR + 1); k++) {
  1179. Vector3 pos = offset + probe_size * Vector3(i, j, k);
  1180. Vector3 res;
  1181. if (Geometry3D::segment_intersects_sphere(ray_from, ray_to, pos, sphere_radius, &res)) {
  1182. float d = ray_from.distance_to(res);
  1183. if (d < closest_dist) {
  1184. closest_dist = d;
  1185. gi->sdfgi_debug_probe_enabled = true;
  1186. gi->sdfgi_debug_probe_index = probe_from + Vector3i(i, j, k);
  1187. }
  1188. }
  1189. }
  1190. }
  1191. }
  1192. gi->sdfgi_debug_probe_dir = Vector3();
  1193. }
  1194. if (gi->sdfgi_debug_probe_enabled) {
  1195. uint32_t cascade = 0;
  1196. uint32_t probe_cells = (cascade_size / SDFGI::PROBE_DIVISOR);
  1197. Vector3i probe_from = cascades[cascade].position / probe_cells;
  1198. Vector3i ofs = gi->sdfgi_debug_probe_index - probe_from;
  1199. if (ofs.x < 0 || ofs.y < 0 || ofs.z < 0) {
  1200. return;
  1201. }
  1202. if (ofs.x > SDFGI::PROBE_DIVISOR || ofs.y > SDFGI::PROBE_DIVISOR || ofs.z > SDFGI::PROBE_DIVISOR) {
  1203. return;
  1204. }
  1205. uint32_t mult = (SDFGI::PROBE_DIVISOR + 1);
  1206. uint32_t index = ofs.z * mult * mult + ofs.y * mult + ofs.x;
  1207. push_constant.probe_debug_index = index;
  1208. uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2;
  1209. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, gi->sdfgi_shader.debug_probes_pipeline[SDFGIShader::PROBE_DEBUG_VISIBILITY].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  1210. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, debug_probes_uniform_set, 0);
  1211. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDFGIShader::DebugProbesPushConstant));
  1212. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, total_points);
  1213. }
  1214. }
  1215. void RendererSceneGIRD::SDFGI::pre_process_gi(const Transform3D &p_transform, RenderDataRD *p_render_data, RendererSceneRenderRD *p_scene_render) {
  1216. /* Update general SDFGI Buffer */
  1217. SDFGIData sdfgi_data;
  1218. sdfgi_data.grid_size[0] = cascade_size;
  1219. sdfgi_data.grid_size[1] = cascade_size;
  1220. sdfgi_data.grid_size[2] = cascade_size;
  1221. sdfgi_data.max_cascades = cascades.size();
  1222. sdfgi_data.probe_axis_size = probe_axis_count;
  1223. sdfgi_data.cascade_probe_size[0] = sdfgi_data.probe_axis_size - 1; //float version for performance
  1224. sdfgi_data.cascade_probe_size[1] = sdfgi_data.probe_axis_size - 1;
  1225. sdfgi_data.cascade_probe_size[2] = sdfgi_data.probe_axis_size - 1;
  1226. float csize = cascade_size;
  1227. sdfgi_data.probe_to_uvw = 1.0 / float(sdfgi_data.cascade_probe_size[0]);
  1228. sdfgi_data.use_occlusion = uses_occlusion;
  1229. //sdfgi_data.energy = energy;
  1230. sdfgi_data.y_mult = y_mult;
  1231. float cascade_voxel_size = (csize / sdfgi_data.cascade_probe_size[0]);
  1232. float occlusion_clamp = (cascade_voxel_size - 0.5) / cascade_voxel_size;
  1233. sdfgi_data.occlusion_clamp[0] = occlusion_clamp;
  1234. sdfgi_data.occlusion_clamp[1] = occlusion_clamp;
  1235. sdfgi_data.occlusion_clamp[2] = occlusion_clamp;
  1236. sdfgi_data.normal_bias = (normal_bias / csize) * sdfgi_data.cascade_probe_size[0];
  1237. //vec2 tex_pixel_size = 1.0 / vec2(ivec2( (OCT_SIZE+2) * params.probe_axis_size * params.probe_axis_size, (OCT_SIZE+2) * params.probe_axis_size ) );
  1238. //vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
  1239. uint32_t oct_size = SDFGI::LIGHTPROBE_OCT_SIZE;
  1240. sdfgi_data.lightprobe_tex_pixel_size[0] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size * sdfgi_data.probe_axis_size);
  1241. sdfgi_data.lightprobe_tex_pixel_size[1] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size);
  1242. sdfgi_data.lightprobe_tex_pixel_size[2] = 1.0;
  1243. sdfgi_data.energy = energy;
  1244. sdfgi_data.lightprobe_uv_offset[0] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1245. sdfgi_data.lightprobe_uv_offset[1] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[1];
  1246. sdfgi_data.lightprobe_uv_offset[2] = float((oct_size + 2) * sdfgi_data.probe_axis_size) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1247. sdfgi_data.occlusion_renormalize[0] = 0.5;
  1248. sdfgi_data.occlusion_renormalize[1] = 1.0;
  1249. sdfgi_data.occlusion_renormalize[2] = 1.0 / float(sdfgi_data.max_cascades);
  1250. int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
  1251. for (uint32_t i = 0; i < sdfgi_data.max_cascades; i++) {
  1252. SDFGIData::ProbeCascadeData &c = sdfgi_data.cascades[i];
  1253. Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascades[i].position)) * cascades[i].cell_size;
  1254. Vector3 cam_origin = p_transform.origin;
  1255. cam_origin.y *= y_mult;
  1256. pos -= cam_origin; //make pos local to camera, to reduce numerical error
  1257. c.position[0] = pos.x;
  1258. c.position[1] = pos.y;
  1259. c.position[2] = pos.z;
  1260. c.to_probe = 1.0 / (float(cascade_size) * cascades[i].cell_size / float(probe_axis_count - 1));
  1261. Vector3i probe_ofs = cascades[i].position / probe_divisor;
  1262. c.probe_world_offset[0] = probe_ofs.x;
  1263. c.probe_world_offset[1] = probe_ofs.y;
  1264. c.probe_world_offset[2] = probe_ofs.z;
  1265. c.to_cell = 1.0 / cascades[i].cell_size;
  1266. }
  1267. RD::get_singleton()->buffer_update(gi->sdfgi_ubo, 0, sizeof(SDFGIData), &sdfgi_data, RD::BARRIER_MASK_COMPUTE);
  1268. /* Update dynamic lights in SDFGI cascades */
  1269. for (uint32_t i = 0; i < cascades.size(); i++) {
  1270. SDFGI::Cascade &cascade = cascades[i];
  1271. SDFGIShader::Light lights[SDFGI::MAX_DYNAMIC_LIGHTS];
  1272. uint32_t idx = 0;
  1273. for (uint32_t j = 0; j < (uint32_t)p_scene_render->render_state.sdfgi_update_data->directional_lights->size(); j++) {
  1274. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1275. break;
  1276. }
  1277. RendererSceneRenderRD::LightInstance *li = p_scene_render->light_instance_owner.get_or_null(p_scene_render->render_state.sdfgi_update_data->directional_lights->get(j));
  1278. ERR_CONTINUE(!li);
  1279. if (RSG::light_storage->light_directional_get_sky_mode(li->light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
  1280. continue;
  1281. }
  1282. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1283. dir.y *= y_mult;
  1284. dir.normalize();
  1285. lights[idx].direction[0] = dir.x;
  1286. lights[idx].direction[1] = dir.y;
  1287. lights[idx].direction[2] = dir.z;
  1288. Color color = RSG::light_storage->light_get_color(li->light);
  1289. color = color.srgb_to_linear();
  1290. lights[idx].color[0] = color.r;
  1291. lights[idx].color[1] = color.g;
  1292. lights[idx].color[2] = color.b;
  1293. lights[idx].type = RS::LIGHT_DIRECTIONAL;
  1294. lights[idx].energy = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  1295. lights[idx].has_shadow = RSG::light_storage->light_has_shadow(li->light);
  1296. idx++;
  1297. }
  1298. AABB cascade_aabb;
  1299. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cascade.position)) * cascade.cell_size;
  1300. cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cascade.cell_size;
  1301. for (uint32_t j = 0; j < p_scene_render->render_state.sdfgi_update_data->positional_light_count; j++) {
  1302. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1303. break;
  1304. }
  1305. RendererSceneRenderRD::LightInstance *li = p_scene_render->light_instance_owner.get_or_null(p_scene_render->render_state.sdfgi_update_data->positional_light_instances[j]);
  1306. ERR_CONTINUE(!li);
  1307. uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(li->light);
  1308. if (i > max_sdfgi_cascade) {
  1309. continue;
  1310. }
  1311. if (!cascade_aabb.intersects(li->aabb)) {
  1312. continue;
  1313. }
  1314. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1315. //faster to not do this here
  1316. //dir.y *= y_mult;
  1317. //dir.normalize();
  1318. lights[idx].direction[0] = dir.x;
  1319. lights[idx].direction[1] = dir.y;
  1320. lights[idx].direction[2] = dir.z;
  1321. Vector3 pos = li->transform.origin;
  1322. pos.y *= y_mult;
  1323. lights[idx].position[0] = pos.x;
  1324. lights[idx].position[1] = pos.y;
  1325. lights[idx].position[2] = pos.z;
  1326. Color color = RSG::light_storage->light_get_color(li->light);
  1327. color = color.srgb_to_linear();
  1328. lights[idx].color[0] = color.r;
  1329. lights[idx].color[1] = color.g;
  1330. lights[idx].color[2] = color.b;
  1331. lights[idx].type = RSG::light_storage->light_get_type(li->light);
  1332. lights[idx].energy = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  1333. lights[idx].has_shadow = RSG::light_storage->light_has_shadow(li->light);
  1334. lights[idx].attenuation = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  1335. lights[idx].radius = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  1336. lights[idx].cos_spot_angle = Math::cos(Math::deg2rad(RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE)));
  1337. lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1338. idx++;
  1339. }
  1340. if (idx > 0) {
  1341. RD::get_singleton()->buffer_update(cascade.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights, RD::BARRIER_MASK_COMPUTE);
  1342. }
  1343. cascade_dynamic_light_count[i] = idx;
  1344. }
  1345. }
  1346. void RendererSceneGIRD::SDFGI::render_region(RID p_render_buffers, int p_region, const PagedArray<RendererSceneRender::GeometryInstance *> &p_instances, RendererSceneRenderRD *p_scene_render) {
  1347. //print_line("rendering region " + itos(p_region));
  1348. RendererSceneRenderRD::RenderBuffers *rb = p_scene_render->render_buffers_owner.get_or_null(p_render_buffers);
  1349. ERR_FAIL_COND(!rb); // we wouldn't be here if this failed but...
  1350. AABB bounds;
  1351. Vector3i from;
  1352. Vector3i size;
  1353. int cascade_prev = get_pending_region_data(p_region - 1, from, size, bounds);
  1354. int cascade_next = get_pending_region_data(p_region + 1, from, size, bounds);
  1355. int cascade = get_pending_region_data(p_region, from, size, bounds);
  1356. ERR_FAIL_COND(cascade < 0);
  1357. if (cascade_prev != cascade) {
  1358. //initialize render
  1359. RD::get_singleton()->texture_clear(render_albedo, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1360. RD::get_singleton()->texture_clear(render_emission, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1361. RD::get_singleton()->texture_clear(render_emission_aniso, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1362. RD::get_singleton()->texture_clear(render_geom_facing, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1363. }
  1364. //print_line("rendering cascade " + itos(p_region) + " objects: " + itos(p_cull_count) + " bounds: " + bounds + " from: " + from + " size: " + size + " cell size: " + rtos(cascades[cascade].cell_size));
  1365. p_scene_render->_render_sdfgi(p_render_buffers, from, size, bounds, p_instances, render_albedo, render_emission, render_emission_aniso, render_geom_facing);
  1366. if (cascade_next != cascade) {
  1367. RD::get_singleton()->draw_command_begin_label("SDFGI Pre-Process Cascade");
  1368. RENDER_TIMESTAMP("> SDFGI Update SDF");
  1369. //done rendering! must update SDF
  1370. //clear dispatch indirect data
  1371. SDFGIShader::PreprocessPushConstant push_constant;
  1372. memset(&push_constant, 0, sizeof(SDFGIShader::PreprocessPushConstant));
  1373. RENDER_TIMESTAMP("SDFGI Scroll SDF");
  1374. //scroll
  1375. if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  1376. //for scroll
  1377. Vector3i dirty = cascades[cascade].dirty_regions;
  1378. push_constant.scroll[0] = dirty.x;
  1379. push_constant.scroll[1] = dirty.y;
  1380. push_constant.scroll[2] = dirty.z;
  1381. } else {
  1382. //for no scroll
  1383. push_constant.scroll[0] = 0;
  1384. push_constant.scroll[1] = 0;
  1385. push_constant.scroll[2] = 0;
  1386. }
  1387. cascades[cascade].all_dynamic_lights_dirty = true;
  1388. push_constant.grid_size = cascade_size;
  1389. push_constant.cascade = cascade;
  1390. if (cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  1391. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1392. //must pre scroll existing data because not all is dirty
  1393. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL]);
  1394. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_uniform_set, 0);
  1395. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1396. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascades[cascade].solid_cell_dispatch_buffer, 0);
  1397. // no barrier do all together
  1398. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_SCROLL_OCCLUSION]);
  1399. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].scroll_occlusion_uniform_set, 0);
  1400. Vector3i dirty = cascades[cascade].dirty_regions;
  1401. Vector3i groups;
  1402. groups.x = cascade_size - ABS(dirty.x);
  1403. groups.y = cascade_size - ABS(dirty.y);
  1404. groups.z = cascade_size - ABS(dirty.z);
  1405. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1406. RD::get_singleton()->compute_list_dispatch_threads(compute_list, groups.x, groups.y, groups.z);
  1407. //no barrier, continue together
  1408. {
  1409. //scroll probes and their history also
  1410. SDFGIShader::IntegratePushConstant ipush_constant;
  1411. ipush_constant.grid_size[1] = cascade_size;
  1412. ipush_constant.grid_size[2] = cascade_size;
  1413. ipush_constant.grid_size[0] = cascade_size;
  1414. ipush_constant.max_cascades = cascades.size();
  1415. ipush_constant.probe_axis_size = probe_axis_count;
  1416. ipush_constant.history_index = 0;
  1417. ipush_constant.history_size = history_size;
  1418. ipush_constant.ray_count = 0;
  1419. ipush_constant.ray_bias = 0;
  1420. ipush_constant.sky_mode = 0;
  1421. ipush_constant.sky_energy = 0;
  1422. ipush_constant.sky_color[0] = 0;
  1423. ipush_constant.sky_color[1] = 0;
  1424. ipush_constant.sky_color[2] = 0;
  1425. ipush_constant.y_mult = y_mult;
  1426. ipush_constant.store_ambient_texture = false;
  1427. ipush_constant.image_size[0] = probe_axis_count * probe_axis_count;
  1428. ipush_constant.image_size[1] = probe_axis_count;
  1429. int32_t probe_divisor = cascade_size / SDFGI::PROBE_DIVISOR;
  1430. ipush_constant.cascade = cascade;
  1431. ipush_constant.world_offset[0] = cascades[cascade].position.x / probe_divisor;
  1432. ipush_constant.world_offset[1] = cascades[cascade].position.y / probe_divisor;
  1433. ipush_constant.world_offset[2] = cascades[cascade].position.z / probe_divisor;
  1434. ipush_constant.scroll[0] = dirty.x / probe_divisor;
  1435. ipush_constant.scroll[1] = dirty.y / probe_divisor;
  1436. ipush_constant.scroll[2] = dirty.z / probe_divisor;
  1437. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL]);
  1438. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
  1439. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
  1440. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
  1441. RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
  1442. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1443. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_SCROLL_STORE]);
  1444. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
  1445. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
  1446. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
  1447. RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count, probe_axis_count, 1);
  1448. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1449. if (bounce_feedback > 0.0) {
  1450. //multibounce requires this to be stored so direct light can read from it
  1451. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.integrate_pipeline[SDFGIShader::INTEGRATE_MODE_STORE]);
  1452. //convert to octahedral to store
  1453. ipush_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1454. ipush_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1455. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].integrate_uniform_set, 0);
  1456. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi->sdfgi_shader.integrate_default_sky_uniform_set, 1);
  1457. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDFGIShader::IntegratePushConstant));
  1458. RD::get_singleton()->compute_list_dispatch_threads(compute_list, probe_axis_count * probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, 1);
  1459. }
  1460. }
  1461. //ok finally barrier
  1462. RD::get_singleton()->compute_list_end();
  1463. }
  1464. //clear dispatch indirect data
  1465. uint32_t dispatch_indirct_data[4] = { 0, 0, 0, 0 };
  1466. RD::get_singleton()->buffer_update(cascades[cascade].solid_cell_dispatch_buffer, 0, sizeof(uint32_t) * 4, dispatch_indirct_data);
  1467. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1468. bool half_size = true; //much faster, very little difference
  1469. static const int optimized_jf_group_size = 8;
  1470. if (half_size) {
  1471. push_constant.grid_size >>= 1;
  1472. uint32_t cascade_half_size = cascade_size >> 1;
  1473. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF]);
  1474. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_half_uniform_set, 0);
  1475. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1476. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
  1477. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1478. //must start with regular jumpflood
  1479. push_constant.half_size = true;
  1480. {
  1481. RENDER_TIMESTAMP("SDFGI Jump Flood (Half-Size)");
  1482. uint32_t s = cascade_half_size;
  1483. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD]);
  1484. int jf_us = 0;
  1485. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  1486. while (s > 1) {
  1487. s /= 2;
  1488. push_constant.step_size = s;
  1489. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
  1490. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1491. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
  1492. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1493. jf_us = jf_us == 0 ? 1 : 0;
  1494. if (cascade_half_size / (s / 2) >= optimized_jf_group_size) {
  1495. break;
  1496. }
  1497. }
  1498. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Half-Size)");
  1499. //continue with optimized jump flood for smaller reads
  1500. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  1501. while (s > 1) {
  1502. s /= 2;
  1503. push_constant.step_size = s;
  1504. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_half_uniform_set[jf_us], 0);
  1505. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1506. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size);
  1507. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1508. jf_us = jf_us == 0 ? 1 : 0;
  1509. }
  1510. }
  1511. // restore grid size for last passes
  1512. push_constant.grid_size = cascade_size;
  1513. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE]);
  1514. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_upscale_uniform_set, 0);
  1515. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1516. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1517. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1518. //run one pass of fullsize jumpflood to fix up half size arctifacts
  1519. push_constant.half_size = false;
  1520. push_constant.step_size = 1;
  1521. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  1522. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[upscale_jfa_uniform_set_index], 0);
  1523. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1524. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1525. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1526. } else {
  1527. //full size jumpflood
  1528. RENDER_TIMESTAMP("SDFGI Jump Flood (Full-Size)");
  1529. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE]);
  1530. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdf_initialize_uniform_set, 0);
  1531. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1532. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1533. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1534. push_constant.half_size = false;
  1535. {
  1536. uint32_t s = cascade_size;
  1537. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD]);
  1538. int jf_us = 0;
  1539. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  1540. while (s > 1) {
  1541. s /= 2;
  1542. push_constant.step_size = s;
  1543. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
  1544. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1545. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1546. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1547. jf_us = jf_us == 0 ? 1 : 0;
  1548. if (cascade_size / (s / 2) >= optimized_jf_group_size) {
  1549. break;
  1550. }
  1551. }
  1552. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Full-Size)");
  1553. //continue with optimized jump flood for smaller reads
  1554. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  1555. while (s > 1) {
  1556. s /= 2;
  1557. push_constant.step_size = s;
  1558. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, jump_flood_uniform_set[jf_us], 0);
  1559. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1560. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1561. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1562. jf_us = jf_us == 0 ? 1 : 0;
  1563. }
  1564. }
  1565. }
  1566. RENDER_TIMESTAMP("SDFGI Occlusion");
  1567. // occlusion
  1568. {
  1569. uint32_t probe_size = cascade_size / SDFGI::PROBE_DIVISOR;
  1570. Vector3i probe_global_pos = cascades[cascade].position / probe_size;
  1571. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_OCCLUSION]);
  1572. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, occlusion_uniform_set, 0);
  1573. for (int i = 0; i < 8; i++) {
  1574. //dispatch all at once for performance
  1575. Vector3i offset(i & 1, (i >> 1) & 1, (i >> 2) & 1);
  1576. if ((probe_global_pos.x & 1) != 0) {
  1577. offset.x = (offset.x + 1) & 1;
  1578. }
  1579. if ((probe_global_pos.y & 1) != 0) {
  1580. offset.y = (offset.y + 1) & 1;
  1581. }
  1582. if ((probe_global_pos.z & 1) != 0) {
  1583. offset.z = (offset.z + 1) & 1;
  1584. }
  1585. push_constant.probe_offset[0] = offset.x;
  1586. push_constant.probe_offset[1] = offset.y;
  1587. push_constant.probe_offset[2] = offset.z;
  1588. push_constant.occlusion_index = i;
  1589. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1590. Vector3i groups = Vector3i(probe_size + 1, probe_size + 1, probe_size + 1) - offset; //if offset, it's one less probe per axis to compute
  1591. RD::get_singleton()->compute_list_dispatch(compute_list, groups.x, groups.y, groups.z);
  1592. }
  1593. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1594. }
  1595. RENDER_TIMESTAMP("SDFGI Store");
  1596. // store
  1597. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.preprocess_pipeline[SDFGIShader::PRE_PROCESS_STORE]);
  1598. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascades[cascade].sdf_store_uniform_set, 0);
  1599. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDFGIShader::PreprocessPushConstant));
  1600. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_size, cascade_size, cascade_size);
  1601. RD::get_singleton()->compute_list_end();
  1602. //clear these textures, as they will have previous garbage on next draw
  1603. RD::get_singleton()->texture_clear(cascades[cascade].light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1604. RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1605. RD::get_singleton()->texture_clear(cascades[cascade].light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  1606. #if 0
  1607. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(cascades[cascade].sdf, 0);
  1608. Ref<Image> img;
  1609. img.instantiate();
  1610. for (uint32_t i = 0; i < cascade_size; i++) {
  1611. Vector<uint8_t> subarr = data.slice(128 * 128 * i, 128 * 128 * (i + 1));
  1612. img->create(cascade_size, cascade_size, false, Image::FORMAT_L8, subarr);
  1613. img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png");
  1614. }
  1615. //finalize render and update sdf
  1616. #endif
  1617. #if 0
  1618. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(render_albedo, 0);
  1619. Ref<Image> img;
  1620. img.instantiate();
  1621. for (uint32_t i = 0; i < cascade_size; i++) {
  1622. Vector<uint8_t> subarr = data.slice(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2);
  1623. img->createcascade_size, cascade_size, false, Image::FORMAT_RGB565, subarr);
  1624. img->convert(Image::FORMAT_RGBA8);
  1625. img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png");
  1626. }
  1627. //finalize render and update sdf
  1628. #endif
  1629. RENDER_TIMESTAMP("< SDFGI Update SDF");
  1630. RD::get_singleton()->draw_command_end_label();
  1631. }
  1632. }
  1633. void RendererSceneGIRD::SDFGI::render_static_lights(RID p_render_buffers, uint32_t p_cascade_count, const uint32_t *p_cascade_indices, const PagedArray<RID> *p_positional_light_cull_result, RendererSceneRenderRD *p_scene_render) {
  1634. RendererSceneRenderRD::RenderBuffers *rb = p_scene_render->render_buffers_owner.get_or_null(p_render_buffers);
  1635. ERR_FAIL_COND(!rb); // we wouldn't be here if this failed but...
  1636. RD::get_singleton()->draw_command_begin_label("SDFGI Render Static Lights");
  1637. update_cascades();
  1638. SDFGIShader::Light lights[SDFGI::MAX_STATIC_LIGHTS];
  1639. uint32_t light_count[SDFGI::MAX_STATIC_LIGHTS];
  1640. for (uint32_t i = 0; i < p_cascade_count; i++) {
  1641. ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
  1642. SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
  1643. { //fill light buffer
  1644. AABB cascade_aabb;
  1645. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(cascade_size >> 1) + cc.position)) * cc.cell_size;
  1646. cascade_aabb.size = Vector3(1, 1, 1) * cascade_size * cc.cell_size;
  1647. int idx = 0;
  1648. for (uint32_t j = 0; j < (uint32_t)p_positional_light_cull_result[i].size(); j++) {
  1649. if (idx == SDFGI::MAX_STATIC_LIGHTS) {
  1650. break;
  1651. }
  1652. RendererSceneRenderRD::LightInstance *li = p_scene_render->light_instance_owner.get_or_null(p_positional_light_cull_result[i][j]);
  1653. ERR_CONTINUE(!li);
  1654. uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(li->light);
  1655. if (p_cascade_indices[i] > max_sdfgi_cascade) {
  1656. continue;
  1657. }
  1658. if (!cascade_aabb.intersects(li->aabb)) {
  1659. continue;
  1660. }
  1661. lights[idx].type = RSG::light_storage->light_get_type(li->light);
  1662. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1663. if (lights[idx].type == RS::LIGHT_DIRECTIONAL) {
  1664. dir.y *= y_mult; //only makes sense for directional
  1665. dir.normalize();
  1666. }
  1667. lights[idx].direction[0] = dir.x;
  1668. lights[idx].direction[1] = dir.y;
  1669. lights[idx].direction[2] = dir.z;
  1670. Vector3 pos = li->transform.origin;
  1671. pos.y *= y_mult;
  1672. lights[idx].position[0] = pos.x;
  1673. lights[idx].position[1] = pos.y;
  1674. lights[idx].position[2] = pos.z;
  1675. Color color = RSG::light_storage->light_get_color(li->light);
  1676. color = color.srgb_to_linear();
  1677. lights[idx].color[0] = color.r;
  1678. lights[idx].color[1] = color.g;
  1679. lights[idx].color[2] = color.b;
  1680. lights[idx].energy = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  1681. lights[idx].has_shadow = RSG::light_storage->light_has_shadow(li->light);
  1682. lights[idx].attenuation = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  1683. lights[idx].radius = RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  1684. lights[idx].cos_spot_angle = Math::cos(Math::deg2rad(RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE)));
  1685. lights[idx].inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1686. idx++;
  1687. }
  1688. if (idx > 0) {
  1689. RD::get_singleton()->buffer_update(cc.lights_buffer, 0, idx * sizeof(SDFGIShader::Light), lights);
  1690. }
  1691. light_count[i] = idx;
  1692. }
  1693. }
  1694. /* Static Lights */
  1695. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1696. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->sdfgi_shader.direct_light_pipeline[SDFGIShader::DIRECT_LIGHT_MODE_STATIC]);
  1697. SDFGIShader::DirectLightPushConstant dl_push_constant;
  1698. dl_push_constant.grid_size[0] = cascade_size;
  1699. dl_push_constant.grid_size[1] = cascade_size;
  1700. dl_push_constant.grid_size[2] = cascade_size;
  1701. dl_push_constant.max_cascades = cascades.size();
  1702. dl_push_constant.probe_axis_size = probe_axis_count;
  1703. dl_push_constant.bounce_feedback = 0.0; // this is static light, do not multibounce yet
  1704. dl_push_constant.y_mult = y_mult;
  1705. dl_push_constant.use_occlusion = uses_occlusion;
  1706. //all must be processed
  1707. dl_push_constant.process_offset = 0;
  1708. dl_push_constant.process_increment = 1;
  1709. for (uint32_t i = 0; i < p_cascade_count; i++) {
  1710. ERR_CONTINUE(p_cascade_indices[i] >= cascades.size());
  1711. SDFGI::Cascade &cc = cascades[p_cascade_indices[i]];
  1712. dl_push_constant.light_count = light_count[i];
  1713. dl_push_constant.cascade = p_cascade_indices[i];
  1714. if (dl_push_constant.light_count > 0) {
  1715. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cc.sdf_direct_light_uniform_set, 0);
  1716. RD::get_singleton()->compute_list_set_push_constant(compute_list, &dl_push_constant, sizeof(SDFGIShader::DirectLightPushConstant));
  1717. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cc.solid_cell_dispatch_buffer, 0);
  1718. }
  1719. }
  1720. RD::get_singleton()->compute_list_end();
  1721. RD::get_singleton()->draw_command_end_label();
  1722. }
  1723. ////////////////////////////////////////////////////////////////////////////////
  1724. // VoxelGIInstance
  1725. void RendererSceneGIRD::VoxelGIInstance::update(bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RendererSceneRender::GeometryInstance *> &p_dynamic_objects, RendererSceneRenderRD *p_scene_render) {
  1726. uint32_t data_version = storage->voxel_gi_get_data_version(probe);
  1727. // (RE)CREATE IF NEEDED
  1728. if (last_probe_data_version != data_version) {
  1729. //need to re-create everything
  1730. if (texture.is_valid()) {
  1731. RD::get_singleton()->free(texture);
  1732. RD::get_singleton()->free(write_buffer);
  1733. mipmaps.clear();
  1734. }
  1735. for (int i = 0; i < dynamic_maps.size(); i++) {
  1736. RD::get_singleton()->free(dynamic_maps[i].texture);
  1737. RD::get_singleton()->free(dynamic_maps[i].depth);
  1738. }
  1739. dynamic_maps.clear();
  1740. Vector3i octree_size = storage->voxel_gi_get_octree_size(probe);
  1741. if (octree_size != Vector3i()) {
  1742. //can create a 3D texture
  1743. Vector<int> levels = storage->voxel_gi_get_level_counts(probe);
  1744. RD::TextureFormat tf;
  1745. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1746. tf.width = octree_size.x;
  1747. tf.height = octree_size.y;
  1748. tf.depth = octree_size.z;
  1749. tf.texture_type = RD::TEXTURE_TYPE_3D;
  1750. tf.mipmaps = levels.size();
  1751. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1752. texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1753. RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1);
  1754. {
  1755. int total_elements = 0;
  1756. for (int i = 0; i < levels.size(); i++) {
  1757. total_elements += levels[i];
  1758. }
  1759. write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
  1760. }
  1761. for (int i = 0; i < levels.size(); i++) {
  1762. VoxelGIInstance::Mipmap mipmap;
  1763. mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), texture, 0, i, 1, RD::TEXTURE_SLICE_3D);
  1764. mipmap.level = levels.size() - i - 1;
  1765. mipmap.cell_offset = 0;
  1766. for (uint32_t j = 0; j < mipmap.level; j++) {
  1767. mipmap.cell_offset += levels[j];
  1768. }
  1769. mipmap.cell_count = levels[mipmap.level];
  1770. Vector<RD::Uniform> uniforms;
  1771. {
  1772. RD::Uniform u;
  1773. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1774. u.binding = 1;
  1775. u.append_id(storage->voxel_gi_get_octree_buffer(probe));
  1776. uniforms.push_back(u);
  1777. }
  1778. {
  1779. RD::Uniform u;
  1780. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1781. u.binding = 2;
  1782. u.append_id(storage->voxel_gi_get_data_buffer(probe));
  1783. uniforms.push_back(u);
  1784. }
  1785. {
  1786. RD::Uniform u;
  1787. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1788. u.binding = 4;
  1789. u.append_id(write_buffer);
  1790. uniforms.push_back(u);
  1791. }
  1792. {
  1793. RD::Uniform u;
  1794. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1795. u.binding = 9;
  1796. u.append_id(storage->voxel_gi_get_sdf_texture(probe));
  1797. uniforms.push_back(u);
  1798. }
  1799. {
  1800. RD::Uniform u;
  1801. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1802. u.binding = 10;
  1803. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1804. uniforms.push_back(u);
  1805. }
  1806. {
  1807. Vector<RD::Uniform> copy_uniforms = uniforms;
  1808. if (i == 0) {
  1809. {
  1810. RD::Uniform u;
  1811. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1812. u.binding = 3;
  1813. u.append_id(gi->voxel_gi_lights_uniform);
  1814. copy_uniforms.push_back(u);
  1815. }
  1816. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT], 0);
  1817. copy_uniforms = uniforms; //restore
  1818. {
  1819. RD::Uniform u;
  1820. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1821. u.binding = 5;
  1822. u.append_id(texture);
  1823. copy_uniforms.push_back(u);
  1824. }
  1825. mipmap.second_bounce_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE], 0);
  1826. } else {
  1827. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP], 0);
  1828. }
  1829. }
  1830. {
  1831. RD::Uniform u;
  1832. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1833. u.binding = 5;
  1834. u.append_id(mipmap.texture);
  1835. uniforms.push_back(u);
  1836. }
  1837. mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE], 0);
  1838. mipmaps.push_back(mipmap);
  1839. }
  1840. {
  1841. uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  1842. uint32_t oversample = nearest_power_of_2_templated(4);
  1843. int mipmap_index = 0;
  1844. while (mipmap_index < mipmaps.size()) {
  1845. VoxelGIInstance::DynamicMap dmap;
  1846. if (oversample > 0) {
  1847. dmap.size = dynamic_map_size * (1 << oversample);
  1848. dmap.mipmap = -1;
  1849. oversample--;
  1850. } else {
  1851. dmap.size = dynamic_map_size >> mipmap_index;
  1852. dmap.mipmap = mipmap_index;
  1853. mipmap_index++;
  1854. }
  1855. RD::TextureFormat dtf;
  1856. dtf.width = dmap.size;
  1857. dtf.height = dmap.size;
  1858. dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1859. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1860. if (dynamic_maps.size() == 0) {
  1861. dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1862. }
  1863. dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1864. if (dynamic_maps.size() == 0) {
  1865. // Render depth for first one.
  1866. // Use 16-bit depth when supported to improve performance.
  1867. dtf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D16_UNORM, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D16_UNORM : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  1868. dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1869. dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1870. }
  1871. //just use depth as-is
  1872. dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1873. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1874. dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1875. if (dynamic_maps.size() == 0) {
  1876. dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1877. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1878. dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1879. dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1880. dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1881. Vector<RID> fb;
  1882. fb.push_back(dmap.albedo);
  1883. fb.push_back(dmap.normal);
  1884. fb.push_back(dmap.orm);
  1885. fb.push_back(dmap.texture); //emission
  1886. fb.push_back(dmap.depth);
  1887. fb.push_back(dmap.fb_depth);
  1888. dmap.fb = RD::get_singleton()->framebuffer_create(fb);
  1889. {
  1890. Vector<RD::Uniform> uniforms;
  1891. {
  1892. RD::Uniform u;
  1893. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1894. u.binding = 3;
  1895. u.append_id(gi->voxel_gi_lights_uniform);
  1896. uniforms.push_back(u);
  1897. }
  1898. {
  1899. RD::Uniform u;
  1900. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1901. u.binding = 5;
  1902. u.append_id(dmap.albedo);
  1903. uniforms.push_back(u);
  1904. }
  1905. {
  1906. RD::Uniform u;
  1907. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1908. u.binding = 6;
  1909. u.append_id(dmap.normal);
  1910. uniforms.push_back(u);
  1911. }
  1912. {
  1913. RD::Uniform u;
  1914. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1915. u.binding = 7;
  1916. u.append_id(dmap.orm);
  1917. uniforms.push_back(u);
  1918. }
  1919. {
  1920. RD::Uniform u;
  1921. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1922. u.binding = 8;
  1923. u.append_id(dmap.fb_depth);
  1924. uniforms.push_back(u);
  1925. }
  1926. {
  1927. RD::Uniform u;
  1928. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1929. u.binding = 9;
  1930. u.append_id(storage->voxel_gi_get_sdf_texture(probe));
  1931. uniforms.push_back(u);
  1932. }
  1933. {
  1934. RD::Uniform u;
  1935. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1936. u.binding = 10;
  1937. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1938. uniforms.push_back(u);
  1939. }
  1940. {
  1941. RD::Uniform u;
  1942. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1943. u.binding = 11;
  1944. u.append_id(dmap.texture);
  1945. uniforms.push_back(u);
  1946. }
  1947. {
  1948. RD::Uniform u;
  1949. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1950. u.binding = 12;
  1951. u.append_id(dmap.depth);
  1952. uniforms.push_back(u);
  1953. }
  1954. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
  1955. }
  1956. } else {
  1957. bool plot = dmap.mipmap >= 0;
  1958. bool write = dmap.mipmap < (mipmaps.size() - 1);
  1959. Vector<RD::Uniform> uniforms;
  1960. {
  1961. RD::Uniform u;
  1962. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1963. u.binding = 5;
  1964. u.append_id(dynamic_maps[dynamic_maps.size() - 1].texture);
  1965. uniforms.push_back(u);
  1966. }
  1967. {
  1968. RD::Uniform u;
  1969. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1970. u.binding = 6;
  1971. u.append_id(dynamic_maps[dynamic_maps.size() - 1].depth);
  1972. uniforms.push_back(u);
  1973. }
  1974. if (write) {
  1975. {
  1976. RD::Uniform u;
  1977. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1978. u.binding = 7;
  1979. u.append_id(dmap.texture);
  1980. uniforms.push_back(u);
  1981. }
  1982. {
  1983. RD::Uniform u;
  1984. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1985. u.binding = 8;
  1986. u.append_id(dmap.depth);
  1987. uniforms.push_back(u);
  1988. }
  1989. }
  1990. {
  1991. RD::Uniform u;
  1992. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1993. u.binding = 9;
  1994. u.append_id(storage->voxel_gi_get_sdf_texture(probe));
  1995. uniforms.push_back(u);
  1996. }
  1997. {
  1998. RD::Uniform u;
  1999. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  2000. u.binding = 10;
  2001. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  2002. uniforms.push_back(u);
  2003. }
  2004. if (plot) {
  2005. {
  2006. RD::Uniform u;
  2007. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2008. u.binding = 11;
  2009. u.append_id(mipmaps[dmap.mipmap].texture);
  2010. uniforms.push_back(u);
  2011. }
  2012. }
  2013. dmap.uniform_set = RD::get_singleton()->uniform_set_create(
  2014. uniforms,
  2015. gi->voxel_gi_lighting_shader_version_shaders[(write && plot) ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : (write ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT)],
  2016. 0);
  2017. }
  2018. dynamic_maps.push_back(dmap);
  2019. }
  2020. }
  2021. }
  2022. last_probe_data_version = data_version;
  2023. p_update_light_instances = true; //just in case
  2024. p_scene_render->_base_uniforms_changed();
  2025. }
  2026. // UDPDATE TIME
  2027. if (has_dynamic_object_data) {
  2028. //if it has dynamic object data, it needs to be cleared
  2029. RD::get_singleton()->texture_clear(texture, Color(0, 0, 0, 0), 0, mipmaps.size(), 0, 1);
  2030. }
  2031. uint32_t light_count = 0;
  2032. if (p_update_light_instances || p_dynamic_objects.size() > 0) {
  2033. light_count = MIN(gi->voxel_gi_max_lights, (uint32_t)p_light_instances.size());
  2034. {
  2035. Transform3D to_cell = storage->voxel_gi_get_to_cell_xform(probe);
  2036. Transform3D to_probe_xform = (transform * to_cell.affine_inverse()).affine_inverse();
  2037. //update lights
  2038. for (uint32_t i = 0; i < light_count; i++) {
  2039. VoxelGILight &l = gi->voxel_gi_lights[i];
  2040. RID light_instance = p_light_instances[i];
  2041. RID light = p_scene_render->light_instance_get_base_light(light_instance);
  2042. l.type = RSG::light_storage->light_get_type(light);
  2043. if (l.type == RS::LIGHT_DIRECTIONAL && RSG::light_storage->light_directional_get_sky_mode(light) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
  2044. light_count--;
  2045. continue;
  2046. }
  2047. l.attenuation = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
  2048. l.energy = RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2049. l.radius = to_cell.basis.xform(Vector3(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_RANGE), 0, 0)).length();
  2050. Color color = RSG::light_storage->light_get_color(light).srgb_to_linear();
  2051. l.color[0] = color.r;
  2052. l.color[1] = color.g;
  2053. l.color[2] = color.b;
  2054. l.cos_spot_angle = Math::cos(Math::deg2rad(RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE)));
  2055. l.inv_spot_attenuation = 1.0f / RSG::light_storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2056. Transform3D xform = p_scene_render->light_instance_get_base_transform(light_instance);
  2057. Vector3 pos = to_probe_xform.xform(xform.origin);
  2058. Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_axis(2)).normalized();
  2059. l.position[0] = pos.x;
  2060. l.position[1] = pos.y;
  2061. l.position[2] = pos.z;
  2062. l.direction[0] = dir.x;
  2063. l.direction[1] = dir.y;
  2064. l.direction[2] = dir.z;
  2065. l.has_shadow = RSG::light_storage->light_has_shadow(light);
  2066. }
  2067. RD::get_singleton()->buffer_update(gi->voxel_gi_lights_uniform, 0, sizeof(VoxelGILight) * light_count, gi->voxel_gi_lights);
  2068. }
  2069. }
  2070. if (has_dynamic_object_data || p_update_light_instances || p_dynamic_objects.size()) {
  2071. // PROCESS MIPMAPS
  2072. if (mipmaps.size()) {
  2073. //can update mipmaps
  2074. Vector3i probe_size = storage->voxel_gi_get_octree_size(probe);
  2075. VoxelGIPushConstant push_constant;
  2076. push_constant.limits[0] = probe_size.x;
  2077. push_constant.limits[1] = probe_size.y;
  2078. push_constant.limits[2] = probe_size.z;
  2079. push_constant.stack_size = mipmaps.size();
  2080. push_constant.emission_scale = 1.0;
  2081. push_constant.propagation = storage->voxel_gi_get_propagation(probe);
  2082. push_constant.dynamic_range = storage->voxel_gi_get_dynamic_range(probe);
  2083. push_constant.light_count = light_count;
  2084. push_constant.aniso_strength = 0;
  2085. /* print_line("probe update to version " + itos(last_probe_version));
  2086. print_line("propagation " + rtos(push_constant.propagation));
  2087. print_line("dynrange " + rtos(push_constant.dynamic_range));
  2088. */
  2089. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  2090. int passes;
  2091. if (p_update_light_instances) {
  2092. passes = storage->voxel_gi_is_using_two_bounces(probe) ? 2 : 1;
  2093. } else {
  2094. passes = 1; //only re-blitting is necessary
  2095. }
  2096. int wg_size = 64;
  2097. int64_t wg_limit_x = (int64_t)RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  2098. for (int pass = 0; pass < passes; pass++) {
  2099. if (p_update_light_instances) {
  2100. for (int i = 0; i < mipmaps.size(); i++) {
  2101. if (i == 0) {
  2102. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[pass == 0 ? VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT : VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE]);
  2103. } else if (i == 1) {
  2104. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP]);
  2105. }
  2106. if (pass == 1 || i > 0) {
  2107. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  2108. }
  2109. if (pass == 0 || i > 0) {
  2110. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].uniform_set, 0);
  2111. } else {
  2112. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].second_bounce_uniform_set, 0);
  2113. }
  2114. push_constant.cell_offset = mipmaps[i].cell_offset;
  2115. push_constant.cell_count = mipmaps[i].cell_count;
  2116. int64_t wg_todo = (mipmaps[i].cell_count - 1) / wg_size + 1;
  2117. while (wg_todo) {
  2118. int64_t wg_count = MIN(wg_todo, wg_limit_x);
  2119. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
  2120. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  2121. wg_todo -= wg_count;
  2122. push_constant.cell_offset += wg_count * wg_size;
  2123. }
  2124. }
  2125. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  2126. }
  2127. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE]);
  2128. for (int i = 0; i < mipmaps.size(); i++) {
  2129. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mipmaps[i].write_uniform_set, 0);
  2130. push_constant.cell_offset = mipmaps[i].cell_offset;
  2131. push_constant.cell_count = mipmaps[i].cell_count;
  2132. int64_t wg_todo = (mipmaps[i].cell_count - 1) / wg_size + 1;
  2133. while (wg_todo) {
  2134. int64_t wg_count = MIN(wg_todo, wg_limit_x);
  2135. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIPushConstant));
  2136. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  2137. wg_todo -= wg_count;
  2138. push_constant.cell_offset += wg_count * wg_size;
  2139. }
  2140. }
  2141. }
  2142. RD::get_singleton()->compute_list_end();
  2143. }
  2144. }
  2145. has_dynamic_object_data = false; //clear until dynamic object data is used again
  2146. if (p_dynamic_objects.size() && dynamic_maps.size()) {
  2147. Vector3i octree_size = storage->voxel_gi_get_octree_size(probe);
  2148. int multiplier = dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  2149. Transform3D oversample_scale;
  2150. oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
  2151. Transform3D to_cell = oversample_scale * storage->voxel_gi_get_to_cell_xform(probe);
  2152. Transform3D to_world_xform = transform * to_cell.affine_inverse();
  2153. Transform3D to_probe_xform = to_world_xform.affine_inverse();
  2154. AABB probe_aabb(Vector3(), octree_size);
  2155. //this could probably be better parallelized in compute..
  2156. for (int i = 0; i < (int)p_dynamic_objects.size(); i++) {
  2157. RendererSceneRender::GeometryInstance *instance = p_dynamic_objects[i];
  2158. //transform aabb to voxel_gi
  2159. AABB aabb = (to_probe_xform * p_scene_render->geometry_instance_get_transform(instance)).xform(p_scene_render->geometry_instance_get_aabb(instance));
  2160. //this needs to wrap to grid resolution to avoid jitter
  2161. //also extend margin a bit just in case
  2162. Vector3i begin = aabb.position - Vector3i(1, 1, 1);
  2163. Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
  2164. for (int j = 0; j < 3; j++) {
  2165. if ((end[j] - begin[j]) & 1) {
  2166. end[j]++; //for half extents split, it needs to be even
  2167. }
  2168. begin[j] = MAX(begin[j], 0);
  2169. end[j] = MIN(end[j], octree_size[j] * multiplier);
  2170. }
  2171. //aabb = aabb.intersection(probe_aabb); //intersect
  2172. aabb.position = begin;
  2173. aabb.size = end - begin;
  2174. //print_line("aabb: " + aabb);
  2175. for (int j = 0; j < 6; j++) {
  2176. //if (j != 0 && j != 3) {
  2177. // continue;
  2178. //}
  2179. static const Vector3 render_z[6] = {
  2180. Vector3(1, 0, 0),
  2181. Vector3(0, 1, 0),
  2182. Vector3(0, 0, 1),
  2183. Vector3(-1, 0, 0),
  2184. Vector3(0, -1, 0),
  2185. Vector3(0, 0, -1),
  2186. };
  2187. static const Vector3 render_up[6] = {
  2188. Vector3(0, 1, 0),
  2189. Vector3(0, 0, 1),
  2190. Vector3(0, 1, 0),
  2191. Vector3(0, 1, 0),
  2192. Vector3(0, 0, 1),
  2193. Vector3(0, 1, 0),
  2194. };
  2195. Vector3 render_dir = render_z[j];
  2196. Vector3 up_dir = render_up[j];
  2197. Vector3 center = aabb.get_center();
  2198. Transform3D xform;
  2199. xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
  2200. Vector3 x_dir = xform.basis.get_axis(0).abs();
  2201. int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
  2202. Vector3 y_dir = xform.basis.get_axis(1).abs();
  2203. int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
  2204. Vector3 z_dir = -xform.basis.get_axis(2);
  2205. int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
  2206. Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
  2207. bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(0)) < 0);
  2208. bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(1)) < 0);
  2209. bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(2)) > 0);
  2210. CameraMatrix cm;
  2211. cm.set_orthogonal(-rect.size.width / 2, rect.size.width / 2, -rect.size.height / 2, rect.size.height / 2, 0.0001, aabb.size[z_axis]);
  2212. if (p_scene_render->cull_argument.size() == 0) {
  2213. p_scene_render->cull_argument.push_back(nullptr);
  2214. }
  2215. p_scene_render->cull_argument[0] = instance;
  2216. p_scene_render->_render_material(to_world_xform * xform, cm, true, p_scene_render->cull_argument, dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size));
  2217. VoxelGIDynamicPushConstant push_constant;
  2218. memset(&push_constant, 0, sizeof(VoxelGIDynamicPushConstant));
  2219. push_constant.limits[0] = octree_size.x;
  2220. push_constant.limits[1] = octree_size.y;
  2221. push_constant.limits[2] = octree_size.z;
  2222. push_constant.light_count = p_light_instances.size();
  2223. push_constant.x_dir[0] = x_dir[0];
  2224. push_constant.x_dir[1] = x_dir[1];
  2225. push_constant.x_dir[2] = x_dir[2];
  2226. push_constant.y_dir[0] = y_dir[0];
  2227. push_constant.y_dir[1] = y_dir[1];
  2228. push_constant.y_dir[2] = y_dir[2];
  2229. push_constant.z_dir[0] = z_dir[0];
  2230. push_constant.z_dir[1] = z_dir[1];
  2231. push_constant.z_dir[2] = z_dir[2];
  2232. push_constant.z_base = xform.origin[z_axis];
  2233. push_constant.z_sign = (z_flip ? -1.0 : 1.0);
  2234. push_constant.pos_multiplier = float(1.0) / multiplier;
  2235. push_constant.dynamic_range = storage->voxel_gi_get_dynamic_range(probe);
  2236. push_constant.flip_x = x_flip;
  2237. push_constant.flip_y = y_flip;
  2238. push_constant.rect_pos[0] = rect.position[0];
  2239. push_constant.rect_pos[1] = rect.position[1];
  2240. push_constant.rect_size[0] = rect.size[0];
  2241. push_constant.rect_size[1] = rect.size[1];
  2242. push_constant.prev_rect_ofs[0] = 0;
  2243. push_constant.prev_rect_ofs[1] = 0;
  2244. push_constant.prev_rect_size[0] = 0;
  2245. push_constant.prev_rect_size[1] = 0;
  2246. push_constant.on_mipmap = false;
  2247. push_constant.propagation = storage->voxel_gi_get_propagation(probe);
  2248. push_constant.pad[0] = 0;
  2249. push_constant.pad[1] = 0;
  2250. push_constant.pad[2] = 0;
  2251. //process lighting
  2252. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  2253. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
  2254. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[0].uniform_set, 0);
  2255. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
  2256. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  2257. //print_line("rect: " + itos(i) + ": " + rect);
  2258. for (int k = 1; k < dynamic_maps.size(); k++) {
  2259. // enlarge the rect if needed so all pixels fit when downscaled,
  2260. // this ensures downsampling is smooth and optimal because no pixels are left behind
  2261. //x
  2262. if (rect.position.x & 1) {
  2263. rect.size.x++;
  2264. push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
  2265. } else {
  2266. push_constant.prev_rect_ofs[0] = 0;
  2267. }
  2268. if (rect.size.x & 1) {
  2269. rect.size.x++;
  2270. }
  2271. rect.position.x >>= 1;
  2272. rect.size.x = MAX(1, rect.size.x >> 1);
  2273. //y
  2274. if (rect.position.y & 1) {
  2275. rect.size.y++;
  2276. push_constant.prev_rect_ofs[1] = 1;
  2277. } else {
  2278. push_constant.prev_rect_ofs[1] = 0;
  2279. }
  2280. if (rect.size.y & 1) {
  2281. rect.size.y++;
  2282. }
  2283. rect.position.y >>= 1;
  2284. rect.size.y = MAX(1, rect.size.y >> 1);
  2285. //shrink limits to ensure plot does not go outside map
  2286. if (dynamic_maps[k].mipmap > 0) {
  2287. for (int l = 0; l < 3; l++) {
  2288. push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
  2289. }
  2290. }
  2291. //print_line("rect: " + itos(i) + ": " + rect);
  2292. push_constant.rect_pos[0] = rect.position[0];
  2293. push_constant.rect_pos[1] = rect.position[1];
  2294. push_constant.prev_rect_size[0] = push_constant.rect_size[0];
  2295. push_constant.prev_rect_size[1] = push_constant.rect_size[1];
  2296. push_constant.rect_size[0] = rect.size[0];
  2297. push_constant.rect_size[1] = rect.size[1];
  2298. push_constant.on_mipmap = dynamic_maps[k].mipmap > 0;
  2299. RD::get_singleton()->compute_list_add_barrier(compute_list);
  2300. if (dynamic_maps[k].mipmap < 0) {
  2301. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
  2302. } else if (k < dynamic_maps.size() - 1) {
  2303. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT]);
  2304. } else {
  2305. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi->voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
  2306. }
  2307. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, dynamic_maps[k].uniform_set, 0);
  2308. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VoxelGIDynamicPushConstant));
  2309. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  2310. }
  2311. RD::get_singleton()->compute_list_end();
  2312. }
  2313. }
  2314. has_dynamic_object_data = true; //clear until dynamic object data is used again
  2315. }
  2316. last_probe_version = storage->voxel_gi_get_version(probe);
  2317. }
  2318. void RendererSceneGIRD::VoxelGIInstance::debug(RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
  2319. if (mipmaps.size() == 0) {
  2320. return;
  2321. }
  2322. CameraMatrix cam_transform = (p_camera_with_transform * CameraMatrix(transform)) * CameraMatrix(storage->voxel_gi_get_to_cell_xform(probe).affine_inverse());
  2323. int level = 0;
  2324. Vector3i octree_size = storage->voxel_gi_get_octree_size(probe);
  2325. VoxelGIDebugPushConstant push_constant;
  2326. push_constant.alpha = p_alpha;
  2327. push_constant.dynamic_range = storage->voxel_gi_get_dynamic_range(probe);
  2328. push_constant.cell_offset = mipmaps[level].cell_offset;
  2329. push_constant.level = level;
  2330. push_constant.bounds[0] = octree_size.x >> level;
  2331. push_constant.bounds[1] = octree_size.y >> level;
  2332. push_constant.bounds[2] = octree_size.z >> level;
  2333. push_constant.pad = 0;
  2334. for (int i = 0; i < 4; i++) {
  2335. for (int j = 0; j < 4; j++) {
  2336. push_constant.projection[i * 4 + j] = cam_transform.matrix[i][j];
  2337. }
  2338. }
  2339. if (gi->voxel_gi_debug_uniform_set.is_valid()) {
  2340. RD::get_singleton()->free(gi->voxel_gi_debug_uniform_set);
  2341. }
  2342. Vector<RD::Uniform> uniforms;
  2343. {
  2344. RD::Uniform u;
  2345. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2346. u.binding = 1;
  2347. u.append_id(storage->voxel_gi_get_data_buffer(probe));
  2348. uniforms.push_back(u);
  2349. }
  2350. {
  2351. RD::Uniform u;
  2352. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2353. u.binding = 2;
  2354. u.append_id(texture);
  2355. uniforms.push_back(u);
  2356. }
  2357. {
  2358. RD::Uniform u;
  2359. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  2360. u.binding = 3;
  2361. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  2362. uniforms.push_back(u);
  2363. }
  2364. int cell_count;
  2365. if (!p_emission && p_lighting && has_dynamic_object_data) {
  2366. cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
  2367. } else {
  2368. cell_count = mipmaps[level].cell_count;
  2369. }
  2370. gi->voxel_gi_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi->voxel_gi_debug_shader_version_shaders[0], 0);
  2371. int voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_COLOR;
  2372. if (p_emission) {
  2373. voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_EMISSION;
  2374. } else if (p_lighting) {
  2375. voxel_gi_debug_pipeline = has_dynamic_object_data ? VOXEL_GI_DEBUG_LIGHT_FULL : VOXEL_GI_DEBUG_LIGHT;
  2376. }
  2377. RD::get_singleton()->draw_list_bind_render_pipeline(
  2378. p_draw_list,
  2379. gi->voxel_gi_debug_shader_version_pipelines[voxel_gi_debug_pipeline].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  2380. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, gi->voxel_gi_debug_uniform_set, 0);
  2381. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(VoxelGIDebugPushConstant));
  2382. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
  2383. }
  2384. ////////////////////////////////////////////////////////////////////////////////
  2385. // GIRD
  2386. RendererSceneGIRD::RendererSceneGIRD() {
  2387. sdfgi_ray_count = RS::EnvironmentSDFGIRayCount(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/probe_ray_count")), 0, int32_t(RS::ENV_SDFGI_RAY_COUNT_MAX - 1)));
  2388. sdfgi_frames_to_converge = RS::EnvironmentSDFGIFramesToConverge(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/frames_to_converge")), 0, int32_t(RS::ENV_SDFGI_CONVERGE_MAX - 1)));
  2389. sdfgi_frames_to_update_light = RS::EnvironmentSDFGIFramesToUpdateLight(CLAMP(int32_t(GLOBAL_GET("rendering/global_illumination/sdfgi/frames_to_update_lights")), 0, int32_t(RS::ENV_SDFGI_UPDATE_LIGHT_MAX - 1)));
  2390. }
  2391. RendererSceneGIRD::~RendererSceneGIRD() {
  2392. }
  2393. void RendererSceneGIRD::init(RendererStorageRD *p_storage, RendererSceneSkyRD *p_sky) {
  2394. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  2395. storage = p_storage;
  2396. /* GI */
  2397. {
  2398. //kinda complicated to compute the amount of slots, we try to use as many as we can
  2399. voxel_gi_lights = memnew_arr(VoxelGILight, voxel_gi_max_lights);
  2400. voxel_gi_lights_uniform = RD::get_singleton()->uniform_buffer_create(voxel_gi_max_lights * sizeof(VoxelGILight));
  2401. voxel_gi_quality = RS::VoxelGIQuality(CLAMP(int(GLOBAL_GET("rendering/global_illumination/voxel_gi/quality")), 0, 1));
  2402. String defines = "\n#define MAX_LIGHTS " + itos(voxel_gi_max_lights) + "\n";
  2403. Vector<String> versions;
  2404. versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
  2405. versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
  2406. versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
  2407. versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
  2408. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
  2409. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  2410. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
  2411. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  2412. voxel_gi_shader.initialize(versions, defines);
  2413. voxel_gi_lighting_shader_version = voxel_gi_shader.version_create();
  2414. for (int i = 0; i < VOXEL_GI_SHADER_VERSION_MAX; i++) {
  2415. voxel_gi_lighting_shader_version_shaders[i] = voxel_gi_shader.version_get_shader(voxel_gi_lighting_shader_version, i);
  2416. voxel_gi_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(voxel_gi_lighting_shader_version_shaders[i]);
  2417. }
  2418. }
  2419. {
  2420. String defines;
  2421. Vector<String> versions;
  2422. versions.push_back("\n#define MODE_DEBUG_COLOR\n");
  2423. versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
  2424. versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
  2425. versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
  2426. voxel_gi_debug_shader.initialize(versions, defines);
  2427. voxel_gi_debug_shader_version = voxel_gi_debug_shader.version_create();
  2428. for (int i = 0; i < VOXEL_GI_DEBUG_MAX; i++) {
  2429. voxel_gi_debug_shader_version_shaders[i] = voxel_gi_debug_shader.version_get_shader(voxel_gi_debug_shader_version, i);
  2430. RD::PipelineRasterizationState rs;
  2431. rs.cull_mode = RD::POLYGON_CULL_FRONT;
  2432. RD::PipelineDepthStencilState ds;
  2433. ds.enable_depth_test = true;
  2434. ds.enable_depth_write = true;
  2435. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2436. voxel_gi_debug_shader_version_pipelines[i].setup(voxel_gi_debug_shader_version_shaders[i], RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  2437. }
  2438. }
  2439. /* SDGFI */
  2440. {
  2441. Vector<String> preprocess_modes;
  2442. preprocess_modes.push_back("\n#define MODE_SCROLL\n");
  2443. preprocess_modes.push_back("\n#define MODE_SCROLL_OCCLUSION\n");
  2444. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD\n");
  2445. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD_HALF\n");
  2446. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD\n");
  2447. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD_OPTIMIZED\n");
  2448. preprocess_modes.push_back("\n#define MODE_UPSCALE_JUMP_FLOOD\n");
  2449. preprocess_modes.push_back("\n#define MODE_OCCLUSION\n");
  2450. preprocess_modes.push_back("\n#define MODE_STORE\n");
  2451. String defines = "\n#define OCCLUSION_SIZE " + itos(SDFGI::CASCADE_SIZE / SDFGI::PROBE_DIVISOR) + "\n";
  2452. sdfgi_shader.preprocess.initialize(preprocess_modes, defines);
  2453. sdfgi_shader.preprocess_shader = sdfgi_shader.preprocess.version_create();
  2454. for (int i = 0; i < SDFGIShader::PRE_PROCESS_MAX; i++) {
  2455. sdfgi_shader.preprocess_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, i));
  2456. }
  2457. }
  2458. {
  2459. //calculate tables
  2460. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  2461. Vector<String> direct_light_modes;
  2462. direct_light_modes.push_back("\n#define MODE_PROCESS_STATIC\n");
  2463. direct_light_modes.push_back("\n#define MODE_PROCESS_DYNAMIC\n");
  2464. sdfgi_shader.direct_light.initialize(direct_light_modes, defines);
  2465. sdfgi_shader.direct_light_shader = sdfgi_shader.direct_light.version_create();
  2466. for (int i = 0; i < SDFGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
  2467. sdfgi_shader.direct_light_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.direct_light.version_get_shader(sdfgi_shader.direct_light_shader, i));
  2468. }
  2469. }
  2470. {
  2471. //calculate tables
  2472. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  2473. defines += "\n#define SH_SIZE " + itos(SDFGI::SH_SIZE) + "\n";
  2474. if (p_sky->sky_use_cubemap_array) {
  2475. defines += "\n#define USE_CUBEMAP_ARRAY\n";
  2476. }
  2477. Vector<String> integrate_modes;
  2478. integrate_modes.push_back("\n#define MODE_PROCESS\n");
  2479. integrate_modes.push_back("\n#define MODE_STORE\n");
  2480. integrate_modes.push_back("\n#define MODE_SCROLL\n");
  2481. integrate_modes.push_back("\n#define MODE_SCROLL_STORE\n");
  2482. sdfgi_shader.integrate.initialize(integrate_modes, defines);
  2483. sdfgi_shader.integrate_shader = sdfgi_shader.integrate.version_create();
  2484. for (int i = 0; i < SDFGIShader::INTEGRATE_MODE_MAX; i++) {
  2485. sdfgi_shader.integrate_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, i));
  2486. }
  2487. {
  2488. Vector<RD::Uniform> uniforms;
  2489. {
  2490. RD::Uniform u;
  2491. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2492. u.binding = 0;
  2493. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_CUBEMAP_WHITE));
  2494. uniforms.push_back(u);
  2495. }
  2496. {
  2497. RD::Uniform u;
  2498. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  2499. u.binding = 1;
  2500. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  2501. uniforms.push_back(u);
  2502. }
  2503. sdfgi_shader.integrate_default_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, 0), 1);
  2504. }
  2505. }
  2506. //GK
  2507. {
  2508. //calculate tables
  2509. String defines = "\n#define SDFGI_OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  2510. Vector<String> gi_modes;
  2511. gi_modes.push_back("\n#define USE_VOXEL_GI_INSTANCES\n");
  2512. gi_modes.push_back("\n#define USE_SDFGI\n");
  2513. gi_modes.push_back("\n#define USE_SDFGI\n\n#define USE_VOXEL_GI_INSTANCES\n");
  2514. gi_modes.push_back("\n#define MODE_HALF_RES\n#define USE_VOXEL_GI_INSTANCES\n");
  2515. gi_modes.push_back("\n#define MODE_HALF_RES\n#define USE_SDFGI\n");
  2516. gi_modes.push_back("\n#define MODE_HALF_RES\n#define USE_SDFGI\n\n#define USE_VOXEL_GI_INSTANCES\n");
  2517. shader.initialize(gi_modes, defines);
  2518. shader_version = shader.version_create();
  2519. for (int i = 0; i < MODE_MAX; i++) {
  2520. pipelines[i] = RD::get_singleton()->compute_pipeline_create(shader.version_get_shader(shader_version, i));
  2521. }
  2522. sdfgi_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGIData));
  2523. }
  2524. {
  2525. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  2526. Vector<String> debug_modes;
  2527. debug_modes.push_back("");
  2528. sdfgi_shader.debug.initialize(debug_modes, defines);
  2529. sdfgi_shader.debug_shader = sdfgi_shader.debug.version_create();
  2530. sdfgi_shader.debug_shader_version = sdfgi_shader.debug.version_get_shader(sdfgi_shader.debug_shader, 0);
  2531. sdfgi_shader.debug_pipeline = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.debug_shader_version);
  2532. }
  2533. {
  2534. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  2535. Vector<String> versions;
  2536. versions.push_back("\n#define MODE_PROBES\n");
  2537. versions.push_back("\n#define MODE_VISIBILITY\n");
  2538. sdfgi_shader.debug_probes.initialize(versions, defines);
  2539. sdfgi_shader.debug_probes_shader = sdfgi_shader.debug_probes.version_create();
  2540. {
  2541. RD::PipelineRasterizationState rs;
  2542. rs.cull_mode = RD::POLYGON_CULL_DISABLED;
  2543. RD::PipelineDepthStencilState ds;
  2544. ds.enable_depth_test = true;
  2545. ds.enable_depth_write = true;
  2546. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2547. for (int i = 0; i < SDFGIShader::PROBE_DEBUG_MAX; i++) {
  2548. RID debug_probes_shader_version = sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, i);
  2549. sdfgi_shader.debug_probes_pipeline[i].setup(debug_probes_shader_version, RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  2550. }
  2551. }
  2552. }
  2553. default_voxel_gi_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(VoxelGIData) * MAX_VOXEL_GI_INSTANCES);
  2554. half_resolution = GLOBAL_GET("rendering/global_illumination/gi/use_half_resolution");
  2555. }
  2556. void RendererSceneGIRD::free() {
  2557. RD::get_singleton()->free(default_voxel_gi_buffer);
  2558. RD::get_singleton()->free(voxel_gi_lights_uniform);
  2559. RD::get_singleton()->free(sdfgi_ubo);
  2560. voxel_gi_debug_shader.version_free(voxel_gi_debug_shader_version);
  2561. voxel_gi_shader.version_free(voxel_gi_lighting_shader_version);
  2562. shader.version_free(shader_version);
  2563. sdfgi_shader.debug_probes.version_free(sdfgi_shader.debug_probes_shader);
  2564. sdfgi_shader.debug.version_free(sdfgi_shader.debug_shader);
  2565. sdfgi_shader.direct_light.version_free(sdfgi_shader.direct_light_shader);
  2566. sdfgi_shader.integrate.version_free(sdfgi_shader.integrate_shader);
  2567. sdfgi_shader.preprocess.version_free(sdfgi_shader.preprocess_shader);
  2568. if (voxel_gi_lights) {
  2569. memdelete_arr(voxel_gi_lights);
  2570. }
  2571. }
  2572. RendererSceneGIRD::SDFGI *RendererSceneGIRD::create_sdfgi(RendererSceneEnvironmentRD *p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size) {
  2573. SDFGI *sdfgi = memnew(SDFGI);
  2574. sdfgi->create(p_env, p_world_position, p_requested_history_size, this);
  2575. return sdfgi;
  2576. }
  2577. void RendererSceneGIRD::setup_voxel_gi_instances(RID p_render_buffers, const Transform3D &p_transform, const PagedArray<RID> &p_voxel_gi_instances, uint32_t &r_voxel_gi_instances_used, RendererSceneRenderRD *p_scene_render) {
  2578. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  2579. r_voxel_gi_instances_used = 0;
  2580. // feels a little dirty to use our container this way but....
  2581. RendererSceneRenderRD::RenderBuffers *rb = p_scene_render->render_buffers_owner.get_or_null(p_render_buffers);
  2582. ERR_FAIL_COND(rb == nullptr);
  2583. RID voxel_gi_buffer = p_scene_render->render_buffers_get_voxel_gi_buffer(p_render_buffers);
  2584. VoxelGIData voxel_gi_data[MAX_VOXEL_GI_INSTANCES];
  2585. bool voxel_gi_instances_changed = false;
  2586. Transform3D to_camera;
  2587. to_camera.origin = p_transform.origin; //only translation, make local
  2588. for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
  2589. RID texture;
  2590. if (i < (int)p_voxel_gi_instances.size()) {
  2591. VoxelGIInstance *gipi = get_probe_instance(p_voxel_gi_instances[i]);
  2592. if (gipi) {
  2593. texture = gipi->texture;
  2594. VoxelGIData &gipd = voxel_gi_data[i];
  2595. RID base_probe = gipi->probe;
  2596. Transform3D to_cell = storage->voxel_gi_get_to_cell_xform(gipi->probe) * gipi->transform.affine_inverse() * to_camera;
  2597. gipd.xform[0] = to_cell.basis.elements[0][0];
  2598. gipd.xform[1] = to_cell.basis.elements[1][0];
  2599. gipd.xform[2] = to_cell.basis.elements[2][0];
  2600. gipd.xform[3] = 0;
  2601. gipd.xform[4] = to_cell.basis.elements[0][1];
  2602. gipd.xform[5] = to_cell.basis.elements[1][1];
  2603. gipd.xform[6] = to_cell.basis.elements[2][1];
  2604. gipd.xform[7] = 0;
  2605. gipd.xform[8] = to_cell.basis.elements[0][2];
  2606. gipd.xform[9] = to_cell.basis.elements[1][2];
  2607. gipd.xform[10] = to_cell.basis.elements[2][2];
  2608. gipd.xform[11] = 0;
  2609. gipd.xform[12] = to_cell.origin.x;
  2610. gipd.xform[13] = to_cell.origin.y;
  2611. gipd.xform[14] = to_cell.origin.z;
  2612. gipd.xform[15] = 1;
  2613. Vector3 bounds = storage->voxel_gi_get_octree_size(base_probe);
  2614. gipd.bounds[0] = bounds.x;
  2615. gipd.bounds[1] = bounds.y;
  2616. gipd.bounds[2] = bounds.z;
  2617. gipd.dynamic_range = storage->voxel_gi_get_dynamic_range(base_probe) * storage->voxel_gi_get_energy(base_probe);
  2618. gipd.bias = storage->voxel_gi_get_bias(base_probe);
  2619. gipd.normal_bias = storage->voxel_gi_get_normal_bias(base_probe);
  2620. gipd.blend_ambient = !storage->voxel_gi_is_interior(base_probe);
  2621. gipd.mipmaps = gipi->mipmaps.size();
  2622. }
  2623. r_voxel_gi_instances_used++;
  2624. }
  2625. if (texture == RID()) {
  2626. texture = texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  2627. }
  2628. if (texture != rb->gi.voxel_gi_textures[i]) {
  2629. voxel_gi_instances_changed = true;
  2630. rb->gi.voxel_gi_textures[i] = texture;
  2631. }
  2632. }
  2633. if (voxel_gi_instances_changed) {
  2634. if (RD::get_singleton()->uniform_set_is_valid(rb->gi.uniform_set)) {
  2635. RD::get_singleton()->free(rb->gi.uniform_set);
  2636. }
  2637. rb->gi.uniform_set = RID();
  2638. if (rb->volumetric_fog) {
  2639. if (RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->fog_uniform_set)) {
  2640. RD::get_singleton()->free(rb->volumetric_fog->fog_uniform_set);
  2641. RD::get_singleton()->free(rb->volumetric_fog->process_uniform_set);
  2642. RD::get_singleton()->free(rb->volumetric_fog->process_uniform_set2);
  2643. }
  2644. rb->volumetric_fog->fog_uniform_set = RID();
  2645. rb->volumetric_fog->process_uniform_set = RID();
  2646. rb->volumetric_fog->process_uniform_set2 = RID();
  2647. }
  2648. }
  2649. if (p_voxel_gi_instances.size() > 0) {
  2650. RD::get_singleton()->draw_command_begin_label("VoxelGIs Setup");
  2651. RD::get_singleton()->buffer_update(voxel_gi_buffer, 0, sizeof(VoxelGIData) * MIN((uint64_t)MAX_VOXEL_GI_INSTANCES, p_voxel_gi_instances.size()), voxel_gi_data, RD::BARRIER_MASK_COMPUTE);
  2652. RD::get_singleton()->draw_command_end_label();
  2653. }
  2654. }
  2655. void RendererSceneGIRD::process_gi(RID p_render_buffers, RID p_normal_roughness_buffer, RID p_voxel_gi_buffer, RID p_environment, const CameraMatrix &p_projection, const Transform3D &p_transform, const PagedArray<RID> &p_voxel_gi_instances, RendererSceneRenderRD *p_scene_render) {
  2656. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  2657. RD::get_singleton()->draw_command_begin_label("GI Render");
  2658. RendererSceneRenderRD::RenderBuffers *rb = p_scene_render->render_buffers_owner.get_or_null(p_render_buffers);
  2659. ERR_FAIL_COND(rb == nullptr);
  2660. if (rb->ambient_buffer.is_null() || rb->gi.using_half_size_gi != half_resolution) {
  2661. if (rb->ambient_buffer.is_valid()) {
  2662. RD::get_singleton()->free(rb->ambient_buffer);
  2663. RD::get_singleton()->free(rb->reflection_buffer);
  2664. }
  2665. RD::TextureFormat tf;
  2666. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2667. tf.width = rb->internal_width;
  2668. tf.height = rb->internal_height;
  2669. if (half_resolution) {
  2670. tf.width >>= 1;
  2671. tf.height >>= 1;
  2672. }
  2673. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2674. rb->reflection_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2675. rb->ambient_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2676. rb->gi.using_half_size_gi = half_resolution;
  2677. }
  2678. PushConstant push_constant;
  2679. push_constant.screen_size[0] = rb->internal_width;
  2680. push_constant.screen_size[1] = rb->internal_height;
  2681. push_constant.z_near = p_projection.get_z_near();
  2682. push_constant.z_far = p_projection.get_z_far();
  2683. push_constant.orthogonal = p_projection.is_orthogonal();
  2684. push_constant.proj_info[0] = -2.0f / (rb->internal_width * p_projection.matrix[0][0]);
  2685. push_constant.proj_info[1] = -2.0f / (rb->internal_height * p_projection.matrix[1][1]);
  2686. push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  2687. push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  2688. push_constant.max_voxel_gi_instances = MIN((uint64_t)MAX_VOXEL_GI_INSTANCES, p_voxel_gi_instances.size());
  2689. push_constant.high_quality_vct = voxel_gi_quality == RS::VOXEL_GI_QUALITY_HIGH;
  2690. bool use_sdfgi = rb->sdfgi != nullptr;
  2691. bool use_voxel_gi_instances = push_constant.max_voxel_gi_instances > 0;
  2692. push_constant.cam_rotation[0] = p_transform.basis[0][0];
  2693. push_constant.cam_rotation[1] = p_transform.basis[1][0];
  2694. push_constant.cam_rotation[2] = p_transform.basis[2][0];
  2695. push_constant.cam_rotation[3] = 0;
  2696. push_constant.cam_rotation[4] = p_transform.basis[0][1];
  2697. push_constant.cam_rotation[5] = p_transform.basis[1][1];
  2698. push_constant.cam_rotation[6] = p_transform.basis[2][1];
  2699. push_constant.cam_rotation[7] = 0;
  2700. push_constant.cam_rotation[8] = p_transform.basis[0][2];
  2701. push_constant.cam_rotation[9] = p_transform.basis[1][2];
  2702. push_constant.cam_rotation[10] = p_transform.basis[2][2];
  2703. push_constant.cam_rotation[11] = 0;
  2704. if (rb->gi.uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(rb->gi.uniform_set)) {
  2705. Vector<RD::Uniform> uniforms;
  2706. {
  2707. RD::Uniform u;
  2708. u.binding = 1;
  2709. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2710. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  2711. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  2712. u.append_id(rb->sdfgi->cascades[j].sdf_tex);
  2713. } else {
  2714. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  2715. }
  2716. }
  2717. uniforms.push_back(u);
  2718. }
  2719. {
  2720. RD::Uniform u;
  2721. u.binding = 2;
  2722. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2723. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  2724. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  2725. u.append_id(rb->sdfgi->cascades[j].light_tex);
  2726. } else {
  2727. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  2728. }
  2729. }
  2730. uniforms.push_back(u);
  2731. }
  2732. {
  2733. RD::Uniform u;
  2734. u.binding = 3;
  2735. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2736. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  2737. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  2738. u.append_id(rb->sdfgi->cascades[j].light_aniso_0_tex);
  2739. } else {
  2740. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  2741. }
  2742. }
  2743. uniforms.push_back(u);
  2744. }
  2745. {
  2746. RD::Uniform u;
  2747. u.binding = 4;
  2748. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2749. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  2750. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  2751. u.append_id(rb->sdfgi->cascades[j].light_aniso_1_tex);
  2752. } else {
  2753. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  2754. }
  2755. }
  2756. uniforms.push_back(u);
  2757. }
  2758. {
  2759. RD::Uniform u;
  2760. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2761. u.binding = 5;
  2762. if (rb->sdfgi) {
  2763. u.append_id(rb->sdfgi->occlusion_texture);
  2764. } else {
  2765. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  2766. }
  2767. uniforms.push_back(u);
  2768. }
  2769. {
  2770. RD::Uniform u;
  2771. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  2772. u.binding = 6;
  2773. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  2774. uniforms.push_back(u);
  2775. }
  2776. {
  2777. RD::Uniform u;
  2778. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  2779. u.binding = 7;
  2780. u.append_id(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  2781. uniforms.push_back(u);
  2782. }
  2783. {
  2784. RD::Uniform u;
  2785. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2786. u.binding = 9;
  2787. u.append_id(rb->ambient_buffer);
  2788. uniforms.push_back(u);
  2789. }
  2790. {
  2791. RD::Uniform u;
  2792. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2793. u.binding = 10;
  2794. u.append_id(rb->reflection_buffer);
  2795. uniforms.push_back(u);
  2796. }
  2797. {
  2798. RD::Uniform u;
  2799. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2800. u.binding = 11;
  2801. if (rb->sdfgi) {
  2802. u.append_id(rb->sdfgi->lightprobe_texture);
  2803. } else {
  2804. u.append_id(texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
  2805. }
  2806. uniforms.push_back(u);
  2807. }
  2808. {
  2809. RD::Uniform u;
  2810. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2811. u.binding = 12;
  2812. u.append_id(rb->depth_texture);
  2813. uniforms.push_back(u);
  2814. }
  2815. {
  2816. RD::Uniform u;
  2817. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2818. u.binding = 13;
  2819. u.append_id(p_normal_roughness_buffer);
  2820. uniforms.push_back(u);
  2821. }
  2822. {
  2823. RD::Uniform u;
  2824. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2825. u.binding = 14;
  2826. RID buffer = p_voxel_gi_buffer.is_valid() ? p_voxel_gi_buffer : texture_storage->texture_rd_get_default(RendererRD::DEFAULT_RD_TEXTURE_BLACK);
  2827. u.append_id(buffer);
  2828. uniforms.push_back(u);
  2829. }
  2830. {
  2831. RD::Uniform u;
  2832. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  2833. u.binding = 15;
  2834. u.append_id(sdfgi_ubo);
  2835. uniforms.push_back(u);
  2836. }
  2837. {
  2838. RD::Uniform u;
  2839. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  2840. u.binding = 16;
  2841. u.append_id(rb->gi.voxel_gi_buffer);
  2842. uniforms.push_back(u);
  2843. }
  2844. {
  2845. RD::Uniform u;
  2846. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2847. u.binding = 17;
  2848. for (int i = 0; i < MAX_VOXEL_GI_INSTANCES; i++) {
  2849. u.append_id(rb->gi.voxel_gi_textures[i]);
  2850. }
  2851. uniforms.push_back(u);
  2852. }
  2853. rb->gi.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.version_get_shader(shader_version, 0), 0);
  2854. }
  2855. Mode mode;
  2856. if (rb->gi.using_half_size_gi) {
  2857. mode = (use_sdfgi && use_voxel_gi_instances) ? MODE_HALF_RES_COMBINED : (use_sdfgi ? MODE_HALF_RES_SDFGI : MODE_HALF_RES_VOXEL_GI);
  2858. } else {
  2859. mode = (use_sdfgi && use_voxel_gi_instances) ? MODE_COMBINED : (use_sdfgi ? MODE_SDFGI : MODE_VOXEL_GI);
  2860. }
  2861. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(true);
  2862. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[mode]);
  2863. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->gi.uniform_set, 0);
  2864. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  2865. if (rb->gi.using_half_size_gi) {
  2866. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->internal_width >> 1, rb->internal_height >> 1, 1);
  2867. } else {
  2868. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->internal_width, rb->internal_height, 1);
  2869. }
  2870. //do barrier later to allow oeverlap
  2871. //RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER); //no barriers, let other compute, raster and transfer happen at the same time
  2872. RD::get_singleton()->draw_command_end_label();
  2873. }
  2874. RID RendererSceneGIRD::voxel_gi_instance_create(RID p_base) {
  2875. VoxelGIInstance voxel_gi;
  2876. voxel_gi.gi = this;
  2877. voxel_gi.storage = storage;
  2878. voxel_gi.probe = p_base;
  2879. RID rid = voxel_gi_instance_owner.make_rid(voxel_gi);
  2880. return rid;
  2881. }
  2882. void RendererSceneGIRD::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
  2883. VoxelGIInstance *voxel_gi = get_probe_instance(p_probe);
  2884. ERR_FAIL_COND(!voxel_gi);
  2885. voxel_gi->transform = p_xform;
  2886. }
  2887. bool RendererSceneGIRD::voxel_gi_needs_update(RID p_probe) const {
  2888. VoxelGIInstance *voxel_gi = get_probe_instance(p_probe);
  2889. ERR_FAIL_COND_V(!voxel_gi, false);
  2890. return voxel_gi->last_probe_version != storage->voxel_gi_get_version(voxel_gi->probe);
  2891. }
  2892. void RendererSceneGIRD::voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RendererSceneRender::GeometryInstance *> &p_dynamic_objects, RendererSceneRenderRD *p_scene_render) {
  2893. VoxelGIInstance *voxel_gi = get_probe_instance(p_probe);
  2894. ERR_FAIL_COND(!voxel_gi);
  2895. voxel_gi->update(p_update_light_instances, p_light_instances, p_dynamic_objects, p_scene_render);
  2896. }
  2897. void RendererSceneGIRD::debug_voxel_gi(RID p_voxel_gi, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
  2898. VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_voxel_gi);
  2899. ERR_FAIL_COND(!voxel_gi);
  2900. voxel_gi->debug(p_draw_list, p_framebuffer, p_camera_with_transform, p_lighting, p_emission, p_alpha);
  2901. }