Преглед изворни кода

Frame replay: the CPU's copies leave what the card wrote; buffers in the card's memory fit

Co-Authored-By: Claude Opus 5.5 <[email protected]>
local пре 2 дана
родитељ
комит
475409218e

+ 25 - 5
apps/victory/src/replay.rs

@@ -62,6 +62,10 @@ pub(crate) struct Replay {
     /// 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,
@@ -158,6 +162,7 @@ pub(crate) fn run(r: Replay) -> Result<()> {
         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,
@@ -179,13 +184,27 @@ pub(crate) fn run(r: Replay) -> Result<()> {
     }
     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 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)) {
+    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,
@@ -209,7 +228,8 @@ pub(crate) fn run(r: Replay) -> Result<()> {
                 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_picture(&search, commands, r.step, &mut |line| println!("{line}"));
         }
         return player.diverge(&search, &mut |line| println!("{line}"));
     }

+ 121 - 7
crates/victory-gpu/src/replay/play.rs

@@ -30,6 +30,7 @@ use super::wire::{
 };
 
 pub mod diverge;
+mod merge;
 
 /// The name of the argument clamp's pipeline, by which its passes are
 /// recognised.
@@ -69,6 +70,9 @@ pub struct Options {
     pub skip_clamps: bool,
     pub skip_hash_passes: bool,
     pub one_submit: bool,
+    /// Copies of what the CPU wrote leave alone what the card wrote in this
+    /// run (`merge`).
+    pub merge: bool,
     /// A full wait after every command that works.
     pub wait_all: bool,
     /// Each list sent on its own, and waited for.
@@ -98,6 +102,7 @@ impl Default for Options {
             skip_clamps: false,
             skip_hash_passes: false,
             one_submit: false,
+            merge: true,
             wait_all: false,
             wait_lists: false,
             only: None,
@@ -372,6 +377,11 @@ pub struct Player {
     /// States at the start of the frame and after it.
     start: HashMap<u32, Vec<u32>>,
     end: HashMap<u32, Vec<u32>>,
+    /// For `Options::merge`.
+    merger: Option<merge::Merger>,
+    merging: bool,
+    /// Resources the card may write in the frame.
+    card_writes: HashSet<u32>,
     /// The results of clamp passes ([`RESULTS`]).
     results: Option<ID3D12Resource>,
     commands: Vec<Vec<Cmd>>,
@@ -437,6 +447,9 @@ impl Player {
             delta_parts: Vec::new(),
             start: HashMap::new(),
             end: HashMap::new(),
+            merger: None,
+            merging: false,
+            card_writes: HashSet::new(),
             results: None,
             commands,
             fresh: true,
@@ -463,12 +476,22 @@ impl Player {
         step("descriptors", &mut at);
         player.load_contents()?;
         step("contents", &mut at);
-        player.make_masters()?;
+        player.make_masters(options.buffers)?;
         step("copies to restore from", &mut at);
         let steps: Vec<String> = steps
             .iter()
             .map(|(w, d)| format!("{w} {:.1} s", d.as_secs_f64()))
             .collect();
+        if let Some([budget, used, pc]) = player.memory() {
+            let over = if used + 256 > budget {
+                ": over the budget, so the system moves memory to the PC's and the times are not the card's"
+            } else {
+                ""
+            };
+            player.notes.push(format!(
+                "{used} MB of the card's memory used, budget {budget} MB; {pc} MB of the PC's{over}"
+            ));
+        }
         player.notes.push(format!(
             "loaded in {:.1} s: {}",
             began.elapsed().as_secs_f64(),
@@ -1422,7 +1445,7 @@ impl Player {
     }
 
     /// Copies of what the frame changes; and the states it leaves.
-    fn make_masters(&mut self) -> Result<()> {
+    fn make_masters(&mut self, buffers: Buffers) -> Result<()> {
         let mut states = States::default();
         for r in &self.frame.resources {
             if r.desc.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0 {
@@ -1454,6 +1477,7 @@ impl Player {
             .filter(|r| r.state.iter().any(|s| s & WRITE != 0))
             .map(|r| r.id)
             .collect();
+        let mut by_cpu: HashSet<u32> = HashSet::new();
         for (i, s) in self.frame.submits.iter().enumerate() {
             for &l in s.lists.iter().filter(|&&l| l != MISSING) {
                 for c in &self.commands[l as usize] {
@@ -1504,7 +1528,7 @@ impl Player {
             }
             states.decay();
             if i > 0 {
-                written.extend(self.delta_parts[i].iter().map(|(d, _)| d.res));
+                by_cpu.extend(self.delta_parts[i].iter().map(|(d, _)| d.res));
             }
         }
         // Buffers any unordered-access view names.
@@ -1537,6 +1561,8 @@ impl Player {
                 }
             }
         }
+        self.card_writes = written.clone();
+        written.extend(by_cpu);
         for id in self.start.keys() {
             self.end
                 .insert(*id, states.get(*id).unwrap_or(vec![COMMON]));
@@ -1568,7 +1594,10 @@ impl Player {
             let size = unsafe { self.device.GetResourceAllocationInfo(0, &[desc]) }.SizeInBytes;
             let room = self.room();
             let buffer = r.desc.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0;
-            let pc = buffer && room.is_some_and(|room| room < size + (1 << 30));
+            // With all buffers in the card's memory there is no room for
+            // their copies as well.
+            let pc = buffer
+                && (buffers == Buffers::Card || room.is_some_and(|room| room < size + (1 << 30)));
             let props = if pc {
                 D3D12_HEAP_PROPERTIES {
                     Type: D3D12_HEAP_TYPE_CUSTOM,
@@ -1642,6 +1671,31 @@ impl Player {
         Ok(())
     }
 
+    /// The card's memory budget and use, and the use of the PC's memory, in
+    /// MB.
+    fn memory(&self) -> Option<[u64; 3]> {
+        let mut own = DXGI_QUERY_VIDEO_MEMORY_INFO::default();
+        let mut pc = DXGI_QUERY_VIDEO_MEMORY_INFO::default();
+        // SAFETY: plain queries.
+        unsafe {
+            let factory: IDXGIFactory4 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?;
+            let adapter: IDXGIAdapter3 = factory
+                .EnumAdapterByLuid(self.device.GetAdapterLuid())
+                .ok()?;
+            adapter
+                .QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &mut own)
+                .ok()?;
+            adapter
+                .QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &mut pc)
+                .ok()?;
+        }
+        Some([
+            own.Budget >> 20,
+            own.CurrentUsage >> 20,
+            pc.CurrentUsage >> 20,
+        ])
+    }
+
     /// What is free of the card's memory budget.
     fn room(&self) -> Option<u64> {
         let mut own = DXGI_QUERY_VIDEO_MEMORY_INFO::default();
@@ -1696,6 +1750,13 @@ impl Player {
                 for (id, m) in &restored {
                     list.CopyResource(&objects[id].res, *m);
                 }
+                if let Some(merger) = &self.merger {
+                    for (id, base) in &merger.bases {
+                        if let Some((_, m)) = self.masters.iter().find(|m| m.0 == *id) {
+                            list.CopyResource(base, m);
+                        }
+                    }
+                }
                 barriers(list, &back);
             }
         })?;
@@ -1886,10 +1947,15 @@ impl Player {
                     unsafe {
                         let mut seen = HashSet::new();
                         let mut after = Vec::new();
+                        let mut merges = Vec::new();
                         for (d, at) in parts {
                             let Some(ob) = self.objects.get(&d.res) else {
                                 continue;
                             };
+                            if self.merging && self.card_writes.contains(&d.res) {
+                                merges.push((d.res, d.at, *at, d.len));
+                                continue;
+                            }
                             open.1.CopyBufferRegion(&ob.res, d.at, from, *at, d.len);
                             if seen.insert(d.res) {
                                 // Back to the common state by itself, for
@@ -1898,6 +1964,11 @@ impl Player {
                             }
                         }
                         barriers(&open.1, &after);
+                        if !merges.is_empty() {
+                            self.wait_for_all(&open.1);
+                            self.merge_all(&open.1, from.GetGPUVirtualAddress(), &merges);
+                            self.wait_for_all(&open.1);
+                        }
                     }
                     Some(self.close(open, &heap, &mut next, Vec::new())?)
                 }
@@ -2077,13 +2148,51 @@ impl Player {
             .collect();
         let mut stamps = Vec::new();
         let mut bound = NONE;
+        // What the compute side has bound.
+        let mut compute: Vec<Cmd> = Vec::new();
         for c in cmds {
             if let Cmd::Pipeline { pso } = c {
                 bound = *pso;
             }
-            // SAFETY: an open list; the objects are the frame's, alive with
-            // the player; commands the host recorded the same way.
-            unsafe { self.command(list, c, version) };
+            let merged = self.merged_copy(c);
+            if let Some((from, what)) = merged {
+                // SAFETY: an open list; live buffers; what the compute side
+                // of it had bound is bound again.
+                unsafe {
+                    self.wait_for_all(list);
+                    self.merge_all(list, from, &[what]);
+                    self.wait_for_all(list);
+                    for c in &compute {
+                        self.command(list, c, version);
+                    }
+                }
+            } else {
+                // SAFETY: an open list; the objects are the frame's, alive
+                // with the player; commands the host recorded the same way.
+                unsafe { self.command(list, c, version) };
+            }
+            match c {
+                Cmd::RootSignature {
+                    graphics: false, ..
+                } => {
+                    compute.retain(|c| matches!(c, Cmd::Pipeline { .. }));
+                    compute.push(c.clone());
+                }
+                Cmd::Pipeline { .. } => {
+                    compute.retain(|c| !matches!(c, Cmd::Pipeline { .. }));
+                    compute.push(c.clone());
+                }
+                Cmd::Table {
+                    graphics: false, ..
+                }
+                | Cmd::Constants {
+                    graphics: false, ..
+                }
+                | Cmd::RootView {
+                    graphics: false, ..
+                } => compute.push(c.clone()),
+                _ => {}
+            }
             if wait && c.works() {
                 // SAFETY: an open list.
                 unsafe { self.wait_for_all(list) };
@@ -2608,6 +2717,7 @@ impl Player {
             notes: self.notes.clone(),
             ..Default::default()
         };
+        self.set_merging(o.merge)?;
         let clamps = if o.skip_clamps {
             self.clamp_results(&mut report)?
         } else {
@@ -2654,6 +2764,10 @@ impl Player {
         if o.detailed {
             self.detail(&built, &stamps, o, &mut report);
         }
+        if self.merging {
+            let notes = self.merge_notes(o.loops)?;
+            report.notes.extend(notes);
+        }
         self.picture(o, &mut report)?;
         if let Some(q) = &self.info {
             report.notes.extend(debug_messages(q));

+ 428 - 88
crates/victory-gpu/src/replay/play/diverge.rs

@@ -104,13 +104,20 @@ impl Player {
                     }
                     roots += 1;
                     // The shortest start of the rest that differs.
-                    let works: Vec<usize> = (from..cmds.len()).filter(|&c| cmds[c].works()).collect();
+                    let works: Vec<usize> =
+                        (from..cmds.len()).filter(|&c| cmds[c].works()).collect();
                     let (mut lo, mut hi) = (0, works.len() - 1);
                     while lo < hi {
                         let mid = (lo + hi) / 2;
-                        let cut = Unit { to: works[mid] + 1, ..unit };
+                        let cut = Unit {
+                            to: works[mid] + 1,
+                            ..unit
+                        };
                         self.put_back(&states)?;
-                        if self.trials(&reader, &cut, &states, s.runs, s.wait_all)?.is_empty() {
+                        if self
+                            .trials(&reader, &cut, &states, s.runs, s.wait_all)?
+                            .is_empty()
+                        {
                             lo = mid + 1;
                         } else {
                             hi = mid;
@@ -172,13 +179,18 @@ impl Player {
         let mut states = self.start_states();
         for (here, &(i, j, cmds)) in flat.iter().enumerate() {
             let inside = s.lists.is_some_and(|(a, b)| a == b && a as usize == here);
-            let looks = s.lists.is_none_or(|(a, b)| (a as usize..=b as usize).contains(&here));
+            let looks = s
+                .lists
+                .is_none_or(|(a, b)| (a as usize..=b as usize).contains(&here));
             let works: Vec<usize> = (0..cmds.len()).filter(|&c| cmds[c].works()).collect();
             // Where the list is cut.
             let mut cuts: Vec<usize> = vec![0];
             if inside {
                 let (a, b) = commands.unwrap_or((0, u32::MAX));
-                let some = works.iter().map(|&c| c + 1).filter(|c| (a as usize..=b as usize).contains(c));
+                let some = works
+                    .iter()
+                    .map(|&c| c + 1)
+                    .filter(|c| (a as usize..=b as usize).contains(c));
                 cuts.extend(some.step_by(step.max(1)));
             }
             for (k, &from) in cuts.iter().enumerate() {
@@ -189,14 +201,26 @@ impl Player {
                             self.put_back(&states)?;
                         }
                         let mut now = states.clone();
-                        let first = Unit { submit: i, cmds, from, to: cmds.len(), delta: j == 0 && from == 0 };
+                        let first = Unit {
+                            submit: i,
+                            cmds,
+                            from,
+                            to: cmds.len(),
+                            delta: j == 0 && from == 0,
+                        };
                         self.go(&first, s.wait_all)?;
                         now = self.after(&first, &now);
                         for (n, &(i, j, cmds)) in flat.iter().enumerate().skip(here + 1) {
                             if flat[n - 1].0 != i {
                                 now.decay();
                             }
-                            let unit = Unit { submit: i, cmds, from: 0, to: cmds.len(), delta: j == 0 };
+                            let unit = Unit {
+                                submit: i,
+                                cmds,
+                                from: 0,
+                                to: cmds.len(),
+                                delta: j == 0,
+                            };
                             self.go(&unit, s.wait_all)?;
                             now = self.after(&unit, &now);
                         }
@@ -222,7 +246,13 @@ impl Player {
                 }
                 // On to the next cut.
                 let to = cuts.get(k + 1).copied().unwrap_or(cmds.len());
-                let unit = Unit { submit: i, cmds, from, to, delta: j == 0 && from == 0 };
+                let unit = Unit {
+                    submit: i,
+                    cmds,
+                    from,
+                    to,
+                    delta: j == 0 && from == 0,
+                };
                 self.go(&unit, s.wait_all)?;
                 self.commit(&unit, &mut states)?;
                 self.at = states.clone();
@@ -248,7 +278,9 @@ impl Player {
     fn start_states(&self) -> States {
         let mut states = States::default();
         for (id, all) in &self.start {
-            let Some(o) = self.objects.get(id) else { continue };
+            let Some(o) = self.objects.get(id) else {
+                continue;
+            };
             let flags = self.frame.resources[o.index].desc.flags;
             let decays = flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS.0 != 0;
             states.add(*id, o.subs, all[0], decays);
@@ -285,38 +317,73 @@ impl Player {
             let start = self.start_states();
             let before = self.hashes_now(&reader, &start)?;
             let mut again = self.hashes_now(&reader, &start)?;
-            say(format!("read twice, {} parts differ", before.iter().zip(&again).filter(|(a, b)| a != b).count()));
+            say(format!(
+                "read twice, {} parts differ",
+                before.iter().zip(&again).filter(|(a, b)| a != b).count()
+            ));
             for i in 0..3 {
-                self.run(&Options { loops: 1, ..Options::default() })?;
+                self.run(&Options {
+                    loops: 1,
+                    ..Options::default()
+                })?;
                 let ran = {
-                    let end: Vec<(u32, Vec<u32>)> = self.end.iter().map(|(k, v)| (*k, v.clone())).collect();
+                    let end: Vec<(u32, Vec<u32>)> =
+                        self.end.iter().map(|(k, v)| (*k, v.clone())).collect();
                     let mut s = start.clone();
                     for (id, v) in end {
                         for (sub, st) in v.iter().enumerate() {
                             let sub = if v.len() == 1 { ALL } else { sub as u32 };
-                            s.change(&states::Change { res: id, sub, after: *st, promoted: false, alias: false });
+                            s.change(&states::Change {
+                                res: id,
+                                sub,
+                                after: *st,
+                                promoted: false,
+                                alias: false,
+                            });
                         }
                     }
                     self.hashes_now(&reader, &s)?
                 };
                 self.restore(true)?;
                 again = self.hashes_now(&reader, &start)?;
-                let differ: Vec<usize> = (0..before.len()).filter(|&i| before[i] != again[i]).collect();
+                let differ: Vec<usize> = (0..before.len())
+                    .filter(|&i| before[i] != again[i])
+                    .collect();
                 let changed = (0..before.len()).filter(|&i| before[i] != ran[i]).count();
-                say(format!("run {i}: {changed} parts changed by the frame, {} not put back as they were", differ.len()));
+                say(format!(
+                    "run {i}: {changed} parts changed by the frame, {} not put back as they were",
+                    differ.len()
+                ));
                 for &d in differ.iter().take(20) {
-                    let pieces = before[d].iter().zip(&again[d]).filter(|(a, b)| a != b).count();
-                    say(format!("    {} {:?}: {pieces} of {} pieces", self.called(reader.parts[d].id), reader.parts[d].sub.map(|s| s.0), before[d].len()));
+                    let pieces = before[d]
+                        .iter()
+                        .zip(&again[d])
+                        .filter(|(a, b)| a != b)
+                        .count();
+                    say(format!(
+                        "    {} {:?}: {pieces} of {} pieces",
+                        self.called(reader.parts[d].id),
+                        reader.parts[d].sub.map(|s| s.0),
+                        before[d].len()
+                    ));
                 }
             }
         }
         let put_back: HashSet<u32> = self.masters.iter().map(|m| m.0).collect();
-        let others: Vec<u32> = self.objects.keys().copied().filter(|id| !put_back.contains(id)).collect();
+        let others: Vec<u32> = self
+            .objects
+            .keys()
+            .copied()
+            .filter(|id| !put_back.contains(id))
+            .collect();
         let reader = self.reader(&others)?;
         self.restore(true)?;
         let start = self.start_states();
         let before = self.hashes_now(&reader, &start)?;
-        let o = Options { loops: 1, ..Options::default() };
+        let o = Options {
+            loops: 1,
+            ..Options::default()
+        };
         self.run(&o)?;
         self.restore(true)?;
         let after = self.hashes_now(&reader, &start)?;
@@ -337,7 +404,11 @@ impl Player {
                 ));
             }
         }
-        say(format!("{} parts of {} resources looked at, {n} changed", reader.parts.len(), others.len()));
+        say(format!(
+            "{} parts of {} resources looked at, {n} changed",
+            reader.parts.len(),
+            others.len()
+        ));
         Ok(())
     }
 
@@ -381,7 +452,12 @@ impl Player {
         }
         let mut blocks = Vec::new();
         for size in sizes {
-            let (b, data) = gpu::buffer(&self.device, D3D12_HEAP_TYPE_READBACK, size, D3D12_RESOURCE_FLAG_NONE)?;
+            let (b, data) = gpu::buffer(
+                &self.device,
+                D3D12_HEAP_TYPE_READBACK,
+                size,
+                D3D12_RESOURCE_FLAG_NONE,
+            )?;
             blocks.push((b, data as usize));
         }
         Ok(Reader { blocks, parts })
@@ -410,12 +486,22 @@ impl Player {
         let before: Vec<Cmd> = unit.cmds[..unit.from]
             .iter()
             .filter(|c| {
-                !c.works() && !matches!(c, Cmd::Barriers { .. } | Cmd::BeginQuery { .. } | Cmd::EndQuery { .. })
+                !c.works()
+                    && !matches!(
+                        c,
+                        Cmd::Barriers { .. } | Cmd::BeginQuery { .. } | Cmd::EndQuery { .. }
+                    )
             })
             .cloned()
             .collect();
         self.translate(&l.1, &before, unit.submit, None, false);
-        self.translate(&l.1, &unit.cmds[unit.from..unit.to], unit.submit, None, wait);
+        self.translate(
+            &l.1,
+            &unit.cmds[unit.from..unit.to],
+            unit.submit,
+            None,
+            wait,
+        );
         // SAFETY: an open list.
         unsafe { l.1.Close()? };
         lists.push(l);
@@ -437,7 +523,10 @@ impl Player {
             for p in parts.iter().filter(|p| p.sub.is_some()) {
                 if seen.insert(p.id) {
                     let state = states.get(p.id).unwrap_or(vec![COMMON]);
-                    let to: Vec<u32> = state.iter().map(|&s| if s & COPY_SOURCE != 0 { s } else { COPY_SOURCE }).collect();
+                    let to: Vec<u32> = state
+                        .iter()
+                        .map(|&s| if s & COPY_SOURCE != 0 { s } else { COPY_SOURCE })
+                        .collect();
                     gpu::moves(&objects[&p.id].res, &state, &to, &mut before);
                     gpu::moves(&objects[&p.id].res, &to, &state, &mut after);
                 }
@@ -453,7 +542,14 @@ impl Player {
                             None => list.CopyBufferRegion(to, p.at, res, 0, p.len),
                             Some((sub, mut f)) => {
                                 f.Offset = p.at;
-                                list.CopyTextureRegion(&gpu::placed_location(to, f), 0, 0, 0, &gpu::sub_location(res, sub), None);
+                                list.CopyTextureRegion(
+                                    &gpu::placed_location(to, f),
+                                    0,
+                                    0,
+                                    0,
+                                    &gpu::sub_location(res, sub),
+                                    None,
+                                );
                             }
                         }
                     }
@@ -462,7 +558,8 @@ impl Player {
             })?;
         }
         let next = AtomicUsize::new(0);
-        let out: std::sync::Mutex<Vec<Vec<u64>>> = std::sync::Mutex::new(vec![Vec::new(); reader.parts.len()]);
+        let out: std::sync::Mutex<Vec<Vec<u64>>> =
+            std::sync::Mutex::new(vec![Vec::new(); reader.parts.len()]);
         std::thread::scope(|scope| {
             for _ in 0..16 {
                 scope.spawn(|| {
@@ -488,7 +585,14 @@ impl Player {
 
     /// Runs the unit `runs` times (memory is as at its start, and is put
     /// back between runs) and compares. Memory is as the last run left it.
-    fn trials(&mut self, reader: &Reader, unit: &Unit, states: &States, runs: u32, wait: bool) -> Result<Pieces> {
+    fn trials(
+        &mut self,
+        reader: &Reader,
+        unit: &Unit,
+        states: &States,
+        runs: u32,
+        wait: bool,
+    ) -> Result<Pieces> {
         let after = self.after(unit, states);
         let mut first: Option<Vec<Vec<u64>>> = None;
         let mut pieces = std::collections::BTreeSet::new();
@@ -505,7 +609,13 @@ impl Player {
                 None => first = Some(now),
                 Some(first) => {
                     for (p, (a, b)) in first.iter().zip(&now).enumerate() {
-                        pieces.extend(a.iter().zip(b).enumerate().filter(|(_, (a, b))| a != b).map(|(i, _)| (p, i)));
+                        pieces.extend(
+                            a.iter()
+                                .zip(b)
+                                .enumerate()
+                                .filter(|(_, (a, b))| a != b)
+                                .map(|(i, _)| (p, i)),
+                        );
                     }
                 }
             }
@@ -537,12 +647,18 @@ impl Player {
     /// Copies the masters over the resources (which are in `from` and go to
     /// `to`), or the resources into the masters.
     fn copy_masters(&mut self, from: &States, to: &States, save: bool) -> Result<()> {
-        let known: HashMap<u32, &ID3D12Resource> = self.masters.iter().map(|(id, m)| (*id, m)).collect();
+        let known: HashMap<u32, &ID3D12Resource> =
+            self.masters.iter().map(|(id, m)| (*id, m)).collect();
         let (mut before, mut after, mut plain) = (Vec::new(), Vec::new(), Vec::new());
         let here = if save { COPY_SOURCE } else { COPY_DEST };
         for id in self.start.keys() {
-            let Some(o) = self.objects.get(id) else { continue };
-            let (a, b) = (from.get(*id).unwrap_or(vec![COMMON]), to.get(*id).unwrap_or(vec![COMMON]));
+            let Some(o) = self.objects.get(id) else {
+                continue;
+            };
+            let (a, b) = (
+                from.get(*id).unwrap_or(vec![COMMON]),
+                to.get(*id).unwrap_or(vec![COMMON]),
+            );
             let Some(m) = known.get(id) else {
                 gpu::moves(&o.res, &a, &b, &mut plain);
                 continue;
@@ -590,27 +706,59 @@ impl Player {
             match c {
                 Cmd::Pipeline { pso: p } => pso = *p,
                 Cmd::RootSignature { graphics, .. } => binds.retain(|k, _| k.0 != *graphics),
-                Cmd::RootView { graphics, kind, param, va } => {
-                    let kind = ["", "cbv", "srv", "uav"].get(*kind as usize).copied().unwrap_or("");
+                Cmd::RootView {
+                    graphics,
+                    kind,
+                    param,
+                    va,
+                } => {
+                    let kind = ["", "cbv", "srv", "uav"]
+                        .get(*kind as usize)
+                        .copied()
+                        .unwrap_or("");
                     let text = match va {
-                        Va::At { res, offset } => format!("{kind} {} +{offset:#x}", self.called(*res)),
+                        Va::At { res, offset } => {
+                            format!("{kind} {} +{offset:#x}", self.called(*res))
+                        }
                         _ => format!("{kind} null"),
                     };
                     binds.insert((*graphics, *param), text);
                 }
-                Cmd::Table { graphics, param, heap, slot } => {
-                    binds.insert((*graphics, *param), format!("table heap {heap} slot {slot}"));
+                Cmd::Table {
+                    graphics,
+                    param,
+                    heap,
+                    slot,
+                } => {
+                    binds.insert(
+                        (*graphics, *param),
+                        format!("table heap {heap} slot {slot}"),
+                    );
                 }
-                Cmd::Constants { graphics, param, offset, values } => {
+                Cmd::Constants {
+                    graphics,
+                    param,
+                    offset,
+                    values,
+                } => {
                     let text = format!("constants +{offset} {values:x?}");
-                    binds.entry((*graphics, *param)).and_modify(|t| *t = format!("{t}, {text}")).or_insert(text);
+                    binds
+                        .entry((*graphics, *param))
+                        .and_modify(|t| *t = format!("{t}, {text}"))
+                        .or_insert(text);
                 }
                 Cmd::Targets { colors, depth } => {
-                    let all: Vec<String> = colors.iter().chain(depth).map(|s| self.called(s.res)).collect();
+                    let all: Vec<String> = colors
+                        .iter()
+                        .chain(depth)
+                        .map(|s| self.called(s.res))
+                        .collect();
                     targets = all.join(", ");
                 }
                 Cmd::Barriers { all } => since.extend(all.iter().map(|b| match b {
-                    Barrier::Transition { res, before, after, .. } => format!("{} {before:#x}->{after:#x}", self.called(*res)),
+                    Barrier::Transition {
+                        res, before, after, ..
+                    } => format!("{} {before:#x}->{after:#x}", self.called(*res)),
                     Barrier::Uav { res: 0, .. } => "all uav".into(),
                     Barrier::Uav { res, .. } => format!("uav {}", self.called(*res)),
                     Barrier::Alias { .. } => "alias".into(),
@@ -622,7 +770,11 @@ impl Player {
             }
         }
         let graphics = !matches!(cmds[at], Cmd::Dispatch { .. });
-        let binds: Vec<String> = binds.iter().filter(|(k, _)| k.0 == graphics).map(|(k, t)| format!("[{}] {t}", k.1)).collect();
+        let binds: Vec<String> = binds
+            .iter()
+            .filter(|(k, _)| k.0 == graphics)
+            .map(|(k, t)| format!("[{}] {t}", k.1))
+            .collect();
         let mut text = format!("{:?}", cmds[at]);
         if cmds[at].runs_shaders() {
             text += &format!(" of {}", self.pipeline_name(pso));
@@ -631,12 +783,20 @@ impl Player {
                 text += &format!("\n    targets: {targets}");
             }
         }
-        text + &format!("\n    barriers since the command before: {}", since.join("; "))
+        text + &format!(
+            "\n    barriers since the command before: {}",
+            since.join("; ")
+        )
     }
 
     /// Says which lists (of the list in `s.lists`: which commands) change
     /// words of a buffer, and to what.
-    pub fn watch(&mut self, s: &Search, parts: &[(u32, u64, u64)], say: &mut dyn FnMut(String)) -> Result<()> {
+    pub fn watch(
+        &mut self,
+        s: &Search,
+        parts: &[(u32, u64, u64)],
+        say: &mut dyn FnMut(String),
+    ) -> Result<()> {
         let mut report = Report::default();
         let submits = self.edited(&s.options, &mut report, &[]);
         self.restore(true)?;
@@ -646,7 +806,10 @@ impl Player {
                 .iter()
                 .map(|&(id, at, len)| {
                     let bytes = p.slice(id, at, len)?;
-                    Ok(bytes.chunks_exact(4).map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default())).collect())
+                    Ok(bytes
+                        .chunks_exact(4)
+                        .map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default()))
+                        .collect())
                 })
                 .collect()
         };
@@ -666,13 +829,25 @@ impl Player {
                     cuts.push(cmds.len());
                 }
                 for k in 0..cuts.len() - 1 {
-                    let unit = Unit { submit: i, cmds, from: cuts[k], to: cuts[k + 1], delta: j == 0 && k == 0 };
+                    let unit = Unit {
+                        submit: i,
+                        cmds,
+                        from: cuts[k],
+                        to: cuts[k + 1],
+                        delta: j == 0 && k == 0,
+                    };
                     self.go(&unit, s.wait_all)?;
                     let now = words(self)?;
                     if now != was {
                         if inside {
-                            let last = (unit.from..unit.to).rev().find(|&c| cmds[c].works()).unwrap_or(unit.to - 1);
-                            say(format!("list {here} (submission {i}, its list {j}) command {last}: {}", self.describe(cmds, last)));
+                            let last = (unit.from..unit.to)
+                                .rev()
+                                .find(|&c| cmds[c].works())
+                                .unwrap_or(unit.to - 1);
+                            say(format!(
+                                "list {here} (submission {i}, its list {j}) command {last}: {}",
+                                self.describe(cmds, last)
+                            ));
                         } else {
                             say(format!("list {here} (submission {i}, its list {j})"));
                         }
@@ -703,7 +878,12 @@ impl Player {
     /// Each draw of one list whose pipeline's name has `text` in it, run
     /// alone (with what prepares its arguments) from the memory at the list's
     /// start, `runs` times: how many different contents its targets get.
-    pub fn diverge_draws(&mut self, s: &Search, text: &str, say: &mut dyn FnMut(String)) -> Result<()> {
+    pub fn diverge_draws(
+        &mut self,
+        s: &Search,
+        text: &str,
+        say: &mut dyn FnMut(String),
+    ) -> Result<()> {
         let mut report = Report::default();
         let submits = self.edited(&s.options, &mut report, &[]);
         let flat: Vec<(usize, usize, &Vec<Cmd>)> = submits
@@ -717,7 +897,13 @@ impl Player {
         let mut states = self.start_states();
         for (here, &(i, j, cmds)) in flat.iter().enumerate() {
             if here < wanted {
-                let unit = Unit { submit: i, cmds, from: 0, to: cmds.len(), delta: j == 0 };
+                let unit = Unit {
+                    submit: i,
+                    cmds,
+                    from: 0,
+                    to: cmds.len(),
+                    delta: j == 0,
+                };
                 self.go(&unit, s.wait_all)?;
                 self.commit(&unit, &mut states)?;
                 if flat.get(here + 1).is_none_or(|n| n.0 != i) {
@@ -732,10 +918,23 @@ impl Player {
             for (c, cmd) in cmds.iter().enumerate() {
                 match cmd {
                     Cmd::Pipeline { pso: p } => pso = *p,
-                    Cmd::Targets { colors, depth } => targets = colors.iter().chain(depth).map(|s| s.res).filter(|r| *r != NONE).collect(),
+                    Cmd::Targets { colors, depth } => {
+                        targets = colors
+                            .iter()
+                            .chain(depth)
+                            .map(|s| s.res)
+                            .filter(|r| *r != NONE)
+                            .collect()
+                    }
                     _ => {}
                 }
-                if !matches!(cmd, Cmd::Indirect { .. } | Cmd::Draw { .. } | Cmd::DrawIndexed { .. } | Cmd::DispatchMesh { .. }) {
+                if !matches!(
+                    cmd,
+                    Cmd::Indirect { .. }
+                        | Cmd::Draw { .. }
+                        | Cmd::DrawIndexed { .. }
+                        | Cmd::DispatchMesh { .. }
+                ) {
                     continue;
                 }
                 let from = std::mem::replace(&mut last, c + 1);
@@ -745,12 +944,20 @@ impl Player {
                 targets.sort_unstable();
                 targets.dedup();
                 let reader = self.reader(&targets)?;
-                let unit = Unit { submit: i, cmds, from, to: c + 1, delta: false };
+                let unit = Unit {
+                    submit: i,
+                    cmds,
+                    from,
+                    to: c + 1,
+                    delta: false,
+                };
                 // Barriers before it in the list are made up for: textures
                 // are in the states the list has them in here.
                 let mut before = states.clone();
                 let mut all = Vec::new();
-                cmds[..from].iter().for_each(|c| states::changes(c, &mut all));
+                cmds[..from]
+                    .iter()
+                    .for_each(|c| states::changes(c, &mut all));
                 all.iter().for_each(|c| before.change(c));
                 let after = self.after(&unit, &before);
                 let mut kinds: HashMap<Vec<Vec<u64>>, u32> = HashMap::new();
@@ -770,7 +977,12 @@ impl Player {
                 }
                 let mut counts: Vec<u32> = kinds.values().copied().collect();
                 counts.sort_unstable();
-                say(format!("command {c} (from {from}) {:?} of {}: {} outcomes {counts:?}", cmd, self.pipeline_name(pso), counts.len()));
+                say(format!(
+                    "command {c} (from {from}) {:?} of {}: {} outcomes {counts:?}",
+                    cmd,
+                    self.pipeline_name(pso),
+                    counts.len()
+                ));
             }
             break;
         }
@@ -779,13 +991,20 @@ impl Player {
 
     /// `len` bytes of a buffer from `at`.
     fn slice(&mut self, id: u32, at: u64, len: u64) -> Result<Vec<u8>> {
-        let Some(o) = self.objects.get(&id) else { return Ok(Vec::new()) };
+        let Some(o) = self.objects.get(&id) else {
+            return Ok(Vec::new());
+        };
         let len = len.min(self.frame.resources[o.index].desc.width.saturating_sub(at));
         if len == 0 {
             return Ok(Vec::new());
         }
         let res = o.res.clone();
-        let (back, data) = gpu::buffer(&self.device, D3D12_HEAP_TYPE_READBACK, len, D3D12_RESOURCE_FLAG_NONE)?;
+        let (back, data) = gpu::buffer(
+            &self.device,
+            D3D12_HEAP_TYPE_READBACK,
+            len,
+            D3D12_RESOURCE_FLAG_NONE,
+        )?;
         self.worker.run(|list| {
             // SAFETY: an open list; live buffers.
             unsafe { list.CopyBufferRegion(&back, 0, &res, at, len) };
@@ -796,20 +1015,39 @@ impl Player {
 
     /// The words of a buffer view (raw or typed, of words).
     fn view_words(&mut self, s: &Slot, first: u64, n: u64) -> Result<Vec<u32>> {
-        let at = u64::from_le_bytes([s.desc[16], s.desc[17], s.desc[18], s.desc[19], s.desc[20], s.desc[21], s.desc[22], s.desc[23]]);
-        let count = u64::from(u32::from_le_bytes([s.desc[24], s.desc[25], s.desc[26], s.desc[27]]));
+        let at = u64::from_le_bytes([
+            s.desc[16], s.desc[17], s.desc[18], s.desc[19], s.desc[20], s.desc[21], s.desc[22],
+            s.desc[23],
+        ]);
+        let count = u64::from(u32::from_le_bytes([
+            s.desc[24], s.desc[25], s.desc[26], s.desc[27],
+        ]));
         let n = n.min(count.saturating_sub(first));
         let bytes = self.slice(s.res, (at + first) * 4, n * 4)?;
-        Ok(bytes.chunks_exact(4).map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default())).collect())
+        Ok(bytes
+            .chunks_exact(4)
+            .map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default()))
+            .collect())
     }
 
     /// What a mesh draw of the title's (with its usual constants) reads.
-    fn peek(&mut self, cmds: &[Cmd], at: usize, submit: usize, say: &mut dyn FnMut(String)) -> Result<()> {
+    fn peek(
+        &mut self,
+        cmds: &[Cmd],
+        at: usize,
+        submit: usize,
+        say: &mut dyn FnMut(String),
+    ) -> Result<()> {
         let mut v = [0u32; 16];
         let mut heap = NONE;
         for c in &cmds[..at] {
             match c {
-                Cmd::Constants { graphics: true, param: 0, offset, values } => {
+                Cmd::Constants {
+                    graphics: true,
+                    param: 0,
+                    offset,
+                    values,
+                } => {
                     for (k, x) in values.iter().enumerate() {
                         if let Some(at) = v.get_mut(*offset as usize + k) {
                             *at = *x;
@@ -834,20 +1072,33 @@ impl Player {
             ("x+2848", v[8] + 2848, 0),
         ] {
             let s = slot(self, byte);
-            let words = if s.kind == slot::SRV { self.view_words(&s, 0, n)? } else { Vec::new() };
-            say(format!("    {name}: slot {} {}: {words:x?}", byte >> 5, self.slot_text(&s)));
+            let words = if s.kind == slot::SRV {
+                self.view_words(&s, 0, n)?
+            } else {
+                Vec::new()
+            };
+            say(format!(
+                "    {name}: slot {} {}: {words:x?}",
+                byte >> 5,
+                self.slot_text(&s)
+            ));
         }
         // The list of visible meshlets, as the mesh shader finds it.
         let lists = slot(self, v[8] + 2752);
         let word = self.view_words(&lists, u64::from((v[13] & 0x07ff_ffff) & 0x07ff_ffff), 4)?;
         let typed = slot(self, v[8] + 1600);
         let index = self.view_words(&typed, 510, 1)?;
-        say(format!("    list words {word:x?} (count {}, first {}), typed view slot {index:x?}", word.first().map_or(0, |w| w >> 15), word.first().map_or(0, |w| w & 0x7fff)));
+        say(format!(
+            "    list words {word:x?} (count {}, first {}), typed view slot {index:x?}",
+            word.first().map_or(0, |w| w >> 15),
+            word.first().map_or(0, |w| w & 0x7fff)
+        ));
         if let (Some(w), Some(i)) = (word.first(), index.first()) {
             let s = self.slot_at(heap, i & 0x07ff_ffff, submit);
             say(format!("    typed view: {}", self.slot_text(&s)));
             if s.kind == slot::SRV && s.res != NONE {
-                let list = self.view_words(&s, u64::from(w & 0x7fff) * 16, u64::from(w >> 15) + 16)?;
+                let list =
+                    self.view_words(&s, u64::from(w & 0x7fff) * 16, u64::from(w >> 15) + 16)?;
                 say(format!("    visible meshlets: {list:x?}"));
             }
         }
@@ -868,7 +1119,12 @@ impl Player {
                     let mut heap = NONE;
                     for c in &cmds[..command] {
                         match c {
-                            Cmd::Constants { param, offset, values: v, .. } => {
+                            Cmd::Constants {
+                                param,
+                                offset,
+                                values: v,
+                                ..
+                            } => {
                                 for (k, x) in v.iter().enumerate() {
                                     if let Some(all) = values.get_mut(*param as usize)
                                         && let Some(at) = all.get_mut(*offset as usize + k)
@@ -882,7 +1138,12 @@ impl Player {
                         }
                     }
                     say(format!("heap {heap}, constants {values:x?}"));
-                    let mut bases: Vec<u32> = values.iter().flatten().map(|v| v >> 5).filter(|v| *v > 64).collect();
+                    let mut bases: Vec<u32> = values
+                        .iter()
+                        .flatten()
+                        .map(|v| v >> 5)
+                        .filter(|v| *v > 64)
+                        .collect();
                     bases.dedup();
                     for base in bases {
                         for k in 0..12 {
@@ -904,10 +1165,19 @@ impl Player {
     /// A heap slot as it was for a submission.
     fn slot_at(&self, heap: u32, at: u32, submit: usize) -> Slot {
         let late = self.frame.submits[..=submit].iter().rev().find_map(|s| {
-            s.views.iter().rev().find(|(h, i, _)| *h == heap && *i == at).map(|v| v.2)
+            s.views
+                .iter()
+                .rev()
+                .find(|(h, i, _)| *h == heap && *i == at)
+                .map(|v| v.2)
         });
         late.unwrap_or_else(|| {
-            let runs = self.frame.descriptor_heaps.iter().find(|h| h.id == heap).map(|h| &h.runs);
+            let runs = self
+                .frame
+                .descriptor_heaps
+                .iter()
+                .find(|h| h.id == heap)
+                .map(|h| &h.runs);
             runs.and_then(|runs| {
                 let k = runs.partition_point(|r| r.0 <= at).checked_sub(1)?;
                 let (first, n, s) = &runs[k];
@@ -918,9 +1188,23 @@ impl Player {
     }
 
     fn slot_text(&self, s: &Slot) -> String {
-        let word = |i: usize| u32::from_le_bytes([s.desc[i * 4], s.desc[i * 4 + 1], s.desc[i * 4 + 2], s.desc[i * 4 + 3]]);
-        let kind = ["empty", "cbv", "srv", "uav", "sampler", "rtv", "dsv"].get(s.kind as usize).copied().unwrap_or("?");
-        let res = if s.res == NONE { "null".into() } else { self.called(s.res) };
+        let word = |i: usize| {
+            u32::from_le_bytes([
+                s.desc[i * 4],
+                s.desc[i * 4 + 1],
+                s.desc[i * 4 + 2],
+                s.desc[i * 4 + 3],
+            ])
+        };
+        let kind = ["empty", "cbv", "srv", "uav", "sampler", "rtv", "dsv"]
+            .get(s.kind as usize)
+            .copied()
+            .unwrap_or("?");
+        let res = if s.res == NONE {
+            "null".into()
+        } else {
+            self.called(s.res)
+        };
         match s.kind {
             slot::CBV => format!("cbv {res} +{:#x} size {:#x}", s.address(), word(2)),
             _ => format!(
@@ -935,7 +1219,7 @@ impl Player {
         }
     }
 
-    fn called(&self, id: u32) -> String {
+    pub(super) fn called(&self, id: u32) -> String {
         let Some(o) = self.objects.get(&id) else {
             return format!("resource {id} (not there)");
         };
@@ -948,12 +1232,22 @@ impl Player {
             };
             format!("#{id} '{}' buffer {:#x} {kind}", r.name, d.width)
         } else {
-            format!("#{id} '{}' {}x{}x{} m{} f{}", r.name, d.width, d.height, d.depth, d.mips, d.format)
+            format!(
+                "#{id} '{}' {}x{}x{} m{} f{}",
+                r.name, d.width, d.height, d.depth, d.mips, d.format
+            )
         }
     }
 
     /// Runs the unit until two runs differ, and says how.
-    fn compare(&mut self, reader: &Reader, unit: &Unit, states: &States, pieces: &Pieces, wait: bool) -> Result<Vec<String>> {
+    fn compare(
+        &mut self,
+        reader: &Reader,
+        unit: &Unit,
+        states: &States,
+        pieces: &Pieces,
+        wait: bool,
+    ) -> Result<Vec<String>> {
         let after = self.after(unit, states);
         let parts: std::collections::BTreeSet<usize> = pieces.iter().map(|p| p.0).collect();
         let mut seen: Vec<(Vec<Vec<u64>>, Vec<Vec<u8>>)> = Vec::new();
@@ -966,7 +1260,10 @@ impl Player {
             if seen.iter().any(|(h, _)| *h == hashes) {
                 continue;
             }
-            let data = parts.iter().map(|&p| reader.bytes(&reader.parts[p]).to_vec()).collect();
+            let data = parts
+                .iter()
+                .map(|&p| reader.bytes(&reader.parts[p]).to_vec())
+                .collect();
             seen.push((hashes, data));
             if seen.len() == 2 {
                 break;
@@ -978,9 +1275,16 @@ impl Player {
             && let Some((p, part)) = reader.parts.iter().enumerate().find(|(_, p)| p.id == *args)
         {
             let bytes = &reader.bytes(part)[*at as usize..];
-            let words: Vec<u32> = bytes.chunks_exact(4).take(8).map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default())).collect();
+            let words: Vec<u32> = bytes
+                .chunks_exact(4)
+                .take(8)
+                .map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default()))
+                .collect();
             let each: HashSet<&Vec<u64>> = seen.iter().map(|s| &s.0[p]).collect();
-            out.push(format!("arguments {words:x?} (the same in all runs: {})", each.len() == 1));
+            out.push(format!(
+                "arguments {words:x?} (the same in all runs: {})",
+                each.len() == 1
+            ));
         }
         self.at = after;
         let [(_, a), (_, b)] = &seen[..] else {
@@ -988,13 +1292,19 @@ impl Player {
             return Ok(out);
         };
         // And without the command.
-        let before = Unit { to: unit.to - 1, ..*unit };
+        let before = Unit {
+            to: unit.to - 1,
+            ..*unit
+        };
         self.put_back(states)?;
         self.go(&before, wait)?;
         self.at = self.after(&before, states);
         let now = self.at.clone();
         self.hashes_now(reader, &now)?;
-        let without: Vec<Vec<u8>> = parts.iter().map(|&p| reader.bytes(&reader.parts[p]).to_vec()).collect();
+        let without: Vec<Vec<u8>> = parts
+            .iter()
+            .map(|&p| reader.bytes(&reader.parts[p]).to_vec())
+            .collect();
         let to = self.folder.join("diverge");
         let _ = std::fs::create_dir_all(&to);
         for (k, &p) in parts.iter().enumerate().take(12) {
@@ -1002,13 +1312,28 @@ impl Player {
             // Both contents, to look at.
             let sub = part.sub.map_or(0, |s| s.0);
             let _ = std::fs::write(to.join(format!("{}-{sub}-0.bin", part.id)), &without[k]);
-            let changed = |v: &[u8]| v.chunks_exact(4).zip(without[k].chunks_exact(4)).filter(|(x, y)| x != y).count();
-            out.push(format!("it changes {} dwords in one run, {} in another", changed(&a[k]), changed(&b[k])));
+            let changed = |v: &[u8]| {
+                v.chunks_exact(4)
+                    .zip(without[k].chunks_exact(4))
+                    .filter(|(x, y)| x != y)
+                    .count()
+            };
+            out.push(format!(
+                "it changes {} dwords in one run, {} in another",
+                changed(&a[k]),
+                changed(&b[k])
+            ));
             let _ = std::fs::write(to.join(format!("{}-{sub}-a.bin", part.id)), &a[k]);
             let _ = std::fs::write(to.join(format!("{}-{sub}-b.bin", part.id)), &b[k]);
-            let words = |v: &[u8]| -> Vec<u32> { v.chunks_exact(4).map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default())).collect() };
+            let words = |v: &[u8]| -> Vec<u32> {
+                v.chunks_exact(4)
+                    .map(|c| u32::from_le_bytes(c.try_into().unwrap_or_default()))
+                    .collect()
+            };
             let (x, y) = (words(&a[k]), words(&b[k]));
-            let differ: Vec<usize> = (0..x.len().min(y.len())).filter(|&i| x[i] != y[i]).collect();
+            let differ: Vec<usize> = (0..x.len().min(y.len()))
+                .filter(|&i| x[i] != y[i])
+                .collect();
             if differ.is_empty() {
                 continue;
             }
@@ -1018,9 +1343,19 @@ impl Player {
                 s.sort_unstable();
                 s
             };
-            let order = if sorted(&x) == sorted(&y) { " (the same values in another order)" } else { "" };
-            let sub = part.sub.map_or(String::new(), |(s, f)| format!(" subresource {s} pitch {}", f.Footprint.RowPitch));
-            let sample: Vec<String> = differ.iter().take(8).map(|&i| format!("+{:#x}: {:08x} {:08x}", i * 4, x[i], y[i])).collect();
+            let order = if sorted(&x) == sorted(&y) {
+                " (the same values in another order)"
+            } else {
+                ""
+            };
+            let sub = part.sub.map_or(String::new(), |(s, f)| {
+                format!(" subresource {s} pitch {}", f.Footprint.RowPitch)
+            });
+            let sample: Vec<String> = differ
+                .iter()
+                .take(8)
+                .map(|&i| format!("+{:#x}: {:08x} {:08x}", i * 4, x[i], y[i]))
+                .collect();
             out.push(format!(
                 "writes {}{sub}: {} of {} dwords differ, {:#x}..{:#x}{order}: {}",
                 self.called(part.id),
@@ -1041,6 +1376,11 @@ impl Player {
 impl Reader {
     fn bytes(&self, p: &Part) -> &[u8] {
         // SAFETY: a part of a mapped block; copies into it have run.
-        unsafe { std::slice::from_raw_parts((self.blocks[p.block].1 + p.at as usize) as *const u8, p.len as usize) }
+        unsafe {
+            std::slice::from_raw_parts(
+                (self.blocks[p.block].1 + p.at as usize) as *const u8,
+                p.len as usize,
+            )
+        }
     }
 }

+ 291 - 0
crates/victory-gpu/src/replay/play/merge.rs

@@ -0,0 +1,291 @@
+//! What the CPU wrote, put in without what the card wrote.
+//!
+//! The host layer brings results of shaders back to title memory, and sends
+//! pieces of title memory up again (64 KiB at a time) when the title changed
+//! some of them. What goes up therefore holds results of the recorded frame.
+//! Shaders that give out places with atomics give other places in every run:
+//! the recorded results put over a replay's own leave lists and what points
+//! into them of two different frames. So where a copy brings memory of the
+//! CPU to a buffer the card writes, words the card has written since the
+//! start of the frame (which are not what was last put there) stay.
+
+use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile;
+use windows::Win32::Graphics::Direct3D::ID3DBlob;
+
+use super::*;
+
+const SOURCE: &str = r#"
+#define RS "RootConstants(num32BitConstants=2, b0), SRV(t0), UAV(u0), UAV(u1), UAV(u2)"
+cbuffer C : register(b0) { uint words; uint call; };
+ByteAddressBuffer from : register(t0);
+RWByteAddressBuffer to : register(u0);
+RWByteAddressBuffer base : register(u1);
+// For each call: words put, words of the card's left that were to get
+// another value, and of those the ones that were to get back what stood
+// there before the card wrote.
+RWByteAddressBuffer counts : register(u2);
+[RootSignature(RS)]
+[numthreads(64, 1, 1)]
+void main(uint3 id : SV_DispatchThreadID) {
+    if (id.x >= words) return;
+    uint at = id.x * 4;
+    uint v = from.Load(at), now = to.Load(at), was = base.Load(at), n;
+    if (now != was) {
+        if (v == now) return;
+        counts.InterlockedAdd(call * 12 + 4, 1, n);
+        if (v == was) counts.InterlockedAdd(call * 12 + 8, 1, n);
+        return;
+    }
+    if (v == now) return;
+    counts.InterlockedAdd(call * 12, 1, n);
+    to.Store(at, v);
+    base.Store(at, v);
+}
+"#;
+
+pub(super) struct Merger {
+    root: ID3D12RootSignature,
+    pso: ID3D12PipelineState,
+    /// For each buffer: what was last put there (at first, what it held at
+    /// the frame's start).
+    pub(super) bases: HashMap<u32, ID3D12Resource>,
+    pub(super) counts: ID3D12Resource,
+    /// The merges recorded: buffer, offset, length (in `CALLS` at the most).
+    pub(super) calls: std::cell::RefCell<Vec<(u32, u64, u64)>>,
+}
+
+const CALLS: u64 = 1 << 16;
+
+impl Player {
+    /// The copy of a piece of a card buffer's CPU side to it: its source's
+    /// address, then the buffer, offset, 0 and length.
+    pub(super) fn merged_copy(&self, c: &Cmd) -> Option<(u64, (u32, u64, u64, u64))> {
+        let Cmd::CopyBuffer {
+            dst,
+            dst_at,
+            src,
+            src_at,
+            len,
+        } = c
+        else {
+            return None;
+        };
+        if !self.merging
+            || !self.pairs(*src, *dst)
+            || dst_at != src_at
+            || dst_at % 4 != 0
+            || len % 4 != 0
+        {
+            return None;
+        }
+        self.merger.as_ref()?.bases.get(dst)?;
+        let from = self.objects.get(src)?.va + src_at;
+        Some((from, (*dst, *dst_at, 0, *len)))
+    }
+
+    /// Whether `src` is the CPU's side of card buffer `dst`.
+    fn pairs(&self, src: u32, dst: u32) -> bool {
+        let (Some(a), Some(b)) = (self.objects.get(&src), self.objects.get(&dst)) else {
+            return false;
+        };
+        let (a, b) = (
+            &self.frame.resources[a.index],
+            &self.frame.resources[b.index],
+        );
+        let cpu =
+            matches!(&a.place, Place::Committed { props, .. } if gpu::seen(props) == Seen::Cached);
+        cpu && b.name.ends_with("on the card")
+            && a.desc.width == b.desc.width
+            && self.card_writes.contains(&dst)
+    }
+
+    /// Records merges: (buffer, offset in it, offset from `from`, length).
+    ///
+    /// # Safety
+    /// An open list; `from` is the address of memory that holds the bytes.
+    pub(super) unsafe fn merge_all(
+        &self,
+        list: &ID3D12GraphicsCommandList,
+        from: u64,
+        all: &[(u32, u64, u64, u64)],
+    ) {
+        let Some(m) = &self.merger else { return };
+        // SAFETY: the caller's; live buffers.
+        unsafe {
+            list.SetComputeRootSignature(&m.root);
+            list.SetPipelineState(&m.pso);
+            for (id, at, source, len) in all {
+                let (Some(o), Some(base)) = (self.objects.get(id), m.bases.get(id)) else {
+                    continue;
+                };
+                let base = base.GetGPUVirtualAddress();
+                // At most 65,535 groups in a dispatch.
+                const MOST: u64 = 8 << 20;
+                let mut done = 0;
+                while done < *len {
+                    let n = MOST.min(len - done);
+                    let words = (n / 4) as u32;
+                    let mut calls = m.calls.borrow_mut();
+                    let call = (calls.len() as u64).min(CALLS - 1) as u32;
+                    if calls.len() < CALLS as usize {
+                        calls.push((*id, at + done, n));
+                    }
+                    drop(calls);
+                    list.SetComputeRoot32BitConstants(0, 2, [words, call].as_ptr().cast(), 0);
+                    list.SetComputeRootUnorderedAccessView(4, m.counts.GetGPUVirtualAddress());
+                    list.SetComputeRootShaderResourceView(1, from + source + done);
+                    list.SetComputeRootUnorderedAccessView(2, o.va + at + done);
+                    list.SetComputeRootUnorderedAccessView(3, base + at + done);
+                    list.Dispatch(words.div_ceil(64), 1, 1);
+                    done += n;
+                }
+            }
+        }
+    }
+
+    /// Switches merging on or off; makes what it needs at first.
+    pub(super) fn set_merging(&mut self, on: bool) -> Result<()> {
+        self.merging = on;
+        if let Some(m) = &self.merger {
+            m.calls.borrow_mut().clear();
+        }
+        if !on || self.merger.is_some() {
+            return Ok(());
+        }
+        let mut code: Option<ID3DBlob> = None;
+        // SAFETY: the source and names live through the call; the blob's own
+        // bytes.
+        let (root, pso) = unsafe {
+            D3DCompile(
+                SOURCE.as_ptr().cast(),
+                SOURCE.len(),
+                None,
+                None,
+                None,
+                windows::core::s!("main"),
+                windows::core::s!("cs_5_1"),
+                0,
+                0,
+                &mut code,
+                None,
+            )?;
+            let code = code.context("no merge shader")?;
+            let bytes = std::slice::from_raw_parts(
+                code.GetBufferPointer().cast::<u8>(),
+                code.GetBufferSize(),
+            );
+            let root: ID3D12RootSignature = self.device.CreateRootSignature(0, bytes)?;
+            let pso =
+                self.device
+                    .CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC {
+                        pRootSignature: std::mem::transmute_copy(&root),
+                        CS: D3D12_SHADER_BYTECODE {
+                            pShaderBytecode: bytes.as_ptr().cast(),
+                            BytecodeLength: bytes.len(),
+                        },
+                        ..Default::default()
+                    })?;
+            (root, pso)
+        };
+        let mut ids: HashSet<u32> = HashSet::new();
+        for c in self.commands.iter().flatten() {
+            if let Cmd::CopyBuffer { dst, src, .. } = c
+                && self.pairs(*src, *dst)
+            {
+                ids.insert(*dst);
+            }
+        }
+        ids.extend(
+            self.delta_parts
+                .iter()
+                .flatten()
+                .map(|(d, _)| d.res)
+                .filter(|id| self.card_writes.contains(id)),
+        );
+        let mut bases = HashMap::new();
+        let mut total = 0;
+        for id in ids {
+            if !self.masters.iter().any(|m| m.0 == id) {
+                continue;
+            }
+            let size = self.frame.resources[self.objects[&id].index].desc.width;
+            let flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
+            bases.insert(
+                id,
+                gpu::buffer(&self.device, D3D12_HEAP_TYPE_DEFAULT, size, flags)?.0,
+            );
+            total += size;
+        }
+        let masters: HashMap<u32, ID3D12Resource> = self.masters.iter().cloned().collect();
+        self.worker.run(|list| {
+            for (id, base) in &bases {
+                // SAFETY: an open list; live buffers of one size.
+                unsafe { list.CopyResource(base, &masters[id]) };
+            }
+        })?;
+        tracing::info!(
+            buffers = bases.len(),
+            mb = total >> 20,
+            "bases of merged copies"
+        );
+        let flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
+        let counts = gpu::buffer(&self.device, D3D12_HEAP_TYPE_DEFAULT, CALLS * 12, flags)?.0;
+        self.merger = Some(Merger {
+            root,
+            pso,
+            bases,
+            counts,
+            calls: Default::default(),
+        });
+        Ok(())
+    }
+
+    /// What the merges of the last run (of `loops`, which add up) found.
+    pub(super) fn merge_notes(&mut self, loops: u32) -> Result<Vec<String>> {
+        let Some(m) = &self.merger else {
+            return Ok(Vec::new());
+        };
+        let (counts, calls) = (m.counts.clone(), m.calls.borrow().clone());
+        let (back, data) = gpu::buffer(
+            &self.device,
+            D3D12_HEAP_TYPE_READBACK,
+            CALLS * 12,
+            D3D12_RESOURCE_FLAG_NONE,
+        )?;
+        self.worker.run(|list| {
+            // SAFETY: an open list; live buffers.
+            unsafe { list.CopyBufferRegion(&back, 0, &counts, 0, CALLS * 12) };
+        })?;
+        // SAFETY: the mapping; the copy into it has run.
+        let all = unsafe { std::slice::from_raw_parts(data.cast::<u32>(), CALLS as usize * 3) };
+        let each = |k: usize| {
+            all.iter()
+                .skip(k)
+                .step_by(3)
+                .map(|&n| u64::from(n))
+                .sum::<u64>()
+                / u64::from(loops.max(1))
+        };
+        let mut out = vec![format!(
+            "{} copies of what the CPU wrote to memory the card writes: {} words put; {} left as the card wrote them in this run, where other values were to go ({} of them the values from before the card wrote)",
+            calls.len(),
+            each(0),
+            each(1),
+            each(2)
+        )];
+        let mut worst: Vec<(u32, usize)> = (0..calls.len())
+            .map(|i| (all[i * 3 + 2], i))
+            .filter(|w| w.0 > 0)
+            .collect();
+        worst.sort_by(|a, b| b.0.cmp(&a.0));
+        for (n, i) in worst.into_iter().take(8) {
+            let (id, at, len) = calls[i];
+            out.push(format!(
+                "    values from before the card wrote: {} words, {} +{at:#x} {len:#x} bytes",
+                n / loops.max(1),
+                self.called(id)
+            ));
+        }
+        Ok(out)
+    }
+}

+ 3 - 1
crates/victory-gpu/src/replay/record.rs

@@ -609,7 +609,9 @@ impl Recorder {
                 // would be a call into what is there then.
                 if let Some(dead) = lock(&self.held).remove(&id) {
                     std::mem::forget(dead);
-                    self.problem("a resource died as the frame started: its contents may be missing");
+                    self.problem(
+                        "a resource died as the frame started: its contents may be missing",
+                    );
                 }
                 self.ids.remove(ptr, id);
                 write(&self.resources).remove(&id);

+ 1 - 3
crates/victory-gpu/src/replay/snap.rs

@@ -406,9 +406,7 @@ impl Capture {
                 }
                 // SAFETY: the reference just taken. (The count does not tell
                 // all that are dying: `Recorder::gone` takes those out.)
-                held.insert(res.id, unsafe {
-                    ID3D12Resource::from_raw(object.as_raw())
-                });
+                held.insert(res.id, unsafe { ID3D12Resource::from_raw(object.as_raw()) });
                 all.push(res.clone());
             }
         }

+ 24 - 0
docs/replay.md

@@ -64,6 +64,7 @@ with the presented one.
 | `--buffers vram` / `sysmem` | all buffers in the card's / the PC's memory |
 | `--one-submit` | one `ExecuteCommandLists`, with a full wait between the former ones |
 | `--only 3..7` | runs these lists; of the others only barriers |
+| `--no-merge` | copies of what the CPU wrote also overwrite the card's own results (below) |
 | `--skip-shader <text>` | leaves out what runs pipelines with the text in their name or DXIL hash (repeatable) |
 | `--no-restore` | leaves what a loop changed |
 | `--debug-layer` | D3D12's debug layer; its errors in the report |
@@ -73,6 +74,29 @@ leaves out waits or work can draw another picture and, with wild data, lose
 the device: save work in other programs first. Do not run the title at the
 same time.
 
+## Runs that differ
+
+Shaders that give out places with atomics (the title's culling) give other
+ones in every run. The host layer brings such results to title memory and
+sends 64 KiB pieces of it up again, so the recorded copies hold the recorded
+run's results. Put over a replay's own, lists and what points into them are of
+two runs, and things are drawn from other things' lists. So copies of what the
+CPU wrote leave alone words the card has written in this run (`merge.rs`; the
+report counts them). `--no-merge` runs the copies as recorded.
+
+| switch | does |
+|---|---|
+| `--wait-all` / `--wait-lists` | a full wait after every command / each list sent alone and waited for: if pictures still differ, waits are not what is missing |
+| `--diverge [runs]` | runs each list this often from the very same memory, finds the first commands that make different memory of it, and says what differs (same values in another order: places given out); contents go to `diverge` in the capture |
+| `--diverge-picture [runs]` | from each list on, runs the rest this often from the same memory: where the pictures start to agree, what lies before decides. `--lists 76 --commands 0..900 --step 8` goes into one list |
+| `--diverge 10 --lists 76 --draws <text>` | each draw of the pipelines named, alone, this often: how many outcomes, and what it reads |
+| `--watch <id>:<offset>:<bytes>` | which lists (with `--lists`: commands) change those words |
+| `--slots <list>:<command>` | the heap slots its constants name |
+| `--check-written` | that everything the frame changes is put back |
+| `--dump <text>` | the DXIL of the pipelines named, in `shaders` |
+
+The `--diverge` family runs the recorded copies without the merge.
+
 ## Limits
 
 - Not recorded, but reported when used: reserved (tiled) resources, bundles,