Formats: share immutable texture swizzle tables
Co-Authored-By: GPT 6.2 Sol <[email protected]> (cherry picked from commit 64b80c310941c8c105c9ac78fe4c29004f5e88a4)
This commit is contained in:
1 parent
eabb82601c
commit
8961e15fc7
2 files changed
+147
-6
No files matched your search
@@ -0,0 +1,89 @@
|
|||||||
|
//! Run with `cargo run --release -p victory-formats --example texture_layout_bench`.
|
||||||
|
|
||||||
|
use std::hint::black_box;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use victory_formats::texture::{Dimension, Swizzle, Texture, TextureDesc};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
const CALLS: u64 = 50_000;
|
||||||
|
let base = TextureDesc {
|
||||||
|
dimension: Dimension::Tex2d,
|
||||||
|
width: 512,
|
||||||
|
height: 512,
|
||||||
|
depth_or_array: 1,
|
||||||
|
mips: 0,
|
||||||
|
format: 98, // BC7
|
||||||
|
samples: 1,
|
||||||
|
swizzle: Swizzle::SW_64KB_D,
|
||||||
|
};
|
||||||
|
let cases = [
|
||||||
|
("BC7 64KB_D", base),
|
||||||
|
(
|
||||||
|
"BC1 64KB_S",
|
||||||
|
TextureDesc {
|
||||||
|
format: 71,
|
||||||
|
swizzle: Swizzle::SW_64KB_S,
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"RGBA8 4KB_D",
|
||||||
|
TextureDesc {
|
||||||
|
format: 28,
|
||||||
|
swizzle: Swizzle::SW_4KB_D,
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"R8 64KB_S",
|
||||||
|
TextureDesc {
|
||||||
|
format: 61,
|
||||||
|
swizzle: Swizzle::SW_64KB_S,
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"RGBA8 volume 64KB_S",
|
||||||
|
TextureDesc {
|
||||||
|
dimension: Dimension::Tex3d,
|
||||||
|
width: 128,
|
||||||
|
height: 128,
|
||||||
|
depth_or_array: 64,
|
||||||
|
format: 28,
|
||||||
|
swizzle: Swizzle::SW_64KB_S,
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"RGBA8 linear",
|
||||||
|
TextureDesc {
|
||||||
|
format: 28,
|
||||||
|
swizzle: Swizzle::LINEAR,
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (name, desc) in cases {
|
||||||
|
let mut samples = [0.0; 7];
|
||||||
|
black_box(Texture::new(black_box(&desc)).unwrap());
|
||||||
|
let mut checksum = 0u64;
|
||||||
|
for sample in &mut samples {
|
||||||
|
let start = Instant::now();
|
||||||
|
for i in 0..CALLS {
|
||||||
|
let desc = TextureDesc {
|
||||||
|
width: desc.width + (i % 32) as u32,
|
||||||
|
..desc
|
||||||
|
};
|
||||||
|
let texture = black_box(Texture::new(black_box(&desc)).unwrap());
|
||||||
|
let layout = &texture.planes[0].layout;
|
||||||
|
checksum =
|
||||||
|
checksum.wrapping_add(texture.size + layout.offset(0, 7, 11, 0).unwrap());
|
||||||
|
}
|
||||||
|
*sample = start.elapsed().as_secs_f64() * 1e9 / CALLS as f64;
|
||||||
|
}
|
||||||
|
samples.sort_by(f64::total_cmp);
|
||||||
|
println!("{name:22} {:8.1} ns/layout {checksum}", samples[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,8 @@
|
|||||||
//! Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
//! Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
//! EXPRESS OR IMPLIED.
|
//! EXPRESS OR IMPLIED.
|
||||||
|
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
/// A swizzle mode, by its hardware number (addrlib's `ADDR_SW_*` order).
|
/// A swizzle mode, by its hardware number (addrlib's `ADDR_SW_*` order).
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Swizzle(pub u32);
|
pub struct Swizzle(pub u32);
|
||||||
@@ -174,11 +176,14 @@ pub struct Layout {
|
|||||||
/// The first mip in the tail; `mips.len()` without one.
|
/// The first mip in the tail; `mips.len()` without one.
|
||||||
pub first_tail_mip: u32,
|
pub first_tail_mip: u32,
|
||||||
pub mips: Vec<Mip>,
|
pub mips: Vec<Mip>,
|
||||||
/// Byte offsets inside a block of each element coordinate bit, per
|
/// Byte offsets inside a block for each coordinate, per axis.
|
||||||
/// axis; empty for linear and unsupported modes.
|
/// Empty for linear and unsupported modes.
|
||||||
pattern: [Vec<u32>; 3],
|
pattern: &'static SwizzlePattern,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SwizzlePattern = [Vec<u32>; 3];
|
||||||
|
static EMPTY_PATTERN: SwizzlePattern = [const { Vec::new() }; 3];
|
||||||
|
|
||||||
/// Micro tile (256 bytes) patterns by log2 of the element size: for each
|
/// Micro tile (256 bytes) patterns by log2 of the element size: for each
|
||||||
/// address bit, the element coordinate bit it takes (`x0`, `y1`, `z0`...),
|
/// address bit, the element coordinate bit it takes (`x0`, `y1`, `z0`...),
|
||||||
/// `--` for the bits inside an element. Thin `S`, thin `D`, thick `S`
|
/// `--` for the bits inside an element. Thin `S`, thin `D`, thick `S`
|
||||||
@@ -247,6 +252,18 @@ fn spread(bits: &[u32]) -> Vec<u32> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tables depend only on the micro-tile kind, block size and element size.
|
||||||
|
/// Texture extents, mip placement and array slices only affect the base
|
||||||
|
/// address, so layouts with the same key can share these immutable tables.
|
||||||
|
fn shared_pattern(kind: usize, element_log2: u32, block_log2: u32) -> &'static SwizzlePattern {
|
||||||
|
// Indexed by MICRO kind, block size (256 B, 4 KiB, 64 KiB), then element size.
|
||||||
|
static PATTERNS: [[[OnceLock<SwizzlePattern>; 5]; 3]; 3] =
|
||||||
|
[const { [const { [const { OnceLock::new() }; 5] }; 3] }; 3];
|
||||||
|
let block_index = ((block_log2 - 8) / 4) as usize;
|
||||||
|
PATTERNS[kind][block_index][element_log2 as usize]
|
||||||
|
.get_or_init(|| pattern(kind, element_log2, block_log2).map(|bits| spread(&bits)))
|
||||||
|
}
|
||||||
|
|
||||||
const MAX_MIPS: u32 = 16;
|
const MAX_MIPS: u32 = 16;
|
||||||
|
|
||||||
/// The largest width, height and depth (or array size) accepted, in texels.
|
/// The largest width, height and depth (or array size) accepted, in texels.
|
||||||
@@ -323,7 +340,7 @@ impl Layout {
|
|||||||
alignment: 256,
|
alignment: 256,
|
||||||
first_tail_mip: d.mips,
|
first_tail_mip: d.mips,
|
||||||
mips,
|
mips,
|
||||||
pattern: Default::default(),
|
pattern: &EMPTY_PATTERN,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -491,9 +508,9 @@ impl Layout {
|
|||||||
} else {
|
} else {
|
||||||
usize::from(d.swizzle.display())
|
usize::from(d.swizzle.display())
|
||||||
};
|
};
|
||||||
pattern(kind, element_log2, block_log2).map(|bits| spread(&bits))
|
shared_pattern(kind, element_log2, block_log2)
|
||||||
} else {
|
} else {
|
||||||
Default::default()
|
&EMPTY_PATTERN
|
||||||
};
|
};
|
||||||
Layout {
|
Layout {
|
||||||
desc: d,
|
desc: d,
|
||||||
@@ -1106,6 +1123,41 @@ mod tests {
|
|||||||
assert_eq!(l.offset(0, 0, 16, 0), Ok(8192));
|
assert_eq!(l.offset(0, 0, 16, 0), Ok(8192));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn layouts_share_identical_swizzle_tables() {
|
||||||
|
for (swizzle, kind, dimension, block_log2) in [
|
||||||
|
(Swizzle::SW_256B_S, 0, Dimension::Tex2d, 8),
|
||||||
|
(Swizzle::SW_256B_D, 1, Dimension::Tex2d, 8),
|
||||||
|
(Swizzle::SW_4KB_S, 0, Dimension::Tex2d, 12),
|
||||||
|
(Swizzle::SW_4KB_D, 1, Dimension::Tex2d, 12),
|
||||||
|
(Swizzle::SW_64KB_S, 0, Dimension::Tex2d, 16),
|
||||||
|
(Swizzle::SW_64KB_D, 1, Dimension::Tex2d, 16),
|
||||||
|
(Swizzle::SW_4KB_S, 2, Dimension::Tex3d, 12),
|
||||||
|
(Swizzle::SW_64KB_S, 2, Dimension::Tex3d, 16),
|
||||||
|
] {
|
||||||
|
for element_log2 in 0..5 {
|
||||||
|
let d = Desc {
|
||||||
|
dimension,
|
||||||
|
depth: 8,
|
||||||
|
..desc(128, 96, 7, 1 << element_log2, swizzle)
|
||||||
|
};
|
||||||
|
let first = Layout::new(&d).unwrap();
|
||||||
|
let second = Layout::new(&Desc {
|
||||||
|
width: 32,
|
||||||
|
height: 24,
|
||||||
|
mips: 3,
|
||||||
|
depth: 1,
|
||||||
|
..d
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let expected = pattern(kind, element_log2, block_log2).map(|bits| spread(&bits));
|
||||||
|
assert_eq!(first.pattern, &expected);
|
||||||
|
assert!(std::ptr::eq(first.pattern, second.pattern));
|
||||||
|
assert!(std::ptr::eq(first.pattern, first.clone().pattern));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A 256x256 BC7 chain in 64 KiB `D`: one tail block holding mips 1-8,
|
/// A 256x256 BC7 chain in 64 KiB `D`: one tail block holding mips 1-8,
|
||||||
/// then mip 0; mip 1 sits at x = 32 elements, 32 KiB into the tail.
|
/// then mip 0; mip 1 sits at x = 32 elements, 32 KiB into the tail.
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in new issue
Block a user