diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 82e18e81f..2c3eb344c 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -1584,10 +1584,9 @@ impl EncoderCore { self.counts().command_noted_as_before(); return Ok(()); } - let fresh = noted.fresh(uses, into); - self.record_aliased(stages, fresh); + let record = |fresh: &[Use]| self.record_aliased(stages, fresh); + noted.note_fresh(stages, (uses, resident), into, record); self.touch_reaching(uses, resident); - noted.note(stages, (uses, resident), into); Ok(()) } @@ -1638,15 +1637,16 @@ impl EncoderCore { self.counts().command_noted_as_before(); return Ok(()); } - let fresh = noted.fresh_draw((uses, prefix.prefix), into); - let recorded = fresh.len(); - self.record_aliased(stages, fresh); + let record = |fresh: &[Use]| { + self.record_aliased(stages, fresh); + fresh.len() + }; + let recorded = noted.note_draw_fresh(stages, (uses, resident, prefix), into, record); let rebinds = self.device().shared().rebinds.any(); let touched = noted.touched(prefix.prefix, (into.core, rebinds)); self.touch_draw((uses, resident), touched, rebinds); - noted.note_draw(stages, (uses, resident, prefix), into); - // The uses of what the encoder made resident it walked: recorded whole (`fresh_draw` - // gave them all), or touched. + // The uses of what the encoder made resident it walked: recorded whole + // (`note_draw_fresh` gave them all), or touched. if touched == 0 || recorded == uses.len() { self.counts().resident_uses_noted(len as u64); } diff --git a/crates/metal2vulkan-runtime/src/noted_apart.rs b/crates/metal2vulkan-runtime/src/noted_apart.rs index 7949b2dd1..123dedfe0 100644 --- a/crates/metal2vulkan-runtime/src/noted_apart.rs +++ b/crates/metal2vulkan-runtime/src/noted_apart.rs @@ -17,9 +17,10 @@ //! The encoder's parts and held images only grow, so their counts tell a change apart. //! //! A command noted into the same state as the last one, but with some uses changed, records -//! only the uses that differ from the last one's at their position (`NotedApart::fresh`): each -//! use equal to the last command's at its place, and so the host aliases' uses beside it, is in -//! the part already, and recording it again changes nothing. Its stages widen the part as before. +//! only the uses that differ from the last one's at their position (`NotedApart::note_fresh`): +//! each use equal to the last command's at its place, and so the host aliases' uses beside it, is +//! in the part already, and recording it again changes nothing. Its stages widen the part as +//! before, and the uses kept for the next command change at those positions alone. //! //! A draw's uses may begin with those of what its encoder made resident, which draws keep from //! one to the next and name by identity (`binding::ReachingPrefix`, with the identity of the keys @@ -51,7 +52,7 @@ pub(super) struct NotedApart { stages: vk::PipelineStageFlags2, /// The identities of the uses of what its encoder made resident at their start and of the /// keys it reached through the resident set alone, where it had them - /// ([`note_draw`](NotedApart::note_draw)): then `uses` holds its uses past them, and + /// ([`note_draw_fresh`](NotedApart::note_draw_fresh)): then `uses` holds its uses past them, and /// `resident` nothing. prefix: Option, /// Its uses as given, and the keys of them it reached through the resident set alone, in @@ -65,9 +66,10 @@ pub(super) struct NotedApart { rebinds: bool, /// The host aliases' version then. aliases: u64, - /// The uses of the command being noted that differ from the last one's - /// ([`fresh`](NotedApart::fresh)), kept so their storage is too. + /// The uses of the command being noted that differ from the last one's, and their + /// positions ([`note_fresh`](NotedApart::note_fresh)), kept so their storage is too. fresh: Vec>, + fresh_at: Vec, /// The uses of what the encoder made resident the last draw that began with some touched /// (`EncoderCore::note_draw`), by identity, with its encoder's core and whether the device /// had a buffer over host pages then: while those stand, a draw that begins with the same @@ -104,6 +106,7 @@ impl std::fmt::Debug for NotedApart { rebinds, aliases, fresh, + fresh_at, touched, #[cfg(debug_assertions)] whole: _, @@ -122,6 +125,7 @@ impl std::fmt::Debug for NotedApart { .field("rebinds", rebinds) .field("aliases", aliases) .field("fresh", fresh) + .field("fresh_at", fresh_at) .field("touched", touched) .finish() } @@ -195,47 +199,80 @@ impl NotedApart { &self.uses } - /// Keep the command just noted, of `stages` with `uses`, of which it reached the keys - /// `resident` through the resident set alone, noted into `into`. - pub(super) fn note( + /// Note the command of `stages` with `uses`, of which it reaches the keys `resident` through + /// the resident set alone, into `into`: `record` records its uses that the encoder's current + /// part may lack (all of them, unless the last command noted was noted into `into`, then + /// those that differ from its use at the same position: the module docs), and it is kept as + /// the last command, its kept uses changed at those positions alone. What `record` answers. + pub(super) fn note_fresh( &mut self, stages: vk::PipelineStageFlags2, (uses, resident): (&[Use], &BTreeSet), into: NotedInto, - ) { + record: impl FnOnce(&[Use]) -> T, + ) -> T { + let found = self.found_fresh(uses, into); + let answer = record(if found { &self.fresh } else { uses }); self.core = Some(into.core); self.stages = stages; self.prefix = None; - // Its storage is kept from one command to the next. - self.uses.clear(); - self.uses.extend_from_slice(uses); + self.keep(uses, found); self.resident.clear(); self.resident.extend(resident); self.keep_into(into); #[cfg(debug_assertions)] self.note_whole((uses, resident)); + answer } - /// [`note`](Self::note) a draw whose `uses` begin with those of what its encoder made - /// resident of the identities `prefix`: those, and its uses past them alone (the module - /// docs). - pub(super) fn note_draw( + /// [`note_fresh`](Self::note_fresh) a draw whose `uses` begin with those of what its encoder + /// made resident of the identities `prefix`: those it keeps by their identities, and its uses + /// past them alone, `record` recording all of them unless the last command began with the + /// same, then those of its uses past them that differ from the last one's at the same + /// position (the module docs). + pub(super) fn note_draw_fresh( &mut self, stages: vk::PipelineStageFlags2, (uses, resident, prefix): (&[Use], &BTreeSet, ReachingPrefix), into: NotedInto, - ) { + record: impl FnOnce(&[Use]) -> T, + ) -> T { + let found = self.found_fresh_draw((uses, prefix.prefix), into); + let answer = record(if found { &self.fresh } else { uses }); self.core = Some(into.core); self.stages = stages; self.prefix = Some(prefix); - self.uses.clear(); - self.uses.extend_from_slice(&uses[prefix.prefix.len..]); + self.keep(&uses[prefix.prefix.len..], found); self.resident.clear(); self.keep_into(into); #[cfg(debug_assertions)] self.note_whole((uses, resident)); #[cfg(not(debug_assertions))] let _ = resident; + answer + } + + /// Keep `uses` as the last command's: where `found`, those of them that differ from the kept + /// ones at their position are `fresh` at `fresh_at` ([`found_fresh`](Self::found_fresh)), so + /// only those positions change (and those past the kept ones are added, every one of them + /// fresh, in order); else all of them, copied. + fn keep(&mut self, uses: &[Use], found: bool) { + if !found { + self.uses.clear(); + self.uses.extend_from_slice(uses); + return; + } + self.uses.truncate(uses.len()); + for (&at, used) in self.fresh_at.iter().zip(&self.fresh) { + match self.uses.get_mut(at) { + Some(kept) => kept.clone_from(used), + None => self.uses.push(used.clone()), + } + } + debug_assert!( + *self.uses == *uses, + "a command noted apart kept other uses than its own" + ); } /// Keep the encoder's state `into` as the last command's. @@ -275,50 +312,37 @@ impl NotedApart { touched } - /// The uses of `uses`, a command noted into `into`, that the encoder's current part may - /// lack: all of them, unless the last command noted was noted into `into`, then those that - /// differ from its use at the same position (the module docs). - pub(super) fn fresh<'s>( - &'s mut self, - uses: &'s [Use], - into: NotedInto, - ) -> &'s [Use] { + /// Whether the last command noted was noted into `into`: then `fresh` holds the uses of + /// `uses` that differ from its use at the same position, which the encoder's current part may + /// lack (the module docs), and `fresh_at` their positions; else the part may lack any of + /// them. + fn found_fresh(&mut self, uses: &[Use], into: NotedInto) -> bool { if self.core != Some(into.core) || NotedApart::into(self) != into { - return uses; + return false; } self.fresh.clear(); - let last = &self.uses; - let differ = |(at, used): &(usize, &Use)| last.get(*at) != Some(*used); - self.fresh.extend( - uses.iter() - .enumerate() - .filter(differ) - .map(|(_, used)| used.clone()), - ); - &self.fresh + self.fresh_at.clear(); + for (at, used) in uses.iter().enumerate() { + if self.uses.get(at) != Some(used) { + self.fresh.push(used.clone()); + self.fresh_at.push(at); + } + } + true } - /// [`fresh`](Self::fresh) of a draw whose `uses` begin with those of what its encoder made - /// resident of the identity `prefix`: all of them unless the last command began with the - /// same, then those of its uses past them that differ from the last one's at the same - /// position. - pub(super) fn fresh_draw<'s>( - &'s mut self, - (uses, prefix): (&'s [Use], ResidentPrefix), + /// [`found_fresh`](Self::found_fresh) of a draw whose `uses` begin with those of what its + /// encoder made resident of the identity `prefix`: only where the last command began with + /// the same, then of its uses past them. + fn found_fresh_draw( + &mut self, + (uses, prefix): (&[Use], ResidentPrefix), into: NotedInto, - ) -> &'s [Use] { + ) -> bool { let same = self.prefix.is_some_and(|then| then.prefix == prefix); - if self.core != Some(into.core) || NotedApart::into(self) != into || !same { - return uses; + if !same || !self.found_fresh(&uses[prefix.len..], into) { + return false; } - self.fresh.clear(); - let last = &self.uses; - let differ = |(at, used): &(usize, &Use)| last.get(*at) != Some(*used); - self.fresh.extend( - (uses[prefix.len..].iter().enumerate()) - .filter(differ) - .map(|(_, used)| used.clone()), - ); #[cfg(debug_assertions)] { let whole = &self.whole.0; @@ -332,7 +356,7 @@ impl NotedApart { "a draw noted apart found fresh by its uses' identity other uses than by its uses" ); } - &self.fresh + true } fn into(&self) -> NotedInto { diff --git a/crates/metal2vulkan-runtime/src/tests/noted_apart.rs b/crates/metal2vulkan-runtime/src/tests/noted_apart.rs index 2283ef44e..24081c2dd 100644 --- a/crates/metal2vulkan-runtime/src/tests/noted_apart.rs +++ b/crates/metal2vulkan-runtime/src/tests/noted_apart.rs @@ -36,7 +36,7 @@ fn a_command_reaching_the_same_resident_keys_is_noted_already() { }; let mut noted = NotedApart::default(); assert!(!noted.holds(stages, (&uses, &resident), into)); - noted.note(stages, (&uses, &resident), into); + noted.note_fresh(stages, (&uses, &resident), into, |_| ()); assert!(noted.holds(stages, (&uses, &resident), into)); // The same uses, reaching no key or another through the resident set alone. assert!(!noted.holds(stages, (&uses, &BTreeSet::new()), into)); @@ -63,14 +63,14 @@ fn a_command_reaching_the_same_resident_keys_is_noted_already() { } // Noted again with another resident key: that one holds, the first no longer. let others = BTreeSet::from([other]); - noted.note(stages, (&uses, &others), into); + noted.note_fresh(stages, (&uses, &others), into, |_| ()); assert!(noted.holds(stages, (&uses, &others), into)); assert!(!noted.holds(stages, (&uses, &resident), into)); } /// A draw whose uses begin with those of what its encoder made resident by identity /// (`binding::ReachingPrefix`) is noted already where the last one began with the same identities -/// and its uses past them are the last's, and finds fresh only those of its uses past them that +/// and its uses past them are the last's, and records fresh only those of its uses past them that /// differ from the last's: not with another identity of them, or of the keys they reach through /// the resident set alone, or after a command without them (every use fresh then). Builds with /// debug assertions check each answer against the one the whole uses give. @@ -95,10 +95,13 @@ fn a_draw_beginning_with_the_same_resident_uses_compares_only_its_others() { }; let mut noted = NotedApart::default(); let (first, second) = (uses(64), uses(128)); - noted.note_draw(stages, (&first, &resident, prefix), into); + let draw = |noted: &mut NotedApart, (uses, prefix): (&[Use], ReachingPrefix)| { + noted.note_draw_fresh(stages, (uses, &resident, prefix), into, <[_]>::to_vec) + }; + // The first draw, noted into no state: all of its uses. + assert_eq!(draw(&mut noted, (&first, prefix)), first); assert!(noted.holds_draw(stages, (&first, &resident, prefix), into)); assert!(!noted.holds_draw(stages, (&second, &resident, prefix), into)); - assert_eq!(noted.fresh_draw((&second, prefix.prefix), into), [own(128)]); // Another identity of the uses, or of the keys they reach through the resident set alone. let others = [ ReachingPrefix { @@ -114,11 +117,64 @@ fn a_draw_beginning_with_the_same_resident_uses_compares_only_its_others() { assert!(!noted.holds_draw(stages, (&first, &resident, other), into)); } assert!(!noted.holds(stages, (&first, &resident), into)); - assert_eq!(noted.fresh_draw((&second, others[0].prefix), into), second); + assert_eq!(draw(&mut noted, (&second, prefix)), [own(128)]); + assert!(noted.holds_draw(stages, (&second, &resident, prefix), into)); + assert_eq!(draw(&mut noted, (&first, others[0])), first); // After a command without them, every use is fresh. - noted.note(stages, (&first, &resident), into); + noted.note_fresh(stages, (&first, &resident), into, |_| ()); assert!(!noted.holds_draw(stages, (&first, &resident, prefix), into)); - assert_eq!(noted.fresh_draw((&second, prefix.prefix), into), second); + assert_eq!(draw(&mut noted, (&second, prefix)), second); +} + +/// A command noted into the state the last one was noted into records the uses that differ from +/// the last one's at their position, and is kept whole though only those change in place: the +/// next command holds by its uses as by a copy of them, where some changed, more came or fewer. +#[test] +fn a_command_noted_after_another_keeps_its_uses_changed_in_place() { + let keys: [ResourceKey; 4] = std::array::from_fn(|_| ResourceKey::new()); + let stages = vk::PipelineStageFlags2::FRAGMENT_SHADER; + let into = NotedInto { + core: 7, + parts: 1, + held: 0, + rebinds: false, + aliases: 3, + }; + let resident = BTreeSet::new(); + let commands = [ + vec![read(keys[0], 0..64), read(keys[1], 0..64)], + vec![read(keys[0], 0..64), read(keys[1], 64..128)], + vec![ + read(keys[0], 0..64), + read(keys[1], 64..128), + read(keys[2], 0..16), + read(keys[3], 0..16), + ], + vec![ + read(keys[3], 0..16), + read(keys[1], 64..128), + read(keys[2], 0..16), + ], + vec![read(keys[3], 0..16)], + vec![], + vec![read(keys[2], 0..8), read(keys[0], 0..64)], + ]; + let mut noted = NotedApart::default(); + let mut last: Vec> = Vec::new(); + for uses in &commands { + let fresh = noted.note_fresh(stages, (uses, &resident), into, <[_]>::to_vec); + let differ = (uses.iter().enumerate()).filter(|(at, used)| last.get(*at) != Some(*used)); + // The first command, noted into no state, records all of its uses. + assert_eq!( + fresh, + differ.map(|(_, used)| used.clone()).collect::>() + ); + assert!(noted.holds(stages, (uses, &resident), into)); + for other in commands.iter().filter(|other| *other != uses) { + assert!(!noted.holds(stages, (other, &resident), into)); + } + last.clone_from(uses); + } } /// A draw's uses of what its encoder made resident are touched already where the last draw that