diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index ddb9ef2a82..264a38c05b 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -689,9 +689,9 @@ fn uav_format_supported_by_caps(format: u32, support1: u32, support2: u32) -> bo (support2 & typed_rw) != 0 } -static UAV_FORMAT_SUPPORT: Mutex>> = Mutex::new(None); +static UAV_FORMAT_SUPPORT: Mutex>> = Mutex::new(None); -/// Query (and cache per device + format) whether the PC device supports `ALLOW_UNORDERED_ACCESS` +/// Query (and cache per adapter LUID + format) whether the PC device supports `ALLOW_UNORDERED_ACCESS` /// on `format`. Falls back to `texture_format_allows_uav` if the device cannot be queried. unsafe fn device_format_supports_uav(device: *mut c_void, format: u32) -> bool { if !texture_format_allows_uav(format) { @@ -700,28 +700,30 @@ unsafe fn device_format_supports_uav(device: *mut c_void, format: u32) -> bool { if is_typeless_uav_format(format) || device.is_null() { return true; } - let key = (device as usize, format); + let Some(dev) = ID3D12Device::from_raw_borrowed(&device) else { + return true; + }; + let luid = dev.GetAdapterLuid(); + let key = ((luid.LowPart, luid.HighPart), format); if let Some(&cached) = UAV_FORMAT_SUPPORT.lock().unwrap().as_ref().and_then(|m| m.get(&key)) { return cached; } - let supported = ID3D12Device::from_raw_borrowed(&device).map_or(true, |dev| { - let mut fs = D3D12_FEATURE_DATA_FORMAT_SUPPORT { - Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(format as i32), - ..Default::default() - }; - if dev - .CheckFeatureSupport( - D3D12_FEATURE_FORMAT_SUPPORT, - &mut fs as *mut _ as *mut c_void, - std::mem::size_of_val(&fs) as u32, - ) - .is_ok() - { - uav_format_supported_by_caps(format, fs.Support1.0 as u32, fs.Support2.0 as u32) - } else { - true - } - }); + let mut fs = D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(format as i32), + ..Default::default() + }; + let supported = if dev + .CheckFeatureSupport( + D3D12_FEATURE_FORMAT_SUPPORT, + &mut fs as *mut _ as *mut c_void, + std::mem::size_of_val(&fs) as u32, + ) + .is_ok() + { + uav_format_supported_by_caps(format, fs.Support1.0 as u32, fs.Support2.0 as u32) + } else { + true + }; UAV_FORMAT_SUPPORT.lock().unwrap().get_or_insert_with(HashMap::new).insert(key, supported); supported } @@ -945,6 +947,29 @@ pub(super) unsafe fn pc_clear(clear: *const c_void, buf: &mut [u8; 24]) -> *cons buf.as_ptr() as *const c_void } +/// The PC format of an optimized clear value (`clear_format`) for a resource whose PC format is +/// `pc_format`: aligns promoted `B5G6R5` (`85 -> 28`) and `B8G8R8X8` (`88 -> 87`) UAV targets +/// while preserving native `85`/`88` on render-target-only resources without UAV. +pub(super) fn pc_clear_format(pc_format: u32, clear_format: u32) -> u32 { + match (pc_format, pc_target_format(clear_format)) { + (28, 85) => 28, + (87, 88) => 87, + (_, f) => f, + } +} + +/// Like `pc_clear`, also aligning `clear.Format` with `pc_format` when `pc_resource_desc` promoted +/// a UAV-capable `B5G6R5` (`85 -> 28`) or `B8G8R8X8` (`88 -> 87`) resource. +pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf: &mut [u8; 24]) -> *const c_void { + if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) { + return clear; + } + std::ptr::copy_nonoverlapping(clear as *const u8, buf.as_mut_ptr(), 24); + let f = u32::from_le_bytes(buf[0..4].try_into().unwrap()); + buf[0..4].copy_from_slice(&pc_clear_format(pc_format, f).to_le_bytes()); + buf.as_ptr() as *const c_void +} + /// device #25 GetResourceAllocationInfo(visible mask, count, descs) → {size, alignment}, /// returned through a hidden pointer. Called the C way – (return buffer, device, …), XDefiant – /// or the C++ way – (device, return buffer, …): whichever argument is the device is `this`. @@ -1072,7 +1097,8 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, // PC's: the PC refused the resource (E_INVALIDARG) and the title threw 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); + let pc_format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()); + let clear = pc_clear_for_desc(clear, pc_format, &mut clear_buf); if buffer { if let Some(hr) = real::place_at_title_address(this, va as usize, &d, state, clear, riid, ppv) { return hr; @@ -1199,7 +1225,7 @@ pub(super) unsafe extern "system" fn get_copyable_footprints_x( *t = u64::MAX; f(this, x.as_ptr(), first, num, base, layouts, rows, row_sizes, t); if *t == u64::MAX { - let d = pc_resource_desc(&x); + let d = pc_resource_desc_for_device(this, &x); f(this, d.as_ptr(), first, num, base, layouts, rows, row_sizes, total); } } @@ -1544,4 +1570,147 @@ mod tests { assert_eq!(pc_uav_linear_format(93), 87); assert_eq!(pc_uav_linear_format(28), 28); } + + #[test] + fn pc_clear_for_desc_aligns_promoted_uav_targets_only() { + let desc_fmt = |fmt: u32, flags: u32| { + let mut x = [0u8; 0x38]; + x[0] = 3; + x[0x20..0x24].copy_from_slice(&fmt.to_le_bytes()); + x[0x24] = 1; + x[0x30..0x34].copy_from_slice(&flags.to_le_bytes()); + u32::from_le_bytes(pc_resource_desc(&x)[0x20..0x24].try_into().unwrap()) + }; + // Promoted RT+UAV resources align clear.Format; RT-only preserves native 85/88. + assert_eq!((pc_clear_format(desc_fmt(85, 5), 85), pc_clear_format(desc_fmt(88, 5), 88)), (28, 87)); + assert_eq!((pc_clear_format(desc_fmt(85, 1), 85), pc_clear_format(desc_fmt(88, 1), 88)), (85, 88)); + assert_eq!((pc_clear_format(28, 86), pc_clear_format(28, 115), pc_clear_format(26, 67), pc_clear_format(44, 118)), (28, 28, 26, 45)); + let (mut raw, mut buf) = ([0u8; 24], [0u8; 24]); + raw[0..4].copy_from_slice(&85u32.to_le_bytes()); + raw[4..8].copy_from_slice(&0.5f32.to_le_bytes()); + unsafe { + assert_eq!(pc_clear_for_desc(raw.as_ptr() as *const c_void, 28, &mut buf), buf.as_ptr() as *const c_void); + assert_eq!((u32::from_le_bytes(buf[0..4].try_into().unwrap()), &buf[4..24]), (28, &raw[4..24])); + assert!(pc_clear_for_desc(std::ptr::null(), 28, &mut buf).is_null()); + } + } + + #[test] + fn gpu_promoted_uav_clear_values_pass_create_committed_resource() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + let dev = unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let Ok(f) = CreateDXGIFactory1::() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + let Ok(w) = f.EnumWarpAdapter::() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + if D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); } + } + dev.unwrap() + }; + let heap = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + for (orig_fmt, promoted_fmt) in [(85u32, 28u32), (88u32, 87u32)] { + let mut x = [0u8; 0x38]; + x[0] = 3; + x[0x10..0x18].copy_from_slice(&64u64.to_le_bytes()); + x[0x18..0x1C].copy_from_slice(&64u32.to_le_bytes()); + x[0x1C] = 1; + x[0x1E] = 1; + x[0x20..0x24].copy_from_slice(&orig_fmt.to_le_bytes()); + x[0x24] = 1; + x[0x30] = 5; // RT | UAV + unsafe { + let d_bytes = pc_resource_desc_for_device(dev.as_raw(), &x); + let pc_fmt = u32::from_le_bytes(d_bytes[0x20..0x24].try_into().unwrap()); + assert_eq!(pc_fmt, promoted_fmt); + let d: D3D12_RESOURCE_DESC = std::ptr::read_unaligned(d_bytes.as_ptr() as *const _); + let clear = D3D12_CLEAR_VALUE { + Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(orig_fmt as i32), + Anonymous: D3D12_CLEAR_VALUE_0 { Color: [0.0, 0.0, 0.0, 1.0] }, + }; + let (mut old_buf, mut new_buf) = ([0u8; 24], [0u8; 24]); + let old_cv: D3D12_CLEAR_VALUE = std::ptr::read_unaligned(pc_clear(&clear as *const _ as *const c_void, &mut old_buf) as *const _); + let new_cv: D3D12_CLEAR_VALUE = std::ptr::read_unaligned(pc_clear_for_desc(&clear as *const _ as *const c_void, pc_fmt, &mut new_buf) as *const _); + let mut r: Option = None; + assert!(dev.CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COMMON, Some(&old_cv), &mut r).is_err()); + assert_eq!(new_cv.Format.0 as u32, promoted_fmt); + dev.CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COMMON, Some(&new_cv), &mut r) + .unwrap_or_else(|e| panic!("promoted {orig_fmt}->{promoted_fmt} CreateCommittedResource failed: {e}")); + assert!(r.is_some()); + } + } + } + + #[test] + fn device_format_supports_uav_keys_cache_by_adapter_luid() { + use windows::Win32::Foundation::LUID; + #[repr(C)] + struct FakeDev { vt: *const usize, luid: LUID, support_uav: bool, checks: u32 } + unsafe extern "system" fn check_feature(this: *mut c_void, feature: D3D12_FEATURE, data: *mut c_void, size: u32) -> HRESULT { + let d = &mut *(this as *mut FakeDev); + d.checks += 1; + if feature == D3D12_FEATURE_FORMAT_SUPPORT && size as usize >= std::mem::size_of::() { + let fs = &mut *(data as *mut D3D12_FEATURE_DATA_FORMAT_SUPPORT); + fs.Support1 = if d.support_uav { D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW } else { D3D12_FORMAT_SUPPORT1(0) }; + fs.Support2 = if d.support_uav { D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD | D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE } else { D3D12_FORMAT_SUPPORT2(0) }; + } + S_OK + } + unsafe extern "system" fn adapter_luid(this: *mut c_void, out: *mut LUID) { *out = (*(this as *const FakeDev)).luid; } + let mut vt = [0usize; 44]; + (vt[13], vt[43]) = (check_feature as usize, adapter_luid as usize); + let luid_a = LUID { LowPart: 0xD120_0001, HighPart: 0x5A17_0001 }; + let luid_b = LUID { LowPart: 0xD120_0002, HighPart: 0x5A17_0001 }; + let mut dev = FakeDev { vt: vt.as_ptr(), luid: luid_a, support_uav: false, checks: 0 }; + let ptr = &mut dev as *mut FakeDev as *mut c_void; + unsafe { + assert!(!device_format_supports_uav(ptr, 24) && dev.checks == 1); + // Reuse exact same pointer for a different adapter LUID (pointer ABA). + (dev.luid, dev.support_uav) = (luid_b, true); + assert!(device_format_supports_uav(ptr, 24) && dev.checks == 2); + assert!(device_format_supports_uav(ptr, 24) && dev.checks == 2); + } + if let Some(m) = UAV_FORMAT_SUPPORT.lock().unwrap().as_mut() { + m.remove(&((luid_a.LowPart, luid_a.HighPart), 24)); + m.remove(&((luid_b.LowPart, luid_b.HighPart), 24)); + } + } + + #[test] + fn get_copyable_footprints_x_strips_device_unsupported_uav_on_retry() { + use windows::Win32::Foundation::LUID; + #[repr(C)] + struct FakeAdoptedDev { vt: *const usize, luid: LUID, calls: u32, last_flags: u32 } + unsafe extern "system" fn check_feature(_this: *mut c_void, feature: D3D12_FEATURE, data: *mut c_void, size: u32) -> HRESULT { + if feature == D3D12_FEATURE_FORMAT_SUPPORT && size as usize >= std::mem::size_of::() { + let fs = &mut *(data as *mut D3D12_FEATURE_DATA_FORMAT_SUPPORT); + (fs.Support1, fs.Support2) = (D3D12_FORMAT_SUPPORT1(0), D3D12_FORMAT_SUPPORT2(0)); + } + S_OK + } + unsafe extern "system" fn adapter_luid(this: *mut c_void, out: *mut LUID) { *out = (*(this as *const FakeAdoptedDev)).luid; } + unsafe extern "system" fn footprints(this: *mut c_void, desc: *const u8, _: u32, _: u32, _: u64, _: *mut c_void, _: *mut u32, _: *mut u64, total: *mut u64) { + let d = &mut *(this as *mut FakeAdoptedDev); + d.calls += 1; + d.last_flags = u32::from_le_bytes(*(desc.add(0x30) as *const [u8; 4])); + if !total.is_null() { *total = if d.last_flags & 4 != 0 { u64::MAX } else { 65536 }; } + } + let mut orig_vt = [0usize; 44]; + (orig_vt[13], orig_vt[38], orig_vt[43]) = (check_feature as usize, footprints as usize, adapter_luid as usize); + let mut ext_buf = [0usize; 45]; + ext_buf[0] = orig_vt.as_ptr() as usize; + ext_buf[1..].copy_from_slice(&orig_vt); + let luid = LUID { LowPart: 0xD120_0003, HighPart: 0x5A17_0001 }; + let mut dev = FakeAdoptedDev { vt: unsafe { ext_buf.as_ptr().add(1) }, luid, calls: 0, last_flags: u32::MAX }; + let mut x = [0u8; 0x38]; + (x[0], x[0x10], x[0x18], x[0x1C], x[0x1E], x[0x20], x[0x24], x[0x30]) = (3, 64, 64, 1, 1, 24, 1, 4); + let mut total = 0u64; + unsafe { + get_copyable_footprints_x(&mut dev as *mut FakeAdoptedDev as *mut c_void, x.as_ptr(), 0, 1, 0, std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut(), &mut total); + } + assert_eq!((dev.calls, dev.last_flags & 4, total), (2, 0, 65536)); + if let Some(m) = UAV_FORMAT_SUPPORT.lock().unwrap().as_mut() { + m.remove(&((luid.LowPart, luid.HighPart), 24)); + } + } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 6cf818107b..43089a96c2 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2797,7 +2797,10 @@ unsafe extern "system" fn dev_create_committed_resource_x(this: *mut c_void, pro .then(|| super::device_x::pc_resource_desc_for_device(this, &(console_desc as *const [u8; 0x38]).read_unaligned())); 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; + let clear = match pc_full.as_ref() { + Some(d) => 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), + None => 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; a resource // 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| { @@ -3203,7 +3206,9 @@ 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 = 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 mut clear_buf2 = [0u8; 24]; + let clear_d = super::device_x::pc_clear_for_desc(clear as *const c_void, u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), &mut clear_buf2) as usize; + let hr2 = dev_create_placed_resource(this, heap, offset, d.as_ptr() as usize, st as usize, clear_d, riid, ppv); let heap_desc = pc_heap_desc(heap); let pc_size = pc_allocation_size(this, &d); // A texture the title placed by the console's size (`get_resource_allocation_info_x` @@ -3212,7 +3217,7 @@ pub(super) unsafe extern "system" fn dev_create_placed_resource_x(this: *mut c_v // heap's kind; the console's aliasing of that one placement is lost. let is_texture = u32::from_le_bytes(x[0..4].try_into().unwrap()) != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32; let hr3 = match heap_desc { - Some(h) if hr2 < 0 && is_texture => Some(dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear, riid, ppv)), + Some(h) if hr2 < 0 && is_texture => Some(dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear_d, riid, ppv)), _ => None, }; static N: AtomicU64 = AtomicU64::new(0); @@ -3257,6 +3262,8 @@ unsafe fn own_memory_if_larger_on_the_pc(this: *mut c_void, heap: usize, offset: } let h = pc_heap_desc(heap)?; let st = super::device_x::pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state as u32); + 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 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