diff --git a/.github/workflows/linux-subset.yml b/.github/workflows/linux-subset.yml index a7b7233..c07c3e5 100644 --- a/.github/workflows/linux-subset.yml +++ b/.github/workflows/linux-subset.yml @@ -794,6 +794,7 @@ jobs: IGNORED_HERMETIC: >- bcpack::tests::bcpack_throughput emit::bitcode::tests::bench_write + texture::tests::detile_throughput IGNORED_EXTERNAL: >- bcpack::tests::reference_chunks emit::lower::tests::a_missing_value_stays_missing_through_two_live_joins diff --git a/crates/victory-formats/src/texture.rs b/crates/victory-formats/src/texture.rs index fa2fd8d..3313f60 100644 --- a/crates/victory-formats/src/texture.rs +++ b/crates/victory-formats/src/texture.rs @@ -620,7 +620,13 @@ impl Layout { row_pitch: usize, depth_pitch: usize, ) -> Result<(), Error> { - self.walk::(tiled, linear, mip, slice, row_pitch, depth_pitch) + self.walk( + Buffers::Detile { tiled, linear }, + mip, + slice, + row_pitch, + depth_pitch, + ) } /// The reverse of [`Layout::detile`]: linear rows into tiled memory. @@ -633,15 +639,19 @@ impl Layout { row_pitch: usize, depth_pitch: usize, ) -> Result<(), Error> { - self.walk::(linear, tiled, mip, slice, row_pitch, depth_pitch) + self.walk( + Buffers::Tile { tiled, linear }, + mip, + slice, + row_pitch, + depth_pitch, + ) } - /// Copies contiguous runs in one direction between the two layouts. - #[allow(clippy::too_many_arguments)] - fn walk( + /// Validates the full subresource before writing any bytes. + fn walk( &self, - src: &[u8], - dst: &mut [u8], + mut copy: Buffers<'_>, mip: u32, slice: u32, row_pitch: usize, @@ -665,115 +675,168 @@ impl Layout { } let n = self.desc.element_bytes as usize; let bytes = m.width as usize * n; + let slice_bytes = (m.height as usize - 1) + .checked_mul(row_pitch) + .and_then(|offset| offset.checked_add(bytes)) + .ok_or(Error::TooSmall)?; + + // Multiple depth slices must not overwrite each other. + if depth > 1 && depth_pitch < slice_bytes { + return Err(Error::TooSmall); + } + let need = (depth as usize - 1) .checked_mul(depth_pitch) - .zip((m.height as usize - 1).checked_mul(row_pitch)) - .and_then(|(depth_offset, row_offset)| depth_offset.checked_add(row_offset)) - .and_then(|offset| offset.checked_add(bytes)) + .and_then(|offset| offset.checked_add(slice_bytes)) .ok_or(Error::TooSmall)?; - let (tiled_len, linear_len) = if DETILE { - (src.len(), dst.len()) - } else { - (dst.len(), src.len()) + let (tiled_len, linear_len) = match © { + Buffers::Tile { tiled, linear } => (tiled.len(), linear.len()), + Buffers::Detile { tiled, linear } => (tiled.len(), linear.len()), }; if tiled_len < self.size as usize || linear_len < need || row_pitch < bytes { return Err(Error::TooSmall); } - let linear = self.desc.swizzle == Swizzle::LINEAR; - // The row and depth contribute one common base to every column, so - // the same contiguous runs repeat on every row, including mip tails. - let mut runs = Vec::new(); - if !linear { - let mut first_linear = 0; - let mut first_tiled = self.column(m, 0) as usize; - for x in 1..m.width { - let offset = self.column(m, x) as usize; - let at = x as usize * n; - if offset != first_tiled + (at - first_linear) { - runs.push((first_tiled, first_linear, at - first_linear)); - first_tiled = offset; - first_linear = at; + if self.desc.swizzle == Swizzle::LINEAR { + for z in 0..depth { + let base = (u64::from(first + z) * self.slice_size + m.offset) as usize; + for y in 0..m.height as usize { + copy.span( + base + y * m.pitch as usize * n, + z as usize * depth_pitch + y * row_pitch, + bytes, + ); } } - runs.push((first_tiled, first_linear, bytes - first_linear)); + return Ok(()); } - // A mip can end partway through the last run. Check the other runs - // once so each row can copy them as fixed-size chunks. - let (body, last) = runs.split_at(runs.len().saturating_sub(1)); - let chunk_size = body.first().map(|run| run.2).filter(|&size| { - body.iter() - .all(|&(offset, _, len)| len == size && offset.is_multiple_of(size)) - }); - for z in 0..depth { - let (s, inner) = self.split(first + z); - let base = u64::from(s) * self.slice_size + m.offset; - for y in 0..m.height { - let linear_row = z as usize * depth_pitch + y as usize * row_pitch; - if linear { - let tiled_row = base + u64::from(y) * u64::from(m.pitch) * n as u64; - copy_run::(src, dst, tiled_row as usize, linear_row, bytes); - } else { - let tiled_row = (base + self.row(m, y, inner)) as usize; - match chunk_size { - Some(8) if tiled_row.is_multiple_of(8) => { - copy_runs::<8, DETILE>(src, dst, body, tiled_row, linear_row) - } - Some(16) if tiled_row.is_multiple_of(16) => { - copy_runs::<16, DETILE>(src, dst, body, tiled_row, linear_row) - } - Some(32) if tiled_row.is_multiple_of(32) => { - copy_runs::<32, DETILE>(src, dst, body, tiled_row, linear_row) + + // The low contiguous x bits repeat in every block. Tail origins are + // micro-tile aligned, so only the last run of a row can be short. + let xs = &self.pattern[0]; + let run = xs + .iter() + .enumerate() + .take_while(|&(x, &offset)| offset as usize == x * n) + .count(); + match run * n { + 4 => self.walk_runs::<4>(m, first, row_pitch, depth_pitch, &mut copy), + 8 => self.walk_runs::<8>(m, first, row_pitch, depth_pitch, &mut copy), + 16 => self.walk_runs::<16>(m, first, row_pitch, depth_pitch, &mut copy), + 32 => self.walk_runs::<32>(m, first, row_pitch, depth_pitch, &mut copy), + _ => unreachable!("GFX10 contiguous run width"), + } + Ok(()) + } + + fn walk_runs( + &self, + m: &Mip, + first: u32, + row_pitch: usize, + depth_pitch: usize, + copy: &mut Buffers<'_>, + ) { + let n = self.desc.element_bytes as usize; + let run = N / n; + let bytes = m.width as usize * n; + // Column indices are in N-byte units, shared by all rows and slices. + let columns: Vec<_> = (0..bytes / N) + .map(|i| self.column(m, (i * run) as u32) as usize / N) + .collect(); + let end = columns.len() * N; + let last = (end < bytes).then(|| self.column(m, (end / n) as u32) as usize); + // Pair short runs to keep writes to nearby tiled addresses together. + // Wide runs already transfer 32 bytes per visit. + let rows = if N < 32 { 2 } else { 1 }; + let bd = self.block[2]; + for slab in (0..m.depth).step_by(bd as usize) { + let (slice, _) = self.split(first + slab); + let base = u64::from(slice) * self.slice_size + m.offset; + for y in (0..m.height).step_by(rows) { + // Tail origins and row pairs both start on even y. The low y + // bit gives the distance to the second row in this block. + let second = (rows == 2 && y + 1 < m.height).then(|| self.pattern[1][1] as usize); + let row = base + self.row(m, y, 0); + // Depth slices in this slab share the same physical blocks. + for inner in 0..bd.min(m.depth - slab) { + let t = row as usize + self.pattern[2][inner as usize] as usize; + let l = (slab + inner) as usize * depth_pitch + y as usize * row_pitch; + copy.runs::(&columns, t, l, row_pitch, second); + if let Some(col) = last { + copy.span(t + col, l + end, bytes - end); + if let Some(dy) = second { + copy.span(t + dy + col, l + row_pitch + end, bytes - end); } - _ => copy_runs::<0, DETILE>(src, dst, body, tiled_row, linear_row), } - copy_runs::<0, DETILE>(src, dst, last, tiled_row, linear_row); } } } - Ok(()) } } -fn copy_run( - src: &[u8], - dst: &mut [u8], - tiled: usize, - linear: usize, - len: usize, -) { - let (from, to) = if DETILE { - (tiled, linear) - } else { - (linear, tiled) - }; - dst[to..][..len].copy_from_slice(&src[from..][..len]); +enum Buffers<'a> { + Detile { + tiled: &'a [u8], + linear: &'a mut [u8], + }, + Tile { + tiled: &'a mut [u8], + linear: &'a [u8], + }, } -/// A row of equal runs is contiguous in linear memory. Treat it as a -/// slice of arrays so only the tiled chunk needs a bounds check per copy. -/// Use the byte ranges for irregular runs and the last run of each row. -fn copy_runs( - src: &[u8], - dst: &mut [u8], - runs: &[(usize, usize, usize)], - tiled_row: usize, - linear_row: usize, -) { - if N == 0 { - for &(tiled, linear, len) in runs { - copy_run::(src, dst, tiled_row + tiled, linear_row + linear, len); - } - } else if DETILE { - let tiled = src.as_chunks::().0; - let linear = dst[linear_row..][..runs.len() * N].as_chunks_mut::().0; - for (out, &(offset, ..)) in linear.iter_mut().zip(runs) { - *out = tiled[(tiled_row + offset) / N]; +impl Buffers<'_> { + fn span(&mut self, t: usize, l: usize, len: usize) { + match self { + Self::Detile { tiled, linear } => { + linear[l..][..len].copy_from_slice(&tiled[t..][..len]) + } + Self::Tile { tiled, linear } => tiled[t..][..len].copy_from_slice(&linear[l..][..len]), } - } else { - let tiled = dst.as_chunks_mut::().0; - let linear = src[linear_row..][..runs.len() * N].as_chunks::().0; - for (input, &(offset, ..)) in linear.iter().zip(runs) { - tiled[(tiled_row + offset) / N] = *input; + } + + fn runs( + &mut self, + columns: &[usize], + t: usize, + l: usize, + pitch: usize, + second: Option, + ) { + let len = columns.len() * N; + match self { + Self::Detile { tiled, linear } => { + let (src, _) = tiled[t..].as_chunks::(); + let (top, rest) = linear[l..].split_at_mut(len); + let (top, _) = top.as_chunks_mut::(); + if let Some(dy) = second { + let (bottom, _) = rest[pitch - len..][..len].as_chunks_mut::(); + for ((a, b), &x) in top.iter_mut().zip(bottom).zip(columns) { + *a = src[x]; + *b = src[x + dy / N]; + } + } else { + for (out, &x) in top.iter_mut().zip(columns) { + *out = src[x]; + } + } + } + Self::Tile { tiled, linear } => { + let (dst, _) = tiled[t..].as_chunks_mut::(); + let (top, rest) = linear[l..].split_at(len); + let (top, _) = top.as_chunks::(); + if let Some(dy) = second { + let (bottom, _) = rest[pitch - len..][..len].as_chunks::(); + for ((a, b), &x) in top.iter().zip(bottom).zip(columns) { + dst[x] = *a; + dst[x + dy / N] = *b; + } + } else { + for (value, &x) in top.iter().zip(columns) { + dst[x] = *value; + } + } + } } } } @@ -927,6 +990,83 @@ mod tests { } } + /// Active bytes copied per second on synthetic surfaces: + /// `cargo test --release -p victory-formats detile_throughput -- --ignored --nocapture`. + #[test] + #[ignore = "benchmark"] + fn detile_throughput() { + use std::hint::black_box; + use std::time::Instant; + + let bc = |n, swizzle| Desc { + block: 4, + ..desc(4096, 4096, 13, n, swizzle) + }; + let cases = [ + ("BC7 64KB_D", bc(16, Swizzle::SW_64KB_D), 0), + ("BC7 64KB_S", bc(16, Swizzle::SW_64KB_S), 0), + ("BC1 64KB_D", bc(8, Swizzle::SW_64KB_D), 0), + ( + "RGBA8 64KB_D", + desc(1920, 1080, 1, 4, Swizzle::SW_64KB_D), + 0, + ), + ("RGBA8 4KB_S", desc(1920, 1080, 1, 4, Swizzle::SW_4KB_S), 0), + ("BC7 64KB_D tail", bc(16, Swizzle::SW_64KB_D), 5), + ( + "RGBA8 3D 4KB_S", + Desc { + dimension: Dimension::Tex3d, + depth: 64, + ..desc(128, 128, 1, 4, Swizzle::SW_4KB_S) + }, + 0, + ), + ]; + for (name, d, mip) in cases { + let layout = Layout::new(&d).unwrap(); + let m = layout.mips[mip as usize]; + let row = m.width as usize * d.element_bytes as usize; + let plane = row * m.height as usize; + let bytes = plane * m.depth as usize; + let mut tiled = vec![0x5a; layout.size as usize]; + let mut linear = vec![0; bytes]; + let runs = (256usize << 20).div_ceil(bytes); + for detile in [true, false] { + let mut copy = || { + if detile { + layout.detile( + black_box(&tiled), + mip, + 0, + black_box(&mut linear), + row, + plane, + ) + } else { + layout.tile( + black_box(&mut tiled), + mip, + 0, + black_box(&linear), + row, + plane, + ) + } + .unwrap(); + }; + copy(); + let start = Instant::now(); + for _ in 0..runs { + copy(); + } + let rate = (bytes * runs) as f64 / start.elapsed().as_secs_f64() / 1e6; + let op = if detile { "detile" } else { "tile" }; + println!("{name} {op}: {rate:.1} MB/s"); + } + } + } + /// Sizes past the hardware's 16384 texels per side are refused instead /// of overflowing the size arithmetic (found by random-input testing). #[test] @@ -997,10 +1137,38 @@ mod tests { ..desc(64, 32, 7, 4, Swizzle::SW_4KB_S) }, ]; - for d in cases { + // Odd dimensions exercise a short final run and an unpaired row. + // Cross a block boundary for every addressable pattern and run width. + let arrays = [ + Swizzle::LINEAR, + Swizzle::SW_256B_S, + Swizzle::SW_256B_D, + Swizzle::SW_4KB_S, + Swizzle::SW_4KB_D, + Swizzle::SW_64KB_S, + Swizzle::SW_64KB_D, + ] + .into_iter() + .flat_map(|swizzle| { + [1, 2, 4, 8, 16].map(|n| Desc { + depth: 2, + ..desc(259, 19, 8, n, swizzle) + }) + }); + let volumes = [Swizzle::LINEAR, Swizzle::SW_4KB_S, Swizzle::SW_64KB_S] + .into_iter() + .flat_map(|swizzle| { + [1, 2, 4, 8, 16].map(|n| Desc { + dimension: Dimension::Tex3d, + depth: 35, + ..desc(37, 19, 6, n, swizzle) + }) + }); + for d in cases.into_iter().chain(arrays).chain(volumes) { let l = Layout::new(&d).unwrap(); let mut seen = vec![false; l.size as usize]; - let mut tiled = vec![0u8; l.size as usize]; + let mut tiled = vec![0xcc; l.size as usize]; + let mut expected = tiled.clone(); let n = d.element_bytes as usize; let slices = if d.dimension == Dimension::Tex3d { 1 @@ -1014,7 +1182,9 @@ mod tests { let row = active + 13; let plane = row * m.height as usize + 11; let linear: Vec = (0..plane * m.depth as usize) - .map(|i| (i * 7 + mip as usize * 13 + slice as usize) as u8) + .map(|i| { + ((i ^ (i >> 8) ^ (i >> 16)) + mip as usize * 13 + slice as usize) as u8 + }) .collect(); l.tile(&mut tiled, mip, slice, &linear, row, plane).unwrap(); let mut back = vec![0xcc; linear.len()]; @@ -1028,7 +1198,7 @@ mod tests { for x in 0..m.width { let at = l.offset(mip, x, y, slice + z).unwrap() as usize; let source = start + x as usize * n; - assert_eq!(tiled[at..at + n], linear[source..source + n]); + expected[at..at + n].copy_from_slice(&linear[source..source + n]); assert!(at + n <= seen.len(), "{d:?} mip {mip} outside"); assert!(at.is_multiple_of(n)); assert!(!seen[at], "{d:?} mip {mip} ({x},{y},{z}) twice"); @@ -1041,6 +1211,7 @@ mod tests { .all(|&b| b == 0xcc) ); } + assert!(tiled == expected, "{d:?} mip {mip} slice {slice}"); } } } @@ -1243,6 +1414,108 @@ mod tests { ); } + #[test] + fn copy_errors_precede_writes_in_both_directions() { + for (swizzle, samples, mip, slice, error) in [ + (Swizzle::SW_64KB_D_X, 1, 0, 0, Error::Unsupported), + (Swizzle::SW_64KB_S_X, 1, 0, 0, Error::Unsupported), + (Swizzle::SW_64KB_S_T, 1, 0, 0, Error::Unsupported), + (Swizzle::SW_64KB_D, 2, 0, 0, Error::Unsupported), + (Swizzle::SW_64KB_D_X, 1, 1, 0, Error::Invalid), + (Swizzle::SW_64KB_S_X, 1, 0, 1, Error::Invalid), + (Swizzle::SW_64KB_D, 1, 0, 0, Error::TooSmall), + ] { + let l = Layout::new(&Desc { + samples, + ..desc(17, 9, 1, 16, swizzle) + }) + .unwrap(); + let mut tiled = [0xac]; + let mut linear = [0xdc]; + assert_eq!(l.tile(&mut tiled, mip, slice, &linear, 0, 0), Err(error)); + assert_eq!(l.detile(&tiled, mip, slice, &mut linear, 0, 0), Err(error)); + assert_eq!((tiled, linear), ([0xac], [0xdc])); + } + let l = volume_pitch_layout(Swizzle::SW_4KB_S); + assert_eq!(l.tile(&mut [], 0, 1, &[], 0, 0), Err(Error::Invalid)); + assert_eq!(l.detile(&[], 0, 1, &mut [], 0, 0), Err(Error::Invalid)); + } + + fn volume_pitch_layout(swizzle: Swizzle) -> Layout { + Layout::new(&Desc { + dimension: Dimension::Tex3d, + depth: 2, + ..desc(4, 2, 1, 4, swizzle) + }) + .unwrap() + } + + #[test] + fn depth_pitch_rejects_overlapping_detile() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + let tiled = vec![0x42; l.size as usize]; + let mut linear = [0xcc; 72]; + + for pitch in [0, 16, 35] { + assert_eq!( + l.detile(&tiled, 0, 0, &mut linear, 20, pitch), + Err(Error::TooSmall), + "{swizzle:?}, pitch {pitch}" + ); + + assert_eq!(linear, [0xcc; 72]); + } + } + } + + #[test] + fn depth_pitch_rejects_overlapping_tile() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + let mut tiled = vec![0xcc; l.size as usize]; + let linear = [0x42; 72]; + + for pitch in [0, 16, 35] { + assert_eq!( + l.tile(&mut tiled, 0, 0, &linear, 20, pitch), + Err(Error::TooSmall), + "{swizzle:?}, pitch {pitch}" + ); + + assert!(tiled.iter().all(|&b| b == 0xcc)); + } + } + } + + #[test] + fn depth_pitch_accepts_exact_and_padded_slices() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + + for pitch in [36usize, 40, 64] { + let len = pitch + 36; + let mut tiled = vec![0; l.size as usize]; + + let linear: Vec = (0..len).map(|i| i as u8).collect(); + + let mut back = vec![0xcc; len]; + + l.tile(&mut tiled, 0, 0, &linear, 20, pitch).unwrap(); + + l.detile(&tiled, 0, 0, &mut back, 20, pitch).unwrap(); + + for z in 0..2 { + for y in 0..2 { + let start = z * pitch + y * 20; + + assert_eq!(back[start..start + 16], linear[start..start + 16]); + } + } + } + } + } + #[test] fn volume_copy_rejects_overflowing_depth_pitch() { let layout = Layout::new(&Desc {