gi.cpp 150 KB

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