Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_scene_occlusion_cull.h
10277 views
1
/**************************************************************************/
2
/* renderer_scene_occlusion_cull.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/projection.h"
34
#include "core/templates/local_vector.h"
35
#include "servers/rendering_server.h"
36
37
class RendererSceneOcclusionCull {
38
protected:
39
static RendererSceneOcclusionCull *singleton;
40
41
public:
42
class HZBuffer {
43
protected:
44
static const Vector3 corners[8];
45
46
LocalVector<float> data;
47
LocalVector<Size2i> sizes;
48
LocalVector<float *> mips;
49
50
RID debug_texture;
51
Ref<Image> debug_image;
52
PackedByteArray debug_data;
53
float debug_tex_range = 0.0f;
54
55
uint64_t occlusion_frame = 0;
56
Size2i occlusion_buffer_size;
57
58
_FORCE_INLINE_ bool _is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near) const {
59
if (is_empty()) {
60
return false;
61
}
62
63
Vector3 closest_point = p_cam_position.clamp(Vector3(p_bounds[0], p_bounds[1], p_bounds[2]), Vector3(p_bounds[3], p_bounds[4], p_bounds[5]));
64
65
if (closest_point == p_cam_position) {
66
return false;
67
}
68
69
Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point);
70
if (closest_point_view.z > -p_near) {
71
return false;
72
}
73
74
// Force distance calculation to use double precision to avoid floating-point overflow for distant objects.
75
closest_point = closest_point - p_cam_position;
76
float min_depth = Math::sqrt((double)closest_point.x * (double)closest_point.x + (double)closest_point.y * (double)closest_point.y + (double)closest_point.z * (double)closest_point.z);
77
78
Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);
79
Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);
80
81
for (int j = 0; j < 8; j++) {
82
const Vector3 &c = RendererSceneOcclusionCull::HZBuffer::corners[j];
83
Vector3 nc = Vector3(1, 1, 1) - c;
84
Vector3 corner = Vector3(p_bounds[0] * c.x + p_bounds[3] * nc.x, p_bounds[1] * c.y + p_bounds[4] * nc.y, p_bounds[2] * c.z + p_bounds[5] * nc.z);
85
Vector3 view = p_cam_inv_transform.xform(corner);
86
87
// When using an orthogonal camera, the closest point of an AABB to the camera is guaranteed to be a corner.
88
if (p_cam_projection.is_orthogonal()) {
89
min_depth = MIN(min_depth, -view.z);
90
}
91
92
Plane vp = Plane(view, 1.0);
93
Plane projected = p_cam_projection.xform4(vp);
94
95
float w = projected.d;
96
if (w < 1.0) {
97
rect_min = Vector2(0.0f, 0.0f);
98
rect_max = Vector2(1.0f, 1.0f);
99
break;
100
}
101
102
Vector2 normalized = Vector2(projected.normal.x / w * 0.5f + 0.5f, projected.normal.y / w * 0.5f + 0.5f);
103
rect_min = rect_min.min(normalized);
104
rect_max = rect_max.max(normalized);
105
}
106
107
rect_max = rect_max.minf(1);
108
rect_min = rect_min.maxf(0);
109
110
int mip_count = mips.size();
111
112
Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0];
113
float size = MAX(screen_diagonal.x, screen_diagonal.y);
114
float l = Math::ceil(Math::log2(size));
115
int lod = CLAMP(l, 0, mip_count - 1);
116
117
const int max_samples = 512;
118
int sample_count = 0;
119
bool visible = true;
120
121
for (; lod >= 0; lod--) {
122
int w = sizes[lod].x;
123
int h = sizes[lod].y;
124
125
int minx = CLAMP(rect_min.x * w - 1, 0, w - 1);
126
int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1);
127
128
int miny = CLAMP(rect_min.y * h - 1, 0, h - 1);
129
int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1);
130
131
sample_count += (maxx - minx + 1) * (maxy - miny + 1);
132
133
if (sample_count > max_samples) {
134
return false;
135
}
136
137
visible = false;
138
for (int y = miny; y <= maxy; y++) {
139
for (int x = minx; x <= maxx; x++) {
140
float depth = mips[lod][y * w + x];
141
if (depth > min_depth) {
142
visible = true;
143
break;
144
}
145
}
146
if (visible) {
147
break;
148
}
149
}
150
151
if (!visible) {
152
return true;
153
}
154
}
155
156
return !visible;
157
}
158
159
public:
160
static bool occlusion_jitter_enabled;
161
162
bool is_empty() const;
163
virtual void clear();
164
virtual void resize(const Size2i &p_size);
165
166
void update_mips();
167
168
// Thin wrapper around _is_occluded(),
169
// allowing occlusion timers to delay the disappearance
170
// of objects to prevent flickering when using jittering.
171
_FORCE_INLINE_ bool is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near, uint64_t &r_occlusion_timeout) const {
172
bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near);
173
174
// Special case, temporal jitter disabled,
175
// so we don't use occlusion timers.
176
if (!occlusion_jitter_enabled) {
177
return occluded;
178
}
179
180
if (!occluded) {
181
//#define DEBUG_RASTER_OCCLUSION_JITTER
182
#ifdef DEBUG_RASTER_OCCLUSION_JITTER
183
r_occlusion_timeout = occlusion_frame + 1;
184
#else
185
r_occlusion_timeout = occlusion_frame + 9;
186
#endif
187
} else if (r_occlusion_timeout) {
188
// Regular timeout, allow occlusion culling
189
// to proceed as normal after the delay.
190
if (occlusion_frame >= r_occlusion_timeout) {
191
r_occlusion_timeout = 0;
192
}
193
}
194
195
return occluded && !r_occlusion_timeout;
196
}
197
198
RID get_debug_texture();
199
const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; }
200
201
virtual ~HZBuffer() {}
202
};
203
204
static RendererSceneOcclusionCull *get_singleton() { return singleton; }
205
206
void _print_warning() {
207
WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");
208
}
209
210
virtual bool is_occluder(RID p_rid) { return false; }
211
virtual RID occluder_allocate() { return RID(); }
212
virtual void occluder_initialize(RID p_occluder) {}
213
virtual void free_occluder(RID p_occluder) { _print_warning(); }
214
virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }
215
216
virtual void add_scenario(RID p_scenario) {}
217
virtual void remove_scenario(RID p_scenario) {}
218
virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }
219
virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }
220
221
virtual void add_buffer(RID p_buffer) { _print_warning(); }
222
virtual void remove_buffer(RID p_buffer) { _print_warning(); }
223
virtual HZBuffer *buffer_get_ptr(RID p_buffer) {
224
return nullptr;
225
}
226
virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }
227
virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }
228
virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}
229
230
virtual RID buffer_get_debug_texture(RID p_buffer) {
231
_print_warning();
232
return RID();
233
}
234
235
virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}
236
237
RendererSceneOcclusionCull() {
238
singleton = this;
239
}
240
241
virtual ~RendererSceneOcclusionCull() {
242
singleton = nullptr;
243
}
244
};
245
246