renderer_scene_gi_rd.cpp 131 KB

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