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