From ec0687d29016bcb483cbb35f97a9b80da43fc580 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 20:18:08 -0800 Subject: [PATCH] rt tests: the empty stream's handlers run in commit order on completed command buffers with equal nonzero GPU times, and so does the texture synchronize's Each of the 10,000 empty command buffers carries a completed handler that checks its place in commit order, the status Completed, and gpu_start_time == gpu_end_time > 0; the test asserts 10,000 ran and none was wrong. The texture synchronize that now takes no submission asserts its completed handler saw it completed and its GPU start and end equal and nonzero. --- crates/metal2vulkan-rt/src/tests/blit.rs | 25 ++++++++++++++++++--- crates/metal2vulkan-rt/src/tests/command.rs | 25 ++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/crates/metal2vulkan-rt/src/tests/blit.rs b/crates/metal2vulkan-rt/src/tests/blit.rs index 4ae15dff7..6c3a1d819 100644 --- a/crates/metal2vulkan-rt/src/tests/blit.rs +++ b/crates/metal2vulkan-rt/src/tests/blit.rs @@ -744,10 +744,12 @@ fn a_fill_takes_whole_words_of_a_range_inside_the_buffer() { pub(crate) mod on_device { use crate::tests::command::on_device::{each_device, finished, within}; use crate::{ - BlitCommandEncoder, Buffer, BufferLayout, BytesPlace, CommandBuffer, Device, DispatchType, - Error, Gap, IndexClass, MemoryClass, PixelFormat, Refusal, Region, Resource, StorageMode, - Texture, TextureDescriptor, TexturePlace, TextureType, TextureUsage, + BlitCommandEncoder, Buffer, BufferLayout, BytesPlace, CommandBuffer, CommandBufferStatus, + Device, DispatchType, Error, Gap, IndexClass, MemoryClass, PixelFormat, Refusal, Region, + Resource, StorageMode, Texture, TextureDescriptor, TexturePlace, TextureType, TextureUsage, }; + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::Arc; /// A byte pattern of `len` bytes, different at every offset of one call and between calls. fn pattern(len: usize, seed: u8) -> Vec { @@ -1603,8 +1605,25 @@ pub(crate) mod on_device { .synchronize_resource(Resource::Texture(&texture)) .unwrap_or_else(|error| panic!("{name}: {error}")); encoder.end_encoding(); + let ran = Arc::new(AtomicBool::new(false)); + let handled = Arc::clone(&ran); + command_buffer + .add_completed_handler(move |done| { + let completed = done.status() == CommandBufferStatus::Completed; + handled.store(completed, Ordering::SeqCst); + }) + .expect("a completed handler"); run(name, &command_buffer); let after = counters.metrics(); + within("the completed handler", || ran.load(Ordering::SeqCst)); + let (start, end) = ( + command_buffer.gpu_start_time(), + command_buffer.gpu_end_time(), + ); + assert!( + start == end && start > 0, + "{name}: GPU times {start}, {end}" + ); assert_eq!( ( after.submissions - metrics.submissions, diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index e6629828d..92ef9a177 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -1554,7 +1554,9 @@ pub(crate) mod on_device { /// A stream of command buffers with nothing for the GPU leaves nothing behind: 10,000 /// committed, none waited for until the last, take no submission, the live command buffers - /// never pass the queue's 64, and every gauge is back where it was once they retire. + /// never pass the queue's 64, and every gauge is back where it was once they retire. Each + /// one's completed handler runs, in commit order, seeing it completed with its GPU start and + /// end equal and nonzero. #[test] fn every_device_keeps_its_gauges_flat_under_ten_thousand_empty_commits() { each_device("command_empty_stream", |name, _, device| { @@ -1563,9 +1565,25 @@ pub(crate) mod on_device { // The block-maker thread has made its ready blocks: the gauges hold still. crate::tests::blit::on_device::settled(&device); let (gauges, submissions) = (counters.gauges(), counters.metrics().submissions); + // Handlers run so far, and those that ran out of commit order or saw a command buffer + // not completed or with other times. + let (ran, wrong) = (Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0))); let mut last = None; for at in 0..10_000 { let command_buffer = queue.command_buffer().expect("a command buffer"); + let (ran, wrong) = (Arc::clone(&ran), Arc::clone(&wrong)); + command_buffer + .add_completed_handler(move |done| { + let (start, end) = (done.gpu_start_time(), done.gpu_end_time()); + let right = ran.fetch_add(1, Ordering::SeqCst) == at + && done.status() == CommandBufferStatus::Completed + && start == end + && start > 0; + if !right { + wrong.fetch_add(1, Ordering::SeqCst); + } + }) + .expect("a completed handler"); command_buffer.commit().expect("a commit"); let live = counters.gauges().command_buffers; assert!( @@ -1585,6 +1603,11 @@ pub(crate) mod on_device { submissions, "{name}: a submission" ); + assert_eq!( + (ran.load(Ordering::SeqCst), wrong.load(Ordering::SeqCst)), + (10_000, 0), + "{name}: handlers run, and those out of order, not completed or with other times" + ); drop((queue, device)); }); }