From b149d2631bb3dc1ad8e124d65821341f3a9f8531 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:17:03 -0500 Subject: [PATCH] D3D12.X: clamp single-adapter NodeMask on CreateRootSignature The test no longer requires the unhooked CreateRootSignature to refuse NodeMask 2: Microsoft's D3D12 refuses it, vkd3d-proton takes it, and then only the hooked call is checked. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/real.rs | 95 ++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index eafeccd069..9f16ab9343 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -10854,6 +10854,7 @@ unsafe extern "system" fn dev_create_root_signature(this: *mut c_void, mask: u32 crate::gpu::shader::capture("rootsig", blob as usize, len); type F = unsafe extern "system" fn(*mut c_void, u32, *const c_void, usize, *const GUID, *mut *mut c_void) -> HRESULT; let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "CreateRootSignature"))); + let pc_mask = if mask > 1 { 1 } else { mask }; let sanitized = if !blob.is_null() { super::console_list::pc_root_signature_blob(std::slice::from_raw_parts(blob as *const u8, len)) } else { @@ -10863,10 +10864,10 @@ unsafe extern "system" fn dev_create_root_signature(this: *mut c_void, mask: u32 Some(b) => (b.as_ptr() as *const c_void, b.len()), None => (blob, len), }; - let mut hr = f(this, mask, pc_blob, pc_len, riid, ppv); + let mut hr = f(this, pc_mask, pc_blob, pc_len, riid, ppv); if hr == E_NOINTERFACE { let iid = pc_iid(Kind::RootSignature); - hr = f(this, mask, pc_blob, pc_len, &iid, ppv); + hr = f(this, pc_mask, pc_blob, pc_len, &iid, ppv); } if hr < 0 && !super::title_textures::is_own_work() { // console root signature rejected → empty one so the title keeps going @@ -10875,7 +10876,7 @@ unsafe extern "system" fn dev_create_root_signature(this: *mut c_void, mask: u32 if D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut ser, None).is_ok() { if let Some(b) = ser { let iid = pc_iid(Kind::RootSignature); - let hr2 = f(this, mask, b.GetBufferPointer(), b.GetBufferSize(), &iid, ppv); + let hr2 = f(this, pc_mask, b.GetBufferPointer(), b.GetBufferSize(), &iid, ppv); if hr2 >= 0 { let n = FALLBACK_RS.fetch_add(1, Ordering::Relaxed); if n < 5 { @@ -10989,6 +10990,94 @@ mod root_signature_tests { assert_eq!((layout.size, layout.inline, layout.inline_gfx, layout.inline_ms), (16, 0, 0, 0)); } } + + #[test] + fn dev_create_root_signature_sanitizes_node_mask() { + let mut dev: Option = None; + unsafe { + let Ok(warp) = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()) else { + return crate::gpu::recomp::runner::skip_test("no WARP adapter"); + }; + if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + return crate::gpu::recomp::runner::skip_test("no WARP device"); + } + let Some(dev) = dev else { return crate::gpu::recomp::runner::skip_test("no WARP device") }; + adopt(dev.as_raw(), Kind::Device); + + let param = D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 4 }, + }, + }; + let desc = D3D12_ROOT_SIGNATURE_DESC { + NumParameters: 1, + pParameters: ¶m, + NumStaticSamplers: 0, + pStaticSamplers: std::ptr::null(), + Flags: D3D12_ROOT_SIGNATURE_FLAG_NONE, + }; + let mut ser = None; + D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut ser, None).expect("serialize root sig"); + let b = ser.expect("blob"); + let riid = &ID3D12RootSignature::IID as *const windows::core::GUID as *const GUID; + + // Microsoft's D3D12 rejects NodeMask > 1 on a single-adapter device unhooked; one that + // takes it (vkd3d-proton) leaves only the hooked call to check. + type CreateRsFn = unsafe extern "system" fn(*mut c_void, u32, *const c_void, usize, *const GUID, *mut *mut c_void) -> HRESULT; + let raw_create_rs: CreateRsFn = std::mem::transmute(orig_fn(dev.as_raw(), slot_of(dev.as_raw(), "CreateRootSignature"))); + let mut raw_out = std::ptr::null_mut(); + let raw_hr = raw_create_rs(dev.as_raw(), 2, b.GetBufferPointer(), b.GetBufferSize(), riid, &mut raw_out); + if raw_hr >= 0 { + if !raw_out.is_null() { + drop(windows::core::IUnknown::from_raw(raw_out)); + } + println!("this D3D12 takes NodeMask 2 unhooked: only the hooked call is checked"); + } else { + assert!(raw_out.is_null(), "a refused CreateRootSignature returned an object"); + } + + // Valid blob with NodeMask > 1 must create the real root signature (preserving parameter 0). + let mut out = std::ptr::null_mut(); + let hr = dev_create_root_signature(dev.as_raw(), 2, b.GetBufferPointer(), b.GetBufferSize(), riid, &mut out); + assert!(hr >= 0 && !out.is_null(), "dev_create_root_signature with NodeMask 2 failed: {hr:#x}"); + let rs = ID3D12RootSignature::from_raw(out); + + let args = [ + D3D12_INDIRECT_ARGUMENT_DESC { + Type: D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, + Anonymous: D3D12_INDIRECT_ARGUMENT_DESC_0 { + Constant: D3D12_INDIRECT_ARGUMENT_DESC_0_1 { + RootParameterIndex: 0, + DestOffsetIn32BitValues: 0, + Num32BitValuesToSet: 4, + }, + }, + }, + D3D12_INDIRECT_ARGUMENT_DESC { + Type: D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, + ..Default::default() + }, + ]; + let cs_desc = D3D12_COMMAND_SIGNATURE_DESC { + ByteStride: 28, + NumArgumentDescs: args.len() as u32, + pArgumentDescs: args.as_ptr(), + NodeMask: 0, + }; + let mut cmd_sig: Option = None; + dev.CreateCommandSignature(&cs_desc, &rs, &mut cmd_sig) + .expect("real root signature must keep parameter 0 for CreateCommandSignature"); + + // Invalid blob with NodeMask > 1 must also succeed via the empty fallback. + let junk = [0u8; 32]; + let mut fb = std::ptr::null_mut(); + let hr_fb = dev_create_root_signature(dev.as_raw(), 0xff, junk.as_ptr() as *const c_void, junk.len(), riid, &mut fb); + assert!(hr_fb >= 0 && !fb.is_null(), "fallback root signature with NodeMask 0xff failed: {hr_fb:#x}"); + drop(ID3D12RootSignature::from_raw(fb)); + } + } } // ---------------------------------------------------------------------------------------