diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 4154b667c..140aa4e52 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1873,8 +1873,12 @@ made, with no lock, and never rewritten for layouts. command buffers behind it, while the segments before it run, so the host can see a signal the command buffer makes before its wait. Other queues go on. Each queue groups on its own count: a command buffer of a queue with nothing running is submitted at once, ahead of every command - buffer committed after it on any queue, while one its queue holds, in a group or for room, may - be passed by another queue's command buffer committed after it, which only a fence or an event + buffer committed after it on any queue, its part before an event wait included (of the groups + that go at once, the one committed first goes first: on an M3 Max, a copy on another queue, + committed after a command buffer that fills, waits for the event the copy then signals, and + fills again, read the first fill and not the second, job m3-max-68673), while one its queue + holds, in a group or for room, may be passed by another queue's command buffer committed after + it, which only a fence or an event then orders after it. Metal's hazard tracking orders such a pair against work already submitted, and no more. Recorded on an M3 Max with macOS 27.0.1, with tracked buffers and no fence, a blit fill on one queue and a copy of what it filled on another, committed after it, five runs each: where the diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 226c415e1..245d17b76 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -2004,6 +2004,26 @@ impl WorkState { fn unheld(&self) -> Option { (self.fifos.iter()).position(|fifo| fifo.goes(self.stop)) } + + /// The FIFO, other than the one at `index`, whose group goes now ([`Fifo::goes`]) and whose + /// first command buffer was committed first, before every command buffer the group at `index` + /// holds ([`Fifo::first_committed`]): it goes before that group. So a group that goes at a + /// signal, as a host wait sends it, or at the end of a pass goes no sooner than another + /// queue's group that goes now and was committed before it, the part of a command buffer + /// before its event wait included: Metal submits a command buffer committed to a queue with + /// nothing ahead of it at its commit, and its hazard tracking orders after that part a command + /// buffer of another queue, committed later, that shares a tracked resource with it (the + /// runtime README's M3 Max rows, and the conformance case event-signaller-of-a-wait). Both go + /// now either way: neither waits for anything more. + fn goes_before(&self, index: usize) -> Option { + let before = self.fifos[index].first_committed(); + (0..self.fifos.len()) + .filter(|&other| other != index && self.fifos[other].goes(self.stop)) + .map(|other| (other, self.fifos[other].first_committed())) + .filter(|&(_, first)| first < before) + .min_by_key(|&(_, first)| first) + .map(|(other, _)| other) + } } impl Batch { @@ -2044,6 +2064,17 @@ impl Fifo { }) } + /// The place in the commit order of the first command buffer its group holds: the first that + /// finished in it, else its head, whose segments it holds; `u64::MAX` with no group. + fn first_committed(&self) -> u64 { + let Some(group) = &self.group else { + return u64::MAX; + }; + let finished = (group.finished.first()).map(|(_, pending, _)| pending.seq); + let partial = self.head().filter(|_| group.partial).map(|head| head.seq); + finished.or(partial).unwrap_or(u64::MAX) + } + /// Ends `entry` with `error` (its own, where it has one): behind the group held, which it /// joins to retire with; else once the queue's submissions in flight and the command buffers /// it ended before complete, at once where none is. One not committed ends at its commit. @@ -2242,9 +2273,17 @@ fn pass<'a>(core: &'a Core, mut state: MutexGuard<'a, WorkState>) -> MutexGuard< let mut joined: Vec = Vec::new(); let mut ended: Vec = Vec::new(); let mut sent = false; + // The group that goes once the groups committed before it that go now have gone + // (`WorkState::goes_before`), before anything more is staged: nothing staged after it + // passes it, so no waiter goes ahead of a signal it staged. + let mut then: Option = None; loop { let now = times::host_time(); - let index = match state.step(now, &core.counters, &mut joined, &mut ended) { + let step = match then.take() { + Some(index) => Step::Goes(index), + None => state.step(now, &core.counters, &mut joined, &mut ended), + }; + let index = match step { Step::Idle => match state.unheld() { Some(index) => index, None => { @@ -2259,6 +2298,13 @@ fn pass<'a>(core: &'a Core, mut state: MutexGuard<'a, WorkState>) -> MutexGuard< Step::Staged => continue, Step::Goes(index) => index, }; + let index = match state.goes_before(index) { + Some(before) => { + then = Some(index); + before + } + None => index, + }; let Some((value, uploads, batch)) = state.close(index, core, &work.released) else { continue; };