renderer_scene_gi_rd.cpp 131 KB

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