diff --git a/crates/metal2vulkan-runtime/src/live_bind.rs b/crates/metal2vulkan-runtime/src/live_bind.rs index 4e6645fe7..4ed78f99c 100644 --- a/crates/metal2vulkan-runtime/src/live_bind.rs +++ b/crates/metal2vulkan-runtime/src/live_bind.rs @@ -36,9 +36,13 @@ //! makes none; else it is made again, whole. //! - **One per pipeline state lately drawn.** Beside the state its last draw brought in place, the //! encoder sets aside the states of up to [`SET_ASIDE`] pipeline states it drew before that one -//! ([`LiveBind::select`]), and a draw starts from its pipeline state's, where one is set aside: -//! draws that switch between pipeline states each find their own state's shape kept, and their -//! stage tables read through their own interface, where no slot of theirs changed its shape. +//! ([`LiveBind::select`]), each under the pipeline state whose draw left it, and a draw starts +//! from its pipeline state's, where one is set aside, though another state's draws share its +//! interface (states of one function pair, blending apart): draws that switch between pipeline +//! states each find their own state's shape kept, their stage tables read through their own +//! interface, where no slot of theirs changed its shape, and their own state's last draw kept +//! (`render/draw_again.rs`). Where none is, it starts from one no live state's draw left, else +//! from the latest read through its interface, else from the one set aside longest. //! Exact for the reason a draw's is: whichever state a draw starts from, it brings every field //! in place, and the starting state decides only whether the shape is kept. //! - **The settle kept with the state.** Beside each state, the variant and plan its last recorded @@ -99,7 +103,7 @@ use super::argument_table::Stamp; use super::binding::{CommandUses, PushWrite, SetWrite, Write}; use super::device::lock; use super::noted_apart::NotedApart; -use super::pipeline::{BufferForms, GraphicsState, ProgramName, Settled}; +use super::pipeline::{BufferForms, GraphicsState, ProgramName, Settled, WeakRenderPipelineState}; use super::render::{DrawKept, KeptHeap, KeptStorage}; use super::sampler::OwnedSampler; use super::shadow::ResidentShadows; @@ -111,7 +115,7 @@ const SPARE_LIVE_BINDS: usize = 4; /// The most states a [`LiveBind`] sets aside, of the pipeline states its draws drew through before /// the last draw's ([`LiveBind::select`]): a draw of one of more pipeline states in turn than -/// this starts from the state set aside longest. +/// this starts from one another state's draw left, read through its interface where one is. const SET_ASIDE: usize = 7; /// The most shapes a state keeps of those it took lately ([`RecentShapes`]): one per primitive @@ -238,6 +242,9 @@ pub(super) struct LiveBind { pub(super) listed_at: Option, /// What the state's stage tables were read from ([`ReadFrom`]). pub(super) read_from: ReadFrom, + /// The pipeline state whose draw last brought the state in place ([`select`](Self::select)), + /// named without holding it; `None` before any did. + drawn: Option, /// The states the encoder's draws of other pipeline states brought in place, each as its /// pipeline state's last draw left it ([`select`](Self::select)). set_aside: [SetAside; SET_ASIDE], @@ -338,6 +345,7 @@ struct SetAside { recent: RecentShapes, listed_at: Option, read_from: ReadFrom, + drawn: Option, settled: Option, rebinds: SlotRebinds, /// Its last draw's storage and kept draw, as [`LiveBind`] holds them for its last draw's @@ -553,26 +561,52 @@ impl LiveBind { self.forget_settles(); } - /// Start a draw through `interface` from the state that pipeline state's last draw left, where - /// one is set aside: it and the state in place change places. Where none is, the state in - /// place changes places with the one set aside longest, which the draw starts from. Both are - /// states as draws left them (or empty, as a new encoder's): exact whichever a draw starts - /// from, as it brings every field of the state in place (the module docs), so this chooses - /// only whether the shape may be kept, and which reads the draw skips. - pub(super) fn select(&mut self, interface: &PipelineInterface) { - if self.read_from.through(interface) { + /// Start a draw of `pipeline` from the state that pipeline state's last draw left, where one + /// is set aside: it and the state in place change places. Where none is, the draw starts from + /// a state no live pipeline state's draw left, the one in place first; else from the one in + /// place where its tables were read through `pipeline`'s interface (so the draw reads only + /// what changed since), else from the one last set aside of those so read, else from the one + /// set aside longest, which it takes as `pipeline`'s. Each is a state as draws left it (or + /// empty, as a new encoder's): exact whichever a draw starts from, as it brings every field of + /// the state in place (the module docs), so this chooses only whether the shape and the draw + /// may be kept, and which reads the draw skips. + pub(super) fn select(&mut self, pipeline: &GraphicsState) { + if self.draws(pipeline) { return; } - let set_aside = self.set_aside.iter(); - let at = match set_aside - .clone() - .position(|aside| aside.read_from.through(interface)) - { - Some(at) => at, - None => (set_aside.enumerate()) - .min_by_key(|(_, aside)| aside.at) - .map_or(0, |(at, _)| at), + let interface = &pipeline.base().interface; + let lives = |drawn: &Option| { + (drawn.as_ref()).is_some_and(WeakRenderPipelineState::lives) }; + let set_aside = self.set_aside.iter().enumerate(); + let own = (set_aside.clone()) + .find(|(_, aside)| (aside.drawn.as_ref()).is_some_and(|drawn| drawn.is(pipeline))); + // `None` starts from the state in place. + let at = if let Some((at, _)) = own { + Some(at) + } else if !lives(&self.drawn) { + None + } else if let Some((at, _)) = set_aside.clone().find(|(_, aside)| !lives(&aside.drawn)) { + Some(at) + } else if self.read_from.through(interface) { + None + } else { + let through = (set_aside.clone()) + .filter(|(_, aside)| aside.read_from.through(interface)) + .max_by_key(|(_, aside)| aside.at); + let longest = || set_aside.clone().min_by_key(|(_, aside)| aside.at); + through.or_else(longest).map(|(at, _)| at) + }; + if let Some(at) = at { + self.swap_aside(at); + } + if !self.draws(pipeline) { + self.drawn = Some(pipeline.downgrade()); + } + } + + /// Change the state in place for the one set aside at `at`. + fn swap_aside(&mut self, at: usize) { self.asides += 1; let aside = &mut self.set_aside[at]; std::mem::swap(&mut self.state, &mut aside.state); @@ -580,6 +614,7 @@ impl LiveBind { std::mem::swap(&mut self.recent, &mut aside.recent); std::mem::swap(&mut self.listed_at, &mut aside.listed_at); std::mem::swap(&mut self.read_from, &mut aside.read_from); + std::mem::swap(&mut self.drawn, &mut aside.drawn); std::mem::swap(&mut self.settled, &mut aside.settled); std::mem::swap(&mut self.rebinds, &mut aside.rebinds); std::mem::swap(&mut self.commands, &mut aside.commands); @@ -594,10 +629,10 @@ impl LiveBind { aside.at = self.asides; } - /// Whether the state in place is the one a draw through `interface` starts from - /// ([`select`](Self::select) keeps it). - pub(super) fn reads_through(&self, interface: &PipelineInterface) -> bool { - self.read_from.through(interface) + /// Whether the state in place is the one `pipeline`'s last draw left, which a draw of it + /// starts from ([`select`](Self::select) keeps it). + pub(super) fn draws(&self, pipeline: &GraphicsState) -> bool { + (self.drawn.as_ref()).is_some_and(|drawn| drawn.is(pipeline)) } /// Set `kept` aside with the state in place, its pipeline state's (the field's docs). diff --git a/crates/metal2vulkan-runtime/src/pipeline/render.rs b/crates/metal2vulkan-runtime/src/pipeline/render.rs index e3a31dd2c..071c605ed 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/render.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/render.rs @@ -131,10 +131,20 @@ impl WeakRenderPipelineState { /// state lies at its address while it is kept. pub(crate) fn names(&self, state: &RenderPipelineState) -> bool { match &state.0 { - Kind::Graphics(graphics) => std::ptr::eq(self.0.as_ptr(), Arc::as_ptr(&graphics.0)), + Kind::Graphics(graphics) => self.is(graphics), Kind::Tile(_) => false, } } + + /// Whether it names `state` ([`names`](Self::names) of a drawing state). + pub(crate) fn is(&self, state: &GraphicsState) -> bool { + std::ptr::eq(self.0.as_ptr(), Arc::as_ptr(&state.0)) + } + + /// Whether the state it names lives still. + pub(crate) fn lives(&self) -> bool { + self.0.strong_count() > 0 + } } /// The same drawing state ([`WeakRenderPipelineState::names`]'s identity). @@ -2217,6 +2227,12 @@ impl GraphicsState { self.0.made_tables.load(Ordering::Acquire) } + /// It, named without holding it: what a draw's encoder keeps the bind state its last draw + /// left under (`live_bind.rs`), which [`WeakRenderPipelineState::is`] tells apart. + pub(crate) fn downgrade(&self) -> WeakRenderPipelineState { + WeakRenderPipelineState(Arc::downgrade(&self.0)) + } + /// Its program, named without holding it: what a draw's encoder keeps the state's last /// settle under (`live_bind.rs`), which [`runs`](Self::runs) tells apart. pub(crate) fn program_name(&self) -> ProgramName { diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index be1648573..29ff94b0e 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -11127,7 +11127,7 @@ impl RenderLowering { // reads and writes beside the codes (`render/extended_shadow.rs`). let mut shadows = self.extended_shadows_bound(); // From the state the pipeline state's last draw left, where the encoder set it aside. - live.select(&pipeline.base().interface); + live.select(pipeline); loop { let mut reshape = self.refresh_bind_state( live, diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index 1e4b82adf..f2b74ee93 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -46,8 +46,8 @@ //! tables' stamps (`rebound.rs`), the changes of its tables since it read them brought in, its //! uses noted, added to the pass's reads and gathered whole, and every state command its //! record does not hold recorded; it takes no shortcut of a draw moved, which reads what the -//! encoder's last draw left. Two states of one interface share a bind state: the draw kept of -//! one is not the other's. +//! encoder's last draw left. Each state's bind state is its own, though states of one function +//! pair share an interface, so the draw kept of one is never the other's. //! - **What a kept draw left holds for the next.** Each of its steps adds to what the encoder //! holds, and adding the same again changes nothing: its drawn area and coverage (rectangles //! and boxes, `clear_reach::DrawnArea`, `linear_attachment::Coverage`), the pass's reads @@ -301,14 +301,14 @@ impl RenderLowering { else { return; }; - let interface = &pipeline.base().interface; - if live.reads_through(interface) { + if live.draws(pipeline) { return; } live.set_aside_kept(self.again.kept.take()); - live.select(interface); + live.select(pipeline); let epoch = self.again.epoch; - self.again.kept = live.take_kept().filter(|kept| kept.epoch == epoch); + self.again.kept = + (live.take_kept()).filter(|kept| kept.epoch == epoch && kept.pipeline.is(pipeline)); if let Some(kept) = self.again.kept.as_mut() { kept.between = true; }