renderer_scene_gi_rd.cpp 130 KB

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