From 3db4c3aa347d4bf5503d16967ef10fed2259b85e Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 22:07:40 -0800 Subject: [PATCH] D3D12.X: default AMD driver-blob persistence off Record the selected runtime or precompile adapter vendor and conservatively disable persisted driver blobs after AMD is observed. AMD cached-PSO blobs can reference the driver-owned cache instead of containing self-sufficient code. XCL_PSO_DISK_CACHE=1 remains an explicit override; =0 disables persistence everywhere. NVIDIA, Intel, and WARP retain default-on behavior. Adapted from #2577. Locally cross-checked with the GNU Windows target; Windows execution remains untested here. Assisted-by: Codex (GPT-5.6-sol) --- README.md | 2 +- crates/xcl/src/hle/d3d12x/real.rs | 54 ++++++++++++++++++++++++++++--- crates/xcl/src/shaders.rs | 9 +++--- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 307a5a7ff7..ac22f6e807 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ xcl inspect D:\Game\game.exe # the game's imports and what XCL has xcl run D:\Game [-- game arguments] # run xcl disasm [--hex] # RDNA2 disassembler (console containers too) xcl shaders D:\data\shaders.rpf --top 50 # pack statistics; --recomp / --compile N -xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines # build compute pipelines and save driver blobs for the next run +xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines # build compute pipelines and save driver blobs for the next run (except by default on AMD) xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines --used-only # only the compute pipelines an earlier run of the title used xcl shaders D:\data\shaders.rpf --export-hlsl out --record-hash HEX # exact compute HLSL for investigation xcl shaders D:\data\shaders.rpf --measure-pso HEX --repeat 3 # cold driver PSO timing (cache disabled) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 3a342c6244..870b7da102 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -526,6 +526,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result = None; let mut name = String::new(); + let mut vendor = 0x1414; // WARP is Microsoft's adapter let sel = crate::gpu::adapter::Selector::from_env_or(&cfg.d3d12_adapter); if sel != crate::gpu::adapter::Selector::Warp { let mut list = crate::gpu::adapter::candidates(&factory, &sel); @@ -536,6 +537,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result> 20); break; } @@ -551,6 +553,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result() { @@ -4562,10 +4565,11 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL /// again. A blob whose build took `KEEP` or longer is written under the hash of its DXIL and /// its flags (`\driver\`) and read when a later run has none in memory; the /// driver refuses the blob of another driver, adapter or root signature (the file is dropped -/// and the program compiled as before). `XCL_PSO_DISK_CACHE=0`: off. +/// and the program compiled as before). Off by default on AMD; `XCL_PSO_DISK_CACHE=1` forces +/// it on, and `XCL_PSO_DISK_CACHE=0` turns it off everywhere. pub(crate) mod driver_cache { use std::path::{Path, PathBuf}; - use std::sync::atomic::{AtomicU64, Ordering}; + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; /// builds shorter than this are not worth a file const KEEP: std::time::Duration = std::time::Duration::from_millis(250); @@ -4586,6 +4590,22 @@ pub(crate) mod driver_cache { (HITS.load(Ordering::Relaxed), STALE.load(Ordering::Relaxed), STORED.load(Ordering::Relaxed)) } + static REFERENCE_BLOBS: AtomicBool = AtomicBool::new(false); + + /// AMD's cached-PSO blobs name entries in the driver's own cache instead of containing + /// self-sufficient code. They are not safe to carry across runs unless explicitly enabled. + pub fn note_vendor(vendor: u32) { + let references = reference_blob_vendor(vendor); + let was = REFERENCE_BLOBS.fetch_or(references, Ordering::Relaxed); + if references && !was && !enabled() { + crate::xlog_info!("D3D12.X: driver-blob disk cache off on AMD (XCL_PSO_DISK_CACHE=1: force on)"); + } + } + + fn reference_blob_vendor(vendor: u32) -> bool { + vendor == 0x1002 + } + /// The file of the pipeline made from `dxil` with `flags`. pub fn name(dxil: &[u8], flags: i32) -> String { name_for(dxil, flags, false) @@ -4603,9 +4623,21 @@ pub(crate) mod driver_cache { format!("gfx-{hash:016x}-{bytes:x}.pso") } + fn env_setting() -> Option { + static ON: std::sync::OnceLock> = std::sync::OnceLock::new(); + *ON.get_or_init(|| configured(std::env::var("XCL_PSO_DISK_CACHE").ok().as_deref())) + } + + fn configured(value: Option<&str>) -> Option { + value.map(|v| v.trim() != "0") + } + + fn enabled_with(setting: Option, reference_blobs: bool) -> bool { + setting.unwrap_or(!reference_blobs) + } + pub(crate) fn enabled() -> bool { - static ON: std::sync::OnceLock = std::sync::OnceLock::new(); - *ON.get_or_init(|| std::env::var("XCL_PSO_DISK_CACHE").map_or(true, |v| v.trim() != "0")) + enabled_with(env_setting(), REFERENCE_BLOBS.load(Ordering::Relaxed)) } pub fn path(dxil: &[u8], flags: i32) -> Option { @@ -4725,6 +4757,20 @@ pub(crate) mod driver_cache { assert!(name_for(&[0u8; 0x1234], 2, true).ends_with("-1234-2.chain.pso")); } + #[test] + fn disk_blobs_default_off_only_for_amd_and_can_be_overridden() { + assert!(reference_blob_vendor(0x1002)); + assert!(!reference_blob_vendor(0x10de)); // NVIDIA + assert!(!reference_blob_vendor(0x8086)); // Intel + assert!(!reference_blob_vendor(0x1414)); // WARP + + assert!(!enabled_with(None, true)); + assert!(enabled_with(None, false)); + assert!(enabled_with(configured(Some("1")), true)); + assert!(!enabled_with(configured(Some(" 0 ")), false)); + assert_eq!(configured(None), None); + } + #[test] fn a_kept_blob_is_read_back_and_a_refused_one_is_dropped() { let dir = std::env::temp_dir().join(format!("xcl-driver-cache-test-{}", std::process::id())); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 4966a83039..2a09ca422f 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1567,13 +1567,14 @@ impl Pipelines { unsafe { let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(|e| e.to_string())?; let sel = crate::gpu::adapter::Selector::from_env_or(adapter); - let dev = if sel == crate::gpu::adapter::Selector::Warp { + let (dev, vendor) = if sel == crate::gpu::adapter::Selector::Warp { let warp: IDXGIAdapter = factory.EnumWarpAdapter().map_err(|e| e.to_string())?; - crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())? + (crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, 0) } else { - let (adapter, _) = crate::gpu::adapter::candidates(&factory, &sel).into_iter().next().ok_or_else(|| crate::gpu::adapter::no_match(&factory, &sel))?; - crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())? + let (adapter, desc) = crate::gpu::adapter::candidates(&factory, &sel).into_iter().next().ok_or_else(|| crate::gpu::adapter::no_match(&factory, &sel))?; + (crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, desc.VendorId) }; + crate::hle::d3d12x::real::driver_cache::note_vendor(vendor); let most = pipeline_threads(); let max_dxil = pipeline_cap_kb(std::env::var("XCL_PIPELINE_MAX_KB").ok().as_deref()).saturating_mul(1024); Ok(Pipelines {