diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 0210c522f1..cb07d5ed27 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -4259,6 +4259,45 @@ pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64 write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); } +/// command list #137 Write32BitValueBottomOfPipe2X(address, value, sync_flags, control_flags, flags) +pub(super) unsafe extern "system" fn write32_bottom_2x( + this: *mut c_void, + va: u64, + value: u32, + sync_flags: u32, + control_flags: u32, + flags: u32, +) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); + if sync_flags != 0 || control_flags != 0 { + flush_barrier(this); + } + record_write_trace(this, va, value as u64, flags, "W32B2"); + write(this, va, &[value], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, "Write32BitValueBottomOfPipe2X"); +} + +/// command list #138 Write64BitValueBottomOfPipe2X(address, value, sync_flags, control_flags, flags) +pub(super) unsafe extern "system" fn write64_bottom_2x( + this: *mut c_void, + va: u64, + value: u64, + sync_flags: u32, + control_flags: u32, + flags: u32, +) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); + const W: &str = "Write64BitValueBottomOfPipe2X"; + if sync_flags != 0 || control_flags != 0 { + flush_barrier(this); + } + record_write_trace(this, va, value, flags, "W64B2"); + if flags & BOTTOM_TIMESTAMPS != 0 { + return timestamp(this, va, W); + } + note_write64_recorded(this, va, value); + write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); +} + /// The title Resets its list (`title_list` `wrapper`) while the GPU has not finished the last /// submit of the PC list `primary` behind it: a console lets the title submit the new recording /// at once, the PC does not let `primary` be submitted again yet (#1452, debug layer id 553; @@ -4968,6 +5007,23 @@ pub(super) unsafe extern "system" fn flush_cache_range_x(this: *mut c_void, flag } } +/// command list #140 ResourceBarrierX(num_barriers, barriers, shared_flags, supplemental_actions): +/// PC D3D12 tracks full resource states and owns its own compression, so zero any non-null +/// supplemental-actions out-parameter and forward the barrier batch through `resource_barrier_x` +/// even if `shared_flags` requests preventing a console pipeline sync. +pub(super) unsafe extern "system" fn resource_barrier_ext_x( + this: *mut c_void, + n: u32, + barriers: *const [u32; 8], + _flags: u32, + actions: *mut u64, +) { + if !actions.is_null() { + actions.write_unaligned(0); + } + resource_barrier_x(this, n, barriers); +} + /// command list #67 (newer layout: #69) FlushPipelineX(flags, address, bytes) pub(super) unsafe extern "system" fn flush_pipeline_x(this: *mut c_void, _flags: u32, _va: u64, _bytes: u64) { let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListBarrier); @@ -10600,6 +10656,162 @@ mod tests { } } + /// Write32/64BitValueBottomOfPipe2X emit a UAV barrier when sync/control flags are set (on + /// DIRECT/COMPUTE lists only) and ResourceBarrierX zeros pSupplementalActions and forwards + /// through resource_barrier_x even when PREVENT_PIPELINE_SYNC is set. + #[test] + fn bop2x_and_resource_barrier_ext_x_behavior() { + #[repr(C)] + struct FakeList { + vt: *const usize, + ty: i32, + barriers: Mutex>, + } + unsafe extern "system" fn get_type(this: *mut c_void) -> D3D12_COMMAND_LIST_TYPE { + D3D12_COMMAND_LIST_TYPE((*(this as *const FakeList)).ty) + } + unsafe extern "system" fn barrier(this: *mut c_void, n: u32, b: *const D3D12_RESOURCE_BARRIER) { + for k in 0..n as usize { + let b = &*b.add(k); + let res = if b.Type == D3D12_RESOURCE_BARRIER_TYPE_UAV { + (*b.Anonymous.UAV).pResource.as_ref().map_or(0, |r| r.as_raw() as usize) + } else { + 0 + }; + (*(this as *const FakeList)).barriers.lock().unwrap().push((b.Type.0, res)); + } + } + let mut orig_vt = [0usize; 64]; + orig_vt[PC_GET_TYPE] = get_type as usize; + orig_vt[PC_RESOURCE_BARRIER] = barrier as usize; + let mut ext_vt = vec![0usize; 65]; + ext_vt[0] = orig_vt.as_ptr() as usize; + ext_vt[1..].copy_from_slice(&orig_vt); + let mut fake = Box::new(FakeList { vt: unsafe { ext_vt.as_ptr().add(1) }, ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, barriers: Mutex::new(Vec::new()) }); + let uav = (D3D12_RESOURCE_BARRIER_TYPE_UAV.0, 0); + unsafe { + let list = &mut *fake as *mut FakeList as *mut c_void; + // Use an unaligned VA so `write(...)` exits before lazily initializing global UMA. + write32_bottom_2x(list, 1, 3, 0, 0, 0); + write64_bottom_2x(list, 1, 4, 0, 0, 0); + assert!(fake.barriers.lock().unwrap().is_empty(), "zero sync/control flags must not emit extra barriers"); + write32_bottom_2x(list, 1, 3, 0x10_0000, 0, 0); + write64_bottom_2x(list, 1, 4, 0, 1, 0); + assert_eq!(*fake.barriers.lock().unwrap(), vec![uav; 2]); + // ResourceBarrierX zeros pActions and forwards through resource_barrier_x even with PREVENT_PIPELINE_SYNC + let mut actions = 0xDEAD_BEEF_CAFE_BABEu64; + let uav_raw = [D3D12_RESOURCE_BARRIER_TYPE_UAV.0 as u32, 0, 0, 0, 0, 0, 0, 0]; + resource_barrier_ext_x(list, 1, &uav_raw, 0x1_0000, &mut actions); + assert_eq!(actions, 0); + assert_eq!(*fake.barriers.lock().unwrap(), vec![uav; 3]); + resource_barrier_ext_x(list, 1, &uav_raw, 0x1_0000, std::ptr::null_mut()); + assert_eq!(*fake.barriers.lock().unwrap(), vec![uav; 4]); + for ty in [D3D12_COMMAND_LIST_TYPE_COPY, D3D12_COMMAND_LIST_TYPE_BUNDLE] { + fake.ty = ty.0; + write32_bottom_2x(list, 1, 5, 0x10_0000, 0, 0); + write64_bottom_2x(list, 1, 6, 0, 1, 0); + } + assert_eq!(fake.barriers.lock().unwrap().len(), 4); + } + } + + /// Real D3D12 GPU verification of Write64BitValueBottomOfPipe2X (sync + timestamp) + /// and ResourceBarrierX (non-null/null supplemental actions + real texture transitions). + #[test] + fn gpu_bop2x_and_resource_barrier_ext_x_execute_cleanly() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let Ok(factory) = CreateDXGIFactory1::() else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + let Ok(warp) = factory.EnumWarpAdapter::() else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + } + let dev = dev.unwrap(); + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + + let tex_desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 16, + Height: 16, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + Alignment: 0, + }; + let hp = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + let mut tex: Option = None; + dev.CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &tex_desc, D3D12_RESOURCE_STATE_COMMON, None, &mut tex).unwrap(); + let tex = tex.unwrap(); + + let list_ptr = list.as_raw(); + let orig_vt = *(list_ptr as *const *const usize); + let mut ext_vt = [0usize; 65]; + ext_vt[0] = orig_vt as usize; + std::ptr::copy_nonoverlapping(orig_vt, ext_vt.as_mut_ptr().add(1), 64); + + on_create(list_ptr, alloc.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_DIRECT); + *(list_ptr as *mut *const usize) = ext_vt.as_ptr().add(1); + + let ts64 = Box::new(0u64); + let ts_va = ts64.as_ref() as *const u64 as u64; + + // Write64BitValueBottomOfPipe2X with sync_flags + BOTTOM_TIMESTAMPS (5th arg) + write64_bottom_2x(list_ptr, ts_va, 0, 0x8000_0000, 0, BOTTOM_TIMESTAMPS); + // ResourceBarrierX transitioning COMMON -> UNORDERED_ACCESS -> COMMON with PREVENT_PIPELINE_SYNC + let tex_raw = tex.as_raw() as usize; + let make_tr = |before: u32, after: u32| -> [u32; 8] { + [ + D3D12_RESOURCE_BARRIER_TYPE_TRANSITION.0 as u32, + 0, + tex_raw as u32, + (tex_raw >> 32) as u32, + D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + before, + after, + 0, + ] + }; + let b1 = make_tr(D3D12_RESOURCE_STATE_COMMON.0 as u32, D3D12_RESOURCE_STATE_UNORDERED_ACCESS.0 as u32); + let b2 = make_tr(D3D12_RESOURCE_STATE_UNORDERED_ACCESS.0 as u32, D3D12_RESOURCE_STATE_COMMON.0 as u32); + let mut actions = u64::MAX; + resource_barrier_ext_x(list_ptr, 1, &b1, 0x1_0000, &mut actions); + assert_eq!(actions, 0); + resource_barrier_ext_x(list_ptr, 1, &b2, 0x1_0000, std::ptr::null_mut()); + + *(list_ptr as *mut *const usize) = orig_vt; + list.Close().expect("list with bop2x/barrier_x must close cleanly"); + let cl: ID3D12CommandList = list.cast().unwrap(); + q.ExecuteCommandLists(&[Some(cl)]); + cpu_writes_after(q.as_raw(), &[list_ptr]); + + let t = Instant::now(); + while (*(ts_va as *const AtomicU64)).load(Ordering::Acquire) == 0 { + assert!(t.elapsed().as_secs() < 5, "bop2x GPU timestamp never landed"); + std::thread::yield_now(); + } + assert_ne!(*ts64, 0); + + on_release(list_ptr as usize); + } + } + /// The next frame's Signal of a title fence does not break a "cycle" through a wait an earlier /// signal of that fence satisfies; one that only the new signal satisfies is a cycle, broken /// by releasing the cell's wait. diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index bd9cd731cf..72f57d3a94 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1674,6 +1674,9 @@ fn console_slots(kind: Kind) -> Vec<(usize, usize)> { // the newer layout's (called before anything tells the layouts apart) (131, l::flush_cache_range_x as usize), (132, l::flush_cache_range_x as usize), + (137, l::write32_bottom_2x as usize), + (138, l::write64_bottom_2x as usize), + (140, l::resource_barrier_ext_x as usize), // console compute/BVH builder extensions (`XboxBvhBuilder.dll`) (146, l::uav_barrier_x as usize), (148, b::allocate_upload_memory as usize), @@ -12791,6 +12794,18 @@ mod readback_routing_tests { assert!(slots.contains(&(54, l::clear_uav_float_x as usize))); } + /// Newer console command-list sync / BOP-write / ResourceBarrierX slots are hooked. + #[test] + fn console_slots_hook_sync_bop2x_and_resource_barrier_x() { + use super::super::list_x as l; + let slots = console_slots(Kind::CommandList); + assert!(slots.contains(&(131, l::flush_cache_range_x as usize))); + assert!(slots.contains(&(132, l::flush_cache_range_x as usize))); + assert!(slots.contains(&(137, l::write32_bottom_2x as usize))); + assert!(slots.contains(&(138, l::write64_bottom_2x as usize))); + assert!(slots.contains(&(140, l::resource_barrier_ext_x as usize))); + } + /// Bug #6: `Kind::DmaList` must install `cl_release` so releasing a DMA command list removes /// its entry from `list_x::LISTS`. #[test]