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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
|
/*************************************************************************/
/* renderer_scene_cull.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef RENDERING_SERVER_SCENE_CULL_H
#define RENDERING_SERVER_SCENE_CULL_H
#include "core/templates/pass_func.h"
#include "servers/rendering/renderer_compositor.h"
#include "core/math/dynamic_bvh.h"
#include "core/math/geometry_3d.h"
#include "core/math/octree.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"
#include "core/templates/local_vector.h"
#include "core/templates/paged_allocator.h"
#include "core/templates/paged_array.h"
#include "core/templates/rid_owner.h"
#include "core/templates/self_list.h"
#include "servers/rendering/renderer_scene.h"
#include "servers/rendering/renderer_scene_occlusion_cull.h"
#include "servers/rendering/renderer_scene_render.h"
#include "servers/xr/xr_interface.h"
class RendererSceneCull : public RendererScene {
public:
RendererSceneRender *scene_render;
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;
/* 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 effects;
Transform3D transform;
Camera() {
visible_layers = 0xFFFFFFFF;
fov = 75;
type = PERSPECTIVE;
znear = 0.05;
zfar = 4000;
size = 1.0;
offset = Vector2();
vaspect = false;
}
};
mutable RID_PtrOwner<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_effects(RID p_camera, RID p_fx);
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);
RendererSceneOcclusionCull *dummy_occlusion_culling;
/* 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 InstanceData {
// Store instance pointer as well as common instance processing information,
// to make processing more cache friendly.
enum Flags {
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_GI_PROBE_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),
};
uint32_t flags = 0;
uint32_t layer_mask = 0; //for fast layer-mask discard
RID base_rid;
union {
uint64_t instance_data_rid;
RendererSceneRender::GeometryInstance *instance_geometry;
};
Instance *instance = nullptr;
};
PagedArrayPool<InstanceBounds> instance_aabb_page_pool;
PagedArrayPool<InstanceData> instance_data_page_pool;
struct Scenario {
enum IndexerType {
INDEXER_GEOMETRY, //for geometry
INDEXER_VOLUMES, //for everything else
INDEXER_MAX
};
DynamicBVH indexers[INDEXER_MAX];
RS::ScenarioDebugMode debug;
RID self;
List<Instance *> directional_lights;
RID environment;
RID fallback_environment;
RID camera_effects;
RID reflection_probe_shadow_atlas;
RID reflection_atlas;
SelfList<Instance>::List instances;
LocalVector<RID> dynamic_lights;
PagedArray<InstanceBounds> instance_aabbs;
PagedArray<InstanceData> instance_data;
Scenario() {
indexers[INDEXER_GEOMETRY].set_index(INDEXER_GEOMETRY);
indexers[INDEXER_VOLUMES].set_index(INDEXER_VOLUMES);
debug = RS::SCENARIO_DEBUG_DISABLED;
}
};
int indexer_update_iterations = 0;
mutable RID_PtrOwner<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);
virtual RID scenario_allocate();
virtual void scenario_initialize(RID p_rid);
virtual void scenario_set_debug(RID p_scenario, RS::ScenarioDebugMode p_debug_mode);
virtual void scenario_set_environment(RID p_scenario, RID p_environment);
virtual void scenario_set_camera_effects(RID p_scenario, RID p_fx);
virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
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);
/* INSTANCING API */
struct InstancePair {
Instance *a;
Instance *b;
SelfList<InstancePair> list_a;
SelfList<InstancePair> list_b;
InstancePair() :
list_a(this), list_b(this) {}
};
PagedAllocator<InstancePair> pair_allocator;
struct InstanceBaseData {
virtual ~InstanceBaseData() {}
};
struct Instance {
RS::InstanceType base_type;
RID base;
RID skeleton;
RID material_override;
RID mesh_instance; //only used for meshes and when skeleton/blendshapes exist
Transform3D transform;
float lod_bias;
bool ignore_occlusion_culling;
Vector<RID> materials;
RS::ShadowCastingSetting cast_shadows;
uint32_t layer_mask;
//fit in 32 bits
bool mirror : 8;
bool receive_shadows : 8;
bool visible : 8;
bool baked_light : 2; //this flag is only to know if it actually did use baked light
bool dynamic_gi : 2; //same above for dynamic objects
bool redraw_if_visible : 4;
Instance *lightmap;
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;
struct InstanceShaderParameter {
int32_t index = -1;
Variant value;
Variant default_value;
PropertyInfo info;
};
Map<StringName, InstanceShaderParameter> instance_shader_parameters;
bool instance_allocated_shader_parameters = false;
int32_t instance_allocated_shader_parameters_offset = -1;
//
RID self;
//scenario stuff
DynamicBVH::ID indexer_id;
int32_t array_index;
Scenario *scenario;
SelfList<Instance> scenario_item;
//aabb stuff
bool update_aabb;
bool update_dependencies;
SelfList<Instance> update_item;
AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
float extra_margin;
ObjectID object_id;
float lod_begin;
float lod_end;
float lod_begin_hysteresis;
float lod_end_hysteresis;
RID lod_instance;
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;
SelfList<InstancePair>::List pairs;
uint64_t pair_check;
RendererStorage::DependencyTracker dependency_tracker;
static void dependency_changed(RendererStorage::DependencyChangedNotification p_notification, RendererStorage::DependencyTracker *tracker) {
Instance *instance = (Instance *)tracker->userdata;
switch (p_notification) {
case RendererStorage::DEPENDENCY_CHANGED_SKELETON_DATA:
case RendererStorage::DEPENDENCY_CHANGED_AABB: {
singleton->_instance_queue_update(instance, true, false);
} break;
case RendererStorage::DEPENDENCY_CHANGED_MATERIAL: {
singleton->_instance_queue_update(instance, false, true);
} break;
case RendererStorage::DEPENDENCY_CHANGED_MESH:
case RendererStorage::DEPENDENCY_CHANGED_PARTICLES:
case RendererStorage::DEPENDENCY_CHANGED_MULTIMESH:
case RendererStorage::DEPENDENCY_CHANGED_DECAL:
case RendererStorage::DEPENDENCY_CHANGED_LIGHT:
case RendererStorage::DEPENDENCY_CHANGED_REFLECTION_PROBE: {
singleton->_instance_queue_update(instance, true, true);
} break;
case RendererStorage::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES:
case RendererStorage::DEPENDENCY_CHANGED_SKELETON_BONES: {
//ignored
} break;
}
}
static void dependency_deleted(const RID &p_dependency, RendererStorage::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 {
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 = false;
dynamic_gi = false;
redraw_if_visible = false;
lightmap_slice_index = 0;
lightmap = nullptr;
lightmap_cull_index = 0;
lod_bias = 1.0;
ignore_occlusion_culling = false;
scenario = nullptr;
update_aabb = false;
update_dependencies = false;
extra_margin = 0;
visible = true;
lod_begin = 0;
lod_end = 0;
lod_begin_hysteresis = 0;
lod_end_hysteresis = 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);
}
}
};
SelfList<Instance>::List _instance_update_list;
void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies = false);
struct InstanceGeometryData : public InstanceBaseData {
RendererSceneRender::GeometryInstance *geometry_instance = nullptr;
Set<Instance *> lights;
bool can_cast_shadows;
bool material_is_animated;
Set<Instance *> decals;
Set<Instance *> reflection_probes;
Set<Instance *> gi_probes;
Set<Instance *> lightmap_captures;
InstanceGeometryData() {
can_cast_shadows = true;
material_is_animated = true;
}
};
struct InstanceReflectionProbeData : public InstanceBaseData {
Instance *owner;
Set<Instance *> geometries;
RID instance;
SelfList<InstanceReflectionProbeData> update_list;
int render_step;
InstanceReflectionProbeData() :
update_list(this) {
render_step = -1;
}
};
struct InstanceDecalData : public InstanceBaseData {
Instance *owner;
RID instance;
Set<Instance *> geometries;
InstanceDecalData() {
}
};
SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
struct InstanceParticlesCollisionData : public InstanceBaseData {
RID instance;
};
struct InstanceLightData : public InstanceBaseData {
RID instance;
uint64_t last_version;
List<Instance *>::Element *D; // directional light in scenario
bool shadow_dirty;
Set<Instance *> geometries;
Instance *baked_light;
RS::LightBakeMode bake_mode;
uint32_t max_sdfgi_cascade = 2;
InstanceLightData() {
bake_mode = RS::LIGHT_BAKE_DISABLED;
shadow_dirty = true;
D = nullptr;
last_version = 0;
baked_light = nullptr;
}
};
struct InstanceGIProbeData : public InstanceBaseData {
Instance *owner;
Set<Instance *> geometries;
Set<Instance *> dynamic_geometries;
Set<Instance *> lights;
struct LightCache {
RS::LightType type;
Transform3D transform;
Color color;
float energy;
float bake_energy;
float radius;
float attenuation;
float spot_angle;
float spot_attenuation;
bool has_shadow;
bool sky_only;
};
Vector<LightCache> light_cache;
Vector<RID> light_instances;
RID probe_instance;
bool invalid;
uint32_t base_version;
SelfList<InstanceGIProbeData> update_element;
InstanceGIProbeData() :
update_element(this) {
invalid = true;
base_version = 0;
}
};
SelfList<InstanceGIProbeData>::List gi_probe_update_list;
struct InstanceLightmapData : public InstanceBaseData {
RID instance;
Set<Instance *> geometries;
Set<Instance *> users;
InstanceLightmapData() {
}
};
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;
_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))) {
//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);
}
}
};
Set<Instance *> heightfield_particle_colliders_update_list;
PagedArrayPool<Instance *> instance_cull_page_pool;
PagedArrayPool<RendererSceneRender::GeometryInstance *> geometry_instance_cull_page_pool;
PagedArrayPool<RID> rid_cull_page_pool;
PagedArray<Instance *> instance_cull_result;
PagedArray<Instance *> instance_shadow_cull_result;
struct FrustumCullResult {
PagedArray<RendererSceneRender::GeometryInstance *> geometry_instances;
PagedArray<Instance *> lights;
PagedArray<RID> light_instances;
PagedArray<RID> lightmaps;
PagedArray<RID> reflections;
PagedArray<RID> decals;
PagedArray<RID> gi_probes;
PagedArray<RID> mesh_instances;
struct DirectionalShadow {
PagedArray<RendererSceneRender::GeometryInstance *> cascade_geometry_instances[RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES];
} directional_shadows[RendererSceneRender::MAX_DIRECTIONAL_LIGHTS];
PagedArray<RendererSceneRender::GeometryInstance *> 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();
gi_probes.clear();
mesh_instances.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();
gi_probes.reset();
mesh_instances.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(FrustumCullResult &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);
gi_probes.merge_unordered(p_cull_result.gi_probes);
mesh_instances.merge_unordered(p_cull_result.mesh_instances);
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<RendererSceneRender::GeometryInstance *> *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);
lightmaps.set_page_pool(p_rid_pool);
reflections.set_page_pool(p_rid_pool);
decals.set_page_pool(p_rid_pool);
gi_probes.set_page_pool(p_rid_pool);
mesh_instances.set_page_pool(p_rid_pool);
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].set_page_pool(p_geometry_instance_pool);
}
}
for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
sdfgi_region_geometry_instances[i].set_page_pool(p_geometry_instance_pool);
}
for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
sdfgi_cascade_lights[i].set_page_pool(p_rid_pool);
}
}
};
FrustumCullResult frustum_cull_result;
LocalVector<FrustumCullResult> frustum_cull_result_threads;
RendererSceneRender::RenderShadowData render_shadow_data[MAX_UPDATE_SHADOWS];
uint32_t max_shadows_used = 0;
RendererSceneRender::RenderSDFGIData render_sdfgi_data[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE];
RendererSceneRender::RenderSDFGIUpdateData sdfgi_update_data;
uint32_t thread_cull_threshold = 200;
RID_PtrOwner<Instance, true> instance_owner;
uint32_t geometry_instance_pair_mask; // used in traditional forward, unnecessary on clustered
virtual RID instance_allocate();
virtual void instance_initialize(RID p_rid);
virtual void instance_set_base(RID p_instance, RID p_base);
virtual void instance_set_scenario(RID p_instance, RID p_scenario);
virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
virtual void instance_set_transform(RID p_instance, const Transform3D &p_transform);
virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
virtual void instance_set_surface_override_material(RID p_instance, int p_surface, RID p_material);
virtual void instance_set_visible(RID p_instance, bool p_visible);
virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
virtual void instance_set_exterior(RID p_instance, bool p_enabled);
virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
// don't use these in a game!
virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
virtual void instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled);
virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting);
virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
virtual void instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index);
virtual void instance_geometry_set_lod_bias(RID p_instance, float p_lod_bias);
void _update_instance_shader_parameters_from_material(Map<StringName, Instance::InstanceShaderParameter> &isparams, const Map<StringName, Instance::InstanceShaderParameter> &existing_isparams, RID p_material);
virtual void instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value);
virtual void instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const;
virtual Variant instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const;
virtual Variant instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const;
_FORCE_INLINE_ void _update_instance(Instance *p_instance);
_FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
_FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
_FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
void _unpair_instance(Instance *p_instance);
void _light_instance_setup_directional_shadow(int p_shadow_index, Instance *p_instance, const Transform3D p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect);
_FORCE_INLINE_ bool _light_instance_update_shadow(Instance *p_instance, const Transform3D p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario, float p_scren_lod_threshold);
RID _render_get_environment(RID p_camera, RID p_scenario);
struct Cull {
struct Shadow {
RID light_instance;
struct Cascade {
Frustum frustum;
CameraMatrix projection;
Transform3D transform;
real_t zfar;
real_t split;
real_t shadow_texel_size;
real_t bias_scale;
real_t range_begin;
Vector2 uv_scale;
} cascades[RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES]; //max 4 cascades
uint32_t cascade_count;
} shadows[RendererSceneRender::MAX_DIRECTIONAL_LIGHTS];
uint32_t shadow_count;
struct SDFGI {
//have arrays here because SDFGI functions expects this, plus regions can have areas
AABB region_aabb[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE]; //max 3 regions per cascade
uint32_t region_cascade[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE]; //max 3 regions per cascade
uint32_t region_count = 0;
uint32_t cascade_light_index[SDFGI_MAX_CASCADES];
uint32_t cascade_light_count = 0;
} sdfgi;
SpinLock lock;
Frustum frustum;
} cull;
struct CullData {
Cull *cull;
Scenario *scenario;
RID shadow_atlas;
Transform3D cam_transform;
uint32_t visible_layers;
Instance *render_reflection_probe;
const RendererSceneOcclusionCull::HZBuffer *occlusion_buffer;
const CameraMatrix *camera_matrix;
};
void _frustum_cull_threaded(uint32_t p_thread, CullData *cull_data);
void _frustum_cull(CullData &cull_data, FrustumCullResult &cull_result, uint64_t p_from, uint64_t p_to);
bool _render_reflection_probe_step(Instance *p_instance, int p_step);
void _render_scene(const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_render_buffers, RID p_environment, RID p_force_camera_effects, uint32_t p_visible_layers, RID p_scenario, RID p_viewport, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_lod_threshold, bool p_using_shadows = true);
void render_empty_scene(RID p_render_buffers, RID p_scenario, RID p_shadow_atlas);
void render_camera(RID p_render_buffers, RID p_camera, RID p_scenario, RID p_viewport, Size2 p_viewport_size, float p_screen_lod_threshold, RID p_shadow_atlas);
void render_camera(RID p_render_buffers, Ref<XRInterface> &p_interface, XRInterface::Eyes p_eye, RID p_camera, RID p_scenario, RID p_viewport, Size2 p_viewport_size, float p_screen_lod_threshold, RID p_shadow_atlas);
void update_dirty_instances();
void render_particle_colliders();
virtual void render_probes();
TypedArray<Image> bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size);
//pass to scene render
/* ENVIRONMENT API */
#ifdef PASSBASE
#undef PASSBASE
#endif
#define PASSBASE scene_render
PASS2(directional_shadow_atlas_set_size, int, bool)
PASS1(gi_probe_set_quality, RS::GIProbeQuality)
/* SKY API */
PASS0R(RID, sky_allocate)
PASS1(sky_initialize, RID)
PASS2(sky_set_radiance_size, RID, int)
PASS2(sky_set_mode, RID, RS::SkyMode)
PASS2(sky_set_material, RID, RID)
PASS4R(Ref<Image>, sky_bake_panorama, RID, float, bool, const Size2i &)
PASS0R(RID, environment_allocate)
PASS1(environment_initialize, RID)
PASS1RC(bool, is_environment, RID)
PASS2(environment_set_background, RID, RS::EnvironmentBG)
PASS2(environment_set_sky, RID, RID)
PASS2(environment_set_sky_custom_fov, RID, float)
PASS2(environment_set_sky_orientation, RID, const Basis &)
PASS2(environment_set_bg_color, RID, const Color &)
PASS2(environment_set_bg_energy, RID, float)
PASS2(environment_set_canvas_max_layer, RID, int)
PASS7(environment_set_ambient_light, RID, const Color &, RS::EnvironmentAmbientSource, float, float, RS::EnvironmentReflectionSource, const Color &)
PASS6(environment_set_ssr, RID, bool, int, float, float, float)
PASS1(environment_set_ssr_roughness_quality, RS::EnvironmentSSRRoughnessQuality)
PASS10(environment_set_ssao, RID, bool, float, float, float, float, float, float, float, float)
PASS6(environment_set_ssao_quality, RS::EnvironmentSSAOQuality, bool, float, int, float, float)
PASS11(environment_set_glow, RID, bool, Vector<float>, float, float, float, float, RS::EnvironmentGlowBlendMode, float, float, float)
PASS1(environment_glow_set_use_bicubic_upscale, bool)
PASS1(environment_glow_set_use_high_quality, bool)
PASS9(environment_set_tonemap, RID, RS::EnvironmentToneMapper, float, float, bool, float, float, float, float)
PASS7(environment_set_adjustment, RID, bool, float, float, float, bool, RID)
PASS9(environment_set_fog, RID, bool, const Color &, float, float, float, float, float, float)
PASS10(environment_set_volumetric_fog, RID, bool, float, const Color &, float, float, float, float, bool, float)
PASS2(environment_set_volumetric_fog_volume_size, int, int)
PASS1(environment_set_volumetric_fog_filter_active, bool)
PASS11(environment_set_sdfgi, RID, bool, RS::EnvironmentSDFGICascades, float, RS::EnvironmentSDFGIYScale, bool, float, bool, float, float, float)
PASS1(environment_set_sdfgi_ray_count, RS::EnvironmentSDFGIRayCount)
PASS1(environment_set_sdfgi_frames_to_converge, RS::EnvironmentSDFGIFramesToConverge)
PASS1(environment_set_sdfgi_frames_to_update_light, RS::EnvironmentSDFGIFramesToUpdateLight)
PASS1RC(RS::EnvironmentBG, environment_get_background, RID)
PASS1RC(int, environment_get_canvas_max_layer, RID)
PASS3R(Ref<Image>, environment_bake_panorama, RID, bool, const Size2i &)
PASS3(screen_space_roughness_limiter_set_active, bool, float, float)
PASS1(sub_surface_scattering_set_quality, RS::SubSurfaceScatteringQuality)
PASS2(sub_surface_scattering_set_scale, float, float)
/* CAMERA EFFECTS */
PASS0R(RID, camera_effects_allocate)
PASS1(camera_effects_initialize, RID)
PASS2(camera_effects_set_dof_blur_quality, RS::DOFBlurQuality, bool)
PASS1(camera_effects_set_dof_blur_bokeh_shape, RS::DOFBokehShape)
PASS8(camera_effects_set_dof_blur, RID, bool, float, float, bool, float, float, float)
PASS3(camera_effects_set_custom_exposure, RID, bool, float)
PASS1(shadows_quality_set, RS::ShadowQuality)
PASS1(directional_shadow_quality_set, RS::ShadowQuality)
PASS2(sdfgi_set_debug_probe_select, const Vector3 &, const Vector3 &)
/* Render Buffers */
PASS0R(RID, render_buffers_create)
PASS7(render_buffers_configure, RID, RID, int, int, RS::ViewportMSAA, RS::ViewportScreenSpaceAA, bool)
PASS1(gi_set_use_half_resolution, bool)
/* Shadow Atlas */
PASS0R(RID, shadow_atlas_create)
PASS3(shadow_atlas_set_size, RID, int, bool)
PASS3(shadow_atlas_set_quadrant_subdivision, RID, int, int)
PASS1(set_debug_draw_mode, RS::ViewportDebugDraw)
virtual void update();
bool free(RID p_rid);
void set_scene_render(RendererSceneRender *p_scene_render);
RendererSceneCull();
virtual ~RendererSceneCull();
};
#endif // VISUALSERVERSCENE_H
|