From a94e1511e0a5f378ff10e4daf701ebaa9da506eb Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 23 Nov 2026 19:10:42 -0500 Subject: [PATCH] Textures: a layout's high-coordinate tables hold one period, not 65536 entries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Layout::from_bits built the tables for the coordinates above a block (hx, hy, hz) over the whole 16-bit range. For a 2D surface the z table alone is 65536 u32 entries – 256 KiB – and every xg::Texture builds its own Layout, so each live tiled title texture carried about 256 KiB of CPU memory. The entries depend only on the coordinate bits the block's pattern masks use. Each table now covers the power of two above the highest such bit (one entry when no bit is used); offset_z already indexes the tables modulo their length, so every offset is unchanged. The test compares every 2D and thick 3D mode, element size and pipe configuration against the full-range tables, for coordinates up to and past 16 bits, and fails before the change (a 2D z table of 65536 entries). --- crates/xcl/src/gpu/tiling.rs | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/tiling.rs b/crates/xcl/src/gpu/tiling.rs index 7d9f4be87..66c52ad33 100644 --- a/crates/xcl/src/gpu/tiling.rs +++ b/crates/xcl/src/gpu/tiling.rs @@ -131,7 +131,10 @@ impl Layout { }; let (bw, bh, bd) = (1usize << wl, 1usize << hl, 1usize << dl); // the pattern masks are 16 bits: coordinates above them do not matter - let high = |l: u32, shift: u32| table(1 << (16 - l.min(16)), shift, l); + let high = |l: u32, shift: u32| { + let used = bits[..blk as usize].iter().fold(0u32, |m, &b| m | ((b >> shift) & 0xFFFF) as u32) >> l.min(16); + table(1 << (32 - used.leading_zeros()), shift, l) + }; Layout { mode, elem_log2, @@ -469,6 +472,45 @@ mod tests { } } + #[test] + fn high_coordinate_tables_keep_their_offsets_at_one_period() { + let full = |bits: &[u64; 20], blk: u32, l: u32, shift: u32| -> Vec { + (0..1u32 << (16 - l.min(16))) + .map(|c| { + let mut off = 0u32; + for (i, &b) in bits[..blk as usize].iter().enumerate() { + let mask = ((b >> shift) & 0xFFFF) as u32; + off |= ((mask & (c << l)).count_ones() & 1) << i; + } + off + }) + .collect() + }; + let check = |l: &Layout, bits: &[u64; 20], what: &str| { + let blk = l.block_bytes.trailing_zeros(); + for (t, b, shift) in [(&l.hx, l.block_w, 0), (&l.hy, l.block_h, 16), (&l.hz, l.block_d, 32)] { + let want = full(bits, blk, b.trailing_zeros(), shift); + assert!(t.len().is_power_of_two() && t.len() <= want.len(), "{what}: table of {} for {}", t.len(), want.len()); + for c in (0..1usize << 18).step_by(3).chain([u32::MAX as usize, (1 << 40) + 5]) { + assert_eq!(t[c % t.len()], want[c % want.len()], "{what}: shift {shift} coordinate block {c}"); + } + } + assert!(l.hz.len() <= 64, "{what}: {} z entries", l.hz.len()); + }; + for cfg in configs() { + for e in 0..5 { + for mode in MODES { + let l = Layout::new(mode, e, cfg).unwrap(); + check(&l, &pattern_bits(PATTERNS, mode, e, cfg).unwrap(), &format!("2D mode {mode} e {e} {cfg:?}")); + } + for mode in [5u32, 9, 17, 21, 25, 26] { + let l = Layout::new_3d(mode, e, cfg).unwrap(); + check(&l, &pattern_bits(PATTERNS_3D, mode, e, cfg).unwrap(), &format!("3D mode {mode} e {e} {cfg:?}")); + } + } + } + } + /// Sizes of retail textures (TTPF): the space each takes in the archive. #[test] fn mip_chain_sizes_match_retail_textures() {