Argument clamps in batches (VICTORY_INDIRECT_BATCH)

One pass clamps the arguments of all indirect executions between two
points where they may change; its jobs are in upload memory that is filled
while the list is recorded. The replayed frame: 2,355 clamps become 980
passes, 86.1 -> 77.7 ms; with VICTORY_INDIRECT_BATCH=2 133 passes,
84.6 -> 75.3 ms.

Replay: --batch-clamps, --only-buffers, --listing; --compare runs turn by
turn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:10:47 -06:00
1 parent 65b4c43ec1
commit 037596241a
9 files changed
+1492 -53

No files matched your search

+143 -1
View File
@@ -186,6 +186,67 @@ fn windows() -> bool {
*ON.get_or_init(|| std::env::var_os("VICTORY_INDIRECT_WINDOW").is_some_and(|v| v == "1"))
}
/// Whether indirect executions of one record are clamped in batches
/// (`crate::indirect::batch`; `VICTORY_INDIRECT_BATCH=0`: each on its own,
/// or in windows ([`windows`])).
///
/// A frame of gameplay has 4,809 indirect executions (a capture: 3,523 mesh
/// draws, 948 dispatches, 338 draws), and with the windows still 2,355
/// clamps: each a dispatch between two full waits, with the scratch buffer
/// moved twice. The replayed frame's 84.6 ms are 75.3 ms with the clamps in
/// 133 passes, and 77.7 ms in the 980 of this.
///
/// A batch is opened by the first execution after a point where arguments
/// may have changed, and all executions up to the next such point read
/// theirs from it, each exactly the record it asks for (windows guess which
/// are next, and clamp what they do not know). Arguments in buffers change
/// by dispatches and by draws whose shaders store to buffers; those end the
/// batch, so the record an execution reads is clamped after all that was
/// recorded before it, and the pass waits for all of it, exactly as the
/// clamp of its own did. Between two executions of a batch is no wait of
/// ours: there is nothing between them to wait for but draws that store to
/// no buffer.
///
/// `VICTORY_INDIRECT_BATCH=2`: a dispatch does not end the batch before the
/// title waits for it (a barrier, or a pipeline sync) or draws: what a
/// dispatch writes is not there for a later command without a wait, and
/// dispatches the title runs side by side (its tile classes, up to 92 at a
/// time) are not held up by waits of ours. Off: whether the title has ways
/// to wait that do not pass through its lists' methods is not known.
fn batches() -> u8 {
static ON: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
*ON.get_or_init(|| match std::env::var("VICTORY_INDIRECT_BATCH").as_deref() {
Ok("0") => 0,
Ok("2") => 2,
_ => 1,
})
}
/// The records clamped by the pass opened last ([`batches`]).
struct Batch {
table: crate::indirect::batch::Table,
scratch: ID3D12Resource,
/// Dispatches since, which may have written arguments
/// (`VICTORY_INDIRECT_BATCH=2`).
wrote: bool,
}
/// The host copies arguments were read from last: what a batch opens with,
/// as the executions after the first may read theirs from another one.
static ARGUMENT_COPIES: std::sync::Mutex<Vec<std::sync::Weak<crate::shadow::Shadow>>> = std::sync::Mutex::new(Vec::new());
/// [`ARGUMENT_COPIES`] that live, and `first` put in front of them.
fn argument_copies(first: &std::sync::Arc<crate::shadow::Shadow>) -> Vec<(u64, ID3D12Resource)> {
let mut copies = ARGUMENT_COPIES.lock().unwrap_or_else(|e| e.into_inner());
let live: Vec<_> = copies.iter().filter_map(std::sync::Weak::upgrade).filter(|s| s.id() != first.id()).collect();
*copies = std::iter::once(first)
.chain(&live)
.take(crate::indirect::batch::SOURCES)
.map(std::sync::Arc::downgrade)
.collect();
live.iter().map(|s| (s.id(), s.buffer.clone())).collect()
}
/// What one title list has set, and what its host list was last given.
#[derive(Default)]
pub(crate) struct State {
@@ -219,6 +280,9 @@ pub(crate) struct State {
/// The indirect records clamped last, if later draws may still read
/// them ([`windows`]).
window: Option<Window>,
/// The records clamped in a batch, if later executions may still read
/// them ([`batches`]).
batch: Option<Batch>,
/// What the host list has: pipeline, root signature, heaps.
host_pipeline: usize,
/// Graphics, compute.
@@ -2031,6 +2095,7 @@ impl State {
list.note_op(|| self.op_text(format!("dispatch {x}x{y}x{z}"), true));
// SAFETY: an open list with everything set.
unsafe { list.list.Dispatch(x, y, z) };
self.buffers_written();
DISPATCHED.fetch_add(1, Ordering::Relaxed);
}
@@ -2301,6 +2366,7 @@ impl State {
self.mesh_scissor(list, true);
// SAFETY: an open list with everything set.
unsafe { l6.DispatchMesh(x, 1, 1) };
self.drew(crate::pipeline::host_pipeline(self.pipeline as This));
self.mesh_scissor(list, false);
DRAWN.fetch_add(1, Ordering::Relaxed);
true
@@ -2702,6 +2768,7 @@ impl State {
list.list
.DrawInstanced(count, instances, from, first_instance)
};
self.drew(crate::pipeline::host_pipeline(self.pipeline as This));
DRAWN.fetch_add(1, Ordering::Relaxed);
}
if !ran {
@@ -2803,6 +2870,7 @@ impl State {
list.list
.DrawIndexedInstanced(indices, instances, first, base, first_instance)
};
self.drew(crate::pipeline::host_pipeline(self.pipeline as This));
DRAWN.fetch_add(1, Ordering::Relaxed);
}
if !ran {
@@ -2814,6 +2882,65 @@ impl State {
}
impl State {
/// Shaders that can write buffers were recorded: arguments may be others
/// from here on.
fn buffers_written(&mut self) {
match &mut self.batch {
Some(batch) if batches() == 2 => batch.wrote = true,
batch => *batch = None,
}
}
/// A draw with the pipeline `p` was recorded (as [`State::buffers_written`]
/// if its shaders can store to buffers).
fn drew(&mut self, p: Option<&HostPipeline>) {
if p.is_none_or(|p| p.stages.iter().any(|stage| stage.translated().stores)) {
self.batch = None;
}
}
/// The title waits for earlier work (a barrier or a pipeline sync).
pub(crate) fn synced(&mut self) {
if self.batch.as_ref().is_some_and(|b| b.wrote) {
self.batch = None;
}
}
/// Where the clamped record of `kind` at `arguments` of the copy `args`
/// is, from the open batch or a new one ([`batches`]). `None` if it
/// cannot be clamped in one.
fn batched(
&mut self,
list: &HostList,
args: &std::sync::Arc<crate::shadow::Shadow>,
arguments: u64,
kind: u32,
) -> Option<(ID3D12Resource, u64)> {
const BATCH_USE: usize = usize::MAX - 0x78;
const DISPATCH: u32 = 2;
// Another host list, or this one after its reset ([`windows`]).
if list.first_use(BATCH_USE) {
self.batch = None;
}
// A draw after dispatches waits for them.
if kind != DISPATCH {
self.synced();
}
let (id, offset) = (args.id(), args.offset(arguments));
if let Some(b) = &mut self.batch
&& let Some(at) = b.table.place(id, offset, kind)
{
return Some((b.scratch.clone(), at));
}
self.batch = None;
let (mut table, scratch) = crate::indirect::open_batch(list, (id, &args.buffer), &argument_copies(args))?;
self.host_pipeline = 0;
self.host_roots[1] = 0;
let at = table.place(id, offset, kind)?;
self.batch = Some(Batch { table, scratch: scratch.clone(), wrote: false });
Some((scratch, at))
}
/// Indirect execution of a one-argument signature of `kind` (PC
/// numbering: 0 draw, 1 indexed draw, 2 dispatch, 10 mesh dispatch) with
/// records `stride` bytes apart at title address `arguments`, and the
@@ -2905,6 +3032,16 @@ impl State {
why("mesh draws are switched off (VICTORY_MESH_TEST=skip)");
return false;
}
// One record without a count: clamped in a batch (`batches`).
let batch = if batches() > 0 && max == 1 && count.is_none() {
args.refresh(list, arguments, len);
self.batched(list, &args, arguments, kind)
} else {
None
};
if batch.is_some() {
self.window = None;
}
// A window of records clamped in one pass (`windows`): for single
// records without a count buffer, drawn by shaders that store to no
// buffer. Anything else ends the window: it uses the scratch buffer
@@ -2928,7 +3065,7 @@ impl State {
.filter(|w| same(w) && offset >= w.base)
.filter(|w| (offset - w.base) % u64::from(w.step) == 0 && (offset - w.base) / u64::from(w.step) < u64::from(w.records))
.map(|w| (w.scratch.clone(), offset - w.base));
let (bounded, at) = match hit {
let (bounded, at) = match batch.or(hit) {
Some(hit) => hit,
None => {
// A record a short step after the last window's last one
@@ -3018,6 +3155,11 @@ impl State {
if kind == MESH {
self.mesh_scissor(list, false);
}
if kind == DISPATCH {
self.buffers_written();
} else {
self.drew(Some(p));
}
// The scratch buffer stays readable as arguments: the next draw of
// the window reads it with no barrier, and the next clamp moves it
// back (`crate::indirect`).