From a5c7cbadfa7dfb6c43b57e7ec4b4445b40f1fa97 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 21:50:17 -0800 Subject: [PATCH] Test a buffer over host pages made on another thread while a draw takes every step What: tests::draw_again gains a_buffer_over_host_pages_made_while_a_draw_takes_every_step_is_met_by_the_next_draw. A new test-build hook, Hooks::at_keep (device/line.rs, with the recovery tests' hooks), runs once on the lowering thread as the n-th draw that takes every step reaches its keeping, after its uses' noting. The test holds the run's first draw again there (in a debug build, the first draw again takes every step for its check) while another thread makes a no-copy buffer over host pages. That moves the host aliases' version and the device's "has buffers over host pages" flag, which a kept draw's Under reads through NotedInto. The test asserts that the next draw takes every step (draws_unlike_kept_under 1), one draw fewer draws again than the reference run, and the pixels match. Why: render/draw_again.rs:994 "a draw again kept no draw" fired intermittently in lone_fetch::a_lone_fetched_bgra8unorm_color_over_host_pages_reaches_them (jobs 24214, 24235) and in pass_end_tiles. The device's in-place check (in_place_check.rs) runs on a thread of its own and makes and lets go no-copy buffers over host pages on the program's device. When one lands between a checked draw again's noting and its keeping, noted_now compares the noting's alias version with the moved one and the draw keeps nothing. The hook forces that moment, so the test fails without the fix instead of waiting for the race. Verified: expected to fail here (the panic) and pass with the next commit, as potluck jobs on lavapipe; reported with the shas. --- .../metal2vulkan-runtime/src/device/line.rs | 33 ++++++- crates/metal2vulkan-runtime/src/render.rs | 4 + .../src/tests/draw_again.rs | 87 ++++++++++++++++++- 3 files changed, 121 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/device/line.rs b/crates/metal2vulkan-runtime/src/device/line.rs index ffe1f205c..ecfe9bee1 100644 --- a/crates/metal2vulkan-runtime/src/device/line.rs +++ b/crates/metal2vulkan-runtime/src/device/line.rs @@ -848,14 +848,19 @@ impl Device { } } -/// The device tests' hooks into a line's recoveries (`tests::device_recovery`), in those tests' -/// builds only: each set on one device, so a test's settings reach no other test's device. +/// The device tests' hooks into a line's recoveries (`tests::device_recovery`) and its render +/// encoders' draws (`tests::draw_again`), in those tests' builds only: each set on one device, so +/// a test's settings reach no other test's device. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) mod hooks { + use super::lock; use std::sync::atomic::AtomicBool; use std::sync::Mutex; use std::time::Duration; + /// What runs at a draw's keeping ([`Hooks::at_keep`]). + pub(crate) type AtKeep = Box; + /// What a test set of a line's recoveries. #[derive(Default)] pub(crate) struct Hooks { @@ -876,5 +881,29 @@ pub(crate) mod hooks { /// as Metal's timed-out command buffer (`command_buffer/stall.rs`), in place of its /// `WATCHDOG`. pub(crate) watchdog_after: Mutex>, + /// Run once, on the thread that lowers it, as the draw that is the count's (from zero) + /// to take every step since it was set reaches its keeping, after its uses' noting: what + /// another thread may do at that moment (`render/draw_again.rs`). + pub(crate) at_keep: Mutex>, + } + + impl Hooks { + /// A draw that took every step reached its keeping: run [`at_keep`](Self::at_keep)'s + /// hook where it is this draw's, without its lock. + pub(crate) fn reach_keep(&self) { + let mut at_keep = lock(&self.at_keep); + let hook = match at_keep.as_mut() { + Some((0, _)) => at_keep.take(), + Some((count, _)) => { + *count -= 1; + None + } + None => None, + }; + drop(at_keep); + if let Some((_, hook)) = hook { + hook(); + } + } } } diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 0328438cd..e5ae9043d 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -11354,6 +11354,10 @@ impl RenderLowering { .pipeline .as_ref() .and_then(RenderPipelineState::downgrade); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if !self.replaying { + device.line().hooks.reach_keep(); + } let keeps = plain && !self.replaying && self.core.noted_now(&live.noted); // What kept a direct draw of its own from being kept (`WorkCounts::draws_unkept`). if !keeps && !self.replaying && !self.rerouting && !empty && !culled { diff --git a/crates/metal2vulkan-runtime/src/tests/draw_again.rs b/crates/metal2vulkan-runtime/src/tests/draw_again.rs index a7f669647..a6f48498b 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw_again.rs @@ -3,7 +3,8 @@ //! Vulkan draw command alone (`WorkCounts::draws_as_before`), with the same commands, barriers //! and pixels as the same draws with a call between each that changes nothing; and only then: not //! a draw of other arguments, an indirect draw, a draw that writes memory, a draw after a no-copy -//! buffer moved (its binding may read elsewhere), nor a draw that meets a notice its device +//! buffer moved (its binding may read elsewhere) or after another thread made a buffer over host +//! pages while the draw before it took every step, nor a draw that meets a notice its device //! announces at every occurrence, which announces it at every draw. A draw again after calls that //! bound other buffers, offsets or bytes at its buffer slots, or other textures or sampler states //! at its texture and sampler slots, rebinds those alone (`render/draw_again/rebound.rs`, @@ -650,6 +651,90 @@ fn a_draw_after_a_no_copy_buffer_moved_takes_every_step() { assert_eq!(moving.words, reference.words, "the pixels of the draws"); } +/// A buffer over host pages made on another thread while a draw takes every step, after its uses' +/// noting and before its keeping (`device/line.rs`'s `Hooks::at_keep` holds the draw there until +/// it is made), moves what draws read that changes with no call of the encoder (the host aliases' +/// version, `noted_apart.rs`), as the device's in-place check does making its own buffers +/// (`in_place_check.rs`): the draw is kept under the state its steps began in, and the next draw +/// takes every step, finding it moved. In a build with debug assertions the draw is the first draw +/// again, whose check compares nothing once the state moved under it. The pixels are those of the +/// draws with nothing made meanwhile. +#[test] +fn a_buffer_over_host_pages_made_while_a_draw_takes_every_step_is_met_by_the_next_draw() { + let page = Page::new(); + let scratch = Scratch::new("draw-again-meanwhile"); + let heard = Heard::default(); + let device = device(&scratch.0, &[], &heard); + let state = adding_state(&device); + let plain = |encoder: &mut RenderCommandEncoder| rectangles(encoder, &state, |_| {}); + warm(&device, plain); + let as_before = |counts: &WorkCounts| counts.draws_as_before; + let under = |counts: &WorkCounts| counts.draws_unlike_kept_under; + let reference = run(&device, plain); + assert_eq!(reference.counted(as_before), drawn_alone(HITS)); + // The draw held at its keeping: the first draw again, which takes every step, in a build with + // debug assertions; else the run's first draw, the only one that does. + let held = u64::from(cfg!(debug_assertions)); + let (ask, asked) = std::sync::mpsc::channel::<()>(); + let (tell, told) = std::sync::mpsc::channel::<()>(); + let (end, ended) = std::sync::mpsc::channel::<()>(); + let (on, over) = (&device, &page); + let (made, ran, moved) = std::thread::scope(|scope| { + let maker = scope.spawn(move || { + asked.recv().ok()?; + let before = on.shared().aliases.version(); + // SAFETY: the page outlives the buffer (dropped after the device), and no command + // uses the buffer. + let made = unsafe { + on.new_buffer_with_bytes_no_copy( + over.memory(), + BYTES as u64, + ResourceOptions::shared(), + None, + ) + }; + let moved = on.shared().aliases.version() != before; + tell.send(()).ok()?; + match made { + // It goes once the run ended, so the version moves once inside the run. + Ok(buffer) => { + ended.recv().ok()?; + drop(buffer); + Some(moved) + } + Err(Error::Unsupported(gap)) => { + eprintln!("DRAW-AGAIN: the device imports no host memory: {gap:?}"); + None + } + Err(error) => panic!("new_buffer_with_bytes_no_copy: {error:?}"), + } + }); + let hook: crate::device::line::hooks::AtKeep = Box::new(move || { + ask.send(()).unwrap(); + told.recv().unwrap(); + }); + *device.line().hooks.at_keep.lock().unwrap() = Some((held, hook)); + let made = run(&device, plain); + // A hook that did not run goes, with its sender: the maker then ends at once. + let unran = device.line().hooks.at_keep.lock().unwrap().take(); + let ran = unran.is_none(); + drop(unran); + // Sent to a maker that made a buffer; one that made none ended already. + let _ = end.send(()); + (made, ran, maker.join().unwrap()) + }); + assert!(ran, "the hook ran"); + let Some(moved) = moved else { + return; + }; + assert!(moved, "the buffer's making moved the host aliases' version"); + // The draw after the held one took every step, finding the version moved; the rest drew + // again as before. + assert_eq!(made.counted(under), 1); + assert_eq!(made.counted(as_before), reference.counted(as_before) - 1); + assert_eq!(made.words, reference.words, "the pixels of the draws"); +} + /// A draw that meets a notice its device announces at every occurrence (`UnsizedPoints`: points /// of a vertex function that writes no point size) takes every step at every draw, and announces /// it at each; on a device that announces it once, the draws after the first draw again.