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
Cheap
A few multiplications per object. It often runs on the GPU, one thread per object.
Correct
The difference to the full mesh stays under what the eye can see: about a pixel.
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.
The distances ignore four things:
The screen's resolution
4K shows more detail.
The field of view
Zooming in makes things big.
The object's scale
A tree scaled ×3 is three times bigger at the same distance.
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:
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.
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:
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.
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:
| Input | Safe choice | Why |
|---|---|---|
| Depth | w − r, the nearest point of the bounding sphere | The error can be anywhere on the object. The nearest point shows it biggest. |
| Camera near or inside | w − r ≤ 0 → LOD 0 | The formula divides by zero or flips sign there. |
| Scale | the world matrix's largest stretch | A 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 level | an upper bound, e.g. the sum of the steps' errors | A smaller number than the truth would show the difference. |
| Direction of the error | assume it is at right angles to the view | An 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:
| Level | Error e | Under 1 px from depth w − r = e × 935.3 |
|---|---|---|
| LOD 0 | 0 | always |
| LOD 1 | 0.005 m | 4.7 m |
| LOD 2 | 0.012 m | 11.2 m |
| LOD 3 | 0.03 m | 28.1 m |
| LOD 4 | 0.08 m | 74.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
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
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
Three levels are under the limit now.
4. The sphere at 200 m: LOD 4
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.
8The orthographic case
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
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.
The techniques side by side
A small limit (τ ≈ 1 px)
Hides almost everything.
Cost: fewer triangles saved
Hysteresis
Hides flicker at a threshold.
Cost: one remembered level per object
Dithered cross-fade
Hides the pop itself.
Cost: two draws during the fade, a timer per object, a dither pattern
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
Quality setting
Scale τ: 2 pixels on a low setting, 0.5 on a high one.
Triangle budget
Raise τ until the scene fits a fixed number of triangles. Keeps the frame time steady, at a varying quality.
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.
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).
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
| Part | This page | RASM |
|---|---|---|
| Rule | screen-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. |
| Formula | e × s × P11 × H / 2 / w, turned around once per object | The 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. |
| Depth | view depth of the sphere's nearest point | w of the sphere's center (lodDepthPlane) minus its radius. Zero or less: LOD 0. |
| Scale | the largest stretch | Sphere::scale from getWorldSphere, the same bound frustum culling uses for the radius. |
| Orthographic | no depth division | For an orthographic camera lodDepthPlane is all zeros and pixelsPerUnit is not divided by a depth. |
| What gets a level | an object | Each draw: one sub-mesh of one entity. Sub-meshes of one model can draw different levels; their borders are locked, so they still meet. |
| When | every frame | Every frame and scene view, in countMain, only for visible draws. A scene view drawn without the culling pipelines draws LOD 0. |
| Hysteresis, cross-fade, geomorph | section 9 | None. 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, clusters | section 10 | One 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
| What | Where |
|---|---|
| The rule | selectLod in shaders/lib/lodSelection.slang |
| Swapping the index range | getLodDraw, getDrawGeometry in shaders/lib/lodSelection.slang |
| Where it is called | countMain (picks, counts, packs with CULL_LOD_SHIFT) and writeMain (unpacks, writes) in shaders/passes/cull.slang |
| Sphere and scale | getWorldSphere in shaders/lib/frustumCulling.slang |
| Settings on the GPU | SceneData::lodErrorPixels, SceneData::forcedLod, viewportHeight in shaders/lib/core.slang; filled in Renderer::prepareSceneView, src/renderer/gpuScene.cpp |
| Settings for the app | EngineConfig::lodErrorPixels, EngineConfig::forcedLod, EngineConfig::meshLods in include/rasm/types.h |
| The F2 key | SDLK_F2 in src/platform/sdlPlatform.cpp |
| Stats per level | STATS_LOD_DRAWS in cull.slang; drawsPerLod, trianglesVisibleLod0 in src/renderer/frameStats.cpp; shown by src/renderer/statsOverlay.cpp |
- 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
writeMainwrites a coarser one, into the visible draws. SceneDataandGeometryDataare CPU↔GPU structs: a change follows the struct contract.
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
- David Luebke and others, Level of Detail for 3D Graphics (2002): selection, hysteresis, blending and geomorphing.
- Tomas Akenine-Möller and others, Real-Time Rendering, 4th edition: the section on level of detail.
- Hugues Hoppe, View-Dependent Refinement of Progressive Meshes, SIGGRAPH 1997: screen-space error for refining a mesh.
- Brian Karis, Rune Stubbe and Graham Wihlidal, Nanite: A Deep Dive, SIGGRAPH 2021 Advances in Real-Time Rendering: per-cluster levels.