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