Compare a state's used resources listed in token order with a shape's in place
BindState::is_shape made the used resources' shape, a BTreeMap by token, before it compared it with a shape's, at most once per position_of_shape. The runtime's kept_shape (GraphicsState) asks it for each draw whose state the live bind and its recent shapes did not hold, a few times a frame: on the replayed textured page about 20 000 used resources each time, 63 of the steps thread's 2039 samples in a 6 s window on the M3 at 3a4b3e74e2 (kept_shape -> position_of_shape -> is_shape and used_shape, the map's eq and free). A consumer that lists them in token order (used_in_token_order, which the runtime sets) lies a token's entries together, so is_shape now walks them beside the shape's map, unions each token's entries as used_shape does (the first entry's class kept) and compares entry by entry, making nothing. A list in no order still makes the map once. Verified (M3, potluck): the new test tests::bind_shape_of::a_state_listed_in_token_order_compares_its_used_resources_in_place passes: over 13 listings (a token moved, a usage, a stage, a class, a token listed twice four ways, one less, one more, one only) each ordered state answers has_shape as state.shape() == shape and as the unordered state does, against every shape, and position_of_shape finds the same, with 0 allocations. Negative control: is_shape making the map for an ordered list fails it (its "made" assertion, state 0 against shape 0). Timing, the replayed textured page on the M3 (5001 draws a frame), this commit with the next (they were measured together), against 3a4b3e74e2, interleaved, two rounds of 3 frames: warm host_step_ms 33.7-35.3 -> 28.5-30.9, encoding to completion 66.6-70.3 -> 59.6-63.8 ms, peak resident 309-311 -> 289-291 MB, pixels e3306dc52a1c9e86 at every frame of both. Sampled (macOS sample, 6 s, steps thread): kept_shape 63 -> 15 samples, used_shape 50 -> 0. The runtime's suite 1656/1656 with --cfg m2v_gpu; the frame-cost lane passes (debug 19.38 allocations a draw, bound 20; release 7.55, bound 9); the residency and draw lanes 247/248, the one other (indirect_executions::...as_its_dispatch_17) stalled past 180 s once and passed in 4.6 s on a rerun of its 64 shards (64/64). clippy -D warnings with --cfg m2v_gpu over the translator, the runtime and validation. (cherry picked from commit 241946905eb8d5c89113d9b09fce348f4629f38e)
This commit is contained in:
1 parent
b23d78eb50
commit
dc7b3d5dae
2 files changed
+136
-16
No files matched your search
@@ -38,8 +38,9 @@ pub struct BindState {
|
||||
/// The consumer lists `used_resources` in token order: each entry's token at least the one
|
||||
/// before it, so a token's entries lie together. A heap member then finds its entries by
|
||||
/// search, at the logarithm of their count (a debug build asserts the order), rather than
|
||||
/// every command's instantiation reading every entry. Not part of the shape; `false` (the
|
||||
/// default): listed in any order.
|
||||
/// every command's instantiation reading every entry, and a comparison with a shape
|
||||
/// ([`has_shape`](Self::has_shape)) reads the list in place, making none of its shape. Not
|
||||
/// part of the shape; `false` (the default): listed in any order.
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
pub used_in_token_order: bool,
|
||||
/// The render encoder's vertex amplification (`setVertexAmplificationCount:viewMappings:`);
|
||||
@@ -736,9 +737,10 @@ impl BindState {
|
||||
|
||||
/// Whether `shape` is this state's shape: `self.shape() == *shape`, answered without making
|
||||
/// the shape, so a consumer that keeps the shapes it made can find a state's among them
|
||||
/// without allocating. Its used resources' shape alone is made, where it lists any and every
|
||||
/// other field matches; [`position_of_shape`](Self::position_of_shape) makes it once for
|
||||
/// many shapes.
|
||||
/// without allocating. Its used resources' shape alone is made, where it lists any in no
|
||||
/// token order ([`used_in_token_order`](Self::used_in_token_order)) and every other field
|
||||
/// matches; [`position_of_shape`](Self::position_of_shape) makes it once for many shapes. A
|
||||
/// list in token order is compared in place, making nothing.
|
||||
pub fn has_shape(&self, shape: &BindShape) -> bool {
|
||||
self.is_shape(shape, &mut None)
|
||||
}
|
||||
@@ -746,7 +748,7 @@ impl BindState {
|
||||
/// The position among `shapes` of the first that is this state's shape (of which
|
||||
/// [`has_shape`](Self::has_shape) answers true), found without making the shape: its used
|
||||
/// resources' shape is made at most once, however many shapes it is compared with, and only
|
||||
/// where it lists any and a shape's every other field matches.
|
||||
/// where it lists any in no token order and a shape's every other field matches.
|
||||
pub fn position_of_shape<'s>(
|
||||
&self,
|
||||
shapes: impl IntoIterator<Item = &'s BindShape>,
|
||||
@@ -758,16 +760,47 @@ impl BindState {
|
||||
}
|
||||
|
||||
/// Whether `shape` is this state's shape ([`has_shape`](Self::has_shape)), its used
|
||||
/// resources' shape taken from `made`, or made into it where it is needed first.
|
||||
/// resources' compared in place where they are listed in token order, else their shape taken
|
||||
/// from `made`, or made into it where it is needed first.
|
||||
fn is_shape(&self, shape: &BindShape, made: &mut Option<BTreeMap<u32, UsedShape>>) -> bool {
|
||||
// Last: the one field compared through a shape made of it.
|
||||
self.has_shape_beside_used(shape)
|
||||
&& match self.used_resources.is_empty() {
|
||||
true => shape.used.is_empty(),
|
||||
false => *made.get_or_insert_with(|| self.used_shape()) == shape.used,
|
||||
&& match (self.used_resources.is_empty(), self.used_in_token_order) {
|
||||
(true, _) => shape.used.is_empty(),
|
||||
(false, true) => self.used_in_order_are(&shape.used),
|
||||
(false, false) => *made.get_or_insert_with(|| self.used_shape()) == shape.used,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `used`, a shape's used resources, is [`used_shape`](Self::used_shape)'s answer for
|
||||
/// `used_resources` listed in token order: each token's entries, which lie together, unioned
|
||||
/// as `used_shape` unions them and compared with the shape's entry of that token, in place.
|
||||
fn used_in_order_are(&self, used: &BTreeMap<u32, UsedShape>) -> bool {
|
||||
let listed = &self.used_resources;
|
||||
debug_assert!(
|
||||
listed.windows(2).all(|w| w[0].token <= w[1].token),
|
||||
"used resources listed in token order are not"
|
||||
);
|
||||
let mut shapes = used.iter();
|
||||
let mut listed = listed.iter().peekable();
|
||||
while let Some(first) = listed.next() {
|
||||
let mut made = UsedShape {
|
||||
class: UsedClass::of(first.class),
|
||||
usage: first.usage,
|
||||
stages: first.stages,
|
||||
};
|
||||
while let Some(again) = listed.next_if(|u| u.token == first.token) {
|
||||
made.usage |= again.usage;
|
||||
made.stages |= again.stages;
|
||||
}
|
||||
match shapes.next() {
|
||||
Some((token, shape)) if *token == first.token && *shape == made => {}
|
||||
Some(_) | None => return false,
|
||||
}
|
||||
}
|
||||
shapes.next().is_none()
|
||||
}
|
||||
|
||||
/// Whether `shape` is this state's shape but for its used resources' ([`BindShape::used`]),
|
||||
/// which are not looked at: every other field compared as [`has_shape`](Self::has_shape)
|
||||
/// compares it. A consumer that keeps the shapes a state took while its used resources stayed
|
||||
@@ -836,13 +869,8 @@ impl BindState {
|
||||
fn used_shape(&self) -> BTreeMap<u32, UsedShape> {
|
||||
let mut used: BTreeMap<u32, UsedShape> = BTreeMap::new();
|
||||
for u in &self.used_resources {
|
||||
let class = match u.class {
|
||||
ResourceClass::Buffer => UsedClass::Buffer,
|
||||
ResourceClass::Texture(facts) => UsedClass::Texture(facts.shape()),
|
||||
ResourceClass::Heap => UsedClass::Heap,
|
||||
};
|
||||
let e = used.entry(u.token).or_insert(UsedShape {
|
||||
class,
|
||||
class: UsedClass::of(u.class),
|
||||
usage: metal::ResourceUsage::empty(),
|
||||
stages: metal::RenderStages::empty(),
|
||||
});
|
||||
@@ -1912,6 +1940,17 @@ pub enum UsedClass {
|
||||
Heap,
|
||||
}
|
||||
|
||||
impl UsedClass {
|
||||
/// The kind of a used resource of `class`.
|
||||
fn of(class: ResourceClass) -> UsedClass {
|
||||
match class {
|
||||
ResourceClass::Buffer => UsedClass::Buffer,
|
||||
ResourceClass::Texture(facts) => UsedClass::Texture(facts.shape()),
|
||||
ResourceClass::Heap => UsedClass::Heap,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The shape of one stage's argument tables.
|
||||
#[non_exhaustive]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
|
||||
@@ -355,3 +355,84 @@ fn a_state_s_shape_is_found_among_many_with_its_used_resources_made_once() {
|
||||
let (found, made) = allocations_during(|| other.position_of_shape(&shapes));
|
||||
assert_eq!((found, made), (None, 0), "a state of another field");
|
||||
}
|
||||
|
||||
/// A state that lists its used resources in token order (`BindState::used_in_token_order`)
|
||||
/// compares them with a shape's in place, making nothing: over listings that differ in a token, a
|
||||
/// usage, a stage, a class, a token listed twice (whose entries union, the first's class kept) or
|
||||
/// one listed more or less, each answers as the same listing in no order does, against the shape
|
||||
/// of every other, and is found among them all without allocating.
|
||||
#[test]
|
||||
fn a_state_listed_in_token_order_compares_its_used_resources_in_place() {
|
||||
use super::constant_blob_footprint::allocations_during;
|
||||
let entry = |class, token, usage, stages| {
|
||||
let mut used = UsedResource::new(class, token);
|
||||
used.usage = usage;
|
||||
used.stages = stages;
|
||||
used
|
||||
};
|
||||
let buffer = ResourceClass::Buffer;
|
||||
let read = |token| entry(buffer, token, ResourceUsage::READ, RenderStages::FRAGMENT);
|
||||
let listing = |change: &dyn Fn(&mut Vec<UsedResource>)| {
|
||||
let mut state = base();
|
||||
state.used_resources = (0..16).map(read).collect();
|
||||
change(&mut state.used_resources);
|
||||
state
|
||||
};
|
||||
let facts = |format| ResourceClass::Texture(TextureFacts::new(TextureType::Type2D, format));
|
||||
let unorm = facts(PixelFormat::RGBA8Unorm);
|
||||
let states = [
|
||||
listing(&|_| {}),
|
||||
listing(&|used| used[15].token = 40),
|
||||
listing(&|used| used[5].usage = ResourceUsage::WRITE),
|
||||
listing(&|used| used[5].stages = RenderStages::VERTEX),
|
||||
listing(&|used| used[7].class = unorm),
|
||||
listing(&|used| used[7].class = facts(PixelFormat::R32Float)),
|
||||
listing(&|used| used.insert(8, read(8))),
|
||||
listing(&|used| {
|
||||
used.insert(
|
||||
9,
|
||||
entry(buffer, 8, ResourceUsage::WRITE, RenderStages::VERTEX),
|
||||
)
|
||||
}),
|
||||
listing(&|used| {
|
||||
used.insert(
|
||||
9,
|
||||
entry(unorm, 8, ResourceUsage::WRITE, RenderStages::VERTEX),
|
||||
)
|
||||
}),
|
||||
listing(&|used| {
|
||||
used[8].usage = ResourceUsage::READ | ResourceUsage::WRITE;
|
||||
used[8].stages = RenderStages::VERTEX | RenderStages::FRAGMENT;
|
||||
}),
|
||||
listing(&|used| {
|
||||
used.remove(10);
|
||||
}),
|
||||
listing(&|used| used.push(read(16))),
|
||||
listing(&|used| used.truncate(1)),
|
||||
];
|
||||
let shapes: Vec<_> = states.iter().map(BindState::shape).collect();
|
||||
let mut same = 0;
|
||||
for (at, state) in states.iter().enumerate() {
|
||||
let mut ordered = state.clone();
|
||||
ordered.used_in_token_order = true;
|
||||
for (of, shape) in shapes.iter().enumerate() {
|
||||
let equal = shapes[at] == *shape;
|
||||
same += usize::from(equal && at != of);
|
||||
let (has, made) = allocations_during(|| ordered.has_shape(shape));
|
||||
assert_eq!(has, equal, "state {at} in token order, shape {of}");
|
||||
assert_eq!(
|
||||
has,
|
||||
state.has_shape(shape),
|
||||
"state {at} in no order, shape {of}"
|
||||
);
|
||||
assert_eq!(made, 0, "state {at} in token order, shape {of}: made");
|
||||
}
|
||||
let (found, made) = allocations_during(|| ordered.position_of_shape(&shapes));
|
||||
assert_eq!(found, state.position_of_shape(&shapes), "state {at}: found");
|
||||
assert_eq!(made, 0, "state {at}: found among them all, made");
|
||||
}
|
||||
// A token listed twice with the same usage and stages is the listing's shape (one pair); one
|
||||
// listed again with a vertex write, as a buffer or as a texture, is the one listed once that
|
||||
// reads and writes it in both stages (three of one shape).
|
||||
assert_eq!(same, 2 + 3 * 2, "pairs of one shape");
|
||||
}
|
||||
Reference in new issue
Block a user