Debian Codegraph @

drivers/gles3/storage/light_storage.cpp

raw symbols SCIP
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
31 #ifdef GLES3_ENABLED
32
33 #include "light_storage.h"
34 #include "../rasterizer_gles3.h"
35 #include "../rasterizer_scene_gles3.h"
36 #include "core/config/project_settings.h"
37 #include "texture_storage.h"
38
39 using namespace GLES3;
40
41 LightStorage *LightStorage::singleton = nullptr;
42
43 LightStorage *LightStorage::get_singleton() {
44 return singleton;
45 }
46
47 LightStorage::LightStorage() {
48 singleton = this;
49
50 directional_shadow.size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
51 directional_shadow.use_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
52
53 lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
54 }
55
56 LightStorage::~LightStorage() {
57 singleton = nullptr;
58 }
59
60 /* Light API */
61
62 void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
63 Light light;
64 light.type = p_type;
65
66 light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
67 light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
68 light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
69 light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
70 light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
71 light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
72 light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
73 light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
74 light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
75 light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
76 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
77 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
78 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
79 light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
80 light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
81 light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
82 light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
83 light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
84 light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
85 light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
86 light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
87
88 light_owner.initialize_rid(p_light, light);
89 }
90
91 RID LightStorage::directional_light_allocate() {
92 return light_owner.allocate_rid();
93 }
94
95 void LightStorage::directional_light_initialize(RID p_rid) {
96 _light_initialize(p_rid, RS::LIGHT_DIRECTIONAL);
97 }
98
99 RID LightStorage::omni_light_allocate() {
100 return light_owner.allocate_rid();
101 }
102
103 void LightStorage::omni_light_initialize(RID p_rid) {
104 _light_initialize(p_rid, RS::LIGHT_OMNI);
105 }
106
107 RID LightStorage::spot_light_allocate() {
108 return light_owner.allocate_rid();
109 }
110
111 void LightStorage::spot_light_initialize(RID p_rid) {
112 _light_initialize(p_rid, RS::LIGHT_SPOT);
113 }
114
115 void LightStorage::light_free(RID p_rid) {
116 light_set_projector(p_rid, RID()); //clear projector
117
118 // delete the texture
119 Light *light = light_owner.get_or_null(p_rid);
120 light->dependency.deleted_notify(p_rid);
121 light_owner.free(p_rid);
122 }
123
124 void LightStorage::light_set_color(RID p_light, const Color &p_color) {
125 Light *light = light_owner.get_or_null(p_light);
126 ERR_FAIL_NULL(light);
127
128 light->color = p_color;
129 }
130
131 void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
132 Light *light = light_owner.get_or_null(p_light);
133 ERR_FAIL_NULL(light);
134 ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
135
136 if (light->param[p_param] == p_value) {
137 return;
138 }
139
140 switch (p_param) {
141 case RS::LIGHT_PARAM_RANGE:
142 case RS::LIGHT_PARAM_SPOT_ANGLE:
143 case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
144 case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
145 case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
146 case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
147 case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
148 case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
149 case RS::LIGHT_PARAM_SHADOW_BIAS: {
150 light->version++;
151 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
152 } break;
153 case RS::LIGHT_PARAM_SIZE: {
154 if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
155 //changing from no size to size and the opposite
156 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
157 }
158 } break;
159 default: {
160 }
161 }
162
163 light->param[p_param] = p_value;
164 }
165
166 void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
167 Light *light = light_owner.get_or_null(p_light);
168 ERR_FAIL_NULL(light);
169 light->shadow = p_enabled;
170
171 light->version++;
172 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
173 }
174
175 void LightStorage::light_set_projector(RID p_light, RID p_texture) {
176 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
177 Light *light = light_owner.get_or_null(p_light);
178 ERR_FAIL_NULL(light);
179
180 if (light->projector == p_texture) {
181 return;
182 }
183
184 if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
185 texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
186 }
187
188 light->projector = p_texture;
189
190 if (light->type != RS::LIGHT_DIRECTIONAL) {
191 if (light->projector.is_valid()) {
192 texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
193 }
194 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
195 }
196 }
197
198 void LightStorage::light_set_negative(RID p_light, bool p_enable) {
199 Light *light = light_owner.get_or_null(p_light);
200 ERR_FAIL_NULL(light);
201
202 light->negative = p_enable;
203 }
204
205 void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
206 Light *light = light_owner.get_or_null(p_light);
207 ERR_FAIL_NULL(light);
208
209 light->cull_mask = p_mask;
210
211 light->version++;
212 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_CULL_MASK);
213 }
214
215 void LightStorage::light_set_shadow_caster_mask(RID p_light, uint32_t p_caster_mask) {
216 Light *light = light_owner.get_or_null(p_light);
217 ERR_FAIL_NULL(light);
218
219 light->shadow_caster_mask = p_caster_mask;
220
221 light->version++;
222 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
223 }
224
225 uint32_t LightStorage::light_get_shadow_caster_mask(RID p_light) const {
226 Light *light = light_owner.get_or_null(p_light);
227 ERR_FAIL_NULL_V(light, 0);
228
229 return light->shadow_caster_mask;
230 }
231
232 void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
233 Light *light = light_owner.get_or_null(p_light);
234 ERR_FAIL_NULL(light);
235
236 light->distance_fade = p_enabled;
237 light->distance_fade_begin = p_begin;
238 light->distance_fade_shadow = p_shadow;
239 light->distance_fade_length = p_length;
240 }
241
242 void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
243 Light *light = light_owner.get_or_null(p_light);
244 ERR_FAIL_NULL(light);
245
246 light->reverse_cull = p_enabled;
247
248 light->version++;
249 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
250 }
251
252 void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
253 Light *light = light_owner.get_or_null(p_light);
254 ERR_FAIL_NULL(light);
255
256 light->bake_mode = p_bake_mode;
257
258 light->version++;
259 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
260 }
261
262 void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
263 Light *light = light_owner.get_or_null(p_light);
264 ERR_FAIL_NULL(light);
265
266 light->omni_shadow_mode = p_mode;
267
268 light->version++;
269 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
270 }
271
272 RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
273 const Light *light = light_owner.get_or_null(p_light);
274 ERR_FAIL_NULL_V(light, RS::LIGHT_OMNI_SHADOW_CUBE);
275
276 return light->omni_shadow_mode;
277 }
278
279 void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
280 Light *light = light_owner.get_or_null(p_light);
281 ERR_FAIL_NULL(light);
282
283 light->directional_shadow_mode = p_mode;
284 light->version++;
285 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
286 }
287
288 void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
289 Light *light = light_owner.get_or_null(p_light);
290 ERR_FAIL_NULL(light);
291
292 light->directional_blend_splits = p_enable;
293 light->version++;
294 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
295 }
296
297 bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
298 const Light *light = light_owner.get_or_null(p_light);
299 ERR_FAIL_NULL_V(light, false);
300
301 return light->directional_blend_splits;
302 }
303
304 void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
305 Light *light = light_owner.get_or_null(p_light);
306 ERR_FAIL_NULL(light);
307
308 light->directional_sky_mode = p_mode;
309 }
310
311 RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
312 const Light *light = light_owner.get_or_null(p_light);
313 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
314
315 return light->directional_sky_mode;
316 }
317
318 RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
319 const Light *light = light_owner.get_or_null(p_light);
320 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
321
322 return light->directional_shadow_mode;
323 }
324
325 RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
326 const Light *light = light_owner.get_or_null(p_light);
327 ERR_FAIL_NULL_V(light, RS::LIGHT_BAKE_DISABLED);
328
329 return light->bake_mode;
330 }
331
332 uint64_t LightStorage::light_get_version(RID p_light) const {
333 const Light *light = light_owner.get_or_null(p_light);
334 ERR_FAIL_NULL_V(light, 0);
335
336 return light->version;
337 }
338
339 uint32_t LightStorage::light_get_cull_mask(RID p_light) const {
340 const Light *light = light_owner.get_or_null(p_light);
341 ERR_FAIL_NULL_V(light, 0);
342
343 return light->cull_mask;
344 }
345
346 AABB LightStorage::light_get_aabb(RID p_light) const {
347 const Light *light = light_owner.get_or_null(p_light);
348 ERR_FAIL_NULL_V(light, AABB());
349
350 switch (light->type) {
351 case RS::LIGHT_SPOT: {
352 float len = light->param[RS::LIGHT_PARAM_RANGE];
353 float angle = Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE]);
354
355 if (angle > Math::PI * 0.5) {
356 // Light casts backwards as well.
357 return AABB(Vector3(-1, -1, -1) * len, Vector3(2, 2, 2) * len);
358 }
359
360 float size = Math::sin(angle) * len;
361 return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
362 };
363 case RS::LIGHT_OMNI: {
364 float r = light->param[RS::LIGHT_PARAM_RANGE];
365 return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
366 };
367 case RS::LIGHT_DIRECTIONAL: {
368 return AABB();
369 };
370 }
371
372 ERR_FAIL_V(AABB());
373 }
374
375 /* LIGHT INSTANCE API */
376
377 RID LightStorage::light_instance_create(RID p_light) {
378 RID li = light_instance_owner.make_rid(LightInstance());
379
380 LightInstance *light_instance = light_instance_owner.get_or_null(li);
381
382 light_instance->self = li;
383 light_instance->light = p_light;
384 light_instance->light_type = light_get_type(p_light);
385
386 return li;
387 }
388
389 void LightStorage::light_instance_free(RID p_light_instance) {
390 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
391 ERR_FAIL_NULL(light_instance);
392
393 // Remove from shadow atlases.
394 for (const RID &E : light_instance->shadow_atlases) {
395 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(E);
396 ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_light_instance));
397 uint32_t key = shadow_atlas->shadow_owners[p_light_instance];
398 uint32_t q = (key >> QUADRANT_SHIFT) & 0x3;
399 uint32_t s = key & SHADOW_INDEX_MASK;
400
401 shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
402
403 shadow_atlas->shadow_owners.erase(p_light_instance);
404 }
405
406 light_instance_owner.free(p_light_instance);
407 }
408
409 void LightStorage::light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) {
410 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
411 ERR_FAIL_NULL(light_instance);
412
413 light_instance->transform = p_transform;
414 }
415
416 void LightStorage::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
417 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
418 ERR_FAIL_NULL(light_instance);
419
420 light_instance->aabb = p_aabb;
421 }
422
423 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) {
424 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
425 ERR_FAIL_NULL(light_instance);
426
427 ERR_FAIL_INDEX(p_pass, 6);
428
429 light_instance->shadow_transform[p_pass].camera = p_projection;
430 light_instance->shadow_transform[p_pass].transform = p_transform;
431 light_instance->shadow_transform[p_pass].farplane = p_far;
432 light_instance->shadow_transform[p_pass].split = p_split;
433 light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
434 light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
435 light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
436 light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
437 }
438
439 void LightStorage::light_instance_mark_visible(RID p_light_instance) {
440 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
441 ERR_FAIL_NULL(light_instance);
442
443 light_instance->last_scene_pass = RasterizerSceneGLES3::get_singleton()->get_scene_pass();
444 }
445
446 /* PROBE API */
447
448 RID LightStorage::reflection_probe_allocate() {
449 return reflection_probe_owner.allocate_rid();
450 }
451
452 void LightStorage::reflection_probe_initialize(RID p_rid) {
453 ReflectionProbe probe;
454
455 reflection_probe_owner.initialize_rid(p_rid, probe);
456 }
457
458 void LightStorage::reflection_probe_free(RID p_rid) {
459 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_rid);
460 reflection_probe->dependency.deleted_notify(p_rid);
461
462 reflection_probe_owner.free(p_rid);
463 }
464
465 void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
466 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
467 ERR_FAIL_NULL(reflection_probe);
468
469 reflection_probe->update_mode = p_mode;
470 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
471 }
472
473 void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
474 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
475 ERR_FAIL_NULL(reflection_probe);
476
477 reflection_probe->intensity = p_intensity;
478 }
479
480 void LightStorage::reflection_probe_set_blend_distance(RID p_probe, float p_blend_distance) {
481 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
482 ERR_FAIL_NULL(reflection_probe);
483
484 reflection_probe->blend_distance = p_blend_distance;
485 }
486
487 void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
488 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
489 ERR_FAIL_NULL(reflection_probe);
490
491 reflection_probe->ambient_mode = p_mode;
492 }
493
494 void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
495 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
496 ERR_FAIL_NULL(reflection_probe);
497
498 reflection_probe->ambient_color = p_color;
499 }
500
501 void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
502 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
503 ERR_FAIL_NULL(reflection_probe);
504
505 reflection_probe->ambient_color_energy = p_energy;
506 }
507
508 void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
509 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
510 ERR_FAIL_NULL(reflection_probe);
511
512 reflection_probe->max_distance = p_distance;
513 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
514 }
515
516 void LightStorage::reflection_probe_set_size(RID p_probe, const Vector3 &p_size) {
517 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
518 ERR_FAIL_NULL(reflection_probe);
519
520 reflection_probe->size = p_size;
521 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
522 }
523
524 void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
525 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
526 ERR_FAIL_NULL(reflection_probe);
527
528 reflection_probe->origin_offset = p_offset;
529 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
530 }
531
532 void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
533 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
534 ERR_FAIL_NULL(reflection_probe);
535
536 reflection_probe->interior = p_enable;
537 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
538 }
539
540 void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
541 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
542 ERR_FAIL_NULL(reflection_probe);
543
544 reflection_probe->box_projection = p_enable;
545 }
546
547 void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
548 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
549 ERR_FAIL_NULL(reflection_probe);
550
551 reflection_probe->enable_shadows = p_enable;
552 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
553 }
554
555 void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
556 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
557 ERR_FAIL_NULL(reflection_probe);
558
559 reflection_probe->cull_mask = p_layers;
560 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
561 }
562
563 void LightStorage::reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) {
564 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
565 ERR_FAIL_NULL(reflection_probe);
566
567 reflection_probe->reflection_mask = p_layers;
568 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
569 }
570
571 void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
572 WARN_PRINT_ONCE("reflection_probe_set_resolution is not available in Godot 4. ReflectionProbe size is configured in the project settings with the rendering/reflections/reflection_atlas/reflection_size setting.");
573 }
574
575 AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
576 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
577 ERR_FAIL_NULL_V(reflection_probe, AABB());
578
579 AABB aabb;
580 aabb.position = -reflection_probe->size / 2;
581 aabb.size = reflection_probe->size;
582
583 return aabb;
584 }
585
586 RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
587 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
588 ERR_FAIL_NULL_V(reflection_probe, RenderingServer::REFLECTION_PROBE_UPDATE_ONCE);
589
590 return reflection_probe->update_mode;
591 }
592
593 uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
594 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
595 ERR_FAIL_NULL_V(reflection_probe, 0);
596
597 return reflection_probe->cull_mask;
598 }
599
600 uint32_t LightStorage::reflection_probe_get_reflection_mask(RID p_probe) const {
601 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
602 ERR_FAIL_NULL_V(reflection_probe, 0);
603
604 return reflection_probe->reflection_mask;
605 }
606
607 Vector3 LightStorage::reflection_probe_get_size(RID p_probe) const {
608 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
609 ERR_FAIL_NULL_V(reflection_probe, Vector3());
610
611 return reflection_probe->size;
612 }
613
614 Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
615 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
616 ERR_FAIL_NULL_V(reflection_probe, Vector3());
617
618 return reflection_probe->origin_offset;
619 }
620
621 float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
622 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
623 ERR_FAIL_NULL_V(reflection_probe, 0.0);
624
625 return reflection_probe->max_distance;
626 }
627
628 bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
629 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
630 ERR_FAIL_NULL_V(reflection_probe, false);
631
632 return reflection_probe->enable_shadows;
633 }
634
635 void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
636 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
637 ERR_FAIL_NULL(reflection_probe);
638
639 reflection_probe->mesh_lod_threshold = p_ratio;
640 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
641 }
642
643 float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
644 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
645 ERR_FAIL_NULL_V(reflection_probe, 0.0);
646
647 return reflection_probe->mesh_lod_threshold;
648 }
649
650 Dependency *LightStorage::reflection_probe_get_dependency(RID p_probe) const {
651 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
652 ERR_FAIL_NULL_V(reflection_probe, nullptr);
653
654 return &reflection_probe->dependency;
655 }
656
657 /* REFLECTION ATLAS */
658
659 RID LightStorage::reflection_atlas_create() {
660 ReflectionAtlas ra;
661 ra.count = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_count");
662 ra.size = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_size");
663
664 return reflection_atlas_owner.make_rid(ra);
665 }
666
667 void LightStorage::reflection_atlas_free(RID p_ref_atlas) {
668 reflection_atlas_set_size(p_ref_atlas, 0, 0);
669
670 reflection_atlas_owner.free(p_ref_atlas);
671 }
672
673 int LightStorage::reflection_atlas_get_size(RID p_ref_atlas) const {
674 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
675 ERR_FAIL_NULL_V(ra, 0);
676
677 return ra->size;
678 }
679
680 void LightStorage::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
681 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
682 ERR_FAIL_NULL(ra);
683
684 if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
685 return; //no changes
686 }
687
688 ra->size = p_reflection_size;
689 ra->count = p_reflection_count;
690
691 if (ra->depth != 0) {
692 //clear and invalidate everything
693 for (int i = 0; i < ra->reflections.size(); i++) {
694 for (int j = 0; j < 7; j++) {
695 if (ra->reflections[i].fbos[j] != 0) {
696 glDeleteFramebuffers(1, &ra->reflections[i].fbos[j]);
697 ra->reflections.write[i].fbos[j] = 0;
698 }
699 }
700
701 GLES3::Utilities::get_singleton()->texture_free_data(ra->reflections[i].color);
702 ra->reflections.write[i].color = 0;
703
704 GLES3::Utilities::get_singleton()->texture_free_data(ra->reflections[i].radiance);
705 ra->reflections.write[i].radiance = 0;
706
707 if (ra->reflections[i].owner.is_null()) {
708 continue;
709 }
710 reflection_probe_release_atlas_index(ra->reflections[i].owner);
711 //rp->atlasindex clear
712 }
713
714 ra->reflections.clear();
715
716 GLES3::Utilities::get_singleton()->texture_free_data(ra->depth);
717 ra->depth = 0;
718 }
719
720 if (ra->render_buffers.is_valid()) {
721 ra->render_buffers->free_render_buffer_data();
722 }
723 }
724
725 /* REFLECTION PROBE INSTANCE */
726
727 RID LightStorage::reflection_probe_instance_create(RID p_probe) {
728 ReflectionProbeInstance rpi;
729 rpi.probe = p_probe;
730
731 return reflection_probe_instance_owner.make_rid(rpi);
732 }
733
734 void LightStorage::reflection_probe_instance_free(RID p_instance) {
735 reflection_probe_release_atlas_index(p_instance);
736 reflection_probe_instance_owner.free(p_instance);
737 }
738
739 void LightStorage::reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) {
740 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
741 ERR_FAIL_NULL(rpi);
742
743 rpi->transform = p_transform;
744 rpi->dirty = true;
745 }
746
747 bool LightStorage::reflection_probe_has_atlas_index(RID p_instance) {
748 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
749 ERR_FAIL_NULL_V(rpi, false);
750
751 if (rpi->atlas.is_null()) {
752 return false;
753 }
754
755 return rpi->atlas_index >= 0;
756 }
757
758 void LightStorage::reflection_probe_release_atlas_index(RID p_instance) {
759 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
760 ERR_FAIL_NULL(rpi);
761
762 if (rpi->atlas.is_null()) {
763 return; //nothing to release
764 }
765 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
766 ERR_FAIL_NULL(atlas);
767
768 ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
769 atlas->reflections.write[rpi->atlas_index].owner = RID();
770
771 if (rpi->rendering) {
772 // We were cancelled mid rendering, trigger refresh.
773 rpi->rendering = false;
774 rpi->dirty = true;
775 rpi->processing_layer = 0;
776 }
777
778 rpi->atlas_index = -1;
779 rpi->atlas = RID();
780 }
781
782 bool LightStorage::reflection_probe_instance_needs_redraw(RID p_instance) {
783 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
784 ERR_FAIL_NULL_V(rpi, false);
785
786 if (rpi->rendering) {
787 return false;
788 }
789
790 if (rpi->dirty) {
791 return true;
792 }
793
794 if (reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
795 return true;
796 }
797
798 return rpi->atlas_index == -1;
799 }
800
801 bool LightStorage::reflection_probe_instance_has_reflection(RID p_instance) {
802 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
803 ERR_FAIL_NULL_V(rpi, false);
804
805 return rpi->atlas.is_valid();
806 }
807
808 bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
809 TextureStorage *texture_storage = TextureStorage::get_singleton();
810 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
811
812 ERR_FAIL_NULL_V(atlas, false);
813
814 ERR_FAIL_COND_V_MSG(atlas->size < 4, false, "Attempted to render to a reflection atlas of invalid resolution.");
815 ERR_FAIL_COND_V_MSG(atlas->count < 1, false, "Attempted to render to a reflection atlas of size < 1.");
816
817 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
818 ERR_FAIL_NULL_V(rpi, false);
819
820 if (atlas->render_buffers.is_null()) {
821 atlas->render_buffers.instantiate();
822 atlas->render_buffers->configure_for_probe(Size2i(atlas->size, atlas->size));
823 }
824
825 // First we check if our atlas is initialized.
826
827 // Not making an exception for update_mode = REFLECTION_PROBE_UPDATE_ALWAYS, we are using
828 // the same render techniques regardless of realtime or update once (for now).
829
830 if (atlas->depth == 0) {
831 // We need to create our textures
832 atlas->mipmap_count = Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) - 1;
833 atlas->mipmap_count = MIN(atlas->mipmap_count, 8); // No more than 8 please..
834
835 glActiveTexture(GL_TEXTURE0);
836
837 {
838 // We create one set of 6 layers for depth, we can reuse this when rendering.
839 glGenTextures(1, &atlas->depth);
840 glBindTexture(GL_TEXTURE_2D_ARRAY, atlas->depth);
841
842 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT24, atlas->size, atlas->size, 6, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
843
844 GLES3::Utilities::get_singleton()->texture_allocated_data(atlas->depth, atlas->size * atlas->size * 6 * 3, "Reflection probe atlas (depth)");
845 }
846
847 // Make room for our atlas entries
848 atlas->reflections.resize(atlas->count);
849
850 for (int i = 0; i < atlas->count; i++) {
851 // Create a cube map for this atlas entry
852 GLuint color = 0;
853 glGenTextures(1, &color);
854 glBindTexture(GL_TEXTURE_CUBE_MAP, color);
855 atlas->reflections.write[i].color = color;
856
857 #ifdef GL_API_ENABLED
858 if (RasterizerGLES3::is_gles_over_gl()) {
859 for (int s = 0; s < 6; s++) {
860 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + s, 0, GL_RGB10_A2, atlas->size, atlas->size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
861 }
862 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
863 }
864 #endif
865 #ifdef GLES_API_ENABLED
866 if (!RasterizerGLES3::is_gles_over_gl()) {
867 glTexStorage2D(GL_TEXTURE_CUBE_MAP, atlas->mipmap_count, GL_RGB10_A2, atlas->size, atlas->size);
868 }
869 #endif // GLES_API_ENABLED
870
871 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
872 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
873 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
874 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
875 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
876 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, atlas->mipmap_count - 1);
877
878 // Setup sizes and calculate how much memory we're using.
879 int mipmap_size = atlas->size;
880 uint32_t data_size = 0;
881 for (int m = 0; m < atlas->mipmap_count; m++) {
882 atlas->mipmap_size[m] = mipmap_size;
883 data_size += mipmap_size * mipmap_size * 6 * 4;
884 mipmap_size = MAX(mipmap_size >> 1, 1);
885 }
886
887 GLES3::Utilities::get_singleton()->texture_allocated_data(color, data_size, String("Reflection probe atlas (") + String::num_int64(i) + String(", color)"));
888
889 // Create a radiance map for this atlas entry
890 GLuint radiance = 0;
891 glGenTextures(1, &radiance);
892 glBindTexture(GL_TEXTURE_CUBE_MAP, radiance);
893 atlas->reflections.write[i].radiance = radiance;
894
895 #ifdef GL_API_ENABLED
896 if (RasterizerGLES3::is_gles_over_gl()) {
897 for (int s = 0; s < 6; s++) {
898 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + s, 0, GL_RGB10_A2, atlas->size, atlas->size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
899 }
900 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
901 }
902 #endif
903 #ifdef GLES_API_ENABLED
904 if (!RasterizerGLES3::is_gles_over_gl()) {
905 glTexStorage2D(GL_TEXTURE_CUBE_MAP, atlas->mipmap_count, GL_RGB10_A2, atlas->size, atlas->size);
906 }
907 #endif // GLES_API_ENABLED
908
909 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
910 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
911 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
912 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
913 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
914 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, atlas->mipmap_count - 1);
915
916 // Same data size as our color buffer
917 GLES3::Utilities::get_singleton()->texture_allocated_data(radiance, data_size, String("Reflection probe atlas (") + String::num_int64(i) + String(", radiance)"));
918
919 // Create our framebuffers so we can draw to all sides
920 for (int side = 0; side < 6; side++) {
921 GLuint fbo = 0;
922 glGenFramebuffers(1, &fbo);
923 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
924
925 // We use glFramebufferTexture2D for the color buffer as glFramebufferTextureLayer doesn't always work with cubemaps.
926 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, color, 0);
927 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, atlas->depth, 0, side);
928
929 // Validate framebuffer
930 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
931 if (status != GL_FRAMEBUFFER_COMPLETE) {
932 WARN_PRINT("Could not create reflections framebuffer, status: " + texture_storage->get_framebuffer_error(status));
933 }
934
935 atlas->reflections.write[i].fbos[side] = fbo;
936 }
937
938 // Create an extra framebuffer for building our radiance
939 {
940 GLuint fbo = 0;
941 glGenFramebuffers(1, &fbo);
942 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
943
944 atlas->reflections.write[i].fbos[6] = fbo;
945 }
946 }
947
948 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
949 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
950 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
951 }
952
953 // Then we find a free slot for our reflection probe
954
955 if (rpi->atlas_index == -1) {
956 for (int i = 0; i < atlas->reflections.size(); i++) {
957 if (atlas->reflections[i].owner.is_null()) {
958 rpi->atlas_index = i;
959 break;
960 }
961 }
962 //find the one used last
963 if (rpi->atlas_index == -1) {
964 //everything is in use, find the one least used via LRU
965 uint64_t pass_min = 0;
966
967 for (int i = 0; i < atlas->reflections.size(); i++) {
968 ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.get_or_null(atlas->reflections[i].owner);
969 if (rpi2->last_pass < pass_min) {
970 pass_min = rpi2->last_pass;
971 rpi->atlas_index = i;
972 }
973 }
974 }
975 }
976
977 if (rpi->atlas_index != -1) { // should we fail if this is still -1 ?
978 atlas->reflections.write[rpi->atlas_index].owner = p_instance;
979 }
980
981 rpi->atlas = p_reflection_atlas;
982 rpi->rendering = true;
983 rpi->dirty = false;
984 rpi->processing_layer = 0;
985
986 return true;
987 }
988
989 bool LightStorage::reflection_probe_instance_end_render(RID p_instance, RID p_reflection_atlas) {
990 return true;
991 }
992
993 Ref<RenderSceneBuffers> LightStorage::reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) {
994 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
995 ERR_FAIL_NULL_V(atlas, Ref<RenderSceneBuffersGLES3>());
996
997 return atlas->render_buffers;
998 }
999
1000 bool LightStorage::reflection_probe_instance_postprocess_step(RID p_instance) {
1001 GLES3::CubemapFilter *cubemap_filter = GLES3::CubemapFilter::get_singleton();
1002 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1003 ERR_FAIL_NULL_V(rpi, false);
1004 ERR_FAIL_COND_V(!rpi->rendering, false);
1005 ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
1006
1007 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1008 if (!atlas || rpi->atlas_index == -1) {
1009 //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
1010 rpi->rendering = false;
1011 rpi->processing_layer = 0;
1012 return false;
1013 }
1014
1015 if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
1016 // Using real time reflections, all roughness is done in one step
1017 for (int m = 0; m < atlas->mipmap_count; m++) {
1018 const GLES3::ReflectionAtlas::Reflection &reflection = atlas->reflections[rpi->atlas_index];
1019 cubemap_filter->filter_radiance(reflection.color, reflection.radiance, reflection.fbos[6], atlas->size, atlas->mipmap_count, m);
1020 }
1021
1022 rpi->rendering = false;
1023 rpi->processing_layer = 0;
1024 return true;
1025 } else {
1026 const GLES3::ReflectionAtlas::Reflection &reflection = atlas->reflections[rpi->atlas_index];
1027 cubemap_filter->filter_radiance(reflection.color, reflection.radiance, reflection.fbos[6], atlas->size, atlas->mipmap_count, rpi->processing_layer);
1028
1029 rpi->processing_layer++;
1030 if (rpi->processing_layer == atlas->mipmap_count) {
1031 rpi->rendering = false;
1032 rpi->processing_layer = 0;
1033 return true;
1034 }
1035 }
1036
1037 return false;
1038 }
1039
1040 GLuint LightStorage::reflection_probe_instance_get_texture(RID p_instance) {
1041 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1042 ERR_FAIL_NULL_V(rpi, 0);
1043
1044 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1045 ERR_FAIL_NULL_V(atlas, 0);
1046 ERR_FAIL_COND_V(rpi->atlas_index < 0, 0);
1047
1048 return atlas->reflections[rpi->atlas_index].radiance;
1049 }
1050
1051 GLuint LightStorage::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
1052 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1053 ERR_FAIL_NULL_V(rpi, 0);
1054 ERR_FAIL_INDEX_V(p_index, 6, 0);
1055
1056 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1057 ERR_FAIL_NULL_V(atlas, 0);
1058 ERR_FAIL_COND_V(rpi->atlas_index < 0, 0);
1059
1060 return atlas->reflections[rpi->atlas_index].fbos[p_index];
1061 }
1062
1063 /* LIGHTMAP CAPTURE */
1064
1065 RID LightStorage::lightmap_allocate() {
1066 return lightmap_owner.allocate_rid();
1067 }
1068
1069 void LightStorage::lightmap_initialize(RID p_rid) {
1070 lightmap_owner.initialize_rid(p_rid, Lightmap());
1071 }
1072
1073 void LightStorage::lightmap_free(RID p_rid) {
1074 Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
1075 ERR_FAIL_NULL(lightmap);
1076 lightmap->dependency.deleted_notify(p_rid);
1077 lightmap_owner.free(p_rid);
1078 }
1079
1080 void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
1081 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1082 ERR_FAIL_NULL(lightmap);
1083 lightmap->light_texture = p_light;
1084 lightmap->uses_spherical_harmonics = p_uses_spherical_haromics;
1085
1086 Vector3i light_texture_size = GLES3::TextureStorage::get_singleton()->texture_get_size(lightmap->light_texture);
1087 lightmap->light_texture_size = Vector2i(light_texture_size.x, light_texture_size.y);
1088
1089 GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lightmap->light_texture);
1090 glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
1091 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1092 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1093 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1094 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1095 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
1096 }
1097
1098 void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
1099 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1100 ERR_FAIL_NULL(lightmap);
1101 lightmap->bounds = p_bounds;
1102 }
1103
1104 void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
1105 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1106 ERR_FAIL_NULL(lightmap);
1107 lightmap->interior = p_interior;
1108 }
1109
1110 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) {
1111 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1112 ERR_FAIL_NULL(lightmap);
1113
1114 if (p_points.size()) {
1115 ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
1116 ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
1117 ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
1118 }
1119
1120 lightmap->points = p_points;
1121 lightmap->point_sh = p_point_sh;
1122 lightmap->tetrahedra = p_tetrahedra;
1123 lightmap->bsp_tree = p_bsp_tree;
1124 }
1125
1126 void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
1127 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1128 ERR_FAIL_NULL(lightmap);
1129
1130 lightmap->baked_exposure = p_exposure;
1131 }
1132
1133 PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
1134 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1135 ERR_FAIL_NULL_V(lightmap, PackedVector3Array());
1136 return lightmap->points;
1137 }
1138
1139 PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
1140 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1141 ERR_FAIL_NULL_V(lightmap, PackedColorArray());
1142 return lightmap->point_sh;
1143 }
1144
1145 PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
1146 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1147 ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
1148 return lightmap->tetrahedra;
1149 }
1150
1151 PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
1152 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1153 ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
1154 return lightmap->bsp_tree;
1155 }
1156
1157 AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
1158 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1159 ERR_FAIL_NULL_V(lightmap, AABB());
1160 return lightmap->bounds;
1161 }
1162
1163 void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
1164 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1165 ERR_FAIL_NULL(lm);
1166
1167 for (int i = 0; i < 9; i++) {
1168 r_sh[i] = Color(0, 0, 0, 0);
1169 }
1170
1171 if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
1172 return;
1173 }
1174
1175 static_assert(sizeof(Lightmap::BSP) == 24);
1176
1177 const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
1178 int32_t node = 0;
1179 while (node >= 0) {
1180 if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
1181 #ifdef DEBUG_ENABLED
1182 ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
1183 #endif
1184
1185 node = bsp[node].over;
1186 } else {
1187 #ifdef DEBUG_ENABLED
1188 ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
1189 #endif
1190 node = bsp[node].under;
1191 }
1192 }
1193
1194 if (node == Lightmap::BSP::EMPTY_LEAF) {
1195 return; // Nothing could be done.
1196 }
1197
1198 node = Math::abs(node) - 1;
1199
1200 uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
1201 Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
1202 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] };
1203 Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
1204
1205 for (int i = 0; i < 4; i++) {
1206 float c = CLAMP(barycentric[i], 0.0, 1.0);
1207 for (int j = 0; j < 9; j++) {
1208 r_sh[j] += sh_colors[i][j] * c;
1209 }
1210 }
1211 }
1212
1213 bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
1214 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1215 ERR_FAIL_NULL_V(lightmap, false);
1216 return lightmap->interior;
1217 }
1218
1219 void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
1220 lightmap_probe_capture_update_speed = p_speed;
1221 }
1222
1223 float LightStorage::lightmap_get_probe_capture_update_speed() const {
1224 return lightmap_probe_capture_update_speed;
1225 }
1226
1227 void LightStorage::lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) {
1228 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1229 ERR_FAIL_NULL(lightmap);
1230 lightmap->shadow_texture = p_shadow;
1231
1232 GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lightmap->shadow_texture);
1233 glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
1234 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1235 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1236 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1237 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1238 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
1239 }
1240
1241 RS::ShadowmaskMode LightStorage::lightmap_get_shadowmask_mode(RID p_lightmap) {
1242 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1243 ERR_FAIL_NULL_V(lightmap, RS::SHADOWMASK_MODE_NONE);
1244
1245 return lightmap->shadowmask_mode;
1246 }
1247
1248 void LightStorage::lightmap_set_shadowmask_mode(RID p_lightmap, RS::ShadowmaskMode p_mode) {
1249 Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
1250 ERR_FAIL_NULL(lightmap);
1251 lightmap->shadowmask_mode = p_mode;
1252 }
1253
1254 /* LIGHTMAP INSTANCE */
1255
1256 RID LightStorage::lightmap_instance_create(RID p_lightmap) {
1257 LightmapInstance li;
1258 li.lightmap = p_lightmap;
1259 return lightmap_instance_owner.make_rid(li);
1260 }
1261
1262 void LightStorage::lightmap_instance_free(RID p_lightmap) {
1263 lightmap_instance_owner.free(p_lightmap);
1264 }
1265
1266 void LightStorage::lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) {
1267 LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap);
1268 ERR_FAIL_NULL(li);
1269 li->transform = p_transform;
1270 }
1271
1272 /* SHADOW ATLAS API */
1273
1274 RID LightStorage::shadow_atlas_create() {
1275 return shadow_atlas_owner.make_rid(ShadowAtlas());
1276 }
1277
1278 void LightStorage::shadow_atlas_free(RID p_atlas) {
1279 shadow_atlas_set_size(p_atlas, 0);
1280 shadow_atlas_owner.free(p_atlas);
1281 }
1282
1283 void LightStorage::shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits) {
1284 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
1285 ERR_FAIL_NULL(shadow_atlas);
1286 ERR_FAIL_COND(p_size < 0);
1287 p_size = next_power_of_2((uint32_t)p_size);
1288
1289 if (p_size == shadow_atlas->size && p_16_bits == shadow_atlas->use_16_bits) {
1290 return;
1291 }
1292
1293 for (uint32_t i = 0; i < 4; i++) {
1294 // Clear all subdivisions and free shadows.
1295 for (uint32_t j = 0; j < shadow_atlas->quadrants[i].textures.size(); j++) {
1296 glDeleteTextures(1, &shadow_atlas->quadrants[i].textures[j]);
1297 glDeleteFramebuffers(1, &shadow_atlas->quadrants[i].fbos[j]);
1298 }
1299 shadow_atlas->quadrants[i].textures.clear();
1300 shadow_atlas->quadrants[i].fbos.clear();
1301
1302 shadow_atlas->quadrants[i].shadows.clear();
1303 shadow_atlas->quadrants[i].shadows.resize(shadow_atlas->quadrants[i].subdivision * shadow_atlas->quadrants[i].subdivision);
1304 }
1305
1306 // Erase shadow atlas reference from lights.
1307 for (const KeyValue<RID, uint32_t> &E : shadow_atlas->shadow_owners) {
1308 LightInstance *li = light_instance_owner.get_or_null(E.key);
1309 ERR_CONTINUE(!li);
1310 li->shadow_atlases.erase(p_atlas);
1311 }
1312
1313 if (shadow_atlas->debug_texture != 0) {
1314 glDeleteTextures(1, &shadow_atlas->debug_texture);
1315 }
1316
1317 if (shadow_atlas->debug_fbo != 0) {
1318 glDeleteFramebuffers(1, &shadow_atlas->debug_fbo);
1319 }
1320
1321 // Clear owners.
1322 shadow_atlas->shadow_owners.clear();
1323
1324 shadow_atlas->size = p_size;
1325 shadow_atlas->use_16_bits = p_16_bits;
1326 }
1327
1328 void LightStorage::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
1329 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
1330 ERR_FAIL_NULL(shadow_atlas);
1331 ERR_FAIL_INDEX(p_quadrant, 4);
1332 ERR_FAIL_INDEX(p_subdivision, 16384);
1333
1334 uint32_t subdiv = next_power_of_2((uint32_t)p_subdivision);
1335 if (subdiv & 0xaaaaaaaa) { // sqrt(subdiv) must be integer.
1336 subdiv <<= 1;
1337 }
1338
1339 subdiv = int(Math::sqrt((float)subdiv));
1340
1341 if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
1342 return;
1343 }
1344
1345 // Erase all data from quadrant.
1346 for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
1347 if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
1348 shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
1349 LightInstance *li = light_instance_owner.get_or_null(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
1350 ERR_CONTINUE(!li);
1351 li->shadow_atlases.erase(p_atlas);
1352 }
1353 }
1354
1355 for (uint32_t j = 0; j < shadow_atlas->quadrants[p_quadrant].textures.size(); j++) {
1356 glDeleteTextures(1, &shadow_atlas->quadrants[p_quadrant].textures[j]);
1357 glDeleteFramebuffers(1, &shadow_atlas->quadrants[p_quadrant].fbos[j]);
1358 }
1359
1360 shadow_atlas->quadrants[p_quadrant].textures.clear();
1361 shadow_atlas->quadrants[p_quadrant].fbos.clear();
1362
1363 shadow_atlas->quadrants[p_quadrant].shadows.clear();
1364 shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
1365 shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
1366
1367 // Cache the smallest subdiv (for faster allocation in light update).
1368
1369 shadow_atlas->smallest_subdiv = 1 << 30;
1370
1371 for (int i = 0; i < 4; i++) {
1372 if (shadow_atlas->quadrants[i].subdivision) {
1373 shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
1374 }
1375 }
1376
1377 if (shadow_atlas->smallest_subdiv == 1 << 30) {
1378 shadow_atlas->smallest_subdiv = 0;
1379 }
1380
1381 // Re-sort the size orders, simple bubblesort for 4 elements.
1382
1383 int swaps = 0;
1384 do {
1385 swaps = 0;
1386
1387 for (int i = 0; i < 3; i++) {
1388 if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
1389 SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
1390 swaps++;
1391 }
1392 }
1393 } while (swaps > 0);
1394 }
1395
1396 bool LightStorage::shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) {
1397 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
1398 ERR_FAIL_NULL_V(shadow_atlas, false);
1399
1400 LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
1401 ERR_FAIL_NULL_V(li, false);
1402
1403 if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
1404 return false;
1405 }
1406
1407 uint32_t quad_size = shadow_atlas->size >> 1;
1408 int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(uint32_t(quad_size * p_coverage)));
1409
1410 int valid_quadrants[4];
1411 int valid_quadrant_count = 0;
1412 int best_size = -1; // Best size found.
1413 int best_subdiv = -1; // Subdiv for the best size.
1414
1415 // Find the quadrants this fits into, and the best possible size it can fit into.
1416 for (int i = 0; i < 4; i++) {
1417 int q = shadow_atlas->size_order[i];
1418 int sd = shadow_atlas->quadrants[q].subdivision;
1419 if (sd == 0) {
1420 continue; // Unused.
1421 }
1422
1423 int max_fit = quad_size / sd;
1424
1425 if (best_size != -1 && max_fit > best_size) {
1426 break; // Too large.
1427 }
1428
1429 valid_quadrants[valid_quadrant_count++] = q;
1430 best_subdiv = sd;
1431
1432 if (max_fit >= desired_fit) {
1433 best_size = max_fit;
1434 }
1435 }
1436
1437 ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
1438
1439 uint64_t tick = OS::get_singleton()->get_ticks_msec();
1440
1441 uint32_t old_key = SHADOW_INVALID;
1442 uint32_t old_quadrant = SHADOW_INVALID;
1443 uint32_t old_shadow = SHADOW_INVALID;
1444 int old_subdivision = -1;
1445
1446 bool should_realloc = false;
1447 bool should_redraw = false;
1448
1449 if (shadow_atlas->shadow_owners.has(p_light_instance)) {
1450 old_key = shadow_atlas->shadow_owners[p_light_instance];
1451 old_quadrant = (old_key >> QUADRANT_SHIFT) & 0x3;
1452 old_shadow = old_key & SHADOW_INDEX_MASK;
1453
1454 // Only re-allocate if a better option is available, and enough time has passed.
1455 should_realloc = shadow_atlas->quadrants[old_quadrant].subdivision != (uint32_t)best_subdiv && (tick - shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].alloc_tick > shadow_atlas_realloc_tolerance_msec);
1456 should_redraw = shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].version != p_light_version;
1457
1458 if (!should_realloc) {
1459 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = p_light_version;
1460 // Already existing, see if it should redraw or it's just OK.
1461 return should_redraw;
1462 }
1463
1464 old_subdivision = shadow_atlas->quadrants[old_quadrant].subdivision;
1465 }
1466
1467 bool is_omni = li->light_type == RS::LIGHT_OMNI;
1468 bool found_shadow = false;
1469 int new_quadrant = -1;
1470 int new_shadow = -1;
1471
1472 found_shadow = _shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, is_omni, new_quadrant, new_shadow);
1473
1474 // For new shadows if we found an atlas.
1475 // Or for existing shadows that found a better atlas.
1476 if (found_shadow) {
1477 if (old_quadrant != SHADOW_INVALID) {
1478 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = 0;
1479 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].owner = RID();
1480 }
1481
1482 uint32_t new_key = new_quadrant << QUADRANT_SHIFT;
1483 new_key |= new_shadow;
1484
1485 ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
1486 _shadow_atlas_invalidate_shadow(sh, p_atlas, shadow_atlas, new_quadrant, new_shadow);
1487
1488 sh->owner = p_light_instance;
1489 sh->owner_is_omni = is_omni;
1490 sh->alloc_tick = tick;
1491 sh->version = p_light_version;
1492
1493 li->shadow_atlases.insert(p_atlas);
1494
1495 // Update it in map.
1496 shadow_atlas->shadow_owners[p_light_instance] = new_key;
1497 // Make it dirty, as it should redraw anyway.
1498 return true;
1499 }
1500
1501 return should_redraw;
1502 }
1503
1504 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) {
1505 for (int i = p_quadrant_count - 1; i >= 0; i--) {
1506 int qidx = p_in_quadrants[i];
1507
1508 if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
1509 return false;
1510 }
1511
1512 // Look for an empty space.
1513 int sc = shadow_atlas->quadrants[qidx].shadows.size();
1514 const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
1515
1516 // We have a free space in this quadrant, allocate a texture and use it.
1517 if (sc > (int)shadow_atlas->quadrants[qidx].textures.size()) {
1518 GLuint fbo_id = 0;
1519 glGenFramebuffers(1, &fbo_id);
1520 glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);
1521
1522 GLuint texture_id = 0;
1523 glGenTextures(1, &texture_id);
1524 glActiveTexture(GL_TEXTURE0);
1525
1526 int size = (shadow_atlas->size >> 1) / shadow_atlas->quadrants[qidx].subdivision;
1527
1528 GLenum format = shadow_atlas->use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
1529 GLenum type = shadow_atlas->use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
1530
1531 if (is_omni) {
1532 glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
1533 for (int id = 0; id < 6; id++) {
1534 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + id, 0, format, size / 2, size / 2, 0, GL_DEPTH_COMPONENT, type, nullptr);
1535 }
1536
1537 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1538 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1539
1540 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1541 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1542 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1543
1544 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1545 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
1546
1547 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X, texture_id, 0);
1548
1549 #ifdef DEBUG_ENABLED
1550 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1551 if (status != GL_FRAMEBUFFER_COMPLETE) {
1552 ERR_PRINT("Could not create omni light shadow framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
1553 }
1554 #endif
1555 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
1556 } else {
1557 glBindTexture(GL_TEXTURE_2D, texture_id);
1558
1559 glTexImage2D(GL_TEXTURE_2D, 0, format, size, size, 0, GL_DEPTH_COMPONENT, type, nullptr);
1560
1561 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1562 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1563 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1565
1566 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1567 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
1568
1569 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0);
1570
1571 glBindTexture(GL_TEXTURE_2D, 0);
1572 }
1573 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
1574
1575 r_quadrant = qidx;
1576 r_shadow = shadow_atlas->quadrants[qidx].textures.size();
1577
1578 shadow_atlas->quadrants[qidx].textures.push_back(texture_id);
1579 shadow_atlas->quadrants[qidx].fbos.push_back(fbo_id);
1580
1581 return true;
1582 }
1583
1584 int found_used_idx = -1; // Found existing one, must steal it.
1585 uint64_t min_pass = 0; // Pass of the existing one, try to use the least recently used one (LRU fashion).
1586
1587 for (int j = 0; j < sc; j++) {
1588 if (sarr[j].owner_is_omni != is_omni) {
1589 // Existing light instance type doesn't match new light instance type skip.
1590 continue;
1591 }
1592
1593 LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
1594 if (!sli) {
1595 // Found a released light instance.
1596 found_used_idx = j;
1597 break;
1598 }
1599
1600 if (sli->last_scene_pass != RasterizerSceneGLES3::get_singleton()->get_scene_pass()) {
1601 // Was just allocated, don't kill it so soon, wait a bit.
1602 if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
1603 continue;
1604 }
1605
1606 if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
1607 found_used_idx = j;
1608 min_pass = sli->last_scene_pass;
1609 }
1610 }
1611 }
1612
1613 if (found_used_idx != -1) {
1614 r_quadrant = qidx;
1615 r_shadow = found_used_idx;
1616
1617 return true;
1618 }
1619 }
1620
1621 return false;
1622 }
1623
1624 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) {
1625 if (p_shadow->owner.is_valid()) {
1626 LightInstance *sli = light_instance_owner.get_or_null(p_shadow->owner);
1627
1628 p_shadow_atlas->shadow_owners.erase(p_shadow->owner);
1629 p_shadow->version = 0;
1630 p_shadow->owner = RID();
1631 sli->shadow_atlases.erase(p_atlas);
1632 }
1633 }
1634
1635 void LightStorage::shadow_atlas_update(RID p_atlas) {
1636 // Do nothing as there is no shadow atlas texture.
1637 }
1638
1639 /* DIRECTIONAL SHADOW */
1640
1641 // Create if necessary and clear.
1642 void LightStorage::update_directional_shadow_atlas() {
1643 if (directional_shadow.depth == 0 && directional_shadow.size > 0) {
1644 glGenFramebuffers(1, &directional_shadow.fbo);
1645 glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
1646
1647 glGenTextures(1, &directional_shadow.depth);
1648 glActiveTexture(GL_TEXTURE0);
1649 glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
1650
1651 GLenum format = directional_shadow.use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
1652 GLenum type = directional_shadow.use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
1653
1654 glTexImage2D(GL_TEXTURE_2D, 0, format, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, type, nullptr);
1655
1656 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1657 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1658 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1659 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1660
1661 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1662 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
1663
1664 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
1665 }
1666 glUseProgram(0);
1667 glDepthMask(GL_TRUE);
1668 glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
1669 RasterizerGLES3::clear_depth(0.0);
1670 glClear(GL_DEPTH_BUFFER_BIT);
1671
1672 glBindTexture(GL_TEXTURE_2D, 0);
1673 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
1674 }
1675
1676 void LightStorage::directional_shadow_atlas_set_size(int p_size, bool p_16_bits) {
1677 p_size = nearest_power_of_2_templated(p_size);
1678
1679 if (directional_shadow.size == p_size && directional_shadow.use_16_bits == p_16_bits) {
1680 return;
1681 }
1682
1683 directional_shadow.size = p_size;
1684 directional_shadow.use_16_bits = p_16_bits;
1685
1686 if (directional_shadow.depth != 0) {
1687 glDeleteTextures(1, &directional_shadow.depth);
1688 directional_shadow.depth = 0;
1689 glDeleteFramebuffers(1, &directional_shadow.fbo);
1690 directional_shadow.fbo = 0;
1691 }
1692 }
1693
1694 void LightStorage::set_directional_shadow_count(int p_count) {
1695 directional_shadow.light_count = p_count;
1696 directional_shadow.current_light = 0;
1697 }
1698
1699 static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
1700 int split_h = 1;
1701 int split_v = 1;
1702
1703 while (split_h * split_v < p_shadow_count) {
1704 if (split_h == split_v) {
1705 split_h <<= 1;
1706 } else {
1707 split_v <<= 1;
1708 }
1709 }
1710
1711 Rect2i rect(0, 0, p_size, p_size);
1712 rect.size.width /= split_h;
1713 rect.size.height /= split_v;
1714
1715 rect.position.x = rect.size.width * (p_shadow_index % split_h);
1716 rect.position.y = rect.size.height * (p_shadow_index / split_h);
1717
1718 return rect;
1719 }
1720
1721 Rect2i LightStorage::get_directional_shadow_rect() {
1722 return _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
1723 }
1724
1725 int LightStorage::get_directional_light_shadow_size(RID p_light_instance) {
1726 ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
1727
1728 Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
1729
1730 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
1731 ERR_FAIL_NULL_V(light_instance, 0);
1732
1733 switch (light_directional_get_shadow_mode(light_instance->light)) {
1734 case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
1735 break; //none
1736 case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
1737 r.size.height /= 2;
1738 break;
1739 case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
1740 r.size /= 2;
1741 break;
1742 }
1743
1744 return MAX(r.size.width, r.size.height);
1745 }
1746
1747 #endif // !GLES3_ENABLED