Replay: preserve global synchronization, float UAV clears and initial residency

(cherry picked from commit 35ff992b59d936bb4a0f50f7dc13f5232f27befe)
This commit is contained in:
txmazing authored and trustno1-coder committed 2026-11-27 17:51:24 -08:00
1 parent e68f48a75e
commit 9c76817074
7 files changed
+258 -35

No files matched your search

+84 -22
View File
@@ -631,15 +631,25 @@ impl Player {
*at = Instant::now();
};
let mut at = Instant::now();
player.make_resources(options.buffers, &options.only_buffers)?;
player.make_resources(options.buffers, &options.only_buffers).context("creating replay resources")?;
step("resources", &mut at);
player.make_pipelines(options.shaders.as_deref())?;
player.make_pipelines(options.shaders.as_deref()).context("creating replay pipelines")?;
step("pipelines", &mut at);
player.make_descriptors()?;
player.make_descriptors().context("creating replay descriptor heaps")?;
step("descriptors", &mut at);
player.load_contents()?;
if let Err(error) = player.load_contents() {
if let Some(queue) = &player.info {
for message in debug_messages(queue) {
tracing::error!("{message}");
}
}
if options.breadcrumbs {
log_replay_breadcrumbs(&player.device);
}
return Err(error).context("loading the frame's initial contents");
}
step("contents", &mut at);
player.make_masters(options.buffers, options.restore)?;
player.make_masters(options.buffers, options.restore).context("preparing replay restore copies")?;
step("copies to restore from", &mut at);
let steps: Vec<String> = steps
.iter()
@@ -756,8 +766,16 @@ impl Player {
_ => gpu::to_props(p),
}
};
// Flags a replay has no use for: sharing, display, not zeroed.
const DROP: i32 = 0x1 | 0x8 | 0x20 | 0x1000;
// The title makes its non-resident allocations resident separately.
// Replay does not record residency calls and needs them from its first copy.
const DROP: i32 = 0x1 | 0x8 | 0x20 | D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT.0
| D3D12_HEAP_FLAG_CREATE_NOT_ZEROED.0;
let nonresident = |flags: i32| flags & D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT.0 != 0;
let heaps = self.frame.heaps.iter().filter(|h| nonresident(h.flags)).count();
let resources = self.frame.resources.iter().filter(|r| {
matches!(r.place, Place::Committed { flags, .. } if nonresident(flags))
}).count();
tracing::debug!(heaps, resources, "recreating non-resident allocations as resident");
let used_by_buffers_only: HashSet<u32> = {
let mut textures = HashSet::new();
for r in &self.frame.resources {
@@ -1317,8 +1335,8 @@ impl Player {
"{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();
// CPU-only views for render targets, depth, and UAV clears.
let mut views: [HashSet<Slot>; 3] = Default::default();
for c in self.commands.iter().flatten() {
match c {
Cmd::Targets { colors, depth } => {
@@ -1327,6 +1345,7 @@ impl Player {
}
Cmd::ClearTarget { view, .. } => _ = views[0].insert(*view),
Cmd::ClearDepth { view, .. } => _ = views[1].insert(*view),
Cmd::ClearUavFloat { view, .. } => _ = views[2].insert(*view),
_ => {}
}
}
@@ -1337,6 +1356,7 @@ impl Player {
let kind = [
D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
D3D12_DESCRIPTOR_HEAP_TYPE_DSV,
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
][i];
let d = D3D12_DESCRIPTOR_HEAP_DESC {
Type: kind,
@@ -1370,7 +1390,7 @@ impl Player {
None
};
self.device.CreateRenderTargetView(res, d, at);
} else {
} else if i == 1 {
let d = s
.desc
.as_ptr()
@@ -1389,6 +1409,8 @@ impl Player {
None
};
self.device.CreateDepthStencilView(res, d, at);
} else {
self.view(s, at, false);
}
self.targets.insert(*s, at);
at.ptr += step;
@@ -1439,7 +1461,8 @@ impl Player {
}
for (n, batch) in batches.iter().enumerate() {
let k = n % stagings.len();
self.worker.wait(stagings[k].2)?;
self.worker.wait(stagings[k].2)
.with_context(|| format!("waiting to reuse staging for upload batch {n}"))?;
let map = stagings[k].1;
let next = AtomicUsize::new(0);
let failed: std::sync::Mutex<Option<std::io::Error>> = std::sync::Mutex::default();
@@ -1586,7 +1609,7 @@ impl Player {
}
}
}
})?;
}).context("uploading the recorded resource contents")?;
// To the states of the frame's start.
let mut back = Vec::new();
for r in &self.frame.resources {
@@ -1602,7 +1625,7 @@ impl Player {
// SAFETY: an open list; live resources.
unsafe { list.ResourceBarrier(all) };
}
})?;
}).context("restoring initial resource states after upload")?;
if missing > 0 {
self.note(format!(
"contents of {missing} parts of resources that could not be made are left out"
@@ -1656,7 +1679,7 @@ impl Player {
unsafe { list.CopyBufferRegion(&o.res, d.at, staging, *at, *len) };
}
}
})?;
}).context("uploading initial CPU writes")?;
// The later ones wait in a buffer of their own.
let mut later: Vec<((u64, Delta), u64, u64)> = Vec::new();
let mut total = 0u64;
@@ -1681,7 +1704,7 @@ impl Player {
// SAFETY: an open list; live buffers.
unsafe { list.CopyBufferRegion(&buffer, *to, staging, *at, *len) };
}
})?;
}).context("uploading later CPU writes")?;
}
Ok(())
}
@@ -1720,6 +1743,7 @@ impl Player {
}
Cmd::Indirect { args, count, .. } => named.extend([*args, *count]),
Cmd::ResolveQuery { dst, .. } => _ = named.insert(*dst),
Cmd::ClearUavFloat { view, .. } => _ = named.insert(view.res),
_ => {}
}
}
@@ -1789,6 +1813,7 @@ impl Player {
Cmd::CopyBuffer { dst, .. }
| Cmd::CopyResource { dst, .. }
| Cmd::ResolveQuery { dst, .. } => _ = written.insert(*dst),
Cmd::ClearUavFloat { view, .. } => _ = written.insert(view.res),
Cmd::CopyTexture {
dst: Loc::Sub { res, .. } | Loc::Placed { res, .. },
..
@@ -2289,7 +2314,7 @@ impl Player {
.map(|c| {
2 + if o.detailed {
c.iter()
.filter(|c| c.works() || matches!(c, Cmd::Barriers { .. }))
.filter(|c| c.works() || c.is_barrier())
.count()
} else {
0
@@ -2574,12 +2599,12 @@ impl Player {
// SAFETY: an open list.
unsafe { self.wait_for_all(list) };
}
let timed = c.works() || matches!(c, Cmd::Barriers { .. });
let timed = c.works() || c.is_barrier();
if timed && let Some((heap, next)) = &mut stamp {
// SAFETY: as above.
unsafe { list.EndQuery(*heap, D3D12_QUERY_TYPE_TIMESTAMP, **next) };
let category = match c {
Cmd::Barriers { .. } => "barriers",
Cmd::Barriers { .. } | Cmd::GlobalSync {} => "barriers",
Cmd::Dispatch { .. } if clamps.contains(&bound) => "argument clamps (ours)",
Cmd::Dispatch { .. } if compares.contains(&bound) => {
"hash and compare passes (ours)"
@@ -2593,7 +2618,7 @@ impl Player {
Cmd::Draw { .. } | Cmd::DrawIndexed { .. } | Cmd::DispatchMesh { .. } => {
"draws"
}
Cmd::ClearDepth { .. } | Cmd::ClearTarget { .. } | Cmd::Discard { .. } => {
Cmd::ClearDepth { .. } | Cmd::ClearTarget { .. } | Cmd::ClearUavFloat { .. } | Cmd::Discard { .. } => {
"clears"
}
Cmd::Resolve { .. } | Cmd::ResolveRegion { .. } => "resolves",
@@ -2806,6 +2831,7 @@ impl Player {
all.iter().filter_map(|b| self.barrier(b)).collect();
barriers(l, &all);
}
Cmd::GlobalSync {} => self.wait_for_all(l),
Cmd::Heaps { heaps } => {
let all: Vec<Option<ID3D12DescriptorHeap>> = heaps
.iter()
@@ -2943,6 +2969,18 @@ impl Player {
l.ClearRenderTargetView(view(v), color, rects);
}
}
Cmd::ClearUavFloat { heap, index, view: v, color, rects } => {
if let Some((versions, step)) = self.descriptors.get(heap)
&& let Some(version) = versions.get(version)
&& let (Some(resource), Some(cpu)) = (res(v.res), self.targets.get(v))
{
let gpu = D3D12_GPU_DESCRIPTOR_HANDLE {
ptr: version.gpu + u64::from(*index) * step,
};
let rects: Vec<RECT> = rects.iter().map(|r| to_rect(*r)).collect();
l.ClearUnorderedAccessViewFloat(gpu, *cpu, resource, color, &rects);
}
}
Cmd::Discard {
res: id,
rects,
@@ -3160,10 +3198,10 @@ impl Player {
/// One run of the frame, noted in the report; its timestamps.
fn once(&mut self, o: &Options, (built, report): &mut (Sends, Report)) -> Result<Vec<u64>> {
self.restore(o.restore)?;
self.restore(o.restore).context("restoring the recorded frame")?;
self.fresh = false;
let stamps = self.send(built, o)?;
if let Some((db, _, hash, _)) = self.picture_now()? {
let stamps = self.send(built, o).context("executing recorded command lists")?;
if let Some((db, _, hash, _)) = self.picture_now().context("reading the replayed picture")? {
report.pictures.push((db, hash));
}
let ms = |a: u32, b: u32| {
@@ -3545,6 +3583,30 @@ fn make_device(frame: &Frame, debug: bool, breadcrumbs: bool) -> Result<(ID3D12D
}
}
fn log_replay_breadcrumbs(device: &ID3D12Device) {
let Ok(dred) = device.cast::<ID3D12DeviceRemovedExtendedData1>() else { return };
// SAFETY: DRED's arrays and linked nodes live as long as the device.
unsafe {
if let Ok(fault) = dred.GetPageFaultAllocationOutput1()
&& fault.PageFaultVA != 0
{
tracing::error!(va = format_args!("{:#x}", fault.PageFaultVA), "replay GPU page fault");
}
if let Ok(crumbs) = dred.GetAutoBreadcrumbsOutput() {
let mut node = crumbs.pHeadAutoBreadcrumbNode;
for _ in 0..32 {
let Some(crumb) = node.as_ref() else { break };
let done = crumb.pLastBreadcrumbValue.as_ref().copied().unwrap_or(0);
if done < crumb.BreadcrumbCount && !crumb.pCommandHistory.is_null() {
let op = *crumb.pCommandHistory.add(done as usize);
tracing::error!(done, total = crumb.BreadcrumbCount, op = op.0, "incomplete replay command list");
}
node = crumb.pNext;
}
}
}
}
/// 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();