Culling and the frame

Meshlets and cluster culling

Frustum and occlusion culling keep or drop a whole draw. A big mesh that is only partly seen is then drawn whole. Meshlets fix that: the mesh is cut into small pieces when it loads, each piece gets its own bounds, and the GPU culls each piece again. This page explains the pieces, the three tests and how the survivors are drawn. The last part shows how RASM does it.

In short

  • A meshlet (also called a cluster) is a small piece of a mesh: about 64 vertices and 124 triangles that lie close together.
  • Each meshlet has a bounding sphere and a normal cone: every triangle's normal lies inside the cone.
  • Three cheap tests per meshlet: outside the frustum, all triangles facing away (the cone test), or hidden behind the Hi-Z.
  • Culling runs in two levels: first the whole draw (and its LOD), then the meshlets of the draws that pass.
  • The survivors are drawn as one indirect draw per meshlet, or by task and mesh shaders on GPUs that have them.
  • It pays when triangles are the cost: big, dense meshes near the camera. With good LODs there is often little left to save.

1Why a draw is too big to cull

A draw is all or nothing. If one corner of a terrain tile is in view, the GPU runs the vertex shader on every vertex of the tile. A rock in front of the camera shows only its front half, but the back half is drawn too; the rasterizer then throws those triangles away:

the whole tile and the whole rock are drawn

One test per draw

The tile touches the view, so all of it is drawn. The rock's back half is drawn and then dropped.

only the pieces that can be seen are drawn

One test per meshlet

Pieces outside the view, pieces that face away and pieces hidden behind a hill are skipped before any vertex work.

LODs help with far meshes: a far rock is drawn with few triangles. They do not help with a mesh the camera is near or inside, like terrain under the camera: its nearest point decides, so it stays at the finest level. Meshlets cull inside such a mesh.

2What a meshlet is

A tool cuts the mesh into pieces with at most V vertices and T triangles. It grows each piece from a start triangle, adding the neighbour that keeps the piece small and round, so a piece is a compact patch of the surface:

A mesh cut into meshlets Stored as a range of indices 372 indices firstIndex + indexCount per meshlet: an ordinary indexed draw Stored for a mesh shader ≤ 64 vertex numbers (32 bit) ≤ 124 × 3 local indices (8 bit) each workgroup loads its vertices once and emits its triangles: no index buffer needed
The same triangles, cut into four meshlets. An indexed draw needs only a range of the index list; a mesh shader wants a small vertex list and 8-bit local indices.

The sizes come from the hardware that draws them. A mesh shader workgroup writes its vertices and triangles into on-chip memory, so the limits are about that memory:

WhoVerticesTrianglesWhy
NVIDIA (Turing and later)64124 to 126Output memory is allocated in blocks; 126 fits, and 124 keeps the 8-bit index list a multiple of 4 bytes.
AMD (RDNA 2)6464 to 128Older AMD GPUs like the same number of vertices and triangles.
Indexed draws (no mesh shaders)anymore is cheaperEach meshlet is one draw command; fewer, bigger meshlets mean fewer commands, but they cull less.

64 vertices and 124 triangles is the usual middle ground, and what meshoptimizer suggests for cone culling. One mesh needs one set of meshlets per LOD.

3The bounds of a meshlet

Each meshlet stores two shapes. The sphere is the same kind of bound as a whole mesh's, for the frustum and Hi-Z tests. The normal cone is new: an axis and an angle such that every triangle's normal lies inside it.

sphere: frustum and Hi-Z tests triangle normals axis half angle θ normal cone: every normal is within θ of the axis
The four normals moved to one point show the cone. A flat patch has a narrow cone; a patch that wraps around a corner has a wide one, and a cone of 90° or more can never be culled.

4The cone test: all triangles face away

A triangle faces away from the camera when the camera is behind its plane. For a whole meshlet: if the camera is far enough behind the cone, it is behind every triangle's plane, and the meshlet can be skipped. With the sphere (center c, radius r), the cone (axis a, cutoff s = sin θ) and the camera at e:

dot(c − e, a) ≥ s · |c − e| + r  → culled

c − e
from the camera to the meshlet
s
the cone's cutoff, sin θ; a wide cone needs the camera far behind it
r
the sphere's radius: the triangles are spread over it, not at its center

An orthographic view (a sun's shadow map) looks the same way from everywhere, so the test is only dot(view direction, a) ≥ s. The test is conservative: it never culls a triangle that faces the camera, and it misses some that face away. Drag the camera around the shape:

Move the camera around the shape and watch which meshlets drop out (dashed). Then make the meshlets bigger: their cones get wider, and fewer of the edges that face away are caught.
Only for one-sided surfaces

A double-sided material shows its back faces, so its meshlets must never be cone culled. A mirroring transform turns the triangles' winding around; the test must use the same side the rasterizer culls.

5Two levels, and occlusion per meshlet

Meshlets do not replace culling whole draws: that first test is still the cheapest way to drop most of the scene. Only the draws that pass it, at the LOD they picked, are split into meshlets:

  1. Per draw frustum and Hi-Z test of the whole mesh's sphere.
  2. Pick the LOD a coarser LOD has fewer meshlets.
  3. Per meshlet frustum, cone and Hi-Z test of each meshlet of that LOD.
  4. Pack the visible meshlets into a list the draw reads.

Occlusion culling with a Hi-Z works in two passes: draw what was visible last frame, build the Hi-Z from that depth, then test everything else (Occlusion culling). With meshlets, "visible last frame" must be stored per meshlet too, or a big mesh that is always partly visible would never lose its hidden meshlets. So each view keeps one bit per meshlet:

bits from last frame 110010 early pass draws bit 1 and in view: meshlets 0, 1, 4 build the Hi-Z late pass tests all against the Hi-Z, draws 2 (newly seen) bits for next frame 101010 meshlet 1 is now hidden: drawn once more this frame, skipped next frame
The bits are only a hint. If they are wrong (the mesh changed LOD, or the bits were cleared), the early pass draws a different set, and the late pass still draws every seen meshlet the early pass left out. The image stays right.

6Packing the survivors in order

Thousands of threads test meshlets at the same time, and each visible one needs a slot in the output list. The quick way is an atomic counter: each thread adds 1 and takes the old value. Then the order changes from frame to frame, and so do the pixels where two surfaces have the same depth. A stable order needs stream compaction with prefix sums:

chunk masks 3131 group sums 4 (atomic adds: any order, same sum) 4 first slots 0347 4 (earlier group) + 3 (earlier chunk) a meshlet's slot = its chunk's first slot + the visible meshlets below it in the mask
The sums do not depend on the order of the atomic adds, so the slots are the same every frame: the visible meshlets keep the meshes' order and the order inside each mesh.

7Drawing the visible meshlets

Two ways, with the same culling in front of them:

compute draw one draw command per meshlet works on every GPU

Indirect draws (vertex pulling)

Compute writes a draw command per visible meshlet; the usual vertex shader draws them. Runs everywhere.

Costs extra dispatches and barriers, and one command per meshlet.

task mesh mesh ras culling inside the draw no list in memory

Task and mesh shaders

A task shader culls a chunk of meshlets and launches a mesh shader workgroup per survivor. No list, no extra pass.

Needs mesh shader support (Vulkan VK_EXT_mesh_shader, D3D12, Metal 3).

8When it pays, and the pitfalls

Meshlet culling trades culling work for vertex and triangle work. It wins when there are many triangles to drop: dense meshes near the camera, terrain, scanned objects, scenes without LODs. With good LODs, far meshes are already cheap, and the extra culling passes can cost more than they save.

Culling costs more than it saves
Every view gets more dispatches and barriers, a fixed cost even when little is culled. Split only meshes with many meshlets, and measure.
A skinned or morphed mesh loses pieces
Its vertices move every frame, so bounds made at load do not hold. Draw it whole, or rebuild the bounds each frame from the posed vertices.
Holes on a stretched object
A non-uniform scale changes the normals' angles. Do the cone test in the object's own space (move the camera there), not on a cone moved into the world.
Back faces of a leaf are missing
The cone test ran on a double-sided material. Only test surfaces whose back faces are culled anyway.
Flicker where two surfaces touch
The visible meshlets come out in a different order each frame (an atomic counter). Pack them with prefix sums.
Memory for the list explodes
One draw command per meshlet per view: a list sized for every meshlet of every instance is huge. Give it a budget and draw what does not fit whole.

9In RASM

Every sub-mesh of at least 1024 triangles that is not skinned or morphed gets meshlets of 64 vertices and 124 triangles per LOD when it loads (buildMeshlets, after the LODs, on the same worker job). Each LOD's indices are rewritten in meshlet order, so a meshlet is a range of them, and a whole draw of the LOD draws the same triangles. Drawing by meshlets is EngineConfig::meshletPath (Renderer::setMeshletPath at run time), with both ways of section 7: MeshletPath::VERTEX_PULLING for the indirect draws, MeshletPath::MESH_SHADERS for task and mesh shaders (vertex pulling where the GPU has none), MeshletPath::OFF for whole draws. The default, MeshletPath::AUTO, takes the mesh shaders where the GPU has them and is off elsewhere.

Vertex pulling. Two more compute passes per culling phase turn the chunks into one indirect draw per visible meshlet:

Cull pass, per view and phase (compute) countMain a thread per draw writeMain draws or chunks meshletCountMain a group per chunk meshletWriteMain a draw per meshlet The view's visible draw buffer draws, per run meshlet draws, per run slots · chunks · masks · sums · counts Scene pass, per run drawIndexedIndirectCount(draws) + drawIndexedIndirectCount(meshlet draws)
Two indirect dispatches per run (the chunk count is the GPU's), behind the two of before. Both lists are DrawInfos in one buffer, so the vertex shader is the same and only PushConstants::firstDraw differs.

Mesh shaders. The culling pass stops after writeMain. The draw pass culls the meshlets itself: one task workgroup per chunk runs the same test (cullMeshletChunk, shared with meshletCountMain, so both paths keep the same meshlets), and launches one mesh workgroup per survivor. No meshlet draw list, so no budget, and no extra dispatch or barrier.

Cull pass (compute) countMain writeMain chunks of 64 meshlets group counts per run Draw pass (graphics), per run drawMeshTasksIndirect(group counts) taskMain a workgroup per chunk: 64 threads, 64 meshlets payload: the chunk, visible lanes in order meshMain meshMain meshMain rasterizer one per visible meshlet One meshlet in the meshlet data stream (32-bit words) v0 v1 … v(n−1) t0: a | b | c | 0 t1: a | b | c | 0 … n ≤ 64 sub-mesh vertex numbers, then a word per triangle (bytes)
The task shader reads the chunks writeMain wrote, with the group counts it wrote for the vertex-pulling dispatches. Each thread of a mesh workgroup runs one vertex through getVertexOutput, the function vertMain uses, so both paths compute the same vertices.

Where RASM follows the page, and where it differs

PartThis pageRASM
Building64 / 124, cone weightmeshopt_buildMeshlets with 64 / 124 and cone weight 0.25, then meshopt_optimizeMeshlet and meshopt_computeMeshletBounds per meshlet; MIN_MESHLET_TRIANGLES (1024) per sub-mesh.
Storageindex range or local listsBoth. An index range (GeometryMeshlet: sphere, cone, firstIndex, indexCount, firstData, vertexCount, 48 B) in the geometry pool's meshlet stream (EngineConfig::geometryPoolMeshlets); each GeometryLod names its firstMeshlet and meshletCount. Where the GPU has mesh shaders, also its local lists in the meshlet data stream (EngineConfig::geometryPoolMeshletData).
Which drawsbig meshesA visible draw whose LOD has at least MESHLET_MIN_DRAW (8) meshlets, in a run that is not the blend run (it is sorted per object). Skinned and morphed sub-meshes have none (D37).
Cone testcamera in object spaceisMeshletBackFacing: the camera (or an orthographic view's direction, SceneData::cullViewPoint) moved by the inverse world matrix, only in runs culled by face (SceneData::coneRuns).
Occlusiona bit per meshletPer view, in SceneView::meshletVisibility; each instance row of a mesh with meshlets owns words at InstanceData::meshletVisibility, one word per row on average (EngineConfig::maxInstances words per view). A row that finds none is still culled, only without its bits.
Orderprefix sumsChunks of 64 meshlets in list order; a sum per 64 chunks added with atomics; meshletWriteMain adds the earlier sums and chunks.
Memorya budgetVertex pulling: EngineConfig::maxViewMeshlets (2¹⁶, 1.3 MiB) meshlet draws per view and frame slot, made only for views that draw meshlets; a draw past it is drawn whole. Mesh shaders: no list, no budget.
Mesh shaderstask + meshVK_EXT_mesh_shader, turned on where the GPU has it (RenderContext::hasMeshShaders; not on MoltenVK). A task workgroup of 64 threads per chunk, a mesh workgroup of 64 threads per visible meshlet. The draw passes then read (Scene 1) and write (Scene 2) the meshlet bits.

Numbers

examples/18_meshlets --bench (Release, headless, 1920 × 1080, RTX 3060, 600 frames after 120): a terrain of 256 tiles and 2500 rocks of 20480 triangles, with four sun cascades. GPU frame time, off / vertex pulling / mesh shaders:

1.71 / 1.17 / 1.19 msevery mesh at LOD 0, no shadows: 4.72 → 1.80 M triangles--lod-error 0 --no-shadows
5.81 / 5.59 / 5.15 msLOD 0 with the 4 cascades: 21.4 → 15.9 M triangles by vertex pulling (over its budget), 13.1 M by mesh shaders--lod-error 0
1.97 / 2.18 / 1.99 mswith LODs (the default): 3.32 → 2.33 M triangles; vertex pulling adds 0.2 ms of culling passesdefault flags

With LODs, the vertex-pulling passes cost more than the triangles they drop; the mesh shaders, with no passes of their own, cost about 1%. 05_city, with few big meshes, is the same on all three (1.91, 1.92, 1.93 ms). So MeshletPath::AUTO takes the mesh shaders where the GPU has them and leaves meshlets off elsewhere; vertex pulling is there to choose for dense meshes seen up close (P28).

Code map

WhatWhere
BuildingbuildMeshlets in src/assets/meshlets.cpp, called by the load jobs in src/assets/mesh.cpp
StorageGeometryPool::allocate, GeometryPool::write in src/renderer/geometryPool.cpp
Draw levelcountMain, writeMain in shaders/passes/cull.slang
Meshlet levelcullMeshletChunk, isMeshletBackFacing in shaders/lib/meshletCulling.slang; vertex pulling: meshletCountMain, meshletWriteMain in shaders/passes/cull.slang
Mesh shaderstaskMain in shaders/lib/meshletTask.slang; meshMain in shaders/passes/rasm.slang and shaders/passes/shadow.slang (and meshMask); Renderer::createMeshletPipelines; VulkanDevice::enableMeshShaders in src/rhi/vulkan/vulkanDevice.cpp
Buffers, dispatches, drawsRenderer::prepareSceneView, Renderer::recordMeshletCulling, Renderer::drawSceneView (RenderContext::drawMeshTasksIndirect) in src/renderer/gpuScene.cpp
Seeing itDebugView::MESHLETS (a color per meshlet), FrameStats::meshletsVisible, FrameStats::meshletsCulled, "Meshlets saved" (FrameStats::trianglesVisibleLod minus FrameStats::trianglesVisible), the Meshlets combo in the RASM window
Rules

A meshlet's indices are a range of its LOD's, so the LOD draws the same triangles whole or by meshlets: the image must not change when the path changes (the meshlets and mesh_* image tests check it). Both paths cull with cullMeshletChunk and compute vertices with the vertex shader's functions: change them there, not in one path. Keep the meshlet draws, and the task shader's lanes, in chunk order. A new kind of draw that moves its vertices on the GPU must not get meshlets until its bounds move too.

Further reading