use crate::config::CompatRules;
use crate::frame::{MotionVectors, ShapeInstance, WavePoint};
use crate::warp_mesh::MeshGeometry;
use bytemuck::{Pod, Zeroable};
use lactea_abi::MeshVertex;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct WfVertex {
pub pos: [f32; 2],
pub color: [f32; 4],
}
pub const FCGSX: usize = 32;
pub const FCGSY: usize = 24;
pub fn d3d_color_component(x: f32) -> f32 {
(((x * 255.0) as i32) & 0xFF) as f32 / 255.0
}
pub fn modulo_color_component(x: f32) -> f32 {
if (0.0..=1.0).contains(&x) {
return x;
}
let m = 256.0 / 255.0;
((x % m) + m) % m
}
pub fn wave_point_clip(rules: &CompatRules, p: &WavePoint, inv_aspect: (f32, f32)) -> [f32; 2] {
let (x, y) = (p.x * 2.0 - 1.0, p.y * 2.0 - 1.0);
if rules.wave_point_f64_aspect {
[
(x * f64::from(inv_aspect.0)) as f32,
(y * f64::from(inv_aspect.1)) as f32,
]
} else {
[x as f32 * inv_aspect.0, y as f32 * inv_aspect.1]
}
}
pub fn conv_color(rules: &CompatRules, c: [f32; 4]) -> [f32; 4] {
let f = if rules.quantize_vertex_colors {
d3d_color_component
} else {
modulo_color_component
};
[f(c[0]), f(c[1]), f(c[2]), f(c[3])]
}
pub fn conv_color_raw(rules: &CompatRules, c: [f32; 4]) -> [f32; 4] {
if rules.quantize_vertex_colors {
[
d3d_color_component(c[0]),
d3d_color_component(c[1]),
d3d_color_component(c[2]),
d3d_color_component(c[3]),
]
} else {
c
}
}
fn d3d_alpha_component(a: f32, alpha_mult: f32) -> f32 {
((((a * 255.0) * alpha_mult) as i32) & 0xFF) as f32 / 255.0
}
pub fn conv_color_alpha_mult(rules: &CompatRules, c: [f32; 4], alpha_mult: f32) -> [f32; 4] {
let mut o = conv_color(rules, c);
if alpha_mult != 1.0 {
o[3] = if rules.quantize_vertex_colors {
d3d_alpha_component(c[3], alpha_mult)
} else {
modulo_color_component(c[3] * alpha_mult)
};
}
o
}
pub fn conv_color_raw_alpha_mult(rules: &CompatRules, c: [f32; 4], alpha_mult: f32) -> [f32; 4] {
let mut o = conv_color_raw(rules, c);
if alpha_mult != 1.0 {
o[3] = if rules.quantize_vertex_colors {
d3d_alpha_component(c[3], alpha_mult)
} else {
c[3] * alpha_mult
};
}
o
}
#[inline]
fn to_px(p: [f32; 2], w: f32, h: f32) -> [f32; 2] {
[(p[0] * 0.5 + 0.5) * w, (0.5 - p[1] * 0.5) * h]
}
#[inline]
fn from_px(p: [f32; 2], w: f32, h: f32) -> [f32; 2] {
[p[0] / w * 2.0 - 1.0, 1.0 - p[1] / h * 2.0]
}
fn push_quad(
out: &mut Vec<WfVertex>,
a0: [f32; 2],
a1: [f32; 2],
b0: [f32; 2],
b1: [f32; 2],
w: f32,
h: f32,
color: [f32; 4],
) {
let v = |p: [f32; 2]| WfVertex {
pos: from_px(p, w, h),
color,
};
out.extend_from_slice(&[v(a0), v(a1), v(b0), v(b0), v(a1), v(b1)]);
}
pub fn push_line(
out: &mut Vec<WfVertex>,
p0: [f32; 2],
p1: [f32; 2],
w: f32,
h: f32,
color: [f32; 4],
) {
let a = to_px(p0, w, h);
let b = to_px(p1, w, h);
let dx = b[0] - a[0];
let dy = b[1] - a[1];
if dx == 0.0 && dy == 0.0 {
return;
}
if dx.abs() >= dy.abs() {
push_quad(
out,
[a[0], a[1] - 0.5],
[a[0], a[1] + 0.5],
[b[0], b[1] - 0.5],
[b[0], b[1] + 0.5],
w,
h,
color,
);
} else {
push_quad(
out,
[a[0] - 0.5, a[1]],
[a[0] + 0.5, a[1]],
[b[0] - 0.5, b[1]],
[b[0] + 0.5, b[1]],
w,
h,
color,
);
}
}
pub fn strip_segments(pts: &[[f32; 2]], color_of: impl Fn(usize) -> [f32; 4]) -> Vec<WfVertex> {
let mut out = Vec::with_capacity(pts.len().saturating_sub(1) * 2);
for i in 0..pts.len().saturating_sub(1) {
out.push(WfVertex {
pos: pts[i],
color: color_of(i),
});
out.push(WfVertex {
pos: pts[i + 1],
color: color_of(i + 1),
});
}
out
}
pub fn push_point(
out: &mut Vec<WfVertex>,
p: [f32; 2],
size: f32,
w: f32,
h: f32,
color: [f32; 4],
) {
let c = to_px(p, w, h);
let r = size * 0.5;
push_quad(
out,
[c[0] - r, c[1] - r],
[c[0] - r, c[1] + r],
[c[0] + r, c[1] - r],
[c[0] + r, c[1] + r],
w,
h,
color,
);
}
pub fn fan_to_list(pts: &[[f32; 2]], colors: &[[f32; 4]]) -> Vec<WfVertex> {
let mut out = Vec::new();
for i in 1..pts.len() - 1 {
for k in [0, i, i + 1] {
out.push(WfVertex {
pos: pts[k],
color: colors[k],
});
}
}
out
}
pub fn build_warp_vertices(
g: &MeshGeometry,
uv: &[[f32; 2]],
diffuse: [f32; 4],
texel_offset_uv: [f32; 2],
seam_fix: bool,
diag_flip: bool,
) -> Vec<MeshVertex> {
warp_vertices_impl(
g,
uv,
&|_| diffuse,
&|_| true,
texel_offset_uv,
seam_fix,
diag_flip,
)
}
pub fn build_warp_vertices_blend(
g: &MeshGeometry,
uv: &[[f32; 2]],
rgb: [f32; 3],
alpha: &[u8],
texel_offset_uv: [f32; 2],
seam_fix: bool,
cull: Option<bool>,
) -> Vec<MeshVertex> {
warp_vertices_impl(
g,
uv,
&|n| [rgb[0], rgb[1], rgb[2], f32::from(alpha[n]) / 255.0],
&|t| match cull {
Some(flip) => crate::blend::tile_needed([alpha[t[0]], alpha[t[1]], alpha[t[2]]], flip),
None => true,
},
texel_offset_uv,
seam_fix,
false,
)
}
fn warp_vertices_impl(
g: &MeshGeometry,
uv: &[[f32; 2]],
diffuse_of: &dyn Fn(usize) -> [f32; 4],
keep: &dyn Fn([usize; 3]) -> bool,
texel_offset_uv: [f32; 2],
seam_fix: bool,
diag_flip: bool,
) -> Vec<MeshVertex> {
let (gx, gy) = (g.gx, g.gy);
let mut out = Vec::with_capacity((gx * gy * 6) as usize);
let mk = |x: u32, y: u32, lower_half: bool| -> MeshVertex {
let n = g.index(x, y);
let mut ang = g.ang[n];
if seam_fix && y == gy / 2 && x < gx / 2 {
ang = if lower_half {
-std::f32::consts::PI
} else {
std::f32::consts::PI
};
}
MeshVertex {
pos: [g.x[n], g.y[n]],
diffuse: diffuse_of(n),
uv: [
uv[n][0],
uv[n][1],
g.x[n] * 0.5 + 0.5 + texel_offset_uv[0],
-g.y[n] * 0.5 + 0.5 + texel_offset_uv[1],
],
rad_ang: [g.rad[n], ang],
}
};
for cy in 0..gy {
let lower = cy < gy / 2;
for cx in 0..gx {
let a = mk(cx, cy, lower);
let b = mk(cx + 1, cy, lower);
let c = mk(cx, cy + 1, lower);
let d = mk(cx + 1, cy + 1, lower);
let (na, nb, nc, nd) = (
g.index(cx, cy),
g.index(cx + 1, cy),
g.index(cx, cy + 1),
g.index(cx + 1, cy + 1),
);
if diag_flip {
out.extend_from_slice(&[a, b, d, a, d, c]);
} else {
if keep([na, nb, nc]) {
out.extend_from_slice(&[a, b, c]);
}
if keep([nb, nc, nd]) {
out.extend_from_slice(&[b, c, d]);
}
}
}
}
out
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct WarpVertex {
pub pos: [f32; 2],
pub rad_ang: [f32; 2],
pub transforms: [f32; 4],
pub center: [f32; 2],
pub dist: [f32; 2],
pub stretch: [f32; 2],
}
pub fn warp_gpu_vertex(
g: &MeshGeometry,
vars: &[crate::warp_mesh::VertexVars],
x: u32,
y: u32,
) -> WarpVertex {
let n = g.index(x, y);
let (pos, rad, ang) = g.projectm_vertex(x, y);
let v = &vars[n];
WarpVertex {
pos,
rad_ang: [rad, ang],
transforms: [v.zoom, v.zoomexp, v.rot, v.warp],
center: [v.cx, v.cy],
dist: [v.dx, v.dy],
stretch: [v.sx, v.sy],
}
}
pub fn build_warp_gpu_vertices(
g: &MeshGeometry,
vars: &[crate::warp_mesh::VertexVars],
diag_flip: bool,
) -> Vec<WarpVertex> {
let (gx, gy) = (g.gx, g.gy);
let mut out = Vec::with_capacity((gx * gy * 6) as usize);
let mk = |x: u32, y: u32| warp_gpu_vertex(g, vars, x, y);
for cy in 0..gy {
for cx in 0..gx {
let a = mk(cx, cy);
let b = mk(cx + 1, cy);
let c = mk(cx, cy + 1);
let d = mk(cx + 1, cy + 1);
if diag_flip {
out.extend_from_slice(&[c, d, a, d, a, b]);
} else {
out.extend_from_slice(&[a, b, c, b, c, d]);
}
}
}
out
}
pub fn fix_nan_uv_triangles(verts: &mut [MeshVertex]) {
for t in verts.as_chunks_mut::<3>().0 {
if t.iter().any(|v| v.uv[0].is_nan() || v.uv[1].is_nan()) {
for v in t.iter_mut() {
v.uv[0] = 0.0;
v.uv[1] = 0.0;
}
}
}
}
fn squish_to_center(x: f32, exp: f32) -> f32 {
if x > 0.5 {
return (x * 2.0 - 1.0).powf(exp) * 0.5 + 0.5;
}
(1.0 - (1.0 - x * 2.0).powf(exp)) * 0.5
}
fn uv_to_math_space(ax: f32, ay: f32, u: f32, v: f32) -> (f32, f32) {
let px = (u * 2.0 - 1.0) * ax;
let py = (v * 2.0 - 1.0) * ay;
let rad = (px * px + py * py).sqrt() / (ax * ax + ay * ay).sqrt();
let mut ang = py.atan2(px);
if ang < 0.0 {
ang += 6.2831853071796;
}
(rad, ang)
}
pub fn hue_shade(time: f32, rand_start: [f32; 4]) -> [[f32; 3]; 4] {
let mut shade = [[1.0f32; 3]; 4];
for i in 0..4 {
let fi = i as f32;
shade[i][0] = 0.6 + 0.3 * (time * 30.0 * 0.0143 + 3.0 + fi * 21.0 + rand_start[3]).sin();
shade[i][1] = 0.6 + 0.3 * (time * 30.0 * 0.0107 + 1.0 + fi * 13.0 + rand_start[1]).sin();
shade[i][2] = 0.6 + 0.3 * (time * 30.0 * 0.0129 + 6.0 + fi * 9.0 + rand_start[2]).sin();
let mut max = if shade[i][0] > shade[i][1] {
shade[i][0]
} else {
shade[i][1]
};
if shade[i][2] > max {
max = shade[i][2];
}
for k in 0..3 {
shade[i][k] /= max;
shade[i][k] = 0.5 + 0.5 * shade[i][k];
}
}
shade
}
pub fn comp_grid(
w: u32,
h: u32,
aspect: (f32, f32),
shade: &[[f32; 3]; 4],
uv_half_texel: bool,
) -> Vec<MeshVertex> {
let (ax, ay) = aspect;
let half = if uv_half_texel {
[0.5 / w as f32, 0.5 / h as f32]
} else {
[0.0, 0.0]
};
let div_x = 1.0 / (FCGSX - 2) as f32;
let div_y = 1.0 / (FCGSY - 2) as f32;
let pi = 3.1415926535898f32;
let mut verts = vec![MeshVertex::default(); FCGSX * FCGSY];
for j in 0..FCGSY {
let j2 = j - j / (FCGSY / 2);
let v = squish_to_center(j2 as f32 * div_y, 3.0);
let sy = -(v * 2.0 - 1.0);
for i in 0..FCGSX {
let i2 = i - i / (FCGSX / 2);
let u = squish_to_center(i2 as f32 * div_x, 3.0);
let sx = u * 2.0 - 1.0;
let (rad, mut ang) = uv_to_math_space(ax, ay, u, v);
if i == FCGSX / 2 - 1 {
ang = if j < FCGSY / 2 - 1 {
pi * 1.5
} else if j == FCGSY / 2 - 1 {
pi * 1.25
} else if j == FCGSY / 2 {
pi * 0.75
} else {
pi * 0.5
};
} else if i == FCGSX / 2 {
ang = if j < FCGSY / 2 - 1 {
pi * 1.5
} else if j == FCGSY / 2 - 1 {
pi * 1.75
} else if j == FCGSY / 2 {
pi * 0.25
} else {
pi * 0.5
};
} else if j == FCGSY / 2 - 1 {
ang = if i < FCGSX / 2 - 1 {
pi * 1.0
} else {
pi * 2.0
};
} else if j == FCGSY / 2 {
ang = if i < FCGSX / 2 - 1 {
pi * 1.0
} else {
pi * 0.0
};
}
let x01 = sx * 0.5 + 0.5;
let y01 = sy * 0.5 + 0.5;
let mut col = [1.0f32; 3];
for c in 0..3 {
col[c] = shade[0][c] * x01 * y01
+ shade[1][c] * (1.0 - x01) * y01
+ shade[2][c] * x01 * (1.0 - y01)
+ shade[3][c] * (1.0 - x01) * (1.0 - y01);
}
verts[i + j * FCGSX] = MeshVertex {
pos: [sx, sy],
diffuse: [col[0], col[1], col[2], 1.0],
uv: [u + half[0], v + half[1], 0.0, 0.0],
rad_ang: [rad, ang],
};
}
}
verts
}
pub fn comp_indices() -> Vec<usize> {
let mut out = Vec::with_capacity((FCGSX - 2) * (FCGSY - 2) * 6);
for y in 0..FCGSY - 1 {
if y == FCGSY / 2 - 1 {
continue;
}
for x in 0..FCGSX - 1 {
if x == FCGSX / 2 - 1 {
continue;
}
let left_half = x < FCGSX / 2;
let top_half = y < FCGSY / 2;
let center_4 =
(x == FCGSX / 2 || x == FCGSX / 2 - 1) && (y == FCGSY / 2 || y == FCGSY / 2 - 1);
let idx = |xx: usize, yy: usize| yy * FCGSX + xx;
let tri: [usize; 6] = if (left_half as i32 + top_half as i32 + center_4 as i32) % 2 == 1
{
[
idx(x, y),
idx(x + 1, y),
idx(x + 1, y + 1),
idx(x + 1, y + 1),
idx(x, y + 1),
idx(x, y),
]
} else {
[
idx(x, y + 1),
idx(x, y),
idx(x + 1, y),
idx(x + 1, y),
idx(x + 1, y + 1),
idx(x, y + 1),
]
};
out.extend_from_slice(&tri);
}
}
out
}
pub fn build_comp_vertices(
w: u32,
h: u32,
aspect: (f32, f32),
shade: &[[f32; 3]; 4],
uv_half_texel: bool,
) -> Vec<MeshVertex> {
let verts = comp_grid(w, h, aspect, shade, uv_half_texel);
comp_indices().into_iter().map(|t| verts[t]).collect()
}
pub fn echo_aspect_mult(w: u32, h: u32) -> [f32; 2] {
let (wf, hf) = (w as f32, h as f32);
let aspect_y = if w > h { hf / wf } else { 1.0 };
let inv_aspect_y = 1.0 / aspect_y;
let aspect = wf / (hf * inv_aspect_y);
if aspect > 1.0 {
[1.0, aspect]
} else {
[1.0 / aspect, 1.0]
}
}
pub fn echo_quad(
w: u32,
h: u32,
uv: [[f32; 2]; 4],
colors: [[f32; 4]; 4],
diag_flip: bool,
mult: [f32; 2],
) -> Vec<MeshVertex> {
let ex = (1.0 + 1.0 / w as f32) * mult[0];
let ey = (1.0 + 1.0 / h as f32) * mult[1];
let pos = [[-ex, -ey], [ex, -ey], [-ex, ey], [ex, ey]];
let v = |i: usize| MeshVertex {
pos: pos[i],
diffuse: colors[i],
uv: [uv[i][0], uv[i][1], 0.0, 0.0],
rad_ang: [0.0; 2],
};
if diag_flip {
vec![v(0), v(1), v(3), v(0), v(3), v(2)]
} else {
vec![v(0), v(1), v(2), v(2), v(1), v(3)]
}
}
fn bilerp_uv(g: &MeshGeometry, uv: &[[f32; 2]], gxf: f32, gyf: f32) -> Option<[f32; 2]> {
let x0 = gxf as i32;
let y0 = gyf as i32;
let dx = gxf - x0 as f32;
let dy = gyf - y0 as f32;
let (x1, y1) = (x0 + 1, y0 + 1);
if x0 < 0 || y0 < 0 || x1 > g.gx as i32 || y1 > g.gy as i32 {
return None;
}
let at = |x: i32, y: i32| uv[g.index(x as u32, y as u32)];
let (a, b, c, d) = (at(x0, y0), at(x1, y0), at(x0, y1), at(x1, y1));
let mut r = [0.0; 2];
for k in 0..2 {
r[k] = a[k] * (1.0 - dx) * (1.0 - dy)
+ b[k] * dx * (1.0 - dy)
+ c[k] * (1.0 - dx) * dy
+ d[k] * dx * dy;
}
Some(r)
}
pub fn motion_vector_lines(
mv: &MotionVectors,
g: &MeshGeometry,
prev_uv: &[[f32; 2]],
w: u32,
h: u32,
rules: &CompatRules,
) -> Vec<[f32; 2]> {
let mut out = Vec::new();
let mut nx = mv.x as i32;
let mut ny = mv.y as i32;
let mut dx = mv.x - nx as f32;
let mut dy = mv.y - ny as f32;
if nx > 64 {
nx = 64;
dx = 0.0;
}
if ny > 48 {
ny = 48;
dy = 0.0;
}
if nx <= 0 || ny <= 0 {
return out;
}
let dx2 = mv.dx;
let dy2 = mv.dy;
let len_mult = mv.l;
dx = dx.clamp(0.0, 1.0);
dy = dy.clamp(0.0, 1.0);
let (wf, hf) = (w as f32, h as f32);
let min_len = if rules.mv_mirrored_lookup {
1.0 / wf
} else {
rules.mv_min_len_px * ((1.0 / wf).powi(2) + (1.0 / hf).powi(2)).sqrt()
};
for y in 0..ny {
let mut fy = (y as f32 + 0.25) / (ny as f32 + dy + 0.25 - 1.0);
fy -= dy2;
if !(fy > 0.0001 && fy < 0.9999) {
continue;
}
for x in 0..nx {
let mut fx = (x as f32 + 0.25) / (nx as f32 + dx + 0.25 - 1.0);
fx += dx2;
if !(fx > 0.0001 && fx < 0.9999) {
continue;
}
let (gxf, gyf) = if rules.mv_mirrored_lookup {
(fx * g.gx as f32, fy * g.gy as f32)
} else {
(fx * g.gx as f32, (1.0 - fy) * g.gy as f32)
};
let Some(uv) = bilerp_uv(g, prev_uv, gxf, gyf) else {
continue;
};
let fx2 = uv[0];
let fy2 = if rules.mv_mirrored_lookup {
1.0 - uv[1]
} else {
uv[1]
};
let mut ddx = (fx2 - fx) * len_mult;
let mut ddy = (fy2 - fy) * len_mult;
let mut len = (ddx * ddx + ddy * ddy).sqrt();
if len > min_len {
} else if len > 0.00000001 {
len = min_len / len;
ddx *= len;
ddy *= len;
} else {
ddx = min_len;
ddy = min_len;
}
let (ex, ey) = (fx + ddx, fy + ddy);
out.push([fx * 2.0 - 1.0, 1.0 - fy * 2.0]);
out.push([ex * 2.0 - 1.0, 1.0 - ey * 2.0]);
}
}
out
}
pub struct ShapeGeom {
pub pts: Vec<[f32; 2]>,
pub uv: Vec<[f32; 2]>,
#[allow(dead_code)]
pub sides: usize,
}
pub fn shape_geometry(s: &ShapeInstance, aspect_y: f32) -> ShapeGeom {
shape_geometry_tex(s, aspect_y, aspect_y)
}
pub fn shape_geometry_tex(s: &ShapeInstance, aspect_y: f32, tex_aspect_y: f32) -> ShapeGeom {
let sides = s.sides.clamp(3, 100) as usize;
let pi = 3.1415927f32;
let c0 = [(s.x * 2.0 - 1.0) as f32, (s.y * 2.0 - 1.0) as f32];
let mut pts = vec![c0];
let mut uv = vec![[0.5f32, 0.5f32]];
for j in 1..sides + 1 {
let t = (j - 1) as f32 / sides as f32;
let a = t * pi * 2.0 + s.ang + pi * 0.25;
pts.push([c0[0] + s.rad * a.cos() * aspect_y, c0[1] - s.rad * a.sin()]);
let ta = t * pi * 2.0 + s.tex_ang + pi * 0.25;
uv.push([
0.5 + 0.5 * ta.cos() / s.tex_zoom * tex_aspect_y,
1.0 - (0.5 - 0.5 * ta.sin() / s.tex_zoom),
]);
}
pts.push(pts[1]);
uv.push(uv[1]);
ShapeGeom { pts, uv, sides }
}
pub fn darken_center_triangles(aspect_y: f32, rules: &CompatRules) -> Vec<WfVertex> {
let hs = 0.05f32;
let pts = [
[0.0, 0.0],
[-hs * aspect_y, 0.0],
[0.0, -hs],
[hs * aspect_y, 0.0],
[0.0, hs],
[-hs * aspect_y, 0.0],
];
let c0 = conv_color(rules, [0.0, 0.0, 0.0, 3.0 / 32.0]);
let c1 = conv_color(rules, [0.0, 0.0, 0.0, 0.0]);
let colors = [c0, c1, c1, c1, c1, c1];
fan_to_list(&pts, &colors)
}
pub fn border_triangles(it: usize, ob_size: f32, ib_size: f32, rgba: [f32; 4]) -> Vec<WfVertex> {
let inner = if it == 0 {
1.0 - ob_size
} else {
1.0 - ob_size - ib_size
};
let outer = if it == 0 { 1.0 } else { 1.0 - ob_size };
let v = [
[outer, outer],
[outer, -outer],
[-outer, outer],
[-outer, -outer],
[inner, inner],
[inner, -inner],
[-inner, inner],
[-inner, -inner],
];
const INDICES: [usize; 24] = [
0, 1, 4, 1, 4, 5, 2, 3, 6, 3, 7, 6, 2, 0, 6, 0, 4, 6, 3, 7, 5, 1, 3, 5,
];
INDICES
.iter()
.map(|&i| WfVertex {
pos: [v[i][0], -v[i][1]],
color: rgba,
})
.collect()
}
pub fn border_triangles_md2(
it: usize,
ob_size: f32,
ib_size: f32,
rgba: [f32; 4],
) -> Vec<WfVertex> {
let inner = if it == 0 {
1.0 - ob_size
} else {
1.0 - ob_size - ib_size
};
let outer = if it == 0 { 1.0 } else { 1.0 - ob_size };
let mut v = [
[inner, inner],
[outer, outer],
[outer, -outer],
[inner, -inner],
];
let mut out = Vec::new();
for _ in 0..4 {
out.extend(fan_to_list(&v, &[rgba; 4]));
let t = 1.570796327f32;
for p in v.iter_mut() {
let (x, y) = (p[0], p[1]);
p[0] = x * t.cos() - y * t.sin();
p[1] = x * t.sin() + y * t.cos();
}
}
out
}
const SUB_PIXEL: f32 = 256.0;
pub fn gl_fill_ties(tris: &mut [WfVertex], w: f32, h: f32) {
let snap = |v: f32| (v * SUB_PIXEL).round_ties_even() as i64;
let key = |p: [f32; 2]| {
let q = to_px(p, w, h);
(snap(q[0]), snap(q[1]))
};
let half = SUB_PIXEL as i64 / 2;
let mut moved: Vec<(i64, i64)> = Vec::new();
for t in tris.as_chunks::<3>().0 {
for (a, b) in [(0, 1), (1, 2), (2, 0)] {
let (ka, kb) = (key(t[a].pos), key(t[b].pos));
if ka.1 == kb.1 && ka.0 != kb.0 && ka.1.rem_euclid(SUB_PIXEL as i64) == half {
moved.push(ka);
moved.push(kb);
}
}
}
if moved.is_empty() {
return;
}
moved.sort_unstable();
moved.dedup();
for v in tris.iter_mut() {
let k = key(v.pos);
if moved.binary_search(&k).is_ok() {
v.pos[1] = from_px([0.0, (k.1 + 1) as f32 / SUB_PIXEL], w, h)[1];
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Compat;
fn px_extent(v: &[WfVertex], w: f32, h: f32) -> ([f32; 2], [f32; 2]) {
let mut mn = [f32::MAX; 2];
let mut mx = [f32::MIN; 2];
for p in v {
let q = to_px(p.pos, w, h);
for k in 0..2 {
mn[k] = mn[k].min(q[k]);
mx[k] = mx[k].max(q[k]);
}
}
(mn, mx)
}
#[test]
fn horizontal_line_is_one_pixel_tall() {
let (w, h) = (64.0, 32.0);
let a = from_px([10.5, 5.5], w, h);
let b = from_px([20.5, 5.5], w, h);
let mut v = Vec::new();
push_line(&mut v, a, b, w, h, [1.0; 4]);
assert_eq!(v.len(), 6);
let (mn, mx) = px_extent(&v, w, h);
assert!((mn[0] - 10.5).abs() < 1e-4 && (mx[0] - 20.5).abs() < 1e-4);
assert!(
(mn[1] - 5.0).abs() < 1e-4 && (mx[1] - 6.0).abs() < 1e-4,
"covers exactly pixel row 5"
);
}
#[test]
fn steep_line_is_one_pixel_wide_and_zero_length_is_empty() {
let (w, h) = (64.0, 32.0);
let mut v = Vec::new();
push_line(
&mut v,
from_px([3.5, 2.5], w, h),
from_px([5.5, 20.5], w, h),
w,
h,
[1.0; 4],
);
let (mn, mx) = px_extent(&v, w, h);
assert!((mn[1] - 2.5).abs() < 1e-4 && (mx[1] - 20.5).abs() < 1e-4);
assert!((mx[0] - mn[0] - 3.0).abs() < 1e-3); let mut e = Vec::new();
push_line(&mut e, [0.1, 0.1], [0.1, 0.1], w, h, [1.0; 4]);
assert!(e.is_empty());
}
#[test]
fn points_are_squares() {
let mut v = Vec::new();
push_point(
&mut v,
from_px([10.5, 10.5], 64.0, 64.0),
2.0,
64.0,
64.0,
[1.0; 4],
);
let (mn, mx) = px_extent(&v, 64.0, 64.0);
assert!((mn[0] - 9.5).abs() < 1e-4 && (mx[0] - 11.5).abs() < 1e-4);
}
#[test]
fn warp_gpu_triangles_use_projectms_vertex_order() {
let g = MeshGeometry::new(4, 2, 64, 48).unwrap();
let vars = vec![crate::warp_mesh::VertexVars::default(); g.vertex_count()];
let v = build_warp_gpu_vertices(&g, &vars, true);
assert_eq!(v.len(), 4 * 2 * 6);
let at = |x: u32, y: u32| warp_gpu_vertex(&g, &vars, x, y);
let want = [at(0, 1), at(1, 1), at(0, 0), at(1, 1), at(0, 0), at(1, 0)];
assert_eq!(&v[..6], &want[..]);
}
#[test]
fn warp_mesh_counts_and_seam() {
let g = MeshGeometry::new(8, 6, 64, 48).unwrap();
let uv: Vec<[f32; 2]> = (0..g.vertex_count()).map(|n| [g.x[n], g.y[n]]).collect();
let v = build_warp_vertices(&g, &uv, [1.0; 4], [0.0; 2], true, false);
assert_eq!(v.len(), 8 * 6 * 6);
let neg = v
.iter()
.filter(|m| m.rad_ang[1] == -std::f32::consts::PI)
.count();
assert!(neg > 0);
let v2 = build_warp_vertices(&g, &uv, [1.0; 4], [0.0; 2], false, false);
assert_eq!(
v2.iter()
.filter(|m| m.rad_ang[1] == -std::f32::consts::PI)
.count(),
0
);
assert!((v[0].uv[3] - 1.0).abs() < 1e-6 && (v[0].uv[2] - 0.0).abs() < 1e-6);
}
#[test]
fn comp_mesh_has_expected_triangle_count_and_uv_range() {
let shade = hue_shade(0.0, [0.0; 4]);
let v = build_comp_vertices(640, 360, (1.0, 0.5625), &shade, false);
assert_eq!(v.len(), (FCGSX - 2) * (FCGSY - 2) * 6);
for m in &v {
assert!(
m.uv[0] >= -1e-6
&& m.uv[0] <= 1.0 + 1e-6
&& m.uv[1] >= -1e-6
&& m.uv[1] <= 1.0 + 1e-6
);
assert!(m.pos[0].abs() <= 1.0 + 1e-6);
}
let v = build_comp_vertices(640, 360, (1.0, 0.5625), &shade, true);
assert!(v.iter().any(|m| m.uv[0] > 1.0));
}
#[test]
fn echo_quad_diagonal_follows_projectm_strip() {
let c = [[0.0; 4]; 4];
let uv = [[0.0; 2]; 4];
let pm = echo_quad(640, 360, uv, c, true, [1.0, 1.0]);
let md2 = echo_quad(640, 360, uv, c, false, [1.0, 1.0]);
assert_eq!(pm.len(), 6);
let corner = |i: usize| echo_quad(640, 360, uv, c, false, [1.0, 1.0])[[0, 1, 2, 5][i]].pos;
assert_eq!(pm.iter().filter(|m| m.pos == corner(0)).count(), 2);
assert_eq!(pm.iter().filter(|m| m.pos == corner(3)).count(), 2);
assert_eq!(pm.iter().filter(|m| m.pos == corner(1)).count(), 1);
assert_eq!(md2.iter().filter(|m| m.pos == corner(1)).count(), 2);
assert_eq!(md2.iter().filter(|m| m.pos == corner(2)).count(), 2);
}
#[test]
fn echo_aspect_mult_is_identity_for_landscape_and_stretches_portrait() {
for (w, h) in [
(320, 180),
(640, 360),
(1280, 720),
(1920, 1080),
(800, 600),
(400, 400),
] {
assert_eq!(echo_aspect_mult(w, h), [1.0, 1.0], "{w}x{h}");
}
let m = echo_aspect_mult(360, 640);
assert_eq!(m[1], 1.0);
assert!((m[0] - 640.0 / 360.0).abs() < 1e-6);
let q = echo_quad(360, 640, [[0.0; 2]; 4], [[0.0; 4]; 4], true, m);
let ex = (1.0 + 1.0 / 360.0) * m[0];
assert!(
q.iter()
.all(|v| v.pos[0].abs() == ex && v.pos[1].abs() == 1.0 + 1.0 / 640.0)
);
}
#[test]
fn hue_shade_in_range() {
let s = hue_shade(12.3, [1.0, 2.0, 3.0, 4.0]);
for c in s {
for k in c {
assert!((0.5..=1.0).contains(&k));
}
assert!(
c.iter().any(|&k| (k - 1.0).abs() < 1e-6),
"max component normalised to 1"
);
}
}
#[test]
fn colors_quantize_and_wrap() {
assert!((d3d_color_component(0.98) - 249.0 / 255.0).abs() < 1e-6);
assert!((d3d_color_component(1.0) - 1.0).abs() < 1e-6);
assert!((d3d_color_component(2.0) - 254.0 / 255.0).abs() < 1e-6);
assert!((d3d_color_component(-1.0) - 1.0 / 255.0).abs() < 1e-6);
assert_eq!(modulo_color_component(0.3), 0.3);
assert!((modulo_color_component(2.0) - (2.0 - 256.0 / 255.0)).abs() < 1e-6);
let _ = Compat::ProjectM;
}
#[test]
fn motion_vectors_identity_warp_gives_min_length_vectors() {
let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
let uv: Vec<[f32; 2]> = (0..g.vertex_count())
.map(|n| [g.x[n] * 0.5 + 0.5, -g.y[n] * 0.5 + 0.5])
.collect();
let mv = MotionVectors {
x: 12.0,
y: 9.0,
dx: 0.0,
dy: 0.0,
l: 0.9,
rgba: [1.0; 4],
};
for compat in [Compat::MilkDrop2, Compat::ProjectM] {
let rules = compat.rules();
let l = motion_vector_lines(&mv, &g, &uv, 640, 360, &rules);
assert_eq!(l.len() % 2, 0);
assert!(l.len() >= 2 * 12 * 9 - 2 * 20, "{compat:?}: {}", l.len());
let (a, b) = (l[0], l[1]);
let len = (((b[0] - a[0]) / 2.0).powi(2) + ((b[1] - a[1]) / 2.0).powi(2)).sqrt();
assert!(len > 0.0 && len < 0.01, "min-length vector {len}");
}
}
#[test]
fn motion_vector_zoom_points_inward_in_both_lookups() {
let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
let anim = crate::warp_mesh::WarpAnim::new(0.0, 1.0, 1.0);
let uv = crate::warp_mesh::warp_mesh_uniform(
&g,
&crate::warp_mesh::VertexVars {
zoom: 2.0,
warp: 0.0,
..Default::default()
},
&anim,
[0.0; 2],
);
let mv = MotionVectors {
x: 4.0,
y: 4.0,
dx: 0.0,
dy: 0.0,
l: 1.0,
rgba: [1.0; 4],
};
for compat in [Compat::MilkDrop2, Compat::ProjectM] {
let l = motion_vector_lines(&mv, &g, &uv, 640, 360, &compat.rules());
for p in l.chunks(2) {
let d0 = (p[0][0].powi(2) + p[0][1].powi(2)).sqrt();
let d1 = (p[1][0].powi(2) + p[1][1].powi(2)).sqrt();
assert!(d1 < d0, "{compat:?}: end not closer to centre {p:?}");
}
}
}
#[test]
fn shape_geometry_matches_formula() {
let s = ShapeInstance {
sides: 4,
x: 0.5,
y: 0.5,
rad: 0.1,
ang: 0.0,
..Default::default()
};
let g = shape_geometry(&s, 0.5625);
assert_eq!(g.pts.len(), 6);
assert_eq!(g.pts[5], g.pts[1]);
assert!((g.pts[1][0] - 0.1 * 0.70710678 * 0.5625).abs() < 1e-5);
assert!((g.pts[1][1] + 0.1 * 0.70710678).abs() < 1e-5);
assert_eq!(g.uv[0], [0.5, 0.5]);
let s2 = ShapeInstance {
sides: 1000,
..Default::default()
};
assert_eq!(shape_geometry(&s2, 1.0).sides, 100);
}
#[test]
fn shape_image_uses_unit_texture_aspect() {
let s = ShapeInstance {
sides: 4,
rad: 0.1,
tex_zoom: 2.0,
..Default::default()
};
let main = shape_geometry(&s, 0.5625);
let img = shape_geometry_tex(&s, 0.5625, 1.0);
assert_eq!(main.pts, img.pts, "the outline is the same");
let c = (std::f32::consts::FRAC_PI_4).cos();
assert!((main.uv[1][0] - (0.5 + 0.5 * c / 2.0 * 0.5625)).abs() < 1e-6);
assert!((img.uv[1][0] - (0.5 + 0.5 * c / 2.0)).abs() < 1e-6);
assert_eq!(main.uv[1][1], img.uv[1][1]);
}
#[test]
fn shape_geometry_rounds_like_projectm() {
let mut uv_differs = 0;
for zoom in [0.274246f32, 0.369642, 0.45, 1.3, 3.0, 4.0] {
for tex_ang in [0.0f32, 0.3, 1.1, 2.5] {
let s = ShapeInstance {
sides: 6,
tex_zoom: zoom,
tex_ang,
..Default::default()
};
let g = shape_geometry(&s, 0.5625);
let pi = 3.1415927f32;
for j in 1..=6usize {
let t = (j - 1) as f32 / 6.0;
let a = t * pi * 2.0 + tex_ang + pi * 0.25;
assert_eq!(
g.uv[j][1].to_bits(),
(1.0 - (0.5 - 0.5 * a.sin() / zoom)).to_bits()
);
uv_differs += (g.uv[j][1] != 0.5 + 0.5 * a.sin() / zoom) as u32;
}
}
}
assert!(
uv_differs > 0,
"the probe values must expose the rounding difference"
);
let s = ShapeInstance {
x: 0.72,
y: 0.28,
..Default::default()
};
let c = shape_geometry(&s, 1.0).pts[0];
assert_eq!(
c,
[(0.72f64 * 2.0 - 1.0) as f32, (0.28f64 * 2.0 - 1.0) as f32]
);
assert_ne!(
c[0],
0.72f32 * 2.0 - 1.0,
"rounding x to f32 first moves the centre by ulps"
);
}
#[test]
fn wave_point_rounding_follows_the_compat_rule() {
let (pm, md2) = (Compat::ProjectM.rules(), Compat::MilkDrop2.rules());
let inv = (1.0f32 / 1.7777778, 1.0f32);
let (mut pm_vs_md2, mut pm_vs_f32) = (0, 0);
for i in 0..2000 {
let x = 0.5 + 0.5 * (f64::from(i) * 0.7311).sin();
let p = WavePoint {
x,
y: x,
rgba: [1.0; 4],
};
let a = wave_point_clip(&pm, &p, inv);
let b = wave_point_clip(&md2, &p, inv);
assert_eq!(a[0], ((x * 2.0 - 1.0) * f64::from(inv.0)) as f32);
assert_eq!(b[0], (x * 2.0 - 1.0) as f32 * inv.0);
assert_eq!(
a[1],
(x * 2.0 - 1.0) as f32,
"unit aspect: only the double -> f32 rounding"
);
pm_vs_md2 += (a[0] != b[0]) as u32;
pm_vs_f32 += (a[0] != (x as f32 * 2.0 - 1.0) * inv.0) as u32;
}
assert!(pm_vs_md2 > 0 && pm_vs_f32 > 0, "{pm_vs_md2} {pm_vs_f32}");
}
#[test]
fn border_and_darken_center() {
let t = border_triangles(0, 0.1, 0.1, [1.0; 4]);
assert_eq!(t.len(), 4 * 6);
let d = darken_center_triangles(0.5625, &Compat::ProjectM.rules());
assert_eq!(d.len(), 4 * 3);
assert!((d[0].color[3] - 3.0 / 32.0).abs() < 1e-6);
}
#[test]
fn border_mesh_is_projectms() {
let t = border_triangles(1, 0.1, 0.05, [0.5; 4]);
let (o, i) = (1.0f32 - 0.1, 1.0f32 - 0.1 - 0.05);
let p: Vec<[f32; 2]> = t.iter().map(|v| v.pos).collect();
assert_eq!(p.len(), 24);
assert_eq!(&p[0..3], &[[o, -o], [o, o], [i, -i]]); assert_eq!(&p[21..24], &[[o, o], [-o, o], [i, i]]); assert!(t.iter().all(|v| v.color == [0.5; 4]));
let d = border_triangles(0, 1.2, 0.0, [1.0; 4]);
let i = 1.0f32 - 1.2; assert_eq!(d[2].pos, [i, -i]); assert_eq!(border_triangles_md2(0, 0.1, 0.0, [1.0; 4]).len(), 24);
}
#[test]
fn gl_fill_ties_nudge_only_horizontal_centre_edges() {
let (w, h) = (40.0f32, 30.0f32);
let mut t = border_triangles(0, 0.1, 0.0, [1.0; 4]); let before = t.clone();
gl_fill_ties(&mut t, w, h);
for (a, b) in before.iter().zip(&t) {
let (pa, pb) = (to_px(a.pos, w, h), to_px(b.pos, w, h));
assert_eq!(pa[0], pb[0]);
if a.pos[1].abs() == 1.0 {
assert_eq!(a.pos, b.pos); } else {
assert!(
(pb[1] - pa[1] - 1.0 / 256.0).abs() < 1e-4,
"{pa:?} -> {pb:?}"
);
}
}
for (size, ww) in [(0.05f32, 40.0f32), (0.075, 40.0)] {
let mut u = border_triangles(0, size, 0.0, [1.0; 4]);
let b = u.clone();
gl_fill_ties(&mut u, ww, h);
assert_eq!(u, b, "size {size}");
}
}
#[test]
fn nan_uv_triangles_sample_uv_zero() {
let g = MeshGeometry::new(2, 2, 64, 64).unwrap();
let mut uv = vec![[0.25f32, 0.75]; g.vertex_count()];
uv[g.index(1, 1)] = [f32::NAN, f32::NAN];
let mut v = build_warp_vertices(&g, &uv, [1.0; 4], [0.0; 2], false, true);
fix_nan_uv_triangles(&mut v);
for t in v.chunks_exact(3) {
let uvs: Vec<[f32; 2]> = t.iter().map(|m| [m.uv[0], m.uv[1]]).collect();
let flat = uvs.iter().all(|u| *u == [0.0, 0.0]);
let keep = uvs.iter().all(|u| *u == [0.25, 0.75]);
assert!(flat ^ keep, "{uvs:?}");
}
assert!(v.iter().any(|m| m.uv[0] == 0.0) && v.iter().any(|m| m.uv[0] == 0.25));
assert!(v.iter().all(|m| !m.uv[0].is_nan() && !m.uv[1].is_nan()));
}
}