renderer_scene_gi_rd.cpp 131 KB

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