Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/rendering_light_culler.h
10277 views
1
/**************************************************************************/
2
/* rendering_light_culler.h */
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
#pragma once
32
33
#include "core/math/plane.h"
34
#include "core/math/vector3.h"
35
#include "renderer_scene_cull.h"
36
37
struct Projection;
38
struct Transform3D;
39
40
// For testing performance improvements from the LightCuller:
41
// Uncomment LIGHT_CULLER_DEBUG_FLASH and it will turn the culler
42
// on and off every LIGHT_CULLER_DEBUG_FLASH_FREQUENCY camera prepares.
43
// Uncomment LIGHT_CULLER_DEBUG_LOGGING to get periodic print of the number of casters culled before / after.
44
// Uncomment LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT to get periodic print of the number of casters culled for the directional light..
45
46
// #define LIGHT_CULLER_DEBUG_LOGGING
47
// #define LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT
48
// #define LIGHT_CULLER_DEBUG_REGULAR_LIGHT
49
// #define LIGHT_CULLER_DEBUG_FLASH
50
#define LIGHT_CULLER_DEBUG_FLASH_FREQUENCY 1024
51
////////////////////////////////////////////////////////////////////////////////////////////////
52
53
// The code to generate the lookup table is included but commented out.
54
// This may be useful for debugging / regenerating the LUT in the future,
55
// especially if the order of planes changes.
56
// When this define is set, the generated lookup table will be printed to debug output.
57
// The generated lookup table can be copy pasted
58
// straight to LUT_entry_sizes and LUT_entries.
59
// See the referenced article for explanation.
60
// #define RENDERING_LIGHT_CULLER_CALCULATE_LUT
61
62
////////////////////////////////////////////////////////////////////////////////////////////////
63
// This define will be set automatically depending on earlier defines, you can leave this as is.
64
#if defined(LIGHT_CULLER_DEBUG_LOGGING) || defined(RENDERING_LIGHT_CULLER_CALCULATE_LUT)
65
#define RENDERING_LIGHT_CULLER_DEBUG_STRINGS
66
#endif
67
68
// Culls shadow casters that can't cast shadows into the camera frustum.
69
class RenderingLightCuller {
70
public:
71
RenderingLightCuller();
72
73
private:
74
class LightSource {
75
public:
76
enum SourceType {
77
ST_UNKNOWN,
78
ST_DIRECTIONAL,
79
ST_SPOTLIGHT,
80
ST_OMNI,
81
};
82
83
LightSource() {
84
type = ST_UNKNOWN;
85
angle = 0.0f;
86
range = FLT_MAX;
87
}
88
89
// All in world space, culling done in world space.
90
Vector3 pos;
91
Vector3 dir;
92
SourceType type;
93
94
float angle; // For spotlight.
95
float range;
96
};
97
98
// Same order as godot.
99
enum PlaneOrder {
100
PLANE_NEAR,
101
PLANE_FAR,
102
PLANE_LEFT,
103
PLANE_TOP,
104
PLANE_RIGHT,
105
PLANE_BOTTOM,
106
PLANE_TOTAL,
107
};
108
109
// Same order as godot.
110
enum PointOrder {
111
PT_FAR_LEFT_TOP,
112
PT_FAR_LEFT_BOTTOM,
113
PT_FAR_RIGHT_TOP,
114
PT_FAR_RIGHT_BOTTOM,
115
PT_NEAR_LEFT_TOP,
116
PT_NEAR_LEFT_BOTTOM,
117
PT_NEAR_RIGHT_TOP,
118
PT_NEAR_RIGHT_BOTTOM,
119
};
120
121
// 6 bits, 6 planes.
122
enum {
123
NUM_CAM_PLANES = 6,
124
NUM_CAM_POINTS = 8,
125
MAX_CULL_PLANES = 17,
126
LUT_SIZE = 64,
127
};
128
129
public:
130
// Before each pass with a different camera, you must call this so the culler can pre-create
131
// the camera frustum planes and corner points in world space which are used for the culling.
132
bool prepare_camera(const Transform3D &p_cam_transform, const Projection &p_cam_matrix);
133
134
// REGULAR LIGHTS (SPOT, OMNI).
135
// These are prepared then used for culling one by one, single threaded.
136
// prepare_regular_light() returns false if the entire light is culled (i.e. there is no intersection between the light and the view frustum).
137
bool prepare_regular_light(const RendererSceneCull::Instance &p_instance) { return _prepare_light(p_instance, -1); }
138
139
// Cull according to the regular light planes that were setup in the previous call to prepare_regular_light.
140
void cull_regular_light(PagedArray<RendererSceneCull::Instance *> &r_instance_shadow_cull_result);
141
142
// Directional lights are prepared in advance, and can be culled multithreaded chopping and changing between
143
// different directional_light_id.
144
void prepare_directional_light(const RendererSceneCull::Instance *p_instance, int32_t p_directional_light_id);
145
146
// Return false if the instance is to be culled.
147
bool cull_directional_light(const RendererSceneCull::InstanceBounds &p_bound, int32_t p_directional_light_id);
148
149
// Can turn on and off from the engine if desired.
150
void set_caster_culling_active(bool p_active) { data.caster_culling_active = p_active; }
151
void set_light_culling_active(bool p_active) { data.light_culling_active = p_active; }
152
153
private:
154
struct LightCullPlanes {
155
void add_cull_plane(const Plane &p);
156
Plane cull_planes[MAX_CULL_PLANES];
157
int num_cull_planes = 0;
158
#ifdef LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT
159
uint32_t rejected_count = 0;
160
#endif
161
};
162
163
bool _prepare_light(const RendererSceneCull::Instance &p_instance, int32_t p_directional_light_id = -1);
164
165
// Avoid adding extra culling planes derived from near colinear triangles.
166
// The normals derived from these will be inaccurate, and can lead to false
167
// culling of objects that should be within the light volume.
168
bool _is_colinear_tri(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c) const {
169
// Lengths of sides a, b and c.
170
float la = (p_b - p_a).length();
171
float lb = (p_c - p_b).length();
172
float lc = (p_c - p_a).length();
173
174
// Get longest side into lc.
175
if (lb < la) {
176
SWAP(la, lb);
177
}
178
if (lc < lb) {
179
SWAP(lb, lc);
180
}
181
182
// Prevent divide by zero.
183
if (lc > 0.001f) {
184
// If the summed length of the smaller two
185
// sides is close to the length of the longest side,
186
// the points are colinear, and the triangle is near degenerate.
187
float ld = ((la + lb) - lc) / lc;
188
189
// ld will be close to zero for colinear tris.
190
return ld < 0.001f;
191
}
192
193
// Don't create planes from tiny triangles,
194
// they won't be accurate.
195
return true;
196
}
197
198
// Internal version uses LightSource.
199
bool _add_light_camera_planes(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);
200
201
// Directional light gives parallel culling planes (as opposed to point lights).
202
bool add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);
203
204
// Is the light culler active? maybe not in the editor...
205
bool is_caster_culling_active() const { return data.caster_culling_active; }
206
bool is_light_culling_active() const { return data.light_culling_active; }
207
208
// Do we want to log some debug output?
209
bool is_logging() const { return data.debug_count == 0; }
210
211
struct Data {
212
// Camera frustum planes (world space) - order ePlane.
213
Vector<Plane> frustum_planes;
214
215
// Camera frustum corners (world space) - order ePoint.
216
Vector3 frustum_points[NUM_CAM_POINTS];
217
218
// Master can have multiple directional lights.
219
// These need to store their own cull planes individually, as master
220
// chops and changes between culling different lights
221
// instead of doing one by one, and we don't want to prepare
222
// lights multiple times per frame.
223
LocalVector<LightCullPlanes> directional_cull_planes;
224
225
// Single threaded cull planes for regular lights
226
// (OMNI, SPOT). These lights reuse the same set of cull plane data.
227
LightCullPlanes regular_cull_planes;
228
229
#ifdef LIGHT_CULLER_DEBUG_REGULAR_LIGHT
230
uint32_t regular_rejected_count = 0;
231
#endif
232
// The whole regular light can be out of range of the view frustum, in which case all casters should be culled.
233
bool out_of_range = false;
234
235
#ifdef RENDERING_LIGHT_CULLER_DEBUG_STRINGS
236
static String plane_bitfield_to_string(unsigned int BF);
237
// Names of the plane and point enums, useful for debugging.
238
static const char *string_planes[];
239
static const char *string_points[];
240
#endif
241
242
// Precalculated look up table.
243
static uint8_t LUT_entry_sizes[LUT_SIZE];
244
static uint8_t LUT_entries[LUT_SIZE][8];
245
246
bool caster_culling_active = true;
247
bool light_culling_active = true;
248
249
// Light culling is a basic on / off switch.
250
// Caster culling only works if light culling is also on.
251
bool is_active() const { return light_culling_active; }
252
253
// Ideally a frame counter, but for ease of implementation
254
// this is just incremented on each prepare_camera.
255
// used to turn on and off debugging features.
256
int debug_count = -1;
257
} data;
258
259
// This functionality is not required in general use (and is compiled out),
260
// as the lookup table can normally be hard coded
261
// (provided order of planes etc does not change).
262
// It is provided for debugging / future maintenance.
263
#ifdef RENDERING_LIGHT_CULLER_CALCULATE_LUT
264
void get_neighbouring_planes(PlaneOrder p_plane, PlaneOrder r_neigh_planes[4]) const;
265
void get_corners_of_planes(PlaneOrder p_plane_a, PlaneOrder p_plane_b, PointOrder r_points[2]) const;
266
void create_LUT();
267
void compact_LUT_entry(uint32_t p_entry_id);
268
void debug_print_LUT();
269
void debug_print_LUT_as_table();
270
void add_LUT(int p_plane_0, int p_plane_1, PointOrder p_pts[2]);
271
void add_LUT_entry(uint32_t p_entry_id, PointOrder p_pts[2]);
272
String debug_string_LUT_entry(const LocalVector<uint8_t> &p_entry, bool p_pair = false);
273
String string_LUT_entry(const LocalVector<uint8_t> &p_entry);
274
275
// Contains a list of points for each combination of plane facing directions.
276
LocalVector<uint8_t> _calculated_LUT[LUT_SIZE];
277
#endif
278
};
279
280