light_storage.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*************************************************************************/
  2. /* light_storage.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "texture_storage.h"
  33. using namespace RendererRD;
  34. LightStorage *LightStorage::singleton = nullptr;
  35. LightStorage *LightStorage::get_singleton() {
  36. return singleton;
  37. }
  38. LightStorage::LightStorage() {
  39. singleton = this;
  40. TextureStorage *texture_storage = TextureStorage::get_singleton();
  41. using_lightmap_array = true; // high end
  42. if (using_lightmap_array) {
  43. uint64_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  44. if (textures_per_stage <= 256) {
  45. lightmap_textures.resize(32);
  46. } else {
  47. lightmap_textures.resize(1024);
  48. }
  49. for (int i = 0; i < lightmap_textures.size(); i++) {
  50. lightmap_textures.write[i] = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  51. }
  52. }
  53. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
  54. }
  55. LightStorage::~LightStorage() {
  56. singleton = nullptr;
  57. }
  58. /* LIGHT */
  59. void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
  60. Light light;
  61. light.type = p_type;
  62. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  63. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  64. light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
  65. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  66. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  67. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  68. light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
  69. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  70. light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
  71. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  72. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  73. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  74. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  75. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  76. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  77. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  78. light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
  79. light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
  80. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  81. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  82. light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
  83. light_owner.initialize_rid(p_light, light);
  84. }
  85. RID LightStorage::directional_light_allocate() {
  86. return light_owner.allocate_rid();
  87. }
  88. void LightStorage::directional_light_initialize(RID p_light) {
  89. _light_initialize(p_light, RS::LIGHT_DIRECTIONAL);
  90. }
  91. RID LightStorage::omni_light_allocate() {
  92. return light_owner.allocate_rid();
  93. }
  94. void LightStorage::omni_light_initialize(RID p_light) {
  95. _light_initialize(p_light, RS::LIGHT_OMNI);
  96. }
  97. RID LightStorage::spot_light_allocate() {
  98. return light_owner.allocate_rid();
  99. }
  100. void LightStorage::spot_light_initialize(RID p_light) {
  101. _light_initialize(p_light, RS::LIGHT_SPOT);
  102. }
  103. void LightStorage::light_free(RID p_rid) {
  104. light_set_projector(p_rid, RID()); //clear projector
  105. // delete the texture
  106. Light *light = light_owner.get_or_null(p_rid);
  107. light->dependency.deleted_notify(p_rid);
  108. light_owner.free(p_rid);
  109. }
  110. void LightStorage::light_set_color(RID p_light, const Color &p_color) {
  111. Light *light = light_owner.get_or_null(p_light);
  112. ERR_FAIL_COND(!light);
  113. light->color = p_color;
  114. }
  115. void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  116. Light *light = light_owner.get_or_null(p_light);
  117. ERR_FAIL_COND(!light);
  118. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  119. if (light->param[p_param] == p_value) {
  120. return;
  121. }
  122. switch (p_param) {
  123. case RS::LIGHT_PARAM_RANGE:
  124. case RS::LIGHT_PARAM_SPOT_ANGLE:
  125. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  126. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  127. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  128. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  129. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  130. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  131. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  132. light->version++;
  133. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  134. } break;
  135. case RS::LIGHT_PARAM_SIZE: {
  136. if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
  137. //changing from no size to size and the opposite
  138. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  139. }
  140. } break;
  141. default: {
  142. }
  143. }
  144. light->param[p_param] = p_value;
  145. }
  146. void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
  147. Light *light = light_owner.get_or_null(p_light);
  148. ERR_FAIL_COND(!light);
  149. light->shadow = p_enabled;
  150. light->version++;
  151. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  152. }
  153. void LightStorage::light_set_projector(RID p_light, RID p_texture) {
  154. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  155. Light *light = light_owner.get_or_null(p_light);
  156. ERR_FAIL_COND(!light);
  157. if (light->projector == p_texture) {
  158. return;
  159. }
  160. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  161. texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  162. }
  163. light->projector = p_texture;
  164. if (light->type != RS::LIGHT_DIRECTIONAL) {
  165. if (light->projector.is_valid()) {
  166. texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  167. }
  168. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  169. }
  170. }
  171. void LightStorage::light_set_negative(RID p_light, bool p_enable) {
  172. Light *light = light_owner.get_or_null(p_light);
  173. ERR_FAIL_COND(!light);
  174. light->negative = p_enable;
  175. }
  176. void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  177. Light *light = light_owner.get_or_null(p_light);
  178. ERR_FAIL_COND(!light);
  179. light->cull_mask = p_mask;
  180. light->version++;
  181. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  182. }
  183. void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
  184. Light *light = light_owner.get_or_null(p_light);
  185. ERR_FAIL_COND(!light);
  186. light->distance_fade = p_enabled;
  187. light->distance_fade_begin = p_begin;
  188. light->distance_fade_shadow = p_shadow;
  189. light->distance_fade_length = p_length;
  190. }
  191. void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  192. Light *light = light_owner.get_or_null(p_light);
  193. ERR_FAIL_COND(!light);
  194. light->reverse_cull = p_enabled;
  195. light->version++;
  196. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  197. }
  198. void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  199. Light *light = light_owner.get_or_null(p_light);
  200. ERR_FAIL_COND(!light);
  201. light->bake_mode = p_bake_mode;
  202. light->version++;
  203. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  204. }
  205. void LightStorage::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
  206. Light *light = light_owner.get_or_null(p_light);
  207. ERR_FAIL_COND(!light);
  208. light->max_sdfgi_cascade = p_cascade;
  209. light->version++;
  210. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  211. }
  212. void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  213. Light *light = light_owner.get_or_null(p_light);
  214. ERR_FAIL_COND(!light);
  215. light->omni_shadow_mode = p_mode;
  216. light->version++;
  217. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  218. }
  219. RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
  220. const Light *light = light_owner.get_or_null(p_light);
  221. ERR_FAIL_COND_V(!light, RS::LIGHT_OMNI_SHADOW_CUBE);
  222. return light->omni_shadow_mode;
  223. }
  224. void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  225. Light *light = light_owner.get_or_null(p_light);
  226. ERR_FAIL_COND(!light);
  227. light->directional_shadow_mode = p_mode;
  228. light->version++;
  229. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  230. }
  231. void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  232. Light *light = light_owner.get_or_null(p_light);
  233. ERR_FAIL_COND(!light);
  234. light->directional_blend_splits = p_enable;
  235. light->version++;
  236. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  237. }
  238. bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
  239. const Light *light = light_owner.get_or_null(p_light);
  240. ERR_FAIL_COND_V(!light, false);
  241. return light->directional_blend_splits;
  242. }
  243. void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
  244. Light *light = light_owner.get_or_null(p_light);
  245. ERR_FAIL_COND(!light);
  246. light->directional_sky_mode = p_mode;
  247. }
  248. RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
  249. const Light *light = light_owner.get_or_null(p_light);
  250. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
  251. return light->directional_sky_mode;
  252. }
  253. RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
  254. const Light *light = light_owner.get_or_null(p_light);
  255. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  256. return light->directional_shadow_mode;
  257. }
  258. uint32_t LightStorage::light_get_max_sdfgi_cascade(RID p_light) {
  259. const Light *light = light_owner.get_or_null(p_light);
  260. ERR_FAIL_COND_V(!light, 0);
  261. return light->max_sdfgi_cascade;
  262. }
  263. RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
  264. const Light *light = light_owner.get_or_null(p_light);
  265. ERR_FAIL_COND_V(!light, RS::LIGHT_BAKE_DISABLED);
  266. return light->bake_mode;
  267. }
  268. uint64_t LightStorage::light_get_version(RID p_light) const {
  269. const Light *light = light_owner.get_or_null(p_light);
  270. ERR_FAIL_COND_V(!light, 0);
  271. return light->version;
  272. }
  273. AABB LightStorage::light_get_aabb(RID p_light) const {
  274. const Light *light = light_owner.get_or_null(p_light);
  275. ERR_FAIL_COND_V(!light, AABB());
  276. switch (light->type) {
  277. case RS::LIGHT_SPOT: {
  278. float len = light->param[RS::LIGHT_PARAM_RANGE];
  279. float size = Math::tan(Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  280. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  281. };
  282. case RS::LIGHT_OMNI: {
  283. float r = light->param[RS::LIGHT_PARAM_RANGE];
  284. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  285. };
  286. case RS::LIGHT_DIRECTIONAL: {
  287. return AABB();
  288. };
  289. }
  290. ERR_FAIL_V(AABB());
  291. }
  292. Dependency *LightStorage::light_get_dependency(RID p_light) const {
  293. Light *light = light_owner.get_or_null(p_light);
  294. ERR_FAIL_NULL_V(light, nullptr);
  295. return &light->dependency;
  296. }
  297. /* REFLECTION PROBE */
  298. RID LightStorage::reflection_probe_allocate() {
  299. return reflection_probe_owner.allocate_rid();
  300. }
  301. void LightStorage::reflection_probe_initialize(RID p_reflection_probe) {
  302. reflection_probe_owner.initialize_rid(p_reflection_probe, ReflectionProbe());
  303. }
  304. void LightStorage::reflection_probe_free(RID p_rid) {
  305. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_rid);
  306. reflection_probe->dependency.deleted_notify(p_rid);
  307. reflection_probe_owner.free(p_rid);
  308. };
  309. void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  310. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  311. ERR_FAIL_COND(!reflection_probe);
  312. reflection_probe->update_mode = p_mode;
  313. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  314. }
  315. void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  316. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  317. ERR_FAIL_COND(!reflection_probe);
  318. reflection_probe->intensity = p_intensity;
  319. }
  320. void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  321. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  322. ERR_FAIL_COND(!reflection_probe);
  323. reflection_probe->ambient_mode = p_mode;
  324. }
  325. void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  326. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  327. ERR_FAIL_COND(!reflection_probe);
  328. reflection_probe->ambient_color = p_color;
  329. }
  330. void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  331. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  332. ERR_FAIL_COND(!reflection_probe);
  333. reflection_probe->ambient_color_energy = p_energy;
  334. }
  335. void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  336. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  337. ERR_FAIL_COND(!reflection_probe);
  338. reflection_probe->max_distance = p_distance;
  339. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  340. }
  341. void LightStorage::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  342. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  343. ERR_FAIL_COND(!reflection_probe);
  344. if (reflection_probe->extents == p_extents) {
  345. return;
  346. }
  347. reflection_probe->extents = p_extents;
  348. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  349. }
  350. void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  351. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  352. ERR_FAIL_COND(!reflection_probe);
  353. reflection_probe->origin_offset = p_offset;
  354. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  355. }
  356. void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  357. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  358. ERR_FAIL_COND(!reflection_probe);
  359. reflection_probe->interior = p_enable;
  360. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  361. }
  362. void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  363. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  364. ERR_FAIL_COND(!reflection_probe);
  365. reflection_probe->box_projection = p_enable;
  366. }
  367. void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  368. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  369. ERR_FAIL_COND(!reflection_probe);
  370. reflection_probe->enable_shadows = p_enable;
  371. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  372. }
  373. void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  374. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  375. ERR_FAIL_COND(!reflection_probe);
  376. reflection_probe->cull_mask = p_layers;
  377. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  378. }
  379. void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  380. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  381. ERR_FAIL_COND(!reflection_probe);
  382. ERR_FAIL_COND(p_resolution < 32);
  383. reflection_probe->resolution = p_resolution;
  384. }
  385. void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
  386. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  387. ERR_FAIL_COND(!reflection_probe);
  388. reflection_probe->mesh_lod_threshold = p_ratio;
  389. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  390. }
  391. void LightStorage::reflection_probe_set_baked_exposure(RID p_probe, float p_exposure) {
  392. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  393. ERR_FAIL_COND(!reflection_probe);
  394. reflection_probe->baked_exposure = p_exposure;
  395. }
  396. AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
  397. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  398. ERR_FAIL_COND_V(!reflection_probe, AABB());
  399. AABB aabb;
  400. aabb.position = -reflection_probe->extents;
  401. aabb.size = reflection_probe->extents * 2.0;
  402. return aabb;
  403. }
  404. RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
  405. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  406. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  407. return reflection_probe->update_mode;
  408. }
  409. uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
  410. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  411. ERR_FAIL_COND_V(!reflection_probe, 0);
  412. return reflection_probe->cull_mask;
  413. }
  414. Vector3 LightStorage::reflection_probe_get_extents(RID p_probe) const {
  415. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  416. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  417. return reflection_probe->extents;
  418. }
  419. Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
  420. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  421. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  422. return reflection_probe->origin_offset;
  423. }
  424. bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
  425. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  426. ERR_FAIL_COND_V(!reflection_probe, false);
  427. return reflection_probe->enable_shadows;
  428. }
  429. float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
  430. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  431. ERR_FAIL_COND_V(!reflection_probe, 0);
  432. return reflection_probe->max_distance;
  433. }
  434. float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
  435. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  436. ERR_FAIL_COND_V(!reflection_probe, 0);
  437. return reflection_probe->mesh_lod_threshold;
  438. }
  439. int LightStorage::reflection_probe_get_resolution(RID p_probe) const {
  440. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  441. ERR_FAIL_COND_V(!reflection_probe, 0);
  442. return reflection_probe->resolution;
  443. }
  444. float LightStorage::reflection_probe_get_baked_exposure(RID p_probe) const {
  445. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  446. ERR_FAIL_COND_V(!reflection_probe, 1.0);
  447. return reflection_probe->baked_exposure;
  448. }
  449. float LightStorage::reflection_probe_get_intensity(RID p_probe) const {
  450. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  451. ERR_FAIL_COND_V(!reflection_probe, 0);
  452. return reflection_probe->intensity;
  453. }
  454. bool LightStorage::reflection_probe_is_interior(RID p_probe) const {
  455. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  456. ERR_FAIL_COND_V(!reflection_probe, false);
  457. return reflection_probe->interior;
  458. }
  459. bool LightStorage::reflection_probe_is_box_projection(RID p_probe) const {
  460. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  461. ERR_FAIL_COND_V(!reflection_probe, false);
  462. return reflection_probe->box_projection;
  463. }
  464. RS::ReflectionProbeAmbientMode LightStorage::reflection_probe_get_ambient_mode(RID p_probe) const {
  465. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  466. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
  467. return reflection_probe->ambient_mode;
  468. }
  469. Color LightStorage::reflection_probe_get_ambient_color(RID p_probe) const {
  470. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  471. ERR_FAIL_COND_V(!reflection_probe, Color());
  472. return reflection_probe->ambient_color;
  473. }
  474. float LightStorage::reflection_probe_get_ambient_color_energy(RID p_probe) const {
  475. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  476. ERR_FAIL_COND_V(!reflection_probe, 0);
  477. return reflection_probe->ambient_color_energy;
  478. }
  479. Dependency *LightStorage::reflection_probe_get_dependency(RID p_probe) const {
  480. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  481. ERR_FAIL_NULL_V(reflection_probe, nullptr);
  482. return &reflection_probe->dependency;
  483. }
  484. /* LIGHTMAP API */
  485. RID LightStorage::lightmap_allocate() {
  486. return lightmap_owner.allocate_rid();
  487. }
  488. void LightStorage::lightmap_initialize(RID p_lightmap) {
  489. lightmap_owner.initialize_rid(p_lightmap, Lightmap());
  490. }
  491. void LightStorage::lightmap_free(RID p_rid) {
  492. lightmap_set_textures(p_rid, RID(), false);
  493. Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
  494. lightmap->dependency.deleted_notify(p_rid);
  495. lightmap_owner.free(p_rid);
  496. }
  497. void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  498. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  499. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  500. ERR_FAIL_COND(!lm);
  501. lightmap_array_version++;
  502. //erase lightmap users
  503. if (lm->light_texture.is_valid()) {
  504. RendererRD::TextureStorage::Texture *t = RendererRD::TextureStorage::get_singleton()->get_texture(lm->light_texture);
  505. if (t) {
  506. t->lightmap_users.erase(p_lightmap);
  507. }
  508. }
  509. RendererRD::TextureStorage::Texture *t = RendererRD::TextureStorage::get_singleton()->get_texture(p_light);
  510. lm->light_texture = p_light;
  511. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  512. RID default_2d_array = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  513. if (!t) {
  514. if (using_lightmap_array) {
  515. if (lm->array_index >= 0) {
  516. lightmap_textures.write[lm->array_index] = default_2d_array;
  517. lm->array_index = -1;
  518. }
  519. }
  520. return;
  521. }
  522. t->lightmap_users.insert(p_lightmap);
  523. if (using_lightmap_array) {
  524. if (lm->array_index < 0) {
  525. //not in array, try to put in array
  526. for (int i = 0; i < lightmap_textures.size(); i++) {
  527. if (lightmap_textures[i] == default_2d_array) {
  528. lm->array_index = i;
  529. break;
  530. }
  531. }
  532. }
  533. 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.");
  534. lightmap_textures.write[lm->array_index] = t->rd_texture;
  535. }
  536. }
  537. void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  538. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  539. ERR_FAIL_COND(!lm);
  540. lm->bounds = p_bounds;
  541. }
  542. void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  543. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  544. ERR_FAIL_COND(!lm);
  545. lm->interior = p_interior;
  546. }
  547. 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) {
  548. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  549. ERR_FAIL_COND(!lm);
  550. if (p_points.size()) {
  551. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  552. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  553. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  554. }
  555. lm->points = p_points;
  556. lm->bsp_tree = p_bsp_tree;
  557. lm->point_sh = p_point_sh;
  558. lm->tetrahedra = p_tetrahedra;
  559. }
  560. void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
  561. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  562. ERR_FAIL_COND(!lm);
  563. lm->baked_exposure = p_exposure;
  564. }
  565. PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
  566. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  567. ERR_FAIL_COND_V(!lm, PackedVector3Array());
  568. return lm->points;
  569. }
  570. PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  571. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  572. ERR_FAIL_COND_V(!lm, PackedColorArray());
  573. return lm->point_sh;
  574. }
  575. PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  576. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  577. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  578. return lm->tetrahedra;
  579. }
  580. PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  581. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  582. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  583. return lm->bsp_tree;
  584. }
  585. void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
  586. lightmap_probe_capture_update_speed = p_speed;
  587. }
  588. Dependency *LightStorage::lightmap_get_dependency(RID p_lightmap) const {
  589. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  590. ERR_FAIL_NULL_V(lm, nullptr);
  591. return &lm->dependency;
  592. }
  593. void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  594. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  595. ERR_FAIL_COND(!lm);
  596. for (int i = 0; i < 9; i++) {
  597. r_sh[i] = Color(0, 0, 0, 0);
  598. }
  599. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  600. return;
  601. }
  602. static_assert(sizeof(Lightmap::BSP) == 24);
  603. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  604. int32_t node = 0;
  605. while (node >= 0) {
  606. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  607. #ifdef DEBUG_ENABLED
  608. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  609. #endif
  610. node = bsp[node].over;
  611. } else {
  612. #ifdef DEBUG_ENABLED
  613. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  614. #endif
  615. node = bsp[node].under;
  616. }
  617. }
  618. if (node == Lightmap::BSP::EMPTY_LEAF) {
  619. return; //nothing could be done
  620. }
  621. node = ABS(node) - 1;
  622. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  623. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  624. 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] };
  625. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  626. for (int i = 0; i < 4; i++) {
  627. float c = CLAMP(barycentric[i], 0.0, 1.0);
  628. for (int j = 0; j < 9; j++) {
  629. r_sh[j] += sh_colors[i][j] * c;
  630. }
  631. }
  632. }
  633. bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
  634. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  635. ERR_FAIL_COND_V(!lm, false);
  636. return lm->interior;
  637. }
  638. AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
  639. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  640. ERR_FAIL_COND_V(!lm, AABB());
  641. return lm->bounds;
  642. }