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
107 changes: 106 additions & 1 deletion crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,30 @@ pub(super) unsafe extern "system" fn get_resource_allocation_info2_x(a: *mut c_v
ret
}

/// Sanitize `GetCustomHeapProperties(nodeMask, heapType)` arguments for single-adapter PC D3D12:
/// nonzero `nodeMask` values clamp to `1`, and console-only heap types above `GPU_UPLOAD` (`5`)
/// fall back to `DEFAULT` (matching `dev_create_heap`), while invalid PC inputs (`0`, `4`) stay
/// untouched so the runtime still rejects them.
fn pc_custom_heap_query(mask: u32, ty: u32) -> (u32, u32) {
let pc_mask = if mask == 0 { 0 } else { 1 };
let pc_ty = if ty > D3D12_HEAP_TYPE_GPU_UPLOAD.0 as u32 {

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.

Two heap types still return zeroed props, which is what this PR sets out to fix. GPU_UPLOAD (5) goes to PC unchanged, so on adapters or runtimes without GPU upload heap support (ReBAR off, older runtimes, possibly WARP) GetCustomHeapProperties(0, 5) still gives {Type=0, UNKNOWN, UNKNOWN}, and the next CreateCommittedResource fails with E_INVALIDARG. UNKNOWN (0) is also left as is, although the doc says the mapping matches dev_create_heap, which maps anything outside 1..=5 (0 included) to DEFAULT. The new test checks neither type.

D3D12_HEAP_TYPE_DEFAULT.0 as u32

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: console-only heap types (> 5) are mapped to DEFAULT, so the title gets CUSTOM + CPU_PAGE_PROPERTY_NOT_AVAILABLE back even when that console type is CPU-visible on the console's UMA memory. A title that queries a type it uses for CPU-written data (e.g. 6), creates a CUSTOM heap or resource from the result and calls Map() gets E_INVALIDARG with a null pointer and dereferences it. Before, CreateHeap's own fallback handled the zeroed props instead.

} else {
ty
};
(pc_mask, pc_ty)
}

/// device #26 GetCustomHeapProperties(node mask, heap type) -> D3D12_HEAP_PROPERTIES (20 bytes),
/// returned through a hidden pointer - like #25 called the C way (a retail game: return buffer, device,
/// ...) or the C++ way.
pub(super) unsafe extern "system" fn get_custom_heap_properties_x(a: *mut c_void, b: *mut c_void, mask: u32, ty: u32) -> *mut [u32; 5] {
let (this, ret) = if real::this_first(a, b) { (a, b as *mut [u32; 5]) } else { (b, a as *mut [u32; 5]) };
type F = unsafe extern "system" fn(*mut c_void, *mut [u32; 5], u32, u32) -> *mut [u32; 5];
let pc: F = std::mem::transmute(real::orig_fn(this, 26));
let (pc_mask, pc_ty) = pc_custom_heap_query(mask, ty);
let mut r = [0u32; 5];
pc(this, &mut r, mask, ty);
pc(this, &mut r, pc_mask, pc_ty);

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: this changes what the retail title gets (CUSTOM props where it used to get zeroed ones), and the PR's own smoke run regressed without explanation: 1583 vs 1695 frames (-6.6%) and 33990 vs 31546 MB peak commit (+2.4 GB). A plausible cause: before, zeroed props made CreateHeap fall back to a plain DEFAULT heap in dev_create_heap. Now READBACK with mask > 1 comes back as CUSTOM + WRITE_BACK, which is_cpu_readback sends to coherent RAM, and CUSTOM heaps get HEAP_UMA sparse-VRAM backing. It's worth checking before merge.

if !ret.is_null() {
*ret = r;
}
Expand Down Expand Up @@ -1572,6 +1587,96 @@ mod tests {
assert_eq!(console_bytes(&sampled), Some(crate::hle::xg::Texture::new(&sampled).unwrap().size()));
}

#[test]
fn pc_custom_heap_query_sanitizes_node_mask_and_console_heap_type() {
assert_eq!(pc_custom_heap_query(0, 1), (0, 1));
assert_eq!(pc_custom_heap_query(1, 2), (1, 2));
assert_eq!(pc_custom_heap_query(0x3, 1), (1, 1));
assert_eq!(pc_custom_heap_query(u32::MAX, 3), (1, 3));
assert_eq!(pc_custom_heap_query(0, 0), (0, 0));
assert_eq!(pc_custom_heap_query(0, 4), (0, 4));
assert_eq!(pc_custom_heap_query(0, 5), (0, 5));
assert_eq!(pc_custom_heap_query(0x3, 6), (1, 1));
assert_eq!(pc_custom_heap_query(0, 0x10), (0, 1));
}

#[test]
fn get_custom_heap_properties_x_sanitizes_node_mask_and_console_heap_type() {
const CHILD_ENV: &str = "XCL_TEST_CUSTOM_HEAP_PROPS_CHILD";
if std::env::var_os(CHILD_ENV).is_none() {
let exe = std::env::current_exe().expect("test binary path");
let out = std::process::Command::new(exe)
.env(CHILD_ENV, "1")
.args([
"--exact",
"hle::d3d12x::device_x::tests::get_custom_heap_properties_x_sanitizes_node_mask_and_console_heap_type",
"--nocapture",
])
.output()
.expect("spawn child test");
assert!(
out.status.success(),
"child test failed ({:?}):\nstdout:\n{}\nstderr:\n{}",
out.status,
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
// (the child's skip, if it skipped)
for line in String::from_utf8_lossy(&out.stdout).lines().filter(|l| l.starts_with("SKIPPED")) {
println!("{line}");
}
return;
}

use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory4};

let Ok(factory) = (unsafe { CreateDXGIFactory1::<IDXGIFactory4>() }) else {
return crate::gpu::recomp::runner::skip_test("no DXGI factory");
};
let Ok(warp) = (unsafe { factory.EnumWarpAdapter::<windows::Win32::Graphics::Dxgi::IDXGIAdapter>() }) else {
return crate::gpu::recomp::runner::skip_test("no WARP");
};
let Ok(dev) = crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_11_0) else {
return crate::gpu::recomp::runner::skip_test("no WARP device");
};

unsafe {
#[repr(C, align(8))]
struct Aligned([u32; 5]);
real::adopt(dev.as_raw(), real::Kind::Device);

// Reference PC answers for DEFAULT (1) and UPLOAD (2) on node 0.
let mut ref_default = Aligned([0u32; 5]);
get_custom_heap_properties_x(dev.as_raw(), &mut ref_default.0 as *mut _ as *mut c_void, 0, 1);
assert_eq!(ref_default.0[0], D3D12_HEAP_TYPE_CUSTOM.0 as u32);

let mut ref_upload = Aligned([0u32; 5]);
get_custom_heap_properties_x(dev.as_raw(), &mut ref_upload.0 as *mut _ as *mut c_void, 0, 2);
assert_eq!(ref_upload.0[0], D3D12_HEAP_TYPE_CUSTOM.0 as u32);

// Multi-bit NodeMask (0x3) with DEFAULT (C++ argument order) must match single-adapter DEFAULT.
let mut out_mask3 = Aligned([0u32; 5]);
get_custom_heap_properties_x(dev.as_raw(), &mut out_mask3.0 as *mut _ as *mut c_void, 3, 1);
assert_eq!(out_mask3.0, ref_default.0, "mask=3 DEFAULT must clamp to single-adapter DEFAULT");

// All-bits NodeMask (u32::MAX) with UPLOAD in C argument order (ret, this, ...) must match UPLOAD.
let mut out_max_c = Aligned([0u32; 5]);
get_custom_heap_properties_x(&mut out_max_c.0 as *mut _ as *mut c_void, dev.as_raw(), u32::MAX, 2);
assert_eq!(out_max_c.0, ref_upload.0, "C-order mask=MAX UPLOAD must clamp to single-adapter UPLOAD");

// Console-only heap type (6) falls back to DEFAULT properties.
let mut out_console = Aligned([0u32; 5]);
get_custom_heap_properties_x(dev.as_raw(), &mut out_console.0 as *mut _ as *mut c_void, 0, 6);
assert_eq!(out_console.0, ref_default.0, "console heap type 6 must map to DEFAULT");

// Invalid PC input CUSTOM (4) must remain rejected (Type == UNKNOWN = 0).
let mut out_custom = Aligned([0xAAu32; 5]);
get_custom_heap_properties_x(dev.as_raw(), &mut out_custom.0 as *mut _ as *mut c_void, 0, 4);
assert_eq!(out_custom.0[0], 0, "CUSTOM (4) input must stay rejected");
}
}

/// GPU: a 256x256 D16 depth buffer the console creates for unordered access too, in
/// UNORDERED_ACCESS, is placed on the PC as its description and initial state say; the state
/// with only the console's bits dropped is refused (the depth buffer has no UAV flag there).
Expand Down
Loading