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 da89c8a5a9
commit ce3c03a801
9 files changed
+1492 -53

No files matched your search

+378 -1
View File
@@ -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()));
}
}
+233 -49
View File
@@ -65,9 +65,13 @@ pub struct Options {
/// Where the picture goes (default: `replay.png` in the capture).
pub image: Option<PathBuf>,
pub buffers: Buffers,
/// Of those, only the ones all of these match ([`chosen`]).
pub only_buffers: Vec<String>,
pub no_barriers: bool,
pub merge_barriers: bool,
pub skip_clamps: bool,
/// The clamps in batches (`edit::batch_clamps`); 2: carefully.
pub batch_clamps: u32,
pub skip_hash_passes: bool,
pub one_submit: bool,
/// Copies of what the CPU wrote leave alone what the card wrote in this
@@ -97,9 +101,11 @@ impl Default for Options {
top: 20,
image: None,
buffers: Buffers::AsRecorded,
only_buffers: Vec::new(),
no_barriers: false,
merge_barriers: false,
skip_clamps: false,
batch_clamps: 0,
skip_hash_passes: false,
one_submit: false,
merge: true,
@@ -118,6 +124,7 @@ impl Options {
self.no_barriers
|| self.merge_barriers
|| self.skip_clamps
|| self.batch_clamps > 0
|| self.skip_hash_passes
|| self.one_submit
|| self.wait_all
@@ -132,6 +139,7 @@ impl Options {
no_barriers: false,
merge_barriers: false,
skip_clamps: false,
batch_clamps: 0,
skip_hash_passes: false,
one_submit: false,
wait_all: false,
@@ -245,6 +253,32 @@ impl Report {
}
}
/// Whether `--only-buffers` means a buffer: all of `only` match it.
/// `name=<text in its name>`, `min=<bytes>`, `max=<bytes>`,
/// `seen=<how the CPU saw it>`, `id=<a>..<b>`; with `!` in front: that does not.
fn chosen(r: &wire::Resource, only: &[String]) -> bool {
only.iter().all(|f| {
let (no, f) = match f.strip_prefix('!') {
Some(f) => (true, f),
None => (false, f.as_str()),
};
let (key, value) = f.split_once('=').unwrap_or(("name", f));
let number = value.parse().unwrap_or(0u64);
let is = match key {
"min" => r.desc.width >= number,
"max" => r.desc.width <= number,
"seen" => {
matches!(&r.place, Place::Committed { props, .. } if format!("{:?}", gpu::seen(props)).eq_ignore_ascii_case(value))
}
"id" => value.split_once("..").is_some_and(|(a, b)| {
(a.parse().unwrap_or(0)..b.parse().unwrap_or(u32::MAX)).contains(&r.id)
}),
_ => r.name.contains(value),
};
is != no
})
}
fn median(v: &[f64]) -> f64 {
let mut v = v.to_vec();
v.sort_by(f64::total_cmp);
@@ -311,20 +345,65 @@ pub fn dump(folder: &Path, text: &str) -> Result<Vec<String>> {
Ok(out)
}
/// Writes, one line each, the commands of lists (by their place in the
/// frame), and before them the frame's buffers: `--listing`.
pub fn listing(folder: &Path, lists: (u32, u32), line: &mut dyn FnMut(&str)) -> Result<()> {
let frame = Frame::read(&find(folder)?.join("frame.bin"))?;
let pipelines: HashMap<u32, &str> =
frame.pipelines.iter().map(|p| (p.id, p.name.as_str())).collect();
for r in &frame.resources {
if r.desc.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0 {
let place = match &r.place {
Place::Committed { props, .. } => format!("{:?}", gpu::seen(props)),
other => format!("{other:?}"),
};
line(&format!("buffer {} {} bytes, {place}: {}", r.id, r.desc.width, r.name));
}
}
let mut place = 0;
for (i, s) in frame.submits.iter().enumerate() {
for &l in s.lists.iter().filter(|&&l| l != MISSING) {
let here = place;
place += 1;
if here < lists.0 || here > lists.1 {
continue;
}
line(&format!("list {here} of submission {i}"));
for (n, c) in Cmd::all(&frame.lists[l as usize].commands.0)?.iter().enumerate() {
match c {
Cmd::Pipeline { pso } => {
line(&format!("{n} Pipeline {pso} {}", pipelines.get(pso).copied().unwrap_or_default()));
}
c => line(&format!("{n} {c:?}")),
}
}
}
}
Ok(())
}
/// Loads a capture and runs it: the whole of `victory replay`. The reports,
/// with what each is of.
pub fn replay(folder: &Path, options: &Options) -> Result<Vec<(String, Report)>> {
let mut player = Player::load(folder, options)?;
let mut out = Vec::new();
if options.compare && options.edited() {
// Turn by turn: what else the machine does falls on both.
let image = Some(player.folder.join("replay-unchanged.png"));
out.push((
"unchanged".into(),
player.run(&Options {
image,
..options.plain()
})?,
));
let plain = Options {
image,
..options.plain()
};
let mut a = player.prepare(&plain)?;
let mut b = player.prepare(options)?;
let (mut stamps_a, mut stamps_b) = (Vec::new(), Vec::new());
for _ in 0..options.loops.max(1) {
stamps_a = player.once(&plain, &mut a)?;
stamps_b = player.once(options, &mut b)?;
}
out.push(("unchanged".into(), player.finish(&plain, a, &stamps_a)?));
out.push(("with the experiments".into(), player.finish(options, b, &stamps_b)?));
return Ok(out);
}
let what = if options.edited() {
"with the experiments"
@@ -468,7 +547,7 @@ impl Player {
*at = Instant::now();
};
let mut at = Instant::now();
player.make_resources(options.buffers)?;
player.make_resources(options.buffers, &options.only_buffers)?;
step("resources", &mut at);
player.make_pipelines()?;
step("pipelines", &mut at);
@@ -558,7 +637,7 @@ impl Player {
}
}
fn make_resources(&mut self, buffers: Buffers) -> Result<()> {
fn make_resources(&mut self, buffers: Buffers, only: &[String]) -> Result<()> {
// A heap of memory the CPU sees becomes one the card may write too:
// what the CPU wrote is put back by the card.
let props = |p: &wire::HeapProps, buffer: bool| -> D3D12_HEAP_PROPERTIES {
@@ -602,7 +681,7 @@ impl Player {
for h in &self.frame.heaps {
let desc = D3D12_HEAP_DESC {
SizeInBytes: h.size,
Properties: props(&h.props, used_by_buffers_only.contains(&h.id)),
Properties: props(&h.props, only.is_empty() && used_by_buffers_only.contains(&h.id)),
Alignment: h.alignment,
Flags: D3D12_HEAP_FLAGS(h.flags & !DROP),
};
@@ -613,8 +692,15 @@ impl Player {
self._heaps.extend(heap.map(|heap| (h.id, heap)));
}
let mut failed = 0;
let mut moves: BTreeMap<String, (u32, u64)> = BTreeMap::new();
for (index, r) in self.frame.resources.iter().enumerate() {
let is_buffer = r.desc.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0;
let moved = is_buffer && chosen(r, only);
if moved && let Place::Committed { props, .. } = &r.place {
let seen = gpu::seen(props);
let m = moves.entry(format!("{seen:?}")).or_insert((0u32, 0u64));
*m = (m.0 + 1, m.1 + r.desc.width);
}
let desc = gpu::to_desc(&r.desc);
let mut made: Option<ID3D12Resource> = None;
let clear: Option<D3D12_CLEAR_VALUE> = r.clear.map(|c| {
@@ -637,7 +723,7 @@ impl Player {
None => Err(windows::core::Error::empty()),
},
Place::Committed { props: p, flags } => {
let p = props(p, is_buffer);
let p = props(p, moved);
let state = if p.Type == D3D12_HEAP_TYPE_READBACK {
D3D12_RESOURCE_STATE_COPY_DEST
} else {
@@ -700,6 +786,11 @@ impl Player {
if failed > 0 {
self.note(format!("{failed} resources could not be made again"));
}
if buffers != Buffers::AsRecorded {
self.notes.push(format!(
"--buffers is of, by how the CPU saw them (buffers, bytes): {moves:?}"
));
}
Ok(())
}
@@ -1800,6 +1891,17 @@ impl Player {
.push(format!("{} pipelines match --skip-shader", skip.len()));
}
let hash = self.pipelines_named(COMPARE);
let clamp = self.pipelines_named(CLAMP);
let compute: HashSet<u32> = self
.frame
.pipelines
.iter()
.filter(|p| p.parts.iter().any(|part| matches!(part, wire::Part::Code { kind, .. } if *kind == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS.0)))
.map(|p| p.id)
.collect();
let mut batched = edit::Batched::default();
let mut jobs = Vec::new();
let mut scratches = Vec::new();
let (mut skipped, mut hashed, mut uavs, mut merged) = (0, 0, 0, 0);
let mut place = 0;
let mut out = Vec::new();
@@ -1818,6 +1920,24 @@ impl Player {
{
cmds = edit::skip_clamps(cmds, c);
}
if o.batch_clamps > 0 {
let scratch = edit::BATCH_SCRATCH - here;
let done;
(cmds, done) = edit::batch_clamps(
cmds,
&clamp,
&compute,
scratch,
o.batch_clamps > 1,
&mut jobs,
);
batched.clamps += done.clamps;
batched.batches += done.batches;
batched.jobs += done.jobs;
if done.scratch > 0 {
scratches.push((scratch, done.scratch));
}
}
let mut n;
if !skip.is_empty() {
(cmds, n) = edit::skip_pipelines(cmds, &skip);
@@ -1839,6 +1959,15 @@ impl Player {
}
out.push(lists);
}
if o.batch_clamps > 0 {
report.notes.push(format!(
"--batch-clamps: {} clamp passes became {}, of {} records",
batched.clamps, batched.batches, batched.jobs
));
if let Err(e) = self.make_batches(&jobs, &scratches) {
report.notes.push(format!("--batch-clamps failed: {e:#}"));
}
}
if !skip.is_empty() {
report
.notes
@@ -1862,6 +1991,44 @@ impl Player {
out
}
/// What lists edited by [`edit::batch_clamps`] name: the pass, its
/// tables and scratch buffers.
fn make_batches(&mut self, jobs: &[u8], scratches: &[(u32, u64)]) -> Result<()> {
let (root, pso) = crate::indirect::batch_for(&self.device)?;
self.roots.insert(edit::BATCH_ROOT, root);
self.pipelines.insert(edit::BATCH, pso);
let (table, data) = gpu::buffer(
&self.device,
D3D12_HEAP_TYPE_UPLOAD,
jobs.len().max(4) as u64,
D3D12_RESOURCE_FLAG_NONE,
)?;
// SAFETY: a mapping of as many bytes.
unsafe { std::ptr::copy_nonoverlapping(jobs.as_ptr(), data.cast(), jobs.len()) };
let mut all = vec![(edit::BATCH_JOBS, table)];
for &(id, bytes) in scratches {
let (scratch, _) = gpu::buffer(
&self.device,
D3D12_HEAP_TYPE_DEFAULT,
bytes.next_multiple_of(64 << 10),
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS,
)?;
all.push((id, scratch));
}
for (id, res) in all {
// SAFETY: a live buffer.
let va = unsafe { res.GetGPUVirtualAddress() };
let object = Object {
res,
va,
index: 0,
subs: 1,
};
self.objects.insert(id, object);
}
Ok(())
}
/// The clamp passes of each list of the frame, in order, and their
/// results in [`RESULTS`] (by a run that keeps them).
fn clamp_results(&mut self, report: &mut Report) -> Result<Vec<Vec<Clamp>>> {
@@ -2126,7 +2293,8 @@ impl Player {
wait: bool,
) -> Vec<Stamp> {
let version = self.version_of.get(submit).copied().unwrap_or(0);
let clamps = self.pipelines_named(CLAMP);
let mut clamps = self.pipelines_named(CLAMP);
clamps.insert(edit::BATCH);
let compares = self.pipelines_named(COMPARE);
let mesh: HashSet<u32> = self
.frame
@@ -2713,6 +2881,16 @@ impl Player {
/// Runs the frame as the options say.
pub fn run(&mut self, o: &Options) -> Result<Report> {
let mut made = self.prepare(o)?;
let mut stamps = Vec::new();
for _ in 0..o.loops.max(1) {
stamps = self.once(o, &mut made)?;
}
self.finish(o, made, &stamps)
}
/// The lists for a run as the options say.
fn prepare(&mut self, o: &Options) -> Result<(Sends, Report)> {
let mut report = Report {
notes: self.notes.clone(),
..Default::default()
@@ -2724,45 +2902,51 @@ impl Player {
Vec::new()
};
let submits = self.edited(o, &mut report, &clamps);
let built = self.build(&submits, o)?;
let mut stamps = Vec::new();
for _ in 0..o.loops.max(1) {
self.restore(o.restore)?;
self.fresh = false;
stamps = self.send(&built, o)?;
if let Some((db, _, hash, _)) = self.picture_now()? {
report.pictures.push((db, hash));
}
let ms = |a: u32, b: u32| {
stamps[b as usize].saturating_sub(stamps[a as usize]) as f64 * 1e3 / self.frequency
};
let lists: Vec<&Built> = built.steps.iter().flat_map(|(_, l)| l).collect();
let (Some(first), Some(last)) = (lists.first(), lists.last()) else {
bail!("no lists to run");
};
let delta = built.steps.iter().filter_map(|(d, _)| d.as_ref());
report.delta_ms.push(
delta
.filter(|d| d.first > first.first)
.map(|d| ms(d.first, d.last))
.sum(),
);
report.frame_ms.push(ms(first.first, last.last));
let each = |l: &[Built]| match (l.first(), l.last()) {
(Some(a), Some(b)) => ms(a.first, b.last),
_ => 0.0,
};
report.submits = built
.steps
.iter()
.map(|(_, l)| (l.len(), each(l)))
.collect();
report
.busy_ms
.push(report.submits.iter().map(|s| s.1).sum());
Ok((self.build(&submits, o)?, report))
}
/// One run of the frame, noted in the report; its timestamps.
fn once(&mut self, o: &Options, (built, report): &mut (Sends, Report)) -> Result<Vec<u64>> {
self.restore(o.restore)?;
self.fresh = false;
let stamps = self.send(built, o)?;
if let Some((db, _, hash, _)) = self.picture_now()? {
report.pictures.push((db, hash));
}
let ms = |a: u32, b: u32| {
stamps[b as usize].saturating_sub(stamps[a as usize]) as f64 * 1e3 / self.frequency
};
let lists: Vec<&Built> = built.steps.iter().flat_map(|(_, l)| l).collect();
let (Some(first), Some(last)) = (lists.first(), lists.last()) else {
bail!("no lists to run");
};
let delta = built.steps.iter().filter_map(|(d, _)| d.as_ref());
report.delta_ms.push(
delta
.filter(|d| d.first > first.first)
.map(|d| ms(d.first, d.last))
.sum(),
);
report.frame_ms.push(ms(first.first, last.last));
let each = |l: &[Built]| match (l.first(), l.last()) {
(Some(a), Some(b)) => ms(a.first, b.last),
_ => 0.0,
};
report.submits = built
.steps
.iter()
.map(|(_, l)| (l.len(), each(l)))
.collect();
report
.busy_ms
.push(report.submits.iter().map(|s| s.1).sum());
Ok(stamps)
}
/// The report after the last run.
fn finish(&mut self, o: &Options, (built, mut report): (Sends, Report), stamps: &[u64]) -> Result<Report> {
if o.detailed {
self.detail(&built, &stamps, o, &mut report);
self.detail(&built, stamps, o, &mut report);
}
if self.merging {
let notes = self.merge_notes(o.loops)?;