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.
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.
A small compute dispatch
It may fill only a few cores.
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.
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:
Graphics
Can run draws, compute, copies and present. Used for the main frame. Every GPU that can draw has one.
Compute only
Can run compute and copies. No draws, no render passes, no graphics pipeline stages. Used for async compute.
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.
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?".
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.
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.
A barrier stays on its queue
It orders the two graphics passes. The compute queue's write is not ordered against the read.
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.
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.
- 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:
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.
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.
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.
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.
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.
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.
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.
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
| Part | This page | RASM |
|---|---|---|
| Which queue | a compute-only family | The 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 passes | independent work | The 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). |
| Semaphores | one timeline per queue | The same: frameFence for graphics and computeFence for compute. A frame slot waits for both before it is reused. |
| Batches | cut where a queue needs the other's result | The 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 waits | from declared reads and writes | The 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 frames | one copy per frame slot | PassBuilder::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. |
| Sharing | exclusive + transfers, or concurrent | Concurrent 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 queue | compute stages only | fitToComputeQueue turns graphics source stages into all commands and drops graphics destination stages and accesses. |
| Swapchain | wait once, present from the last submit | The 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. |
| Uploads | order them against both queues | The 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). |
| Timing | a frame total on one queue | gpuFrameMs uses graphics timers only. Compute pass timers are durations, flagged by timerOnCompute. |
Is it faster?
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
| What | Where |
|---|---|
| The option | EngineConfig::asyncCompute in include/rasm/types.h |
| Picking the compute queue, concurrent sharing | VulkanDevice::initialize, createBuffer, imageCreateInfo in src/rhi/vulkan/vulkanDevice.cpp |
| Submits with waits | RenderContext::submit (SubmitInfo, FenceValue) and VulkanQueue::submit |
| Batches | Renderer::useQueue, waitForQueue, closeBatch, submitBatches in src/renderer/renderer.cpp |
| Waits from declared resources | RenderGraph::bufferBarrier, waitForOtherQueue, textureWaits in src/renderer/renderGraph.cpp |
| The async passes | Renderer::addScenePasses in src/renderer/gpuScene.cpp; Renderer::addAnimationPass in src/renderer/animation.cpp |
| Barriers on the compute queue | fitToComputeQueue in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| Acquire once, present from the last submit | getAcquireWait, getPresentSignals in src/rhi/vulkan/vulkanSwapchain.cpp |
| Uploads against the compute queue | UploadManager::flush, lastSubmit in src/rhi/uploadManager.cpp |
- 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::commandBuffermay 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
- The Vulkan specification: the chapters on queues, semaphores (timeline semaphores) and resource sharing modes.
- The Khronos Vulkan Guide: the sections on queues and on synchronization examples.
- GPU vendors' performance guides (AMD GPUOpen, NVIDIA developer blog): their articles on async compute, and when it helps or hurts.
- VK_KHR_calibrated_timestamps: how to relate timestamps of different queues to one clock.