From d61ffdc435d5b7c71f4635e4fb06b28eed1328a3 Mon Sep 17 00:00:00 2001 From: Bloxive Date: Wed, 25 Nov 2026 15:18:33 -0400 Subject: [PATCH] D3D12.X: canonicalize inactive two-pass stream descriptors Zero-count stream output, input layouts, and view instancing can carry inert pointer values and padding. Treat them and empty optional shader bytecode as omitted so the stream remains eligible for a stable two-pass cache key. Preserve rejection of unsupported active state. Assisted-by: Codex (GPT-6) --- .../src/hle/d3d12x/real/two_pass_identity.rs | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) 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 6ca0572281..c1e7efddc3 100644 --- a/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs +++ b/crates/xcl/src/hle/d3d12x/real/two_pass_identity.rs @@ -200,12 +200,31 @@ pub(super) unsafe fn two_pass_stream_key(stream: &[u8], ps_samplers: &[crate::gp for (ty, payload) in subs { match ty { 0 | 19 => {} // RS replaced by ABI RS via ps_samplers; CACHED_PSO is the lookup input - 7 | 12 | 22 => { - if payload.iter().any(|&b| b != 0) { + // These descriptors can carry inert pointers and padding in a title stream. + // Their counts and flags determine whether any state needs to be hashed. + 7 => { + let state = read_state::(payload)?; + if state.NumEntries != 0 || state.NumStrides != 0 || state.RasterizedStream != 0 { + return None; + } + } + 12 => { + let state = read_state::(payload)?; + if state.NumElements != 0 { + return None; + } + } + 22 => { + let state = read_state::(payload)?; + if state.ViewInstanceCount != 0 || state.Flags.0 != 0 { return None; } } 1..=6 | 24 | 25 => { + let state = read_state::(payload)?; + if state.BytecodeLength == 0 && ty != 25 { + continue; + } let code = payload_shader(payload)?; shader_bytes = shader_bytes.checked_add(code.len())?; h.u32(ty); @@ -333,6 +352,49 @@ mod tests { } } + #[test] + fn empty_stream_descriptors_do_not_change_the_key() { + let ms = [1u8, 2, 3, 4]; + let mut base = StreamOut::default(); + base.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, &ms); + let mut with_empty = StreamOut(base.0.clone(), base.1); + with_empty.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT, &D3D12_STREAM_OUTPUT_DESC { + pSODeclaration: 0x2000 as *const _, + pBufferStrides: 0x3000 as *const _, + ..Default::default() + }); + let mut input = [0u8; std::mem::size_of::()]; + input[..8].copy_from_slice(&0x4000u64.to_le_bytes()); + let padding = std::mem::offset_of!(D3D12_INPUT_LAYOUT_DESC, NumElements) + std::mem::size_of::(); + input[padding..].fill(0xaa); + with_empty.raw(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT.0 as u32, &input, true); + with_empty.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING, &D3D12_VIEW_INSTANCING_DESC { + pViewInstanceLocations: 0x5000 as *const _, + ..Default::default() + }); + let empty_shader = D3D12_SHADER_BYTECODE { pShaderBytecode: 0x6000 as *const _, BytecodeLength: 0 }; + for stage in [ + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_HS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS, + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS, + ] { + with_empty.put(stage, &empty_shader); + } + + unsafe { + let key = two_pass_stream_key(&base.0, &[], 1, 4).expect("base mesh key"); + assert_eq!(two_pass_stream_key(&with_empty.0, &[], 1, 4), Some(key)); + + let mut empty_ms = StreamOut::default(); + empty_ms.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, &empty_shader); + assert_eq!(two_pass_stream_key(&empty_ms.0, &[], 1, 4), None); + } + } + #[test] fn two_pass_keys_cover_mem_windows_invalid_counts_and_duplicates() { let mut s = StreamOut::default();