From cb033efbcfc2379f81b3f8eef8ae6d7b5d9fda22 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 16 Nov 2026 13:18:07 -0800 Subject: [PATCH] RUNTIME 2476: A bind state sets aside 15 pipeline states, so a carry holds 16 kept draws SET_ASIDE goes from 7 to 15. It bounds both the render bind state's set-aside array (LiveBind) and the compute one's (LiveDispatch), and it bounds a carry to SET_ASIDE + 1 kept draws. In guest walks, WindowServer's desktop steps turn over 25-60 pipeline states a frame. Launchpad's command buffers draw through a median of 12 states each (p90 46). Passes of more than 8 states are 1.2-2.0% of passes but 13-22% of draws in Finder and Launchpad. At 8, a pass of state k after 8 or more others finds no kept draw carried and takes every step. Docs that named 7/8 now name 15/16: live_bind.rs, render/carried.rs, render/draw_again.rs, compute/live.rs. Tests that named the bound: carried_draws' passes-of-states test now runs 16 states and expects 15 carried aside; compute_set_aside's doc says sixteen states and 32 KiB. draw_costs gets 20 plain states, so `--states 16` runs and the default pass1/pass17 shape still carries no kept draw. With 15 set aside, all of the old 12 would carry. No FAST-PATHS row names this bound. Verified (release build, msi lavapipe, encoding thread pinned to the P-cores; jobs m3-max-37724 and m3-max-37730, base and after built in one workspace). Instruction counts are the thread's user-space instructions. draw_costs pass1/pass17, figures per pass (pass1 less the draw that pass17 gives), base -> after: states 2: instructions 75978 -> 76208 (+0.3%), cycles -2.2% states 4: instructions 76278 -> 76478 (+0.3%), cycles -1.0% states 12: instructions 90656 -> 77339 (-14.7%), cycles -17.4% states 16: instructions 89686 -> 77547 (-13.5%), cycles -17.1% pass1's draws_rebound_carried at states 12 went 568 -> 9204 of 9216, and at states 16 0 -> 9200. Counts at states 2 and 4 are equal in both builds. dispatch_chain's guest compute shape (5 encoders of 1 dispatch per command buffer, 3 states, --binds tex), 3 interleaved pairs: instructions per dispatch went 45787 -> 45738 (median, -0.1%), thread CPU 5221 -> 5111 ns. Memory bound. size_of went up, render LiveBind 20808 -> 41224 bytes and compute LiveDispatch 4688 -> 8784. Each is boxed, so encoders move a pointer. Holders, each capped by the runtime: - render: 4 device spares (SPARE_LIVE_BINDS); at most one per uncompleted command buffer (its open render encoder's, or the carry it holds for its next render encoder; an opening takes the carry); at most 16 per queue held past commits (queue::CARRIES). - compute: 4 device spares (SPARE_LIVE_DISPATCHES); at most one per uncompleted command buffer (its open compute encoder's, or the one parked in it). A queue holds at most its max command buffer count of uncompleted command buffers (64 for newCommandQueue). Per queue at that default the inline worst case is: render (64 + 16) x 20808 = 1.59 MiB -> 80 x 41224 = 3.15 MiB compute 64 x 4688 = 293 KiB -> 64 x 8784 = 549 KiB and per device, on top of that, the spares: render 81 -> 161 KiB, compute 18 -> 34 KiB. Beyond size_of, each slot also has its boxed BindState and the lists that use grows; those were not measured. compute_set_aside caps a compute slot plus its BindState at 2 KiB. Unbounded by the runtime, as in Metal: how many queues a program makes and each queue's max command buffer count. Checks at the lane tip d074d4ea47 (msi): clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings plain and m2v_cpu_icd 0 (m3-max-37735), m2v_gpu 0 and rustdoc 0 (m3-max-37736); lavapipe carried_draws, compute_set_aside and draw_again 48/48 (m3-max-37735). The state counts per pass and command buffer are the consumer's Iris walks (passstates, m3-max-37560/37561). --- .../examples/draw_costs.rs | 14 ++++++-- .../metal2vulkan-runtime/src/compute/live.rs | 2 +- crates/metal2vulkan-runtime/src/live_bind.rs | 4 +-- .../src/render/carried.rs | 8 ++--- .../src/render/draw_again.rs | 2 +- .../src/tests/carried_draws.rs | 35 +++++++++++-------- .../src/tests/compute_set_aside.rs | 4 +-- crates/metal2vulkan/src/cache/mod.rs | 2 +- 8 files changed, 42 insertions(+), 29 deletions(-) diff --git a/crates/metal2vulkan-runtime/examples/draw_costs.rs b/crates/metal2vulkan-runtime/examples/draw_costs.rs index 67ef92c46..38a63fcef 100644 --- a/crates/metal2vulkan-runtime/examples/draw_costs.rs +++ b/crates/metal2vulkan-runtime/examples/draw_costs.rs @@ -48,8 +48,8 @@ //! view of icons: 8 command buffers of 16 passes of 1 draw, and of 4 passes of 17 draws. Each //! pass loads and stores one of 8 surfaces of 256x256 over buffers of their own as the //! compositor's are (`--pages` too), sets its viewport and draws the compositor's plain quads, -//! each after the pipeline state (once a pass: the next of 12 plain ones of their own blend -//! factors, so the state a pass draws through is none of the eight the last passes drew +//! each after the pipeline state (once a pass: the next of 20 plain ones of their own blend +//! factors, so the state a pass draws through is none of the sixteen the last passes drew //! through, and its first draw takes every step, no kept draw of its state carried in; with //! `--states N`, the first N of them), its //! vertex buffers 0 and 1 and fragment buffer 0, fragment texture 0 (the next of its sampled @@ -945,7 +945,7 @@ struct FrameDraw { /// The plain pipeline states of the small passes ([`Stream::Passes`]), by their color's source /// and destination blend factors: more than the eight a render encoder carries kept draws of (its /// last state's and the seven its bind state sets aside). -const SMALL_BLENDS: [(BlendFactor, BlendFactor); 12] = [ +const SMALL_BLENDS: [(BlendFactor, BlendFactor); 20] = [ (BlendFactor::One, BlendFactor::OneMinusSourceAlpha), (BlendFactor::SourceAlpha, BlendFactor::OneMinusSourceAlpha), (BlendFactor::One, BlendFactor::Zero), @@ -958,6 +958,14 @@ const SMALL_BLENDS: [(BlendFactor, BlendFactor); 12] = [ (BlendFactor::DestinationAlpha, BlendFactor::One), (BlendFactor::OneMinusDestinationAlpha, BlendFactor::One), (BlendFactor::SourceAlphaSaturated, BlendFactor::One), + (BlendFactor::SourceAlpha, BlendFactor::One), + (BlendFactor::One, BlendFactor::SourceAlpha), + (BlendFactor::Zero, BlendFactor::One), + (BlendFactor::Zero, BlendFactor::OneMinusSourceAlpha), + (BlendFactor::OneMinusSourceColor, BlendFactor::One), + (BlendFactor::DestinationColor, BlendFactor::One), + (BlendFactor::SourceAlpha, BlendFactor::Zero), + (BlendFactor::One, BlendFactor::SourceColor), ]; /// The compositor shape's objects (the module docs): its passes with their viewports, their diff --git a/crates/metal2vulkan-runtime/src/compute/live.rs b/crates/metal2vulkan-runtime/src/compute/live.rs index 247e2f4c5..c677240bb 100644 --- a/crates/metal2vulkan-runtime/src/compute/live.rs +++ b/crates/metal2vulkan-runtime/src/compute/live.rs @@ -32,7 +32,7 @@ //! (`ComputePipelineState::planned_for`), so a dispatch whose offsets alone moved compares no //! shape at all, however many resources the encoder made resident. //! - **One per pipeline state lately dispatched.** Beside the state its last dispatch brought in -//! place, the encoder sets aside the states of up to `live_bind::SET_ASIDE` (7) pipeline states +//! place, the encoder sets aside the states of up to `live_bind::SET_ASIDE` (15) pipeline states //! it dispatched before that one ([`LiveDispatch::select`]), each under the pipeline state whose //! dispatch left it (held weakly), with its shape, what its table was read from, what its last //! dispatch settled (`DispatchMemo`) and the commands it kept (`Instantiated`); a dispatch diff --git a/crates/metal2vulkan-runtime/src/live_bind.rs b/crates/metal2vulkan-runtime/src/live_bind.rs index 50973cc8e..31722fea4 100644 --- a/crates/metal2vulkan-runtime/src/live_bind.rs +++ b/crates/metal2vulkan-runtime/src/live_bind.rs @@ -128,10 +128,10 @@ const SPARE_LIVE_BINDS: usize = 4; /// the last draw's ([`LiveBind::select`]): a draw of one of more pipeline states in turn than /// this starts from one another state's draw left, read through its interface where one is. It /// bounds a carry too (`render/carried.rs`): an ended encoder's kept draw and those set aside -/// beside it, eight at most, each holding its settles' variants, its `VkPipeline` and settings +/// beside it, sixteen at most, each holding its settles' variants, its `VkPipeline` and settings /// alive, in the one carry a command buffer holds for its next render encoder or a queue holds /// past a commit. A compute encoder's bind state sets aside as many (`compute/live.rs`). -pub(crate) const SET_ASIDE: usize = 7; +pub(crate) const SET_ASIDE: usize = 15; /// A bind state a command may start from, as [`start_from`] weighs it: the one in place or one /// set aside, of a render encoder's draws ([`LiveBind::select`]) or a compute encoder's diff --git a/crates/metal2vulkan-runtime/src/render/carried.rs b/crates/metal2vulkan-runtime/src/render/carried.rs index d149a50f2..5e75e1c99 100644 --- a/crates/metal2vulkan-runtime/src/render/carried.rs +++ b/crates/metal2vulkan-runtime/src/render/carried.rs @@ -22,10 +22,10 @@ //! (`Kept::carries`) through a state that holds its settle, and read what changes with no call //! as it did (`Kept::carries_with`), so the next encoder's check of it holds for each //! (`LiveBind::carry_aside`, `WorkCounts::kept_draws_carried_aside`); the rest it lets go. A -//! bind state sets aside at most `live_bind::SET_ASIDE` (7) states, so a carry holds at most -//! eight kept draws: an encoder whose draws switch between up to eight states finds each one's -//! first draw carried (`WorkCounts::draws_rebound_carried`). A carried state the next encoder -//! never draws is work its opening did for nothing: the carries an opening takes +//! bind state sets aside at most `live_bind::SET_ASIDE` (15) states, so a carry holds at most +//! sixteen kept draws: an encoder whose draws switch between up to sixteen states finds each +//! one's first draw carried (`WorkCounts::draws_rebound_carried`). A carried state the next +//! encoder never draws is work its opening did for nothing: the carries an opening takes //! (`kept_draws_carried` and `kept_draws_carried_aside`) less the carried states drawn //! (`draws_rebound_carried` and `draws_carry_stopped`). The shadows' //! copies it listed and the heap parts it kept go as they go at any end diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index bfc3ff8ed..820561925 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -185,7 +185,7 @@ //! record holds. Its tables' stamps are its own still: its encoder's tables went on. //! - **Carried to the next encoder.** An encoder that ends with a kept draw of its pass's current //! instance, the first or one after a split (re-homed past it, above, among them), carries it, -//! with its bind state and settles and the kept draws set aside with the states of up to seven +//! with its bind state and settles and the kept draws set aside with the states of up to fifteen //! other pipeline states (`live_bind::SET_ASIDE`), into its command buffer's next render encoder //! where nothing comes between, or past its commit into a render encoder of a later command //! buffer of its queue, where that encoder's pass is its pass's like (`render/carried.rs`): each diff --git a/crates/metal2vulkan-runtime/src/tests/carried_draws.rs b/crates/metal2vulkan-runtime/src/tests/carried_draws.rs index cd19a4fa6..91c3cc09a 100644 --- a/crates/metal2vulkan-runtime/src/tests/carried_draws.rs +++ b/crates/metal2vulkan-runtime/src/tests/carried_draws.rs @@ -397,16 +397,16 @@ fn an_encoder_that_carries_nothing_counts_why() { /// rebound against its own (`draws_rebound_carried`), the later ones after a draw of another /// state began the pass's instance, with no draw unlike its kept draw or stopped, and the pixels /// of the same passes in command buffers of queues of their own; in command buffers of their own -/// of one queue, the same, each state's kept draw carried across. Passes of eight states, the most -/// a bind state holds (its last draw's and seven set aside, `live_bind::SET_ASIDE`), carry every -/// one: the next pass's first draw of each is rebound, none takes every step. +/// of one queue, the same, each state's kept draw carried across. Passes of sixteen states, the +/// most a bind state holds (its last draw's and fifteen set aside, `live_bind::SET_ASIDE`), carry +/// every one: the next pass's first draw of each is rebound, none takes every step. #[test] fn passes_of_several_states_rebind_each_state_s_carried_draw() { let scratch = Scratch::new("carried-states"); let heard = Heard::default(); let device = plan_device(&scratch.0, &[], &heard); let queue = device.new_command_queue().unwrap(); - let states: [RenderPipelineState; 8] = std::array::from_fn(|_| adding_state(&device)); + let states: [RenderPipelineState; 16] = std::array::from_fn(|_| adding_state(&device)); let color = colors(&device, &[[1.0 / 64.0; 4]]); let draw = |encoder: &mut RenderCommandEncoder, order: &[usize]| { for &at in order { @@ -415,8 +415,9 @@ fn passes_of_several_states_rebind_each_state_s_carried_draw() { }; let first = |encoder: &mut RenderCommandEncoder| draw(encoder, &[0, 1, 2]); let turned = |encoder: &mut RenderCommandEncoder| draw(encoder, &[1, 2, 0]); - let eight = |encoder: &mut RenderCommandEncoder| draw(encoder, &[0, 1, 2, 3, 4, 5, 6, 7]); - warm(&device, eight); + let all: [usize; 16] = std::array::from_fn(|at| at); + let sixteen = |encoder: &mut RenderCommandEncoder| draw(encoder, &all); + warm(&device, sixteen); let aside = |frame: &Frame| frame.counted(|c| c.kept_draws_carried_aside); let missed = |c: &WorkCounts| c.draws_unlike_kept + c.draws_rebind_stopped; // Two carried passes of three states: each state's first draw rebound. @@ -441,23 +442,27 @@ fn passes_of_several_states_rebind_each_state_s_carried_draw() { assert_eq!(frame.words, alone.words, "three states, {case}: the pixels"); } assert_eq!(across.across(), 2, "three states: carried across"); - // A carried pass of eight states: all eight rebound, none taking every step but the first + // A carried pass of sixteen states: all sixteen rebound, none taking every step but the first // pass's. - let passes: [Pass<'_>; 2] = [&eight, &eight]; + let passes: [Pass<'_>; 2] = [&sixteen, &sixteen]; let together = frame(&queue, &passes, Apart::Together); let alone = frame(&queue, &passes, Apart::Queues); - let mut expected = rebound(8); + let mut expected = rebound(16); expected[0] = 1; - assert_eq!(together.carries(), expected, "eight states: the counts"); - assert_eq!(aside(&together), 7, "eight states: carried aside"); + assert_eq!(together.carries(), expected, "sixteen states: the counts"); + assert_eq!(aside(&together), 15, "sixteen states: carried aside"); let none_kept = |frame: &Frame| frame.counted(|c| c.draws_none_kept); assert_eq!( none_kept(&together), - 8, - "eight states: the first pass's draws alone" + 16, + "sixteen states: the first pass's draws alone" ); - assert_eq!(together.counted(missed), 0, "eight states: no draw missed"); - assert_eq!(together.words, alone.words, "eight states: the pixels"); + assert_eq!( + together.counted(missed), + 0, + "sixteen states: no draw missed" + ); + assert_eq!(together.words, alone.words, "sixteen states: the pixels"); } /// The next encoder binds the same state and buffers but for one slot's value: the same buffer at diff --git a/crates/metal2vulkan-runtime/src/tests/compute_set_aside.rs b/crates/metal2vulkan-runtime/src/tests/compute_set_aside.rs index 3326208e7..dd7c7ee0c 100644 --- a/crates/metal2vulkan-runtime/src/tests/compute_set_aside.rs +++ b/crates/metal2vulkan-runtime/src/tests/compute_set_aside.rs @@ -275,8 +275,8 @@ fn a_state_whose_bytes_change_keeps_its_commands() { } } -/// A state set aside takes at most 2 KiB beside its bind state's slots and lists: eight bind -/// states (the one in place and seven set aside) at most 16 KiB of an encoder's box. +/// A state set aside takes at most 2 KiB beside its bind state's slots and lists: sixteen bind +/// states (the one in place and fifteen set aside) at most 32 KiB of an encoder's box. #[test] fn a_state_set_aside_is_bounded() { let bytes = crate::compute::SET_ASIDE_BYTES; diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 3b58a034b..0ffd8f516 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 = 524; /// 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 = 2475; +pub const RUNTIME_VERSION: u32 = 2476; /// 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