From c7336a1ecf6dbc16f01635ac33e494cf4407d88d Mon Sep 17 00:00:00 2001 From: Brandon Strong Date: Tue, 24 Nov 2026 17:23:20 -0800 Subject: [PATCH 1/2] D3D12.X: report debug layer messages by id, counted past the log's cap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The debug layer's messages reach XCL's log but only the first 8 of each id are printed, so a run that hit one 5 151 times looked like eight hits in the report. The per-id counts already existed in the callback; they were a local static nothing could read. Hoist them to a module static, count every message, keep the first text of each id and expose them in report.json as d3d12.debug_layer, sorted by id. Measured on a 150 s debug-layer run of a retail title: 13 273 messages in the report against 57 lines in the log – 5 307 GetGPUVirtualAddress-on-non-buffer warnings (XCL's own address probes), 5 151 resource-state errors, 1 618 simultaneous-access hazards across queues and 9 render targets created CREATE_NOT_ZEROED and never initialized before use. The log caps are unchanged: printing still stops at 8 per id and 3 000 in all. Co-Authored-By: Claude Code --- crates/xcl/src/hle/d3d12x/real.rs | 78 ++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9241a3a18..eaecdd776 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -18,7 +18,7 @@ #![allow(non_snake_case)] -use std::collections::{BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::ffi::c_void; use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use std::sync::{Mutex, OnceLock}; @@ -92,6 +92,15 @@ pub struct ClassStat { pub internal_slots: usize, } +/// one debug layer message id and how often the debug layer reported it +#[derive(Debug, Default, Clone, Serialize, serde::Deserialize)] +pub struct DebugMsgStat { + pub id: i32, + pub count: u64, + /// its first message, cut to 200 characters – the log prints only the first few per id + pub first: String, +} + #[derive(Debug, Default, Clone, Serialize, serde::Deserialize)] pub struct D3d12Stats { pub backend: String, @@ -130,6 +139,9 @@ pub struct D3d12Stats { /// DXIL cache and compiler statistics. #[serde(default)] pub dxil_cache: crate::gpu::recomp::dxc::CacheStats, + /// debug layer messages seen, by message id – counted in full, past the log's per-id cap + #[serde(default)] + pub debug_layer: Vec, } static ADAPTER: Mutex = Mutex::new(String::new()); @@ -139,6 +151,30 @@ static FAILED_PSO: AtomicU64 = AtomicU64::new(0); static FALLBACK_RS: AtomicU64 = AtomicU64::new(0); static TRANSLATED_PSO: AtomicU64 = AtomicU64::new(0); static TRANSLATE_ERRORS: Mutex> = Mutex::new(Vec::new()); +/// debug layer messages per message id, counted in full (the log prints only the first few) +static DEBUG_MSGS: Mutex> = Mutex::new(BTreeMap::new()); + +/// Counts a debug layer message id, keeps its first text and says whether it is still under the +/// log's per-id cap: the count grows after the printing stops, so the report shows what hides. +fn note_debug_message(msgs: &mut BTreeMap, id: i32, text: &str) -> bool { + let e = msgs.entry(id).or_default(); + e.0 += 1; + if e.0 == 1 { + e.1 = text.chars().take(200).collect(); + } + e.0 <= DEBUG_MSG_LOG_LIMIT +} +const DEBUG_MSG_LOG_LIMIT: u32 = 8; + +/// The report form of a per-id map: ids in order, each with its count and first text. +fn debug_layer_counts_from(msgs: &BTreeMap) -> Vec { + msgs.iter().map(|(id, (n, first))| DebugMsgStat { id: *id, count: *n as u64, first: first.clone() }).collect() +} + +/// debug layer messages seen, by message id, for the report +pub fn debug_layer_counts() -> Vec { + DEBUG_MSGS.lock().map(|g| debug_layer_counts_from(&g)).unwrap_or_default() +} /// XCL shader ABI root signature on the backend device (raw, kept for the process lifetime) static ABI_RS: AtomicUsize = AtomicUsize::new(0); @@ -410,6 +446,7 @@ pub fn stats() -> D3d12Stats { translation_errors: TRANSLATE_ERRORS.lock().map(|e| e.clone()).unwrap_or_default(), background_pipelines: super::pso_async::counts(), dxil_cache: crate::gpu::recomp::dxc::cache_stats(), + debug_layer: debug_layer_counts(), } } @@ -561,22 +598,19 @@ pub fn init() -> Result { /// 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) { 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 { return; } + let t = if text.is_null() { String::new() } else { String::from_utf8_lossy(text.as_bytes()).into_owned() }; { - let mut g = PER_ID.lock().unwrap(); - let n = g.get_or_insert_with(HashMap::new).entry(id.0).or_insert(0); - *n += 1; - if *n > 8 { + let mut g = DEBUG_MSGS.lock().unwrap(); + if !note_debug_message(&mut g, id.0, &t) { return; } } 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); } @@ -7165,3 +7199,33 @@ mod map_tests { assert!(!parse(Some(40), Some(false)).DepthStencilState.DepthEnable.as_bool()); } } + +#[cfg(test)] +mod debug_layer_report_tests { + use super::*; + + #[test] + fn counts_every_message_and_caps_only_the_printing() { + let mut msgs = BTreeMap::new(); + let printed: Vec = (0..12).map(|_| note_debug_message(&mut msgs, 541, "state error")).collect(); + assert_eq!(printed.iter().filter(|p| **p).count(), DEBUG_MSG_LOG_LIMIT as usize); + assert_eq!(msgs.get(&541).map(|m| m.0), Some(12), "the count grows after the printing stops"); + assert_eq!(msgs.get(&541).map(|m| m.1.as_str()), Some("state error"), "the first text stays"); + note_debug_message(&mut msgs, 725, &"x".repeat(400)); + assert_eq!(msgs.get(&725).map(|m| m.1.chars().count()), Some(200), "the text is cut"); + assert!(note_debug_message(&mut msgs, 999, "new"), "a new id prints"); + } + + #[test] + fn the_report_lists_ids_in_order_with_full_counts() { + // a local map: a concurrent GPU test with the layer on would feed the global one + let mut msgs = BTreeMap::new(); + for _ in 0..3 { + note_debug_message(&mut msgs, 725, "simultaneous access"); + } + note_debug_message(&mut msgs, 541, "state error"); + let got = debug_layer_counts_from(&msgs); + assert_eq!(got.iter().map(|d| (d.id, d.count)).collect::>(), vec![(541, 1), (725, 3)]); + assert_eq!(got[1].first, "simultaneous access"); + } +} From 1039318fce8565e8298b5fea211f23f524a59bf1 Mon Sep 17 00:00:00 2001 From: Brandon Strong Date: Tue, 24 Nov 2026 17:23:22 -0800 Subject: [PATCH 2/2] Scripts: smoke.ps1 -DebugLayer, and pr_check fails new debug layer message ids A run that picked up new validation errors looked clean in the block: frames and peak commit say nothing about them, and the log's per-id cap hides how often each one fired. smoke.ps1 prints `debug layer messages: 538x5151 745x5307` from report.json (n/a when the layer was off, none when there were none) and pr_check.py refuses a PR whose run shows ids main's run did not; ids main's run showed and the PR's no longer shows are a fix, not a problem. -DebugLayer turns the layer on for the run so the line has ids in it. It is a switch and not the default: with the layer requested and no Graphics Tools device creation can fail outright (#1444), so forcing it would break smoke runs there. Both runs of a pair get it; blocks without the line still pass. Co-Authored-By: Claude Code --- scripts/pr_check.py | 12 ++++++++++++ scripts/smoke.ps1 | 24 +++++++++++++++++++++--- scripts/test_pr_check.py | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/scripts/pr_check.py b/scripts/pr_check.py index cb0ef3ef9..1993043a7 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -58,6 +58,14 @@ def number(s): return int(m.group(1)) if m else None +def smoke_ids(s): + """The debug layer message ids of a `debug layer messages` line (`541x38 725x4`); None when + the line is absent or the run did not report them (`n/a`).""" + if not s or s.strip() in ("n/a", "none"): + return None + return {int(m.group(1)) for m in re.finditer(r"(\d+)x\d+", s)} + + def field_problems(block, run): """What `block` (the smoke block of `run`) lacks for the comparison: a missing line or a number that is not one.""" @@ -160,6 +168,10 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): pc, mc = number(pr.get("peak commit")), number(m.get("peak commit")) if pc and mc and pc > COMMIT_GROWTH * mc: problems.append(f"the PR's run commits much more memory than main's ({pc} MB against {mc} MB)") + pi, mi = smoke_ids(pr.get("debug layer messages")), smoke_ids(m.get("debug layer messages")) + if pi is not None and mi is not None and pi - mi: + problems.append("the PR's regression run reports debug layer message ids main's run did not (" + + ", ".join(str(i) for i in sorted(pi - mi)) + "): new validation errors") return problems diff --git a/scripts/smoke.ps1 b/scripts/smoke.ps1 index b7859c924..e2fa9e72d 100644 --- a/scripts/smoke.ps1 +++ b/scripts/smoke.ps1 @@ -2,18 +2,22 @@ # prints an `xcl-smoke` block to paste into the PR's "Regression run" section – once built # from main, once from the PR's head commit (CONTRIBUTING.md, "What a PR needs to be merged"). # -# .\scripts\smoke.ps1 -Game 'D:\Games\SomeTitle' [-Seconds 60] [-Xcl .\target\release\xcl.exe] [-Title