Culling and the frame

Async compute and multiple queues

A GPU can take work from more than one queue at a time. Async compute sends some compute work to a second queue, so it runs next to the graphics work instead of after it. This page explains when that makes a frame shorter, how the two queues stay in order, and why it often does not help. The last part shows how RASM does it.

In short

  • One queue runs its work in order. Parts of the GPU sit idle while a pass is limited by something else, or while a barrier drains the pipeline.
  • A compute queue from a separate queue family can fill those gaps with compute work.
  • It only helps when there is independent work to overlap. A chain where each step needs the last one gets nothing, and pays for the handoffs.
  • A barrier works inside one queue only. Between queues you need a semaphore, and a semaphore can only be waited for at the start of a submit. So the frame is split into batches.
  • A resource used on both queues needs concurrent sharing or an ownership transfer.
  • Timestamps of two queues may not share a clock: time each pass on its own queue. Measure before you keep it.

1Idle parts of a busy GPU

A GPU has many parts: shader cores, the rasterizer, the depth test, blending, the memory bus. A pass rarely uses all of them fully. The bars show how busy each part is in three moments of a frame.

shader cores rasterizer depth test blending memory bus

A shadow map pass

It writes only depth, so it is limited by the rasterizer and the depth test. Most shader cores have little to do.

shader cores rasterizer depth test blending memory bus

A small compute dispatch

It may fill only a few cores.

shader cores rasterizer depth test blending memory bus

At a barrier

The GPU lets the work before it finish before it starts the work after it. For a moment almost everything is idle.

With one queue, the GPU runs the passes in order, so these idle moments add up. With a second queue, the GPU can take compute work from it and run it in the idle parts.

graphics pass compute pass a wait for a result
One queue graphics Shadow map Compute Lighting gap gap end time saved Two queues graphics Shadow map Lighting compute Compute end
The compute pass does not get faster. It runs while the shadow pass leaves shader cores free, so the frame ends earlier. The orange arrow is the wait: lighting needs the compute result.

2Queues and queue families

A GPU offers its queues in queue families. All queues of one family can run the same kinds of work. A typical desktop GPU has three kinds:

draws compute copies present graphics queue

Graphics

Can run draws, compute, copies and present. Used for the main frame. Every GPU that can draw has one.

draws compute copies present compute queue

Compute only

Can run compute and copies. No draws, no render passes, no graphics pipeline stages. Used for async compute.

draws compute copies present transfer queue

Transfer only

Can run copies. Used for uploads and downloads next to rendering, often on a separate copy engine.

Async compute means a queue from the compute-only family. That family is what usually maps to separate hardware that takes work in parallel. Two queues of the graphics family may or may not run side by side: the API does not promise it. A GPU without a compute-only family has no async compute, and the compute work stays on the graphics queue.

Not every GPU overlaps well

Having a compute family only means the API lets you submit to it. How much the hardware really overlaps differs between vendors and generations, and between kinds of work. Integrated and mobile GPUs often gain little.

3Overlap needs independent work

Two passes can run at the same time only when neither needs the other's result. If pass B reads what pass A wrote, B must wait for A, whatever queue each one is on. So the first question is not "is this compute work?" but "what can run next to it?".

compute graphics Cull Draw Pyramid, cull Draw

A chain: nothing to overlap

Cull, draw, build the depth pyramid, cull again, draw again. Each step waits for the one before, so the work only moves between the queues. Each orange arrow is a handoff: a short gap on the other queue.

compute graphics Cull the scene Shadow maps Draw the scene both run at once

Independent work: real overlap

The shadow maps do not need the culling, so the two overlap. One queue fills the other's idle time.

A chain can still gain at its edges. The first step of the next frame may not need anything from the end of this frame. Then it can run next to the last steps of this frame, as long as the two frames use different copies of the buffers they write.

4Crossing queues: semaphores and batches

A pipeline barrier orders commands on one queue, across all submissions to it (see GPU synchronization). It cannot see another queue. Between queues the tool is a semaphore.

compute graphics writes X pass reads X barrier a race

A barrier stays on its queue

It orders the two graphics passes. The compute queue's write is not ordered against the read.

compute graphics writes X reads X signal at the end wait at the start

A semaphore crosses queues

One submit signals it when all its work is done, and another submit waits for it before it starts. The signal also makes the first submit's writes available, and the wait makes them visible, so no extra barrier is needed for memory.

A timeline semaphore holds a number that only grows. Each submit to a queue signals the next value, and a wait names the value it needs.

compute graphics submit 39 submit 40 submit 41 C = 39 C = 40 C = 41 only grows waiting wait C ≥ 41
One timeline per queue is enough: "wait until the compute queue reaches 41" means "wait for the compute queue's 41st submit".
The catch: a submit waits only at its start

There is no "wait here" inside a command buffer for another queue. So each place where a queue needs the other queue's result starts a new submit. Call the commands between two such places a batch: one command buffer and one submit on one queue.

compute graphics batch C1 signal C = 1 batch G1 wait C ≥ 1 signal G = 1 batch C2 wait G ≥ 1 signal C = 2 batch G2 wait C ≥ 2 signal G = 2 present
The cull, draw, cull, draw chain from section 3 as batches. Each orange arrow is one semaphore wait, at the start of a batch. Every change of queue that carries a result is a new batch on the waiting side.
Two rules keep the batches safe
  • Submit the signal before the wait. Timeline semaphores allow a wait to be submitted first, but then nothing may stop the signal from coming. Submitting batches in the order they were cut is simplest: every wait names a submit that already went out.
  • A failed submit signals nothing. A later wait for its value would block forever. Stop the frame there, or lower the wait to a value that was really signaled.

A render graph knows every pass's reads and writes, so it can place these waits itself. Where it would put a barrier on one queue, it puts a semaphore wait between two.

5Sharing resources between queues

Each buffer and image is created for some queue families. There are two ways to use one on two families:

graphics compute image release acquire image

Exclusive + ownership transfer

The resource belongs to one family at a time. Passing it is a release barrier on the old queue and an acquire barrier on the new one, around the semaphore (GPU synchronization, section on queue families).

Cost: more code, and a pair of barriers per handoff. The image keeps its best memory layout and compression.

graphics compute pass pass image

Concurrent

The resource is created for both families and either queue may use it at any time. The semaphores still order the work.

Cost: no transfers. Some GPUs compress concurrent images less, so they can be slower to read and write.

Layout transitions still happen in both modes

A compute queue can only name compute stages in its barriers: it has no vertex, fragment or attachment stages. A transition there of an image that the graphics queue last used as a depth attachment waits for "all commands" instead, which chains to the semaphore wait at the start of the batch.

6Timing work on two queues

Each queue writes timestamps into a query pool, so you can time each pass: a timestamp before and one after, on the same queue. But a timestamp from one queue and one from another may count from different origins.

graphics compute clock 0 a graphics pass end − start on one queue: right compute end − graphics start: can be wrong by milliseconds clock 0 a compute pass unknown offset
Two timestamps of the same queue give a duration. Two timestamps of different queues may not share a clock.

So "latest end minus earliest start" across both queues can be wrong by milliseconds. Keep a frame's total on one queue, and treat the other queue's timers as durations only. The sum of all pass times can now be more than the frame time, because some passes ran at the same time.

The honest measure is the frame time itself

Measure with the GPU as the limit: turn off vsync (or measure on the GPU), and compare the same scene with and without async compute, more than once.

7When it pays off

Good candidates

  • Compute next to depth-only or raster-heavy passes: culling or light binning next to shadow maps.
  • Post-processing of one frame while the next frame's geometry is drawn.
  • Simulation (particles, cloth) whose result is needed only later in the frame.
  • Work for a second view while the first view draws.

Poor candidates

  • A strict chain, where each step waits for the last.
  • Tiny dispatches: the handoffs cost more than the work.
  • Two passes that both need the same unit, like memory bandwidth: they only slow each other down.
  • A frame limited by the CPU: the GPU is idle anyway.

8Pitfalls

Hazards across frames
Next frame's compute may start while this frame's graphics still reads a buffer. A buffer with one copy needs a wait across frames too. One copy per frame slot avoids it.
Graphics stages on a compute queue
A barrier there may not name fragment, vertex or attachment stages or accesses. Code shared by both queues must fit its barriers to the queue.
The swapchain's binary semaphore
The acquire semaphore may be waited for once. A frame of several graphics submits waits for it in the first one only.
Uploads on one queue, readers on the other
A copy's barriers order it only on its own queue. A reader on the other queue must wait for the copy's submit, and the copy must wait for the other queue's earlier reads of what it overwrites.
The handoff cost
Each semaphore wait leaves the waiting queue idle until the signal. Many small batches can make a frame slower than one queue.

9A worked example

Say a frame has three passes, timed alone on the GPU: culling 0.4 ms (compute), a shadow map 1.2 ms (graphics) and the main draw 2.0 ms (graphics). The main draw needs the culling; the shadow map needs nothing.

culling, 0.4 ms graphics passes a handoff
0 1 ms 2 ms 3 ms One queue Cull Shadow map Main draw 3.6 ms Two queues, perfect overlap Shadow map Main draw Cull 3.2 ms Two queues, real Shadow map, a little slower Main draw Cull ≈ 3.3 ms Two queues, chain only Main draw Cull > 2.4 ms one queue: 2.4 ms (the dashed line)
The four setups to the same scale. The grey slivers are handoffs between the queues.
3.6 msOne queue: culling, then shadow map, then main draw0.4 + 1.2 + 2.0
3.2 msTwo queues, perfect overlap: culling next to the shadow map, then main drawmax(0.4, 1.2) + 2.0
≈ 3.3 msTwo queues, real: the shadow map runs a little slower while culling shares its cores, and the main draw waits for one handoff
> 2.4 msTwo queues, chain only (no shadow map): culling, handoff, main draw. A little more than one queue's 0.4 + 2.0 = 2.4 ms0.4 + 2.0 + a handoff

The saving is at most the time of the work that overlaps, here 0.4 ms. Without anything to overlap, the second queue only adds handoffs.

Change the numbers. In the picture below, the shadow map runs at 7/8 of its speed while the compute pass runs next to it, and the main draw waits for one handoff.

the compute pass (culling) graphics passes a handoff the wait for the compute result
Set the shadow map to 0: the frame is a chain, and two queues are slower. Then give it its 1.2 ms back and raise the handoff until the saving is gone.

10In RASM

RASM has an optional compute queue. With EngineConfig::asyncCompute on, and a GPU with a compute family that has no graphics, the frame's animation passes and each scene view's culling and Hi-Z passes run on it. They are added with RenderGraph::addAsyncComputePass, and the graph places the semaphore waits. With the option off (the default), or without such a family, every pass runs on the graphics queue in one submit, as before.

compute batches graphics batches semaphore waits that the graph placed
One scene view with async compute compute graphics Animate, SkinCull 1 Scene 1 Hi-Z, Cull 2 Scene 2, TransparentTone Map, overlay present Animate, SkinCull 1 of frame N+1 runs next to Scene 2
Animate and Skin run once per frame, before every view. Frame N+1's Animate, Skin and Cull 1 write the other frame slot's buffers (animated vertices, visible draws, light clusters), so they wait for nothing of frame N and run next to Scene 2. Batches are cut while the frame is recorded and all submitted at the end of the frame, in that order. The last graphics batch waits for all of the frame's compute work and presents.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Which queuea compute-only familyThe same. VulkanDevice::initialize takes vk-bootstrap's compute queue only from a family without graphics, and only if its timestamps have as many valid bits as the graphics queue's. RenderContext::hasAsyncCompute says whether there is one.
Which passesindependent workThe frame's Animate and Skin, then each scene view's Cull 1 (which also fills the light clusters), Hi-Z and Cull 2: a chain with the draws, so the only overlap is the next frame's first compute work next to this frame's last draws (P11).
Semaphoresone timeline per queueThe same: frameFence for graphics and computeFence for compute. A frame slot waits for both before it is reused.
Batchescut where a queue needs the other's resultThe same, done by the graph: Renderer::waitForQueue closes the other queue's open batch when a pass needs it. All batches go out at the end of the frame, in the order they were closed (Renderer::submitBatches).
Finding the waitsfrom declared reads and writesThe graph keeps, per resource and per queue, how it was used and in which batch. A use on the other queue becomes a wait instead of a barrier. Undeclared resources get no wait.
Hazards across framesone copy per frame slotPassBuilder::write(perFrame) declares one buffer per slot. The visible draws and the Hi-Z use it. The per-resource state also carries over between frames.
Sharingexclusive + transfers, or concurrentConcurrent for every buffer and image when there is a compute queue. No ownership transfers between the two; images a load fills on the transfer queue are concurrent for all three families (transfer queues).
Barriers on the compute queuecompute stages onlyfitToComputeQueue turns graphics source stages into all commands and drops graphics destination stages and accesses.
Swapchainwait once, present from the last submitThe same: every graphics submit asks for the acquire wait, and the swapchain gives it only to the first one. A headless engine's offscreen images need no acquire wait and are not presented.
Uploadsorder them against both queuesThe frames' uploads stay on the graphics queue. They wait for the last compute submit, and a compute submit waits for the last upload (UploadManager::lastSubmit); a frame with such uploads gets no overlap. The loads' copies run on a transfer queue, and a compute submit waits only for a load value the CPU saw reached (transfer queues).
Readbacks—The copies of Renderer::readFrame and Renderer::readTexture go in the frame's last graphics batch, which first waits for all of the frame's compute work. No graph orders them, so the compute batches of the next frames wait for that batch (readbackComputeWait).
Timinga frame total on one queuegpuFrameMs uses graphics timers only. Compute pass timers are durations, flagged by timerOnCompute.

Is it faster?

1.68 to 1.69 msa frame without async compute
1.71 to 1.72 msa frame with async compute

Not yet. On 05_city (an RTX 3060, Release, vsync off for the test, two runs each), a frame took 1.68 to 1.69 ms without async compute and 1.71 to 1.72 ms with it. The scene passes are the chain from section 3, so the handoffs cost a little more than the one overlap saves. That is why asyncCompute is off by default. It should pay off once a frame has graphics work that does not need the culling, like shadow maps or a second view. examples/03_instances turns it on, so the path runs under validation.

Where it lives in code

WhatWhere
The optionEngineConfig::asyncCompute in include/rasm/types.h
Picking the compute queue, concurrent sharingVulkanDevice::initialize, createBuffer, imageCreateInfo in src/rhi/vulkan/vulkanDevice.cpp
Submits with waitsRenderContext::submit (SubmitInfo, FenceValue) and VulkanQueue::submit
BatchesRenderer::useQueue, waitForQueue, closeBatch, submitBatches in src/renderer/renderer.cpp
Waits from declared resourcesRenderGraph::bufferBarrier, waitForOtherQueue, textureWaits in src/renderer/renderGraph.cpp
The async passesRenderer::addScenePasses in src/renderer/gpuScene.cpp; Renderer::addAnimationPass in src/renderer/animation.cpp
Barriers on the compute queuefitToComputeQueue in src/rhi/vulkan/vulkanCommandBuffer.cpp
Acquire once, present from the last submitgetAcquireWait, getPresentSignals in src/rhi/vulkan/vulkanSwapchain.cpp
Uploads against the compute queueUploadManager::flush, lastSubmit in src/rhi/uploadManager.cpp
Keep these true
  • An async compute pass must declare every resource it uses. An undeclared one gets neither a barrier nor a wait, and the two queues can then race.
  • A buffer that each frame writes for itself should be declared per frame slot, or the next frame's compute waits for this frame's graphics.
  • Code that records through FrameResources::commandBuffer may be on either queue. Only compute work belongs in an async pass.
  • Keep the batches in the order they were closed when submitting, and never wait for a value that a failed submit did not signal.
  • A texture used on both queues must be declared, so a wait orders its layout changes: the texture states of a frame that fails partway through its submits are put back per command buffer, which holds only when each texture's changes go out in the order they were recorded.

Further reading