renderer_scene_gi_rd.cpp 132 KB

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