diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 74ee11b6e..3042b1e01 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -9535,12 +9535,18 @@ impl RenderLowering { self.core.note_draw(reaching, &mut live.noted)?; } } - self.unordered.add(uses); // The uses of what the encoder made resident each tracker below walks // (`WorkCounts::resident_uses_unordered_writes` and kin). let resident = uses.prefix().map_or(0, |prefix| prefix.len as u64); let counts = &device.shared().counts; - counts.resident_uses_unordered_writes(resident); + match uses.prefix() { + None => self.unordered.add(uses), + Some(prefix) => { + if self.unordered.add_draw((uses, prefix)) { + counts.resident_uses_unordered_writes(resident); + } + } + } match uses.prefix() { None => { self.pass_start.announce(&device, uses, mesh_before); diff --git a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs index be46b6a70..f85c80d0b 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs @@ -527,10 +527,16 @@ impl RenderLowering { (self.core).note_draw(reaching, &mut live.noted)?; } } - self.unordered.add(uses); let resident = uses.prefix().map_or(0, |prefix| prefix.len as u64); - (self.core.device().shared().counts).resident_uses_unordered_writes(resident); let prefix = uses.prefix(); + match prefix { + None => self.unordered.add(uses), + Some(prefix) => { + if self.unordered.add_draw((uses, prefix)) { + (self.core.device().shared().counts).resident_uses_unordered_writes(resident); + } + } + } match prefix { None => { self.pass_start.announce(self.core.device(), uses, false); diff --git a/crates/metal2vulkan-runtime/src/tests/resident_uses.rs b/crates/metal2vulkan-runtime/src/tests/resident_uses.rs index 8c2b6ca70..9d51b5376 100644 --- a/crates/metal2vulkan-runtime/src/tests/resident_uses.rs +++ b/crates/metal2vulkan-runtime/src/tests/resident_uses.rs @@ -163,6 +163,7 @@ struct Drawn { against: u64, attachments: u64, noted: u64, + unordered: u64, } /// Draw with `draw`, and what that changed. @@ -178,6 +179,8 @@ fn drawn(device: &Device, draw: impl FnOnce()) -> Drawn { attachments: (after.0.resident_uses_against_attachments) - before.0.resident_uses_against_attachments, noted: after.0.resident_uses_noted - before.0.resident_uses_noted, + unordered: (after.0.resident_uses_unordered_writes) + - before.0.resident_uses_unordered_writes, } } @@ -251,15 +254,16 @@ fn draws_keep_the_uses_of_what_their_encoder_made_resident_until_it_or_their_pla drawn.against, drawn.attachments, drawn.noted, + drawn.unordered, ]; match at { // The slot shape's plan lists none. - at if at == entry_draws => assert_eq!(walked, [0; 5], "{what}"), + at if at == entry_draws => assert_eq!(walked, [0; 6], "{what}"), at if made_again.contains(&at) => { assert!(walked.iter().all(|walked| *walked > 0), "{what}") } _ => { - assert_eq!(walked, [0; 5], "{what}"); + assert_eq!(walked, [0; 6], "{what}"); assert!(drawn.checked > 0, "{what}"); } } diff --git a/crates/metal2vulkan-runtime/src/tests/tracker.rs b/crates/metal2vulkan-runtime/src/tests/tracker.rs index 54c78f101..74c951f12 100644 --- a/crates/metal2vulkan-runtime/src/tests/tracker.rs +++ b/crates/metal2vulkan-runtime/src/tests/tracker.rs @@ -2,9 +2,11 @@ //! resources, byte ranges and image subresources, fences, retirement, the order across //! `VkQueue`s, and the legacy masks. +use crate::binding::ResidentPrefix; use crate::tracker::{ merge, ordered, Access, Barrier, EncoderUses, Footprint, GeometryWrites, IndirectReads, - LegacyBarrier, Point, Reach, Summary, Tracker, Use, UseRange, Wait, ENTRIES_PER_KEY, + LegacyBarrier, Point, Reach, Summary, Tracker, UnorderedWrites, Use, UseRange, Wait, + ENTRIES_PER_KEY, }; use ash::vk; use std::ops::Range; @@ -2010,3 +2012,46 @@ fn merging_in_place_keeps_each_key_and_range_once_in_order() { assert_eq!(merged, reference(&uses), "{uses:?}"); } } + +/// A draw's writes of what its encoder made resident, added by their identity +/// (`UnorderedWrites::add_draw`), are walked once until a memory barrier clears them: the draws +/// after the first of that identity walk none of them, past the entries a resource keeps, and a +/// later read of what they wrote meets them all along; after the clear, the next draw of that +/// identity walks them again, and the read meets them again. A draw of another identity walks its +/// own, and the writes after them are added at every draw. +#[test] +fn draws_add_the_writes_of_what_their_encoder_made_resident_once_until_a_barrier() { + let prefix = ResidentPrefix { id: 7, len: 2 }; + let uses = [ + writes(1, bytes(0..16), shader_write()), + reads(2, bytes(0..16), shader_read()), + writes(3, bytes(0..8), shader_write()), + ]; + let read = |key| [reads(key, bytes(4..8), shader_read())]; + let mut unordered = UnorderedWrites::new(); + assert!(unordered.add_draw((&uses, prefix))); + for _ in 0..=ENTRIES_PER_KEY { + assert!(!unordered.add_draw((&uses, prefix))); + } + assert!(unordered.written(&read(1)) && unordered.written(&read(3))); + unordered.clear(); + assert!(!unordered.written(&read(1)) && !unordered.written(&read(3))); + assert!( + unordered.add_draw((&uses, prefix)), + "the draw after the barrier walks them again" + ); + assert!(unordered.written(&read(1)) && unordered.written(&read(3))); + let other = ResidentPrefix { id: 8, len: 2 }; + assert!( + unordered.add_draw((&uses, other)), + "another identity walks its own" + ); + assert!( + unordered.add_draw((&uses, prefix)), + "and the first's again after it" + ); + unordered.clear(); + unordered.add(&uses[2..]); + assert!(unordered.add_draw((&uses, other))); + assert!(unordered.written(&read(1))); +} diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs index a048407b9..0959a5419 100644 --- a/crates/metal2vulkan-runtime/src/tracker.rs +++ b/crates/metal2vulkan-runtime/src/tracker.rs @@ -80,6 +80,7 @@ //! `VkQueue`'s listed in its submission order as their plans were applied, not every resource the //! state holds. +use super::binding::ResidentPrefix; use crate::device::functions::Vk; use crate::key_hash::KeyMap; use ash::vk; @@ -1512,6 +1513,10 @@ impl fmt::Debug for EncoderUses { /// as [`EncoderUses`] merges its own, which claims more and stays exact. pub(super) struct UnorderedWrites { writes: KeyMap>>, + /// The identity and count of the uses of what the encoder made resident the last + /// [`add_draw`](Self::add_draw) since the last [`clear`](Self::clear) began with + /// (`binding::ResidentPrefix`): each of their writes is held, merged or not, until it clears. + added: Option<(u64, usize)>, } impl UnorderedWrites { @@ -1519,15 +1524,18 @@ impl UnorderedWrites { pub(super) fn new() -> UnorderedWrites { UnorderedWrites { writes: KeyMap::default(), + added: None, } } + /// Whether [`add`](Self::add) adds `used`: a tracked write of something. + fn adds(used: &Use) -> bool { + used.tracked && !used.write.is_empty() && !used.range.is_empty() + } + /// Add the tracked writes of a command with `uses`. pub(super) fn add(&mut self, uses: &[Use]) { - for used in uses - .iter() - .filter(|used| used.tracked && !used.write.is_empty() && !used.range.is_empty()) - { + for used in uses.iter().filter(|used| Self::adds(used)) { let ranges = self.writes.entry(used.key).or_default(); ranges.push(Use { read: Access::NONE, @@ -1539,9 +1547,44 @@ impl UnorderedWrites { } } + /// [`add`](Self::add) a draw's `uses`, which begin with the uses of what its encoder made + /// resident of the identity `prefix`: those not again where the last call of this since the + /// last [`clear`](Self::clear) began with the same, whose writes it holds as they were added + /// (two draws' uses of one identity begin with the same uses, each of the same key, range, + /// stages and access types: `binding::ResidentPrefix`). Whether it walked them. + pub(super) fn add_draw(&mut self, (uses, prefix): (&[Use], ResidentPrefix)) -> bool { + let now = Some((prefix.id, prefix.len)); + let past = match self.added == now { + true => prefix.len, + false => 0, + }; + #[cfg(debug_assertions)] + for used in &uses[..past] { + assert!( + self.holds(used), + "an encoder added a draw's writes by their identity less than its uses" + ); + } + self.add(&uses[past..]); + self.added = now; + past == 0 + } + + /// Whether it holds `used`'s write, where [`add`](Self::add) adds it: adding it again changes + /// nothing a query reads (builds with debug assertions: [`add_draw`](Self::add_draw)'s + /// check). + #[cfg(debug_assertions)] + fn holds(&self, used: &Use) -> bool { + let held = + |known: &Use| known.range.contains(&used.range) && known.write.covers(used.write); + !Self::adds(used) + || (self.writes.get(&used.key)).is_some_and(|known| known.iter().any(held)) + } + /// A memory barrier orders every write so far before the commands after it. pub(super) fn clear(&mut self) { self.writes.clear(); + self.added = None; } /// Whether one of the writes overlaps memory one of `uses` reads, as