Vulkan: MAP_WRITE staging buffers allocated in ReBAR VRAM since gpu-allocator switch
Disclaimer: This issue was investigated and diagnosed by Claude Opus 5.5. It profiled the regression, traced the cause through the wgpu-hal and gpu-allocator sources, and verified the workaround below. I reviewed the findings and the measurements come from my machine, but please double-check the analysis.
Description
Since wgpu 28 (#8158, gpu-alloc → gpu-allocator), every MAP_WRITE buffer on the Vulkan backend is allocated with gpu_allocator::MemoryLocation::CpuToGpu (wgpu-hal/src/vulkan/device.rs, create_buffer):
let location = match (is_cpu_read, is_cpu_write) {
(true, true) => gpu_allocator::MemoryLocation::CpuToGpu,
(true, false) => gpu_allocator::MemoryLocation::GpuToCpu,
(false, true) => gpu_allocator::MemoryLocation::CpuToGpu,
(false, false) => gpu_allocator::MemoryLocation::GpuOnly,
};gpu-allocator 0.28 prefers HOST_VISIBLE | HOST_COHERENT | DEVICE_LOCAL for CpuToGpu, which it documents as "memory useful for uploading data to the GPU and potentially for constant buffers". On a discrete GPU with Resizable BAR, that is VRAM. So every staging buffer, including those behind create_buffer_init, Queue::write_buffer and write_texture, is now written by the CPU across PCIe into VRAM.
wgpu 27 requested these buffers from gpu-alloc as HOST_ACCESS | UPLOAD, without FAST_DEVICE_ACCESS. gpu-alloc penalises DEVICE_LOCAL for that usage, so staging lived in system memory.
Without MAPPABLE_PRIMARY_BUFFERS, a MAP_WRITE buffer can only be COPY_SRC: the GPU reads it once, as a copy source, so it gains nothing from device-local memory.
Discrete GPUs without ReBAR are affected too: staging now fills the 256 MiB BAR heap first. DX12 is not affected, since gpu-allocator's D3D12 path already uses the system memory pool for CpuToGpu.
Repro steps
I don't have a minimal runnable example yet. The pattern that shows it:
- Use the Vulkan backend on a discrete GPU with ReBAR enabled.
- Every frame, while the GPU renders, create a handful of vertex/index buffers of a few hundred KB each with
create_buffer_init, or upload them withQueue::write_buffer. - Compare the CPU time of those calls between wgpu 27 and wgpu 28/29.
I can turn this into a standalone example if that helps.
Expected vs observed behavior
- Expected: staging buffers are placed in host memory, as in wgpu 27, so uploads cost the same as before.
- Observed: staging moves from memory type 2 (host) to memory type 4 (device-local, host-visible). In our app, which remeshes terrain chunks and uploads their vertex and index buffers with
create_buffer_init:- per-chunk upload CPU cost went from ~0.03 ms (wgpu 27) to 0.12–0.23 ms;
- frame time in a stage that remeshes every chunk each frame went from ~1.05 ms to 4–6 ms;
- profile samples show the time in the
memcpyinto the mapped allocation and in lock contention around unmapping.
Unified-memory GPUs (AMD APU on RADV) show no measurable change, as expected.
Workaround we verified: we patched gpu-allocator 0.28.0 so CpuToGpu prefers HOST_VISIBLE | HOST_COHERENT, with DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT as fallback. That restores wgpu 27's upload cost (~0.03 ms per chunk; the remesh stage drops to ~1.35 ms) with byte-identical rendered output. It's too blunt as an upstream fix, since it also moves CpuToGpu allocations the GPU reads repeatedly, which are the ones that benefit from device-local host-visible memory.
Possible fixes:
- For buffers that are
MAP_WRITEand not used for anything butCOPY_SRC(always the case withoutMAPPABLE_PRIMARY_BUFFERS), request host-preferred memory rather thanCpuToGpu. That probably needs a host-preferred upload location or preference flag in gpu-allocator; I'm happy to open the matching issue there. - Alternatively, pick the memory type in wgpu-hal for staging buffers, restoring gpu-alloc's
UPLOADbehaviour.
Extra materials
No validation warnings or errors. The patch we used against gpu-allocator 0.28.0 (src/vulkan/mod.rs, Allocator::allocate):
MemoryLocation::CpuToGpu => {
- vk::MemoryPropertyFlags::HOST_VISIBLE
- | vk::MemoryPropertyFlags::HOST_COHERENT
- | vk::MemoryPropertyFlags::DEVICE_LOCAL
+ vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT
}The fallback block for a failed CpuToGpu allocation searches DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT instead.
Platform
- Linux (CachyOS, kernel 7.2), Wayland (cosmic-comp)
- NVIDIA RTX 4090 with Resizable BAR, driver 615.71, Vulkan backend
- AMD Ryzen 7 7800X3D iGPU (RADV, Mesa 26.2.3) as the unified-memory comparison
- wgpu 29.0.4 (via Bevy 0.19.1) against wgpu 27.0.1 (via Bevy 0.18.1)
- gpu-allocator 0.28.0
Source: gfx-rs/wgpu