replay.rs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //! `victory replay`: a captured frame run again (docs/replay.md).
  2. use std::path::PathBuf;
  3. use anyhow::{Context, Result};
  4. use clap::{Args, ValueEnum};
  5. use victory_hle::replay::play::{self, Options};
  6. #[derive(Clone, Copy, ValueEnum)]
  7. enum Timing {
  8. /// Each list.
  9. Lists,
  10. /// Each command (adds some time of its own).
  11. Detailed,
  12. }
  13. #[derive(Clone, Copy, ValueEnum)]
  14. enum Buffers {
  15. AsRecorded,
  16. /// All in the card's memory.
  17. Vram,
  18. /// All in the PC's memory.
  19. Sysmem,
  20. }
  21. #[derive(Args)]
  22. pub(crate) struct Replay {
  23. /// The folder given as VICTORY_RECORD (its last capture) or a capture.
  24. folder: PathBuf,
  25. /// How often the frame is run.
  26. #[arg(long, default_value_t = 5)]
  27. loops: u32,
  28. /// Do not put back, between runs, what a run changed.
  29. #[arg(long)]
  30. no_restore: bool,
  31. #[arg(long, value_enum, default_value_t = Timing::Lists)]
  32. timing: Timing,
  33. /// Pipelines listed with detailed timing.
  34. #[arg(long, default_value_t = 20)]
  35. top: usize,
  36. /// Where the picture goes (default: replay.png in the capture).
  37. #[arg(long)]
  38. image: Option<PathBuf>,
  39. /// Run the frame unchanged first, and print both.
  40. #[arg(long)]
  41. compare: bool,
  42. /// Leave out unordered-access barriers.
  43. #[arg(long)]
  44. no_barriers: bool,
  45. /// Join barrier calls with no work between them.
  46. #[arg(long)]
  47. merge_barriers: bool,
  48. /// Leave out the argument clamps: what they bound is kept from a first run.
  49. #[arg(long)]
  50. skip_clamps: bool,
  51. /// Leave out the hash and compare passes over card buffers.
  52. #[arg(long)]
  53. skip_hash_passes: bool,
  54. /// Where buffers lie.
  55. #[arg(long, value_enum, default_value_t = Buffers::AsRecorded)]
  56. buffers: Buffers,
  57. /// One ExecuteCommandLists for the whole frame.
  58. #[arg(long)]
  59. one_submit: bool,
  60. /// Let copies of what the CPU wrote also overwrite what the card wrote in
  61. /// the replay (as the recorded commands say).
  62. #[arg(long)]
  63. no_merge: bool,
  64. /// A full wait after every draw, dispatch, copy and clear.
  65. #[arg(long)]
  66. wait_all: bool,
  67. /// Each list sent on its own, and waited for.
  68. #[arg(long)]
  69. wait_lists: bool,
  70. /// Run only these lists, by their place in the frame (`3..7`, or `5`);
  71. /// of the others only barriers.
  72. #[arg(long)]
  73. only: Option<String>,
  74. /// Leave out what runs a pipeline whose name or DXIL hash has this in it
  75. /// (may be given several times).
  76. #[arg(long = "skip-shader")]
  77. skip_shader: Vec<String>,
  78. /// Find the commands that make identical runs differ: each list is run
  79. /// this often from the same memory.
  80. #[arg(long, num_args = 0..=1, default_missing_value = "3")]
  81. diverge: Option<u32>,
  82. /// Find where the picture's variants are decided: from each list on, the
  83. /// rest of the frame is run this often from the same memory.
  84. #[arg(long, num_args = 0..=1, default_missing_value = "5")]
  85. diverge_picture: Option<u32>,
  86. /// Run each draw of one list (--lists) whose pipeline's name has this in
  87. /// it alone, --diverge times: how many outcomes it has.
  88. #[arg(long)]
  89. draws: Option<String>,
  90. /// Say which lists (with --lists: commands) change words of buffers:
  91. /// `<resource>:<offset>:<bytes>` (hexadecimal; repeatable).
  92. #[arg(long)]
  93. watch: Vec<String>,
  94. /// With --diverge-picture and one list in --lists: its commands (`0..900`)
  95. /// and how many are stepped over at a time.
  96. #[arg(long)]
  97. commands: Option<String>,
  98. #[arg(long, default_value_t = 1)]
  99. step: usize,
  100. /// With --diverge and --diverge-picture: only these lists (`3..7`).
  101. #[arg(long)]
  102. lists: Option<String>,
  103. /// With --diverge: the most commands named of one list.
  104. #[arg(long, default_value_t = 4)]
  105. roots: u32,
  106. /// Name resources the frame changes that the replay does not put back.
  107. #[arg(long)]
  108. check_written: bool,
  109. /// Name what the heap slots hold that a command's constants name:
  110. /// `<list>:<command>`.
  111. #[arg(long)]
  112. slots: Option<String>,
  113. /// Only write the DXIL of pipelines with this in their name to the
  114. /// capture's `shaders` folder.
  115. #[arg(long)]
  116. dump: Option<String>,
  117. /// Use the D3D12 debug layer (slow).
  118. #[arg(long)]
  119. debug_layer: bool,
  120. }
  121. /// `3..7` (both included) or `5`.
  122. fn range(text: &str) -> Option<(u32, u32)> {
  123. match text.split_once("..") {
  124. Some((a, b)) => Some((a.trim().parse().ok()?, b.trim().parse().ok()?)),
  125. None => text.trim().parse().ok().map(|n| (n, n)),
  126. }
  127. }
  128. pub(crate) fn run(r: Replay) -> Result<()> {
  129. if let Some(text) = &r.dump {
  130. for line in play::dump(&r.folder, text)? {
  131. println!("{line}");
  132. }
  133. return Ok(());
  134. }
  135. let only = match &r.only {
  136. Some(text) => Some(
  137. range(text)
  138. .with_context(|| format!("--only {text}: not a list or a range of lists (3..7)"))?,
  139. ),
  140. None => None,
  141. };
  142. let options = Options {
  143. loops: r.loops.max(1),
  144. restore: !r.no_restore,
  145. detailed: matches!(r.timing, Timing::Detailed),
  146. top: r.top,
  147. image: r.image,
  148. buffers: match r.buffers {
  149. Buffers::AsRecorded => play::Buffers::AsRecorded,
  150. Buffers::Vram => play::Buffers::Card,
  151. Buffers::Sysmem => play::Buffers::Pc,
  152. },
  153. no_barriers: r.no_barriers,
  154. merge_barriers: r.merge_barriers,
  155. skip_clamps: r.skip_clamps,
  156. skip_hash_passes: r.skip_hash_passes,
  157. one_submit: r.one_submit,
  158. merge: !r.no_merge,
  159. wait_all: r.wait_all,
  160. wait_lists: r.wait_lists,
  161. only,
  162. skip_shaders: r.skip_shader,
  163. compare: r.compare,
  164. debug: r.debug_layer,
  165. };
  166. if let Some(text) = &r.slots {
  167. let (list, command) = text
  168. .split_once(':')
  169. .and_then(|(l, c)| Some((l.parse().ok()?, c.parse().ok()?)))
  170. .with_context(|| format!("--slots {text}: not <list>:<command>"))?;
  171. let player = play::Player::load(&r.folder, &options)?;
  172. return player.slots(list, command, &mut |line| println!("{line}"));
  173. }
  174. if r.check_written {
  175. let mut player = play::Player::load(&r.folder, &options)?;
  176. return player.check_written(&mut |line| println!("{line}"));
  177. }
  178. let mut watched = Vec::new();
  179. for text in &r.watch {
  180. let mut n = text
  181. .split(':')
  182. .map(|p| u64::from_str_radix(p.trim_start_matches("0x"), 16));
  183. let mut next = || {
  184. n.next()
  185. .and_then(Result::ok)
  186. .with_context(|| format!("--watch {text}"))
  187. };
  188. let id = text
  189. .split(':')
  190. .next()
  191. .and_then(|p| p.parse().ok())
  192. .with_context(|| format!("--watch {text}"))?;
  193. let _ = next();
  194. watched.push((id, next()?, next()?));
  195. }
  196. if let Some(runs) = r
  197. .diverge
  198. .or(r.diverge_picture)
  199. .or((!watched.is_empty()).then_some(2))
  200. {
  201. let lists = match &r.lists {
  202. Some(text) => Some(range(text).with_context(|| format!("--lists {text}"))?),
  203. None => None,
  204. };
  205. let search = play::diverge::Search {
  206. runs: runs.max(2),
  207. lists,
  208. roots: r.roots,
  209. wait_all: options.wait_all,
  210. options: options.clone(),
  211. };
  212. let mut player = play::Player::load(&r.folder, &options)?;
  213. if !watched.is_empty() {
  214. return player.watch(&search, &watched, &mut |line| println!("{line}"));
  215. }
  216. if let Some(text) = &r.draws {
  217. return player.diverge_draws(&search, text, &mut |line| println!("{line}"));
  218. }
  219. if r.diverge_picture.is_some() {
  220. let commands = match &r.commands {
  221. Some(text) => Some(range(text).with_context(|| format!("--commands {text}"))?),
  222. None => None,
  223. };
  224. return player
  225. .diverge_picture(&search, commands, r.step, &mut |line| println!("{line}"));
  226. }
  227. return player.diverge(&search, &mut |line| println!("{line}"));
  228. }
  229. let reports = play::replay(&r.folder, &options)?;
  230. for (what, report) in &reports {
  231. println!("{}", report.text(what));
  232. }
  233. if let [(_, before), (_, after)] = &reports[..] {
  234. println!("{:+.2} ms", after.median_ms() - before.median_ms());
  235. }
  236. Ok(())
  237. }
  238. #[cfg(test)]
  239. mod tests {
  240. #[test]
  241. fn ranges() {
  242. assert_eq!(super::range("3..7"), Some((3, 7)));
  243. assert_eq!(super::range("5"), Some((5, 5)));
  244. assert_eq!(super::range("x"), None);
  245. assert_eq!(super::range("1..x"), None);
  246. }
  247. }