diff --git a/crates/xcl/src/hle/d3d12x/cpu_writes.rs b/crates/xcl/src/hle/d3d12x/cpu_writes.rs index eeb524893..2c420bf0d 100644 --- a/crates/xcl/src/hle/d3d12x/cpu_writes.rs +++ b/crates/xcl/src/hle/d3d12x/cpu_writes.rs @@ -18,12 +18,13 @@ use windows::Win32::Foundation::HANDLE; use windows::Win32::Graphics::Direct3D12::*; use windows_sys::Win32::System::Threading as th; -/// A bottom-of-pipe write into memory outside unified memory, applied by the CPU once the -/// queue's fence reaches the submission that recorded it. +/// A deferred CPU write into title memory (plain values, OS QPC timestamps, or GPU readback +/// slots copied atomically once the queue's fence completes). #[derive(Clone)] pub(super) enum Write { - /// Immediate dwords to store starting at `va`. - Immediate(usize, Vec), + U32(usize, u32), + U64(usize, u64), + OsTimestamp(usize), /// A 64-bit value resolved by the GPU into a persistently mapped READBACK buffer slot /// (`src`), copied atomically to `va` once the queue's fence completes. Holding `_buf` keeps /// the mapping alive until the worker finishes. @@ -71,13 +72,7 @@ fn worker() -> &'static Mutex> { } } for w in &job.writes { - match w { - Write::Immediate(va, words) => write_now(*va, words), - Write::ReadbackU64 { va, src, .. } => { - let value = (*(*src as *const AtomicU64)).load(Ordering::Acquire); - write_u64_now(*va, value); - } - } + write_now(w); } } }); @@ -88,45 +83,56 @@ fn worker() -> &'static Mutex> { }) } -/// The dwords at `va`, if the memory is still there and writable. -unsafe fn write_now(va: usize, words: &[u32]) { - use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_READWRITE}; - let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); - if VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.State != MEM_COMMIT { - return; - } - if m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) == 0 || (m.BaseAddress as usize + m.RegionSize) < va + 4 * words.len() { - return; - } - for (i, &w) in words.iter().enumerate() { - (*((va + 4 * i) as *const AtomicU32)).store(w, Ordering::Release); - } +fn protect_writable(protect: u32) -> bool { + use windows_sys::Win32::System::Memory::{PAGE_EXECUTE_READWRITE, PAGE_GUARD, PAGE_READWRITE}; + protect & PAGE_GUARD == 0 && protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0 } -/// An 8-byte aligned 64-bit value at `va`, if the memory is still there and writable. -unsafe fn write_u64_now(va: usize, value: u64) { - use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_GUARD, PAGE_READWRITE}; - let Some(end) = va.checked_add(8) else { return }; - let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); - if VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.State != MEM_COMMIT { +/// Perform `w` now if its destination is still committed, writable (not `PAGE_GUARD`), +/// and properly aligned for an atomic store. +pub(super) unsafe fn write_now(w: &Write) { + let (va, bytes) = match *w { + Write::U32(va, _) => (va, 4usize), + Write::U64(va, _) | Write::OsTimestamp(va) | Write::ReadbackU64 { va, .. } => (va, 8usize), + }; + if va == 0 || !va.is_multiple_of(bytes) { return; } - if m.Protect & PAGE_GUARD != 0 || m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) == 0 || (m.BaseAddress as usize).saturating_add(m.RegionSize) < end { - return; + let Some(end) = va.checked_add(bytes) else { return }; + // Committed coherent UMA windows are committed and RW as a whole; skip VirtualQuery. + if !crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, bytes)) { + use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT}; + let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + if VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.State != MEM_COMMIT { + return; + } + if !protect_writable(m.Protect) || (m.BaseAddress as usize).saturating_add(m.RegionSize) < end { + return; + } + } + match *w { + Write::U32(va, v) => (*(va as *const AtomicU32)).store(v, Ordering::Release), + Write::U64(va, v) => (*(va as *const AtomicU64)).store(v, Ordering::Release), + Write::OsTimestamp(va) => (*(va as *const AtomicU64)).store(super::frame::qpc() as u64, Ordering::Release), + Write::ReadbackU64 { va, src, .. } => { + let value = (*(src as *const AtomicU64)).load(Ordering::Acquire); + (*(va as *const AtomicU64)).store(value, Ordering::Release); + } } - (*(va as *const AtomicU64)).store(value, Ordering::Release); } /// Whether `[va, va + bytes)` is memory the CPU can write for the GPU later. pub(super) fn writable(va: usize, bytes: usize) -> bool { + let Some(end) = va.checked_add(bytes) else { return false }; + // Fast path for committed coherent UMA windows: always committed RW, avoiding VirtualQuery. + if bytes > 0 && crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, bytes)) { + return true; + } crate::hle::kernelx::lazy::ensure(va, bytes); - use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_READWRITE}; + use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT}; let mut m: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; let found = unsafe { VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) } != 0; - found - && m.State == MEM_COMMIT - && m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0 - && m.BaseAddress as usize + m.RegionSize >= va + bytes + found && m.State == MEM_COMMIT && protect_writable(m.Protect) && (m.BaseAddress as usize).saturating_add(m.RegionSize) >= end } /// `writes` of lists just submitted to `queue`: done when the GPU has finished them. @@ -179,16 +185,57 @@ mod tests { } let dev = dev.unwrap(); let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); - let cell = Box::new([0u32; 2]); - let va = cell.as_ptr() as usize; + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as usize; assert!(writable(va, 8)); - after(q.as_raw(), vec![Write::Immediate(va, vec![7, 9])]); + let want = 7u64 | (9u64 << 32); + after(q.as_raw(), vec![Write::U64(va, want)]); let t = std::time::Instant::now(); - while (*(va as *const AtomicU32)).load(Ordering::Acquire) != 7 { + while cell.load(Ordering::Acquire) != want { assert!(t.elapsed().as_secs() < 5, "never written"); std::thread::yield_now(); } - assert_eq!(*cell, [7, 9]); + assert_eq!(cell.load(Ordering::Acquire), want); + } + } + + /// OS QPC timestamp writes land a QPC value in range after the queue completes, and + /// `PAGE_GUARD` pages are rejected without faulting. + #[test] + fn os_timestamp_writes_qpc_and_writable_rejects_page_guard() { + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, VirtualProtect, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_GUARD, PAGE_READWRITE}; + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let factory: IDXGIFactory4 = CreateDXGIFactory1().unwrap(); + let warp: IDXGIAdapter = factory.EnumWarpAdapter().unwrap(); + D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).expect("WARP D3D12 device"); + } + let dev = dev.unwrap(); + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); + + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as usize; + let before = super::super::frame::qpc() as u64; + after(q.as_raw(), vec![Write::OsTimestamp(va)]); + let t = std::time::Instant::now(); + while cell.load(Ordering::Acquire) == 0 { + assert!(t.elapsed().as_secs() < 5, "OS timestamp never written"); + std::thread::yield_now(); + } + let after_qpc = super::super::frame::qpc() as u64; + let got = cell.load(Ordering::Acquire); + assert!(got >= before && got <= after_qpc, "QPC {got} outside [{before}, {after_qpc}]"); + + let page = VirtualAlloc(std::ptr::null(), 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + assert!(!page.is_null()); + let mut old = 0u32; + assert_ne!(VirtualProtect(page, 4096, PAGE_READWRITE | PAGE_GUARD, &mut old), 0); + assert!(!writable(page as usize, 8), "PAGE_GUARD must not be treated as writable"); + write_now(&Write::U64(page as usize, 0x1234)); + assert_ne!(VirtualProtect(page, 4096, PAGE_READWRITE, &mut old), 0); + assert_eq!((*(page as *const AtomicU64)).load(Ordering::Acquire), 0); + VirtualFree(page, 0, MEM_RELEASE); } } diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 794c418ab..39310c6bc 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -48,10 +48,31 @@ const PC_OM_SET_DEPTH_BOUNDS: usize = 62; const PC_WRITE_BUFFER_IMMEDIATE: usize = 66; const PC_QUEUE_EXECUTE_COMMAND_LISTS: usize = 10; -/// Write64BitValueBottomOfPipeX flags 1 / 2, Write64BitValueTopOfPipeX flags 4 / 8: the value -/// written is a GPU / system timestamp -const BOTTOM_TIMESTAMPS: u32 = 0x3; -const TOP_TIMESTAMPS: u32 = 0xC; +/// `D3D12XBOX_WRITE_VALUE_BOTTOM_OF_PIPE_FLAG`: +/// bit 0 (`0x1`) writes the GPU timestamp; bit 1 (`0x2`) writes the OS QPC timestamp. +const BOTTOM_GPU_TIMESTAMP: u32 = 0x1; +const BOTTOM_OS_TIMESTAMP: u32 = 0x2; +/// `D3D12XBOX_WRITE_VALUE_TOP_OF_PIPE_FLAG`: +/// bit 2 (`0x4`) writes the GPU timestamp; bit 3 (`0x8`) writes the OS QPC timestamp. +const TOP_GPU_TIMESTAMP: u32 = 0x4; +const TOP_OS_TIMESTAMP: u32 = 0x8; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Write64Kind { + Value, + GpuTimestamp, + OsTimestamp, +} + +fn classify_write64(flags: u32, gpu_bit: u32, os_bit: u32) -> Write64Kind { + if flags & os_bit != 0 { + Write64Kind::OsTimestamp + } else if flags & gpu_bit != 0 { + Write64Kind::GpuTimestamp + } else { + Write64Kind::Value + } +} /// fills larger than this are not done (one pattern buffer per value, up to this size) const MAX_FILL: u64 = 256 << 20; /// Bounded wait timeout for Wait32BitValueX / Wait64BitValueX so an unsatisfied wait never hangs. @@ -578,6 +599,8 @@ struct ListState { pending_waits: Vec, /// bottom-of-pipe writes into memory the GPU does not see (`cpu_writes`) cpu_writes: Vec, + /// top-of-pipe OS QPC timestamp destinations stamped right before submission + os_top_writes: Vec, /// Dedicated 256-byte buffer holding the current predicate value (created lazily in COMMON state). pred_buf: Option, /// True while `pred_buf` is in `D3D12_RESOURCE_STATE_PREDICATION` on this open recording. @@ -792,6 +815,7 @@ pub(super) fn clear_segment_state(list: usize) { s.clear_write64_signals(); s.clear_waits(); s.cpu_writes.clear(); + s.os_top_writes.clear(); s.predication = None; s.pred_in_state = false; s.bindings = Bindings::default(); @@ -1893,6 +1917,18 @@ pub(super) unsafe fn readback_sparse_writes(queue: *mut c_void, lists: &[*mut c_ } } +/// Right before `lists` are submitted to a queue: stamp any top-of-pipe OS QPC timestamps. +pub(super) unsafe fn cpu_writes_before(lists: &[*mut c_void]) { + let vas: Vec = { + let g = LISTS.lock().unwrap(); + let Some(m) = g.as_ref() else { return }; + lists.iter().filter_map(|&l| m.get(&(l as usize))).flat_map(|s| s.os_top_writes.iter().copied()).collect() + }; + for va in vas { + super::cpu_writes::write_now(&super::cpu_writes::Write::OsTimestamp(va)); + } +} + /// After `lists` went to `queue`: their writes into memory the GPU does not see (`cpu_writes`). pub(super) unsafe fn cpu_writes_after(queue: *mut c_void, lists: &[*mut c_void]) { let writes: Vec = { @@ -2079,9 +2115,16 @@ unsafe fn write(list: *mut c_void, va: u64, words: &[u32], mode: D3D12_WRITEBUFF let (Some((d, _)), Some(at)) = (real::uma_window_buffer(va as usize, n as usize), real::uma_va_of(va as usize)) else { // the title's own memory: written by the CPU once the GPU is there if super::cpu_writes::writable(va as usize, n as usize) { - if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(list as usize))) { - s.cpu_writes.push(super::cpu_writes::Write::Immediate(va as usize, words.to_vec())); - return; + let entry = match words { + [w0] => Some(super::cpu_writes::Write::U32(va as usize, *w0)), + [w0, w1] => Some(super::cpu_writes::Write::U64(va as usize, (*w0 as u64) | ((*w1 as u64) << 32))), + _ => None, + }; + if let Some(entry) = entry { + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(list as usize))) { + s.cpu_writes.push(entry); + return; + } } } return warn(&N, what, format_args!("{va:#x} is not in unified memory")); @@ -2197,6 +2240,26 @@ unsafe fn timestamp(list: *mut c_void, va: u64, what: &str) { } } +/// Record an OS QPC timestamp write at `va` (`bottom`: after GPU completion via `cpu_writes`, +/// else before submission via `os_top_writes`). Works on any command list type and any writable +/// title memory (UMA or plain CPU memory). +unsafe fn os_timestamp(list: *mut c_void, va: u64, bottom: bool, what: &str) { + static N: AtomicU64 = AtomicU64::new(0); + if !va.is_multiple_of(8) { + return warn(&N, what, format_args!("{va:#x} is not 8-byte aligned")); + } + if !super::cpu_writes::writable(va as usize, 8) { + return warn(&N, what, format_args!("{va:#x} is not writable memory")); + } + let mut g = LISTS.lock().unwrap(); + let s = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); + if bottom { + s.cpu_writes.push(super::cpu_writes::Write::OsTimestamp(va as usize)); + } else { + s.os_top_writes.push(va as usize); + } +} + /// command list #69 Write32BitValueBottomOfPipeX(address, value, flush, flags) /// /// # Safety @@ -2210,11 +2273,14 @@ pub unsafe extern "system" fn write32_bottom_x(this: *mut c_void, va: u64, value pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64, value: u64, _flush: u32, flags: u32) { const W: &str = "Write64BitValueBottomOfPipeX"; record_write_trace(this, va, value, flags, "W64B"); - if flags & BOTTOM_TIMESTAMPS != 0 { - return timestamp(this, va, W); + match classify_write64(flags, BOTTOM_GPU_TIMESTAMP, BOTTOM_OS_TIMESTAMP) { + Write64Kind::GpuTimestamp => timestamp(this, va, W), + Write64Kind::OsTimestamp => os_timestamp(this, va, true, W), + Write64Kind::Value => { + note_write64_recorded(this, va, value); + write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); + } } - note_write64_recorded(this, va, value); - write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); } /// Deferred mid-list split for `Wait32/64BitValueX` recorded after a `Write64BitValue*` in the @@ -2483,11 +2549,14 @@ pub(super) unsafe extern "system" fn write32_top_x(this: *mut c_void, va: u64, v pub(super) unsafe extern "system" fn write64_top_x(this: *mut c_void, va: u64, value: u64, flags: u32) { const W: &str = "Write64BitValueTopOfPipeX"; record_write_trace(this, va, value, flags, "W64T"); - if flags & TOP_TIMESTAMPS != 0 { - return timestamp(this, va, W); + match classify_write64(flags, TOP_GPU_TIMESTAMP, TOP_OS_TIMESTAMP) { + Write64Kind::GpuTimestamp => timestamp(this, va, W), + Write64Kind::OsTimestamp => os_timestamp(this, va, false, W), + Write64Kind::Value => { + note_write64_recorded(this, va, value); + write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_IN, W); + } } - note_write64_recorded(this, va, value); - write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_IN, W); } /// command list #90 Write32BitValueBottomOfShaderX(address, value, stage) @@ -2732,11 +2801,12 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let write64_signals = s.clear_write64_signals(); s.written_ranges.clear(); let cpu_writes = std::mem::take(&mut s.cpu_writes); + let os_top_writes = std::mem::take(&mut s.os_top_writes); let pred_in_state = std::mem::replace(&mut s.pred_in_state, false); let pred_buf = s.pred_buf.as_ref().map(|r| r.as_raw() as usize); - (s.allocator, q, s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, waits, (sparse_writes, write64_signals, cpu_writes)) + (s.allocator, q, s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, waits, (sparse_writes, write64_signals, cpu_writes, os_top_writes)) }; - let (sparse_writes, write64_signals, cpu_writes) = sparse_writes; + let (sparse_writes, write64_signals, cpu_writes, os_top_writes) = sparse_writes; // End any active occlusion query before Close (PC forbids closing with an open query). if let Some(((h, i), qty)) = active_query { @@ -2766,6 +2836,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { if let Some((_, ref prior, _)) = kickoff_split { if !prior.is_empty() { execute_with_waits_on(q_raw, prior, |chunk| { + cpu_writes_before(chunk); exec(q_raw, chunk.len() as u32, chunk.as_ptr()); super::alloc_pool::executed(q_raw, chunk); signal_write64_fences(q_raw, chunk); @@ -2783,6 +2854,10 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { wait_all_on(q_raw, &waits, &write64_signals); } + for va in os_top_writes { + super::cpu_writes::write_now(&super::cpu_writes::Write::OsTimestamp(va)); + } + let one = [this]; exec(q_raw, 1, one.as_ptr()); super::alloc_pool::executed(q_raw, &one); @@ -2826,6 +2901,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { s.clear_write64_signals(); s.clear_waits(); s.cpu_writes.clear(); + s.os_top_writes.clear(); s.bindings = bindings_copy; } primary @@ -3411,6 +3487,7 @@ pub unsafe extern "system" fn reset_x(this: *mut c_void, alloc: usize, pso: usiz s.clear_write64_signals(); s.clear_waits(); s.cpu_writes.clear(); + s.os_top_writes.clear(); s.predication = None; s.pred_in_state = false; s.bindings = Bindings::default(); @@ -4966,16 +5043,16 @@ mod tests { let top_va = top_ts.as_ref() as *const u64 as u64; // Unaligned and null targets are ignored without queueing CPU writes. - write64_bottom_x(list_ptr, bottom_va + 4, 0, 0, BOTTOM_TIMESTAMPS); - write64_bottom_x(list_ptr, 0, 0, 0, BOTTOM_TIMESTAMPS); + write64_bottom_x(list_ptr, bottom_va + 4, 0, 0, BOTTOM_GPU_TIMESTAMP); + write64_bottom_x(list_ptr, 0, 0, 0, BOTTOM_GPU_TIMESTAMP); { let g = LISTS.lock().unwrap(); assert!(g.as_ref().unwrap()[&(list_ptr as usize)].cpu_writes.is_empty()); } // Valid 8-byte aligned non-UMA targets record GPU timestamp queries + readback copies. - write64_top_x(list_ptr, top_va, 0, TOP_TIMESTAMPS); - write64_bottom_x(list_ptr, bottom_va, 0, 0, BOTTOM_TIMESTAMPS); + write64_top_x(list_ptr, top_va, 0, TOP_GPU_TIMESTAMP); + write64_bottom_x(list_ptr, bottom_va, 0, 0, BOTTOM_GPU_TIMESTAMP); { let g = LISTS.lock().unwrap(); assert_eq!(g.as_ref().unwrap()[&(list_ptr as usize)].cpu_writes.len(), 2); @@ -5000,6 +5077,43 @@ mod tests { } } + #[test] + fn write64_os_timestamp_flags_route_to_qpc_instead_of_gpu_timestamps() { + assert_eq!(classify_write64(0, BOTTOM_GPU_TIMESTAMP, BOTTOM_OS_TIMESTAMP), Write64Kind::Value); + assert_eq!(classify_write64(BOTTOM_GPU_TIMESTAMP, BOTTOM_GPU_TIMESTAMP, BOTTOM_OS_TIMESTAMP), Write64Kind::GpuTimestamp); + assert_eq!(classify_write64(BOTTOM_OS_TIMESTAMP, BOTTOM_GPU_TIMESTAMP, BOTTOM_OS_TIMESTAMP), Write64Kind::OsTimestamp); + assert_eq!(classify_write64(0, TOP_GPU_TIMESTAMP, TOP_OS_TIMESTAMP), Write64Kind::Value); + assert_eq!(classify_write64(TOP_GPU_TIMESTAMP, TOP_GPU_TIMESTAMP, TOP_OS_TIMESTAMP), Write64Kind::GpuTimestamp); + assert_eq!(classify_write64(TOP_OS_TIMESTAMP, TOP_GPU_TIMESTAMP, TOP_OS_TIMESTAMP), Write64Kind::OsTimestamp); + + // Recording top/bottom OS timestamps into plain CPU memory works without UMA or a GPU + // timestamp heap, and cpu_writes_before stamps a live QPC value. + let fake_list = 0x5EED_0040usize as *mut c_void; + let top_cell = Box::new(AtomicU64::new(0)); + let bot_cell = Box::new(AtomicU64::new(0)); + let top_va = &*top_cell as *const AtomicU64 as u64; + let bot_va = &*bot_cell as *const AtomicU64 as u64; + unsafe { + write64_top_x(fake_list, top_va, 0, TOP_OS_TIMESTAMP); + write64_bottom_x(fake_list, bot_va, 0, 0, BOTTOM_OS_TIMESTAMP); + let before = super::super::frame::qpc() as u64; + cpu_writes_before(&[fake_list]); + let after = super::super::frame::qpc() as u64; + let got = top_cell.load(Ordering::Acquire); + assert!(got >= before && got <= after, "top OS QPC {got} outside [{before}, {after}]"); + { + let g = LISTS.lock().unwrap(); + let s = g.as_ref().unwrap().get(&(fake_list as usize)).unwrap(); + assert!(matches!( + s.cpu_writes.as_slice(), + [super::super::cpu_writes::Write::OsTimestamp(va)] if *va == bot_va as usize + )); + assert_eq!(s.os_top_writes, vec![top_va as usize]); + } + on_release(fake_list as usize); + } + } + #[test] fn write_buffer_immediate_on_open_existing_heap_visible_to_cpu() { use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 5b01e8569..5854d0a12 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -5099,6 +5099,7 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis } super::list_x::note_queue_for_lists(this as usize, &mapped); super::list_x::execute_with_waits_on(this, &mapped, |chunk| { + super::list_x::cpu_writes_before(chunk); f(this, chunk.len() as u32, chunk.as_ptr()); super::alloc_pool::executed(this, chunk); super::list_x::signal_write64_fences(this, chunk);