gi.cpp 151 KB

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