//! `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, /// 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, /// 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, /// 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, /// 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, /// 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, /// 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, /// 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, /// Say which lists (with --lists: commands) change words of buffers: /// `::` (hexadecimal; repeatable). #[arg(long)] watch: Vec, /// 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, #[arg(long, default_value_t = 1)] step: usize, /// With --diverge and --diverge-picture: only these lists (`3..7`). #[arg(long)] lists: Option, /// 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, /// Name what the heap slots hold that a command's constants name: /// `:`. #[arg(long)] slots: Option, /// Only write the DXIL of pipelines with this in their name to the /// capture's `shaders` folder. #[arg(long)] dump: Option, /// 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)), } } 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(()); } 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, }, no_barriers: r.no_barriers, merge_barriers: r.merge_barriers, skip_clamps: r.skip_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, 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 :"))?; let player = play::Player::load(&r.folder, &options)?; return player.slots(list, command, &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}")); } 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::range("1..x"), None); } }