| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- //! `victory replay`: a captured frame run again (docs/replay.md).
- use std::path::PathBuf;
- use anyhow::{Context, Result};
- use clap::{Args, ValueEnum};
- use victory_hle::replay::play::{self, Options};
- #[derive(Clone, Copy, ValueEnum)]
- enum Timing {
- /// Each list.
- Lists,
- /// Each command (adds some time of its own).
- Detailed,
- }
- #[derive(Clone, Copy, ValueEnum)]
- enum Buffers {
- AsRecorded,
- /// All in the card's memory.
- Vram,
- /// All in the PC's memory.
- Sysmem,
- }
- #[derive(Args)]
- pub(crate) struct Replay {
- /// The folder given as VICTORY_RECORD (its last capture) or a capture.
- folder: PathBuf,
- /// How often the frame is run.
- #[arg(long, default_value_t = 5)]
- loops: u32,
- /// Do not put back, between runs, what a run changed.
- #[arg(long)]
- no_restore: bool,
- #[arg(long, value_enum, default_value_t = Timing::Lists)]
- timing: Timing,
- /// Pipelines listed with detailed timing.
- #[arg(long, default_value_t = 20)]
- top: usize,
- /// Where the picture goes (default: replay.png in the capture).
- #[arg(long)]
- image: Option<PathBuf>,
- /// Run the frame unchanged first, and print both.
- #[arg(long)]
- compare: bool,
- /// Leave out unordered-access barriers.
- #[arg(long)]
- no_barriers: bool,
- /// Join barrier calls with no work between them.
- #[arg(long)]
- merge_barriers: bool,
- /// Leave out the argument clamps: what they bound is kept from a first run.
- #[arg(long)]
- skip_clamps: bool,
- /// The argument clamps in batches, as VICTORY_INDIRECT_BATCH does (2:
- /// a wait before every indirect dispatch).
- #[arg(long, num_args = 0..=1, default_missing_value = "1", default_value_t = 0)]
- batch_clamps: u32,
- /// Leave out the hash and compare passes over card buffers.
- #[arg(long)]
- skip_hash_passes: bool,
- /// Where buffers lie.
- #[arg(long, value_enum, default_value_t = Buffers::AsRecorded)]
- buffers: Buffers,
- /// With --buffers, only the buffers all of these match: `name=<text>`,
- /// `min=<bytes>`, `max=<bytes>`, `seen=<No, Cached, Combined>`,
- /// `id=<a>..<b>`, or `!` and one of those (may be given several times).
- #[arg(long)]
- only_buffers: Vec<String>,
- /// One ExecuteCommandLists for the whole frame.
- #[arg(long)]
- one_submit: bool,
- /// Let copies of what the CPU wrote also overwrite what the card wrote in
- /// the replay (as the recorded commands say).
- #[arg(long)]
- no_merge: bool,
- /// A full wait after every draw, dispatch, copy and clear.
- #[arg(long)]
- wait_all: bool,
- /// Each list sent on its own, and waited for.
- #[arg(long)]
- wait_lists: bool,
- /// Run only these lists, by their place in the frame (`3..7`, or `5`);
- /// of the others only barriers.
- #[arg(long)]
- only: Option<String>,
- /// Leave out what runs a pipeline whose name or DXIL hash has this in it
- /// (may be given several times).
- #[arg(long = "skip-shader")]
- skip_shader: Vec<String>,
- /// Find the commands that make identical runs differ: each list is run
- /// this often from the same memory.
- #[arg(long, num_args = 0..=1, default_missing_value = "3")]
- diverge: Option<u32>,
- /// Find where the picture's variants are decided: from each list on, the
- /// rest of the frame is run this often from the same memory.
- #[arg(long, num_args = 0..=1, default_missing_value = "5")]
- diverge_picture: Option<u32>,
- /// Run each draw of one list (--lists) whose pipeline's name has this in
- /// it alone, --diverge times: how many outcomes it has.
- #[arg(long)]
- draws: Option<String>,
- /// Say which lists (with --lists: commands) change words of buffers:
- /// `<resource>:<offset>:<bytes>` (hexadecimal; repeatable).
- #[arg(long)]
- watch: Vec<String>,
- /// With --diverge-picture and one list in --lists: its commands (`0..900`)
- /// and how many are stepped over at a time.
- #[arg(long)]
- commands: Option<String>,
- #[arg(long, default_value_t = 1)]
- step: usize,
- /// With --diverge and --diverge-picture: only these lists (`3..7`).
- #[arg(long)]
- lists: Option<String>,
- /// With --diverge: the most commands named of one list.
- #[arg(long, default_value_t = 4)]
- roots: u32,
- /// Name resources the frame changes that the replay does not put back.
- #[arg(long)]
- check_written: bool,
- /// Run the frame twice and say which words the copies of the CPU's sides
- /// of card buffers bring that the card holds another value of, and what
- /// shaders wrote that was not declared as written.
- #[arg(long)]
- upcopies: bool,
- /// Name what the heap slots hold that a command's constants name:
- /// `<list>:<command>`.
- #[arg(long)]
- slots: Option<String>,
- /// Only write the DXIL of pipelines with this in their name to the
- /// capture's `shaders` folder.
- #[arg(long)]
- dump: Option<String>,
- /// Only write the frame's buffers and the commands of these lists (`3..7`).
- #[arg(long)]
- listing: Option<String>,
- /// Only write what the dispatches of these lists have bound (`3..7`).
- #[arg(long)]
- flow: Option<String>,
- /// With --flow: draws too.
- #[arg(long)]
- flow_draws: bool,
- /// With --flow: heap slots shown from each place named.
- #[arg(long, default_value_t = 40)]
- flow_slots: u32,
- /// Run the frame once and save textures as pictures in this folder, with
- /// what values they hold (see --texture, --until).
- #[arg(long)]
- images: Option<PathBuf>,
- /// With --images: `<resource>[.<subresource>]` (repeatable); none: all
- /// that the frame's shaders write.
- #[arg(long)]
- texture: Vec<String>,
- /// With --images and no --texture: from this many texels on.
- #[arg(long, default_value_t = 4096)]
- least: u64,
- /// With --images: the widest picture.
- #[arg(long, default_value_t = 960)]
- width: u32,
- /// With --images: the values too.
- #[arg(long)]
- raw: bool,
- /// The last command that runs, `<list>:<command>`; after it only barriers.
- #[arg(long)]
- until: Option<String>,
- /// Say which commands change the pixel: `<x>,<y>` of the presented
- /// picture, or `<resource>[.<subresource>]:<x>,<y>` of a texture
- /// (repeatable). With --lists, only those are looked into.
- #[arg(long)]
- pixel: Vec<String>,
- /// With --pixel: the size of the picture the points are of (`1280x720`),
- /// if it is another than the presented one's.
- #[arg(long)]
- of: Option<String>,
- /// With --pixel: depth targets too.
- #[arg(long)]
- depth: bool,
- /// With --pixel: leave out textures that shaders may write (unordered
- /// access).
- #[arg(long)]
- targets_only: bool,
- /// With --pixel: indirect calls whose draws are run one by one:
- /// `<list>:<command>` (repeatable).
- #[arg(long)]
- split: Vec<String>,
- /// Use the D3D12 debug layer (slow).
- #[arg(long)]
- debug_layer: bool,
- }
- /// `3..7` (both included) or `5`.
- fn range(text: &str) -> Option<(u32, u32)> {
- match text.split_once("..") {
- Some((a, b)) => Some((a.trim().parse().ok()?, b.trim().parse().ok()?)),
- None => text.trim().parse().ok().map(|n| (n, n)),
- }
- }
- /// Two numbers with `by` between them.
- fn pair(text: &str, by: char) -> Option<(u32, u32)> {
- let (a, b) = text.split_once(by)?;
- Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
- }
- pub(crate) fn run(r: Replay) -> Result<()> {
- if let Some(text) = &r.dump {
- for line in play::dump(&r.folder, text)? {
- println!("{line}");
- }
- return Ok(());
- }
- if let Some(text) = &r.listing {
- let lists = range(text).with_context(|| format!("--listing {text}"))?;
- return play::listing(&r.folder, lists, &mut |line| println!("{line}"));
- }
- if let Some(text) = &r.flow {
- let lists = range(text).with_context(|| format!("--flow {text}"))?;
- return play::look::flow(&r.folder, lists, r.flow_draws, r.flow_slots, &mut |line| println!("{line}"));
- }
- let until = match &r.until {
- Some(text) => Some(
- text.split_once(':')
- .and_then(|(l, c)| Some((l.parse().ok()?, c.parse().ok()?)))
- .with_context(|| format!("--until {text}: not <list>:<command>"))?,
- ),
- None => None,
- };
- let only = match &r.only {
- Some(text) => Some(
- range(text)
- .with_context(|| format!("--only {text}: not a list or a range of lists (3..7)"))?,
- ),
- None => None,
- };
- let options = Options {
- loops: r.loops.max(1),
- restore: !r.no_restore,
- detailed: matches!(r.timing, Timing::Detailed),
- top: r.top,
- image: r.image,
- buffers: match r.buffers {
- Buffers::AsRecorded => play::Buffers::AsRecorded,
- Buffers::Vram => play::Buffers::Card,
- Buffers::Sysmem => play::Buffers::Pc,
- },
- only_buffers: r.only_buffers,
- no_barriers: r.no_barriers,
- merge_barriers: r.merge_barriers,
- skip_clamps: r.skip_clamps,
- batch_clamps: r.batch_clamps,
- skip_hash_passes: r.skip_hash_passes,
- one_submit: r.one_submit,
- merge: !r.no_merge,
- wait_all: r.wait_all,
- wait_lists: r.wait_lists,
- only,
- until,
- skip_shaders: r.skip_shader,
- compare: r.compare,
- debug: r.debug_layer,
- };
- if let Some(text) = &r.slots {
- let (list, command) = text
- .split_once(':')
- .and_then(|(l, c)| Some((l.parse().ok()?, c.parse().ok()?)))
- .with_context(|| format!("--slots {text}: not <list>:<command>"))?;
- let player = play::Player::load(&r.folder, &options)?;
- return player.slots(list, command, &mut |line| println!("{line}"));
- }
- if let Some(folder) = &r.images {
- let mut which = Vec::new();
- for text in &r.texture {
- let (id, sub) = text.split_once('.').unwrap_or((text, "0"));
- let bad = || format!("--texture {text}");
- which.push((id.parse().with_context(bad)?, sub.parse().with_context(bad)?));
- }
- let images = play::look::Images {
- which,
- least: r.least,
- folder: folder.clone(),
- width: r.width,
- raw: r.raw,
- };
- let mut player = play::Player::load(&r.folder, &options)?;
- return player.images(&options, &images, &mut |line| println!("{line}"));
- }
- if !r.pixel.is_empty() {
- let mut player = play::Player::load(&r.folder, &options)?;
- let mut pixels = play::pixel::Pixels {
- depth: r.depth,
- written: !r.targets_only,
- ..Default::default()
- };
- if let Some(text) = &r.lists {
- pixels.lists = Some(range(text).with_context(|| format!("--lists {text}"))?);
- }
- let of = match &r.of {
- Some(text) => Some(pair(text, 'x').with_context(|| format!("--of {text}"))?),
- None => None,
- };
- for text in &r.pixel {
- let bad = || format!("--pixel {text}");
- match text.split_once(':') {
- Some((what, at)) => {
- let (id, sub) = what.split_once('.').unwrap_or((what, "0"));
- let (x, y) = pair(at, ',').with_context(bad)?;
- let id = id.parse().with_context(bad)?;
- let sub = sub.parse().with_context(bad)?;
- pixels.texels.push((id, sub, x, y));
- }
- None => {
- let (mut x, mut y) = pair(text, ',').with_context(bad)?;
- if let (Some((w, h)), Some((pw, ph))) = (of, player.picture_size()) {
- x = x * pw / w.max(1);
- y = y * ph / h.max(1);
- }
- pixels.points.push((x, y));
- }
- }
- }
- for text in &r.split {
- pixels
- .split
- .push(pair(text, ':').with_context(|| format!("--split {text}"))?);
- }
- return player.pixel(&options, &pixels, &mut |line| println!("{line}"));
- }
- if r.check_written {
- let mut player = play::Player::load(&r.folder, &options)?;
- return player.check_written(&mut |line| println!("{line}"));
- }
- if r.upcopies {
- let mut player = play::Player::load(&r.folder, &options)?;
- return player.upcopies(&options, &mut |line| println!("{line}"));
- }
- let mut watched = Vec::new();
- for text in &r.watch {
- let mut n = text
- .split(':')
- .map(|p| u64::from_str_radix(p.trim_start_matches("0x"), 16));
- let mut next = || {
- n.next()
- .and_then(Result::ok)
- .with_context(|| format!("--watch {text}"))
- };
- let id = text
- .split(':')
- .next()
- .and_then(|p| p.parse().ok())
- .with_context(|| format!("--watch {text}"))?;
- let _ = next();
- watched.push((id, next()?, next()?));
- }
- if let Some(runs) = r
- .diverge
- .or(r.diverge_picture)
- .or((!watched.is_empty()).then_some(2))
- {
- let lists = match &r.lists {
- Some(text) => Some(range(text).with_context(|| format!("--lists {text}"))?),
- None => None,
- };
- let search = play::diverge::Search {
- runs: runs.max(2),
- lists,
- roots: r.roots,
- wait_all: options.wait_all,
- options: options.clone(),
- };
- let mut player = play::Player::load(&r.folder, &options)?;
- if !watched.is_empty() {
- return player.watch(&search, &watched, &mut |line| println!("{line}"));
- }
- if let Some(text) = &r.draws {
- return player.diverge_draws(&search, text, &mut |line| println!("{line}"));
- }
- if r.diverge_picture.is_some() {
- let commands = match &r.commands {
- Some(text) => Some(range(text).with_context(|| format!("--commands {text}"))?),
- None => None,
- };
- return player
- .diverge_picture(&search, commands, r.step, &mut |line| println!("{line}"));
- }
- return player.diverge(&search, &mut |line| println!("{line}"));
- }
- let reports = play::replay(&r.folder, &options)?;
- for (what, report) in &reports {
- println!("{}", report.text(what));
- }
- if let [(_, before), (_, after)] = &reports[..] {
- println!("{:+.2} ms", after.median_ms() - before.median_ms());
- }
- Ok(())
- }
- #[cfg(test)]
- mod tests {
- #[test]
- fn ranges() {
- assert_eq!(super::range("3..7"), Some((3, 7)));
- assert_eq!(super::range("5"), Some((5, 5)));
- assert_eq!(super::range("x"), None);
- assert_eq!(super::pair("12,7", ','), Some((12, 7)));
- assert_eq!(super::pair("12", ','), None);
- assert_eq!(super::range("1..x"), None);
- }
- }
|