skip to content

In GPU-driven rendering, how does a compute pass cull tens of thousands of instances and compact the survivors, and what does moving culling off the CPU cost you?

level: seniorimportance: should knowfreq 40%

answer

  1. one thread per instance
  2. frustum, then depth pyramid, then LOD
  3. append through an atomic counter
  4. draw arguments written by the GPU
  5. the CPU no longer knows what is visible

basics

~20 s

A compute pass runs one thread per instance, tests its bounds against the frustum and a depth pyramid, and appends survivors to dense per-draw lists through atomic counters. The CPU loses same-frame knowledge of visibility and debugging gets harder.

solid answer

~50 s

With every instance's transform and bounds resident in GPU memory, a compute pass launches one thread per instance. Each thread runs the frustum test, a Hi-Z occlusion test and often LOD selection; a survivor increments its draw bucket's counter with an **atomic fetch-and-add** and writes its instance index into the slot it got back, so each bucket ends up as a **dense list**. A small pass then copies each bucket's count into draw arguments that the GPU consumes directly, with no readback. It scales because thousands of tests run in parallel and the CPU no longer touches each object. The costs: the CPU no longer knows what is visible that frame, output buffers are sized for the worst case, atomic append can make the order differ from frame to frame, and a missing object can only be debugged from a GPU capture or a delayed readback.

code

pseudocode · 16 lines
pseudocode
// one thread per instance; counts[] and visible[] live in GPU memory
// counts[] is cleared to 0 before the pass runs
// bucket b owns visible[offset[b] .. offset[b] + instancesInBucket[b] - 1]
kernel cullInstances(id):
    inst = instances[id]
    if not insideFrustum(inst.worldSphere):
        return
    if isOccluded(inst.worldBox, viewProj, depthPyramid):
        return
    b = bucketOf(inst.mesh, inst.material, selectLod(inst))
    slot = fetchAndAdd(counts[b], 1)       // returns the value before the add
    visible[offset[b] + slot] = id

kernel writeDrawArguments(b):
    drawArgs[b].instanceCount = counts[b]
    drawArgs[b].firstInstance = offset[b]

go deeper

for a junior

Recall that GPU-driven rendering moves the per-object visibility test into a compute pass so the CPU no longer loops over every object.

for a middle

Explain one thread per instance, the order of tests, and why survivors are compacted into a dense list per draw.

for a senior

Discuss atomic append against prefix sums, SIMD-group aggregation, cluster and cone culling, worst-case buffers and how you debug a missing object.

for a principal

Judge whether the content and the team justify the move: instance counts, platform compute strength, debugging tools and the CPU systems that lose visibility data.

## Why move culling to the GPU A CPU that culls tens of thousands of instances and then issues one draw per survivor spends milliseconds of a core on work that is embarrassingly parallel. In **GPU-driven rendering** the scene's instance data (transform, bounds, mesh and material identifiers) stays resident in GPU memory, and a compute pass decides what is visible. Its output is consumed by draws whose arguments the GPU itself writes, so no result travels back to the CPU. How those indirect draws are recorded and submitted is a submission concern; this answer is about the culling decision. ## One thread per instance Each thread loads one instance and runs, in order of increasing cost: 1. a **frustum test** of its bounding sphere against the six planes; 2. an **occlusion test** of its bounding box against a depth pyramid, often inside a two-phase scheme so the pyramid is current; 3. **LOD selection** from its projected size or screen-space error, which decides which mesh, and so which draw bucket, it belongs to. A thread whose instance fails a test simply exits. ## Compaction: from flags to a dense list A draw consumes a count and a contiguous range of instance indices, so survivors must be **compacted**. Two methods dominate: - **Atomic append.** Each draw bucket (one mesh, material and LOD combination) owns a counter and a reserved range in an output buffer. A survivor does a fetch-and-add on its bucket's counter and writes its instance index at the slot returned. It is simple and fast, but the order in which threads arrive is not fixed, so the list order can change from frame to frame. - **Prefix sum.** Write a 0 or 1 visibility flag per instance, compute an exclusive prefix sum, and write each survivor at its summed index. It costs extra passes but gives a **stable order**. To reduce contention, threads in one SIMD group (32 or 64 lanes, depending on the GPU) first count their survivors together, one lane does a single atomic add for the whole group, and each lane writes at the base plus its rank among the group's survivors. A final small pass copies each bucket's counter into that bucket's **draw arguments** (instance count and first instance), which the following draws read. ## Going finer than instances The same pattern applies below the instance. Meshes can be split into **clusters** of a few dozen to a couple of hundred triangles, each with its own bounds and a **normal cone** that bounds its triangles' facing directions. Clusters are culled by frustum, depth pyramid and cone (a whole cluster facing away is backface-culled at once), and some renderers cull individual triangles in compute: back-facing, zero-area or too small to cover a sample. Finer culling discards more work but adds passes and bookkeeping. ## What it costs you | Cost | Why | Mitigation | |---|---|---| | CPU blind to visibility | results stay on the GPU | read back with a frame or two of delay for statistics and gameplay hints | | Worst-case buffers | a bucket may see every instance survive | reserve each bucket's range at its instance count | | Unstable order | atomics arrive in any order | prefix-sum compaction where order matters, such as coplanar surfaces at equal depth | | Harder debugging | a missing object is a GPU result | debug views of culled bounds, per-test counters, GPU captures | | Data upkeep | transforms must reach the GPU every frame they change | upload only changed instances | The CPU still decides what is loaded and records a fixed set of draws; the GPU decides how many instances each of those draws actually renders. ## Where it sits in the frame 1. Upload the instance data that changed this frame. 2. Cull last frame's visible instances against the frustum and draw them. 3. Build the depth pyramid from that depth. 4. Run the culling kernel over the remaining instances against the new pyramid, then draw the survivors. 5. Run the same kernel for each shadow or reflection view with that view's frustum, usually without the main camera's depth pyramid, because those views see different occluders. Each step only reads what the GPU produced in an earlier step, so the CPU never waits on a result.

  • How do you find out why one building disappears with GPU culling?
    Add per-test counters and a debug mode that draws the culled instances' bounds in a colour per failing test: frustum, occlusion or LOD. Capture a frame and inspect the depth pyramid level the object read. Common causes are bounds not updated after a transform change, an odd-sized pyramid level that drops a row, or a near-plane crossing not treated as visible.
  • Why do threads in a SIMD group aggregate survivors before touching the counter?
    Thousands of threads hitting one counter serialise on it. If the group first counts its survivors, one lane does a single atomic add for the whole group and each lane writes at the returned base plus its rank among the survivors, cutting atomic traffic by the group's width.

  • GPU culling must read the visible list back to the CPU before drawing.
  • Atomic append keeps survivors in the same order every frame.
  • Moving culling to the GPU removes all CPU cost of rendering.
  • Output buffers can be sized to last frame's survivor count.
  • Atomic counters may drop increments under heavy contention.