Debian Codegraph @

servers/rendering/renderer_rd/storage_rd/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 #include "light_storage.h"
32 #include "core/config/project_settings.h"
33 #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
34 #include "texture_storage.h"
35
36 using namespace RendererRD;
37
38 LightStorage *LightStorage::singleton = nullptr;
39
40 LightStorage *LightStorage::get_singleton() {
41 return singleton;
42 }
43
44 LightStorage::LightStorage() {
45 singleton = this;
46
47 TextureStorage *texture_storage = TextureStorage::get_singleton();
48
49 directional_shadow.size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
50 directional_shadow.use_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
51
52 using_lightmap_array = true; // high end
53 if (using_lightmap_array) {
54 uint64_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
55
56 if (textures_per_stage <= 256) {
57 lightmap_textures.resize(32);
58 shadowmask_textures.resize(32);
59 } else {
60 lightmap_textures.resize(1024);
61 shadowmask_textures.resize(1024);
62 }
63
64 for (RID &lightmap_texture : lightmap_textures) {
65 lightmap_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
66 }
67
68 for (RID &shadowmask_texture : shadowmask_textures) {
69 shadowmask_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
70 }
71 }
72
73 lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
74 }
75
76 LightStorage::~LightStorage() {
77 free_reflection_data();
78 free_light_data();
79
80 for (const KeyValue<int, ShadowCubemap> &E : shadow_cubemaps) {
81 RD::get_singleton()->free_rid(E.value.cubemap);
82 }
83
84 singleton = nullptr;
85 }
86
87 bool LightStorage::free(RID p_rid) {
88 if (owns_reflection_probe(p_rid)) {
89 reflection_probe_free(p_rid);
90 return true;
91 } else if (owns_reflection_atlas(p_rid)) {
92 reflection_atlas_free(p_rid);
93 return true;
94 } else if (owns_reflection_probe_instance(p_rid)) {
95 reflection_probe_instance_free(p_rid);
96 return true;
97 } else if (owns_light(p_rid)) {
98 light_free(p_rid);
99 return true;
100 } else if (owns_light_instance(p_rid)) {
101 light_instance_free(p_rid);
102 return true;
103 } else if (owns_lightmap(p_rid)) {
104 lightmap_free(p_rid);
105 return true;
106 } else if (owns_lightmap_instance(p_rid)) {
107 lightmap_instance_free(p_rid);
108 return true;
109 } else if (owns_shadow_atlas(p_rid)) {
110 shadow_atlas_free(p_rid);
111 return true;
112 }
113
114 return false;
115 }
116
117 /* LIGHT */
118
119 void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
120 Light light;
121 light.type = p_type;
122
123 light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
124 light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
125 light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
126 light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
127 light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
128 light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
129 light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
130 light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
131 light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
132 light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
133 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
134 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
135 light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
136 light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
137 light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
138 light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
139 light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
140 light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
141 light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
142 light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
143 light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
144
145 light_owner.initialize_rid(p_light, light);
146 }
147
148 RID LightStorage::directional_light_allocate() {
149 return light_owner.allocate_rid();
150 }
151
152 void LightStorage::directional_light_initialize(RID p_light) {
153 _light_initialize(p_light, RS::LIGHT_DIRECTIONAL);
154 }
155
156 RID LightStorage::omni_light_allocate() {
157 return light_owner.allocate_rid();
158 }
159
160 void LightStorage::omni_light_initialize(RID p_light) {
161 _light_initialize(p_light, RS::LIGHT_OMNI);
162 }
163
164 RID LightStorage::spot_light_allocate() {
165 return light_owner.allocate_rid();
166 }
167
168 void LightStorage::spot_light_initialize(RID p_light) {
169 _light_initialize(p_light, RS::LIGHT_SPOT);
170 }
171
172 void LightStorage::light_free(RID p_rid) {
173 light_set_projector(p_rid, RID()); //clear projector
174
175 // delete the texture
176 Light *light = light_owner.get_or_null(p_rid);
177 light->dependency.deleted_notify(p_rid);
178 light_owner.free(p_rid);
179 }
180
181 void LightStorage::light_set_color(RID p_light, const Color &p_color) {
182 Light *light = light_owner.get_or_null(p_light);
183 ERR_FAIL_NULL(light);
184
185 light->color = p_color;
186 }
187
188 void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
189 Light *light = light_owner.get_or_null(p_light);
190 ERR_FAIL_NULL(light);
191 ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
192
193 if (light->param[p_param] == p_value) {
194 return;
195 }
196
197 switch (p_param) {
198 case RS::LIGHT_PARAM_RANGE:
199 case RS::LIGHT_PARAM_SPOT_ANGLE:
200 case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
201 case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
202 case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
203 case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
204 case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
205 case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
206 case RS::LIGHT_PARAM_SHADOW_BIAS: {
207 light->version++;
208 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
209 } break;
210 case RS::LIGHT_PARAM_SIZE: {
211 if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
212 //changing from no size to size and the opposite
213 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
214 }
215 } break;
216 default: {
217 }
218 }
219
220 light->param[p_param] = p_value;
221 }
222
223 void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
224 Light *light = light_owner.get_or_null(p_light);
225 ERR_FAIL_NULL(light);
226 light->shadow = p_enabled;
227
228 light->version++;
229 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
230 }
231
232 void LightStorage::light_set_projector(RID p_light, RID p_texture) {
233 TextureStorage *texture_storage = TextureStorage::get_singleton();
234 Light *light = light_owner.get_or_null(p_light);
235 ERR_FAIL_NULL(light);
236
237 if (light->projector == p_texture) {
238 return;
239 }
240
241 ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture));
242
243 if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
244 texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
245 }
246
247 light->projector = p_texture;
248
249 if (light->type != RS::LIGHT_DIRECTIONAL) {
250 if (light->projector.is_valid()) {
251 texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
252 }
253 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
254 }
255 }
256
257 void LightStorage::light_set_negative(RID p_light, bool p_enable) {
258 Light *light = light_owner.get_or_null(p_light);
259 ERR_FAIL_NULL(light);
260
261 light->negative = p_enable;
262 }
263
264 void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
265 Light *light = light_owner.get_or_null(p_light);
266 ERR_FAIL_NULL(light);
267
268 light->cull_mask = p_mask;
269
270 light->version++;
271 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_CULL_MASK);
272 }
273
274 void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
275 Light *light = light_owner.get_or_null(p_light);
276 ERR_FAIL_NULL(light);
277
278 light->distance_fade = p_enabled;
279 light->distance_fade_begin = p_begin;
280 light->distance_fade_shadow = p_shadow;
281 light->distance_fade_length = p_length;
282 }
283
284 void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
285 Light *light = light_owner.get_or_null(p_light);
286 ERR_FAIL_NULL(light);
287
288 light->reverse_cull = p_enabled;
289
290 light->version++;
291 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
292 }
293
294 void LightStorage::light_set_shadow_caster_mask(RID p_light, uint32_t p_caster_mask) {
295 Light *light = light_owner.get_or_null(p_light);
296 ERR_FAIL_NULL(light);
297
298 light->shadow_caster_mask = p_caster_mask;
299
300 light->version++;
301 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
302 }
303
304 uint32_t LightStorage::light_get_shadow_caster_mask(RID p_light) const {
305 Light *light = light_owner.get_or_null(p_light);
306 ERR_FAIL_NULL_V(light, 0);
307
308 return light->shadow_caster_mask;
309 }
310
311 void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
312 Light *light = light_owner.get_or_null(p_light);
313 ERR_FAIL_NULL(light);
314
315 light->bake_mode = p_bake_mode;
316
317 light->version++;
318 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
319 }
320
321 void LightStorage::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
322 Light *light = light_owner.get_or_null(p_light);
323 ERR_FAIL_NULL(light);
324
325 light->max_sdfgi_cascade = p_cascade;
326
327 light->version++;
328 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
329 }
330
331 void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
332 Light *light = light_owner.get_or_null(p_light);
333 ERR_FAIL_NULL(light);
334
335 light->omni_shadow_mode = p_mode;
336
337 light->version++;
338 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
339
340 if (p_mode == RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID) {
341 shadow_dual_paraboloid_used = true;
342 } else if (p_mode == RS::LIGHT_OMNI_SHADOW_CUBE) {
343 shadow_cubemaps_used = true;
344 }
345 }
346
347 RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
348 const Light *light = light_owner.get_or_null(p_light);
349 ERR_FAIL_NULL_V(light, RS::LIGHT_OMNI_SHADOW_CUBE);
350
351 return light->omni_shadow_mode;
352 }
353
354 void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
355 Light *light = light_owner.get_or_null(p_light);
356 ERR_FAIL_NULL(light);
357
358 light->directional_shadow_mode = p_mode;
359 light->version++;
360 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
361 }
362
363 void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
364 Light *light = light_owner.get_or_null(p_light);
365 ERR_FAIL_NULL(light);
366
367 light->directional_blend_splits = p_enable;
368 light->version++;
369 light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
370 }
371
372 bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
373 const Light *light = light_owner.get_or_null(p_light);
374 ERR_FAIL_NULL_V(light, false);
375
376 return light->directional_blend_splits;
377 }
378
379 void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
380 Light *light = light_owner.get_or_null(p_light);
381 ERR_FAIL_NULL(light);
382
383 light->directional_sky_mode = p_mode;
384 }
385
386 RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
387 const Light *light = light_owner.get_or_null(p_light);
388 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
389
390 return light->directional_sky_mode;
391 }
392
393 RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
394 const Light *light = light_owner.get_or_null(p_light);
395 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
396
397 return light->directional_shadow_mode;
398 }
399
400 uint32_t LightStorage::light_get_max_sdfgi_cascade(RID p_light) {
401 const Light *light = light_owner.get_or_null(p_light);
402 ERR_FAIL_NULL_V(light, 0);
403
404 return light->max_sdfgi_cascade;
405 }
406
407 RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
408 const Light *light = light_owner.get_or_null(p_light);
409 ERR_FAIL_NULL_V(light, RS::LIGHT_BAKE_DISABLED);
410
411 return light->bake_mode;
412 }
413
414 uint64_t LightStorage::light_get_version(RID p_light) const {
415 const Light *light = light_owner.get_or_null(p_light);
416 ERR_FAIL_NULL_V(light, 0);
417
418 return light->version;
419 }
420
421 uint32_t LightStorage::light_get_cull_mask(RID p_light) const {
422 const Light *light = light_owner.get_or_null(p_light);
423 ERR_FAIL_NULL_V(light, 0);
424
425 return light->cull_mask;
426 }
427
428 AABB LightStorage::light_get_aabb(RID p_light) const {
429 const Light *light = light_owner.get_or_null(p_light);
430 ERR_FAIL_NULL_V(light, AABB());
431
432 switch (light->type) {
433 case RS::LIGHT_SPOT: {
434 float len = light->param[RS::LIGHT_PARAM_RANGE];
435 float angle = Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE]);
436
437 if (angle > Math::PI * 0.5) {
438 // Light casts backwards as well.
439 return AABB(Vector3(-1, -1, -1) * len, Vector3(2, 2, 2) * len);
440 }
441
442 float size = Math::sin(angle) * len;
443 return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
444 };
445 case RS::LIGHT_OMNI: {
446 float r = light->param[RS::LIGHT_PARAM_RANGE];
447 return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
448 };
449 case RS::LIGHT_DIRECTIONAL: {
450 return AABB();
451 };
452 }
453
454 ERR_FAIL_V(AABB());
455 }
456
457 Dependency *LightStorage::light_get_dependency(RID p_light) const {
458 Light *light = light_owner.get_or_null(p_light);
459 ERR_FAIL_NULL_V(light, nullptr);
460
461 return &light->dependency;
462 }
463
464 /* LIGHT INSTANCE API */
465
466 RID LightStorage::light_instance_create(RID p_light) {
467 RID li = light_instance_owner.make_rid(LightInstance());
468
469 LightInstance *light_instance = light_instance_owner.get_or_null(li);
470
471 light_instance->self = li;
472 light_instance->light = p_light;
473 light_instance->light_type = light_get_type(p_light);
474 if (light_instance->light_type != RS::LIGHT_DIRECTIONAL) {
475 light_instance->forward_id = ForwardIDStorage::get_singleton()->allocate_forward_id(light_instance->light_type == RS::LIGHT_OMNI ? FORWARD_ID_TYPE_OMNI_LIGHT : FORWARD_ID_TYPE_SPOT_LIGHT);
476 }
477
478 return li;
479 }
480
481 void LightStorage::light_instance_free(RID p_light) {
482 LightInstance *light_instance = light_instance_owner.get_or_null(p_light);
483
484 //remove from shadow atlases..
485 for (const RID &E : light_instance->shadow_atlases) {
486 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(E);
487 ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_light));
488 uint32_t key = shadow_atlas->shadow_owners[p_light];
489 uint32_t q = (key >> QUADRANT_SHIFT) & 0x3;
490 uint32_t s = key & SHADOW_INDEX_MASK;
491
492 shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
493
494 if (key & OMNI_LIGHT_FLAG) {
495 // Omni lights use two atlas spots, make sure to clear the other as well
496 shadow_atlas->quadrants[q].shadows.write[s + 1].owner = RID();
497 }
498
499 shadow_atlas->shadow_owners.erase(p_light);
500 }
501
502 if (light_instance->light_type != RS::LIGHT_DIRECTIONAL) {
503 ForwardIDStorage::get_singleton()->free_forward_id(light_instance->light_type == RS::LIGHT_OMNI ? FORWARD_ID_TYPE_OMNI_LIGHT : FORWARD_ID_TYPE_SPOT_LIGHT, light_instance->forward_id);
504 }
505 light_instance_owner.free(p_light);
506 }
507
508 void LightStorage::light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) {
509 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
510 ERR_FAIL_NULL(light_instance);
511
512 light_instance->transform = p_transform;
513 }
514
515 void LightStorage::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
516 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
517 ERR_FAIL_NULL(light_instance);
518
519 light_instance->aabb = p_aabb;
520 }
521
522 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) {
523 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
524 ERR_FAIL_NULL(light_instance);
525
526 ERR_FAIL_INDEX(p_pass, 6);
527
528 light_instance->shadow_transform[p_pass].camera = p_projection;
529 light_instance->shadow_transform[p_pass].transform = p_transform;
530 light_instance->shadow_transform[p_pass].farplane = p_far;
531 light_instance->shadow_transform[p_pass].split = p_split;
532 light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
533 light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
534 light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
535 light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
536 }
537
538 void LightStorage::light_instance_mark_visible(RID p_light_instance) {
539 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
540 ERR_FAIL_NULL(light_instance);
541
542 light_instance->last_scene_pass = RendererSceneRenderRD::get_singleton()->get_scene_pass();
543 }
544
545 /* LIGHT DATA */
546
547 void LightStorage::free_light_data() {
548 if (directional_light_buffer.is_valid()) {
549 RD::get_singleton()->free_rid(directional_light_buffer);
550 directional_light_buffer = RID();
551 }
552
553 if (omni_light_buffer.is_valid()) {
554 RD::get_singleton()->free_rid(omni_light_buffer);
555 omni_light_buffer = RID();
556 }
557
558 if (spot_light_buffer.is_valid()) {
559 RD::get_singleton()->free_rid(spot_light_buffer);
560 spot_light_buffer = RID();
561 }
562
563 if (directional_lights != nullptr) {
564 memdelete_arr(directional_lights);
565 directional_lights = nullptr;
566 }
567
568 if (omni_lights != nullptr) {
569 memdelete_arr(omni_lights);
570 omni_lights = nullptr;
571 }
572
573 if (spot_lights != nullptr) {
574 memdelete_arr(spot_lights);
575 spot_lights = nullptr;
576 }
577
578 if (omni_light_sort != nullptr) {
579 memdelete_arr(omni_light_sort);
580 omni_light_sort = nullptr;
581 }
582
583 if (spot_light_sort != nullptr) {
584 memdelete_arr(spot_light_sort);
585 spot_light_sort = nullptr;
586 }
587 }
588
589 void LightStorage::set_max_lights(const uint32_t p_max_lights) {
590 max_lights = p_max_lights;
591
592 uint32_t light_buffer_size = max_lights * sizeof(LightData);
593 omni_lights = memnew_arr(LightData, max_lights);
594 omni_light_buffer = RD::get_singleton()->storage_buffer_create(light_buffer_size);
595 omni_light_sort = memnew_arr(LightInstanceDepthSort, max_lights);
596 spot_lights = memnew_arr(LightData, max_lights);
597 spot_light_buffer = RD::get_singleton()->storage_buffer_create(light_buffer_size);
598 spot_light_sort = memnew_arr(LightInstanceDepthSort, max_lights);
599 //defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(max_lights) + "\n";
600
601 max_directional_lights = RendererSceneRender::MAX_DIRECTIONAL_LIGHTS;
602 uint32_t directional_light_buffer_size = max_directional_lights * sizeof(DirectionalLightData);
603 directional_lights = memnew_arr(DirectionalLightData, max_directional_lights);
604 directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
605 }
606
607 void LightStorage::update_light_buffers(RenderDataRD *p_render_data, const PagedArray<RID> &p_lights, const Transform3D &p_camera_transform, RID p_shadow_atlas, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_positional_light_count, bool &r_directional_light_soft_shadows) {
608 ForwardIDStorage *forward_id_storage = ForwardIDStorage::get_singleton();
609 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
610
611 Transform3D inverse_transform = p_camera_transform.affine_inverse();
612
613 r_directional_light_count = 0;
614 r_positional_light_count = 0;
615
616 omni_light_count = 0;
617 spot_light_count = 0;
618
619 r_directional_light_soft_shadows = false;
620
621 for (int i = 0; i < (int)p_lights.size(); i++) {
622 LightInstance *light_instance = light_instance_owner.get_or_null(p_lights[i]);
623 if (!light_instance) {
624 continue;
625 }
626 Light *light = light_owner.get_or_null(light_instance->light);
627
628 ERR_CONTINUE(light == nullptr);
629
630 switch (light->type) {
631 case RS::LIGHT_DIRECTIONAL: {
632 if (r_directional_light_count >= max_directional_lights || light->directional_sky_mode == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
633 continue;
634 }
635
636 DirectionalLightData &light_data = directional_lights[r_directional_light_count];
637
638 Transform3D light_transform = light_instance->transform;
639
640 Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
641
642 light_data.direction[0] = direction.x;
643 light_data.direction[1] = direction.y;
644 light_data.direction[2] = direction.z;
645
646 float sign = light->negative ? -1 : 1;
647
648 light_data.energy = sign * light->param[RS::LIGHT_PARAM_ENERGY];
649
650 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
651 light_data.energy *= light->param[RS::LIGHT_PARAM_INTENSITY];
652 } else {
653 light_data.energy *= Math::PI;
654 }
655
656 if (p_render_data->camera_attributes.is_valid()) {
657 light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
658 }
659
660 Color linear_col = light->color.srgb_to_linear();
661 light_data.color[0] = linear_col.r;
662 light_data.color[1] = linear_col.g;
663 light_data.color[2] = linear_col.b;
664
665 light_data.specular = light->param[RS::LIGHT_PARAM_SPECULAR];
666 light_data.volumetric_fog_energy = light->param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY];
667 light_data.mask = light->cull_mask;
668
669 float size = light->param[RS::LIGHT_PARAM_SIZE];
670
671 light_data.size = 1.0 - Math::cos(Math::deg_to_rad(size)); //angle to cosine offset
672
673 light_data.shadow_opacity = (p_using_shadows && light->shadow)
674 ? light->param[RS::LIGHT_PARAM_SHADOW_OPACITY]
675 : 0.0;
676
677 float angular_diameter = light->param[RS::LIGHT_PARAM_SIZE];
678 if (angular_diameter > 0.0) {
679 // I know tan(0) is 0, but let's not risk it with numerical precision.
680 // technically this will keep expanding until reaching the sun, but all we care
681 // is expand until we reach the radius of the near plane (there can't be more occluders than that)
682 angular_diameter = Math::tan(Math::deg_to_rad(angular_diameter));
683 if (light->shadow && light->param[RS::LIGHT_PARAM_SHADOW_BLUR] > 0.0) {
684 // Only enable PCSS-like soft shadows if blurring is enabled.
685 // Otherwise, performance would decrease with no visual difference.
686 r_directional_light_soft_shadows = true;
687 }
688 } else {
689 angular_diameter = 0.0;
690 }
691
692 light_data.bake_mode = light->bake_mode;
693
694 if (light_data.shadow_opacity > 0.001) {
695 RS::LightDirectionalShadowMode smode = light->directional_shadow_mode;
696
697 light_data.soft_shadow_scale = light->param[RS::LIGHT_PARAM_SHADOW_BLUR];
698 light_data.softshadow_angle = angular_diameter;
699
700 if (angular_diameter <= 0.0) {
701 light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->directional_shadow_quality_radius_get(); // Only use quality radius for PCF
702 }
703
704 int limit = smode == RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ? 0 : (smode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS ? 1 : 3);
705 light_data.blend_splits = (smode != RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL) && light->directional_blend_splits;
706 for (int j = 0; j < 4; j++) {
707 Rect2 atlas_rect = light_instance->shadow_transform[j].atlas_rect;
708 Projection correction;
709 correction.set_depth_correction(false, true, false);
710 Projection matrix = correction * light_instance->shadow_transform[j].camera;
711 float split = light_instance->shadow_transform[MIN(limit, j)].split;
712
713 Projection bias;
714 bias.set_light_bias();
715 Projection rectm;
716 rectm.set_light_atlas_rect(atlas_rect);
717
718 Transform3D modelview = (inverse_transform * light_instance->shadow_transform[j].transform).inverse();
719
720 Projection shadow_mtx = rectm * bias * matrix * modelview;
721 light_data.shadow_split_offsets[j] = split;
722 float bias_scale = light_instance->shadow_transform[j].bias_scale * light_data.soft_shadow_scale;
723 light_data.shadow_bias[j] = light->param[RS::LIGHT_PARAM_SHADOW_BIAS] / 100.0 * bias_scale;
724 light_data.shadow_normal_bias[j] = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * light_instance->shadow_transform[j].shadow_texel_size;
725 light_data.shadow_transmittance_bias[j] = light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] / 100.0 * bias_scale;
726 light_data.shadow_z_range[j] = light_instance->shadow_transform[j].farplane;
727 light_data.shadow_range_begin[j] = light_instance->shadow_transform[j].range_begin;
728 RendererRD::MaterialStorage::store_camera(shadow_mtx, light_data.shadow_matrices[j]);
729
730 Vector2 uv_scale = light_instance->shadow_transform[j].uv_scale;
731 uv_scale *= atlas_rect.size; //adapt to atlas size
732 switch (j) {
733 case 0: {
734 light_data.uv_scale1[0] = uv_scale.x;
735 light_data.uv_scale1[1] = uv_scale.y;
736 } break;
737 case 1: {
738 light_data.uv_scale2[0] = uv_scale.x;
739 light_data.uv_scale2[1] = uv_scale.y;
740 } break;
741 case 2: {
742 light_data.uv_scale3[0] = uv_scale.x;
743 light_data.uv_scale3[1] = uv_scale.y;
744 } break;
745 case 3: {
746 light_data.uv_scale4[0] = uv_scale.x;
747 light_data.uv_scale4[1] = uv_scale.y;
748 } break;
749 }
750 }
751
752 float fade_start = light->param[RS::LIGHT_PARAM_SHADOW_FADE_START];
753 light_data.fade_from = -light_data.shadow_split_offsets[3] * MIN(fade_start, 0.999); //using 1.0 would break smoothstep
754 light_data.fade_to = -light_data.shadow_split_offsets[3];
755 }
756
757 r_directional_light_count++;
758 } break;
759 case RS::LIGHT_OMNI: {
760 if (omni_light_count >= max_lights) {
761 continue;
762 }
763
764 Transform3D light_transform = light_instance->transform;
765 const real_t distance = p_camera_transform.origin.distance_to(light_transform.origin);
766
767 if (light->distance_fade) {
768 const float fade_begin = light->distance_fade_begin;
769 const float fade_length = light->distance_fade_length;
770
771 if (distance > fade_begin) {
772 if (distance > fade_begin + fade_length) {
773 // Out of range, don't draw this light to improve performance.
774 continue;
775 }
776 }
777 }
778
779 omni_light_sort[omni_light_count].light_instance = light_instance;
780 omni_light_sort[omni_light_count].light = light;
781 omni_light_sort[omni_light_count].depth = distance;
782 omni_light_count++;
783 } break;
784 case RS::LIGHT_SPOT: {
785 if (spot_light_count >= max_lights) {
786 continue;
787 }
788
789 Transform3D light_transform = light_instance->transform;
790 const real_t distance = p_camera_transform.origin.distance_to(light_transform.origin);
791
792 if (light->distance_fade) {
793 const float fade_begin = light->distance_fade_begin;
794 const float fade_length = light->distance_fade_length;
795
796 if (distance > fade_begin) {
797 if (distance > fade_begin + fade_length) {
798 // Out of range, don't draw this light to improve performance.
799 continue;
800 }
801 }
802 }
803
804 spot_light_sort[spot_light_count].light_instance = light_instance;
805 spot_light_sort[spot_light_count].light = light;
806 spot_light_sort[spot_light_count].depth = distance;
807 spot_light_count++;
808 } break;
809 }
810
811 light_instance->last_pass = RSG::rasterizer->get_frame_number();
812 }
813
814 if (omni_light_count) {
815 SortArray<LightInstanceDepthSort> sorter;
816 sorter.sort(omni_light_sort, omni_light_count);
817 }
818
819 if (spot_light_count) {
820 SortArray<LightInstanceDepthSort> sorter;
821 sorter.sort(spot_light_sort, spot_light_count);
822 }
823
824 bool using_forward_ids = forward_id_storage->uses_forward_ids();
825
826 for (uint32_t i = 0; i < (omni_light_count + spot_light_count); i++) {
827 uint32_t index = (i < omni_light_count) ? i : i - (omni_light_count);
828 LightData &light_data = (i < omni_light_count) ? omni_lights[index] : spot_lights[index];
829 RS::LightType type = (i < omni_light_count) ? RS::LIGHT_OMNI : RS::LIGHT_SPOT;
830 LightInstance *light_instance = (i < omni_light_count) ? omni_light_sort[index].light_instance : spot_light_sort[index].light_instance;
831 Light *light = (i < omni_light_count) ? omni_light_sort[index].light : spot_light_sort[index].light;
832 real_t distance = (i < omni_light_count) ? omni_light_sort[index].depth : spot_light_sort[index].depth;
833
834 if (using_forward_ids) {
835 forward_id_storage->map_forward_id(type == RS::LIGHT_OMNI ? RendererRD::FORWARD_ID_TYPE_OMNI_LIGHT : RendererRD::FORWARD_ID_TYPE_SPOT_LIGHT, light_instance->forward_id, index, light_instance->last_pass);
836 }
837
838 Transform3D light_transform = light_instance->transform;
839
840 float sign = light->negative ? -1 : 1;
841 Color linear_col = light->color.srgb_to_linear();
842
843 light_data.attenuation = light->param[RS::LIGHT_PARAM_ATTENUATION];
844
845 // Reuse fade begin, fade length and distance for shadow LOD determination later.
846 float fade_begin = 0.0;
847 float fade_shadow = 0.0;
848 float fade_length = 0.0;
849
850 float fade = 1.0;
851 float shadow_opacity_fade = 1.0;
852 if (light->distance_fade) {
853 fade_begin = light->distance_fade_begin;
854 fade_shadow = light->distance_fade_shadow;
855 fade_length = light->distance_fade_length;
856
857 // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
858 if (distance > fade_begin) {
859 fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
860 }
861
862 if (distance > fade_shadow) {
863 shadow_opacity_fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_shadow) / fade_length);
864 }
865 }
866
867 float energy = sign * light->param[RS::LIGHT_PARAM_ENERGY] * fade;
868
869 if (RendererSceneRenderRD::get_singleton()->is_using_physical_light_units()) {
870 energy *= light->param[RS::LIGHT_PARAM_INTENSITY];
871
872 // Convert from Luminous Power to Luminous Intensity
873 if (type == RS::LIGHT_OMNI) {
874 energy *= 1.0 / (Math::PI * 4.0);
875 } else {
876 // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
877 // We make this assumption to keep them easy to control.
878 energy *= 1.0 / Math::PI;
879 }
880 } else {
881 energy *= Math::PI;
882 }
883
884 if (p_render_data->camera_attributes.is_valid()) {
885 energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
886 }
887
888 light_data.color[0] = linear_col.r * energy;
889 light_data.color[1] = linear_col.g * energy;
890 light_data.color[2] = linear_col.b * energy;
891 light_data.specular_amount = light->param[RS::LIGHT_PARAM_SPECULAR] * 2.0;
892 light_data.volumetric_fog_energy = light->param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY];
893 light_data.bake_mode = light->bake_mode;
894
895 float radius = MAX(0.001, light->param[RS::LIGHT_PARAM_RANGE]);
896 light_data.inv_radius = 1.0 / radius;
897
898 Vector3 pos = inverse_transform.xform(light_transform.origin);
899
900 light_data.position[0] = pos.x;
901 light_data.position[1] = pos.y;
902 light_data.position[2] = pos.z;
903
904 Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
905
906 light_data.direction[0] = direction.x;
907 light_data.direction[1] = direction.y;
908 light_data.direction[2] = direction.z;
909
910 float size = light->param[RS::LIGHT_PARAM_SIZE];
911
912 light_data.size = size;
913
914 light_data.inv_spot_attenuation = 1.0f / light->param[RS::LIGHT_PARAM_SPOT_ATTENUATION];
915 float spot_angle = light->param[RS::LIGHT_PARAM_SPOT_ANGLE];
916 light_data.cos_spot_angle = Math::cos(Math::deg_to_rad(spot_angle));
917
918 light_data.mask = light->cull_mask;
919
920 light_data.atlas_rect[0] = 0;
921 light_data.atlas_rect[1] = 0;
922 light_data.atlas_rect[2] = 0;
923 light_data.atlas_rect[3] = 0;
924
925 RID projector = light->projector;
926
927 if (projector.is_valid()) {
928 Rect2 rect = texture_storage->decal_atlas_get_texture_rect(projector);
929
930 if (type == RS::LIGHT_SPOT) {
931 light_data.projector_rect[0] = rect.position.x;
932 light_data.projector_rect[1] = rect.position.y + rect.size.height; //flip because shadow is flipped
933 light_data.projector_rect[2] = rect.size.width;
934 light_data.projector_rect[3] = -rect.size.height;
935 } else {
936 light_data.projector_rect[0] = rect.position.x;
937 light_data.projector_rect[1] = rect.position.y;
938 light_data.projector_rect[2] = rect.size.width;
939 light_data.projector_rect[3] = rect.size.height * 0.5; //used by dp, so needs to be half
940 }
941 } else {
942 light_data.projector_rect[0] = 0;
943 light_data.projector_rect[1] = 0;
944 light_data.projector_rect[2] = 0;
945 light_data.projector_rect[3] = 0;
946 }
947
948 const bool needs_shadow =
949 p_using_shadows &&
950 owns_shadow_atlas(p_shadow_atlas) &&
951 shadow_atlas_owns_light_instance(p_shadow_atlas, light_instance->self) &&
952 light->shadow;
953
954 bool in_shadow_range = true;
955 if (needs_shadow && light->distance_fade) {
956 if (distance > light->distance_fade_shadow + light->distance_fade_length) {
957 // Out of range, don't draw shadows to improve performance.
958 in_shadow_range = false;
959 }
960 }
961
962 if (needs_shadow && in_shadow_range) {
963 // fill in the shadow information
964
965 light_data.shadow_opacity = light->param[RS::LIGHT_PARAM_SHADOW_OPACITY] * shadow_opacity_fade;
966
967 float shadow_texel_size = light_instance_get_shadow_texel_size(light_instance->self, p_shadow_atlas);
968 light_data.shadow_normal_bias = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * shadow_texel_size * 10.0;
969
970 if (type == RS::LIGHT_SPOT) {
971 light_data.shadow_bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS] / 100.0;
972 } else { //omni
973 light_data.shadow_bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS];
974 }
975
976 light_data.transmittance_bias = light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
977
978 Vector2i omni_offset;
979 Rect2 rect = light_instance_get_shadow_atlas_rect(light_instance->self, p_shadow_atlas, omni_offset);
980
981 light_data.atlas_rect[0] = rect.position.x;
982 light_data.atlas_rect[1] = rect.position.y;
983 light_data.atlas_rect[2] = rect.size.width;
984 light_data.atlas_rect[3] = rect.size.height;
985
986 light_data.soft_shadow_scale = light->param[RS::LIGHT_PARAM_SHADOW_BLUR];
987
988 if (type == RS::LIGHT_OMNI) {
989 Transform3D proj = (inverse_transform * light_transform).inverse();
990
991 RendererRD::MaterialStorage::store_transform(proj, light_data.shadow_matrix);
992
993 if (size > 0.0 && light_data.soft_shadow_scale > 0.0) {
994 // Only enable PCSS-like soft shadows if blurring is enabled.
995 // Otherwise, performance would decrease with no visual difference.
996 light_data.soft_shadow_size = size;
997 } else {
998 light_data.soft_shadow_size = 0.0;
999 light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->shadows_quality_radius_get(); // Only use quality radius for PCF
1000 }
1001
1002 light_data.direction[0] = omni_offset.x * float(rect.size.width);
1003 light_data.direction[1] = omni_offset.y * float(rect.size.height);
1004 } else if (type == RS::LIGHT_SPOT) {
1005 Transform3D modelview = (inverse_transform * light_transform).inverse();
1006 Projection bias;
1007 bias.set_light_bias();
1008
1009 Projection correction;
1010 correction.set_depth_correction(false, true, false);
1011 Projection cm = correction * light_instance->shadow_transform[0].camera;
1012 Projection shadow_mtx = bias * cm * modelview;
1013 RendererRD::MaterialStorage::store_camera(shadow_mtx, light_data.shadow_matrix);
1014
1015 if (size > 0.0 && light_data.soft_shadow_scale > 0.0) {
1016 // Only enable PCSS-like soft shadows if blurring is enabled.
1017 // Otherwise, performance would decrease with no visual difference.
1018 float half_np = cm.get_z_near() * Math::tan(Math::deg_to_rad(spot_angle));
1019 light_data.soft_shadow_size = (size * 0.5 / radius) / (half_np / cm.get_z_near()) * rect.size.width;
1020 } else {
1021 light_data.soft_shadow_size = 0.0;
1022 light_data.soft_shadow_scale *= RendererSceneRenderRD::get_singleton()->shadows_quality_radius_get(); // Only use quality radius for PCF
1023 }
1024 light_data.shadow_bias *= light_data.soft_shadow_scale;
1025 }
1026 } else {
1027 light_data.shadow_opacity = 0.0;
1028 }
1029
1030 light_instance->cull_mask = light->cull_mask;
1031
1032 // hook for subclass to do further processing.
1033 RendererSceneRenderRD::get_singleton()->setup_added_light(type, light_transform, radius, spot_angle);
1034
1035 r_positional_light_count++;
1036 }
1037
1038 //update without barriers
1039 if (omni_light_count) {
1040 RD::get_singleton()->buffer_update(omni_light_buffer, 0, sizeof(LightData) * omni_light_count, omni_lights);
1041 }
1042
1043 if (spot_light_count) {
1044 RD::get_singleton()->buffer_update(spot_light_buffer, 0, sizeof(LightData) * spot_light_count, spot_lights);
1045 }
1046
1047 if (r_directional_light_count) {
1048 RD::get_singleton()->buffer_update(directional_light_buffer, 0, sizeof(DirectionalLightData) * r_directional_light_count, directional_lights);
1049 }
1050 }
1051
1052 /* REFLECTION PROBE */
1053
1054 RID LightStorage::reflection_probe_allocate() {
1055 return reflection_probe_owner.allocate_rid();
1056 }
1057
1058 void LightStorage::reflection_probe_initialize(RID p_reflection_probe) {
1059 reflection_probe_owner.initialize_rid(p_reflection_probe, ReflectionProbe());
1060 }
1061
1062 void LightStorage::reflection_probe_free(RID p_rid) {
1063 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_rid);
1064 reflection_probe->dependency.deleted_notify(p_rid);
1065 reflection_probe_owner.free(p_rid);
1066 }
1067
1068 void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
1069 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1070 ERR_FAIL_NULL(reflection_probe);
1071
1072 reflection_probe->update_mode = p_mode;
1073 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1074 }
1075
1076 void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
1077 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1078 ERR_FAIL_NULL(reflection_probe);
1079
1080 reflection_probe->intensity = p_intensity;
1081 }
1082
1083 void LightStorage::reflection_probe_set_blend_distance(RID p_probe, float p_blend_distance) {
1084 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1085 ERR_FAIL_NULL(reflection_probe);
1086
1087 reflection_probe->blend_distance = p_blend_distance;
1088 }
1089
1090 void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
1091 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1092 ERR_FAIL_NULL(reflection_probe);
1093
1094 reflection_probe->ambient_mode = p_mode;
1095 }
1096
1097 void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
1098 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1099 ERR_FAIL_NULL(reflection_probe);
1100
1101 reflection_probe->ambient_color = p_color;
1102 }
1103
1104 void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
1105 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1106 ERR_FAIL_NULL(reflection_probe);
1107
1108 reflection_probe->ambient_color_energy = p_energy;
1109 }
1110
1111 void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
1112 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1113 ERR_FAIL_NULL(reflection_probe);
1114
1115 reflection_probe->max_distance = p_distance;
1116
1117 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1118 }
1119
1120 void LightStorage::reflection_probe_set_size(RID p_probe, const Vector3 &p_size) {
1121 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1122 ERR_FAIL_NULL(reflection_probe);
1123
1124 if (reflection_probe->size == p_size) {
1125 return;
1126 }
1127 reflection_probe->size = p_size;
1128 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1129 }
1130
1131 void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
1132 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1133 ERR_FAIL_NULL(reflection_probe);
1134
1135 reflection_probe->origin_offset = p_offset;
1136 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1137 }
1138
1139 void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
1140 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1141 ERR_FAIL_NULL(reflection_probe);
1142
1143 reflection_probe->interior = p_enable;
1144 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1145 }
1146
1147 void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
1148 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1149 ERR_FAIL_NULL(reflection_probe);
1150
1151 reflection_probe->box_projection = p_enable;
1152 }
1153
1154 void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
1155 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1156 ERR_FAIL_NULL(reflection_probe);
1157
1158 reflection_probe->enable_shadows = p_enable;
1159 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1160 }
1161
1162 void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
1163 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1164 ERR_FAIL_NULL(reflection_probe);
1165
1166 reflection_probe->cull_mask = p_layers;
1167 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1168 }
1169
1170 void LightStorage::reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) {
1171 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1172 ERR_FAIL_NULL(reflection_probe);
1173
1174 reflection_probe->reflection_mask = p_layers;
1175 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1176 }
1177
1178 void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
1179 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.");
1180 }
1181
1182 void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
1183 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1184 ERR_FAIL_NULL(reflection_probe);
1185
1186 reflection_probe->mesh_lod_threshold = p_ratio;
1187
1188 reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
1189 }
1190
1191 void LightStorage::reflection_probe_set_baked_exposure(RID p_probe, float p_exposure) {
1192 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1193 ERR_FAIL_NULL(reflection_probe);
1194
1195 reflection_probe->baked_exposure = p_exposure;
1196 }
1197
1198 AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
1199 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1200 ERR_FAIL_NULL_V(reflection_probe, AABB());
1201
1202 AABB aabb;
1203 aabb.position = -reflection_probe->size / 2;
1204 aabb.size = reflection_probe->size;
1205
1206 return aabb;
1207 }
1208
1209 RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
1210 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1211 ERR_FAIL_NULL_V(reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
1212
1213 return reflection_probe->update_mode;
1214 }
1215
1216 uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
1217 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1218 ERR_FAIL_NULL_V(reflection_probe, 0);
1219
1220 return reflection_probe->cull_mask;
1221 }
1222
1223 uint32_t LightStorage::reflection_probe_get_reflection_mask(RID p_probe) const {
1224 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1225 ERR_FAIL_NULL_V(reflection_probe, 0);
1226
1227 return reflection_probe->reflection_mask;
1228 }
1229
1230 Vector3 LightStorage::reflection_probe_get_size(RID p_probe) const {
1231 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1232 ERR_FAIL_NULL_V(reflection_probe, Vector3());
1233
1234 return reflection_probe->size;
1235 }
1236
1237 Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
1238 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1239 ERR_FAIL_NULL_V(reflection_probe, Vector3());
1240
1241 return reflection_probe->origin_offset;
1242 }
1243
1244 bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
1245 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1246 ERR_FAIL_NULL_V(reflection_probe, false);
1247
1248 return reflection_probe->enable_shadows;
1249 }
1250
1251 float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
1252 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1253 ERR_FAIL_NULL_V(reflection_probe, 0);
1254
1255 return reflection_probe->max_distance;
1256 }
1257
1258 float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
1259 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1260 ERR_FAIL_NULL_V(reflection_probe, 0);
1261
1262 return reflection_probe->mesh_lod_threshold;
1263 }
1264
1265 float LightStorage::reflection_probe_get_baked_exposure(RID p_probe) const {
1266 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1267 ERR_FAIL_NULL_V(reflection_probe, 1.0);
1268
1269 return reflection_probe->baked_exposure;
1270 }
1271
1272 float LightStorage::reflection_probe_get_intensity(RID p_probe) const {
1273 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1274 ERR_FAIL_NULL_V(reflection_probe, 0);
1275
1276 return reflection_probe->intensity;
1277 }
1278
1279 float LightStorage::reflection_probe_get_blend_distance(RID p_probe) const {
1280 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1281 ERR_FAIL_NULL_V(reflection_probe, 0);
1282
1283 return reflection_probe->blend_distance;
1284 }
1285
1286 bool LightStorage::reflection_probe_is_interior(RID p_probe) const {
1287 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1288 ERR_FAIL_NULL_V(reflection_probe, false);
1289
1290 return reflection_probe->interior;
1291 }
1292
1293 bool LightStorage::reflection_probe_is_box_projection(RID p_probe) const {
1294 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1295 ERR_FAIL_NULL_V(reflection_probe, false);
1296
1297 return reflection_probe->box_projection;
1298 }
1299
1300 RS::ReflectionProbeAmbientMode LightStorage::reflection_probe_get_ambient_mode(RID p_probe) const {
1301 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1302 ERR_FAIL_NULL_V(reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
1303 return reflection_probe->ambient_mode;
1304 }
1305
1306 Color LightStorage::reflection_probe_get_ambient_color(RID p_probe) const {
1307 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1308 ERR_FAIL_NULL_V(reflection_probe, Color());
1309
1310 return reflection_probe->ambient_color;
1311 }
1312 float LightStorage::reflection_probe_get_ambient_color_energy(RID p_probe) const {
1313 const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1314 ERR_FAIL_NULL_V(reflection_probe, 0);
1315
1316 return reflection_probe->ambient_color_energy;
1317 }
1318
1319 Dependency *LightStorage::reflection_probe_get_dependency(RID p_probe) const {
1320 ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
1321 ERR_FAIL_NULL_V(reflection_probe, nullptr);
1322
1323 return &reflection_probe->dependency;
1324 }
1325
1326 /* REFLECTION ATLAS */
1327
1328 RID LightStorage::reflection_atlas_create() {
1329 ReflectionAtlas ra;
1330 ra.count = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_count");
1331 ra.size = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_size");
1332 ra.cluster_builder = nullptr;
1333
1334 return reflection_atlas_owner.make_rid(ra);
1335 }
1336
1337 void LightStorage::reflection_atlas_free(RID p_ref_atlas) {
1338 reflection_atlas_set_size(p_ref_atlas, 0, 0);
1339 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
1340 if (ra->cluster_builder) {
1341 memdelete(ra->cluster_builder);
1342 }
1343 reflection_atlas_owner.free(p_ref_atlas);
1344 }
1345
1346 void LightStorage::_reflection_atlas_clear(ReflectionAtlas *p_reflection_atlas) {
1347 RD::get_singleton()->free_rid(p_reflection_atlas->reflection);
1348 p_reflection_atlas->reflection = RID();
1349
1350 RD::get_singleton()->free_rid(p_reflection_atlas->depth_fb);
1351 p_reflection_atlas->depth_fb = RID();
1352
1353 RD::get_singleton()->free_rid(p_reflection_atlas->depth_buffer);
1354 p_reflection_atlas->depth_buffer = RID();
1355
1356 for (int i = 0; i < p_reflection_atlas->reflections.size(); i++) {
1357 p_reflection_atlas->reflections.write[i].data.clear_reflection_data();
1358 if (p_reflection_atlas->reflections[i].owner.is_null()) {
1359 continue;
1360 }
1361
1362 reflection_probe_release_atlas_index(p_reflection_atlas->reflections[i].owner);
1363 }
1364
1365 p_reflection_atlas->reflections.clear();
1366 }
1367
1368 void LightStorage::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
1369 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
1370 ERR_FAIL_NULL(ra);
1371
1372 if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
1373 return; //no changes
1374 }
1375
1376 if (ra->cluster_builder) {
1377 // only if we're using our cluster
1378 ra->cluster_builder->setup(Size2i(ra->size, ra->size), max_cluster_elements, RID(), RID(), RID());
1379 }
1380
1381 ra->size = p_reflection_size;
1382 ra->count = p_reflection_count;
1383
1384 if (ra->reflection.is_valid()) {
1385 _reflection_atlas_clear(ra);
1386 }
1387
1388 if (ra->render_buffers.is_valid()) {
1389 ra->render_buffers->cleanup();
1390 }
1391 }
1392
1393 int LightStorage::reflection_atlas_get_size(RID p_ref_atlas) const {
1394 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
1395 ERR_FAIL_NULL_V(ra, 0);
1396
1397 return ra->size;
1398 }
1399
1400 /* REFLECTION PROBE INSTANCE */
1401
1402 RID LightStorage::reflection_probe_instance_create(RID p_probe) {
1403 ReflectionProbeInstance rpi;
1404 rpi.probe = p_probe;
1405 rpi.forward_id = ForwardIDStorage::get_singleton()->allocate_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE);
1406
1407 return reflection_probe_instance_owner.make_rid(rpi);
1408 }
1409
1410 void LightStorage::reflection_probe_instance_free(RID p_instance) {
1411 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1412 ForwardIDStorage::get_singleton()->free_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE, rpi->forward_id);
1413 reflection_probe_release_atlas_index(p_instance);
1414 reflection_probe_instance_owner.free(p_instance);
1415 }
1416
1417 void LightStorage::reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) {
1418 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1419 ERR_FAIL_NULL(rpi);
1420
1421 rpi->transform = p_transform;
1422 rpi->dirty = true;
1423 }
1424
1425 bool LightStorage::reflection_probe_has_atlas_index(RID p_instance) {
1426 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1427 ERR_FAIL_NULL_V(rpi, false);
1428
1429 if (rpi->atlas.is_null()) {
1430 return false;
1431 }
1432
1433 return rpi->atlas_index >= 0;
1434 }
1435
1436 void LightStorage::reflection_probe_release_atlas_index(RID p_instance) {
1437 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1438 ERR_FAIL_NULL(rpi);
1439
1440 if (rpi->atlas.is_null()) {
1441 return; //nothing to release
1442 }
1443 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1444 ERR_FAIL_NULL(atlas);
1445 ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
1446 atlas->reflections.write[rpi->atlas_index].owner = RID();
1447
1448 // TODO investigate if this is enough? shouldn't we be freeing our textures and framebuffers?
1449
1450 if (rpi->rendering) {
1451 // We were cancelled mid rendering, trigger refresh.
1452 rpi->rendering = false;
1453 rpi->dirty = true;
1454 rpi->processing_layer = 1;
1455 }
1456
1457 rpi->atlas_index = -1;
1458 rpi->atlas = RID();
1459 }
1460
1461 bool LightStorage::reflection_probe_instance_needs_redraw(RID p_instance) {
1462 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1463 ERR_FAIL_NULL_V(rpi, false);
1464
1465 if (rpi->rendering) {
1466 return false;
1467 }
1468
1469 if (rpi->dirty) {
1470 return true;
1471 }
1472
1473 if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
1474 return true;
1475 }
1476
1477 return rpi->atlas_index == -1;
1478 }
1479
1480 bool LightStorage::reflection_probe_instance_has_reflection(RID p_instance) {
1481 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1482 ERR_FAIL_NULL_V(rpi, false);
1483
1484 return rpi->atlas.is_valid();
1485 }
1486
1487 bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
1488 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
1489
1490 ERR_FAIL_NULL_V(atlas, false);
1491
1492 ERR_FAIL_COND_V_MSG(atlas->size < 2, false, "Attempted to render to a reflection atlas of invalid resolution.");
1493 ERR_FAIL_COND_V_MSG(atlas->count < 1, false, "Attempted to render to a reflection atlas of size < 1.");
1494
1495 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1496 ERR_FAIL_NULL_V(rpi, false);
1497
1498 if (atlas->render_buffers.is_null()) {
1499 atlas->render_buffers.instantiate();
1500 }
1501
1502 RD::get_singleton()->draw_command_begin_label("Reflection Probe Render");
1503
1504 const bool update_always = LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS;
1505 if (update_always && atlas->reflection.is_valid() && atlas->size != 256) {
1506 WARN_PRINT("ReflectionProbes set to UPDATE_ALWAYS must have an atlas size of 256. Please update the atlas size in the ProjectSettings.");
1507 reflection_atlas_set_size(p_reflection_atlas, 256, atlas->count);
1508 }
1509
1510 // Reflection atlas only allows recreating the texture at a lower resolution when going from not real time to real time,
1511 // but not the other way around. This can lead to situations where the reflection atlas will be stuck on a lower resolution
1512 // even if no real-time probes are present. This is intentional behavior until a future solution can accommodate for both
1513 // quality levels being used simultaneously.
1514 const int required_real_time_mipmaps = 7;
1515 const bool switched_to_real_time = !atlas->update_always && update_always && atlas->reflection.is_valid();
1516 const bool real_time_mipmaps_different = update_always && atlas->reflection.is_valid() && atlas->reflections[0].data.layers[0].mipmaps.size() != required_real_time_mipmaps;
1517 if (switched_to_real_time || real_time_mipmaps_different) {
1518 _reflection_atlas_clear(atlas);
1519 }
1520
1521 if (atlas->reflection.is_null()) {
1522 RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
1523 ERR_FAIL_NULL_V_MSG(copy_effects, false, "Effects haven't been initialized");
1524
1525 int mipmaps = MIN(RendererSceneRenderRD::get_singleton()->get_sky()->roughness_layers, Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) + 1);
1526 mipmaps = update_always ? required_real_time_mipmaps : mipmaps;
1527
1528 // Double size to approximate texel density of cubemaps + add border for proper filtering/mipmapping.
1529 uint32_t padding_pixels = (1 << (mipmaps - 1));
1530 atlas->reflection_texture_size = atlas->size * 2 + padding_pixels * 2;
1531 atlas->uv_border_size = float(padding_pixels) / float(atlas->reflection_texture_size);
1532
1533 bool use_storage = !copy_effects->get_raster_effects().has_flag(CopyEffects::RASTER_EFFECT_OCTMAP);
1534 {
1535 RD::TextureFormat tf;
1536 tf.array_layers = atlas->count;
1537 tf.format = get_reflection_probe_color_format();
1538 tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
1539 tf.mipmaps = mipmaps;
1540 tf.width = atlas->reflection_texture_size;
1541 tf.height = atlas->reflection_texture_size;
1542 tf.usage_bits = get_reflection_probe_color_usage_bits(use_storage);
1543 atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
1544 }
1545 {
1546 RD::TextureFormat tf;
1547 tf.array_layers = 6;
1548 tf.format = get_reflection_probe_color_format();
1549 tf.texture_type = RD::TEXTURE_TYPE_CUBE;
1550 tf.width = atlas->size;
1551 tf.height = atlas->size;
1552 tf.usage_bits = get_reflection_probe_color_usage_bits(use_storage);
1553 atlas->color_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
1554 }
1555 {
1556 RD::TextureFormat tf;
1557 tf.format = get_reflection_probe_depth_format();
1558 tf.width = atlas->size;
1559 tf.height = atlas->size;
1560 tf.usage_bits = get_reflection_probe_depth_usage_bits();
1561 atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
1562 }
1563 atlas->reflections.resize(atlas->count);
1564 for (int i = 0; i < atlas->count; i++) {
1565 atlas->reflections.write[i].data.update_reflection_data(atlas->reflection_texture_size, mipmaps, false, atlas->reflection, i, update_always, RendererSceneRenderRD::get_singleton()->get_sky()->roughness_layers, RendererSceneRenderRD::get_singleton()->_render_buffers_get_preferred_color_format(), atlas->uv_border_size);
1566 }
1567
1568 for (int i = 0; i < 6; i++) {
1569 atlas->color_views[i] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), atlas->color_buffer, i, 0);
1570 atlas->color_fbs[i] = RendererSceneRenderRD::get_singleton()->reflection_probe_create_framebuffer(atlas->color_views[i], atlas->depth_buffer);
1571 }
1572
1573 Vector<RID> fbs;
1574 fbs.push_back(atlas->depth_buffer);
1575 atlas->depth_fb = RD::get_singleton()->framebuffer_create(fbs);
1576
1577 atlas->render_buffers->configure_for_reflections(Size2i(atlas->size, atlas->size));
1578 atlas->update_always = update_always;
1579 }
1580
1581 if (rpi->atlas_index == -1) {
1582 for (int i = 0; i < atlas->reflections.size(); i++) {
1583 if (atlas->reflections[i].owner.is_null()) {
1584 rpi->atlas_index = i;
1585 break;
1586 }
1587 }
1588 //find the one used last
1589 if (rpi->atlas_index == -1) {
1590 //everything is in use, find the one least used via LRU
1591 uint64_t pass_min = 0;
1592
1593 for (int i = 0; i < atlas->reflections.size(); i++) {
1594 ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.get_or_null(atlas->reflections[i].owner);
1595 if (rpi2->last_pass < pass_min) {
1596 pass_min = rpi2->last_pass;
1597 rpi->atlas_index = i;
1598 }
1599 }
1600 }
1601 }
1602
1603 if (rpi->atlas_index != -1) { // should we fail if this is still -1 ?
1604 atlas->reflections.write[rpi->atlas_index].owner = p_instance;
1605 }
1606
1607 rpi->atlas = p_reflection_atlas;
1608 rpi->rendering = true;
1609 rpi->dirty = false;
1610 rpi->processing_layer = 1;
1611
1612 RD::get_singleton()->draw_command_end_label();
1613
1614 return true;
1615 }
1616
1617 bool LightStorage::reflection_probe_instance_end_render(RID p_instance, RID p_reflection_atlas) {
1618 RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
1619 ERR_FAIL_NULL_V_MSG(copy_effects, false, "Effects haven't been initialized");
1620
1621 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
1622 ERR_FAIL_NULL_V(atlas, false);
1623
1624 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1625 ERR_FAIL_NULL_V(rpi, false);
1626
1627 RD::get_singleton()->draw_command_begin_label("Convert reflection probe to octahedral");
1628 copy_effects->copy_cubemap_to_octmap(atlas->color_buffer, atlas->reflections.write[rpi->atlas_index].data.layers[0].mipmaps[0].framebuffer, atlas->uv_border_size);
1629 RD::get_singleton()->draw_command_end_label();
1630
1631 return true;
1632 }
1633
1634 Ref<RenderSceneBuffers> LightStorage::reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) {
1635 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
1636 ERR_FAIL_NULL_V(atlas, Ref<RenderSceneBuffersRD>());
1637
1638 return atlas->render_buffers;
1639 }
1640
1641 bool LightStorage::reflection_probe_instance_postprocess_step(RID p_instance) {
1642 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1643 ERR_FAIL_NULL_V(rpi, false);
1644 ERR_FAIL_COND_V(!rpi->rendering, false);
1645
1646 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1647 if (!atlas || rpi->atlas_index == -1) {
1648 // Does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering).
1649 rpi->rendering = false;
1650 return false;
1651 }
1652
1653 if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
1654 // Using real time reflections, all roughness is done in one step
1655 atlas->reflections.write[rpi->atlas_index].data.create_reflection_fast_filter(false);
1656 rpi->rendering = false;
1657 rpi->processing_layer = 1;
1658 return true;
1659 }
1660
1661 atlas->reflections.write[rpi->atlas_index].data.create_reflection_importance_sample(false, rpi->processing_layer, RendererSceneRenderRD::get_singleton()->get_sky()->sky_ggx_samples_quality);
1662
1663 rpi->processing_layer++;
1664 if (rpi->processing_layer == (int)atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
1665 rpi->rendering = false;
1666 rpi->processing_layer = 1;
1667 return true;
1668 }
1669
1670 return false;
1671 }
1672
1673 uint32_t LightStorage::reflection_probe_instance_get_resolution(RID p_instance) {
1674 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1675 ERR_FAIL_NULL_V(rpi, 0);
1676
1677 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1678 ERR_FAIL_NULL_V(atlas, 0);
1679 return atlas->size;
1680 }
1681
1682 RID LightStorage::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
1683 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1684 ERR_FAIL_NULL_V(rpi, RID());
1685 ERR_FAIL_INDEX_V(p_index, 6, RID());
1686
1687 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1688 ERR_FAIL_NULL_V(atlas, RID());
1689 ERR_FAIL_COND_V_MSG(rpi->atlas_index < 0, RID(), "Reflection probe atlas index invalid. Maximum amount of reflection probes in use (" + itos(atlas->count) + ") may have been exceeded, reflections will not display properly. Consider increasing Rendering > Reflections > Reflection Atlas > Reflection Count in the Project Settings.");
1690
1691 return atlas->color_fbs[p_index];
1692 }
1693
1694 RID LightStorage::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
1695 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1696 ERR_FAIL_NULL_V(rpi, RID());
1697 ERR_FAIL_INDEX_V(p_index, 6, RID());
1698
1699 ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
1700 ERR_FAIL_NULL_V(atlas, RID());
1701 return atlas->depth_fb;
1702 }
1703
1704 ClusterBuilderRD *LightStorage::reflection_probe_instance_get_cluster_builder(RID p_instance, ClusterBuilderSharedDataRD *p_cluster_builder_shared) {
1705 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
1706 ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(rpi->atlas);
1707 if (!ra) {
1708 ERR_PRINT("reflection probe has no reflection atlas! Bug?");
1709 return nullptr;
1710 } else {
1711 if (ra->cluster_builder == nullptr) {
1712 ra->cluster_builder = memnew(ClusterBuilderRD);
1713 ra->cluster_builder->set_shared(p_cluster_builder_shared);
1714 ra->cluster_builder->setup(Size2i(ra->size, ra->size), get_max_cluster_elements(), RID(), RID(), RID());
1715 }
1716 return ra->cluster_builder;
1717 }
1718 }
1719
1720 /* REFLECTION DATA */
1721
1722 void LightStorage::free_reflection_data() {
1723 if (reflection_buffer.is_valid()) {
1724 RD::get_singleton()->free_rid(reflection_buffer);
1725 reflection_buffer = RID();
1726 }
1727
1728 if (reflections != nullptr) {
1729 memdelete_arr(reflections);
1730 reflections = nullptr;
1731 }
1732
1733 if (reflection_sort != nullptr) {
1734 memdelete_arr(reflection_sort);
1735 reflection_sort = nullptr;
1736 }
1737 }
1738
1739 void LightStorage::set_max_reflection_probes(const uint32_t p_max_reflection_probes) {
1740 max_reflections = p_max_reflection_probes;
1741 reflections = memnew_arr(ReflectionData, max_reflections);
1742 reflection_sort = memnew_arr(ReflectionProbeInstanceSort, max_reflections);
1743 reflection_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ReflectionData) * max_reflections);
1744 }
1745
1746 void LightStorage::update_reflection_probe_buffer(RenderDataRD *p_render_data, const PagedArray<RID> &p_reflections, const Transform3D &p_camera_inverse_transform, RID p_environment) {
1747 ForwardIDStorage *forward_id_storage = ForwardIDStorage::get_singleton();
1748
1749 reflection_count = 0;
1750
1751 for (uint32_t i = 0; i < (uint32_t)p_reflections.size(); i++) {
1752 if (reflection_count == max_reflections) {
1753 break;
1754 }
1755
1756 ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_reflections[i]);
1757 if (!rpi) {
1758 continue;
1759 }
1760 ReflectionProbe *probe = reflection_probe_owner.get_or_null(rpi->probe);
1761 Vector3 extents = probe->size / 2;
1762 float probe_size = extents.length();
1763
1764 reflection_sort[reflection_count].probe_instance = rpi;
1765 reflection_sort[reflection_count].size = -probe_size;
1766 reflection_count++;
1767 }
1768
1769 if (reflection_count > 0) {
1770 SortArray<ReflectionProbeInstanceSort> sort_array;
1771 sort_array.sort(reflection_sort, reflection_count);
1772 }
1773
1774 bool using_forward_ids = forward_id_storage->uses_forward_ids();
1775 for (uint32_t i = 0; i < reflection_count; i++) {
1776 ReflectionProbeInstance *rpi = reflection_sort[i].probe_instance;
1777
1778 rpi->last_pass = RSG::rasterizer->get_frame_number();
1779
1780 if (using_forward_ids) {
1781 forward_id_storage->map_forward_id(FORWARD_ID_TYPE_REFLECTION_PROBE, rpi->forward_id, i, rpi->last_pass);
1782 }
1783
1784 ReflectionProbe *probe = reflection_probe_owner.get_or_null(rpi->probe);
1785
1786 ReflectionData &reflection_ubo = reflections[i];
1787
1788 Vector3 extents = probe->size / 2;
1789
1790 rpi->cull_mask = probe->reflection_mask;
1791
1792 reflection_ubo.box_extents[0] = extents.x;
1793 reflection_ubo.box_extents[1] = extents.y;
1794 reflection_ubo.box_extents[2] = extents.z;
1795 reflection_ubo.index = rpi->atlas_index;
1796
1797 Vector3 origin_offset = probe->origin_offset;
1798
1799 reflection_ubo.box_offset[0] = origin_offset.x;
1800 reflection_ubo.box_offset[1] = origin_offset.y;
1801 reflection_ubo.box_offset[2] = origin_offset.z;
1802 reflection_ubo.mask = probe->reflection_mask;
1803
1804 reflection_ubo.intensity = probe->intensity;
1805 reflection_ubo.blend_distance = probe->blend_distance;
1806 reflection_ubo.ambient_mode = probe->ambient_mode;
1807
1808 reflection_ubo.exterior = !probe->interior;
1809 reflection_ubo.box_project = probe->box_projection;
1810 reflection_ubo.exposure_normalization = 1.0;
1811
1812 if (p_render_data->camera_attributes.is_valid()) {
1813 float exposure = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1814 reflection_ubo.exposure_normalization = exposure / probe->baked_exposure;
1815 }
1816
1817 Color ambient_linear = probe->ambient_color.srgb_to_linear();
1818 float interior_ambient_energy = probe->ambient_color_energy;
1819 reflection_ubo.ambient[0] = ambient_linear.r * interior_ambient_energy;
1820 reflection_ubo.ambient[1] = ambient_linear.g * interior_ambient_energy;
1821 reflection_ubo.ambient[2] = ambient_linear.b * interior_ambient_energy;
1822
1823 Transform3D transform = rpi->transform;
1824 Transform3D proj = (p_camera_inverse_transform * transform).inverse();
1825 MaterialStorage::store_transform(proj, reflection_ubo.local_matrix);
1826
1827 // hook for subclass to do further processing.
1828 RendererSceneRenderRD::get_singleton()->setup_added_reflection_probe(transform, extents);
1829 }
1830
1831 if (reflection_count) {
1832 RD::get_singleton()->buffer_update(reflection_buffer, 0, reflection_count * sizeof(ReflectionData), reflections);
1833 }
1834 }
1835
1836 RD::DataFormat LightStorage::get_reflection_probe_color_format() {
1837 return RendererSceneRenderRD::get_singleton()->_render_buffers_get_preferred_color_format();
1838 }
1839
1840 uint32_t LightStorage::get_reflection_probe_color_usage_bits(bool p_storage) {
1841 return RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | (p_storage ? RD::TEXTURE_USAGE_STORAGE_BIT : 0);
1842 }
1843
1844 RD::DataFormat LightStorage::get_reflection_probe_depth_format() {
1845 return RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
1846 }
1847
1848 uint32_t LightStorage::get_reflection_probe_depth_usage_bits() {
1849 return RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
1850 }
1851
1852 /* LIGHTMAP API */
1853
1854 RID LightStorage::lightmap_allocate() {
1855 return lightmap_owner.allocate_rid();
1856 }
1857
1858 void LightStorage::lightmap_initialize(RID p_lightmap) {
1859 lightmap_owner.initialize_rid(p_lightmap, Lightmap());
1860 }
1861
1862 void LightStorage::lightmap_free(RID p_rid) {
1863 lightmap_set_textures(p_rid, RID(), false);
1864 Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
1865 lightmap->dependency.deleted_notify(p_rid);
1866 lightmap_owner.free(p_rid);
1867 }
1868
1869 void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
1870 TextureStorage *texture_storage = TextureStorage::get_singleton();
1871
1872 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1873 ERR_FAIL_NULL(lm);
1874
1875 lightmap_array_version++;
1876
1877 //erase lightmap users
1878 if (lm->light_texture.is_valid()) {
1879 TextureStorage::Texture *t = texture_storage->get_texture(lm->light_texture);
1880 if (t) {
1881 t->lightmap_users.erase(p_lightmap);
1882 }
1883 }
1884
1885 TextureStorage::Texture *t = texture_storage->get_texture(p_light);
1886 lm->light_texture = p_light;
1887 lm->uses_spherical_harmonics = p_uses_spherical_haromics;
1888
1889 RID default_2d_array = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
1890 if (!t) {
1891 if (using_lightmap_array) {
1892 if (lm->array_index >= 0) {
1893 lightmap_textures.write[lm->array_index] = default_2d_array;
1894 lm->array_index = -1;
1895 }
1896 }
1897
1898 return;
1899 }
1900
1901 t->lightmap_users.insert(p_lightmap);
1902 lm->light_texture_size = Vector2i(t->width, t->height);
1903
1904 if (using_lightmap_array) {
1905 if (lm->array_index < 0) {
1906 //not in array, try to put in array
1907 for (int i = 0; i < lightmap_textures.size(); i++) {
1908 if (lightmap_textures[i] == default_2d_array) {
1909 lm->array_index = i;
1910 break;
1911 }
1912 }
1913 }
1914 ERR_FAIL_COND_MSG(lm->array_index < 0, "Maximum amount of lightmaps in use (" + itos(lightmap_textures.size()) + ") has been exceeded, lightmap will not display properly.");
1915
1916 lightmap_textures.write[lm->array_index] = t->rd_texture;
1917 }
1918 }
1919
1920 void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
1921 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1922 ERR_FAIL_NULL(lm);
1923 lm->bounds = p_bounds;
1924 }
1925
1926 void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
1927 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1928 ERR_FAIL_NULL(lm);
1929 lm->interior = p_interior;
1930 }
1931
1932 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) {
1933 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1934 ERR_FAIL_NULL(lm);
1935
1936 if (p_points.size()) {
1937 ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
1938 ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
1939 ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
1940 }
1941
1942 lm->points = p_points;
1943 lm->bsp_tree = p_bsp_tree;
1944 lm->point_sh = p_point_sh;
1945 lm->tetrahedra = p_tetrahedra;
1946 }
1947
1948 void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
1949 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1950 ERR_FAIL_NULL(lm);
1951
1952 lm->baked_exposure = p_exposure;
1953 }
1954
1955 PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
1956 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1957 ERR_FAIL_NULL_V(lm, PackedVector3Array());
1958
1959 return lm->points;
1960 }
1961
1962 PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
1963 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1964 ERR_FAIL_NULL_V(lm, PackedColorArray());
1965 return lm->point_sh;
1966 }
1967
1968 PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
1969 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1970 ERR_FAIL_NULL_V(lm, PackedInt32Array());
1971 return lm->tetrahedra;
1972 }
1973
1974 PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
1975 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1976 ERR_FAIL_NULL_V(lm, PackedInt32Array());
1977 return lm->bsp_tree;
1978 }
1979
1980 void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
1981 lightmap_probe_capture_update_speed = p_speed;
1982 }
1983
1984 Dependency *LightStorage::lightmap_get_dependency(RID p_lightmap) const {
1985 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1986 ERR_FAIL_NULL_V(lm, nullptr);
1987
1988 return &lm->dependency;
1989 }
1990
1991 void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
1992 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
1993 ERR_FAIL_NULL(lm);
1994
1995 for (int i = 0; i < 9; i++) {
1996 r_sh[i] = Color(0, 0, 0, 0);
1997 }
1998
1999 if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
2000 return;
2001 }
2002
2003 static_assert(sizeof(Lightmap::BSP) == 24);
2004
2005 const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
2006 int32_t node = 0;
2007 while (node >= 0) {
2008 if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
2009 #ifdef DEBUG_ENABLED
2010 ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
2011 #endif
2012
2013 node = bsp[node].over;
2014 } else {
2015 #ifdef DEBUG_ENABLED
2016 ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
2017 #endif
2018 node = bsp[node].under;
2019 }
2020 }
2021
2022 if (node == Lightmap::BSP::EMPTY_LEAF) {
2023 return; //nothing could be done
2024 }
2025
2026 node = Math::abs(node) - 1;
2027
2028 uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
2029 Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
2030 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] };
2031 Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
2032
2033 for (int i = 0; i < 4; i++) {
2034 float c = CLAMP(barycentric[i], 0.0, 1.0);
2035 for (int j = 0; j < 9; j++) {
2036 r_sh[j] += sh_colors[i][j] * c;
2037 }
2038 }
2039 }
2040
2041 bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
2042 const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
2043 ERR_FAIL_NULL_V(lm, false);
2044 return lm->interior;
2045 }
2046
2047 AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
2048 const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
2049 ERR_FAIL_NULL_V(lm, AABB());
2050 return lm->bounds;
2051 }
2052
2053 void LightStorage::lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) {
2054 TextureStorage *texture_storage = TextureStorage::get_singleton();
2055
2056 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
2057 ERR_FAIL_NULL(lm);
2058
2059 // Erase lightmap users from shadow texture.
2060 if (lm->shadow_texture.is_valid()) {
2061 TextureStorage::Texture *t = texture_storage->get_texture(lm->shadow_texture);
2062 if (t) {
2063 t->lightmap_users.erase(p_lightmap);
2064 }
2065 }
2066
2067 TextureStorage::Texture *t = texture_storage->get_texture(p_shadow);
2068 lm->shadow_texture = p_shadow;
2069
2070 RID default_2d_array = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
2071 if (!t) {
2072 if (lm->array_index >= 0) {
2073 shadowmask_textures.write[lm->array_index] = default_2d_array;
2074 lm->array_index = -1;
2075 }
2076
2077 return;
2078 }
2079
2080 t->lightmap_users.insert(p_lightmap);
2081
2082 if (lm->array_index < 0) {
2083 // Not in array, try to put in array.
2084 for (int i = 0; i < shadowmask_textures.size(); i++) {
2085 if (shadowmask_textures[i] == default_2d_array) {
2086 lm->array_index = i;
2087 break;
2088 }
2089 }
2090 }
2091
2092 ERR_FAIL_COND_MSG(lm->array_index < 0, vformat("Maximum amount of shadowmasks in use (%d) has been exceeded, shadowmask will not display properly.", shadowmask_textures.size()));
2093
2094 shadowmask_textures.write[lm->array_index] = t->rd_texture;
2095 }
2096
2097 RS::ShadowmaskMode LightStorage::lightmap_get_shadowmask_mode(RID p_lightmap) {
2098 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
2099 ERR_FAIL_NULL_V(lm, RS::SHADOWMASK_MODE_NONE);
2100
2101 return lm->shadowmask_mode;
2102 }
2103
2104 void LightStorage::lightmap_set_shadowmask_mode(RID p_lightmap, RS::ShadowmaskMode p_mode) {
2105 Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
2106 ERR_FAIL_NULL(lm);
2107
2108 lm->shadowmask_mode = p_mode;
2109 }
2110
2111 /* LIGHTMAP INSTANCE */
2112
2113 RID LightStorage::lightmap_instance_create(RID p_lightmap) {
2114 LightmapInstance li;
2115 li.lightmap = p_lightmap;
2116 return lightmap_instance_owner.make_rid(li);
2117 }
2118
2119 void LightStorage::lightmap_instance_free(RID p_lightmap) {
2120 lightmap_instance_owner.free(p_lightmap);
2121 }
2122
2123 void LightStorage::lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) {
2124 LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap);
2125 ERR_FAIL_NULL(li);
2126 li->transform = p_transform;
2127 }
2128
2129 /* SHADOW ATLAS API */
2130
2131 RID LightStorage::shadow_atlas_create() {
2132 return shadow_atlas_owner.make_rid(ShadowAtlas());
2133 }
2134
2135 void LightStorage::shadow_atlas_free(RID p_atlas) {
2136 shadow_atlas_set_size(p_atlas, 0);
2137 shadow_atlas_owner.free(p_atlas);
2138 }
2139
2140 void LightStorage::_update_shadow_atlas(ShadowAtlas *shadow_atlas) {
2141 if (shadow_atlas->size > 0 && shadow_atlas->depth.is_null()) {
2142 RD::TextureFormat tf;
2143 tf.format = get_shadow_atlas_depth_format(shadow_atlas->use_16_bits);
2144 tf.width = shadow_atlas->size;
2145 tf.height = shadow_atlas->size;
2146 tf.usage_bits = get_shadow_atlas_depth_usage_bits();
2147
2148 shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
2149 Vector<RID> fb_tex;
2150 fb_tex.push_back(shadow_atlas->depth);
2151 shadow_atlas->fb = RD::get_singleton()->framebuffer_create(fb_tex);
2152 }
2153 }
2154
2155 void LightStorage::shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits) {
2156 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
2157 ERR_FAIL_NULL(shadow_atlas);
2158 ERR_FAIL_COND(p_size < 0);
2159 p_size = next_power_of_2((uint32_t)p_size);
2160
2161 if (p_size == shadow_atlas->size && p_16_bits == shadow_atlas->use_16_bits) {
2162 return;
2163 }
2164
2165 // erasing atlas
2166 if (shadow_atlas->depth.is_valid()) {
2167 RD::get_singleton()->free_rid(shadow_atlas->depth);
2168 shadow_atlas->depth = RID();
2169 }
2170 for (int i = 0; i < 4; i++) {
2171 //clear subdivisions
2172 shadow_atlas->quadrants[i].shadows.clear();
2173 shadow_atlas->quadrants[i].shadows.resize(int64_t(shadow_atlas->quadrants[i].subdivision * shadow_atlas->quadrants[i].subdivision));
2174 }
2175
2176 //erase shadow atlas reference from lights
2177 for (const KeyValue<RID, uint32_t> &E : shadow_atlas->shadow_owners) {
2178 LightInstance *li = light_instance_owner.get_or_null(E.key);
2179 ERR_CONTINUE(!li);
2180 li->shadow_atlases.erase(p_atlas);
2181 }
2182
2183 //clear owners
2184 shadow_atlas->shadow_owners.clear();
2185
2186 shadow_atlas->size = p_size;
2187 shadow_atlas->use_16_bits = p_16_bits;
2188 }
2189
2190 void LightStorage::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
2191 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
2192 ERR_FAIL_NULL(shadow_atlas);
2193 ERR_FAIL_INDEX(p_quadrant, 4);
2194 ERR_FAIL_INDEX(p_subdivision, 16384);
2195
2196 uint32_t subdiv = next_power_of_2((uint32_t)p_subdivision);
2197 if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
2198 subdiv <<= 1;
2199 }
2200
2201 subdiv = int(Math::sqrt((float)subdiv));
2202
2203 //obtain the number that will be x*x
2204
2205 if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
2206 return;
2207 }
2208
2209 //erase all data from quadrant
2210 for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
2211 if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
2212 shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
2213 LightInstance *li = light_instance_owner.get_or_null(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
2214 ERR_CONTINUE(!li);
2215 li->shadow_atlases.erase(p_atlas);
2216 }
2217 }
2218
2219 shadow_atlas->quadrants[p_quadrant].shadows.clear();
2220 shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
2221 shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
2222
2223 //cache the smallest subdiv (for faster allocation in light update)
2224
2225 shadow_atlas->smallest_subdiv = 1 << 30;
2226
2227 for (int i = 0; i < 4; i++) {
2228 if (shadow_atlas->quadrants[i].subdivision) {
2229 shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
2230 }
2231 }
2232
2233 if (shadow_atlas->smallest_subdiv == 1 << 30) {
2234 shadow_atlas->smallest_subdiv = 0;
2235 }
2236
2237 //resort the size orders, simple bublesort for 4 elements..
2238
2239 int swaps = 0;
2240 do {
2241 swaps = 0;
2242
2243 for (int i = 0; i < 3; i++) {
2244 if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
2245 SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
2246 swaps++;
2247 }
2248 }
2249 } while (swaps > 0);
2250 }
2251
2252 bool LightStorage::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
2253 for (int i = p_quadrant_count - 1; i >= 0; i--) {
2254 int qidx = p_in_quadrants[i];
2255
2256 if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
2257 return false;
2258 }
2259
2260 //look for an empty space
2261 int sc = shadow_atlas->quadrants[qidx].shadows.size();
2262 const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
2263
2264 int found_free_idx = -1; //found a free one
2265 int found_used_idx = -1; //found existing one, must steal it
2266 uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
2267
2268 for (int j = 0; j < sc; j++) {
2269 if (!sarr[j].owner.is_valid()) {
2270 found_free_idx = j;
2271 break;
2272 }
2273
2274 LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
2275 ERR_CONTINUE(!sli);
2276
2277 if (sli->last_scene_pass != RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
2278 //was just allocated, don't kill it so soon, wait a bit..
2279 if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
2280 continue;
2281 }
2282
2283 if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
2284 found_used_idx = j;
2285 min_pass = sli->last_scene_pass;
2286 }
2287 }
2288 }
2289
2290 if (found_free_idx == -1 && found_used_idx == -1) {
2291 continue; //nothing found
2292 }
2293
2294 if (found_free_idx == -1 && found_used_idx != -1) {
2295 found_free_idx = found_used_idx;
2296 }
2297
2298 r_quadrant = qidx;
2299 r_shadow = found_free_idx;
2300
2301 return true;
2302 }
2303
2304 return false;
2305 }
2306
2307 bool LightStorage::_shadow_atlas_find_omni_shadows(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
2308 for (int i = p_quadrant_count - 1; i >= 0; i--) {
2309 int qidx = p_in_quadrants[i];
2310
2311 if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
2312 return false;
2313 }
2314
2315 //look for an empty space
2316 int sc = shadow_atlas->quadrants[qidx].shadows.size();
2317 const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
2318
2319 int found_idx = -1;
2320 uint64_t min_pass = 0; // sum of currently selected spots, try to get the least recently used pair
2321
2322 for (int j = 0; j < sc - 1; j++) {
2323 uint64_t pass = 0;
2324
2325 if (sarr[j].owner.is_valid()) {
2326 LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
2327 ERR_CONTINUE(!sli);
2328
2329 if (sli->last_scene_pass == RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
2330 continue;
2331 }
2332
2333 //was just allocated, don't kill it so soon, wait a bit..
2334 if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
2335 continue;
2336 }
2337 pass += sli->last_scene_pass;
2338 }
2339
2340 if (sarr[j + 1].owner.is_valid()) {
2341 LightInstance *sli = light_instance_owner.get_or_null(sarr[j + 1].owner);
2342 ERR_CONTINUE(!sli);
2343
2344 if (sli->last_scene_pass == RendererSceneRenderRD::get_singleton()->get_scene_pass()) {
2345 continue;
2346 }
2347
2348 //was just allocated, don't kill it so soon, wait a bit..
2349 if (p_tick - sarr[j + 1].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
2350 continue;
2351 }
2352 pass += sli->last_scene_pass;
2353 }
2354
2355 if (found_idx == -1 || pass < min_pass) {
2356 found_idx = j;
2357 min_pass = pass;
2358
2359 // we found two empty spots, no need to check the rest
2360 if (pass == 0) {
2361 break;
2362 }
2363 }
2364 }
2365
2366 if (found_idx == -1) {
2367 continue; //nothing found
2368 }
2369
2370 r_quadrant = qidx;
2371 r_shadow = found_idx;
2372
2373 return true;
2374 }
2375
2376 return false;
2377 }
2378
2379 bool LightStorage::shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) {
2380 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
2381 ERR_FAIL_NULL_V(shadow_atlas, false);
2382
2383 LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
2384 ERR_FAIL_NULL_V(li, false);
2385
2386 if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
2387 return false;
2388 }
2389
2390 uint32_t quad_size = shadow_atlas->size >> 1;
2391 int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(uint32_t(quad_size * p_coverage)));
2392
2393 int valid_quadrants[4];
2394 int valid_quadrant_count = 0;
2395 int best_size = -1; //best size found
2396 int best_subdiv = -1; //subdiv for the best size
2397
2398 //find the quadrants this fits into, and the best possible size it can fit into
2399 for (int i = 0; i < 4; i++) {
2400 int q = shadow_atlas->size_order[i];
2401 int sd = shadow_atlas->quadrants[q].subdivision;
2402 if (sd == 0) {
2403 continue; //unused
2404 }
2405
2406 int max_fit = quad_size / sd;
2407
2408 if (best_size != -1 && max_fit > best_size) {
2409 break; //too large
2410 }
2411
2412 valid_quadrants[valid_quadrant_count++] = q;
2413 best_subdiv = sd;
2414
2415 if (max_fit >= desired_fit) {
2416 best_size = max_fit;
2417 }
2418 }
2419
2420 ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
2421
2422 uint64_t tick = OS::get_singleton()->get_ticks_msec();
2423
2424 uint32_t old_key = SHADOW_INVALID;
2425 uint32_t old_quadrant = SHADOW_INVALID;
2426 uint32_t old_shadow = SHADOW_INVALID;
2427 int old_subdivision = -1;
2428
2429 bool should_realloc = false;
2430 bool should_redraw = false;
2431
2432 if (shadow_atlas->shadow_owners.has(p_light_instance)) {
2433 old_key = shadow_atlas->shadow_owners[p_light_instance];
2434 old_quadrant = (old_key >> QUADRANT_SHIFT) & 0x3;
2435 old_shadow = old_key & SHADOW_INDEX_MASK;
2436
2437 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);
2438 should_redraw = shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].version != p_light_version;
2439
2440 if (!should_realloc) {
2441 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = p_light_version;
2442 //already existing, see if it should redraw or it's just OK
2443 return should_redraw;
2444 }
2445
2446 old_subdivision = shadow_atlas->quadrants[old_quadrant].subdivision;
2447 }
2448
2449 bool is_omni = li->light_type == RS::LIGHT_OMNI;
2450 bool found_shadow = false;
2451 int new_quadrant = -1;
2452 int new_shadow = -1;
2453
2454 if (is_omni) {
2455 found_shadow = _shadow_atlas_find_omni_shadows(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, new_quadrant, new_shadow);
2456 } else {
2457 found_shadow = _shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, new_quadrant, new_shadow);
2458 }
2459
2460 if (found_shadow) {
2461 if (old_quadrant != SHADOW_INVALID) {
2462 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = 0;
2463 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].owner = RID();
2464
2465 if (old_key & OMNI_LIGHT_FLAG) {
2466 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow + 1].version = 0;
2467 shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow + 1].owner = RID();
2468 }
2469 }
2470
2471 uint32_t new_key = new_quadrant << QUADRANT_SHIFT;
2472 new_key |= new_shadow;
2473
2474 ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
2475 _shadow_atlas_invalidate_shadow(sh, p_atlas, shadow_atlas, new_quadrant, new_shadow);
2476
2477 sh->owner = p_light_instance;
2478 sh->alloc_tick = tick;
2479 sh->version = p_light_version;
2480
2481 if (is_omni) {
2482 new_key |= OMNI_LIGHT_FLAG;
2483
2484 int new_omni_shadow = new_shadow + 1;
2485 ShadowAtlas::Quadrant::Shadow *extra_sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_omni_shadow];
2486 _shadow_atlas_invalidate_shadow(extra_sh, p_atlas, shadow_atlas, new_quadrant, new_omni_shadow);
2487
2488 extra_sh->owner = p_light_instance;
2489 extra_sh->alloc_tick = tick;
2490 extra_sh->version = p_light_version;
2491 }
2492
2493 li->shadow_atlases.insert(p_atlas);
2494
2495 //update it in map
2496 shadow_atlas->shadow_owners[p_light_instance] = new_key;
2497 //make it dirty, as it should redraw anyway
2498 return true;
2499 }
2500
2501 return should_redraw;
2502 }
2503
2504 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) {
2505 if (p_shadow->owner.is_valid()) {
2506 LightInstance *sli = light_instance_owner.get_or_null(p_shadow->owner);
2507 uint32_t old_key = p_shadow_atlas->shadow_owners[p_shadow->owner];
2508
2509 if (old_key & OMNI_LIGHT_FLAG) {
2510 uint32_t s = old_key & SHADOW_INDEX_MASK;
2511 uint32_t omni_shadow_idx = p_shadow_idx + (s == (uint32_t)p_shadow_idx ? 1 : -1);
2512 ShadowAtlas::Quadrant::Shadow *omni_shadow = &p_shadow_atlas->quadrants[p_quadrant].shadows.write[omni_shadow_idx];
2513 omni_shadow->version = 0;
2514 omni_shadow->owner = RID();
2515 }
2516
2517 p_shadow_atlas->shadow_owners.erase(p_shadow->owner);
2518 p_shadow->version = 0;
2519 p_shadow->owner = RID();
2520 sli->shadow_atlases.erase(p_atlas);
2521 }
2522 }
2523
2524 void LightStorage::shadow_atlas_update(RID p_atlas) {
2525 ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
2526 ERR_FAIL_NULL(shadow_atlas);
2527
2528 _update_shadow_atlas(shadow_atlas);
2529 }
2530
2531 RD::DataFormat LightStorage::get_shadow_atlas_depth_format(bool p_16_bits) {
2532 return p_16_bits ? RD::DATA_FORMAT_D16_UNORM : RD::DATA_FORMAT_D32_SFLOAT;
2533 }
2534
2535 uint32_t LightStorage::get_shadow_atlas_depth_usage_bits() {
2536 return RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
2537 }
2538
2539 /* DIRECTIONAL SHADOW */
2540
2541 void LightStorage::update_directional_shadow_atlas() {
2542 if (directional_shadow.depth.is_null() && directional_shadow.size > 0) {
2543 RD::TextureFormat tf;
2544 tf.format = get_shadow_atlas_depth_format(directional_shadow.use_16_bits);
2545 tf.width = directional_shadow.size;
2546 tf.height = directional_shadow.size;
2547 tf.usage_bits = get_shadow_atlas_depth_usage_bits();
2548
2549 directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
2550 Vector<RID> fb_tex;
2551 fb_tex.push_back(directional_shadow.depth);
2552 directional_shadow.fb = RD::get_singleton()->framebuffer_create(fb_tex);
2553 }
2554 }
2555 void LightStorage::directional_shadow_atlas_set_size(int p_size, bool p_16_bits) {
2556 p_size = nearest_power_of_2_templated(p_size);
2557
2558 if (directional_shadow.size == p_size && directional_shadow.use_16_bits == p_16_bits) {
2559 return;
2560 }
2561
2562 directional_shadow.size = p_size;
2563 directional_shadow.use_16_bits = p_16_bits;
2564
2565 if (directional_shadow.depth.is_valid()) {
2566 RD::get_singleton()->free_rid(directional_shadow.depth);
2567 directional_shadow.depth = RID();
2568 RendererSceneRenderRD::get_singleton()->base_uniforms_changed();
2569 }
2570 }
2571
2572 void LightStorage::set_directional_shadow_count(int p_count) {
2573 directional_shadow.light_count = p_count;
2574 directional_shadow.current_light = 0;
2575 }
2576
2577 static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
2578 int split_h = 1;
2579 int split_v = 1;
2580
2581 while (split_h * split_v < p_shadow_count) {
2582 if (split_h == split_v) {
2583 split_h <<= 1;
2584 } else {
2585 split_v <<= 1;
2586 }
2587 }
2588
2589 Rect2i rect(0, 0, p_size, p_size);
2590 rect.size.width /= split_h;
2591 rect.size.height /= split_v;
2592
2593 rect.position.x = rect.size.width * (p_shadow_index % split_h);
2594 rect.position.y = rect.size.height * (p_shadow_index / split_h);
2595
2596 return rect;
2597 }
2598
2599 Rect2i LightStorage::get_directional_shadow_rect() {
2600 return _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
2601 }
2602
2603 int LightStorage::get_directional_light_shadow_size(RID p_light_instance) {
2604 ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
2605
2606 Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
2607
2608 LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
2609 ERR_FAIL_NULL_V(light_instance, 0);
2610
2611 switch (light_directional_get_shadow_mode(light_instance->light)) {
2612 case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
2613 break; //none
2614 case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
2615 r.size.height /= 2;
2616 break;
2617 case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
2618 r.size /= 2;
2619 break;
2620 }
2621
2622 return MAX(r.size.width, r.size.height);
2623 }
2624
2625 /* SHADOW CUBEMAPS */
2626
2627 LightStorage::ShadowCubemap *LightStorage::_get_shadow_cubemap(int p_size) {
2628 if (!shadow_cubemaps.has(p_size)) {
2629 ShadowCubemap sc;
2630 {
2631 RD::TextureFormat tf;
2632 tf.format = get_cubemap_depth_format();
2633 tf.width = p_size;
2634 tf.height = p_size;
2635 tf.texture_type = RD::TEXTURE_TYPE_CUBE;
2636 tf.array_layers = 6;
2637 tf.usage_bits = get_cubemap_depth_usage_bits();
2638 sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
2639 }
2640
2641 for (int i = 0; i < 6; i++) {
2642 RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
2643 Vector<RID> fbtex;
2644 fbtex.push_back(side_texture);
2645 sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
2646 }
2647
2648 shadow_cubemaps[p_size] = sc;
2649 }
2650
2651 return &shadow_cubemaps[p_size];
2652 }
2653
2654 RID LightStorage::get_cubemap(int p_size) {
2655 ShadowCubemap *cubemap = _get_shadow_cubemap(p_size);
2656
2657 return cubemap->cubemap;
2658 }
2659
2660 RID LightStorage::get_cubemap_fb(int p_size, int p_pass) {
2661 ShadowCubemap *cubemap = _get_shadow_cubemap(p_size);
2662
2663 return cubemap->side_fb[p_pass];
2664 }
2665
2666 RD::DataFormat LightStorage::get_cubemap_depth_format() {
2667 return RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
2668 }
2669
2670 uint32_t LightStorage::get_cubemap_depth_usage_bits() {
2671 return RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
2672 }
2673
2674 bool LightStorage::get_shadow_cubemaps_used() const {
2675 return shadow_cubemaps_used;
2676 }
2677
2678 bool LightStorage::get_shadow_dual_paraboloid_used() const {
2679 return shadow_dual_paraboloid_used;
2680 }