Add a draw's resident writes to the unordered writes once per identity

UnorderedWrites::add walked every use of every draw, the uses of what
the encoder made resident included. Those come first and are the same
for every draw of one identity (binding::ResidentPrefix): one key,
range, stages and access types each, a function of the plan and the
residency's generation alone (command_uses_into). The new
UnorderedWrites::add_draw((uses, prefix)) adds the uses after the
prefix at every draw. It adds the prefix's writes only where the last
add_draw since the last clear began with another identity. Otherwise
they are held already, merged or not, and adding them again changes
nothing written() reads.
- clear() (a memory barrier) forgets the identity, so the next draw
  adds them again.
- A draw of another identity adds its own, and becomes the identity
  held.
- Builds with debug assertions check that every skipped write is held,
  covered by range and access.
render.rs and render/draw_again/rebound.rs call add_draw where the draw
has a prefix and count the resident uses only where it walked them
(WorkCounts::resident_uses_unordered_writes). Tile dispatches keep
add.

Verified:
- tests::tracker::draws_add_the_writes_of_what_their_encoder_made_
  resident_once_until_a_barrier: walked once past ENTRIES_PER_KEY
  draws; the writes are read; after a clear they are walked again and
  read again; another identity walks its own, and the first walks
  again after it. 65/65 tracker tests pass, CPU, debug.
- Negative control, the reset in clear() removed: that test fails at
  the debug check ("an encoder added a draw's writes by their identity
  less than its uses").
- tests::resident_uses checks the new count with the others: 0 for a
  draw that keeps its uses, more where they were made again. It runs
  in the device suite at this stack's tip.

Measured, M3 Max, release, resident_draws lane at N 5000 (job
m3-max-348; the parent 8e4e904fb6 in job m3-max-273), marginal over
100,000 draws:

| | parent | this commit |
|---|---|---|
| own encoder, instructions a draw | 236,696 | 196,489 (-17.0%) |
| own encoder, cycles a draw | 65,573 | 51,823 |
| own write, instructions a draw | 277,582 | 237,561 (-14.4%) |
| resident_uses_unordered_writes, 2 frames | 50,020,000 | 10,004 |

The count is now one walk of the 5,002 uses a frame, where they are
made.

draws.10k runtime leg: 8375.7 / 8375.7 instructions a draw, against
8387.6 / 8395.8 at the parent.

(cherry picked from commit de4b3e99842757dbfd023b2e55b7b11d6158f6b5)
(cherry picked from commit cd699661642fbb21444d09e48dc3d5be6218f5ba)
This commit is contained in:
Anees Iqbal committed 2026-11-05 02:08:26 -08:00
1 parent 23f8b141a1
commit 1181ebe77f
5 files changed
+115 -11

No files matched your search

+8 -2
View File
@@ -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);
@@ -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);
@@ -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}");
}
}
@@ -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)));
}
+47 -4
View File
@@ -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<K> fmt::Debug for EncoderUses<K> {
/// as [`EncoderUses`] merges its own, which claims more and stays exact.
pub(super) struct UnorderedWrites<K> {
writes: KeyMap<K, Vec<Use<K>>>,
/// 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<K: Copy + Ord + Hash> UnorderedWrites<K> {
@@ -1519,15 +1524,18 @@ impl<K: Copy + Ord + Hash> UnorderedWrites<K> {
pub(super) fn new() -> UnorderedWrites<K> {
UnorderedWrites {
writes: KeyMap::default(),
added: None,
}
}
/// Whether [`add`](Self::add) adds `used`: a tracked write of something.
fn adds(used: &Use<K>) -> 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<K>]) {
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<K: Copy + Ord + Hash> UnorderedWrites<K> {
}
}
/// [`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<K>], 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<K>) -> bool {
let held =
|known: &Use<K>| 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