light_storage.cpp 88 KB

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