From 31d8f4bedf34742bcfcfc5787aa3ab01a4c65022 Mon Sep 17 00:00:00 2001 From: Simon-CR <292446+Simon-CR@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:32:14 -0500 Subject: [PATCH] D3D12.X: read DRED PageFault1 ObjectNameW and name internal resources ID3D12Object::SetName populates wide ObjectNameW in DRED allocation nodes, whereas log_device_removed previously read only ObjectNameA and printed unnamed internal objects. Prefer ObjectNameW with fallback to ObjectNameA via format_dred_allocation_name, query PageFaultOutput1 to include object pointers when available, and under XCL_DRED=1 name internal resources and heaps in real, list_x, bind, sparse, vram, and title_textures. Assisted-by: Antigravity --- crates/xcl/src/hle/d3d12x/bind.rs | 3 + crates/xcl/src/hle/d3d12x/list_x.rs | 11 ++ crates/xcl/src/hle/d3d12x/real.rs | 119 +++++++++++++++++--- crates/xcl/src/hle/d3d12x/sparse.rs | 12 ++ crates/xcl/src/hle/d3d12x/title_textures.rs | 3 + crates/xcl/src/hle/d3d12x/vram.rs | 10 ++ 6 files changed, 143 insertions(+), 15 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index da5b15ef4..d5785ecc1 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -367,6 +367,9 @@ unsafe fn argument_order_buffer(list: &ID3D12GraphicsCommandList) -> Option Option Option Option { let mut buf: Option = None; dev.CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, None, &mut buf).ok()?; let buf = buf?; + if super::real::dred_on() { + super::real::name_resource(buf.as_raw(), "xcl list_x pred_buf"); + } let (raw, extra) = { let mut g = LISTS.lock().unwrap(); let s = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9241a3a18..847fddb19 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -498,7 +498,7 @@ pub fn init() -> Result { // `XCL_DRED=1`: on a device removal the faulting address and the last GPU operations // (logged by `log_device_removed`); no debug layer needed. Opt-in: with it XDefiant // stalled in its first seconds twice. - if std::env::var_os("XCL_DRED").is_some() { + if dred_on() { match crate::gpu::agility::configuration_interface::( &CLSID_D3D12DeviceRemovedExtendedData, ) { @@ -1256,6 +1256,9 @@ unsafe fn place_console_texture(this: *mut c_void, desc: usize, state: usize, cl return None; } UMA_BUFFERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (p, size as usize, true)); + if dred_on() { + name_resource(*ppv, &format!("xcl uma_tex p={p:#x}")); + } Some(hr) } @@ -1312,6 +1315,9 @@ unsafe fn place_console_buffer_sparse(this: *mut c_void, d: &D3D12_RESOURCE_DESC let (p, _) = super::sparse::alloc(bytes, 0x1_0000)?; if let Some((hr, delta)) = super::sparse::place_buffer(this, p, d, state, riid, ppv) { UMA_BUFFERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (p, bytes, true)); + if dred_on() { + name_resource(*ppv, &format!("xcl sparse_buf p={p:#x} sz={bytes:#x}")); + } if delta != 0 { BUFFER_DELTA.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, delta as u64); } @@ -1378,6 +1384,9 @@ unsafe fn place_console_buffer(this: *mut c_void, props: usize, desc: usize, sta return None; } UMA_BUFFERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (p, bytes, true)); + if dred_on() { + name_resource(*ppv, &format!("xcl uma_buf p={p:#x} sz={bytes:#x}")); + } // Map must hand out the same memory the shaders read (checked once) static CHECKED: AtomicU64 = AtomicU64::new(0); if CHECKED.fetch_add(1, Ordering::Relaxed) == 0 { @@ -2189,6 +2198,9 @@ unsafe fn resource_at(this: *mut c_void, desc: *const u8, va: u64) -> *mut c_voi } r = std::ptr::null_mut(); } + if !r.is_null() && dred_on() { + name_resource(r, &format!("xcl resource_at va={va:#x}")); + } MADE.lock().unwrap().get_or_insert_with(HashMap::new).insert((va, x), r as usize); r } @@ -6281,6 +6293,49 @@ pub fn drain_gpu() { } } +/// Whether DRED diagnostics were requested via `XCL_DRED=1`. +pub(super) fn dred_on() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("XCL_DRED").is_some()) +} + +/// Name an `ID3D12Resource` when `XCL_DRED=1` so DRED allocation lists report which internal XCL +/// allocation backed a faulting VA. +pub(super) unsafe fn name_resource(res: *mut c_void, label: &str) { + if !dred_on() || res.is_null() { + return; + } + if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { + let _ = r.SetName(&windows::core::HSTRING::from(label)); + } +} + +/// Name an `ID3D12Heap` when `XCL_DRED=1` so DRED allocation lists identify the backing heap pool. +pub(super) unsafe fn name_heap(heap: *mut c_void, label: &str) { + if !dred_on() || heap.is_null() { + return; + } + if let Some(h) = ID3D12Heap::from_raw_borrowed(&heap) { + let _ = h.SetName(&windows::core::HSTRING::from(label)); + } +} + +/// Read a DRED allocation node's debug name, preferring `ObjectNameW` (populated by +/// `ID3D12Object::SetName`) and falling back to `ObjectNameA`. +unsafe fn format_dred_allocation_name(name_w: windows::core::PCWSTR, name_a: *const u8) -> String { + if !name_w.is_null() { + if let Ok(s) = name_w.to_string() { + if !s.is_empty() { + return s; + } + } + } + if !name_a.is_null() { + return std::ffi::CStr::from_ptr(name_a as *const std::ffi::c_char).to_string_lossy().into_owned(); + } + String::new() +} + /// DRED gives us a linked list in driver order. Keep the diagnostic bounded even on a /// long chain, with room in the log for work that the GPU had already started. unsafe fn report_dred_lists(head: *const D3D12_AUTO_BREADCRUMB_NODE, mut report: impl FnMut(&D3D12_AUTO_BREADCRUMB_NODE, usize)) { @@ -6324,6 +6379,18 @@ mod dred_tests { assert_eq!((node, done), (std::ptr::from_ref(&nodes[i]), 0)); } } + #[test] + fn format_dred_allocation_name_prefers_wide_and_falls_back_to_ansi() { + let wide: Vec = "wide_name".encode_utf16().chain([0]).collect(); + let empty_wide: [u16; 1] = [0]; + let ansi = b"ansi_name\0"; + unsafe { + assert_eq!(format_dred_allocation_name(windows::core::PCWSTR(wide.as_ptr()), ansi.as_ptr()), "wide_name"); + assert_eq!(format_dred_allocation_name(windows::core::PCWSTR::null(), ansi.as_ptr()), "ansi_name"); + assert_eq!(format_dred_allocation_name(windows::core::PCWSTR(empty_wide.as_ptr()), ansi.as_ptr()), "ansi_name"); + assert_eq!(format_dred_allocation_name(windows::core::PCWSTR::null(), std::ptr::null()), ""); + } + } } /// If the device was removed: the reason, DRED's page fault (address, the allocations around @@ -6338,21 +6405,43 @@ pub fn log_device_removed() { } crate::xlog_error!("D3D12 device removed: {:#x} {}", e.code().0 as u32, e.message()); unsafe { + let mut pf_logged = false; + if let Ok(dred1) = dev.cast::() { + if let Ok(pf) = dred1.GetPageFaultAllocationOutput1() { + let names = |mut n: *const D3D12_DRED_ALLOCATION_NODE1| { + let mut v = Vec::new(); + while !n.is_null() && v.len() < 8 { + let a = &*n; + let name = format_dred_allocation_name(a.ObjectNameW, a.ObjectNameA); + let ptr = (&*a.pObject).as_ref().map(|obj| format!(" ptr={:#x}", obj.as_raw() as usize)).unwrap_or_default(); + v.push(format!("type {} {}{}", a.AllocationType.0, name, ptr)); + n = a.pNext; + } + v.join(", ") + }; + let va = pf.PageFaultVA; + let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default(); + crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode)); + pf_logged = true; + } + } let Ok(dred) = dev.cast::() else { return }; - if let Ok(pf) = dred.GetPageFaultAllocationOutput() { - let names = |mut n: *const D3D12_DRED_ALLOCATION_NODE| { - let mut v = Vec::new(); - while !n.is_null() && v.len() < 8 { - let a = &*n; - let name = if a.ObjectNameA.is_null() { String::new() } else { std::ffi::CStr::from_ptr(a.ObjectNameA as *const std::ffi::c_char).to_string_lossy().into_owned() }; - v.push(format!("type {} {}", a.AllocationType.0, name)); - n = a.pNext; - } - v.join(", ") - }; - let va = pf.PageFaultVA; - let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default(); - crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode)); + if !pf_logged { + if let Ok(pf) = dred.GetPageFaultAllocationOutput() { + let names = |mut n: *const D3D12_DRED_ALLOCATION_NODE| { + let mut v = Vec::new(); + while !n.is_null() && v.len() < 8 { + let a = &*n; + let name = format_dred_allocation_name(a.ObjectNameW, a.ObjectNameA); + v.push(format!("type {} {}", a.AllocationType.0, name)); + n = a.pNext; + } + v.join(", ") + }; + let va = pf.PageFaultVA; + let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default(); + crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode)); + } } if let Ok(bc) = dred.GetAutoBreadcrumbsOutput() { report_dred_lists(bc.pHeadAutoBreadcrumbNode, |node, done| { diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 91eb2ba35..c1881b0f2 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -505,6 +505,9 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { return Vec::new(); } let Some(r) = r else { return Vec::new() }; + if super::real::dred_on() { + super::real::name_resource(r.as_raw(), &format!("xcl sparse window {k}")); + } out.push((k, r.clone().into_raw() as usize, r.GetGPUVirtualAddress())); windows.push((base, r)); } @@ -895,6 +898,9 @@ impl State { ) .ok()?; let r = r?; + if super::real::dred_on() { + super::real::name_resource(r.as_raw(), "xcl sparse staging"); + } let mut p = std::ptr::null_mut(); r.Map(0, None, Some(&mut p)).ok()?; self.staging = Some((r, p as *mut u8)); @@ -922,6 +928,9 @@ impl State { return false; } let Some(r) = r else { return false }; + if super::real::dred_on() { + super::real::name_resource(r.as_raw(), "xcl sparse zero"); + } let mut p = std::ptr::null_mut(); if r.Map(0, None, Some(&mut p)).is_err() { return false; @@ -949,6 +958,9 @@ impl State { ) .ok()?; let r = r?; + if super::real::dred_on() { + super::real::name_resource(r.as_raw(), "xcl sparse readback"); + } let mut p = std::ptr::null_mut(); r.Map(0, None, Some(&mut p)).ok()?; self.readback = Some((r, p as *mut u8)); diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index ac38429be..0375235ad 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -966,6 +966,9 @@ pub unsafe fn probe_gpu_content(res: usize) { ) .map_err(|e| e.to_string())?; let rb = rb.ok_or("no readback buffer")?; + if super::real::dred_on() { + super::real::name_resource(rb.as_raw(), "xcl title_tex readback"); + } let barrier = |a: D3D12_RESOURCE_STATES, b: D3D12_RESOURCE_STATES| D3D12_RESOURCE_BARRIER { Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 8a727aaf1..83d7f89f6 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -153,6 +153,11 @@ unsafe fn arena_heap(dev: &ID3D12Device, va: usize, size: usize, align: usize) - if n < 16 || n.is_power_of_two() { crate::xlog_info!("D3D12.X: video-memory arena #{n} for title addresses {va:#x}..{:#x} ({made} bytes of arenas made so far)", va + size); } + if super::real::dred_on() { + if let Some(ref h) = heap { + super::real::name_heap(h.as_raw(), &format!("xcl vram arena va={va:#x}")); + } + } heap } @@ -376,6 +381,11 @@ unsafe fn alloc_pool_heap(dev: &ID3D12Device) -> Option { crate::xlog_warn!("D3D12.X: no video memory for more pages of title textures ({e})"); return None; } + if super::real::dred_on() { + if let Some(ref h) = heap { + super::real::name_heap(h.as_raw(), "xcl vram tile_pool"); + } + } heap }