From caf1bbb98eb085b8143d0495b0e8610a08c255de Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Mon, 23 Nov 2026 23:53:09 +0100 Subject: [PATCH] Textures: detile linear levels a row at a time, tiled ones with per-row and per-column parts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Chain::detile_level` computed every element's address from scratch (`element_at`: block index, six table lookups, divisions) and copied it with a bounds check. Kept-in-step title textures are detiled on the CPU at every upload. A retail title's intro movie is decoded on the CPU into three linear R8 planes (3840x2160 + 2 x 1920x1080, 12.4 MB a frame) that are uploaded again whenever the decoder writes them: on main 85bdf21..3516080 detiling took ~1.75 s of every 2 s of the movie, and it played at 7-8 presented frames per 2 s. - Linear levels (mode 0, no base swizzle, not in a mip tail): each row is copied whole, `pitch` elements apart (the case gw1337x profiled in #283; also closed PR #334). - Other levels: the address is a block row part plus a block column part, and the XOR of an x part and a y (and slice) part; the x parts are worked out once per level, the y parts once per row. Same bytes as before: a new test compares with `element_at` for every mode (linear too), each element size, three pipe configurations, mips in and out of the tail, two slices, a base swizzle and a source cut short. Synthetic release timing, the three planes: linear 107-113 ms → 1.4-2.0 ms; 64KB_R_X 112-116 ms → 22-33 ms. Retail movie on main 3516080 (RTX 5090, exclusive runs): 7-8 → 34-46 presented frames per 2 s; read + detile 1.75 s → ~0.33 s per 2 s. What remains is the staging upload (~1.3 s per 2 s, ~1.1 GB: each plane is uploaded about three times per presented frame). AI-assisted. --- crates/xcl/src/gpu/tiling.rs | 81 +++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/gpu/tiling.rs b/crates/xcl/src/gpu/tiling.rs index 7d9f4be87..3f87922fa 100644 --- a/crates/xcl/src/gpu/tiling.rs +++ b/crates/xcl/src/gpu/tiling.rs @@ -396,13 +396,41 @@ impl Chain { /// bytes past the end of `src` read as 0 pub fn detile_level(&self, src: &[u8], i: usize, slice: usize) -> Vec { let l = self.levels[i]; - let e = 1usize << self.layout.elem_log2; + let lay = &self.layout; + let e = 1usize << lay.elem_log2; let mut out = vec![0u8; l.width * l.height * e]; - for y in 0..l.height { - for x in 0..l.width { - let o = self.element_at(&l, x, y, slice); + // linear (a retail game's video planes): each row is whole, `pitch` elements apart + if lay.mode == 0 && lay.base_xor == 0 && l.origin == (0, 0) { + let row = l.width * e; + for (y, d) in out.chunks_exact_mut(row).enumerate() { + let o = l.offset + y * l.pitch.div_ceil(lay.block_w) * lay.block_bytes; + let n = row.min(src.len().saturating_sub(o) / e * e); + if n > 0 { + d[..n].copy_from_slice(&src[o..o + n]); + } + } + return out; + } + // `element_at` with its x and y parts worked out once (a block column and the in-block + // bits of x; a block row and the bits of y and the slice): per element it took ~5.7 ms + // a MB, and a retail game's video planes are 12 MB a frame + let hi = |t: &[u32], c: usize, b: usize| t[(c / b) % t.len()]; + let xs: Vec<(usize, u32)> = (0..l.width) + .map(|x| { + let x = x + l.origin.0; + (x / lay.block_w * lay.block_bytes, lay.ox[x % lay.block_w] ^ hi(&lay.hx, x, lay.block_w)) + }) + .collect(); + let zbits = lay.oz[slice % lay.block_d] ^ hi(&lay.hz, slice, lay.block_d); + let pitch_blocks = l.pitch.div_ceil(lay.block_w); + for (y, row) in out.chunks_exact_mut(l.width * e).enumerate() { + let yy = y + l.origin.1; + let base = l.offset + (yy / lay.block_h) * pitch_blocks * lay.block_bytes; + let ybits = (lay.oy[yy % lay.block_h] ^ hi(&lay.hy, yy, lay.block_h) ^ zbits) as usize ^ lay.base_xor; + for (d, &(bx, xb)) in row.chunks_exact_mut(e).zip(&xs) { + let o = base + bx + (xb as usize ^ ybits); if let Some(s) = src.get(o..o + e) { - out[(y * l.width + x) * e..][..e].copy_from_slice(s); + d.copy_from_slice(s); } } } @@ -439,6 +467,49 @@ mod tests { const MODES: [u32; 14] = [1, 2, 5, 6, 9, 10, 17, 18, 21, 22, 24, 25, 26, 27]; + /// `detile_level` reads every element where `element_at` puts it: every mode (linear too), + /// element size and a few pipe configurations, mips in and out of the tail, array slices, + /// a base swizzle, a source cut short + #[test] + fn detile_level_matches_element_at() { + for mode in std::iter::once(0).chain(MODES) { + for e in 0..5u32 { + for cfg in [PipeConfig { pipes_log2: 0, pkrs_log2: 0 }, PipeConfig { pipes_log2: 3, pkrs_log2: 2 }, PipeConfig { pipes_log2: 5, pkrs_log2: 3 }] { + let Some(mut lay) = Layout::new(mode, e, cfg) else { continue }; + if lay.block_bytes > 256 { + lay.base_xor = 0x500 & (lay.block_bytes - 1); + } + let c = Chain::new(lay, 300, 77, 1, 9); + let src: Vec = (0..c.slice_bytes * 2).map(|k| (k * 7 + k / 251) as u8).collect(); + let eb = 1usize << e; + for slice in 0..2 { + let s = &src[slice * c.slice_bytes..][..c.slice_bytes]; + for (i, l) in c.levels.iter().enumerate() { + let mut want = vec![0u8; l.width * l.height * eb]; + for y in 0..l.height { + for x in 0..l.width { + let o = c.element_at(l, x, y, slice); + if let Some(b) = s.get(o..o + eb) { + want[(y * l.width + x) * eb..][..eb].copy_from_slice(b); + } + } + } + assert!(c.detile_level(s, i, slice) == want, "mode {mode} elem {e} {cfg:?} level {i} slice {slice}"); + // a source cut short: what is past its end reads as 0 + let short = &s[..s.len() / 3 + 1]; + let cut: Vec = (0..want.len()).map(|k| { + let (x, y) = ((k / eb) % l.width, (k / eb) / l.width); + let o = c.element_at(l, x, y, slice); + if o + eb <= short.len() { short[o + k % eb] } else { 0 } + }).collect(); + assert!(c.detile_level(short, i, slice) == cut, "mode {mode} elem {e} {cfg:?} level {i} slice {slice}, cut short"); + } + } + } + } + } + } + fn configs() -> Vec { let mut v = vec![]; for (p, k) in [(0, 0), (1, 0), (2, 0), (3, 1), (2, 2), (3, 2), (4, 2), (3, 3), (4, 3), (5, 3)] {