mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Textures: detiling a mip level computes column and row parts once
`Chain::detile_level` called `element_at` / `Layout::offset_z` for every element: a `div_ceil`, several divisions and modulos and six table lookups each. Once the upload buffers were reused, it was most of a retail title's render thread while the title streamed textures the CPU writes (thread samples, frames named from a symbol build). The parts of the offset that depend on x alone (block column and the x and high-x pattern bits) are now computed once per level, those that depend on y and the slice once per row, leaving two lookups and an XOR per element. The result is the same: a new test compares it with `element_at` for every mode, element size, pipe configuration and level (mip tails too), with a base swizzle, two slices and a source cut short. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -396,13 +396,30 @@ impl Chain {
|
||||
/// bytes past the end of `src` read as 0
|
||||
pub fn detile_level(&self, src: &[u8], i: usize, slice: usize) -> Vec<u8> {
|
||||
let l = self.levels[i];
|
||||
let e = 1usize << self.layout.elem_log2;
|
||||
let t = &self.layout;
|
||||
let e = 1usize << t.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);
|
||||
// `element_at` (`Layout::offset_z`) split: what depends on x alone once per level, what
|
||||
// depends on y (and the slice) once per row – per element only two lookups and an XOR.
|
||||
// Element by element it was most of a retail game's render thread while it streamed
|
||||
// textures the CPU writes.
|
||||
let hi = |tab: &[u32], c: usize, b: usize| tab[(c / b) % tab.len()];
|
||||
let pitch_blocks = l.pitch.div_ceil(t.block_w);
|
||||
let columns: Vec<(usize, u32)> = (0..l.width)
|
||||
.map(|x| {
|
||||
let x = x + l.origin.0;
|
||||
(x / t.block_w, t.ox[x % t.block_w] ^ hi(&t.hx, x, t.block_w))
|
||||
})
|
||||
.collect();
|
||||
let z = t.oz[slice % t.block_d] ^ hi(&t.hz, slice, t.block_d);
|
||||
for (y, row) in out.chunks_exact_mut(l.width * e).enumerate() {
|
||||
let y = y + l.origin.1;
|
||||
let row_blocks = (y / t.block_h) * pitch_blocks;
|
||||
let yz = t.oy[y % t.block_h] ^ hi(&t.hy, y, t.block_h) ^ z;
|
||||
for (&(block_x, x), dst) in columns.iter().zip(row.chunks_exact_mut(e)) {
|
||||
let o = l.offset + (row_blocks + block_x) * t.block_bytes + ((x ^ yz) as usize ^ t.base_xor);
|
||||
if let Some(s) = src.get(o..o + e) {
|
||||
out[(y * l.width + x) * e..][..e].copy_from_slice(s);
|
||||
dst.copy_from_slice(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -447,6 +464,39 @@ mod tests {
|
||||
v
|
||||
}
|
||||
|
||||
/// `detile_level` (offsets split by row and column) gives the bytes element-by-element
|
||||
/// `element_at` does: every mode, element size and pipe configuration, every level (mip
|
||||
/// tails too), a base swizzle and a slice, and a source cut short (bytes past it read as 0).
|
||||
#[test]
|
||||
fn detile_level_matches_element_by_element() {
|
||||
for mode in MODES {
|
||||
for e in 0..5u32 {
|
||||
for cfg in configs() {
|
||||
let mut lay = Layout::new(mode, e, cfg).unwrap();
|
||||
lay.base_xor = (lay.block_bytes / 2) & !((1 << e) - 1);
|
||||
let c = Chain::new(lay, 77, 45, 1, 7);
|
||||
let mem: Vec<u8> = (0..c.slice_bytes * 2).map(|i| (i * 131 % 251) as u8).collect();
|
||||
let short = &mem[..mem.len() * 3 / 5];
|
||||
let es = 1usize << e;
|
||||
for (k, l) in c.levels.iter().enumerate() {
|
||||
for (slice, src) in [(0, &mem[..]), (3, short)] {
|
||||
let mut want = vec![0u8; l.width * l.height * es];
|
||||
for y in 0..l.height {
|
||||
for x in 0..l.width {
|
||||
let o = c.element_at(l, x, y, slice);
|
||||
if let Some(s) = src.get(o..o + es) {
|
||||
want[(y * l.width + x) * es..][..es].copy_from_slice(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(c.detile_level(src, k, slice) == want, "mode {mode} e {e} {cfg:?} level {k} slice {slice}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// every mode, element size and pipe configuration: the in-block mapping is a permutation
|
||||
/// of the block's elements (catches a mis-parsed table or a wrong block shape)
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user