From 8dde8ff7ee96fa78068191a384f5467fe2108ca2 Mon Sep 17 00:00:00 2001 From: MadaraXor Date: Tue, 24 Nov 2026 11:13:00 +0100 Subject: [PATCH] D3D12.X: debug layer messages reach the log without ID3D12InfoQueue1 With XCL_D3D12_DEBUG=1, the debug layer's messages went to the log only through an ID3D12InfoQueue1 message callback. Windows 10 has no ID3D12InfoQueue1 (the cast fails with E_NOINTERFACE, measured on 19045 with the Agility SDK 1.620.2), the failure was ignored, and the log got no message at all. test_title_draws_with_a_root_signature_under_the_debug_layer passed there without seeing any. Without ID3D12InfoQueue1 the device's ID3D12InfoQueue is kept, and its stored messages are logged (same caps) and cleared at every submission and present, in stop_if_device_removed, before a removal is reported. The test adds an application message to the debug layer's queue and expects it in the log after stop_if_device_removed. It fails on main on Windows 10. On the retail title, the log now shows the debug layer's errors (e.g. ids 538 and 1422) where it showed none. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/real.rs | 67 ++++++++++++++++++++++++++--- crates/xcl/tests/debug_layer_log.rs | 38 ++++++++++++++++ 2 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 crates/xcl/tests/debug_layer_log.rs diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 5b01e85692..f0731c2825 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -525,10 +525,21 @@ pub fn init() -> Result { } }; if debug { - // the debug layer's messages into XCL's log (they otherwise go to a debugger only) - if let Ok(q) = device.cast::() { - let mut cookie = 0u32; - let _ = q.RegisterMessageCallback(Some(debug_message), D3D12_MESSAGE_CALLBACK_FLAG_NONE, std::ptr::null_mut(), &mut cookie); + // the debug layer's messages into XCL's log (they otherwise go to a debugger only): + // through a callback where the system has ID3D12InfoQueue1; Windows 10 does not, and + // there they are read from the device's message queue (`poll_debug_messages`) + let mut cookie = 0u32; + let callback = device + .cast::() + .is_ok_and(|q| q.RegisterMessageCallback(Some(debug_message), D3D12_MESSAGE_CALLBACK_FLAG_NONE, std::ptr::null_mut(), &mut cookie).is_ok()); + if !callback { + match device.cast::() { + Ok(q) => { + *POLLED_MESSAGES.lock().unwrap() = Some(q); + crate::xlog_info!("D3D12: no ID3D12InfoQueue1 – the debug layer's messages are read at every submission and present"); + } + Err(e) => crate::xlog_warn!("D3D12: the debug layer's messages cannot be read ({e})"), + } } } open_uma(&device); @@ -541,10 +552,54 @@ pub fn init() -> Result { } } +/// The debug layer's message callback (systems with ID3D12InfoQueue1). +unsafe extern "system" fn debug_message(_category: D3D12_MESSAGE_CATEGORY, severity: D3D12_MESSAGE_SEVERITY, id: D3D12_MESSAGE_ID, text: windows::core::PCSTR, _context: *mut c_void) { + let t = if text.is_null() { String::new() } else { String::from_utf8_lossy(text.as_bytes()).into_owned() }; + log_debug_message(severity, id, &t); +} + +/// The device's debug layer message queue where the system has no ID3D12InfoQueue1 for a +/// callback (Windows 10), read by `poll_debug_messages`. +static POLLED_MESSAGES: Mutex> = Mutex::new(None); + +/// The debug layer's stored messages into the log, then the queue emptied (without +/// ID3D12InfoQueue1 only). Called at every submission and present, before a device removal is +/// reported (`stop_if_device_removed`), so the messages that led to one come first. A message +/// added while the stored ones are read may go with them (the queue only empties as a whole). +fn poll_debug_messages() { + let Ok(g) = POLLED_MESSAGES.try_lock() else { return }; + let Some(q) = g.as_ref() else { return }; + unsafe { + let n = q.GetNumStoredMessages(); + for i in 0..n { + let mut len = 0usize; + if q.GetMessage(i, None, &mut len).is_err() || len < std::mem::size_of::() { + continue; + } + // D3D12_MESSAGE holds pointers: an 8-byte aligned buffer + let mut buf = vec![0u64; len.div_ceil(8)]; + let m = buf.as_mut_ptr() as *mut D3D12_MESSAGE; + if q.GetMessage(i, Some(m), &mut len).is_err() { + continue; + } + let m = &*m; + let t = if m.pDescription.is_null() { + String::new() + } else { + String::from_utf8_lossy(std::slice::from_raw_parts(m.pDescription, m.DescriptionByteLength.saturating_sub(1))).into_owned() + }; + log_debug_message(m.Severity, m.ID, &t); + } + if n > 0 { + q.ClearStoredMessages(); + } + } +} + /// A debug layer message: errors and warnings into the log – the first 8 of each message id /// (3 000 in all). One cap for all of them (200) was used up in seconds by two per-frame state /// errors of a retail game, long before the message that came with its device removal. -unsafe extern "system" fn debug_message(_category: D3D12_MESSAGE_CATEGORY, severity: D3D12_MESSAGE_SEVERITY, id: D3D12_MESSAGE_ID, text: windows::core::PCSTR, _context: *mut c_void) { +fn log_debug_message(severity: D3D12_MESSAGE_SEVERITY, id: D3D12_MESSAGE_ID, t: &str) { static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); static PER_ID: Mutex>> = Mutex::new(None); if severity.0 > D3D12_MESSAGE_SEVERITY_WARNING.0 { @@ -561,7 +616,6 @@ unsafe extern "system" fn debug_message(_category: D3D12_MESSAGE_CATEGORY, sever if N.fetch_add(1, Ordering::Relaxed) >= 3000 { return; } - let t = if text.is_null() { String::new() } else { String::from_utf8_lossy(text.as_bytes()).into_owned() }; crate::xlog_warn!("D3D12 debug layer ({}, id {}): {t}", if severity.0 <= D3D12_MESSAGE_SEVERITY_ERROR.0 { "error" } else { "warning" }, id.0); } @@ -5214,6 +5268,7 @@ pub fn log_device_removed() { /// submitting work to a hung device can keep the driver from recovering), with the reason in /// the log and the report. pub fn stop_if_device_removed() { + poll_debug_messages(); let raw = device_raw(); let Some(dev) = (unsafe { ID3D12Device::from_raw_borrowed(&raw) }) else { return }; if let Err(e) = unsafe { dev.GetDeviceRemovedReason() } { diff --git a/crates/xcl/tests/debug_layer_log.rs b/crates/xcl/tests/debug_layer_log.rs new file mode 100644 index 0000000000..108c1cea90 --- /dev/null +++ b/crates/xcl/tests/debug_layer_log.rs @@ -0,0 +1,38 @@ +//! `XCL_D3D12_DEBUG=1`: the D3D12 debug layer's messages reach XCL's log, also on systems +//! without `ID3D12InfoQueue1` (Windows 10), where the message callback XCL registers does not +//! exist. Its own process: the debug layer has to be on before the first device exists. + +use windows::core::{Interface, PCSTR}; +use windows::Win32::Graphics::Direct3D12::*; + +use xcl::gpu::recomp::runner; +use xcl::hle::d3d12x::real; + +/// A message in the device's debug layer queue is in the log after what XCL does at every +/// submission and present (`stop_if_device_removed`). +#[test] +fn debug_layer_messages_reach_the_log() { + let dir = std::env::temp_dir().join(format!("xcl-debug-layer-log-{}", std::process::id())); + let path = dir.join("xcl.log"); + xcl::log::init(Some(&path), xcl::log::Level::Info, false); + std::env::set_var("XCL_D3D12_DEBUG", "1"); + if xcl::hle::d3d12x::backend() != xcl::hle::d3d12x::Backend::Real { + runner::skip_test("no GPU"); + return; + } + let raw = real::device_raw(); + let dev = unsafe { ID3D12Device::from_raw_borrowed(&raw) }.expect("backend device").clone(); + let Ok(queue) = dev.cast::() else { + println!("SKIPPED debug_layer_messages_reach_the_log: no D3D12 debug layer"); + return; + }; + let text = "xcl test: a debug layer message"; + let c = std::ffi::CString::new(text).unwrap(); + unsafe { queue.AddApplicationMessage(D3D12_MESSAGE_SEVERITY_ERROR, PCSTR(c.as_ptr() as *const u8)) }.expect("AddApplicationMessage"); + real::stop_if_device_removed(); + xcl::log::flush(); + let log = std::fs::read_to_string(&path).unwrap_or_default(); + let _ = std::fs::remove_dir_all(&dir); + assert!(log.contains("D3D12: debug layer enabled"), "the debug layer did not come up:\n{log}"); + assert!(log.lines().any(|l| l.contains("D3D12 debug layer (error") && l.contains(text)), "the debug layer's message is not in the log:\n{log}"); +}