gi.cpp 154 KB

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