Patch a rebound draw's buffer-slot uses through a list of them

patch_buffer_uses found the uses of the buffer slots a call changed by
walking every use of the kept draw past its resident ones and looking
up each one's resource use to learn its slot. CommandUses now keeps,
in uses a caller keeps (command_uses_into), the slot and position of
each use of a buffer slot (`slots`), filled in the loop that makes the
uses; patch_buffer_uses walks that list alone. Uses that command_uses
makes whole (dispatches, tile dispatches, GPU-executed commands and the
debug check's) list nothing, since nothing patches them, so they make
no allocation more: a first version that listed slots in every
CommandUses failed frame_costs' debug draw allocation bound (20.04 a
draw, over 20) through the debug check's whole uses.

Builds with debug assertions check the list against the uses before
each patch (slot_uses_listed) and count the patches and the uses they
looked at (patched_slot_uses).

Verified on the M3 Max, debug, --cfg m2v_gpu:
- New test draw_again::draws_rebound_after_an_offset_moved_patch_that_
  slot_s_use_alone: draws of tob_sample with the texture and sampler
  bound once and vertex buffer 0's offset moving; each patch looks at
  one use, the vertex buffer's. Negative controls, each failing that
  test: listing every slot class trips "a command's uses of buffer
  slots other than it lists"; counting every use past the resident
  ones (what the old walk looked at) gives 21 uses over 7 patches
  against 7.
- Runtime suite 1620/1620 (potluck job m3-max-155); the validation
  lanes over residency, draws and notices 248/248 (m3-max-157).

draws.10k (reims-vgpu's draw_cost harness, release, M3 Max, 3 rounds
interleaved, job m3-max-168), runtime leg per draw:
- instructions 8337.5 / 8334.8 / 8334.3 against 8375.3 / 8386.3 /
  8380.2: -44 (-0.5%);
- CPU ns 430.6 / 432.5 / 430.3 against 422.6 / 432.1 / 429.5: no change
  beyond the noise.
The base binary is 43be622f72, whose tree differs from this commit's
parent only in noted_apart.rs's d2.1 variant; the two variants measured
equal at draws.10k (8386.5 / 8381.0 against 8382.4 / 8383.9).

(cherry picked from commit dbf5576dc25c7ee2bcb3e709c2b786758763aa1e)
This commit is contained in:
Anees Iqbal committed 2026-11-04 21:54:24 -08:00
1 parent 71e9c43319
commit e4d238a31d
2 files changed
+123 -17

No files matched your search

+79 -17
View File
@@ -2374,12 +2374,16 @@ fn uses_into(
let CommandUses {
uses,
sources,
slots,
others,
reaching,
named_now,
..
} = into;
uses.reserve(others.len() + command.uses.len());
// Listed in uses a caller keeps alone, which alone [`patch_buffer_uses`] patches.
slots.clear();
let listed = kept.is_some();
// Every use but those names its resource (a descriptor, a root table's static, a view):
// the keys of `reaching` named are gathered.
named_now.clear();
@@ -2393,6 +2397,11 @@ fn uses_into(
if !reaching.is_empty() && reaching.contains(&used.key) {
named_now.push(used.key);
}
if let UseTarget::Slot(slot) = resource.resource {
if listed && slot.class == SlotClass::Buffer {
slots.push((slot, uses.len() as u32));
}
}
uses.push(used);
sources.push(source);
}
@@ -2599,39 +2608,50 @@ fn bytes_use(
}
/// Patch in `into`, as [`command_uses_into`] made it of `plan`'s and `command`'s uses, the uses
/// of the buffer slots `changed`, as `tables` now bind them: whether it did, which it does where
/// each such slot binds the buffer it bound (its offset may have moved) and `into` holds the uses
/// that function made alone. Its uses are then those `command_uses_into` makes: every other use
/// is of a slot, buffer or texture no call changed, and the keys, so the resident ones, are the
/// same (`render/draw_again/rebound.rs`).
/// of the buffer slots `changed`, as `tables` now bind them, found by `into`'s list of its uses of
/// buffer slots (`CommandUses::slots`), looking at no other use: whether it did, which it does
/// where each such slot binds the buffer it bound (its offset may have moved) and `into` holds the
/// uses that function made alone. Its uses are then those `command_uses_into` makes: every other
/// use is of a slot, buffer or texture no call changed, and the keys, so the resident ones, are
/// the same (`render/draw_again/rebound.rs`). Builds with debug assertions check the list against
/// the uses, and count the uses it looked at ([`patched_slot_uses`]).
pub(super) fn patch_buffer_uses(
(plan, command, tables): (&BindingPlan, &Command<'_>, &Tables<'_>),
changed: &[SlotRef],
into: &mut CommandUses,
) -> Result<bool, Error> {
if into.sources.len() != into.uses.len() {
return Ok(false);
}
#[cfg(debug_assertions)]
{
slot_uses_listed((plan, command), into);
let looked = into.slots.len() as u64;
PATCHED_SLOT_USES.with(|patched| {
let (calls, walked) = patched.get();
patched.set((calls + 1, walked + looked));
});
}
let CommandUses {
uses,
sources,
used: resident,
slots,
..
} = into;
if sources.len() != uses.len() {
return Ok(false);
}
// The uses of what the encoder made resident, first, are of no slot.
for (used, &source) in (uses.iter_mut().zip(sources.iter())).skip(*resident) {
for &(slot, at) in slots.iter() {
if !changed.contains(&slot) {
continue;
}
let at = at as usize;
let (Some(used), Some(&source)) = (uses.get_mut(at), sources.get(at)) else {
return Err(internal("a use of a buffer slot past the command's uses"));
};
let source = source as usize;
let resource = match source.checked_sub(plan.uses.len()) {
None => plan.uses.get(source),
Some(at) => command.uses.get(at),
};
let resource = resource.ok_or_else(|| internal("a use of a resource use since gone"))?;
let UseTarget::Slot(slot) = resource.resource else {
continue;
};
if slot.class != SlotClass::Buffer || !changed.contains(&slot) {
continue;
}
let Some((buffer, offset)) = slot_buffer(tables, slot) else {
return Ok(false);
};
@@ -2645,6 +2665,43 @@ pub(super) fn patch_buffer_uses(
Ok(true)
}
/// Check that `into`, as [`command_uses_into`] made it of `plan`'s and `command`'s uses, lists
/// each of its uses of a buffer slot at its position (`CommandUses::slots`): every use past those
/// of what the encoder made resident (of no slot) of a resource use of a buffer slot. In builds
/// with debug assertions.
#[cfg(debug_assertions)]
fn slot_uses_listed((plan, command): (&BindingPlan, &Command<'_>), into: &CommandUses) {
let source = |at: usize| {
let source = into.sources[at] as usize;
match source.checked_sub(plan.uses.len()) {
None => plan.uses.get(source),
Some(at) => command.uses.get(at),
}
};
let listed = (into.used..into.uses.len()).filter_map(|at| match source(at)?.resource {
UseTarget::Slot(slot) if slot.class == SlotClass::Buffer => Some((slot, at as u32)),
_ => None,
});
assert!(
listed.eq(into.slots.iter().copied()),
"a command's uses of buffer slots other than it lists"
);
}
#[cfg(debug_assertions)]
thread_local! {
/// The calls of [`patch_buffer_uses`] that reached its uses on this thread, and the uses of
/// buffer slots they looked at, in builds with debug assertions.
static PATCHED_SLOT_USES: std::cell::Cell<(u64, u64)> = const { std::cell::Cell::new((0, 0)) };
}
/// How many calls of [`patch_buffer_uses`] reached their uses on this thread so far, and how
/// many uses of buffer slots they looked at.
#[cfg(all(test, debug_assertions, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn patched_slot_uses() -> (u64, u64) {
PATCHED_SLOT_USES.with(std::cell::Cell::get)
}
/// A dispatch's or draw's uses (`command_uses`), for the tracker, and the keys among them that
/// the command reaches through its root tables' resident set alone: buffers and heaps an encoder
/// made resident, which no use of the command names. The encoder's core takes both
@@ -2658,6 +2715,11 @@ pub(super) struct CommandUses {
/// Of each use [`command_uses_into`] made, in order, the index among its plan's and command's
/// resource uses of the one it was made of: what [`patch_buffer_uses`] patches by.
sources: Vec<u32>,
/// Of each of those uses of a buffer slot, in order, the slot and the use's position, in uses
/// a caller keeps ([`command_uses_into`]): where [`patch_buffer_uses`] finds the uses of the
/// slots a call changed, looking at no other. Empty in those [`command_uses`] makes, which
/// nothing patches.
slots: Vec<(SlotRef, u32)>,
/// How many of `uses` and `sources`, first, are of what the encoder made resident, made for
/// `of` (`command_uses_into`); and how many of those the last call made (0 where it kept
/// them: `WorkCounts::resident_uses_made`).
@@ -713,6 +713,50 @@ fn sampled_rectangles(
}
}
/// Draws rebound after their vertex buffer's offset alone moved patch that slot's use in place,
/// looking at no other use (`binding::patch_buffer_uses`, which finds the uses of buffer slots in
/// the list `binding::command_uses_into` keeps of them): each draw of `tob_sample`'s rectangles
/// also samples a texture, bound once before them, whose use no patch looks at. Builds with debug
/// assertions, the only builds of this test, count the uses a patch looked at
/// (`binding::patched_slot_uses`) and check the list against the uses.
#[cfg(debug_assertions)]
#[test]
fn draws_rebound_after_an_offset_moved_patch_that_slot_s_use_alone() {
let scratch = Scratch::new("draw-again-patched-slot-uses");
let heard = Heard::default();
let device = device(&scratch.0, &[], &heard);
let state = state(&device, "engine/tob_renders.ll", ("tob_rect", "tob_sample"));
let rectangles = rectangles_from_columns(&device);
let textures = textures(&device);
let sampler = sampler_addressing(&device, SamplerAddressMode::ClampToEdge, f32::MAX);
let draws = |encoder: &mut RenderCommandEncoder| {
encoder.set_render_pipeline_state(&state).unwrap();
encoder.set_vertex_buffer(Some(&rectangles), 0, 0).unwrap();
encoder.set_fragment_texture(Some(&textures[0]), 0).unwrap();
encoder
.set_fragment_sampler_state(Some(&sampler), 0)
.unwrap();
for at in 0..DRAWS {
if at > 0 {
encoder.set_vertex_buffer_offset(16 * at, 0).unwrap();
}
rectangle(encoder);
}
};
warm(&device, draws);
let before = (crate::binding::patched_slot_uses(), device.work_counts());
run(&device, draws);
let after = (crate::binding::patched_slot_uses(), device.work_counts());
let (calls, walked) = (after.0 .0 - before.0 .0, after.0 .1 - before.0 .1);
let rebound = after.1.draws_rebound - before.1.draws_rebound;
assert_eq!(rebound, drawn_alone(HITS));
assert!(calls >= rebound, "{calls} patches, {rebound} rebound");
assert_eq!(
walked, calls,
"each patch looks at the vertex buffer's use alone"
);
}
/// Draws after calls that bound other textures and sampler states at the slots their fragment
/// function samples (`tob_sample`, which samples its 4x4 texture at the pixel's position over the
/// texture's size: inside it in the target's first four columns, outside it in the others) rebind