renderer_scene_gi_rd.cpp 132 KB

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