From 65704ff166792e6158579031908917fa4d768471 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 01:06:45 -0800 Subject: [PATCH] D3D12.X: complete graphics driver cache identity Translated graphics PSOs could share a driver-blob key even when one retained a native geometry shader that the other did not. Hash and size the final GS bytes, including native GS bytecode, rather than only XCL's generated rectangle shader. Scope bounded RAM entries to the effective per-device root signature. Persisted paths remain adapter-scoped and encode the static-sampler ABI, while in-process entries can no longer cross devices. Stream-output declarations are pointer-backed state not represented by the descriptor key, so leave those pipelines uncached instead of aliasing different layouts. An unreadable nonempty GS is also conservatively uncached. This follows the cache-identity findings in PR #902 and issue #2508. The Windows GNU cross-target library/test check passes; native Windows tests and smoke were unavailable. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/real.rs | 68 +++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index e95d50d066..cf4abdec2e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -5901,23 +5901,25 @@ fn keep_driver_blob( } /// The driver's compiled form of translated graphics pipelines (`GetCachedBlob`), -/// keyed by `(full_state_and_shader_hash, total_dxil_bytes)`, up to `GRAPHICS_BLOBS_MAX`. -static GRAPHICS_BLOBS: DriverBlobs<(u64, usize)> = DriverBlobs::new(); +/// keyed by `(full_state_and_shader_hash, total_dxil_bytes, effective_root_signature)`, up to +/// `GRAPHICS_BLOBS_MAX`. The root address scopes RAM entries to the device; persisted paths are +/// already adapter-scoped and encode the root's static-sampler ABI in the state hash. +static GRAPHICS_BLOBS: DriverBlobs<(u64, usize, usize)> = DriverBlobs::new(); const GRAPHICS_BLOBS_MAX: usize = 256 << 20; -fn graphics_blob(key: (u64, usize)) -> Option>> { +fn graphics_blob(key: (u64, usize, usize)) -> Option>> { GRAPHICS_BLOBS.get(key) } -fn forget_graphics_blob(key: (u64, usize), rejected: &std::sync::Arc>) -> bool { +fn forget_graphics_blob(key: (u64, usize, usize), rejected: &std::sync::Arc>) -> bool { GRAPHICS_BLOBS.forget_if(key, rejected) } -fn remember_graphics_blob(key: (u64, usize), blob: std::sync::Arc>) { +fn remember_graphics_blob(key: (u64, usize, usize), blob: std::sync::Arc>) { GRAPHICS_BLOBS.remember(key, blob, GRAPHICS_BLOBS_MAX); } -fn forget_graphics_blob_if(key: (u64, usize), expected: &std::sync::Arc>) -> bool { +fn forget_graphics_blob_if(key: (u64, usize, usize), expected: &std::sync::Arc>) -> bool { GRAPHICS_BLOBS.forget_if(key, expected) } @@ -5928,7 +5930,7 @@ pub(super) unsafe fn graphics_blob_key( copy: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, vs_dxil: &[u8], ps_dxil: Option<&[u8]>, - rect_gs: Option<&[u8]>, + gs_dxil: Option<&[u8]>, samplers: &[crate::gpu::recomp::pso::StaticSampler], ) -> (u64, usize) { let mut h = 0xcbf2_9ce4_8422_2325u64; @@ -5950,7 +5952,7 @@ pub(super) unsafe fn graphics_blob_key( } else { feed_u64(&mut h, 0); } - if let Some(gs) = rect_gs { + if let Some(gs) = gs_dxil { feed_u64(&mut h, crate::gpu::shader::fnv64(gs)); feed_u64(&mut h, gs.len() as u64); } else { @@ -6035,10 +6037,19 @@ pub(super) unsafe fn graphics_blob_key( feed_u32(&mut h, copy.SampleDesc.Quality); feed_u32(&mut h, copy.NodeMask); feed_u32(&mut h, copy.Flags.0 as u32); - let total_bytes = vs_dxil.len() + ps_dxil.map_or(0, <[u8]>::len) + rect_gs.map_or(0, <[u8]>::len); + let total_bytes = vs_dxil.len() + ps_dxil.map_or(0, <[u8]>::len) + gs_dxil.map_or(0, <[u8]>::len); (h, total_bytes) } +/// Stream-output declarations contain pointer-backed arrays that are not represented in the +/// graphics blob key. Keep those PSOs out of this cache rather than aliasing distinct layouts. +fn graphics_blob_cacheable(requested: bool, desc: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, gs_readable: bool) -> bool { + requested + && desc.StreamOutput.NumEntries == 0 + && desc.StreamOutput.NumStrides == 0 + && (desc.GS.BytecodeLength == 0 || gs_readable) +} + #[cfg(test)] mod graphics_blob_tests { use super::*; @@ -6081,6 +6092,22 @@ mod graphics_blob_tests { } } + #[test] + fn graphics_blob_cache_skips_pointer_backed_stream_output() { + let mut d = D3D12_GRAPHICS_PIPELINE_STATE_DESC::default(); + assert!(graphics_blob_cacheable(true, &d, false)); + assert!(!graphics_blob_cacheable(false, &d, false)); + d.StreamOutput.NumEntries = 1; + assert!(!graphics_blob_cacheable(true, &d, false)); + d.StreamOutput.NumEntries = 0; + d.StreamOutput.NumStrides = 1; + assert!(!graphics_blob_cacheable(true, &d, false)); + d.StreamOutput.NumStrides = 0; + d.GS.BytecodeLength = 4; + assert!(!graphics_blob_cacheable(true, &d, false), "an unreadable native GS has no safe key"); + assert!(graphics_blob_cacheable(true, &d, true)); + } + #[test] fn graphics_blob_key_ignores_struct_padding_bytes() { unsafe { @@ -6410,9 +6437,12 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const if debug_pso_switch("XCL_DEBUG_NO_CULL", ps_hash) { copy.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; } - let bytes = vs_dxil.len() + ps_dxil.as_ref().map_or(0, Vec::len) + rect_gs.as_ref().map_or(0, Vec::len); - let gkey = depth_bounds_key(graphics_blob_key(©, &vs_dxil, ps_dxil.as_deref(), rect_gs.as_deref(), &samplers), depth_bounds); - let library_name = driver_cache::graphics_name(gkey.0, gkey.1); + let final_gs = shader_bytes(©.GS); + let bytes = vs_dxil.len() + ps_dxil.as_ref().map_or(0, Vec::len) + final_gs.map_or(0, <[u8]>::len); + let disk_key = depth_bounds_key(graphics_blob_key(©, &vs_dxil, ps_dxil.as_deref(), final_gs, &samplers), depth_bounds); + let gkey = (disk_key.0, disk_key.1, rs as usize); + let cacheable = graphics_blob_cacheable(cacheable, ©, final_gs.is_some()); + let library_name = driver_cache::graphics_name(disk_key.0, disk_key.1); let library_pso = if !cacheable || ppv.is_null() { None } else if depth_bounds { @@ -6423,7 +6453,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const crate::shaders::pipeline_library::load_graphics(&library_name, ©) }; let mut cached = (cacheable && library_pso.is_none()).then(|| graphics_blob(gkey)).flatten(); - let disk = cacheable.then(|| driver_cache::graphics_path(gkey.0, gkey.1)).flatten(); + let disk = cacheable.then(|| driver_cache::graphics_path(disk_key.0, disk_key.1)).flatten(); let from_disk = cacheable && library_pso.is_none() && cached.is_none(); let mut loaded = None; if from_disk { @@ -7501,12 +7531,13 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: *ppv = std::ptr::null_mut(); let Some(rs) = abi_rs_for(this, &tp.samplers) else { return E_INVALIDARG }; let mesh_bytes = stream.shader_bytes(); - let gkey = (mesh_bytes != 0).then(|| two_pass_driver_id(this)).flatten().and_then(|driver| { + let disk_key = (mesh_bytes != 0).then(|| two_pass_driver_id(this)).flatten().and_then(|driver| { two_pass_identity::two_pass_stream_key(&stream.0, ps_samplers, driver, crate::gpu::recomp::pso::mem_windows()) }); - let gdisk = gkey.and_then(|(hash, bytes)| driver_cache::graphics_path(hash, bytes)); + let gkey = disk_key.map(|(hash, bytes)| (hash, bytes, mesh_rs as usize)); + let gdisk = disk_key.and_then(|(hash, bytes)| driver_cache::graphics_path(hash, bytes)); let stream_desc = D3D12_PIPELINE_STATE_STREAM_DESC { SizeInBytes: stream.0.len(), pPipelineStateSubobjectStream: stream.0.as_ptr() as *mut c_void }; - let library_mesh = gkey.and_then(|(hash, bytes)| { + let library_mesh = disk_key.and_then(|(hash, bytes)| { crate::shaders::pipeline_library::load_stream(&driver_cache::graphics_name(hash, bytes), &stream_desc) }); let mesh_from_library = library_mesh.is_some(); @@ -8617,9 +8648,10 @@ unsafe fn translated_graphics_stream_once(this: *mut c_void, desc: *const c_void let fs: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreatePipelineState"))); let stream = rewrite_graphics_stream(&subs, rs, &vs_c.dxil, ps_dxil.as_deref(), rect_gs.as_deref(), no_stencil, no_cull); let without_rect_gs = rect_gs.as_ref().map(|_| rewrite_graphics_stream(&subs, rs, &vs_c.dxil, ps_dxil.as_deref(), None, no_stencil, no_cull)); - let key = two_pass_identity::graphics_stream_key(&stream.0, &samplers, crate::gpu::recomp::pso::mem_windows()); + let disk_key = two_pass_identity::graphics_stream_key(&stream.0, &samplers, crate::gpu::recomp::pso::mem_windows()); + let key = disk_key.map(|(hash, bytes)| (hash, bytes, rs as usize)); let mut cached = key.and_then(graphics_blob); - let disk = key.and_then(|k| driver_cache::graphics_path(k.0, k.1)); + let disk = disk_key.and_then(|k| driver_cache::graphics_path(k.0, k.1)); let from_disk = key.is_some() && cached.is_none(); let mut loaded = None; if from_disk {