From e4c3df141a748cb444df1cc61c1345c5145cdcbf Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 11:28:30 -0800 Subject: [PATCH] D3D12.X: measure texture selection contention Dirty-page selection scans every active texture while holding the texture table, but existing activity reports hid both lock contention and scan cost. Preserve the exact resource-ordered selection and retention semantics while reporting lock wait, hold time, predicate work, matches, and retained uploads through the existing opt-in look sample. Cross-checked debug tests and release all-target builds for x86_64-pc-windows-gnu; Windows runtime tests, GPU tests, and smoke were not run here. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/sparse.rs | 59 ++++++++++++++++-- crates/xcl/src/hle/d3d12x/title_textures.rs | 69 ++++++++++++++------- 2 files changed, 101 insertions(+), 27 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index af4e734729..bd1be3eba4 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -3711,7 +3711,7 @@ pub fn mapped_ranges() -> Vec<(usize, usize)> { s.runs.clone().unwrap_or_default() } -const LOOK_FIELDS: usize = 19; +const LOOK_FIELDS: usize = 25; const ACTIVE_US: usize = 0; const REFRESH_WAIT_US: usize = 1; const WATCH_US: usize = 2; @@ -3731,6 +3731,12 @@ const SUBMIT_GATE_HOLD_US: usize = 15; const READBACK_GATE_WAIT_US: usize = 16; const READBACK_GATE_HOLD_US: usize = 17; const READBACK_GATE_RELEASED_US: usize = 18; +const TEXTURE_SELECT_WAIT_US: usize = 19; +const TEXTURE_SELECT_HOLD_US: usize = 20; +const TEXTURES_VISITED: usize = 21; +const TEXTURE_DIRTY_TESTS: usize = 22; +const TEXTURE_MATCHES: usize = 23; +const TEXTURES_RETAINED: usize = 24; /// One complete `XCL_ACTIVITY` look. It is built without shared updates, then published once so /// the reporting thread cannot split one look across adjacent intervals. @@ -3778,6 +3784,23 @@ pub(super) fn note_watch(sample: &mut LookSample, t: std::time::Duration, pages: sample.add(RANGES, ranges as u64); } +pub(super) fn note_texture_selection( + sample: &mut LookSample, + wait: std::time::Duration, + hold: std::time::Duration, + visited: usize, + dirty_tests: usize, + matches: usize, + retained: usize, +) { + sample.add_time(TEXTURE_SELECT_WAIT_US, wait); + sample.add_time(TEXTURE_SELECT_HOLD_US, hold); + sample.add(TEXTURES_VISITED, visited as u64); + sample.add(TEXTURE_DIRTY_TESTS, dirty_tests as u64); + sample.add(TEXTURE_MATCHES, matches as u64); + sample.add(TEXTURES_RETAINED, retained as u64); +} + pub(super) fn note_look(mut sample: LookSample, active: std::time::Duration, refresh_wait: std::time::Duration) { sample.add_time(ACTIVE_US, active); sample.add_time(REFRESH_WAIT_US, refresh_wait); @@ -3799,18 +3822,22 @@ pub fn look_line(d: [u64; LOOK_FIELDS], secs: u64) -> String { let [ active, refresh_wait, watch, pages, looks, watched, ranges, queue_wait, maintenance, scan, upload, runs, transfer_bytes, upload_wait, submit_gate_wait, submit_gate_hold, readback_gate_wait, - readback_gate_hold, readback_gate_released, + readback_gate_hold, readback_gate_released, texture_select_wait, texture_select_hold, + textures_visited, texture_dirty_tests, texture_matches, textures_retained, ] = d; let ms = |us: u64| us / 1000; let sparse = queue_wait.saturating_add(maintenance).saturating_add(scan).saturating_add(upload).saturating_add(upload_wait); + let texture_select = texture_select_wait.saturating_add(texture_select_hold); format!( - "looks before submits ({secs} s): {looks} looks did {} ms active work ({:.1} % of the time) after {} ms waiting for the refresh turn: write-watch {} ms ({:.1} MB in {:.1} ranges/look, {pages} pages written); sparse queue wait/fence+state/scan/upload/ownership wait {}/{}/{}/{}/{} ms ({runs} runs, {:.1} MB zero/dirty bytes); upload gate submit wait/hold {}/{} ms, readback wait/hold/released wait+reacquire {}/{}/{} ms; other active work {} ms", + "looks before submits ({secs} s): {looks} looks did {} ms active work ({:.1} % of the time) after {} ms waiting for the refresh turn: write-watch {} ms ({:.1} MB in {:.1} ranges/look, {pages} pages written); texture select wait/hold {}/{} ms ({textures_visited} visited, {texture_dirty_tests} dirty-tested, {texture_matches} matched, {textures_retained} retained); sparse queue wait/fence+state/scan/upload/ownership wait {}/{}/{}/{}/{} ms ({runs} runs, {:.1} MB zero/dirty bytes); upload gate submit wait/hold {}/{} ms, readback wait/hold/released wait+reacquire {}/{}/{} ms; other active work {} ms", ms(active), active as f64 / (secs.max(1) * 10_000) as f64, ms(refresh_wait), ms(watch), watched as f64 / looks.max(1) as f64 / (1 << 20) as f64, ranges as f64 / looks.max(1) as f64, + ms(texture_select_wait), + ms(texture_select_hold), ms(queue_wait), ms(maintenance), ms(scan), @@ -3822,7 +3849,7 @@ pub fn look_line(d: [u64; LOOK_FIELDS], secs: u64) -> String { ms(readback_gate_wait), ms(readback_gate_hold), ms(readback_gate_released), - ms(active.saturating_sub(watch.saturating_add(sparse))) + ms(active.saturating_sub(watch.saturating_add(sparse).saturating_add(texture_select))) ) } @@ -4309,11 +4336,18 @@ mod tests { 3_000, 70_000, 12_000, + 7_000, + 13_000, + 24_000, + 8_000, + 1_200, + 1_190, ], 2); assert!(line.contains("240 looks did 830 ms active work (41.5 % of the time) after 120 ms waiting"), "{line}"); assert!(line.contains("write-watch 610 ms (1641.0 MB in 69.0 ranges/look, 4800 pages written)"), "{line}"); + assert!(line.contains("texture select wait/hold 7/13 ms (24000 visited, 8000 dirty-tested, 1200 matched, 1190 retained)"), "{line}"); assert!(line.contains("sparse queue wait/fence+state/scan/upload/ownership wait 10/30/40/40/20 ms (90 runs, 96.0 MB zero/dirty bytes)"), "{line}"); - assert!(line.contains("upload gate submit wait/hold 5/105 ms, readback wait/hold/released wait+reacquire 3/70/12 ms; other active work 80 ms"), "{line}"); + assert!(line.contains("upload gate submit wait/hold 5/105 ms, readback wait/hold/released wait+reacquire 3/70/12 ms; other active work 60 ms"), "{line}"); let idle = look_line([0; LOOK_FIELDS], 2); assert!(idle.contains("0 looks did 0 ms active work (0.0 % of the time) after 0 ms waiting"), "{idle}"); assert!(idle.contains("0.0 MB in 0.0 ranges/look"), "{idle}"); @@ -4324,6 +4358,21 @@ mod tests { assert_eq!(look_delta([0; LOOK_FIELDS], [u64::MAX; LOOK_FIELDS]), [1; LOOK_FIELDS]); } + #[test] + fn texture_selection_metrics_keep_wait_hold_and_counts_distinct() { + let mut sample = LookSample::default(); + note_texture_selection( + &mut sample, + std::time::Duration::from_micros(7), + std::time::Duration::from_micros(13), + 101, + 79, + 11, + 10, + ); + assert_eq!(sample.0[TEXTURE_SELECT_WAIT_US..=TEXTURES_RETAINED], [7, 13, 101, 79, 11, 10]); + } + #[test] fn upload_gate_times_accumulate_into_distinct_submit_and_readback_fields() { let mut sample = [0; LOOK_FIELDS]; diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index e7fdaa7d44..5e0241d4cb 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -1070,35 +1070,60 @@ unsafe fn refresh_once(queue: *mut c_void, mut look: Option<&mut super::sparse:: } // textures made since the last submit and those with a written page; a reference each // while they are uploaded - let todo: Vec<(ID3D12Resource, Upload)> = DIRTY_BUF.with(|dcell| { + let measure_selection = look.is_some(); + let (todo, selection_measurement): (Vec<(ID3D12Resource, Upload)>, _) = DIRTY_BUF.with(|dcell| { let dirty = dcell.borrow(); + let wait_started = measure_selection.then(std::time::Instant::now); let mut t = TEXTURES.lock().unwrap(); - t.by_res - .iter_mut() - .filter(|(_, e)| { - if !e.cpu_backed { - return false; - } - if e.pending { - return true; - } - if e.gpu_written { - return false; + let locked_at = wait_started.map(|_| std::time::Instant::now()); + let (mut visited, mut dirty_tests, mut matches, mut retained) = (0usize, 0usize, 0usize, 0usize); + let mut todo = Vec::new(); + for (&res, e) in &mut t.by_res { + if measure_selection { + visited += 1; + } + if !e.cpu_backed { + continue; + } + let selected = if e.pending { + true + } else if e.gpu_written { + false + } else { + if measure_selection { + dirty_tests += 1; } let i = dirty.partition_point(|&p| p + page as usize <= e.src); dirty.get(i).is_some_and(|&p| p < e.src + e.tex.size() as usize) - }) - .filter_map(|(&res, e)| { - if e.pending { - e.pending = false; - PENDING.fetch_sub(1, Ordering::Relaxed); + }; + if !selected { + continue; + } + if measure_selection { + matches += 1; + } + if e.pending { + e.pending = false; + PENDING.fetch_sub(1, Ordering::Relaxed); + } + let p = res as *mut c_void; + if let Some(r) = ID3D12Resource::from_raw_borrowed(&p) { + todo.push((r.clone(), (e.device, e.src, Arc::clone(&e.tex), e.states.clone(), e.generation))); + if measure_selection { + retained += 1; } - let p = res as *mut c_void; - ID3D12Resource::from_raw_borrowed(&p) - .map(|r| (r.clone(), (e.device, e.src, Arc::clone(&e.tex), e.states.clone(), e.generation))) - }) - .collect() + } + } + drop(t); + let finished_at = locked_at.map(|_| std::time::Instant::now()); + let measurement = wait_started.zip(locked_at).zip(finished_at).map(|((started, locked), finished)| { + (locked.duration_since(started), finished.duration_since(locked), visited, dirty_tests, matches, retained) + }); + (todo, measurement) }); + if let (Some(sample), Some((wait, hold, visited, dirty_tests, matches, retained))) = (look.as_deref_mut(), selection_measurement) { + super::sparse::note_texture_selection(sample, wait, hold, visited, dirty_tests, matches, retained); + } seed_stream_recheck(&todo, &ranges.ranges, recheck_frame); upload_all(todo, ID3D12CommandQueue::from_raw_borrowed(&queue)); super::sparse::PreparationOutcome::Ready