diff --git a/crates/metal2vulkan/src/bind/state.rs b/crates/metal2vulkan/src/bind/state.rs index 2c1d23838..898b439bb 100644 --- a/crates/metal2vulkan/src/bind/state.rs +++ b/crates/metal2vulkan/src/bind/state.rs @@ -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, @@ -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>) -> 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) -> 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 { let mut used: BTreeMap = 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)] diff --git a/crates/metal2vulkan/src/tests/bind_shape_of.rs b/crates/metal2vulkan/src/tests/bind_shape_of.rs index 190390f29..21c315396 100644 --- a/crates/metal2vulkan/src/tests/bind_shape_of.rs +++ b/crates/metal2vulkan/src/tests/bind_shape_of.rs @@ -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)| { + 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"); +}