Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 92 additions & 3 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one: if mask > 1 { 1 } else { mask } never checks the device's node count, so it also rewrites masks that are valid on a linked multi-node adapter. With GetNodeCount() == 2, NodeMask 2 (node 1) or 3 (both) becomes 1, so the root signature exists only on node 0, and PSOs/lists the title creates on node 1 with it fail. Masking against the real node set (mask & ((1 << GetNodeCount()) - 1), falling back when nothing is left) would only change masks the device can't satisfy. Relatedly, CreateCommandQueue/List, descriptor and query heaps, command signatures and the PSO NodeMask fields (e.g. ~9047/9104/9347, 3907, 1746) still forward the title's mask unchanged. A title passing 2 here likely passes it there too and fails one call later, so a shared pc_node_mask(dev, mask) used by every hook would cover it.

let sanitized = if !blob.is_null() {
super::console_list::pc_root_signature_blob(std::slice::from_raw_parts(blob as *const u8, len))
} else {
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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<ID3D12Device> = None;
unsafe {
let Ok(warp) = CreateDXGIFactory1::<IDXGIFactory4>().and_then(|f| f.EnumWarpAdapter::<IDXGIAdapter>()) 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: &param,
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<ID3D12CommandSignature> = 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));
}
}
}

// ---------------------------------------------------------------------------------------
Expand Down
Loading