gi.cpp 157 KB

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