RUNTIME 2477: A part of one command is summarized from its uses, building no map
An ordered encoder's part whose one command waits uncompared (`first`) now keeps that command's tracked uses where they wait, rather than noting them in the part's tracked map: at the encoder's end its summary is made from them (the tracked uses that touch something, a stable sort by key, the uses of one key and range joined into the first), the very list the map read by key would give. A command of at most ENTRIES_PER_KEY uses can merge no key's ranges, so that is the only rule it has to repeat. Anything else that adds to the part, orders after it or splits it (a second command, take_in_first, note_kept, split) puts the waiting uses in the map first, through the same noting. The readers of the map before the end (written, accessed, touched_in, uses_key, written_ranges, tracked_in) read the waiting uses beside it, giving the map's answers. Why: a guest's compute encoders hold one dispatch each. The profile of the guest shape (dispatch_chain --host 400 --only encoders --dispatches 5 --groups 64 --states 3 --binds tex-moved, msi lavapipe, release, pinned) had the part's map at ~60 ns of the dispatch call (hashing each use, the map's allocation) and finish_parts' walk of it at ~104 ns of end_encoding, both for a list the dispatch already had. The encoder's order (inside, first) is untouched, so every barrier inside an encoder is the same; the summary is the identical list, so the fold's join, the batch tracker's barriers and waits and the residency see the same input. Builds with debug assertions also make the map's summary at the end and assert it equal. Tests: tests::tracker::a_part_of_one_command_is_summarized_as_its_map_would_be (the summary against an unordered encoder's map: a key at two ranges, one range twice, an untracked use, a use that touches nothing; no command noted in a map, and both of a part of two), a_part_of_one_command_answers_its_readers_as_its_map_would, and on a device tests::compute_lone_parts (a command buffer of 5 one-dispatch encoders notes no command in a part's map, of 5 two-dispatch encoders 10), counted by the test-only tracker::part_notes. Timings and suite results are in the lane's report. Checks at the lane tip 3495163515 (msi): clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings 0 (m3-max-37756); on lavapipe in a debug build, so the map-equality assertion ran in every ordered one-command encoder, tests::tracker (81) plus compute_lone_parts and compute_parked: 91/91 (m3-max-37756). The pinned interleaved timing (m3-max-37764), clippy m2v_gpu and m2v_cpu_icd, rustdoc and the lavapipe suites follow on development; the expected cut is ~150 ns a guest dispatch (the map note ~60 ns in the dispatch call, finish_parts' walk ~104 ns in end_encoding).
This commit is contained in:
1 parent
b9eadef0c9
commit
86d7480c92
6 files changed
+398
-64
No files matched your search
@@ -0,0 +1,58 @@
|
||||
//! A compute encoder of one dispatch, as a guest's encoders are, summarizes its hazards from the
|
||||
//! dispatch's own uses (`tracker.rs`, `EncoderUses`'s `lone`): its part builds no map of them, so
|
||||
//! a command buffer of such encoders notes no command in one (`tracker::part_notes`), and one of
|
||||
//! encoders of two dispatches notes both of each.
|
||||
//!
|
||||
//! 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, with `compute_parked.rs`'s kernel.
|
||||
|
||||
use super::blit_holds::Scratch;
|
||||
use super::compute_parked::{written, Kernel, WORDS};
|
||||
use super::encoder_holds::words_of;
|
||||
use crate::tracker::part_notes;
|
||||
|
||||
/// The compute encoders of a command buffer, a guest's.
|
||||
const ENCODERS: u64 = 5;
|
||||
|
||||
/// A command buffer of [`ENCODERS`] compute encoders of one dispatch each, each writing its own
|
||||
/// words: no dispatch notes its uses in a part's map, and every word is what it wrote.
|
||||
#[test]
|
||||
fn encoders_of_one_dispatch_note_no_part_map() {
|
||||
let scratch = Scratch::new("lone-parts");
|
||||
let kernel = Kernel::new(&scratch);
|
||||
kernel.warm();
|
||||
let queue = kernel.device.new_command_queue().unwrap();
|
||||
let output = kernel.output(ENCODERS);
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let before = part_notes();
|
||||
for k in 0..ENCODERS {
|
||||
kernel.dispatch(&command_buffer, (&output, k * WORDS * 4));
|
||||
}
|
||||
let noted = part_notes() - before;
|
||||
kernel.run(&command_buffer);
|
||||
assert_eq!(words_of(&output), written(ENCODERS), "the words");
|
||||
assert_eq!(
|
||||
noted, 0,
|
||||
"commands noted in a part's map, {ENCODERS} encoders"
|
||||
);
|
||||
}
|
||||
|
||||
/// The same command buffer with each encoder dispatching twice over its words: each encoder's
|
||||
/// part notes both its dispatches in its map, the first as the second comes.
|
||||
#[test]
|
||||
fn encoders_of_two_dispatches_note_both() {
|
||||
let scratch = Scratch::new("lone-parts-two");
|
||||
let kernel = Kernel::new(&scratch);
|
||||
kernel.warm();
|
||||
let queue = kernel.device.new_command_queue().unwrap();
|
||||
let output = kernel.output(ENCODERS);
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let before = part_notes();
|
||||
for k in 0..ENCODERS {
|
||||
kernel.dispatches(&command_buffer, (&output, k * WORDS * 4), 2);
|
||||
}
|
||||
let noted = part_notes() - before;
|
||||
kernel.run(&command_buffer);
|
||||
assert_eq!(words_of(&output), written(ENCODERS), "the words");
|
||||
assert_eq!(noted, 2 * ENCODERS, "commands noted in a part's map");
|
||||
}
|
||||
@@ -22,7 +22,7 @@ use crate::{
|
||||
use metal2vulkan::bind::SamplerDescriptor;
|
||||
|
||||
/// The words a dispatch writes, one threadgroup's.
|
||||
const WORDS: u64 = 64;
|
||||
pub(super) const WORDS: u64 = 64;
|
||||
|
||||
/// The compute encoders of a command buffer.
|
||||
const ENCODERS: u64 = 8;
|
||||
@@ -65,19 +65,27 @@ impl Kernel {
|
||||
|
||||
/// A compute encoder of `command_buffer` of one dispatch that writes `output` from byte
|
||||
/// `offset` on, ended.
|
||||
pub(super) fn dispatch(
|
||||
pub(super) fn dispatch(&self, command_buffer: &CommandBuffer, output: (&Buffer, u64)) {
|
||||
self.dispatches(command_buffer, output, 1);
|
||||
}
|
||||
|
||||
/// [`dispatch`](Self::dispatch) with `count` dispatches of the same words in the encoder.
|
||||
pub(super) fn dispatches(
|
||||
&self,
|
||||
command_buffer: &CommandBuffer,
|
||||
(output, offset): (&Buffer, u64),
|
||||
count: u32,
|
||||
) {
|
||||
let mut encoder = command_buffer.compute_command_encoder().unwrap();
|
||||
encoder.set_compute_pipeline_state(&self.state).unwrap();
|
||||
encoder.set_buffer(Some(&self.input), 0, 0).unwrap();
|
||||
encoder.set_buffer(Some(output), offset, 1).unwrap();
|
||||
encoder.set_buffer(Some(&self.scale), 0, 2).unwrap();
|
||||
encoder
|
||||
.dispatch_threads(Size::new(WORDS, 1, 1), Size::new(WORDS, 1, 1))
|
||||
.unwrap();
|
||||
for _ in 0..count {
|
||||
encoder
|
||||
.dispatch_threads(Size::new(WORDS, 1, 1), Size::new(WORDS, 1, 1))
|
||||
.unwrap();
|
||||
}
|
||||
encoder.end_encoding().unwrap();
|
||||
}
|
||||
|
||||
@@ -131,7 +139,7 @@ impl Moved {
|
||||
}
|
||||
|
||||
/// The words `2 * i` of `count` dispatches, as floats' bits.
|
||||
fn written(count: u64) -> Vec<u32> {
|
||||
pub(super) fn written(count: u64) -> Vec<u32> {
|
||||
(0..count)
|
||||
.flat_map(|_| (0..WORDS).map(|i| (2.0 * i as f32).to_bits()))
|
||||
.collect()
|
||||
|
||||
@@ -100,6 +100,8 @@ mod compute_create_scaling_bench;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod compute_instantiated;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod compute_lone_parts;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod compute_parked;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod compute_sampler_listing;
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
use crate::binding::ResidentPrefix;
|
||||
use crate::encoder_fold::{joined, FoldOrder};
|
||||
use crate::tracker::{
|
||||
join_looked_at, merge, ordered, Access, Barrier, EncoderUses, Footprint, GeometryWrites,
|
||||
ImagePlace, IndirectReads, LegacyBarrier, Owed, Point, Reach, Summary, Tracker,
|
||||
join_looked_at, merge, ordered, part_notes, Access, Barrier, EncoderUses, Footprint,
|
||||
GeometryWrites, ImagePlace, IndirectReads, LegacyBarrier, Owed, Point, Reach, Summary, Tracker,
|
||||
UnorderedWrites, Use, UseRange, Wait, ENTRIES_PER_KEY, OWED,
|
||||
};
|
||||
use ash::vk;
|
||||
@@ -3042,3 +3042,113 @@ fn runs_of_9_and_20_uses_join_exactly_and_one_with_an_empty_range_does_not() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A command of one key at two ranges, one of them twice (a write, then a read), another key,
|
||||
/// an untracked use and a use that touches nothing: what a part of it alone keeps.
|
||||
fn lone_command() -> [Use<Key>; 6] {
|
||||
[
|
||||
writes(3, bytes(0..64), shader_write()),
|
||||
reads(1, bytes(64..128), shader_read()),
|
||||
reads(3, bytes(128..256), shader_read()),
|
||||
reads(3, bytes(0..64), shader_read()),
|
||||
Use::reads(2, false, bytes(0..64), shader_read()),
|
||||
reads(4, bytes(0..64), Access::NONE),
|
||||
]
|
||||
}
|
||||
|
||||
/// An encoder, ordered or not, of the one command `command`, and the commands it noted in a
|
||||
/// part's map.
|
||||
fn encoder_of_one(ordered: bool, command: &[Use<Key>]) -> (EncoderUses<Key>, u64) {
|
||||
let before = part_notes();
|
||||
let mut uses = EncoderUses::new(ordered);
|
||||
uses.order(COMPUTE, command, Reach::Unnamed);
|
||||
(uses, part_notes() - before)
|
||||
}
|
||||
|
||||
/// A part of one command of an ordered encoder, which waits uncompared, is summarized from the
|
||||
/// uses it recorded and notes nothing in its map: its summary is the one an unordered encoder's
|
||||
/// map makes of the same command (each key's ranges in the order recorded, the uses of one range
|
||||
/// joined, the untracked use and the one that touches nothing left out). A second command notes
|
||||
/// both in the map.
|
||||
#[test]
|
||||
fn a_part_of_one_command_is_summarized_as_its_map_would_be() {
|
||||
let command = lone_command();
|
||||
let (lone, lone_notes) = encoder_of_one(true, &command);
|
||||
let (mapped, mapped_notes) = encoder_of_one(false, &command);
|
||||
assert_eq!(
|
||||
(lone_notes, mapped_notes),
|
||||
(0, 1),
|
||||
"the commands noted in a map"
|
||||
);
|
||||
let (lone, mapped) = (lone.finish_parts(), mapped.finish_parts());
|
||||
assert!(lone == mapped, "the summaries: {lone:?} and {mapped:?}");
|
||||
let want = [
|
||||
reads(1, bytes(64..128), shader_read()),
|
||||
Use {
|
||||
read: shader_read(),
|
||||
..writes(3, bytes(0..64), shader_write())
|
||||
},
|
||||
reads(3, bytes(128..256), shader_read()),
|
||||
];
|
||||
assert_eq!(lone[0].uses(), &want[..]);
|
||||
let before = part_notes();
|
||||
let mut two = EncoderUses::new(true);
|
||||
two.order(COMPUTE, &command, Reach::Unnamed);
|
||||
two.order(
|
||||
COMPUTE,
|
||||
&[reads(5, bytes(0..4), shader_read())],
|
||||
Reach::Unnamed,
|
||||
);
|
||||
assert_eq!(
|
||||
part_notes() - before,
|
||||
2,
|
||||
"a part of two commands notes both"
|
||||
);
|
||||
}
|
||||
|
||||
/// A part of one command answers what its encoder's later commands ask of the commands before
|
||||
/// them as an unordered encoder's map of the same command does: its writes and accesses met, its
|
||||
/// touches in a stage, the ranges of a key it writes and its tracked uses in a stage.
|
||||
#[test]
|
||||
fn a_part_of_one_command_answers_its_readers_as_its_map_would() {
|
||||
let command = lone_command();
|
||||
let (lone, _) = encoder_of_one(true, &command);
|
||||
let (mapped, _) = encoder_of_one(false, &command);
|
||||
let probes = [
|
||||
reads(3, bytes(32..48), shader_read()),
|
||||
reads(1, bytes(64..72), shader_read()),
|
||||
writes(1, bytes(100..101), shader_write()),
|
||||
writes(3, bytes(200..201), shader_write()),
|
||||
writes(2, bytes(0..64), shader_write()),
|
||||
writes(4, bytes(0..8), shader_write()),
|
||||
];
|
||||
let mut met = 0;
|
||||
for probe in &probes {
|
||||
let probe = std::slice::from_ref(probe);
|
||||
let answers = |uses: &EncoderUses<Key>| {
|
||||
[
|
||||
uses.written(probe),
|
||||
uses.accessed(probe),
|
||||
uses.touched_in(probe, COMPUTE),
|
||||
uses.touched_in(probe, TRANSFER),
|
||||
]
|
||||
};
|
||||
assert_eq!(answers(&lone), answers(&mapped), "{probe:?}");
|
||||
met += answers(&lone).iter().filter(|&&met| met).count();
|
||||
}
|
||||
assert!(met > 0, "no probe met the command");
|
||||
for key in 1..=5 {
|
||||
let ranges =
|
||||
|uses: &EncoderUses<Key>| uses.written_ranges(key).cloned().collect::<Vec<_>>();
|
||||
assert_eq!(ranges(&lone), ranges(&mapped), "key {key}");
|
||||
}
|
||||
let by_key = |mut uses: Vec<Use<Key>>| {
|
||||
uses.sort_by_key(|used| used.key);
|
||||
uses
|
||||
};
|
||||
assert_eq!(
|
||||
by_key(lone.tracked_in(COMPUTE)),
|
||||
by_key(mapped.tracked_in(COMPUTE))
|
||||
);
|
||||
assert_eq!(lone.written_ranges(3).count(), 1);
|
||||
}
|
||||
@@ -1374,6 +1374,13 @@ pub(super) struct EncoderUses<K> {
|
||||
/// ([`take_in_first`](Self::take_in_first)), so a part of one command builds no order at all.
|
||||
/// The reach is `None` where none waits; the list's storage is kept for the next part's.
|
||||
first: (Vec<Use<K>>, Option<Reach>),
|
||||
/// The current part's one command so far, waiting in `first`, is noted with the very uses it
|
||||
/// waits with, which its part's tracked map does not hold yet: a part of one command is
|
||||
/// summarized from them at its end ([`lone_summary`]), as `note` would leave its map, and
|
||||
/// builds no map at all. Anything else that adds to the part, or orders after or splits it,
|
||||
/// puts them in its map first ([`note_waiting`](Self::note_waiting)), and the readers of the
|
||||
/// map read them beside it ([`lone_uses`](Self::lone_uses)).
|
||||
lone: bool,
|
||||
/// How many commands the part's orders took in so far (`WorkCounts::commands_ordered_inside`).
|
||||
taken_in: u64,
|
||||
/// How many commands [`order_joined`](Self::order_joined) ordered by their runs joined.
|
||||
@@ -1552,6 +1559,7 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
compared: Vec::new(),
|
||||
kept: Vec::new(),
|
||||
first: (Vec::new(), None),
|
||||
lone: false,
|
||||
taken_in: 0,
|
||||
#[cfg(test)]
|
||||
joined: 0,
|
||||
@@ -1648,6 +1656,7 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
/// and named by no other use of the command: each key's list as [`note`](Self::note) leaves it
|
||||
/// from the run's uses, put in at once.
|
||||
fn note_kept(&mut self, kept: Vec<(K, Few<Use<K>>)>) {
|
||||
self.note_waiting();
|
||||
let part = self.current();
|
||||
part.tracked.reserve(kept.len());
|
||||
for (key, entries) in kept {
|
||||
@@ -1662,7 +1671,9 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
fn uses_key(&self, key: K) -> bool {
|
||||
let inside = (self.inside.get(&Class::Key(key))).is_some_and(|entries| !entries.is_empty());
|
||||
let part = self.parts.last().expect("an encoder has a part");
|
||||
inside || (part.tracked.get(&key)).is_some_and(|known| !known.is_empty())
|
||||
inside
|
||||
|| (part.tracked.get(&key)).is_some_and(|known| !known.is_empty())
|
||||
|| self.lone_uses().any(|used| used.key == key)
|
||||
}
|
||||
|
||||
/// [`order`](Self::order) of a command whose uses, as the order compares and takes them in,
|
||||
@@ -1674,7 +1685,8 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
reach: Reach,
|
||||
) -> Option<Barrier> {
|
||||
let barrier = self.barrier(ordered, reach);
|
||||
if self.ordered && !self.wait_first(ordered, reach) {
|
||||
let waits = self.ordered && self.wait_first(ordered, reach);
|
||||
if self.ordered && !waits {
|
||||
let kept = match ordered.iter().all(|used| used.tracked) {
|
||||
true => &self.compared,
|
||||
false => {
|
||||
@@ -1685,7 +1697,8 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
admit(&mut self.inside, kept, barrier, &[], Point::default());
|
||||
self.taken_in += 1;
|
||||
}
|
||||
self.note(stages, noted);
|
||||
// Waiting with the very uses it notes, it may be the part's one command (`lone`).
|
||||
self.note(stages, noted, waits && std::ptr::eq(ordered, noted));
|
||||
barrier
|
||||
}
|
||||
|
||||
@@ -1698,15 +1711,17 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
uses: &[Use<K>],
|
||||
barrier: Option<Barrier>,
|
||||
) {
|
||||
let mut waits = false;
|
||||
if self.ordered {
|
||||
self.take_in_first();
|
||||
if !self.wait_first(uses, Reach::Unnamed) {
|
||||
waits = self.wait_first(uses, Reach::Unnamed);
|
||||
if !waits {
|
||||
inside_uses(uses, Reach::Unnamed, true, &mut self.kept);
|
||||
admit(&mut self.inside, &self.kept, barrier, &[], Point::default());
|
||||
self.taken_in += 1;
|
||||
}
|
||||
}
|
||||
self.note(stages, uses);
|
||||
self.note(stages, uses, waits);
|
||||
}
|
||||
|
||||
/// Keep a command's `uses` and `reach` out of the part's order while nothing is in it (no
|
||||
@@ -1731,6 +1746,7 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
/// into it, where one waits: before anything compares with or adds to the order. Its list is
|
||||
/// made in the storage the next command compares in, which it writes over after.
|
||||
fn take_in_first(&mut self) {
|
||||
self.note_waiting();
|
||||
let (waiting, at) = &mut self.first;
|
||||
let Some(reach) = at.take() else {
|
||||
return;
|
||||
@@ -1753,42 +1769,56 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
}
|
||||
|
||||
/// Note a command the encoder orders recorded, in `stages`, with `uses`: its held images'
|
||||
/// holds, its part's stages and tracked uses.
|
||||
fn note(&mut self, stages: vk::PipelineStageFlags2, uses: &[Use<K>]) {
|
||||
/// holds, its part's stages and tracked uses. Where it `waits` in `first` with these very
|
||||
/// uses, the part's first command with no tracked use before it, of no more uses than a key
|
||||
/// keeps ranges (so no key's list would merge), its tracked uses stay there (`lone`).
|
||||
fn note(&mut self, stages: vk::PipelineStageFlags2, uses: &[Use<K>], waits: bool) {
|
||||
self.note_waiting();
|
||||
if !self.held.is_empty() {
|
||||
let held = &self.held;
|
||||
self.broke |= uses.iter().any(|used| {
|
||||
held.contains(&used.key) && !used.is_nothing() && !used.is_depth_attachment()
|
||||
});
|
||||
}
|
||||
let part = self.current();
|
||||
let part = self.parts.last_mut().expect("an encoder has a part");
|
||||
// Every stage, untracked work's too: a fence wait orders all of the encoder's work.
|
||||
part.stages |= uses.iter().fold(stages, |all, used| {
|
||||
all | used.read.stages | used.write.stages
|
||||
});
|
||||
// Room for a few resources at once, made at the part's first: grown from nothing, the map
|
||||
// would allocate again at its fourth and eighth.
|
||||
if part.tracked.capacity() == 0 {
|
||||
part.tracked.reserve(TRACKED_ROOM);
|
||||
if waits && uses.len() <= ENTRIES_PER_KEY && part.tracked.is_empty() {
|
||||
debug_assert!(
|
||||
uses == &self.first.0[..],
|
||||
"a part's one command noted with other uses than it waits with"
|
||||
);
|
||||
self.lone = true;
|
||||
return;
|
||||
}
|
||||
for used in uses
|
||||
note_tracked(part, uses);
|
||||
}
|
||||
|
||||
/// Put the uses of the current part's one command, waiting in `first` (`lone`), in its
|
||||
/// tracked map, where they are not: as `note` would have put them there.
|
||||
fn note_waiting(&mut self) {
|
||||
if !self.lone {
|
||||
return;
|
||||
}
|
||||
self.lone = false;
|
||||
let part = self.parts.last_mut().expect("an encoder has a part");
|
||||
note_tracked(part, &self.first.0);
|
||||
}
|
||||
|
||||
/// The tracked uses that touch something of the current part's one command, waiting in
|
||||
/// `first` with its part's map not holding them (`lone`), as it recorded them: none else.
|
||||
/// Each key's are those `note` would keep in its list, but that two of one range are not
|
||||
/// joined (a part of one command keeps none past a list's length, so none merge either).
|
||||
fn lone_uses(&self) -> impl Iterator<Item = &Use<K>> + Clone + '_ {
|
||||
let waiting = match self.lone {
|
||||
true => &self.first.0[..],
|
||||
false => &[],
|
||||
};
|
||||
waiting
|
||||
.iter()
|
||||
.filter(|used| used.tracked && !used.is_nothing())
|
||||
{
|
||||
let ranges = part.tracked.entry(used.key).or_default();
|
||||
match ranges.iter_mut().find(|known| known.range == used.range) {
|
||||
Some(known) => {
|
||||
known.read = known.read.union(used.read);
|
||||
known.write = known.write.union(used.write);
|
||||
}
|
||||
// A resource used at a second range (a buffer whose draws move along it) takes
|
||||
// room for every range it keeps before they merge, and the merge's own, at once.
|
||||
None => ranges.push_with_room(used.clone(), ENTRIES_PER_KEY + 1),
|
||||
}
|
||||
if ranges.len() > ENTRIES_PER_KEY {
|
||||
ranges.change_all(coalesce_uses);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a command the encoder recorded so far writes memory one of `uses` reads: an
|
||||
@@ -1799,13 +1829,10 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
uses.iter()
|
||||
.filter(|read| read.tracked && !read.read.is_empty())
|
||||
.any(|read| {
|
||||
let writes = |k: &Use<K>| !k.write.is_empty() && k.range.overlaps(&read.range);
|
||||
self.parts.iter().any(|part| {
|
||||
part.tracked.get(&read.key).is_some_and(|known| {
|
||||
known
|
||||
.iter()
|
||||
.any(|k| !k.write.is_empty() && k.range.overlaps(&read.range))
|
||||
})
|
||||
})
|
||||
(part.tracked.get(&read.key)).is_some_and(|known| known.iter().any(writes))
|
||||
}) || self.lone_uses().any(|k| k.key == read.key && writes(k))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1817,19 +1844,23 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
uses.iter()
|
||||
.filter(|write| write.tracked && !write.write.is_empty())
|
||||
.any(|write| {
|
||||
let meets = |k: &Use<K>| k.range.overlaps(&write.range);
|
||||
self.parts.iter().any(|part| {
|
||||
part.tracked
|
||||
.get(&write.key)
|
||||
.is_some_and(|known| known.iter().any(|k| k.range.overlaps(&write.range)))
|
||||
})
|
||||
(part.tracked.get(&write.key)).is_some_and(|known| known.iter().any(meets))
|
||||
}) || self.lone_uses().any(|k| k.key == write.key && meets(k))
|
||||
})
|
||||
}
|
||||
|
||||
/// The tracked uses of the commands recorded so far that ran in one of `stages`.
|
||||
pub(super) fn tracked_in(&self, stages: vk::PipelineStageFlags2) -> Vec<Use<K>> {
|
||||
let lone = match self.lone {
|
||||
true => lone_summary(self.first.0.clone()),
|
||||
false => Vec::new(),
|
||||
};
|
||||
self.parts
|
||||
.iter()
|
||||
.flat_map(|part| part.tracked.values().flatten())
|
||||
.chain(&lone)
|
||||
.filter(|known| (known.read.stages | known.write.stages).intersects(stages))
|
||||
.cloned()
|
||||
.collect()
|
||||
@@ -1843,7 +1874,7 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
part.tracked
|
||||
.get(&used.key)
|
||||
.is_some_and(|known| known.iter().any(|k| touches(k, used, stages)))
|
||||
})
|
||||
}) || self.lone_uses().any(|k| touches(k, used, stages))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1904,16 +1935,37 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
/// The ranges of `key` its tracked uses so far write, in every part: what a copy out decided
|
||||
/// as it is encoded takes of its encoder's commands before it (`managed_sync.rs`).
|
||||
pub(super) fn written_ranges(&self, key: K) -> impl Iterator<Item = &UseRange> + '_ {
|
||||
// The current part's one command's ([`lone_uses`](Self::lone_uses)): each range of the
|
||||
// key once, where it first was, if a use of it writes, as its list would hold them.
|
||||
let lone = self.lone_uses().filter(move |used| used.key == key);
|
||||
let lone_ranges = (lone.clone().enumerate())
|
||||
.filter({
|
||||
let lone = lone.clone();
|
||||
move |(at, used)| {
|
||||
lone.clone()
|
||||
.take(*at)
|
||||
.all(|known| known.range != used.range)
|
||||
}
|
||||
})
|
||||
.filter({
|
||||
let lone = lone.clone();
|
||||
move |(_, used)| {
|
||||
(lone.clone()).any(|known| known.range == used.range && !known.write.is_empty())
|
||||
}
|
||||
})
|
||||
.map(|(_, used)| &used.range);
|
||||
(self.parts.iter())
|
||||
.filter_map(move |part| part.tracked.get(&key))
|
||||
.flat_map(Few::iter)
|
||||
.filter(|used| !used.write.is_empty())
|
||||
.map(|used| &used.range)
|
||||
.chain(lone_ranges)
|
||||
}
|
||||
|
||||
/// End the current part: the commands after this run in a part of their own, after
|
||||
/// everything before it completed, so they need no barrier after the commands before.
|
||||
pub(super) fn split(&mut self) {
|
||||
self.note_waiting();
|
||||
self.inside.clear();
|
||||
// The part's first command, ordered after nothing, orders nothing after it any more.
|
||||
self.first.1 = None;
|
||||
@@ -1938,28 +1990,37 @@ impl<K: Copy + Ord + Hash> EncoderUses<K> {
|
||||
parts,
|
||||
waits,
|
||||
updates,
|
||||
first: (waiting, _),
|
||||
lone,
|
||||
..
|
||||
} = self;
|
||||
let mut summaries: Vec<Summary<K>> = parts
|
||||
.into_iter()
|
||||
.map(|part| {
|
||||
// By key, each resource's uses in the order they were recorded, as a stable sort
|
||||
// of the uses by key leaves them: a resource's uses are one list, so ordering
|
||||
// the resources orders them, and only their keys, each once, are sorted, each
|
||||
// beside its list where the part holds it (`by_key`).
|
||||
// Sized once: a flattened iterator tells no length, which would grow the vector
|
||||
// use by use.
|
||||
let count = part.tracked.values().map(Few::len).sum();
|
||||
let mut uses: Vec<Use<K>> = Vec::with_capacity(count);
|
||||
by_key(&part.tracked, |few| uses.extend(few.iter().cloned()));
|
||||
Summary {
|
||||
uses,
|
||||
stages: part.stages,
|
||||
waits: Vec::new(),
|
||||
updates: Vec::new(),
|
||||
}
|
||||
.map(|part| Summary {
|
||||
uses: part_uses(&part),
|
||||
stages: part.stages,
|
||||
waits: Vec::new(),
|
||||
updates: Vec::new(),
|
||||
})
|
||||
.collect();
|
||||
// The last part's one command's, which its map does not hold (`lone`): summarized from
|
||||
// the uses it waited with, checked against those its map would have held in builds with
|
||||
// debug assertions.
|
||||
if let (true, Some(last)) = (lone, summaries.last_mut()) {
|
||||
debug_assert!(last.uses.is_empty(), "a part of one command with a map");
|
||||
#[cfg(debug_assertions)]
|
||||
let noted = {
|
||||
let mut part = Part::new();
|
||||
note_map(&mut part, &waiting);
|
||||
part_uses(&part)
|
||||
};
|
||||
last.uses = lone_summary(waiting);
|
||||
#[cfg(debug_assertions)]
|
||||
assert!(
|
||||
last.uses == noted,
|
||||
"a part of one command summarized otherwise than its map would be"
|
||||
);
|
||||
}
|
||||
if let Some(first) = summaries.first_mut() {
|
||||
first.waits = waits;
|
||||
}
|
||||
@@ -1992,6 +2053,101 @@ fn by_key<K: Copy + Ord, V, S>(tracked: &HashMap<K, V, S>, mut each: impl FnMut(
|
||||
order.iter().for_each(|&(_, value)| each(value));
|
||||
}
|
||||
|
||||
/// Note `uses`, a command's, in `part`'s tracked map ([`EncoderUses::note`]), counted in
|
||||
/// tests (`part_notes`).
|
||||
fn note_tracked<K: Copy + Eq + Hash>(part: &mut Part<K>, uses: &[Use<K>]) {
|
||||
#[cfg(test)]
|
||||
PART_NOTES.with(|notes| notes.set(notes.get() + 1));
|
||||
note_map(part, uses);
|
||||
}
|
||||
|
||||
/// [`note_tracked`]'s work: each tracked use that touches something joins its key's use of the
|
||||
/// same range, else its key's list, whose ranges merge past [`ENTRIES_PER_KEY`].
|
||||
fn note_map<K: Copy + Eq + Hash>(part: &mut Part<K>, uses: &[Use<K>]) {
|
||||
// Room for a few resources at once, made at the part's first: grown from nothing, the map
|
||||
// would allocate again at its fourth and eighth.
|
||||
if part.tracked.capacity() == 0 {
|
||||
part.tracked.reserve(TRACKED_ROOM);
|
||||
}
|
||||
for used in uses
|
||||
.iter()
|
||||
.filter(|used| used.tracked && !used.is_nothing())
|
||||
{
|
||||
let ranges = part.tracked.entry(used.key).or_default();
|
||||
match ranges.iter_mut().find(|known| known.range == used.range) {
|
||||
Some(known) => {
|
||||
known.read = known.read.union(used.read);
|
||||
known.write = known.write.union(used.write);
|
||||
}
|
||||
// A resource used at a second range (a buffer whose draws move along it) takes
|
||||
// room for every range it keeps before they merge, and the merge's own, at once.
|
||||
None => ranges.push_with_room(used.clone(), ENTRIES_PER_KEY + 1),
|
||||
}
|
||||
if ranges.len() > ENTRIES_PER_KEY {
|
||||
ranges.change_all(coalesce_uses);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// The commands noted in a part's tracked map on this thread ([`note_tracked`]), in tests.
|
||||
static PART_NOTES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// How many commands this thread noted in a part's tracked map so far ([`note_tracked`]): none
|
||||
/// for a part of one command ([`EncoderUses`]'s `lone`).
|
||||
#[cfg(test)]
|
||||
pub(super) fn part_notes() -> u64 {
|
||||
PART_NOTES.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
/// A part's tracked uses, as its summary holds them: by key, each resource's uses in the order
|
||||
/// they were recorded, as a stable sort of the uses by key leaves them. A resource's uses are one
|
||||
/// list, so ordering the resources orders them, and only their keys, each once, are sorted, each
|
||||
/// beside its list where the part holds it ([`by_key`]).
|
||||
fn part_uses<K: Copy + Ord>(part: &Part<K>) -> Vec<Use<K>> {
|
||||
// Sized once: a flattened iterator tells no length, which would grow the vector use by use.
|
||||
let count = part.tracked.values().map(Few::len).sum();
|
||||
let mut uses: Vec<Use<K>> = Vec::with_capacity(count);
|
||||
by_key(&part.tracked, |few| uses.extend(few.iter().cloned()));
|
||||
uses
|
||||
}
|
||||
|
||||
/// The summary's uses of a part of one command, `uses` its own as it recorded them (the
|
||||
/// `lone` of [`EncoderUses`]): those tracked that touch something, by key in the order recorded,
|
||||
/// and the uses of one key and range joined into the first, as the part's tracked map would hold
|
||||
/// them, read by key ([`by_key`]). Of no more uses than [`ENTRIES_PER_KEY`], no key's ranges merge.
|
||||
fn lone_summary<K: Copy + Ord>(mut uses: Vec<Use<K>>) -> Vec<Use<K>> {
|
||||
debug_assert!(uses.len() <= ENTRIES_PER_KEY);
|
||||
uses.retain(|used| used.tracked && !used.is_nothing());
|
||||
uses.sort_by_key(|used| used.key);
|
||||
// The uses kept so far lie before `kept`; one of the key and range of one of those joins it.
|
||||
let mut kept = 0;
|
||||
for at in 0..uses.len() {
|
||||
let same = {
|
||||
let used = &uses[at];
|
||||
(0..kept)
|
||||
.rev()
|
||||
.take_while(|&known| uses[known].key == used.key)
|
||||
.find(|&known| uses[known].range == used.range)
|
||||
};
|
||||
match same {
|
||||
Some(known) => {
|
||||
let (read, write) = (uses[at].read, uses[at].write);
|
||||
uses[known].read = uses[known].read.union(read);
|
||||
uses[known].write = uses[known].write.union(write);
|
||||
}
|
||||
None => {
|
||||
uses.swap(kept, at);
|
||||
kept += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
uses.truncate(kept);
|
||||
uses
|
||||
}
|
||||
|
||||
impl<K> fmt::Debug for EncoderUses<K> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("EncoderUses")
|
||||
|
||||
Vendored
+1
-1
@@ -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 = 2476;
|
||||
pub const RUNTIME_VERSION: u32 = 2477;
|
||||
|
||||
/// 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
|
||||
|
||||
Reference in new issue
Block a user