replay.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. /// The argument clamps in batches, as VICTORY_INDIRECT_BATCH does (2:
  52. /// a wait before every indirect dispatch).
  53. #[arg(long, num_args = 0..=1, default_missing_value = "1", default_value_t = 0)]
  54. batch_clamps: u32,
  55. /// Leave out the hash and compare passes over card buffers.
  56. #[arg(long)]
  57. skip_hash_passes: bool,
  58. /// Where buffers lie.
  59. #[arg(long, value_enum, default_value_t = Buffers::AsRecorded)]
  60. buffers: Buffers,
  61. /// With --buffers, only the buffers all of these match: `name=<text>`,
  62. /// `min=<bytes>`, `max=<bytes>`, `seen=<No, Cached, Combined>`,
  63. /// `id=<a>..<b>`, or `!` and one of those (may be given several times).
  64. #[arg(long)]
  65. only_buffers: Vec<String>,
  66. /// One ExecuteCommandLists for the whole frame.
  67. #[arg(long)]
  68. one_submit: bool,
  69. /// Let copies of what the CPU wrote also overwrite what the card wrote in
  70. /// the replay (as the recorded commands say).
  71. #[arg(long)]
  72. no_merge: bool,
  73. /// A full wait after every draw, dispatch, copy and clear.
  74. #[arg(long)]
  75. wait_all: bool,
  76. /// Each list sent on its own, and waited for.
  77. #[arg(long)]
  78. wait_lists: bool,
  79. /// Run only these lists, by their place in the frame (`3..7`, or `5`);
  80. /// of the others only barriers.
  81. #[arg(long)]
  82. only: Option<String>,
  83. /// Leave out what runs a pipeline whose name or DXIL hash has this in it
  84. /// (may be given several times).
  85. #[arg(long = "skip-shader")]
  86. skip_shader: Vec<String>,
  87. /// Find the commands that make identical runs differ: each list is run
  88. /// this often from the same memory.
  89. #[arg(long, num_args = 0..=1, default_missing_value = "3")]
  90. diverge: Option<u32>,
  91. /// Find where the picture's variants are decided: from each list on, the
  92. /// rest of the frame is run this often from the same memory.
  93. #[arg(long, num_args = 0..=1, default_missing_value = "5")]
  94. diverge_picture: Option<u32>,
  95. /// Run each draw of one list (--lists) whose pipeline's name has this in
  96. /// it alone, --diverge times: how many outcomes it has.
  97. #[arg(long)]
  98. draws: Option<String>,
  99. /// Say which lists (with --lists: commands) change words of buffers:
  100. /// `<resource>:<offset>:<bytes>` (hexadecimal; repeatable).
  101. #[arg(long)]
  102. watch: Vec<String>,
  103. /// With --diverge-picture and one list in --lists: its commands (`0..900`)
  104. /// and how many are stepped over at a time.
  105. #[arg(long)]
  106. commands: Option<String>,
  107. #[arg(long, default_value_t = 1)]
  108. step: usize,
  109. /// With --diverge and --diverge-picture: only these lists (`3..7`).
  110. #[arg(long)]
  111. lists: Option<String>,
  112. /// With --diverge: the most commands named of one list.
  113. #[arg(long, default_value_t = 4)]
  114. roots: u32,
  115. /// Name resources the frame changes that the replay does not put back.
  116. #[arg(long)]
  117. check_written: bool,
  118. /// Run the frame twice and say which words the copies of the CPU's sides
  119. /// of card buffers bring that the card holds another value of, and what
  120. /// shaders wrote that was not declared as written.
  121. #[arg(long)]
  122. upcopies: bool,
  123. /// Name what the heap slots hold that a command's constants name:
  124. /// `<list>:<command>`.
  125. #[arg(long)]
  126. slots: Option<String>,
  127. /// Only write the DXIL of pipelines with this in their name to the
  128. /// capture's `shaders` folder.
  129. #[arg(long)]
  130. dump: Option<String>,
  131. /// Only write the frame's buffers and the commands of these lists (`3..7`).
  132. #[arg(long)]
  133. listing: Option<String>,
  134. /// Only write what the dispatches of these lists have bound (`3..7`).
  135. #[arg(long)]
  136. flow: Option<String>,
  137. /// With --flow: draws too.
  138. #[arg(long)]
  139. flow_draws: bool,
  140. /// With --flow: heap slots shown from each place named.
  141. #[arg(long, default_value_t = 40)]
  142. flow_slots: u32,
  143. /// Run the frame once and save textures as pictures in this folder, with
  144. /// what values they hold (see --texture, --until).
  145. #[arg(long)]
  146. images: Option<PathBuf>,
  147. /// With --images: `<resource>[.<subresource>]` (repeatable); none: all
  148. /// that the frame's shaders write.
  149. #[arg(long)]
  150. texture: Vec<String>,
  151. /// With --images and no --texture: from this many texels on.
  152. #[arg(long, default_value_t = 4096)]
  153. least: u64,
  154. /// With --images: the widest picture.
  155. #[arg(long, default_value_t = 960)]
  156. width: u32,
  157. /// With --images: the values too.
  158. #[arg(long)]
  159. raw: bool,
  160. /// The last command that runs, `<list>:<command>`; after it only barriers.
  161. #[arg(long)]
  162. until: Option<String>,
  163. /// Say which commands change the pixel: `<x>,<y>` of the presented
  164. /// picture, or `<resource>[.<subresource>]:<x>,<y>` of a texture
  165. /// (repeatable). With --lists, only those are looked into.
  166. #[arg(long)]
  167. pixel: Vec<String>,
  168. /// With --pixel: the size of the picture the points are of (`1280x720`),
  169. /// if it is another than the presented one's.
  170. #[arg(long)]
  171. of: Option<String>,
  172. /// With --pixel: depth targets too.
  173. #[arg(long)]
  174. depth: bool,
  175. /// With --pixel: leave out textures that shaders may write (unordered
  176. /// access).
  177. #[arg(long)]
  178. targets_only: bool,
  179. /// With --pixel: indirect calls whose draws are run one by one:
  180. /// `<list>:<command>` (repeatable).
  181. #[arg(long)]
  182. split: Vec<String>,
  183. /// Use the D3D12 debug layer (slow).
  184. #[arg(long)]
  185. debug_layer: bool,
  186. }
  187. /// `3..7` (both included) or `5`.
  188. fn range(text: &str) -> Option<(u32, u32)> {
  189. match text.split_once("..") {
  190. Some((a, b)) => Some((a.trim().parse().ok()?, b.trim().parse().ok()?)),
  191. None => text.trim().parse().ok().map(|n| (n, n)),
  192. }
  193. }
  194. /// Two numbers with `by` between them.
  195. fn pair(text: &str, by: char) -> Option<(u32, u32)> {
  196. let (a, b) = text.split_once(by)?;
  197. Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
  198. }
  199. pub(crate) fn run(r: Replay) -> Result<()> {
  200. if let Some(text) = &r.dump {
  201. for line in play::dump(&r.folder, text)? {
  202. println!("{line}");
  203. }
  204. return Ok(());
  205. }
  206. if let Some(text) = &r.listing {
  207. let lists = range(text).with_context(|| format!("--listing {text}"))?;
  208. return play::listing(&r.folder, lists, &mut |line| println!("{line}"));
  209. }
  210. if let Some(text) = &r.flow {
  211. let lists = range(text).with_context(|| format!("--flow {text}"))?;
  212. return play::look::flow(&r.folder, lists, r.flow_draws, r.flow_slots, &mut |line| println!("{line}"));
  213. }
  214. let until = match &r.until {
  215. Some(text) => Some(
  216. text.split_once(':')
  217. .and_then(|(l, c)| Some((l.parse().ok()?, c.parse().ok()?)))
  218. .with_context(|| format!("--until {text}: not <list>:<command>"))?,
  219. ),
  220. None => None,
  221. };
  222. let only = match &r.only {
  223. Some(text) => Some(
  224. range(text)
  225. .with_context(|| format!("--only {text}: not a list or a range of lists (3..7)"))?,
  226. ),
  227. None => None,
  228. };
  229. let options = Options {
  230. loops: r.loops.max(1),
  231. restore: !r.no_restore,
  232. detailed: matches!(r.timing, Timing::Detailed),
  233. top: r.top,
  234. image: r.image,
  235. buffers: match r.buffers {
  236. Buffers::AsRecorded => play::Buffers::AsRecorded,
  237. Buffers::Vram => play::Buffers::Card,
  238. Buffers::Sysmem => play::Buffers::Pc,
  239. },
  240. only_buffers: r.only_buffers,
  241. no_barriers: r.no_barriers,
  242. merge_barriers: r.merge_barriers,
  243. skip_clamps: r.skip_clamps,
  244. batch_clamps: r.batch_clamps,
  245. skip_hash_passes: r.skip_hash_passes,
  246. one_submit: r.one_submit,
  247. merge: !r.no_merge,
  248. wait_all: r.wait_all,
  249. wait_lists: r.wait_lists,
  250. only,
  251. until,
  252. skip_shaders: r.skip_shader,
  253. compare: r.compare,
  254. debug: r.debug_layer,
  255. };
  256. if let Some(text) = &r.slots {
  257. let (list, command) = text
  258. .split_once(':')
  259. .and_then(|(l, c)| Some((l.parse().ok()?, c.parse().ok()?)))
  260. .with_context(|| format!("--slots {text}: not <list>:<command>"))?;
  261. let player = play::Player::load(&r.folder, &options)?;
  262. return player.slots(list, command, &mut |line| println!("{line}"));
  263. }
  264. if let Some(folder) = &r.images {
  265. let mut which = Vec::new();
  266. for text in &r.texture {
  267. let (id, sub) = text.split_once('.').unwrap_or((text, "0"));
  268. let bad = || format!("--texture {text}");
  269. which.push((id.parse().with_context(bad)?, sub.parse().with_context(bad)?));
  270. }
  271. let images = play::look::Images {
  272. which,
  273. least: r.least,
  274. folder: folder.clone(),
  275. width: r.width,
  276. raw: r.raw,
  277. };
  278. let mut player = play::Player::load(&r.folder, &options)?;
  279. return player.images(&options, &images, &mut |line| println!("{line}"));
  280. }
  281. if !r.pixel.is_empty() {
  282. let mut player = play::Player::load(&r.folder, &options)?;
  283. let mut pixels = play::pixel::Pixels {
  284. depth: r.depth,
  285. written: !r.targets_only,
  286. ..Default::default()
  287. };
  288. if let Some(text) = &r.lists {
  289. pixels.lists = Some(range(text).with_context(|| format!("--lists {text}"))?);
  290. }
  291. let of = match &r.of {
  292. Some(text) => Some(pair(text, 'x').with_context(|| format!("--of {text}"))?),
  293. None => None,
  294. };
  295. for text in &r.pixel {
  296. let bad = || format!("--pixel {text}");
  297. match text.split_once(':') {
  298. Some((what, at)) => {
  299. let (id, sub) = what.split_once('.').unwrap_or((what, "0"));
  300. let (x, y) = pair(at, ',').with_context(bad)?;
  301. let id = id.parse().with_context(bad)?;
  302. let sub = sub.parse().with_context(bad)?;
  303. pixels.texels.push((id, sub, x, y));
  304. }
  305. None => {
  306. let (mut x, mut y) = pair(text, ',').with_context(bad)?;
  307. if let (Some((w, h)), Some((pw, ph))) = (of, player.picture_size()) {
  308. x = x * pw / w.max(1);
  309. y = y * ph / h.max(1);
  310. }
  311. pixels.points.push((x, y));
  312. }
  313. }
  314. }
  315. for text in &r.split {
  316. pixels
  317. .split
  318. .push(pair(text, ':').with_context(|| format!("--split {text}"))?);
  319. }
  320. return player.pixel(&options, &pixels, &mut |line| println!("{line}"));
  321. }
  322. if r.check_written {
  323. let mut player = play::Player::load(&r.folder, &options)?;
  324. return player.check_written(&mut |line| println!("{line}"));
  325. }
  326. if r.upcopies {
  327. let mut player = play::Player::load(&r.folder, &options)?;
  328. return player.upcopies(&options, &mut |line| println!("{line}"));
  329. }
  330. let mut watched = Vec::new();
  331. for text in &r.watch {
  332. let mut n = text
  333. .split(':')
  334. .map(|p| u64::from_str_radix(p.trim_start_matches("0x"), 16));
  335. let mut next = || {
  336. n.next()
  337. .and_then(Result::ok)
  338. .with_context(|| format!("--watch {text}"))
  339. };
  340. let id = text
  341. .split(':')
  342. .next()
  343. .and_then(|p| p.parse().ok())
  344. .with_context(|| format!("--watch {text}"))?;
  345. let _ = next();
  346. watched.push((id, next()?, next()?));
  347. }
  348. if let Some(runs) = r
  349. .diverge
  350. .or(r.diverge_picture)
  351. .or((!watched.is_empty()).then_some(2))
  352. {
  353. let lists = match &r.lists {
  354. Some(text) => Some(range(text).with_context(|| format!("--lists {text}"))?),
  355. None => None,
  356. };
  357. let search = play::diverge::Search {
  358. runs: runs.max(2),
  359. lists,
  360. roots: r.roots,
  361. wait_all: options.wait_all,
  362. options: options.clone(),
  363. };
  364. let mut player = play::Player::load(&r.folder, &options)?;
  365. if !watched.is_empty() {
  366. return player.watch(&search, &watched, &mut |line| println!("{line}"));
  367. }
  368. if let Some(text) = &r.draws {
  369. return player.diverge_draws(&search, text, &mut |line| println!("{line}"));
  370. }
  371. if r.diverge_picture.is_some() {
  372. let commands = match &r.commands {
  373. Some(text) => Some(range(text).with_context(|| format!("--commands {text}"))?),
  374. None => None,
  375. };
  376. return player
  377. .diverge_picture(&search, commands, r.step, &mut |line| println!("{line}"));
  378. }
  379. return player.diverge(&search, &mut |line| println!("{line}"));
  380. }
  381. let reports = play::replay(&r.folder, &options)?;
  382. for (what, report) in &reports {
  383. println!("{}", report.text(what));
  384. }
  385. if let [(_, before), (_, after)] = &reports[..] {
  386. println!("{:+.2} ms", after.median_ms() - before.median_ms());
  387. }
  388. Ok(())
  389. }
  390. #[cfg(test)]
  391. mod tests {
  392. #[test]
  393. fn ranges() {
  394. assert_eq!(super::range("3..7"), Some((3, 7)));
  395. assert_eq!(super::range("5"), Some((5, 5)));
  396. assert_eq!(super::range("x"), None);
  397. assert_eq!(super::pair("12,7", ','), Some((12, 7)));
  398. assert_eq!(super::pair("12", ','), None);
  399. assert_eq!(super::range("1..x"), None);
  400. }
  401. }