SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
CullingSystem.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <chrono>
4#include <cmath>
5#include <limits>
6#include <vector>
7
8#if defined(__SSE2__) || defined(_M_X64)
9#define SLEAK_CULL_SSE2 1
10#include <emmintrin.h>
11#else
12#define SLEAK_CULL_SSE2 0
13#endif
14
15namespace Sleak {
16namespace {
17
18constexpr float kWEps = 1e-4f;
19constexpr float kZBias = 1e-4f;
20constexpr int kLanes = 4;
21
22/// Clip-space vertex (row-vector convention: clip = point * VP).
23struct ClipVert {
24 float x, y, z, w;
25};
26
27/// One submitted occluder queued for FinalizeOccluders, either a box or a range into triVerts.
28struct OccluderEntry {
29 bool isBox = true;
30 Math::AABB bounds{};
31 uint32_t triStart = 0;
32 uint32_t triCount = 0;
33 float distSq = 0.0f;
34};
35
36/// All per-frame culling state: settings, the software depth buffer, and pending occluders.
37struct CullState {
38 bool frustumEnabled = true;
39 bool occlusionEnabled = true;
40 uint32_t width = 256;
41 uint32_t height = 144;
42 uint32_t maxOccluders = 192;
43
44 std::vector<float> depth;
45 std::vector<OccluderEntry> occluders;
46 std::vector<Math::Vector3D> triVerts;
47
48 ViewFrustum frustum{};
49 Math::Matrix4 viewProj{};
50 Math::Vector3D cameraPos{};
51
52 bool hasFrame = false;
53 bool rasterizedThisFrame = false;
54 bool everRasterized = false;
55
56 bool adaptiveEnabled = true;
57 uint32_t probeInterval = 20;
58 uint32_t skipFrames = 0;
59
60 CullingSystem::Stats stats{};
61};
62
63/// Process-wide singleton culling state (the class is all static methods).
64CullState& State() {
65 static CullState s;
66 return s;
67}
68
69/// World point -> clip space.
70inline ClipVert ToClip(const Math::Vector3D& p, const Math::Matrix4& m) {
71 float x = p.GetX(), y = p.GetY(), z = p.GetZ();
72 ClipVert c;
73 c.x = x * m(0, 0) + y * m(1, 0) + z * m(2, 0) + m(3, 0);
74 c.y = x * m(0, 1) + y * m(1, 1) + z * m(2, 1) + m(3, 1);
75 c.z = x * m(0, 2) + y * m(1, 2) + z * m(2, 2) + m(3, 2);
76 c.w = x * m(0, 3) + y * m(1, 3) + z * m(2, 3) + m(3, 3);
77 return c;
78}
79
80/// Clip space -> buffer pixels + NDC z (0 near, 1 far).
81inline void ToScreen(const ClipVert& c, float w, float h, float& sx,
82 float& sy, float& sz) {
83 float inv = 1.0f / c.w;
84 float ndcx = c.x * inv;
85 float ndcy = c.y * inv;
86 sz = c.z * inv;
87 sx = (ndcx * 0.5f + 0.5f) * w;
88 sy = (0.5f - ndcy * 0.5f) * h;
89}
90
91/// Sutherland-Hodgman clip against near plane (w >= kWEps).
92int ClipNear(const ClipVert* in, int n, ClipVert* out) {
93 int m = 0;
94 for (int i = 0; i < n; ++i) {
95 const ClipVert& A = in[i];
96 const ClipVert& B = in[(i + 1) % n];
97 bool inA = A.w >= kWEps;
98 bool inB = B.w >= kWEps;
99 if (inA) out[m++] = A;
100 if (inA != inB) {
101 float t = (kWEps - A.w) / (B.w - A.w);
102 ClipVert I;
103 I.x = A.x + t * (B.x - A.x);
104 I.y = A.y + t * (B.y - A.y);
105 I.z = A.z + t * (B.z - A.z);
106 I.w = A.w + t * (B.w - A.w);
107 out[m++] = I;
108 }
109 }
110 return m;
111}
112
113/// Clamped pixel bounds, edge values at the first pixel center and their
114/// per-pixel (A) and per-row (B) steps for one screen triangle.
115struct TriSetup {
116 int minX, maxX, minY, maxY;
117 float w0Row, w1Row, w2Row;
118 float A0, A1, A2;
119 float B0, B1, B2;
120 float z0, z1, z2;
121 float invArea;
122 bool positive;
123};
124
125#if SLEAK_CULL_SSE2
126/// Per-lane inside test for one pixel column; kPositive picks the winding.
127template <bool kPositive>
128inline __m128 EdgeMask(__m128 w0, __m128 w1, __m128 w2) {
129 const __m128 zero = _mm_setzero_ps();
130 if constexpr (kPositive) {
131 return _mm_and_ps(
132 _mm_and_ps(_mm_cmpge_ps(w0, zero), _mm_cmpge_ps(w1, zero)),
133 _mm_cmpge_ps(w2, zero));
134 } else {
135 return _mm_and_ps(
136 _mm_and_ps(_mm_cmple_ps(w0, zero), _mm_cmple_ps(w1, zero)),
137 _mm_cmple_ps(w2, zero));
138 }
139}
140
141/// Min-merges the first n lanes of v into dst with the scalar compare.
142inline void MinStorePartial(float* dst, __m128 v, int n) {
143 alignas(16) float tmp[kLanes];
144 _mm_store_ps(tmp, v);
145 for (int i = 0; i < n; ++i)
146 if (tmp[i] < dst[i]) dst[i] = tmp[i];
147}
148
149/// SSE2 fill of 4-row strips; lanes are rows so each lane repeats the scalar
150/// x-stepping exactly, and 4x4 tiles are transposed for row-major stores.
151template <bool kPositive>
152void RasterTriSse2(const TriSetup& t, float* depth, int W) {
153 const __m128 a0 = _mm_set1_ps(t.A0);
154 const __m128 a1 = _mm_set1_ps(t.A1);
155 const __m128 a2 = _mm_set1_ps(t.A2);
156 const __m128 zv0 = _mm_set1_ps(t.z0);
157 const __m128 zv1 = _mm_set1_ps(t.z1);
158 const __m128 zv2 = _mm_set1_ps(t.z2);
159 const __m128 inv = _mm_set1_ps(t.invArea);
160 const __m128 zero = _mm_setzero_ps();
161 const __m128 one = _mm_set1_ps(1.0f);
162 const __m128 untouched =
163 _mm_set1_ps(std::numeric_limits<float>::infinity());
164
165 float w0Row = t.w0Row, w1Row = t.w1Row, w2Row = t.w2Row;
166 for (int y = t.minY; y <= t.maxY; y += kLanes) {
167 alignas(16) float r0[kLanes], r1[kLanes], r2[kLanes];
168 for (int i = 0; i < kLanes; ++i) {
169 r0[i] = w0Row;
170 r1[i] = w1Row;
171 r2[i] = w2Row;
172 w0Row += t.B0;
173 w1Row += t.B1;
174 w2Row += t.B2;
175 }
176 const int rows = std::min(kLanes, t.maxY - y + 1);
177 __m128 e0 = _mm_load_ps(r0);
178 __m128 e1 = _mm_load_ps(r1);
179 __m128 e2 = _mm_load_ps(r2);
180 float* rowBase = depth + y * W;
181
182 for (int x = t.minX; x <= t.maxX; x += kLanes) {
183 __m128 s0 = e0, s1 = e1, s2 = e2;
184 __m128 m[kLanes];
185 __m128 any = zero;
186 for (int k = 0; k < kLanes; ++k) {
187 m[k] = EdgeMask<kPositive>(e0, e1, e2);
188 any = _mm_or_ps(any, m[k]);
189 e0 = _mm_add_ps(e0, a0);
190 e1 = _mm_add_ps(e1, a1);
191 e2 = _mm_add_ps(e2, a2);
192 }
193 if (_mm_movemask_ps(any) == 0) continue;
194
195 __m128 zc[kLanes];
196 for (int k = 0; k < kLanes; ++k) {
197 __m128 z = _mm_add_ps(
198 _mm_add_ps(_mm_mul_ps(s0, zv0), _mm_mul_ps(s1, zv1)),
199 _mm_mul_ps(s2, zv2));
200 z = _mm_min_ps(one, _mm_mul_ps(z, inv));
201 zc[k] = _mm_max_ps(_mm_andnot_ps(m[k], untouched), z);
202 s0 = _mm_add_ps(s0, a0);
203 s1 = _mm_add_ps(s1, a1);
204 s2 = _mm_add_ps(s2, a2);
205 }
206 _MM_TRANSPOSE4_PS(zc[0], zc[1], zc[2], zc[3]);
207
208 float* p = rowBase + x;
209 const int cols = std::min(kLanes, t.maxX - x + 1);
210 if (cols == kLanes) {
211 for (int r = 0; r < rows; ++r) {
212 float* q = p + r * W;
213 _mm_storeu_ps(q, _mm_min_ps(zc[r], _mm_loadu_ps(q)));
214 }
215 } else {
216 for (int r = 0; r < rows; ++r)
217 MinStorePartial(p + r * W, zc[r], cols);
218 }
219 }
220 }
221}
222#else
223/// Scalar fill of the triangle's bounding box with min-depth writes.
224void RasterTriScalar(const TriSetup& t, float* depth, int W) {
225 float w0Row = t.w0Row, w1Row = t.w1Row, w2Row = t.w2Row;
226 for (int y = t.minY; y <= t.maxY; ++y) {
227 float w0 = w0Row, w1 = w1Row, w2 = w2Row;
228 int row = y * W;
229 for (int x = t.minX; x <= t.maxX; ++x) {
230 bool inside = t.positive ? (w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f)
231 : (w0 <= 0.0f && w1 <= 0.0f && w2 <= 0.0f);
232 if (inside) {
233 float z = (w0 * t.z0 + w1 * t.z1 + w2 * t.z2) * t.invArea;
234 if (z < 0.0f) z = 0.0f;
235 if (z > 1.0f) z = 1.0f;
236 int idx = row + x;
237 if (z < depth[idx]) depth[idx] = z;
238 }
239 w0 += t.A0;
240 w1 += t.A1;
241 w2 += t.A2;
242 }
243 w0Row += t.B0;
244 w1Row += t.B1;
245 w2Row += t.B2;
246 }
247}
248#endif
249
250/// Edge-function rasterizer with incremental row stepping, min-depth write.
251void RasterScreenTri(float x0, float y0, float z0, float x1, float y1,
252 float z1, float x2, float y2, float z2) {
253 CullState& s = State();
254 int W = static_cast<int>(s.width);
255 int H = static_cast<int>(s.height);
256
257 float area = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
258 if (std::fabs(area) < 1e-4f) return;
259 float invArea = 1.0f / area;
260
261 float fminX = std::min(x0, std::min(x1, x2));
262 float fmaxX = std::max(x0, std::max(x1, x2));
263 float fminY = std::min(y0, std::min(y1, y2));
264 float fmaxY = std::max(y0, std::max(y1, y2));
265
266 int minX = static_cast<int>(std::floor(fminX));
267 int maxX = static_cast<int>(std::ceil(fmaxX));
268 int minY = static_cast<int>(std::floor(fminY));
269 int maxY = static_cast<int>(std::ceil(fmaxY));
270 if (minX < 0) minX = 0;
271 if (minY < 0) minY = 0;
272 if (maxX > W - 1) maxX = W - 1;
273 if (maxY > H - 1) maxY = H - 1;
274 if (minX > maxX || minY > maxY) return;
275
276 float px0 = minX + 0.5f;
277 float py0 = minY + 0.5f;
278 TriSetup t;
279 t.minX = minX;
280 t.maxX = maxX;
281 t.minY = minY;
282 t.maxY = maxY;
283 t.w0Row = (x2 - x1) * (py0 - y1) - (y2 - y1) * (px0 - x1);
284 t.w1Row = (x0 - x2) * (py0 - y2) - (y0 - y2) * (px0 - x2);
285 t.w2Row = (x1 - x0) * (py0 - y0) - (y1 - y0) * (px0 - x0);
286 t.A0 = y1 - y2;
287 t.A1 = y2 - y0;
288 t.A2 = y0 - y1;
289 t.B0 = x2 - x1;
290 t.B1 = x0 - x2;
291 t.B2 = x1 - x0;
292 t.z0 = z0;
293 t.z1 = z1;
294 t.z2 = z2;
295 t.invArea = invArea;
296 t.positive = area > 0.0f;
297
298#if SLEAK_CULL_SSE2
299 if (t.positive)
300 RasterTriSse2<true>(t, s.depth.data(), W);
301 else
302 RasterTriSse2<false>(t, s.depth.data(), W);
303#else
304 RasterTriScalar(t, s.depth.data(), W);
305#endif
306}
307
308/// Near-clip a clip-space triangle, fan-triangulate, rasterize.
309void RasterClipTri(const ClipVert& a, const ClipVert& b, const ClipVert& c) {
310 ClipVert in[3] = {a, b, c};
311 ClipVert out[8];
312 int n = ClipNear(in, 3, out);
313 if (n < 3) return;
314
315 CullState& s = State();
316 float w = static_cast<float>(s.width);
317 float h = static_cast<float>(s.height);
318
319 float sx0, sy0, sz0;
320 ToScreen(out[0], w, h, sx0, sy0, sz0);
321 for (int i = 1; i + 1 < n; ++i) {
322 float sx1, sy1, sz1, sx2, sy2, sz2;
323 ToScreen(out[i], w, h, sx1, sy1, sz1);
324 ToScreen(out[i + 1], w, h, sx2, sy2, sz2);
325 RasterScreenTri(sx0, sy0, sz0, sx1, sy1, sz1, sx2, sy2, sz2);
326 }
327}
328
329/// Projects a world-space triangle and rasterizes it into the depth buffer.
330void RasterWorldTri(const Math::Vector3D& p0, const Math::Vector3D& p1,
331 const Math::Vector3D& p2, const Math::Matrix4& vp) {
332 RasterClipTri(ToClip(p0, vp), ToClip(p1, vp), ToClip(p2, vp));
333}
334
335/// Rasterize up to 3 camera-facing faces of a solid box.
336void RasterBox(const Math::AABB& box, const Math::Matrix4& vp,
337 const Math::Vector3D& cam) {
338 float mnx = box.min.GetX(), mny = box.min.GetY(), mnz = box.min.GetZ();
339 float mxx = box.max.GetX(), mxy = box.max.GetY(), mxz = box.max.GetZ();
340 using V = Math::Vector3D;
341 V c000(mnx, mny, mnz), c100(mxx, mny, mnz);
342 V c010(mnx, mxy, mnz), c110(mxx, mxy, mnz);
343 V c001(mnx, mny, mxz), c101(mxx, mny, mxz);
344 V c011(mnx, mxy, mxz), c111(mxx, mxy, mxz);
345
346 auto quad = [&](const V& a, const V& b, const V& c, const V& d) {
347 RasterWorldTri(a, b, c, vp);
348 RasterWorldTri(a, c, d, vp);
349 };
350
351 if (cam.GetX() < mnx) quad(c000, c001, c011, c010);
352 if (cam.GetX() > mxx) quad(c100, c110, c111, c101);
353 if (cam.GetY() < mny) quad(c000, c100, c101, c001);
354 if (cam.GetY() > mxy) quad(c010, c011, c111, c110);
355 if (cam.GetZ() < mnz) quad(c000, c010, c110, c100);
356 if (cam.GetZ() > mxz) quad(c001, c101, c111, c011);
357}
358
359/// True when any depth in the inclusive pixel rect is >= thresh.
360bool AnyDepthAtOrBehind(const float* depth, int W, int minX, int maxX, int minY,
361 int maxY, float thresh) {
362#if SLEAK_CULL_SSE2
363 if (maxX - minX + 1 >= kLanes) {
364 const __m128 t = _mm_set1_ps(thresh);
365 const int lastX = maxX - kLanes + 1;
366 for (int y = minY; y <= maxY; ++y) {
367 const float* row = depth + y * W;
368 __m128 hit = _mm_cmpge_ps(_mm_loadu_ps(row + lastX), t);
369 for (int x = minX; x < lastX; x += kLanes)
370 hit = _mm_or_ps(hit, _mm_cmpge_ps(_mm_loadu_ps(row + x), t));
371 if (_mm_movemask_ps(hit) != 0) return true;
372 }
373 return false;
374 }
375#endif
376 for (int y = minY; y <= maxY; ++y) {
377 const float* row = depth + y * W;
378 for (int x = minX; x <= maxX; ++x) {
379 if (row[x] >= thresh) return true;
380 }
381 }
382 return false;
383}
384
385/// Conservative depth test: true when the box could be in front of the
386/// rasterized occluders anywhere in its projected rect.
387bool DepthRectVisible(const Math::AABB& box) {
388 CullState& s = State();
389 float w = static_cast<float>(s.width);
390 float h = static_cast<float>(s.height);
391 float mnx = 1e30f, mny = 1e30f, mxx = -1e30f, mxy = -1e30f;
392 float boxMinZ = 1e30f;
393 for (int i = 0; i < 8; ++i) {
394 ClipVert c = ToClip(box.Corner(i), s.viewProj);
395 if (c.w <= kWEps) return true;
396 float sx, sy, sz;
397 ToScreen(c, w, h, sx, sy, sz);
398 mnx = std::min(mnx, sx);
399 mxx = std::max(mxx, sx);
400 mny = std::min(mny, sy);
401 mxy = std::max(mxy, sy);
402 boxMinZ = std::min(boxMinZ, sz);
403 }
404
405 int W = static_cast<int>(s.width);
406 int H = static_cast<int>(s.height);
407 int minX = static_cast<int>(std::floor(mnx)) - 1;
408 int maxX = static_cast<int>(std::ceil(mxx)) + 1;
409 int minY = static_cast<int>(std::floor(mny)) - 1;
410 int maxY = static_cast<int>(std::ceil(mxy)) + 1;
411 if (minX < 0) minX = 0;
412 if (minY < 0) minY = 0;
413 if (maxX > W - 1) maxX = W - 1;
414 if (maxY > H - 1) maxY = H - 1;
415 if (minX > maxX || minY > maxY) return true;
416
417 return AnyDepthAtOrBehind(s.depth.data(), W, minX, maxX, minY, maxY,
418 boxMinZ - kZBias);
419}
420
421/// True when the box projects entirely in front but spans < 2x2 px.
422bool ProjectedTooSmall(const Math::AABB& b, const Math::Matrix4& vp) {
423 CullState& s = State();
424 float w = static_cast<float>(s.width);
425 float h = static_cast<float>(s.height);
426 float mnx = 1e30f, mny = 1e30f, mxx = -1e30f, mxy = -1e30f;
427 for (int i = 0; i < 8; ++i) {
428 ClipVert c = ToClip(b.Corner(i), vp);
429 if (c.w <= kWEps) return false;
430 float sx, sy, sz;
431 ToScreen(c, w, h, sx, sy, sz);
432 mnx = std::min(mnx, sx);
433 mxx = std::max(mxx, sx);
434 mny = std::min(mny, sy);
435 mxy = std::max(mxy, sy);
436 }
437 return (mxx - mnx) < 2.0f || (mxy - mny) < 2.0f;
438}
439
440} // namespace
441
443 State().frustumEnabled = enabled;
444}
445
447 State().occlusionEnabled = enabled;
448}
449
451 return State().frustumEnabled;
452}
453
455 return State().occlusionEnabled;
456}
457
459 CullState& s = State();
460 s.width = width > 0 ? width : 1;
461 s.height = height > 0 ? height : 1;
462}
463
464void CullingSystem::SetMaxOccluders(uint32_t count) {
465 State().maxOccluders = count;
466}
467
469 uint32_t probeInterval) {
470 CullState& s = State();
471 s.adaptiveEnabled = enabled;
472 s.probeInterval = probeInterval > 0 ? probeInterval : 1;
473 if (!enabled) s.skipFrames = 0;
474}
475
477 const Math::Matrix4& viewProj,
478 const Math::Vector3D& cameraPos) {
479 CullState& s = State();
480 if (s.width == 0) s.width = 1;
481 if (s.height == 0) s.height = 1;
482
483 // Adaptive: a rasterized frame that culled nothing idles the pass.
484 if (s.adaptiveEnabled && s.rasterizedThisFrame &&
485 s.stats.occlusionCulled == 0)
486 s.skipFrames = s.probeInterval;
487
488 size_t need = static_cast<size_t>(s.width) * s.height;
489 if (s.depth.size() != need) s.depth.assign(need, 1.0f);
490
491 s.occluders.clear();
492 s.triVerts.clear();
493 s.frustum = frustum;
494 s.viewProj = viewProj;
495 s.cameraPos = cameraPos;
496 s.hasFrame = true;
497 s.rasterizedThisFrame = false;
498 s.stats = Stats{};
499}
500
502 CullState& s = State();
503 if (!s.hasFrame || !s.occlusionEnabled) return;
504 if (!box.IsValid()) return;
505 if (!s.frustum.IsAABBVisible(box.min, box.max)) return;
506
507 OccluderEntry e;
508 e.isBox = true;
509 e.bounds = box;
510 e.distSq = box.DistanceSq(s.cameraPos);
511 s.occluders.push_back(e);
512 ++s.stats.occludersSubmitted;
513}
514
516 uint32_t vertexCount,
517 const uint32_t* indices,
518 uint32_t indexCount) {
519 CullState& s = State();
520 if (!s.hasFrame || !s.occlusionEnabled) return;
521 if (!vertices || vertexCount == 0) return;
522 if (!indices || indexCount < 3) return;
523 indexCount -= indexCount % 3;
524
525 Math::AABB bounds(vertices[0], vertices[0]);
526 for (uint32_t i = 1; i < vertexCount; ++i)
527 bounds.Merge(Math::AABB(vertices[i], vertices[i]));
528 if (!s.frustum.IsAABBVisible(bounds.min, bounds.max)) return;
529
530 uint32_t start = static_cast<uint32_t>(s.triVerts.size());
531 for (uint32_t i = 0; i < indexCount; ++i) {
532 uint32_t idx = indices[i];
533 if (idx >= vertexCount) {
534 s.triVerts.resize(start);
535 return;
536 }
537 s.triVerts.push_back(vertices[idx]);
538 }
539
540 OccluderEntry e;
541 e.isBox = false;
542 e.bounds = bounds;
543 e.triStart = start;
544 e.triCount = indexCount;
545 e.distSq = bounds.DistanceSq(s.cameraPos);
546 s.occluders.push_back(e);
547 ++s.stats.occludersSubmitted;
548}
549
551 CullState& s = State();
552 if (!s.hasFrame) return;
553
554 if (s.skipFrames > 0) {
555 --s.skipFrames;
556 s.occluders.clear();
557 s.triVerts.clear();
558 s.rasterizedThisFrame = false;
559 s.stats.occlusionSkipped = true;
560 return;
561 }
562
563 auto t0 = std::chrono::high_resolution_clock::now();
564
565 std::fill(s.depth.begin(), s.depth.end(), 1.0f);
566
567 std::sort(s.occluders.begin(), s.occluders.end(),
568 [](const OccluderEntry& a, const OccluderEntry& b) {
569 return a.distSq < b.distSq;
570 });
571
572 uint32_t rasterized = 0;
573 for (const auto& e : s.occluders) {
574 if (rasterized >= s.maxOccluders) break;
575 if (ProjectedTooSmall(e.bounds, s.viewProj)) continue;
576 // Occluder fusion: skip occluders fully behind rasterized ones.
577 if (rasterized > 0 && !DepthRectVisible(e.bounds)) continue;
578 if (e.isBox) {
579 RasterBox(e.bounds, s.viewProj, s.cameraPos);
580 } else {
581 for (uint32_t i = 0; i + 2 < e.triCount; i += 3) {
582 RasterWorldTri(s.triVerts[e.triStart + i],
583 s.triVerts[e.triStart + i + 1],
584 s.triVerts[e.triStart + i + 2], s.viewProj);
585 }
586 }
587 ++rasterized;
588 }
589
590 s.stats.occludersRasterized = rasterized;
591 s.rasterizedThisFrame = rasterized > 0;
592 if (rasterized > 0) s.everRasterized = true;
593
594 auto t1 = std::chrono::high_resolution_clock::now();
595 s.stats.rasterizeMs =
596 std::chrono::duration<float, std::milli>(t1 - t0).count();
597}
598
600 CullState& s = State();
601 ++s.stats.tested;
602 if (!s.hasFrame) return true;
603
604 if (s.frustumEnabled) {
605 if (!s.frustum.IsAABBVisible(box.min, box.max)) {
606 ++s.stats.frustumCulled;
607 return false;
608 }
609 }
610
611 if (!s.occlusionEnabled || !s.rasterizedThisFrame) return true;
612
613 if (DepthRectVisible(box)) return true;
614
615 ++s.stats.occlusionCulled;
616 return false;
617}
618
620 CullState& s = State();
621 ++s.stats.tested;
622 if (!s.hasFrame) return true;
623 if (s.frustumEnabled) {
624 if (!s.frustum.IsAABBVisible(box.min, box.max)) {
625 ++s.stats.frustumCulled;
626 return false;
627 }
628 }
629 return true;
630}
631
633 return State().stats;
634}
635
636const float* CullingSystem::GetDepthBuffer(uint32_t& width, uint32_t& height) {
637 CullState& s = State();
638 if (!s.everRasterized || s.depth.empty()) {
639 width = 0;
640 height = 0;
641 return nullptr;
642 }
643 width = s.width;
644 height = s.height;
645 return s.depth.data();
646}
647
649 CullState& s = State();
650 s.depth.clear();
651 s.depth.shrink_to_fit();
652 s.occluders.clear();
653 s.occluders.shrink_to_fit();
654 s.triVerts.clear();
655 s.triVerts.shrink_to_fit();
656 s.hasFrame = false;
657 s.rasterizedThisFrame = false;
658 s.everRasterized = false;
659 s.stats = Stats{};
660}
661
662} // namespace Sleak
int width
int height
static bool IsVisibleFrustumOnly(const Math::AABB &box)
Frustum-only visibility test, skipping the occlusion buffer entirely.
static const Stats & GetStats()
static void SetAdaptiveOcclusion(bool enabled, uint32_t probeInterval)
static void SetOcclusionBufferSize(uint32_t width, uint32_t height)
Occlusion depth buffer resolution (default 256x144).
static void SetOcclusionCullingEnabled(bool enabled)
static const float * GetDepthBuffer(uint32_t &width, uint32_t &height)
static void SubmitOccluderTriangles(const Math::Vector3D *vertices, uint32_t vertexCount, const uint32_t *indices, uint32_t indexCount)
World-space triangle occluder; same fully-solid-volume requirement as SubmitOccluderBox.
static void SubmitOccluderBox(const Math::AABB &box)
World-space occluders. Boxes must be fully solid volumes.
static void BeginFrame(const ViewFrustum &frustum, const Math::Matrix4 &viewProj, const Math::Vector3D &cameraPos)
static bool IsVisible(const Math::AABB &box)
static void FinalizeOccluders()
Sorts submitted occluders by distance and rasterizes them into the depth buffer up to the max-occlude...
static bool IsFrustumCullingEnabled()
static bool IsOcclusionCullingEnabled()
static void SetMaxOccluders(uint32_t count)
static void SetFrustumCullingEnabled(bool enabled)
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:404
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Per-frame counters for the last completed culling pass.
void Merge(const AABB &o)
Grows this box to also cover o.
Definition AABB.hpp:36
Vector3D min
Definition AABB.hpp:12
bool IsValid() const
True if min is componentwise <= max.
Definition AABB.hpp:19
Vector3D max
Definition AABB.hpp:13
float DistanceSq(const Vector3D &p) const
Definition AABB.hpp:59