From 0383fdbded84ccb83337557272570d7d409d8dcd Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 18:51:04 -0800 Subject: [PATCH] rt: the timestamp read waits for the two values, and reads only a command buffer that wrote both On MoltenVK 1.4.2 (M3 Max) the timeline semaphore signals before the timestamp queries are available for most command buffers: a read without VK_QUERY_RESULT_WAIT_BIT then answered NOT_READY and the command buffer had no GPU times. Probe jobs on the M3 (MoltenVK admitted past its floor items for the measurement only): m3-max-43673 read both ways at retire, 58 of 72 reads NOT_READY with both availability bits 0, one NOT_READY then available a moment later, 13 Ok; m3-max-43654 read with WAIT: every one of the test's 34 command buffers got its times, the read holding the completion thread p50 2.0 us, p90 21.8 us, p99 76.3 us, max 80.5 us. A WAIT read of a query never written may never return, so the completion thread reads only a command buffer whose segments wrote both queries (stamps_written == 2); one that wrote fewer has no times, as before. The readback site's bound and the README's list of sites say so. --- crates/metal2vulkan-rt/README.md | 3 ++- crates/metal2vulkan-rt/src/complete.rs | 8 ++++++-- crates/metal2vulkan-rt/src/readback.rs | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index 2653b2f8c..e1f7b87e4 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -515,7 +515,8 @@ that touch blocks first. - Values the runtime reads back from GPU-written memory decide nothing beyond the reading command buffer's own results (`src/readback.rs`; a rule test holds every such read there). The sites: the two timestamp queries of a command buffer, which decide only its `gpu_start_time`, through - arithmetic defined for every value and never later than its `gpu_end_time`; and + arithmetic defined for every value and never later than its `gpu_end_time`, read once both were + written by a completed submission, waiting for the driver to make them available; and `Texture::get_bytes`'s copy out of its staging span, whose bytes reach only the caller's buffer, at offsets and lengths from the call's checked region. The parameter ring and an indirect dispatch's arguments are read only by the GPU: the runtime writes the ring and diff --git a/crates/metal2vulkan-rt/src/complete.rs b/crates/metal2vulkan-rt/src/complete.rs index 79d14aff9..1f2e3d19d 100644 --- a/crates/metal2vulkan-rt/src/complete.rs +++ b/crates/metal2vulkan-rt/src/complete.rs @@ -321,11 +321,15 @@ impl Retiree { } } - /// A completed command buffer's two timestamps. + /// A completed command buffer's two timestamps, where it wrote both: the read waits for them, + /// and a query never written would never become available. fn stamps(&self, core: &Core) -> Option<(u64, u64)> { match &self.item { Item::Command { - cb, error: None, .. + cb, + error: None, + written: 2, + .. } => cb.stamps(core), _ => None, } diff --git a/crates/metal2vulkan-rt/src/readback.rs b/crates/metal2vulkan-rt/src/readback.rs index f6e85adcb..3e50b96ae 100644 --- a/crates/metal2vulkan-rt/src/readback.rs +++ b/crates/metal2vulkan-rt/src/readback.rs @@ -9,12 +9,18 @@ use ash::vk; /// A completed command buffer's two timestamps, from its queue's pool; None where the driver does /// not report both, so the command buffer's GPU times stay 0. /// +/// The read waits for the two values (`VK_QUERY_RESULT_WAIT_BIT`): a driver may make a timestamp +/// available only after the submission's signal (MoltenVK 1.4.2 on an M3 Max did for most +/// command buffers, which then had no times). Both were written by a submission that completed, +/// so Vulkan makes them available; the caller reads only such queries. +/// /// Bound: the values only decide that command buffer's `GPUStartTime`, through arithmetic defined -/// for every value that gives a time no later than its `GPUEndTime`. The read does not wait. +/// for every value that gives a time no later than its `GPUEndTime`. /// /// # Safety /// -/// `pool` is a timestamp pool of `device` with the queries `first` and `first + 1`. +/// `pool` is a timestamp pool of `device` with the queries `first` and `first + 1`, both written +/// by a submission that completed. pub(crate) unsafe fn timestamps( device: &ash::Device, pool: vk::QueryPool, @@ -23,7 +29,12 @@ pub(crate) unsafe fn timestamps( let mut values = [0_u64; 2]; // SAFETY: by the caller; `values` holds two 64-bit results. let read = unsafe { - device.get_query_pool_results(pool, first, &mut values, vk::QueryResultFlags::TYPE_64) + device.get_query_pool_results( + pool, + first, + &mut values, + vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT, + ) }; read.ok().map(|()| (values[0], values[1])) }