light_storage.cpp 91 KB

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