gi.cpp 157 KB

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