light_storage.cpp 91 KB

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