gi.cpp 157 KB

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