From 5bf7ae2eaea7800a4d027a73f91ba0bf01279f4b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 13 Nov 2026 14:26:06 -0800 Subject: [PATCH] RUNTIME 2243: a committing command buffer counts as uncommitted until the scheduler holds its batches The consumer's Iris walk at pin 73 (appwalk10 w-iris-26) failed one guest command buffer during login with the runtime's own invariant "a batch's end recorded where the batch does not end" (command_buffer/submit.rs), read as VK_ERROR_UNKNOWN (-13): the first tagged sighting of the -13 seen untagged three times on the 3090 and the 780M since pin 59. Cause: a texture's face turn (texture/faces.rs turn_face) and a storage-form switch need the device's Uncommitted count at 1 and the scheduler holding nothing. Both held for a command buffer B in commit_now between slot.committed() and scheduler.commit(), where B's end is folded into its last command buffer (face_swaps.any() false). Interleaving: B encodes an attached-read pass that copies into the texture's other face G; B commits and folds its end; before B reaches the scheduler, A opens a pass that turns the texture F -> G; at B's submission record_face_fixes finds A's record and pushes a back copy G -> F after B's folded command buffer, so work.last() is not the folded one and the command buffer fails. The same back copy would also have written stale texels over B's rendering in F. Now a committing command buffer stays in the Uncommitted count until scheduler.commit holds its batches (Slot::handed; held.open still drops at committed(), so slot_wait is unchanged): no turn or storage-form switch lands between the fold and the submission. Only turn_face and storage_form_switch read the count, and both refuse rather than wait (Unturned::Busy and the copy-in path; no switch), so nothing waits on it. Verified (batchend, 42e70c6642 on 33de04117d), lavapipe on msi: new device tests holding B at a new trace point Point::Committing while A's pass opens. Negative control (the tip without the fix, 26731): the folded test panics on the invariant (submit.rs:1145), the apart test finds B's pixels clobbered (4096 bytes differ). Tip (26755): 6/6, finale folded 1 / apart 1 as built, Moved {swaps 0, busy 2, fixes 0}, B's pixels right at every step. Turns still happen: the existing a_texture_s_faces_take_turns_for_its_attached_reads shows swaps 1 in each of its turn cases, the same at base and tip. clippy --cfg m2v_cpu_icd (26758), --cfg m2v_gpu (26726), plain (26693) and rustdoc (26725) 0. The command_buffer/submission/queue/storage_form/attached-read suites on lavapipe (26759) and the XTX leg follow on development. RUNTIME_VERSION 2242 -> 2243. --- .../src/command_buffer/completion.rs | 6 +- .../src/command_buffer/mod.rs | 19 ++- crates/metal2vulkan-runtime/src/queue.rs | 32 +++- .../src/tests/attached_read_faces.rs | 137 ++++++++++++++++++ .../metal2vulkan-runtime/src/tests/queue.rs | 15 +- .../metal2vulkan-runtime/src/texture/faces.rs | 3 +- crates/metal2vulkan/src/cache/mod.rs | 2 +- 7 files changed, 201 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs index 7dd5ee5db..f4c58ab2e 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs @@ -1646,10 +1646,14 @@ pub(crate) mod trace { /// A queue's waiter, once it published a value its queue's timeline completed and before /// the batches at or below it leave the `VkQueue`'s flight (`QueueSlot::completed`). Leave, + /// A committing thread, once its command buffer's last command buffer ended (with its + /// batch's end where it may) and before it hands the batches to the scheduler + /// (`CommandBuffer::commit`). + Committing, } /// Of each [`Point`]: whether a test holds threads there, and whether one reached it since. - static POINTS: Mutex<[(bool, bool); 3]> = Mutex::new([(false, false); 3]); + static POINTS: Mutex<[(bool, bool); 4]> = Mutex::new([(false, false); 4]); static POINTS_CHANGED: std::sync::Condvar = std::sync::Condvar::new(); /// Hold every thread that reaches `point` from now on, until [`release`]. diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index f6cb4b065..c9f5cac80 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -1993,8 +1993,11 @@ impl State { /// decode its submission may record after it, `texture/decoded_twin.rs`), and name no texture /// over the caller's image that moves between layouts (whose move back its submission plans /// before the end), while no texture's turn may leave its submission a copy after its work - /// (`texture/faces.rs`). An event's kept signal that waits for another queue's still goes - /// after that wait, in an end of its own at the submission (`submit.rs`). + /// (`texture/faces.rs`); none begins before the batch's submission, a turn waiting for the + /// device's only uncommitted command buffer, which counts this one until the scheduler holds + /// its batches (`queue.rs`'s `Uncommitted`), and for a scheduler that holds no batch. An + /// event's kept signal that waits for another queue's still + /// goes after that wait, in an end of its own at the submission (`submit.rs`). fn may_fold(&self, device: &Device, index: usize) -> bool { let parts = &self.recorded[self.batch_range(index)]; !device.shared().face_swaps.any() @@ -4207,11 +4210,23 @@ impl CommandBuffer { // Its reads and writes of new textures' patterns, in commit order: announced in the // committing call, before the scheduler sees it (`fresh_unwritten.rs`). FreshUnwritten::commit(device, &self.0.fresh, run != CommitRun::Refused); + // A device test holds the commit here, its last command buffer ended and its batches not + // yet the scheduler's (`completion::trace::Point::Committing`). + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + completion::trace::at(completion::trace::Point::Committing); // The scheduler's lock is taken before a command buffer's, never under it. device .shared() .scheduler .commit(&self.0, self.0.queue.id(), waits, hand); + // Only now, its batches the scheduler's, it leaves the device's uncommitted command + // buffers (`queue.rs`'s `Uncommitted`): until then no texture's face turns and no storage + // form switches beside it, whose commands, recorded for good and their batches' ends + // folded where they may, would meet the turn's copies or the switch as their batches are + // submitted. + if let Some(slot) = &mut self.state().slot { + slot.handed(); + } // Its passes that took a rasterization-rate map since reset, encoded again in this call // as a reset's are; its first submission waits for them (marked above). Those it cannot // encode again are announced. diff --git a/crates/metal2vulkan-runtime/src/queue.rs b/crates/metal2vulkan-runtime/src/queue.rs index bfe46c9bc..ca499dcb4 100644 --- a/crates/metal2vulkan-runtime/src/queue.rs +++ b/crates/metal2vulkan-runtime/src/queue.rs @@ -287,10 +287,14 @@ impl Drop for HeldContext { } } -/// The command buffers of a device's queues made and not committed (nor dropped): its slots open, -/// over all its queues. A fetched render target takes its storage form only as the only one -/// commits (`texture/storage_form.rs`); a new one counts itself as it takes its slot, before it -/// reads that module's gate. +/// The command buffers of a device's queues made and not yet handed to the scheduler (nor +/// dropped), over all its queues: those not committed, and those committing, until their commit +/// handed their batches to the scheduler ([`Slot::handed`]). A fetched render target takes its +/// storage form only as the only one commits (`texture/storage_form.rs`), and a texture's face +/// turns only in the only one (`texture/faces.rs`), where the scheduler holds no batch: so no +/// command buffer whose commands may name what the switch or the turn changes is between its +/// commit and the scheduler then. A new one counts itself as it takes its slot, before it reads +/// those modules' gates. #[derive(Default)] pub(crate) struct Uncommitted(AtomicUsize); @@ -313,6 +317,9 @@ pub(crate) struct Slot { slots: Arc, /// The command buffer holding it is not committed. open: bool, + /// The command buffer holding it counts among the device's [`Uncommitted`]: its commit has not + /// handed its batches to the scheduler. + counted: bool, } impl Slots { @@ -358,16 +365,29 @@ impl Slots { Some(Slot { slots: Arc::clone(self), open: true, + counted: true, }) } } impl Slot { - /// The command buffer holding the slot was committed: only its completion gives it back now. + /// The command buffer holding the slot was committed: only its completion gives it back now, + /// and the program can no longer drop it to give it back (`own.rs`'s `slot_wait`). It counts + /// among the device's [`Uncommitted`] until its batches are handed to the scheduler + /// ([`handed`](Self::handed)). pub(crate) fn committed(&mut self) { if self.open { self.open = false; self.slots.lock().open -= 1; + } + } + + /// The commit of the command buffer holding the slot handed its batches to the scheduler, + /// which holds them until each is submitted: it no longer counts among the device's + /// [`Uncommitted`]. + pub(crate) fn handed(&mut self) { + if self.counted { + self.counted = false; self.slots.uncommitted.0.fetch_sub(1, Ordering::SeqCst); } } @@ -380,6 +400,8 @@ impl Drop for Slot { held.all -= 1; if self.open { held.open -= 1; + } + if self.counted { self.slots.uncommitted.0.fetch_sub(1, Ordering::SeqCst); } // The last committed here completed: the next place's context runs on. diff --git a/crates/metal2vulkan-runtime/src/tests/attached_read_faces.rs b/crates/metal2vulkan-runtime/src/tests/attached_read_faces.rs index 9d8e750ca..c3727fe04 100644 --- a/crates/metal2vulkan-runtime/src/tests/attached_read_faces.rs +++ b/crates/metal2vulkan-runtime/src/tests/attached_read_faces.rs @@ -32,6 +32,15 @@ //! passes old. The pass turns nothing (`Unturned::Busy`) and copies in, and the command reads the //! texels from before the pass, Metal's answer. //! +//! A fourth and a fifth test encode a pass beside a command buffer that commits, held as it +//! commits once its last command buffer ended and before the scheduler holds its batches: the +//! committing command buffer counts among the device's uncommitted ones until then +//! (`queue.rs`'s `Uncommitted`), so the pass turns nothing and copies in. A turn there would +//! leave a batch submitted after it that a face fix copies around, though its end may already lie +//! in its last command buffer (the fourth: the submission found that end where the batch does not +//! end), and whose rendering the fix's copy back would overwrite with the face from before (the +//! fifth, whose end is a command buffer of its own while another texture's turn is pending). +//! //! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. The functions are `tob_rect`, //! `tob_solid` and `tob_read` of the owned fixture `validation/fixtures/public/engine/tob_renders.ll`, @@ -40,6 +49,7 @@ use super::blit_holds::{device, over_host, Host, Scratch, WAIT}; use super::encoder_holds::{bytes_of, library}; +use crate::command_buffer::completion::trace::{self, Point}; use crate::{ BlitOption, Buffer, ClearColor, CommandBuffer, CommandQueue, Device, EventRef, IndirectCommandBufferDescriptor, LoadAction, Origin, PipelineOptions, RenderPassDescriptor, @@ -690,3 +700,130 @@ fn an_execution_before_a_clearing_pass_reads_the_texels_from_before_it() { let differ = (0..got.len()).filter(|&at| got[at] != want[at]).count(); assert_eq!(differ, 0, "the pass: {differ} bytes differ"); } + +/// The points a lane of the commit tests holds, let go as this drops, a failing test's unwinding +/// too. +struct HeldCommit; + +impl HeldCommit { + fn new() -> HeldCommit { + trace::hold_at(Point::Committing); + HeldCommit + } +} + +impl Drop for HeldCommit { + fn drop(&mut self) { + trace::release(Point::Committing); + } +} + +/// A lane of the commit tests (the module docs): a command buffer `committing` that draws into +/// the texture, reads it beside an open one, `turning`, and draws over the read, held as it commits +/// (`Point::Committing`: its last command buffer ended, its batches not yet the scheduler's); +/// meanwhile `turning`'s pass, which clears the texture and reads it, opens. Where `folds`, the +/// committing command buffer's one batch ends in its last command buffer; else a turn of another +/// texture whose command buffer went uncommitted is pending as it commits, so its end is a command +/// buffer of its own. Every step's bytes are checked: the committing command buffer's pass +/// leaves 0x30 outside [`RECT`] and 0x70 inside it, which the turning pass reads. +fn beside_a_commit(lane: &str, folds: bool) { + let scratch = Scratch::new(lane); + let device = device(&scratch.0); + let tools = Tools::new(&device); + // The texture whose turn is pending, of the lane whose end does not fold. + let other = (!folds).then(|| texture(&device)); + let texture = texture(&device); + let check = |want: Vec, what: &str| { + let (got, _) = tools.bytes(&texture); + let differ = (0..got.len()).filter(|&at| got[at] != want[at]).count(); + assert_eq!(differ, 0, "{lane}: {what}: {differ} bytes differ"); + }; + // The faces, made by the first read; then 0x20 outside the rectangle and 0x50 inside it. + tools.pass(&texture, Some(0x10), Draw::Nothing); + tools.pass(&texture, None, Draw::Solid(0x40)); + let moved = tools.pass(&texture, Some(0x20), Draw::Read); + assert_eq!(moved.made, 1, "{lane}: the first read makes the faces"); + check(image(0x20, 0x40), "the first read"); + tools.pass(&texture, None, Draw::Solid(0x50)); + check(image(0x20, 0x50), "the solid draw"); + // A turn of the other texture, whose command buffer goes uncommitted: pending until a batch + // names that texture, which none does. + if let Some(other) = &other { + tools.pass(other, Some(0x10), Draw::Nothing); + let moved = tools.pass(other, Some(0x20), Draw::Read); + assert_eq!(moved.made, 1, "{lane}: the other texture's faces"); + let before = device.work_counts(); + let dropped = tools.queue.command_buffer().unwrap(); + tools.encode(&dropped, other, Some(0x30), Draw::Read); + drop(dropped); + let moved = Moved::of(&before, &device.work_counts()); + assert_eq!( + moved.swaps, 1, + "{lane}: the other texture's uncommitted turn" + ); + } + let before = device.work_counts(); + let turning = tools.queue.command_buffer().unwrap(); + let committing = tools.queue.command_buffer().unwrap(); + // A draw before the read, so the read's copy in is recorded in its encoder, not left to the + // batch's submission (`shadow_standing.rs`, **The copy in**), which would hold the batch's end + // apart and the heap sets' ticket the turn waits for. + tools.encode(&committing, &texture, None, Draw::Solid(0x55)); + tools.encode(&committing, &texture, Some(0x30), Draw::Read); + tools.encode(&committing, &texture, None, Draw::Solid(0x70)); + let encoded = device.work_counts(); + let held = HeldCommit::new(); + let commit = { + let committing = committing.clone(); + std::thread::spawn(move || committing.commit()) + }; + assert!( + trace::reached(Point::Committing, WAIT), + "{lane}: the commit reached its hold" + ); + tools.encode(&turning, &texture, Some(0x60), Draw::Read); + drop(held); + commit.join().unwrap().unwrap(); + committing.wait_until_completed_for(WAIT).unwrap(); + assert_eq!( + committing.error(), + None, + "{lane}: the committing command buffer" + ); + let ended = device.work_counts(); + let (folded, apart) = ( + ended.finales_folded - encoded.finales_folded, + ended.finales_apart - encoded.finales_apart, + ); + eprintln!("ATTACHED-READ-FACES: {lane}: its end folded {folded}, apart {apart}"); + let want = match folds { + true => (1, 0), + false => (0, 1), + }; + assert_eq!((folded, apart), want, "{lane}: the committing batch's end"); + check(image(0x30, 0x70), "the committing command buffer"); + Tools::run(&turning); + check(image(0x60, 0x70), "the pass beside the commit"); + let moved = Moved::of(&before, &device.work_counts()); + eprintln!("ATTACHED-READ-FACES: {lane}: {moved:?}"); + assert_eq!( + (moved.swaps, moved.busy, moved.fixes), + (0, 2, 0), + "{lane}: neither pass turns, beside an open command buffer or a commit" + ); + drop(other); +} + +/// The fourth test of the module docs: the committing command buffer's batch ends in its last +/// command buffer. +#[test] +fn a_pass_beside_a_commit_turns_nothing_and_the_commit_s_end_stays_folded() { + beside_a_commit("attached-read-faces-commit-folded", true); +} + +/// The fifth test of the module docs: the committing command buffer's batch ends in a command +/// buffer of its own, so its submission would take a turn's copies. +#[test] +fn a_pass_beside_a_commit_turns_nothing_and_the_commit_s_pixels_stay() { + beside_a_commit("attached-read-faces-commit-apart", false); +} diff --git a/crates/metal2vulkan-runtime/src/tests/queue.rs b/crates/metal2vulkan-runtime/src/tests/queue.rs index d5419541d..644858564 100644 --- a/crates/metal2vulkan-runtime/src/tests/queue.rs +++ b/crates/metal2vulkan-runtime/src/tests/queue.rs @@ -1,5 +1,6 @@ //! A command queue's slots (`queue.rs`): one per uncompleted command buffer, at most the queue's -//! `maxCommandBufferCount`, each counted open until its command buffer is committed. +//! `maxCommandBufferCount`, each counted open until its command buffer is committed, and among the +//! device's uncommitted command buffers until its commit handed its batches to the scheduler. use crate::queue::{Held, Slots, Uncommitted}; use std::sync::Arc; @@ -19,10 +20,14 @@ fn a_queue_hands_out_its_slots_up_to_its_count_and_takes_each_back_when_dropped( .take(&mut others.lock()) .expect("another queue's slot"); assert_eq!(uncommitted.count(), 3); - // Committed, a slot is no longer open, and only its drop gives it back. + // Committed, a slot is no longer open, and only its drop gives it back; it counts among the + // uncommitted until its batches are handed to the scheduler. second.committed(); second.committed(); assert_eq!(*slots.lock(), Held { all: 2, open: 1 }); + assert_eq!(uncommitted.count(), 3); + second.handed(); + second.handed(); assert_eq!(uncommitted.count(), 2); drop(second); assert_eq!(uncommitted.count(), 2); @@ -31,7 +36,11 @@ fn a_queue_hands_out_its_slots_up_to_its_count_and_takes_each_back_when_dropped( drop(first); assert_eq!(*slots.lock(), Held { all: 0, open: 0 }); assert_eq!(uncommitted.count(), 1); - drop(other); + // One committed and dropped before its batches were handed counts no more once dropped. + let mut committed = other; + committed.committed(); + assert_eq!(uncommitted.count(), 1); + drop(committed); assert_eq!(uncommitted.count(), 0); let again: Vec<_> = { let mut held = slots.lock(); diff --git a/crates/metal2vulkan-runtime/src/texture/faces.rs b/crates/metal2vulkan-runtime/src/texture/faces.rs index 8975a5427..e1bfca263 100644 --- a/crates/metal2vulkan-runtime/src/texture/faces.rs +++ b/crates/metal2vulkan-runtime/src/texture/faces.rs @@ -33,7 +33,8 @@ //! follows the pass either. Each copy in of a color texture's attached reads that no turn took //! off counts by why (`WorkCounts::attached_read_unswapped_*`, [`Unturned`]). //! - **When.** Only where nothing else can name the face presented until then: the opening command -//! buffer is the device's only uncommitted one, the scheduler holds no batch, no command buffer +//! buffer is the device's only uncommitted one (a committing one counts until the scheduler holds +//! its batches, `queue.rs`'s `Uncommitted`), the scheduler holds no batch, no command buffer //! that may read the heap sets is incomplete and this one bound none (as a storage form's switch, //! `texture/storage_form.rs`), nor holds an execution of indirect commands, whose commands bind //! them as the commit or a host step records them, after the turn, though they run before the diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index d50029bb7..47d35e47e 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 518; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2242; +pub const RUNTIME_VERSION: u32 = 2243; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles