Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ unsafe fn argument_order_buffer(list: &ID3D12GraphicsCommandList) -> Option<ID3D
return None;
}
let b = b?;
if super::real::dred_on() {
super::real::name_resource(b.as_raw(), "xcl bind indirect_order");
}
*g = Some((dev.as_raw() as usize, b.clone()));
Some(b)
}
Expand Down
11 changes: 11 additions & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,11 @@ unsafe fn make_copy_scratch(list: *mut c_void, bytes: u64) -> Option<ID3D12Resou
device_of(list)?
.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, None, &mut b)
.ok()?;
if super::real::dred_on() {
if let Some(ref r) = b {
super::real::name_resource(r.as_raw(), "xcl list_x copy_scratch");
}
}
b
}

Expand Down Expand Up @@ -2283,6 +2288,9 @@ unsafe fn pattern_buffer(list: *mut c_void, pat: [u8; 16], n: u64) -> Option<usi
chunk.copy_from_slice(&pat);
}
b.Unmap(0, None);
if super::real::dred_on() {
super::real::name_resource(b.as_raw(), "xcl list_x pattern_buf");
}
let raw = b.as_raw() as usize;
cache.push((pat, b, len));
Some(raw)
Expand Down Expand Up @@ -2817,6 +2825,9 @@ unsafe fn predication_buffer(list: *mut c_void) -> Option<usize> {
let mut buf: Option<ID3D12Resource> = 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();
Expand Down
119 changes: 104 additions & 15 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ pub fn init() -> Result<String, String> {
// `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::<ID3D12DeviceRemovedExtendedDataSettings>(
&CLSID_D3D12DeviceRemovedExtendedData,
) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<bool> = 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)) {
Expand Down Expand Up @@ -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<u16> = "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
Expand All @@ -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::<ID3D12DeviceRemovedExtendedData1>() {
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::<ID3D12DeviceRemovedExtendedData>() 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| {
Expand Down
12 changes: 12 additions & 0 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -376,6 +381,11 @@ unsafe fn alloc_pool_heap(dev: &ID3D12Device) -> Option<ID3D12Heap> {
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
}

Expand Down
Loading