diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs index 206d46dfa..93cc1aa0b 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs @@ -577,10 +577,11 @@ impl CommandBufferShared { reached.push((value > 0).then_some((vk_queue.timeline, value))); } reached.extend(device.own_completed()); - // The batches before it on its queue that the host has not seen complete: each took the - // next value (`WorkCounts::batches_with_none_pending` and kin). - let completed = shared.queues[queue].completed_value(); - (shared.counts).batch_submitted_behind(scheduler.values[queue].saturating_sub(completed)); + // The batches before it on its queue that the host has not seen complete, as of the value + // the tracker just took, which its plan orders by: each took the next value + // (`WorkCounts::batches_with_none_pending` and kin). A second read of the queue's value + // could see a completion the plan did not. + (shared.counts).batch_submitted_behind(scheduler.tracker.pending_before(at)); // One a hang's thread made `Scheduled` early began then (`hang.rs`). if index == 0 && self.kernel.start() == 0 { self.kernel.begin(); diff --git a/crates/metal2vulkan-runtime/src/tests/tracker.rs b/crates/metal2vulkan-runtime/src/tests/tracker.rs index 21d9d97cf..215806bb7 100644 --- a/crates/metal2vulkan-runtime/src/tests/tracker.rs +++ b/crates/metal2vulkan-runtime/src/tests/tracker.rs @@ -860,6 +860,43 @@ fn the_state_retires_only_through_the_completed_value() { assert_eq!(queue.entries(), 0); } +/// A batch's count of the earlier batches of its `VkQueue` the host has not seen complete +/// ([`Tracker::pending_before`], which `WorkCounts::batches_with_none_pending` and kin count) is +/// the tracker's own, from the completed value its plan retires by: a read after the write of the +/// batch before it has a barrier exactly where that batch is counted pending. A count from a +/// later read of the queue's value could find that batch complete where the plan ordered after it. +#[test] +fn a_batch_is_counted_pending_behind_exactly_the_work_its_plan_orders_after() { + let write = || one(&[writes(1, bytes(0..256), copy_write())]); + let read = || one(&[reads(1, bytes(0..256), copy_read())]); + for seen in [0, 1] { + let mut queue = Tracker::default(); + assert_eq!(queue.pending_before(at(1)), 0); + assert_eq!(commit(&mut queue, &[write()], 1), [None]); + queue.completed(0, seen); + let pending = queue.pending_before(at(2)); + let barriers = commit(&mut queue, &[read()], 2); + let ordered = (seen == 0) + .then(|| barrier(copy_write(), copy_read())) + .flatten(); + assert_eq!( + (pending, barriers), + (1 - seen, vec![ordered]), + "seen {seen}" + ); + } + // Another queue's completion, and an older value, leave a queue's count as it was. + let mut queue = Tracker::default(); + for value in 1..=3 { + assert_eq!(commit(&mut queue, &[read()], value), [None]); + } + queue.completed(0, 2); + queue.completed(1, 3); + queue.completed(0, 1); + assert_eq!(queue.pending_before(at(4)), 1); + assert_eq!(queue.pending_before(on(1, 4)), 0); +} + /// A completion looks only at the resources its batches touched: a thousand batches in flight, /// each reading a resource of its own, and the first one's completion retires its resource alone. #[test] diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs index 21fc6c2f2..5c0f52acc 100644 --- a/crates/metal2vulkan-runtime/src/tracker.rs +++ b/crates/metal2vulkan-runtime/src/tracker.rs @@ -2647,6 +2647,15 @@ impl Tracker { } } + /// The batches of `at`'s `VkQueue` before it that the host had not seen complete as of the + /// last [`Tracker::completed`] of that queue: the value a plan at `at` retires by, so a plan + /// orders after the work of exactly these batches (`WorkCounts::batches_with_none_pending` and + /// kin count them). + pub(super) fn pending_before(&self, at: Point) -> u64 { + let through = self.completed.get(at.queue).copied().unwrap_or(0); + at.value.saturating_sub(1).saturating_sub(through) + } + /// The barriers before the `encoders` of the batch submitted next, at `at`, and its waits. /// The state is unchanged; the plan's lists come from those plans applied before gave back. pub(super) fn plan<'a>( diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 18528647b..eda151e60 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -5515,8 +5515,9 @@ pub struct WorkCounts { pub group_ends_held: u64, /// Batches submitted (each held in a group counted as it is held) while no earlier batch of /// their `VkQueue` was pending: every one submitted before them the host had seen complete - /// (their queue's timeline at their values). With the three fields after this one, every - /// batch, by how far its queue's work ran behind its submission. + /// (their queue's timeline at their values), as of the completed value the hazard tracker took + /// for their plan, so a batch counted here is planned after no work in flight. With the three + /// fields after this one, every batch, by how far its queue's work ran behind its submission. pub batches_with_none_pending: u64, /// Batches submitted while one earlier batch of their `VkQueue` was pending. pub batches_with_one_pending: u64,