From 5cd0c4aecc52ca8c066ae8b97086bd45dc08082a Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 00:43:22 -0800 Subject: [PATCH] D3D12.X: isolate ABI roots by device D3D12 root signatures belong to the device that created them. Cache both plain and static-sampler ABI signatures by device so secondary devices cannot receive a foreign COM object. A synthetic cache test covers identical sampler layouts on distinct devices. Checked with the Windows GNU cross-target cargo check; native Windows GPU tests were unavailable. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/real.rs | 62 +++++++++++++++++++------------ 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 2227307236..e5a61e7dd4 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -177,15 +177,19 @@ 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()); -/// XCL shader ABI root signature on the backend device (raw, kept for the process lifetime) +/// XCL shader ABI root signature on the primary backend device (raw, kept for the process lifetime) static ABI_RS: AtomicUsize = AtomicUsize::new(0); +static ABI_RS_DEV: AtomicUsize = AtomicUsize::new(0); +static ABI_RS_BY_DEV: Mutex>> = Mutex::new(None); /// XCL's shader ABI root signature, once a translated pipeline created it. pub fn abi_rs_cached() -> Option<*mut c_void> { - match ABI_RS.load(Ordering::Acquire) { - 0 => None, - p => Some(p as *mut c_void), + let dev = device_raw() as usize; + let primary = ABI_RS.load(Ordering::Acquire); + if primary != 0 && (dev == 0 || ABI_RS_DEV.load(Ordering::Acquire) == dev) { + return Some(primary as *mut c_void); } + ABI_RS_BY_DEV.lock().unwrap().as_ref()?.get(&dev).map(|&p| p as *mut c_void) } thread_local! { @@ -198,10 +202,7 @@ thread_local! { /// Root signature of the pipelines built from translated console shaders (bind it, then the /// XCL ABI root UAVs io / res / mem, to draw with them). pub fn abi_root_signature() -> Option { - let raw = ABI_RS.load(Ordering::Acquire) as *mut c_void; - if raw.is_null() { - return None; - } + let raw = abi_rs_cached()?; unsafe { ID3D12RootSignature::from_raw_borrowed(&raw).cloned() } } /// Each UMA window as one D3D12 buffer (placed in a heap opened over the process memory): @@ -4228,15 +4229,15 @@ pub fn pso_root_signature(pso: usize) -> Option { PSO_RS.lock().unwrap().as_ref()?.get(&pso).copied() } -/// ABI root signatures with static samplers, by sampler set (owned here, never released). -static ABI_RS_SAMPLERS: Mutex, usize>>> = Mutex::new(None); +/// ABI root signatures with static samplers, by device and sampler set (owned here, never released). +static ABI_RS_SAMPLERS: Mutex), usize>>> = Mutex::new(None); /// The XCL ABI root signature with a pipeline's static samplers (the plain one without). unsafe fn abi_rs_for(this: *mut c_void, samplers: &[crate::gpu::recomp::pso::StaticSampler]) -> Option<*mut c_void> { if samplers.is_empty() { return abi_rs_raw(this); } - let key: Vec = samplers.iter().flat_map(|(w, c)| w.iter().copied().chain([*c as u32])).collect(); + let key = (this as usize, samplers.iter().flat_map(|(w, c)| w.iter().copied().chain([*c as u32])).collect::>()); let mut g = ABI_RS_SAMPLERS.lock().unwrap(); let map = g.get_or_insert_with(HashMap::new); if let Some(&rs) = map.get(&key) { @@ -4256,23 +4257,28 @@ unsafe fn abi_rs_for(this: *mut c_void, samplers: &[crate::gpu::recomp::pso::Sta } } -/// XCL ABI root signature, created once on the backend device. +/// XCL ABI root signature, created once per backend device. unsafe fn abi_rs_raw(this: *mut c_void) -> Option<*mut c_void> { + let dev_raw = this as usize; let cur = ABI_RS.load(Ordering::Acquire); - if cur != 0 { + if cur != 0 && ABI_RS_DEV.load(Ordering::Acquire) == dev_raw { return Some(cur as *mut c_void); } + let mut roots = ABI_RS_BY_DEV.lock().unwrap(); + if let Some(&cached) = roots.as_ref().and_then(|m| m.get(&dev_raw)) { + return Some(cached as *mut c_void); + } let dev = ID3D12Device::from_raw_borrowed(&this)?; match crate::gpu::recomp::pso::abi_root_signature(dev, true) { Ok(rs) => { let raw = rs.into_raw(); - match ABI_RS.compare_exchange(0, raw as usize, Ordering::AcqRel, Ordering::Acquire) { - Ok(_) => Some(raw), - Err(other) => { - drop(ID3D12RootSignature::from_raw(raw)); - Some(other as *mut c_void) - } + if ABI_RS.load(Ordering::Acquire) == 0 { + ABI_RS_DEV.store(dev_raw, Ordering::Release); + ABI_RS.store(raw as usize, Ordering::Release); + } else { + roots.get_or_insert_with(HashMap::new).insert(dev_raw, raw as usize); } + Some(raw) } Err(e) => { note_translation_error(format!("XCL ABI root signature: {e}")); @@ -10670,17 +10676,18 @@ mod mesh_stream_tests { // Unique synthetic sampler key so we never touch global ABI_RS or real sampler caches. let sampler: crate::gpu::recomp::pso::StaticSampler = ([0x55AA_1968, 0xCAFE_1968, 0xBEEF_1968, 0x0001_1968], true); - let key: Vec = sampler.0.iter().copied().chain([1u32]).collect(); + let key = (this as usize, sampler.0.iter().copied().chain([1u32]).collect::>()); + let other_key = (0x0D15_71C7usize, key.1.clone()); struct Cleanup { ext: usize, - key: Vec, + keys: Vec<(usize, Vec)>, } impl Drop for Cleanup { fn drop(&mut self) { CLASSES.lock().unwrap().retain(|c| c.ext != self.ext); if let Some(m) = ABI_RS_SAMPLERS.lock().unwrap().as_mut() { - m.remove(&self.key); + for key in &self.keys { m.remove(key); } } } } @@ -10692,8 +10699,15 @@ mod mesh_stream_tests { public: iface.methods.len(), scanned: iface.methods.len(), }); - ABI_RS_SAMPLERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(key.clone(), 0x1234_5678); - let _cleanup = Cleanup { ext: ext_vtbl, key }; + { + let mut roots = ABI_RS_SAMPLERS.lock().unwrap(); + let roots = roots.get_or_insert_with(HashMap::new); + roots.insert(key.clone(), 0x1234_5678); + roots.insert(other_key.clone(), 0x8765_4321); + } + assert_eq!(unsafe { abi_rs_for(this, &[sampler]) }, Some(0x1234_5678 as *mut c_void)); + assert_eq!(unsafe { abi_rs_for(other_key.0 as *mut c_void, &[sampler]) }, Some(0x8765_4321 as *mut c_void)); + let _cleanup = Cleanup { ext: ext_vtbl, keys: vec![key, other_key] }; let make_tp = || crate::gpu::recomp::pso::TwoPass { cs: vec![0; 16],