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:
1 parent
da89c8a5a9
commit
ce3c03a801
9 files changed
+1492
-53
No files matched your search
@@ -3,7 +3,7 @@
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::wire::{ALL, Barrier, Cmd, NONE, Va};
|
||||
use super::wire::{ALL, Barrier, Cmd, NONE, Va, slot};
|
||||
|
||||
/// The buffer the clamped arguments of all clamp passes lie in, in lists
|
||||
/// edited by [`clamps`].
|
||||
@@ -298,6 +298,276 @@ pub fn skip_clamps(cmds: Vec<Cmd>, clamps: &[Clamp]) -> Vec<Cmd> {
|
||||
out
|
||||
}
|
||||
|
||||
/// The root signature, pipeline and table of the batched clamp
|
||||
/// (`crate::indirect::batch`), and the scratch buffer it writes in the
|
||||
/// first list of the frame (the next lists' are the ids before it), in lists
|
||||
/// edited by [`batch_clamps`].
|
||||
pub const BATCH_ROOT: u32 = u32::MAX - 2;
|
||||
pub const BATCH: u32 = u32::MAX - 3;
|
||||
pub const BATCH_JOBS: u32 = u32::MAX - 4;
|
||||
pub const BATCH_SCRATCH: u32 = u32::MAX - 16;
|
||||
|
||||
/// What [`batch_clamps`] did to a list.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Batched {
|
||||
/// Clamp passes before; after; the records the latter clamp.
|
||||
pub clamps: u32,
|
||||
pub batches: u32,
|
||||
pub jobs: u32,
|
||||
/// The bytes of the scratch buffer.
|
||||
pub scratch: u64,
|
||||
}
|
||||
|
||||
/// A clamp batch being filled by [`batch_clamps`].
|
||||
struct Batch {
|
||||
/// Where its table is in the jobs, and its root views in the list.
|
||||
table: usize,
|
||||
at: usize,
|
||||
sources: Vec<u32>,
|
||||
/// By source, offset and kind: the job, and how often it is read.
|
||||
known: HashMap<(u32, u64, u32), (u32, u32)>,
|
||||
n: u32,
|
||||
base: u64,
|
||||
}
|
||||
|
||||
/// The list with its clamps done in batches, as `crate::draw` does with
|
||||
/// `VICTORY_INDIRECT_BATCH`: one pass at the first execution after a
|
||||
/// point where arguments may have changed (a wait for shader writes, with
|
||||
/// dispatches since the batch before it; or a draw, with dispatches since
|
||||
/// the last wait). `careful`: before indirect dispatches too, with
|
||||
/// dispatches since the last wait. `compute`: the pipelines that are compute
|
||||
/// ones. The tables go to `jobs` (the contents of [`BATCH_JOBS`]).
|
||||
pub fn batch_clamps(
|
||||
cmds: Vec<Cmd>,
|
||||
psos: &HashSet<u32>,
|
||||
compute: &HashSet<u32>,
|
||||
scratch: u32,
|
||||
careful: bool,
|
||||
jobs: &mut Vec<u8>,
|
||||
) -> (Vec<Cmd>, Batched) {
|
||||
use crate::indirect::batch::{JOBS, SLOT, SOURCES, TIMES, job};
|
||||
const DISPATCH: u32 = 2;
|
||||
let mut done = Batched::default();
|
||||
let scratches: HashSet<u32> = {
|
||||
let mut next = 0;
|
||||
let found = find_clamps(&cmds, psos, &mut next);
|
||||
found.iter().map(|c| c.scratch).collect()
|
||||
};
|
||||
if scratches.is_empty() {
|
||||
return (cmds, done);
|
||||
}
|
||||
let of_clamp =
|
||||
|b: &Barrier| matches!(b, Barrier::Transition { res, .. } if scratches.contains(res));
|
||||
let moved = |before, after| Barrier::Transition {
|
||||
flags: 0,
|
||||
res: scratch,
|
||||
sub: ALL,
|
||||
before,
|
||||
after,
|
||||
};
|
||||
// What the last clamp into a scratch buffer clamped: source, offset,
|
||||
// kind.
|
||||
let mut clamps: HashMap<u32, (u32, u64, u32)> = HashMap::new();
|
||||
let (mut bound, mut constants, mut views) = (NONE, [0u32; 6], [(NONE, 0u64); 5]);
|
||||
let mut out: Vec<Cmd> = Vec::with_capacity(cmds.len());
|
||||
let mut open: Option<Batch> = None;
|
||||
let (mut written, mut dirty, mut readable) = (true, true, false);
|
||||
let mut cmds = cmds.into_iter().peekable();
|
||||
while let Some(c) = cmds.next() {
|
||||
match &c {
|
||||
// A clamp's own commands: its root signature and all it sets.
|
||||
Cmd::RootSignature {
|
||||
graphics: false, ..
|
||||
} if matches!(cmds.peek(), Some(Cmd::Pipeline { pso }) if psos.contains(pso)) => {}
|
||||
Cmd::Pipeline { pso } => {
|
||||
bound = *pso;
|
||||
if !psos.contains(pso) {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
Cmd::Constants {
|
||||
graphics: false,
|
||||
values,
|
||||
offset,
|
||||
..
|
||||
} if psos.contains(&bound) => {
|
||||
for (i, v) in values.iter().enumerate() {
|
||||
if let Some(c) = constants.get_mut(*offset as usize + i) {
|
||||
*c = *v;
|
||||
}
|
||||
}
|
||||
}
|
||||
Cmd::RootView {
|
||||
graphics: false,
|
||||
param,
|
||||
va: Va::At { res, offset },
|
||||
..
|
||||
} if psos.contains(&bound) => {
|
||||
if let Some(v) = views.get_mut(*param as usize) {
|
||||
*v = (*res, *offset);
|
||||
}
|
||||
}
|
||||
Cmd::Dispatch { .. } if psos.contains(&bound) => {
|
||||
done.clamps += 1;
|
||||
let (source, offset) = views[1];
|
||||
clamps.insert(
|
||||
views[3].0,
|
||||
(source, offset + u64::from(constants[3]), constants[0]),
|
||||
);
|
||||
// What it was clamped for follows: a batch is open by then.
|
||||
let full =
|
||||
|o: &Batch| !o.sources.contains(&source) && o.sources.len() == SOURCES;
|
||||
if dirty && (constants[0] != DISPATCH || careful) || open.as_ref().is_some_and(full)
|
||||
{
|
||||
open = None;
|
||||
}
|
||||
if open.is_some() {
|
||||
continue;
|
||||
}
|
||||
done.batches += 1;
|
||||
let table = jobs.len();
|
||||
jobs.resize(table + 16 + 16 * 2048, 0);
|
||||
out.push(Cmd::Barriers {
|
||||
all: vec![
|
||||
Barrier::Uav {
|
||||
flags: 0,
|
||||
res: NONE,
|
||||
},
|
||||
moved(if readable { INDIRECT_ARGUMENT } else { 0 }, UNORDERED_ACCESS),
|
||||
],
|
||||
});
|
||||
readable = true;
|
||||
out.push(Cmd::RootSignature {
|
||||
graphics: false,
|
||||
root: BATCH_ROOT,
|
||||
});
|
||||
out.push(Cmd::Pipeline { pso: BATCH });
|
||||
let at = out.len();
|
||||
let mut view = |kind, param: usize, res, offset| {
|
||||
out.push(Cmd::RootView {
|
||||
graphics: false,
|
||||
kind,
|
||||
param: param as u32,
|
||||
va: Va::At { res, offset },
|
||||
});
|
||||
};
|
||||
for i in 0..SOURCES {
|
||||
view(slot::SRV, i, source, 0);
|
||||
}
|
||||
view(slot::SRV, SOURCES, BATCH_JOBS, table as u64);
|
||||
view(slot::UAV, SOURCES + 1, scratch, 0);
|
||||
view(slot::UAV, SOURCES + 2, views[4].0, views[4].1);
|
||||
out.push(Cmd::Dispatch {
|
||||
groups: [JOBS.div_ceil(64), 1, 1],
|
||||
});
|
||||
out.push(Cmd::Barriers {
|
||||
all: vec![moved(UNORDERED_ACCESS, INDIRECT_ARGUMENT)],
|
||||
});
|
||||
open = Some(Batch {
|
||||
table,
|
||||
at,
|
||||
sources: Vec::new(),
|
||||
known: HashMap::new(),
|
||||
n: 0,
|
||||
base: done.scratch,
|
||||
});
|
||||
(written, dirty) = (false, false);
|
||||
}
|
||||
Cmd::Barriers { all } => {
|
||||
if all.iter().any(of_clamp) {
|
||||
continue;
|
||||
}
|
||||
let waits = |b: &Barrier| matches!(b, Barrier::Uav { res: NONE, .. });
|
||||
if all.iter().any(waits) {
|
||||
if written {
|
||||
open = None;
|
||||
}
|
||||
dirty = false;
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
Cmd::Indirect {
|
||||
signature,
|
||||
most,
|
||||
args,
|
||||
at,
|
||||
count,
|
||||
count_at,
|
||||
} if clamps.contains_key(args) => {
|
||||
let (source, offset, kind) = clamps.get(args).copied().unwrap_or_default();
|
||||
let Some(o) = &mut open else {
|
||||
// Not clamped by any batch: left out, which shows.
|
||||
continue;
|
||||
};
|
||||
let slot = match o.sources.iter().position(|&s| s == source) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
o.sources.push(source);
|
||||
let i = o.sources.len() - 1;
|
||||
if let Some(Cmd::RootView {
|
||||
va: Va::At { res, .. },
|
||||
..
|
||||
}) = out.get_mut(o.at + i)
|
||||
{
|
||||
*res = source;
|
||||
}
|
||||
i
|
||||
}
|
||||
};
|
||||
let offset = offset + at;
|
||||
let entry = o.known.entry((source, offset, kind)).or_insert((o.n, 0));
|
||||
entry.1 += 1;
|
||||
let (i, times) = *entry;
|
||||
if i == o.n {
|
||||
o.n += 1;
|
||||
done.jobs += 1;
|
||||
done.scratch = o.base + u64::from(o.n * SLOT);
|
||||
// Wider than the live pass where its table is longer.
|
||||
if let Some(Cmd::Dispatch { groups }) = out.get_mut(o.at + SOURCES + 3) {
|
||||
groups[0] = groups[0].max(o.n.div_ceil(64));
|
||||
}
|
||||
}
|
||||
let place = o.base + u64::from(i * SLOT);
|
||||
let words = [
|
||||
job(slot as u32, kind, times.min(TIMES)),
|
||||
offset as u32,
|
||||
place as u32,
|
||||
0,
|
||||
];
|
||||
let mut put = |at: usize, w: u32| {
|
||||
jobs[o.table + at..o.table + at + 4].copy_from_slice(&w.to_le_bytes());
|
||||
};
|
||||
for (k, w) in words.into_iter().enumerate() {
|
||||
put(16 + 16 * i as usize + 4 * k, w);
|
||||
}
|
||||
put(0, o.n);
|
||||
out.push(Cmd::Indirect {
|
||||
signature: *signature,
|
||||
most: *most,
|
||||
args: scratch,
|
||||
at: place,
|
||||
count: *count,
|
||||
count_at: *count_at,
|
||||
});
|
||||
if compute.contains(&bound) {
|
||||
(written, dirty) = (true, true);
|
||||
}
|
||||
}
|
||||
Cmd::Dispatch { .. } => {
|
||||
(written, dirty) = (true, true);
|
||||
out.push(c);
|
||||
}
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
if readable {
|
||||
out.push(Cmd::Barriers {
|
||||
all: vec![moved(INDIRECT_ARGUMENT, 0)],
|
||||
});
|
||||
}
|
||||
(out, done)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -501,4 +771,111 @@ mod tests {
|
||||
[(RESULTS, 12), (RESULTS, 0), (6, 0), (RESULTS, 256 + 24)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clamps_are_batched() {
|
||||
use crate::indirect::batch::job;
|
||||
let clamp = |kind, offset, records, source| {
|
||||
let view = |param, res| Cmd::RootView {
|
||||
graphics: false,
|
||||
kind: 2,
|
||||
param,
|
||||
va: Va::At { res, offset: 0 },
|
||||
};
|
||||
vec![
|
||||
barriers(&[uav(0), moved(5, 0x200, 8)]),
|
||||
Cmd::RootSignature {
|
||||
graphics: false,
|
||||
root: 8,
|
||||
},
|
||||
pso(9),
|
||||
Cmd::Constants {
|
||||
graphics: false,
|
||||
param: 0,
|
||||
offset: 0,
|
||||
values: vec![kind, 32, records, offset, 0, 0],
|
||||
},
|
||||
view(1, source),
|
||||
view(2, source),
|
||||
view(3, 5),
|
||||
view(4, 6),
|
||||
go(),
|
||||
barriers(&[uav(0), moved(5, 8, 0x200)]),
|
||||
]
|
||||
};
|
||||
let run = |at| Cmd::Indirect {
|
||||
signature: 1,
|
||||
most: 1,
|
||||
args: 5,
|
||||
at,
|
||||
count: 0,
|
||||
count_at: 0,
|
||||
};
|
||||
// A dispatch, two indirect ones with a wait of the title's between
|
||||
// them, then draws: a window of two and another buffer's.
|
||||
let mut list = vec![pso(1), go()];
|
||||
list.extend(clamp(2, 0, 1, 70));
|
||||
list.extend([pso(1), run(0)]);
|
||||
list.extend(clamp(2, 64, 1, 70));
|
||||
list.extend([pso(1), run(0), barriers(&[uav(0)])]);
|
||||
list.extend(clamp(2, 64, 1, 70));
|
||||
list.extend([pso(1), run(0)]);
|
||||
list.extend(clamp(10, 128, 2, 70));
|
||||
list.extend([pso(2), run(0), run(32), run(32)]);
|
||||
list.extend(clamp(10, 256, 1, 71));
|
||||
list.extend([pso(2), run(0)]);
|
||||
let (clamps, compute) = (HashSet::from([9]), HashSet::from([1]));
|
||||
let table = 16 + 16 * 2048;
|
||||
for (careful, batches) in [(false, 3), (true, 4)] {
|
||||
let mut jobs = Vec::new();
|
||||
let (out, done) = batch_clamps(list.clone(), &clamps, &compute, 99, careful, &mut jobs);
|
||||
assert_eq!((done.clamps, done.batches, done.jobs), (5, batches, 6), "careful {careful}");
|
||||
assert_eq!(jobs.len(), table * batches as usize);
|
||||
let passes = out.iter().filter(|c| matches!(c, Cmd::Pipeline { pso: BATCH })).count();
|
||||
assert_eq!(passes, batches as usize);
|
||||
// Nothing of the clamps is left; the title's wait is.
|
||||
assert!(!out.contains(&pso(9)) && out.contains(&barriers(&[uav(0)])));
|
||||
let names_5 = |c: &Cmd| matches!(c, Cmd::Barriers { all } if all.iter().any(|b| matches!(b, Barrier::Transition { res: 5, .. })));
|
||||
assert!(!out.iter().any(names_5));
|
||||
let places: Vec<u64> = out
|
||||
.iter()
|
||||
.filter_map(|c| match c {
|
||||
Cmd::Indirect { args: 99, at, .. } => Some(*at),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(places, [0, 32, 64, 96, 128, 128, 160]);
|
||||
// The last table: two records, one read twice, then one of the
|
||||
// other buffer.
|
||||
let word = |i: usize| {
|
||||
let at = jobs.len() - table + 4 * i;
|
||||
u32::from_le_bytes(jobs[at..at + 4].try_into().unwrap())
|
||||
};
|
||||
assert_eq!(word(0), 3);
|
||||
let all: Vec<u32> = (4..16).map(word).collect();
|
||||
assert_eq!(
|
||||
all,
|
||||
[
|
||||
[job(0, 10, 1), 128, 96, 0],
|
||||
[job(0, 10, 2), 160, 128, 0],
|
||||
[job(1, 10, 1), 256, 160, 0]
|
||||
]
|
||||
.concat()
|
||||
);
|
||||
let sources: Vec<u32> = out
|
||||
.iter()
|
||||
.filter_map(|c| match c {
|
||||
Cmd::RootView { param: 1, va: Va::At { res, .. }, .. } => Some(*res),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(sources.last(), Some(&71));
|
||||
// It ends common, as it began.
|
||||
assert_eq!(out.last(), Some(&barriers(&[moved(99, 0x200, 0)])));
|
||||
}
|
||||
// A list without clamps is left as it is.
|
||||
let plain = vec![pso(1), go(), barriers(&[uav(0)])];
|
||||
let (out, done) = batch_clamps(plain.clone(), &clamps, &compute, 99, false, &mut Vec::new());
|
||||
assert_eq!((out, done), (plain, Batched::default()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user