From 712bbbe4e5163b5fe479cccfa0b3ff636ecf8fd8 Mon Sep 17 00:00:00 2001 From: Dudis Date: Mon, 23 Nov 2026 22:18:18 -0300 Subject: [PATCH] Title textures: the write watch scans in a few calls, not thousands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A write-watch call costs about 3.5 µs however little it covers, while one call over 2 GiB of committed pages takes about 1.3 ms (measured with a small GetWriteWatch benchmark on Windows 11). With ranges merged only across gaps under 1 MiB, the look before each submit made thousands of calls over a retail game's mapped sparse tiles: its intro video had the submitting thread at 42 % in GetWriteWatch (sampled). Ranges under 64 MiB apart are now one scan; the pages in a gap are reported written as well, which the uploads already tolerate (they keep to what they track). The ranges are clipped to the write-watched regions with a binary search per region instead of a pass over every merged range for each region: both lists run to thousands while a game streams its world. Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/hle/d3d12x/title_textures.rs | 31 +++++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 5686538e0..683671435 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -990,9 +990,12 @@ unsafe fn upload_queue(dev: &ID3D12Device, device: usize) -> Result) -> _ => merged.push((a, b)), } } + // each region against the merged ranges it meets (sorted and disjoint: a binary search, not + // a pass over all of them – both lists run to thousands while a game streams) let mut out = Vec::new(); - for (a, b) in merged { - for &(base, size) in regions { - let (lo, hi) = (a.max(base), b.min(base + size)); + for &(base, size) in regions { + let end = base + size; + let first = merged.partition_point(|&(_, b)| b <= base); + for &(a, b) in merged[first..].iter().take_while(|&&(a, _)| a < end) { + let (lo, hi) = (a.max(base), b.min(end)); if lo < hi { out.push((lo, hi - lo)); } @@ -1370,9 +1377,15 @@ mod tests { let regions = [(0x10000, 0x10000), (0x40000, 0x10000), (0x1000_0000, 0x1000_0000)]; let wanted = vec![(0x12000, 0x13000), (0x11000, 0x12800), (0x1F000, 0x41000), (0x60000, 0x61000)]; assert_eq!(watch_ranges(®ions, wanted), vec![(0x11000, 0xF000), (0x40000, 0x10000)]); - // ranges a gap of under 1 MiB apart: one scan; further apart: two - let wanted = vec![(0x1000_0000, 0x1000_1000), (0x1008_0000, 0x1008_1000), (0x1100_0000, 0x1100_1000)]; - assert_eq!(watch_ranges(®ions, wanted), vec![(0x1000_0000, 0x8_1000), (0x1100_0000, 0x1000)]); + // ranges a gap of under WATCH_GAP apart: one scan; further apart: two + let wanted = vec![(0x1000_0000, 0x1000_1000), (0x1008_0000, 0x1008_1000), (0x1100_0000, 0x1100_1000), (0x1800_0000, 0x1800_1000)]; + assert_eq!(watch_ranges(®ions, wanted), vec![(0x1000_0000, 0x100_1000), (0x1800_0000, 0x1000)]); + // a merged range over two regions: a piece in each, whatever order the regions come in + let regions = [(0x40000, 0x10000), (0x10000, 0x10000)]; + assert_eq!(watch_ranges(®ions, vec![(0x18000, 0x48000)]), vec![(0x18000, 0x8000), (0x40000, 0x8000)]); + // a region between two merged ranges, and one past them all + let regions = [(0x2000_0000, 0x10_0000), (0x9000_0000, 0x1000)]; + assert_eq!(watch_ranges(®ions, vec![(0x1000_0000, 0x1000_1000), (0x2008_0000, 0x2009_0000)]), vec![(0x2008_0000, 0x1_0000)]); } fn console_desc(dim: u32, w: u64, h: u32, array_or_depth: u16, mips: u16, format: u32, layout: u32) -> [u8; 0x38] {