Files
metal2vulkan/crates/metal2vulkan-runtime/examples
Anees Iqbal 97baf233de Queue staging-queue texture copies and carry a burst in one submission
What: a replace_region into a Shared or Managed texture that copies on the device's
staging queue (a transfer-only family: the RTX 5080's) packs the caller's bytes into a
staging buffer of their own before it returns and queues the copy, instead of making a
staging submission and waiting for it. The queued copies go to the staging queue in one
submission, in their order (a barrier between two into one image), made by the first of:
the device's next batch or internal submission, before its own, which waits for it on the
GPU (Device::staging_wait, beside binding_wait); a get_bytes, which carries them before its
read; a VkImage hand-out (settle) and CommandQueue::with_vk_queue, which wait on the host;
a write into a texture whose VkImage was handed out; and the call past STAGED_COPIES /
STAGED_BYTES, which waits on the host. The staging queue now records into up to eight
command buffers and signals its own timeline on every device (no fence); a submission is
waited for only where the CPU needs its result. The batch keeps what the flush copied
(images, staging buffers) in its retained list until it completes; a queued copy holds its
image weakly, so a texture gone takes its copy with it.

Why: the consumer measured Safari's GPU process making 1776 replace_region_with_host_bytes
calls at its first page on the RTX 5080, each waiting for its own staging submission (p50
120 us, p99 232 us, 223 ms in all). Metal's replaceRegion is a CPU copy; only the
submission may move.

Counted: WorkCounts::staging_copies_queued, staging_flushes, staging_gpu_waits (API table
blessed: three fields added; conformance cost puts the last two in the submission region).

Verified on the M3 Max (debug, --cfg m2v_gpu, under maclock and memcap): the new lane
runtime_lane::staging_batches (6 tests) and runtime_lane::first_use (10), runtime tests
cpu_copy_layouts, first_move, staging (30): all pass. The M3 has no staging queue
(MoltenVK lists no transfer-only family), so there they run the host path and check the
contract only; the staging path's counts and negative control run on the RTX 5080.
clippy (plain and --cfg m2v_gpu, workspace, all targets, all features) and the release
check of the runtime's tests are clean.

(cherry picked from commit d47df5a36970e54e51465588e4ae3d6b299bcb0a)
2026-11-04 18:50:29 -08:00
..