diff --git a/README.md b/README.md index 33ccbc64a0..a0def49ac0 100644 --- a/README.md +++ b/README.md @@ -406,6 +406,10 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`). - `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in. Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles. +- `XCL_UNWATCHED_RECHECK=1` keeps checking small title textures (up to 4 MiB) in any committed window that is not + fully write-watched (such as the `GPU_UPLOAD` part of `XCL_UMA_DRIVER=mixed`) every 16 frames for as long as they + live (at most 8 MiB and 64 textures read a frame), and uploads them again when the title changed them + (control-hint glyphs missing, #1049). Off by default. - `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib requests with virtual destinations. Defaults: four workers and a shared 32 MiB staging budget (`XCL_DSTORAGE_WORKERS`, `XCL_DSTORAGE_MEMORY_BUDGET_MIB`). Requests retire in queue order. diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..0626cbaf4f 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -2322,6 +2322,75 @@ fn stream_recheck_enabled() -> bool { *ON.get_or_init(|| std::env::var("XCL_TEXTURE_STREAM_RECHECK").map_or(true, |v| v.trim() != "0")) } +/// `XCL_UNWATCHED_RECHECK=1` (off unless given): a texture of up to `STEADY_MAX_BYTES` in a +/// committed window that is not fully write-watched (such as the `GPU_UPLOAD` segments of +/// `XCL_UMA_DRIVER=mixed`) is not retired after the last milestone: it is checked again every +/// `STEADY_EVERY` frames for as long as it lives, and its fingerprint hashes all of it (read by +/// the GPU: `with_title_bytes`). A font atlas the title fills glyph by glyph is written minutes +/// after it was made and outside the three sampled slices, and its text lost letters. +fn steady_recheck() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var("XCL_UNWATCHED_RECHECK").is_ok_and(|v| v.trim() == "1")) +} +const STEADY_MAX_BYTES: usize = 4 << 20; +const STEADY_EVERY: u64 = 16; +/// the bytes the steady checks of one frame read at most (the first one is always made) +const STEADY_BUDGET: usize = 8 << 20; +/// the steady checks (whole-hash reads of a texture) of one frame at most +const STEADY_MAX_CHECKS: usize = 64; + +/// Frames (below `STEADY_EVERY`) by which the schedule of the entry of `res` is moved, so that +/// the entries seeded in one frame do not all come due together every `STEADY_EVERY` frames. +fn steady_stagger(res: usize) -> u64 { + ((res as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15) >> 32) % STEADY_EVERY +} + +/// The `base_frame` of an entry made or reset at `frame`. +fn steady_base(res: usize, frame: u64, kept: bool) -> u64 { + if kept { frame + steady_stagger(res) } else { frame } +} + +/// Whether the unwatched texture at `src` is checked whole and for as long as it lives. +fn steady_keeps(src: usize, size: usize) -> bool { + steady_recheck() && size <= STEADY_MAX_BYTES && crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(src, size)) +} + +/// The stage an entry starts at: a kept one (`steady_keeps`) past the milestones, so that all its +/// checks, which read all of it, come under the frame's budget. +fn first_stage(kept: bool) -> u8 { + if kept { STREAM_RECHECK_MILESTONES.len() as u8 } else { 0 } +} + +/// Frames after its `base_frame` at which an entry at `stage` is checked next: the milestones, +/// then (an entry kept past them) every `STEADY_EVERY` frames. +fn recheck_interval(stage: u8) -> u64 { + STREAM_RECHECK_MILESTONES.get(stage as usize).copied().unwrap_or(STEADY_EVERY) +} + +/// The steady checks `(base_frame, resource, bytes)` due this frame that wait for a later one: +/// the longest unchecked go first, and those past `budget` bytes or `max_checks` wait (the first +/// never does). +fn steady_waiting(mut due: Vec<(u64, usize, usize)>, budget: usize, max_checks: usize) -> std::collections::BTreeSet { + due.sort_unstable(); + let mut bytes = 0usize; + due.into_iter() + .enumerate() + .filter_map(|(i, (_, res, size))| { + bytes = bytes.saturating_add(size); + (i > 0 && (bytes > budget || i >= max_checks)).then_some(res) + }) + .collect() +} + +/// The fingerprint an unwatched texture is compared by: all of it when `whole` (`steady_keeps`), +/// else the three slices. +unsafe fn stream_fingerprint(src: usize, size: usize, whole: bool) -> u64 { + if whole { + return with_title_bytes(src, size, texture_upload::content_hash); + } + sample_fingerprint(src, size) +} + /// Whether every 4 KiB page of `[src, src + len)` is covered by the sorted, disjoint /// write-watched spans `watched` (`(base, size)` pairs from `cached_watch_ranges`). fn range_is_fully_watched(src: usize, len: usize, watched: &[(usize, usize)]) -> bool { @@ -2396,8 +2465,17 @@ unsafe fn seed_stream_recheck(todo: &[Due], watched: &[(usize, usize)], frame: u unsent.push(d.res.as_raw() as usize); continue; } - let fp = sample_fingerprint(d.src, size); - seeded.push((d.res.as_raw() as usize, StreamRecheck { base_frame: frame, stage: 0, last_fp: Some(fp) })); + let kept = steady_keeps(d.src, size); + if kept { + static KEPT: AtomicU64 = AtomicU64::new(0); + let n = KEPT.fetch_add(1, Ordering::Relaxed) + 1; + if n == 1 || (n.is_power_of_two() && n >= 16) { + crate::xlog_info!("D3D12.X: XCL_UNWATCHED_RECHECK: {n} uploads of textures in unwatched video memory so far: each is checked whole every {STEADY_EVERY} frames ({} MiB a frame at most)", STEADY_BUDGET >> 20); + } + } + let fp = stream_fingerprint(d.src, size, kept); + let res = d.res.as_raw() as usize; + seeded.push((res, StreamRecheck { base_frame: steady_base(res, frame, kept), stage: first_stage(kept), last_fp: Some(fp) })); } if !seeded.is_empty() || !unsent.is_empty() { let mut g = STREAM_RECHECK.lock().unwrap(); @@ -2424,15 +2502,17 @@ unsafe fn recheck_streamed_unwatched(watched: &[(usize, usize)], frame: u64) { /// Re-fingerprint due unwatched textures at `frame`, re-queuing changed entries and resetting /// their milestone schedule so subsequent higher-resolution mip streams are also caught. unsafe fn recheck_streamed_unwatched_at(watched: &[(usize, usize)], frame: u64) { - let due: Vec<(usize, Option)> = { + // (resource, baseline, `base_frame` of an entry kept past the milestones or with no baseline, + // whose check hashes all of the texture when it is kept) + let due: Vec<(usize, Option, Option)> = { let g = STREAM_RECHECK.lock().unwrap(); if g.is_empty() { return; } g.iter() .filter_map(|(&res, st)| { - let target = *STREAM_RECHECK_MILESTONES.get(st.stage as usize)?; - (frame.saturating_sub(st.base_frame) >= target).then_some((res, st.last_fp)) + let steady = (st.stage as usize >= STREAM_RECHECK_MILESTONES.len() || st.last_fp.is_none()).then_some(st.base_frame); + (frame.saturating_sub(st.base_frame) >= recheck_interval(st.stage)).then_some((res, st.last_fp, steady)) }) .collect() }; @@ -2442,10 +2522,15 @@ unsafe fn recheck_streamed_unwatched_at(watched: &[(usize, usize)], frame: u64) let mut check = Vec::with_capacity(due.len()); let mut prune = Vec::new(); + let mut steady_due = Vec::new(); { let t = TEXTURES.lock().unwrap(); - for (res, prev_fp) in due { + for (res, prev_fp, steady) in due { match t.by_res.get(&res) { + // (`XCL_UNWATCHED_RECHECK`: the entry of a kept texture that is GPU-written stays + // if it is still here, but `publish_submission_effects` removes it when the + // texture becomes GPU-written, so this is not what keeps the checks going) + Some(e) if e.gpu_written && steady_keeps(e.src, e.tex.size() as usize) => {} Some(e) if e.gpu_written => prune.push(res), Some(e) if e.pending => {} Some(e) => { @@ -2454,6 +2539,9 @@ unsafe fn recheck_streamed_unwatched_at(watched: &[(usize, usize)], frame: u64) prune.push(res); } else { check.push((res, e.src, size, prev_fp)); + if steady_keeps(e.src, size) { + steady_due.extend(steady.map(|base| (base, res, size))); + } } } None => { @@ -2470,29 +2558,39 @@ unsafe fn recheck_streamed_unwatched_at(watched: &[(usize, usize)], frame: u64) g.remove(&res); } } + if !steady_due.is_empty() { + let waiting = steady_waiting(steady_due, STEADY_BUDGET, STEADY_MAX_CHECKS); + check.retain(|c| !waiting.contains(&c.0)); + } if check.is_empty() { return; } - let results: Vec<(usize, Option, u64)> = check + let results: Vec<(usize, Option, u64, bool)> = check .into_iter() - .map(|(res, src, size, prev_fp)| (res, prev_fp, sample_fingerprint(src, size))) + .map(|(res, src, size, prev_fp)| { + let kept = steady_keeps(src, size); + (res, prev_fp, stream_fingerprint(src, size, kept), kept) + }) .collect(); let mut changed = Vec::new(); { let mut g = STREAM_RECHECK.lock().unwrap(); - for (res, prev_fp, cur_fp) in results { + for (res, prev_fp, cur_fp, kept) in results { let Some(st) = g.get_mut(&res) else { continue; }; // (an entry without a baseline is always queued) if prev_fp != Some(cur_fp) { - *st = StreamRecheck { base_frame: frame, stage: 0, last_fp: Some(cur_fp) }; + *st = StreamRecheck { base_frame: steady_base(res, frame, kept), stage: first_stage(kept), last_fp: Some(cur_fp) }; changed.push(res); } else { st.stage = st.stage.saturating_add(1); - if st.stage as usize >= STREAM_RECHECK_MILESTONES.len() { + if st.stage as usize >= STREAM_RECHECK_MILESTONES.len() && kept { + // (`XCL_UNWATCHED_RECHECK`: checked again `STEADY_EVERY` frames from now) + (st.stage, st.base_frame) = (first_stage(true), frame); + } else if st.stage as usize >= STREAM_RECHECK_MILESTONES.len() { g.remove(&res); } } @@ -2507,9 +2605,17 @@ unsafe fn recheck_streamed_unwatched_at(watched: &[(usize, usize)], frame: u64) if !e.gpu_written && !e.pending { e.pending = true; PENDING.fetch_add(1, Ordering::Relaxed); + let (va, bytes, target) = (e.va, e.tex.size(), e.target); // (a submit takes the pending ones from this set, not from every texture) t.pending_res.insert(res); queued += 1; + if steady_recheck() { + static N: AtomicU64 = AtomicU64::new(0); + let n = N.fetch_add(1, Ordering::Relaxed) + 1; + if n <= 8 || n.is_power_of_two() { + crate::xlog_info!("D3D12.X: XCL_UNWATCHED_RECHECK: the texture at {va:#x} ({bytes} bytes, target {target}) changed in unwatched memory at frame {frame}: it goes up again ({n} so far)"); + } + } } } } @@ -6617,6 +6723,38 @@ pub(super) mod tests { forget(gpu_res_id); } + /// `XCL_UNWATCHED_RECHECK`: an entry kept past the milestones is due every `STEADY_EVERY` + /// frames, and a frame's steady checks are the longest unchecked ones up to the budget. + #[test] + fn steady_rechecks_follow_the_milestones_and_keep_to_the_frame_budget() { + assert_eq!((0..5).map(recheck_interval).collect::>(), STREAM_RECHECK_MILESTONES); + assert_eq!(recheck_interval(STREAM_RECHECK_MILESTONES.len() as u8), STEADY_EVERY); + // oldest first: 3 + 4 MiB fit in 8 MiB, the two newest wait + let due = vec![(40, 0xA, 2 << 20), (10, 0xB, 3 << 20), (20, 0xC, 4 << 20), (30, 0xD, 2 << 20)]; + assert_eq!(steady_waiting(due, 8 << 20, STEADY_MAX_CHECKS).into_iter().collect::>(), vec![0xA, 0xD]); + // the first is checked whatever its size, so every texture gets its turn + assert_eq!(steady_waiting(vec![(1, 0xA, 64 << 20), (2, 0xB, 1)], 8 << 20, STEADY_MAX_CHECKS).into_iter().collect::>(), vec![0xB]); + assert!(steady_waiting(vec![(1, 0xA, 4 << 20), (1, 0xB, 4 << 20)], 8 << 20, STEADY_MAX_CHECKS).is_empty()); + // small textures are held to a count: the oldest STEADY_MAX_CHECKS are read, the rest wait + let many = (0..STEADY_MAX_CHECKS as u64 + 36).map(|i| (i, 0x1000 + i as usize, 16)).collect::>(); + let waiting = steady_waiting(many, 8 << 20, STEADY_MAX_CHECKS); + assert_eq!(waiting.len(), 36); + assert_eq!(waiting.first(), Some(&(0x1000 + STEADY_MAX_CHECKS))); + // entries made in the same frame are spread over the period, not all due in one frame + let bases = (1..=64usize).map(|i| steady_base(i * 0x1000, 1000, true)).collect::>(); + assert!(bases.iter().all(|&b| (1000..1000 + STEADY_EVERY).contains(&b))); + assert!(bases.len() >= 8, "only {} distinct due frames", bases.len()); + assert_eq!(steady_base(0x1000, 1000, false), 1000); + // all of the texture is hashed: a byte outside the three sampled slices changes it + let mut bytes = vec![0u8; 1 << 20]; + let at = bytes.as_ptr() as usize; + let (whole, sampled) = unsafe { (stream_fingerprint(at, bytes.len(), true), stream_fingerprint(at, bytes.len(), false)) }; + bytes[100 << 10] = 1; + let at = bytes.as_ptr() as usize; + assert_ne!(unsafe { stream_fingerprint(at, bytes.len(), true) }, whole); + assert_eq!(unsafe { stream_fingerprint(at, bytes.len(), false) }, sampled); + } + /// A partial upload does not replace the baseline of the last whole upload: an unwatched write /// it did not send is still caught at the milestone. #[test]