rasterizer_scene_rd.cpp 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. /*************************************************************************/
  2. /* rasterizer_scene_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "rasterizer_scene_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "servers/visual/visual_server_raster.h"
  34. uint64_t RasterizerSceneRD::auto_exposure_counter = 2;
  35. void RasterizerSceneRD::_clear_reflection_data(ReflectionData &rd) {
  36. rd.layers.clear();
  37. rd.radiance_base_cubemap = RID();
  38. if (rd.downsampled_radiance_cubemap.is_valid()) {
  39. RD::get_singleton()->free(rd.downsampled_radiance_cubemap);
  40. }
  41. rd.downsampled_radiance_cubemap = RID();
  42. rd.downsampled_layer.mipmaps.clear();
  43. rd.coefficient_buffer = RID();
  44. }
  45. void RasterizerSceneRD::_update_reflection_data(ReflectionData &rd, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer, bool p_low_quality) {
  46. //recreate radiance and all data
  47. int mipmaps = p_mipmaps;
  48. uint32_t w = p_size, h = p_size;
  49. if (p_use_array) {
  50. int layers = p_low_quality ? 7 : roughness_layers;
  51. for (int i = 0; i < layers; i++) {
  52. ReflectionData::Layer layer;
  53. uint32_t mmw = w;
  54. uint32_t mmh = h;
  55. layer.mipmaps.resize(mipmaps);
  56. layer.views.resize(mipmaps);
  57. for (int j = 0; j < mipmaps; j++) {
  58. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  59. mm.size.width = mmw;
  60. mm.size.height = mmh;
  61. for (int k = 0; k < 6; k++) {
  62. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  63. Vector<RID> fbtex;
  64. fbtex.push_back(mm.views[k]);
  65. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  66. }
  67. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6, j, RD::TEXTURE_SLICE_CUBEMAP);
  68. mmw = MAX(1, mmw >> 1);
  69. mmh = MAX(1, mmh >> 1);
  70. }
  71. rd.layers.push_back(layer);
  72. }
  73. } else {
  74. mipmaps = p_low_quality ? 7 : mipmaps;
  75. //regular cubemap, lower quality (aliasing, less memory)
  76. ReflectionData::Layer layer;
  77. uint32_t mmw = w;
  78. uint32_t mmh = h;
  79. layer.mipmaps.resize(mipmaps);
  80. layer.views.resize(mipmaps);
  81. for (int j = 0; j < mipmaps; j++) {
  82. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  83. mm.size.width = mmw;
  84. mm.size.height = mmh;
  85. for (int k = 0; k < 6; k++) {
  86. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  87. Vector<RID> fbtex;
  88. fbtex.push_back(mm.views[k]);
  89. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  90. }
  91. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, j, RD::TEXTURE_SLICE_CUBEMAP);
  92. mmw = MAX(1, mmw >> 1);
  93. mmh = MAX(1, mmh >> 1);
  94. }
  95. rd.layers.push_back(layer);
  96. }
  97. rd.radiance_base_cubemap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, 0, RD::TEXTURE_SLICE_CUBEMAP);
  98. RD::TextureFormat tf;
  99. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  100. tf.width = 64; // Always 64x64
  101. tf.height = 64;
  102. tf.type = RD::TEXTURE_TYPE_CUBE;
  103. tf.array_layers = 6;
  104. tf.mipmaps = 7;
  105. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  106. rd.downsampled_radiance_cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  107. {
  108. uint32_t mmw = 64;
  109. uint32_t mmh = 64;
  110. rd.downsampled_layer.mipmaps.resize(7);
  111. for (int j = 0; j < rd.downsampled_layer.mipmaps.size(); j++) {
  112. ReflectionData::DownsampleLayer::Mipmap &mm = rd.downsampled_layer.mipmaps.write[j];
  113. mm.size.width = mmw;
  114. mm.size.height = mmh;
  115. mm.view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rd.downsampled_radiance_cubemap, 0, j, RD::TEXTURE_SLICE_CUBEMAP);
  116. mmw = MAX(1, mmw >> 1);
  117. mmh = MAX(1, mmh >> 1);
  118. }
  119. }
  120. }
  121. void RasterizerSceneRD::_create_reflection_from_panorama(ReflectionData &rd, RID p_panorama, bool p_quality) {
  122. if (sky_use_cubemap_array) {
  123. if (p_quality) {
  124. //render directly to the layers
  125. for (int i = 0; i < rd.layers.size(); i++) {
  126. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[i].views[0], 10, sky_ggx_samples_quality, float(i) / (rd.layers.size() - 1.0), rd.layers[i].mipmaps[0].size.x);
  127. }
  128. } else {
  129. // Use fast filtering. Render directly to base mip levels
  130. storage->get_effects()->cubemap_downsample(p_panorama, true, rd.downsampled_layer.mipmaps[0].view, rd.downsampled_layer.mipmaps[0].size);
  131. for (int i = 1; i < rd.downsampled_layer.mipmaps.size(); i++) {
  132. storage->get_effects()->cubemap_downsample(rd.downsampled_layer.mipmaps[i - 1].view, false, rd.downsampled_layer.mipmaps[i].view, rd.downsampled_layer.mipmaps[i].size);
  133. }
  134. Vector<RID> views;
  135. for (int i = 0; i < rd.layers.size(); i++) {
  136. views.push_back(rd.layers[i].views[0]);
  137. }
  138. storage->get_effects()->cubemap_filter(rd.downsampled_radiance_cubemap, views, true);
  139. }
  140. } else {
  141. if (p_quality) {
  142. //render directly to the layers
  143. for (int i = 0; i < rd.layers[0].mipmaps.size(); i++) {
  144. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[0].views[i], 10, sky_ggx_samples_quality, float(i) / (rd.layers[0].mipmaps.size() - 1.0), rd.layers[0].mipmaps[i].size.x);
  145. }
  146. } else {
  147. // Use fast filtering. Render directly to each mip level
  148. storage->get_effects()->cubemap_downsample(p_panorama, true, rd.downsampled_layer.mipmaps[0].view, rd.downsampled_layer.mipmaps[0].size);
  149. for (int i = 1; i < rd.downsampled_layer.mipmaps.size(); i++) {
  150. storage->get_effects()->cubemap_downsample(rd.downsampled_layer.mipmaps[i - 1].view, false, rd.downsampled_layer.mipmaps[i].view, rd.downsampled_layer.mipmaps[i].size);
  151. }
  152. storage->get_effects()->cubemap_filter(rd.downsampled_radiance_cubemap, rd.layers[0].views, false);
  153. }
  154. }
  155. }
  156. void RasterizerSceneRD::_create_reflection_from_base_mipmap(ReflectionData &rd, bool p_use_arrays, bool p_quality, int p_cube_side, int p_base_layer) {
  157. if (p_use_arrays) {
  158. if (p_quality) {
  159. //render directly to the layers
  160. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[p_base_layer].views[0], p_cube_side, sky_ggx_samples_quality, float(p_base_layer) / (rd.layers.size() - 1.0), rd.layers[p_base_layer].mipmaps[0].size.x);
  161. } else {
  162. storage->get_effects()->cubemap_downsample(rd.radiance_base_cubemap, false, rd.downsampled_layer.mipmaps[0].view, rd.downsampled_layer.mipmaps[0].size);
  163. for (int i = 1; i < rd.downsampled_layer.mipmaps.size(); i++) {
  164. storage->get_effects()->cubemap_downsample(rd.downsampled_layer.mipmaps[i - 1].view, false, rd.downsampled_layer.mipmaps[i].view, rd.downsampled_layer.mipmaps[i].size);
  165. }
  166. Vector<RID> views;
  167. for (int i = 0; i < rd.layers.size(); i++) {
  168. views.push_back(rd.layers[i].views[0]);
  169. }
  170. storage->get_effects()->cubemap_filter(rd.downsampled_radiance_cubemap, views, true);
  171. }
  172. } else {
  173. if (p_quality) {
  174. storage->get_effects()->cubemap_roughness(rd.layers[0].views[p_base_layer - 1], false, rd.layers[0].views[p_base_layer], p_cube_side, sky_ggx_samples_quality, float(p_base_layer) / (rd.layers[0].mipmaps.size() - 1.0), rd.layers[0].mipmaps[p_base_layer].size.x);
  175. } else {
  176. storage->get_effects()->cubemap_downsample(rd.radiance_base_cubemap, false, rd.downsampled_layer.mipmaps[0].view, rd.downsampled_layer.mipmaps[0].size);
  177. for (int i = 1; i < rd.downsampled_layer.mipmaps.size(); i++) {
  178. storage->get_effects()->cubemap_downsample(rd.downsampled_layer.mipmaps[i - 1].view, false, rd.downsampled_layer.mipmaps[i].view, rd.downsampled_layer.mipmaps[i].size);
  179. }
  180. storage->get_effects()->cubemap_filter(rd.downsampled_radiance_cubemap, rd.layers[0].views, false);
  181. }
  182. }
  183. }
  184. void RasterizerSceneRD::_update_reflection_mipmaps(ReflectionData &rd, bool p_quality) {
  185. if (sky_use_cubemap_array) {
  186. for (int i = 0; i < rd.layers.size(); i++) {
  187. for (int j = 0; j < rd.layers[i].mipmaps.size() - 1; j++) {
  188. for (int k = 0; k < 6; k++) {
  189. RID view = rd.layers[i].mipmaps[j].views[k];
  190. RID fb = rd.layers[i].mipmaps[j + 1].framebuffers[k];
  191. Vector2 size = rd.layers[i].mipmaps[j].size;
  192. size = Vector2(1.0 / size.x, 1.0 / size.y);
  193. storage->get_effects()->make_mipmap(view, fb, size);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. RID RasterizerSceneRD::sky_create() {
  200. return sky_owner.make_rid(Sky());
  201. }
  202. void RasterizerSceneRD::_sky_invalidate(Sky *p_sky) {
  203. if (!p_sky->dirty) {
  204. p_sky->dirty = true;
  205. p_sky->dirty_list = dirty_sky_list;
  206. dirty_sky_list = p_sky;
  207. }
  208. }
  209. void RasterizerSceneRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  210. Sky *sky = sky_owner.getornull(p_sky);
  211. ERR_FAIL_COND(!sky);
  212. ERR_FAIL_COND(p_radiance_size < 32 || p_radiance_size > 2048);
  213. if (sky->radiance_size == p_radiance_size) {
  214. return;
  215. }
  216. sky->radiance_size = p_radiance_size;
  217. if (sky->mode == VS::SKY_MODE_REALTIME && sky->radiance_size != 128) {
  218. WARN_PRINT("Realtime Skies can only use a radiance size of 128. Radiance size will be set to 128 internally.");
  219. sky->radiance_size = 128;
  220. }
  221. _sky_invalidate(sky);
  222. if (sky->radiance.is_valid()) {
  223. RD::get_singleton()->free(sky->radiance);
  224. sky->radiance = RID();
  225. }
  226. _clear_reflection_data(sky->reflection);
  227. }
  228. void RasterizerSceneRD::sky_set_mode(RID p_sky, VS::SkyMode p_mode) {
  229. Sky *sky = sky_owner.getornull(p_sky);
  230. ERR_FAIL_COND(!sky);
  231. if (sky->mode == p_mode) {
  232. return;
  233. }
  234. sky->mode = p_mode;
  235. if (sky->mode == VS::SKY_MODE_REALTIME && sky->radiance_size != 128) {
  236. WARN_PRINT("Realtime Skies can only use a radiance size of 128. Radiance size will be set to 128 internally.");
  237. sky_set_radiance_size(p_sky, 128);
  238. }
  239. _sky_invalidate(sky);
  240. if (sky->radiance.is_valid()) {
  241. RD::get_singleton()->free(sky->radiance);
  242. sky->radiance = RID();
  243. }
  244. _clear_reflection_data(sky->reflection);
  245. }
  246. void RasterizerSceneRD::sky_set_texture(RID p_sky, RID p_panorama) {
  247. Sky *sky = sky_owner.getornull(p_sky);
  248. ERR_FAIL_COND(!sky);
  249. if (sky->panorama.is_valid()) {
  250. sky->panorama = RID();
  251. if (sky->radiance.is_valid()) {
  252. RD::get_singleton()->free(sky->radiance);
  253. sky->radiance = RID();
  254. }
  255. _clear_reflection_data(sky->reflection);
  256. }
  257. sky->panorama = p_panorama;
  258. if (!sky->panorama.is_valid())
  259. return; //cleared
  260. _sky_invalidate(sky);
  261. }
  262. void RasterizerSceneRD::_update_dirty_skys() {
  263. Sky *sky = dirty_sky_list;
  264. while (sky) {
  265. //update sky configuration if texture is missing
  266. if (sky->radiance.is_null()) {
  267. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  268. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  269. int layers = sky->mode == VS::SKY_MODE_REALTIME ? 7 : roughness_layers;
  270. if (sky_use_cubemap_array) {
  271. //array (higher quality, 6 times more memory)
  272. RD::TextureFormat tf;
  273. tf.array_layers = layers * 6;
  274. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  275. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  276. tf.mipmaps = mipmaps;
  277. tf.width = w;
  278. tf.height = h;
  279. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  280. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  281. _update_reflection_data(sky->reflection, sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == VS::SKY_MODE_REALTIME);
  282. } else {
  283. //regular cubemap, lower quality (aliasing, less memory)
  284. RD::TextureFormat tf;
  285. tf.array_layers = 6;
  286. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  287. tf.type = RD::TEXTURE_TYPE_CUBE;
  288. tf.mipmaps = MIN(mipmaps, layers);
  289. tf.width = w;
  290. tf.height = h;
  291. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  292. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  293. _update_reflection_data(sky->reflection, sky->radiance_size, MIN(mipmaps, layers), false, sky->radiance, 0, sky->mode == VS::SKY_MODE_REALTIME);
  294. }
  295. }
  296. RID panorama_texture = storage->texture_get_rd_texture(sky->panorama);
  297. if (panorama_texture.is_valid()) {
  298. //is there a panorama texture?
  299. _create_reflection_from_panorama(sky->reflection, panorama_texture, sky->mode == VS::SKY_MODE_QUALITY);
  300. _update_reflection_mipmaps(sky->reflection, sky->mode == VS::SKY_MODE_QUALITY);
  301. }
  302. Sky *next = sky->dirty_list;
  303. sky->dirty_list = nullptr;
  304. sky->dirty = false;
  305. sky = next;
  306. }
  307. dirty_sky_list = nullptr;
  308. }
  309. RID RasterizerSceneRD::sky_get_panorama_texture_rd(RID p_sky) const {
  310. Sky *sky = sky_owner.getornull(p_sky);
  311. ERR_FAIL_COND_V(!sky, RID());
  312. if (sky->panorama.is_null()) {
  313. return RID();
  314. }
  315. return storage->texture_get_rd_texture(sky->panorama, true);
  316. }
  317. RID RasterizerSceneRD::sky_get_radiance_texture_rd(RID p_sky) const {
  318. Sky *sky = sky_owner.getornull(p_sky);
  319. ERR_FAIL_COND_V(!sky, RID());
  320. return sky->radiance;
  321. }
  322. RID RasterizerSceneRD::sky_get_radiance_uniform_set_rd(RID p_sky, RID p_shader, int p_set) const {
  323. Sky *sky = sky_owner.getornull(p_sky);
  324. ERR_FAIL_COND_V(!sky, RID());
  325. if (sky->uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(sky->uniform_set)) {
  326. sky->uniform_set = RID();
  327. if (sky->radiance.is_valid()) {
  328. Vector<RD::Uniform> uniforms;
  329. {
  330. RD::Uniform u;
  331. u.type = RD::UNIFORM_TYPE_TEXTURE;
  332. u.binding = 0;
  333. u.ids.push_back(sky->radiance);
  334. uniforms.push_back(u);
  335. }
  336. sky->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  337. }
  338. }
  339. return sky->uniform_set;
  340. }
  341. RID RasterizerSceneRD::environment_create() {
  342. return environment_owner.make_rid(Environent());
  343. }
  344. void RasterizerSceneRD::environment_set_background(RID p_env, VS::EnvironmentBG p_bg) {
  345. Environent *env = environment_owner.getornull(p_env);
  346. ERR_FAIL_COND(!env);
  347. env->background = p_bg;
  348. }
  349. void RasterizerSceneRD::environment_set_sky(RID p_env, RID p_sky) {
  350. Environent *env = environment_owner.getornull(p_env);
  351. ERR_FAIL_COND(!env);
  352. env->sky = p_sky;
  353. }
  354. void RasterizerSceneRD::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  355. Environent *env = environment_owner.getornull(p_env);
  356. ERR_FAIL_COND(!env);
  357. env->sky_custom_fov = p_scale;
  358. }
  359. void RasterizerSceneRD::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  360. Environent *env = environment_owner.getornull(p_env);
  361. ERR_FAIL_COND(!env);
  362. env->sky_orientation = p_orientation;
  363. }
  364. void RasterizerSceneRD::environment_set_bg_material(RID p_env, RID p_material) {
  365. Environent *env = environment_owner.getornull(p_env);
  366. ERR_FAIL_COND(!env);
  367. env->bg_material = p_material;
  368. }
  369. void RasterizerSceneRD::environment_set_bg_color(RID p_env, const Color &p_color) {
  370. Environent *env = environment_owner.getornull(p_env);
  371. ERR_FAIL_COND(!env);
  372. env->bg_color = p_color;
  373. }
  374. void RasterizerSceneRD::environment_set_bg_energy(RID p_env, float p_energy) {
  375. Environent *env = environment_owner.getornull(p_env);
  376. ERR_FAIL_COND(!env);
  377. env->bg_energy = p_energy;
  378. }
  379. void RasterizerSceneRD::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  380. Environent *env = environment_owner.getornull(p_env);
  381. ERR_FAIL_COND(!env);
  382. env->canvas_max_layer = p_max_layer;
  383. }
  384. void RasterizerSceneRD::environment_set_ambient_light(RID p_env, const Color &p_color, VS::EnvironmentAmbientSource p_ambient, float p_energy, float p_sky_contribution, VS::EnvironmentReflectionSource p_reflection_source, const Color &p_ao_color) {
  385. Environent *env = environment_owner.getornull(p_env);
  386. ERR_FAIL_COND(!env);
  387. env->ambient_light = p_color;
  388. env->ambient_source = p_ambient;
  389. env->ambient_light_energy = p_energy;
  390. env->ambient_sky_contribution = p_sky_contribution;
  391. env->reflection_source = p_reflection_source;
  392. env->ao_color = p_ao_color;
  393. }
  394. VS::EnvironmentBG RasterizerSceneRD::environment_get_background(RID p_env) const {
  395. Environent *env = environment_owner.getornull(p_env);
  396. ERR_FAIL_COND_V(!env, VS::ENV_BG_MAX);
  397. return env->background;
  398. }
  399. RID RasterizerSceneRD::environment_get_sky(RID p_env) const {
  400. Environent *env = environment_owner.getornull(p_env);
  401. ERR_FAIL_COND_V(!env, RID());
  402. return env->sky;
  403. }
  404. float RasterizerSceneRD::environment_get_sky_custom_fov(RID p_env) const {
  405. Environent *env = environment_owner.getornull(p_env);
  406. ERR_FAIL_COND_V(!env, 0);
  407. return env->sky_custom_fov;
  408. }
  409. Basis RasterizerSceneRD::environment_get_sky_orientation(RID p_env) const {
  410. Environent *env = environment_owner.getornull(p_env);
  411. ERR_FAIL_COND_V(!env, Basis());
  412. return env->sky_orientation;
  413. }
  414. RID RasterizerSceneRD::environment_get_bg_material(RID p_env) const {
  415. Environent *env = environment_owner.getornull(p_env);
  416. ERR_FAIL_COND_V(!env, RID());
  417. return env->bg_material;
  418. }
  419. Color RasterizerSceneRD::environment_get_bg_color(RID p_env) const {
  420. Environent *env = environment_owner.getornull(p_env);
  421. ERR_FAIL_COND_V(!env, Color());
  422. return env->bg_color;
  423. }
  424. float RasterizerSceneRD::environment_get_bg_energy(RID p_env) const {
  425. Environent *env = environment_owner.getornull(p_env);
  426. ERR_FAIL_COND_V(!env, 0);
  427. return env->bg_energy;
  428. }
  429. int RasterizerSceneRD::environment_get_canvas_max_layer(RID p_env) const {
  430. Environent *env = environment_owner.getornull(p_env);
  431. ERR_FAIL_COND_V(!env, 0);
  432. return env->canvas_max_layer;
  433. }
  434. Color RasterizerSceneRD::environment_get_ambient_light_color(RID p_env) const {
  435. Environent *env = environment_owner.getornull(p_env);
  436. ERR_FAIL_COND_V(!env, Color());
  437. return env->ambient_light;
  438. }
  439. VS::EnvironmentAmbientSource RasterizerSceneRD::environment_get_ambient_light_ambient_source(RID p_env) const {
  440. Environent *env = environment_owner.getornull(p_env);
  441. ERR_FAIL_COND_V(!env, VS::ENV_AMBIENT_SOURCE_BG);
  442. return env->ambient_source;
  443. }
  444. float RasterizerSceneRD::environment_get_ambient_light_ambient_energy(RID p_env) const {
  445. Environent *env = environment_owner.getornull(p_env);
  446. ERR_FAIL_COND_V(!env, 0);
  447. return env->ambient_light_energy;
  448. }
  449. float RasterizerSceneRD::environment_get_ambient_sky_contribution(RID p_env) const {
  450. Environent *env = environment_owner.getornull(p_env);
  451. ERR_FAIL_COND_V(!env, 0);
  452. return env->ambient_sky_contribution;
  453. }
  454. VS::EnvironmentReflectionSource RasterizerSceneRD::environment_get_reflection_source(RID p_env) const {
  455. Environent *env = environment_owner.getornull(p_env);
  456. ERR_FAIL_COND_V(!env, VS::ENV_REFLECTION_SOURCE_DISABLED);
  457. return env->reflection_source;
  458. }
  459. Color RasterizerSceneRD::environment_get_ao_color(RID p_env) const {
  460. Environent *env = environment_owner.getornull(p_env);
  461. ERR_FAIL_COND_V(!env, Color());
  462. return env->ao_color;
  463. }
  464. void RasterizerSceneRD::environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) {
  465. Environent *env = environment_owner.getornull(p_env);
  466. ERR_FAIL_COND(!env);
  467. env->exposure = p_exposure;
  468. env->tone_mapper = p_tone_mapper;
  469. if (!env->auto_exposure && p_auto_exposure) {
  470. env->auto_exposure_version = ++auto_exposure_counter;
  471. }
  472. env->auto_exposure = p_auto_exposure;
  473. env->white = p_white;
  474. env->min_luminance = p_min_luminance;
  475. env->max_luminance = p_max_luminance;
  476. env->auto_exp_speed = p_auto_exp_speed;
  477. env->auto_exp_scale = p_auto_exp_scale;
  478. }
  479. void RasterizerSceneRD::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) {
  480. Environent *env = environment_owner.getornull(p_env);
  481. ERR_FAIL_COND(!env);
  482. env->glow_enabled = p_enable;
  483. env->glow_levels = p_level_flags;
  484. env->glow_intensity = p_intensity;
  485. env->glow_strength = p_strength;
  486. env->glow_mix = p_mix;
  487. env->glow_bloom = p_bloom_threshold;
  488. env->glow_blend_mode = p_blend_mode;
  489. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  490. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  491. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  492. env->glow_bicubic_upscale = p_bicubic_upscale;
  493. }
  494. void RasterizerSceneRD::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_bias, float p_light_affect, float p_ao_channel_affect, VS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) {
  495. Environent *env = environment_owner.getornull(p_env);
  496. ERR_FAIL_COND(!env);
  497. env->ssao_enabled = p_enable;
  498. env->ssao_radius = p_radius;
  499. env->ssao_intensity = p_intensity;
  500. env->ssao_bias = p_bias;
  501. env->ssao_direct_light_affect = p_light_affect;
  502. env->ssao_ao_channel_affect = p_ao_channel_affect;
  503. env->ssao_blur = p_blur;
  504. }
  505. void RasterizerSceneRD::environment_set_ssao_quality(VS::EnvironmentSSAOQuality p_quality, bool p_half_size) {
  506. ssao_quality = p_quality;
  507. ssao_half_size = p_half_size;
  508. }
  509. bool RasterizerSceneRD::environment_is_ssao_enabled(RID p_env) const {
  510. Environent *env = environment_owner.getornull(p_env);
  511. ERR_FAIL_COND_V(!env, false);
  512. return env->ssao_enabled;
  513. }
  514. float RasterizerSceneRD::environment_get_ssao_ao_affect(RID p_env) const {
  515. Environent *env = environment_owner.getornull(p_env);
  516. ERR_FAIL_COND_V(!env, false);
  517. return env->ssao_ao_channel_affect;
  518. }
  519. float RasterizerSceneRD::environment_get_ssao_light_affect(RID p_env) const {
  520. Environent *env = environment_owner.getornull(p_env);
  521. ERR_FAIL_COND_V(!env, false);
  522. return env->ssao_direct_light_affect;
  523. }
  524. bool RasterizerSceneRD::environment_is_ssr_enabled(RID p_env) const {
  525. Environent *env = environment_owner.getornull(p_env);
  526. ERR_FAIL_COND_V(!env, false);
  527. return false;
  528. }
  529. bool RasterizerSceneRD::is_environment(RID p_env) const {
  530. return environment_owner.owns(p_env);
  531. }
  532. ////////////////////////////////////////////////////////////
  533. RID RasterizerSceneRD::reflection_atlas_create() {
  534. ReflectionAtlas ra;
  535. ra.count = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_count");
  536. ra.size = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_size");
  537. return reflection_atlas_owner.make_rid(ra);
  538. }
  539. void RasterizerSceneRD::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  540. ReflectionAtlas *ra = reflection_atlas_owner.getornull(p_ref_atlas);
  541. ERR_FAIL_COND(!ra);
  542. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  543. return; //no changes
  544. }
  545. ra->size = p_reflection_size;
  546. ra->count = p_reflection_count;
  547. if (ra->reflection.is_valid()) {
  548. //clear and invalidate everything
  549. RD::get_singleton()->free(ra->reflection);
  550. ra->reflection = RID();
  551. RD::get_singleton()->free(ra->depth_buffer);
  552. ra->depth_buffer = RID();
  553. for (int i = 0; i < ra->reflections.size(); i++) {
  554. _clear_reflection_data(ra->reflections.write[i].data);
  555. if (ra->reflections[i].owner.is_null()) {
  556. continue;
  557. }
  558. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  559. //rp->atlasindex clear
  560. }
  561. ra->reflections.clear();
  562. }
  563. }
  564. ////////////////////////
  565. RID RasterizerSceneRD::reflection_probe_instance_create(RID p_probe) {
  566. ReflectionProbeInstance rpi;
  567. rpi.probe = p_probe;
  568. return reflection_probe_instance_owner.make_rid(rpi);
  569. }
  570. void RasterizerSceneRD::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  571. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  572. ERR_FAIL_COND(!rpi);
  573. rpi->transform = p_transform;
  574. rpi->dirty = true;
  575. }
  576. void RasterizerSceneRD::reflection_probe_release_atlas_index(RID p_instance) {
  577. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  578. ERR_FAIL_COND(!rpi);
  579. if (rpi->atlas.is_null()) {
  580. return; //nothing to release
  581. }
  582. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  583. ERR_FAIL_COND(!atlas);
  584. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  585. atlas->reflections.write[rpi->atlas_index].owner = RID();
  586. rpi->atlas_index = -1;
  587. rpi->atlas = RID();
  588. }
  589. bool RasterizerSceneRD::reflection_probe_instance_needs_redraw(RID p_instance) {
  590. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  591. ERR_FAIL_COND_V(!rpi, false);
  592. if (rpi->rendering) {
  593. return false;
  594. }
  595. if (rpi->dirty) {
  596. return true;
  597. }
  598. if (storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  599. return true;
  600. }
  601. return rpi->atlas_index == -1;
  602. }
  603. bool RasterizerSceneRD::reflection_probe_instance_has_reflection(RID p_instance) {
  604. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  605. ERR_FAIL_COND_V(!rpi, false);
  606. return rpi->atlas.is_valid();
  607. }
  608. bool RasterizerSceneRD::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  609. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  610. ERR_FAIL_COND_V(!atlas, false);
  611. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  612. ERR_FAIL_COND_V(!rpi, false);
  613. if (storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->size != 128) {
  614. WARN_PRINT("ReflectionProbes set to UPDATE_ALWAYS must have an atlas size of 128. Please update the atlas size in the ProjectSettings.");
  615. reflection_atlas_set_size(p_reflection_atlas, 128, atlas->count);
  616. }
  617. if (storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->reflections[0].data.layers[0].mipmaps.size() != 7) {
  618. // Invalidate reflection atlas, need to regenerate
  619. RD::get_singleton()->free(atlas->reflection);
  620. atlas->reflection = RID();
  621. for (int i = 0; i < atlas->reflections.size(); i++) {
  622. if (atlas->reflections[i].owner.is_null()) {
  623. continue;
  624. }
  625. reflection_probe_release_atlas_index(atlas->reflections[i].owner);
  626. }
  627. atlas->reflections.clear();
  628. }
  629. if (atlas->reflection.is_null()) {
  630. int mipmaps = MIN(roughness_layers, Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) + 1);
  631. mipmaps = storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS ? 7 : mipmaps; // always use 7 mipmaps with real time filtering
  632. {
  633. //reflection atlas was unused, create:
  634. RD::TextureFormat tf;
  635. tf.array_layers = 6 * atlas->count;
  636. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  637. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  638. tf.mipmaps = mipmaps;
  639. tf.width = atlas->size;
  640. tf.height = atlas->size;
  641. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  642. atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
  643. }
  644. {
  645. RD::TextureFormat tf;
  646. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  647. tf.width = atlas->size;
  648. tf.height = atlas->size;
  649. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  650. atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  651. }
  652. atlas->reflections.resize(atlas->count);
  653. for (int i = 0; i < atlas->count; i++) {
  654. _update_reflection_data(atlas->reflections.write[i].data, atlas->size, mipmaps, false, atlas->reflection, i * 6, storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS);
  655. for (int j = 0; j < 6; j++) {
  656. Vector<RID> fb;
  657. fb.push_back(atlas->reflections.write[i].data.layers[0].mipmaps[0].views[j]);
  658. fb.push_back(atlas->depth_buffer);
  659. atlas->reflections.write[i].fbs[j] = RD::get_singleton()->framebuffer_create(fb);
  660. }
  661. }
  662. Vector<RID> fb;
  663. fb.push_back(atlas->depth_buffer);
  664. atlas->depth_fb = RD::get_singleton()->framebuffer_create(fb);
  665. }
  666. if (rpi->atlas_index == -1) {
  667. for (int i = 0; i < atlas->reflections.size(); i++) {
  668. if (atlas->reflections[i].owner.is_null()) {
  669. rpi->atlas_index = i;
  670. break;
  671. }
  672. }
  673. //find the one used last
  674. if (rpi->atlas_index == -1) {
  675. //everything is in use, find the one least used via LRU
  676. uint64_t pass_min = 0;
  677. for (int i = 0; i < atlas->reflections.size(); i++) {
  678. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.getornull(atlas->reflections[i].owner);
  679. if (rpi2->last_pass < pass_min) {
  680. pass_min = rpi2->last_pass;
  681. rpi->atlas_index = i;
  682. }
  683. }
  684. }
  685. }
  686. rpi->atlas = p_reflection_atlas;
  687. rpi->rendering = true;
  688. rpi->dirty = false;
  689. rpi->processing_layer = 1;
  690. rpi->processing_side = 0;
  691. return true;
  692. }
  693. bool RasterizerSceneRD::reflection_probe_instance_postprocess_step(RID p_instance) {
  694. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  695. ERR_FAIL_COND_V(!rpi, false);
  696. ERR_FAIL_COND_V(!rpi->rendering, false);
  697. ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
  698. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  699. if (!atlas || rpi->atlas_index == -1) {
  700. //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
  701. rpi->rendering = false;
  702. return false;
  703. }
  704. if (rpi->processing_layer > 1) {
  705. _create_reflection_from_base_mipmap(atlas->reflections.write[rpi->atlas_index].data, false, storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ONCE, 10, rpi->processing_layer);
  706. rpi->processing_layer++;
  707. if (rpi->processing_layer == atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
  708. rpi->rendering = false;
  709. rpi->processing_side = 0;
  710. rpi->processing_layer = 1;
  711. return true;
  712. }
  713. return false;
  714. } else {
  715. _create_reflection_from_base_mipmap(atlas->reflections.write[rpi->atlas_index].data, false, storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ONCE, rpi->processing_side, rpi->processing_layer);
  716. }
  717. if (storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  718. // Using real time reflections, all roughness is done in one step
  719. rpi->rendering = false;
  720. rpi->processing_side = 0;
  721. rpi->processing_layer = 1;
  722. return true;
  723. }
  724. rpi->processing_side++;
  725. if (rpi->processing_side == 6) {
  726. rpi->processing_side = 0;
  727. rpi->processing_layer++;
  728. }
  729. return false;
  730. }
  731. uint32_t RasterizerSceneRD::reflection_probe_instance_get_resolution(RID p_instance) {
  732. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  733. ERR_FAIL_COND_V(!rpi, 0);
  734. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  735. ERR_FAIL_COND_V(!atlas, 0);
  736. return atlas->size;
  737. }
  738. RID RasterizerSceneRD::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  739. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  740. ERR_FAIL_COND_V(!rpi, RID());
  741. ERR_FAIL_INDEX_V(p_index, 6, RID());
  742. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  743. ERR_FAIL_COND_V(!atlas, RID());
  744. return atlas->reflections[rpi->atlas_index].fbs[p_index];
  745. }
  746. RID RasterizerSceneRD::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
  747. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  748. ERR_FAIL_COND_V(!rpi, RID());
  749. ERR_FAIL_INDEX_V(p_index, 6, RID());
  750. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  751. ERR_FAIL_COND_V(!atlas, RID());
  752. return atlas->depth_fb;
  753. }
  754. ///////////////////////////////////////////////////////////
  755. RID RasterizerSceneRD::shadow_atlas_create() {
  756. return shadow_atlas_owner.make_rid(ShadowAtlas());
  757. }
  758. void RasterizerSceneRD::shadow_atlas_set_size(RID p_atlas, int p_size) {
  759. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  760. ERR_FAIL_COND(!shadow_atlas);
  761. ERR_FAIL_COND(p_size < 0);
  762. p_size = next_power_of_2(p_size);
  763. if (p_size == shadow_atlas->size)
  764. return;
  765. // erasing atlas
  766. if (shadow_atlas->depth.is_valid()) {
  767. RD::get_singleton()->free(shadow_atlas->depth);
  768. shadow_atlas->depth = RID();
  769. shadow_atlas->fb = RID();
  770. }
  771. for (int i = 0; i < 4; i++) {
  772. //clear subdivisions
  773. shadow_atlas->quadrants[i].shadows.resize(0);
  774. shadow_atlas->quadrants[i].shadows.resize(1 << shadow_atlas->quadrants[i].subdivision);
  775. }
  776. //erase shadow atlas reference from lights
  777. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  778. LightInstance *li = light_instance_owner.getornull(E->key());
  779. ERR_CONTINUE(!li);
  780. li->shadow_atlases.erase(p_atlas);
  781. }
  782. //clear owners
  783. shadow_atlas->shadow_owners.clear();
  784. shadow_atlas->size = p_size;
  785. if (shadow_atlas->size) {
  786. RD::TextureFormat tf;
  787. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  788. tf.width = shadow_atlas->size;
  789. tf.height = shadow_atlas->size;
  790. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  791. shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  792. Vector<RID> fb;
  793. fb.push_back(shadow_atlas->depth);
  794. shadow_atlas->fb = RD::get_singleton()->framebuffer_create(fb);
  795. }
  796. }
  797. void RasterizerSceneRD::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  798. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  799. ERR_FAIL_COND(!shadow_atlas);
  800. ERR_FAIL_INDEX(p_quadrant, 4);
  801. ERR_FAIL_INDEX(p_subdivision, 16384);
  802. uint32_t subdiv = next_power_of_2(p_subdivision);
  803. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  804. subdiv <<= 1;
  805. }
  806. subdiv = int(Math::sqrt((float)subdiv));
  807. //obtain the number that will be x*x
  808. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv)
  809. return;
  810. //erase all data from quadrant
  811. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  812. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  813. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  814. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  815. ERR_CONTINUE(!li);
  816. li->shadow_atlases.erase(p_atlas);
  817. }
  818. }
  819. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  820. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  821. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  822. //cache the smallest subdiv (for faster allocation in light update)
  823. shadow_atlas->smallest_subdiv = 1 << 30;
  824. for (int i = 0; i < 4; i++) {
  825. if (shadow_atlas->quadrants[i].subdivision) {
  826. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  827. }
  828. }
  829. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  830. shadow_atlas->smallest_subdiv = 0;
  831. }
  832. //resort the size orders, simple bublesort for 4 elements..
  833. int swaps = 0;
  834. do {
  835. swaps = 0;
  836. for (int i = 0; i < 3; i++) {
  837. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  838. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  839. swaps++;
  840. }
  841. }
  842. } while (swaps > 0);
  843. }
  844. bool RasterizerSceneRD::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
  845. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  846. int qidx = p_in_quadrants[i];
  847. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  848. return false;
  849. }
  850. //look for an empty space
  851. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  852. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  853. int found_free_idx = -1; //found a free one
  854. int found_used_idx = -1; //found existing one, must steal it
  855. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  856. for (int j = 0; j < sc; j++) {
  857. if (!sarr[j].owner.is_valid()) {
  858. found_free_idx = j;
  859. break;
  860. }
  861. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  862. ERR_CONTINUE(!sli);
  863. if (sli->last_scene_pass != scene_pass) {
  864. //was just allocated, don't kill it so soon, wait a bit..
  865. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec)
  866. continue;
  867. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  868. found_used_idx = j;
  869. min_pass = sli->last_scene_pass;
  870. }
  871. }
  872. }
  873. if (found_free_idx == -1 && found_used_idx == -1)
  874. continue; //nothing found
  875. if (found_free_idx == -1 && found_used_idx != -1) {
  876. found_free_idx = found_used_idx;
  877. }
  878. r_quadrant = qidx;
  879. r_shadow = found_free_idx;
  880. return true;
  881. }
  882. return false;
  883. }
  884. bool RasterizerSceneRD::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  885. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  886. ERR_FAIL_COND_V(!shadow_atlas, false);
  887. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  888. ERR_FAIL_COND_V(!li, false);
  889. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  890. return false;
  891. }
  892. uint32_t quad_size = shadow_atlas->size >> 1;
  893. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  894. int valid_quadrants[4];
  895. int valid_quadrant_count = 0;
  896. int best_size = -1; //best size found
  897. int best_subdiv = -1; //subdiv for the best size
  898. //find the quadrants this fits into, and the best possible size it can fit into
  899. for (int i = 0; i < 4; i++) {
  900. int q = shadow_atlas->size_order[i];
  901. int sd = shadow_atlas->quadrants[q].subdivision;
  902. if (sd == 0)
  903. continue; //unused
  904. int max_fit = quad_size / sd;
  905. if (best_size != -1 && max_fit > best_size)
  906. break; //too large
  907. valid_quadrants[valid_quadrant_count++] = q;
  908. best_subdiv = sd;
  909. if (max_fit >= desired_fit) {
  910. best_size = max_fit;
  911. }
  912. }
  913. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  914. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  915. //see if it already exists
  916. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  917. //it does!
  918. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  919. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  920. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  921. bool should_realloc = shadow_atlas->quadrants[q].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[q].shadows[s].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  922. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  923. if (!should_realloc) {
  924. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  925. //already existing, see if it should redraw or it's just OK
  926. return should_redraw;
  927. }
  928. int new_quadrant, new_shadow;
  929. //find a better place
  930. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  931. //found a better place!
  932. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  933. if (sh->owner.is_valid()) {
  934. //is taken, but is invalid, erasing it
  935. shadow_atlas->shadow_owners.erase(sh->owner);
  936. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  937. sli->shadow_atlases.erase(p_atlas);
  938. }
  939. //erase previous
  940. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  941. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  942. sh->owner = p_light_intance;
  943. sh->alloc_tick = tick;
  944. sh->version = p_light_version;
  945. li->shadow_atlases.insert(p_atlas);
  946. //make new key
  947. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  948. key |= new_shadow;
  949. //update it in map
  950. shadow_atlas->shadow_owners[p_light_intance] = key;
  951. //make it dirty, as it should redraw anyway
  952. return true;
  953. }
  954. //no better place for this shadow found, keep current
  955. //already existing, see if it should redraw or it's just OK
  956. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  957. return should_redraw;
  958. }
  959. int new_quadrant, new_shadow;
  960. //find a better place
  961. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  962. //found a better place!
  963. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  964. if (sh->owner.is_valid()) {
  965. //is taken, but is invalid, erasing it
  966. shadow_atlas->shadow_owners.erase(sh->owner);
  967. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  968. sli->shadow_atlases.erase(p_atlas);
  969. }
  970. sh->owner = p_light_intance;
  971. sh->alloc_tick = tick;
  972. sh->version = p_light_version;
  973. li->shadow_atlases.insert(p_atlas);
  974. //make new key
  975. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  976. key |= new_shadow;
  977. //update it in map
  978. shadow_atlas->shadow_owners[p_light_intance] = key;
  979. //make it dirty, as it should redraw anyway
  980. return true;
  981. }
  982. //no place to allocate this light, apologies
  983. return false;
  984. }
  985. void RasterizerSceneRD::directional_shadow_atlas_set_size(int p_size) {
  986. p_size = nearest_power_of_2_templated(p_size);
  987. if (directional_shadow.size == p_size) {
  988. return;
  989. }
  990. directional_shadow.size = p_size;
  991. if (directional_shadow.depth.is_valid()) {
  992. RD::get_singleton()->free(directional_shadow.depth);
  993. directional_shadow.depth = RID();
  994. directional_shadow.fb = RID();
  995. }
  996. if (p_size > 0) {
  997. RD::TextureFormat tf;
  998. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  999. tf.width = p_size;
  1000. tf.height = p_size;
  1001. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1002. directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1003. Vector<RID> fb;
  1004. fb.push_back(directional_shadow.depth);
  1005. directional_shadow.fb = RD::get_singleton()->framebuffer_create(fb);
  1006. }
  1007. _base_uniforms_changed();
  1008. }
  1009. void RasterizerSceneRD::set_directional_shadow_count(int p_count) {
  1010. directional_shadow.light_count = p_count;
  1011. directional_shadow.current_light = 0;
  1012. }
  1013. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  1014. int split_h = 1;
  1015. int split_v = 1;
  1016. while (split_h * split_v < p_shadow_count) {
  1017. if (split_h == split_v) {
  1018. split_h <<= 1;
  1019. } else {
  1020. split_v <<= 1;
  1021. }
  1022. }
  1023. Rect2i rect(0, 0, p_size, p_size);
  1024. rect.size.width /= split_h;
  1025. rect.size.height /= split_v;
  1026. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  1027. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  1028. return rect;
  1029. }
  1030. int RasterizerSceneRD::get_directional_light_shadow_size(RID p_light_intance) {
  1031. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  1032. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  1033. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  1034. ERR_FAIL_COND_V(!light_instance, 0);
  1035. switch (storage->light_directional_get_shadow_mode(light_instance->light)) {
  1036. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1037. break; //none
  1038. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: r.size.height /= 2; break;
  1039. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: r.size /= 2; break;
  1040. }
  1041. return MAX(r.size.width, r.size.height);
  1042. }
  1043. //////////////////////////////////////////////////
  1044. RID RasterizerSceneRD::camera_effects_create() {
  1045. return camera_effects_owner.make_rid(CameraEffects());
  1046. }
  1047. void RasterizerSceneRD::camera_effects_set_dof_blur_quality(VS::DOFBlurQuality p_quality, bool p_use_jitter) {
  1048. dof_blur_quality = p_quality;
  1049. dof_blur_use_jitter = p_use_jitter;
  1050. }
  1051. void RasterizerSceneRD::camera_effects_set_dof_blur_bokeh_shape(VS::DOFBokehShape p_shape) {
  1052. dof_blur_bokeh_shape = p_shape;
  1053. }
  1054. void RasterizerSceneRD::camera_effects_set_dof_blur(RID p_camera_effects, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount) {
  1055. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  1056. ERR_FAIL_COND(!camfx);
  1057. camfx->dof_blur_far_enabled = p_far_enable;
  1058. camfx->dof_blur_far_distance = p_far_distance;
  1059. camfx->dof_blur_far_transition = p_far_transition;
  1060. camfx->dof_blur_near_enabled = p_near_enable;
  1061. camfx->dof_blur_near_distance = p_near_distance;
  1062. camfx->dof_blur_near_transition = p_near_transition;
  1063. camfx->dof_blur_amount = p_amount;
  1064. }
  1065. void RasterizerSceneRD::camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure) {
  1066. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  1067. ERR_FAIL_COND(!camfx);
  1068. camfx->override_exposure_enabled = p_enable;
  1069. camfx->override_exposure = p_exposure;
  1070. }
  1071. RID RasterizerSceneRD::light_instance_create(RID p_light) {
  1072. RID li = light_instance_owner.make_rid(LightInstance());
  1073. LightInstance *light_instance = light_instance_owner.getornull(li);
  1074. light_instance->self = li;
  1075. light_instance->light = p_light;
  1076. light_instance->light_type = storage->light_get_type(p_light);
  1077. return li;
  1078. }
  1079. void RasterizerSceneRD::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  1080. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  1081. ERR_FAIL_COND(!light_instance);
  1082. light_instance->transform = p_transform;
  1083. }
  1084. void RasterizerSceneRD::light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale) {
  1085. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  1086. ERR_FAIL_COND(!light_instance);
  1087. if (storage->light_get_type(light_instance->light) != VS::LIGHT_DIRECTIONAL) {
  1088. p_pass = 0;
  1089. }
  1090. ERR_FAIL_INDEX(p_pass, 4);
  1091. light_instance->shadow_transform[p_pass].camera = p_projection;
  1092. light_instance->shadow_transform[p_pass].transform = p_transform;
  1093. light_instance->shadow_transform[p_pass].farplane = p_far;
  1094. light_instance->shadow_transform[p_pass].split = p_split;
  1095. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  1096. }
  1097. void RasterizerSceneRD::light_instance_mark_visible(RID p_light_instance) {
  1098. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  1099. ERR_FAIL_COND(!light_instance);
  1100. light_instance->last_scene_pass = scene_pass;
  1101. }
  1102. RasterizerSceneRD::ShadowCubemap *RasterizerSceneRD::_get_shadow_cubemap(int p_size) {
  1103. if (!shadow_cubemaps.has(p_size)) {
  1104. ShadowCubemap sc;
  1105. {
  1106. RD::TextureFormat tf;
  1107. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  1108. tf.width = p_size;
  1109. tf.height = p_size;
  1110. tf.type = RD::TEXTURE_TYPE_CUBE;
  1111. tf.array_layers = 6;
  1112. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1113. sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1114. }
  1115. for (int i = 0; i < 6; i++) {
  1116. RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
  1117. Vector<RID> fbtex;
  1118. fbtex.push_back(side_texture);
  1119. sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
  1120. }
  1121. shadow_cubemaps[p_size] = sc;
  1122. }
  1123. return &shadow_cubemaps[p_size];
  1124. }
  1125. RasterizerSceneRD::ShadowMap *RasterizerSceneRD::_get_shadow_map(const Size2i &p_size) {
  1126. if (!shadow_maps.has(p_size)) {
  1127. ShadowMap sm;
  1128. {
  1129. RD::TextureFormat tf;
  1130. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  1131. tf.width = p_size.width;
  1132. tf.height = p_size.height;
  1133. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1134. sm.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1135. }
  1136. Vector<RID> fbtex;
  1137. fbtex.push_back(sm.depth);
  1138. sm.fb = RD::get_singleton()->framebuffer_create(fbtex);
  1139. shadow_maps[p_size] = sm;
  1140. }
  1141. return &shadow_maps[p_size];
  1142. }
  1143. /////////////////////////////////
  1144. RID RasterizerSceneRD::gi_probe_instance_create(RID p_base) {
  1145. //find a free slot
  1146. int index = -1;
  1147. for (int i = 0; i < gi_probe_slots.size(); i++) {
  1148. if (gi_probe_slots[i] == RID()) {
  1149. index = i;
  1150. break;
  1151. }
  1152. }
  1153. ERR_FAIL_COND_V(index == -1, RID());
  1154. GIProbeInstance gi_probe;
  1155. gi_probe.slot = index;
  1156. gi_probe.probe = p_base;
  1157. RID rid = gi_probe_instance_owner.make_rid(gi_probe);
  1158. gi_probe_slots.write[index] = rid;
  1159. return rid;
  1160. }
  1161. void RasterizerSceneRD::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  1162. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1163. ERR_FAIL_COND(!gi_probe);
  1164. gi_probe->transform = p_xform;
  1165. }
  1166. bool RasterizerSceneRD::gi_probe_needs_update(RID p_probe) const {
  1167. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1168. ERR_FAIL_COND_V(!gi_probe, false);
  1169. //return true;
  1170. return gi_probe->last_probe_version != storage->gi_probe_get_version(gi_probe->probe);
  1171. }
  1172. void RasterizerSceneRD::gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects) {
  1173. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1174. ERR_FAIL_COND(!gi_probe);
  1175. uint32_t data_version = storage->gi_probe_get_data_version(gi_probe->probe);
  1176. // (RE)CREATE IF NEEDED
  1177. if (gi_probe->last_probe_data_version != data_version) {
  1178. //need to re-create everything
  1179. if (gi_probe->texture.is_valid()) {
  1180. RD::get_singleton()->free(gi_probe->texture);
  1181. if (gi_probe_use_anisotropy) {
  1182. RD::get_singleton()->free(gi_probe->anisotropy_r16[0]);
  1183. RD::get_singleton()->free(gi_probe->anisotropy_r16[1]);
  1184. }
  1185. RD::get_singleton()->free(gi_probe->write_buffer);
  1186. gi_probe->mipmaps.clear();
  1187. }
  1188. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  1189. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  1190. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  1191. }
  1192. gi_probe->dynamic_maps.clear();
  1193. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1194. if (octree_size != Vector3i()) {
  1195. //can create a 3D texture
  1196. Vector<int> levels = storage->gi_probe_get_level_counts(gi_probe->probe);
  1197. RD::TextureFormat tf;
  1198. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1199. tf.width = octree_size.x;
  1200. tf.height = octree_size.y;
  1201. tf.depth = octree_size.z;
  1202. tf.type = RD::TEXTURE_TYPE_3D;
  1203. tf.mipmaps = levels.size();
  1204. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1205. gi_probe->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1206. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1207. if (gi_probe_use_anisotropy) {
  1208. tf.format = RD::DATA_FORMAT_R16_UINT;
  1209. tf.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
  1210. tf.shareable_formats.push_back(RD::DATA_FORMAT_R5G6B5_UNORM_PACK16);
  1211. //need to create R16 first, else driver does not like the storage bit for compute..
  1212. gi_probe->anisotropy_r16[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1213. gi_probe->anisotropy_r16[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1214. RD::TextureView tv;
  1215. tv.format_override = RD::DATA_FORMAT_R5G6B5_UNORM_PACK16;
  1216. gi_probe->anisotropy[0] = RD::get_singleton()->texture_create_shared(tv, gi_probe->anisotropy_r16[0]);
  1217. gi_probe->anisotropy[1] = RD::get_singleton()->texture_create_shared(tv, gi_probe->anisotropy_r16[1]);
  1218. RD::get_singleton()->texture_clear(gi_probe->anisotropy[0], Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1219. RD::get_singleton()->texture_clear(gi_probe->anisotropy[1], Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1220. }
  1221. {
  1222. int total_elements = 0;
  1223. for (int i = 0; i < levels.size(); i++) {
  1224. total_elements += levels[i];
  1225. }
  1226. if (gi_probe_use_anisotropy) {
  1227. total_elements *= 6;
  1228. }
  1229. gi_probe->write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
  1230. }
  1231. for (int i = 0; i < levels.size(); i++) {
  1232. GIProbeInstance::Mipmap mipmap;
  1233. mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), gi_probe->texture, 0, i, RD::TEXTURE_SLICE_3D);
  1234. if (gi_probe_use_anisotropy) {
  1235. RD::TextureView tv;
  1236. tv.format_override = RD::DATA_FORMAT_R16_UINT;
  1237. mipmap.anisotropy[0] = RD::get_singleton()->texture_create_shared_from_slice(tv, gi_probe->anisotropy[0], 0, i, RD::TEXTURE_SLICE_3D);
  1238. mipmap.anisotropy[1] = RD::get_singleton()->texture_create_shared_from_slice(tv, gi_probe->anisotropy[1], 0, i, RD::TEXTURE_SLICE_3D);
  1239. }
  1240. mipmap.level = levels.size() - i - 1;
  1241. mipmap.cell_offset = 0;
  1242. for (uint32_t j = 0; j < mipmap.level; j++) {
  1243. mipmap.cell_offset += levels[j];
  1244. }
  1245. mipmap.cell_count = levels[mipmap.level];
  1246. Vector<RD::Uniform> uniforms;
  1247. {
  1248. RD::Uniform u;
  1249. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1250. u.binding = 1;
  1251. u.ids.push_back(storage->gi_probe_get_octree_buffer(gi_probe->probe));
  1252. uniforms.push_back(u);
  1253. }
  1254. {
  1255. RD::Uniform u;
  1256. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1257. u.binding = 2;
  1258. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  1259. uniforms.push_back(u);
  1260. }
  1261. {
  1262. RD::Uniform u;
  1263. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1264. u.binding = 4;
  1265. u.ids.push_back(gi_probe->write_buffer);
  1266. uniforms.push_back(u);
  1267. }
  1268. {
  1269. RD::Uniform u;
  1270. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1271. u.binding = 9;
  1272. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1273. uniforms.push_back(u);
  1274. }
  1275. {
  1276. RD::Uniform u;
  1277. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1278. u.binding = 10;
  1279. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1280. uniforms.push_back(u);
  1281. }
  1282. {
  1283. Vector<RD::Uniform> copy_uniforms = uniforms;
  1284. if (i == 0) {
  1285. {
  1286. RD::Uniform u;
  1287. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1288. u.binding = 3;
  1289. u.ids.push_back(gi_probe_lights_uniform);
  1290. copy_uniforms.push_back(u);
  1291. }
  1292. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT], 0);
  1293. copy_uniforms = uniforms; //restore
  1294. {
  1295. RD::Uniform u;
  1296. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1297. u.binding = 5;
  1298. u.ids.push_back(gi_probe->texture);
  1299. copy_uniforms.push_back(u);
  1300. }
  1301. if (gi_probe_use_anisotropy) {
  1302. {
  1303. RD::Uniform u;
  1304. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1305. u.binding = 7;
  1306. u.ids.push_back(gi_probe->anisotropy[0]);
  1307. copy_uniforms.push_back(u);
  1308. }
  1309. {
  1310. RD::Uniform u;
  1311. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1312. u.binding = 8;
  1313. u.ids.push_back(gi_probe->anisotropy[1]);
  1314. copy_uniforms.push_back(u);
  1315. }
  1316. }
  1317. mipmap.second_bounce_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE], 0);
  1318. } else {
  1319. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP], 0);
  1320. }
  1321. }
  1322. {
  1323. RD::Uniform u;
  1324. u.type = RD::UNIFORM_TYPE_IMAGE;
  1325. u.binding = 5;
  1326. u.ids.push_back(mipmap.texture);
  1327. uniforms.push_back(u);
  1328. }
  1329. if (gi_probe_use_anisotropy) {
  1330. {
  1331. RD::Uniform u;
  1332. u.type = RD::UNIFORM_TYPE_IMAGE;
  1333. u.binding = 6;
  1334. u.ids.push_back(mipmap.anisotropy[0]);
  1335. uniforms.push_back(u);
  1336. }
  1337. {
  1338. RD::Uniform u;
  1339. u.type = RD::UNIFORM_TYPE_IMAGE;
  1340. u.binding = 7;
  1341. u.ids.push_back(mipmap.anisotropy[1]);
  1342. uniforms.push_back(u);
  1343. }
  1344. }
  1345. mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE], 0);
  1346. gi_probe->mipmaps.push_back(mipmap);
  1347. }
  1348. {
  1349. uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  1350. uint32_t oversample = nearest_power_of_2_templated(4);
  1351. int mipmap_index = 0;
  1352. while (mipmap_index < gi_probe->mipmaps.size()) {
  1353. GIProbeInstance::DynamicMap dmap;
  1354. if (oversample > 0) {
  1355. dmap.size = dynamic_map_size * (1 << oversample);
  1356. dmap.mipmap = -1;
  1357. oversample--;
  1358. } else {
  1359. dmap.size = dynamic_map_size >> mipmap_index;
  1360. dmap.mipmap = mipmap_index;
  1361. mipmap_index++;
  1362. }
  1363. RD::TextureFormat dtf;
  1364. dtf.width = dmap.size;
  1365. dtf.height = dmap.size;
  1366. dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1367. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1368. if (gi_probe->dynamic_maps.size() == 0) {
  1369. dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1370. }
  1371. dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1372. if (gi_probe->dynamic_maps.size() == 0) {
  1373. //render depth for first one
  1374. dtf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  1375. dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1376. dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1377. }
  1378. //just use depth as-is
  1379. dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1380. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1381. dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1382. if (gi_probe->dynamic_maps.size() == 0) {
  1383. dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1384. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1385. dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1386. dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1387. dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1388. Vector<RID> fb;
  1389. fb.push_back(dmap.albedo);
  1390. fb.push_back(dmap.normal);
  1391. fb.push_back(dmap.orm);
  1392. fb.push_back(dmap.texture); //emission
  1393. fb.push_back(dmap.depth);
  1394. fb.push_back(dmap.fb_depth);
  1395. dmap.fb = RD::get_singleton()->framebuffer_create(fb);
  1396. {
  1397. Vector<RD::Uniform> uniforms;
  1398. {
  1399. RD::Uniform u;
  1400. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1401. u.binding = 3;
  1402. u.ids.push_back(gi_probe_lights_uniform);
  1403. uniforms.push_back(u);
  1404. }
  1405. {
  1406. RD::Uniform u;
  1407. u.type = RD::UNIFORM_TYPE_IMAGE;
  1408. u.binding = 5;
  1409. u.ids.push_back(dmap.albedo);
  1410. uniforms.push_back(u);
  1411. }
  1412. {
  1413. RD::Uniform u;
  1414. u.type = RD::UNIFORM_TYPE_IMAGE;
  1415. u.binding = 6;
  1416. u.ids.push_back(dmap.normal);
  1417. uniforms.push_back(u);
  1418. }
  1419. {
  1420. RD::Uniform u;
  1421. u.type = RD::UNIFORM_TYPE_IMAGE;
  1422. u.binding = 7;
  1423. u.ids.push_back(dmap.orm);
  1424. uniforms.push_back(u);
  1425. }
  1426. {
  1427. RD::Uniform u;
  1428. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1429. u.binding = 8;
  1430. u.ids.push_back(dmap.fb_depth);
  1431. uniforms.push_back(u);
  1432. }
  1433. {
  1434. RD::Uniform u;
  1435. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1436. u.binding = 9;
  1437. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1438. uniforms.push_back(u);
  1439. }
  1440. {
  1441. RD::Uniform u;
  1442. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1443. u.binding = 10;
  1444. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1445. uniforms.push_back(u);
  1446. }
  1447. {
  1448. RD::Uniform u;
  1449. u.type = RD::UNIFORM_TYPE_IMAGE;
  1450. u.binding = 11;
  1451. u.ids.push_back(dmap.texture);
  1452. uniforms.push_back(u);
  1453. }
  1454. {
  1455. RD::Uniform u;
  1456. u.type = RD::UNIFORM_TYPE_IMAGE;
  1457. u.binding = 12;
  1458. u.ids.push_back(dmap.depth);
  1459. uniforms.push_back(u);
  1460. }
  1461. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
  1462. }
  1463. } else {
  1464. bool plot = dmap.mipmap >= 0;
  1465. bool write = dmap.mipmap < (gi_probe->mipmaps.size() - 1);
  1466. Vector<RD::Uniform> uniforms;
  1467. {
  1468. RD::Uniform u;
  1469. u.type = RD::UNIFORM_TYPE_IMAGE;
  1470. u.binding = 5;
  1471. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].texture);
  1472. uniforms.push_back(u);
  1473. }
  1474. {
  1475. RD::Uniform u;
  1476. u.type = RD::UNIFORM_TYPE_IMAGE;
  1477. u.binding = 6;
  1478. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].depth);
  1479. uniforms.push_back(u);
  1480. }
  1481. if (write) {
  1482. {
  1483. RD::Uniform u;
  1484. u.type = RD::UNIFORM_TYPE_IMAGE;
  1485. u.binding = 7;
  1486. u.ids.push_back(dmap.texture);
  1487. uniforms.push_back(u);
  1488. }
  1489. {
  1490. RD::Uniform u;
  1491. u.type = RD::UNIFORM_TYPE_IMAGE;
  1492. u.binding = 8;
  1493. u.ids.push_back(dmap.depth);
  1494. uniforms.push_back(u);
  1495. }
  1496. }
  1497. {
  1498. RD::Uniform u;
  1499. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1500. u.binding = 9;
  1501. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1502. uniforms.push_back(u);
  1503. }
  1504. {
  1505. RD::Uniform u;
  1506. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1507. u.binding = 10;
  1508. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1509. uniforms.push_back(u);
  1510. }
  1511. if (plot) {
  1512. {
  1513. RD::Uniform u;
  1514. u.type = RD::UNIFORM_TYPE_IMAGE;
  1515. u.binding = 11;
  1516. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].texture);
  1517. uniforms.push_back(u);
  1518. }
  1519. if (gi_probe_is_anisotropic()) {
  1520. {
  1521. RD::Uniform u;
  1522. u.type = RD::UNIFORM_TYPE_IMAGE;
  1523. u.binding = 12;
  1524. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].anisotropy[0]);
  1525. uniforms.push_back(u);
  1526. }
  1527. {
  1528. RD::Uniform u;
  1529. u.type = RD::UNIFORM_TYPE_IMAGE;
  1530. u.binding = 13;
  1531. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].anisotropy[1]);
  1532. uniforms.push_back(u);
  1533. }
  1534. }
  1535. }
  1536. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[(write && plot) ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : write ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT], 0);
  1537. }
  1538. gi_probe->dynamic_maps.push_back(dmap);
  1539. }
  1540. }
  1541. }
  1542. gi_probe->last_probe_data_version = data_version;
  1543. p_update_light_instances = true; //just in case
  1544. _base_uniforms_changed();
  1545. }
  1546. // UDPDATE TIME
  1547. if (gi_probe->has_dynamic_object_data) {
  1548. //if it has dynamic object data, it needs to be cleared
  1549. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1550. if (gi_probe_is_anisotropic()) {
  1551. RD::get_singleton()->texture_clear(gi_probe->anisotropy[0], Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1552. RD::get_singleton()->texture_clear(gi_probe->anisotropy[1], Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1553. }
  1554. }
  1555. uint32_t light_count = 0;
  1556. if (p_update_light_instances || p_dynamic_object_count > 0) {
  1557. light_count = MIN(gi_probe_max_lights, (uint32_t)p_light_instances.size());
  1558. {
  1559. Transform to_cell = storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  1560. Transform to_probe_xform = (gi_probe->transform * to_cell.affine_inverse()).affine_inverse();
  1561. //update lights
  1562. for (uint32_t i = 0; i < light_count; i++) {
  1563. GIProbeLight &l = gi_probe_lights[i];
  1564. RID light_instance = p_light_instances[i];
  1565. RID light = light_instance_get_base_light(light_instance);
  1566. l.type = storage->light_get_type(light);
  1567. l.attenuation = storage->light_get_param(light, VS::LIGHT_PARAM_ATTENUATION);
  1568. l.energy = storage->light_get_param(light, VS::LIGHT_PARAM_ENERGY) * storage->light_get_param(light, VS::LIGHT_PARAM_INDIRECT_ENERGY);
  1569. l.radius = to_cell.basis.xform(Vector3(storage->light_get_param(light, VS::LIGHT_PARAM_RANGE), 0, 0)).length();
  1570. Color color = storage->light_get_color(light).to_linear();
  1571. l.color[0] = color.r;
  1572. l.color[1] = color.g;
  1573. l.color[2] = color.b;
  1574. l.spot_angle_radians = Math::deg2rad(storage->light_get_param(light, VS::LIGHT_PARAM_SPOT_ANGLE));
  1575. l.spot_attenuation = storage->light_get_param(light, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  1576. Transform xform = light_instance_get_base_transform(light_instance);
  1577. Vector3 pos = to_probe_xform.xform(xform.origin);
  1578. Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_axis(2)).normalized();
  1579. l.position[0] = pos.x;
  1580. l.position[1] = pos.y;
  1581. l.position[2] = pos.z;
  1582. l.direction[0] = dir.x;
  1583. l.direction[1] = dir.y;
  1584. l.direction[2] = dir.z;
  1585. l.has_shadow = storage->light_has_shadow(light);
  1586. }
  1587. RD::get_singleton()->buffer_update(gi_probe_lights_uniform, 0, sizeof(GIProbeLight) * light_count, gi_probe_lights, true);
  1588. }
  1589. }
  1590. if (gi_probe->has_dynamic_object_data || p_update_light_instances || p_dynamic_object_count) {
  1591. // PROCESS MIPMAPS
  1592. if (gi_probe->mipmaps.size()) {
  1593. //can update mipmaps
  1594. Vector3i probe_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1595. GIProbePushConstant push_constant;
  1596. push_constant.limits[0] = probe_size.x;
  1597. push_constant.limits[1] = probe_size.y;
  1598. push_constant.limits[2] = probe_size.z;
  1599. push_constant.stack_size = gi_probe->mipmaps.size();
  1600. push_constant.emission_scale = 1.0;
  1601. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  1602. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1603. push_constant.light_count = light_count;
  1604. push_constant.aniso_strength = storage->gi_probe_get_anisotropy_strength(gi_probe->probe);
  1605. /* print_line("probe update to version " + itos(gi_probe->last_probe_version));
  1606. print_line("propagation " + rtos(push_constant.propagation));
  1607. print_line("dynrange " + rtos(push_constant.dynamic_range));
  1608. */
  1609. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1610. int passes;
  1611. if (p_update_light_instances) {
  1612. passes = storage->gi_probe_is_using_two_bounces(gi_probe->probe) ? 2 : 1;
  1613. } else {
  1614. passes = 1; //only re-blitting is necessary
  1615. }
  1616. int wg_size = 64;
  1617. int wg_limit_x = RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  1618. for (int pass = 0; pass < passes; pass++) {
  1619. if (p_update_light_instances) {
  1620. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  1621. if (i == 0) {
  1622. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[pass == 0 ? GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT : GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE]);
  1623. } else if (i == 1) {
  1624. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP]);
  1625. }
  1626. if (pass == 1 || i > 0) {
  1627. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  1628. }
  1629. if (pass == 0 || i > 0) {
  1630. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].uniform_set, 0);
  1631. } else {
  1632. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].second_bounce_uniform_set, 0);
  1633. }
  1634. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  1635. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  1636. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  1637. while (wg_todo) {
  1638. int wg_count = MIN(wg_todo, wg_limit_x);
  1639. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  1640. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  1641. wg_todo -= wg_count;
  1642. push_constant.cell_offset += wg_count * wg_size;
  1643. }
  1644. }
  1645. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  1646. }
  1647. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE]);
  1648. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  1649. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].write_uniform_set, 0);
  1650. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  1651. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  1652. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  1653. while (wg_todo) {
  1654. int wg_count = MIN(wg_todo, wg_limit_x);
  1655. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  1656. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  1657. wg_todo -= wg_count;
  1658. push_constant.cell_offset += wg_count * wg_size;
  1659. }
  1660. }
  1661. }
  1662. RD::get_singleton()->compute_list_end();
  1663. }
  1664. }
  1665. gi_probe->has_dynamic_object_data = false; //clear until dynamic object data is used again
  1666. if (p_dynamic_object_count && gi_probe->dynamic_maps.size()) {
  1667. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1668. int multiplier = gi_probe->dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  1669. Transform oversample_scale;
  1670. oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
  1671. Transform to_cell = oversample_scale * storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  1672. Transform to_world_xform = gi_probe->transform * to_cell.affine_inverse();
  1673. Transform to_probe_xform = to_world_xform.affine_inverse();
  1674. AABB probe_aabb(Vector3(), octree_size);
  1675. //this could probably be better parallelized in compute..
  1676. for (int i = 0; i < p_dynamic_object_count; i++) {
  1677. InstanceBase *instance = p_dynamic_objects[i];
  1678. //not used, so clear
  1679. instance->depth_layer = 0;
  1680. instance->depth = 0;
  1681. //transform aabb to giprobe
  1682. AABB aabb = (to_probe_xform * instance->transform).xform(instance->aabb);
  1683. //this needs to wrap to grid resolution to avoid jitter
  1684. //also extend margin a bit just in case
  1685. Vector3i begin = aabb.position - Vector3i(1, 1, 1);
  1686. Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
  1687. for (int j = 0; j < 3; j++) {
  1688. if ((end[j] - begin[j]) & 1) {
  1689. end[j]++; //for half extents split, it needs to be even
  1690. }
  1691. begin[j] = MAX(begin[j], 0);
  1692. end[j] = MIN(end[j], octree_size[j] * multiplier);
  1693. }
  1694. //aabb = aabb.intersection(probe_aabb); //intersect
  1695. aabb.position = begin;
  1696. aabb.size = end - begin;
  1697. //print_line("aabb: " + aabb);
  1698. for (int j = 0; j < 6; j++) {
  1699. //if (j != 0 && j != 3) {
  1700. // continue;
  1701. //}
  1702. static const Vector3 render_z[6] = {
  1703. Vector3(1, 0, 0),
  1704. Vector3(0, 1, 0),
  1705. Vector3(0, 0, 1),
  1706. Vector3(-1, 0, 0),
  1707. Vector3(0, -1, 0),
  1708. Vector3(0, 0, -1),
  1709. };
  1710. static const Vector3 render_up[6] = {
  1711. Vector3(0, 1, 0),
  1712. Vector3(0, 0, 1),
  1713. Vector3(0, 1, 0),
  1714. Vector3(0, 1, 0),
  1715. Vector3(0, 0, 1),
  1716. Vector3(0, 1, 0),
  1717. };
  1718. Vector3 render_dir = render_z[j];
  1719. Vector3 up_dir = render_up[j];
  1720. Vector3 center = aabb.position + aabb.size * 0.5;
  1721. Transform xform;
  1722. xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
  1723. Vector3 x_dir = xform.basis.get_axis(0).abs();
  1724. int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
  1725. Vector3 y_dir = xform.basis.get_axis(1).abs();
  1726. int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
  1727. Vector3 z_dir = -xform.basis.get_axis(2);
  1728. int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
  1729. Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
  1730. bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(0)) < 0);
  1731. bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(1)) < 0);
  1732. bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(2)) > 0);
  1733. CameraMatrix cm;
  1734. 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]);
  1735. _render_material(to_world_xform * xform, cm, true, &instance, 1, gi_probe->dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size));
  1736. GIProbeDynamicPushConstant push_constant;
  1737. zeromem(&push_constant, sizeof(GIProbeDynamicPushConstant));
  1738. push_constant.limits[0] = octree_size.x;
  1739. push_constant.limits[1] = octree_size.y;
  1740. push_constant.limits[2] = octree_size.z;
  1741. push_constant.light_count = p_light_instances.size();
  1742. push_constant.x_dir[0] = x_dir[0];
  1743. push_constant.x_dir[1] = x_dir[1];
  1744. push_constant.x_dir[2] = x_dir[2];
  1745. push_constant.y_dir[0] = y_dir[0];
  1746. push_constant.y_dir[1] = y_dir[1];
  1747. push_constant.y_dir[2] = y_dir[2];
  1748. push_constant.z_dir[0] = z_dir[0];
  1749. push_constant.z_dir[1] = z_dir[1];
  1750. push_constant.z_dir[2] = z_dir[2];
  1751. push_constant.z_base = xform.origin[z_axis];
  1752. push_constant.z_sign = (z_flip ? -1.0 : 1.0);
  1753. push_constant.pos_multiplier = float(1.0) / multiplier;
  1754. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1755. push_constant.flip_x = x_flip;
  1756. push_constant.flip_y = y_flip;
  1757. push_constant.rect_pos[0] = rect.position[0];
  1758. push_constant.rect_pos[1] = rect.position[1];
  1759. push_constant.rect_size[0] = rect.size[0];
  1760. push_constant.rect_size[1] = rect.size[1];
  1761. push_constant.prev_rect_ofs[0] = 0;
  1762. push_constant.prev_rect_ofs[1] = 0;
  1763. push_constant.prev_rect_size[0] = 0;
  1764. push_constant.prev_rect_size[1] = 0;
  1765. push_constant.on_mipmap = false;
  1766. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  1767. push_constant.pad[0] = 0;
  1768. push_constant.pad[1] = 0;
  1769. push_constant.pad[2] = 0;
  1770. //process lighting
  1771. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1772. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
  1773. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[0].uniform_set, 0);
  1774. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  1775. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  1776. //print_line("rect: " + itos(i) + ": " + rect);
  1777. for (int k = 1; k < gi_probe->dynamic_maps.size(); k++) {
  1778. // enlarge the rect if needed so all pixels fit when downscaled,
  1779. // this ensures downsampling is smooth and optimal because no pixels are left behind
  1780. //x
  1781. if (rect.position.x & 1) {
  1782. rect.size.x++;
  1783. push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
  1784. } else {
  1785. push_constant.prev_rect_ofs[0] = 0;
  1786. }
  1787. if (rect.size.x & 1) {
  1788. rect.size.x++;
  1789. }
  1790. rect.position.x >>= 1;
  1791. rect.size.x = MAX(1, rect.size.x >> 1);
  1792. //y
  1793. if (rect.position.y & 1) {
  1794. rect.size.y++;
  1795. push_constant.prev_rect_ofs[1] = 1;
  1796. } else {
  1797. push_constant.prev_rect_ofs[1] = 0;
  1798. }
  1799. if (rect.size.y & 1) {
  1800. rect.size.y++;
  1801. }
  1802. rect.position.y >>= 1;
  1803. rect.size.y = MAX(1, rect.size.y >> 1);
  1804. //shrink limits to ensure plot does not go outside map
  1805. if (gi_probe->dynamic_maps[k].mipmap > 0) {
  1806. for (int l = 0; l < 3; l++) {
  1807. push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
  1808. }
  1809. }
  1810. //print_line("rect: " + itos(i) + ": " + rect);
  1811. push_constant.rect_pos[0] = rect.position[0];
  1812. push_constant.rect_pos[1] = rect.position[1];
  1813. push_constant.prev_rect_size[0] = push_constant.rect_size[0];
  1814. push_constant.prev_rect_size[1] = push_constant.rect_size[1];
  1815. push_constant.rect_size[0] = rect.size[0];
  1816. push_constant.rect_size[1] = rect.size[1];
  1817. push_constant.on_mipmap = gi_probe->dynamic_maps[k].mipmap > 0;
  1818. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1819. if (gi_probe->dynamic_maps[k].mipmap < 0) {
  1820. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
  1821. } else if (k < gi_probe->dynamic_maps.size() - 1) {
  1822. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT]);
  1823. } else {
  1824. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
  1825. }
  1826. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[k].uniform_set, 0);
  1827. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  1828. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  1829. }
  1830. RD::get_singleton()->compute_list_end();
  1831. }
  1832. }
  1833. gi_probe->has_dynamic_object_data = true; //clear until dynamic object data is used again
  1834. }
  1835. gi_probe->last_probe_version = storage->gi_probe_get_version(gi_probe->probe);
  1836. }
  1837. void RasterizerSceneRD::_debug_giprobe(RID p_gi_probe, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
  1838. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_gi_probe);
  1839. ERR_FAIL_COND(!gi_probe);
  1840. if (gi_probe->mipmaps.size() == 0) {
  1841. return;
  1842. }
  1843. CameraMatrix transform = (p_camera_with_transform * CameraMatrix(gi_probe->transform)) * CameraMatrix(storage->gi_probe_get_to_cell_xform(gi_probe->probe).affine_inverse());
  1844. int level = 0;
  1845. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1846. GIProbeDebugPushConstant push_constant;
  1847. push_constant.alpha = p_alpha;
  1848. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1849. push_constant.cell_offset = gi_probe->mipmaps[level].cell_offset;
  1850. push_constant.level = level;
  1851. push_constant.bounds[0] = octree_size.x >> level;
  1852. push_constant.bounds[1] = octree_size.y >> level;
  1853. push_constant.bounds[2] = octree_size.z >> level;
  1854. push_constant.pad = 0;
  1855. for (int i = 0; i < 4; i++) {
  1856. for (int j = 0; j < 4; j++) {
  1857. push_constant.projection[i * 4 + j] = transform.matrix[i][j];
  1858. }
  1859. }
  1860. if (giprobe_debug_uniform_set.is_valid()) {
  1861. RD::get_singleton()->free(giprobe_debug_uniform_set);
  1862. }
  1863. Vector<RD::Uniform> uniforms;
  1864. {
  1865. RD::Uniform u;
  1866. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1867. u.binding = 1;
  1868. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  1869. uniforms.push_back(u);
  1870. }
  1871. {
  1872. RD::Uniform u;
  1873. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1874. u.binding = 2;
  1875. u.ids.push_back(gi_probe->texture);
  1876. uniforms.push_back(u);
  1877. }
  1878. {
  1879. RD::Uniform u;
  1880. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1881. u.binding = 3;
  1882. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1883. uniforms.push_back(u);
  1884. }
  1885. if (gi_probe_use_anisotropy) {
  1886. {
  1887. RD::Uniform u;
  1888. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1889. u.binding = 4;
  1890. u.ids.push_back(gi_probe->anisotropy[0]);
  1891. uniforms.push_back(u);
  1892. }
  1893. {
  1894. RD::Uniform u;
  1895. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1896. u.binding = 5;
  1897. u.ids.push_back(gi_probe->anisotropy[1]);
  1898. uniforms.push_back(u);
  1899. }
  1900. }
  1901. int cell_count;
  1902. if (!p_emission && p_lighting && gi_probe->has_dynamic_object_data) {
  1903. cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
  1904. } else {
  1905. cell_count = gi_probe->mipmaps[level].cell_count;
  1906. }
  1907. giprobe_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_debug_shader_version_shaders[0], 0);
  1908. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, giprobe_debug_shader_version_pipelines[p_emission ? GI_PROBE_DEBUG_EMISSION : p_lighting ? (gi_probe->has_dynamic_object_data ? GI_PROBE_DEBUG_LIGHT_FULL : GI_PROBE_DEBUG_LIGHT) : GI_PROBE_DEBUG_COLOR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  1909. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, giprobe_debug_uniform_set, 0);
  1910. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(GIProbeDebugPushConstant));
  1911. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
  1912. }
  1913. const Vector<RID> &RasterizerSceneRD::gi_probe_get_slots() const {
  1914. return gi_probe_slots;
  1915. }
  1916. RasterizerSceneRD::GIProbeQuality RasterizerSceneRD::gi_probe_get_quality() const {
  1917. return gi_probe_quality;
  1918. }
  1919. ////////////////////////////////
  1920. RID RasterizerSceneRD::render_buffers_create() {
  1921. RenderBuffers rb;
  1922. rb.data = _create_render_buffer_data();
  1923. return render_buffers_owner.make_rid(rb);
  1924. }
  1925. void RasterizerSceneRD::_allocate_blur_textures(RenderBuffers *rb) {
  1926. ERR_FAIL_COND(!rb->blur[0].texture.is_null());
  1927. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rb->width, rb->height, Image::FORMAT_RGBAH);
  1928. RD::TextureFormat tf;
  1929. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1930. tf.width = rb->width;
  1931. tf.height = rb->height;
  1932. tf.type = RD::TEXTURE_TYPE_2D;
  1933. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1934. tf.mipmaps = mipmaps_required;
  1935. rb->blur[0].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1936. //the second one is smaller (only used for separatable part of blur)
  1937. tf.width >>= 1;
  1938. tf.height >>= 1;
  1939. tf.mipmaps--;
  1940. rb->blur[1].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1941. int base_width = rb->width;
  1942. int base_height = rb->height;
  1943. for (uint32_t i = 0; i < mipmaps_required; i++) {
  1944. RenderBuffers::Blur::Mipmap mm;
  1945. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[0].texture, 0, i);
  1946. {
  1947. Vector<RID> fbs;
  1948. fbs.push_back(mm.texture);
  1949. mm.framebuffer = RD::get_singleton()->framebuffer_create(fbs);
  1950. }
  1951. mm.width = base_width;
  1952. mm.height = base_height;
  1953. rb->blur[0].mipmaps.push_back(mm);
  1954. if (i > 0) {
  1955. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[1].texture, 0, i - 1);
  1956. {
  1957. Vector<RID> fbs;
  1958. fbs.push_back(mm.texture);
  1959. mm.framebuffer = RD::get_singleton()->framebuffer_create(fbs);
  1960. }
  1961. rb->blur[1].mipmaps.push_back(mm);
  1962. }
  1963. base_width = MAX(1, base_width >> 1);
  1964. base_height = MAX(1, base_height >> 1);
  1965. }
  1966. }
  1967. void RasterizerSceneRD::_allocate_luminance_textures(RenderBuffers *rb) {
  1968. ERR_FAIL_COND(!rb->luminance.current.is_null());
  1969. int w = rb->width;
  1970. int h = rb->height;
  1971. while (true) {
  1972. w = MAX(w / 8, 1);
  1973. h = MAX(h / 8, 1);
  1974. RD::TextureFormat tf;
  1975. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1976. tf.width = w;
  1977. tf.height = h;
  1978. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1979. bool final = w == 1 && h == 1;
  1980. if (final) {
  1981. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  1982. }
  1983. RID texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1984. rb->luminance.reduce.push_back(texture);
  1985. if (final) {
  1986. rb->luminance.current = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1987. break;
  1988. }
  1989. }
  1990. }
  1991. void RasterizerSceneRD::_free_render_buffer_data(RenderBuffers *rb) {
  1992. if (rb->texture.is_valid()) {
  1993. RD::get_singleton()->free(rb->texture);
  1994. rb->texture = RID();
  1995. }
  1996. if (rb->depth_texture.is_valid()) {
  1997. RD::get_singleton()->free(rb->depth_texture);
  1998. rb->depth_texture = RID();
  1999. }
  2000. for (int i = 0; i < 2; i++) {
  2001. if (rb->blur[i].texture.is_valid()) {
  2002. RD::get_singleton()->free(rb->blur[i].texture);
  2003. rb->blur[i].texture = RID();
  2004. rb->blur[i].mipmaps.clear();
  2005. }
  2006. }
  2007. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  2008. RD::get_singleton()->free(rb->luminance.reduce[i]);
  2009. }
  2010. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  2011. RD::get_singleton()->free(rb->luminance.reduce[i]);
  2012. }
  2013. rb->luminance.reduce.clear();
  2014. if (rb->luminance.current.is_valid()) {
  2015. RD::get_singleton()->free(rb->luminance.current);
  2016. rb->luminance.current = RID();
  2017. }
  2018. if (rb->ssao.ao[0].is_valid()) {
  2019. RD::get_singleton()->free(rb->ssao.depth);
  2020. RD::get_singleton()->free(rb->ssao.ao[0]);
  2021. if (rb->ssao.ao[1].is_valid()) {
  2022. RD::get_singleton()->free(rb->ssao.ao[1]);
  2023. }
  2024. if (rb->ssao.ao_full.is_valid()) {
  2025. RD::get_singleton()->free(rb->ssao.ao_full);
  2026. }
  2027. rb->ssao.depth = RID();
  2028. rb->ssao.ao[0] = RID();
  2029. rb->ssao.ao[1] = RID();
  2030. rb->ssao.ao_full = RID();
  2031. rb->ssao.depth_slices.clear();
  2032. }
  2033. }
  2034. void RasterizerSceneRD::_process_ssao(RID p_render_buffers, RID p_environment, RID p_normal_buffer, const CameraMatrix &p_projection) {
  2035. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2036. ERR_FAIL_COND(!rb);
  2037. Environent *env = environment_owner.getornull(p_environment);
  2038. ERR_FAIL_COND(!env);
  2039. if (rb->ssao.ao[0].is_valid() && rb->ssao.ao_full.is_valid() != ssao_half_size) {
  2040. RD::get_singleton()->free(rb->ssao.depth);
  2041. RD::get_singleton()->free(rb->ssao.ao[0]);
  2042. if (rb->ssao.ao[1].is_valid()) {
  2043. RD::get_singleton()->free(rb->ssao.ao[1]);
  2044. }
  2045. if (rb->ssao.ao_full.is_valid()) {
  2046. RD::get_singleton()->free(rb->ssao.ao_full);
  2047. }
  2048. rb->ssao.depth = RID();
  2049. rb->ssao.ao[0] = RID();
  2050. rb->ssao.ao[1] = RID();
  2051. rb->ssao.ao_full = RID();
  2052. rb->ssao.depth_slices.clear();
  2053. }
  2054. if (!rb->ssao.ao[0].is_valid()) {
  2055. //allocate depth slices
  2056. {
  2057. RD::TextureFormat tf;
  2058. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  2059. tf.width = rb->width / 2;
  2060. tf.height = rb->height / 2;
  2061. tf.mipmaps = Image::get_image_required_mipmaps(tf.width, tf.height, Image::FORMAT_RF) + 1;
  2062. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2063. rb->ssao.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2064. for (uint32_t i = 0; i < tf.mipmaps; i++) {
  2065. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->ssao.depth, 0, i);
  2066. rb->ssao.depth_slices.push_back(slice);
  2067. }
  2068. }
  2069. {
  2070. RD::TextureFormat tf;
  2071. tf.format = RD::DATA_FORMAT_R8_UNORM;
  2072. tf.width = ssao_half_size ? rb->width / 2 : rb->width;
  2073. tf.height = ssao_half_size ? rb->height / 2 : rb->height;
  2074. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2075. rb->ssao.ao[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2076. rb->ssao.ao[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2077. }
  2078. if (ssao_half_size) {
  2079. //upsample texture
  2080. RD::TextureFormat tf;
  2081. tf.format = RD::DATA_FORMAT_R8_UNORM;
  2082. tf.width = rb->width;
  2083. tf.height = rb->height;
  2084. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2085. rb->ssao.ao_full = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2086. }
  2087. _render_buffers_uniform_set_changed(p_render_buffers);
  2088. }
  2089. storage->get_effects()->generate_ssao(rb->depth_texture, p_normal_buffer, Size2i(rb->width, rb->height), rb->ssao.depth, rb->ssao.depth_slices, rb->ssao.ao[0], rb->ssao.ao_full.is_valid(), rb->ssao.ao[1], rb->ssao.ao_full, env->ssao_intensity, env->ssao_radius, env->ssao_bias, p_projection, ssao_quality, env->ssao_blur, env->ssao_blur_edge_sharpness);
  2090. }
  2091. void RasterizerSceneRD::_render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection) {
  2092. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2093. ERR_FAIL_COND(!rb);
  2094. Environent *env = environment_owner.getornull(p_environment);
  2095. //glow (if enabled)
  2096. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  2097. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  2098. if (can_use_effects && camfx && (camfx->dof_blur_near_enabled || camfx->dof_blur_far_enabled) && camfx->dof_blur_amount > 0.0) {
  2099. if (rb->blur[0].texture.is_null()) {
  2100. _allocate_blur_textures(rb);
  2101. _render_buffers_uniform_set_changed(p_render_buffers);
  2102. }
  2103. float bokeh_size = camfx->dof_blur_amount * 64.0;
  2104. storage->get_effects()->bokeh_dof(rb->texture, rb->depth_texture, Size2i(rb->width, rb->height), rb->blur[0].mipmaps[0].texture, rb->blur[1].mipmaps[0].texture, rb->blur[0].mipmaps[1].texture, camfx->dof_blur_far_enabled, camfx->dof_blur_far_distance, camfx->dof_blur_far_transition, camfx->dof_blur_near_enabled, camfx->dof_blur_near_distance, camfx->dof_blur_near_transition, bokeh_size, dof_blur_bokeh_shape, dof_blur_quality, dof_blur_use_jitter, p_projection.get_z_near(), p_projection.get_z_far(), p_projection.is_orthogonal());
  2105. }
  2106. if (can_use_effects && env && env->auto_exposure) {
  2107. if (rb->luminance.current.is_null()) {
  2108. _allocate_luminance_textures(rb);
  2109. _render_buffers_uniform_set_changed(p_render_buffers);
  2110. }
  2111. bool set_immediate = env->auto_exposure_version != rb->auto_exposure_version;
  2112. rb->auto_exposure_version = env->auto_exposure_version;
  2113. double step = env->auto_exp_speed * time_step;
  2114. storage->get_effects()->luminance_reduction(rb->texture, Size2i(rb->width, rb->height), rb->luminance.reduce, rb->luminance.current, env->min_luminance, env->max_luminance, step, set_immediate);
  2115. //swap final reduce with prev luminance
  2116. SWAP(rb->luminance.current, rb->luminance.reduce.write[rb->luminance.reduce.size() - 1]);
  2117. VisualServerRaster::redraw_request(); //redraw all the time if auto exposure rendering is on
  2118. }
  2119. int max_glow_level = -1;
  2120. int glow_mask = 0;
  2121. if (can_use_effects && env && env->glow_enabled) {
  2122. /* see that blur textures are allocated */
  2123. if (rb->blur[0].texture.is_null()) {
  2124. _allocate_blur_textures(rb);
  2125. _render_buffers_uniform_set_changed(p_render_buffers);
  2126. }
  2127. for (int i = 0; i < VS::MAX_GLOW_LEVELS; i++) {
  2128. if (env->glow_levels & (1 << i)) {
  2129. if (i >= rb->blur[1].mipmaps.size()) {
  2130. max_glow_level = rb->blur[1].mipmaps.size() - 1;
  2131. glow_mask |= 1 << max_glow_level;
  2132. } else {
  2133. max_glow_level = i;
  2134. glow_mask |= (1 << i);
  2135. }
  2136. }
  2137. }
  2138. for (int i = 0; i < (max_glow_level + 1); i++) {
  2139. int vp_w = rb->blur[1].mipmaps[i].width;
  2140. int vp_h = rb->blur[1].mipmaps[i].height;
  2141. if (i == 0) {
  2142. RID luminance_texture;
  2143. if (env->auto_exposure && rb->luminance.current.is_valid()) {
  2144. luminance_texture = rb->luminance.current;
  2145. }
  2146. storage->get_effects()->gaussian_glow(rb->texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength, true, env->glow_hdr_luminance_cap, env->exposure, env->glow_bloom, env->glow_hdr_bleed_threshold, env->glow_hdr_bleed_scale, luminance_texture, env->auto_exp_scale);
  2147. } else {
  2148. storage->get_effects()->gaussian_glow(rb->blur[1].mipmaps[i - 1].texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength);
  2149. }
  2150. }
  2151. }
  2152. {
  2153. //tonemap
  2154. RasterizerEffectsRD::TonemapSettings tonemap;
  2155. tonemap.color_correction_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  2156. if (can_use_effects && env && env->auto_exposure && rb->luminance.current.is_valid()) {
  2157. tonemap.use_auto_exposure = true;
  2158. tonemap.exposure_texture = rb->luminance.current;
  2159. tonemap.auto_exposure_grey = env->auto_exp_scale;
  2160. } else {
  2161. tonemap.exposure_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
  2162. }
  2163. if (can_use_effects && env && env->glow_enabled) {
  2164. tonemap.use_glow = true;
  2165. tonemap.glow_mode = RasterizerEffectsRD::TonemapSettings::GlowMode(env->glow_blend_mode);
  2166. tonemap.glow_intensity = env->glow_blend_mode == VS::ENV_GLOW_BLEND_MODE_MIX ? env->glow_mix : env->glow_intensity;
  2167. tonemap.glow_level_flags = glow_mask;
  2168. tonemap.glow_texture_size.x = rb->blur[1].mipmaps[0].width;
  2169. tonemap.glow_texture_size.y = rb->blur[1].mipmaps[0].height;
  2170. tonemap.glow_use_bicubic_upscale = env->glow_bicubic_upscale;
  2171. tonemap.glow_texture = rb->blur[1].texture;
  2172. } else {
  2173. tonemap.glow_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  2174. }
  2175. if (env) {
  2176. tonemap.tonemap_mode = env->tone_mapper;
  2177. tonemap.white = env->white;
  2178. tonemap.exposure = env->exposure;
  2179. }
  2180. storage->get_effects()->tonemapper(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), tonemap);
  2181. }
  2182. storage->render_target_disable_clear_request(rb->render_target);
  2183. }
  2184. void RasterizerSceneRD::_render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas) {
  2185. RasterizerEffectsRD *effects = storage->get_effects();
  2186. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2187. ERR_FAIL_COND(!rb);
  2188. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
  2189. if (p_shadow_atlas.is_valid()) {
  2190. RID shadow_atlas_texture = shadow_atlas_get_texture(p_shadow_atlas);
  2191. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2192. effects->copy_to_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 2), false, true);
  2193. }
  2194. }
  2195. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
  2196. if (directional_shadow_get_texture().is_valid()) {
  2197. RID shadow_atlas_texture = directional_shadow_get_texture();
  2198. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2199. effects->copy_to_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 2), false, true);
  2200. }
  2201. }
  2202. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) {
  2203. if (rb->luminance.current.is_valid()) {
  2204. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2205. effects->copy_to_rect(rb->luminance.current, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 8), false, true);
  2206. }
  2207. }
  2208. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SSAO && rb->ssao.ao[0].is_valid()) {
  2209. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2210. RID ao_buf = rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  2211. effects->copy_to_rect(ao_buf, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, true);
  2212. }
  2213. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_ROUGHNESS_LIMITER && _render_buffers_get_roughness_texture(p_render_buffers).is_valid()) {
  2214. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2215. effects->copy_to_rect(_render_buffers_get_roughness_texture(p_render_buffers), storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, true);
  2216. }
  2217. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER && _render_buffers_get_normal_texture(p_render_buffers).is_valid()) {
  2218. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  2219. effects->copy_to_rect(_render_buffers_get_normal_texture(p_render_buffers), storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize));
  2220. }
  2221. }
  2222. RID RasterizerSceneRD::render_buffers_get_back_buffer_texture(RID p_render_buffers) {
  2223. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2224. ERR_FAIL_COND_V(!rb, RID());
  2225. if (!rb->blur[0].texture.is_valid()) {
  2226. return RID(); //not valid at the moment
  2227. }
  2228. return rb->blur[0].texture;
  2229. }
  2230. RID RasterizerSceneRD::render_buffers_get_ao_texture(RID p_render_buffers) {
  2231. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2232. ERR_FAIL_COND_V(!rb, RID());
  2233. return rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  2234. }
  2235. void RasterizerSceneRD::render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, VS::ViewportMSAA p_msaa) {
  2236. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2237. rb->width = p_width;
  2238. rb->height = p_height;
  2239. rb->render_target = p_render_target;
  2240. rb->msaa = p_msaa;
  2241. _free_render_buffer_data(rb);
  2242. {
  2243. RD::TextureFormat tf;
  2244. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2245. tf.width = rb->width;
  2246. tf.height = rb->height;
  2247. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  2248. rb->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2249. }
  2250. {
  2251. RD::TextureFormat tf;
  2252. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D24_UNORM_S8_UINT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D24_UNORM_S8_UINT : RD::DATA_FORMAT_D32_SFLOAT_S8_UINT;
  2253. tf.width = p_width;
  2254. tf.height = p_height;
  2255. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  2256. rb->depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2257. }
  2258. rb->data->configure(rb->texture, rb->depth_texture, p_width, p_height, p_msaa);
  2259. _render_buffers_uniform_set_changed(p_render_buffers);
  2260. }
  2261. int RasterizerSceneRD::get_roughness_layers() const {
  2262. return roughness_layers;
  2263. }
  2264. bool RasterizerSceneRD::is_using_radiance_cubemap_array() const {
  2265. return sky_use_cubemap_array;
  2266. }
  2267. RasterizerSceneRD::RenderBufferData *RasterizerSceneRD::render_buffers_get_data(RID p_render_buffers) {
  2268. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2269. ERR_FAIL_COND_V(!rb, NULL);
  2270. return rb->data;
  2271. }
  2272. void RasterizerSceneRD::render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID p_environment, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  2273. Color clear_color;
  2274. if (p_render_buffers.is_valid()) {
  2275. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2276. ERR_FAIL_COND(!rb);
  2277. clear_color = storage->render_target_get_clear_request_color(rb->render_target);
  2278. } else {
  2279. clear_color = storage->get_default_clear_color();
  2280. }
  2281. _render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_light_cull_result, p_light_cull_count, p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_gi_probe_cull_result, p_gi_probe_cull_count, p_environment, p_camera_effects, p_shadow_atlas, p_reflection_atlas, p_reflection_probe, p_reflection_probe_pass, clear_color);
  2282. if (p_render_buffers.is_valid()) {
  2283. RENDER_TIMESTAMP("Tonemap");
  2284. _render_buffers_post_process_and_tonemap(p_render_buffers, p_environment, p_camera_effects, p_cam_projection);
  2285. _render_buffers_debug_draw(p_render_buffers, p_shadow_atlas);
  2286. }
  2287. }
  2288. void RasterizerSceneRD::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  2289. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  2290. ERR_FAIL_COND(!light_instance);
  2291. Rect2i atlas_rect;
  2292. RID atlas_fb;
  2293. bool using_dual_paraboloid = false;
  2294. bool using_dual_paraboloid_flip = false;
  2295. float zfar = 0;
  2296. RID render_fb;
  2297. RID render_texture;
  2298. float bias = 0;
  2299. float normal_bias = 0;
  2300. bool render_cubemap = false;
  2301. bool finalize_cubemap = false;
  2302. CameraMatrix light_projection;
  2303. Transform light_transform;
  2304. if (storage->light_get_type(light_instance->light) == VS::LIGHT_DIRECTIONAL) {
  2305. //set pssm stuff
  2306. if (light_instance->last_scene_shadow_pass != scene_pass) {
  2307. light_instance->directional_rect = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  2308. directional_shadow.current_light++;
  2309. light_instance->last_scene_shadow_pass = scene_pass;
  2310. }
  2311. light_projection = light_instance->shadow_transform[p_pass].camera;
  2312. light_transform = light_instance->shadow_transform[p_pass].transform;
  2313. atlas_rect.position.x = light_instance->directional_rect.position.x;
  2314. atlas_rect.position.y = light_instance->directional_rect.position.y;
  2315. atlas_rect.size.width = light_instance->directional_rect.size.x;
  2316. atlas_rect.size.height = light_instance->directional_rect.size.y;
  2317. if (storage->light_directional_get_shadow_mode(light_instance->light) == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  2318. atlas_rect.size.width /= 2;
  2319. atlas_rect.size.height /= 2;
  2320. if (p_pass == 1) {
  2321. atlas_rect.position.x += atlas_rect.size.width;
  2322. } else if (p_pass == 2) {
  2323. atlas_rect.position.y += atlas_rect.size.height;
  2324. } else if (p_pass == 3) {
  2325. atlas_rect.position.x += atlas_rect.size.width;
  2326. atlas_rect.position.y += atlas_rect.size.height;
  2327. }
  2328. } else if (storage->light_directional_get_shadow_mode(light_instance->light) == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  2329. atlas_rect.size.height /= 2;
  2330. if (p_pass == 0) {
  2331. } else {
  2332. atlas_rect.position.y += atlas_rect.size.height;
  2333. }
  2334. }
  2335. light_instance->shadow_transform[p_pass].atlas_rect = atlas_rect;
  2336. light_instance->shadow_transform[p_pass].atlas_rect.position /= directional_shadow.size;
  2337. light_instance->shadow_transform[p_pass].atlas_rect.size /= directional_shadow.size;
  2338. float bias_mult = Math::lerp(1.0f, light_instance->shadow_transform[p_pass].bias_scale, storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE));
  2339. zfar = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_RANGE);
  2340. bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS) * bias_mult;
  2341. normal_bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * bias_mult;
  2342. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2343. render_fb = shadow_map->fb;
  2344. render_texture = shadow_map->depth;
  2345. atlas_fb = directional_shadow.fb;
  2346. } else {
  2347. //set from shadow atlas
  2348. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2349. ERR_FAIL_COND(!shadow_atlas);
  2350. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  2351. uint32_t key = shadow_atlas->shadow_owners[p_light];
  2352. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2353. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2354. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  2355. uint32_t quadrant_size = shadow_atlas->size >> 1;
  2356. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  2357. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  2358. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2359. atlas_rect.position.x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2360. atlas_rect.position.y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2361. atlas_rect.size.width = shadow_size;
  2362. atlas_rect.size.height = shadow_size;
  2363. atlas_fb = shadow_atlas->fb;
  2364. zfar = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_RANGE);
  2365. bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS);
  2366. normal_bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS);
  2367. if (storage->light_get_type(light_instance->light) == VS::LIGHT_OMNI) {
  2368. if (storage->light_omni_get_shadow_mode(light_instance->light) == VS::LIGHT_OMNI_SHADOW_CUBE) {
  2369. ShadowCubemap *cubemap = _get_shadow_cubemap(shadow_size / 2);
  2370. render_fb = cubemap->side_fb[p_pass];
  2371. render_texture = cubemap->cubemap;
  2372. light_projection = light_instance->shadow_transform[0].camera;
  2373. light_transform = light_instance->shadow_transform[0].transform;
  2374. render_cubemap = true;
  2375. finalize_cubemap = p_pass == 5;
  2376. } else {
  2377. light_projection = light_instance->shadow_transform[0].camera;
  2378. light_transform = light_instance->shadow_transform[0].transform;
  2379. atlas_rect.size.height /= 2;
  2380. atlas_rect.position.y += p_pass * atlas_rect.size.height;
  2381. using_dual_paraboloid = true;
  2382. using_dual_paraboloid_flip = p_pass == 1;
  2383. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2384. render_fb = shadow_map->fb;
  2385. render_texture = shadow_map->depth;
  2386. }
  2387. } else if (storage->light_get_type(light_instance->light) == VS::LIGHT_SPOT) {
  2388. light_projection = light_instance->shadow_transform[0].camera;
  2389. light_transform = light_instance->shadow_transform[0].transform;
  2390. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2391. render_fb = shadow_map->fb;
  2392. render_texture = shadow_map->depth;
  2393. }
  2394. }
  2395. if (render_cubemap) {
  2396. //rendering to cubemap
  2397. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, 0, 0, false, false);
  2398. if (finalize_cubemap) {
  2399. //reblit
  2400. atlas_rect.size.height /= 2;
  2401. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), bias, false);
  2402. atlas_rect.position.y += atlas_rect.size.height;
  2403. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), bias, true);
  2404. }
  2405. } else {
  2406. //render shadow
  2407. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, bias, normal_bias, using_dual_paraboloid, using_dual_paraboloid_flip);
  2408. //copy to atlas
  2409. storage->get_effects()->copy_to_rect(render_texture, atlas_fb, atlas_rect, true);
  2410. //does not work from depth to color
  2411. //RD::get_singleton()->texture_copy(render_texture, atlas_texture, Vector3(0, 0, 0), Vector3(atlas_rect.position.x, atlas_rect.position.y, 0), Vector3(atlas_rect.size.x, atlas_rect.size.y, 1), 0, 0, 0, 0, true);
  2412. }
  2413. }
  2414. void RasterizerSceneRD::render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) {
  2415. _render_material(p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_framebuffer, p_region);
  2416. }
  2417. bool RasterizerSceneRD::free(RID p_rid) {
  2418. if (render_buffers_owner.owns(p_rid)) {
  2419. RenderBuffers *rb = render_buffers_owner.getornull(p_rid);
  2420. _free_render_buffer_data(rb);
  2421. memdelete(rb->data);
  2422. render_buffers_owner.free(p_rid);
  2423. } else if (environment_owner.owns(p_rid)) {
  2424. //not much to delete, just free it
  2425. environment_owner.free(p_rid);
  2426. } else if (camera_effects_owner.owns(p_rid)) {
  2427. //not much to delete, just free it
  2428. camera_effects_owner.free(p_rid);
  2429. } else if (reflection_atlas_owner.owns(p_rid)) {
  2430. reflection_atlas_set_size(p_rid, 0, 0);
  2431. reflection_atlas_owner.free(p_rid);
  2432. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  2433. //not much to delete, just free it
  2434. //ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_rid);
  2435. reflection_probe_release_atlas_index(p_rid);
  2436. reflection_probe_instance_owner.free(p_rid);
  2437. } else if (gi_probe_instance_owner.owns(p_rid)) {
  2438. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_rid);
  2439. if (gi_probe->texture.is_valid()) {
  2440. RD::get_singleton()->free(gi_probe->texture);
  2441. RD::get_singleton()->free(gi_probe->write_buffer);
  2442. }
  2443. if (gi_probe->anisotropy[0].is_valid()) {
  2444. RD::get_singleton()->free(gi_probe->anisotropy[0]);
  2445. RD::get_singleton()->free(gi_probe->anisotropy[1]);
  2446. }
  2447. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  2448. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  2449. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  2450. }
  2451. gi_probe_slots.write[gi_probe->slot] = RID();
  2452. gi_probe_instance_owner.free(p_rid);
  2453. } else if (sky_owner.owns(p_rid)) {
  2454. _update_dirty_skys();
  2455. Sky *sky = sky_owner.getornull(p_rid);
  2456. if (sky->radiance.is_valid()) {
  2457. RD::get_singleton()->free(sky->radiance);
  2458. sky->radiance = RID();
  2459. }
  2460. _clear_reflection_data(sky->reflection);
  2461. sky_owner.free(p_rid);
  2462. } else if (light_instance_owner.owns(p_rid)) {
  2463. LightInstance *light_instance = light_instance_owner.getornull(p_rid);
  2464. //remove from shadow atlases..
  2465. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  2466. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(E->get());
  2467. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  2468. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  2469. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2470. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2471. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  2472. shadow_atlas->shadow_owners.erase(p_rid);
  2473. }
  2474. light_instance_owner.free(p_rid);
  2475. } else if (shadow_atlas_owner.owns(p_rid)) {
  2476. shadow_atlas_set_size(p_rid, 0);
  2477. shadow_atlas_owner.free(p_rid);
  2478. } else {
  2479. return false;
  2480. }
  2481. return true;
  2482. }
  2483. void RasterizerSceneRD::set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) {
  2484. debug_draw = p_debug_draw;
  2485. }
  2486. void RasterizerSceneRD::update() {
  2487. _update_dirty_skys();
  2488. }
  2489. void RasterizerSceneRD::set_time(double p_time, double p_step) {
  2490. time_step = p_step;
  2491. }
  2492. void RasterizerSceneRD::screen_space_roughness_limiter_set_active(bool p_enable, float p_curve) {
  2493. screen_space_roughness_limiter = p_enable;
  2494. screen_space_roughness_limiter_curve = p_curve;
  2495. }
  2496. bool RasterizerSceneRD::screen_space_roughness_limiter_is_active() const {
  2497. return screen_space_roughness_limiter;
  2498. }
  2499. float RasterizerSceneRD::screen_space_roughness_limiter_get_curve() const {
  2500. return screen_space_roughness_limiter_curve;
  2501. }
  2502. RasterizerSceneRD::RasterizerSceneRD(RasterizerStorageRD *p_storage) {
  2503. storage = p_storage;
  2504. roughness_layers = GLOBAL_GET("rendering/quality/reflections/roughness_layers");
  2505. sky_ggx_samples_quality = GLOBAL_GET("rendering/quality/reflections/ggx_samples");
  2506. sky_use_cubemap_array = GLOBAL_GET("rendering/quality/reflections/texture_array_reflections");
  2507. // sky_use_cubemap_array = false;
  2508. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  2509. {
  2510. //kinda complicated to compute the amount of slots, we try to use as many as we can
  2511. gi_probe_max_lights = 32;
  2512. gi_probe_lights = memnew_arr(GIProbeLight, gi_probe_max_lights);
  2513. gi_probe_lights_uniform = RD::get_singleton()->uniform_buffer_create(gi_probe_max_lights * sizeof(GIProbeLight));
  2514. gi_probe_use_anisotropy = GLOBAL_GET("rendering/quality/gi_probes/anisotropic");
  2515. gi_probe_quality = GIProbeQuality(CLAMP(int(GLOBAL_GET("rendering/quality/gi_probes/quality")), 0, 2));
  2516. if (textures_per_stage <= 16) {
  2517. gi_probe_slots.resize(2); //thats all you can get
  2518. gi_probe_use_anisotropy = false;
  2519. } else if (textures_per_stage <= 31) {
  2520. gi_probe_slots.resize(4); //thats all you can get, iOS
  2521. gi_probe_use_anisotropy = false;
  2522. } else if (textures_per_stage <= 128) {
  2523. gi_probe_slots.resize(32); //old intel
  2524. gi_probe_use_anisotropy = false;
  2525. } else if (textures_per_stage <= 256) {
  2526. gi_probe_slots.resize(64); //old intel too
  2527. gi_probe_use_anisotropy = false;
  2528. } else {
  2529. if (gi_probe_use_anisotropy) {
  2530. gi_probe_slots.resize(1024 / 3); //needs 3 textures
  2531. } else {
  2532. gi_probe_slots.resize(1024); //modern intel, nvidia, 8192 or greater
  2533. }
  2534. }
  2535. String defines = "\n#define MAX_LIGHTS " + itos(gi_probe_max_lights) + "\n";
  2536. if (gi_probe_use_anisotropy) {
  2537. defines += "\n#define MODE_ANISOTROPIC\n";
  2538. }
  2539. Vector<String> versions;
  2540. versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
  2541. versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
  2542. versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
  2543. versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
  2544. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
  2545. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  2546. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
  2547. 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");
  2548. giprobe_shader.initialize(versions, defines);
  2549. giprobe_lighting_shader_version = giprobe_shader.version_create();
  2550. for (int i = 0; i < GI_PROBE_SHADER_VERSION_MAX; i++) {
  2551. giprobe_lighting_shader_version_shaders[i] = giprobe_shader.version_get_shader(giprobe_lighting_shader_version, i);
  2552. giprobe_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(giprobe_lighting_shader_version_shaders[i]);
  2553. }
  2554. }
  2555. {
  2556. String defines;
  2557. if (gi_probe_use_anisotropy) {
  2558. defines += "\n#define USE_ANISOTROPY\n";
  2559. }
  2560. Vector<String> versions;
  2561. versions.push_back("\n#define MODE_DEBUG_COLOR\n");
  2562. versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
  2563. versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
  2564. versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
  2565. giprobe_debug_shader.initialize(versions, defines);
  2566. giprobe_debug_shader_version = giprobe_debug_shader.version_create();
  2567. for (int i = 0; i < GI_PROBE_DEBUG_MAX; i++) {
  2568. giprobe_debug_shader_version_shaders[i] = giprobe_debug_shader.version_get_shader(giprobe_debug_shader_version, i);
  2569. RD::PipelineRasterizationState rs;
  2570. rs.cull_mode = RD::POLYGON_CULL_FRONT;
  2571. RD::PipelineDepthStencilState ds;
  2572. ds.enable_depth_test = true;
  2573. ds.enable_depth_write = true;
  2574. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2575. giprobe_debug_shader_version_pipelines[i].setup(giprobe_debug_shader_version_shaders[i], RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  2576. }
  2577. }
  2578. camera_effects_set_dof_blur_bokeh_shape(VS::DOFBokehShape(int(GLOBAL_GET("rendering/quality/filters/depth_of_field_bokeh_shape"))));
  2579. camera_effects_set_dof_blur_quality(VS::DOFBlurQuality(int(GLOBAL_GET("rendering/quality/filters/depth_of_field_bokeh_quality"))), GLOBAL_GET("rendering/quality/filters/depth_of_field_use_jitter"));
  2580. environment_set_ssao_quality(VS::EnvironmentSSAOQuality(int(GLOBAL_GET("rendering/quality/ssao/quality"))), GLOBAL_GET("rendering/quality/ssao/half_size"));
  2581. screen_space_roughness_limiter = GLOBAL_GET("rendering/quality/filters/screen_space_roughness_limiter");
  2582. screen_space_roughness_limiter_curve = GLOBAL_GET("rendering/quality/filters/screen_space_roughness_limiter_curve");
  2583. }
  2584. RasterizerSceneRD::~RasterizerSceneRD() {
  2585. for (Map<Vector2i, ShadowMap>::Element *E = shadow_maps.front(); E; E = E->next()) {
  2586. RD::get_singleton()->free(E->get().depth);
  2587. }
  2588. for (Map<int, ShadowCubemap>::Element *E = shadow_cubemaps.front(); E; E = E->next()) {
  2589. RD::get_singleton()->free(E->get().cubemap);
  2590. }
  2591. RD::get_singleton()->free(gi_probe_lights_uniform);
  2592. giprobe_debug_shader.version_free(giprobe_debug_shader_version);
  2593. giprobe_shader.version_free(giprobe_lighting_shader_version);
  2594. memdelete_arr(gi_probe_lights);
  2595. }