pub(super) const ENTRY: usize = 8;
pub(super) fn entry(
(width, height): (u8, u8),
(grid_x, grid_y): (u8, u8),
texel_x: u8,
texel_y: u8,
) -> Option<[u8; ENTRY]> {
let (x, y) = (texel_x, texel_y);
if width < 2 || height < 2 || grid_x < 2 || grid_y < 2 || grid_x > width || grid_y > height {
return None;
}
if x >= width || y >= height {
return None;
}
let (fw, fh, gx, gy) = (
u32::from(width),
u32::from(height),
u32::from(grid_x),
u32::from(grid_y),
);
let xw = (((1024 + fw / 2) / (fw - 1)) * u32::from(x) * (gx - 1) + 32) >> 6;
let yw = (((1024 + fh / 2) / (fh - 1)) * u32::from(y) * (gy - 1) + 32) >> 6;
let (xf, yf) = (xw & 15, yw & 15);
let q0 = (xw >> 4) + (yw >> 4) * gx;
let index = [q0, q0 + 1, q0 + gx, q0 + gx + 1];
let w3 = (xf * yf + 8) >> 4;
let weight = [
(16 + w3).checked_sub(xf + yf)?,
xf.checked_sub(w3)?,
yf.checked_sub(w3)?,
w3,
];
let mut out = [0; ENTRY];
let mut n = 0;
for (&i, &c) in index.iter().zip(&weight) {
if c != 0 {
*out.get_mut(n)? = u8::try_from(i).ok()?;
*out.get_mut(4 + n)? = u8::try_from(c).ok()?;
n += 1;
}
}
Some(out)
}
#[allow(clippy::inline_always, reason = "measured: the texel loop's hot call")]
#[inline(always)]
pub(super) fn infill(entry: [u8; ENTRY], grid: &[u8; 64]) -> u8 {
let [i0, i1, i2, i3, c0, c1, c2, c3] = entry;
let at = |i: u8| u32::from(grid.get(usize::from(i & 63)).copied().unwrap_or(0));
let sum = 8
+ at(i0) * u32::from(c0)
+ at(i1) * u32::from(c1)
+ at(i2) * u32::from(c2)
+ at(i3) * u32::from(c3);
u8::try_from(sum >> 4).unwrap_or(64)
}
#[cfg(test)]
mod tests {
use super::{ENTRY, entry, infill};
const FOOTPRINTS: [(u8, u8, u8); 14] = super::super::context::FOOTPRINTS;
#[test]
fn every_stored_index_is_inside_its_grid() {
for (w, h, _) in FOOTPRINTS {
for gx in 2..=w {
for gy in 2..=h {
for y in 0..h {
for x in 0..w {
let e = entry((w, h), (gx, gy), x, y).unwrap();
let sum: u32 = e[4..].iter().map(|&c| u32::from(c)).sum();
assert_eq!(sum, 16, "{w}x{h} grid {gx}x{gy} at {x},{y}");
for i in 0..4 {
if e[4 + i] != 0 {
assert!(e[i] < gx * gy, "{w}x{h} grid {gx}x{gy} at {x},{y}");
}
}
}
}
}
}
}
assert_eq!(entry((4, 4), (5, 4), 0, 0), None);
assert_eq!(entry((4, 4), (4, 4), 4, 0), None);
assert_eq!(entry((1, 4), (1, 4), 0, 0), None);
}
#[test]
fn identity_and_bilinear_infill() {
for (w, h, _) in FOOTPRINTS {
for y in 0..h {
for x in 0..w {
let e = entry((w, h), (w, h), x, y).unwrap();
assert_eq!(e, [y * w + x, 0, 0, 0, 16, 0, 0, 0], "{w}x{h} at {x},{y}");
}
}
}
assert_eq!(entry((5, 5), (2, 2), 2, 2), Some([0, 1, 2, 3, 4, 4, 4, 4]));
let mut grid = [0; 64];
grid[..4].copy_from_slice(&[0, 64, 64, 0]);
assert_eq!(infill(entry((5, 5), (2, 2), 2, 2).unwrap(), &grid), 32);
assert_eq!(infill([0; ENTRY], &grid), 0);
assert_eq!(infill([1, 0, 0, 0, 16, 0, 0, 0], &grid), 64);
}
}