light_storage.cpp 86 KB

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