light_storage.cpp 27 KB

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