use crate::place::put_blocks;
type Lanes = [u16; 8];
#[allow(clippy::indexing_slicing, reason = "x is below S, the array's length")]
const fn axis<const S: usize>() -> [[u16; 3]; S] {
let mut out = [[0; 3]; S];
#[allow(clippy::cast_possible_truncation, reason = "a block side, 4 or 8")]
let (size, half) = (S as u16, S as u16 / 2);
let mut x = 0;
while x < S {
#[allow(clippy::cast_possible_truncation, reason = "below the side")]
let xi = x as u16;
out[x] = if xi <= half {
[half - xi, half + xi, 0]
} else {
[0, size + half - xi, xi - half]
};
x += 1;
}
out
}
fn word(data: &[u8], n: usize) -> u64 {
let at = n.saturating_mul(8);
data.get(at..at.saturating_add(8))
.and_then(|b| b.first_chunk::<8>())
.map_or(0, |b| u64::from_le_bytes(*b))
}
fn colour(c: u16, a: bool) -> [u16; 4] {
let opaque = [
(c >> 10) & 31,
(c >> 5) & 31,
if a {
(c & 0x1e) | ((c >> 4) & 1)
} else {
c & 31
},
15,
];
let translucent = [
((c >> 7) & 0x1e) | ((c >> 11) & 1),
((c >> 3) & 0x1e) | ((c >> 7) & 1),
if a {
((c << 1) & 0x1c) | ((c >> 2) & 3)
} else {
((c << 1) & 0x1e) | ((c >> 3) & 1)
},
(c >> 11) & 0x0e,
];
std::hint::select_unpredictable(c & 0x8000 != 0, opaque, translucent)
}
fn colours(word: u64) -> Lanes {
#[allow(clippy::cast_possible_truncation, reason = "the texel's fields")]
let (ca, cb) = ((word >> 32) as u16, (word >> 48) as u16);
let [[r, g, b, a], [r2, g2, b2, a2]] = [colour(ca, true), colour(cb, false)];
[r, g, b, a, r2, g2, b2, a2]
}
fn modulation(word: u64, is2: bool) -> ([i8; 32], u32) {
let [m0, _, m2, _, a0, ..] = word.to_le_bytes();
let mut weights = [0; 32];
let mut punch = 0;
#[allow(clippy::cast_possible_truncation, reason = "the low half")]
let mut bits = word as u32;
let mode = a0 & 1 != 0;
let pick = |t: &[i8; 4], i: u32| t.get(i as usize).copied().unwrap_or(0);
for (i, weight) in weights
.iter_mut()
.enumerate()
.take(if is2 { 32 } else { 16 })
{
if !is2 {
let index = bits & 3;
*weight = pick(if mode { &[0, 4, 4, 8] } else { &[0, 3, 5, 8] }, index);
if mode && index == 2 {
punch |= 1 << i;
}
bits >>= 2;
} else if !mode {
*weight = if bits & 1 != 0 { 8 } else { 0 };
bits >>= 1;
} else if (i % 8 + i / 8) % 2 == 0 {
*weight = pick(&[0, 3, 5, 8], bits & 3);
if i == 0 || (i == 20 && m0 & 1 != 0) {
*weight = (*weight + 3) & 8;
}
bits >>= 2;
} else {
*weight = if m0 & 1 == 0 {
-3
} else if m2 & 16 != 0 {
-1
} else {
-2
};
}
}
(weights, punch)
}
fn fill_missing(weights: &mut [i8; 32], near: [u64; 4]) {
let [up, left, right, down] = near.map(|w| modulation(w, true).0);
let own = *weights;
let read = |t: &[i8; 32], i: usize| i32::from(t.get(i).copied().unwrap_or(0));
for (i, w) in weights.iter_mut().enumerate() {
if *w >= 0 {
continue;
}
let (x, y) = (i % 8, i / 8);
let up = if y == 0 {
read(&up, i + 24)
} else {
read(&own, i - 8)
};
let down = if y == 3 {
read(&down, i - 24)
} else {
read(&own, i + 8)
};
let left = if x == 0 {
read(&left, i + 7)
} else {
read(&own, i - 1)
};
let right = if x == 7 {
read(&right, i - 7)
} else {
read(&own, i + 1)
};
let v = match *w {
-1 => (up + down + 1) / 2,
-2 => (left + right + 1) / 2,
_ => (up + down + left + right + 2) / 4,
};
*w = i8::try_from(v).unwrap_or(0);
}
}
fn morton_part(v: usize, min: usize, odd: bool) -> usize {
let (mut offset, mut shift, mut mask) = (0, 0, 1);
while mask < min {
offset |= ((v & mask) << usize::from(odd)) << shift;
shift += 1;
mask <<= 1;
}
offset | ((v >> shift) << (shift * 2))
}
#[derive(Clone, Copy, Default)]
struct Column {
words: [u64; 3],
colours: [Lanes; 3],
}
pub(crate) fn decode(data: &[u8], w: usize, h: usize, is2: bool, out: &mut [u8]) {
if is2 {
blocks::<8, 32>(data, w, h, out);
} else {
blocks::<4, 16>(data, w, h, out);
}
}
#[allow(
clippy::inline_always,
reason = "measured: each block inlined into the block loop"
)]
fn blocks<const BW: usize, const N: usize>(data: &[u8], w: usize, h: usize, out: &mut [u8]) {
let (nx, ny) = (w.div_ceil(BW), h.div_ceil(4));
if nx == 0 || ny == 0 {
return;
}
let min = nx.min(ny);
let mut rows_at = [0; 3];
let mut window = [Column::default(); 3];
let column = |x: usize, rows_at: &[usize; 3]| {
let mx = morton_part(x, min, true);
let words = rows_at.map(|my| word(data, mx | my));
Column {
words,
colours: words.map(colours),
}
};
put_blocks::<N>(
out,
w.saturating_mul(4),
h,
(nx, ny),
#[inline(always)]
|bx, by, rows| {
if bx == 0 {
rows_at =
[(by + ny - 1) % ny, by, (by + 1) % ny].map(|y| morton_part(y, min, false));
window = [(nx - 1) % nx, 0, 1 % nx].map(|x| column(x, &rows_at));
} else {
let [_, centre, right] = window;
window = [centre, right, column((bx + 1) % nx, &rows_at)];
}
block::<BW, N>(&window, rows);
true
},
);
}
fn weights<const BW: usize>(window: &[Column; 3]) -> ([u16; 32], u32) {
let [left, centre, right] = window;
let [up, word, down] = centre.words;
let mut out = [0; 32];
if BW == 4 {
const TABLE: [[u16; 4]; 2] = [[0, 3, 5, 8], [0, 4, 4, 8]];
let mode = usize::from(word >> 32 & 1 != 0);
let table = TABLE.get(mode).copied().unwrap_or_default();
let mut punch = 0;
for (i, w) in out.iter_mut().enumerate().take(16) {
#[allow(clippy::cast_possible_truncation, reason = "masked to 2 bits")]
let index = (word >> (2 * i) & 3) as usize;
*w = table.get(index).copied().unwrap_or(0);
punch |= u32::from(mode != 0 && index == 2) << i;
}
return (out, punch);
}
let (mut weights, punch) = modulation(word, true);
if weights.iter().any(|&w| w < 0) {
fill_missing(&mut weights, [up, left.words[1], right.words[1], down]);
}
for (o, w) in out.iter_mut().zip(weights) {
*o = u16::try_from(w).unwrap_or(0);
}
(out, punch)
}
fn block<const BW: usize, const N: usize>(window: &[Column; 3], rows: [&mut [u8; N]; 4]) {
let wx = axis::<BW>();
let wy = axis::<4>();
let [left, centre, right] = window;
let mut across = [[[0u16; 8]; BW]; 3];
for (j, sums) in across.iter_mut().enumerate() {
let at = |c: &Column| c.colours.get(j).copied().unwrap_or_default();
let (l, c, r) = (at(left), at(centre), at(right));
for (sum, &[fl, fc, fr]) in sums.iter_mut().zip(&wx) {
*sum = mac(mac(mul(l, fl), fc, c), fr, r);
}
}
let (weights, punch) = weights::<BW>(window);
let [above, at, below] = &across;
for (y, (row, &[fa, fb, fc])) in rows.into_iter().zip(&wy).enumerate() {
let pixels = row.as_chunks_mut::<4>().0.iter_mut();
let sums = above.iter().zip(at).zip(below);
for (x, (dst, ((a, b), c))) in pixels.zip(sums).enumerate() {
let i = y * BW + x;
let sum = mac(mac(mul(*a, fa), fb, *b), fc, *c);
let m = weights.get(i).copied().unwrap_or(0);
let mut px = blend(scale::<BW>(sum), m);
if punch >> i & 1 != 0 {
px[3] = 0;
}
*dst = px;
}
}
}
#[inline]
fn mul(v: Lanes, f: u16) -> Lanes {
let [v0, v1, v2, v3, v4, v5, v6, v7] = v;
[
v0 * f,
v1 * f,
v2 * f,
v3 * f,
v4 * f,
v5 * f,
v6 * f,
v7 * f,
]
}
#[inline]
fn mac(acc: Lanes, f: u16, v: Lanes) -> Lanes {
let [a0, a1, a2, a3, a4, a5, a6, a7] = acc;
let [v0, v1, v2, v3, v4, v5, v6, v7] = mul(v, f);
[
a0 + v0,
a1 + v1,
a2 + v2,
a3 + v3,
a4 + v4,
a5 + v5,
a6 + v6,
a7 + v7,
]
}
#[inline]
fn scale<const BW: usize>(c: Lanes) -> Lanes {
let two = |s1: u32, s2: u32| {
let [c0, c1, c2, c3, c4, c5, c6, c7] = c;
[
(c0 >> s1) + (c0 >> s2),
(c1 >> s1) + (c1 >> s2),
(c2 >> s1) + (c2 >> s2),
(c3 >> s1) + (c3 >> s2),
(c4 >> s1) + (c4 >> s2),
(c5 >> s1) + (c5 >> s2),
(c6 >> s1) + (c6 >> s2),
(c7 >> s1) + (c7 >> s2),
]
};
let ([r0, g0, b0, _, r1, g1, b1, _], [_, _, _, a0, _, _, _, a1]) = if BW == 8 {
(two(2, 7), two(1, 5))
} else {
(two(1, 6), two(0, 4))
};
[r0, g0, b0, a0, r1, g1, b1, a1]
}
#[inline]
fn blend(c: Lanes, m: u16) -> [u8; 4] {
let [a0, a1, a2, a3, b0, b1, b2, b3] = c;
let n = 8_u16.saturating_sub(m);
[
a0 * n + b0 * m,
a1 * n + b1 * m,
a2 * n + b2 * m,
a3 * n + b3 * m,
]
.map(|v| u8::try_from(v / 8).unwrap_or(0))
}
#[cfg(test)]
mod tests {
fn morton(x: usize, y: usize, min: usize) -> usize {
let (mut offset, mut shift, mut mask) = (0, 0, 1);
while mask < min {
offset |= ((y & mask) | ((x & mask) << 1)) << shift;
shift += 1;
mask <<= 1;
}
offset | (((x | y) >> shift) << (shift * 2))
}
#[test]
fn morton_parts_join_to_the_reference_index() {
for (nx, ny) in [(1, 1), (1, 8), (8, 1), (4, 4), (16, 2), (2, 64), (32, 32)] {
let min = usize::min(nx, ny);
for x in 0..nx {
for y in 0..ny {
let joined =
super::morton_part(x, min, true) | super::morton_part(y, min, false);
assert_eq!(joined, morton(x, y, min), "{x} {y} of {nx}x{ny}");
}
}
}
}
}