light_storage.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. #ifdef GLES3_ENABLED
  31. #include "light_storage.h"
  32. #include "../rasterizer_gles3.h"
  33. #include "../rasterizer_scene_gles3.h"
  34. #include "config.h"
  35. #include "core/config/project_settings.h"
  36. #include "texture_storage.h"
  37. using namespace GLES3;
  38. LightStorage *LightStorage::singleton = nullptr;
  39. LightStorage *LightStorage::get_singleton() {
  40. return singleton;
  41. }
  42. LightStorage::LightStorage() {
  43. singleton = this;
  44. directional_shadow.size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
  45. directional_shadow.use_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
  46. // lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
  47. }
  48. LightStorage::~LightStorage() {
  49. singleton = nullptr;
  50. }
  51. /* Light API */
  52. void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
  53. Light light;
  54. light.type = p_type;
  55. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  56. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  57. light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
  58. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  59. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  60. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  61. light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
  62. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  63. light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
  64. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  65. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  66. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  67. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  68. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  69. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  70. light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
  71. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  72. light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
  73. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  74. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  75. light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
  76. light_owner.initialize_rid(p_light, light);
  77. }
  78. RID LightStorage::directional_light_allocate() {
  79. return light_owner.allocate_rid();
  80. }
  81. void LightStorage::directional_light_initialize(RID p_rid) {
  82. _light_initialize(p_rid, RS::LIGHT_DIRECTIONAL);
  83. }
  84. RID LightStorage::omni_light_allocate() {
  85. return light_owner.allocate_rid();
  86. }
  87. void LightStorage::omni_light_initialize(RID p_rid) {
  88. _light_initialize(p_rid, RS::LIGHT_OMNI);
  89. }
  90. RID LightStorage::spot_light_allocate() {
  91. return light_owner.allocate_rid();
  92. }
  93. void LightStorage::spot_light_initialize(RID p_rid) {
  94. _light_initialize(p_rid, RS::LIGHT_SPOT);
  95. }
  96. void LightStorage::light_free(RID p_rid) {
  97. light_set_projector(p_rid, RID()); //clear projector
  98. // delete the texture
  99. Light *light = light_owner.get_or_null(p_rid);
  100. light->dependency.deleted_notify(p_rid);
  101. light_owner.free(p_rid);
  102. }
  103. void LightStorage::light_set_color(RID p_light, const Color &p_color) {
  104. Light *light = light_owner.get_or_null(p_light);
  105. ERR_FAIL_NULL(light);
  106. light->color = p_color;
  107. }
  108. void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  109. Light *light = light_owner.get_or_null(p_light);
  110. ERR_FAIL_NULL(light);
  111. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  112. if (light->param[p_param] == p_value) {
  113. return;
  114. }
  115. switch (p_param) {
  116. case RS::LIGHT_PARAM_RANGE:
  117. case RS::LIGHT_PARAM_SPOT_ANGLE:
  118. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  119. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  120. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  121. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  122. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  123. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  124. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  125. light->version++;
  126. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  127. } break;
  128. case RS::LIGHT_PARAM_SIZE: {
  129. if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
  130. //changing from no size to size and the opposite
  131. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  132. }
  133. } break;
  134. default: {
  135. }
  136. }
  137. light->param[p_param] = p_value;
  138. }
  139. void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
  140. Light *light = light_owner.get_or_null(p_light);
  141. ERR_FAIL_NULL(light);
  142. light->shadow = p_enabled;
  143. light->version++;
  144. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  145. }
  146. void LightStorage::light_set_projector(RID p_light, RID p_texture) {
  147. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  148. Light *light = light_owner.get_or_null(p_light);
  149. ERR_FAIL_NULL(light);
  150. if (light->projector == p_texture) {
  151. return;
  152. }
  153. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  154. texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  155. }
  156. light->projector = p_texture;
  157. if (light->type != RS::LIGHT_DIRECTIONAL) {
  158. if (light->projector.is_valid()) {
  159. texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  160. }
  161. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  162. }
  163. }
  164. void LightStorage::light_set_negative(RID p_light, bool p_enable) {
  165. Light *light = light_owner.get_or_null(p_light);
  166. ERR_FAIL_NULL(light);
  167. light->negative = p_enable;
  168. }
  169. void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  170. Light *light = light_owner.get_or_null(p_light);
  171. ERR_FAIL_NULL(light);
  172. light->cull_mask = p_mask;
  173. light->version++;
  174. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  175. }
  176. void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
  177. Light *light = light_owner.get_or_null(p_light);
  178. ERR_FAIL_NULL(light);
  179. light->distance_fade = p_enabled;
  180. light->distance_fade_begin = p_begin;
  181. light->distance_fade_shadow = p_shadow;
  182. light->distance_fade_length = p_length;
  183. }
  184. void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  185. Light *light = light_owner.get_or_null(p_light);
  186. ERR_FAIL_NULL(light);
  187. light->reverse_cull = p_enabled;
  188. light->version++;
  189. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  190. }
  191. void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  192. Light *light = light_owner.get_or_null(p_light);
  193. ERR_FAIL_NULL(light);
  194. light->bake_mode = p_bake_mode;
  195. light->version++;
  196. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  197. }
  198. void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  199. Light *light = light_owner.get_or_null(p_light);
  200. ERR_FAIL_NULL(light);
  201. light->omni_shadow_mode = p_mode;
  202. light->version++;
  203. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  204. }
  205. RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
  206. const Light *light = light_owner.get_or_null(p_light);
  207. ERR_FAIL_NULL_V(light, RS::LIGHT_OMNI_SHADOW_CUBE);
  208. return light->omni_shadow_mode;
  209. }
  210. void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  211. Light *light = light_owner.get_or_null(p_light);
  212. ERR_FAIL_NULL(light);
  213. light->directional_shadow_mode = p_mode;
  214. light->version++;
  215. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  216. }
  217. void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  218. Light *light = light_owner.get_or_null(p_light);
  219. ERR_FAIL_NULL(light);
  220. light->directional_blend_splits = p_enable;
  221. light->version++;
  222. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  223. }
  224. bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
  225. const Light *light = light_owner.get_or_null(p_light);
  226. ERR_FAIL_NULL_V(light, false);
  227. return light->directional_blend_splits;
  228. }
  229. void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
  230. Light *light = light_owner.get_or_null(p_light);
  231. ERR_FAIL_NULL(light);
  232. light->directional_sky_mode = p_mode;
  233. }
  234. RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
  235. const Light *light = light_owner.get_or_null(p_light);
  236. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
  237. return light->directional_sky_mode;
  238. }
  239. RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
  240. const Light *light = light_owner.get_or_null(p_light);
  241. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  242. return light->directional_shadow_mode;
  243. }
  244. RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
  245. const Light *light = light_owner.get_or_null(p_light);
  246. ERR_FAIL_NULL_V(light, RS::LIGHT_BAKE_DISABLED);
  247. return light->bake_mode;
  248. }
  249. uint64_t LightStorage::light_get_version(RID p_light) const {
  250. const Light *light = light_owner.get_or_null(p_light);
  251. ERR_FAIL_NULL_V(light, 0);
  252. return light->version;
  253. }
  254. uint32_t LightStorage::light_get_cull_mask(RID p_light) const {
  255. const Light *light = light_owner.get_or_null(p_light);
  256. ERR_FAIL_NULL_V(light, 0);
  257. return light->cull_mask;
  258. }
  259. AABB LightStorage::light_get_aabb(RID p_light) const {
  260. const Light *light = light_owner.get_or_null(p_light);
  261. ERR_FAIL_NULL_V(light, AABB());
  262. switch (light->type) {
  263. case RS::LIGHT_SPOT: {
  264. float len = light->param[RS::LIGHT_PARAM_RANGE];
  265. float size = Math::tan(Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  266. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  267. };
  268. case RS::LIGHT_OMNI: {
  269. float r = light->param[RS::LIGHT_PARAM_RANGE];
  270. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  271. };
  272. case RS::LIGHT_DIRECTIONAL: {
  273. return AABB();
  274. };
  275. }
  276. ERR_FAIL_V(AABB());
  277. }
  278. /* LIGHT INSTANCE API */
  279. RID LightStorage::light_instance_create(RID p_light) {
  280. RID li = light_instance_owner.make_rid(LightInstance());
  281. LightInstance *light_instance = light_instance_owner.get_or_null(li);
  282. light_instance->self = li;
  283. light_instance->light = p_light;
  284. light_instance->light_type = light_get_type(p_light);
  285. return li;
  286. }
  287. void LightStorage::light_instance_free(RID p_light_instance) {
  288. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  289. ERR_FAIL_NULL(light_instance);
  290. // Remove from shadow atlases.
  291. for (const RID &E : light_instance->shadow_atlases) {
  292. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(E);
  293. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_light_instance));
  294. uint32_t key = shadow_atlas->shadow_owners[p_light_instance];
  295. uint32_t q = (key >> QUADRANT_SHIFT) & 0x3;
  296. uint32_t s = key & SHADOW_INDEX_MASK;
  297. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  298. shadow_atlas->shadow_owners.erase(p_light_instance);
  299. }
  300. light_instance_owner.free(p_light_instance);
  301. }
  302. void LightStorage::light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) {
  303. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  304. ERR_FAIL_NULL(light_instance);
  305. light_instance->transform = p_transform;
  306. }
  307. void LightStorage::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
  308. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  309. ERR_FAIL_NULL(light_instance);
  310. light_instance->aabb = p_aabb;
  311. }
  312. 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) {
  313. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  314. ERR_FAIL_NULL(light_instance);
  315. ERR_FAIL_INDEX(p_pass, 6);
  316. light_instance->shadow_transform[p_pass].camera = p_projection;
  317. light_instance->shadow_transform[p_pass].transform = p_transform;
  318. light_instance->shadow_transform[p_pass].farplane = p_far;
  319. light_instance->shadow_transform[p_pass].split = p_split;
  320. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  321. light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
  322. light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
  323. light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
  324. }
  325. void LightStorage::light_instance_mark_visible(RID p_light_instance) {
  326. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  327. ERR_FAIL_NULL(light_instance);
  328. light_instance->last_scene_pass = RasterizerSceneGLES3::get_singleton()->get_scene_pass();
  329. }
  330. /* PROBE API */
  331. RID LightStorage::reflection_probe_allocate() {
  332. return RID();
  333. }
  334. void LightStorage::reflection_probe_initialize(RID p_rid) {
  335. }
  336. void LightStorage::reflection_probe_free(RID p_rid) {
  337. }
  338. void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  339. }
  340. void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  341. }
  342. void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  343. }
  344. void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  345. }
  346. void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  347. }
  348. void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  349. }
  350. void LightStorage::reflection_probe_set_size(RID p_probe, const Vector3 &p_size) {
  351. }
  352. void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  353. }
  354. void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  355. }
  356. void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  357. }
  358. void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  359. }
  360. void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  361. }
  362. void LightStorage::reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) {
  363. }
  364. void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  365. }
  366. AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
  367. return AABB();
  368. }
  369. RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
  370. return RenderingServer::REFLECTION_PROBE_UPDATE_ONCE;
  371. }
  372. uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
  373. return 0;
  374. }
  375. uint32_t LightStorage::reflection_probe_get_reflection_mask(RID p_probe) const {
  376. return 0;
  377. }
  378. Vector3 LightStorage::reflection_probe_get_size(RID p_probe) const {
  379. return Vector3();
  380. }
  381. Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
  382. return Vector3();
  383. }
  384. float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
  385. return 0.0;
  386. }
  387. bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
  388. return false;
  389. }
  390. void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
  391. }
  392. float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
  393. return 0.0;
  394. }
  395. /* REFLECTION ATLAS */
  396. RID LightStorage::reflection_atlas_create() {
  397. return RID();
  398. }
  399. void LightStorage::reflection_atlas_free(RID p_ref_atlas) {
  400. }
  401. int LightStorage::reflection_atlas_get_size(RID p_ref_atlas) const {
  402. return 0;
  403. }
  404. void LightStorage::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  405. }
  406. /* REFLECTION PROBE INSTANCE */
  407. RID LightStorage::reflection_probe_instance_create(RID p_probe) {
  408. return RID();
  409. }
  410. void LightStorage::reflection_probe_instance_free(RID p_instance) {
  411. }
  412. void LightStorage::reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) {
  413. }
  414. void LightStorage::reflection_probe_release_atlas_index(RID p_instance) {
  415. }
  416. bool LightStorage::reflection_probe_instance_needs_redraw(RID p_instance) {
  417. return false;
  418. }
  419. bool LightStorage::reflection_probe_instance_has_reflection(RID p_instance) {
  420. return false;
  421. }
  422. bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  423. return false;
  424. }
  425. Ref<RenderSceneBuffers> LightStorage::reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) {
  426. return Ref<RenderSceneBuffers>();
  427. }
  428. bool LightStorage::reflection_probe_instance_postprocess_step(RID p_instance) {
  429. return true;
  430. }
  431. /* LIGHTMAP CAPTURE */
  432. RID LightStorage::lightmap_allocate() {
  433. return lightmap_owner.allocate_rid();
  434. }
  435. void LightStorage::lightmap_initialize(RID p_rid) {
  436. lightmap_owner.initialize_rid(p_rid, Lightmap());
  437. }
  438. void LightStorage::lightmap_free(RID p_rid) {
  439. Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
  440. ERR_FAIL_NULL(lightmap);
  441. lightmap->dependency.deleted_notify(p_rid);
  442. lightmap_owner.free(p_rid);
  443. }
  444. void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  445. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  446. ERR_FAIL_NULL(lightmap);
  447. lightmap->light_texture = p_light;
  448. lightmap->uses_spherical_harmonics = p_uses_spherical_haromics;
  449. GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lightmap->light_texture);
  450. glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  451. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  452. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  453. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  454. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  455. glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
  456. }
  457. void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  458. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  459. ERR_FAIL_NULL(lightmap);
  460. lightmap->bounds = p_bounds;
  461. }
  462. void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  463. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  464. ERR_FAIL_NULL(lightmap);
  465. lightmap->interior = p_interior;
  466. }
  467. 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) {
  468. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  469. ERR_FAIL_NULL(lightmap);
  470. if (p_points.size()) {
  471. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  472. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  473. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  474. }
  475. lightmap->points = p_points;
  476. lightmap->point_sh = p_point_sh;
  477. lightmap->tetrahedra = p_tetrahedra;
  478. lightmap->bsp_tree = p_bsp_tree;
  479. }
  480. void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
  481. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  482. ERR_FAIL_NULL(lightmap);
  483. lightmap->baked_exposure = p_exposure;
  484. }
  485. PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
  486. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  487. ERR_FAIL_NULL_V(lightmap, PackedVector3Array());
  488. return lightmap->points;
  489. }
  490. PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  491. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  492. ERR_FAIL_NULL_V(lightmap, PackedColorArray());
  493. return lightmap->point_sh;
  494. }
  495. PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  496. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  497. ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
  498. return lightmap->tetrahedra;
  499. }
  500. PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  501. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  502. ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
  503. return lightmap->bsp_tree;
  504. }
  505. AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
  506. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  507. ERR_FAIL_NULL_V(lightmap, AABB());
  508. return lightmap->bounds;
  509. }
  510. void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  511. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  512. ERR_FAIL_NULL(lm);
  513. for (int i = 0; i < 9; i++) {
  514. r_sh[i] = Color(0, 0, 0, 0);
  515. }
  516. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  517. return;
  518. }
  519. static_assert(sizeof(Lightmap::BSP) == 24);
  520. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  521. int32_t node = 0;
  522. while (node >= 0) {
  523. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  524. #ifdef DEBUG_ENABLED
  525. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  526. #endif
  527. node = bsp[node].over;
  528. } else {
  529. #ifdef DEBUG_ENABLED
  530. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  531. #endif
  532. node = bsp[node].under;
  533. }
  534. }
  535. if (node == Lightmap::BSP::EMPTY_LEAF) {
  536. return; // Nothing could be done.
  537. }
  538. node = ABS(node) - 1;
  539. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  540. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  541. 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] };
  542. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  543. for (int i = 0; i < 4; i++) {
  544. float c = CLAMP(barycentric[i], 0.0, 1.0);
  545. for (int j = 0; j < 9; j++) {
  546. r_sh[j] += sh_colors[i][j] * c;
  547. }
  548. }
  549. }
  550. bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
  551. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  552. ERR_FAIL_NULL_V(lightmap, false);
  553. return lightmap->interior;
  554. }
  555. void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
  556. lightmap_probe_capture_update_speed = p_speed;
  557. }
  558. float LightStorage::lightmap_get_probe_capture_update_speed() const {
  559. return lightmap_probe_capture_update_speed;
  560. }
  561. /* LIGHTMAP INSTANCE */
  562. RID LightStorage::lightmap_instance_create(RID p_lightmap) {
  563. LightmapInstance li;
  564. li.lightmap = p_lightmap;
  565. return lightmap_instance_owner.make_rid(li);
  566. }
  567. void LightStorage::lightmap_instance_free(RID p_lightmap) {
  568. lightmap_instance_owner.free(p_lightmap);
  569. }
  570. void LightStorage::lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) {
  571. LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap);
  572. ERR_FAIL_NULL(li);
  573. li->transform = p_transform;
  574. }
  575. /* SHADOW ATLAS API */
  576. RID LightStorage::shadow_atlas_create() {
  577. return shadow_atlas_owner.make_rid(ShadowAtlas());
  578. }
  579. void LightStorage::shadow_atlas_free(RID p_atlas) {
  580. shadow_atlas_set_size(p_atlas, 0);
  581. shadow_atlas_owner.free(p_atlas);
  582. }
  583. void LightStorage::shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits) {
  584. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  585. ERR_FAIL_NULL(shadow_atlas);
  586. ERR_FAIL_COND(p_size < 0);
  587. p_size = next_power_of_2(p_size);
  588. if (p_size == shadow_atlas->size && p_16_bits == shadow_atlas->use_16_bits) {
  589. return;
  590. }
  591. for (uint32_t i = 0; i < 4; i++) {
  592. // Clear all subdivisions and free shadows.
  593. for (uint32_t j = 0; j < shadow_atlas->quadrants[i].textures.size(); j++) {
  594. glDeleteTextures(1, &shadow_atlas->quadrants[i].textures[j]);
  595. glDeleteFramebuffers(1, &shadow_atlas->quadrants[i].fbos[j]);
  596. }
  597. shadow_atlas->quadrants[i].textures.clear();
  598. shadow_atlas->quadrants[i].fbos.clear();
  599. shadow_atlas->quadrants[i].shadows.clear();
  600. shadow_atlas->quadrants[i].shadows.resize(shadow_atlas->quadrants[i].subdivision * shadow_atlas->quadrants[i].subdivision);
  601. }
  602. // Erase shadow atlas reference from lights.
  603. for (const KeyValue<RID, uint32_t> &E : shadow_atlas->shadow_owners) {
  604. LightInstance *li = light_instance_owner.get_or_null(E.key);
  605. ERR_CONTINUE(!li);
  606. li->shadow_atlases.erase(p_atlas);
  607. }
  608. if (shadow_atlas->debug_texture != 0) {
  609. glDeleteTextures(1, &shadow_atlas->debug_texture);
  610. }
  611. if (shadow_atlas->debug_fbo != 0) {
  612. glDeleteFramebuffers(1, &shadow_atlas->debug_fbo);
  613. }
  614. // Clear owners.
  615. shadow_atlas->shadow_owners.clear();
  616. shadow_atlas->size = p_size;
  617. shadow_atlas->use_16_bits = p_16_bits;
  618. }
  619. void LightStorage::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  620. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  621. ERR_FAIL_NULL(shadow_atlas);
  622. ERR_FAIL_INDEX(p_quadrant, 4);
  623. ERR_FAIL_INDEX(p_subdivision, 16384);
  624. uint32_t subdiv = next_power_of_2(p_subdivision);
  625. if (subdiv & 0xaaaaaaaa) { // sqrt(subdiv) must be integer.
  626. subdiv <<= 1;
  627. }
  628. subdiv = int(Math::sqrt((float)subdiv));
  629. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  630. return;
  631. }
  632. // Erase all data from quadrant.
  633. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  634. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  635. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  636. LightInstance *li = light_instance_owner.get_or_null(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  637. ERR_CONTINUE(!li);
  638. li->shadow_atlases.erase(p_atlas);
  639. }
  640. }
  641. for (uint32_t j = 0; j < shadow_atlas->quadrants[p_quadrant].textures.size(); j++) {
  642. glDeleteTextures(1, &shadow_atlas->quadrants[p_quadrant].textures[j]);
  643. glDeleteFramebuffers(1, &shadow_atlas->quadrants[p_quadrant].fbos[j]);
  644. }
  645. shadow_atlas->quadrants[p_quadrant].textures.clear();
  646. shadow_atlas->quadrants[p_quadrant].fbos.clear();
  647. shadow_atlas->quadrants[p_quadrant].shadows.clear();
  648. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  649. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  650. // Cache the smallest subdiv (for faster allocation in light update).
  651. shadow_atlas->smallest_subdiv = 1 << 30;
  652. for (int i = 0; i < 4; i++) {
  653. if (shadow_atlas->quadrants[i].subdivision) {
  654. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  655. }
  656. }
  657. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  658. shadow_atlas->smallest_subdiv = 0;
  659. }
  660. // Re-sort the size orders, simple bubblesort for 4 elements.
  661. int swaps = 0;
  662. do {
  663. swaps = 0;
  664. for (int i = 0; i < 3; i++) {
  665. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  666. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  667. swaps++;
  668. }
  669. }
  670. } while (swaps > 0);
  671. }
  672. bool LightStorage::shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) {
  673. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  674. ERR_FAIL_NULL_V(shadow_atlas, false);
  675. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  676. ERR_FAIL_NULL_V(li, false);
  677. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  678. return false;
  679. }
  680. uint32_t quad_size = shadow_atlas->size >> 1;
  681. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  682. int valid_quadrants[4];
  683. int valid_quadrant_count = 0;
  684. int best_size = -1; // Best size found.
  685. int best_subdiv = -1; // Subdiv for the best size.
  686. // Find the quadrants this fits into, and the best possible size it can fit into.
  687. for (int i = 0; i < 4; i++) {
  688. int q = shadow_atlas->size_order[i];
  689. int sd = shadow_atlas->quadrants[q].subdivision;
  690. if (sd == 0) {
  691. continue; // Unused.
  692. }
  693. int max_fit = quad_size / sd;
  694. if (best_size != -1 && max_fit > best_size) {
  695. break; // Too large.
  696. }
  697. valid_quadrants[valid_quadrant_count++] = q;
  698. best_subdiv = sd;
  699. if (max_fit >= desired_fit) {
  700. best_size = max_fit;
  701. }
  702. }
  703. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  704. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  705. uint32_t old_key = SHADOW_INVALID;
  706. uint32_t old_quadrant = SHADOW_INVALID;
  707. uint32_t old_shadow = SHADOW_INVALID;
  708. int old_subdivision = -1;
  709. bool should_realloc = false;
  710. bool should_redraw = false;
  711. if (shadow_atlas->shadow_owners.has(p_light_instance)) {
  712. old_key = shadow_atlas->shadow_owners[p_light_instance];
  713. old_quadrant = (old_key >> QUADRANT_SHIFT) & 0x3;
  714. old_shadow = old_key & SHADOW_INDEX_MASK;
  715. // Only re-allocate if a better option is available, and enough time has passed.
  716. 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);
  717. should_redraw = shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].version != p_light_version;
  718. if (!should_realloc) {
  719. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = p_light_version;
  720. // Already existing, see if it should redraw or it's just OK.
  721. return should_redraw;
  722. }
  723. old_subdivision = shadow_atlas->quadrants[old_quadrant].subdivision;
  724. }
  725. bool is_omni = li->light_type == RS::LIGHT_OMNI;
  726. bool found_shadow = false;
  727. int new_quadrant = -1;
  728. int new_shadow = -1;
  729. found_shadow = _shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, is_omni, new_quadrant, new_shadow);
  730. // For new shadows if we found an atlas.
  731. // Or for existing shadows that found a better atlas.
  732. if (found_shadow) {
  733. if (old_quadrant != SHADOW_INVALID) {
  734. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = 0;
  735. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].owner = RID();
  736. }
  737. uint32_t new_key = new_quadrant << QUADRANT_SHIFT;
  738. new_key |= new_shadow;
  739. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  740. _shadow_atlas_invalidate_shadow(sh, p_atlas, shadow_atlas, new_quadrant, new_shadow);
  741. sh->owner = p_light_instance;
  742. sh->owner_is_omni = is_omni;
  743. sh->alloc_tick = tick;
  744. sh->version = p_light_version;
  745. li->shadow_atlases.insert(p_atlas);
  746. // Update it in map.
  747. shadow_atlas->shadow_owners[p_light_instance] = new_key;
  748. // Make it dirty, as it should redraw anyway.
  749. return true;
  750. }
  751. return should_redraw;
  752. }
  753. 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, bool is_omni, int &r_quadrant, int &r_shadow) {
  754. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  755. int qidx = p_in_quadrants[i];
  756. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  757. return false;
  758. }
  759. // Look for an empty space.
  760. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  761. const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
  762. // We have a free space in this quadrant, allocate a texture and use it.
  763. if (sc > (int)shadow_atlas->quadrants[qidx].textures.size()) {
  764. GLuint fbo_id = 0;
  765. glGenFramebuffers(1, &fbo_id);
  766. glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);
  767. GLuint texture_id = 0;
  768. glGenTextures(1, &texture_id);
  769. glActiveTexture(GL_TEXTURE0);
  770. int size = (shadow_atlas->size >> 1) / shadow_atlas->quadrants[qidx].subdivision;
  771. GLenum format = shadow_atlas->use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
  772. GLenum type = shadow_atlas->use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  773. if (is_omni) {
  774. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
  775. for (int id = 0; id < 6; id++) {
  776. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + id, 0, format, size / 2, size / 2, 0, GL_DEPTH_COMPONENT, type, nullptr);
  777. }
  778. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  779. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  780. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  781. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  782. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  783. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  784. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  785. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X, texture_id, 0);
  786. #ifdef DEBUG_ENABLED
  787. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  788. if (status != GL_FRAMEBUFFER_COMPLETE) {
  789. ERR_PRINT("Could not create omni light shadow framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  790. }
  791. #endif
  792. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  793. } else {
  794. glBindTexture(GL_TEXTURE_2D, texture_id);
  795. glTexImage2D(GL_TEXTURE_2D, 0, format, size, size, 0, GL_DEPTH_COMPONENT, type, nullptr);
  796. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  797. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  798. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  799. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  800. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  801. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  802. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0);
  803. glBindTexture(GL_TEXTURE_2D, 0);
  804. }
  805. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  806. r_quadrant = qidx;
  807. r_shadow = shadow_atlas->quadrants[qidx].textures.size();
  808. shadow_atlas->quadrants[qidx].textures.push_back(texture_id);
  809. shadow_atlas->quadrants[qidx].fbos.push_back(fbo_id);
  810. return true;
  811. }
  812. int found_used_idx = -1; // Found existing one, must steal it.
  813. uint64_t min_pass = 0; // Pass of the existing one, try to use the least recently used one (LRU fashion).
  814. for (int j = 0; j < sc; j++) {
  815. LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
  816. ERR_CONTINUE(!sli);
  817. if (sli->last_scene_pass != RasterizerSceneGLES3::get_singleton()->get_scene_pass()) {
  818. // Was just allocated, don't kill it so soon, wait a bit.
  819. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  820. continue;
  821. }
  822. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  823. found_used_idx = j;
  824. min_pass = sli->last_scene_pass;
  825. }
  826. }
  827. }
  828. if (found_used_idx != -1) {
  829. r_quadrant = qidx;
  830. r_shadow = found_used_idx;
  831. return true;
  832. }
  833. }
  834. return false;
  835. }
  836. 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) {
  837. if (p_shadow->owner.is_valid()) {
  838. LightInstance *sli = light_instance_owner.get_or_null(p_shadow->owner);
  839. p_shadow_atlas->shadow_owners.erase(p_shadow->owner);
  840. p_shadow->version = 0;
  841. p_shadow->owner = RID();
  842. sli->shadow_atlases.erase(p_atlas);
  843. }
  844. }
  845. void LightStorage::shadow_atlas_update(RID p_atlas) {
  846. // Do nothing as there is no shadow atlas texture.
  847. }
  848. /* DIRECTIONAL SHADOW */
  849. // Create if necessary and clear.
  850. void LightStorage::update_directional_shadow_atlas() {
  851. if (directional_shadow.depth == 0 && directional_shadow.size > 0) {
  852. glGenFramebuffers(1, &directional_shadow.fbo);
  853. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  854. glGenTextures(1, &directional_shadow.depth);
  855. glActiveTexture(GL_TEXTURE0);
  856. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  857. GLenum format = directional_shadow.use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
  858. GLenum type = directional_shadow.use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  859. glTexImage2D(GL_TEXTURE_2D, 0, format, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, type, nullptr);
  860. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  861. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  862. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  863. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  864. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  865. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  866. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
  867. }
  868. glUseProgram(0);
  869. glDepthMask(GL_TRUE);
  870. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  871. RasterizerGLES3::clear_depth(1.0);
  872. glClear(GL_DEPTH_BUFFER_BIT);
  873. glBindTexture(GL_TEXTURE_2D, 0);
  874. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  875. }
  876. void LightStorage::directional_shadow_atlas_set_size(int p_size, bool p_16_bits) {
  877. p_size = nearest_power_of_2_templated(p_size);
  878. if (directional_shadow.size == p_size && directional_shadow.use_16_bits == p_16_bits) {
  879. return;
  880. }
  881. directional_shadow.size = p_size;
  882. directional_shadow.use_16_bits = p_16_bits;
  883. if (directional_shadow.depth != 0) {
  884. glDeleteTextures(1, &directional_shadow.depth);
  885. directional_shadow.depth = 0;
  886. glDeleteFramebuffers(1, &directional_shadow.fbo);
  887. directional_shadow.fbo = 0;
  888. }
  889. }
  890. void LightStorage::set_directional_shadow_count(int p_count) {
  891. directional_shadow.light_count = p_count;
  892. directional_shadow.current_light = 0;
  893. }
  894. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  895. int split_h = 1;
  896. int split_v = 1;
  897. while (split_h * split_v < p_shadow_count) {
  898. if (split_h == split_v) {
  899. split_h <<= 1;
  900. } else {
  901. split_v <<= 1;
  902. }
  903. }
  904. Rect2i rect(0, 0, p_size, p_size);
  905. rect.size.width /= split_h;
  906. rect.size.height /= split_v;
  907. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  908. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  909. return rect;
  910. }
  911. Rect2i LightStorage::get_directional_shadow_rect() {
  912. return _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  913. }
  914. int LightStorage::get_directional_light_shadow_size(RID p_light_instance) {
  915. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  916. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  917. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  918. ERR_FAIL_NULL_V(light_instance, 0);
  919. switch (light_directional_get_shadow_mode(light_instance->light)) {
  920. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  921. break; //none
  922. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  923. r.size.height /= 2;
  924. break;
  925. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  926. r.size /= 2;
  927. break;
  928. }
  929. return MAX(r.size.width, r.size.height);
  930. }
  931. #endif // !GLES3_ENABLED