From f058e4ea0278699935f61ba2384a2fde4c27c44b Mon Sep 17 00:00:00 2001 From: throny <11830644+throny@users.noreply.github.com> Date: Fri, 27 Nov 2026 06:39:41 +0100 Subject: [PATCH] D3D12.X: DRED is switched on before the process's first device With XCL_DRED=1 a device removal on nvidia-dev answered every DRED query with 0x887a0004 (not enabled) since the launch preparation landed (#2920): the DRED settings only count for devices made after them, and the preparation's runner makes a device before the title's backend applied them. The settings are now applied once per process at the start of agility::create_device, which every device of a title process goes through (runner, preflight, the title's backend, shaders, textures). Nothing changes when XCL_DRED is unset. Checked on a real removal (RTX 4080, retail title, 145 s after the first frame): the log now has the DRED device state, the page-fault line and the breadcrumb answer instead of "not enabled". Fixes #2920 Assisted-by: Claude Code (Claude Fable 5.2; written by a Claude Sonnet 5.5 sub-agent, reviewed by a Claude Opus 5.5 sub-agent) Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/agility.rs | 45 +++++++++++++++++++++++++++++ crates/xcl/src/gpu/agility_tests.rs | 25 ++++++++++++++++ crates/xcl/src/hle/d3d12x/real.rs | 24 ++------------- 3 files changed, 72 insertions(+), 22 deletions(-) diff --git a/crates/xcl/src/gpu/agility.rs b/crates/xcl/src/gpu/agility.rs index 795dfddd11..82d229e48f 100644 --- a/crates/xcl/src/gpu/agility.rs +++ b/crates/xcl/src/gpu/agility.rs @@ -46,6 +46,10 @@ pub enum State { static STATE: OnceLock = OnceLock::new(); static DEBUG_LAYER: OnceLock> = OnceLock::new(); +static DRED: OnceLock>> = OnceLock::new(); +/// Tests: set when any `create_device` started while the DRED step had not run yet. +#[cfg(test)] +pub(crate) static DRED_LATE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// Candidate directories in search order. pub(crate) fn candidates(env_override: Option, exe_dir: Option, local_app_data: Option) -> Vec { @@ -321,7 +325,13 @@ pub fn create_device

(adapter: P, feature_level: D3D_FEATURE_LEVEL) -> windows where P: Param, { + // DRED only applies to devices created after it is set: before the process's first one + enable_dred_once(); let mut device: Option = None; + #[cfg(test)] + if DRED.get().is_none() { + DRED_LATE.store(true, std::sync::atomic::Ordering::SeqCst); + } match state() { State::Loaded { factory, .. } => { // SAFETY: `factory` is a valid `ID3D12DeviceFactory` kept alive for `'static`, @@ -357,6 +367,41 @@ pub fn configuration_interface(clsid: &GUID) -> windows::core::Res } } +/// What `XCL_DRED` asks for: `None` when it is unset, else whether the breadcrumbs are on too +/// (`XCL_DRED=pagefault`: the faulting address and what was there only; the breadcrumbs put a +/// marker around every operation of every list, which is what made a title stall). +pub(crate) fn dred_request(var: Option) -> Option { + var.map(|v| v != "pagefault") +} + +/// `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. The settings apply only to devices created after them, so this runs +/// from `create_device`, before the first device of the process (the launch preparation's +/// runner makes one before the title's); once per process. +pub fn enable_dred_once() -> &'static Option> { + DRED.get_or_init(|| { + let breadcrumbs = dred_request(std::env::var_os("XCL_DRED"))?; + Some(match configuration_interface::(&CLSID_D3D12DeviceRemovedExtendedData) { + Ok(d) => { + // SAFETY: `d` is a valid settings interface; the calls only record the setting. + unsafe { + if breadcrumbs { + d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); + } + d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); + } + crate::xlog_info!("D3D12: DRED on ({})", if breadcrumbs { "page faults and the lists' last operations" } else { "page faults" }); + Ok(()) + } + Err(e) => { + crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}"); + Err(e.to_string()) + } + }) + }) +} + /// Enable the debug layer at most once per process. The GPU test runner may create a /// device before D3D12.X starts; enabling it again after that removes the device. pub fn enable_debug_layer() -> &'static Result<(), String> { diff --git a/crates/xcl/src/gpu/agility_tests.rs b/crates/xcl/src/gpu/agility_tests.rs index 234c6a6031..5317a3e3ac 100644 --- a/crates/xcl/src/gpu/agility_tests.rs +++ b/crates/xcl/src/gpu/agility_tests.rs @@ -183,6 +183,31 @@ fn rejects_malformed_export_rvas_and_cross_section_reads() { assert!(read_sdk_version_from_bytes(&x86).is_err()); } +#[test] +fn dred_request_reads_the_variable() { + use std::ffi::OsString; + + assert_eq!(dred_request(None), None); + assert_eq!(dred_request(Some(OsString::from("1"))), Some(true)); + assert_eq!(dred_request(Some(OsString::from("pagefault"))), Some(false)); +} + +/// The DRED settings apply only to devices created after them: `create_device` (the runner's +/// and the title's) has to have set them by the time it returns the process's first device. +#[test] +fn create_device_sets_dred_first() { + // Needs no device: the record is made before the creation call, whether or not it succeeds. + let dev = create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0); + assert!( + !DRED_LATE.load(std::sync::atomic::Ordering::SeqCst), + "a create_device call started before the DRED step had run" + ); + assert!(DRED.get().is_some(), "create_device did not run the DRED step"); + if dev.is_err() { + crate::gpu::recomp::runner::skip_test("no D3D12 device (the ordering was checked)"); + } +} + #[test] fn env_trimming_trailing_slash_and_no_sm66_messages() { use std::ffi::OsString; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 570ef64a10..a6cc5a314f 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -614,28 +614,8 @@ 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 let Some(dred) = std::env::var_os("XCL_DRED") { - match crate::gpu::agility::configuration_interface::( - &CLSID_D3D12DeviceRemovedExtendedData, - ) { - Ok(d) => { - // (`XCL_DRED=pagefault`: the faulting address and what was there only; the - // breadcrumbs put a marker around every operation of every list, which is - // what made a title stall) - if dred != "pagefault" { - d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); - } - d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); - crate::xlog_info!("D3D12: DRED on ({})", if dred == "pagefault" { "page faults" } else { "page faults and the lists' last operations" }); - } - Err(e) => { - crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}"); - } - } - } + // `XCL_DRED=1` (`create_device` does it too; this call only puts the log line before the device) + crate::gpu::agility::enable_dred_once(); let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(|e| format!("DXGI: {e}"))?; let mut device: Option = None; let mut name = String::new();