Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 128 additions & 22 deletions d3d12/crates/leonida-formats/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,39 @@ impl Layout {
// 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))
matches!(size, 4 | 8 | 16 | 32 | 64)
&& body.iter().all(|&(offset, _, len)| {
len == size && offset.is_multiple_of(size) && offset / size <= u32::MAX as usize
})
});
const STACK_CHUNKS: usize = 32;
let mut stack_offs = [0u32; STACK_CHUNKS];
let mut heap_offs = Vec::new();
let mut max_chunk_off = 0usize;
let chunk_offsets: &[u32] = if let Some(size) = chunk_size {
if body.len() <= STACK_CHUNKS {
for (dst_off, &(offset, _, _)) in stack_offs[..body.len()].iter_mut().zip(body) {
let c = offset / size;
if c > max_chunk_off {
max_chunk_off = c;
}
*dst_off = c as u32;
}
&stack_offs[..body.len()]
} else {
heap_offs.reserve_exact(body.len());
for &(offset, _, _) in body {
let c = offset / size;
if c > max_chunk_off {
max_chunk_off = c;
}
heap_offs.push(c as u32);
}
&heap_offs
}
} else {
&[]
};
for z in 0..depth {
let (s, inner) = self.split(first + z);
let base = u64::from(s) * self.slice_size + m.offset;
Expand All @@ -743,18 +773,24 @@ impl Layout {
} else {
let tiled_row = (base + self.row(m, y, inner)) as usize;
match chunk_size {
Some(4) if tiled_row.is_multiple_of(4) => {
copy_chunks::<4, DETILE>(src, dst, chunk_offsets, max_chunk_off, tiled_row, linear_row)
}
Some(8) if tiled_row.is_multiple_of(8) => {
copy_runs::<8, DETILE>(src, dst, body, tiled_row, linear_row)
copy_chunks::<8, DETILE>(src, dst, chunk_offsets, max_chunk_off, tiled_row, linear_row)
}
Some(16) if tiled_row.is_multiple_of(16) => {
copy_runs::<16, DETILE>(src, dst, body, tiled_row, linear_row)
copy_chunks::<16, DETILE>(src, dst, chunk_offsets, max_chunk_off, tiled_row, linear_row)
}
Some(32) if tiled_row.is_multiple_of(32) => {
copy_runs::<32, DETILE>(src, dst, body, tiled_row, linear_row)
copy_chunks::<32, DETILE>(src, dst, chunk_offsets, max_chunk_off, tiled_row, linear_row)
}
_ => copy_runs::<0, DETILE>(src, dst, body, tiled_row, linear_row),
Some(64) if tiled_row.is_multiple_of(64) => {
copy_chunks::<64, DETILE>(src, dst, chunk_offsets, max_chunk_off, tiled_row, linear_row)
}
_ => copy_runs::<DETILE>(src, dst, body, tiled_row, linear_row),
}
copy_runs::<0, DETILE>(src, dst, last, tiled_row, linear_row);
copy_runs::<DETILE>(src, dst, last, tiled_row, linear_row);
}
}
}
Expand All @@ -777,31 +813,51 @@ fn copy_run<const DETILE: bool>(
dst[to..][..len].copy_from_slice(&src[from..][..len]);
}

/// 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<const N: usize, const DETILE: bool>(
/// Irregular runs and the last run of each row.
fn copy_runs<const DETILE: bool>(
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::<DETILE>(src, dst, tiled_row + tiled, linear_row + linear, len);
}
} else if DETILE {
for &(tiled, linear, len) in runs {
copy_run::<DETILE>(src, dst, tiled_row + tiled, linear_row + linear, len);
}
}

/// A row of equal `N`-byte runs is contiguous in linear memory. Pre-divided
/// `u32` chunk offsets shrink the offset table 6x (4 B vs 24 B) and a single
/// per-row bounds check lets the inner loop copy without per-chunk checks.
fn copy_chunks<const N: usize, const DETILE: bool>(
src: &[u8],
dst: &mut [u8],
chunk_offsets: &[u32],
max_chunk_off: usize,
tiled_row: usize,
linear_row: usize,
) {
if chunk_offsets.is_empty() {
return;
}
let base = tiled_row / N;
if DETILE {
let tiled = src.as_chunks::<N>().0;
let linear = dst[linear_row..][..runs.len() * N].as_chunks_mut::<N>().0;
for (out, &(offset, ..)) in linear.iter_mut().zip(runs) {
*out = tiled[(tiled_row + offset) / N];
let linear = dst[linear_row..][..chunk_offsets.len() * N].as_chunks_mut::<N>().0;
assert!(base + max_chunk_off < tiled.len());
let tiled_base = &tiled[base..];
for (out, &off) in linear.iter_mut().zip(chunk_offsets) {
// SAFETY: `off as usize <= max_chunk_off` and `base + max_chunk_off < tiled.len()`.
*out = unsafe { *tiled_base.get_unchecked(off as usize) };
}
} else {
let tiled = dst.as_chunks_mut::<N>().0;
let linear = src[linear_row..][..runs.len() * N].as_chunks::<N>().0;
for (input, &(offset, ..)) in linear.iter().zip(runs) {
tiled[(tiled_row + offset) / N] = *input;
let linear = src[linear_row..][..chunk_offsets.len() * N].as_chunks::<N>().0;
assert!(base + max_chunk_off < tiled.len());
let tiled_base = &mut tiled[base..];
for (input, &off) in linear.iter().zip(chunk_offsets) {
// SAFETY: `off as usize <= max_chunk_off` and `base + max_chunk_off < tiled.len()`.
unsafe { *tiled_base.get_unchecked_mut(off as usize) = *input };
}
}
}
Expand Down Expand Up @@ -1510,4 +1566,54 @@ mod tests {
);
assert_eq!(l.offset(0, 64, 0, 0), Err(Error::Invalid));
}

#[test]
#[ignore]
fn texture_walk_bench() {
let cases: &[(&str, u32, u32, u32, u32, Swizzle, u32)] = &[
("8x8_4B_64KB_S", 8, 8, 0, 4, Swizzle::SW_64KB_S, 20_000),
("32x24_4B_64KB_D", 32, 24, 0, 4, Swizzle::SW_64KB_D, 10_000),
("256x256_4B_64KB_D_m5", 256, 256, 5, 4, Swizzle::SW_64KB_D, 20_000),
("256x256_4B_64KB_S", 256, 256, 0, 4, Swizzle::SW_64KB_S, 400),
("256x256_8B_64KB_S", 256, 256, 0, 8, Swizzle::SW_64KB_S, 400),
("256x256_4B_64KB_D", 256, 256, 0, 4, Swizzle::SW_64KB_D, 400),
("256x256_8B_64KB_D", 256, 256, 0, 8, Swizzle::SW_64KB_D, 400),
("512x512_16B_64KB_S", 512, 512, 0, 16, Swizzle::SW_64KB_S, 100),
("512x512_16B_64KB_D", 512, 512, 0, 16, Swizzle::SW_64KB_D, 100),
("1024x1024_8B_64KB_S", 1024, 1024, 0, 8, Swizzle::SW_64KB_S, 50),
("1024x1024_8B_64KB_D", 1024, 1024, 0, 8, Swizzle::SW_64KB_D, 50),
];
for &(name, w, h, mip, bpp, swizzle, iters) in cases {
let mips = if mip > 0 { mip + 1 } else { 1 };
let l = Layout::new(&Desc { mips, ..desc(w, h, 1, bpp, swizzle) }).unwrap();
let mw = (w >> mip).max(1);
let mh = (h >> mip).max(1);
let mut tiled = vec![0x5Au8; l.size as usize];
let pitch = (mw * bpp) as usize;
let mut linear = vec![0xA5u8; pitch * mh as usize];
let bytes = (pitch * mh as usize) as f64 * iters as f64;
// warmup
for _ in 0..4 {
l.detile(&tiled, mip, 0, &mut linear, pitch, 0).unwrap();
l.tile(&mut tiled, mip, 0, &linear, pitch, 0).unwrap();
}
let t0 = std::time::Instant::now();
for _ in 0..iters {
l.detile(std::hint::black_box(&tiled), mip, 0, std::hint::black_box(&mut linear), pitch, 0).unwrap();
}
let dt_detile = t0.elapsed().as_secs_f64();
let t1 = std::time::Instant::now();
for _ in 0..iters {
l.tile(std::hint::black_box(&mut tiled), mip, 0, std::hint::black_box(&linear), pitch, 0).unwrap();
}
let dt_tile = t1.elapsed().as_secs_f64();
eprintln!(
"{name:<24} detile={:7.1} MB/s ({:6.3} us) tile={:7.1} MB/s ({:6.3} us)",
bytes / (1024.0 * 1024.0) / dt_detile,
dt_detile * 1e6 / iters as f64,
bytes / (1024.0 * 1024.0) / dt_tile,
dt_tile * 1e6 / iters as f64,
);
}
}
}