victory replay, larger tests of it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
59a556e4b0
commit
048201b13c
9 files changed
+938
-52
No files matched your search
@@ -1303,7 +1303,14 @@ fn timing() -> bool {
|
||||
pub(crate) fn name(object: &impl Interface, text: impl FnOnce() -> String) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
if crate::replay::record::on() {
|
||||
crate::replay::record::name(object, &text());
|
||||
let text = text();
|
||||
crate::replay::record::name(object, &text);
|
||||
if dred_on()
|
||||
&& let Ok(object) = object.cast::<ID3D12Object>()
|
||||
{
|
||||
// SAFETY: a plain call on a live object.
|
||||
let _ = unsafe { object.SetName(&windows::core::HSTRING::from(text)) };
|
||||
}
|
||||
return;
|
||||
}
|
||||
if dred_on()
|
||||
|
||||
@@ -345,7 +345,7 @@ impl Player {
|
||||
player.make_masters()?;
|
||||
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();
|
||||
tracing::info!(seconds = began.elapsed().as_secs_f64(), "capture loaded: {}", steps.join(", "));
|
||||
player.notes.push(format!("loaded in {:.1} s: {}", began.elapsed().as_secs_f64(), steps.join(", ")));
|
||||
Ok(player)
|
||||
}
|
||||
|
||||
@@ -635,9 +635,15 @@ impl Player {
|
||||
// version of the heaps of its own: lists are sent without waiting,
|
||||
// so a heap cannot be written between them.
|
||||
let mut now: HashMap<(u32, u32), Slot> = HashMap::new();
|
||||
let replaced: HashSet<(u32, u32)> = self.frame.submits.iter().flat_map(|s| s.views.iter().map(|v| (v.0, v.1))).collect();
|
||||
for h in &self.frame.descriptor_heaps {
|
||||
for (first, n, s) in &h.runs {
|
||||
now.extend((*first..first + n).map(|i| ((h.id, i), *s)));
|
||||
for (id, i) in replaced.iter().filter(|k| k.0 == h.id) {
|
||||
let k = h.runs.partition_point(|run| run.0 <= *i);
|
||||
if let Some((first, n, s)) = k.checked_sub(1).and_then(|k| h.runs.get(k))
|
||||
&& i - first < *n
|
||||
{
|
||||
now.insert((*id, *i), *s);
|
||||
}
|
||||
}
|
||||
}
|
||||
// What each version changes against the one before.
|
||||
@@ -660,6 +666,7 @@ impl Player {
|
||||
self.version_of.push(versions.len() - 1);
|
||||
}
|
||||
let mut made = HashMap::new();
|
||||
let mut full = 0;
|
||||
for h in &self.frame.descriptor_heaps {
|
||||
let sampler = h.kind == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER.0;
|
||||
let kind = D3D12_DESCRIPTOR_HEAP_TYPE(h.kind);
|
||||
@@ -703,7 +710,20 @@ impl Player {
|
||||
all.push(same);
|
||||
continue;
|
||||
}
|
||||
let heap = make(D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)?;
|
||||
let heap = match make(D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE) {
|
||||
Ok(heap) => heap,
|
||||
// Room for no more of them: the changes go into the
|
||||
// last one, and lists before see them too.
|
||||
Err(_) if !all.is_empty() => {
|
||||
full += 1;
|
||||
let Some(Version { heap, gpu }) = all.last() else { break };
|
||||
let same = Version { heap: ID3D12DescriptorHeap::clone(heap), gpu: *gpu };
|
||||
self.device.CopyDescriptorsSimple(h.count, same.heap.GetCPUDescriptorHandleForHeapStart(), start, kind);
|
||||
all.push(same);
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
self.device.CopyDescriptorsSimple(h.count, heap.GetCPUDescriptorHandleForHeapStart(), start, kind);
|
||||
let gpu = heap.GetGPUDescriptorHandleForHeapStart().ptr;
|
||||
all.push(Version { heap, gpu });
|
||||
@@ -712,6 +732,12 @@ impl Player {
|
||||
}
|
||||
}
|
||||
self.descriptors = made;
|
||||
if versions.len() > 1 {
|
||||
self.notes.push(format!("{} versions of the descriptor heaps (views replaced during the frame)", versions.len()));
|
||||
}
|
||||
if full > 0 {
|
||||
self.note(format!("{full} versions of descriptor heaps had no room: earlier lists see later views"));
|
||||
}
|
||||
// Render target and depth views.
|
||||
let mut views: [HashSet<Slot>; 2] = Default::default();
|
||||
for c in self.commands.iter().flatten() {
|
||||
@@ -839,11 +865,12 @@ impl Player {
|
||||
fn load_contents(&mut self) -> Result<()> {
|
||||
// Idle aliases first: a change of their state may write memory the
|
||||
// one in use holds.
|
||||
let known: HashSet<u32> = self.frame.contents.iter().map(|c| c.res).collect();
|
||||
let mut early = Vec::new();
|
||||
let mut into = Vec::new();
|
||||
for r in &self.frame.resources {
|
||||
let (Some(o), Some(state)) = (self.objects.get(&r.id), self.start.get(&r.id)) else { continue };
|
||||
let loaded = !r.idle && self.frame.contents.iter().any(|c| c.res == r.id);
|
||||
let loaded = !r.idle && known.contains(&r.id);
|
||||
gpu::moves(&o.res, &[COMMON], if loaded { &[COPY_DEST] } else { state }, if loaded { &mut into } else { &mut early });
|
||||
}
|
||||
let textures: Vec<(Content, u64, u64)> = self
|
||||
@@ -890,7 +917,7 @@ impl Player {
|
||||
let mut back = Vec::new();
|
||||
for r in &self.frame.resources {
|
||||
let (Some(o), Some(state)) = (self.objects.get(&r.id), self.start.get(&r.id)) else { continue };
|
||||
if !r.idle && self.frame.contents.iter().any(|c| c.res == r.id) {
|
||||
if !r.idle && known.contains(&r.id) {
|
||||
gpu::moves(&o.res, &[COPY_DEST], state, &mut back);
|
||||
}
|
||||
}
|
||||
@@ -906,7 +933,30 @@ impl Player {
|
||||
// What the CPU had put into the memory both see by the first
|
||||
// submission: part of the start of the frame.
|
||||
let file = File::open(self.folder.join("deltas.bin"))?;
|
||||
let first: Vec<(Delta, u64, u64)> = self.frame.submits[0].deltas.iter().map(|d| (d.clone(), d.len, d.file)).collect();
|
||||
let named = self.named_buffers();
|
||||
let of = |s: &wire::Submit| s.deltas.iter().filter(|d| named.contains(&d.res)).cloned().collect::<Vec<Delta>>();
|
||||
// In memory the card cannot write, bytes looked at first when a
|
||||
// later submission read them can stand there from the start.
|
||||
let upload: HashSet<u32> = self
|
||||
.frame
|
||||
.resources
|
||||
.iter()
|
||||
.filter(|r| matches!(&r.place, Place::Committed { props, .. } if props.kind == D3D12_HEAP_TYPE_UPLOAD.0))
|
||||
.map(|r| r.id)
|
||||
.collect();
|
||||
let mut first = of(&self.frame.submits[0]);
|
||||
let mut seen: HashSet<(u32, u64)> = HashSet::new();
|
||||
let pages = |d: &Delta| (d.at / 4096..(d.at + d.len).div_ceil(4096)).map(|p| (d.res, p)).collect::<Vec<_>>();
|
||||
first.iter().for_each(|d| seen.extend(pages(d)));
|
||||
let mut rest: Vec<Vec<Delta>> = vec![Vec::new()];
|
||||
for s in self.frame.submits.iter().skip(1) {
|
||||
let (early, late): (Vec<Delta>, Vec<Delta>) =
|
||||
of(s).into_iter().partition(|d| upload.contains(&d.res) && pages(d).iter().all(|p| !seen.contains(p)));
|
||||
early.iter().for_each(|d| seen.extend(pages(d)));
|
||||
first.extend(early);
|
||||
rest.push(late);
|
||||
}
|
||||
let first: Vec<(Delta, u64, u64)> = first.into_iter().map(|d| (d.clone(), d.len, d.file)).collect();
|
||||
self.upload(&first, &file, |player, list, parts, staging| {
|
||||
for (d, len, at) in parts {
|
||||
if let Some(o) = player.objects.get(&d.res) {
|
||||
@@ -919,11 +969,11 @@ impl Player {
|
||||
let mut later: Vec<((u64, Delta), u64, u64)> = Vec::new();
|
||||
let mut total = 0u64;
|
||||
self.delta_parts = vec![Vec::new(); self.frame.submits.len()];
|
||||
for (i, s) in self.frame.submits.iter().enumerate().skip(1) {
|
||||
for d in &s.deltas {
|
||||
for (i, all) in rest.into_iter().enumerate() {
|
||||
for d in all {
|
||||
later.push(((total, d.clone()), d.len, d.file));
|
||||
self.delta_parts[i].push((d.clone(), total));
|
||||
total += d.len;
|
||||
self.delta_parts[i].push((d, total - later.last().map_or(0, |l| l.1)));
|
||||
}
|
||||
}
|
||||
if total > 0 {
|
||||
@@ -939,6 +989,46 @@ impl Player {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The buffers the frame names, in commands or descriptors: what the CPU
|
||||
/// writes to others (buffers it has results fetched to) is of no use.
|
||||
fn named_buffers(&self) -> HashSet<u32> {
|
||||
let mut named = HashSet::new();
|
||||
let mut va = |va: &Va| {
|
||||
if let Va::At { res, .. } = va {
|
||||
named.insert(*res);
|
||||
}
|
||||
};
|
||||
for c in self.commands.iter().flatten() {
|
||||
match c {
|
||||
Cmd::RootView { va: v, .. } => va(v),
|
||||
Cmd::IndexBuffer { view: Some((v, _, _)) } => va(v),
|
||||
Cmd::VertexBuffers { views: Some(all), .. } => all.iter().for_each(|(v, _, _)| va(v)),
|
||||
Cmd::WriteImmediate { all } => all.iter().for_each(|(v, _, _)| va(v)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for c in self.commands.iter().flatten() {
|
||||
match c {
|
||||
Cmd::CopyBuffer { dst, src, .. } | Cmd::CopyResource { dst, src } => named.extend([*dst, *src]),
|
||||
Cmd::CopyTexture { dst, src, .. } => {
|
||||
for Loc::Sub { res, .. } | Loc::Placed { res, .. } in [dst, src] {
|
||||
named.insert(*res);
|
||||
}
|
||||
}
|
||||
Cmd::Indirect { args, count, .. } => named.extend([*args, *count]),
|
||||
Cmd::ResolveQuery { dst, .. } => drop(named.insert(*dst)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for h in &self.frame.descriptor_heaps {
|
||||
named.extend(h.runs.iter().flat_map(|(_, _, s)| [s.res, s.counter]));
|
||||
}
|
||||
for s in &self.frame.submits {
|
||||
named.extend(s.views.iter().flat_map(|(_, _, s)| [s.res, s.counter]));
|
||||
}
|
||||
named
|
||||
}
|
||||
|
||||
/// Copies of what the frame changes; and the states it leaves.
|
||||
fn make_masters(&mut self) -> Result<()> {
|
||||
let mut states = States::default();
|
||||
@@ -991,7 +1081,7 @@ impl Player {
|
||||
}
|
||||
states.decay();
|
||||
if i > 0 {
|
||||
written.extend(s.deltas.iter().map(|d| d.res));
|
||||
written.extend(self.delta_parts[i].iter().map(|(d, _)| d.res));
|
||||
}
|
||||
}
|
||||
// Buffers any unordered-access view names.
|
||||
@@ -1759,8 +1849,7 @@ impl Player {
|
||||
}
|
||||
self.picture(o, &mut report)?;
|
||||
if let Some(q) = &self.info {
|
||||
let n = debug_messages(q);
|
||||
report.notes.push(format!("{n} errors from the debug layer"));
|
||||
report.notes.extend(debug_messages(q));
|
||||
}
|
||||
Ok(report)
|
||||
}
|
||||
@@ -1822,6 +1911,11 @@ impl Player {
|
||||
/// What a resource holds now (after a run): a buffer's bytes, or a
|
||||
/// texture's first subresource in its copyable layout.
|
||||
pub fn contents(&mut self, id: u32) -> Result<Option<Vec<u8>>> {
|
||||
self.contents_of(id, 0)
|
||||
}
|
||||
|
||||
/// As [`Player::contents`], of a texture's subresource `sub`.
|
||||
pub fn contents_of(&mut self, id: u32, sub: u32) -> Result<Option<Vec<u8>>> {
|
||||
let Some(o) = self.objects.get(&id) else {
|
||||
return Ok(None);
|
||||
};
|
||||
@@ -1830,21 +1924,21 @@ impl Player {
|
||||
let is_buffer = r.desc.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0;
|
||||
let states = if self.fresh { &self.start } else { &self.end };
|
||||
let state = states.get(&id).cloned().unwrap_or(vec![COMMON]);
|
||||
let (f, total) = if is_buffer { (Vec::new(), r.desc.width) } else { gpu::footprints(&self.device, &desc, 0, 1) };
|
||||
let (f, total) = if is_buffer { (Vec::new(), r.desc.width) } else { gpu::footprints(&self.device, &desc, sub, 1) };
|
||||
let (back, data) = gpu::buffer(&self.device, D3D12_HEAP_TYPE_READBACK, total, D3D12_RESOURCE_FLAG_NONE)?;
|
||||
self.worker.run(|list| {
|
||||
if is_buffer {
|
||||
// SAFETY: an open list; live buffers.
|
||||
unsafe { list.CopyBufferRegion(&back, 0, &res, 0, total) };
|
||||
} else if state.len() == 1 || subs == 1 {
|
||||
read_sub(list, &res, 0, state[0], &back, f[0]);
|
||||
read_sub(list, &res, sub, state[0], &back, f[0]);
|
||||
} else {
|
||||
let (mut before, mut after) = (Vec::new(), Vec::new());
|
||||
gpu::moves(&res, &state, &[COPY_SOURCE], &mut before);
|
||||
gpu::moves(&res, &[COPY_SOURCE], &state, &mut after);
|
||||
// SAFETY: an open list; live resources.
|
||||
unsafe { barriers(list, &before) };
|
||||
read_sub(list, &res, 0, COPY_SOURCE, &back, f[0]);
|
||||
read_sub(list, &res, sub, COPY_SOURCE, &back, f[0]);
|
||||
// SAFETY: as above.
|
||||
unsafe { barriers(list, &after) };
|
||||
}
|
||||
@@ -1969,10 +2063,9 @@ fn make_device(frame: &Frame, debug: bool) -> Result<(ID3D12Device, Option<ID3D1
|
||||
}
|
||||
}
|
||||
|
||||
/// Logs the debug layer's messages; how many errors.
|
||||
fn debug_messages(q: &ID3D12InfoQueue) -> usize {
|
||||
let mut errors = 0;
|
||||
let mut seen: HashMap<i32, u32> = HashMap::new();
|
||||
/// The debug layer's errors: how many of each kind, and its first text.
|
||||
fn debug_messages(q: &ID3D12InfoQueue) -> Vec<String> {
|
||||
let mut seen: BTreeMap<i32, (u32, String)> = BTreeMap::new();
|
||||
// SAFETY: messages read into buffers of the size asked for.
|
||||
unsafe {
|
||||
for i in 0..q.GetNumStoredMessages() {
|
||||
@@ -1987,18 +2080,14 @@ fn debug_messages(q: &ID3D12InfoQueue) -> usize {
|
||||
}
|
||||
let m = &*message;
|
||||
if m.Severity.0 <= D3D12_MESSAGE_SEVERITY_ERROR.0 {
|
||||
errors += 1;
|
||||
let n = seen.entry(m.ID.0).or_default();
|
||||
*n += 1;
|
||||
if *n <= 3 {
|
||||
let text = std::slice::from_raw_parts(m.pDescription, m.DescriptionByteLength);
|
||||
tracing::warn!("D3D12 {}: {}", m.ID.0, String::from_utf8_lossy(text).trim_end_matches('\0'));
|
||||
}
|
||||
let text = std::slice::from_raw_parts(m.pDescription, m.DescriptionByteLength);
|
||||
let text = || String::from_utf8_lossy(text).trim_end_matches('\0').to_owned();
|
||||
seen.entry(m.ID.0).or_insert_with(|| (0, text())).0 += 1;
|
||||
}
|
||||
}
|
||||
q.ClearStoredMessages();
|
||||
}
|
||||
errors
|
||||
seen.into_iter().map(|(id, (n, text))| format!("debug layer: {n} errors {id}, first: {text}")).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -38,6 +38,9 @@ pub struct Settings {
|
||||
pub at: At,
|
||||
/// Frames to capture.
|
||||
pub frames: u32,
|
||||
/// Record lists from the start on, not only once a capture is near: for
|
||||
/// a title that submits lists it recorded long before.
|
||||
pub always: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -52,13 +55,14 @@ pub enum At {
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// From `VICTORY_RECORD`, `VICTORY_RECORD_AT` (seconds, or `f<frame>`)
|
||||
/// and `VICTORY_RECORD_FRAMES`.
|
||||
/// From `VICTORY_RECORD`, `VICTORY_RECORD_AT` (seconds, or `f<frame>`),
|
||||
/// `VICTORY_RECORD_FRAMES` and `VICTORY_RECORD_ALWAYS=1`.
|
||||
pub fn from_env() -> Option<Settings> {
|
||||
let folder = PathBuf::from(std::env::var_os("VICTORY_RECORD").filter(|v| !v.is_empty())?);
|
||||
let at = parse_at(&std::env::var("VICTORY_RECORD_AT").unwrap_or_default());
|
||||
let frames = std::env::var("VICTORY_RECORD_FRAMES").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(1);
|
||||
Some(Settings { folder, at, frames: std::cmp::max(frames, 1) })
|
||||
let always = std::env::var_os("VICTORY_RECORD_ALWAYS").is_some_and(|v| v == "1");
|
||||
Some(Settings { folder, at, frames: std::cmp::max(frames, 1), always })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -733,6 +737,7 @@ pub fn start(settings: Settings, device: &ID3D12Device) -> std::io::Result<PathB
|
||||
}
|
||||
let r = recorder().ok_or_else(|| std::io::Error::other("no recorder"))?;
|
||||
*lock(&r.device) = Some(device.clone());
|
||||
r.arm(r.settings.always);
|
||||
// The newer interfaces may be other pointers (they are the same one
|
||||
// without the debug layer).
|
||||
let mut all = vec![device.as_raw()];
|
||||
|
||||
@@ -15,7 +15,7 @@ use windows::core::Interface;
|
||||
use super::gpu::{self, Seen, Worker};
|
||||
use super::record::{At, Heap, Ours, Recorder, Res, lock, not_recorded_calls, read};
|
||||
use super::states::{COMMON, COPY_SOURCE, Change, States};
|
||||
use super::wire::{self, ALL, Cmd, Content, Delta, MISSING, NONE, Place, Slot, Va, ZERO, slot};
|
||||
use super::wire::{self, ALL, Cmd, Content, Delta, Loc, MISSING, NONE, Place, Slot, Va, ZERO, slot};
|
||||
|
||||
type Fault = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
@@ -94,7 +94,8 @@ fn stop(r: &Recorder, phase: &mut Phase, e: &(dyn std::error::Error + Send + Syn
|
||||
phase.stage = Stage::Done;
|
||||
r.watching.store(false, Ordering::SeqCst);
|
||||
r.arm(false);
|
||||
lock(&r.held).clear();
|
||||
let held = std::mem::take(&mut *lock(&r.held));
|
||||
drop(held);
|
||||
}
|
||||
|
||||
pub(super) fn present(r: &Recorder, texture: &ID3D12Resource, state: u32) {
|
||||
@@ -159,6 +160,69 @@ pub(super) fn present(r: &Recorder, texture: &ID3D12Resource, state: u32) {
|
||||
drop(finished);
|
||||
}
|
||||
|
||||
/// What a submission reads of buffers, by id: parts of them (copied from),
|
||||
/// or all (read some other way).
|
||||
#[derive(Default)]
|
||||
struct Reads {
|
||||
all: HashSet<u32>,
|
||||
parts: HashMap<u32, Vec<(u64, u64)>>,
|
||||
}
|
||||
|
||||
impl Reads {
|
||||
fn note(&mut self, cmds: &[Cmd]) {
|
||||
let (all, parts) = (&mut self.all, &mut self.parts);
|
||||
let mut whole = |va: &Va| {
|
||||
if let Va::At { res, .. } = va {
|
||||
all.insert(*res);
|
||||
}
|
||||
};
|
||||
for c in cmds {
|
||||
match c {
|
||||
Cmd::RootView { va, .. } | Cmd::IndexBuffer { view: Some((va, _, _)) } => whole(va),
|
||||
Cmd::VertexBuffers { views: Some(views), .. } => views.iter().for_each(|(va, _, _)| whole(va)),
|
||||
Cmd::Indirect { args, count, .. } => {
|
||||
for res in [*args, *count] {
|
||||
whole(&Va::At { res, offset: 0 });
|
||||
}
|
||||
}
|
||||
Cmd::CopyResource { src, .. } => whole(&Va::At { res: *src, offset: 0 }),
|
||||
Cmd::CopyBuffer { src, src_at, len, .. } => parts.entry(*src).or_default().push((*src_at, src_at + len)),
|
||||
Cmd::CopyTexture { src: Loc::Placed { res, offset, size, pitch, .. }, .. } => {
|
||||
// At the most: compressed formats have fewer rows.
|
||||
let len = u64::from(*pitch) * u64::from(size[1]) * u64::from(size[2].max(1));
|
||||
parts.entry(*res).or_default().push((*offset, offset + len));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The whole pages read of a buffer of `size` bytes; `viewed` by a
|
||||
/// descriptor, all of it is.
|
||||
fn pages(&self, id: u32, size: usize, viewed: bool) -> Vec<(usize, usize)> {
|
||||
if viewed || self.all.contains(&id) {
|
||||
return vec![(0, size)];
|
||||
}
|
||||
let Some(parts) = self.parts.get(&id) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut pages: Vec<(usize, usize)> = parts
|
||||
.iter()
|
||||
.map(|&(from, to)| (from as usize / PAGE * PAGE, (to as usize).next_multiple_of(PAGE).min(size)))
|
||||
.filter(|(from, to)| from < to)
|
||||
.collect();
|
||||
pages.sort_unstable();
|
||||
let mut out: Vec<(usize, usize)> = Vec::new();
|
||||
for (from, to) in pages {
|
||||
match out.last_mut() {
|
||||
Some(last) if from <= last.1 => last.1 = last.1.max(to),
|
||||
_ => out.push((from, to)),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// A buffer the CPU sees.
|
||||
struct CpuBuffer {
|
||||
id: u32,
|
||||
@@ -191,6 +255,8 @@ struct Capture {
|
||||
contents: Vec<Content>,
|
||||
buffers: Vec<CpuBuffer>,
|
||||
known: HashSet<u32>,
|
||||
/// Resources descriptors name, or named during the frame.
|
||||
viewed: HashSet<u32>,
|
||||
/// Read-back buffers for the slow ones: the buffer, its mapping, size,
|
||||
/// and how much of it is taken.
|
||||
blocks: Vec<(ID3D12Resource, usize, u64, u64)>,
|
||||
@@ -228,6 +294,7 @@ impl Capture {
|
||||
contents: Vec::new(),
|
||||
buffers: Vec::new(),
|
||||
known: HashSet::new(),
|
||||
viewed: HashSet::new(),
|
||||
blocks: Vec::new(),
|
||||
deltas: File::create(r.folder.join("deltas.bin"))?,
|
||||
delta_at: AtomicU64::new(0),
|
||||
@@ -464,22 +531,33 @@ impl Capture {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// What changed in buffers the CPU sees since the submission before.
|
||||
fn changed(&mut self, r: &Recorder) -> Result<Vec<Delta>, Fault> {
|
||||
/// What changed in the buffers the CPU sees, of what a submission reads
|
||||
/// of them, since they were last looked at (the first time: what is not
|
||||
/// zero).
|
||||
fn changed(&mut self, r: &Recorder, reads: &Reads) -> Result<Vec<Delta>, Fault> {
|
||||
self.new_buffers(r)?;
|
||||
if self.buffers.iter().any(|b| b.through.is_some()) {
|
||||
let parts: Vec<(usize, usize, usize)> = self
|
||||
.buffers
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(i, b)| reads.pages(b.id, b.size, self.viewed.contains(&b.id)).into_iter().map(move |(from, to)| (i, from, to)))
|
||||
.collect();
|
||||
if parts.iter().any(|p| self.buffers[p.0].through.is_some()) {
|
||||
let (buffers, blocks) = (&self.buffers, &self.blocks);
|
||||
self.worker.run(|list| {
|
||||
for b in buffers {
|
||||
if let Some((i, at)) = b.through {
|
||||
for &(i, from, to) in &parts {
|
||||
let b = &buffers[i];
|
||||
if let Some((block, at)) = b.through {
|
||||
// SAFETY: an open list and live buffers.
|
||||
unsafe { list.CopyBufferRegion(&blocks[i].0, at, &b.res, 0, b.size as u64) };
|
||||
unsafe { list.CopyBufferRegion(&blocks[block].0, at + from as u64, &b.res, from as u64, (to - from) as u64) };
|
||||
}
|
||||
}
|
||||
})?;
|
||||
}
|
||||
let chunks: Vec<(usize, usize)> =
|
||||
self.buffers.iter().enumerate().flat_map(|(i, b)| (0..b.size).step_by(CHUNK).map(move |at| (i, at))).collect();
|
||||
let chunks: Vec<(usize, usize, usize)> = parts
|
||||
.iter()
|
||||
.flat_map(|&(i, from, to)| (from..to).step_by(CHUNK).map(move |at| (i, at, CHUNK.min(to - at))))
|
||||
.collect();
|
||||
// Each chunk is one thread's; threads share what is kept by parts.
|
||||
struct Kept(*mut u8);
|
||||
// SAFETY: parts of it are used by one thread each.
|
||||
@@ -496,8 +574,7 @@ impl Capture {
|
||||
let mut mine = Vec::new();
|
||||
loop {
|
||||
let i = next.fetch_add(1, Ordering::Relaxed);
|
||||
let Some(&(b, at)) = chunks.get(i) else { break };
|
||||
let len = CHUNK.min(buffers[b].size - at);
|
||||
let Some(&(b, at, len)) = chunks.get(i) else { break };
|
||||
// SAFETY: a chunk of the buffer's memory (it may
|
||||
// change meanwhile: what is kept is what was read)
|
||||
// and of what is kept, which no other thread has.
|
||||
@@ -531,14 +608,14 @@ impl Capture {
|
||||
} else {
|
||||
self.timed("waiting for the card", |c| Ok(c.worker.idle()?))?;
|
||||
}
|
||||
let deltas = self.timed("what the CPU changed", |c| c.changed(r))?;
|
||||
// Descriptors last: their memory is ready now.
|
||||
// Descriptors first: what they name is looked at below.
|
||||
let mut written = std::mem::take(&mut *lock(&r.writes));
|
||||
let mut views = Vec::new();
|
||||
if first {
|
||||
let heaps: Vec<Arc<Heap>> = read(&r.descriptors).visible().cloned().collect();
|
||||
for h in heaps {
|
||||
let runs = runs_of(r, &h.all());
|
||||
self.viewed.extend(runs.iter().flat_map(|(_, _, s)| [s.res, s.counter]));
|
||||
self.heaps.push((h, runs));
|
||||
}
|
||||
} else {
|
||||
@@ -556,11 +633,18 @@ impl Capture {
|
||||
};
|
||||
for i in at..at + n {
|
||||
if seen.insert((heap, i)) {
|
||||
views.push((heap, i, resolved(r, h.get(i))));
|
||||
let s = resolved(r, h.get(i));
|
||||
self.viewed.extend([s.res, s.counter]);
|
||||
views.push((heap, i, s));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut reads = Reads::default();
|
||||
for l in lists.iter().flatten() {
|
||||
reads.note(&Cmd::all(l)?);
|
||||
}
|
||||
let deltas = self.timed("what the CPU changed", |c| c.changed(r, &reads))?;
|
||||
let mut numbers = Vec::new();
|
||||
for l in lists {
|
||||
numbers.push(match l {
|
||||
@@ -810,6 +894,11 @@ pub(super) fn read_sub(
|
||||
/// A slot as it goes into the file: a constant buffer view's address as
|
||||
/// buffer and offset.
|
||||
fn resolved(r: &Recorder, mut s: Slot) -> Slot {
|
||||
let kind = i32::from_le_bytes([s.desc[4], s.desc[5], s.desc[6], s.desc[7]]);
|
||||
if s.kind == slot::SRV && s.described != 0 && kind == D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE.0 {
|
||||
r.problem("views of acceleration structures are null in the capture");
|
||||
return Slot { kind: slot::SRV, ..Slot::EMPTY };
|
||||
}
|
||||
if s.kind == slot::CBV && s.described != 0 {
|
||||
match r.va(s.address()) {
|
||||
Va::At { res, offset } => {
|
||||
@@ -910,6 +999,32 @@ mod tests {
|
||||
assert!(find(&live, &mut kept).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_a_submission_reads() {
|
||||
let mut reads = Reads::default();
|
||||
let part = |from, len| Cmd::CopyBuffer { dst: 9, dst_at: 0, src: 1, src_at: from, len };
|
||||
reads.note(&[
|
||||
part(100, 50),
|
||||
part(4000, 200),
|
||||
part(20_000, 1),
|
||||
Cmd::CopyTexture {
|
||||
dst: Loc::Sub { res: 9, sub: 0 },
|
||||
at: [0; 3],
|
||||
src: Loc::Placed { res: 2, offset: 4096, format: 28, size: [16, 16, 1], pitch: 256 },
|
||||
part: None,
|
||||
},
|
||||
Cmd::Indirect { signature: 1, most: 1, args: 3, at: 8, count: 0, count_at: 0 },
|
||||
Cmd::RootView { graphics: true, kind: slot::CBV, param: 0, va: Va::At { res: 4, offset: 5 } },
|
||||
]);
|
||||
assert_eq!(reads.pages(1, 1 << 20, false), [(0, 8192), (16384, 20480)]);
|
||||
assert_eq!(reads.pages(1, 20_100, false), [(0, 8192), (16384, 20_100)], "its end");
|
||||
assert_eq!(reads.pages(2, 1 << 20, false), [(4096, 8192)]);
|
||||
assert_eq!(reads.pages(3, 100, false), [(0, 100)]);
|
||||
assert_eq!(reads.pages(4, 100, false), [(0, 100)]);
|
||||
assert_eq!(reads.pages(5, 100, true), [(0, 100)], "a descriptor names it");
|
||||
assert!(reads.pages(5, 100, false).is_empty() && reads.pages(9, 100, false).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zeros() {
|
||||
assert!(zero(&[]));
|
||||
|
||||
Reference in new issue
Block a user