GPU data
Transfer queues and ownership transfers
Most desktop GPUs have a copy engine: a part that only moves bytes, with a queue of its own. Uploads can run there, next to the frames, instead of in front of them. This page explains which copies may move there, how the frames learn that a copy is done, and how an image moves from one queue to another. The last part shows how RASM does it.
In short
- On one queue, upload copies run between frames and make the GPU's frame longer while a level loads.
- A transfer queue from a family that only copies is the copy engine (DMA). Its copies run next to the frames.
- Copies into new memory need no wait for the frames. Copies that overwrite what a frame reads must stay ordered with the frames.
- Use the data only once its copy is done. Then a frame never waits for a copy in flight; its semaphore wait is on a value already reached.
- A resource used by two queue families is concurrent, or exclusive and moved by a release and an acquire barrier. Buffers are usually concurrent; textures exclusive.
- It pays off when the GPU is busy. With an idle GPU it changes nothing. Measure it.
1Copies on the graphics queue
A loaded texture goes into a staging buffer, then a copy command moves it into the image. On a GPU with one queue, those copies run in the queue's order: after the last frame, before the next one. Look at the graphics queue in the top row: each frame starts late by the copies in front of it.
This matters only while something loads: a level, a streamed tile, a texture the camera just reached. With nothing to load, the two pictures are the same.
2Queue families that only copy
A GPU offers its queues in queue families. Every family that can draw or compute can also copy. A family that can only copy is the copy engine.
Graphics family
Every GPU has one. It runs the frames, and it can copy too.
Compute family
Shader cores without the drawing parts. See async compute.
Transfer family
Only VK_QUEUE_TRANSFER_BIT. NVIDIA and AMD GPUs have one; lavapipe, MoltenVK and most Intel GPUs do not.
Each family has a minImageTransferGranularity. (1, 1, 1) means a copy may start at any texel. A larger value means image copies must start and end on multiples of it, and (0, 0, 0) means whole mip levels only. An uploader that copies a few rows at a time needs (1, 1, 1), or must round its copies.
3Two kinds of uploads
On one queue, a barrier puts each copy after everything earlier on the queue. On another queue there is no such order. So ask of each copy: does it overwrite memory that a frame on the GPU may still read?
Overwrites data in use
A per-frame table, a buffer the application refills. The copy must wait for the frames that read it: keep it on the graphics queue, or wait on their semaphore and lose the overlap.
Writes new memory
A new texture, or a range of a pool that was freed only after every frame that used it was done. No frame reads it yet: the copy can start at once.
Loads are almost all of the second kind, and they are the big copies. Per-frame writes are small and of the first kind. So the usual split is: loads on the transfer queue, the rest on the graphics queue.
4Use the data only once its copy is done
The frame that first draws the new texture needs the copy finished. One way is to make that frame's submit wait for the copy's semaphore value. But if the copy is still running, the whole frame waits for it, and the overlap is gone. The better way is to publish the data only once the CPU sees the copy done.
Frame N fills the staging buffer and submits the copies on the transfer queue at once. The submit will signal value 7 on the transfer queue's timeline semaphore.
Frame N+1 reads the semaphore's value, without waiting. It is 6, so the copies are not done. The frame draws without the texture (or with a stand-in) and never waits.
Frame N+2 reads 7: the texture is published. Its graphics submit still waits for value 7, which is already reached, so the wait costs nothing; the wait makes the copies visible to the graphics queue, and the acquire barrier comes after it (section 6).
Reading the value on the CPU tells the CPU, not the graphics queue. The spec makes the copy's writes visible to another queue only through a semaphore wait (or a fence and a later submit). A wait for a value already reached is free, so keep it.
5Exclusive or concurrent
Each buffer and image is created for one family (exclusive) or for several (concurrent, VK_SHARING_MODE_CONCURRENT with a list of families). An exclusive resource that a second family uses must be handed over with an ownership transfer, or its contents are undefined there.
Buffers: concurrent
A pool of geometry is copied into in one range while frames draw from the others. An ownership transfer moves the whole buffer, so copies and draws would have to take turns.
Cost: none known for buffers; drivers do not compress them.
Textures: exclusive, moved once
A texture is written once, then only read. One release and one acquire move it to the graphics family for good.
Cost: two barriers per texture. It keeps the compression a driver may turn off for concurrent images.
6Release and acquire
An ownership transfer is one barrier recorded twice: a release on the queue that gives the resource up, and an acquire on the queue that takes it, with a semaphore between them. Both name the same two families, the same subresources and the same layouts, so the layout changes once.
// on the transfer queue, after the copies: the release
VkImageMemoryBarrier2 release = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};
release.srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT;
release.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
release.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
release.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
release.srcQueueFamilyIndex = transferFamily;
release.dstQueueFamilyIndex = graphicsFamily;
// on the graphics queue, in a submit that waits for the copies' semaphore: the acquire
VkImageMemoryBarrier2 acquire = release;
acquire.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
acquire.srcAccessMask = VK_ACCESS_2_NONE;
acquire.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
acquire.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;
A new exclusive resource is owned by no family: the first queue that uses it takes it. And a resource whose old contents are not needed (a barrier from VK_IMAGE_LAYOUT_UNDEFINED) can be used by any family without a transfer, once the semaphores order it after the last use.
7Where the staging buffer lives
The CPU writes the staging buffer; the copy reads it. Where it lives decides who crosses the bus.
System memory
The usual place. The CPU writes fast; the copy engine pulls the bytes across the bus.
A small BAR heap
Video memory the CPU can map, but only 256 MiB of it. A staging ring there takes room the per-frame buffers need.
All of video memory
With resizable BAR (or memory shared with the CPU) there is no small heap to fill. The CPU crosses the bus once, and the GPU's copy stays in video memory: shorter.
8Big copies in parts
The CPU side of an upload is a copy into the staging buffer, a few milliseconds for a big texture. Done in one go, it makes one long frame. A texture that is used only once its copy is done can be filled in parts, a few megabytes per frame: no frame samples it half filled.
9When it pays off
The copy engine saves the copies' time on the graphics queue. That is time on a busy GPU only: when the CPU sets the pace and the GPU idles half the frame, copies in front of a frame cost nothing visible. Move the sliders and watch the frame time of each case.
- A frame waits for a copy in flight
- Its submit waits for the newest value, not for one the CPU saw reached. Publish first, then wait for the published value.
- Validation: an image used by a family that does not own it
- A release without its acquire, or an acquire in a submit that does not wait for the release's semaphore. Record the acquire in a submit that waits, before any use.
- Garbage in the first frame that samples a texture
- The data was used before its copy was done, or the frame skipped the semaphore wait that makes the writes visible.
- A copy writes into memory a frame still reads
- A reused range went back to the allocator before every queue was done with it, or a per-frame write went to the transfer queue. Free ranges only after all queues' fences, and keep overwrites on the frames' queue.
- The acquire is lost when a frame is dropped
- Forget a texture's acquire only once the submit that carries it went out; a skipped or failed frame must take it again.
- Copies fail on another GPU
- Its transfer family has a coarser
minImageTransferGranularity: row copies at any offset are not allowed there.
10In RASM
RASM has a transfer queue for the loads' copies, on by default (EngineConfig::transferQueue). Everything a load fills goes there; the frames' own fills stay on the graphics queue. A load counts as loaded once its copies are on the GPU, so the frames never wait for copies in flight. On a GPU without a copy-only family the same code runs with the graphics queue in place of the transfer queue.
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Which queue | a family that only copies | VulkanDevice::initialize takes the first family with only VK_QUEUE_TRANSFER_BIT (and sparse binding or protected) and a minImageTransferGranularity of (1, 1, 1). None: the loads' copies use the graphics queue. QueueType::TRANSFER is the engine's own: RenderContext::createCommandBuffer and submit refuse it. |
| Which copies | new memory on the transfer queue | Every fill between RenderContext::beginLoadUploads and endLoadUploads, which AssetLoader::Impl::finishReadyLoads brackets: texture levels, geometry and animation pool ranges, an environment's image. An application's fillBuffer and fillTexture stay on the graphics queue, ready for the next frame. |
| Publish | after the copy is done | AssetLoader::Impl::publishUploadedLoads, at the start of the next finishReadyLoads: a mesh becomes READY, a texture leaves pendingTextures, an environment may bake. Entities already wait for their mesh and textures, so they appear then. |
| Waits | for a value already reached | updateLoadUploadsDone reads the fence once a frame; every graphics and compute submit waits for that value, once per new value (loadWaited). |
| Sharing | buffers concurrent, textures exclusive | The same without a compute queue: rhi::Texture::ownershipTransfer. With one, images filled from the ring are concurrent for the graphics, compute and transfer families (they were already concurrent for two, P11). |
| Release, acquire | one barrier recorded twice | copyToTextures releases a texture with its fill's last copy (TextureCopy::last). UploadManager::recordAcquires records the acquires (acquireTextures) into one of three graphics command buffers, which RenderContext::submit puts first; acquiresSubmitted forgets them only once that submit went out. |
| Staging | system memory, or all of video memory | Each upload manager has a 64 MB ring of BufferUsage::TRANSFER: system memory, or video memory where all of it is host-visible (P10). |
| Parts | a few MiB per frame | RenderContext::fillTexturePart with TextureFillProgress, 4 MiB at a time under the 4 ms load budget, begun textures first; a part stops at a full ring. A model's geometry is still one step (P24). |
| Timing | time the copies on their queue | Each upload batch has two timestamps; FrameStats::uploadGpuMs sums the batches that finished since the last frame. |
Is it faster?
Measured with tools/load_bench.py (Release, headless, RTX 3060, median of 5 runs, both queues; the examples take --transfer-queue on|off). With a busy GPU, loads end 6 to 11 % sooner; with an idle one, nothing changes, and the 300-image burst of 17_streaming takes 433 ms against 421 ms. The staging rings stay in video memory on this GPU, which has resizable BAR: in system memory the copies took 2.6 times as long on the GPU. So the option is on by default. Uploads are still recorded on the main thread (D13).
Where it lives in code
| What | Where |
|---|---|
| The option | EngineConfig::transferQueue in include/rasm/types.h |
| Picking the queue, sharing, ring placement | VulkanDevice::initialize, getImageFamilies, createBuffer in src/rhi/vulkan/vulkanDevice.cpp |
| Release and acquire barriers | copyToTextures, acquireTextures in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| Rings, batches, releases, timers | UploadManager in src/rhi/uploadManager.cpp |
| The two upload managers, waits, acquires in a submit | beginLoadUploads, endLoadUploads, submit in src/rhi/renderContext.cpp |
| Fill and publish loads | AssetLoader::Impl::finishReadyLoads, publishUploadedLoads in src/renderer/renderer.cpp; finishTextureLoad in src/assets/texture.cpp |
| Release queue timelines | RELEASE_TIMELINES in src/rhi/releaseQueue.h |
| Tests | tests/uploadManagerTests.cpp, tests/releaseQueueTests.cpp |
- A fill between
beginLoadUploadsandendLoadUploadswrites only memory no submitted work uses, and its data is used only afterisLoadUploadDone. - A graphics or compute submit waits only for a load value the CPU saw reached; never for one in flight.
- An exclusive texture is sampled only after the graphics submit that acquired it; its acquire is forgotten only once that submit went out.
- What a destroy frees waits for all four timelines (
RELEASE_TIMELINES).
Further reading
- The Vulkan specification: the sections on queue family ownership transfer, resource sharing modes and
minImageTransferGranularity. - The Khronos Vulkan Guide and synchronization examples: transferring a resource between queue families.
- The VMA documentation: the usage patterns for staging buffers and resizable BAR.
- Related guides: GPU memory and uploads, texture uploads, streaming assets, async compute, GPU synchronization.