What: a command buffer's ShadowCarry (an earlier encoder left the shadow's image holding the
truth, so a later one takes no copy in) now lasts while its encoders stay in one batch.
State::shadows_to_carry clears it where the encoder opening next starts another batch:
- the batches split since the last ended encoder, at a wait, a host step or a resource state
encoder (State::carry_batches, set at end_encoder);
- or the current batch signals (Batches::signals_pending), so the next encoder starts the next.
ShadowCopies::touched and shadow::Touched are gone, with every field initialiser, Entry::met,
Carried::touched, ShadowCarry::queue and ShadowCarry::on. Another command buffer's encoder
ending in between, of this queue or another, drops nothing.
Why: the consumer's ruling. A later encoder that sees a CPU write made after its command
buffer's commit is a correctness bug on every device. On the GPU nothing runs between two
encoders of one batch but the command buffer's own commands. A queue's batches go in commit
order, and another queue's batch that conflicts with this one's uses of the texture runs
wholly before or after it: each copy in is a tracked write of the image. So the counted ends
dropped carries that held, and the later encoder then copied in at its own run, a CPU write made
after the commit among it. Another queue's work comes between only where the batches split.
There the carry used to survive a signal, and the encoder after it read the image as the
earlier encoder left it, missing another queue's write that ran in between.
shadow.rs's Copy in rule now records what Metal does with a CPU write made once the command
buffer's own signal was seen, measured by the new probe
metal2vulkan-conformance/probes/cpu_write_after_a_signal.m. On the M3 Max (macOS 27.0.1),
three runs of each case, every run alike:
- the read after the signal took the write, in the encoder after it (out2 64 of 64 NEW) and
in a later dispatch of that encoder (64 of 64);
- with no signal, a write while the command buffer ran reached no later encoder (64 of 64 OLD);
- a write before the commit reached both reads.
The GPU does not see a CPU store to a word while a kernel runs. Metal ended a spin waiting for
one ("Caused GPU Hang Error"), and also one dispatch of about 1.5 s, so the probe and the tests
spin in 150 dispatches of about 10 ms each, the cap timed first.
Tests:
- tests::batches::a_signal_is_pending_until_the_encoder_after_it_starts_a_batch (new).
- tests::shadow::a_carried_surface_is_dropped_by_anything_that_lets_another_writer_in: another
command buffer's encoder ending between, or while the encoder was open, now keeps the carry.
only_another_queue_s_encoder_drops_a_carried_surface is gone with Touched.
- runtime_lane::cross_queue_shadows (new), on the device the cfg selects:
- a_cpu_write_while_a_command_buffer_runs_reaches_none_of_its_encoders: X of queue 1 reads,
spins and reads; Y of queue 2 reads between X's reads; the CPU writes while X spins (X
Scheduled). Both of X's reads hold the bytes as of the commit, the second through the
carry (64 texels carried).
- an_encoder_after_a_signal_reads_what_another_queue_wrote_after_it: Y of queue 2 waits for
an event and fills a row; X of queue 1 reads, signals, reads. The second read holds the fill.
- a_cpu_write_once_a_signal_was_seen_reaches_the_reads_after_it: the probe's two signal
shapes through the runtime. The read after the signal holds the CPU's write.
Verified on the M3 Max (MoltenVK 1.4.2) under --cfg m2v_gpu, on f7abac8bbb plus the per-draw
log fix: those three, parallel_encodes, and the runtime's tests::shadow, tests::batches and
tests::fresh_unwritten pass. In that run the waiting-writer test that follows failed, as
expected without its fix. With this commit's runtime changes left out (95f22fb742, the same
validation tests), all three device tests fail on the M3 Max:
- the CPU-write test's second read held the write (0 texels carried);
- the signal test's second read missed the fill;
- the signal-seen test's read after the signal missed the write.
The checks at this landing's tip are in its last commit.
(cherry picked from commit 4cf86795b2430feb28062aa467333249dc62310e)
(cherry picked from commit 4b8b5eda8025d7abaaaacc929eacdfd1f1bcc968)
210 lines
7.9 KiB
Rust
210 lines
7.9 KiB
Rust
//! A command buffer's batches: its encoders, split where it waits for events or for the caller's
|
|
//! binary semaphores, and before the part of an encoder that executes commands the GPU may write,
|
|
//! which runs once everything before it completed.
|
|
//!
|
|
//! A batch is its waits, then its encoders, then its signals, and the scheduler submits each batch
|
|
//! once its waits are satisfied. A wait after an encoder or a signal starts a new batch, since
|
|
//! everything encoded before it must be able to run, and to signal, while it waits (measured on
|
|
//! Metal). An encoder after a signal starts one too, so the signal happens once the encoders before
|
|
//! it completed, not the ones after. Within one batch, two waits for one event are the wait for the
|
|
//! larger value, and two signals of one event the signal of the larger value, which is what Metal's
|
|
//! maximum makes of them. A binary semaphore is waited for or signaled at most once per batch: each
|
|
//! wait consumes one signal, so a second wait or signal of the same semaphore starts a new batch,
|
|
//! in order. Every command buffer has at least one batch, so even one with nothing encoded
|
|
//! completes in its queue's order.
|
|
|
|
/// One batch: waits, a run of encoders, and signals, with events of type `E` and binary
|
|
/// semaphores of type `S`.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub(crate) struct Batch<E, S> {
|
|
/// The events waited for, each once, with the value.
|
|
pub(crate) waits: Vec<(E, u64)>,
|
|
/// The binary semaphores waited for, each once.
|
|
pub(crate) binary_waits: Vec<S>,
|
|
/// The number of the command buffer's encoders before this batch's end.
|
|
pub(crate) end: usize,
|
|
/// The events signaled, each once, with the value.
|
|
pub(crate) signals: Vec<(E, u64)>,
|
|
/// The binary semaphores signaled, each once.
|
|
pub(crate) binary_signals: Vec<S>,
|
|
}
|
|
|
|
impl<E, S> Batch<E, S> {
|
|
fn new(end: usize) -> Batch<E, S> {
|
|
Batch {
|
|
waits: Vec::new(),
|
|
binary_waits: Vec::new(),
|
|
end,
|
|
signals: Vec::new(),
|
|
binary_signals: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// Whether the batch signals anything.
|
|
fn signals_any(&self) -> bool {
|
|
!self.signals.is_empty() || !self.binary_signals.is_empty()
|
|
}
|
|
}
|
|
|
|
/// The batches of a command buffer being encoded: the last one is the current one.
|
|
#[derive(Debug)]
|
|
pub(crate) struct Batches<E, S> {
|
|
batches: Vec<Batch<E, S>>,
|
|
/// The encoders of the current batch.
|
|
encoders: usize,
|
|
/// The resource state encoders the current batch begins with.
|
|
binds: usize,
|
|
/// The event waits encoded so far (`encodeWaitForEvent:value:`), each call counted.
|
|
event_waits: usize,
|
|
/// The event signals encoded so far (`encodeSignalEvent:value:`), each call counted.
|
|
event_signals: usize,
|
|
}
|
|
|
|
impl<E, S> Default for Batches<E, S> {
|
|
fn default() -> Batches<E, S> {
|
|
Batches {
|
|
batches: vec![Batch::new(0)],
|
|
encoders: 0,
|
|
binds: 0,
|
|
event_waits: 0,
|
|
event_signals: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E, S> Batches<E, S> {
|
|
/// An encoder ended.
|
|
pub(crate) fn encoder(&mut self) {
|
|
if self.current().signals_any() {
|
|
self.split();
|
|
}
|
|
self.encoders += 1;
|
|
self.current().end += 1;
|
|
}
|
|
|
|
/// The next encoder, the part of one that executes commands the GPU may write, starts a
|
|
/// batch of its own: the scheduler submits it once everything before it completed and its
|
|
/// host step recorded the commands.
|
|
pub(crate) fn host_step(&mut self) {
|
|
self.split();
|
|
}
|
|
|
|
/// The next encoder is a resource state encoder: its updates bind before its batch's work, so
|
|
/// it starts a batch unless the current one holds only such encoders and no signal.
|
|
pub(crate) fn sparse(&mut self) {
|
|
if self.encoders > self.binds || self.current().signals_any() {
|
|
self.split();
|
|
}
|
|
self.binds += 1;
|
|
}
|
|
|
|
/// The batches, in order: at least one.
|
|
pub(crate) fn all(&self) -> &[Batch<E, S>] {
|
|
&self.batches
|
|
}
|
|
|
|
/// Whether the current batch signals, so the next encoder starts a batch of its own
|
|
/// ([`encoder`](Self::encoder)).
|
|
pub(crate) fn signals_pending(&self) -> bool {
|
|
self.batches.last().is_some_and(Batch::signals_any)
|
|
}
|
|
|
|
/// The event waits and the event signals encoded so far, each call counted, binary
|
|
/// semaphores' none: an encoder that ends now has them all before it
|
|
/// (`CommandBuffer::ended_encoders`).
|
|
pub(crate) fn events(&self) -> (usize, usize) {
|
|
(self.event_waits, self.event_signals)
|
|
}
|
|
|
|
/// Signal no event (`CommandBuffer::commit_failed` without signals): every batch keeps its
|
|
/// waits, encoders and binary semaphore signals.
|
|
pub(crate) fn drop_signals(&mut self) {
|
|
for batch in &mut self.batches {
|
|
batch.signals.clear();
|
|
}
|
|
}
|
|
|
|
/// Signal no event after the first `parts` encoders (or parts of them): a command buffer
|
|
/// Metal fails from an encoder on signals what was encoded before it. The batches that end
|
|
/// past them keep their waits, encoders and binary semaphore signals.
|
|
pub(crate) fn drop_signals_after(&mut self, parts: usize) {
|
|
for batch in self.batches.iter_mut().filter(|batch| batch.end > parts) {
|
|
batch.signals.clear();
|
|
}
|
|
}
|
|
|
|
fn current(&mut self) -> &mut Batch<E, S> {
|
|
let last = self.batches.len() - 1;
|
|
&mut self.batches[last]
|
|
}
|
|
|
|
fn split(&mut self) {
|
|
let end = self.current().end;
|
|
self.batches.push(Batch::new(end));
|
|
self.encoders = 0;
|
|
self.binds = 0;
|
|
}
|
|
|
|
/// Start a new batch where a wait cannot join the current one: it has encoders or signals.
|
|
fn before_wait(&mut self) {
|
|
if self.encoders > 0 || self.current().signals_any() {
|
|
self.split();
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: PartialEq, S: PartialEq> Batches<E, S> {
|
|
/// `encodeWaitForEvent:value:`.
|
|
pub(crate) fn wait(&mut self, event: E, value: u64) {
|
|
self.event_waits += 1;
|
|
self.before_wait();
|
|
add_max(&mut self.current().waits, event, value);
|
|
}
|
|
|
|
/// The runtime's own wait for `event` to reach `value`, which no `encodeWaitForEvent:value:`
|
|
/// made: it splits the batches as one does, and is not counted among the event waits
|
|
/// ([`events`](Self::events)).
|
|
pub(crate) fn hold(&mut self, event: E, value: u64) {
|
|
self.before_wait();
|
|
add_max(&mut self.current().waits, event, value);
|
|
}
|
|
|
|
/// The runtime's own signal of `event` to `value`, which no `encodeSignalEvent:value:` made:
|
|
/// it ends the batch as one does, and is not counted among the event signals
|
|
/// ([`events`](Self::events)).
|
|
pub(crate) fn hold_signal(&mut self, event: E, value: u64) {
|
|
add_max(&mut self.current().signals, event, value);
|
|
}
|
|
|
|
/// `encodeSignalEvent:value:`.
|
|
pub(crate) fn signal(&mut self, event: E, value: u64) {
|
|
self.event_signals += 1;
|
|
add_max(&mut self.current().signals, event, value);
|
|
}
|
|
|
|
/// A wait for binary semaphore `semaphore` (`encode_wait_for_vk_semaphore`).
|
|
pub(crate) fn binary_wait(&mut self, semaphore: S) {
|
|
self.before_wait();
|
|
if self.current().binary_waits.contains(&semaphore) {
|
|
self.split();
|
|
}
|
|
self.current().binary_waits.push(semaphore);
|
|
}
|
|
|
|
/// A signal of binary semaphore `semaphore` (`encode_signal_vk_semaphore`).
|
|
pub(crate) fn binary_signal(&mut self, semaphore: S) {
|
|
if self.current().binary_signals.contains(&semaphore) {
|
|
self.split();
|
|
}
|
|
self.current().binary_signals.push(semaphore);
|
|
}
|
|
}
|
|
|
|
/// Add `event` at `value` to `list`, which holds each event once at its largest value.
|
|
fn add_max<E: PartialEq>(list: &mut Vec<(E, u64)>, event: E, value: u64) {
|
|
match list.iter_mut().find(|(known, _)| *known == event) {
|
|
Some((_, known)) => *known = (*known).max(value),
|
|
None => list.push((event, value)),
|
|
}
|
|
}
|