light_storage.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. /**************************************************************************/
  2. /* light_storage.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "light_storage.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
  33. #include "texture_storage.h"
  34. using namespace RendererRD;
  35. LightStorage *LightStorage::singleton = nullptr;
  36. LightStorage *LightStorage::get_singleton() {
  37. return singleton;
  38. }
  39. LightStorage::LightStorage() {
  40. singleton = this;
  41. TextureStorage *texture_storage = TextureStorage::get_singleton();
  42. directional_shadow.size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
  43. directional_shadow.use_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
  44. using_lightmap_array = true; // high end
  45. if (using_lightmap_array) {
  46. uint64_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  47. if (textures_per_stage <= 256) {
  48. lightmap_textures.resize(32);
  49. } else {
  50. lightmap_textures.resize(1024);
  51. }
  52. for (int i = 0; i < lightmap_textures.size(); i++) {
  53. lightmap_textures.write[i] = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  54. }
  55. }
  56. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
  57. }
  58. LightStorage::~LightStorage() {
  59. free_reflection_data();
  60. free_light_data();
  61. for (const KeyValue<int, ShadowCubemap> &E : shadow_cubemaps) {
  62. RD::get_singleton()->free(E.value.cubemap);
  63. }
  64. singleton = nullptr;
  65. }
  66. bool LightStorage::free(RID p_rid) {
  67. if (owns_reflection_probe(p_rid)) {
  68. reflection_probe_free(p_rid);
  69. return true;
  70. } else if (owns_reflection_atlas(p_rid)) {
  71. reflection_atlas_free(p_rid);
  72. return true;
  73. } else if (owns_reflection_probe_instance(p_rid)) {
  74. reflection_probe_instance_free(p_rid);
  75. return true;
  76. } else if (owns_light(p_rid)) {
  77. light_free(p_rid);
  78. return true;
  79. } else if (owns_light_instance(p_rid)) {
  80. light_instance_free(p_rid);
  81. return true;
  82. } else if (owns_lightmap(p_rid)) {
  83. lightmap_free(p_rid);
  84. return true;
  85. } else if (owns_lightmap_instance(p_rid)) {
  86. lightmap_instance_free(p_rid);
  87. return true;
  88. } else if (owns_shadow_atlas(p_rid)) {
  89. shadow_atlas_free(p_rid);
  90. return true;
  91. }
  92. return false;
  93. }
  94. /* LIGHT */
  95. void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
  96. Light light;
  97. light.type = p_type;
  98. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  99. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  100. light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
  101. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  102. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  103. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  104. light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
  105. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  106. light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
  107. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  108. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  109. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  110. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  111. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  112. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  113. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  114. light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
  115. light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
  116. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  117. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  118. light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
  119. light_owner.initialize_rid(p_light, light);
  120. }
  121. RID LightStorage::directional_light_allocate() {
  122. return light_owner.allocate_rid();
  123. }
  124. void LightStorage::directional_light_initialize(RID p_light) {
  125. _light_initialize(p_light, RS::LIGHT_DIRECTIONAL);
  126. }
  127. RID LightStorage::omni_light_allocate() {
  128. return light_owner.allocate_rid();
  129. }
  130. void LightStorage::omni_light_initialize(RID p_light) {
  131. _light_initialize(p_light, RS::LIGHT_OMNI);
  132. }
  133. RID LightStorage::spot_light_allocate() {
  134. return light_owner.allocate_rid();
  135. }
  136. void LightStorage::spot_light_initialize(RID p_light) {
  137. _light_initialize(p_light, RS::LIGHT_SPOT);
  138. }
  139. void LightStorage::light_free(RID p_rid) {
  140. light_set_projector(p_rid, RID()); //clear projector
  141. // delete the texture
  142. Light *light = light_owner.get_or_null(p_rid);
  143. light->dependency.deleted_notify(p_rid);
  144. light_owner.free(p_rid);
  145. }
  146. void LightStorage::light_set_color(RID p_light, const Color &p_color) {
  147. Light *light = light_owner.get_or_null(p_light);
  148. ERR_FAIL_NULL(light);
  149. light->color = p_color;
  150. }
  151. void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  152. Light *light = light_owner.get_or_null(p_light);
  153. ERR_FAIL_NULL(light);
  154. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  155. if (light->param[p_param] == p_value) {
  156. return;
  157. }
  158. switch (p_param) {
  159. case RS::LIGHT_PARAM_RANGE:
  160. case RS::LIGHT_PARAM_SPOT_ANGLE:
  161. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  162. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  163. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  164. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  165. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  166. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  167. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  168. light->version++;
  169. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  170. } break;
  171. case RS::LIGHT_PARAM_SIZE: {
  172. if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
  173. //changing from no size to size and the opposite
  174. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  175. }
  176. } break;
  177. default: {
  178. }
  179. }
  180. light->param[p_param] = p_value;
  181. }
  182. void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
  183. Light *light = light_owner.get_or_null(p_light);
  184. ERR_FAIL_NULL(light);
  185. light->shadow = p_enabled;
  186. light->version++;
  187. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  188. }
  189. void LightStorage::light_set_projector(RID p_light, RID p_texture) {
  190. TextureStorage *texture_storage = TextureStorage::get_singleton();
  191. Light *light = light_owner.get_or_null(p_light);
  192. ERR_FAIL_NULL(light);
  193. if (light->projector == p_texture) {
  194. return;
  195. }
  196. ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture));
  197. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  198. texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  199. }
  200. light->projector = p_texture;
  201. if (light->type != RS::LIGHT_DIRECTIONAL) {
  202. if (light->projector.is_valid()) {
  203. texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  204. }
  205. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  206. }
  207. }
  208. void LightStorage::light_set_negative(RID p_light, bool p_enable) {
  209. Light *light = light_owner.get_or_null(p_light);
  210. ERR_FAIL_NULL(light);
  211. light->negative = p_enable;
  212. }
  213. void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  214. Light *light = light_owner.get_or_null(p_light);
  215. ERR_FAIL_NULL(light);
  216. light->cull_mask = p_mask;
  217. light->version++;
  218. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  219. }
  220. void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
  221. Light *light = light_owner.get_or_null(p_light);
  222. ERR_FAIL_NULL(light);
  223. light->distance_fade = p_enabled;
  224. light->distance_fade_begin = p_begin;
  225. light->distance_fade_shadow = p_shadow;
  226. light->distance_fade_length = p_length;
  227. }
  228. void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  229. Light *light = light_owner.get_or_null(p_light);
  230. ERR_FAIL_NULL(light);
  231. light->reverse_cull = p_enabled;
  232. light->version++;
  233. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  234. }
  235. void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  236. Light *light = light_owner.get_or_null(p_light);
  237. ERR_FAIL_NULL(light);
  238. light->bake_mode = p_bake_mode;
  239. light->version++;
  240. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  241. }
  242. void LightStorage::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
  243. Light *light = light_owner.get_or_null(p_light);
  244. ERR_FAIL_NULL(light);
  245. light->max_sdfgi_cascade = p_cascade;
  246. light->version++;
  247. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  248. }
  249. void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  250. Light *light = light_owner.get_or_null(p_light);
  251. ERR_FAIL_NULL(light);
  252. light->omni_shadow_mode = p_mode;
  253. light->version++;
  254. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  255. }
  256. RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
  257. const Light *light = light_owner.get_or_null(p_light);
  258. ERR_FAIL_NULL_V(light, RS::LIGHT_OMNI_SHADOW_CUBE);
  259. return light->omni_shadow_mode;
  260. }
  261. void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  262. Light *light = light_owner.get_or_null(p_light);
  263. ERR_FAIL_NULL(light);
  264. light->directional_shadow_mode = p_mode;
  265. light->version++;
  266. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  267. }
  268. void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  269. Light *light = light_owner.get_or_null(p_light);
  270. ERR_FAIL_NULL(light);
  271. light->directional_blend_splits = p_enable;
  272. light->version++;
  273. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  274. }
  275. bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
  276. const Light *light = light_owner.get_or_null(p_light);
  277. ERR_FAIL_NULL_V(light, false);
  278. return light->directional_blend_splits;
  279. }
  280. void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
  281. Light *light = light_owner.get_or_null(p_light);
  282. ERR_FAIL_NULL(light);
  283. light->directional_sky_mode = p_mode;
  284. }
  285. RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
  286. const Light *light = light_owner.get_or_null(p_light);
  287. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
  288. return light->directional_sky_mode;
  289. }
  290. RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
  291. const Light *light = light_owner.get_or_null(p_light);
  292. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  293. return light->directional_shadow_mode;
  294. }
  295. uint32_t LightStorage::light_get_max_sdfgi_cascade(RID p_light) {
  296. const Light *light = light_owner.get_or_null(p_light);
  297. ERR_FAIL_NULL_V(light, 0);
  298. return light->max_sdfgi_cascade;
  299. }
  300. RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
  301. const Light *light = light_owner.get_or_null(p_light);
  302. ERR_FAIL_NULL_V(light, RS::LIGHT_BAKE_DISABLED);
  303. return light->bake_mode;
  304. }
  305. uint64_t LightStorage::light_get_version(RID p_light) const {
  306. const Light *light = light_owner.get_or_null(p_light);
  307. ERR_FAIL_NULL_V(light, 0);
  308. return light->version;
  309. }
  310. uint32_t LightStorage::light_get_cull_mask(RID p_light) const {
  311. const Light *light = light_owner.get_or_null(p_light);
  312. ERR_FAIL_NULL_V(light, 0);
  313. return light->cull_mask;
  314. }
  315. AABB LightStorage::light_get_aabb(RID p_light) const {
  316. const Light *light = light_owner.get_or_null(p_light);
  317. ERR_FAIL_NULL_V(light, AABB());
  318. switch (light->type) {
  319. case RS::LIGHT_SPOT: {
  320. float len = light->param[RS::LIGHT_PARAM_RANGE];
  321. float size = Math::tan(Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  322. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  323. };
  324. case RS::LIGHT_OMNI: {
  325. float r = light->param[RS::LIGHT_PARAM_RANGE];
  326. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  327. };
  328. case RS::LIGHT_DIRECTIONAL: {
  329. return AABB();
  330. };
  331. }
  332. ERR_FAIL_V(AABB());
  333. }
  334. Dependency *LightStorage::light_get_dependency(RID p_light) const {
  335. Light *light = light_owner.get_or_null(p_light);
  336. ERR_FAIL_NULL_V(light, nullptr);
  337. return &light->dependency;
  338. }
  339. /* LIGHT INSTANCE API */
  340. RID LightStorage::light_instance_create(RID p_light) {
  341. RID li = light_instance_owner.make_rid(LightInstance());
  342. LightInstance *light_instance = light_instance_owner.get_or_null(li);
  343. light_instance->self = li;
  344. light_instance->light = p_light;
  345. light_instance->light_type = light_get_type(p_light);
  346. if (light_instance->light_type != RS::LIGHT_DIRECTIONAL) {
  347. light_instance->forward_id = ForwardIDStorage::get_singleton()->allocate_forward_id(light_instance->light_type == RS::LIGHT_OMNI ? FORWARD_ID_TYPE_OMNI_LIGHT : FORWARD_ID_TYPE_SPOT_LIGHT);
  348. }
  349. return li;
  350. }
  351. void LightStorage::light_instance_free(RID p_light) {
  352. LightInstance *light_instance = light_instance_owner.get_or_null(p_light);
  353. //remove from shadow atlases..
  354. for (const RID &E : light_instance->shadow_atlases) {
  355. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(E);
  356. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_light));
  357. uint32_t key = shadow_atlas->shadow_owners[p_light];
  358. uint32_t q = (key >> QUADRANT_SHIFT) & 0x3;
  359. uint32_t s = key & SHADOW_INDEX_MASK;
  360. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  361. if (key & OMNI_LIGHT_FLAG) {
  362. // Omni lights use two atlas spots, make sure to clear the other as well
  363. shadow_atlas->quadrants[q].shadows.write[s + 1].owner = RID();
  364. }
  365. shadow_atlas->shadow_owners.erase(p_light);
  366. }
  367. if (light_instance->light_type != RS::LIGHT_DIRECTIONAL) {
  368. ForwardIDStorage::get_singleton()->free_forward_id(light_instance->light_type == RS::LIGHT_OMNI ? FORWARD_ID_TYPE_OMNI_LIGHT : FORWARD_ID_TYPE_SPOT_LIGHT, light_instance->forward_id);
  369. }
  370. light_instance_owner.free(p_light);
  371. }
  372. void LightStorage::light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) {
  373. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  374. ERR_FAIL_NULL(light_instance);
  375. light_instance->transform = p_transform;
  376. }
  377. void LightStorage::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
  378. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  379. ERR_FAIL_NULL(light_instance);
  380. light_instance->aabb = p_aabb;
  381. }
  382. void LightStorage::light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale, float p_range_begin, const Vector2 &p_uv_scale) {
  383. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  384. ERR_FAIL_NULL(light_instance);
  385. ERR_FAIL_INDEX(p_pass, 6);
  386. light_instance->shadow_transform[p_pass].camera = p_projection;
  387. light_instance->shadow_transform[p_pass].transform = p_transform;
  388. light_instance->shadow_transform[p_pass].farplane = p_far;
  389. light_instance->shadow_transform[p_pass].split = p_split;
  390. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  391. light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
  392. light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
  393. light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
  394. }
  395. void LightStorage::light_instance_mark_visible(RID p_light_instance) {
  396. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  397. ERR_FAIL_NULL(light_instance);
  398. light_instance->last_scene_pass = RendererSceneRenderRD::get_singleton()->get_scene_pass();
  399. }
  400. /* LIGHT DATA */
  401. void LightStorage::free_light_data() {
  402. if (directional_light_buffer.is_valid()) {
  403. RD::get_singleton()->free(directional_light_buffer);
  404. directional_light_buffer = RID();
  405. }
  406. if (omni_light_buffer.is_valid()) {
  407. RD::get_singleton()->free(omni_light_buffer);
  408. omni_light_buffer = RID();
  409. }
  410. if (spot_light_buffer.is_valid()) {
  411. RD::get_singleton()->free(spot_light_buffer);
  412. spot_light_buffer = RID();
  413. }
  414. if (directional_lights != nullptr) {
  415. memdelete_arr(directional_lights);
  416. directional_lights = nullptr;
  417. }
  418. if (omni_lights != nullptr) {
  419. memdelete_arr(omni_lights);
  420. omni_lights = nullptr;
  421. }
  422. if (spot_lights != nullptr) {
  423. memdelete_arr(spot_lights);
  424. spot_lights = nullptr;
  425. }
  426. if (omni_light_sort != nullptr) {
  427. memdelete_arr(omni_light_sort);
  428. omni_light_sort = nullptr;
  429. }
  430. if (spot_light_sort != nullptr) {
  431. memdelete_arr(spot_light_sort);
  432. spot_light_sort = nullptr;
  433. }
  434. }
  435. void LightStorage::set_max_lights(const uint32_t p_max_lights) {
  436. max_lights = p_max_lights;
  437. uint32_t light_buffer_size = max_lights * sizeof(LightData);
  438. omni_lights = memnew_arr(LightData, max_lights);
  439. omni_light_buffer = RD::get_singleton()->storage_buffer_create(light_buffer_size);
  440. omni_light_sort = memnew_arr(LightInstanceDepthSort, max_lights);
  441. spot_lights = memnew_arr(LightData, max_lights);
  442. spot_light_buffer = RD::get_singleton()->storage_buffer_create(light_buffer_size);
  443. spot_light_sort = memnew_arr(LightInstanceDepthSort, max_lights);
  444. //defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(max_lights) + "\n";
  445. max_directional_lights = RendererSceneRender::MAX_DIRECTIONAL_LIGHTS;
  446. uint32_t directional_light_buffer_size = max_directional_lights * sizeof(DirectionalLightData);
  447. directional_lights = memnew_arr(DirectionalLightData, max_directional_lights);
  448. directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  449. }
  450. void LightStorage::update_light_buffers(RenderDataRD *p_render_data, const PagedArray<RID> &p_lights, const Transform3D &p_camera_transform, RID p_shadow_atlas, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_positional_light_count, bool &r_directional_light_soft_shadows) {
  451. ForwardIDStorage *forward_id_storage = ForwardIDStorage::get_singleton();
  452. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  453. Transform3D inverse_transform = p_camera_transform.affine_inverse();
  454. r_directional_light_count = 0;
  455. r_positional_light_count = 0;
  456. omni_light_count = 0;
  457. spot_light_count = 0;
  458. r_directional_light_soft_shadows = false;
  459. for (int i = 0; i < (int)p_lights.size(); i++) {
  460. LightInstance *light_instance = light_instance_owner.get_or_null(p_lights[i]);
  461. if (!light_instance) {
  462. continue;
  463. }
  464. Light *light = light_owner.get_or_null(light_instance->light);
  465. ERR_CONTINUE(light == nullptr);
  466. switch (light->type) {
  467. case RS::LIGHT_DIRECTIONAL: {
  468. if (r_directional_light_count >= max_directional_lights || light->directional_sky_mode == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
  469. continue;
  470. }
  471. DirectionalLightData &light_data = directional_lights[r_directional_light_count];
  472. Transform3D light_transform = light_instance->transform;
  473. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
  474. light_data.direction[0] = direction.x;
  475. light_data.direction[1] = direction.y;
  476. light_data.direction[2] = direction.z;
  477. float sign = light->negative ? -1 : 1;
  478. light_data.energy = sign * light->param[RS::LIGHT_PARAM_ENERGY];
  479. if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
  480. light_data.energy *= light->param[RS::LIGHT_PARAM_INTENSITY];
  481. } else {
  482. light_data.energy *= Math_PI;
  483. }
  484. if (p_render_data->camera_attributes.is_valid()) {
  485. light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  486. }
  487. Color linear_col = light->color.srgb_to_linear();
  488. light_data.color[0] = linear_col.r;
  489. light_data.color[1] = linear_col.g;
  490. light_data.color[2] = linear_col.b;
  491. light_data.specular = light->param[RS::LIGHT_PARAM_SPECULAR];
  492. light_data.volumetric_fog_energy = light->param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY];
  493. light_data.mask = light->cull_mask;
  494. float size = light->param[RS::LIGHT_PARAM_SIZE];
  495. light_data.size = 1.0 - Math::cos(Math::deg_to_rad(size)); //angle to cosine offset
  496. light_data.shadow_opacity = (p_using_shadows && light->shadow)
  497. ? light->param[RS::LIGHT_PARAM_SHADOW_OPACITY]
  498. : 0.0;
  499. float angular_diameter = light->param[RS::LIGHT_PARAM_SIZE];
  500. if (angular_diameter > 0.0) {
  501. // I know tan(0) is 0, but let's not risk it with numerical precision.
  502. // technically this will keep expanding until reaching the sun, but all we care
  503. // is expand until we reach the radius of the near plane (there can't be more occluders than that)
  504. angular_diameter = Math::tan(Math::deg_to_rad(angular_diameter));
  505. if (light->shadow && light->param[RS::LIGHT_PARAM_SHADOW_BLUR] > 0.0) {
  506. // Only enable PCSS-like soft shadows if blurring is enabled.
  507. // Otherwise, performance would decrease with no visual difference.
  508. r_directional_light_soft_shadows = true;
  509. }
  510. } else {
  511. angular_diameter = 0.0;
  512. }
  513. if (light_data.shadow_opacity > 0.001) {
  514. RS::LightDirectionalShadowMode smode = light->directional_shadow_mode;
  515. light_data.soft_shadow_scale = light->param[RS::LIGHT_PARAM_SHADOW_BLUR];
  516. light_data.softshadow_angle = angular_diameter;
  517. light_data.bake_mode = light->bake_mode;
  518. if (angular_diameter <= 0.0) {
  519. light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->directional_shadow_quality_radius_get(); // Only use quality radius for PCF
  520. }
  521. int limit = smode == RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ? 0 : (smode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS ? 1 : 3);
  522. light_data.blend_splits = (smode != RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL) && light->directional_blend_splits;
  523. for (int j = 0; j < 4; j++) {
  524. Rect2 atlas_rect = light_instance->shadow_transform[j].atlas_rect;
  525. Projection matrix = light_instance->shadow_transform[j].camera;
  526. float split = light_instance->shadow_transform[MIN(limit, j)].split;
  527. Projection bias;
  528. bias.set_light_bias();
  529. Projection rectm;
  530. rectm.set_light_atlas_rect(atlas_rect);
  531. Transform3D modelview = (inverse_transform * light_instance->shadow_transform[j].transform).inverse();
  532. Projection shadow_mtx = rectm * bias * matrix * modelview;
  533. light_data.shadow_split_offsets[j] = split;
  534. float bias_scale = light_instance->shadow_transform[j].bias_scale * light_data.soft_shadow_scale;
  535. light_data.shadow_bias[j] = light->param[RS::LIGHT_PARAM_SHADOW_BIAS] / 100.0 * bias_scale;
  536. light_data.shadow_normal_bias[j] = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * light_instance->shadow_transform[j].shadow_texel_size;
  537. light_data.shadow_transmittance_bias[j] = light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] * bias_scale;
  538. light_data.shadow_z_range[j] = light_instance->shadow_transform[j].farplane;
  539. light_data.shadow_range_begin[j] = light_instance->shadow_transform[j].range_begin;
  540. RendererRD::MaterialStorage::store_camera(shadow_mtx, light_data.shadow_matrices[j]);
  541. Vector2 uv_scale = light_instance->shadow_transform[j].uv_scale;
  542. uv_scale *= atlas_rect.size; //adapt to atlas size
  543. switch (j) {
  544. case 0: {
  545. light_data.uv_scale1[0] = uv_scale.x;
  546. light_data.uv_scale1[1] = uv_scale.y;
  547. } break;
  548. case 1: {
  549. light_data.uv_scale2[0] = uv_scale.x;
  550. light_data.uv_scale2[1] = uv_scale.y;
  551. } break;
  552. case 2: {
  553. light_data.uv_scale3[0] = uv_scale.x;
  554. light_data.uv_scale3[1] = uv_scale.y;
  555. } break;
  556. case 3: {
  557. light_data.uv_scale4[0] = uv_scale.x;
  558. light_data.uv_scale4[1] = uv_scale.y;
  559. } break;
  560. }
  561. }
  562. float fade_start = light->param[RS::LIGHT_PARAM_SHADOW_FADE_START];
  563. light_data.fade_from = -light_data.shadow_split_offsets[3] * MIN(fade_start, 0.999); //using 1.0 would break smoothstep
  564. light_data.fade_to = -light_data.shadow_split_offsets[3];
  565. }
  566. r_directional_light_count++;
  567. } break;
  568. case RS::LIGHT_OMNI: {
  569. if (omni_light_count >= max_lights) {
  570. continue;
  571. }
  572. Transform3D light_transform = light_instance->transform;
  573. const real_t distance = p_camera_transform.origin.distance_to(light_transform.origin);
  574. if (light->distance_fade) {
  575. const float fade_begin = light->distance_fade_begin;
  576. const float fade_length = light->distance_fade_length;
  577. if (distance > fade_begin) {
  578. if (distance > fade_begin + fade_length) {
  579. // Out of range, don't draw this light to improve performance.
  580. continue;
  581. }
  582. }
  583. }
  584. omni_light_sort[omni_light_count].light_instance = light_instance;
  585. omni_light_sort[omni_light_count].light = light;
  586. omni_light_sort[omni_light_count].depth = distance;
  587. omni_light_count++;
  588. } break;
  589. case RS::LIGHT_SPOT: {
  590. if (spot_light_count >= max_lights) {
  591. continue;
  592. }
  593. Transform3D light_transform = light_instance->transform;
  594. const real_t distance = p_camera_transform.origin.distance_to(light_transform.origin);
  595. if (light->distance_fade) {
  596. const float fade_begin = light->distance_fade_begin;
  597. const float fade_length = light->distance_fade_length;
  598. if (distance > fade_begin) {
  599. if (distance > fade_begin + fade_length) {
  600. // Out of range, don't draw this light to improve performance.
  601. continue;
  602. }
  603. }
  604. }
  605. spot_light_sort[spot_light_count].light_instance = light_instance;
  606. spot_light_sort[spot_light_count].light = light;
  607. spot_light_sort[spot_light_count].depth = distance;
  608. spot_light_count++;
  609. } break;
  610. }
  611. light_instance->last_pass = RSG::rasterizer->get_frame_number();
  612. }
  613. if (omni_light_count) {
  614. SortArray<LightInstanceDepthSort> sorter;
  615. sorter.sort(omni_light_sort, omni_light_count);
  616. }
  617. if (spot_light_count) {
  618. SortArray<LightInstanceDepthSort> sorter;
  619. sorter.sort(spot_light_sort, spot_light_count);
  620. }
  621. bool using_forward_ids = forward_id_storage->uses_forward_ids();
  622. for (uint32_t i = 0; i < (omni_light_count + spot_light_count); i++) {
  623. uint32_t index = (i < omni_light_count) ? i : i - (omni_light_count);
  624. LightData &light_data = (i < omni_light_count) ? omni_lights[index] : spot_lights[index];
  625. RS::LightType type = (i < omni_light_count) ? RS::LIGHT_OMNI : RS::LIGHT_SPOT;
  626. LightInstance *light_instance = (i < omni_light_count) ? omni_light_sort[index].light_instance : spot_light_sort[index].light_instance;
  627. Light *light = (i < omni_light_count) ? omni_light_sort[index].light : spot_light_sort[index].light;
  628. real_t distance = (i < omni_light_count) ? omni_light_sort[index].depth : spot_light_sort[index].depth;
  629. if (using_forward_ids) {
  630. forward_id_storage->map_forward_id(type == RS::LIGHT_OMNI ? RendererRD::FORWARD_ID_TYPE_OMNI_LIGHT : RendererRD::FORWARD_ID_TYPE_SPOT_LIGHT, light_instance->forward_id, index, light_instance->last_pass);
  631. }
  632. Transform3D light_transform = light_instance->transform;
  633. float sign = light->negative ? -1 : 1;
  634. Color linear_col = light->color.srgb_to_linear();
  635. light_data.attenuation = light->param[RS::LIGHT_PARAM_ATTENUATION];
  636. // Reuse fade begin, fade length and distance for shadow LOD determination later.
  637. float fade_begin = 0.0;
  638. float fade_shadow = 0.0;
  639. float fade_length = 0.0;
  640. float fade = 1.0;
  641. float shadow_opacity_fade = 1.0;
  642. if (light->distance_fade) {
  643. fade_begin = light->distance_fade_begin;
  644. fade_shadow = light->distance_fade_shadow;
  645. fade_length = light->distance_fade_length;
  646. // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
  647. if (distance > fade_begin) {
  648. fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
  649. }
  650. if (distance > fade_shadow) {
  651. shadow_opacity_fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_shadow) / fade_length);
  652. }
  653. }
  654. float energy = sign * light->param[RS::LIGHT_PARAM_ENERGY] * fade;
  655. if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
  656. energy *= light->param[RS::LIGHT_PARAM_INTENSITY];
  657. // Convert from Luminous Power to Luminous Intensity
  658. if (type == RS::LIGHT_OMNI) {
  659. energy *= 1.0 / (Math_PI * 4.0);
  660. } else {
  661. // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
  662. // We make this assumption to keep them easy to control.
  663. energy *= 1.0 / Math_PI;
  664. }
  665. } else {
  666. energy *= Math_PI;
  667. }
  668. if (p_render_data->camera_attributes.is_valid()) {
  669. energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  670. }
  671. light_data.color[0] = linear_col.r * energy;
  672. light_data.color[1] = linear_col.g * energy;
  673. light_data.color[2] = linear_col.b * energy;
  674. light_data.specular_amount = light->param[RS::LIGHT_PARAM_SPECULAR] * 2.0;
  675. light_data.volumetric_fog_energy = light->param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY];
  676. light_data.bake_mode = light->bake_mode;
  677. float radius = MAX(0.001, light->param[RS::LIGHT_PARAM_RANGE]);
  678. light_data.inv_radius = 1.0 / radius;
  679. Vector3 pos = inverse_transform.xform(light_transform.origin);
  680. light_data.position[0] = pos.x;
  681. light_data.position[1] = pos.y;
  682. light_data.position[2] = pos.z;
  683. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
  684. light_data.direction[0] = direction.x;
  685. light_data.direction[1] = direction.y;
  686. light_data.direction[2] = direction.z;
  687. float size = light->param[RS::LIGHT_PARAM_SIZE];
  688. light_data.size = size;
  689. light_data.inv_spot_attenuation = 1.0f / light->param[RS::LIGHT_PARAM_SPOT_ATTENUATION];
  690. float spot_angle = light->param[RS::LIGHT_PARAM_SPOT_ANGLE];
  691. light_data.cos_spot_angle = Math::cos(Math::deg_to_rad(spot_angle));
  692. light_data.mask = light->cull_mask;
  693. light_data.atlas_rect[0] = 0;
  694. light_data.atlas_rect[1] = 0;
  695. light_data.atlas_rect[2] = 0;
  696. light_data.atlas_rect[3] = 0;
  697. RID projector = light->projector;
  698. if (projector.is_valid()) {
  699. Rect2 rect = texture_storage->decal_atlas_get_texture_rect(projector);
  700. if (type == RS::LIGHT_SPOT) {
  701. light_data.projector_rect[0] = rect.position.x;
  702. light_data.projector_rect[1] = rect.position.y + rect.size.height; //flip because shadow is flipped
  703. light_data.projector_rect[2] = rect.size.width;
  704. light_data.projector_rect[3] = -rect.size.height;
  705. } else {
  706. light_data.projector_rect[0] = rect.position.x;
  707. light_data.projector_rect[1] = rect.position.y;
  708. light_data.projector_rect[2] = rect.size.width;
  709. light_data.projector_rect[3] = rect.size.height * 0.5; //used by dp, so needs to be half
  710. }
  711. } else {
  712. light_data.projector_rect[0] = 0;
  713. light_data.projector_rect[1] = 0;
  714. light_data.projector_rect[2] = 0;
  715. light_data.projector_rect[3] = 0;
  716. }
  717. const bool needs_shadow =
  718. p_using_shadows &&
  719. owns_shadow_atlas(p_shadow_atlas) &&
  720. shadow_atlas_owns_light_instance(p_shadow_atlas, light_instance->self) &&
  721. light->shadow;
  722. bool in_shadow_range = true;
  723. if (needs_shadow && light->distance_fade) {
  724. if (distance > light->distance_fade_shadow + light->distance_fade_length) {
  725. // Out of range, don't draw shadows to improve performance.
  726. in_shadow_range = false;
  727. }
  728. }
  729. if (needs_shadow && in_shadow_range) {
  730. // fill in the shadow information
  731. light_data.shadow_opacity = light->param[RS::LIGHT_PARAM_SHADOW_OPACITY] * shadow_opacity_fade;
  732. float shadow_texel_size = light_instance_get_shadow_texel_size(light_instance->self, p_shadow_atlas);
  733. light_data.shadow_normal_bias = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * shadow_texel_size * 10.0;
  734. if (type == RS::LIGHT_SPOT) {
  735. light_data.shadow_bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS] / 100.0;
  736. } else { //omni
  737. light_data.shadow_bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS];
  738. }
  739. light_data.transmittance_bias = light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
  740. Vector2i omni_offset;
  741. Rect2 rect = light_instance_get_shadow_atlas_rect(light_instance->self, p_shadow_atlas, omni_offset);
  742. light_data.atlas_rect[0] = rect.position.x;
  743. light_data.atlas_rect[1] = rect.position.y;
  744. light_data.atlas_rect[2] = rect.size.width;
  745. light_data.atlas_rect[3] = rect.size.height;
  746. light_data.soft_shadow_scale = light->param[RS::LIGHT_PARAM_SHADOW_BLUR];
  747. if (type == RS::LIGHT_OMNI) {
  748. Transform3D proj = (inverse_transform * light_transform).inverse();
  749. RendererRD::MaterialStorage::store_transform(proj, light_data.shadow_matrix);
  750. if (size > 0.0 && light_data.soft_shadow_scale > 0.0) {
  751. // Only enable PCSS-like soft shadows if blurring is enabled.
  752. // Otherwise, performance would decrease with no visual difference.
  753. light_data.soft_shadow_size = size;
  754. } else {
  755. light_data.soft_shadow_size = 0.0;
  756. light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->shadows_quality_radius_get(); // Only use quality radius for PCF
  757. }
  758. light_data.direction[0] = omni_offset.x * float(rect.size.width);
  759. light_data.direction[1] = omni_offset.y * float(rect.size.height);
  760. } else if (type == RS::LIGHT_SPOT) {
  761. Transform3D modelview = (inverse_transform * light_transform).inverse();
  762. Projection bias;
  763. bias.set_light_bias();
  764. Projection cm = light_instance->shadow_transform[0].camera;
  765. Projection shadow_mtx = bias * cm * modelview;
  766. RendererRD::MaterialStorage::store_camera(shadow_mtx, light_data.shadow_matrix);
  767. if (size > 0.0 && light_data.soft_shadow_scale > 0.0) {
  768. // Only enable PCSS-like soft shadows if blurring is enabled.
  769. // Otherwise, performance would decrease with no visual difference.
  770. float half_np = cm.get_z_near() * Math::tan(Math::deg_to_rad(spot_angle));
  771. light_data.soft_shadow_size = (size * 0.5 / radius) / (half_np / cm.get_z_near()) * rect.size.width;
  772. } else {
  773. light_data.soft_shadow_size = 0.0;
  774. light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->shadows_quality_radius_get(); // Only use quality radius for PCF
  775. }
  776. light_data.shadow_bias *= light_data.soft_shadow_scale;
  777. }
  778. } else {
  779. light_data.shadow_opacity = 0.0;
  780. }
  781. light_instance->cull_mask = light->cull_mask;
  782. // hook for subclass to do further processing.
  783. RendererSceneRenderRD::get_singleton()->setup_added_light(type, light_transform, radius, spot_angle);
  784. r_positional_light_count++;
  785. }
  786. //update without barriers
  787. if (omni_light_count) {
  788. RD::get_singleton()->buffer_update(omni_light_buffer, 0, sizeof(LightData) * omni_light_count, omni_lights);
  789. }
  790. if (spot_light_count) {
  791. RD::get_singleton()->buffer_update(spot_light_buffer, 0, sizeof(LightData) * spot_light_count, spot_lights);
  792. }
  793. if (r_directional_light_count) {
  794. RD::get_singleton()->buffer_update(directional_light_buffer, 0, sizeof(DirectionalLightData) * r_directional_light_count, directional_lights);
  795. }
  796. }
  797. /* REFLECTION PROBE */
  798. RID LightStorage::reflection_probe_allocate() {
  799. return reflection_probe_owner.allocate_rid();
  800. }
  801. void LightStorage::reflection_probe_initialize(RID p_reflection_probe) {
  802. reflection_probe_owner.initialize_rid(p_reflection_probe, ReflectionProbe());
  803. }
  804. void LightStorage::reflection_probe_free(RID p_rid) {
  805. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_rid);
  806. reflection_probe->dependency.deleted_notify(p_rid);
  807. reflection_probe_owner.free(p_rid);
  808. };
  809. void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  810. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  811. ERR_FAIL_NULL(reflection_probe);
  812. reflection_probe->update_mode = p_mode;
  813. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  814. }
  815. void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  816. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  817. ERR_FAIL_NULL(reflection_probe);
  818. reflection_probe->intensity = p_intensity;
  819. }
  820. void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  821. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  822. ERR_FAIL_NULL(reflection_probe);
  823. reflection_probe->ambient_mode = p_mode;
  824. }
  825. void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  826. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  827. ERR_FAIL_NULL(reflection_probe);
  828. reflection_probe->ambient_color = p_color;
  829. }
  830. void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  831. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  832. ERR_FAIL_NULL(reflection_probe);
  833. reflection_probe->ambient_color_energy = p_energy;
  834. }
  835. void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  836. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  837. ERR_FAIL_NULL(reflection_probe);
  838. reflection_probe->max_distance = p_distance;
  839. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  840. }
  841. void LightStorage::reflection_probe_set_size(RID p_probe, const Vector3 &p_size) {
  842. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  843. ERR_FAIL_NULL(reflection_probe);
  844. if (reflection_probe->size == p_size) {
  845. return;
  846. }
  847. reflection_probe->size = p_size;
  848. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  849. }
  850. void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  851. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  852. ERR_FAIL_NULL(reflection_probe);
  853. reflection_probe->origin_offset = p_offset;
  854. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  855. }
  856. void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  857. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  858. ERR_FAIL_NULL(reflection_probe);
  859. reflection_probe->interior = p_enable;
  860. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  861. }
  862. void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  863. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  864. ERR_FAIL_NULL(reflection_probe);
  865. reflection_probe->box_projection = p_enable;
  866. }
  867. void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  868. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  869. ERR_FAIL_NULL(reflection_probe);
  870. reflection_probe->enable_shadows = p_enable;
  871. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  872. }
  873. void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  874. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  875. ERR_FAIL_NULL(reflection_probe);
  876. reflection_probe->cull_mask = p_layers;
  877. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  878. }
  879. void LightStorage::reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) {
  880. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  881. ERR_FAIL_NULL(reflection_probe);
  882. reflection_probe->reflection_mask = p_layers;
  883. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  884. }
  885. void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  886. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  887. ERR_FAIL_NULL(reflection_probe);
  888. ERR_FAIL_COND(p_resolution < 32);
  889. reflection_probe->resolution = p_resolution;
  890. }
  891. void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
  892. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  893. ERR_FAIL_NULL(reflection_probe);
  894. reflection_probe->mesh_lod_threshold = p_ratio;
  895. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  896. }
  897. void LightStorage::reflection_probe_set_baked_exposure(RID p_probe, float p_exposure) {
  898. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  899. ERR_FAIL_NULL(reflection_probe);
  900. reflection_probe->baked_exposure = p_exposure;
  901. }
  902. AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
  903. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  904. ERR_FAIL_NULL_V(reflection_probe, AABB());
  905. AABB aabb;
  906. aabb.position = -reflection_probe->size / 2;
  907. aabb.size = reflection_probe->size;
  908. return aabb;
  909. }
  910. RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
  911. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  912. ERR_FAIL_NULL_V(reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  913. return reflection_probe->update_mode;
  914. }
  915. uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
  916. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  917. ERR_FAIL_NULL_V(reflection_probe, 0);
  918. return reflection_probe->cull_mask;
  919. }
  920. uint32_t LightStorage::reflection_probe_get_reflection_mask(RID p_probe) const {
  921. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  922. ERR_FAIL_NULL_V(reflection_probe, 0);
  923. return reflection_probe->reflection_mask;
  924. }
  925. Vector3 LightStorage::reflection_probe_get_size(RID p_probe) const {
  926. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  927. ERR_FAIL_NULL_V(reflection_probe, Vector3());
  928. return reflection_probe->size;
  929. }
  930. Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
  931. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  932. ERR_FAIL_NULL_V(reflection_probe, Vector3());
  933. return reflection_probe->origin_offset;
  934. }
  935. bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
  936. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  937. ERR_FAIL_NULL_V(reflection_probe, false);
  938. return reflection_probe->enable_shadows;
  939. }
  940. float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
  941. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  942. ERR_FAIL_NULL_V(reflection_probe, 0);
  943. return reflection_probe->max_distance;
  944. }
  945. float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
  946. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  947. ERR_FAIL_NULL_V(reflection_probe, 0);
  948. return reflection_probe->mesh_lod_threshold;
  949. }
  950. int LightStorage::reflection_probe_get_resolution(RID p_probe) const {
  951. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  952. ERR_FAIL_NULL_V(reflection_probe, 0);
  953. return reflection_probe->resolution;
  954. }
  955. float LightStorage::reflection_probe_get_baked_exposure(RID p_probe) const {
  956. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  957. ERR_FAIL_NULL_V(reflection_probe, 1.0);
  958. return reflection_probe->baked_exposure;
  959. }
  960. float LightStorage::reflection_probe_get_intensity(RID p_probe) const {
  961. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  962. ERR_FAIL_NULL_V(reflection_probe, 0);
  963. return reflection_probe->intensity;
  964. }
  965. bool LightStorage::reflection_probe_is_interior(RID p_probe) const {
  966. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  967. ERR_FAIL_NULL_V(reflection_probe, false);
  968. return reflection_probe->interior;
  969. }
  970. bool LightStorage::reflection_probe_is_box_projection(RID p_probe) const {
  971. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  972. ERR_FAIL_NULL_V(reflection_probe, false);
  973. return reflection_probe->box_projection;
  974. }
  975. RS::ReflectionProbeAmbientMode LightStorage::reflection_probe_get_ambient_mode(RID p_probe) const {
  976. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  977. ERR_FAIL_NULL_V(reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
  978. return reflection_probe->ambient_mode;
  979. }
  980. Color LightStorage::reflection_probe_get_ambient_color(RID p_probe) const {
  981. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  982. ERR_FAIL_NULL_V(reflection_probe, Color());
  983. return reflection_probe->ambient_color;
  984. }
  985. float LightStorage::reflection_probe_get_ambient_color_energy(RID p_probe) const {
  986. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  987. ERR_FAIL_NULL_V(reflection_probe, 0);
  988. return reflection_probe->ambient_color_energy;
  989. }
  990. Dependency *LightStorage::reflection_probe_get_dependency(RID p_probe) const {
  991. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  992. ERR_FAIL_NULL_V(reflection_probe, nullptr);
  993. return &reflection_probe->dependency;
  994. }
  995. /* REFLECTION ATLAS */
  996. RID LightStorage::reflection_atlas_create() {
  997. ReflectionAtlas ra;
  998. ra.count = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_count");
  999. ra.size = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_size");
  1000. ra.cluster_builder = nullptr;
  1001. return reflection_atlas_owner.make_rid(ra);
  1002. }
  1003. void LightStorage::reflection_atlas_free(RID p_ref_atlas) {
  1004. reflection_atlas_set_size(p_ref_atlas, 0, 0);
  1005. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
  1006. if (ra->cluster_builder) {
  1007. memdelete(ra->cluster_builder);
  1008. }
  1009. reflection_atlas_owner.free(p_ref_atlas);
  1010. }
  1011. void LightStorage::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  1012. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
  1013. ERR_FAIL_NULL(ra);
  1014. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  1015. return; //no changes
  1016. }
  1017. if (ra->cluster_builder) {
  1018. // only if we're using our cluster
  1019. ra->cluster_builder->setup(Size2i(ra->size, ra->size), max_cluster_elements, RID(), RID(), RID());
  1020. }
  1021. ra->size = p_reflection_size;
  1022. ra->count = p_reflection_count;
  1023. if (ra->reflection.is_valid()) {
  1024. //clear and invalidate everything
  1025. RD::get_singleton()->free(ra->reflection);
  1026. ra->reflection = RID();
  1027. RD::get_singleton()->free(ra->depth_buffer);
  1028. ra->depth_buffer = RID();
  1029. for (int i = 0; i < ra->reflections.size(); i++) {
  1030. ra->reflections.write[i].data.clear_reflection_data();
  1031. if (ra->reflections[i].owner.is_null()) {
  1032. continue;
  1033. }
  1034. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  1035. //rp->atlasindex clear
  1036. }
  1037. ra->reflections.clear();
  1038. }
  1039. if (ra->render_buffers.is_valid()) {
  1040. ra->render_buffers->cleanup();
  1041. }
  1042. }
  1043. int LightStorage::reflection_atlas_get_size(RID p_ref_atlas) const {
  1044. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
  1045. ERR_FAIL_NULL_V(ra, 0);
  1046. return ra->size;
  1047. }
  1048. /* REFLECTION PROBE INSTANCE */
  1049. RID LightStorage::reflection_probe_instance_create(RID p_probe) {
  1050. ReflectionProbeInstance rpi;
  1051. rpi.probe = p_probe;
  1052. rpi.forward_id = ForwardIDStorage::get_singleton()->allocate_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE);
  1053. return reflection_probe_instance_owner.make_rid(rpi);
  1054. }
  1055. void LightStorage::reflection_probe_instance_free(RID p_instance) {
  1056. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1057. ForwardIDStorage::get_singleton()->free_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE, rpi->forward_id);
  1058. reflection_probe_release_atlas_index(p_instance);
  1059. reflection_probe_instance_owner.free(p_instance);
  1060. }
  1061. void LightStorage::reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) {
  1062. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1063. ERR_FAIL_NULL(rpi);
  1064. rpi->transform = p_transform;
  1065. rpi->dirty = true;
  1066. }
  1067. bool LightStorage::reflection_probe_has_atlas_index(RID p_instance) {
  1068. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1069. ERR_FAIL_NULL_V(rpi, false);
  1070. if (rpi->atlas.is_null()) {
  1071. return false;
  1072. }
  1073. return rpi->atlas_index >= 0;
  1074. }
  1075. void LightStorage::reflection_probe_release_atlas_index(RID p_instance) {
  1076. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1077. ERR_FAIL_NULL(rpi);
  1078. if (rpi->atlas.is_null()) {
  1079. return; //nothing to release
  1080. }
  1081. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  1082. ERR_FAIL_NULL(atlas);
  1083. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  1084. atlas->reflections.write[rpi->atlas_index].owner = RID();
  1085. // TODO investigate if this is enough? shouldn't we be freeing our textures and framebuffers?
  1086. if (rpi->rendering) {
  1087. // We were cancelled mid rendering, trigger refresh.
  1088. rpi->rendering = false;
  1089. rpi->dirty = true;
  1090. rpi->processing_layer = 1;
  1091. rpi->processing_side = 0;
  1092. }
  1093. rpi->atlas_index = -1;
  1094. rpi->atlas = RID();
  1095. }
  1096. bool LightStorage::reflection_probe_instance_needs_redraw(RID p_instance) {
  1097. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1098. ERR_FAIL_NULL_V(rpi, false);
  1099. if (rpi->rendering) {
  1100. return false;
  1101. }
  1102. if (rpi->dirty) {
  1103. return true;
  1104. }
  1105. if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  1106. return true;
  1107. }
  1108. return rpi->atlas_index == -1;
  1109. }
  1110. bool LightStorage::reflection_probe_instance_has_reflection(RID p_instance) {
  1111. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1112. ERR_FAIL_NULL_V(rpi, false);
  1113. return rpi->atlas.is_valid();
  1114. }
  1115. bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  1116. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
  1117. ERR_FAIL_NULL_V(atlas, false);
  1118. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1119. ERR_FAIL_NULL_V(rpi, false);
  1120. if (atlas->render_buffers.is_null()) {
  1121. atlas->render_buffers.instantiate();
  1122. }
  1123. RD::get_singleton()->draw_command_begin_label("Reflection probe render");
  1124. if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->size != 256) {
  1125. WARN_PRINT("ReflectionProbes set to UPDATE_ALWAYS must have an atlas size of 256. Please update the atlas size in the ProjectSettings.");
  1126. reflection_atlas_set_size(p_reflection_atlas, 256, atlas->count);
  1127. }
  1128. if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->reflections[0].data.layers[0].mipmaps.size() != 8) {
  1129. // Invalidate reflection atlas, need to regenerate
  1130. RD::get_singleton()->free(atlas->reflection);
  1131. atlas->reflection = RID();
  1132. for (int i = 0; i < atlas->reflections.size(); i++) {
  1133. if (atlas->reflections[i].owner.is_null()) {
  1134. continue;
  1135. }
  1136. reflection_probe_release_atlas_index(atlas->reflections[i].owner);
  1137. }
  1138. atlas->reflections.clear();
  1139. }
  1140. if (atlas->reflection.is_null()) {
  1141. int mipmaps = MIN(RendererSceneRenderRD::get_singleton()->get_sky()->roughness_layers, Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) + 1);
  1142. mipmaps = LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS ? 8 : mipmaps; // always use 8 mipmaps with real time filtering
  1143. {
  1144. //reflection atlas was unused, create:
  1145. RD::TextureFormat tf;
  1146. tf.array_layers = 6 * atlas->count;
  1147. tf.format = RendererSceneRenderRD::get_singleton()->_render_buffers_get_color_format();
  1148. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1149. tf.mipmaps = mipmaps;
  1150. tf.width = atlas->size;
  1151. tf.height = atlas->size;
  1152. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage() ? RD::TEXTURE_USAGE_STORAGE_BIT : 0);
  1153. atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1154. }
  1155. {
  1156. RD::TextureFormat tf;
  1157. 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;
  1158. tf.width = atlas->size;
  1159. tf.height = atlas->size;
  1160. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1161. atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1162. }
  1163. atlas->reflections.resize(atlas->count);
  1164. for (int i = 0; i < atlas->count; i++) {
  1165. atlas->reflections.write[i].data.update_reflection_data(atlas->size, mipmaps, false, atlas->reflection, i * 6, LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS, RendererSceneRenderRD::get_singleton()->get_sky()->roughness_layers, RendererSceneRenderRD::get_singleton()->_render_buffers_get_color_format());
  1166. for (int j = 0; j < 6; j++) {
  1167. atlas->reflections.write[i].fbs[j] = RendererSceneRenderRD::get_singleton()->reflection_probe_create_framebuffer(atlas->reflections.write[i].data.layers[0].mipmaps[0].views[j], atlas->depth_buffer);
  1168. }
  1169. }
  1170. Vector<RID> fb;
  1171. fb.push_back(atlas->depth_buffer);
  1172. atlas->depth_fb = RD::get_singleton()->framebuffer_create(fb);
  1173. atlas->render_buffers->configure_for_reflections(Size2i(atlas->size, atlas->size));
  1174. }
  1175. if (rpi->atlas_index == -1) {
  1176. for (int i = 0; i < atlas->reflections.size(); i++) {
  1177. if (atlas->reflections[i].owner.is_null()) {
  1178. rpi->atlas_index = i;
  1179. break;
  1180. }
  1181. }
  1182. //find the one used last
  1183. if (rpi->atlas_index == -1) {
  1184. //everything is in use, find the one least used via LRU
  1185. uint64_t pass_min = 0;
  1186. for (int i = 0; i < atlas->reflections.size(); i++) {
  1187. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.get_or_null(atlas->reflections[i].owner);
  1188. if (rpi2->last_pass < pass_min) {
  1189. pass_min = rpi2->last_pass;
  1190. rpi->atlas_index = i;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. if (rpi->atlas_index != -1) { // should we fail if this is still -1 ?
  1196. atlas->reflections.write[rpi->atlas_index].owner = p_instance;
  1197. }
  1198. rpi->atlas = p_reflection_atlas;
  1199. rpi->rendering = true;
  1200. rpi->dirty = false;
  1201. rpi->processing_layer = 1;
  1202. rpi->processing_side = 0;
  1203. RD::get_singleton()->draw_command_end_label();
  1204. return true;
  1205. }
  1206. Ref<RenderSceneBuffers> LightStorage::reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) {
  1207. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
  1208. ERR_FAIL_NULL_V(atlas, Ref<RenderSceneBuffersRD>());
  1209. return atlas->render_buffers;
  1210. }
  1211. bool LightStorage::reflection_probe_instance_postprocess_step(RID p_instance) {
  1212. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1213. ERR_FAIL_NULL_V(rpi, false);
  1214. ERR_FAIL_COND_V(!rpi->rendering, false);
  1215. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  1216. if (!atlas || rpi->atlas_index == -1) {
  1217. // Does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering).
  1218. rpi->rendering = false;
  1219. return false;
  1220. }
  1221. if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  1222. // Using real time reflections, all roughness is done in one step
  1223. atlas->reflections.write[rpi->atlas_index].data.create_reflection_fast_filter(false);
  1224. rpi->rendering = false;
  1225. rpi->processing_side = 0;
  1226. rpi->processing_layer = 1;
  1227. return true;
  1228. }
  1229. if (rpi->processing_layer > 1) {
  1230. atlas->reflections.write[rpi->atlas_index].data.create_reflection_importance_sample(false, 10, rpi->processing_layer, RendererSceneRenderRD::get_singleton()->get_sky()->sky_ggx_samples_quality);
  1231. rpi->processing_layer++;
  1232. if (rpi->processing_layer == atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
  1233. rpi->rendering = false;
  1234. rpi->processing_side = 0;
  1235. rpi->processing_layer = 1;
  1236. return true;
  1237. }
  1238. return false;
  1239. } else {
  1240. atlas->reflections.write[rpi->atlas_index].data.create_reflection_importance_sample(false, rpi->processing_side, rpi->processing_layer, RendererSceneRenderRD::get_singleton()->get_sky()->sky_ggx_samples_quality);
  1241. }
  1242. rpi->processing_side++;
  1243. if (rpi->processing_side == 6) {
  1244. rpi->processing_side = 0;
  1245. rpi->processing_layer++;
  1246. if (rpi->processing_layer == atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
  1247. rpi->rendering = false;
  1248. rpi->processing_layer = 1;
  1249. return true;
  1250. }
  1251. }
  1252. return false;
  1253. }
  1254. uint32_t LightStorage::reflection_probe_instance_get_resolution(RID p_instance) {
  1255. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1256. ERR_FAIL_NULL_V(rpi, 0);
  1257. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  1258. ERR_FAIL_NULL_V(atlas, 0);
  1259. return atlas->size;
  1260. }
  1261. RID LightStorage::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  1262. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1263. ERR_FAIL_NULL_V(rpi, RID());
  1264. ERR_FAIL_INDEX_V(p_index, 6, RID());
  1265. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  1266. ERR_FAIL_NULL_V(atlas, RID());
  1267. return atlas->reflections[rpi->atlas_index].fbs[p_index];
  1268. }
  1269. RID LightStorage::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
  1270. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1271. ERR_FAIL_NULL_V(rpi, RID());
  1272. ERR_FAIL_INDEX_V(p_index, 6, RID());
  1273. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  1274. ERR_FAIL_NULL_V(atlas, RID());
  1275. return atlas->depth_fb;
  1276. }
  1277. ClusterBuilderRD *LightStorage::reflection_probe_instance_get_cluster_builder(RID p_instance, ClusterBuilderSharedDataRD *p_cluster_builder_shared) {
  1278. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  1279. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(rpi->atlas);
  1280. if (!ra) {
  1281. ERR_PRINT("reflection probe has no reflection atlas! Bug?");
  1282. return nullptr;
  1283. } else {
  1284. if (ra->cluster_builder == nullptr) {
  1285. ra->cluster_builder = memnew(ClusterBuilderRD);
  1286. ra->cluster_builder->set_shared(p_cluster_builder_shared);
  1287. ra->cluster_builder->setup(Size2i(ra->size, ra->size), get_max_cluster_elements(), RID(), RID(), RID());
  1288. }
  1289. return ra->cluster_builder;
  1290. }
  1291. }
  1292. /* REFLECTION DATA */
  1293. void LightStorage::free_reflection_data() {
  1294. if (reflection_buffer.is_valid()) {
  1295. RD::get_singleton()->free(reflection_buffer);
  1296. reflection_buffer = RID();
  1297. }
  1298. if (reflections != nullptr) {
  1299. memdelete_arr(reflections);
  1300. reflections = nullptr;
  1301. }
  1302. if (reflection_sort != nullptr) {
  1303. memdelete_arr(reflection_sort);
  1304. reflection_sort = nullptr;
  1305. }
  1306. }
  1307. void LightStorage::set_max_reflection_probes(const uint32_t p_max_reflection_probes) {
  1308. max_reflections = p_max_reflection_probes;
  1309. reflections = memnew_arr(ReflectionData, max_reflections);
  1310. reflection_sort = memnew_arr(ReflectionProbeInstanceSort, max_reflections);
  1311. reflection_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ReflectionData) * max_reflections);
  1312. }
  1313. void LightStorage::update_reflection_probe_buffer(RenderDataRD *p_render_data, const PagedArray<RID> &p_reflections, const Transform3D &p_camera_inverse_transform, RID p_environment) {
  1314. ForwardIDStorage *forward_id_storage = ForwardIDStorage::get_singleton();
  1315. reflection_count = 0;
  1316. for (uint32_t i = 0; i < (uint32_t)p_reflections.size(); i++) {
  1317. if (reflection_count == max_reflections) {
  1318. break;
  1319. }
  1320. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_reflections[i]);
  1321. if (!rpi) {
  1322. continue;
  1323. }
  1324. Transform3D transform = rpi->transform;
  1325. reflection_sort[reflection_count].probe_instance = rpi;
  1326. reflection_sort[reflection_count].depth = -p_camera_inverse_transform.xform(transform.origin).z;
  1327. reflection_count++;
  1328. }
  1329. if (reflection_count > 0) {
  1330. SortArray<ReflectionProbeInstanceSort> sort_array;
  1331. sort_array.sort(reflection_sort, reflection_count);
  1332. }
  1333. bool using_forward_ids = forward_id_storage->uses_forward_ids();
  1334. for (uint32_t i = 0; i < reflection_count; i++) {
  1335. ReflectionProbeInstance *rpi = reflection_sort[i].probe_instance;
  1336. rpi->last_pass = RSG::rasterizer->get_frame_number();
  1337. if (using_forward_ids) {
  1338. forward_id_storage->map_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE, rpi->forward_id, i, rpi->last_pass);
  1339. }
  1340. ReflectionProbe *probe = reflection_probe_owner.get_or_null(rpi->probe);
  1341. ReflectionData &reflection_ubo = reflections[i];
  1342. Vector3 extents = probe->size / 2;
  1343. rpi->cull_mask = probe->reflection_mask;
  1344. reflection_ubo.box_extents[0] = extents.x;
  1345. reflection_ubo.box_extents[1] = extents.y;
  1346. reflection_ubo.box_extents[2] = extents.z;
  1347. reflection_ubo.index = rpi->atlas_index;
  1348. Vector3 origin_offset = probe->origin_offset;
  1349. reflection_ubo.box_offset[0] = origin_offset.x;
  1350. reflection_ubo.box_offset[1] = origin_offset.y;
  1351. reflection_ubo.box_offset[2] = origin_offset.z;
  1352. reflection_ubo.mask = probe->reflection_mask;
  1353. reflection_ubo.intensity = probe->intensity;
  1354. reflection_ubo.ambient_mode = probe->ambient_mode;
  1355. reflection_ubo.exterior = !probe->interior;
  1356. reflection_ubo.box_project = probe->box_projection;
  1357. reflection_ubo.exposure_normalization = 1.0;
  1358. if (p_render_data->camera_attributes.is_valid()) {
  1359. float exposure = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1360. reflection_ubo.exposure_normalization = exposure / probe->baked_exposure;
  1361. }
  1362. Color ambient_linear = probe->ambient_color.srgb_to_linear();
  1363. float interior_ambient_energy = probe->ambient_color_energy;
  1364. reflection_ubo.ambient[0] = ambient_linear.r * interior_ambient_energy;
  1365. reflection_ubo.ambient[1] = ambient_linear.g * interior_ambient_energy;
  1366. reflection_ubo.ambient[2] = ambient_linear.b * interior_ambient_energy;
  1367. Transform3D transform = rpi->transform;
  1368. Transform3D proj = (p_camera_inverse_transform * transform).inverse();
  1369. MaterialStorage::store_transform(proj, reflection_ubo.local_matrix);
  1370. // hook for subclass to do further processing.
  1371. RendererSceneRenderRD::get_singleton()->setup_added_reflection_probe(transform, extents);
  1372. }
  1373. if (reflection_count) {
  1374. RD::get_singleton()->buffer_update(reflection_buffer, 0, reflection_count * sizeof(ReflectionData), reflections);
  1375. }
  1376. }
  1377. /* LIGHTMAP API */
  1378. RID LightStorage::lightmap_allocate() {
  1379. return lightmap_owner.allocate_rid();
  1380. }
  1381. void LightStorage::lightmap_initialize(RID p_lightmap) {
  1382. lightmap_owner.initialize_rid(p_lightmap, Lightmap());
  1383. }
  1384. void LightStorage::lightmap_free(RID p_rid) {
  1385. lightmap_set_textures(p_rid, RID(), false);
  1386. Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
  1387. lightmap->dependency.deleted_notify(p_rid);
  1388. lightmap_owner.free(p_rid);
  1389. }
  1390. void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  1391. TextureStorage *texture_storage = TextureStorage::get_singleton();
  1392. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1393. ERR_FAIL_NULL(lm);
  1394. lightmap_array_version++;
  1395. //erase lightmap users
  1396. if (lm->light_texture.is_valid()) {
  1397. TextureStorage::Texture *t = texture_storage->get_texture(lm->light_texture);
  1398. if (t) {
  1399. t->lightmap_users.erase(p_lightmap);
  1400. }
  1401. }
  1402. TextureStorage::Texture *t = texture_storage->get_texture(p_light);
  1403. lm->light_texture = p_light;
  1404. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  1405. RID default_2d_array = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  1406. if (!t) {
  1407. if (using_lightmap_array) {
  1408. if (lm->array_index >= 0) {
  1409. lightmap_textures.write[lm->array_index] = default_2d_array;
  1410. lm->array_index = -1;
  1411. }
  1412. }
  1413. return;
  1414. }
  1415. t->lightmap_users.insert(p_lightmap);
  1416. if (using_lightmap_array) {
  1417. if (lm->array_index < 0) {
  1418. //not in array, try to put in array
  1419. for (int i = 0; i < lightmap_textures.size(); i++) {
  1420. if (lightmap_textures[i] == default_2d_array) {
  1421. lm->array_index = i;
  1422. break;
  1423. }
  1424. }
  1425. }
  1426. ERR_FAIL_COND_MSG(lm->array_index < 0, "Maximum amount of lightmaps in use (" + itos(lightmap_textures.size()) + ") has been exceeded, lightmap will nod display properly.");
  1427. lightmap_textures.write[lm->array_index] = t->rd_texture;
  1428. }
  1429. }
  1430. void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  1431. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1432. ERR_FAIL_NULL(lm);
  1433. lm->bounds = p_bounds;
  1434. }
  1435. void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  1436. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1437. ERR_FAIL_NULL(lm);
  1438. lm->interior = p_interior;
  1439. }
  1440. void LightStorage::lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) {
  1441. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1442. ERR_FAIL_NULL(lm);
  1443. if (p_points.size()) {
  1444. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  1445. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  1446. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  1447. }
  1448. lm->points = p_points;
  1449. lm->bsp_tree = p_bsp_tree;
  1450. lm->point_sh = p_point_sh;
  1451. lm->tetrahedra = p_tetrahedra;
  1452. }
  1453. void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
  1454. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1455. ERR_FAIL_NULL(lm);
  1456. lm->baked_exposure = p_exposure;
  1457. }
  1458. PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
  1459. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1460. ERR_FAIL_NULL_V(lm, PackedVector3Array());
  1461. return lm->points;
  1462. }
  1463. PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  1464. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1465. ERR_FAIL_NULL_V(lm, PackedColorArray());
  1466. return lm->point_sh;
  1467. }
  1468. PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  1469. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1470. ERR_FAIL_NULL_V(lm, PackedInt32Array());
  1471. return lm->tetrahedra;
  1472. }
  1473. PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  1474. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1475. ERR_FAIL_NULL_V(lm, PackedInt32Array());
  1476. return lm->bsp_tree;
  1477. }
  1478. void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
  1479. lightmap_probe_capture_update_speed = p_speed;
  1480. }
  1481. Dependency *LightStorage::lightmap_get_dependency(RID p_lightmap) const {
  1482. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1483. ERR_FAIL_NULL_V(lm, nullptr);
  1484. return &lm->dependency;
  1485. }
  1486. void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  1487. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1488. ERR_FAIL_NULL(lm);
  1489. for (int i = 0; i < 9; i++) {
  1490. r_sh[i] = Color(0, 0, 0, 0);
  1491. }
  1492. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  1493. return;
  1494. }
  1495. static_assert(sizeof(Lightmap::BSP) == 24);
  1496. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  1497. int32_t node = 0;
  1498. while (node >= 0) {
  1499. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  1500. #ifdef DEBUG_ENABLED
  1501. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  1502. #endif
  1503. node = bsp[node].over;
  1504. } else {
  1505. #ifdef DEBUG_ENABLED
  1506. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  1507. #endif
  1508. node = bsp[node].under;
  1509. }
  1510. }
  1511. if (node == Lightmap::BSP::EMPTY_LEAF) {
  1512. return; //nothing could be done
  1513. }
  1514. node = ABS(node) - 1;
  1515. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  1516. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  1517. const Color *sh_colors[4]{ &lm->point_sh[tetrahedron[0] * 9], &lm->point_sh[tetrahedron[1] * 9], &lm->point_sh[tetrahedron[2] * 9], &lm->point_sh[tetrahedron[3] * 9] };
  1518. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  1519. for (int i = 0; i < 4; i++) {
  1520. float c = CLAMP(barycentric[i], 0.0, 1.0);
  1521. for (int j = 0; j < 9; j++) {
  1522. r_sh[j] += sh_colors[i][j] * c;
  1523. }
  1524. }
  1525. }
  1526. bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
  1527. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1528. ERR_FAIL_NULL_V(lm, false);
  1529. return lm->interior;
  1530. }
  1531. AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
  1532. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1533. ERR_FAIL_NULL_V(lm, AABB());
  1534. return lm->bounds;
  1535. }
  1536. /* LIGHTMAP INSTANCE */
  1537. RID LightStorage::lightmap_instance_create(RID p_lightmap) {
  1538. LightmapInstance li;
  1539. li.lightmap = p_lightmap;
  1540. return lightmap_instance_owner.make_rid(li);
  1541. }
  1542. void LightStorage::lightmap_instance_free(RID p_lightmap) {
  1543. lightmap_instance_owner.free(p_lightmap);
  1544. }
  1545. void LightStorage::lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) {
  1546. LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap);
  1547. ERR_FAIL_NULL(li);
  1548. li->transform = p_transform;
  1549. }
  1550. /* SHADOW ATLAS API */
  1551. RID LightStorage::shadow_atlas_create() {
  1552. return shadow_atlas_owner.make_rid(ShadowAtlas());
  1553. }
  1554. void LightStorage::shadow_atlas_free(RID p_atlas) {
  1555. shadow_atlas_set_size(p_atlas, 0);
  1556. shadow_atlas_owner.free(p_atlas);
  1557. }
  1558. void LightStorage::_update_shadow_atlas(ShadowAtlas *shadow_atlas) {
  1559. if (shadow_atlas->size > 0 && shadow_atlas->depth.is_null()) {
  1560. RD::TextureFormat tf;
  1561. tf.format = shadow_atlas->use_16_bits ? RD::DATA_FORMAT_D16_UNORM : RD::DATA_FORMAT_D32_SFLOAT;
  1562. tf.width = shadow_atlas->size;
  1563. tf.height = shadow_atlas->size;
  1564. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1565. shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1566. Vector<RID> fb_tex;
  1567. fb_tex.push_back(shadow_atlas->depth);
  1568. shadow_atlas->fb = RD::get_singleton()->framebuffer_create(fb_tex);
  1569. }
  1570. }
  1571. void LightStorage::shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits) {
  1572. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1573. ERR_FAIL_NULL(shadow_atlas);
  1574. ERR_FAIL_COND(p_size < 0);
  1575. p_size = next_power_of_2(p_size);
  1576. if (p_size == shadow_atlas->size && p_16_bits == shadow_atlas->use_16_bits) {
  1577. return;
  1578. }
  1579. // erasing atlas
  1580. if (shadow_atlas->depth.is_valid()) {
  1581. RD::get_singleton()->free(shadow_atlas->depth);
  1582. shadow_atlas->depth = RID();
  1583. }
  1584. for (int i = 0; i < 4; i++) {
  1585. //clear subdivisions
  1586. shadow_atlas->quadrants[i].shadows.clear();
  1587. shadow_atlas->quadrants[i].shadows.resize(int64_t(1) << int64_t(shadow_atlas->quadrants[i].subdivision));
  1588. }
  1589. //erase shadow atlas reference from lights
  1590. for (const KeyValue<RID, uint32_t> &E : shadow_atlas->shadow_owners) {
  1591. LightInstance *li = light_instance_owner.get_or_null(E.key);
  1592. ERR_CONTINUE(!li);
  1593. li->shadow_atlases.erase(p_atlas);
  1594. }
  1595. //clear owners
  1596. shadow_atlas->shadow_owners.clear();
  1597. shadow_atlas->size = p_size;
  1598. shadow_atlas->use_16_bits = p_16_bits;
  1599. }
  1600. void LightStorage::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  1601. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1602. ERR_FAIL_NULL(shadow_atlas);
  1603. ERR_FAIL_INDEX(p_quadrant, 4);
  1604. ERR_FAIL_INDEX(p_subdivision, 16384);
  1605. uint32_t subdiv = next_power_of_2(p_subdivision);
  1606. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  1607. subdiv <<= 1;
  1608. }
  1609. subdiv = int(Math::sqrt((float)subdiv));
  1610. //obtain the number that will be x*x
  1611. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  1612. return;
  1613. }
  1614. //erase all data from quadrant
  1615. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  1616. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  1617. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  1618. LightInstance *li = light_instance_owner.get_or_null(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  1619. ERR_CONTINUE(!li);
  1620. li->shadow_atlases.erase(p_atlas);
  1621. }
  1622. }
  1623. shadow_atlas->quadrants[p_quadrant].shadows.clear();
  1624. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  1625. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  1626. //cache the smallest subdiv (for faster allocation in light update)
  1627. shadow_atlas->smallest_subdiv = 1 << 30;
  1628. for (int i = 0; i < 4; i++) {
  1629. if (shadow_atlas->quadrants[i].subdivision) {
  1630. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  1631. }
  1632. }
  1633. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  1634. shadow_atlas->smallest_subdiv = 0;
  1635. }
  1636. //resort the size orders, simple bublesort for 4 elements..
  1637. int swaps = 0;
  1638. do {
  1639. swaps = 0;
  1640. for (int i = 0; i < 3; i++) {
  1641. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  1642. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  1643. swaps++;
  1644. }
  1645. }
  1646. } while (swaps > 0);
  1647. }
  1648. bool LightStorage::_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) {
  1649. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  1650. int qidx = p_in_quadrants[i];
  1651. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  1652. return false;
  1653. }
  1654. //look for an empty space
  1655. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  1656. const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
  1657. int found_free_idx = -1; //found a free one
  1658. int found_used_idx = -1; //found existing one, must steal it
  1659. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  1660. for (int j = 0; j < sc; j++) {
  1661. if (!sarr[j].owner.is_valid()) {
  1662. found_free_idx = j;
  1663. break;
  1664. }
  1665. LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
  1666. ERR_CONTINUE(!sli);
  1667. if (sli->last_scene_pass != RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
  1668. //was just allocated, don't kill it so soon, wait a bit..
  1669. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  1670. continue;
  1671. }
  1672. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  1673. found_used_idx = j;
  1674. min_pass = sli->last_scene_pass;
  1675. }
  1676. }
  1677. }
  1678. if (found_free_idx == -1 && found_used_idx == -1) {
  1679. continue; //nothing found
  1680. }
  1681. if (found_free_idx == -1 && found_used_idx != -1) {
  1682. found_free_idx = found_used_idx;
  1683. }
  1684. r_quadrant = qidx;
  1685. r_shadow = found_free_idx;
  1686. return true;
  1687. }
  1688. return false;
  1689. }
  1690. bool LightStorage::_shadow_atlas_find_omni_shadows(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) {
  1691. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  1692. int qidx = p_in_quadrants[i];
  1693. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  1694. return false;
  1695. }
  1696. //look for an empty space
  1697. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  1698. const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
  1699. int found_idx = -1;
  1700. uint64_t min_pass = 0; // sum of currently selected spots, try to get the least recently used pair
  1701. for (int j = 0; j < sc - 1; j++) {
  1702. uint64_t pass = 0;
  1703. if (sarr[j].owner.is_valid()) {
  1704. LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
  1705. ERR_CONTINUE(!sli);
  1706. if (sli->last_scene_pass == RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
  1707. continue;
  1708. }
  1709. //was just allocated, don't kill it so soon, wait a bit..
  1710. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  1711. continue;
  1712. }
  1713. pass += sli->last_scene_pass;
  1714. }
  1715. if (sarr[j + 1].owner.is_valid()) {
  1716. LightInstance *sli = light_instance_owner.get_or_null(sarr[j + 1].owner);
  1717. ERR_CONTINUE(!sli);
  1718. if (sli->last_scene_pass == RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
  1719. continue;
  1720. }
  1721. //was just allocated, don't kill it so soon, wait a bit..
  1722. if (p_tick - sarr[j + 1].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  1723. continue;
  1724. }
  1725. pass += sli->last_scene_pass;
  1726. }
  1727. if (found_idx == -1 || pass < min_pass) {
  1728. found_idx = j;
  1729. min_pass = pass;
  1730. // we found two empty spots, no need to check the rest
  1731. if (pass == 0) {
  1732. break;
  1733. }
  1734. }
  1735. }
  1736. if (found_idx == -1) {
  1737. continue; //nothing found
  1738. }
  1739. r_quadrant = qidx;
  1740. r_shadow = found_idx;
  1741. return true;
  1742. }
  1743. return false;
  1744. }
  1745. bool LightStorage::shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) {
  1746. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1747. ERR_FAIL_NULL_V(shadow_atlas, false);
  1748. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  1749. ERR_FAIL_NULL_V(li, false);
  1750. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  1751. return false;
  1752. }
  1753. uint32_t quad_size = shadow_atlas->size >> 1;
  1754. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  1755. int valid_quadrants[4];
  1756. int valid_quadrant_count = 0;
  1757. int best_size = -1; //best size found
  1758. int best_subdiv = -1; //subdiv for the best size
  1759. //find the quadrants this fits into, and the best possible size it can fit into
  1760. for (int i = 0; i < 4; i++) {
  1761. int q = shadow_atlas->size_order[i];
  1762. int sd = shadow_atlas->quadrants[q].subdivision;
  1763. if (sd == 0) {
  1764. continue; //unused
  1765. }
  1766. int max_fit = quad_size / sd;
  1767. if (best_size != -1 && max_fit > best_size) {
  1768. break; //too large
  1769. }
  1770. valid_quadrants[valid_quadrant_count++] = q;
  1771. best_subdiv = sd;
  1772. if (max_fit >= desired_fit) {
  1773. best_size = max_fit;
  1774. }
  1775. }
  1776. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  1777. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  1778. uint32_t old_key = SHADOW_INVALID;
  1779. uint32_t old_quadrant = SHADOW_INVALID;
  1780. uint32_t old_shadow = SHADOW_INVALID;
  1781. int old_subdivision = -1;
  1782. bool should_realloc = false;
  1783. bool should_redraw = false;
  1784. if (shadow_atlas->shadow_owners.has(p_light_instance)) {
  1785. old_key = shadow_atlas->shadow_owners[p_light_instance];
  1786. old_quadrant = (old_key >> QUADRANT_SHIFT) & 0x3;
  1787. old_shadow = old_key & SHADOW_INDEX_MASK;
  1788. should_realloc = shadow_atlas->quadrants[old_quadrant].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  1789. should_redraw = shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].version != p_light_version;
  1790. if (!should_realloc) {
  1791. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = p_light_version;
  1792. //already existing, see if it should redraw or it's just OK
  1793. return should_redraw;
  1794. }
  1795. old_subdivision = shadow_atlas->quadrants[old_quadrant].subdivision;
  1796. }
  1797. bool is_omni = li->light_type == RS::LIGHT_OMNI;
  1798. bool found_shadow = false;
  1799. int new_quadrant = -1;
  1800. int new_shadow = -1;
  1801. if (is_omni) {
  1802. found_shadow = _shadow_atlas_find_omni_shadows(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, new_quadrant, new_shadow);
  1803. } else {
  1804. found_shadow = _shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, new_quadrant, new_shadow);
  1805. }
  1806. if (found_shadow) {
  1807. if (old_quadrant != SHADOW_INVALID) {
  1808. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = 0;
  1809. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].owner = RID();
  1810. if (old_key & OMNI_LIGHT_FLAG) {
  1811. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow + 1].version = 0;
  1812. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow + 1].owner = RID();
  1813. }
  1814. }
  1815. uint32_t new_key = new_quadrant << QUADRANT_SHIFT;
  1816. new_key |= new_shadow;
  1817. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  1818. _shadow_atlas_invalidate_shadow(sh, p_atlas, shadow_atlas, new_quadrant, new_shadow);
  1819. sh->owner = p_light_instance;
  1820. sh->alloc_tick = tick;
  1821. sh->version = p_light_version;
  1822. if (is_omni) {
  1823. new_key |= OMNI_LIGHT_FLAG;
  1824. int new_omni_shadow = new_shadow + 1;
  1825. ShadowAtlas::Quadrant::Shadow *extra_sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_omni_shadow];
  1826. _shadow_atlas_invalidate_shadow(extra_sh, p_atlas, shadow_atlas, new_quadrant, new_omni_shadow);
  1827. extra_sh->owner = p_light_instance;
  1828. extra_sh->alloc_tick = tick;
  1829. extra_sh->version = p_light_version;
  1830. }
  1831. li->shadow_atlases.insert(p_atlas);
  1832. //update it in map
  1833. shadow_atlas->shadow_owners[p_light_instance] = new_key;
  1834. //make it dirty, as it should redraw anyway
  1835. return true;
  1836. }
  1837. return should_redraw;
  1838. }
  1839. void LightStorage::_shadow_atlas_invalidate_shadow(ShadowAtlas::Quadrant::Shadow *p_shadow, RID p_atlas, ShadowAtlas *p_shadow_atlas, uint32_t p_quadrant, uint32_t p_shadow_idx) {
  1840. if (p_shadow->owner.is_valid()) {
  1841. LightInstance *sli = light_instance_owner.get_or_null(p_shadow->owner);
  1842. uint32_t old_key = p_shadow_atlas->shadow_owners[p_shadow->owner];
  1843. if (old_key & OMNI_LIGHT_FLAG) {
  1844. uint32_t s = old_key & SHADOW_INDEX_MASK;
  1845. uint32_t omni_shadow_idx = p_shadow_idx + (s == (uint32_t)p_shadow_idx ? 1 : -1);
  1846. ShadowAtlas::Quadrant::Shadow *omni_shadow = &p_shadow_atlas->quadrants[p_quadrant].shadows.write[omni_shadow_idx];
  1847. omni_shadow->version = 0;
  1848. omni_shadow->owner = RID();
  1849. }
  1850. p_shadow_atlas->shadow_owners.erase(p_shadow->owner);
  1851. p_shadow->version = 0;
  1852. p_shadow->owner = RID();
  1853. sli->shadow_atlases.erase(p_atlas);
  1854. }
  1855. }
  1856. void LightStorage::shadow_atlas_update(RID p_atlas) {
  1857. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1858. ERR_FAIL_NULL(shadow_atlas);
  1859. _update_shadow_atlas(shadow_atlas);
  1860. }
  1861. /* DIRECTIONAL SHADOW */
  1862. void LightStorage::update_directional_shadow_atlas() {
  1863. if (directional_shadow.depth.is_null() && directional_shadow.size > 0) {
  1864. RD::TextureFormat tf;
  1865. tf.format = directional_shadow.use_16_bits ? RD::DATA_FORMAT_D16_UNORM : RD::DATA_FORMAT_D32_SFLOAT;
  1866. tf.width = directional_shadow.size;
  1867. tf.height = directional_shadow.size;
  1868. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1869. directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1870. Vector<RID> fb_tex;
  1871. fb_tex.push_back(directional_shadow.depth);
  1872. directional_shadow.fb = RD::get_singleton()->framebuffer_create(fb_tex);
  1873. }
  1874. }
  1875. void LightStorage::directional_shadow_atlas_set_size(int p_size, bool p_16_bits) {
  1876. p_size = nearest_power_of_2_templated(p_size);
  1877. if (directional_shadow.size == p_size && directional_shadow.use_16_bits == p_16_bits) {
  1878. return;
  1879. }
  1880. directional_shadow.size = p_size;
  1881. directional_shadow.use_16_bits = p_16_bits;
  1882. if (directional_shadow.depth.is_valid()) {
  1883. RD::get_singleton()->free(directional_shadow.depth);
  1884. directional_shadow.depth = RID();
  1885. RendererSceneRenderRD::get_singleton()->base_uniforms_changed();
  1886. }
  1887. }
  1888. void LightStorage::set_directional_shadow_count(int p_count) {
  1889. directional_shadow.light_count = p_count;
  1890. directional_shadow.current_light = 0;
  1891. }
  1892. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  1893. int split_h = 1;
  1894. int split_v = 1;
  1895. while (split_h * split_v < p_shadow_count) {
  1896. if (split_h == split_v) {
  1897. split_h <<= 1;
  1898. } else {
  1899. split_v <<= 1;
  1900. }
  1901. }
  1902. Rect2i rect(0, 0, p_size, p_size);
  1903. rect.size.width /= split_h;
  1904. rect.size.height /= split_v;
  1905. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  1906. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  1907. return rect;
  1908. }
  1909. Rect2i LightStorage::get_directional_shadow_rect() {
  1910. return _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  1911. }
  1912. int LightStorage::get_directional_light_shadow_size(RID p_light_intance) {
  1913. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  1914. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  1915. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_intance);
  1916. ERR_FAIL_NULL_V(light_instance, 0);
  1917. switch (light_directional_get_shadow_mode(light_instance->light)) {
  1918. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1919. break; //none
  1920. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1921. r.size.height /= 2;
  1922. break;
  1923. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1924. r.size /= 2;
  1925. break;
  1926. }
  1927. return MAX(r.size.width, r.size.height);
  1928. }
  1929. /* SHADOW CUBEMAPS */
  1930. LightStorage::ShadowCubemap *LightStorage::_get_shadow_cubemap(int p_size) {
  1931. if (!shadow_cubemaps.has(p_size)) {
  1932. ShadowCubemap sc;
  1933. {
  1934. RD::TextureFormat tf;
  1935. 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;
  1936. tf.width = p_size;
  1937. tf.height = p_size;
  1938. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  1939. tf.array_layers = 6;
  1940. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1941. sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1942. }
  1943. for (int i = 0; i < 6; i++) {
  1944. RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
  1945. Vector<RID> fbtex;
  1946. fbtex.push_back(side_texture);
  1947. sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
  1948. }
  1949. shadow_cubemaps[p_size] = sc;
  1950. }
  1951. return &shadow_cubemaps[p_size];
  1952. }
  1953. RID LightStorage::get_cubemap(int p_size) {
  1954. ShadowCubemap *cubemap = _get_shadow_cubemap(p_size);
  1955. return cubemap->cubemap;
  1956. }
  1957. RID LightStorage::get_cubemap_fb(int p_size, int p_pass) {
  1958. ShadowCubemap *cubemap = _get_shadow_cubemap(p_size);
  1959. return cubemap->side_fb[p_pass];
  1960. }