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:
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.
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:
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:
| Who | Vertices | Triangles | Why |
|---|---|---|---|
| NVIDIA (Turing and later) | 64 | 124 to 126 | Output memory is allocated in blocks; 126 fits, and 124 keeps the 8-bit index list a multiple of 4 bytes. |
| AMD (RDNA 2) | 64 | 64 to 128 | Older AMD GPUs like the same number of vertices and triangles. |
| Indexed draws (no mesh shaders) | any | more is cheaper | Each 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.
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:
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:
- Per draw frustum and Hi-Z test of the whole mesh's sphere.
- Pick the LOD a coarser LOD has fewer meshlets.
- Per meshlet frustum, cone and Hi-Z test of each meshlet of that LOD.
- 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:
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:
7Drawing the visible meshlets
Two ways, with the same culling in front of them:
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 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:
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.
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
| Part | This page | RASM |
|---|---|---|
| Building | 64 / 124, cone weight | meshopt_buildMeshlets with 64 / 124 and cone weight 0.25, then meshopt_optimizeMeshlet and meshopt_computeMeshletBounds per meshlet; MIN_MESHLET_TRIANGLES (1024) per sub-mesh. |
| Storage | index range or local lists | Both. 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 draws | big meshes | A 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 test | camera in object space | isMeshletBackFacing: the camera (or an orthographic view's direction, SceneData::cullViewPoint) moved by the inverse world matrix, only in runs culled by face (SceneData::coneRuns). |
| Occlusion | a bit per meshlet | Per 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. |
| Order | prefix sums | Chunks of 64 meshlets in list order; a sum per 64 chunks added with atomics; meshletWriteMain adds the earlier sums and chunks. |
| Memory | a budget | Vertex 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 shaders | task + mesh | VK_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:
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
| What | Where |
|---|---|
| Building | buildMeshlets in src/assets/meshlets.cpp, called by the load jobs in src/assets/mesh.cpp |
| Storage | GeometryPool::allocate, GeometryPool::write in src/renderer/geometryPool.cpp |
| Draw level | countMain, writeMain in shaders/passes/cull.slang |
| Meshlet level | cullMeshletChunk, isMeshletBackFacing in shaders/lib/meshletCulling.slang; vertex pulling: meshletCountMain, meshletWriteMain in shaders/passes/cull.slang |
| Mesh shaders | taskMain 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, draws | Renderer::prepareSceneView, Renderer::recordMeshletCulling, Renderer::drawSceneView (RenderContext::drawMeshTasksIndirect) in src/renderer/gpuScene.cpp |
| Seeing it | DebugView::MESHLETS (a color per meshlet), FrameStats::meshletsVisible, FrameStats::meshletsCulled, "Meshlets saved" (FrameStats::trianglesVisibleLod minus FrameStats::trianglesVisible), the Meshlets combo in the RASM window |
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
- Arseny Kapoulkine, meshoptimizer: "Mesh shading" and cluster culling in its README; the niagara renderer's task-shader culling.
- Christoph Kubisch, "Introduction to Turing Mesh Shaders", NVIDIA, 2018.
- Graham Wihlidal, "Optimizing the Graphics Pipeline with Compute", GDC 2016: cluster and triangle culling in compute.
- Ulrich Haar and Sebastian Aaltonen, "GPU-Driven Rendering Pipelines", SIGGRAPH 2015.