renderer_scene_gi_rd.cpp 131 KB

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