Note a command apart in one pass, its kept uses changed only where they differ
A command noted apart (EncoderCore::note_reaching and note_draw) compared its uses with the last command's to find the fresh ones (NotedApart::fresh and fresh_draw), recorded those, then copied all of its uses over the kept ones (note and note_draw). A draw rebound with one buffer offset moved, the shape the consumer's draws.10k takes at every draw, so cloned every use twice. NotedApart::note_fresh and note_draw_fresh now do both in one step: they find the fresh uses and their positions, hand the fresh ones to the caller's record, and change the kept uses at those positions alone (those past the kept ones are added in order; with nothing found, every use is copied as before). What the part records is unchanged: the same fresh uses, in the same order. The fresh, fresh_draw, note and note_draw methods go; their tests observe the fresh uses through the record they hand on. Verified: the new test a_command_noted_after_another_keeps_its_uses_changed_in_place notes seven commands that change uses at some positions, grow and shrink, and checks the fresh uses against a positional diff and that each command, and no other, is held after it is noted. Builds with debug assertions check that the kept uses equal the command's after each in-place change. (cherry picked from commit 3dd6b4aafb1e5a837066878d017803fc3c5fdc4c)
This commit is contained in:
1 parent
55422842f5
commit
969dac667f
3 files changed
+154
-74
No files matched your search
@@ -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<ResourceKey>]| 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<ResourceKey>]| {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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<ReachingPrefix>,
|
||||
/// 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<Use<ResourceKey>>,
|
||||
fresh_at: Vec<usize>,
|
||||
/// 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<T>(
|
||||
&mut self,
|
||||
stages: vk::PipelineStageFlags2,
|
||||
(uses, resident): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>),
|
||||
into: NotedInto,
|
||||
) {
|
||||
record: impl FnOnce(&[Use<ResourceKey>]) -> 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<T>(
|
||||
&mut self,
|
||||
stages: vk::PipelineStageFlags2,
|
||||
(uses, resident, prefix): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>, ReachingPrefix),
|
||||
into: NotedInto,
|
||||
) {
|
||||
record: impl FnOnce(&[Use<ResourceKey>]) -> 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<ResourceKey>], 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<ResourceKey>],
|
||||
into: NotedInto,
|
||||
) -> &'s [Use<ResourceKey>] {
|
||||
/// 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<ResourceKey>], 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<ResourceKey>)| 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<ResourceKey>], 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<ResourceKey>], ResidentPrefix),
|
||||
into: NotedInto,
|
||||
) -> &'s [Use<ResourceKey>] {
|
||||
) -> 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<ResourceKey>)| 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 {
|
||||
|
||||
@@ -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<ResourceKey>], 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<Use<ResourceKey>> = 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::<Vec<_>>()
|
||||
);
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user