Debian Codegraph @

servers/rendering/renderer_scene_occlusion_cull.h

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