From 177e6e8133a7f743aa36dbd3cc659a6fe4198cf6 Mon Sep 17 00:00:00 2001
From: Filip722 <37023444+Filip722@users.noreply.github.com>
Date: Tue, 24 Nov 2026 15:16:18 +0100
Subject: [PATCH] D3D12.X: retain live render targets for frame dumps
Historical render-target counts can outlive their RTV references. Select a target only while an RTV still owns it and clone the reference before releasing the lock. The synthetic regression selects the untracked address with the old scan and passes with the live selection.
Assisted-by: OpenAI Codex (GPT-6.2-Sol high)
---
README.md | 2 +
crates/xcl/src/hle/d3d12x/present.rs | 108 ++++++++++++++++++++++++---
2 files changed, 98 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index 9c047fd54e..9f9539cca6 100644
--- a/README.md
+++ b/README.md
@@ -134,6 +134,8 @@ 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).
+ With `XCL_TRACE_FRAME=1`, the intermediate dump prefers the watched target, then the most
+ frequently bound single-sampled target still tracked by an RTV descriptor.
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/present.rs b/crates/xcl/src/hle/d3d12x/present.rs
index 01b3c92a4a..e83aae58b8 100644
--- a/crates/xcl/src/hle/d3d12x/present.rs
+++ b/crates/xcl/src/hle/d3d12x/present.rs
@@ -709,10 +709,10 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, usize)>) {
let followed = FOLLOWED.lock().unwrap();
let targets = busiest
.into_iter()
- .chain(followed.iter().copied())
- .filter_map(|r| {
- let rr = r as *mut c_void;
- let res = ID3D12Resource::from_raw_borrowed(&rr)?.clone();
+ .chain(followed.iter().filter_map(|&r| {
+ ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).cloned()
+ }))
+ .filter_map(|res| {
let d = res.GetDesc();
if d.SampleDesc.Count > 1 {
return None;
@@ -900,14 +900,31 @@ pub unsafe fn watch_rt(rt: usize) {
}
}
-/// the single-sampled resource bound as a render target most often (`XCL_TRACE_FRAME`; a
-/// multisampled one cannot be copied to a buffer – the present list would not close)
-fn busiest_rt() -> Option {
- if let Some(r) = *WATCHED_RT.lock().unwrap() {
- return Some(r);
- }
- let single = |r: usize| unsafe { ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).is_some_and(|x| x.GetDesc().SampleDesc.Count <= 1) };
- USES.lock().unwrap().as_ref()?.iter().filter(|(&r, u)| u[USE_RT] > 0 && single(r)).max_by_key(|(_, u)| u[USE_RT]).map(|(&r, _)| r)
+/// The watched target or the live, single-sampled target bound most often (`XCL_TRACE_FRAME`).
+fn busiest_rt() -> Option {
+ if let Some(watched) = WATCHED_RT.lock().unwrap().and_then(|r| unsafe {
+ ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).cloned()
+ }) {
+ return Some(watched);
+ }
+ let rtvs = RTVS.lock().unwrap();
+ let uses = USES.lock().unwrap();
+ // RTVS owns references while locked. USES keeps only historical addresses and counts.
+ unsafe { most_bound_live_rt(rtvs.as_ref()?, uses.as_ref()?) }
+}
+
+/// The caller keeps all resources in `rtvs` alive throughout selection.
+unsafe fn most_bound_live_rt(rtvs: &HashMap, uses: &HashMap) -> Option {
+ rtvs.values()
+ .filter_map(|&r| {
+ let count = uses.get(&r)?[USE_RT];
+ let raw = r as *mut c_void;
+ let res = ID3D12Resource::from_raw_borrowed(&raw)?;
+ // The dump cannot copy a multisampled target to its readback buffer.
+ (count > 0 && res.GetDesc().SampleDesc.Count <= 1).then_some((r, count))
+ })
+ .max_by_key(|&(_, count)| count)
+ .and_then(|(r, _)| ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).cloned())
}
/// an 11- / 10-bit unsigned float (5-bit exponent) -> f32
@@ -1079,6 +1096,73 @@ mod frame_dump_tests {
use super::super::real::D3d12Stats;
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
+ #[test]
+ fn frame_dump_selects_only_live_rtv_resources() {
+ use std::mem::{offset_of, size_of};
+
+ #[repr(C)]
+ struct Resource {
+ vtable: *const usize,
+ refs: Cell,
+ descs: Cell,
+ samples: Cell,
+ }
+
+ extern "system" fn unexpected_call() {
+ std::process::abort();
+ }
+ unsafe extern "system" fn add(this: *mut c_void) -> u32 {
+ let refs = &(*this.cast::()).refs;
+ refs.set(refs.get() + 1);
+ refs.get()
+ }
+ unsafe extern "system" fn release(this: *mut c_void) -> u32 {
+ let refs = &(*this.cast::()).refs;
+ refs.set(refs.get() - 1);
+ refs.get()
+ }
+ unsafe extern "system" fn desc(this: *mut c_void, out: *mut D3D12_RESOURCE_DESC) {
+ let resource = &*this.cast::();
+ resource.descs.set(resource.descs.get() + 1);
+ (*out).SampleDesc.Count = resource.samples.get();
+ }
+
+ const SLOT: usize = size_of::();
+ let mut vtable = [unexpected_call as usize; size_of::() / SLOT];
+ vtable[offset_of!(ID3D12Resource_Vtbl, base__.base__.base__.base__.AddRef) / SLOT] = add as usize;
+ vtable[offset_of!(ID3D12Resource_Vtbl, base__.base__.base__.base__.Release) / SLOT] = release as usize;
+ vtable[offset_of!(ID3D12Resource_Vtbl, GetDesc) / SLOT] = desc as usize;
+ let resource = || Resource {
+ vtable: vtable.as_ptr(),
+ refs: Cell::new(1),
+ descs: Cell::new(0),
+ samples: Cell::new(1),
+ };
+ let (earlier, active) = (resource(), resource());
+ let earlier_ptr = std::ptr::from_ref(&earlier) as usize;
+ let active_ptr = std::ptr::from_ref(&active) as usize;
+ let uses = HashMap::from([(earlier_ptr, [24, 0, 0, 0]), (active_ptr, [3, 0, 0, 0])]);
+ let mut rtvs = HashMap::from([(1, active_ptr)]);
+ unsafe {
+ // The earlier resource is still alive for this test, but its RTV was replaced.
+ let selected = most_bound_live_rt(&rtvs, &uses).unwrap();
+ assert_eq!(selected.as_raw() as usize, active_ptr);
+ assert_eq!(earlier.descs.get(), 0);
+ assert_eq!(active.refs.get(), 2);
+ drop(selected);
+ assert_eq!(active.refs.get(), 1);
+
+ rtvs.insert(2, earlier_ptr);
+ let selected = most_bound_live_rt(&rtvs, &uses).unwrap();
+ assert_eq!(selected.as_raw() as usize, earlier_ptr);
+ drop(selected);
+
+ earlier.samples.set(4);
+ let selected = most_bound_live_rt(&rtvs, &uses).unwrap();
+ assert_eq!(selected.as_raw() as usize, active_ptr);
+ }
+ }
+
#[test]
fn reports_without_frame_dumps_remain_readable() {
let stats = D3d12Stats { presents: 100, ..Default::default() };