From c48a65c8eb92d7ad2cec5703a5da530de619fcdc Mon Sep 17 00:00:00 2001 From: Lino Emmenegger Date: Wed, 25 Nov 2026 22:12:05 +0100 Subject: [PATCH 1/3] D3D12.X: translated compute pipelines are described with node mask 0 `xcl shaders --pipelines` makes each compute pipeline with NodeMask 0; a title's run copied the title's description, so a title that passes NodeMask 1 handed the driver a description the offline blob was not made for. The blob's file name has only the DXIL and the flags, so both forms share one file: the run refuses the offline blob ("compute retry"), deletes it and writes its own, which the next offline pass refuses in turn (#2453; vkd3d-proton's refusal is measured in #1368). translated_compute_desc builds the description for both sides: the DXIL, its root signature, no blob, NodeMask 0 (XCL drives one adapter, where 0 and 1 are the same node) and the title's flags, which are in the blob's file name. compute_container and Pipelines::make_required both use it; make_required no longer owns the root signature through the description, so its five explicit drops go. Part of #2453. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/real.rs | 57 ++++++++++++++++++++++++++++--- crates/xcl/src/shaders.rs | 13 ++----- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1c6f911d88..87f2db8da1 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4425,6 +4425,58 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const pub(super) mod dispatch_chain; pub(crate) use dispatch_chain::log_counts as log_dispatch_chain_counts; +/// The description the driver gets for the translated compute pipeline of `title`: `dxil` under +/// `rs` (not owned: it has to outlive the description), no blob yet, and of `title` only its +/// `Flags`, which are in the blob's file name (`driver_cache`). `xcl shaders --pipelines` makes +/// its pipelines from a default description with this, so its blobs are the ones a title's run +/// asks for. +/// +/// `NodeMask` is 0 whatever the title's: XCL drives one adapter, where 0 and 1 are the same node, +/// and a driver may refuse a blob made under the other mask (vkd3d-proton does: #1368, #2453). +pub(crate) unsafe fn translated_compute_desc(title: &D3D12_COMPUTE_PIPELINE_STATE_DESC, rs: *mut c_void, dxil: &[u8]) -> D3D12_COMPUTE_PIPELINE_STATE_DESC { + D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute::<*mut c_void, std::mem::ManuallyDrop>>(rs), + CS: bytecode(dxil), + NodeMask: 0, + CachedPSO: D3D12_CACHED_PIPELINE_STATE::default(), + Flags: title.Flags, + } +} + +#[cfg(test)] +mod translated_compute_desc_tests { + use super::*; + + fn fields(d: &D3D12_COMPUTE_PIPELINE_STATE_DESC) -> (Option<*mut c_void>, *const c_void, usize, u32, *const c_void, usize, i32) { + let rs = d.pRootSignature.as_ref().map(|r| r.as_raw()); + (rs, d.CS.pShaderBytecode, d.CS.BytecodeLength, d.NodeMask, d.CachedPSO.pCachedBlob, d.CachedPSO.CachedBlobSizeInBytes, d.Flags.0) + } + + #[test] + fn a_titles_pipeline_is_described_to_the_driver_as_the_offline_pass_describes_it() { + // (never used: the description does not own its root signature) + let rs = 0x1000 as *mut c_void; + let dxil = [1u8, 2, 3, 4]; + // `xcl shaders --pipelines`: a default description + let offline = unsafe { translated_compute_desc(&D3D12_COMPUTE_PIPELINE_STATE_DESC::default(), rs, &dxil) }; + assert_eq!(fields(&offline), (Some(rs), dxil.as_ptr().cast(), 4, 0, std::ptr::null(), 0, 0)); + // a title's: node mask 1 (one adapter: the same node as 0), its console CS and a blob + let console = [5u8, 6]; + let blob = [9u8; 8]; + let title = D3D12_COMPUTE_PIPELINE_STATE_DESC { + CS: bytecode(&console), + NodeMask: 1, + CachedPSO: D3D12_CACHED_PIPELINE_STATE { pCachedBlob: blob.as_ptr().cast(), CachedBlobSizeInBytes: blob.len() }, + ..Default::default() + }; + assert_eq!(fields(&unsafe { translated_compute_desc(&title, rs, &dxil) }), fields(&offline)); + // the title's flags stay: the blob's file name has them + let debug = D3D12_COMPUTE_PIPELINE_STATE_DESC { NodeMask: 1, Flags: D3D12_PIPELINE_STATE_FLAG_TOOL_DEBUG, ..Default::default() }; + let d = unsafe { translated_compute_desc(&debug, rs, &dxil) }; + assert_eq!((d.NodeMask, d.Flags), (0, D3D12_PIPELINE_STATE_FLAG_TOOL_DEBUG)); + } +} + #[derive(Clone, Copy, PartialEq)] enum ComputeBuild { Whole, @@ -4437,10 +4489,7 @@ enum ComputeBuild { unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPELINE_STATE_DESC, rs: *mut c_void, dxil: &[u8], record: u64, riid: *const GUID, ppv: *mut *mut c_void, mode: ComputeBuild) -> (HRESULT, std::time::Duration, bool) { - let mut copy: D3D12_COMPUTE_PIPELINE_STATE_DESC = std::ptr::read(d); - copy.pRootSignature = std::mem::transmute::<*mut c_void, std::mem::ManuallyDrop>>(rs); - copy.CS = bytecode(dxil); - copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default(); + let mut copy = translated_compute_desc(d, rs, dxil); let library_name = driver_cache::name_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain); if !ppv.is_null() { if let Some(pso) = crate::shaders::pipeline_library::load_compute(&library_name, ©) { diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index f9567215f8..8c3f1ac4c2 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1659,6 +1659,7 @@ impl Pipelines { fn make_required(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], chained: bool, path_override: Option, required: bool) -> Result<(), String> { + use windows::core::Interface; use windows::Win32::Graphics::Direct3D12::*; let key: (bool, Vec) = (chained, samplers.iter().flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])).collect()); let layout = self.warm_blobs.as_ref().map(|_| warm_blobs::digest(&key)); @@ -1685,11 +1686,8 @@ impl Pipelines { } } }; - let mut desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { - pRootSignature: std::mem::ManuallyDrop::new(Some(rs)), - CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr() as *const _, BytecodeLength: dxil.len() }, - ..Default::default() - }; + // the description a title's pipeline of this program has (`rs` outlives it) + let mut desc = unsafe { crate::hle::d3d12x::real::translated_compute_desc(&Default::default(), rs.as_raw(), dxil) }; use crate::hle::d3d12x::real::driver_cache; let path = path_override.or_else(|| { #[cfg(test)] @@ -1703,7 +1701,6 @@ impl Pipelines { if let (Some(((book, layout), path)), Some(blob)) = (receipt, cached.as_ref()) { if !pipeline_library::building() && book.contains(&warm_blobs::stamp(path, layout, blob)) { self.verified.fetch_add(1, Ordering::Relaxed); - drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return Ok(()); } } @@ -1714,7 +1711,6 @@ impl Pipelines { if cached.is_none() && over_cap { self.capped.fetch_add(1, Ordering::Relaxed); self.capped_bytes.fetch_add(dxil.len(), Ordering::Relaxed); - drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return Err(format!("Compute DXIL exceeds the native pipeline size limit ({} bytes)", dxil.len())); } { @@ -1740,7 +1736,6 @@ impl Pipelines { *self.running.0.lock().unwrap() -= 1; self.running.1.notify_one(); self.elsewhere.fetch_add(1, Ordering::Relaxed); - drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return Err("Another process is preparing this compute pipeline".into()); } }, @@ -1764,7 +1759,6 @@ impl Pipelines { self.running.1.notify_one(); self.capped.fetch_add(1, Ordering::Relaxed); self.capped_bytes.fetch_add(dxil.len(), Ordering::Relaxed); - drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return Err(format!("The cached compute pipeline was rejected and {} bytes of DXIL exceed the limit", dxil.len())); } let r = if let Some(pso) = from_cache { @@ -1797,7 +1791,6 @@ impl Pipelines { drop(claim); *self.running.0.lock().unwrap() -= 1; self.running.1.notify_one(); - drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); let done = match &r { Ok(_) => self.made.fetch_add(1, Ordering::Relaxed) + 1, Err(_) => self.refused.fetch_add(1, Ordering::Relaxed) + 1, From 8e4272767588b9607ec2a06ce878f8b45d2d698a Mon Sep 17 00:00:00 2001 From: Lino Emmenegger Date: Wed, 25 Nov 2026 22:13:40 +0100 Subject: [PATCH 2/3] D3D12.X: compute driver blobs are named by their static samplers too A compute program's static samplers live only in its root signature: the programs of two S# sets can translate to one DXIL, and the blob file name (DXIL hash, length, flags) was the same for both. Whichever ran second was handed the other's blob, the driver refused it ("compute retry"), and the file was deleted and written again - in a title's run and in `xcl shaders --pipelines` alike (#2453). driver_cache::name_for / path_for take the samplers and add an FNV digest of them (two_pass_identity's samplers_hash_into, as the two-pass compute key does) as `-s`. A program without static samplers keeps its old file name, so its blobs of earlier runs still apply. compute_container gets the samplers from its callers (the single translated pipeline, the dispatch chain's pieces), the compute pass of two-pass pipelines uses them too, and compute pipeline-library entries share the sampler-qualified name. The in-memory blob key already differs by root signature. Part of #2453. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 Assisted-by: Codex (gpt-6.2-sol) Assisted-by: Antigravity (Gemini 4 Neon) --- README.md | 3 + crates/xcl/src/hle/d3d12x/real.rs | 105 +++++++++++++----- .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 5 +- .../src/hle/d3d12x/real/two_pass_identity.rs | 2 +- crates/xcl/src/shaders.rs | 12 +- 5 files changed, 88 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 9f57eef865..056ef0850d 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,9 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - For compute shaders that XCL can split, the runtime first tries the saved driver blob for the whole shader. A usable blob lets the GPU run it in one dispatch. If the blob is missing or the driver rejects it, XCL builds the split pieces. +- **Compute driver cache**: programs with static samplers get cache filenames that include + their sampler state. The first run after upgrading can rebuild those blobs. Programs without + static samplers keep their existing filenames. - **`--pipelines` size cap**: a compute program whose DXIL is over 8 MiB and that has no saved blob is left to the runtime instead of being compiled in the pass — those compiles take tens of minutes and gigabytes and stopped the pass on the biggest programs. `XCL_PIPELINE_MAX_KB` diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 87f2db8da1..b0ef066ec1 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4364,7 +4364,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const let record = crate::gpu::shader::fnv64(cs); let warm = compiled.dispatch_chain.as_ref().and_then(|_| { let rs = abi_rs_for(this, &compiled.samplers)?; - let result = compute_container(this, f, d, rs, &dxil, record, riid, ppv, ComputeBuild::CachedWhole); + let result = compute_container(this, f, d, rs, &compiled.samplers, &dxil, record, riid, ppv, ComputeBuild::CachedWhole); (result.0 >= 0).then_some((rs, result)) }); if let Some(plan) = compiled.dispatch_chain.filter(|_| warm.is_none()) { @@ -4384,7 +4384,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const Some(hit) => hit, None => { let Some(rs) = abi_rs_for(this, &compiled.samplers) else { return Some(Err(())) }; - (rs, compute_container(this, f, d, rs, &dxil, record, riid, ppv, ComputeBuild::Whole)) + (rs, compute_container(this, f, d, rs, &compiled.samplers, &dxil, record, riid, ppv, ComputeBuild::Whole)) } }; LAST_COMPUTE_RS.with(|c| c.set(rs as usize)); @@ -4399,7 +4399,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const crate::xlog_warn!( "D3D12.X: the driver refused the compute PSO of record {record:016x} ({} B DXIL, driver cache {}) with {hr:#x} after {} ms in the driver: its dispatches are skipped", dxil.len(), - driver_cache::name(&dxil, d.Flags.0), + driver_cache::name(&dxil, &compiled.samplers, d.Flags.0), in_driver.as_millis() ); } @@ -4487,10 +4487,12 @@ enum ComputeBuild { Chain, } +/// `samplers` are the static samplers of `rs`. unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPELINE_STATE_DESC, rs: *mut c_void, + samplers: &[crate::gpu::recomp::pso::StaticSampler], dxil: &[u8], record: u64, riid: *const GUID, ppv: *mut *mut c_void, mode: ComputeBuild) -> (HRESULT, std::time::Duration, bool) { let mut copy = translated_compute_desc(d, rs, dxil); - let library_name = driver_cache::name_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain); + let library_name = driver_cache::name_for(dxil, samplers, copy.Flags.0, mode == ComputeBuild::Chain); if !ppv.is_null() { if let Some(pso) = crate::shaders::pipeline_library::load_compute(&library_name, ©) { *ppv = pso.into_raw(); @@ -4502,7 +4504,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL let key = compute_blob_key(dxil, rs as usize, copy.Flags.0); let mut cached = compute_blob(key); // … and one built in an earlier run gets the blob kept on disk then (`driver_cache`) - let disk = driver_cache::path_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain); + let disk = driver_cache::path_for(dxil, samplers, copy.Flags.0, mode == ComputeBuild::Chain); let from_disk = cached.is_none(); if from_disk { cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); @@ -4558,11 +4560,12 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL /// run still goes through the driver's compiler. On an NVIDIA GPU that is 5 – 27 s for each of /// a retail title's larger compute programs when its world loads: the title dispatches without /// them meanwhile and is ended before all are built, so the next run starts the same compiles -/// 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. +/// again. A blob whose build took `KEEP` or longer is written under the hash of its DXIL, its +/// flags and its static samplers (`\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. pub(crate) mod driver_cache { + use crate::gpu::recomp::pso::StaticSampler; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; @@ -4585,16 +4588,28 @@ pub(crate) mod driver_cache { (HITS.load(Ordering::Relaxed), STALE.load(Ordering::Relaxed), STORED.load(Ordering::Relaxed)) } - /// The file of the pipeline made from `dxil` with `flags`. - pub fn name(dxil: &[u8], flags: i32) -> String { - name_for(dxil, flags, false) + /// The file of the pipeline made from `dxil` with `flags`, under a root signature with + /// `samplers` as its static samplers. + pub fn name(dxil: &[u8], samplers: &[StaticSampler], flags: i32) -> String { + name_for(dxil, samplers, flags, false) } /// Like [`name`], using `.chain.pso` for pipelines built with the dispatch chain's root /// signature (`dispatch_chain`). - pub fn name_for(dxil: &[u8], flags: i32, chained: bool) -> String { + /// + /// Static samplers are only in the root signature: the programs of two S# sets can have one + /// DXIL, and each one's blob is refused for the other (#2453). A program without static + /// samplers keeps the name of its blob of earlier runs. + pub fn name_for(dxil: &[u8], samplers: &[StaticSampler], flags: i32, chained: bool) -> String { let ext = if chained { "chain.pso" } else { "pso" }; - format!("{:016x}-{:x}-{:x}.{ext}", crate::gpu::shader::fnv64(dxil), dxil.len(), flags) + let samplers = if samplers.is_empty() { + String::new() + } else { + let mut h = super::two_pass_identity::Fnv64::new(); + super::two_pass_identity::samplers_hash_into(&mut h, samplers); + format!("-s{:016x}", h.finish()) + }; + format!("{:016x}-{:x}-{:x}{samplers}.{ext}", crate::gpu::shader::fnv64(dxil), dxil.len(), flags) } /// The file of a translated graphics pipeline keyed by its full state + shader digest. @@ -4607,12 +4622,12 @@ pub(crate) mod driver_cache { *ON.get_or_init(|| std::env::var("XCL_PSO_DISK_CACHE").map_or(true, |v| v.trim() != "0")) } - pub fn path(dxil: &[u8], flags: i32) -> Option { - path_for(dxil, flags, false) + pub fn path(dxil: &[u8], samplers: &[StaticSampler], flags: i32) -> Option { + path_for(dxil, samplers, flags, false) } - pub fn path_for(dxil: &[u8], flags: i32, chained: bool) -> Option { - enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name_for(dxil, flags, chained))) + pub fn path_for(dxil: &[u8], samplers: &[StaticSampler], flags: i32, chained: bool) -> Option { + enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name_for(dxil, samplers, flags, chained))) } pub fn graphics_path(hash: u64, bytes: usize) -> Option { @@ -4701,20 +4716,50 @@ pub(crate) mod driver_cache { #[test] fn a_file_is_named_after_its_dxil_and_flags() { - assert_eq!(name(b"abc", 0), name(b"abc", 0)); - assert_ne!(name(b"abc", 0), name(b"abd", 0)); - assert_ne!(name(b"abc", 0), name(b"abc", 1)); + assert_eq!(name(b"abc", &[], 0), name(b"abc", &[], 0)); + assert_ne!(name(b"abc", &[], 0), name(b"abd", &[], 0)); + assert_ne!(name(b"abc", &[], 0), name(b"abc", &[], 1)); // (the length is in the name: two programs of one hash and different lengths differ) - assert!(name(&[0u8; 0x1234], 2).ends_with("-1234-2.pso")); - assert_eq!(name_for(b"abc", 0, false), name(b"abc", 0)); - assert_ne!(name_for(b"abc", 0, true), name_for(b"abc", 0, false)); - assert!(name_for(&[0u8; 0x1234], 2, true).ends_with("-1234-2.chain.pso")); + assert!(name(&[0u8; 0x1234], &[], 2).ends_with("-1234-2.pso")); + assert_eq!(name_for(b"abc", &[], 0, false), name(b"abc", &[], 0)); + assert_ne!(name_for(b"abc", &[], 0, true), name_for(b"abc", &[], 0, false)); + assert!(name_for(&[0u8; 0x1234], &[], 2, true).ends_with("-1234-2.chain.pso")); + } + + #[test] + fn one_dxil_under_other_static_samplers_is_another_file() { + // S# words that differ in one word, and a comparison sampler of the first's words + let point: StaticSampler = ([0, 0, 0, 0], false); + let linear: StaticSampler = ([0, 0, 0x0050_0000, 0], false); + let compared: StaticSampler = (point.0, true); + let dxil = b"one program"; + // a program without static samplers keeps the name of its earlier runs' blob + assert_eq!(name(dxil, &[], 0), format!("{:016x}-{:x}-0.pso", crate::gpu::shader::fnv64(dxil), dxil.len())); + let names = [ + name(dxil, &[], 0), + name(dxil, &[point], 0), + name(dxil, &[linear], 0), + name(dxil, &[compared], 0), + name(dxil, &[point, linear], 0), + // (the order is the root signature's registers s0, s1…) + name(dxil, &[linear, point], 0), + name_for(dxil, &[point], 0, true), + ]; + for (i, a) in names.iter().enumerate() { + for b in &names[i + 1..] { + assert_ne!(a, b); + } + } + let copy = dxil.to_vec(); + assert_eq!(name(dxil, &[point, linear], 0), name(©, &[point, linear], 0)); + assert!(name(dxil, &[point], 2).starts_with(&name(dxil, &[], 2).replace(".pso", "-s"))); + assert!(name_for(dxil, &[point], 0, true).ends_with(".chain.pso")); } #[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())); - let p = dir.join("driver").join(name(b"program", 0)); + let p = dir.join("driver").join(name(b"program", &[], 0)); assert_eq!(load(&p), None, "nothing kept yet"); assert!(!write(&p, &[]), "an empty blob is not kept"); keep(&p, &[1, 2, 3], std::time::Duration::from_millis(10)); @@ -4811,7 +4856,7 @@ mod compute_blob_tests { let code = std::fs::read(input).unwrap(); let c = console_to_dxil_for(&code, Stage::Compute, 0, wave32_only(raw)).unwrap(); assert!(c.dispatch_chain.is_some()); - let path = driver_cache::path(&c.dxil, 0).unwrap(); + let path = driver_cache::path(&c.dxil, &c.samplers, 0).unwrap(); assert!(path.starts_with(&cache)); assert_eq!(path.exists(), mode == "offline", "expected driver cache entry at {}", path.display()); unsafe { @@ -6516,10 +6561,10 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: // Reuse the compiled driver blob of the compute pass across all `(MS/GS, PS)` pipelines that // share `tp.cs` (in memory and on disk), matching `translated_compute_pso_once`. let ckey = compute_blob_key(&tp.cs, rs as usize, 0); - let library_compute = crate::shaders::pipeline_library::load_compute(&driver_cache::name(&tp.cs, 0), &cdesc); + let library_compute = crate::shaders::pipeline_library::load_compute(&driver_cache::name(&tp.cs, &tp.samplers, 0), &cdesc); let compute_from_library = library_compute.is_some(); let mut ccached = if compute_from_library { None } else { compute_blob(ckey) }; - let cdisk = driver_cache::path(&tp.cs, 0); + let cdisk = driver_cache::path(&tp.cs, &tp.samplers, 0); let cfrom_disk = !compute_from_library && ccached.is_none(); if cfrom_disk { ccached = cdisk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); @@ -6583,7 +6628,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: ).and_then(|_| { let compute_pso = ID3D12PipelineState::from_raw_borrowed(&compute) .ok_or_else(|| "The two-pass compute pipeline has no native object".to_string())?; - crate::shaders::pipeline_library::store(driver_cache::name(&tp.cs, 0), compute_pso) + crate::shaders::pipeline_library::store(driver_cache::name(&tp.cs, &tp.samplers, 0), compute_pso) }); if let Err(e) = library { note_translation_error(e); diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 6fa76b4cbf..004615517c 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -33,6 +33,7 @@ struct PipelineBuilder { device: ID3D12Device, create: PsoFn, signature: ID3D12RootSignature, + samplers: Vec, flags: D3D12_PIPELINE_STATE_FLAGS, node: u32, record: u64, @@ -43,7 +44,7 @@ impl PipelineBuilder { let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { NodeMask: self.node, Flags: self.flags, ..Default::default() }; let mut raw = std::ptr::null_mut(); let (hr, _, _) = compute_container(self.device.as_raw(), self.create, &desc, self.signature.as_raw(), - dxil, self.record, iid, &mut raw, ComputeBuild::Chain); + &self.samplers, dxil, self.record, iid, &mut raw, ComputeBuild::Chain); (hr >= 0 && !raw.is_null()).then(|| ID3D12PipelineState::from_raw(raw)) } } @@ -77,7 +78,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT let dev = ID3D12Device::from_raw_borrowed(&this)?; let signature = pso::dispatch_chain_root_signature(dev, samplers).ok()?; let rs = signature.as_raw() as usize; - let builder = Arc::new(PipelineBuilder { device: dev.clone(), create, signature, + let builder = Arc::new(PipelineBuilder { device: dev.clone(), create, signature, samplers: samplers.to_vec(), flags: desc.Flags, node: desc.NodeMask, record }); let jobs = (0..plan.pieces.len()).map(|i| { let plan = plan.clone(); diff --git a/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs b/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs index 187c9885fa..88ef3a11dc 100644 --- a/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs +++ b/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs @@ -35,7 +35,7 @@ impl Fnv64 { } } -fn samplers_hash_into(h: &mut Fnv64, samplers: &[crate::gpu::recomp::pso::StaticSampler]) { +pub(super) fn samplers_hash_into(h: &mut Fnv64, samplers: &[crate::gpu::recomp::pso::StaticSampler]) { h.u64(samplers.len() as u64); for (w, cmp) in samplers { for word in w { diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 8c3f1ac4c2..906d2fa4b7 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1507,9 +1507,9 @@ mod pipeline_cap_tests { pipelines.max_dxil = max_piece; pipelines.make(&compiled); - let p0_path = scratch.0.join(driver_cache::name_for(&piece0, 0, true)); - let p1_path = scratch.0.join(driver_cache::name_for(&piece1, 0, true)); - let whole_path = scratch.0.join(driver_cache::name_for(&whole, 0, false)); + let p0_path = scratch.0.join(driver_cache::name_for(&piece0, &[], 0, true)); + let p1_path = scratch.0.join(driver_cache::name_for(&piece1, &[], 0, true)); + let whole_path = scratch.0.join(driver_cache::name_for(&whole, &[], 0, false)); assert_eq!(pipelines.split_programs.load(Ordering::Relaxed), 1); assert_eq!(pipelines.split_pieces.load(Ordering::Relaxed), 2); assert_eq!(pipelines.made.load(Ordering::Relaxed), 2); @@ -1692,9 +1692,9 @@ impl Pipelines { let path = path_override.or_else(|| { #[cfg(test)] if let Some(dir) = self.cache_dir.as_deref() { - return Some(dir.join(driver_cache::name_for(dxil, desc.Flags.0, chained))); + return Some(dir.join(driver_cache::name_for(dxil, samplers, desc.Flags.0, chained))); } - driver_cache::path_for(dxil, desc.Flags.0, chained) + driver_cache::path_for(dxil, samplers, desc.Flags.0, chained) }); let mut cached = path.as_deref().and_then(driver_cache::load); let receipt = self.warm_blobs.as_ref().zip(layout).zip(path.as_deref()); @@ -1785,7 +1785,7 @@ impl Pipelines { pso }; let library = match &r { - Ok(pso) => pipeline_library::store(driver_cache::name_for(dxil, desc.Flags.0, chained), pso), + Ok(pso) => pipeline_library::store(driver_cache::name_for(dxil, samplers, desc.Flags.0, chained), pso), Err(_) => Ok(()), }; drop(claim); From befb5f286b59564d5192b8aec8fedcbaddb7b556 Mon Sep 17 00:00:00 2001 From: Lino Emmenegger Date: Wed, 25 Nov 2026 22:14:56 +0100 Subject: [PATCH 3/3] Shaders: --pipelines turns on a title's device layers before its device `xcl shaders --precompile --pipelines` made its device without the layers a title's run turns on: XCL_D3D12_DEBUG / d3d12_debug and XCL_DRED were ignored. With the debug layer asked for, the layer came on later, when precompile's capability queries started the GPU test runner (runner::init enables it before its own device), and enabling the debug layer after a device is made removes that device - the one the pass was making its pipelines on (#2453). enable_device_layers is the debug layer / DRED setup of real::init_with_remap, moved into a function; init_with_remap and Pipelines::for_adapter call it before they make their device. The debug layer goes on once per process (agility::enable_debug_layer), so the runner's later call does nothing. Part of #2453. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/real.rs | 63 ++++++++++++++++++------------- crates/xcl/src/shaders.rs | 3 ++ 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index b0ef066ec1..c370288e77 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -474,6 +474,41 @@ fn graphics_wave32(device: *mut c_void) -> bool { } } +/// The debug layer (`d3d12_debug` / `XCL_D3D12_DEBUG`) and DRED (`XCL_DRED`), turned on before a +/// device is made: for the title's device (`init_with_remap`) and the one `xcl shaders --pipelines` +/// makes its blobs on. The debug layer goes on once per process, and a device made before it is +/// removed then. Whether the debug layer was asked for. +pub(crate) fn enable_device_layers(cfg: &crate::config::Config) -> bool { + let debug = cfg.d3d12_debug || std::env::var_os("XCL_D3D12_DEBUG").is_some(); + if debug { + match crate::gpu::agility::enable_debug_layer() { + Ok(()) => { + crate::xlog_info!("D3D12: debug layer enabled"); + } + Err(e) => { + crate::xlog_warn!("D3D12: debug layer 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. + if std::env::var_os("XCL_DRED").is_some() { + match crate::gpu::agility::configuration_interface::( + &CLSID_D3D12DeviceRemovedExtendedData, + ) { + Ok(d) => unsafe { + d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); + d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); + }, + Err(e) => { + crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}"); + } + } + } + debug +} + /// Create the process-wide D3D12 device (the adapter `d3d12_adapter` / `XCL_ADAPTER` chooses, /// by default the high-performance GPU → WARP); once – later calls /// return the existing device's adapter. @@ -496,33 +531,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(); - if debug { - match crate::gpu::agility::enable_debug_layer() { - Ok(()) => { - crate::xlog_info!("D3D12: debug layer enabled"); - } - Err(e) => { - crate::xlog_warn!("D3D12: debug layer 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. - if std::env::var_os("XCL_DRED").is_some() { - match crate::gpu::agility::configuration_interface::( - &CLSID_D3D12DeviceRemovedExtendedData, - ) { - Ok(d) => { - d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); - d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON); - } - Err(e) => { - crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}"); - } - } - } + let debug = enable_device_layers(cfg); 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(); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 906d2fa4b7..313eddae9f 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1565,6 +1565,9 @@ impl Pipelines { fn for_adapter(adapter: &str) -> Result { use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0; use windows::Win32::Graphics::Dxgi::*; + // the layers a title's device has, turned on before this device is made: turned on later + // (by `recomp::runner`'s capability queries), the debug layer removes this device + crate::hle::d3d12x::real::enable_device_layers(crate::state::config()); 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);