use super::super::context::{Footprint, Tables};
use super::{ERROR, MAX_TEXELS, decode, lerp};
use unityrs_core::limits::Meter;
fn unorm16_to_sf16(p: u32) -> u32 {
if p == 0xffff {
return 0x3c00;
}
if p < 4 {
return p << 8;
}
let lz = p.leading_zeros() - 16;
let p = ((p << (lz + 1)) & 0xffff) >> 6;
p | ((14 - lz) << 10)
}
fn sf16_to_float(h: u32) -> f32 {
let (exp, man) = ((h >> 10) & 31, h & 1023);
match exp {
0 => f32::from(u16::try_from(man).unwrap()) * 2f32.powi(-24),
_ => f32::from_bits(((exp + 112) << 23) | (man << 13)),
}
}
#[allow(
clippy::cast_possible_truncation,
reason = "C's `(int)` conversion, truncating toward 0, is what the store does"
)]
fn store(v16: u32, fused: bool) -> u8 {
let f = sf16_to_float(unorm16_to_sf16(v16)).min(1.0);
let scaled = if fused {
f.mul_add(255.0, 0.5)
} else {
f * 255.0 + 0.5
};
u8::try_from(scaled as i32).unwrap()
}
#[test]
fn the_unorm8_store_keeps_the_byte() {
for v in 0..=255u32 {
assert_eq!(u32::from(store(v * 257, false)), v);
assert_eq!(u32::from(store(v * 257, true)), v);
}
assert_eq!(unorm16_to_sf16(0xffff), 0x3c00);
assert_eq!(sf16_to_float(unorm16_to_sf16(0x8080)), 0.501_953_1);
assert_eq!(unorm16_to_sf16(2), 0x200);
let lane = |c0, c1, w| lerp([c0; 4], [c1; 4], [w; 4])[0];
assert_eq!(lane(0, 0xffff, 0), 0);
assert_eq!(lane(0, 0xffff, 64), 255);
assert_eq!(lane(0, 0xffff, 32), 128);
assert_eq!(lane(0x1010, 0x1010, 17), 0x10);
assert_eq!(
lerp([0, 0, 0x1010, 0xffff], [0xffff; 4], [0, 64, 17, 5]),
[0, 255, 0x4f, 255]
);
for a in 0..=255 {
for b in 0..=255 {
for w in 0..=64 {
let (c0, c1) = (a * 257, b * 257);
let value = (c0 * (64 - u32::from(w)) + c1 * u32::from(w) + 32) >> 6;
assert_eq!(u32::from(lane(c0, c1, w)), value >> 8, "{a} {b} {w}");
}
}
}
}
fn block(fp: (u8, u8), bytes: [u8; 16]) -> Vec<[u8; 4]> {
let fp = Footprint::new(fp.0, fp.1).unwrap();
let mut tables = Tables::new(fp, &Meter::unlimited()).unwrap();
let mut out = [7; 4 * MAX_TEXELS];
decode(&mut tables, &bytes, &mut out, ERROR);
out.as_chunks::<4>()
.0
.iter()
.take(fp.texels())
.copied()
.collect()
}
#[test]
fn void_extent_blocks() {
let colour = [0x00, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde];
let mut ones = [0xff; 16];
ones[0] = 0xfc;
ones[1] = 0xfd;
ones[8..].copy_from_slice(&colour);
assert!(
block((6, 5), ones)
.iter()
.all(|p| *p == [0x12, 0x56, 0x9a, 0xde])
);
let mut hdr = ones;
hdr[1] |= 0x02;
assert!(block((4, 4), hdr).iter().all(|p| *p == ERROR));
let mut reserved = ones;
reserved[1] &= !0x08;
assert!(block((4, 4), reserved).iter().all(|p| *p == ERROR));
let mut ordered = ones;
ordered[1] = 0x0d;
ordered[2] = 0;
ordered[5] = 0;
assert!(
block((4, 4), ordered)
.iter()
.all(|p| *p == [0x12, 0x56, 0x9a, 0xde])
);
let mut empty = ones;
empty[7] = 0;
assert!(block((4, 4), empty).iter().all(|p| *p == ERROR));
}
#[test]
fn illegal_modes_are_error_blocks() {
for mode in [0u16, 4, 0x1fd] {
let mut b = [0x5a; 16];
b[0] = (mode & 0xff) as u8;
b[1] = (b[1] & !7) | (mode >> 8) as u8;
assert!(block((4, 4), b).iter().all(|p| *p == ERROR), "mode {mode}");
}
let meter = Meter::unlimited();
let mut small = Tables::new(Footprint::new(4, 4).unwrap(), &meter).unwrap();
let mut large = Tables::new(Footprint::new(12, 12).unwrap(), &meter).unwrap();
assert!(small.mode(4).is_none() && large.mode(4).is_some());
}
#[test]
fn error_status_matches_the_error_blocks() {
let mut ones = [0xff; 16];
ones[0] = 0xfc;
ones[1] = 0xfd;
let mut reserved = ones;
reserved[1] &= !0x08;
let mut data = ones.to_vec();
data.extend_from_slice(&reserved);
data.extend_from_slice(&[0; 16]);
data.push(0);
assert_eq!(
crate::astc_ldr_error_blocks(4, 4, &data),
Some(vec![false, true, true])
);
assert_eq!(crate::astc_ldr_error_blocks(7, 7, &data), None);
}