M12.1: render cache + parallel compositing fast paths
- render: LayerCache (content-keyed processed-layer cache: source -> masks -> effects, keyed by a hash of the layer's non-transform props evaluated at the frame time, solid item, switches, scale; time folded in for clock-driven effects / wiggle paths). Static and transform-only layers render once; edits only re-render the edited layer. Session + UI frame jobs share it. - effects: TIME_DEPENDENT list + test that every clock-reading effect is listed. - raster: affine composite fast path (per-row span clipping, unchecked interior bilinear, integer-translation copies, inlined Normal blend), checked against composite_warp_reference; box/Gaussian blur without transposes, two ping-pong buffers; accumulate_warp so motion blur sums into one buffer instead of one full-frame image per sub-sample. - ui: viewer frame is urgent and jumps the prefetch queue; stale-revision jobs are dropped; paused prefetch waits for the viewer frame; "Frame Render Time" reports viewer frames only; parallel f32 -> Color32 conversion. - cli: `frame --bench N` (per-layer/per-effect timings, wall + CPU time) and `frame --bench-play N` (consecutive frames with/without the layer cache). - tests: cache transparency (scrub, edits of values/keys/effects/masks/ solids/text/timing), hit/miss accounting, time-dependent effects, demo cached == uncached, fast warp/blur/accumulate == reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dee16181fe
commit
a2ff724ac2
+142
-41
@@ -1,20 +1,23 @@
|
||||
//! Blurs: separable box passes (exact running sums) approximating Gaussians, plus directional
|
||||
//! and radial (spin/zoom) blurs.
|
||||
//!
|
||||
//! Box passes run straight on row-major pixels: horizontal passes per row, vertical passes over
|
||||
//! blocks of rows with a per-column running sum (no transposes), ping-ponging between two
|
||||
//! buffers so a Gaussian allocates two images regardless of the number of passes.
|
||||
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::{Image, Px};
|
||||
|
||||
/// One horizontal box pass of radius `r` (window 2r+1) over each row; `repeat` = edge pixels
|
||||
/// extend, otherwise transparent.
|
||||
fn box_h(img: &Image, r: usize, repeat: bool) -> Image {
|
||||
/// One horizontal box pass of radius `r` (window 2r+1) over each row of `src` (width `w`) into
|
||||
/// `out`; `repeat` = edge pixels extend, otherwise transparent.
|
||||
fn box_h_into(src: &[Px], out: &mut [Px], w: usize, r: usize, repeat: bool) {
|
||||
if r == 0 {
|
||||
return img.clone();
|
||||
out.copy_from_slice(src);
|
||||
return;
|
||||
}
|
||||
let w = img.width as usize;
|
||||
let mut out = Image::new(img.width, img.height);
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
out.data.par_chunks_mut(w).zip(img.data.par_chunks(w)).for_each(|(o, row)| {
|
||||
out.par_chunks_mut(w).zip(src.par_chunks(w)).for_each(|(o, row)| {
|
||||
let get = |i: isize| -> Px {
|
||||
if i < 0 {
|
||||
if repeat { row[0] } else { [0.0; 4] }
|
||||
@@ -40,18 +43,89 @@ fn box_h(img: &Image, r: usize, repeat: bool) -> Image {
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
fn transpose(img: &Image) -> Image {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
let mut out = Image::new(img.height, img.width);
|
||||
out.data.par_chunks_mut(h.max(1)).enumerate().for_each(|(x, col)| {
|
||||
for y in 0..h {
|
||||
col[y] = img.data[y * w + x];
|
||||
/// One vertical box pass (same semantics as [`box_h_into`] along columns).
|
||||
fn box_v_into(src: &[Px], out: &mut [Px], w: usize, h: usize, r: usize, repeat: bool) {
|
||||
if r == 0 {
|
||||
out.copy_from_slice(src);
|
||||
return;
|
||||
}
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
let block = (4 * r).clamp(32, 256);
|
||||
let row = |i: isize| -> Option<&[Px]> {
|
||||
if i < 0 {
|
||||
repeat.then(|| &src[0..w])
|
||||
} else if i as usize >= h {
|
||||
repeat.then(|| &src[(h - 1) * w..h * w])
|
||||
} else {
|
||||
Some(&src[i as usize * w..(i as usize + 1) * w])
|
||||
}
|
||||
};
|
||||
out.par_chunks_mut(w * block).enumerate().for_each(|(bi, chunk)| {
|
||||
let ya = (bi * block) as isize;
|
||||
let mut acc = vec![[0.0f32; 4]; w];
|
||||
for i in ya - r as isize..=ya + r as isize {
|
||||
if let Some(rw) = row(i) {
|
||||
for (a, p) in acc.iter_mut().zip(rw) {
|
||||
for c in 0..4 {
|
||||
a[c] += p[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (yy, o) in chunk.chunks_mut(w).enumerate() {
|
||||
let y = ya + yy as isize;
|
||||
for (o, a) in o.iter_mut().zip(&acc) {
|
||||
*o = a.map(|v| v * norm);
|
||||
}
|
||||
match (row(y + r as isize + 1), row(y - r as isize)) {
|
||||
(Some(add), Some(sub)) => {
|
||||
for ((a, p), q) in acc.iter_mut().zip(add).zip(sub) {
|
||||
for c in 0..4 {
|
||||
a[c] += p[c] - q[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
(Some(add), None) => {
|
||||
for (a, p) in acc.iter_mut().zip(add) {
|
||||
for c in 0..4 {
|
||||
a[c] += p[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
(None, Some(sub)) => {
|
||||
for (a, q) in acc.iter_mut().zip(sub) {
|
||||
for c in 0..4 {
|
||||
a[c] -= q[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
(None, None) => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// Run horizontal passes with radii `rx` then vertical passes with radii `ry`.
|
||||
fn box_passes(img: &Image, rx: &[usize], ry: &[usize], repeat: bool) -> Image {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
let passes: Vec<(bool, usize)> = rx.iter().map(|&r| (false, r)).chain(ry.iter().map(|&r| (true, r))).filter(|p| p.1 > 0).collect();
|
||||
if passes.is_empty() || w == 0 || h == 0 {
|
||||
return img.clone();
|
||||
}
|
||||
let mut a = Image::new(img.width, img.height);
|
||||
let mut b = Image::new(img.width, img.height);
|
||||
for (i, &(vertical, r)) in passes.iter().enumerate() {
|
||||
let src: &[Px] = if i == 0 { &img.data } else { &a.data };
|
||||
if vertical {
|
||||
box_v_into(src, &mut b.data, w, h, r, repeat);
|
||||
} else {
|
||||
box_h_into(src, &mut b.data, w, r, repeat);
|
||||
}
|
||||
std::mem::swap(&mut a, &mut b);
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
/// Box radii for `n` passes approximating a Gaussian of `sigma` (Kovesi / "boxes for Gauss").
|
||||
@@ -72,36 +146,15 @@ fn box_radii(sigma: f64, n: usize) -> Vec<usize> {
|
||||
|
||||
/// Separable box blur with given radii, `iterations` passes.
|
||||
pub fn box_blur(img: &Image, rx: usize, ry: usize, iterations: usize, repeat: bool) -> Image {
|
||||
let mut cur = img.clone();
|
||||
for _ in 0..iterations.max(1) {
|
||||
cur = box_h(&cur, rx, repeat);
|
||||
}
|
||||
if ry > 0 {
|
||||
let mut t = transpose(&cur);
|
||||
for _ in 0..iterations.max(1) {
|
||||
t = box_h(&t, ry, repeat);
|
||||
}
|
||||
cur = transpose(&t);
|
||||
}
|
||||
cur
|
||||
let n = iterations.max(1);
|
||||
box_passes(img, &vec![rx; n], &vec![ry; n], repeat)
|
||||
}
|
||||
|
||||
/// Gaussian blur with standard deviations in pixels (3 box passes per axis).
|
||||
pub fn gaussian_blur(img: &Image, sigma_x: f64, sigma_y: f64, repeat: bool) -> Image {
|
||||
let mut cur = img.clone();
|
||||
if sigma_x > 0.05 {
|
||||
for r in box_radii(sigma_x, 3) {
|
||||
cur = box_h(&cur, r, repeat);
|
||||
}
|
||||
}
|
||||
if sigma_y > 0.05 {
|
||||
let mut t = transpose(&cur);
|
||||
for r in box_radii(sigma_y, 3) {
|
||||
t = box_h(&t, r, repeat);
|
||||
}
|
||||
cur = transpose(&t);
|
||||
}
|
||||
cur
|
||||
let rx = if sigma_x > 0.05 { box_radii(sigma_x, 3) } else { vec![] };
|
||||
let ry = if sigma_y > 0.05 { box_radii(sigma_y, 3) } else { vec![] };
|
||||
box_passes(img, &rx, &ry, repeat)
|
||||
}
|
||||
|
||||
/// Motion blur along `angle_deg` (0 = vertical in AE's Directional Blur, measured clockwise from
|
||||
@@ -182,6 +235,54 @@ mod tests {
|
||||
assert!(b.data.iter().all(|p| (p[0] - 0.5).abs() < 1e-5));
|
||||
}
|
||||
|
||||
/// Direct (windowed sum) box filter along x or y: the definition the passes must match.
|
||||
fn box_ref(img: &Image, r: usize, vertical: bool, repeat: bool) -> Image {
|
||||
let mut out = Image::new(img.width, img.height);
|
||||
let (w, h) = (img.width as i64, img.height as i64);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let mut acc = [0.0f64; 4];
|
||||
for k in -(r as i64)..=(r as i64) {
|
||||
let (sx, sy) = if vertical { (x, y + k) } else { (x + k, y) };
|
||||
let p = if repeat { img.get_clamped(sx, sy) } else { img.get(sx, sy) };
|
||||
for c in 0..4 {
|
||||
acc[c] += p[c] as f64;
|
||||
}
|
||||
}
|
||||
out.set(x as u32, y as u32, acc.map(|v| (v / (2 * r + 1) as f64) as f32));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_match_direct_box_filter() {
|
||||
let mut img = Image::new(53, 301);
|
||||
for y in 0..301 {
|
||||
for x in 0..53 {
|
||||
let v = crate::hash_noise(x, y, 3);
|
||||
img.set(x, y, [v * 0.5, v * 0.25, v, v]);
|
||||
}
|
||||
}
|
||||
for repeat in [false, true] {
|
||||
for (rx, ry) in [(0, 3), (2, 0), (5, 9), (40, 70), (1, 200)] {
|
||||
let fast = box_passes(&img, &[rx, rx + 1], &[ry, ry / 2], repeat);
|
||||
let mut slow = img.clone();
|
||||
for r in [rx, rx + 1] {
|
||||
slow = box_ref(&slow, r, false, repeat);
|
||||
}
|
||||
for r in [ry, ry / 2] {
|
||||
slow = box_ref(&slow, r, true, repeat);
|
||||
}
|
||||
for (i, (p, q)) in fast.data.iter().zip(&slow.data).enumerate() {
|
||||
for c in 0..4 {
|
||||
assert!((p[c] - q[c]).abs() < 1e-4, "r {rx},{ry} repeat {repeat} px {i}: {p:?} vs {q:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn radii_sane() {
|
||||
let r = box_radii(10.0, 3);
|
||||
|
||||
@@ -10,7 +10,7 @@ use effectcraft_color::{BlendMode, blend_pixel};
|
||||
use rayon::prelude::*;
|
||||
|
||||
pub use blur::{box_blur, directional_blur, gaussian_blur, radial_blur};
|
||||
pub use warp::{Sampling, WarpOpts, composite_warp, resample};
|
||||
pub use warp::{Sampling, WarpOpts, accumulate_warp, composite_warp, composite_warp_reference, resample};
|
||||
|
||||
pub type Px = [f32; 4];
|
||||
|
||||
|
||||
+249
-1
@@ -4,7 +4,7 @@ use effectcraft_color::{BlendMode, blend_pixel};
|
||||
use effectcraft_geom::{Mat3, Rect, vec2};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::{Image, hash_noise};
|
||||
use crate::{Image, Px, hash_noise};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Sampling {
|
||||
@@ -52,7 +52,163 @@ fn half(src: &Image) -> Image {
|
||||
}
|
||||
|
||||
/// Draw `src` transformed by `m` (src pixel space → dst pixel space) onto `dst` with blending.
|
||||
///
|
||||
/// Affine transforms with non-stencil modes take a fast path (per-row span clipping, interior
|
||||
/// bilinear without bounds checks, integer-translation copies, inlined Normal blending); it
|
||||
/// matches [`composite_warp_reference`] to float rounding.
|
||||
pub fn composite_warp(dst: &mut Image, src: &Image, m: &Mat3, opts: &WarpOpts) {
|
||||
composite_warp_impl(dst, src, m, opts, true, None);
|
||||
}
|
||||
|
||||
/// `dst += warp(src) · weight` (plain sum, no blending): motion-blur sub-samples accumulate
|
||||
/// straight into one buffer instead of one full-size image per sample.
|
||||
pub fn accumulate_warp(dst: &mut Image, src: &Image, m: &Mat3, sampling: Sampling, weight: f32) {
|
||||
let opts = WarpOpts { sampling, ..Default::default() };
|
||||
if m.is_affine() && m.inverse().is_some_and(|i| i.is_affine()) {
|
||||
composite_warp_impl(dst, src, m, &opts, true, Some(weight));
|
||||
return;
|
||||
}
|
||||
let mut tmp = Image::new(dst.width, dst.height);
|
||||
composite_warp(&mut tmp, src, m, &opts);
|
||||
dst.data.par_iter_mut().zip(tmp.data.par_iter()).for_each(|(d, s)| {
|
||||
for c in 0..4 {
|
||||
d[c] += s[c] * weight;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The straightforward per-pixel implementation (reference for the fast path's tests).
|
||||
pub fn composite_warp_reference(dst: &mut Image, src: &Image, m: &Mat3, opts: &WarpOpts) {
|
||||
composite_warp_impl(dst, src, m, opts, false, None);
|
||||
}
|
||||
|
||||
/// Bilinear blend of four taps (same operation order as [`Image::sample_bilinear`]).
|
||||
#[inline(always)]
|
||||
fn bilerp(a: Px, b: Px, c: Px, d: Px, tx: f32, ty: f32) -> Px {
|
||||
let mut o = [0.0; 4];
|
||||
for i in 0..4 {
|
||||
let top = a[i] + (b[i] - a[i]) * tx;
|
||||
let bot = c[i] + (d[i] - c[i]) * tx;
|
||||
o[i] = top + (bot - top) * ty;
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
/// Bilinear sample with an unchecked interior fast path.
|
||||
#[inline(always)]
|
||||
fn sample_bilinear_fast(img: &Image, x: f64, y: f64) -> Px {
|
||||
let fx = x - 0.5;
|
||||
let fy = y - 0.5;
|
||||
let x0 = fx.floor();
|
||||
let y0 = fy.floor();
|
||||
let (xi, yi) = (x0 as i64, y0 as i64);
|
||||
let (w, h) = (img.width as i64, img.height as i64);
|
||||
if xi >= 0 && yi >= 0 && xi + 1 < w && yi + 1 < h {
|
||||
let tx = (fx - x0) as f32;
|
||||
let ty = (fy - y0) as f32;
|
||||
let i = yi as usize * w as usize + xi as usize;
|
||||
let row0 = &img.data[i..i + 2];
|
||||
let row1 = &img.data[i + w as usize..i + w as usize + 2];
|
||||
return bilerp(row0[0], row0[1], row1[0], row1[1], tx, ty);
|
||||
}
|
||||
img.sample_bilinear(x, y)
|
||||
}
|
||||
|
||||
/// Index range `[lo, hi)` of `i` in `0..n` for which `p0 + i·s` may lie in `[min, max]`
|
||||
/// (conservative by one step on each side; callers still check per pixel).
|
||||
fn span(p0: f64, s: f64, min: f64, max: f64, n: usize) -> (usize, usize) {
|
||||
if s.abs() < 1e-12 {
|
||||
return if p0 >= min - 1e-9 && p0 <= max + 1e-9 { (0, n) } else { (0, 0) };
|
||||
}
|
||||
let (a, b) = ((min - p0) / s, (max - p0) / s);
|
||||
let (a, b) = if a < b { (a, b) } else { (b, a) };
|
||||
let lo = (a.floor() - 1.0).max(0.0);
|
||||
let hi = (b.ceil() + 2.0).min(n as f64);
|
||||
if hi <= lo { (0, 0) } else { (lo as usize, hi as usize) }
|
||||
}
|
||||
|
||||
/// Fast affine rows: `blend(dst, src, x, y)` is only called for visible source pixels.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn affine_rows(
|
||||
dst: &mut Image,
|
||||
img: &Image,
|
||||
inv: &Mat3,
|
||||
sampling: Sampling,
|
||||
op: f32,
|
||||
x0: usize,
|
||||
x1: usize,
|
||||
y0: usize,
|
||||
y1: usize,
|
||||
blend: impl Fn(Px, Px, u32, u32) -> Px + Sync,
|
||||
) {
|
||||
let w = dst.width as usize;
|
||||
let m = inv.0;
|
||||
let (iw, ih) = (img.width as f64, img.height as f64);
|
||||
let step = vec2(m[0][0], m[1][0]);
|
||||
// Pure integer translation: every sample lands on a pixel centre, so all filters return the
|
||||
// source pixel itself (bicubic still clamps negatives, as in the reference).
|
||||
let int_tx = m[0][0] == 1.0 && m[1][1] == 1.0 && m[0][1] == 0.0 && m[1][0] == 0.0 && m[0][2].fract() == 0.0 && m[1][2].fract() == 0.0;
|
||||
dst.data.par_chunks_mut(w).enumerate().skip(y0).take(y1 - y0).for_each(|(y, row)| {
|
||||
let fy = y as f64 + 0.5;
|
||||
let p0 = vec2(m[0][0] * (x0 as f64 + 0.5) + m[0][1] * fy + m[0][2], m[1][0] * (x0 as f64 + 0.5) + m[1][1] * fy + m[1][2]);
|
||||
let n = x1 - x0;
|
||||
let (ax, bx) = span(p0.x, step.x, -1.0, iw + 1.0, n);
|
||||
let (ay, by) = span(p0.y, step.y, -1.0, ih + 1.0, n);
|
||||
let (lo, hi) = (ax.max(ay), bx.min(by));
|
||||
if lo >= hi {
|
||||
return;
|
||||
}
|
||||
if int_tx {
|
||||
let sy = y as i64 + m[1][2] as i64;
|
||||
if sy < 0 || sy >= img.height as i64 {
|
||||
return;
|
||||
}
|
||||
let dx = m[0][2] as i64;
|
||||
let srow = &img.data[sy as usize * img.width as usize..(sy as usize + 1) * img.width as usize];
|
||||
let xa = (x0 as i64 + lo as i64).max(-dx).max(0) as usize;
|
||||
let xb = ((x0 + hi) as i64).min(img.width as i64 - dx).max(0) as usize;
|
||||
for x in xa..xb.max(xa) {
|
||||
let mut s = srow[(x as i64 + dx) as usize];
|
||||
if sampling == Sampling::Bicubic {
|
||||
s = [s[0].max(0.0), s[1].max(0.0), s[2].max(0.0), s[3].max(0.0)];
|
||||
}
|
||||
if s[3] <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
if op < 1.0 {
|
||||
for c in s.iter_mut() {
|
||||
*c *= op;
|
||||
}
|
||||
}
|
||||
row[x] = blend(row[x], s, x as u32, y as u32);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for i in lo..hi {
|
||||
let x = x0 + i;
|
||||
let p = vec2(p0.x + step.x * i as f64, p0.y + step.y * i as f64);
|
||||
if p.x < -1.0 || p.y < -1.0 || p.x > iw + 1.0 || p.y > ih + 1.0 {
|
||||
continue;
|
||||
}
|
||||
let mut s = match sampling {
|
||||
Sampling::Nearest => img.get(p.x.floor() as i64, p.y.floor() as i64),
|
||||
Sampling::Bilinear => sample_bilinear_fast(img, p.x, p.y),
|
||||
Sampling::Bicubic => img.sample_bicubic(p.x, p.y),
|
||||
};
|
||||
if s[3] <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
if op < 1.0 {
|
||||
for c in s.iter_mut() {
|
||||
*c *= op;
|
||||
}
|
||||
}
|
||||
row[x] = blend(row[x], s, x as u32, y as u32);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn composite_warp_impl(dst: &mut Image, src: &Image, m: &Mat3, opts: &WarpOpts, allow_fast: bool, accumulate: Option<f32>) {
|
||||
if src.is_empty() || dst.is_empty() || opts.opacity <= 0.0 {
|
||||
return;
|
||||
}
|
||||
@@ -91,6 +247,26 @@ pub fn composite_warp(dst: &mut Image, src: &Image, m: &Mat3, opts: &WarpOpts) {
|
||||
let affine = inv.is_affine();
|
||||
let mode = opts.mode;
|
||||
let op = opts.opacity;
|
||||
if let Some(k) = accumulate
|
||||
&& affine
|
||||
{
|
||||
affine_rows(dst, img, &inv, opts.sampling, 1.0, x0, x1, y0, y1, |d, s, _, _| [d[0] + s[0] * k, d[1] + s[1] * k, d[2] + s[2] * k, d[3] + s[3] * k]);
|
||||
return;
|
||||
}
|
||||
if allow_fast && affine && !mode.is_stencil() {
|
||||
match mode {
|
||||
BlendMode::Normal => affine_rows(dst, img, &inv, opts.sampling, op, x0, x1, y0, y1, |d, s, _, _| {
|
||||
let k = 1.0 - s[3];
|
||||
[s[0] + d[0] * k, s[1] + d[1] * k, s[2] + d[2] * k, s[3] + d[3] * k]
|
||||
}),
|
||||
BlendMode::Dissolve | BlendMode::DancingDissolve => {
|
||||
let seed = opts.seed;
|
||||
affine_rows(dst, img, &inv, opts.sampling, op, x0, x1, y0, y1, |d, s, x, y| blend_pixel(mode, d, s, hash_noise(x, y, seed)))
|
||||
}
|
||||
_ => affine_rows(dst, img, &inv, opts.sampling, op, x0, x1, y0, y1, |d, s, _, _| blend_pixel(mode, d, s, 0.5)),
|
||||
}
|
||||
return;
|
||||
}
|
||||
dst.data.par_chunks_mut(w).enumerate().skip(y0).take(y1 - y0).for_each(|(y, row)| {
|
||||
let fy = y as f64 + 0.5;
|
||||
let mut sp = inv.apply(vec2(x0 as f64 + 0.5, fy));
|
||||
@@ -172,6 +348,78 @@ mod tests {
|
||||
assert_eq!(dst.get(3, 2), [1.0; 4]);
|
||||
}
|
||||
|
||||
fn test_image(w: u32, h: u32, seed: u32) -> Image {
|
||||
let mut img = Image::new(w, h);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let a = if (x + y) % 7 == 0 { 0.0 } else { hash_noise(x, y, seed) };
|
||||
img.set(x, y, [hash_noise(x, y, seed + 1) * a, hash_noise(x, y, seed + 2) * a, hash_noise(x, y, seed + 3) * a, a]);
|
||||
}
|
||||
}
|
||||
img
|
||||
}
|
||||
|
||||
/// The fast affine path equals the reference per-pixel path (modes, filters, transforms).
|
||||
#[test]
|
||||
fn fast_path_matches_reference() {
|
||||
let src = test_image(37, 23, 5);
|
||||
let mats = [
|
||||
Mat3::IDENTITY,
|
||||
Mat3::translate(vec2(5.0, -3.0)),
|
||||
Mat3::translate(vec2(-40.0, 2.0)),
|
||||
Mat3::translate(vec2(3.25, 7.5)),
|
||||
Mat3::translate(vec2(20.0, 10.0)) * Mat3::rotate_deg(23.0) * Mat3::scale(vec2(1.3, 0.8)),
|
||||
Mat3::translate(vec2(30.0, 5.0)) * Mat3::rotate_deg(-120.0) * Mat3::scale(vec2(0.7, 0.7)),
|
||||
Mat3::translate(vec2(10.0, 10.0)) * Mat3::scale(vec2(-1.0, 1.0)),
|
||||
Mat3::scale(vec2(0.3, 0.3)),
|
||||
];
|
||||
let modes = [BlendMode::Normal, BlendMode::Add, BlendMode::Multiply, BlendMode::Screen, BlendMode::Dissolve, BlendMode::Overlay];
|
||||
for (mi, m) in mats.iter().enumerate() {
|
||||
for mode in modes {
|
||||
for sampling in [Sampling::Nearest, Sampling::Bilinear, Sampling::Bicubic] {
|
||||
for opacity in [1.0, 0.6] {
|
||||
let opts = WarpOpts { sampling, opacity, mode, seed: 9, clip: None };
|
||||
let mut a = test_image(48, 40, 77);
|
||||
let mut b = a.clone();
|
||||
composite_warp(&mut a, &src, m, &opts);
|
||||
composite_warp_reference(&mut b, &src, m, &opts);
|
||||
for (i, (p, q)) in a.data.iter().zip(&b.data).enumerate() {
|
||||
for c in 0..4 {
|
||||
assert!((p[c] - q[c]).abs() < 1e-4, "mat {mi} {mode:?} {sampling:?} op {opacity} px {i}: {p:?} vs {q:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Accumulating sub-samples equals warping each into its own image and summing.
|
||||
#[test]
|
||||
fn accumulate_matches_per_sample_images() {
|
||||
let src = test_image(30, 20, 11);
|
||||
let persp = Mat3::square_to_quad([vec2(5.0, 4.0), vec2(40.0, 2.0), vec2(44.0, 30.0), vec2(2.0, 35.0)]) * Mat3::scale(vec2(1.0 / 30.0, 1.0 / 20.0));
|
||||
let mats = [Mat3::translate(vec2(3.0, 4.0)), Mat3::translate(vec2(5.5, 4.25)) * Mat3::rotate_deg(12.0), persp];
|
||||
let k = 1.0 / mats.len() as f32;
|
||||
let mut fast = Image::new(48, 40);
|
||||
let mut slow = Image::new(48, 40);
|
||||
for m in &mats {
|
||||
accumulate_warp(&mut fast, &src, m, Sampling::Bilinear, k);
|
||||
let mut img = Image::new(48, 40);
|
||||
composite_warp_reference(&mut img, &src, m, &WarpOpts::default());
|
||||
for (a, b) in slow.data.iter_mut().zip(&img.data) {
|
||||
for c in 0..4 {
|
||||
a[c] += b[c] * k;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (p, q) in fast.data.iter().zip(&slow.data) {
|
||||
for c in 0..4 {
|
||||
assert!((p[c] - q[c]).abs() < 1e-4, "{p:?} vs {q:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downscale_preserves_average() {
|
||||
let src = Image::filled(64, 64, [0.25, 0.5, 0.75, 1.0]);
|
||||
|
||||
Reference in New Issue
Block a user