Culling and the frame

LOD selection

A mesh with levels of detail (LODs) has several versions, from full detail to very coarse. Each frame, the renderer picks one level per object. The goal: the coarsest level that still looks the same as the full one. This page explains how to decide that, from simple distance rules to the screen-space error, and how to hide the moment a level changes. The last part shows how RASM does it. How the levels are made is in Mesh simplification.

In short

  • Each level has a geometric error e: how far its surface may be from the full mesh, in the mesh's own units.
  • Project that error to the screen: pixels = e × s × P11 × H / 2 / w, with the object's scale s, the projection's P11, the screen height H and the view depth w.
  • Pick the coarsest level whose error stays under a limit, usually about 1 pixel.
  • Use the view depth (along the view direction), not the straight distance, and the bounding sphere's nearest point. Both keep the choice conservative.
  • An orthographic camera has no depth division: the level only changes with the zoom.
  • A level change can pop. Hysteresis, cross-fading and a small pixel limit hide it.

1The problem

camera LOD 0 1 2 4 switch? switch? switch? The same object, smaller on screen the further it is. Where should each level start?
The rule runs for every object, every frame, so it must be cheap. It should never pick a level whose difference you can see, and it should not flicker between two levels.
one thread per object

Cheap

A few multiplications per object. It often runs on the GPU, one thread per object.

under 1 px full mesh · coarse level

Correct

The difference to the full mesh stays under what the eye can see: about a pixel.

flickers between two levels stays on one level time →

Stable

An object that barely moves does not switch back and forth.

2Distance thresholds

The simplest rule: an artist sets a distance per level. Closer than 10 m: LOD 0. 10 to 25 m: LOD 1. And so on.

camera LOD 0 LOD 1 LOD 2 LOD 3 10 m 25 m 60 m
Seen from above. Distance rules are easy to understand and still common in tools, but they need tuning per mesh and per camera.

The distances ignore four things:

1080 pixels high 2160 pixels high

The screen's resolution

4K shows more detail.

wide view zoomed in

The field of view

Zooming in makes things big.

× 1 × 3

The object's scale

A tree scaled ×3 is three times bigger at the same distance.

10 m? 25 m? 4 m? 12 m?

The mesh

Each mesh needs its own numbers, set by hand, and checked again after every change to the mesh.

A better variant uses the object's size on screen instead of its distance: the projected height of its bounding sphere, as a fraction of the screen. Unity's LOD Group and Unreal's static mesh LODs both work this way. It fixes the resolution, field-of-view and scale problems, because they all change the size on screen. It still needs a threshold per mesh, because one mesh's LOD 1 may be much worse than another's.

3The screen-space error

The simplifier already knows how bad each level is: it reports a geometric error e, the largest distance between the level's surface and the full mesh, in the mesh's own units (see Mesh simplification). If we turn that distance into pixels, one rule works for every mesh:

The rule

Pick the coarsest level whose error, seen from the camera, is at most τ pixels. With τ = 1, the difference to the full mesh is never more than one pixel.

error in metres on screen at 20 m LOD 0 · 0 1 · 0.005 2 · 0.012 3 · 0.03 4 · 0.08 0 px 0.25 px 0.59 px 1.48 px 3.94 px limit: 1 px ↑ the coarsest level under the limit
The errors grow from level to level, so the levels under the limit are always the first few. The rule picks the last of them. The numbers are for a 60° field of view and a 1080-pixel-high screen, worked out in section 7.

4From an error in metres to pixels

How big is a length L on screen, when it stands at right angles to the view direction, at view depth w? Look at the camera from the side:

eye view direction image plane: NDC −1 … +1 = H pixels L image of L w: the view depth
Similar triangles: the image shrinks with 1 / w. The projection matrix adds the field of view, and the viewport turns normalized device coordinates (NDC) into pixels.

Step by step, for a perspective projection matrix P:

1. clip space:   y_clip = P11 · y_view,   w_clip = w (the view depth)
2. NDC:          y_ndc  = P11 · y_view / w
3. a length L:   Δy_ndc = P11 · L / w
4. to pixels:    2 NDC units = H pixels, so 1 NDC unit = H / 2 pixels
5. so:           pixels = L · P11 · H / (2 · w)

Here P11 is the matrix entry at row 1, column 1 (counting from 0). For a perspective projection with a vertical field of view θ, P11 = 1 / tan(θ / 2): 1.73 for 60°. The error is stored in the mesh's units, and the object's world matrix can scale it by s, so L = e × s:

pixels = e × s × P11 × H / 2 / w

Only w changes per object, and the levels only change e. So the cheap way is to turn the rule around once per object, and compare each level's error with a limit in mesh units:

pixelsPerUnit = P11 × H / 2 / w
eMax          = τ / (pixelsPerUnit × s)
level         = the last level with e ≤ eMax

The horizontal direction gives the same answer: P00 is P11 divided by the aspect ratio, and the screen is wider by the same ratio.

5Depth, not distance

The projection divides by the depth along the view direction, not by the straight distance to the object.

view direction camera distance: 30 m depth w: 23 m 40° The same error, on screen divided by the depth, 23: its true size divided by the distance, 30: 23% smaller The level looks better than it is: too coarse a level is picked.
A planar projection divides by the depth w, the distance along the view direction. Off the centre of the screen, the straight distance is longer.
30 mthe straight distance to the object, 40° off the centre
23 mits depth w30 × cos 40°
23%how much smaller the error looks when you divide by 30 instead of 231 − 23 / 30

Near the edges of a wide view, a distance-based rule picks levels that are too coarse. The depth is also cheaper: it is the w of the point in clip space, one row of the matrix.

6Staying conservative

Like culling, LOD selection should only err on the safe side: it may pick a level finer than needed (that costs a little time), never a coarser one (that shows). Each input has a safe choice:

camera r w − r: measure the error here w: depth of the center camera inside w − r ≤ 0 → always LOD 0
An object is not a point. Its closest part may be much nearer than its center, and the error of a level can sit there.
InputSafe choiceWhy
Depthw − r, the nearest point of the bounding sphereThe error can be anywhere on the object. The nearest point shows it biggest.
Camera near or insidew − r ≤ 0 → LOD 0The formula divides by zero or flips sign there.
Scalethe world matrix's largest stretchA non-uniform scale stretches the error most along one axis. The same bound grows the bounding sphere for culling (see Frustum culling).
Error of a levelan upper bound, e.g. the sum of the steps' errorsA smaller number than the truth would show the difference.
Direction of the errorassume it is at right angles to the viewAn error along the view direction looks smaller on screen, but we do not know its direction.

7Worked example

A 60° vertical field of view (P11 = 1.732), a screen 1080 pixels high, a limit τ = 1 pixel. So at depth w, one metre is 1.732 × 1080 / 2 / w = 935.3 / w pixels. A mesh in metres, scale 1, with a bounding sphere of radius 1 m, and these levels:

LevelError eUnder 1 px from depth w − r = e × 935.3
LOD 00always
LOD 10.005 m4.7 m
LOD 20.012 m11.2 m
LOD 30.03 m28.1 m
LOD 40.08 m74.8 m

Now move the object away. Click through (or use the arrow keys). The bars are each level's error on screen; the limit is the dashed line.

1. The sphere at 5 m: LOD 0

sphere center at depth 5 m, radius 1 m: w − r = 4 m pixels per metre there: 935.3 / 4 = 233.8 limit: 1 px 0 px LOD 1 · error 0.005 m 1.17 px LOD 2 · error 0.012 m 2.81 px LOD 3 · error 0.03 m 7.01 px → LOD 4 · error 0.08 m 18.71 px → → LOD 0: no level is under 1 px

Close up, even LOD 1 would move the surface by more than a pixel. The full mesh is drawn.

2. The sphere at 20 m: LOD 2

sphere center at depth 20 m, radius 1 m: w − r = 19 m pixels per metre there: 935.3 / 19 = 49.2 limit: 1 px 0 px LOD 1 · error 0.005 m 0.25 px LOD 2 · error 0.012 m 0.59 px LOD 3 · error 0.03 m 1.48 px LOD 4 · error 0.08 m 3.94 px → LOD 2: the coarsest level under 1 px

LOD 1 and 2 stay under a pixel; LOD 3 would show 1.5 pixels. LOD 2 draws a quarter of the triangles.

3. The sphere at 60 m: LOD 3

sphere center at depth 60 m, radius 1 m: w − r = 59 m pixels per metre there: 935.3 / 59 = 15.9 limit: 1 px 0 px LOD 1 · error 0.005 m 0.08 px LOD 2 · error 0.012 m 0.19 px LOD 3 · error 0.03 m 0.48 px LOD 4 · error 0.08 m 1.27 px → LOD 3: the coarsest level under 1 px

Three levels are under the limit now.

4. The sphere at 200 m: LOD 4

sphere center at depth 200 m, radius 1 m: w − r = 199 m pixels per metre there: 935.3 / 199 = 4.7 limit: 1 px 0 px LOD 1 · error 0.005 m 0.02 px LOD 2 · error 0.012 m 0.06 px LOD 3 · error 0.03 m 0.14 px LOD 4 · error 0.08 m 0.38 px → LOD 4: the coarsest level under 1 px

Every level is under a pixel. The coarsest one is drawn: about 1/16 of the triangles.

A 4K screen

H = 2160 doubles every pixel value, so every switch depth doubles too: LOD 1 from 9.4 m, LOD 4 from 150 m. A sharper screen keeps the detail longer, with no new numbers.

Zooming in

A 20° field of view gives P11 = 5.67, 3.27 times more. The switch depths grow by the same factor, so a zoomed-in far object gets its full detail back.

Now try it with your own numbers. The bands on top are the depths where each level is used.

Move the depth to 5, 20, 60 and 200 m: you get the four pictures above. Then change the screen height or the field of view, and watch every switch depth move by the same factor.

8The orthographic case

camera plane L, near L, far The rays are parallel: the image of L has the same size at every depth.
An orthographic projection has no perspective division: w = 1 everywhere.
orthographic:  P11 = 2 / (top − bottom),  w = 1
pixels = e × s × P11 × H / 2 = e × s × H / (top − bottom)

The depth drops out. A view that shows 40 m from top to bottom on a 1080-pixel screen has 27 pixels per metre, near or far. The level changes only when the view zooms (changes top − bottom). This is the common case for map engines and CAD views. A matrix tells which case it is: a perspective matrix has 0 in its bottom-right corner, an orthographic one has 1.

9Popping, and how to hide it

When an object switches levels, its shape changes in one frame. If the change is big enough to see, it pops. With τ = 1 pixel the change is at most a pixel, which is hard to see on a moving image. A larger τ (to save more triangles) makes popping visible, and then one of these helps:

Hysteresis

depth → LOD 1 LOD 2 keep the current level moving away: switch at d coming back: switch at d / k d / k d
Two thresholds instead of one: go coarser at τ, go finer only at a bigger error, for example 1.3 τ. It needs to remember each object's last level, which is extra state per object.

Cross-fading and dithering

For a short time after a switch, draw both levels and blend from the old to the new. Real alpha blending needs sorting and has problems with depth. So engines use dithering (screen-door transparency): each level draws only some pixels, chosen by a fixed pattern, and the two patterns fill the screen together. Over a few frames, more pixels go to the new level.

a pixel from the old level a pixel from the new level
25% new 50% new 75% new
Blue: a pixel from the old level. Green: a pixel from the new one. Each level discards the other's pixels in its pixel shader. This is a 4 × 4 ordered (Bayer) pattern; temporal anti-aliasing or a blur hides the pattern. The cost: both levels are drawn while the fade runs, and the pixel shader discards pixels, which can turn off some early depth tests.

The techniques side by side

before and after the switch: 1 px apart

A small limit (τ ≈ 1 px)

Hides almost everything.

Cost: fewer triangles saved

between the two: no change

Hysteresis

Hides flicker at a threshold.

Cost: one remembered level per object

both levels, mixed pixel by pixel

Dithered cross-fade

Hides the pop itself.

Cost: two draws during the fade, a timer per object, a dither pattern

slides over time fine → coarse

Geomorphing

Hides the pop itself, smoothly. Vertices move from their fine to their coarse position over time.

Cost: needs to know where each vertex goes (progressive meshes, terrain)

10Variants

0.5 px 1 px 2 px high low the limit τ

Quality setting

Scale τ: 2 pixels on a low setting, 0.5 on a high one.

budget triangles drawn raise τ until the scene fits

Triangle budget

Raise τ until the scene fits a fixed number of triangles. Keeps the frame time steady, at a varying quality.

fine, near coarse, far one object, many clusters

Per-cluster levels

Split a mesh into small clusters and pick a level per cluster, not per object, from a hierarchy of clusters. One big object (a terrain, a building) then has fine detail near the camera and coarse detail far away. Brian Karis's Nanite talk (2021) describes one such system.

the shadow uses the same level

Other views

Shadow maps and reflections have their own cameras. Using the main camera's level for them avoids a shadow whose shape differs from the object.

11In RASM

RASM picks a level on the GPU, inside the culling pass, for every draw that passed the frustum and occlusion tests. It uses the screen-space error rule of this page, with τ = EngineConfig::lodErrorPixels (1 pixel by default). The levels and their errors come from Mesh simplification. With MeshletPath::VERTEX_PULLING, a level with 8 meshlets or more is then culled again per meshlet (Meshlets and cluster culling).

countMain · one thread per draw getWorldSphere center, radius, scale frustum, Hi-Z visible? selectLod coarsest under τ cullSlotsPtr[draw] slot | lod << 16 SceneData lodErrorPixels, forcedLod GeometryData row lodCount, lods[i].error writeMain · one thread per draw getLodDraw firstIndex, indexCount visible draw with the level's indices
The draw list on the CPU always holds LOD 0's index range. Culling writes the packed visible draws with the chosen level's range instead, so the vertex shader never knows which level it draws: every level shares the vertices.
float pixelsPerUnit = scene->lodPixelsPerUnit;
if (any(scene->lodDepthPlane != float4(0.0)))
{
    float depth = dot(scene->lodDepthPlane, float4(sphere.center, 1.0)) - sphere.radius;
    if (depth <= 0.0)
        return 0;
    pixelsPerUnit /= depth;
}
float limit = scene->lodErrorPixels / (pixelsPerUnit * sphere.scale);

The core of selectLod, shortened. Then it loops over the levels and keeps the last one with lods[i].error <= limit. The CPU (prepareSceneView) works out the camera's part once per view: lodPixelsPerUnit is |P11| × H / 2, and lodDepthPlane is row 3 of projection × view, so its dot product with a point is the point's w.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Rulescreen-space error, coarsest level under τThe same. τ is EngineConfig::lodErrorPixels, copied into each scene view's SceneData every frame. 0 or less always draws LOD 0.
Formulae × s × P11 × H / 2 / w, turned around once per objectThe same, turned around into limit. H is the height of the target the LOD camera's view draws into. The absolute value of P11 is used.
Depthview depth of the sphere's nearest pointw of the sphere's center (lodDepthPlane) minus its radius. Zero or less: LOD 0.
Scalethe largest stretchSphere::scale from getWorldSphere, the same bound frustum culling uses for the radius.
Orthographicno depth divisionFor an orthographic camera lodDepthPlane is all zeros and pixelsPerUnit is not divided by a depth.
What gets a levelan objectEach draw: one sub-mesh of one entity. Sub-meshes of one model can draw different levels; their borders are locked, so they still meet.
Whenevery frameEvery frame and scene view, in countMain, only for visible draws. A scene view drawn without the culling pipelines draws LOD 0.
Hysteresis, cross-fade, geomorphsection 9None. The choice is made from scratch every frame and keeps no state, and a switch happens in one frame. With the 1-pixel default, popping is hard to see.
Debugging—EngineConfig::forcedLod ≥ 0 forces that level (or the mesh's coarsest). F2 cycles it: by error (−1), LOD 0, 1, … 7, by error. The stats overlay (F1) shows the mode, the visible draws per level, and the triangles saved against LOD 0. The LOD debug view (DebugView::LODS, Debug views) tints each draw by the level it draws.
Skinned and morphed meshes—A level is an index range into the sub-mesh's vertices, and a skinned or morphed sub-mesh's vertices are the posed ones the Skin pass writes. So every level draws posed, with no work per level. The sphere is the posed one (getWorldSphere), and the levels were simplified in the base shape without looking at the skin weights or the morph deltas (D29), so a joint that bends far, or a large morph, can show a coarse level's error larger than its number (skinning, morph targets).
Other views, budgets, clusterssection 10One LOD camera per scene view: its own, except a shadow map's view, which uses the camera of the view that reads the map, so caster and surface match (shadow mapping). A level per draw, no triangle budget.

Where it lives in code

WhatWhere
The ruleselectLod in shaders/lib/lodSelection.slang
Swapping the index rangegetLodDraw, getDrawGeometry in shaders/lib/lodSelection.slang
Where it is calledcountMain (picks, counts, packs with CULL_LOD_SHIFT) and writeMain (unpacks, writes) in shaders/passes/cull.slang
Sphere and scalegetWorldSphere in shaders/lib/frustumCulling.slang
Settings on the GPUSceneData::lodErrorPixels, SceneData::forcedLod, viewportHeight in shaders/lib/core.slang; filled in Renderer::prepareSceneView, src/renderer/gpuScene.cpp
Settings for the appEngineConfig::lodErrorPixels, EngineConfig::forcedLod, EngineConfig::meshLods in include/rasm/types.h
The F2 keySDLK_F2 in src/platform/sdlPlatform.cpp
Stats per levelSTATS_LOD_DRAWS in cull.slang; drawsPerLod, trianglesVisibleLod0 in src/renderer/frameStats.cpp; shown by src/renderer/statsOverlay.cpp
Keep these true
  • Stay conservative: the nearest point of the sphere, the largest stretch, and an error that is an upper bound. A tighter number shows the difference.
  • A level's error never gets smaller from one level to the next, and LOD 0's is 0.
  • The level goes above the slot in cullSlotsPtr (slot | lod << CULL_LOD_SHIFT), so a group's slot must stay under 216; it is at most 63.
  • The CPU draw list keeps LOD 0's range; only writeMain writes a coarser one, into the visible draws.
  • SceneData and GeometryData are CPU↔GPU structs: a change follows the struct contract.
Not done yet

No hysteresis or cross-fade, no quality scale beyond lodErrorPixels, no triangle budget, no per-cluster levels. Levels are always in GPU memory: there is no streaming of fine levels for near objects only (a whole mesh can be destroyed and loaded again). A skinned or morphed mesh's levels ignore its weights and deltas (D29).

Further reading