Merge pull request #23 from CuteBooty/fix/bc7-rotation
fix(formats): BC7 rotation only in modes 4 and 5 (BCPack)
This commit is contained in:
1 file changed
+107
-2
@@ -744,10 +744,19 @@ fn pbits(mode: u16) -> bool {
|
|||||||
matches!(mode, 0 | 1 | 3 | 6 | 7)
|
matches!(mode, 0 | 1 | 3 | 6 | 7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Channel rotation bits; only BC7 modes 4 and 5 have a rotation field.
|
||||||
|
fn rotation(ctrl: u16) -> usize {
|
||||||
|
if matches!(ctrl & 7, 4 | 5) {
|
||||||
|
usize::from(ctrl >> 3 & 3)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Channel precisions: the p-bit is red's lowest bit; rotation swaps alpha
|
/// Channel precisions: the p-bit is red's lowest bit; rotation swaps alpha
|
||||||
/// with the rotated channel.
|
/// with the rotated channel.
|
||||||
fn bc7_precision(ctrl: u16) -> [u8; 4] {
|
fn bc7_precision(ctrl: u16) -> [u8; 4] {
|
||||||
let (mode, rot) = (ctrl & 7, usize::from(ctrl >> 3 & 3));
|
let (mode, rot) = (ctrl & 7, rotation(ctrl));
|
||||||
let mut p = BC7_BITS[usize::from(mode)];
|
let mut p = BC7_BITS[usize::from(mode)];
|
||||||
if rot != 0 {
|
if rot != 0 {
|
||||||
p.swap(3, rot - 1);
|
p.swap(3, rot - 1);
|
||||||
@@ -852,7 +861,7 @@ fn bc7_pack(ep: &Bc7Ep, ix: &Bc7Ix, prec: [u8; 4], out: &mut [u8]) {
|
|||||||
let mode = ctrl & 7;
|
let mode = ctrl & 7;
|
||||||
let subsets = usize::from((ctrl & 0xff) >> 6);
|
let subsets = usize::from((ctrl & 0xff) >> 6);
|
||||||
let part = usize::from(ctrl >> 8 & 63);
|
let part = usize::from(ctrl >> 8 & 63);
|
||||||
let rot = usize::from(ctrl >> 3 & 3);
|
let rot = rotation(ctrl);
|
||||||
let mut w = Bits::default();
|
let mut w = Bits::default();
|
||||||
w.put(1 << mode, u32::from(mode) + 1);
|
w.put(1 << mode, u32::from(mode) + 1);
|
||||||
match mode {
|
match mode {
|
||||||
@@ -1341,6 +1350,102 @@ mod tests {
|
|||||||
assert_eq!(dst, w.v.to_le_bytes());
|
assert_eq!(dst, w.v.to_le_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bc7_rotation_applies_only_to_modes_4_and_5() {
|
||||||
|
const BC7_WIDTHS: [u8; 16] = [3, 3, 7, 7, 7, 7, 7, 6, 6, 4, 5, 7, 7, 7, 7, 4];
|
||||||
|
const CTRL7_OFFSET: usize = MODELS * BUCKETS * 2 + 256 + 64 + 256 + 64 + 64;
|
||||||
|
let tables_for = |ctrl: u16| {
|
||||||
|
let mut raw = tables();
|
||||||
|
raw[CTRL7_OFFSET..CTRL7_OFFSET + 2].copy_from_slice(&ctrl.to_le_bytes());
|
||||||
|
let t = Tables::parse(&raw).unwrap();
|
||||||
|
assert_eq!(t.ctrl7[0], ctrl);
|
||||||
|
t
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut record = vec![0u8; 32];
|
||||||
|
for (i, &val) in [11, 23, 5, 19, 13, 29, 7, 17, 3, 25, 9, 21, 15, 31, 1, 12]
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
{
|
||||||
|
record[4 + i] = zigzag(val);
|
||||||
|
}
|
||||||
|
record[20] = zigzag(6);
|
||||||
|
record[21] = zigzag(14);
|
||||||
|
|
||||||
|
// Modes without a BC7 rotation field must ignore ctrl bits 4:3 instead of
|
||||||
|
// panicking on shift overflow (modes 0/1/3 with rot=1) or corrupting channels.
|
||||||
|
for (mode, subsets) in [(0u16, 3u16), (1, 2), (2, 3), (3, 2), (6, 1), (7, 2)] {
|
||||||
|
let base_ctrl = (subsets << 6) | mode;
|
||||||
|
let base_tables = tables_for(base_ctrl);
|
||||||
|
let base_src = stream(&base_tables, &BC7_WIDTHS, 2, std::slice::from_ref(&record));
|
||||||
|
let mut want = [0u8; 16];
|
||||||
|
decode(7, &base_src, &mut want, &base_tables).unwrap();
|
||||||
|
|
||||||
|
for rot in 1..=3u16 {
|
||||||
|
let ctrl = base_ctrl | (rot << 3);
|
||||||
|
let t = tables_for(ctrl);
|
||||||
|
let src = stream(&t, &BC7_WIDTHS, 2, std::slice::from_ref(&record));
|
||||||
|
let mut got = [0u8; 16];
|
||||||
|
decode(7, &src, &mut got, &t).unwrap();
|
||||||
|
assert_eq!(got, want, "mode {mode} rot {rot}");
|
||||||
|
assert_eq!(
|
||||||
|
bc7_precision(ctrl),
|
||||||
|
bc7_precision(base_ctrl),
|
||||||
|
"mode {mode} rot {rot}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modes 4 and 5 do have a 2-bit rotation field: rotation swaps alpha with channel rot-1.
|
||||||
|
for (mode, base_bits) in [(4u16, [5u8, 5, 5, 6]), (5u16, [7u8, 7, 7, 8])] {
|
||||||
|
for rot in 1..=3usize {
|
||||||
|
let ctrl = (1 << 6) | ((rot as u16) << 3) | mode;
|
||||||
|
let mut expected_prec = base_bits;
|
||||||
|
expected_prec.swap(3, rot - 1);
|
||||||
|
assert_eq!(bc7_precision(ctrl), expected_prec, "mode {mode} rot {rot}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode mode 4/5 with rot=1 (swap R and A) and check the packed BC7 block.
|
||||||
|
let ctrl = (1 << 6) | (1 << 3) | mode;
|
||||||
|
let t = tables_for(ctrl);
|
||||||
|
let mut s = vec![0u8; 32];
|
||||||
|
s[4] = zigzag(0x21); // stored R0 (holds alpha precision after rot=1)
|
||||||
|
s[5] = zigzag(0x12); // stored R1
|
||||||
|
s[6] = zigzag(0x05); // stored G0
|
||||||
|
s[7] = zigzag(0x06); // stored G1
|
||||||
|
s[8] = zigzag(0x09); // stored B0
|
||||||
|
s[9] = zigzag(0x0a); // stored B1
|
||||||
|
s[10] = zigzag(0x0d); // stored A0 (holds color precision after rot=1)
|
||||||
|
s[11] = zigzag(0x0e); // stored A1
|
||||||
|
let src = stream(&t, &BC7_WIDTHS, 2, &[s]);
|
||||||
|
let mut dst = [0u8; 16];
|
||||||
|
decode(7, &src, &mut dst, &t).unwrap();
|
||||||
|
|
||||||
|
let color_bits = u32::from(base_bits[0]);
|
||||||
|
let alpha_bits = u32::from(base_bits[3]);
|
||||||
|
let mut w = Bits::default();
|
||||||
|
w.put(1 << mode, u32::from(mode) + 1);
|
||||||
|
w.put(1, 2); // rotation = 1
|
||||||
|
if mode == 4 {
|
||||||
|
w.put(0, 1); // indexSelectionBit = 0
|
||||||
|
}
|
||||||
|
// Endpoint order after rot=1 swaps indices 3 and 0: [3 (R), 1 (G), 2 (B), 0 (A)].
|
||||||
|
for (v, n) in [
|
||||||
|
(0x0d, color_bits),
|
||||||
|
(0x0e, color_bits),
|
||||||
|
(0x05, color_bits),
|
||||||
|
(0x06, color_bits),
|
||||||
|
(0x09, color_bits),
|
||||||
|
(0x0a, color_bits),
|
||||||
|
(0x21, alpha_bits),
|
||||||
|
(0x12, alpha_bits),
|
||||||
|
] {
|
||||||
|
w.put(v, n);
|
||||||
|
}
|
||||||
|
assert_eq!(dst, w.v.to_le_bytes(), "mode {mode} rot 1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn malformed_input_is_an_error() {
|
fn malformed_input_is_an_error() {
|
||||||
let t = Tables::parse(&tables()).unwrap();
|
let t = Tables::parse(&tables()).unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user