diff --git a/crates/xcl/src/hle/api_cost.rs b/crates/xcl/src/hle/api_cost.rs index 8178e3b93e..dc7bf549ba 100644 --- a/crates/xcl/src/hle/api_cost.rs +++ b/crates/xcl/src/hle/api_cost.rs @@ -29,9 +29,26 @@ pub enum Call { FenceWait, /// copies back landed for the title's look at a fence (`sparse::settle_readbacks_within`) FenceSettle, + /// draw recording on command lists (`DrawInstanced`, `DrawIndexedInstanced`, `DrawIndexedX`, + /// `DispatchMesh`, non-dispatch `ExecuteIndirect`) + DrawRecord, + /// compute dispatch recording (`Dispatch`, `DispatchX`, dispatch `ExecuteIndirect`) + DispatchRecord, + /// root signatures/arguments, pipeline state, descriptor heaps, IA buffers, render targets + BindState, + /// device view creation and descriptor copies + ViewDescriptor, + /// GPU-view tile asks (`view_want::want`; sub-timer, may nest inside draw/dispatch/bind/view/copy) + ViewWant, + /// resource barriers, UAV barriers, and console cache/pipeline flushes + ListBarrier, + /// copies, fills, clears, resolves, and bottom-of-pipe writes + ListCopy, + /// command list Close/Reset and command allocator Reset + ListLifecycle, } -pub const CALLS: [(Call, &str); 12] = [ +pub const CALLS: [(Call, &str); 20] = [ (Call::PlacedTexture, "placed textures"), (Call::PlacedBuffer, "placed buffers"), (Call::PhysicalPages, "physical page calls"), @@ -44,12 +61,72 @@ pub const CALLS: [(Call, &str); 12] = [ (Call::WaitFrameEvent, "WaitFrameEventX"), (Call::FenceWait, "blocking fence waits"), (Call::FenceSettle, "copies back settled at fence looks"), + (Call::DrawRecord, "draw recording"), + (Call::DispatchRecord, "dispatch recording"), + (Call::BindState, "state and root bindings"), + (Call::ViewDescriptor, "view and descriptor calls"), + (Call::ViewWant, "GPU-view tile asks"), + (Call::ListBarrier, "barriers and flushes"), + (Call::ListCopy, "copies, clears, and memory ops"), + (Call::ListLifecycle, "list close and reset"), ]; -/// calls, microseconds +/// Calls that nest inside another timed call and must not be added into the submitting thread's +/// non-overlapping completed-call total. +pub const fn is_sub_call(call: Call) -> bool { + matches!(call, Call::PresentCopyWait | Call::PipelineWait | Call::ViewWant) +} + +/// Coherent single-writer `(count, ns)` counter pair protected by a seqlock generation +/// (odd while updating, even when quiescent). Safe for `ON_SUBMITTER` because `SUBMITTER` +/// latches at most one thread ID via `compare_exchange(0, tid)` for the process lifetime. +struct CounterPair { + seq: AtomicU64, + count: AtomicU64, + ns: AtomicU64, +} + +impl CounterPair { + const fn new() -> Self { + Self { + seq: AtomicU64::new(0), + count: AtomicU64::new(0), + ns: AtomicU64::new(0), + } + } + + fn add_single_writer(&self, ns: u64) { + let g = self.seq.load(Ordering::Relaxed); + self.seq.store(g.wrapping_add(1), Ordering::Relaxed); + std::sync::atomic::fence(Ordering::Release); + self.count.store(self.count.load(Ordering::Relaxed).wrapping_add(1), Ordering::Relaxed); + self.ns.store(self.ns.load(Ordering::Relaxed).saturating_add(ns), Ordering::Relaxed); + self.seq.store(g.wrapping_add(2), Ordering::Release); + } + + fn load_coherent(&self) -> (u64, u64) { + loop { + let g1 = self.seq.load(Ordering::Acquire); + if g1 & 1 != 0 { + std::hint::spin_loop(); + continue; + } + let count = self.count.load(Ordering::Relaxed); + let ns = self.ns.load(Ordering::Relaxed); + std::sync::atomic::fence(Ordering::Acquire); + let g2 = self.seq.load(Ordering::Relaxed); + if g1 == g2 { + return (count, ns / 1000); + } + std::hint::spin_loop(); + } + } +} + +/// calls, nanoseconds (sub-µs calls accumulate without per-call truncation; exposed as µs) static COST: [[AtomicU64; 2]; CALLS.len()] = [const { [AtomicU64::new(0), AtomicU64::new(0)] }; CALLS.len()]; -/// the same on the submitting thread (`SUBMITTER`) -static ON_SUBMITTER: [[AtomicU64; 2]; CALLS.len()] = [const { [AtomicU64::new(0), AtomicU64::new(0)] }; CALLS.len()]; +/// the same on the submitting thread (`SUBMITTER`), read coherently via `CounterPair`'s seqlock +static ON_SUBMITTER: [CounterPair; CALLS.len()] = [const { CounterPair::new() }; CALLS.len()]; /// the thread that makes the title's submits (0: none seen, or `XCL_LOOK_STATS` is not 2) static SUBMITTER: AtomicU32 = AtomicU32::new(0); @@ -63,6 +140,12 @@ pub fn time(call: Call) -> Timer { Timer { call, start: Instant::now() } } +/// Times `call` only when `on` is true (zero clock, TLS, or atomic overhead when false). +#[inline] +pub fn time_if(on: bool, call: Call) -> Option { + on.then(|| time(call)) +} + impl Drop for Timer { fn drop(&mut self) { add(self.call, self.start.elapsed()); @@ -75,15 +158,14 @@ fn this_thread() -> u32 { /// A call that took `d`. pub fn add(call: Call, d: Duration) { - let us = d.as_micros() as u64; - let c = &COST[call as usize]; + let ns = d.as_nanos().min(u64::MAX as u128) as u64; + let idx = call as usize; + let c = &COST[idx]; c[0].fetch_add(1, Ordering::Relaxed); - c[1].fetch_add(us, Ordering::Relaxed); - let s = SUBMITTER.load(Ordering::Relaxed); + c[1].fetch_add(ns, Ordering::Relaxed); + let s = SUBMITTER.load(Ordering::Acquire); if s != 0 && s == this_thread() { - let c = &ON_SUBMITTER[call as usize]; - c[0].fetch_add(1, Ordering::Relaxed); - c[1].fetch_add(us, Ordering::Relaxed); + ON_SUBMITTER[idx].add_single_writer(ns); } } @@ -91,17 +173,62 @@ pub fn add(call: Call, d: Duration) { /// thread from then on (a retail title's world made every submit and present on one thread). /// Called with `XCL_LOOK_STATS=2` only. pub fn note_submitter() { - let _ = SUBMITTER.compare_exchange(0, this_thread(), Ordering::Relaxed, Ordering::Relaxed); + let _ = SUBMITTER.compare_exchange(0, this_thread(), Ordering::AcqRel, Ordering::Acquire); } /// (calls, microseconds) so far, in the order of `CALLS`. pub fn totals() -> [(u64, u64); CALLS.len()] { - std::array::from_fn(|k| (COST[k][0].load(Ordering::Relaxed), COST[k][1].load(Ordering::Relaxed))) + std::array::from_fn(|k| (COST[k][0].load(Ordering::Relaxed), COST[k][1].load(Ordering::Relaxed) / 1000)) } -/// `totals` of the submitting thread +/// Coherent `(calls, microseconds)` snapshot of the submitting thread, in the order of `CALLS`. pub fn submitter_totals() -> [(u64, u64); CALLS.len()] { - std::array::from_fn(|k| (ON_SUBMITTER[k][0].load(Ordering::Relaxed), ON_SUBMITTER[k][1].load(Ordering::Relaxed))) + std::array::from_fn(|k| ON_SUBMITTER[k].load_coherent()) +} + +/// Formats per-category `(calls, ms)` deltas across all threads since `before`, retaining any +/// category with a nonzero count **or** duration delta so multi-writer snapshots never drop +/// completed duration when count and duration are observed on separate ticks. +pub fn all_threads_summary_parts(now: &[(u64, u64)], before: &[(u64, u64)]) -> Vec { + CALLS + .iter() + .zip(now.iter().zip(before)) + .filter_map(|(c, (n, o))| { + let dc = n.0.saturating_sub(o.0); + let dus = n.1.saturating_sub(o.1); + if dc == 0 && dus == 0 { + return None; + } + Some(format!("{dc} {} in {} ms", c.1, dus / 1000)) + }) + .collect() +} + +/// Formats durations of submitting-thread calls **completed** since `before` alongside the +/// sample's elapsed `window_ms`. Summing only top-level calls avoids double-counting nested +/// sub-timers (`is_sub_call`). Because a call's full duration is published when it completes, a +/// call that started before the window and finished inside it can make the completed total exceed +/// `window_ms`; accordingly no in-window "untimed" remainder is claimed. Any nonzero duration +/// delta is accounted even if its count delta is zero. +pub fn submitter_summary_line(now: &[(u64, u64)], before: &[(u64, u64)], window_ms: u64) -> Option { + let mut parts = Vec::new(); + let mut completed_us = 0u64; + for (c, (n, o)) in CALLS.iter().zip(now.iter().zip(before)) { + let dc = n.0.saturating_sub(o.0); + let dus = n.1.saturating_sub(o.1); + if dc == 0 && dus == 0 { + continue; + } + if !is_sub_call(c.0) { + completed_us = completed_us.saturating_add(dus); + } + parts.push(format!("{dc} {} in {} ms", c.1, dus / 1000)); + } + if parts.is_empty() { + return None; + } + let completed_ms = completed_us / 1000; + Some(format!("{}; completed {completed_ms} ms (window {window_ms} ms)", parts.join(", "))) } #[cfg(test)] @@ -138,4 +265,128 @@ mod tests { assert_eq!((all2.0 - all.0, all2.1 - all.1), (2, 350)); assert_eq!((mine2.0 - mine.0, mine2.1 - mine.1), (1, 300)); } + + #[test] + fn sub_microsecond_calls_accumulate_without_truncation() { + let k = Call::ListBarrier as usize; + let before = totals()[k]; + for _ in 0..1000 { + add(Call::ListBarrier, Duration::from_nanos(500)); + } + let after = totals()[k]; + assert_eq!(after.0 - before.0, 1000); + assert_eq!(after.1 - before.1, 500); + } + + #[test] + fn time_if_disabled_adds_zero_counts_and_zero_time() { + let k = Call::ListLifecycle as usize; + let before_all = totals()[k]; + let before_sub = submitter_totals()[k]; + { + let guard = time_if(false, Call::ListLifecycle); + assert!(guard.is_none()); + } + assert_eq!(totals()[k], before_all); + assert_eq!(submitter_totals()[k], before_sub); + } + + #[test] + fn submitter_summary_excludes_sub_calls_and_reports_completed_window() { + let before = [(0u64, 0u64); CALLS.len()]; + assert_eq!(submitter_summary_line(&before, &before, 2000), None); + + let mut now = before; + // Top-level calls: 100 + 250 + 50 = 400 ms completed + now[Call::Submit as usize] = (10, 100_000); + now[Call::DrawRecord as usize] = (500, 250_000); + now[Call::BindState as usize] = (2000, 50_000); + // Nested sub-calls: displayed, but excluded from `completed` + now[Call::PipelineWait as usize] = (3, 40_000); + now[Call::PresentCopyWait as usize] = (2, 30_000); + now[Call::ViewWant as usize] = (400, 90_000); + + let line = submitter_summary_line(&now, &before, 2000).unwrap(); + assert!(line.contains("10 ExecuteCommandLists in 100 ms"), "{line}"); + assert!(line.contains("500 draw recording in 250 ms"), "{line}"); + assert!(line.contains("2000 state and root bindings in 50 ms"), "{line}"); + assert!(line.contains("3 waits for pipelines being built in 40 ms"), "{line}"); + assert!(line.contains("2 waits of a present for the last frame's copy in 30 ms"), "{line}"); + assert!(line.contains("400 GPU-view tile asks in 90 ms"), "{line}"); + assert!(line.ends_with("; completed 400 ms (window 2000 ms)"), "{line}"); + assert!(!line.contains("untimed"), "{line}"); + assert!(is_sub_call(Call::PresentCopyWait) && is_sub_call(Call::PipelineWait) && is_sub_call(Call::ViewWant)); + assert!(!is_sub_call(Call::Submit) && !is_sub_call(Call::DrawRecord) && !is_sub_call(Call::FenceWait)); + } + + #[test] + fn summary_retains_nonzero_duration_delta_when_count_delta_is_zero() { + // Deterministic torn-snapshot sequence `(0,0) -> (1,0) -> (1,400_000)` on a top-level + // call and `(0,0) -> (1,0) -> (1,50_000)` on a nested sub-call: the second tick must + // retain the 400 ms duration in both `all_threads_summary_parts` and `submitter_summary_line`, + // without double-counting the nested sub-call into `completed`. + let snap0 = [(0u64, 0u64); CALLS.len()]; + let mut snap1 = snap0; + snap1[Call::FenceWait as usize] = (1, 0); + snap1[Call::ViewWant as usize] = (1, 0); + let mut snap2 = snap1; + snap2[Call::FenceWait as usize] = (1, 400_000); + snap2[Call::ViewWant as usize] = (1, 50_000); + + let sub1 = submitter_summary_line(&snap1, &snap0, 2000).unwrap(); + assert!(sub1.contains("1 blocking fence waits in 0 ms"), "{sub1}"); + assert!(sub1.contains("1 GPU-view tile asks in 0 ms"), "{sub1}"); + assert!(sub1.ends_with("; completed 0 ms (window 2000 ms)"), "{sub1}"); + + let sub2 = submitter_summary_line(&snap2, &snap1, 2000).unwrap(); + assert!(sub2.contains("0 blocking fence waits in 400 ms"), "{sub2}"); + assert!(sub2.contains("0 GPU-view tile asks in 50 ms"), "{sub2}"); + assert!(sub2.ends_with("; completed 400 ms (window 2000 ms)"), "{sub2}"); + + let all2 = all_threads_summary_parts(&snap2, &snap1); + assert!(all2.iter().any(|s| s == "0 blocking fence waits in 400 ms"), "{all2:?}"); + assert!(all2.iter().any(|s| s == "0 GPU-view tile asks in 50 ms"), "{all2:?}"); + } + + #[test] + fn submitter_counter_pair_snapshots_are_coherent_under_concurrent_writes() { + use std::sync::Arc; + const N: u64 = 50_000; + const STEP_NS: u64 = 10_000_000; // 10_000 us per call + let pair = Arc::new(CounterPair::new()); + let writer_pair = Arc::clone(&pair); + let writer = std::thread::spawn(move || { + for _ in 0..N { + writer_pair.add_single_writer(STEP_NS); + } + }); + let mut samples = 0u64; + loop { + let (count, us) = pair.load_coherent(); + assert_eq!(us, count * (STEP_NS / 1000), "torn snapshot observed: count={count}, us={us}"); + samples += 1; + if count == N { + break; + } + } + writer.join().unwrap(); + assert!(samples >= 1); + assert_eq!(pair.load_coherent(), (N, N * (STEP_NS / 1000))); + } + + #[test] + fn submitter_summary_describes_straddling_completed_call_without_untimed_claim() { + // Synthetic 2.5 s blocking fence wait starting at t=0.0 s and completing at t=2.5 s + // across two 2.0 s activity ticks: tick 1 sees no completed calls; tick 2 reports the + // completed 2500 ms call alongside the 2000 ms window without claiming "0 ms untimed". + let snap0 = [(0u64, 0u64); CALLS.len()]; + let tick1_snap = snap0; + assert_eq!(submitter_summary_line(&tick1_snap, &snap0, 2000), None); + + let mut tick2_snap = tick1_snap; + tick2_snap[Call::FenceWait as usize] = (1, 2_500_000); + let tick2 = submitter_summary_line(&tick2_snap, &tick1_snap, 2000).unwrap(); + assert_eq!(tick2, "1 blocking fence waits in 2500 ms; completed 2500 ms (window 2000 ms)"); + assert!(!tick2.contains("untimed"), "{tick2}"); + } } diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b8206dea5c..72db017a88 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -665,6 +665,7 @@ fn put(list: *mut c_void, i: u32, offset: u32, bytes: &[u8]) { /// #34 SetGraphicsRootSignature (PC #30) pub(super) unsafe extern "system" fn set_graphics_root_signature(this: *mut c_void, rs: *mut c_void) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); let _c = super::capture::op(this, || Some(super::capture::Op::RootSig { gfx: true, rs: rs as usize })); let layout = console_list::layout_of(rs as usize); with(this, |s| { @@ -676,16 +677,19 @@ pub(super) unsafe extern "system" fn set_graphics_root_signature(this: *mut c_vo /// #36 SetGraphicsRootDescriptorTable (PC #32) pub(super) unsafe extern "system" fn set_graphics_root_table(this: *mut c_void, i: u32, handle: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); put_in(this, true, i, 0, &(handle as u32).to_le_bytes()); } /// #38 SetGraphicsRoot32BitConstant (PC #34) pub(super) unsafe extern "system" fn set_graphics_root_constant(this: *mut c_void, i: u32, value: u32, offset: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); put_in(this, true, i, offset, &value.to_le_bytes()); } /// #40 SetGraphicsRoot32BitConstants (PC #36) pub(super) unsafe extern "system" fn set_graphics_root_constants(this: *mut c_void, i: u32, n: u32, data: *const u32, offset: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); if !data.is_null() { let bytes: Vec = std::slice::from_raw_parts(data, n as usize).iter().flat_map(|w| w.to_le_bytes()).collect(); put_in(this, true, i, offset, &bytes); @@ -695,6 +699,7 @@ pub(super) unsafe extern "system" fn set_graphics_root_constants(this: *mut c_vo /// #42 / #44 / #46 SetGraphicsRoot{ConstantBuffer,ShaderResource,UnorderedAccess}View (PC #38 / /// #40 / #42) pub(super) unsafe extern "system" fn set_graphics_root_descriptor(this: *mut c_void, i: u32, va: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); note_address(va); put_in(this, true, i, 0, &va.to_le_bytes()); } @@ -2033,6 +2038,7 @@ fn note_no_root_signature() { } pub(super) unsafe extern "system" fn dispatch_mesh(this: *mut c_void, x: u32, y: u32, z: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::DrawRecord); let _c = capture_work(this, || super::capture::WorkKind::DispatchMesh([x, y, z]), true); type PcDm = unsafe extern "system" fn(*mut c_void, u32, u32, u32); let pc_dm: PcDm = std::mem::transmute(super::real::orig_fn(this, 79)); @@ -2134,6 +2140,13 @@ unsafe fn mesh_work(this: *mut c_void, outcome: usize) { /// translated pipeline, on XCL's ABI with the kept root arguments (signatures that change root /// arguments themselves cannot be – skipped, logged) pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: usize, max: u32, args: usize, args_off: u64, count_buf: usize, count_off: u64) { + let _t = super::look_stats::deep().then(|| { + crate::hle::api_cost::time(if signature_dispatches(sig) { + crate::hle::api_cost::Call::DispatchRecord + } else { + crate::hle::api_cost::Call::DrawRecord + }) + }); type F = unsafe extern "system" fn(*mut c_void, usize, u32, usize, u64, usize, u64); let pc: F = std::mem::transmute(super::real::orig_fn(this, 59)); let dispatches = super::capture::on() && signature_dispatches(sig); @@ -2555,6 +2568,7 @@ fn cpu_rect_expansion(pso: usize) -> bool { /// #12 DrawInstanced pub(super) unsafe extern "system" fn draw_instanced(this: *mut c_void, vertices: u32, instances: u32, first_vertex: u32, first_instance: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::DrawRecord); let _c = capture_work(this, || super::capture::WorkKind::Draw { vertices, instances, first_vertex, first_instance }, true); DRAW_ARGS.with(|d| d.set((false, vertices, instances, first_vertex, 0))); if let Some(list) = graphics_bound(this) { @@ -2574,6 +2588,7 @@ thread_local! { /// #13 DrawIndexedInstanced pub(super) unsafe extern "system" fn draw_indexed_instanced(this: *mut c_void, indices: u32, instances: u32, first_index: u32, base_vertex: i32, first_instance: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::DrawRecord); let _c = capture_work(this, || super::capture::WorkKind::DrawIndexed { indices, instances, first_index, base_vertex, first_instance }, true); if let Some(list) = prepare_indexed_draw(this, indices, instances, first_index, base_vertex) { super::list_x::sync_title_buffers(this, Some((first_index, indices, base_vertex)), 0, 0, if first_instance != 0 { instances.max(2) } else { instances }); @@ -2982,6 +2997,7 @@ unsafe fn root_args_summary(this: *mut c_void, buffer_words: usize) -> String { /// #33 SetComputeRootSignature (PC #29) pub(super) unsafe extern "system" fn set_compute_root_signature(this: *mut c_void, rs: *mut c_void) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); let _c = super::capture::op(this, || Some(super::capture::Op::RootSig { gfx: false, rs: rs as usize })); let layout = console_list::layout_of(rs as usize); with(this, |s| { @@ -2992,16 +3008,19 @@ pub(super) unsafe extern "system" fn set_compute_root_signature(this: *mut c_voi /// #35 SetComputeRootDescriptorTable (PC #31): the table's console address, 32 bits pub(super) unsafe extern "system" fn set_compute_root_table(this: *mut c_void, i: u32, handle: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); put(this, i, 0, &(handle as u32).to_le_bytes()); } /// #37 SetComputeRoot32BitConstant (PC #33) pub(super) unsafe extern "system" fn set_compute_root_constant(this: *mut c_void, i: u32, value: u32, offset: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); put(this, i, offset, &value.to_le_bytes()); } /// #39 SetComputeRoot32BitConstants (PC #35) pub(super) unsafe extern "system" fn set_compute_root_constants(this: *mut c_void, i: u32, n: u32, data: *const u32, offset: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); if !data.is_null() { let bytes: Vec = std::slice::from_raw_parts(data, n as usize).iter().flat_map(|w| w.to_le_bytes()).collect(); put(this, i, offset, &bytes); @@ -3011,6 +3030,7 @@ pub(super) unsafe extern "system" fn set_compute_root_constants(this: *mut c_voi /// #41 / #43 / #45 SetComputeRoot{ConstantBuffer,ShaderResource,UnorderedAccess}View (PC #37 / /// #39 / #41): the 64-bit GPU address pub(super) unsafe extern "system" fn set_compute_root_descriptor(this: *mut c_void, i: u32, va: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); note_address(va); put(this, i, 0, &va.to_le_bytes()); } @@ -3019,6 +3039,7 @@ pub(super) unsafe extern "system" fn set_compute_root_descriptor(this: *mut c_vo /// draws on the list, a two-pass pipeline is bound in the run, between the mesh passes where it /// came, `on_pipeline`; another records the run first) pub(super) unsafe extern "system" fn set_pipeline_state(this: *mut c_void, pso: *mut c_void) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); let deferring = DEFERRING_LISTS.load(Ordering::Relaxed) != 0 && DEFERRING.contains(this as usize); match on_pipeline(deferring, deferring && super::real::two_pass_of(pso as usize).is_some()) { OnPipeline::Queue => { @@ -3085,6 +3106,7 @@ fn note_thread_dimensions(threads: [u32; 3], groups: [u32; 3], shape: [u32; 3]) /// `Dispatch` / `DispatchX(x, y, z, flags)`. pub(super) unsafe fn dispatch_flags(this: *mut c_void, x: u32, y: u32, z: u32, flags: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::DispatchRecord); let pso = with(this, |s| s.pso); let _c = capture_work(this, || captured_dispatch(pso, [x, y, z], flags), false); // (recorded at the submit through this call, with everything below: counted there) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index b5e0e98a97..b5c4edb010 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2549,6 +2549,7 @@ pub(super) unsafe fn execute_with_waits(lists: &[*mut c_void], submit: impl FnMu /// command list #60 CopyMemoryToMemoryX(destination, source, size, flags) pub(super) unsafe extern "system" fn copy_memory_x(this: *mut c_void, dst: u64, src: u64, size: u64, _flags: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); static N: AtomicU64 = AtomicU64::new(0); const W: &str = "CopyMemoryToMemoryX"; let refused = size == 0 || !size.is_multiple_of(4); @@ -2845,16 +2846,19 @@ fn pattern(bytes: &[u8]) -> [u8; 16] { /// command list #61 FillMemoryWith32BitValueX(address, size, value) pub(super) unsafe extern "system" fn fill32_x(this: *mut c_void, va: u64, size: u64, value: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); fill(this, va, size, pattern(&value.to_le_bytes()), 4, "FillMemoryWith32BitValueX"); } /// command list #62 FillMemoryWith64BitValueX(address, size, value) pub(super) unsafe extern "system" fn fill64_x(this: *mut c_void, va: u64, size: u64, value: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); fill(this, va, size, pattern(&value.to_le_bytes()), 8, "FillMemoryWith64BitValueX"); } /// command list #63 FillMemoryWith128BitValueX(address, size, value*) pub(super) unsafe extern "system" fn fill128_x(this: *mut c_void, va: u64, size: u64, value: *const [u8; 16]) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); if !value.is_null() { fill(this, va, size, value.read_unaligned(), 16, "FillMemoryWith128BitValueX"); } @@ -3018,12 +3022,14 @@ unsafe fn timestamp(list: *mut c_void, va: u64, what: &str) { /// # Safety /// `this` must be a valid adopted `ID3D12GraphicsCommandList` pointer. pub unsafe extern "system" fn write32_bottom_x(this: *mut c_void, va: u64, value: u32, _flush: u32, flags: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); record_write_trace(this, va, value as u64, flags, "W32B"); write(this, va, &[value], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, "Write32BitValueBottomOfPipeX"); } /// command list #70 Write64BitValueBottomOfPipeX(address, value, flush, flags) pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64, value: u64, _flush: u32, flags: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); const W: &str = "Write64BitValueBottomOfPipeX"; record_write_trace(this, va, value, flags, "W64B"); if flags & BOTTOM_TIMESTAMPS != 0 { @@ -3039,6 +3045,7 @@ pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64 /// NVIDIA removes the device). The new recording goes onto a spare PC list of the wrapper whose /// submits are done, or a new one, reset like `primary` would be. None: it could not be. pub(super) unsafe fn reset_stand_in(wrapper: *mut c_void, primary: *mut c_void, alloc: usize, pso: usize) -> Option { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListLifecycle); let ty = list_type(primary); if ty == D3D12_COMMAND_LIST_TYPE_BUNDLE || alloc == 0 { return None; @@ -3060,7 +3067,7 @@ pub(super) unsafe fn reset_stand_in(wrapper: *mut c_void, primary: *mut c_void, made } }; - if reset_pc(spare, alloc, pso) < 0 { + if reset_pc_inner(spare, alloc, pso) < 0 { super::title_list::put_spare(wrapper, spare); return None; } @@ -3560,9 +3567,7 @@ pub(super) unsafe extern "system" fn dispatch_x(this: *mut c_void, x: u32, y: u3 super::bind::dispatch_flags(this, x, y, z, flags); } -/// command list #146 console compute/UAV barrier (used by `XboxBvhBuilder.dll` between BVH -/// build passes): emits a global UAV barrier so UMA writes become visible to the next pass -pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, _n: u32, _barriers: *const c_void) { +unsafe fn emit_uav_barrier_pc(this: *mut c_void) { let b = D3D12_RESOURCE_BARRIER { Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, @@ -3573,12 +3578,19 @@ pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, _n: u32, _ pc::(this, PC_RESOURCE_BARRIER)(this, 1, &b); } +/// command list #146 console compute/UAV barrier (used by `XboxBvhBuilder.dll` between BVH +/// build passes): emits a global UAV barrier so UMA writes become visible to the next pass +pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, _n: u32, _barriers: *const c_void) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListBarrier); + emit_uav_barrier_pc(this); +} + /// A UAV barrier for all resources on `list`, where a list takes one (a copy list and a bundle /// do not). unsafe fn flush_barrier(list: *mut c_void) { let ty = list_type(list); if ty == D3D12_COMMAND_LIST_TYPE_DIRECT || ty == D3D12_COMMAND_LIST_TYPE_COMPUTE { - uav_barrier_x(list, 0, std::ptr::null()); + emit_uav_barrier_pc(list); } } @@ -3589,6 +3601,7 @@ unsafe fn flush_barrier(list: *mut c_void) { /// and answered with nothing its passes had no order there. The PC has no ranges of caches: a UAV /// barrier for everything. pub(super) unsafe extern "system" fn flush_cache_range_x(this: *mut c_void, flags: u32, wait: u32, _va: u64, bytes: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListBarrier); if (flags != 0 || wait != 0) && bytes != 0 { flush_barrier(this); } @@ -3596,6 +3609,7 @@ pub(super) unsafe extern "system" fn flush_cache_range_x(this: *mut c_void, flag /// 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); flush_barrier(this); } @@ -4470,6 +4484,7 @@ pub(super) unsafe fn ensure_view_heap(this: *mut c_void) { /// console #32 SetDescriptorHeaps (PC #28) pub(super) unsafe extern "system" fn set_descriptor_heaps_x(this: *mut c_void, n: u32, heaps: *const usize) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); const PC_SET_DESCRIPTOR_HEAPS: usize = 28; let f = pc::(this, PC_SET_DESCRIPTOR_HEAPS); let _c = super::capture::op(this, || { @@ -4515,6 +4530,7 @@ const PC_IA_SET_VERTEX_BUFFERS: usize = 44; /// command list #48 IASetVertexBuffers(start slot, count, views) (PC #44): the addresses are /// the title's (its memory: the GPU address of that memory for the PC) pub(super) unsafe extern "system" fn ia_set_vertex_buffers_x(this: *mut c_void, start: u32, n: u32, views: *const D3D12_VERTEX_BUFFER_VIEW) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); let f = pc::(this, PC_IA_SET_VERTEX_BUFFERS); if views.is_null() || n == 0 { return f(this, start, n, views); @@ -4561,6 +4577,7 @@ pub(super) unsafe extern "system" fn ia_set_vertex_buffers_x(this: *mut c_void, /// command list #47 IASetIndexBuffer(view) (PC #43): the address is the title's pub(super) unsafe extern "system" fn ia_set_index_buffer_x(this: *mut c_void, view: *const D3D12_INDEX_BUFFER_VIEW) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); let mut g = recording(this as usize).lock().unwrap(); let s = g.get_or_insert_with(HashMap::new).entry(this as usize).or_default(); if view.is_null() { @@ -4582,6 +4599,7 @@ pub(super) unsafe extern "system" fn ia_set_index_buffer_x(this: *mut c_void, vi /// command list #81 DrawIndexedX(index address, index format, index count, flags): one /// instance of `count` indices read from that address; the bound index buffer stays pub(super) unsafe extern "system" fn draw_indexed_x(this: *mut c_void, va: u64, format: DXGI_FORMAT, count: u32, _flags: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::DrawRecord); let _c = super::bind::capture_work(this, || super::capture::WorkKind::DrawIndexedX { va, format: format.0, indices: count }, true); static N: AtomicU64 = AtomicU64::new(0); let stride = match format { @@ -4646,6 +4664,11 @@ pub unsafe extern "system" fn reset_x(this: *mut c_void, alloc: usize, pso: usiz /// # Safety /// `this` must be a valid adopted `ID3D12GraphicsCommandList` pointer. pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i32 { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListLifecycle); + reset_pc_inner(this, alloc, pso) +} + +unsafe fn reset_pc_inner(this: *mut c_void, alloc: usize, pso: usize) -> i32 { // A failed native Reset leaves the old recording alive. Do not discard its tracked state or // transfer its allocations until the driver accepts the new recording. let ext = *(this as *const usize); @@ -4709,6 +4732,7 @@ const PC_STATES: u32 = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80 | 0x100 /// PC ones – state bits the PC does not know are dropped (logged: the console's own), a /// transition left with nothing to do goes, aliasing barriers name placed textures pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u32, barriers: *const [u32; 8]) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListBarrier); static N: AtomicU64 = AtomicU64::new(0); if barriers.is_null() || n == 0 { return; @@ -4863,6 +4887,7 @@ unsafe fn is_buffer(res: usize) -> bool { /// DMA list #9 Close: as on the PC; a failure is logged (the title stops on it) pub(super) unsafe extern "system" fn dma_close_x(this: *mut c_void) -> i32 { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListLifecycle); let ext = *(this as *const usize); let hr = pc:: i32>(this, 9)(this); real::preserve_vtable(this, ext); @@ -4900,11 +4925,7 @@ fn note_gpu_write(what: &str, va: usize, n: u64) { } } -pub(super) unsafe extern "system" fn copy_buffer_region_x(this: *mut c_void, dst: usize, dst_off: u64, src: usize, src_off: u64, n: u64) { - let _c = super::capture::op(this, || { - use super::capture as c; - Some(c::Op::Copy { kind: "buffer_region", dst: c::buffer(dst, dst_off), src: c::buffer(src, src_off), at: [0; 3], bx: None, bytes: Some(n), refused: false }) - }); +unsafe fn copy_buffer_region_inner(this: *mut c_void, dst: usize, dst_off: u64, src: usize, src_off: u64, n: u64) { if super::present::trace_frame() { if let Some(a) = real::title_address(dst) { note_gpu_write("CopyBufferRegion", a + dst_off as usize, n); @@ -4914,6 +4935,15 @@ pub(super) unsafe extern "system" fn copy_buffer_region_x(this: *mut c_void, dst pc::(this, PC_COPY_BUFFER_REGION)(this, dst, dst_off + real::buffer_delta(dst), src, src_off + real::buffer_delta(src), n) } +pub(super) unsafe extern "system" fn copy_buffer_region_x(this: *mut c_void, dst: usize, dst_off: u64, src: usize, src_off: u64, n: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); + let _c = super::capture::op(this, || { + use super::capture as c; + Some(c::Op::Copy { kind: "buffer_region", dst: c::buffer(dst, dst_off), src: c::buffer(src, src_off), at: [0; 3], bx: None, bytes: Some(n), refused: false }) + }); + copy_buffer_region_inner(this, dst, dst_off, src, src_off, n); +} + /// `XCL_TRACE_FRAME` with a watched target (`present::watch_rt`): copies out of it – that /// object, or another at its title address (the first 16) unsafe fn trace_watched_copy(what: &str, dst: usize, src: usize) { @@ -4940,6 +4970,7 @@ unsafe fn trace_watched_copy(what: &str, dst: usize, src: usize) { /// CopyTextureRegion(dst, x, y, z, src, box) (PC #16; DMA list #12) pub(super) unsafe extern "system" fn copy_texture_region_x(this: *mut c_void, dst: *const D3D12_TEXTURE_COPY_LOCATION, x: u32, y: u32, z: u32, src: *const D3D12_TEXTURE_COPY_LOCATION, bx: *const D3D12_BOX) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); let _c = super::capture::op(this, || { use super::capture as c; let b = bx.as_ref().map(|b| [b.left, b.top, b.front, b.right, b.bottom, b.back]); @@ -5053,6 +5084,7 @@ const PC_CLEAR_DEPTH_STENCIL_VIEW: usize = 47; /// command list #51 ClearDepthStencilView(dsv, flags, depth, stencil, rects, rect list) (PC #47) pub(super) unsafe extern "system" fn clear_depth_stencil_x(this: *mut c_void, dsv: usize, flags: u32, depth: f32, stencil: u8, n: u32, rects: *const windows::Win32::Foundation::RECT) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); let _c = super::capture::op(this, || Some(super::capture::clear("dsv", dsv as u64, None, [depth.to_bits(), stencil as u32, 0, 0], flags, n))); if super::present::trace_frame() { static N: AtomicU64 = AtomicU64::new(0); @@ -5112,6 +5144,7 @@ pub(super) fn uav_clear_resource(res: usize, gpu: Option().as_ref().copied().unwrap_or_default(); Some(super::capture::clear(if slot == PC_CLEAR_UAV_UINT { "uav_uint" } else { "uav_float" }, gpu, Some(res), v, 0, n)) @@ -5171,6 +5204,7 @@ unsafe fn trace_resolve(what: &str, dst: usize, src: usize, format: i32, extra: /// command list #19 ResolveSubresource(dst, dst sub, src, src sub, format) (PC #19) pub(super) unsafe extern "system" fn resolve_subresource_x(this: *mut c_void, dst: usize, dst_sub: u32, src: usize, src_sub: u32, format: i32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); let _c = super::capture::op(this, || { use super::capture as c; Some(c::Op::Resolve { dst: c::describe(dst), dst_sub, src: c::describe(src), src_sub, format, region: None }) @@ -5185,6 +5219,7 @@ pub(super) unsafe extern "system" fn resolve_subresource_x(this: *mut c_void, ds /// mode) (PC #64) #[allow(clippy::too_many_arguments)] pub(super) unsafe extern "system" fn resolve_subresource_region_x(this: *mut c_void, dst: usize, dst_sub: u32, x: u32, y: u32, src: usize, src_sub: u32, rect: *const windows::Win32::Foundation::RECT, format: i32, mode: i32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); let _c = super::capture::op(this, || { use super::capture as c; let r = rect.as_ref().map(|r| [r.left, r.top, r.right, r.bottom]); @@ -5201,6 +5236,7 @@ pub(super) unsafe extern "system" fn resolve_subresource_region_x(this: *mut c_v /// CopyResource(dst, src) (PC #17; DMA list #13): buffers placed below their console address /// are copied as their own bytes (the PC resources also hold what lies before them) pub(super) unsafe extern "system" fn copy_resource_x(this: *mut c_void, dst: usize, src: usize) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListCopy); let _c = super::capture::op(this, || { use super::capture as c; Some(c::Op::Copy { kind: "resource", dst: c::buffer(dst, 0), src: c::buffer(src, 0), at: [0; 3], bx: None, bytes: None, refused: false }) @@ -5219,7 +5255,7 @@ pub(super) unsafe extern "system" fn copy_resource_x(this: *mut c_void, dst: usi return pc::(this, PC_COPY_RESOURCE)(this, dst, src); } let n = real::console_buffer(dst).or(real::console_buffer(src)).map_or(0, |b| b.1 as u64); - copy_buffer_region_x(this, dst, 0, src, 0, n); + copy_buffer_region_inner(this, dst, 0, src, 0, n); } /// command list #28 BeginQuery(heap, type, index) (PC #52) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 4fc883bf4e..f6a1f2472b 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2257,6 +2257,7 @@ fn note_buffer_view() { /// shaders read buffers through `mem`. One in a GPU view of the title's memory asks for its /// tiles (`view_want`). unsafe extern "system" fn dev_create_cbv_x(this: *mut c_void, desc: *const D3D12_CONSTANT_BUFFER_VIEW_DESC, cpu: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let Some(slot) = super::console_heap::slot(cpu).filter(|s| !s.sampler) else { let f: unsafe extern "system" fn(*mut c_void, *const D3D12_CONSTANT_BUFFER_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 17)); return f(this, desc, cpu); @@ -2281,6 +2282,7 @@ fn cbv_at(u: Option<&crate::gpu::uma::Uma>, va: u64, size: u32) -> Option<[u32; /// #22 CreateSampler: the PC sampler at the slot's index, the S# in XCL ABI form (word 3 /// [11:0] = sampler heap index; the state lives in the PC sampler). pub(super) unsafe extern "system" fn dev_create_sampler_x(this: *mut c_void, desc: *const D3D12_SAMPLER_DESC, cpu: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let f: unsafe extern "system" fn(*mut c_void, *const D3D12_SAMPLER_DESC, u64) = std::mem::transmute(orig_fn(this, 22)); let Some(slot) = super::console_heap::slot(cpu).filter(|s| s.sampler) else { return f(this, desc, cpu) }; f(this, desc, slot.pc_srv); @@ -2330,6 +2332,7 @@ fn note_mixed_copy() { /// #24 CopyDescriptorsSimple(n, dst, src, type) unsafe extern "system" fn dev_copy_descriptors_simple_x(this: *mut c_void, n: u32, dst: u64, src: u64, ty: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let ty = pc_heap_type(ty); note_copy_call("CopyDescriptorsSimple", n, dst, src); if copy_console_slots(this, n, dst, src, ty) { @@ -2349,6 +2352,7 @@ unsafe extern "system" fn dev_copy_descriptors_simple_x(this: *mut c_void, n: u3 /// #23 CopyDescriptors(dst ranges, src ranges, type): range by range over console slots. #[allow(clippy::too_many_arguments)] unsafe extern "system" fn dev_copy_descriptors_x(this: *mut c_void, nd: u32, dsts: *const u64, dsizes: *const u32, ns: u32, srcs: *const u64, ssizes: *const u32, ty: u32) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let ty = pc_heap_type(ty); note_copy_call("CopyDescriptors", nd, if dsts.is_null() { 0 } else { *dsts }, if srcs.is_null() { 0 } else { *srcs }); use super::console_heap::{slot, SAMPLER_SLOT, SLOT}; @@ -2439,6 +2443,17 @@ unsafe extern "system" fn dev_create_uav_checked_x( counter: *mut c_void, desc: *const D3D12_UNORDERED_ACCESS_VIEW_DESC, cpu: u64, +) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); + dev_create_uav_checked_inner(this, res, counter, desc, cpu); +} + +unsafe fn dev_create_uav_checked_inner( + this: *mut c_void, + res: *mut c_void, + counter: *mut c_void, + desc: *const D3D12_UNORDERED_ACCESS_VIEW_DESC, + cpu: u64, ) { if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { let rd = r.GetDesc(); @@ -2484,6 +2499,7 @@ unsafe extern "system" fn dev_create_uav_checked_x( /// #21 CreateDepthStencilView: the console's view formats as the PC's (`pc_view_format`). unsafe extern "system" fn dev_create_dsv_x(this: *mut c_void, res: *mut c_void, desc: *const D3D12_DEPTH_STENCIL_VIEW_DESC, cpu: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_DEPTH_STENCIL_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 21)); let mut copy = desc.as_ref().copied(); if let Some(c) = copy.as_mut() { @@ -2611,6 +2627,11 @@ fn empty_bvh() -> Option { } unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void, desc: *const D3D12_SHADER_RESOURCE_VIEW_DESC, cpu: u64) { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); + dev_create_srv_inner(this, res, desc, cpu); +} + +unsafe fn dev_create_srv_inner(this: *mut c_void, res: *mut c_void, desc: *const D3D12_SHADER_RESOURCE_VIEW_DESC, cpu: u64) { let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 18)); let mut copy = desc.as_ref().copied(); if let Some(c) = copy.as_mut() { @@ -2794,6 +2815,7 @@ unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u let va = (view.add(0x28) as *const u64).read_unaligned(); let v = (view as *const D3D12_SHADER_RESOURCE_VIEW_DESC).read_unaligned(); if v.ViewDimension == D3D12_SRV_DIMENSION_BUFFER { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let Some(slot) = super::console_heap::slot(cpu) else { return }; let b = &v.Anonymous.Buffer; let raw = b.Flags.0 & D3D12_BUFFER_SRV_FLAG_RAW.0 != 0; @@ -2801,7 +2823,9 @@ unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u super::view_want::want(super::view_want::Path::ViewAt, at, len); return super::console_heap::write(&slot, &words); } - dev_create_srv_x(this, resource_at(this, rdesc, va), &v, cpu); + let r = resource_at(this, rdesc, va); + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); + dev_create_srv_inner(this, r, &v, cpu); } /// device #71: the same for an unordered access view ({ the PC's UAV desc; GPU address @0x28; @@ -2813,6 +2837,7 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u let va = (view.add(0x28) as *const u64).read_unaligned(); let v = (view as *const D3D12_UNORDERED_ACCESS_VIEW_DESC).read_unaligned(); if v.ViewDimension == D3D12_UAV_DIMENSION_BUFFER { + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); let Some(slot) = super::console_heap::slot(cpu) else { return }; let b = &v.Anonymous.Buffer; let raw = b.Flags.0 & D3D12_BUFFER_UAV_FLAG_RAW.0 != 0; @@ -2820,7 +2845,9 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u super::view_want::want(super::view_want::Path::ViewAt, at, len); return super::console_heap::write(&slot, &words); } - dev_create_uav_x(this, resource_at(this, rdesc, va), std::ptr::null_mut(), &v, cpu); + let r = resource_at(this, rdesc, va); + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewDescriptor); + dev_create_uav_x(this, r, std::ptr::null_mut(), &v, cpu); } /// V# of `n` elements from element `first` of the buffer at `va`, and the bytes it covers @@ -3031,7 +3058,7 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, // a UAV of the stencil plane of a depth buffer with unordered access: a view of its twin // (`depth_uav`), made the way a UAV of a texture with unordered access is let null_why = match super::depth_uav::resolve_uav(this, res, rd_opt.as_ref(), desc.as_ref()) { - super::depth_uav::Resolve::Twin(twin, view) => return dev_create_uav_checked_x(this, twin, std::ptr::null_mut(), &view, cpu), + super::depth_uav::Resolve::Twin(twin, view) => return dev_create_uav_checked_inner(this, twin, std::ptr::null_mut(), &view, cpu), super::depth_uav::Resolve::Null(why) => Some(why), super::depth_uav::Resolve::NotOurs => None, }; @@ -3409,6 +3436,10 @@ macro_rules! as_usize { /// command list's vtable pointer on Close; adopt the runtime's new state vtable so /// subsequent Reset and console calls dispatch through the current native table. unsafe extern "system" fn cl_close(this: *mut c_void) -> HRESULT { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::ListLifecycle, + ); super::bind::flush_two_pass(this, super::bind::RunEnd::Close); super::list_x::before_close(this); super::gpu_times::before_close(this); @@ -3425,6 +3456,10 @@ unsafe extern "system" fn cl_close(this: *mut c_void) -> HRESULT { /// Reset (PC path): re-adopt the runtime's new open-state vtable if Reset replaced it. unsafe extern "system" fn cl_reset(this: *mut c_void, allocator: *mut c_void, initial_state: *mut c_void) -> HRESULT { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::ListLifecycle, + ); let ext = *(this as *const usize); let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void) -> HRESULT = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "Reset"))); @@ -8429,6 +8464,10 @@ unsafe extern "system" fn dev_create_root_signature(this: *mut c_void, mask: u32 // --------------------------------------------------------------------------------------- unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, desc: *const c_void, handle: usize) { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::ViewDescriptor, + ); type F = unsafe extern "system" fn(*mut c_void, *mut c_void, *const c_void, usize); let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "CreateRenderTargetView"))); // a view in a console-only format, or of a target made R8G8B8A8 (`device_x::pc_resource_desc`) @@ -8458,6 +8497,10 @@ unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, de /// ClearRenderTargetView: as on the PC (`XCL_TRACE_FRAME`: the colours logged) unsafe extern "system" fn cl_clear_rtv(this: *mut c_void, rtv: usize, color: *const [f32; 4], n: u32, rects: *const c_void) { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::ListCopy, + ); type F = unsafe extern "system" fn(*mut c_void, usize, *const [f32; 4], u32, *const c_void); let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "ClearRenderTargetView"))); let _c = super::capture::op(this, || Some(super::capture::clear("rtv", rtv as u64, None, color.as_ref().map_or([0; 4], |c| c.map(f32::to_bits)), 0, n))); @@ -8479,6 +8522,10 @@ unsafe extern "system" fn cl_clear_rtv(this: *mut c_void, rtv: usize, color: *co } unsafe extern "system" fn cl_om_set_render_targets(this: *mut c_void, n: u32, rtvs: *const usize, single: i32, dsv: *const usize) { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::BindState, + ); type F = unsafe extern "system" fn(*mut c_void, u32, *const usize, i32, *const usize); let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "OMSetRenderTargets"))); f(this, n, rtvs, single, dsv); @@ -8610,6 +8657,10 @@ pub(super) unsafe fn reset_pc_allocator(raw: *mut c_void) -> HRESULT { } unsafe extern "system" fn alloc_reset(this: *mut c_void) -> HRESULT { + let _t = crate::hle::api_cost::time_if( + super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::ListLifecycle, + ); // (lists may still hold recordings on it that the title submits again: `alloc_pool`, which // also gives back what recordings on its PC allocators used once the GPU is done with them) let hr = match super::alloc_pool::title_reset(this) { @@ -8936,7 +8987,13 @@ unsafe extern "system" fn fence_set_event_on_completion(this: *mut c_void, value use windows_sys::Win32::System::Threading::{CreateEventW, RegisterWaitForSingleObject, UnregisterWaitEx, INFINITE, WT_EXECUTEONLYONCE}; let f: unsafe extern "system" fn(*mut c_void, u64, *mut c_void) -> HRESULT = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12Fence", "SetEventOnCompletion"))); if event.is_null() || !super::sparse::readbacks_async() { - let hr = f(this, value, event); + let hr = { + let _t = crate::hle::api_cost::time_if( + event.is_null() && super::look_stats::deep() && !super::title_textures::is_own_work() && !super::present::presenting(), + crate::hle::api_cost::Call::FenceWait, + ); + f(this, value, event) + }; if event.is_null() { super::sparse::settle_readbacks_try(); } diff --git a/crates/xcl/src/hle/d3d12x/view_want.rs b/crates/xcl/src/hle/d3d12x/view_want.rs index 48f062e274..4f0893200a 100644 --- a/crates/xcl/src/hle/d3d12x/view_want.rs +++ b/crates/xcl/src/hle/d3d12x/view_want.rs @@ -123,6 +123,7 @@ pub fn want(path: Path, va: u64, len: u64) -> bool { if va == 0 || len == 0 || !is_view(va) { return false; } + let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ViewWant); CALLS[path as usize].fetch_add(1, Ordering::Relaxed); let len = reach(path, va, len); if !asked_lately(va, len, generation()) { diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 6fb04afbdb..eb355f20fa 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -104,8 +104,11 @@ pub fn host_main(boot_path: &Path) -> i32 { let mut own_cost = crate::hle::api_cost::submitter_totals(); let mut views = crate::hle::d3d12x::view_want::totals(); let mut tile_calls = crate::hle::d3d12x::sparse::tile_calls(); + let mut last_tick = std::time::Instant::now(); loop { std::thread::sleep(std::time::Duration::from_secs(2)); + let window_ms = last_tick.elapsed().as_millis() as u64; + last_tick = std::time::Instant::now(); let now = snap(); let d: Vec = now.iter().zip(last).map(|(a, b)| a - b).collect(); crate::xlog_info!( @@ -113,16 +116,8 @@ pub fn host_main(boot_path: &Path) -> i32 { d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10] ); // the time the title's threads spent in some of XCL's calls - let line = |spent: &[(u64, u64)], before: &[(u64, u64)]| -> Vec { - crate::hle::api_cost::CALLS - .iter() - .zip(spent.iter().zip(before)) - .filter(|(_, (n, o))| n.0 > o.0) - .map(|(c, (n, o))| format!("{} {} in {} ms", n.0 - o.0, c.1, (n.1 - o.1) / 1000)) - .collect() - }; let spent = crate::hle::api_cost::totals(); - let calls = line(&spent, &cost); + let calls = crate::hle::api_cost::all_threads_summary_parts(&spent, &cost); cost = spent; if !calls.is_empty() { crate::xlog_info!("calls (2 s, all threads): {}", calls.join(", ")); @@ -130,11 +125,10 @@ pub fn host_main(boot_path: &Path) -> i32 { // `XCL_LOOK_STATS=2`: those of the thread that submits, its submits and presents // with them: the time of its frames, added up let spent = crate::hle::api_cost::submitter_totals(); - let calls = line(&spent, &own_cost); - own_cost = spent; - if !calls.is_empty() { - crate::xlog_info!("calls of the submitting thread (2 s): {}", calls.join(", ")); + if let Some(summary) = crate::hle::api_cost::submitter_summary_line(&spent, &own_cost, window_ms) { + crate::xlog_info!("calls of the submitting thread (2 s): {summary}"); } + own_cost = spent; // the addresses in its GPU view the title gave the GPU, by the path they took let asked = crate::hle::d3d12x::view_want::totals(); if let Some(line) = crate::hle::d3d12x::view_want::line(&asked, &views) {