From 06372f9dd6b762c3d7965adcf2dfc03c8d8d1ae4 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 21:51:22 -0800 Subject: [PATCH] A draw is kept under what another thread may change as its steps began What: render/draw_again.rs gains Outside: the parts of a kept draw's Under that another thread may change while a draw takes its steps: - the no-copy buffers' moves; - the sampler slots' changes; - whether the device has buffers over host pages, and the host aliases' version. RenderLowering::draw_began notes them as draw() begins. kept_under answers Under as of now, but with those parts as of the draw's start. Both the plain draw's keeping (render.rs) and a draw rebound (rebound.rs) keep their draw under kept_under. The plain keeping weighs its uses' noting against that state: EncoderCore::noted_now now takes the NotedInto to compare with. So a change made meanwhile on any thread makes the next draw take every step. Before, the keep read the moved state, so one of two things happened: - the draw kept nothing; - or it was kept under a state its bindings and noting never read: a version that moved after noted_now but before Kept::under, or a sampler state made after the draw listed them. The debug check (Checked::check) compares nothing where Outside moved while the checked draw took every step: its steps read another state than the kept draw's. The module docs say both. Why: the device's in-place check (in_place_check.rs) makes and lets go no-copy buffers over host pages on the program's device from its own thread. When one landed between a checked draw again's noting and its keeping, the draw kept nothing and the check panicked with "a draw again kept no draw" (render/draw_again.rs:994). That was the intermittent failure in lone_fetch and pass_end_tiles on lavapipe (jobs 24214, 24235). The skip of first-move zeros was not involved: neither test's Private texture registers an overwrite. Verified: as potluck jobs on lavapipe, the previous commit's test and the draw_again tests, the runtime suite, and clippy; reported with the shas. --- crates/metal2vulkan-runtime/src/encoder.rs | 12 +-- crates/metal2vulkan-runtime/src/render.rs | 13 ++- .../src/render/draw_again.rs | 83 ++++++++++++++++++- .../src/render/draw_again/rebound.rs | 2 +- 4 files changed, 99 insertions(+), 11 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index e0d13fba8..77ee8b801 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -1916,12 +1916,14 @@ impl EncoderCore { } /// Whether the command apart `noted` keeps, the last one [`note_reaching`](Self::note_reaching) - /// noted, was noted into the encoder's state now: then a command of its stages and uses, - /// reaching the keys it reached through the resident set alone, is noted already, and noting - /// it notes only its reads of fresh textures and what the encoder made resident since + /// noted, was noted into the encoder's state `into` (as [`noted_into`](Self::noted_into) + /// answers, or a kept draw is kept under: `render/draw_again.rs`), with the encoder's uses + /// unordered: then, while that state stands, a command of its stages and uses, reaching the + /// keys it reached through the resident set alone, is noted already, and noting it notes only + /// its reads of fresh textures and what the encoder made resident since /// ([`note_again`](Self::note_again)). - pub(super) fn noted_now(&self, noted: &NotedApart) -> bool { - !self.uses.ordered() && noted.noted_into(self.noted_into()) + pub(super) fn noted_now(&self, noted: &NotedApart, into: NotedInto) -> bool { + !self.uses.ordered() && noted.noted_into(into) } /// Note again a command apart noted already ([`noted_now`](Self::noted_now)) with `uses`, as diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index e5ae9043d..3be7a912d 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -8206,6 +8206,9 @@ impl RenderLowering { if let Some(notes) = self.pass_notes.as_mut() { notes.drew(self.pipeline.as_ref(), &self.fragment.textures); } + // What another thread may change of what its steps read, as they begin: what a draw it + // keeps is kept under (`render/draw_again.rs`'s `Outside`). + self.draw_began(); // The encoder's kept draw again, which no call came between: its Vulkan draw alone // (`render/draw_again.rs`), the pipeline state's own where the last draw drew through // another. Any other draw keeps itself in its place where it can, and forgets every @@ -11349,7 +11352,9 @@ impl RenderLowering { } _ => {} } - // Its uses noted as the next draw would find them noted (`noted_apart.rs`). + // Its uses noted as the next draw would find them noted (`noted_apart.rs`), into the + // state it is kept under: what another thread may change as its steps began + // (`render/draw_again.rs`'s `Outside`), so uses noted after such a change keep nothing. let drawn_by = self .pipeline .as_ref() @@ -11358,7 +11363,9 @@ impl RenderLowering { if !self.replaying { device.line().hooks.reach_keep(); } - let keeps = plain && !self.replaying && self.core.noted_now(&live.noted); + let under = self.kept_under(); + let keeps = + plain && !self.replaying && self.core.noted_now(&live.noted, under.noted_into()); // What kept a direct draw of its own from being kept (`WorkCounts::draws_unkept`). if !keeps && !self.replaying && !self.rerouting && !empty && !culled { let unkept = UnkeptDraw { @@ -11428,7 +11435,7 @@ impl RenderLowering { assembly, draw, index: index.and_then(draw_again::KeptIndex::of), - under: self.under(), + under, command_buffer: self.current.draws, command: vk_draws[0].2, again, diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index eb6b786d5..7b1d2d20b 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -52,7 +52,12 @@ //! and its host aliases' version, the encoder's parts and held images (what noting reads, //! `noted_apart.rs`), with the command buffer the current instance records into, and the count //! of the device's sampler IDs' slots' changes (a sampler state made or gone, which every step -//! of a draw lists again: `sampler::ArgumentSamplers`). +//! of a draw lists again: `sampler::ArgumentSamplers`). Another thread may change the moves, the +//! sampler slots and the buffers over host pages with their aliases' version while a draw takes +//! its steps (the device's in-place check makes and lets go buffers over host pages on a thread +//! of its own): a draw is kept under them as its steps began, which they read ([`Outside`]), +//! and its uses' noting counts only where it was noted under them, so a change made meanwhile +//! makes the next draw take every step. //! - **One per pipeline state.** A pipeline state set keeps the kept draw: at the next draw //! through another state, it goes aside with its state's bind state and the storage its draws //! wrote (`LiveBind::select`, `RenderLowering::select_kept`), and the draw starts from its own @@ -113,7 +118,9 @@ //! - **Checked in builds with debug assertions.** The first draw again of a kept draw, and every //! second one after it, takes every step instead, and checks that it announced nothing, recorded //! no state command, kept an equal draw, retained only its index buffer again, and left the -//! encoder's drawn area, coverage, reads, gathered uses, noted uses and instance as they were. +//! encoder's drawn area, coverage, reads, gathered uses, noted uses and instance as they were; +//! where another thread changed what it reads meanwhile ([`Outside`]), its steps read another +//! state than the kept draw's, and it checks nothing. mod rebound; mod restated; @@ -153,6 +160,9 @@ pub(super) struct DrawAgain { /// that sets it ([`restate_again`](RenderLowering::restate_again)), so a kept draw that holds /// another is restated (the module docs). stated: Stamp, + /// What another thread may change of what the encoder's current draw reads, as its steps + /// began ([`Outside`], [`RenderLowering::draw_began`]). + began: Outside, /// Draws again since the draw was kept: in builds with debug assertions, the first and every /// second one after it take every step instead (the module docs). #[cfg(debug_assertions)] @@ -432,6 +442,28 @@ pub(super) struct Under { samplers: u64, } +impl Under { + /// What its uses were noted into. + pub(super) fn noted_into(&self) -> NotedInto { + self.into + } +} + +/// What of an [`Under`] another thread may change while a draw takes its steps: the no-copy +/// buffers' moves, the count of the device's sampler IDs' slots' changes, and whether the device +/// has buffers over host pages and the host aliases' version, which a buffer over host pages made +/// or let go on any thread moves (the device's in-place check makes and lets go its own, +/// `in_place_check.rs`). A draw that takes its steps is kept under these as they were when its +/// steps began, which they read ([`RenderLowering::kept_under`]): a change made meanwhile makes +/// the next draw take every step. +#[derive(Clone, Copy, Default, PartialEq, Eq)] +pub(super) struct Outside { + moved: u64, + rebinds: bool, + aliases: u64, + samplers: u64, +} + /// Whether a draw is its encoder's kept draw again ([`RenderLowering::draw_again`]). pub(super) enum Again { /// It is, and was recorded. @@ -489,6 +521,45 @@ impl RenderLowering { } } + /// What of [`under`](Self::under) another thread may change ([`Outside`]), now. + fn outside(&self) -> Outside { + let Under { + moved, + into, + samplers, + .. + } = self.under(); + Outside { + moved, + rebinds: into.rebinds, + aliases: into.aliases, + samplers, + } + } + + /// A draw begins its steps: note what another thread may change of what it reads + /// ([`Outside`]), which a draw it keeps is kept under ([`kept_under`](Self::kept_under)). + pub(super) fn draw_began(&mut self) { + self.again.began = self.outside(); + } + + /// What a draw that took its steps is kept under: what changes with no call of the encoder + /// as [`under`](Self::under) answers now, but what another thread may change as the draw's + /// steps began ([`Outside`]), which they read; so one made meanwhile differs from the next + /// draw's, which then takes every step. + pub(super) fn kept_under(&self) -> Under { + let Outside { + moved, + rebinds, + aliases, + samplers, + } = self.again.began; + let mut under = self.under(); + (under.moved, under.samplers) = (moved, samplers); + (under.into.rebinds, under.into.aliases) = (rebinds, aliases); + under + } + /// Forget every kept draw, the last direct draw's and those set aside with other pipeline /// states' bind states (a call came, a draw that kept nothing, or the encoder's end). pub(super) fn forget_again(&mut self) { @@ -975,6 +1046,14 @@ impl Checked { announced, expected, } = self; + // What another thread may change moved while the draw took every step ([`Outside`]): its + // steps read the state after the change, which the kept draw never read, so it may note + // its uses anew or keep nothing (kept, it is kept under the state its steps began in), + // as a draw again after the change takes every step. Nothing is compared. + if encoder.outside() != encoder.again.began { + encoder.again.hits = hits; + return; + } let rebound = matches!(expected, Expected::Rebound(_)); match &expected { // Its uses differ from the kept draw's: what noting them adds is every step's own. diff --git a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs index c01fe60bf..4d0c97ae8 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs @@ -1465,7 +1465,7 @@ impl RenderLowering { } self.last_retained .retain_samplers(&mut self.core, &mut live.samplers); - let under = self.under(); + let under = self.kept_under(); if let Some(kept) = self.again.kept.as_mut() { kept.under = under; }