diff --git a/crates/metal2vulkan-runtime/src/tests/compute.rs b/crates/metal2vulkan-runtime/src/tests/compute.rs index 42aa4ed7e..4d07b9776 100644 --- a/crates/metal2vulkan-runtime/src/tests/compute.rs +++ b/crates/metal2vulkan-runtime/src/tests/compute.rs @@ -770,6 +770,52 @@ pub(crate) mod on_device { }); } + /// A dispatch that writes a resource after a dispatch that read through an address outside + /// its resources takes a barrier in a serial encoder: the read may have been of the resource + /// the write reaches, and Metal runs a serial encoder's dispatches in their order, so the read + /// sees the bytes from before the write. Here the second dispatch writes a buffer the first + /// did not bind, so that barrier counts among the unbound ones, the segment's end the only + /// other. Lavapipe runs dispatches one after another, so the values cannot show the order; + /// the count by source does. + #[test] + fn every_device_orders_a_write_after_an_unbound_read() { + each_device("compute_unbound_read", |name, _, device| { + let mut arena = Arena::new(&device); + let step = step(&device); + let fields = vec![region(), address(0, Place::Push(32))]; + let mut wild = interface(vec![(0, Access::ReadWrite)], fields, 40); + wild.reads_unbound = true; + let wild = (ComputePipelineState::for_test(&device, &kernels::STEP, wild)) + .expect("the step kernel, reading outside its resources"); + let queue = device.new_command_queue(4).expect("a queue"); + let [x, y] = std::array::from_fn(|_| arena.buffer()); + for buffer in [&x, &y] { + write_words(buffer, |index| index); + } + let before = device.counters().metrics(); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = command_buffer + .compute_command_encoder(DispatchType::Serial) + .expect("an encoder"); + dispatch(&mut encoder, &wild, &[&y]); + dispatch(&mut encoder, &step, &[&x]); + encoder.end_encoding(); + run(name, &command_buffer); + let after = device.counters().metrics(); + let split = sources(&after, &before); + assert_eq!( + split, + only(5, 1), + "{name}: the barrier between the unbound read and the write after it" + ); + let barriers = after.barriers - before.barriers; + assert_eq!(barriers, 2, "{name}: that barrier and the segment's end"); + let once: Vec = (0..WORDS as u32).map(|x| x * 2 + 1).collect(); + assert_eq!(words(&x), once, "{name}: the step of x"); + assert_eq!(words(&y), once, "{name}: the step of y"); + }); + } + /// What an encoder's commands count reaches the device's counters once, at the encoder's /// end: 2,000 dispatches that each read the one before's writes move none of them while the /// encoder is open, and its end adds the pass, the 1,999 barriers between them, the push and