Memory that the CPU and the GPU share (UMA)

The console has one memory. A game writes vertices, indices and constants with the CPU, and the GPU reads the same bytes with no copy. The renderer's buffer cache (vk/buffer.c) is a copy design: it checks each guest range at each guest submit and copies what changed into a staging ring, and the results of shaders come back through a readback. On a discrete GPU those bytes cross PCIe. On a GPU that shares system memory (an integrated GPU: a Steam Deck, a laptop, a phone) the staging ring is the same memory as guest memory, and the copy is only cost: CPU time on the GPU thread to hash and copy, and memory bandwidth.

The memory model

vk/uma.c decides at device creation, and the log says it on one line (vulkan: the device ...):

The [Debug] setting vk_memory (also --vk-memory=N) chooses the path for guest buffers: 0 auto (in place on UMA, copied otherwise), 1 copy, 2 in place. In place needs VK_EXT_external_memory_host, or on Android with 2 VK_ANDROID_external_memory_android_hardware_buffer (below). Without either the log warns and the path is the copy. Auto with the second alone copies, and the log says (vk_memory=auto: AHardwareBuffers only with vk_memory=2 or uma_memory=2). On a discrete GPU, 2 works but the GPU reads guest memory across the bus: it is for testing correctness there, not for speed.

On a device with both ways, the [Debug] setting uma_memory compares them where guest memory is in place: 0 auto (on Linux moved into dma-bufs where the driver's imports are dear, else imported, and moved only without the first extension and with vk_memory=2), 1 imported only (copied without the first), 2 moved only, into AHardwareBuffers on Android and dma-bufs on Linux, under auto as well (copied where neither can be).

In place, the startup setting uma_copies says whether vertices, indices and constant buffers are read there too: 0 auto, 1 in place, 2 copied. Auto copies them on amdgpu's drivers (RADV, AMDVLK, AMD's on Linux), which look up the pages of every user-pointer buffer of a submission's list again at each submission, so that each import costs every submission (vk_uma_imports_dear). One game's battle map held about 260 MB of imports, 160 MB of them for vertices, indices and constant buffers, and made about 380 submissions a present: its recording thread spent 70% of a core in the kernel. A range a write in place is pending over stays in place, as do a dispatch's storage ranges and what the GPU writes. The headless Vulkan checks of the in-place path take uma_copies=1.

Where uma_copies copies, a constant buffer is copied straight from guest memory into the frame's staging, as on a discrete GPU (cb_get in render.c), when no write in place is pending over it. The buffer cache's checks and hash cost each constant buffer more than its copy. check_uma_constants_copied checks it, and a constant buffer under a write in place of the same submission.

In place, a compute dispatch with nothing staged for the host to read is not submitted on its own: it waits in the submission being recorded for the end of the game's GPU submission (vk_release_submit, at the end of each NVN job) or whatever submits first, as the console's GPU runs what a flush handed it. Submitted each, one game's battle map made 350 to 390 submissions a present by the GPU profiler, and 22 to 24 left so. A CPU read of a write in place still unsubmitted finishes all that was recorded, and the program whose write it waited for is submitted with each dispatch from then on (vk_uma_read_soon), as all were before. check_uma_dispatch_open checks the three cases.

With resizable BAR, the staging ring of streamed uploads goes in device-local memory that the CPU maps (vk_stage_upload, within the heap's budget): the CPU's sequential writes cross the bus once, and every draw that reads them reads local memory. With a small BAR the ring stays in host memory (the window is shared with the whole desktop, cmd.c).

The host memory of a ring that the CPU fills is of one of two kinds (vk_stage_memory, stage_cached in cmd.c). Memory for writes in order (DM_HOST in devmem.h) is a type that is not cached where the device has one, and a driver can map that write-combined. Cached memory (DM_CACHED) is a cached type. Cached is the default only on Intel's Mesa driver, where both types are the same system memory. snx-test-vulkan -H --bench-stage-memory times the CPU's copies into each kind on the device it runs on, for block sizes from 64 bytes to 16 MiB. On an RTX 2080 the type that is not cached takes them faster, 7,300 to 9,700 MB/s against 5,100 to 8,900, so that device keeps it.

What the GPU reads in place

Vertex and index ranges and read-only storage buffers (vk_buf_get_input, vk_buf_get_window), a dispatch's too (vk_buf_get_strict). For each range, buf_get first checks the pending results as before (a graphics or compute result that has not landed, which only the GPU holds) and GPU ownership (own.c). If guest memory holds the bytes, vk_uma_get binds the import that covers them, at the range's offset: no hash, no copy, no own buffer, no window. Bytes written in place by work not done yet are read there too, after a barrier (order, below). A dispatch's read-only storage over a storage image not published yet or a queued release's payload is copied, which lands them first, as a write in place is refused there. Copied, an Ounce game's tensors, each bound as the 16 MiB after its address and written in place by the dispatch before, took a 16 MiB copy out of the import at every binding: in one game's upscaled intro movie about 35 copies, 540 MB and 15 ms of GPU time a frame.

What the GPU writes goes there too:

Guest memory is imported with VK_EXT_external_memory_host in pieces. An import covers the 2 MiB windows that a range touches, clipped to mapped guest memory. Imports never overlap: a range across two imports makes one import of their union. At most 512 imports, the least recently used goes past that, and an import unused for 600 presents goes. A window that the driver refuses is kept in a list of refused spans for 600 presents, or until its pages change, and its ranges are copied. On an RTX 2080 a span with one page the driver cannot pin (a PROT_NONE page) fails as a whole with VK_ERROR_OUT_OF_DEVICE_MEMORY, while anonymous and shared (memfd, the alias pool's) pages import. Before the list, every range in such a window asked the driver again, and one scene fell from 28 to 5 frames a second. The whole guest heap is never one import. A driver that checks each import's pages at each submit (amdgpu's user pointers) pays for the bytes imported, and LASRecomp measured 45 ms of driver time a frame on AMD with the whole heap as one import. On an RX 6700 XT (RADV, Mesa 26.1.8) a submission takes 6.4 us more per MiB imported in one import, from 16 MiB to 512 MiB, and 7.2 us per MiB with 160 MiB in 160 imports.

What still copies, and why


Made from docs/layers/gpu/ownership.md at 69237645142c. GPL-3.0-or-later.