Culling and the frame

Frustum culling

Frustum culling skips the objects that are outside the camera's view, before the GPU does any work for them. It needs three things: the six planes that bound the view, a simple shape around each object, and a test of one against the other. This page explains each part. The last part shows how RASM does it.

In short

  • The camera sees a frustum: a pyramid with its top cut off, bounded by 6 planes (near, far, left, right, top, bottom).
  • A plane gives every point a signed distance: positive inside, negative outside.
  • Each object gets a bounding volume, usually a sphere or a box. Testing it is much cheaper than testing triangles.
  • A sphere is outside when its center is more than one radius behind any one plane. If it passes all 6, it is kept.
  • The 6 planes come straight from the rows of the view-projection matrix (Gribb and Hartmann).
  • The test is conservative: it may keep an object that is not visible, but it never removes one that is.

1Why cull at all

The GPU already throws away triangles outside the view. It does this in clipping, after the vertex shader. So an object behind the camera never shows up on screen, even without culling. But by then the GPU has already done work for it:

the vertex shader runs on every vertex the view

No culling

The GPU reads the object's vertices, runs the vertex shader on each one and builds its triangles. Clipping then drops every triangle. No pixels: all of that is wasted.

one test of one sphere: outside → skip the draw the view

Frustum culling

One test per object removes it before the draw. The vertex shader, clipping and pixels are all skipped. One test replaces thousands of vertex shader runs.

Culling works on whole objects. It saves the work that clipping would throw away later.

This also tells us the one rule of culling. It is only a way to save time: the GPU would get the same image without it. So a culling test may make one kind of mistake, and never the other:

outside, but kept

May keep an invisible object

That only costs a little time.

visible, but removed

Must never remove a visible object

That would make it vanish from the screen.

A test with this property is called conservative.

2The view frustum

A perspective camera sees everything inside a pyramid that starts at the eye. The pyramid is cut off twice: at the near plane, close to the eye, and at the far plane, far away. What is left is called a frustum. It has 6 faces, and each face lies on a plane.

In 3D: a cut-off pyramid camera near plane far plane left, right, top, bottom: the 4 sides Seen from above left right near far normals point inside
The top and bottom planes look the same from the side as left and right look from above. An orthographic camera has a box instead of a pyramid, but it is still 6 planes, and everything on this page works the same.

A point is inside the frustum when it is on the inner side of all 6 planes. So to work with the frustum, we first need a way to ask "which side of this plane is this point on?".

3A plane, and the distance to it

A plane is stored as 4 numbers: a normal n = (a, b, c), the direction the plane faces, and an offset d. A point p is on the plane when n · p + d = 0. For any other point, the same expression gives a number that is not zero, and its sign tells the side.

inside: distance > 0 outside: distance < 0 the plane n +2 2 units along n 0 −1
The convention used on this page and in most engines: the normals of the frustum planes point into the frustum, so inside is positive.

distance(p) = n · p + d

> 0
p is on the side n points to
= 0
p is on the plane
< 0
p is on the other side
Normalized planes

When n has length 1, the number is the real distance, in the same units as p. To get there, divide all 4 numbers (a, b, c, d) by the length of (a, b, c).

For a single point, only the sign matters, so the length of n does not matter either. For a sphere it does: we will compare the distance with the radius, so both must be in the same units. That is why frustum planes are almost always normalized: scaled so that n has length 1.

4Bounding volumes

A mesh can have millions of triangles. Testing each one against the frustum would cost more than drawing them. Instead, every object gets a simple shape that fully contains it, a bounding volume. If the bounding volume is outside, the object inside it is outside too.

Sphere center + radius: 4 numbers cheapest test · loose on long shapes Axis-aligned box (AABB) min + max corner: 6 numbers cheap · grows when the object turns Oriented box (OBB) center, 3 axes, 3 sizes: 15 numbers tightest · costliest test
A tighter shape culls more often, but costs more to store and to test. A loose shape is still correct, only less useful.

Spheres are the most common choice for frustum culling. They are small, they do not change when the object rotates, and the test is one line. Boxes are a good second choice (see below).

How to build a bounding sphere

The sphere is built once, when the mesh is loaded, from its vertex positions. The smallest possible sphere is not needed; any sphere that contains every vertex is correct.

center

Box center

Find the min and max of all positions. The center is halfway between them. The radius is the distance to the farthest vertex.

Two passes over the vertices. A bit bigger than needed, simple.

a vertex outside → grow just enough

Ritter's method

Start with a sphere through two far-apart points. Walk the vertices; when one is outside, grow the sphere just enough to hold it.

Two or three passes. Usually close to the smallest sphere.

nothing smaller holds them all

Welzl's algorithm

A randomized recursive algorithm that finds the exact smallest sphere.

The smallest sphere. More code, and slower in practice.

5The test: a sphere against the frustum

Take a sphere with center c and radius r, and one plane. Compute the signed distance of the center, s = n · c + d. There are three cases:

inside outside plane A B C s = +80, more than r fully inside → keep crosses the plane → keep s = −20, between −r and 0 s = −50, less than −r fully outside → cull
All three spheres have radius r = 40. The only case that removes the object is "the whole sphere is behind the plane": s < −r.
Distance of the centerMeaningFor culling
s ≥ rthe whole sphere is inside this planekeep testing the other planes
−r ≤ s < rthe sphere crosses the planekeep testing the other planes
s < −rthe whole sphere is outside this planecull, stop: no other plane can bring it back

For the whole frustum, run this for all 6 planes. One "fully outside" is enough to cull. The sphere is kept only when no plane rejects it:

bool sphereInFrustum(float4 planes[6], float3 center, float radius)
{
    for (int i = 0; i < 6; ++i)
    {
        if (dot(planes[i].xyz, center) + planes[i].w < -radius)
            return false;
    }
    return true;
}

The picture below runs this test from above, with 4 of the 6 planes (left, right, near, far). Turn the camera and move the big sphere across an edge of the frustum.

kept: drawn culled (dashed) the frustum; the line to the nearest plane
Drag the orange point in front of the camera to turn it, and the point in the big sphere to move it. A sphere stays as long as any part of it is inside: watch it turn red only when the distance passes −26.

That is 6 dot products and 6 compares per object. Each plane is stored as a float4: the normal in xyz, d in w.

6Where the planes come from

One way to get the planes is geometry: take the camera's position, direction, field of view and aspect ratio, build the 8 corners of the frustum, and make a plane from three corners of each face. It works, but it is a lot of code and easy to get wrong. There is a shorter way that needs only the matrix the camera already has.

The idea: the frustum is a box in clip space

The view matrix V moves the world into the camera's space. The projection matrix P then moves it into clip space. A point p = (x, y, z, 1) becomes q = P · V · p = (qx, qy, qz, qw). The projection is built so that the frustum becomes a simple box there. A point is visible when (with the depth range of Vulkan and Direct3D)

−qw ≤ qx ≤ qw      −qw ≤ qy ≤ qw      0 ≤ qz ≤ qw
Clip space: a box x ≥ −w x ≤ w z ≤ w (far) z ≥ 0 (near) P · V maps one onto the other World space: the frustum r3 + r0 r3 − r0 r3 − r2 r2 camera
Seen from above, so the top and bottom sides (qy) are not drawn. r0 … r3 are the four rows of M = P · V.

From an inequality to a plane

Call the rows of M = P · V r0 to r3. Each part of q is one row times the point: qx = r0 · p and qw = r3 · p. Now take the left side of the box:

−qw ≤ qx
⇔  0 ≤ qw + qx
⇔  0 ≤ r3·p + r0·p
⇔  0 ≤ (r3 + r0)·p

The last line is exactly "p is on the inner side of a plane", with the plane's 4 numbers equal to r3 + r0. No angles, no corners: just add two rows. This trick was published by Gil Gribb and Klaus Hartmann in 2001, and almost every engine uses it. Each side of the box gives one plane:

PlaneClip-space sidePlane =
Leftqx ≥ −qwr3 + r0
Rightqx ≤ qwr3 − r0
Bottom, topqy ≥ −qw, qy ≤ qwr3 + r1, r3 − r1 (which one is "top" depends on the API's y direction; Vulkan's points down)
Near, fardepend on the depth range, see the next table

Near and far depend on the depth convention

Depth conventionClip-space rangeNear planeFar plane
OpenGL−qw ≤ qz ≤ qwr3 + r2r3 − r2
Vulkan, Direct3D, Metal0 ≤ qz ≤ qw, near at 0r2r3 − r2
Reversed-Z (0 to 1 range)0 ≤ qz ≤ qw, near at qwr3 − r2r2

The two inequalities are the same in the last two rows; only their names swap. The code that builds the planes can stay the same. Only the names "near" and "far" change.

An infinite far plane

Some projections have no far plane: the depth only gets closer to its far value and never reaches it. Then the far plane comes out as (0, 0, 0, k) with k > 0. It has no direction, and every point gets the distance k, so every sphere passes it. That is correct: nothing is beyond infinity. Do not divide this plane by the length of its normal, which is 0.

Which space the planes are in

The planes are in the space that the matrix starts from. Use P alone and they are in view space. Use P · V and they are in world space, which is the usual choice. Use P · V · W (with one object's world matrix W) and they are in that object's own space.

void extractPlanes(const glm::mat4 &viewProjection, glm::vec4 planes[6])
{
    // glm stores columns: transpose to get the rows.
    const glm::mat4 rows = glm::transpose(viewProjection);
    planes[0] = rows[3] + rows[0];
    planes[1] = rows[3] - rows[0];
    planes[2] = rows[3] + rows[1];
    planes[3] = rows[3] - rows[1];
    planes[4] = rows[2];
    planes[5] = rows[3] - rows[2];
    for (int i = 0; i < 6; ++i)
    {
        const float length = glm::length(glm::vec3(planes[i]));
        if (length > 0.0f)
            planes[i] /= length;
    }
}

Left, right, bottom, top, then the two depth planes for a 0 to qw depth range. A plane with no direction (an infinite far plane) is left as it is.

7Worked example

A small case you can check by hand. The camera sits at the origin and looks down −Z, with a 90° field of view and the near plane at distance 1. With 90°, the side planes are at 45°, so their normals are (±0.707, 0, −0.707). Four spheres, each with radius 1:

left plane right plane near plane (z = −1) camera (0, 0, 0) −Z: the view direction +X A B C D
30 pixels per unit. Top and bottom planes are left out; all four spheres sit at y = 0 and pass them.
SphereCenterNear
(0, 0, −1, −1)
Left
(0.707, 0, −0.707, 0)
Right
(−0.707, 0, −0.707, 0)
Result
A(0, 0, −5)4.003.543.54keep: inside all
B(3.5, 0, −3)2.004.60−0.35keep: −0.35 ≥ −1, it crosses the right plane
C(6, 0, −3)2.006.36−2.12cull: −2.12 < −1, fully right of the view
D(−2, 0, 1)−2.00−2.120.71cull: behind the near plane (the test stops there)

For example, C against the right plane: −0.707 · 6 + (−0.707) · (−3) + 0 = −4.24 + 2.12 = −2.12.

8Moving the sphere into world space

The sphere is built in the mesh's own space. The planes are in world space. So each frame, the sphere is moved with the object's world matrix W. The center is easy: it is a point, so it is W · (c, 1). The radius is harder, because W can scale the object.

Mesh space r = 1 scale (3, 1) World space r × 1: too small, the mesh pokes out r × 3 = 3 the largest stretch: always holds it
The radius must grow by the largest stretch of the matrix in any direction. Using a smaller number makes the sphere too small, and objects at the edge of the screen vanish too early.

The exact largest stretch of a matrix is its largest singular value. Computing that is too slow for every object every frame, so engines use a cheap number that is never smaller than it:

Way to get the stretchCostCorrect?
Longest column of the 3×3 part of W3 dot productsExact for rotate + scale. Can be too small when the matrix also has shear, for example a child rotated under a parent with a non-uniform scale.
Frobenius norm: the square root of the sum of all 9 entries squared9 multipliesAlways safe, but up to 1.73 (√3) times too big.
Gershgorin bound on WTW: take the dot products of the columns, and the largest row sum of their absolute values6 dot productsAlways safe. Exact for rotate + scale, because then the columns are at right angles and the off-diagonal products are 0.

9Boxes instead of spheres

A box is tested almost the same way as a sphere. Store the box as a center c and a half-size e (half its width, height and depth). For one plane, the part of the box that reaches furthest towards the plane is a corner. How far that corner reaches depends on the plane's direction, so the box acts like a sphere whose radius changes per plane:

inside plane n box the corner farthest along n is inside crosses → keep even that corner is outside → cull
The p-vertex test. If even the corner that reaches furthest into the inside is outside, every corner is outside.

s = n · c + d

r = e.x · |n.x| + e.y · |n.y| + e.z · |n.z|

s < −r → cull

s
the signed distance of the box's center, as for a sphere
r
how far the box reaches along n: the distance from c to the corner farthest along n (the "p-vertex")

It is the same test as the sphere, with a radius that depends on the plane.

Boxes have one more nice property. The test only needs the sign of the corner's distance, not its size. So the planes do not need to be normalized, and they can be moved into the object's own space instead of moving the box into world space: a plane π becomes WT · π. The box then stays exact under any rotation and scale, with no stretch bound.

10The corner problem: false positives

The test asks one plane at a time: "is the sphere fully behind this plane?". Near a corner of the frustum, a sphere can be outside the frustum and still cross each plane on its own. Then no plane rejects it, and it is kept.

far plane, extended left plane, extended kept, but not visible frustum the corner
A false positive: the object is kept but nothing of it is on screen. The sphere crosses each plane outside the frustum's faces, past the corner. The test stays conservative, so this is a cost, not a bug.
−0.7 rdistance to the far plane: the sphere crosses its ≥ −r
−0.7 rdistance to the left plane: the sphere crosses its ≥ −r
keptno plane rejects it, so it is drawnone wasted draw
neverdoes this remove a visible objectstill conservative

False positives happen most for big objects and near sharp corners. Most engines accept them. When they matter, there are two fixes:

the box all corners beyond this face

Test the other way round

Also check the frustum's 8 corners against the object's box. If all 8 are outside one face of the box, the box is outside.

A second, cheap test

a line that separates them

Separating-axis test

A full test of the two shapes against each other. It is exact.

Costs much more

11Fewer tests: hierarchies, and the GPU

The test is cheap, but a big scene has many objects. There are two common ways to keep the total cost low.

Test groups first

Put objects into a tree: a scene graph, an octree, or a bounding volume hierarchy (BVH). Each node has a bounding volume around all its children. Test the node first:

A: outside skip it and all children B: inside all planes keep all, no more tests C: crosses test each child
One test can accept or reject a whole group. Only groups that cross a plane go one level down.

A related trick is the plane mask. If a node is fully inside, say, the left plane, all its children are too. So the node passes down a mask of the planes still worth testing, and its children skip the others.

Test everything at once on the GPU

A GPU can run the flat test for every object in parallel, one thread per object. For tens of thousands of objects this takes a fraction of a millisecond, often less than walking a tree on the CPU. The tree tricks matter less there, because the GPU does not branch well and wants the same work on every thread. The hard part on the GPU is what comes next: packing the kept objects into a list that a single indirect draw can read. That is stream compaction, which has its own guide.

Other views use the same code

Shadow maps, reflections and split screens each have their own camera, so each gets its own 6 planes and its own culling. Nothing else changes.

12In RASM

RASM frustum-culls every draw (one sub-mesh of one entity) on the GPU, every frame, in the Cull pass that Renderer::addScenePasses adds for each scene view (a scene, a camera and a target). It uses the plain sphere test from section 5, with planes taken from the matrix as in section 6.

CPU · at load computeBounds box center + farthest vertex Geometry table boundsCenter · boundsRadius CPU · each frame, per scene view getFrustumPlanes projection × view SceneData frustumPlanes[6] GPU · countMain, one thread per draw getWorldSphere × world matrix, radius × stretch isInFrustum 6 planes: keep or cull
The sphere is built once, the planes once per scene view and frame (Renderer::prepareSceneView), and the test runs once per draw.

A skinned mesh changes shape, so its sphere from the load does not hold its posed vertices. For a playing skinned sub-mesh, the Animate pass writes a sphere in its InstanceTransform each frame (boundsCenter, boundsRadius): the sphere around its joints' spheres, each moved by its joint matrix. getWorldSphere takes that one when boundsRadius is not negative, so every view culls the posed mesh (Skinning).

The sphere from the load the hand is outside bind-pose sphere: wrong once the mesh moves Joint spheres, moved each frame the sphere around them holds every vertex
The joint spheres are built at load around the bind-pose vertices each joint moves. Each frame they move with their joints, and the sphere around all of them holds every skinned vertex: a skinned vertex is a weighted mix of points that each lie inside one moved sphere, and a sphere is convex.

A morphed mesh changes shape too. A morph target t with weight wₜ moves a vertex by at most |wₜ| · rₜ, where rₜ is the target's longest position delta (AnimationMeshData::morphRadii, from the load). The moves add up, so the Animate pass grows the radius by their sum (getMorphGrowth).

No skin: grow the geometry sphere r + Σ |wₜ| · rₜ every view culls the grown sphere Skinned: grow each joint sphere first (rⱼ + growth) · stretch, then the sphere around all joint spheres the deltas are in the bind pose, so the growth comes before the joint
Gray: the sphere from the load. Green: the sphere the Animate pass writes this frame. A weight of 0 adds nothing, so a mesh at rest weights 0 keeps its own sphere.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Bounding volumesphere or boxA sphere per sub-mesh only (boundsCenter, boundsRadius). No boxes, so no p-vertex test. A posed skinned or morphed sub-mesh uses the sphere the Animate pass writes from its joints and its weights (above).
Building the spherebox center, Ritter or WelzlBox center, radius to the farthest vertex (computeBounds). Not the smallest sphere.
PlanesGribb–Hartmann rowsThe same, from projection * view, in the view's render space (the world moved so that the camera is at the origin, large worlds), normalized (getFrustumPlanes).
Depth conventionsee the table in section 6Vulkan's 0 to qw range with reversed-Z. So plane 4 (r2) is the far plane and plane 5 (r3 − r2) is the near plane. See Projection and reversed-Z depth.
Infinite far plane(0, 0, 0, k)The default camera has one. Plane 4 comes out as (0, 0, 0, nearZ), is not normalized, and every sphere passes it.
Radius under scalethree ways in section 8The Gershgorin bound (getWorldSphere). Safe for any matrix, exact for rotate + scale.
Hierarchy, plane maskssection 11None. Every draw is tested against all 6 planes, flat, one GPU thread each.
Corner false positivesaccept, or add a second testAccepted. Many of them are then removed by occlusion culling, which runs the frustum test first.
Planes per viewone set per cameraOne set per scene view, in that view's SceneData slot. Two cameras are two views, and cull separately.
Seeing the test–Renderer::setCullingFrozen keeps a shaded view's planes (and its LOD camera) of the frame it froze in, while the camera moves on; shadow views keep following the camera. The culling debug view (DebugView::CULLING) draws what the planes cull as blue ghosts (Debug views).

After the test, the kept draws are packed into one list and drawn with one indirect call per run of one alpha mode and cull mode. That part is stream compaction. The occlusion and LOD steps run in the same shader.

Where it lives in code

WhatWhere
Bounding sphere per sub-meshcomputeBounds in src/renderer/geometryPool.cpp
Planes from the matrixgetFrustumPlanes in src/renderer/gpuScene.cpp, called by Renderer::prepareSceneView
Where the planes are storedSceneData::frustumPlanes in shaders/lib/core.slang and src/renderer/uberMaterialTypes.h
Sphere into world spacegetWorldSphere in shaders/lib/frustumCulling.slang
The testisInFrustum in shaders/lib/frustumCulling.slang
Where it is calledcountMain in shaders/passes/cull.slang
The projection (reversed-Z, infinite far)toProjectionMatrix in src/scene/camera.cpp
Keep these true
  • The test must stay conservative. A smaller radius or a tighter stretch bound that can be too small makes objects vanish at the screen's edges.
  • Plane normals point inside, and inside is dot(xyz, p) + w >= 0, as the comment on frustumPlanes in core.slang says. getFrustumPlanes and isInFrustum must agree.
  • Do not normalize a plane whose normal has length 0. That is the infinite far plane.
  • SceneData is a CPU↔GPU struct: a change to it follows the struct layout rules.

Further reading