renderer_scene_gi_rd.cpp 131 KB

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