diff --git a/README.md b/README.md index b21e067f0b..99930f33e0 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,11 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder those on which the GPU completed at least one operation. For each, it names the translated pipeline bound at the last operation DRED saw complete and those bound after it, by their console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`). +- With `XCL_D3D12_GBV=1`, the D3D12 debug layer runs with GPU-based validation: shader-side + checks (out-of-bounds UAV/descriptor access, mesh output counts past the declared maximum, + resource-state and render-target/depth format mismatches) that the plain debug layer and DRED + miss, written to `xcl.log`. It needs the Agility SDK `D3D12SDKLayers.dll` next to `D3D12Core.dll` + and makes draws much slower. - `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in. Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles. - `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib diff --git a/crates/xcl/src/gpu/agility.rs b/crates/xcl/src/gpu/agility.rs index 795dfddd11..f0b8e2e15f 100644 --- a/crates/xcl/src/gpu/agility.rs +++ b/crates/xcl/src/gpu/agility.rs @@ -46,6 +46,7 @@ pub enum State { static STATE: OnceLock = OnceLock::new(); static DEBUG_LAYER: OnceLock> = OnceLock::new(); +static GPU_VALIDATION: OnceLock> = OnceLock::new(); /// Candidate directories in search order. pub(crate) fn candidates(env_override: Option, exe_dir: Option, local_app_data: Option) -> Vec { @@ -367,6 +368,26 @@ pub fn enable_debug_layer() -> &'static Result<(), String> { }) } +/// Enable the debug layer with GPU-based validation at most once per process (`ID3D12Debug1`). +/// GBV adds shader-side checks the plain debug layer and DRED miss – out-of-bounds UAV or +/// descriptor access, `SetMeshOutputCounts` past the declared maximum, resource-state and +/// RT/DSV-format mismatches – reported as a validation message and often a controlled device +/// removal that names the call. It must run before the first device is created and makes draws +/// much slower, so it is opt-in (`XCL_D3D12_GBV`). Enabling it after a device exists would +/// remove that device, as with the plain debug layer. +pub fn enable_gpu_based_validation() -> &'static Result<(), String> { + GPU_VALIDATION.get_or_init(|| { + let debug = configuration_interface::(&CLSID_D3D12Debug).map_err(|e| e.to_string())?; + // SAFETY: `debug` is a valid `ID3D12Debug1`; both calls configure process-wide debug + // state and must precede device creation (the callers gate on that). + unsafe { + debug.EnableDebugLayer(); + debug.SetEnableGPUBasedValidation(true); + } + Ok(()) + }) +} + #[cfg(test)] #[path = "agility_tests.rs"] mod tests; diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index f3006d87d7..0f1c31682c 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -89,6 +89,10 @@ fn init() -> Result { // The debug layer must be enabled before this runner creates a device. let _ = crate::gpu::agility::enable_debug_layer(); } + if std::env::var_os("XCL_D3D12_GBV").is_some() { + // GPU-based validation (shader-side checks); also before device creation. + let _ = crate::gpu::agility::enable_gpu_based_validation(); + } let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(e)?; // `XCL_ADAPTER` picks the adapter (default: the first in high-performance order) let sel = crate::gpu::adapter::Selector::from_env_or("auto"); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 47152fddc8..2248e7de75 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -805,6 +805,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result {} } let debug = cfg.d3d12_debug || std::env::var_os("XCL_D3D12_DEBUG").is_some(); + let gbv = std::env::var_os("XCL_D3D12_GBV").is_some(); if debug { match crate::gpu::agility::enable_debug_layer() { Ok(()) => { @@ -815,6 +816,15 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result crate::xlog_info!("D3D12: GPU-based validation enabled (draws will be slow)"), + Err(e) => crate::xlog_warn!("D3D12: GPU-based validation requested but unavailable: {e}"), + } + } // `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. @@ -863,8 +873,8 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result() { let mut cookie = 0u32; let _ = q.RegisterMessageCallback(Some(debug_message), D3D12_MESSAGE_CALLBACK_FLAG_NONE, std::ptr::null_mut(), &mut cookie);