diff --git a/README.md b/README.md index a4ff85178..624470b20 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,9 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder (decoded 8-bit R+G+B > 30). The field is `null` until a frame is sampled; `non_dark_samples` is `null` if its format cannot be decoded. Intermediate render-target dumps do not update it. Sampling uses every fourth pixel in each direction (`XCL_FRAME_DUMP_FULL=1`: every pixel). + `XCL_DUMP_TARGETS=[,]` logs, for the first frames with that many draws and dispatches, + each render target and the depth buffer drawn into (draws ran / stand-in / PC / skipped) and dumps + the eight drawn into most (`target_.bmp`); `XCL_DUMP_AFTER_S=` waits `s` seconds first. A coarse sample can miss detail; a non-dark sample does not establish correct rendering or gameplay. - Title data: `%LOCALAPPDATA%\XCL\\` (`%XCL_DATA_ROOT%`): `xcl.toml`, `overlay\`, `saves\`, `pls\` (the title's `S:\`), `temp\` (`T:\`), `cache\`, `logs\xcl.log`, `logs\report.json` (the diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index da5b15ef4..bea771359 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -263,6 +263,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us // doesn't match the currently set Pipeline State Object"; a retail title's world then // hung the GPU, 0x887a0006). Skipped, like a draw or dispatch of such a pipeline. if super::pso_async::is_pending(with(this, |s| s.pso)) { + ei_work(this, sig, super::targets::STANDIN); count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -275,6 +276,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us // can still be XCL's ABI one from a translated dispatch before it (debug layer 953). // A native PC compute pipeline is not one of these and still goes to the PC call. if signature_dispatches(sig) && super::real::is_console_compute(pso) { + super::targets::note_dispatch(); count(DISPATCHES_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -287,9 +289,11 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us if let (Some(list), Some(r)) = (ID3D12GraphicsCommandList::from_raw_borrowed(&this), ID3D12RootSignature::from_raw_borrowed(&rs)) { list.SetGraphicsRootSignature(r); } + ei_work(this, sig, super::targets::PC); return fresh_indirect_arguments(this, || pc(this, sig, max, args, args_off, count_buf, count_off)); } if ROOT_SIGNATURES.lock().unwrap().as_ref().is_some_and(|s| s.contains(&sig)) { + ei_work(this, sig, super::targets::SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { crate::xlog_warn!("D3D12.X: ExecuteIndirect that changes root arguments with a translated pipeline skipped"); @@ -299,10 +303,12 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us let Some(list) = ID3D12GraphicsCommandList::from_raw_borrowed(&this) else { return }; match bind_abi(this, list, pso, gfx) { Ok(()) => { + ei_work(this, sig, super::targets::RAN); count(if gfx { DRAWS } else { DISPATCHES }); fresh_indirect_arguments(this, || pc(this, sig, max, args, args_off, count_buf, count_off)) } Err(e) => { + ei_work(this, sig, super::targets::SKIPPED); count(if gfx { DRAWS_SKIPPED } else { DISPATCHES_SKIPPED }); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -312,6 +318,18 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us } } +/// `XCL_DUMP_TARGETS`: an ExecuteIndirect with `outcome` (a dispatch signature's: a dispatch) +unsafe fn ei_work(this: *mut c_void, sig: usize, outcome: usize) { + if !super::targets::enabled() { + return; + } + if signature_dispatches(sig) { + super::targets::note_dispatch(); + } else { + super::targets::note_draw(this, outcome); + } +} + /// XCL's own buffer for `fresh_indirect_arguments` (device, buffer): COMMON between its uses static ARGUMENT_ORDER: Mutex<Option<(usize, ID3D12Resource)>> = Mutex::new(None); @@ -497,6 +515,9 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<ID3D12GraphicsCommandList> let pso = with(this, |s| s.pso); // a pipeline still being built in the background is waited for (its stand-in draws nothing) if !super::real::is_translated_graphics(super::pso_async::resolve_wait(pso)) { + if super::targets::enabled() { + super::targets::note_draw(this, if super::pso_async::is_pending(pso) { super::targets::STANDIN } else { super::targets::PC }); + } count(DRAWS_PC); if super::present::trace_frame() { // which pipelines draw without a translation (first 12 distinct) @@ -527,10 +548,12 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<ID3D12GraphicsCommandList> } match bind_abi(this, &list, pso, true) { Ok(()) => { + super::targets::note_draw(this, super::targets::RAN); count(DRAWS); Some(list) } Err(e) => { + super::targets::note_draw(this, super::targets::SKIPPED); count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { @@ -801,6 +824,7 @@ pub(super) unsafe extern "system" fn set_pipeline_state(this: *mut c_void, pso: /// #14 Dispatch: with a translated pipeline, on XCL's ABI with the kept root arguments pub(super) unsafe extern "system" fn dispatch(this: *mut c_void, x: u32, y: u32, z: u32) { + super::targets::note_dispatch(); let pso = super::pso_async::resolve_wait(with(this, |s| s.pso)); let Some(list) = ID3D12GraphicsCommandList::from_raw_borrowed(&this) else { return }; if !super::real::is_translated_compute(pso) { diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index a4bc5d223..7ec9d639b 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -508,6 +508,24 @@ pub(super) unsafe fn note_render_targets(this: *mut c_void, n: u32, rtvs: *const with_bindings(this, |b| b.render_targets = Some(RenderTargets { num_targets, handles, single, dsv: d })); } +/// The render target views bound on `list` (each one of a single descriptor range too). +pub(super) unsafe fn bound_render_targets(list: *mut c_void) -> Vec<usize> { + static INC: AtomicU32 = AtomicU32::new(0); + let Some(rt) = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(list as usize))).and_then(|s| s.bindings.render_targets.clone()) else { + return Vec::new(); + }; + if rt.single == 0 || rt.num_targets <= 1 { + return rt.handles; + } + let Some(&base) = rt.handles.first() else { return Vec::new() }; + let mut inc = INC.load(Ordering::Relaxed) as usize; + if inc == 0 { + inc = device_of(list).map_or(0, |d| d.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV) as usize); + INC.store(inc as u32, Ordering::Relaxed); + } + (0..rt.num_targets as usize).map(|i| base + i * inc).collect() +} + /// Set again what `b` holds (after KickoffX's Reset or a deferred mid-list split). unsafe fn restore_bindings(this: *mut c_void, b: &Bindings, ty: D3D12_COMMAND_LIST_TYPE) { if ty == D3D12_COMMAND_LIST_TYPE_COPY { diff --git a/crates/xcl/src/hle/d3d12x/mod.rs b/crates/xcl/src/hle/d3d12x/mod.rs index f3f035798..c062d5c4a 100644 --- a/crates/xcl/src/hle/d3d12x/mod.rs +++ b/crates/xcl/src/hle/d3d12x/mod.rs @@ -28,6 +28,7 @@ pub mod pso_async; pub mod real; mod shader_cache_session; pub mod sparse; +mod targets; pub mod title_list; pub mod title_textures; pub mod vram; diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 01b3c92a4..ec5172f88 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -63,6 +63,7 @@ pub unsafe extern "system" fn present_x(queue: *mut c_void, plane_count: u32, pl unsafe fn present_x_frame(queue: *mut c_void, plane_count: u32, planes: *const u8) { let frames = BOUNDARIES.fetch_add(1, Ordering::Relaxed) + 1; + super::targets::frame_end(); if frames % 300 == 0 { // the title's frame rate, for comparing runs (every 300 frames) static LAST: Mutex<Option<std::time::Instant>> = Mutex::new(None); @@ -202,6 +203,15 @@ pub fn rtv_resource(handle: usize) -> Option<usize> { RTVS.lock().unwrap().as_ref()?.get(&handle).copied() } +/// The resource `r` while a view still names it (AddRef'd), for `XCL_DUMP_TARGETS`. +pub(super) unsafe fn view_resource(r: usize) -> Option<ID3D12Resource> { + let g = RTVS.lock().unwrap(); + if !g.as_ref()?.values().any(|&v| v == r) { + return None; + } + ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).cloned() +} + /// The resource bound as render target 0 on `list` (`XCL_TRACE_FRAME`). pub fn rt0_resource(list: usize) -> Option<usize> { let h = *LISTS.lock().unwrap().as_ref()?.get(&list)?; @@ -695,8 +705,18 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, usize)>) { let b1 = [transition(&bb, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_COPY_DEST)]; p.list.ResourceBarrier(&b1); p.list.CopyResource(&bb, &src); - let due = frame_dump_due(); + // `XCL_DUMP_TARGETS`: a busy frame's render targets go with its picture (copied in the + // state the title left them, as `XCL_TRACE_FRAME`'s; its frame dump is also the + // `last_frame_dump` of the report) + let targets = super::targets::take_pending(); + let dumping_targets = targets.is_some(); + let due = frame_dump_due() || dumping_targets; let mut dumps = Vec::new(); + for (tag, res, d) in targets.map_or(Vec::new(), |f| super::targets::to_dump(PRESENTS.load(Ordering::Relaxed), &f)) { + if let Some((buffer, footprint)) = readback_copy(&p.list, &res, &d) { + dumps.push(FrameDump { _source: res, buffer, footprint, format: d.Format, tag }); + } + } if due { if let Some((buffer, footprint)) = readback_copy(&p.list, &src, &desc) { dumps.push(FrameDump { _source: src.clone(), buffer, footprint, format: desc.Format, tag: "frame".into() }); @@ -744,6 +764,9 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, usize)>) { crate::xlog_error!("present: submit copy: {e}"); return; } + if dumping_targets { + super::targets::submitted(); + } if let Err(e) = sc.Present(0, DXGI_PRESENT(0)).ok() { let e = fence::api_error(&p.dev, "Present", e); p.disabled = matches!(&e, FenceError::Removed(_)); @@ -755,6 +778,9 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, usize)>) { match fence::wait(&p.fence, p.value, &p.dev, Some(p.event), 2000) { Ok(()) => { for dump in p.dumps.drain(..) { + if dump.tag.starts_with("target") { + super::targets::log_census(&dump.buffer, &dump.footprint, &dump.tag); + } write_frame(&dump.buffer, &dump.footprint, dump.format, &dump.tag); } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 014dfa4d9..5569c4405 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1971,6 +1971,11 @@ unsafe extern "system" fn dev_create_dsv_x(this: *mut c_void, res: *mut c_void, if copy.is_none() { copy = ID3D12Resource::from_raw_borrowed(&res).and_then(|r| default_dsv(&r.GetDesc())); } + // `XCL_DUMP_TARGETS`: the depth buffer a DSV names, kept with the RTVs' resources (DSV + // and RTV handles are in different heaps) + if super::targets::enabled() { + super::present::note_rtv(cpu as usize, res); + } f(this, res, copy.as_ref().map_or(desc, |c| c as *const _), cpu) } diff --git a/crates/xcl/src/hle/d3d12x/targets.rs b/crates/xcl/src/hle/d3d12x/targets.rs new file mode 100644 index 000000000..4cd15cb57 --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/targets.rs @@ -0,0 +1,366 @@ +//! `XCL_DUMP_TARGETS=<work>[,<frames>]`: what a busy frame draws into. In the first `frames` +//! frames (default 1) with at least `work` draws and dispatches, every render target the frame +//! drew into (and the depth buffer bound most) is logged with how its draws went – ran, drew +//! nothing (a stand-in while the pipeline is built), went to the PC untranslated, skipped – and +//! the eight drawn into most are written next to the presented frame (`target<k>_<n>.bmp`) with +//! how many of their texels are not zero and how many differ from the first one. A black picture +//! over a scene that was drawn tells a lost composite from a scene that never was. +//! `XCL_DUMP_AFTER_S=<s>`: no frame is dumped until `s` seconds after the first present. +//! +//! A draw counts for the frame whose PresentX follows its recording (bundles, and lists executed +//! twice or never, are not exact); an ExecuteIndirect counts once, whatever its count. +//! Multisampled and non-2D targets are logged as not dumped; an array dumps its subresource 0. +//! The depth buffer is not copied: XCL does not know the state the title left it in, and a copy +//! from DEPTH_WRITE (possibly compressed) is invalid – the present list would not close. +//! Targets are kept by address only (not AddRef'd): `present::view_resource` hands one out only +//! while a view still names it, and one released meanwhile is logged as released. + +use std::collections::HashMap; +use std::ffi::c_void; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Mutex, OnceLock}; +use std::time::{Duration, Instant}; + +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::*; + +/// targets dumped of one frame +const DUMPED: usize = 8; + +/// How a draw went: ran on its translated pipeline, drew nothing (a stand-in for a pipeline still +/// being built), went to the PC untranslated, or was skipped. +pub const RAN: usize = 0; +pub const STANDIN: usize = 1; +pub const PC: usize = 2; +pub const SKIPPED: usize = 3; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Config { + /// draws and dispatches a frame needs + work: u64, + /// frames to dump + frames: u64, + /// no dump before this long after the first present + after: Duration, +} + +fn config() -> Option<Config> { + static C: OnceLock<Option<Config>> = OnceLock::new(); + *C.get_or_init(|| { + let v = std::env::var("XCL_DUMP_TARGETS").ok()?; + let Some((work, frames)) = parse(&v) else { + crate::xlog_warn!("D3D12.X: XCL_DUMP_TARGETS={v:?} is not <draws and dispatches>[,<frames>]: no targets dumped"); + return None; + }; + let after = match std::env::var("XCL_DUMP_AFTER_S") { + Ok(s) => parse_after(&s).unwrap_or_else(|| { + crate::xlog_warn!("D3D12.X: XCL_DUMP_AFTER_S={s:?} is not a number of seconds: dumps start at once"); + Duration::ZERO + }), + Err(_) => Duration::ZERO, + }; + Some(Config { work, frames, after }) + }) +} + +fn parse(v: &str) -> Option<(u64, u64)> { + let mut it = v.split(',').map(|s| s.trim().parse::<u64>()); + let work = it.next()?.ok()?; + let frames = match it.next() { + Some(n) => n.ok()?, + None => 1, + }; + (work > 0 && frames > 0).then_some((work, frames)) +} + +/// `XCL_DUMP_AFTER_S`: seconds, fractions too (None: not a number of seconds) +fn parse_after(v: &str) -> Option<Duration> { + v.trim().parse::<f64>().ok().and_then(|s| Duration::try_from_secs_f64(s).ok()) +} + +pub fn enabled() -> bool { + config().is_some() +} + +/// What a frame drew: its draws and dispatches, and the draws into each target by outcome. +#[derive(Default, Debug, PartialEq)] +pub struct Frame { + work: u64, + targets: HashMap<usize, [u64; 4]>, + depth: HashMap<usize, [u64; 4]>, +} + +impl Frame { + fn draw(&mut self, targets: &[usize], depth: Option<usize>, outcome: usize) { + self.work += 1; + for &t in targets { + self.targets.entry(t).or_default()[outcome] += 1; + } + if let Some(d) = depth { + self.depth.entry(d).or_default()[outcome] += 1; + } + } + + /// targets by draws into them, most first (ties: by address, for a stable order) + fn ranked(&self) -> Vec<(usize, [u64; 4])> { + let mut v: Vec<(usize, [u64; 4])> = self.targets.iter().map(|(&t, &n)| (t, n)).collect(); + v.sort_by(|a, b| b.1.iter().sum::<u64>().cmp(&a.1.iter().sum()).then(a.0.cmp(&b.0))); + v + } + + /// the depth buffer drawn with most + fn depth(&self) -> Option<(usize, [u64; 4])> { + self.depth.iter().map(|(&d, &n)| (d, n)).max_by(|a, b| a.1.iter().sum::<u64>().cmp(&b.1.iter().sum()).then(b.0.cmp(&a.0))) + } +} + +/// the frame being recorded +static FRAME: Mutex<Option<Frame>> = Mutex::new(None); +/// frames dumped so far (their copies submitted) +static DONE: AtomicU64 = AtomicU64::new(0); +/// the frame to dump at the next present that gets as far as copying +static PENDING: Mutex<Option<Frame>> = Mutex::new(None); + +/// A draw on `list` with `outcome`: it counts for each render target and the depth buffer bound. +pub unsafe fn note_draw(list: *mut c_void, outcome: usize) { + if !enabled() { + return; + } + let mut targets: Vec<usize> = super::list_x::bound_render_targets(list).into_iter().filter_map(super::present::rtv_resource).collect(); + targets.sort_unstable(); + targets.dedup(); + let depth = super::list_x::bound_dsv(list).and_then(super::present::rtv_resource); + FRAME.lock().unwrap().get_or_insert_with(Frame::default).draw(&targets, depth, outcome); +} + +/// A dispatch: work of the frame. +pub fn note_dispatch() { + if enabled() { + FRAME.lock().unwrap().get_or_insert_with(Frame::default).work += 1; + } +} + +/// Whether a frame of `work` is dumped, `done` frames dumped before it, `since` the first present. +fn due(c: &Config, work: u64, done: u64, since: Duration) -> bool { + work >= c.work && done < c.frames && since >= c.after +} + +/// The title presents: a frame that did enough work is dumped at the next present that copies +/// (one still waiting for that is kept, and this one dropped). +pub fn frame_end() { + let Some(c) = config() else { return }; + static T0: OnceLock<Instant> = OnceLock::new(); + let t0 = *T0.get_or_init(Instant::now); + let f = FRAME.lock().unwrap().take().unwrap_or_default(); + if !due(&c, f.work, DONE.load(Ordering::Relaxed), t0.elapsed()) { + return; + } + PENDING.lock().unwrap().get_or_insert(f); +} + +/// The frame to dump at this present, if any (`submitted` once its copies are on the GPU). +pub fn take_pending() -> Option<Frame> { + if !enabled() { + return None; + } + PENDING.lock().unwrap().take() +} + +/// The copies of a frame taken by `take_pending` were submitted: it counts as dumped. +pub fn submitted() { + DONE.fetch_add(1, Ordering::Relaxed); +} + +/// Logs what frame `n` drew into; the targets to copy for its dump: (tag, resource, desc). +pub unsafe fn to_dump(n: u64, f: &Frame) -> Vec<(String, ID3D12Resource, D3D12_RESOURCE_DESC)> { + let ranked = f.ranked(); + crate::xlog_info!("D3D12.X: target dump of frame {n}: {} draws and dispatches, {} render targets drawn into", f.work, ranked.len()); + let mut out = Vec::new(); + let depth = f.depth().map(|(r, c)| (usize::MAX, r, c)); + for (k, r, c) in ranked.into_iter().enumerate().map(|(k, (r, c))| (k, r, c)).chain(depth) { + let res = super::present::view_resource(r); + let d = res.as_ref().map(|x| x.GetDesc()); + let what = d.map_or("released".into(), |d| format!("{}x{} format {}", d.Width, d.Height, d.Format.0)); + let copyable = d.is_some_and(|d| d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && d.SampleDesc.Count <= 1); + let tag = match (k, copyable) { + (usize::MAX, _) => " (not dumped: its state is not known)".into(), + (k, true) if k < DUMPED => format!(" → target{k}"), + (_, false) if d.is_some() => " (not dumped)".into(), + _ => String::new(), + }; + crate::xlog_info!( + "D3D12.X: target dump: {} {r:#x} {what}: {} draws (ran {}, stand-in {}, PC {}, skipped {}){tag}", + if k == usize::MAX { "depth buffer" } else { "render target" }, + c.iter().sum::<u64>(), + c[RAN], + c[STANDIN], + c[PC], + c[SKIPPED] + ); + if let (true, Some(res), Some(d)) = (k < DUMPED && copyable, res, d) { + out.push((format!("target{k}"), res, d)); + } + } + out +} + +/// Of the texels sampled in `data` (every 4th of every 4th row, as the dumps do): how many are not +/// all zero, how many differ from the first one sampled (a cleared target has none), and how many +/// were sampled; any format, by its texel size; texels past the end of `data` are not sampled. +fn census(data: &[u8], width: usize, height: usize, pitch: usize, format: DXGI_FORMAT) -> (u64, u64, u64) { + let bytes = texel_bytes(format); + let (mut lit, mut differ, mut n) = (0, 0, 0); + let mut first: Option<&[u8]> = None; + for y in (0..height).step_by(4) { + for x in (0..width).step_by(4) { + let at = y * pitch + x * bytes; + let Some(t) = data.get(at..at + bytes) else { continue }; + n += 1; + if t.iter().any(|&b| b != 0) { + lit += 1; + } + if *first.get_or_insert(t) != t { + differ += 1; + } + } + } + (lit, differ, n) +} + +/// bytes of a texel of a render-target format (or of the plane of a planar format a footprint +/// names: a copied depth-stencil's depth plane is R32 / R24X8 typeless, 4 bytes) +fn texel_bytes(f: DXGI_FORMAT) -> usize { + match f { + DXGI_FORMAT_R32G32B32A32_FLOAT | DXGI_FORMAT_R32G32B32A32_UINT | DXGI_FORMAT_R32G32B32A32_SINT | DXGI_FORMAT_R32G32B32A32_TYPELESS => 16, + DXGI_FORMAT_R16G16B16A16_FLOAT | DXGI_FORMAT_R16G16B16A16_UNORM | DXGI_FORMAT_R16G16B16A16_UINT | DXGI_FORMAT_R16G16B16A16_SNORM + | DXGI_FORMAT_R16G16B16A16_SINT | DXGI_FORMAT_R16G16B16A16_TYPELESS | DXGI_FORMAT_R32G32_FLOAT | DXGI_FORMAT_R32G32_UINT + | DXGI_FORMAT_R32G32_SINT | DXGI_FORMAT_R32G32_TYPELESS | DXGI_FORMAT_D32_FLOAT_S8X24_UINT | DXGI_FORMAT_R32G8X24_TYPELESS => 8, + DXGI_FORMAT_R8G8_UNORM | DXGI_FORMAT_R8G8_UINT | DXGI_FORMAT_R8G8_SNORM | DXGI_FORMAT_R8G8_SINT | DXGI_FORMAT_R8G8_TYPELESS + | DXGI_FORMAT_R16_FLOAT | DXGI_FORMAT_R16_UNORM | DXGI_FORMAT_R16_UINT | DXGI_FORMAT_R16_SNORM | DXGI_FORMAT_R16_SINT + | DXGI_FORMAT_R16_TYPELESS | DXGI_FORMAT_D16_UNORM | DXGI_FORMAT_B5G6R5_UNORM | DXGI_FORMAT_B5G5R5A1_UNORM | DXGI_FORMAT_B4G4R4A4_UNORM => 2, + DXGI_FORMAT_R8_UNORM | DXGI_FORMAT_R8_UINT | DXGI_FORMAT_R8_SNORM | DXGI_FORMAT_R8_SINT | DXGI_FORMAT_A8_UNORM | DXGI_FORMAT_R8_TYPELESS => 1, + // also D32 / R32 typeless, D24S8 / R24G8 typeless, R11G11B10 + _ => 4, + } +} + +/// A dump `tag` read back: how many of its texels are not all zero and differ from the first. +/// The texel size is that of the footprint's format – the copied plane's for a planar format, +/// not the resource's – and the bytes read stop at the buffer's end. +pub unsafe fn log_census(rb: &ID3D12Resource, fp: &D3D12_PLACED_SUBRESOURCE_FOOTPRINT, tag: &str) { + let mut p: *mut c_void = std::ptr::null_mut(); + if rb.Map(0, None, Some(&mut p)).is_err() || p.is_null() { + return; + } + let f = &fp.Footprint; + let len = (rb.GetDesc().Width as usize).saturating_sub(fp.Offset as usize); + let data = std::slice::from_raw_parts((p as *const u8).add(fp.Offset as usize), len); + let (lit, differ, n) = census(data, f.Width as usize, f.Height as usize, f.RowPitch as usize, f.Format); + rb.Unmap(0, None); + crate::xlog_info!("D3D12.X: target dump: {tag}: {lit} of {n} texels sampled not zero, {differ} differ from the first texel (format {})", f.Format.0); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_switch_takes_a_work_threshold_and_a_frame_count() { + assert_eq!(parse("200"), Some((200, 1))); + assert_eq!(parse("200,3"), Some((200, 3))); + assert_eq!(parse(" 50 , 2 "), Some((50, 2))); + assert_eq!(parse("0"), None); + assert_eq!(parse("200,0"), None); + assert_eq!(parse("x"), None); + assert_eq!(parse("200,x"), None); + } + + #[test] + fn dumps_wait_the_seconds_after_the_first_present() { + assert_eq!(parse_after(" 30 "), Some(Duration::from_secs(30))); + assert_eq!(parse_after("2.5"), Some(Duration::from_millis(2500))); + assert_eq!(parse_after("soon"), None); + assert_eq!(parse_after("-1"), None); + assert_eq!(parse_after("inf"), None); + let c = Config { work: 100, frames: 2, after: Duration::from_secs(30) }; + assert!(!due(&c, 500, 0, Duration::from_secs(29)), "too early"); + assert!(due(&c, 500, 0, Duration::from_secs(30))); + assert!(!due(&c, 99, 0, Duration::from_secs(40)), "too little work"); + assert!(due(&c, 100, 1, Duration::from_secs(40))); + assert!(!due(&c, 100, 2, Duration::from_secs(40)), "frames all dumped"); + } + + #[test] + fn draws_count_per_target_by_outcome() { + let mut f = Frame::default(); + f.draw(&[0x10, 0x20], Some(0x90), RAN); + f.draw(&[0x10], Some(0x90), STANDIN); + f.draw(&[0x10], None, PC); + f.draw(&[0x20], Some(0x80), SKIPPED); + f.draw(&[], None, RAN); + assert_eq!(f.work, 5); + assert_eq!(f.targets[&0x10], [1, 1, 1, 0]); + assert_eq!(f.targets[&0x20], [1, 0, 0, 1]); + assert_eq!(f.depth(), Some((0x90, [1, 1, 0, 0]))); + assert_eq!(Frame::default().depth(), None); + } + + #[test] + fn targets_rank_by_draws_and_texels_count_by_format() { + let mut f = Frame::default(); + for (t, n) in [(0x30, 5), (0x10, 9), (0x20, 5)] { + for _ in 0..n { + f.draw(&[t], None, if n == 9 { SKIPPED } else { RAN }); + } + } + let order: Vec<(usize, u64)> = f.ranked().into_iter().map(|(t, c)| (t, c.iter().sum())).collect(); + assert_eq!(order, vec![(0x10, 9), (0x20, 5), (0x30, 5)]); + // 8 x 8 texels of 8 bytes (R16G16B16A16_FLOAT), every 4th of every 4th row sampled: + // (0,0) (4,0) (0,4) (4,4); one of them has a non-zero byte in its last half + let (w, h, bytes) = (8usize, 8usize, 8usize); + let pitch = w * bytes; + let mut data = vec![0u8; pitch * h]; + data[4 * pitch + 4 * bytes + 7] = 1; + data[pitch + bytes] = 9; // a texel not sampled + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_R16G16B16A16_FLOAT), (1, 1, 4)); + // read as 4-byte texels the same bytes land elsewhere + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_R8G8B8A8_UNORM), (0, 0, 4)); + assert_eq!(texel_bytes(DXGI_FORMAT_R11G11B10_FLOAT), 4); + assert_eq!(texel_bytes(DXGI_FORMAT_R8_UNORM), 1); + for f in [DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM, DXGI_FORMAT_B4G4R4A4_UNORM] { + assert_eq!(texel_bytes(f), 2, "{f:?}"); + } + } + + #[test] + fn a_cleared_target_is_not_zero_but_has_no_texel_that_differs() { + // 8 x 8 of R8G8B8A8 cleared to (0, 0, 0, 1): every texel not zero, none differs + let (w, h, pitch) = (8usize, 8usize, 32usize); + let mut data: Vec<u8> = [0, 0, 0, 255].repeat(w * h); + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_R8G8B8A8_UNORM), (4, 0, 4)); + // one draw over texel (4, 4) + data[4 * pitch + 4 * 4] = 200; + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_R8G8B8A8_UNORM), (4, 1, 4)); + } + + #[test] + fn depth_formats_have_their_texel_sizes() { + for f in [DXGI_FORMAT_D32_FLOAT, DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_D24_UNORM_S8_UINT, DXGI_FORMAT_R24G8_TYPELESS] { + assert_eq!(texel_bytes(f), 4, "{f:?}"); + } + assert_eq!(texel_bytes(DXGI_FORMAT_D32_FLOAT_S8X24_UINT), 8); + assert_eq!(texel_bytes(DXGI_FORMAT_R32G8X24_TYPELESS), 8); + assert_eq!(texel_bytes(DXGI_FORMAT_D16_UNORM), 2); + } + + #[test] + fn the_census_stops_at_the_end_of_the_buffer() { + // 8 x 8 of D32S8X24 (8 bytes), a 256-byte pitch, but the buffer ends in the last row + // (the footprint's last row is not padded to its pitch): its texels are not read + let (w, h, pitch) = (8usize, 8usize, 256usize); + let mut data = vec![1u8; pitch * 4 + 4 * 8 + 4]; + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_D32_FLOAT_S8X24_UINT), (3, 0, 3)); + data.extend_from_slice(&[0; 4]); + assert_eq!(census(&data, w, h, pitch, DXGI_FORMAT_D32_FLOAT_S8X24_UINT), (4, 1, 4)); + } +}