mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 09:05:51 +00:00
Merge remote-tracking branch 'upstream/main' into cache-defaults-3298
This commit is contained in:
commit
9924ebb1e0
171 files changed
+6309
-720
No files matched your search
@@ -228,8 +228,21 @@ fn compound_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Camera Lens Blur
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`.
|
||||
/// Largest Camera Lens Blur radius in buffer pixels. Blur Radius goes to 500 in the UI; a project
|
||||
/// or expression value far beyond it would otherwise build an iris table of one row per pixel of
|
||||
/// radius (and sum that many rows per output pixel) before any image size check.
|
||||
pub const CAMERA_LENS_MAX_RADIUS: f64 = 2000.0;
|
||||
|
||||
/// Camera Lens Blur's iris radius in buffer pixels: Blur Radius at the buffer's `scale`, capped
|
||||
/// at [`CAMERA_LENS_MAX_RADIUS`]. Shared by the CPU and GPU paths.
|
||||
pub fn camera_lens_radius(ctx: &EffectCtx, scale: f64) -> f64 {
|
||||
(ctx.params.f("blurRadius").max(0.0) * scale).clamp(0.0, CAMERA_LENS_MAX_RADIUS)
|
||||
}
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`
|
||||
/// (capped at [`CAMERA_LENS_MAX_RADIUS`]).
|
||||
fn iris_spans(r: f64, sides: u32, rotation_deg: f64, roundness: f64) -> Vec<(i64, i64, i64)> {
|
||||
let r = if r.is_nan() { 0.0 } else { r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) };
|
||||
let ri = r.ceil() as i64;
|
||||
let rot = rotation_deg.to_radians();
|
||||
let verts: Vec<(f64, f64)> = (0..sides)
|
||||
@@ -292,7 +305,8 @@ pub fn camera_lens_spans(ctx: &EffectCtx, r: f64) -> (Vec<(i64, i64, i64)>, f64)
|
||||
s.2 = (s.2 as f64 * aspect).round() as i64;
|
||||
}
|
||||
}
|
||||
(spans, r * aspect.max(1.0))
|
||||
// Same cap as iris_spans, so the reach matches the spans for unbounded radii.
|
||||
(spans, r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) * aspect.max(1.0))
|
||||
}
|
||||
|
||||
/// Whether Camera Lens Blur needs more than plain iris spans (Diffraction Fringe or a Blur
|
||||
@@ -302,7 +316,7 @@ pub fn camera_lens_plain(ctx: &EffectCtx) -> bool {
|
||||
}
|
||||
|
||||
fn camera_lens_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return b;
|
||||
}
|
||||
@@ -910,6 +924,19 @@ mod tests {
|
||||
assert!(out.img.get(15 + o, 10 + d)[0] < 0.9, "bright map blurs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_cap_a_huge_radius() {
|
||||
// A project's Blur Radius far beyond the UI's 500 builds a bounded table.
|
||||
let max_rows = 2 * CAMERA_LENS_MAX_RADIUS as usize + 1;
|
||||
assert!(iris_spans(1e12, 6, 0.0, 0.0).len() <= max_rows);
|
||||
assert!(iris_spans(f64::INFINITY, 0, 0.0, 0.0).len() <= max_rows);
|
||||
assert_eq!(iris_spans(f64::NAN, 0, 0.0, 0.0), vec![(0, 0, 0)]);
|
||||
let params = Params { values: [("blurRadius".into(), num(1e15))].into_iter().collect() };
|
||||
let ctx = EffectCtx { params: ¶ms, time: 0.0, layer_size: [4.0, 4.0], seed: 1, adjustment: false, env: Default::default() };
|
||||
assert_eq!(camera_lens_radius(&ctx, 1.0), CAMERA_LENS_MAX_RADIUS);
|
||||
assert!(camera_lens_spans(&ctx, camera_lens_radius(&ctx, 1.0)).0.len() <= max_rows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_hexagon_is_symmetric() {
|
||||
let s = iris_spans(10.0, 6, 0.0, 0.0);
|
||||
|
||||
@@ -98,14 +98,14 @@ pub use sim::particle_state;
|
||||
pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state};
|
||||
pub use stylize2::strobe_on;
|
||||
// CPU helpers the GPU kernels share (effectcraft-gpu).
|
||||
pub use blur2::{camera_lens_plain, camera_lens_spans, radial_fast_pad};
|
||||
pub use blur2::{CAMERA_LENS_MAX_RADIUS, camera_lens_plain, camera_lens_radius, camera_lens_spans, radial_fast_pad};
|
||||
pub use generate::{four_color_softening, gen_mode};
|
||||
pub use generate2::pattern_kind as cell_pattern_kind;
|
||||
pub use generate2::{BoltSeg, Contour, Ghost, RadioPlan, Wave, flare_ghosts, lightning_segments, radio_plan};
|
||||
pub use generate3::{
|
||||
Coverage, MarksPlan, PaintPlan, Seg, eyedropper_color, marks_plan, paint_bucket_region, paint_plan, plan_reads_pixels, poly_segs, scribble_style,
|
||||
};
|
||||
pub use noise::GrainLook;
|
||||
pub use noise::{GrainLook, MEDIAN_MAX_RADIUS};
|
||||
pub use textfx::{BoltPlan, TextLook, TextPass, bolt_plan, text_passes};
|
||||
pub use transition::{GridWipe, place_layer};
|
||||
// effectcraft-gpu fx_noise.
|
||||
|
||||
@@ -370,9 +370,16 @@ fn quant(v: f32) -> u16 {
|
||||
(v.clamp(0.0, 1.0) * (NB - 1) as f32).round() as u16
|
||||
}
|
||||
|
||||
/// Largest median window radius (buffer pixels) for Median, Median (Legacy), Dust & Scratches
|
||||
/// and the other median users. Radius goes to 255 in the UI; filling one (2r + 1)² window per
|
||||
/// row costs the square of the radius, so a project value far beyond it is held here (also
|
||||
/// keeping the window's pixel count well inside `u32` / `i32` on the GPU).
|
||||
pub const MEDIAN_MAX_RADIUS: usize = 512;
|
||||
|
||||
/// Per-channel median over a (2r+1)² window of the premultiplied channels (values quantised to
|
||||
/// 512 levels in 0..1, edges repeated).
|
||||
pub(crate) fn median_image(img: &Image, r: usize) -> Image {
|
||||
let r = r.min(MEDIAN_MAX_RADIUS);
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
if r == 0 || w == 0 || h == 0 {
|
||||
return img.clone();
|
||||
@@ -871,6 +878,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_radius_is_capped() {
|
||||
// A project's Radius far beyond the UI's 255 takes the capped window, not a
|
||||
// (2r + 1)² fill per row (or an overflow) on a tiny image.
|
||||
let mut img = Image::filled(4, 3, [0.2, 0.3, 0.4, 1.0]);
|
||||
img.set(1, 1, [1.0, 1.0, 1.0, 1.0]);
|
||||
let capped = median_image(&img, MEDIAN_MAX_RADIUS);
|
||||
assert_eq!(median_image(&img, usize::MAX).data, capped.data);
|
||||
assert_eq!(median_image(&img, MEDIAN_MAX_RADIUS + 1000).data, capped.data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_matches_brute_force() {
|
||||
let (w, h) = (9u32, 7u32);
|
||||
|
||||
@@ -301,7 +301,8 @@ pub fn frozen_alpha(params: &Params, layer_size: [f64; 2], fps: f64, f: i64, w:
|
||||
let fr = fc.frames.get(&f)?;
|
||||
let [fw, fh] = fc.size;
|
||||
let expected = fw.checked_mul(fh)?;
|
||||
if fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
// Only the layer's own full-resolution grid (see Roto Brush's frozen mattes).
|
||||
if fc.size != roto::frozen_grid(layer_size) || fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
return None;
|
||||
}
|
||||
let a = rle::decode_bounded(&fr.a, expected)?;
|
||||
@@ -719,6 +720,12 @@ mod tests {
|
||||
p.values.insert("objectPoint".into(), Value::Vec2([40.0, 40.0]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc2.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
// A compact plane claiming a size other than the layer's grid is not decoded.
|
||||
let mut fc3 = fc.clone();
|
||||
fc3.size = [4000, 4000];
|
||||
fc3.frames.insert(2, roto::freeze_frame(&vec![1.0; 4000 * 4000], &[]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc3.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
}
|
||||
|
||||
/// Mattes frozen with a model stay usable in a session where that model is not loaded (a
|
||||
|
||||
@@ -394,7 +394,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
};
|
||||
let render = || popup(&["Full", "Outside", "Inside"]);
|
||||
let mut cyl = vec![
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)),
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 30000.0, 0.0, 200.0, 1)),
|
||||
p("position", "Position", pt(0.5, 0.5), ParamUi::Point),
|
||||
p("rotation", "Rotation", num(0.0), ParamUi::Angle),
|
||||
p("rotationX", "Rotation X", num(0.0), ParamUi::Angle),
|
||||
@@ -565,6 +565,23 @@ mod tests {
|
||||
assert_eq!(out.get(50 + 18, 10)[3], 0.0);
|
||||
}
|
||||
|
||||
/// #3368: Radius takes the range After Effects' CC Cylinder has, up to 30000 %, and a cylinder
|
||||
/// that large still renders (nearly flat, fully opaque) instead of clamping to 400 %.
|
||||
#[test]
|
||||
fn cylinder_radius_goes_up_to_thirty_thousand_percent() {
|
||||
let spec = find("ec.perspective.cccylinder").unwrap();
|
||||
let radius = spec.params.iter().find(|p| p.id == "radius").unwrap();
|
||||
assert!(matches!(radius.ui, ParamUi::Slider { min, max, .. } if min == 0.0 && max == 30000.0), "{:?}", radius.ui);
|
||||
// The cylinder's axis on the left edge: at 400 % its radius is 400 / 100 * 100 / 2π ≈ 63.7 px,
|
||||
// so a pixel 70 px along is past it there and inside the 30000 % one.
|
||||
let img = Image::filled(100, 20, [0.5, 0.5, 0.5, 1.0]);
|
||||
let at_edge = |radius: f64| run("ec.perspective.cccylinder", &img, &[("radius", num(radius)), ("position", Value::Vec2([0.0, 10.0]))]).img;
|
||||
let clamped = at_edge(400.0).get(70, 10);
|
||||
assert_eq!(clamped[3], 0.0, "beyond a 400 % cylinder: {clamped:?}");
|
||||
let px = at_edge(30000.0).get(70, 10);
|
||||
assert!(px.iter().all(|v| v.is_finite()) && px[3] > 0.99, "{px:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cylinder_axis_rotations_affect_geometry_and_texture() {
|
||||
let img = Image::filled(100, 100, [0.5, 0.5, 0.5, 1.0]);
|
||||
|
||||
@@ -614,11 +614,72 @@ pub fn triangulate(outline: &[[f64; 2]], opts: &MeshOpts) -> Mesh {
|
||||
Mesh { verts, tris, outline: poly }
|
||||
}
|
||||
|
||||
/// The Expansion range the outline honours, in layer pixels (the property's slider range). A
|
||||
/// project file can store any number; outside this range it is clamped, so the padded grid
|
||||
/// below stays a few hundred pixels larger than the buffer.
|
||||
pub const EXPANSION_RANGE: (f64, f64) = (-100.0, 200.0);
|
||||
/// Most buffer pixels of padding the outline grid takes on each side (Expansion × buffer scale).
|
||||
const MAX_PAD: f64 = 4096.0;
|
||||
/// Most cells the padded outline grid may have (it is expanded with two `f64` distance buffers).
|
||||
const MAX_GRID_CELLS: usize = 1 << 25;
|
||||
/// Most the alpha is shrunk by, per axis, to make the padded grid fit; a layer needing more has no
|
||||
/// outline.
|
||||
const MAX_DOWNSAMPLE: usize = 8;
|
||||
|
||||
/// The outline of the alpha region under `seed` (buffer pixels → layer space via `scale` and
|
||||
/// `offset`), expanded by `expansion` layer pixels.
|
||||
/// `offset`), expanded by `expansion` layer pixels (clamped to [`EXPANSION_RANGE`]). When the
|
||||
/// padded grid would pass [`MAX_PAD`] or [`MAX_GRID_CELLS`] (a high-scale layer with a large
|
||||
/// Expansion), the alpha is traced at a coarser resolution instead of being skipped.
|
||||
pub fn outline(alpha: &[f32], w: usize, h: usize, scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64) -> Vec<[f64; 2]> {
|
||||
// Pad so expansion can grow past the buffer edge.
|
||||
let pad = (expansion.max(0.0) * scale).ceil() as usize + 2;
|
||||
let expansion = if expansion.is_finite() { expansion.clamp(EXPANSION_RANGE.0, EXPANSION_RANGE.1) } else { 0.0 };
|
||||
if alpha.len() < w.saturating_mul(h) {
|
||||
return vec![];
|
||||
}
|
||||
let want = (expansion.max(0.0) * scale).ceil();
|
||||
if !want.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
for k in 1..=MAX_DOWNSAMPLE {
|
||||
let pad = (want / k as f64).ceil();
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let fits = pad <= MAX_PAD && {
|
||||
let pad = pad as usize + 2;
|
||||
kw.checked_add(2 * pad).zip(kh.checked_add(2 * pad)).and_then(|(pw, ph)| pw.checked_mul(ph)).is_some_and(|c| c <= MAX_GRID_CELLS)
|
||||
};
|
||||
if !fits {
|
||||
continue;
|
||||
}
|
||||
if k == 1 {
|
||||
return outline_padded(alpha, [w, h], scale, offset, seed, expansion, pad as usize + 2);
|
||||
}
|
||||
let small = shrink(alpha, w, h, k);
|
||||
let kf = k as f64;
|
||||
return outline_padded(&small, [kw, kh], scale / kf, [offset[0] / kf, offset[1] / kf], seed, expansion, pad as usize + 2);
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// `alpha` (`w × h`) with every `k × k` block averaged into one pixel.
|
||||
fn shrink(alpha: &[f32], w: usize, h: usize, k: usize) -> Vec<f32> {
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let mut out = vec![0.0f32; kw * kh];
|
||||
for y in 0..kh {
|
||||
for x in 0..kw {
|
||||
let (x1, y1) = (((x + 1) * k).min(w), ((y + 1) * k).min(h));
|
||||
let (mut sum, mut n) = (0.0f32, 0u32);
|
||||
for yy in y * k..y1 {
|
||||
for xx in x * k..x1 {
|
||||
sum += alpha[yy * w + xx];
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
out[y * kw + x] = sum / n.max(1) as f32;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn outline_padded(alpha: &[f32], [w, h]: [usize; 2], scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64, pad: usize) -> Vec<[f64; 2]> {
|
||||
let (pw, ph) = (w + 2 * pad, h + 2 * pad);
|
||||
let mut on = vec![false; pw * ph];
|
||||
for y in 0..h {
|
||||
|
||||
@@ -106,6 +106,42 @@ fn expansion_grows_the_outline() {
|
||||
assert!((am - pi * 225.0).abs() < 60.0, "{am}");
|
||||
}
|
||||
|
||||
/// An imported Expansion far past the property's range is clamped instead of padding the grid
|
||||
/// by that many pixels (a 1e12 expansion would ask for terabytes).
|
||||
#[test]
|
||||
fn hostile_expansion_is_clamped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
let max = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], EXPANSION_RANGE.1)).abs();
|
||||
// A non-finite Expansion is taken as zero (the unexpanded outline), not clamped to the maximum.
|
||||
let zero = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], 0.0)).abs();
|
||||
for e in [1e12, f64::MAX, f64::INFINITY, f64::NAN] {
|
||||
let a = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], e)).abs();
|
||||
let want = if e.is_finite() { max } else { zero };
|
||||
assert!((a - want).abs() < 1.0, "{e}: {a} vs {want}");
|
||||
}
|
||||
assert!(outline(&alpha, w, h, 1e9, [0.0; 2], [20.0, 20.0], 100.0).is_empty());
|
||||
let _ = outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], -1e12);
|
||||
}
|
||||
|
||||
/// A high-scale layer with a large Expansion is traced at a coarser resolution, not skipped, and a
|
||||
/// tiny buffer with a huge padded grid stays within the grid budget.
|
||||
#[test]
|
||||
fn high_scale_expansion_is_traced_coarser_instead_of_skipped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
// Scale 32, Expansion 130: 4160 px of padding, past MAX_PAD, so it is shrunk by 2.
|
||||
let scale = 32.0;
|
||||
let o = outline(&alpha, w, h, scale, [0.0; 2], [20.0, 20.0], 130.0);
|
||||
let area = signed_area(&o).abs();
|
||||
// The disc has radius 10 / 32 layer px; expanded by 130.
|
||||
let r = 10.0 / scale + 130.0;
|
||||
assert!((area - std::f64::consts::PI * r * r).abs() < 0.03 * std::f64::consts::PI * r * r, "{area}");
|
||||
// One pixel at scale 20.48 and Expansion 1e12 (clamped): its 8197 px square grid is over budget.
|
||||
let o = outline(&[1.0], 1, 1, 20.48, [0.0; 2], [0.5, 0.5], 1e12);
|
||||
assert!(o.len() >= 3);
|
||||
}
|
||||
|
||||
fn rect_mesh(w: f64, h: f64, tris: f64) -> Mesh {
|
||||
triangulate(&[[0.0, 0.0], [w, 0.0], [w, h], [0.0, h]], &MeshOpts { triangles: tris, density: 50.0, expansion: 0.0 })
|
||||
}
|
||||
|
||||
@@ -378,10 +378,22 @@ pub fn purge() {
|
||||
c.bytes = 0;
|
||||
}
|
||||
|
||||
/// The pixel size of the layer-space grid at `scale` (`round(size · scale)`, at least 1).
|
||||
fn grid_size(layer_size: [f64; 2], scale: f64) -> (u32, u32) {
|
||||
(((layer_size[0] * scale).round() as u32).max(1), ((layer_size[1] * scale).round() as u32).max(1))
|
||||
}
|
||||
|
||||
/// The full-resolution layer grid frozen mattes are stored at. A frozen cache claiming any
|
||||
/// other size did not come from freezing this layer and is not decoded.
|
||||
pub fn frozen_grid(layer_size: [f64; 2]) -> [usize; 2] {
|
||||
let (w, h) = grid_size(layer_size, 1.0);
|
||||
[w as usize, h as usize]
|
||||
}
|
||||
|
||||
/// The layer-space grid (`round(size · scale)` pixels) of a buffer.
|
||||
pub fn layer_grid(buf: &Buf, layer_size: [f64; 2]) -> Image {
|
||||
let s = if buf.scale > 0.0 { buf.scale } else { 1.0 };
|
||||
let (w, h) = (((layer_size[0] * s).round() as u32).max(1), ((layer_size[1] * s).round() as u32).max(1));
|
||||
let (w, h) = grid_size(layer_size, s);
|
||||
let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64);
|
||||
let mut img = Image::new(w, h);
|
||||
img.rows_mut().for_each(|(y, row)| {
|
||||
@@ -1212,8 +1224,11 @@ pub fn freeze_frame(alpha: &[f32], band: &[u8]) -> FrozenFrame {
|
||||
fn matte_for(ctx: &EffectCtx, f: i64, img: &Arc<Image>, scale: f64) -> Option<(Vec<f32>, Vec<u8>, f64)> {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
// Frozen mattes.
|
||||
// Only the layer's own full-resolution grid: a project cannot make a tiny layer decode (and
|
||||
// convert to floats) planes as large as the RLE allows. Any other size falls through to a computed matte.
|
||||
if let Some(fc) = frozen(ctx.params)
|
||||
&& frozen_applies(&fc, ctx.params, ctx.layer_size, ctx.fps())
|
||||
&& fc.size == frozen_grid(ctx.layer_size)
|
||||
&& let Some(fr) = fc.frames.get(&f)
|
||||
{
|
||||
let [fw, fh] = fc.size;
|
||||
@@ -1409,6 +1424,33 @@ mod tests {
|
||||
params
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_frozen_size_falls_back_to_a_computed_matte() {
|
||||
let mut d = RotoData::default();
|
||||
d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0], [45.0, 40.0]], object: 0 }, 20, [0, 24]);
|
||||
let mut params = params_with(&d);
|
||||
let fc =
|
||||
FrozenCache { key: frozen_key(¶ms, [100.0, 80.0], 25.0), size: [10, 8], frames: BTreeMap::from([(5, freeze_frame(&[1.0; 80], &[0; 80]))]) };
|
||||
let host = Seq;
|
||||
let render = |params: &Params| {
|
||||
let ctx = EffectCtx {
|
||||
params,
|
||||
time: 5.0 / 25.0 + 0.001,
|
||||
layer_size: [100.0, 80.0],
|
||||
seed: 1,
|
||||
adjustment: false,
|
||||
env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() },
|
||||
};
|
||||
crate::apply(&specs()[0], &ctx, Buf { img: frame(5.0 / 25.0 + 0.001), offset: [0.0; 2], scale: 1.0 }).img
|
||||
};
|
||||
let computed = render(¶ms);
|
||||
params.values.insert(FROZEN.into(), Value::Str(fc.to_json()));
|
||||
let out = render(¶ms);
|
||||
assert!(out.data[(40 * 100 + 50) as usize][3] > 0.5, "the disk is missing from the output");
|
||||
// The cache's all-opaque matte would leave the background opaque too; the fallback must match a render with no cache.
|
||||
assert!(out.data == computed.data, "the mismatched frozen cache was used");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_the_segmented_disk_on_demand() {
|
||||
let mut d = RotoData::default();
|
||||
@@ -1980,6 +2022,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Frozen mattes are only decoded at the size the freeze pass stores: the layer grid at
|
||||
/// full resolution.
|
||||
#[test]
|
||||
fn frozen_grid_is_the_full_resolution_layer_grid() {
|
||||
for size in [[100.0, 80.0], [99.6, 0.2], [1.0, 1.0]] {
|
||||
let buf = Buf { img: Image::new(3, 3), offset: [0.0; 2], scale: 1.0 };
|
||||
let g = layer_grid(&buf, size);
|
||||
assert_eq!(frozen_grid(size), [g.width as usize, g.height as usize]);
|
||||
}
|
||||
assert_eq!(frozen_grid([100.0, 80.0]), [100, 80]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_tracking_decoders_validate_dimensions_and_plane_lengths() {
|
||||
let valid = rle::encode(&[255, 0, 255, 0]);
|
||||
|
||||
@@ -8,9 +8,11 @@ use super::{Buf, EffectCtx, Image, IntoParallelRefMutIterator, ParallelIterator,
|
||||
/// Steps per second of the stepped particle simulations.
|
||||
pub(crate) const SPS: f64 = 60.0;
|
||||
|
||||
/// Number of whole steps reached at layer time `t`.
|
||||
/// Number of whole steps reached at layer time `t` (at most [`crate::util::MAX_SIM_STEPS`]: the
|
||||
/// per-step histories built from it, such as [`rate_history`], stay bounded however late a
|
||||
/// project places the layer's time).
|
||||
pub(crate) fn steps_at(t: f64) -> u64 {
|
||||
if t <= 0.0 { 0 } else { (t * SPS + 1e-6).floor() as u64 }
|
||||
if t <= 0.0 { 0 } else { ((t * SPS + 1e-6).floor() as u64).min(crate::util::MAX_SIM_STEPS) }
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
@@ -565,7 +565,8 @@ impl PixelMotion {
|
||||
let px = |id: &str| (g(id, 0.0).max(0.0) * scale).round() as u32;
|
||||
PixelMotion {
|
||||
tile,
|
||||
window: ((g("tuning/blockSize", 8.0).max(4.0)) * scale).round().max(4.0) as u32,
|
||||
// Block Size is declared 4..64; a project can store any number.
|
||||
window: ((g("tuning/blockSize", 8.0).clamp(4.0, 64.0)) * scale).round().max(4.0) as u32,
|
||||
global_smoothness: g("tuning/smoothing/globalSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
local_smoothness: g("tuning/smoothing/localSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
iterations: g("tuning/smoothing/smoothingIterations", 20.0).clamp(0.0, 200.0) as u32,
|
||||
|
||||
+130
-21
@@ -115,18 +115,34 @@ fn box_row(src: &[f32], dst: &mut [f32], r: usize) {
|
||||
dst.copy_from_slice(src);
|
||||
return;
|
||||
}
|
||||
let get = |i: isize| src[i.clamp(0, n as isize - 1) as usize];
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
let mut acc = 0.0f64;
|
||||
for i in -(r as isize)..=(r as isize) {
|
||||
acc += get(i) as f64;
|
||||
// Past the row the window only adds more copies of the edge pixels; the radius is clamped
|
||||
// so the index arithmetic below cannot overflow (the means are unchanged in practice).
|
||||
let r = r.min(MAX_BOX_RADIUS);
|
||||
let (last, ri) = (n - 1, r as i64);
|
||||
let get = |i: i64| src[i.clamp(0, last as i64) as usize];
|
||||
let norm = 1.0 / (2.0 * r as f64 + 1.0);
|
||||
// The first window, -r..=r: the edge pixels it repeats are counted, not visited, so the
|
||||
// work depends on the row length and not on the radius.
|
||||
let inside = r.min(last);
|
||||
let mut acc = (r as f64 + 1.0) * src[0] as f64 + src[1..=inside].iter().map(|v| *v as f64).sum::<f64>();
|
||||
// Only when the window actually repeats the last pixel: 0 * inf would be NaN otherwise.
|
||||
if r > inside {
|
||||
acc += (r - inside) as f64 * src[last] as f64;
|
||||
}
|
||||
for x in 0..n {
|
||||
dst[x] = acc as f32 * norm;
|
||||
acc += get(x as isize + r as isize + 1) as f64 - get(x as isize - r as isize) as f64;
|
||||
dst[x] = (acc * norm) as f32;
|
||||
let xi = x as i64;
|
||||
acc += get(xi + ri + 1) as f64 - get(xi - ri) as f64;
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest box radius [`box_row`] uses (larger radii come from absurd blur amounts in a
|
||||
/// project, such as a Mocha shape's feather).
|
||||
const MAX_BOX_RADIUS: usize = 1 << 30;
|
||||
|
||||
/// Largest sigma [`box_radii`] spreads into boxes (see [`MAX_BOX_RADIUS`]).
|
||||
const MAX_BOX_SIGMA: f64 = 1e8;
|
||||
|
||||
/// Separable box mean with radii `rx`, `ry` (edges repeated).
|
||||
pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
let mut cur = if rx > 0 { p.map_rows(|s, d| box_row(s, d, rx)) } else { p.clone() };
|
||||
@@ -138,9 +154,10 @@ pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
|
||||
/// Box radii for `n` passes approximating a Gaussian of `sigma`.
|
||||
pub fn box_radii(sigma: f64, n: usize) -> Vec<usize> {
|
||||
if sigma <= 0.0 {
|
||||
if sigma.is_nan() || sigma <= 0.0 || n == 0 {
|
||||
return vec![0; n];
|
||||
}
|
||||
let sigma = sigma.min(MAX_BOX_SIGMA);
|
||||
let w_ideal = (12.0 * sigma * sigma / n as f64 + 1.0).sqrt();
|
||||
let mut wl = w_ideal.floor() as i64;
|
||||
if wl % 2 == 0 {
|
||||
@@ -545,13 +562,22 @@ struct SimEntry<S> {
|
||||
/// Checkpoint every this many steps.
|
||||
pub const SIM_CHECKPOINT: u64 = 15;
|
||||
|
||||
/// Most steps a simulation runs: three hours of layer time at 60 steps per second. The layer
|
||||
/// time comes from the project (start time, stretch), so without a limit a tiny file could ask
|
||||
/// for years of steps; past this the state stays where it is.
|
||||
pub const MAX_SIM_STEPS: u64 = 60 * 60 * 60 * 3;
|
||||
|
||||
/// Most checkpoints kept per simulation (and made by one run, which spaces them further apart
|
||||
/// as it goes).
|
||||
const MAX_SIM_CHECKPOINTS: usize = 256;
|
||||
|
||||
impl<S: Clone> SimCache<S> {
|
||||
pub const fn new(cap: usize) -> SimCache<S> {
|
||||
SimCache { inner: std::sync::Mutex::new(Vec::new()), cap }
|
||||
}
|
||||
|
||||
/// State after `target` steps. `init` builds step 0; `step(state, i)` advances from step
|
||||
/// `i` to `i + 1`.
|
||||
/// State after `target` steps (at most [`MAX_SIM_STEPS`]). `init` builds step 0;
|
||||
/// `step(state, i)` advances from step `i` to `i + 1`.
|
||||
pub fn run(&self, key: u64, target: u64, init: impl FnOnce() -> S, step: impl FnMut(&mut S, u64)) -> std::sync::Arc<S> {
|
||||
self.run_where(key, target, init, step, |_, _| true)
|
||||
}
|
||||
@@ -569,6 +595,7 @@ impl<S: Clone> SimCache<S> {
|
||||
valid: impl Fn(u64, &S) -> bool,
|
||||
) -> std::sync::Arc<S> {
|
||||
use std::sync::Arc;
|
||||
let target = target.min(MAX_SIM_STEPS);
|
||||
let start = {
|
||||
let mut g = self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let tick = g.iter().map(|e| e.used).max().unwrap_or(0) + 1;
|
||||
@@ -587,11 +614,17 @@ impl<S: Clone> SimCache<S> {
|
||||
if !had_start {
|
||||
fresh.push((0, Arc::new(st.clone())));
|
||||
}
|
||||
let mut every = SIM_CHECKPOINT;
|
||||
while i < target {
|
||||
step(&mut st, i);
|
||||
i += 1;
|
||||
if i % SIM_CHECKPOINT == 0 && i != target {
|
||||
if i % every == 0 && i != target {
|
||||
fresh.push((i, Arc::new(st.clone())));
|
||||
// A long run keeps a bounded number of states: spread them twice as far apart.
|
||||
if fresh.len() > MAX_SIM_CHECKPOINTS {
|
||||
every = every.saturating_mul(2);
|
||||
fresh.retain(|(s, _)| s % every == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
let out = Arc::new(st);
|
||||
@@ -611,19 +644,23 @@ impl<S: Clone> SimCache<S> {
|
||||
};
|
||||
let e = &mut g[idx];
|
||||
e.used = tick;
|
||||
for (s, st) in fresh {
|
||||
e.checkpoints.retain(|(c, _)| *c != s);
|
||||
e.checkpoints.push((s, st));
|
||||
}
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin.
|
||||
if e.checkpoints.len() > 256 {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0);
|
||||
// Fresh checkpoints replace those at the same step (one pass, not one per checkpoint).
|
||||
let steps: std::collections::HashSet<u64> = fresh.iter().map(|(s, _)| *s).collect();
|
||||
e.checkpoints.retain(|(c, _)| !steps.contains(c));
|
||||
e.checkpoints.extend(fresh);
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin. The
|
||||
// state just asked for keeps its slot, so asking for it again is answered from the cache.
|
||||
if e.checkpoints.len() > MAX_SIM_CHECKPOINTS {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0 && *s != target);
|
||||
e.checkpoints.sort_by_key(|(s, _)| *s);
|
||||
let mut k = 0;
|
||||
// Drop only the excess, evenly spaced, so the rest keep their seek distance.
|
||||
let (len, excess) = (e.checkpoints.len(), (e.checkpoints.len() + 1).saturating_sub(MAX_SIM_CHECKPOINTS));
|
||||
let mut i = 0;
|
||||
e.checkpoints.retain(|_| {
|
||||
k += 1;
|
||||
k % 2 == 1
|
||||
i += 1;
|
||||
(i * excess) / len == ((i - 1) * excess) / len
|
||||
});
|
||||
e.checkpoints.push((target, out.clone()));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -801,6 +838,45 @@ mod tests {
|
||||
assert_eq!(d, e);
|
||||
}
|
||||
|
||||
/// Layer time from a project cannot run a simulation without end, and a long run keeps a
|
||||
/// bounded number of checkpoints that still give the same answer when seeking.
|
||||
#[test]
|
||||
fn sim_cache_bounds_steps_and_checkpoints() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let count = |s: &mut u64, _: u64| *s += 1;
|
||||
assert_eq!(*c.run(1, u64::MAX, || 0, count), MAX_SIM_STEPS);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS + 1, "{n} checkpoints");
|
||||
let fresh: SimCache<u64> = SimCache::new(2);
|
||||
let step = |s: &mut u64, i: u64| *s = s.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||
let far = *fresh.run(1, 100_000, || 7, step);
|
||||
let mid = *fresh.run(1, 54_321, || 7, step);
|
||||
let again: SimCache<u64> = SimCache::new(2);
|
||||
assert_eq!(*again.run(1, 54_321, || 7, step), mid);
|
||||
assert_eq!(*again.run(1, 100_000, || 7, step), far);
|
||||
}
|
||||
|
||||
/// A request that leaves the cache over its checkpoint cap still finds its own state there.
|
||||
#[test]
|
||||
fn sim_cache_keeps_the_requested_state_when_trimming() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let steps = std::cell::Cell::new(0u64);
|
||||
let count = |s: &mut u64, _: u64| {
|
||||
steps.set(steps.get() + 1);
|
||||
*s += 1;
|
||||
};
|
||||
let target = SIM_CHECKPOINT * (MAX_SIM_CHECKPOINTS as u64 - 1) + SIM_CHECKPOINT - 1;
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS, "{n} checkpoints");
|
||||
steps.set(0);
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
assert_eq!(steps.get(), 0, "the second request is answered from the cache");
|
||||
// Only the excess is dropped: the other aligned checkpoints stay.
|
||||
let kept = c.inner.lock().map(|g| g[0].checkpoints.len()).unwrap_or(0);
|
||||
assert_eq!(kept, MAX_SIM_CHECKPOINTS, "trimmed to the cap, not halved");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poly_helpers() {
|
||||
let sq = [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]];
|
||||
@@ -841,4 +917,37 @@ mod tests {
|
||||
let o = guided_filter(&g, &p, 2, 1e-3);
|
||||
assert!(o.data.iter().all(|v| (v - 0.7).abs() < 1e-3));
|
||||
}
|
||||
|
||||
/// The box mean counts repeated edge pixels instead of visiting them: the same means as a
|
||||
/// direct sum, and a huge radius (a Mocha feather of 1e300) costs a row's worth of work.
|
||||
#[test]
|
||||
fn box_row_matches_a_direct_sum_and_bounds_the_radius() {
|
||||
let src = [1.0f32, 2.0, 3.0, 5.0, 8.0];
|
||||
for r in [0usize, 1, 2, 4, 5, 9, 40] {
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, r);
|
||||
for (x, d) in dst.iter().enumerate() {
|
||||
let ri = r as i64;
|
||||
let sum: f64 = (x as i64 - ri..=x as i64 + ri).map(|i| src[i.clamp(0, 4) as usize] as f64).sum();
|
||||
let want = (sum / (2 * r + 1) as f64) as f32;
|
||||
assert!((d - want).abs() < 1e-5, "r={r} x={x}: {d} vs {want}");
|
||||
}
|
||||
}
|
||||
// An infinite last pixel inside the window still yields a finite first mean.
|
||||
let mut inf = [0.0f32; 3];
|
||||
box_row(&[1.0, 2.0, f32::INFINITY], &mut inf, 1);
|
||||
assert!((inf[0] - 4.0 / 3.0).abs() < 1e-6, "{inf:?}");
|
||||
let mut one = [0.0f32];
|
||||
box_row(&[0.25], &mut one, usize::MAX);
|
||||
assert!((one[0] - 0.25).abs() < 1e-6);
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, usize::MAX);
|
||||
assert!(dst.iter().all(|v| v.is_finite() && (1.0..=8.0).contains(v)), "{dst:?}");
|
||||
for sigma in [1e300, f64::MAX, f64::INFINITY, f64::NAN, -1.0] {
|
||||
assert_eq!(box_radii(sigma, 3).len(), 3);
|
||||
}
|
||||
let p = Plane { w: 4, h: 3, data: vec![0.5; 12] };
|
||||
let g = gauss_plane(&p, 1e300, 1e300);
|
||||
assert!(g.data.iter().all(|v| (v - 0.5).abs() < 1e-4), "{:?}", g.data);
|
||||
}
|
||||
}
|
||||
@@ -344,7 +344,9 @@ pub fn load_lut(src: &str) -> Option<Arc<Lut>> {
|
||||
{
|
||||
return v.clone();
|
||||
}
|
||||
let inline = src.contains('\n') || src.contains("LUT_");
|
||||
// `.cube` text always spans lines (a size header, then data rows); a path never does, even one
|
||||
// whose file name contains `LUT_` (#3454). Trimmed, as the path is, so a trailing newline is a path.
|
||||
let inline = src.trim().contains('\n');
|
||||
let parsed = if inline { parse_cube(src) } else { std::fs::read_to_string(src.trim()).ok().and_then(|t| parse_cube(&t)) }.map(Arc::new);
|
||||
if let Ok(mut m) = cache.lock() {
|
||||
m.insert(src.to_string(), parsed.clone());
|
||||
@@ -484,6 +486,20 @@ mod tests {
|
||||
assert_eq!(out.img, img);
|
||||
}
|
||||
|
||||
/// #3454: a path containing `LUT_` was taken for `.cube` text and the file never loaded.
|
||||
#[test]
|
||||
fn a_path_whose_name_contains_lut_loads_the_file() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3454-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("LUT_x.cube");
|
||||
std::fs::write(&path, identity_cube(2)).unwrap();
|
||||
let path = path.to_string_lossy().into_owned();
|
||||
assert!(load_lut(&path).is_some(), "{path}");
|
||||
assert!(load_lut(&format!("{path}\r\n")).is_some(), "{path} with a trailing newline");
|
||||
assert!(load_lut(&identity_cube(2)).is_some(), "inline text");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_d_lut_with_domain() {
|
||||
let lut = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 2 2 2\n1 1 1\n0 0 0\n").unwrap();
|
||||
|
||||
Reference in new issue
Block a user