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.

One queue graphics frame 1 copies frame 2 copies frame 3 With a copy engine graphics transfer frame 1 frame 2 frame 3 copies copies the time the copies took
The copy engine reads the staging buffer and writes video memory while the shader cores draw. The frames no longer wait for the copies, but they must not read a copy's data before it is done (section 4).

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.

draw compute copy the frames' queue

Graphics family

Every GPU has one. It runs the frames, and it can copy too.

compute copy async compute

Compute family

Shader cores without the drawing parts. See async compute.

copy the copy engine (DMA)

Transfer family

Only VK_QUEUE_TRANSFER_BIT. NVIDIA and AMD GPUs have one; lavapipe, MoltenVK and most Intel GPUs do not.

Check how exact its copies are

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?

graphics frame reads row 5 transfer copy writes row 5 race

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.

graphics frame reads old data transfer copy fills a new texture nothing shared: no wait

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.

CPU frame N: fill, submit transfer copies → signal 7 the texture is not drawn yet

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.

CPU frame N+1: read value: 6 transfer copies still running 6 < 7: draw without it, no wait

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.

CPU frame N+2: value 7: publish graphics wait 7 acquire frame draws the texture

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).

Why still wait on the semaphore?

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.

new transfer writes frames read

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.

transfer graphics copy mips release acquire sample

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.

transfer graphics copy every mip release barrier signal 7 semaphore wait 7 acquire sample TRANSFER_DST → SHADER_READ_ONLY transfer family → graphics family
The release's destination stages are ignored (the semaphore orders what follows), and so are the acquire's source stages (the semaphore wait orders what came before). Without the pair, the graphics queue would read an image that another family owns, whose contents are undefined there.
// 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;
The first use needs no transfer

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.

RAM: staging video memory the copy crosses the bus

System memory

The usual place. The CPU writes fast; the copy engine pulls the bytes across the bus.

staging 256 MiB BAR heap: full

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.

staging texture resizable BAR: all mappable

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.

One 2048 × 2048 RGBA8 texture, mips included: 21 MiB 4 MiB 4 MiB 4 MiB 4 MiB 4 MiB frame N: until the budget is spent frame N+1: the rest, then publish a part also stops when the staging ring is full, instead of waiting for the GPU to free it

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.

Make the GPU frame longer than the CPU frame: the copies on the graphics queue now add to every frame, and the copy engine saves them. Make the CPU frame the longest: both cases take the same time.
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.

main thread load uploads (transfer queue) frames (graphics queue) waits and barriers
main thread transfer graphics frame N: finishReadyLoads fills in 4 MiB parts, endLoadUploads copies, release → V frame N+k: fence ≥ V: publishUploadedLoads wait V acquire draws the new texture frames N … N+k−1 draw without it, and wait for nothing
The copies wait only for the frame and compute values the CPU saw reached, since what they write is new. Each graphics or compute submit waits for the newest load value the CPU saw done, once per new value; the first graphics submit after it runs the acquires first. The release queue waits for the load uploads' timeline too, so a destroyed texture or a freed pool range outlives its copies.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Which queuea family that only copiesVulkanDevice::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 copiesnew memory on the transfer queueEvery 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.
Publishafter the copy is doneAssetLoader::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.
Waitsfor a value already reachedupdateLoadUploadsDone reads the fence once a frame; every graphics and compute submit waits for that value, once per new value (loadWaited).
Sharingbuffers concurrent, textures exclusiveThe 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, acquireone barrier recorded twicecopyToTextures 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.
Stagingsystem memory, or all of video memoryEach upload manager has a 64 MB ring of BufferUsage::TRANSFER: system memory, or video memory where all of it is host-visible (P10).
Partsa few MiB per frameRenderContext::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).
Timingtime the copies on their queueEach upload batch has two timestamps; FrameStats::uploadGpuMs sums the batches that finished since the last frame.

Is it faster?

220 → 195 msPNG Flight Helmet load, busy GPU4.5 ms frames: 3840 × 2160, 8× MSAA, bloom
190 = 190 msthe same load, idle GPU0.16 ms frames at 1280 × 720
9.2 → 5.0 mslongest frame while it loadstextures filled in 4 MiB parts
1.28 → 1.27 ms05_city steady GPU frameevery buffer concurrent: no cost

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

WhatWhere
The optionEngineConfig::transferQueue in include/rasm/types.h
Picking the queue, sharing, ring placementVulkanDevice::initialize, getImageFamilies, createBuffer in src/rhi/vulkan/vulkanDevice.cpp
Release and acquire barrierscopyToTextures, acquireTextures in src/rhi/vulkan/vulkanCommandBuffer.cpp
Rings, batches, releases, timersUploadManager in src/rhi/uploadManager.cpp
The two upload managers, waits, acquires in a submitbeginLoadUploads, endLoadUploads, submit in src/rhi/renderContext.cpp
Fill and publish loadsAssetLoader::Impl::finishReadyLoads, publishUploadedLoads in src/renderer/renderer.cpp; finishTextureLoad in src/assets/texture.cpp
Release queue timelinesRELEASE_TIMELINES in src/rhi/releaseQueue.h
Teststests/uploadManagerTests.cpp, tests/releaseQueueTests.cpp
Keep these true
  • A fill between beginLoadUploads and endLoadUploads writes only memory no submitted work uses, and its data is used only after isLoadUploadDone.
  • 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