-
-
Notifications
You must be signed in to change notification settings - Fork 22k
/
Copy pathrenderer_scene_cull.h
1441 lines (1159 loc) · 50.1 KB
/
renderer_scene_cull.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* renderer_scene_cull.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/math/dynamic_bvh.h"
#include "core/math/transform_interpolator.h"
#include "core/templates/bin_sorted_array.h"
#include "core/templates/local_vector.h"
#include "core/templates/paged_allocator.h"
#include "core/templates/paged_array.h"
#include "core/templates/pass_func.h"
#include "core/templates/rid_owner.h"
#include "core/templates/self_list.h"
#include "servers/rendering/instance_uniforms.h"
#include "servers/rendering/renderer_scene_occlusion_cull.h"
#include "servers/rendering/renderer_scene_render.h"
#include "servers/rendering/rendering_method.h"
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering/storage/utilities.h"
class RenderingLightCuller;
class RendererSceneCull : public RenderingMethod {
public:
RendererSceneRender *scene_render = nullptr;
enum {
SDFGI_MAX_CASCADES = 8,
SDFGI_MAX_REGIONS_PER_CASCADE = 3,
MAX_INSTANCE_PAIRS = 32,
MAX_UPDATE_SHADOWS = 512
};
uint64_t render_pass;
static RendererSceneCull *singleton;
/* EVENT QUEUING */
void tick();
void pre_draw(bool p_will_draw);
/* CAMERA API */
struct Camera {
enum Type {
PERSPECTIVE,
ORTHOGONAL,
FRUSTUM
};
Type type;
float fov;
float znear, zfar;
float size;
Vector2 offset;
uint32_t visible_layers;
bool vaspect;
RID env;
RID attributes;
RID compositor;
Transform3D transform;
Camera() {
visible_layers = 0xFFFFFFFF;
fov = 75;
type = PERSPECTIVE;
znear = 0.05;
zfar = 4000;
size = 1.0;
offset = Vector2();
vaspect = false;
}
};
mutable RID_Owner<Camera, true> camera_owner;
virtual RID camera_allocate();
virtual void camera_initialize(RID p_rid);
virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
virtual void camera_set_transform(RID p_camera, const Transform3D &p_transform);
virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
virtual void camera_set_environment(RID p_camera, RID p_env);
virtual void camera_set_camera_attributes(RID p_camera, RID p_attributes);
virtual void camera_set_compositor(RID p_camera, RID p_compositor);
virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
virtual bool is_camera(RID p_camera) const;
/* OCCLUDER API */
virtual RID occluder_allocate();
virtual void occluder_initialize(RID p_occluder);
virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices);
/* VISIBILITY NOTIFIER API */
RendererSceneOcclusionCull *dummy_occlusion_culling = nullptr;
/* SCENARIO API */
struct Instance;
struct PlaneSign {
_ALWAYS_INLINE_ PlaneSign() {}
_ALWAYS_INLINE_ PlaneSign(const Plane &p_plane) {
if (p_plane.normal.x > 0) {
signs[0] = 0;
} else {
signs[0] = 3;
}
if (p_plane.normal.y > 0) {
signs[1] = 1;
} else {
signs[1] = 4;
}
if (p_plane.normal.z > 0) {
signs[2] = 2;
} else {
signs[2] = 5;
}
}
uint32_t signs[3];
};
struct Frustum {
Vector<Plane> planes;
Vector<PlaneSign> plane_signs;
const Plane *planes_ptr;
const PlaneSign *plane_signs_ptr;
uint32_t plane_count;
_ALWAYS_INLINE_ Frustum() {}
_ALWAYS_INLINE_ Frustum(const Frustum &p_frustum) {
planes = p_frustum.planes;
plane_signs = p_frustum.plane_signs;
planes_ptr = planes.ptr();
plane_signs_ptr = plane_signs.ptr();
plane_count = p_frustum.plane_count;
}
_ALWAYS_INLINE_ void operator=(const Frustum &p_frustum) {
planes = p_frustum.planes;
plane_signs = p_frustum.plane_signs;
planes_ptr = planes.ptr();
plane_signs_ptr = plane_signs.ptr();
plane_count = p_frustum.plane_count;
}
_ALWAYS_INLINE_ Frustum(const Vector<Plane> &p_planes) {
planes = p_planes;
planes_ptr = planes.ptrw();
plane_count = planes.size();
for (int i = 0; i < planes.size(); i++) {
PlaneSign ps(p_planes[i]);
plane_signs.push_back(ps);
}
plane_signs_ptr = plane_signs.ptr();
}
};
struct InstanceBounds {
// Efficiently store instance bounds.
// Because bounds checking is performed first,
// keep it separated from data.
real_t bounds[6];
_ALWAYS_INLINE_ InstanceBounds() {}
_ALWAYS_INLINE_ InstanceBounds(const AABB &p_aabb) {
bounds[0] = p_aabb.position.x;
bounds[1] = p_aabb.position.y;
bounds[2] = p_aabb.position.z;
bounds[3] = p_aabb.position.x + p_aabb.size.x;
bounds[4] = p_aabb.position.y + p_aabb.size.y;
bounds[5] = p_aabb.position.z + p_aabb.size.z;
}
_ALWAYS_INLINE_ bool in_frustum(const Frustum &p_frustum) const {
// This is not a full SAT check and the possibility of false positives exist,
// but the tradeoff vs performance is still very good.
for (uint32_t i = 0; i < p_frustum.plane_count; i++) {
Vector3 min(
bounds[p_frustum.plane_signs_ptr[i].signs[0]],
bounds[p_frustum.plane_signs_ptr[i].signs[1]],
bounds[p_frustum.plane_signs_ptr[i].signs[2]]);
if (p_frustum.planes_ptr[i].distance_to(min) >= 0.0) {
return false;
}
}
return true;
}
_ALWAYS_INLINE_ bool in_aabb(const AABB &p_aabb) const {
Vector3 end = p_aabb.position + p_aabb.size;
if (bounds[0] >= end.x) {
return false;
}
if (bounds[3] <= p_aabb.position.x) {
return false;
}
if (bounds[1] >= end.y) {
return false;
}
if (bounds[4] <= p_aabb.position.y) {
return false;
}
if (bounds[2] >= end.z) {
return false;
}
if (bounds[5] <= p_aabb.position.z) {
return false;
}
return true;
}
};
struct InstanceVisibilityNotifierData;
struct InstanceData {
// Store instance pointer as well as common instance processing information,
// to make processing more cache friendly.
enum Flags : uint32_t {
FLAG_BASE_TYPE_MASK = 0xFF,
FLAG_CAST_SHADOWS = (1 << 8),
FLAG_CAST_SHADOWS_ONLY = (1 << 9),
FLAG_REDRAW_IF_VISIBLE = (1 << 10),
FLAG_GEOM_LIGHTING_DIRTY = (1 << 11),
FLAG_GEOM_REFLECTION_DIRTY = (1 << 12),
FLAG_GEOM_DECAL_DIRTY = (1 << 13),
FLAG_GEOM_VOXEL_GI_DIRTY = (1 << 14),
FLAG_LIGHTMAP_CAPTURE = (1 << 15),
FLAG_USES_BAKED_LIGHT = (1 << 16),
FLAG_USES_MESH_INSTANCE = (1 << 17),
FLAG_REFLECTION_PROBE_DIRTY = (1 << 18),
FLAG_IGNORE_OCCLUSION_CULLING = (1 << 19),
FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK = (3 << 20), // 2 bits, overlaps with the other vis. dependency flags
FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE = (1 << 20),
FLAG_VISIBILITY_DEPENDENCY_HIDDEN = (1 << 21),
FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN = (1 << 22),
FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY = (1 << 23),
FLAG_IGNORE_ALL_CULLING = (1 << 24),
};
uint32_t flags = 0;
uint32_t layer_mask = 0; //for fast layer-mask discard
RID base_rid;
union {
uint64_t instance_data_rid;
RenderGeometryInstance *instance_geometry;
InstanceVisibilityNotifierData *visibility_notifier = nullptr;
};
Instance *instance = nullptr;
int32_t parent_array_index = -1;
int32_t visibility_index = -1;
// Each time occlusion culling determines an instance is visible,
// set this to occlusion_frame plus some delay.
// Once the timeout is reached, allow the instance to be occlusion culled.
// This creates a delay for occlusion culling, which prevents flickering
// when jittering the raster occlusion projection.
uint64_t occlusion_timeout = 0;
};
struct InstanceVisibilityData {
uint64_t viewport_state = 0;
int32_t array_index = -1;
RS::VisibilityRangeFadeMode fade_mode = RS::VISIBILITY_RANGE_FADE_DISABLED;
Vector3 position;
Instance *instance = nullptr;
float range_begin = 0.0f;
float range_end = 0.0f;
float range_begin_margin = 0.0f;
float range_end_margin = 0.0f;
float children_fade_alpha = 1.0f;
};
class VisibilityArray : public BinSortedArray<InstanceVisibilityData> {
_FORCE_INLINE_ virtual void _update_idx(InstanceVisibilityData &r_element, uint64_t p_idx) {
r_element.instance->visibility_index = p_idx;
if (r_element.instance->scenario && r_element.instance->array_index != -1) {
r_element.instance->scenario->instance_data[r_element.instance->array_index].visibility_index = p_idx;
}
}
};
PagedArrayPool<InstanceBounds> instance_aabb_page_pool;
PagedArrayPool<InstanceData> instance_data_page_pool;
PagedArrayPool<InstanceVisibilityData> instance_visibility_data_page_pool;
struct Scenario {
enum IndexerType {
INDEXER_GEOMETRY, //for geometry
INDEXER_VOLUMES, //for everything else
INDEXER_MAX
};
DynamicBVH indexers[INDEXER_MAX];
RID self;
List<Instance *> directional_lights;
RID environment;
RID fallback_environment;
RID camera_attributes;
RID compositor;
RID reflection_probe_shadow_atlas;
RID reflection_atlas;
uint64_t used_viewport_visibility_bits;
HashMap<RID, uint64_t> viewport_visibility_masks;
SelfList<Instance>::List instances;
LocalVector<RID> dynamic_lights;
PagedArray<InstanceBounds> instance_aabbs;
PagedArray<InstanceData> instance_data;
VisibilityArray instance_visibility;
Scenario() {
indexers[INDEXER_GEOMETRY].set_index(INDEXER_GEOMETRY);
indexers[INDEXER_VOLUMES].set_index(INDEXER_VOLUMES);
used_viewport_visibility_bits = 0;
}
};
int indexer_update_iterations = 0;
mutable RID_Owner<Scenario, true> scenario_owner;
static void _instance_pair(Instance *p_A, Instance *p_B);
static void _instance_unpair(Instance *p_A, Instance *p_B);
void _instance_update_mesh_instance(Instance *p_instance) const;
virtual RID scenario_allocate();
virtual void scenario_initialize(RID p_rid);
virtual void scenario_set_environment(RID p_scenario, RID p_environment);
virtual void scenario_set_camera_attributes(RID p_scenario, RID p_attributes);
virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
virtual void scenario_set_compositor(RID p_scenario, RID p_compositor);
virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_reflection_size, int p_reflection_count);
virtual bool is_scenario(RID p_scenario) const;
virtual RID scenario_get_environment(RID p_scenario);
virtual void scenario_add_viewport_visibility_mask(RID p_scenario, RID p_viewport);
virtual void scenario_remove_viewport_visibility_mask(RID p_scenario, RID p_viewport);
/* INSTANCING API */
struct InstancePair {
Instance *a = nullptr;
Instance *b = nullptr;
SelfList<InstancePair> list_a;
SelfList<InstancePair> list_b;
InstancePair() :
list_a(this), list_b(this) {}
};
mutable PagedAllocator<InstancePair> pair_allocator;
struct InstanceBaseData {
virtual ~InstanceBaseData() {}
};
struct Instance {
RS::InstanceType base_type;
RID base;
RID skeleton;
RID material_override;
RID material_overlay;
RID mesh_instance; //only used for meshes and when skeleton/blendshapes exist
// This is the main transform to be drawn with ...
// This will either be the interpolated transform (when using fixed timestep interpolation)
// or the ONLY transform (when not using FTI).
Transform3D transform;
// For interpolation we store the current transform (this physics tick)
// and the transform in the previous tick.
Transform3D transform_curr;
Transform3D transform_prev;
float lod_bias;
bool ignore_occlusion_culling;
bool ignore_all_culling;
Vector<RID> materials;
RS::ShadowCastingSetting cast_shadows;
uint32_t layer_mask;
// Fit in 32 bits.
bool mirror : 1;
bool receive_shadows : 1;
bool visible : 1;
bool baked_light : 1; // This flag is only to know if it actually did use baked light.
bool dynamic_gi : 1; // Same as above for dynamic objects.
bool redraw_if_visible : 1;
bool on_interpolate_list : 1;
bool on_interpolate_transform_list : 1;
bool interpolated : 1;
TransformInterpolator::Method interpolation_method : 3;
// For fixed timestep interpolation.
// Note 32 bits is plenty for checksum, no need for real_t
float transform_checksum_curr;
float transform_checksum_prev;
Instance *lightmap = nullptr;
Rect2 lightmap_uv_scale;
int lightmap_slice_index;
uint32_t lightmap_cull_index;
Vector<Color> lightmap_sh; //spherical harmonic
AABB aabb;
AABB transformed_aabb;
AABB prev_transformed_aabb;
InstanceUniforms instance_uniforms;
//
RID self;
//scenario stuff
DynamicBVH::ID indexer_id;
int32_t array_index = -1;
int32_t visibility_index = -1;
float visibility_range_begin = 0.0f;
float visibility_range_end = 0.0f;
float visibility_range_begin_margin = 0.0f;
float visibility_range_end_margin = 0.0f;
RS::VisibilityRangeFadeMode visibility_range_fade_mode = RS::VISIBILITY_RANGE_FADE_DISABLED;
Instance *visibility_parent = nullptr;
HashSet<Instance *> visibility_dependencies;
uint32_t visibility_dependencies_depth = 0;
float transparency = 0.0f;
Scenario *scenario = nullptr;
SelfList<Instance> scenario_item;
//aabb stuff
bool update_aabb;
bool update_dependencies;
SelfList<Instance> update_item;
AABB *custom_aabb = nullptr; // <Zylann> would using aabb directly with a bool be better?
float extra_margin;
ObjectID object_id;
// sorting
float sorting_offset = 0.0;
bool use_aabb_center = true;
Vector<Color> lightmap_target_sh; //target is used for incrementally changing the SH over time, this avoids pops in some corner cases and when going interior <-> exterior
uint64_t last_frame_pass;
uint64_t version; // changes to this, and changes to base increase version
InstanceBaseData *base_data = nullptr;
SelfList<InstancePair>::List pairs;
uint64_t pair_check;
DependencyTracker dependency_tracker;
static void dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *tracker) {
Instance *instance = (Instance *)tracker->userdata;
switch (p_notification) {
case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA:
case Dependency::DEPENDENCY_CHANGED_SKELETON_BONES:
case Dependency::DEPENDENCY_CHANGED_AABB: {
singleton->_instance_queue_update(instance, true, false);
} break;
case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES:
case Dependency::DEPENDENCY_CHANGED_MATERIAL: {
singleton->_instance_queue_update(instance, false, true);
} break;
case Dependency::DEPENDENCY_CHANGED_MESH:
case Dependency::DEPENDENCY_CHANGED_PARTICLES:
case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
case Dependency::DEPENDENCY_CHANGED_DECAL:
case Dependency::DEPENDENCY_CHANGED_LIGHT:
case Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE: {
singleton->_instance_queue_update(instance, true, true);
} break;
case Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR: {
//requires repairing
if (instance->indexer_id.is_valid()) {
singleton->_unpair_instance(instance);
singleton->_instance_queue_update(instance, true, true);
}
} break;
default: {
// Ignored notifications.
} break;
}
}
static void dependency_deleted(const RID &p_dependency, DependencyTracker *tracker) {
Instance *instance = (Instance *)tracker->userdata;
if (p_dependency == instance->base) {
singleton->instance_set_base(instance->self, RID());
} else if (p_dependency == instance->skeleton) {
singleton->instance_attach_skeleton(instance->self, RID());
} else {
// It's possible the same material is used in multiple slots,
// so we check whether we need to clear them all.
if (p_dependency == instance->material_override) {
singleton->instance_geometry_set_material_override(instance->self, RID());
}
if (p_dependency == instance->material_overlay) {
singleton->instance_geometry_set_material_overlay(instance->self, RID());
}
for (int i = 0; i < instance->materials.size(); i++) {
if (p_dependency == instance->materials[i]) {
singleton->instance_set_surface_override_material(instance->self, i, RID());
}
}
if (instance->base_type == RS::INSTANCE_PARTICLES) {
RID particle_material = RSG::particles_storage->particles_get_process_material(instance->base);
if (p_dependency == particle_material) {
RSG::particles_storage->particles_set_process_material(instance->base, RID());
}
}
// Even if no change is made we still need to call `_instance_queue_update`.
// This dependency could also be a result of the freed material being used
// by the mesh this mesh instance uses.
singleton->_instance_queue_update(instance, false, true);
}
}
Instance() :
scenario_item(this),
update_item(this) {
base_type = RS::INSTANCE_NONE;
cast_shadows = RS::SHADOW_CASTING_SETTING_ON;
receive_shadows = true;
visible = true;
layer_mask = 1;
baked_light = true;
dynamic_gi = false;
redraw_if_visible = false;
on_interpolate_list = false;
on_interpolate_transform_list = false;
interpolated = true;
interpolation_method = TransformInterpolator::INTERP_LERP;
transform_checksum_curr = 0.0;
transform_checksum_prev = 0.0;
lightmap_slice_index = 0;
lightmap = nullptr;
lightmap_cull_index = 0;
lod_bias = 1.0;
ignore_occlusion_culling = false;
ignore_all_culling = false;
scenario = nullptr;
update_aabb = false;
update_dependencies = false;
extra_margin = 0;
visible = true;
visibility_range_begin = 0;
visibility_range_end = 0;
visibility_range_begin_margin = 0;
visibility_range_end_margin = 0;
last_frame_pass = 0;
version = 1;
base_data = nullptr;
custom_aabb = nullptr;
pair_check = 0;
array_index = -1;
dependency_tracker.userdata = this;
dependency_tracker.changed_callback = dependency_changed;
dependency_tracker.deleted_callback = dependency_deleted;
}
~Instance() {
if (base_data) {
memdelete(base_data);
}
if (custom_aabb) {
memdelete(custom_aabb);
}
}
};
mutable SelfList<Instance>::List _instance_update_list;
void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies = false) const;
struct InstanceGeometryData : public InstanceBaseData {
RenderGeometryInstance *geometry_instance = nullptr;
HashSet<Instance *> lights;
bool can_cast_shadows;
bool material_is_animated;
uint32_t projector_count = 0;
uint32_t softshadow_count = 0;
HashSet<Instance *> decals;
HashSet<Instance *> reflection_probes;
HashSet<Instance *> voxel_gi_instances;
HashSet<Instance *> lightmap_captures;
InstanceGeometryData() {
can_cast_shadows = true;
material_is_animated = true;
}
};
struct InstanceReflectionProbeData : public InstanceBaseData {
Instance *owner = nullptr;
HashSet<Instance *> geometries;
RID instance;
SelfList<InstanceReflectionProbeData> update_list;
int render_step;
InstanceReflectionProbeData() :
update_list(this) {
render_step = -1;
}
};
struct InstanceDecalData : public InstanceBaseData {
Instance *owner = nullptr;
RID instance;
HashSet<Instance *> geometries;
InstanceDecalData() {
}
};
SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
struct InstanceParticlesCollisionData : public InstanceBaseData {
RID instance;
};
struct InstanceFogVolumeData : public InstanceBaseData {
RID instance;
bool is_global;
};
struct InstanceVisibilityNotifierData : public InstanceBaseData {
bool just_visible = false;
uint64_t visible_in_frame = 0;
RID base;
SelfList<InstanceVisibilityNotifierData> list_element;
InstanceVisibilityNotifierData() :
list_element(this) {}
};
SpinLock visible_notifier_list_lock;
SelfList<InstanceVisibilityNotifierData>::List visible_notifier_list;
struct InstanceLightData : public InstanceBaseData {
RID instance;
uint64_t last_version;
List<Instance *>::Element *D; // directional light in scenario
bool uses_projector = false;
bool uses_softshadow = false;
HashSet<Instance *> geometries;
Instance *baked_light = nullptr;
RS::LightBakeMode bake_mode;
uint32_t max_sdfgi_cascade = 2;
private:
// Instead of a single dirty flag, we maintain a count
// so that we can detect lights that are being made dirty
// each frame, and switch on tighter caster culling.
int32_t shadow_dirty_count;
uint32_t light_update_frame_id;
bool light_intersects_multiple_cameras;
uint32_t light_intersects_multiple_cameras_timeout_frame_id;
public:
bool is_shadow_dirty() const { return shadow_dirty_count != 0; }
void make_shadow_dirty() { shadow_dirty_count = light_intersects_multiple_cameras ? 1 : 2; }
void detect_light_intersects_multiple_cameras(uint32_t p_frame_id) {
// We need to detect the case where shadow updates are occurring
// more than once per frame. In this case, we need to turn off
// tighter caster culling, so situation reverts to one full shadow update
// per frame (light_intersects_multiple_cameras is set).
if (p_frame_id == light_update_frame_id) {
light_intersects_multiple_cameras = true;
light_intersects_multiple_cameras_timeout_frame_id = p_frame_id + 60;
} else {
// When shadow_volume_intersects_multiple_cameras is set, we
// want to detect the situation this is no longer the case, via a timeout.
// The system can go back to tighter caster culling in this situation.
// Having a long-ish timeout prevents rapid cycling.
if (light_intersects_multiple_cameras && (p_frame_id >= light_intersects_multiple_cameras_timeout_frame_id)) {
light_intersects_multiple_cameras = false;
light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
}
}
light_update_frame_id = p_frame_id;
}
void decrement_shadow_dirty() {
shadow_dirty_count--;
DEV_ASSERT(shadow_dirty_count >= 0);
}
// Shadow updates can either full (everything in the shadow volume)
// or closely culled to the camera frustum.
bool is_shadow_update_full() const { return shadow_dirty_count == 0; }
InstanceLightData() {
bake_mode = RS::LIGHT_BAKE_DISABLED;
D = nullptr;
last_version = 0;
baked_light = nullptr;
shadow_dirty_count = 1;
light_update_frame_id = UINT32_MAX;
light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
light_intersects_multiple_cameras = false;
}
};
struct InstanceVoxelGIData : public InstanceBaseData {
Instance *owner = nullptr;
HashSet<Instance *> geometries;
HashSet<Instance *> dynamic_geometries;
HashSet<Instance *> lights;
struct LightCache {
RS::LightType type;
Transform3D transform;
Color color;
float energy;
float intensity;
float bake_energy;
float radius;
float attenuation;
float spot_angle;
float spot_attenuation;
bool has_shadow;
RS::LightDirectionalSkyMode sky_mode;
};
Vector<LightCache> light_cache;
Vector<RID> light_instances;
RID probe_instance;
bool invalid;
uint32_t base_version;
SelfList<InstanceVoxelGIData> update_element;
InstanceVoxelGIData() :
update_element(this) {
invalid = true;
base_version = 0;
}
};
SelfList<InstanceVoxelGIData>::List voxel_gi_update_list;
struct InstanceLightmapData : public InstanceBaseData {
RID instance;
HashSet<Instance *> geometries;
HashSet<Instance *> users;
InstanceLightmapData() {
}
};
mutable uint64_t pair_pass = 1;
struct PairInstances {
Instance *instance = nullptr;
PagedAllocator<InstancePair> *pair_allocator = nullptr;
SelfList<InstancePair>::List pairs_found;
DynamicBVH *bvh = nullptr;
DynamicBVH *bvh2 = nullptr; //some may need to cull in two
uint32_t pair_mask;
uint64_t pair_pass;
uint32_t cull_mask = 0xFFFFFFFF; // Needed for decals and lights in the mobile and compatibility renderers.
_FORCE_INLINE_ bool operator()(void *p_data) {
Instance *p_instance = (Instance *)p_data;
if (instance != p_instance && instance->transformed_aabb.intersects(p_instance->transformed_aabb) && (pair_mask & (1 << p_instance->base_type)) && (cull_mask & p_instance->layer_mask)) {
//test is more coarse in indexer
p_instance->pair_check = pair_pass;
InstancePair *pair = pair_allocator->alloc();
pair->a = instance;
pair->b = p_instance;
pairs_found.add(&pair->list_a);
}
return false;
}
void pair() {
if (bvh) {
bvh->aabb_query(instance->transformed_aabb, *this);
}
if (bvh2) {
bvh2->aabb_query(instance->transformed_aabb, *this);
}
while (instance->pairs.first()) {
InstancePair *pair = instance->pairs.first()->self();
Instance *other_instance = instance == pair->a ? pair->b : pair->a;
if (other_instance->pair_check != pair_pass) {
//unpaired
_instance_unpair(instance, other_instance);
} else {
//kept
other_instance->pair_check = 0; // if kept, then put pair check to zero, so we can distinguish with the newly added ones
}
pair_allocator->free(pair);
}
while (pairs_found.first()) {
InstancePair *pair = pairs_found.first()->self();
pairs_found.remove(pairs_found.first());
if (pair->b->pair_check == pair_pass) {
//paired
_instance_pair(instance, pair->b);
}
pair->a->pairs.add(&pair->list_a);
pair->b->pairs.add(&pair->list_b);
}
}
};
mutable HashSet<Instance *> heightfield_particle_colliders_update_list;
PagedArrayPool<Instance *> instance_cull_page_pool;
PagedArrayPool<RenderGeometryInstance *> geometry_instance_cull_page_pool;
PagedArrayPool<RID> rid_cull_page_pool;
PagedArray<Instance *> instance_cull_result;
PagedArray<Instance *> instance_shadow_cull_result;
struct InstanceCullResult {
PagedArray<RenderGeometryInstance *> geometry_instances;
PagedArray<Instance *> lights;
PagedArray<RID> light_instances;
PagedArray<RID> lightmaps;
PagedArray<RID> reflections;
PagedArray<RID> decals;
PagedArray<RID> voxel_gi_instances;
PagedArray<RID> mesh_instances;
PagedArray<RID> fog_volumes;
struct DirectionalShadow {
PagedArray<RenderGeometryInstance *> cascade_geometry_instances[RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES];
} directional_shadows[RendererSceneRender::MAX_DIRECTIONAL_LIGHTS];
PagedArray<RenderGeometryInstance *> sdfgi_region_geometry_instances[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE];
PagedArray<RID> sdfgi_cascade_lights[SDFGI_MAX_CASCADES];
void clear() {
geometry_instances.clear();
lights.clear();
light_instances.clear();
lightmaps.clear();
reflections.clear();
decals.clear();
voxel_gi_instances.clear();
mesh_instances.clear();
fog_volumes.clear();
for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
directional_shadows[i].cascade_geometry_instances[j].clear();
}
}
for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
sdfgi_region_geometry_instances[i].clear();
}
for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
sdfgi_cascade_lights[i].clear();
}
}
void reset() {
geometry_instances.reset();
lights.reset();
light_instances.reset();
lightmaps.reset();
reflections.reset();
decals.reset();
voxel_gi_instances.reset();
mesh_instances.reset();
fog_volumes.reset();
for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
directional_shadows[i].cascade_geometry_instances[j].reset();
}
}
for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
sdfgi_region_geometry_instances[i].reset();
}
for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
sdfgi_cascade_lights[i].reset();
}
}
void append_from(InstanceCullResult &p_cull_result) {
geometry_instances.merge_unordered(p_cull_result.geometry_instances);
lights.merge_unordered(p_cull_result.lights);
light_instances.merge_unordered(p_cull_result.light_instances);
lightmaps.merge_unordered(p_cull_result.lightmaps);
reflections.merge_unordered(p_cull_result.reflections);
decals.merge_unordered(p_cull_result.decals);
voxel_gi_instances.merge_unordered(p_cull_result.voxel_gi_instances);
mesh_instances.merge_unordered(p_cull_result.mesh_instances);
fog_volumes.merge_unordered(p_cull_result.fog_volumes);
for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
directional_shadows[i].cascade_geometry_instances[j].merge_unordered(p_cull_result.directional_shadows[i].cascade_geometry_instances[j]);
}
}
for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
sdfgi_region_geometry_instances[i].merge_unordered(p_cull_result.sdfgi_region_geometry_instances[i]);
}
for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
sdfgi_cascade_lights[i].merge_unordered(p_cull_result.sdfgi_cascade_lights[i]);
}
}
void init(PagedArrayPool<RID> *p_rid_pool, PagedArrayPool<RenderGeometryInstance *> *p_geometry_instance_pool, PagedArrayPool<Instance *> *p_instance_pool) {
geometry_instances.set_page_pool(p_geometry_instance_pool);
light_instances.set_page_pool(p_rid_pool);
lights.set_page_pool(p_instance_pool);