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
240 changes: 237 additions & 3 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3805,13 +3805,48 @@ unsafe extern "system" fn dev_create_committed_resource_layout_x(
dev_create_committed_resource_x(this, props, flags as usize, desc, state, clear, riid, ppv)
}

/// `D3D12_HEAP_FLAGS` valid on PC `CreateCommittedResource`: known PC heap flags minus
/// `ALLOW_DISPLAY` (`0x8`) and tier-1 `DENY_*` flags (`0x4`, `0x40`, `0x80`), which PC
/// `CreateCommittedResource` rejects with `E_INVALIDARG`.
const PC_COMMITTED_HEAP_FLAGS: u32 = PC_HEAP_FLAGS
& !(D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as u32
| D3D12_HEAP_FLAG_DENY_BUFFERS.0 as u32
| D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES.0 as u32
| D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES.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.

PC_COMMITTED_HEAP_FLAGS keeps ALLOW_WRITE_WATCH (0x200), HARDWARE_PROTECTED (0x100) and ALLOW_SHADER_ATOMICS, which are invalid on plain CreateCommittedResource once the type has been rewritten to DEFAULT, or without a protected session. Console flags 0x2C2 with console heap type 6 become DEFAULT + 0x200, which PC D3D12 rejects with E_INVALIDARG, so the cleanup only half-fixes the failure it targets. The failure log (~3934) also still prints the console's raw flags and type, not the cleaned-up values actually rejected.


fn pc_committed_heap_flags(flags: u32) -> u32 {
flags & PC_COMMITTED_HEAP_FLAGS
}

/// Sanitize console-only `D3D12_HEAP_TYPE` values (outside `1..=5`, matching `dev_create_heap`)
/// to `D3D12_HEAP_TYPE_DEFAULT` for PC `CreateCommittedResource`.
unsafe fn pc_heap_properties(props: usize, buf: &mut D3D12_HEAP_PROPERTIES) -> usize {
if props == 0 || !crate::gpu::shader::readable(props, std::mem::size_of::<D3D12_HEAP_PROPERTIES>()) {
return props;
}
let p: D3D12_HEAP_PROPERTIES = std::ptr::read_unaligned(props as *const D3D12_HEAP_PROPERTIES);
if !(1..=5).contains(&p.Type.0) {

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.

pc_heap_properties replaces any console-only heap type (outside 1..=5) with a fresh DEFAULT heap and clears CPUPageProperty, MemoryPoolPreference and both node masks. A committed buffer/texture in a CPU-visible console heap type (e.g. 6), created when UMA placement is off or full, is now GPU-only on the PC. Map() then fails or returns null, and the title's CPU writes or readback crash or are lost, where before the creation failed outright. Mapping CPU-visible console types to a CPU-visible PC heap (or a custom heap with the same CPU page property) would keep access.

*buf = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() };
buf as *const D3D12_HEAP_PROPERTIES as usize
} else {
props
}
}

/// A display target: its console memory comes from the title's allocation hook, as the console
/// runtime's does (a retail game gives its back buffers one block and asserts that it is used up); on the
/// PC it is a resource of its own.
unsafe fn create_display_target(this: *mut c_void, props: usize, desc: usize, state: usize, clear: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT {
let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned();
let d = super::device_x::pc_resource_desc(&x);
let hr = dev_create_committed_resource(this, props, 0, d.as_ptr() as usize, state, clear, riid, ppv);
let d = super::device_x::pc_resource_desc_for_device(this, &x);
let mut props_buf = D3D12_HEAP_PROPERTIES::default();
let props = pc_heap_properties(props, &mut props_buf);
let mut clear_buf = [0u8; 24];
let clear = super::device_x::pc_clear_for_desc(clear as *const c_void, u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), &mut clear_buf) as usize;
let orig_flags = u32::from_le_bytes(x[0x30..0x34].try_into().unwrap());
let pc_flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap());
let st = super::device_x::pc_initial_state_for_desc(orig_flags, pc_flags, state as u32);
let hr = dev_create_committed_resource(this, props, 0, d.as_ptr() as usize, st as usize, clear, riid, ppv);
if hr < 0 || ppv.is_null() || (*ppv).is_null() {
crate::xlog_warn!("D3D12.X: display target: {hr:#x}; desc {x:02x?}");
return hr;
Expand Down Expand Up @@ -3887,7 +3922,10 @@ unsafe extern "system" fn dev_create_committed_resource_x(this: *mut c_void, pro
let pc_flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap());
super::device_x::pc_initial_state_for_desc(orig_flags, pc_flags, state as u32)
});
let hr = dev_create_committed_resource(this, props, flags, desc, st as usize, clear, riid, ppv);
let mut props_buf = D3D12_HEAP_PROPERTIES::default();
let pc_props = pc_heap_properties(props, &mut props_buf);

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.

dev_create_committed_resource_x (slots 27/76) now strips ALLOW_DISPLAY so the call succeeds as a plain PC resource, instead of sending it to create_display_target the way dev_create_committed_resource_layout_x does. Display targets created here never get title memory from runtime_alloc(D3D_DISPLAY_TARGET) or note_title_address. A title that creates its back buffers via CreateCommittedResource with ALLOW_DISPLAY, when UMA placement doesn't take them, used to get a loud E_INVALIDARG. Now it gets S_OK with no title-side allocation, so the title's assert that its back-buffer block is used up fires, or console code reads the target at an address that was never registered. Test case 5 (~15224) asserts the plain-PC result, which locks this in.

let pc_flags = pc_committed_heap_flags(flags as u32) as usize;
let hr = dev_create_committed_resource(this, pc_props, pc_flags, desc, st as usize, clear, riid, ppv);
if hr >= 0 && pc_full.is_some() && !ppv.is_null() && !(*ppv).is_null() {
super::depth_uav::note_created(*ppv, &(console_desc as *const [u8; 0x38]).read_unaligned(), None, st);
super::lut3d::created("CreateCommittedResource (a PC resource: not in the title's memory)", *ppv, Some(&(console_desc as *const [u8; 0x38]).read_unaligned()), None, false, st);
Expand Down Expand Up @@ -15008,3 +15046,199 @@ mod heap_release_order_tests {
}
}

#[cfg(test)]
mod committed_display_target_sanitize_tests {
use super::*;
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_B8G8R8X8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_SAMPLE_DESC};

fn in_child(name: &str) -> bool {
const CHILD: &str = "XCL_TEST_COMMITTED_DISPLAY_SANITIZE_CHILD";
if std::env::var_os(CHILD).is_some() {
return true;
}
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", &format!("hle::d3d12x::real::committed_display_target_sanitize_tests::{name}"), "--nocapture"])
.env(CHILD, "1")
.env("XCL_UMA_MB", "64")
.env("XCL_COHERENT_MB", "64")
.output()
.unwrap();
assert!(
output.status.success(),
"child failed ({:?}):\nstdout:\n{}\nstderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
print!("{}", String::from_utf8_lossy(&output.stdout));
use std::io::Write;
for l in String::from_utf8_lossy(&output.stderr).lines().filter(|l| l.starts_with("SKIPPED")) {
let _ = writeln!(std::io::stderr(), "{l}");
}
false
}

fn warp_device() -> Option<ID3D12Device> {
unsafe {
let Ok(factory) = CreateDXGIFactory1::<IDXGIFactory4>() else {
crate::gpu::recomp::runner::skip_test("no WARP device");
return None;
};
let Ok(warp) = factory.EnumWarpAdapter::<IDXGIAdapter>() else {
crate::gpu::recomp::runner::skip_test("no WARP device");
return None;
};
let mut dev: Option<ID3D12Device> = None;
if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() {
crate::gpu::recomp::runner::skip_test("no WARP device");
return None;
}
dev
}
}

fn tex2d_desc(format: DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS) -> D3D12_RESOURCE_DESC {
D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Width: 64,
Height: 64,
DepthOrArraySize: 1,
MipLevels: 1,
Format: format,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Flags: flags,
..Default::default()
}
}

#[test]
fn pc_committed_heap_flags_and_properties_sanitize_console_values() {
// Strips ALLOW_DISPLAY (0x8), DENY_* (0x4, 0x40, 0x80), and console bits (0x2, 0x10, high bits)
assert_eq!(pc_committed_heap_flags(0x8000_00CE), 0);
// Preserves valid committed heap flags (SHARED, CREATE_NOT_ZEROED, ALLOW_SHADER_ATOMICS)
let valid = (D3D12_HEAP_FLAG_SHARED.0 | D3D12_HEAP_FLAG_CREATE_NOT_ZEROED.0 | D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS.0) as u32;
assert_eq!(pc_committed_heap_flags(valid | (D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as u32) | 0xC2), valid);

unsafe {
let mut buf = D3D12_HEAP_PROPERTIES::default();
let normal = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() };
let ptr = pc_heap_properties(&normal as *const _ as usize, &mut buf);
assert_eq!(ptr, &normal as *const _ as usize, "known PC heap type stays untouched");

let console = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE(6), ..Default::default() };
let ptr = pc_heap_properties(&console as *const _ as usize, &mut buf);
assert_eq!(ptr, &buf as *const _ as usize, "console heap type rewrites into buf");
assert_eq!(buf.Type, D3D12_HEAP_TYPE_DEFAULT);
}
}

#[test]
fn warp_display_target_and_committed_resource_sanitize_console_args() {
if !in_child("warp_display_target_and_committed_resource_sanitize_console_args") {
return;
}
unsafe {
let Some(dev) = warp_device() else { return };
let d = dev.as_raw();
adopt(d, Kind::Device);

let default_props = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() };

// 1. Display target with console format 116 (promoted to R16G16B16A16_FLOAT) + optimized clear value
let desc_116 = tex2d_desc(DXGI_FORMAT(116), D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
let clear_116 = D3D12_CLEAR_VALUE { Format: DXGI_FORMAT(116), Anonymous: D3D12_CLEAR_VALUE_0 { Color: [0.0, 0.0, 0.0, 1.0] } };
let mut raw = std::ptr::null_mut();
let hr = dev_create_committed_resource_layout_x(
d,
&default_props as *const _ as usize,
D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as u32,
&desc_116 as *const _ as usize,
2, // D3D12_BARRIER_LAYOUT_RENDER_TARGET
&clear_116 as *const _ as usize,
0,
0,
0,
&ID3D12Resource::IID as *const _ as *const GUID,
&mut raw,
);
assert!(hr >= 0 && !raw.is_null(), "display target format 116 with clear value failed: {hr:#x}");
let res = ID3D12Resource::from_raw(raw);
assert_eq!(res.GetDesc().Format, DXGI_FORMAT_R16G16B16A16_FLOAT);

// 2. Display target with B8G8R8X8_UNORM + UAV (promoted to B8G8R8A8_UNORM) + optimized clear value
let desc_88 = tex2d_desc(DXGI_FORMAT_B8G8R8X8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
let clear_88 = D3D12_CLEAR_VALUE { Format: DXGI_FORMAT_B8G8R8X8_UNORM, Anonymous: D3D12_CLEAR_VALUE_0 { Color: [0.0, 0.0, 0.0, 1.0] } };
let mut raw = std::ptr::null_mut();
let hr = dev_create_committed_resource_layout_x(
d,
&default_props as *const _ as usize,
D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as u32,
&desc_88 as *const _ as usize,
2, // D3D12_BARRIER_LAYOUT_RENDER_TARGET
&clear_88 as *const _ as usize,
0,
0,
0,
&ID3D12Resource::IID as *const _ as *const GUID,
&mut raw,
);
assert!(hr >= 0 && !raw.is_null(), "display target B8G8R8X8+UAV with clear value failed: {hr:#x}");
let res = ID3D12Resource::from_raw(raw);
assert_eq!(res.GetDesc().Format, DXGI_FORMAT_B8G8R8A8_UNORM);

// 3. Display target with sRGB + UAV (UAV stripped by pc_resource_desc) starting in UNORDERED_ACCESS layout (3)
let desc_srgb = tex2d_desc(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
let mut raw = std::ptr::null_mut();
let hr = dev_create_committed_resource_layout_x(
d,
&default_props as *const _ as usize,
D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as u32,
&desc_srgb as *const _ as usize,
3, // D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS
0,
0,
0,
0,
&ID3D12Resource::IID as *const _ as *const GUID,
&mut raw,
);
assert!(hr >= 0 && !raw.is_null(), "display target sRGB+UAV in UNORDERED_ACCESS layout failed: {hr:#x}");
let _res = ID3D12Resource::from_raw(raw);

// 4. Display target with console-only state bits (0x10_0000)
let desc_rt = tex2d_desc(DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
let mut raw = std::ptr::null_mut();
let hr = create_display_target(
d,
&default_props as *const _ as usize,
&desc_rt as *const _ as usize,
0x10_0000 | D3D12_RESOURCE_STATE_RENDER_TARGET.0 as usize,
0,
&ID3D12Resource::IID as *const _ as *const GUID,
&mut raw,
);
assert!(hr >= 0 && !raw.is_null(), "display target with console state bit 0x10_0000 failed: {hr:#x}");
let _res = ID3D12Resource::from_raw(raw);

// 5. dev_create_committed_resource_x with console heap flags (ALLOW_DISPLAY | 0xC2) and console heap type (6)
let console_props = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE(6), ..Default::default() };
let mut raw = std::ptr::null_mut();
let hr = dev_create_committed_resource_x(
d,
&console_props as *const _ as usize,
(D3D12_HEAP_FLAG_ALLOW_DISPLAY.0 as usize) | 0xC2,
&desc_rt as *const _ as usize,
D3D12_RESOURCE_STATE_RENDER_TARGET.0 as usize,
0,
&ID3D12Resource::IID as *const _ as *const GUID,
&mut raw,
);
assert!(hr >= 0 && !raw.is_null(), "committed resource with console heap flags/type failed: {hr:#x}");
assert!(console_buffer(raw as usize).is_none(), "must exercise PC CreateCommittedResource fallback rather than UMA placement");
let _res = ID3D12Resource::from_raw(raw);
}
}
}


Loading