diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 2af03a02d5..c83beeffbc 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -686,6 +686,18 @@ pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] { d } +/// The PC initial state of a resource the console creates in `state` with resource flags +/// `flags`: the PC's state bits only (console-only ones made the PC refuse it, E_INVALIDARG), +/// and a depth buffer created for unordered access – no longer one on the PC +/// (`pc_resource_desc`), which refuses UNORDERED_ACCESS without the flag – written as depth. +pub fn pc_initial_state(flags: u32, state: u32) -> u32 { + let state = state & real::PC_RESOURCE_STATES; + if flags & 2 != 0 && state & 0x8 != 0 { + return (state & !0x8) | 0x10; + } + state +} + /// The PC format of a view, pipeline or clear value in the console's D16_UNORM_S8_UINT family: /// D24_UNORM_S8_UINT, R24_UNORM_X8_TYPELESS, X24_TYPELESS_G8_UINT (`pc_resource_desc` makes such /// resources R24G8_TYPELESS). @@ -866,11 +878,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, note_alias(va as usize, &x); // console-only state bits (Unicorn Overlord's 4K display target: 0x100000) are not the // PC's: the PC refused the resource (E_INVALIDARG) and the title threw - let mut state = state & real::PC_RESOURCE_STATES; - // (a depth buffer placed for unordered access – no longer one on the PC: written as depth) - if u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()) & 2 != 0 && state & 0x8 != 0 { - state = (state & !0x8) | 0x10; - } + let state = pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state); let mut clear_buf = [0u8; 24]; let clear = pc_clear(clear, &mut clear_buf); if buffer { @@ -1042,6 +1050,61 @@ mod tests { assert_eq!(u32::from_le_bytes(pc_resource_desc(&x)[0x2C..0x30].try_into().unwrap()), 0); } + /// A depth buffer the console also lets shaders write loses ALLOW_UNORDERED_ACCESS on the PC + /// (`pc_resource_desc`), so it cannot start in UNORDERED_ACCESS there: it starts as depth. + #[test] + fn depth_buffers_created_for_unordered_access_start_as_depth() { + assert_eq!(pc_initial_state(0x6, 0x8), 0x10); // DEPTH_STENCIL | UNORDERED_ACCESS + assert_eq!(pc_initial_state(0x8030_0006, 0x8), 0x10); // with console flag bits + assert_eq!(pc_initial_state(0x2, 0x10_0008), 0x10); // console state bits dropped + assert_eq!(pc_initial_state(0x4, 0x8), 0x8); // not a depth buffer: a UAV stays one + assert_eq!(pc_initial_state(0x2, 0x20), 0x20); // depth read: as it is + assert_eq!(pc_initial_state(0x2, 0x10_0000), 0); + assert_eq!(pc_initial_state(0, 0x10_0004), 0x4); + } + + /// 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). + #[test] + fn gpu_depth_buffer_created_for_unordered_access_is_placed() { + unsafe { + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let mut x = [0u8; 0x38]; + x[0..4].copy_from_slice(&3u32.to_le_bytes()); // texture 2D + x[0x10..0x18].copy_from_slice(&256u64.to_le_bytes()); + x[0x18..0x1C].copy_from_slice(&256u32.to_le_bytes()); + x[0x1C..0x1E].copy_from_slice(&1u16.to_le_bytes()); + x[0x1E..0x20].copy_from_slice(&1u16.to_le_bytes()); + x[0x20..0x24].copy_from_slice(&55u32.to_le_bytes()); // D16_UNORM + x[0x24..0x28].copy_from_slice(&1u32.to_le_bytes()); + x[0x30..0x34].copy_from_slice(&6u32.to_le_bytes()); // DEPTH_STENCIL | UNORDERED_ACCESS + let d: D3D12_RESOURCE_DESC = std::ptr::read_unaligned(pc_resource_desc(&x).as_ptr() as *const _); + let info = dev.GetResourceAllocationInfo(0, &[d]); + assert_ne!(info.SizeInBytes, u64::MAX); + let heap_desc = D3D12_HEAP_DESC { + SizeInBytes: info.SizeInBytes.next_multiple_of(info.Alignment), + Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + Alignment: info.Alignment, + Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES, + }; + let mut heap: Option = None; + dev.CreateHeap(&heap_desc, &mut heap).expect("heap"); + let heap = heap.unwrap(); + let place = |state: u32| { + let mut r: Option = None; + dev.CreatePlacedResource(&heap, 0, &d, D3D12_RESOURCE_STATES(state as i32), None, &mut r).map(|_| r) + }; + assert!(place(0x8 & real::PC_RESOURCE_STATES).is_err(), "UNORDERED_ACCESS without the UAV flag"); + let state = pc_initial_state(6, 0x8); + assert_eq!(state, D3D12_RESOURCE_STATE_DEPTH_WRITE.0 as u32); + let r = place(state).unwrap_or_else(|e| panic!("placed in {state:#x}: {e}")); + assert!(r.is_some()); + } + } + /// Console depth buffers become PC ones a shader can view: typeless, the D16S8 family as /// D24S8, no UAV flag; views get the family's readable formats (stencil from plane 1). #[test] diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index f1ea87c8ba..3f490d34c4 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2344,8 +2344,10 @@ unsafe extern "system" fn dev_create_committed_resource_x(this: *mut c_void, pro let desc = pc_full.as_ref().map_or(desc, |d| d.as_ptr() as usize); let mut clear_buf = [0u8; 24]; let clear = super::device_x::pc_clear(clear as *const c_void, &mut clear_buf) as usize; - // console-only state bits (e.g. 0x100000 on a display target) are not the PC's - let hr = dev_create_committed_resource(this, props, flags, desc, state & PC_RESOURCE_STATES as usize, clear, riid, ppv); + // console-only state bits (e.g. 0x100000 on a display target) are not the PC's; a depth + // buffer the PC description took ALLOW_UNORDERED_ACCESS from does not start as a UAV + let st = pc_full.as_ref().map_or(state as u32 & PC_RESOURCE_STATES, |d| super::device_x::pc_initial_state(u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()), state as u32)); + let hr = dev_create_committed_resource(this, props, flags, desc, st as usize, clear, riid, ppv); if hr < 0 { let pw = |o: usize| if props == 0 { 0 } else { *((props + o) as *const u32) }; crate::xlog_warn!( @@ -2738,7 +2740,7 @@ pub(super) unsafe extern "system" fn dev_create_placed_resource_x(this: *mut c_v } let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned(); let d = super::device_x::pc_resource_desc(&x); - let st = state as u32 & PC_RESOURCE_STATES; + let st = super::device_x::pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state as u32); let hr2 = dev_create_placed_resource(this, heap, offset, d.as_ptr() as usize, st as usize, clear, riid, ppv); let heap_desc = pc_heap_desc(heap); let pc_size = pc_allocation_size(this, &d); @@ -2792,7 +2794,7 @@ unsafe fn own_memory_if_larger_on_the_pc(this: *mut c_void, heap: usize, offset: return None; } let h = pc_heap_desc(heap)?; - let st = state as u32 & PC_RESOURCE_STATES; + let st = super::device_x::pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state as u32); let mut hr = dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear, riid, ppv); if hr < 0 && st != 0 { // as in the past-the-end fallback: a state the PC refuses for this desc (e.g. UAV