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