From 468771949addb013b8d0d172fdec19e8411e0ddf Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 22:49:21 -0800 Subject: [PATCH] D3D12.X: make projected stream caching conservative Checked parsing retains a known graphics-stream prefix while marking unknown or truncated suffixes uncacheable. Thread that decision through synchronous and worker builds so projected descriptors cannot populate memory or driver caches when omitted state may affect identity. Also preserve the legacy-representable RASTERIZER1 and RASTERIZER2 fields. Fractional depth bias and RASTERIZER2 line mode remain for the later native-stream stage. Adapted from PR #2621 by Decodere. Assisted-by: Codex (GPT-5.6-sol) Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/real.rs | 184 +++++++++++++++++++++++++----- 1 file changed, 155 insertions(+), 29 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 49a79f55bc..e6dcefb31a 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -5690,11 +5690,11 @@ fn create_graphics_pso_cached( } } -unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> Option { - with_switch_fallback(|| translated_graphics_pso_once(this, f, desc, riid, ppv, false)) +unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, cacheable: bool) -> Option { + with_switch_fallback(|| translated_graphics_pso_once(this, f, desc, riid, ppv, cacheable, false)) } -unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, depth_bounds: bool) -> Option { +unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, cacheable: bool, depth_bounds: bool) -> Option { use crate::gpu::recomp::pso::{console_to_dxil_for, is_console, Stage}; if !crate::gpu::shader::readable(desc as usize, std::mem::size_of::()) { return None; @@ -5770,7 +5770,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const 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 library_pso = if ppv.is_null() { + let library_pso = if !cacheable || ppv.is_null() { None } else if depth_bounds { let stream = depth_bounds_stream(©); @@ -5779,9 +5779,9 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const } else { crate::shaders::pipeline_library::load_graphics(&library_name, ©) }; - let mut cached = library_pso.is_none().then(|| graphics_blob(gkey)).flatten(); - let disk = driver_cache::graphics_path(gkey.0, gkey.1); - let from_disk = library_pso.is_none() && cached.is_none(); + 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 from_disk = cacheable && library_pso.is_none() && cached.is_none(); let mut loaded = None; if from_disk { loaded = disk.as_deref().and_then(driver_cache::load); @@ -5816,7 +5816,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const crate::shaders::warm_replay::accepted(disk.as_deref(), blob); } } - if build.cache_eligible { + if cacheable && build.cache_eligible { keep_driver_blob(&GRAPHICS_BLOBS, GRAPHICS_BLOBS_MAX, gkey, bytes, disk.as_deref(), build.built_in_driver, || read_pso_cached_blob(*ppv)); if let (Some(p), Some(b)) = (disk.as_deref(), graphics_blob(gkey)) { @@ -5825,7 +5825,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const } else if build.reused && from_disk { driver_cache::note(true, loaded.take()); } - if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { + if cacheable && hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { if let Some(pso) = ID3D12PipelineState::from_raw_borrowed(&*ppv) { if let Err(e) = crate::shaders::pipeline_library::store(driver_cache::graphics_name(gkey.0, gkey.1), pso) { note_translation_error(e); @@ -6262,7 +6262,12 @@ unsafe fn go_background( unsafe fn build_graphics(dev: Dev, d: &OwnedDesc, out: *mut *mut c_void) -> bool { let iid = pc_iid(Kind::PipelineState); - matches!(translated_graphics_pso(dev.0 as *mut c_void, std::mem::transmute::(dev.1), d.ptr(), &iid, out), Some(hr) if hr >= 0) + matches!(translated_graphics_pso(dev.0 as *mut c_void, std::mem::transmute::(dev.1), d.ptr(), &iid, out, true), Some(hr) if hr >= 0) +} + +unsafe fn build_graphics_uncacheable(dev: Dev, d: &OwnedDesc, out: *mut *mut c_void) -> bool { + let iid = pc_iid(Kind::PipelineState); + matches!(translated_graphics_pso(dev.0 as *mut c_void, std::mem::transmute::(dev.1), d.ptr(), &iid, out, false), Some(hr) if hr >= 0) } /// This process is a shader preparation worker (`prepare_device`): it keeps the driver's blob of @@ -6287,7 +6292,7 @@ pub(crate) unsafe fn prepare_graphics(device: usize, desc: &D3D12_GRAPHICS_PIPEL let this = device as *mut c_void; let f: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreateGraphicsPipelineState"))); let mut out: *mut c_void = std::ptr::null_mut(); - let hr = with_switch_fallback(|| translated_graphics_pso_once(this, f, desc as *const _ as _, &pc_iid(Kind::PipelineState), &mut out, depth_bounds)); + let hr = with_switch_fallback(|| translated_graphics_pso_once(this, f, desc as *const _ as _, &pc_iid(Kind::PipelineState), &mut out, true, depth_bounds)); let made = matches!(hr, Some(hr) if hr >= 0) && !out.is_null(); super::pso_async::release(out as usize); made @@ -6304,19 +6309,20 @@ unsafe fn build_stream(dev: Dev, d: &OwnedDesc, out: *mut *mut c_void) -> bool { } pub(super) unsafe extern "system" fn dev_create_graphics_pso(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { - graphics_pso(this, desc, riid, ppv, false) + graphics_pso(this, desc, riid, ppv, false, true) } /// `dev_create_graphics_pso`; `stream`: the desc is a pipeline stream's (`stream_pipeline`). -unsafe fn graphics_pso(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, stream: bool) -> HRESULT { +unsafe fn graphics_pso(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, stream: bool, cacheable: bool) -> HRESULT { super::null::capture_graphics_desc(desc as *const u8); let f: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreateGraphicsPipelineState"))); - let (translated, missed) = if super::pso_async::enabled() { attempt(|| translated_graphics_pso(this, f, desc, riid, ppv)) } else { (translated_graphics_pso(this, f, desc, riid, ppv), false) }; + let (translated, missed) = if super::pso_async::enabled() { attempt(|| translated_graphics_pso(this, f, desc, riid, ppv, cacheable)) } else { (translated_graphics_pso(this, f, desc, riid, ppv, cacheable), false) }; if missed { - if let Some(hr) = own_graphics(desc).and_then(|o| go_background(this, f, desc, riid, ppv, false, o, standin_graphics, graphics_key, build_graphics)) { + let build = if cacheable { build_graphics } else { build_graphics_uncacheable }; + if let Some(hr) = own_graphics(desc).and_then(|o| go_background(this, f, desc, riid, ppv, false, o, standin_graphics, graphics_key, build)) { return hr; } - if let Some(hr) = translated_graphics_pso(this, f, desc, riid, ppv) { + if let Some(hr) = translated_graphics_pso(this, f, desc, riid, ppv, cacheable) { return hr; } } else if let Some(hr) = translated { @@ -6557,24 +6563,41 @@ fn stream_subobjects(buf: &[u8]) -> Option> { /// The stream's subobjects up to the first of a type XCL does not know (logged): a console /// shader in front of it must still be found. -fn known_subobjects(buf: &[u8]) -> Vec<(u32, &[u8])> { +// Checked parsing and extended rasterizer projection adapted from PR #2621 by Decodere. +fn known_subobjects_checked(buf: &[u8]) -> (Vec<(u32, &[u8])>, bool) { let mut out = Vec::new(); let mut off = 0usize; - while off + 8 <= buf.len() { + loop { + if off == buf.len() { + return (out, true); + } + let Some(header) = off.checked_add(8) else { return (out, false) }; + if header > buf.len() { + return (out, false); + } let ty = u32::from_le_bytes(buf[off..off + 4].try_into().unwrap()); let Some((psize, palign)) = super::null::subobject_payload(ty) else { static N: AtomicU64 = AtomicU64::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { crate::xlog_warn!("D3D12.X: pipeline stream: unknown subobject type {ty} at {off:#x} of {:#x} - the rest is not read", buf.len()); } - break; + return (out, false); }; let poff = if palign == 8 { 8 } else { 4 }; - let Some(p) = buf.get(off + poff..off + poff + psize) else { break }; + let Some(start) = off.checked_add(poff) else { return (out, false) }; + let Some(end) = start.checked_add(psize) else { return (out, false) }; + let Some(p) = buf.get(start..end) else { return (out, false) }; out.push((ty, p)); - off += (poff + psize + 7) & !7; + let Some(next) = end.checked_add(7).map(|n| n & !7) else { return (out, false) }; + if next > buf.len() { + return (out, false); + } + off = next; } - out +} + +fn known_subobjects(buf: &[u8]) -> Vec<(u32, &[u8])> { + known_subobjects_checked(buf).0 } unsafe fn payload_shader<'a>(p: &[u8]) -> Option<&'a [u8]> { @@ -7580,7 +7603,7 @@ mod pso_blend_and_tsharp_tests { } enum StreamPipeline { - Graphics(Box), + Graphics(Box, bool), Compute(Box), /// neither with console shaders (or not readable) Unknown, @@ -7600,7 +7623,7 @@ unsafe fn stream_pipeline(desc: *const c_void) -> StreamPipeline { if !crate::gpu::shader::readable(base, size) { return StreamPipeline::Unknown; } - let subs = known_subobjects(std::slice::from_raw_parts(base as *const u8, size)); + let (subs, cacheable) = known_subobjects_checked(std::slice::from_raw_parts(base as *const u8, size)); let console = |ty: u32| subs.iter().find(|s| s.0 == ty).and_then(|s| payload_shader(s.1)).is_some_and(is_console); if console(6) { let mut c: D3D12_COMPUTE_PIPELINE_STATE_DESC = std::mem::zeroed(); @@ -7693,8 +7716,7 @@ unsafe fn stream_pipeline(desc: *const c_void) -> StreamPipeline { BackFace: face(&d.BackFace), }; } - 27 | 28 => { - // RASTERIZER1 / 2: fill, cull, winding, depth bias (float), clamp, slope, depth clip + 27 => { let mut d: D3D12_RASTERIZER_DESC1 = std::mem::zeroed(); put(&mut d, p); g.RasterizerState = D3D12_RASTERIZER_DESC { @@ -7705,6 +7727,25 @@ unsafe fn stream_pipeline(desc: *const c_void) -> StreamPipeline { DepthBiasClamp: d.DepthBiasClamp, SlopeScaledDepthBias: d.SlopeScaledDepthBias, DepthClipEnable: d.DepthClipEnable, + MultisampleEnable: d.MultisampleEnable, + AntialiasedLineEnable: d.AntialiasedLineEnable, + ForcedSampleCount: d.ForcedSampleCount, + ConservativeRaster: d.ConservativeRaster, + }; + } + 28 => { + let mut d: D3D12_RASTERIZER_DESC2 = std::mem::zeroed(); + put(&mut d, p); + g.RasterizerState = D3D12_RASTERIZER_DESC { + FillMode: d.FillMode, + CullMode: d.CullMode, + FrontCounterClockwise: d.FrontCounterClockwise, + DepthBias: d.DepthBias as i32, + DepthBiasClamp: d.DepthBiasClamp, + SlopeScaledDepthBias: d.SlopeScaledDepthBias, + DepthClipEnable: d.DepthClipEnable, + ForcedSampleCount: d.ForcedSampleCount, + ConservativeRaster: d.ConservativeRaster, ..Default::default() }; } @@ -7721,7 +7762,7 @@ unsafe fn stream_pipeline(desc: *const c_void) -> StreamPipeline { if g.PrimitiveTopologyType.0 != RECT_LIST_TYPE { pc_topology_type(&mut g); } - StreamPipeline::Graphics(Box::new(g)) + StreamPipeline::Graphics(Box::new(g), cacheable) } /// The bytes of a stream payload as a desc field (no references counted: the desc only borrows). @@ -7765,7 +7806,7 @@ pub(super) unsafe extern "system" fn dev_create_pipeline_state(this: *mut c_void return hr; } match stream_pipeline(desc) { - StreamPipeline::Graphics(d) => return graphics_pso(this, &*d as *const _ as *const c_void, riid, ppv, true), + StreamPipeline::Graphics(d, cacheable) => return graphics_pso(this, &*d as *const _ as *const c_void, riid, ppv, true, cacheable), StreamPipeline::Compute(d) => return dev_create_compute_pso(this, &*d as *const _ as *const c_void, riid, ppv), StreamPipeline::Unknown => {} } @@ -9250,6 +9291,91 @@ mod map_tests { } } + #[test] + fn known_stream_suffix_controls_cacheability() { + let console_vs = crate::gpu::xbox::tests::sample( + 1, + &[crate::gpu::xbox::tests::fssm(&crate::gpu::xbox::tests::CODE, &[])], + ); + let mut stream = StreamOut::default(); + stream.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, &console_vs); + let (subs, cacheable) = known_subobjects_checked(&stream.0); + assert!(cacheable); + assert_eq!(subs.len(), 1); + + let mut unknown = stream.0.clone(); + unknown.extend_from_slice(&999u32.to_le_bytes()); + unknown.extend_from_slice(&0u32.to_le_bytes()); + let (subs, cacheable) = known_subobjects_checked(&unknown); + assert!(!cacheable, "an unknown suffix must not share a projected-desc cache entry"); + assert_eq!(subs.len(), 1, "the known prefix remains usable"); + let desc = [unknown.len(), unknown.as_ptr() as usize]; + assert!(matches!(unsafe { stream_pipeline(desc.as_ptr() as *const c_void) }, StreamPipeline::Graphics(_, false))); + + let mut truncated_header = stream.0.clone(); + truncated_header.extend_from_slice(&[1, 2, 3, 4]); + assert!(!known_subobjects_checked(&truncated_header).1); + let mut truncated_payload = stream.0.clone(); + truncated_payload.pop(); + assert!(!known_subobjects_checked(&truncated_payload).1); + } + + #[test] + fn stream_pipeline_projects_legacy_rasterizer_fields() { + let console_vs = crate::gpu::xbox::tests::sample( + 1, + &[crate::gpu::xbox::tests::fssm(&crate::gpu::xbox::tests::CODE, &[])], + ); + let parse = |ty: u32, raster: &[u8]| -> (D3D12_RASTERIZER_DESC, bool) { + let mut stream = StreamOut::default(); + stream.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, &console_vs); + stream.raw(ty, raster, false); + let desc = [stream.0.len(), stream.0.as_ptr() as usize]; + match unsafe { stream_pipeline(desc.as_ptr() as *const c_void) } { + StreamPipeline::Graphics(g, cacheable) => (g.RasterizerState, cacheable), + _ => panic!("expected graphics stream pipeline"), + } + }; + let bytes = |p: *const u8, n| unsafe { std::slice::from_raw_parts(p, n) }; + + let r1 = D3D12_RASTERIZER_DESC1 { + FillMode: D3D12_FILL_MODE_WIREFRAME, + CullMode: D3D12_CULL_MODE_FRONT, + FrontCounterClockwise: true.into(), + DepthBias: 7.75, + DepthBiasClamp: 2.5, + SlopeScaledDepthBias: 1.5, + DepthClipEnable: false.into(), + MultisampleEnable: true.into(), + AntialiasedLineEnable: true.into(), + ForcedSampleCount: 4, + ConservativeRaster: D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON, + }; + let (got, cacheable) = parse(27, bytes(&r1 as *const _ as *const u8, std::mem::size_of_val(&r1))); + assert!(cacheable); + assert_eq!((got.FillMode, got.CullMode, got.DepthBias), (r1.FillMode, r1.CullMode, 7)); + assert!(got.MultisampleEnable.as_bool() && got.AntialiasedLineEnable.as_bool()); + assert_eq!((got.ForcedSampleCount, got.ConservativeRaster), (4, r1.ConservativeRaster)); + + let r2 = D3D12_RASTERIZER_DESC2 { + FillMode: D3D12_FILL_MODE_SOLID, + CullMode: D3D12_CULL_MODE_NONE, + FrontCounterClockwise: false.into(), + DepthBias: -3.25, + DepthBiasClamp: 3.5, + SlopeScaledDepthBias: -0.5, + DepthClipEnable: true.into(), + LineRasterizationMode: D3D12_LINE_RASTERIZATION_MODE_QUADRILATERAL_WIDE, + ForcedSampleCount: 8, + ConservativeRaster: D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON, + }; + let (got, cacheable) = parse(28, bytes(&r2 as *const _ as *const u8, std::mem::size_of_val(&r2))); + assert!(cacheable); + assert_eq!((got.FillMode, got.CullMode, got.DepthBias), (r2.FillMode, r2.CullMode, -3)); + assert!(!got.MultisampleEnable.as_bool() && !got.AntialiasedLineEnable.as_bool()); + assert_eq!((got.ForcedSampleCount, got.ConservativeRaster), (8, r2.ConservativeRaster)); + } + #[test] fn stream_pipeline_defaults_depth_enable_from_dsv_format() { let console_vs = crate::gpu::xbox::tests::sample( @@ -9292,7 +9418,7 @@ mod map_tests { } let desc = StreamDesc { size: stream.len(), ptr: stream.as_ptr() as usize }; match unsafe { stream_pipeline(&desc as *const _ as *const c_void) } { - StreamPipeline::Graphics(g) => *g, + StreamPipeline::Graphics(g, _) => *g, _ => panic!("expected graphics stream pipeline"), } };