Files
2nd/10_Wiki/Topics/DevOps_and_Security/Frustum Culling.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

199 lines
6.8 KiB
Markdown

---
id: wiki-2026-0508-frustum-culling
title: Frustum Culling
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [View Frustum Culling, VFC, Camera Culling]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [graphics, rendering, culling, optimization, gpu]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: cpp
framework: opengl-vulkan-unity
---
# Frustum Culling
## 매 한 줄
> **"매 carmera 의 view volume (frustum) 밖 object 의 매 draw skip"**. 매 가장 기본적이고 가장 효과적인 매 visibility culling — 매 30-90% draw call 감소가 일반적. 매 modern engine (Unreal 5 Nanite, Unity HDRP, bevy) 은 매 GPU-driven culling 으로 매 millions of objects 를 매 compute shader 안에서 매 frame 마다 cull.
## 매 핵심
### 매 frustum 표현
- **6 planes**: near, far, left, right, top, bottom.
- 매 plane equation: `ax + by + cz + d = 0` with `(a,b,c)` = inward normal.
- 매 view-projection matrix 의 매 row combo 로 6 planes extract (Gribb-Hartmann).
### 매 bounding volume choice
- **AABB (axis-aligned)**: 매 cheapest, 매 conservative — 매 large rotated objects 매 over-conservative.
- **OBB (oriented)**: 매 tighter, 매 더 expensive.
- **Sphere**: 매 cheapest test (single dot product), 매 loosest.
- **Plane mask (frustum culling with masks)**: 매 children inherit parent 의 "fully inside" plane.
### 매 알고리즘 흐름
1. View-projection matrix → 6 frustum planes.
2. 매 object 의 BV 와 매 6 planes test.
3. **Outside** any plane → cull.
4. **Inside** all → render.
5. **Intersect** → render (or recurse children if hierarchy).
### 매 modern (GPU-driven)
- **Compute shader** 가 매 draw arguments buffer 를 build (`DrawIndirect`).
- 매 millions of objects 도 매 sub-millisecond.
- 매 hierarchical Z-buffer occlusion + frustum 결합 (Nanite).
## 💻 패턴
### Extract frustum planes from VP matrix (Gribb-Hartmann)
```cpp
struct Plane { glm::vec3 n; float d; };
void extractPlanes(const glm::mat4& vp, Plane out[6]) {
auto m = glm::transpose(vp); // row-major helper
out[0] = { glm::vec3(m[3]+m[0]), m[3].w + m[0].w }; // left
out[1] = { glm::vec3(m[3]-m[0]), m[3].w - m[0].w }; // right
out[2] = { glm::vec3(m[3]+m[1]), m[3].w + m[1].w }; // bottom
out[3] = { glm::vec3(m[3]-m[1]), m[3].w - m[1].w }; // top
out[4] = { glm::vec3(m[3]+m[2]), m[3].w + m[2].w }; // near
out[5] = { glm::vec3(m[3]-m[2]), m[3].w - m[2].w }; // far
for (int i = 0; i < 6; i++) {
float len = glm::length(out[i].n);
out[i].n /= len; out[i].d /= len;
}
}
```
### Sphere vs frustum (cheapest)
```cpp
bool sphereInFrustum(const Plane planes[6], const glm::vec3& c, float r) {
for (int i = 0; i < 6; i++)
if (glm::dot(planes[i].n, c) + planes[i].d < -r) return false;
return true;
}
```
### AABB vs frustum (positive vertex / p-vertex test)
```cpp
bool aabbInFrustum(const Plane planes[6], const glm::vec3& mn, const glm::vec3& mx) {
for (int i = 0; i < 6; i++) {
glm::vec3 p = {
planes[i].n.x >= 0 ? mx.x : mn.x,
planes[i].n.y >= 0 ? mx.y : mn.y,
planes[i].n.z >= 0 ? mx.z : mn.z
};
if (glm::dot(planes[i].n, p) + planes[i].d < 0) return false;
}
return true;
}
```
### BVH-based hierarchical culling
```cpp
void cullBVH(const BVHNode& node, const Plane planes[6], std::vector<int>& visible) {
auto r = aabbVsFrustumIntersect(planes, node.aabb);
if (r == OUTSIDE) return;
if (r == INSIDE) { addAll(node, visible); return; }
if (node.isLeaf) {
for (int idx : node.objects)
if (aabbInFrustum(planes, objs[idx].mn, objs[idx].mx))
visible.push_back(idx);
return;
}
cullBVH(*node.left, planes, visible);
cullBVH(*node.right, planes, visible);
}
```
### GPU compute culling (HLSL)
```hlsl
// CullCS.hlsl
StructuredBuffer<ObjectData> objects : register(t0);
ConstantBuffer<FrustumCB> frustum : register(b0);
RWStructuredBuffer<DrawArgs> drawArgs : register(u0);
RWByteAddressBuffer counter : register(u1);
[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
if (id.x >= objects.Length) return;
ObjectData o = objects[id.x];
bool visible = true;
[unroll] for (int i = 0; i < 6; i++) {
float4 p = frustum.planes[i];
if (dot(p.xyz, o.center) + p.w < -o.radius) { visible = false; break; }
}
if (visible) {
uint slot;
counter.InterlockedAdd(0, 1, slot);
drawArgs[slot].vertexCount = o.indexCount;
drawArgs[slot].instanceCount = 1;
drawArgs[slot].firstIndex = o.firstIndex;
drawArgs[slot].baseInstance = id.x;
}
}
```
### Unity (Burst) culling job
```csharp
[BurstCompile]
struct FrustumCullJob : IJobParallelFor {
[ReadOnly] public NativeArray<float4> planes; // 6 planes
[ReadOnly] public NativeArray<float4> bounds; // xyz=center, w=radius
[WriteOnly] public NativeArray<bool> visible;
public void Execute(int i) {
float4 b = bounds[i];
for (int p = 0; p < 6; p++) {
float4 pl = planes[p];
if (math.dot(pl.xyz, b.xyz) + pl.w < -b.w) {
visible[i] = false;
return;
}
}
visible[i] = true;
}
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| <1k objects, CPU | per-object sphere/AABB test |
| 1k-100k, hierarchical | BVH / Octree + frustum |
| 100k+ static, GPU | compute shader + DrawIndirect |
| Massive (Nanite-class) | GPU-driven + HZB occlusion |
| Animated skeletal | use skinned bounds (loose) |
**기본값**: 매 modern engine — GPU compute culling + BVH for spatial queries.
## 🔗 Graph
- 부모: [[Real-Time Rendering]]
- 응용: [[GPU-Driven Rendering]] · [[Nanite]]
- Adjacent: [[BVH]] · [[Octree]]
## 🤖 LLM 활용
**언제**: plane extraction code 검토, false-cull bug 디버깅 (e.g., flipped normal), GPU shader skeleton.
**언제 X**: 매 actual rendering decision 의 runtime correctness — unit test + visual verification.
## ❌ 안티패턴
- **No bounding volume cache**: 매 frame 마다 매 mesh 의 bound 재계산 — pre-compute.
- **Sphere only for everything**: 매 long thin object 매 over-conservative.
- **Plane normalization 누락**: 매 distance comparison 부정확.
- **Cull camera == render camera 가정**: 매 shadow camera, planar reflection 시 매 잘못.
- **Animated bound 무시**: 매 skinned mesh 의 bound 가 매 outdated → pop in/out.
## 🧪 검증 / 중복
- Verified (Real-Time Rendering 4th ed, Gribb-Hartmann 2001, Unreal Nanite docs 2026).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — frustum extraction + BV tests + GPU-driven |