Merge main into M13.6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
//! Deterministic rendering (better than After Effects): the same project renders bit-identical
|
||||
//! frames every time and whatever the number of render threads. Renders the demo comps (and a
|
||||
//! comp of seeded-random effects: particles, grain, noise, wiggle expressions, Shatter) twice
|
||||
//! on one thread and on several, and compares every float of every pixel.
|
||||
|
||||
use effectcraft_engine::Session;
|
||||
use effectcraft_engine::project::{ItemId, Project};
|
||||
use effectcraft_engine::render::{RenderOpts, Renderer};
|
||||
use effectcraft_engine::time::Tick;
|
||||
use serde_json::json;
|
||||
|
||||
fn render(s: &Session, p: &Project, comp: ItemId, t: f64) -> Vec<u32> {
|
||||
let mut r = Renderer::new(p, s.footage.as_ref(), RenderOpts { scale: 0.25, ..Default::default() });
|
||||
r.expr = s.expr.as_deref();
|
||||
// No layer cache: every render computes everything.
|
||||
let img = r.comp_frame(comp, Tick::from_seconds_f64(t));
|
||||
img.data.iter().flat_map(|px| px.iter().map(|c| c.to_bits())).collect()
|
||||
}
|
||||
|
||||
fn pool(n: usize) -> rayon::ThreadPool {
|
||||
rayon::ThreadPoolBuilder::new().num_threads(n).build().expect("thread pool")
|
||||
}
|
||||
|
||||
/// A comp of effects that draw random numbers (all seeded per instance).
|
||||
fn random_comp(s: &mut Session) -> ItemId {
|
||||
s.execute("comp.new", json!({"name": "Random", "width": 480, "height": 270, "frameRate": 24, "duration": 4})).unwrap();
|
||||
let cid = s.active_comp_id().unwrap();
|
||||
let solid = |s: &mut Session, name: &str, color: &str| {
|
||||
s.execute("layer.newSolid", json!({"name": name, "color": color, "width": 480, "height": 270})).unwrap()["layer"].as_u64().unwrap()
|
||||
};
|
||||
let a = solid(s, "Particles", "#000000");
|
||||
for fx in ["ec.sim.ccparticleworld", "ec.noise.addgrain"] {
|
||||
s.execute("effect.apply", json!({"layer": a, "effect": fx})).unwrap();
|
||||
}
|
||||
let b = solid(s, "Noise", "#336699");
|
||||
for fx in ["ec.noise.fractal", "ec.sim.ccsnowfall", "ec.noise.noisehls"] {
|
||||
s.execute("effect.apply", json!({"layer": b, "effect": fx})).unwrap();
|
||||
}
|
||||
s.execute("prop.setExpression", json!({"layer": b, "path": "transform/position", "expression": "wiggle(3, 40)"})).unwrap();
|
||||
s.execute("prop.setExpression", json!({"layer": b, "path": "transform/opacity", "expression": "random(40, 90)"})).unwrap();
|
||||
let c = solid(s, "Shatter", "#cc8844");
|
||||
s.execute("layer.setTransform", json!({"layer": c, "prop": "scale", "value": [50, 50]})).unwrap();
|
||||
s.execute("effect.apply", json!({"layer": c, "effect": "ec.sim.shatter"})).unwrap();
|
||||
cid
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_are_bit_identical_across_runs_and_thread_counts() {
|
||||
let mut s = effectcraft_host::session();
|
||||
s.replace_project(effectcraft_engine::demo::demo_project(), None);
|
||||
let random = random_comp(&mut s);
|
||||
let p = (*s.project).clone();
|
||||
let mut comps: Vec<(ItemId, f64)> = p.comps().map(|(id, c)| (*id, c.duration.seconds())).collect();
|
||||
comps.sort_by_key(|c| c.0);
|
||||
assert!(comps.len() >= 3, "demo comps + the random comp");
|
||||
assert!(comps.iter().any(|c| c.0 == random));
|
||||
let (one, many) = (pool(1), pool(6));
|
||||
let mut frames = 0;
|
||||
for (comp, dur) in comps {
|
||||
for t in [0.0, dur * 0.37, dur * 0.81] {
|
||||
let a = one.install(|| render(&s, &p, comp, t));
|
||||
let b = many.install(|| render(&s, &p, comp, t));
|
||||
let c = many.install(|| render(&s, &p, comp, t));
|
||||
let d = one.install(|| render(&s, &p, comp, t));
|
||||
let name = &p.item(comp).unwrap().name;
|
||||
assert!(!a.is_empty());
|
||||
assert!(a == b, "{name} @ {t:.2}s: 1 thread vs 6 threads differ");
|
||||
assert!(b == c, "{name} @ {t:.2}s: two 6-thread renders differ");
|
||||
assert!(a == d, "{name} @ {t:.2}s: two 1-thread renders differ");
|
||||
frames += 1;
|
||||
}
|
||||
}
|
||||
assert!(frames >= 9);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
//! Effect plug-ins in a full session: a Rust plug-in registered through the trait and the
|
||||
//! example WebAssembly plug-in (examples/plugins/posterize-bands) loaded with
|
||||
//! `effect.plugins.load`, both applied by id like built-ins and rendered.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_engine::Session;
|
||||
use effectcraft_engine::effects::plugin::{EffectPlugin, PluginFrame, PluginManifest, PluginParam, PluginParamKind, PluginParams, register_plugin};
|
||||
use effectcraft_engine::time::Tick;
|
||||
use serde_json::json;
|
||||
|
||||
struct Swap {
|
||||
m: PluginManifest,
|
||||
}
|
||||
|
||||
impl EffectPlugin for Swap {
|
||||
fn manifest(&self) -> &PluginManifest {
|
||||
&self.m
|
||||
}
|
||||
fn render(&self, frame: &mut PluginFrame, params: &PluginParams, _time: f64) -> Result<(), String> {
|
||||
if params.b("on") {
|
||||
for p in frame.pixels.iter_mut() {
|
||||
p.swap(0, 2);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn setup() -> (Session, u64) {
|
||||
let mut s = effectcraft_host::session();
|
||||
s.execute("comp.new", json!({"name": "P", "width": 40, "height": 20, "duration": 1})).unwrap();
|
||||
let l = s.execute("layer.newSolid", json!({"color": [1.0, 0.5, 0.0], "width": 40, "height": 20})).unwrap()["layer"].as_u64().unwrap();
|
||||
(s, l)
|
||||
}
|
||||
|
||||
fn px(s: &Session) -> [f32; 4] {
|
||||
let cid = s.active_comp_id().unwrap();
|
||||
s.render(cid, Tick::ZERO, Default::default()).get(20, 10)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_plugins_apply_by_id() {
|
||||
let m = PluginManifest {
|
||||
api: 1,
|
||||
id: "org.test.swap".into(),
|
||||
name: "Swap Red Blue".into(),
|
||||
category: "Channel".into(),
|
||||
version: "1".into(),
|
||||
author: String::new(),
|
||||
description: String::new(),
|
||||
params: vec![PluginParam { id: "on".into(), name: "On".into(), kind: PluginParamKind::Checkbox { default: true } }],
|
||||
};
|
||||
register_plugin(Arc::new(Swap { m })).unwrap();
|
||||
let (mut s, l) = setup();
|
||||
s.execute("effect.apply", json!({"layer": l, "effect": "org.test.swap"})).unwrap();
|
||||
let p = px(&s);
|
||||
assert!(p[0] < 0.01 && (p[1] - 0.5).abs() < 0.01 && p[2] > 0.99, "{p:?}");
|
||||
// Its parameters are ordinary properties: set (and animate) them like any effect's.
|
||||
s.execute("prop.set", json!({"layer": l, "path": "effects/#1/on", "value": false})).unwrap();
|
||||
let p = px(&s);
|
||||
assert!(p[0] > 0.99 && p[2] < 0.01, "{p:?}");
|
||||
let list = s.execute("effect.plugins.list", json!({})).unwrap();
|
||||
assert!(list["plugins"].as_array().unwrap().iter().any(|p| p["id"] == "org.test.swap" && p["category"] == "Channel"));
|
||||
assert_eq!(list["api"], 1);
|
||||
assert!(s.execute("effect.list", json!({"filter": "swap red"})).unwrap().as_array().unwrap().len() == 1);
|
||||
}
|
||||
|
||||
/// Build the example plug-in (skipped when the wasm32 target isn't installed).
|
||||
fn build_example() -> Option<std::path::PathBuf> {
|
||||
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples/plugins/posterize-bands");
|
||||
let target = std::env::var_os("CARGO_TARGET_DIR").map(std::path::PathBuf::from).unwrap_or_else(|| root.join("../../../target")).join("plugin-example");
|
||||
let target = if target.is_absolute() { target } else { root.join("../../..").join(target) };
|
||||
let st = std::process::Command::new(env!("CARGO"))
|
||||
.current_dir(&root)
|
||||
.args(["build", "--release", "--target", "wasm32-unknown-unknown", "--target-dir"])
|
||||
.arg(&target)
|
||||
.status()
|
||||
.ok()?;
|
||||
if !st.success() {
|
||||
eprintln!("skipping: the example plug-in didn't build (rustup target add wasm32-unknown-unknown)");
|
||||
return None;
|
||||
}
|
||||
Some(target.join("wasm32-unknown-unknown/release/posterize_bands.wasm"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_wasm_plugin_loads_and_renders() {
|
||||
let Some(wasm) = build_example() else { return };
|
||||
let (mut s, l) = setup();
|
||||
let r = s.execute("effect.plugins.load", json!({"path": wasm.to_string_lossy()})).unwrap();
|
||||
assert_eq!(r["loaded"][0]["id"], "org.effectcraft.example.posterize-bands", "{r}");
|
||||
s.execute("effect.apply", json!({"layer": l, "effect": "Posterize Bands"})).unwrap();
|
||||
s.execute("prop.set", json!({"layer": l, "path": "effects/#1/mix", "value": 0})).unwrap();
|
||||
s.execute("prop.set", json!({"layer": l, "path": "effects/#1/levels", "value": 2})).unwrap();
|
||||
// Luminance of (1, .5, 0) is ~0.57: with 2 levels it bands to 1, scaling the colour by 1/y
|
||||
// (8 bpc: red clips at 1).
|
||||
let p = px(&s);
|
||||
let y = 0.2126 + 0.7152 * 0.5;
|
||||
assert!((p[1] - 0.5 / y as f32).abs() < 0.01 && p[0] > 0.99 && p[2] < 0.01, "{p:?}");
|
||||
// Invert: band 0.
|
||||
s.execute("prop.set", json!({"layer": l, "path": "effects/#1/invert", "value": true})).unwrap();
|
||||
assert_eq!(px(&s)[..3], [0.0, 0.0, 0.0]);
|
||||
let list = s.execute("effect.plugins.list", json!({})).unwrap();
|
||||
assert!(list["plugins"].as_array().unwrap().iter().any(|p| p["source"].as_str().unwrap_or("").ends_with(".wasm")));
|
||||
}
|
||||
Reference in New Issue
Block a user