From b47d0eb8178579d635d54187438ce853d79b7f1a Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Sun, 29 Nov 2026 03:42:01 +0100 Subject: [PATCH] D3D12.X: mapped ranges asked again at the same generation need no lock `title_textures::cached_watch_ranges` makes the watched ranges again whenever any part of its key moved (the textures' 16 MiB blocks, the graphics reservations, the shown pages, the mapped tiles), and each time it asked `sparse::mapped_ranges`, which takes `sparse::STATE`. In a retail title's world (RX 9070 XT, XCL_LOOK_STATS=2) a submit's "ranges" stage was 0.11-0.31 ms, 0.08-0.29 ms of it waiting for that lock ("the submit's other waits for it: ... ranges"). The tiles' runs (`State::runs`, `src_runs`) change only under the lock, and every change moves `MAPPED_GEN` (which is in the key already). So the last answer is kept with the generation it was made at (`RANGES`) and given again without the lock while the generation stands; `init` moves it when it puts a new state in. This is the "already up to date" check made lock-free, as VIctory-Neo does for its refreshes (55eb862, its write journal's epochs); here there is one generation for all the ranges. XCL_LOOK_STATS=2: the sparse lock split line counts the answers made under the lock and those that were not. Test: mapped_ranges_made_at_this_generation_need_no_lock (answered while another thread holds the lock; once the generation moved, it waits for the lock and makes them again). Ported from amd-dev: the static is `LAST_MAPPED_RANGES` (main has a `const RANGES` look field); main's lock split line has no CPU reads part, so only the mapped ranges count is added to it; `init` moves the generation under main's `STATE` guard, which it keeps for the early commits. (cherry picked from commit 0383b3fe8a6a2595031fb89d702d0ce3e3f898ad) (cherry picked from commit 707123ad7db65a984f0c069623719bc35c5f2cf5) Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/sparse.rs | 73 ++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index cae13913f2..b8dccd616f 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -3448,6 +3448,8 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { let old_queue = QUEUE.lock().unwrap().replace(s.queue.clone()); let _old_rb = READBACKS.lock().unwrap().replace(Arc::clone(&s.readbacks)); let mut state = STATE.lock().unwrap(); + // (the ranges made of the state before are not this one's: `LAST_MAPPED_RANGES`) + MAPPED_GEN.fetch_add(1, Ordering::Release); let old = state.replace(s); let s = state.as_mut().unwrap(); for (a, b) in std::mem::take(&mut *EARLY.lock().unwrap()) { @@ -7084,6 +7086,12 @@ impl State { /// tiles of GPU views show (never the views' own addresses): where a CPU write has to reach the /// GPU. pub fn mapped_ranges() -> Vec<(usize, usize)> { + if let Some((gen, r)) = LAST_MAPPED_RANGES.lock().unwrap().as_ref() { + if *gen == MAPPED_GEN.load(Ordering::Acquire) { + RANGES_TAKEN.add(1, 1); + return r.clone(); + } + } let t0 = super::look_stats::deep().then(std::time::Instant::now); let g = lock_for_submit(); let _held = holding(Holder::Ranges); @@ -7091,9 +7099,24 @@ pub fn mapped_ranges() -> Vec<(usize, usize)> { HELD.add(HELD_RANGES_WAIT, t0.elapsed().as_micros() as u64); } let Some(s) = g.as_ref() else { return Vec::new() }; - s.watched_ranges() + let out = s.watched_ranges(); + // (read with the lock held: the runs change, and it goes up, only under it) + *LAST_MAPPED_RANGES.lock().unwrap() = Some((MAPPED_GEN.load(Ordering::Acquire), out.clone())); + RANGES_TAKEN.add(0, 1); + out } +/// The last `mapped_ranges` and the `MAPPED_GEN` they were made at: asked again with no tile +/// mapped, unmapped or made writable since, they are answered without `STATE`. The watched ranges +/// are made again whenever any part of their key changed (`title_textures::watch_key`: the +/// textures' blocks, the reservations, the shown pages too), and the lock may be held then by a +/// placed buffer, a commit, a CPU read or another queue's submit (a retail title's world: a submit +/// waited 0.1–0.3 ms for it there). Taken alone or under `STATE`, never around it. +static LAST_MAPPED_RANGES: Mutex)>> = Mutex::new(None); +/// `mapped_ranges` [made under the lock, answered from `LAST_MAPPED_RANGES`] (`XCL_LOOK_STATS=2`, +/// with the sparse lock split line) +static RANGES_TAKEN: super::look_stats::Sums<2> = super::look_stats::Sums::new(); + const LOOK_FIELDS: usize = 37; const ACTIVE_US: usize = 0; const REFRESH_WAIT_US: usize = 1; @@ -7760,7 +7783,12 @@ impl State { LOCK_SPLIT.add(LS_UNMAP_TILES, work(2)); if LOCK_SPLIT.add(LS_SUBMITS, 1) >= 1024 { let x = LOCK_SPLIT.take(); - crate::xlog_info!("D3D12.X: {}; {}", lock_split_line(&x), holders_line(&HELD.take(), x[LS_SUBMITS])); + let [made, kept] = RANGES_TAKEN.take(); + crate::xlog_info!( + "D3D12.X: {}; {}; mapped ranges asked for {made} times under the lock, {kept} times not", + lock_split_line(&x), + holders_line(&HELD.take(), x[LS_SUBMITS]) + ); } } let t_maintained = t_started.map(|_| t_locked); @@ -13563,4 +13591,45 @@ mod tests { assert_eq!(iso.rb.lock().unwrap().submitting, 0); } } + + /// `mapped_ranges` asked again with no tile mapped or unmapped since answers without the state's + /// lock; once `MAPPED_GEN` moved it takes the lock again. + #[test] + fn mapped_ranges_made_at_this_generation_need_no_lock() { + let fake = vec![(0x7000_0000usize, 0x7001_0000usize)]; + // (other tests move the generation too: tried a few times) + let mut answered = false; + for _ in 0..20 { + let g = STATE.lock().unwrap_or_else(|e| e.into_inner()); + let gen = MAPPED_GEN.load(Ordering::Acquire); + *LAST_MAPPED_RANGES.lock().unwrap() = Some((gen, fake.clone())); + let (tx, rx) = std::sync::mpsc::channel(); + let asker = std::thread::spawn(move || { + let _ = tx.send(mapped_ranges()); + }); + let got = rx.recv_timeout(std::time::Duration::from_millis(500)); + let moved = MAPPED_GEN.load(Ordering::Acquire) != gen; + drop(g); + asker.join().unwrap(); + if moved { + continue; + } + assert_eq!(got.expect("answered with the state's lock held elsewhere"), fake); + answered = true; + break; + } + assert!(answered, "the generation never stood still"); + // a tile mapped or unmapped since: made again, under the lock + let g = STATE.lock().unwrap_or_else(|e| e.into_inner()); + MAPPED_GEN.fetch_add(1, Ordering::Release); + let (tx, rx) = std::sync::mpsc::channel(); + let asker = std::thread::spawn(move || { + let _ = tx.send(mapped_ranges()); + }); + assert!(rx.recv_timeout(std::time::Duration::from_millis(50)).is_err(), "a stale answer without the lock"); + drop(g); + let again = rx.recv_timeout(std::time::Duration::from_secs(10)).expect("answered once the lock was free"); + asker.join().unwrap(); + assert_ne!(again, fake); + } }