Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge main into #2567; keep both test modules, and convert four layer stacks main's new render tests assign as Vec<Layer> with .into()

bflatastic0039081952a9
diff --cc crates/engine/src/tests.rs
index 8601a46a1,25d05b903..82aa011a2
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -40,6 -41,6 +41,7 @@@ pub(crate) mod roto
mod sequence;
mod session;
mod settings;
++mod shared_layers;
mod shortcut_cmds;
pub(crate) mod stubs;
mod styles;
diff --cc crates/render/src/tests.rs
index 3caf20c30,38baa8f3e..488f38ec7
--- a/crates/render/src/tests.rs
+++ b/crates/render/src/tests.rs
@@@ -346,6 -347,208 +347,208 @@@ fn particle_world_follows_the_comp_came
assert!(b < a - 20.0, "panning the camera right moves the particles left: {a} -> {b}");
}
+ /// A black solid with CC Particle World whose Birth Rate is 3 until layer time `cut`, then 0 a
+ /// frame later (`None`: 3 throughout); the layer starts at comp time `start`, stretched `stretch` %.
+ fn burst_scene(cut: Option<f64>, start: f64, stretch: f64) -> (Project, ItemId) {
+ burst_scene_keys(cut, start, stretch, false)
+ }
+
+ /// [`burst_scene`]; `hold`: Hold keys, 3 until `cut` and 0 from `cut` exactly.
+ fn burst_scene_keys(cut: Option<f64>, start: f64, stretch: f64, hold: bool) -> (Project, ItemId) {
+ let (mut p, cid, comp) = setup();
+ let mut l = solid(&mut p, &comp, [0.0, 0.0, 0.0], 200, 100);
+ let vals = [("birthRate", Value::Scalar(3.0)), ("longevity", Value::Scalar(1.0)), ("particle/particleType", Value::Enum(2))];
+ add_effect(&mut p, &mut l, "ec.sim.ccparticleworld", [200.0, 100.0], &vals);
+ if let Some(cut) = cut {
+ let fx = l.props.sub_mut("effects").unwrap().children.iter_mut().find_map(|c| match c {
+ effectcraft_project::Node::Group(g) => Some(g),
+ _ => None,
+ });
+ let fx = fx.unwrap();
+ let rate = fx.prop_mut("birthRate").unwrap();
+ let key = |t: f64, v: f64| Keyframe::new(Tick::from_seconds_f64(t), Value::Scalar(v));
+ rate.keys = if hold { vec![key(0.0, 3.0).hold(), key(cut, 0.0).hold()] } else { vec![key(0.0, 3.0), key(cut, 3.0), key(cut + 1.0 / 30.0, 0.0)] };
+ }
+ l.start_time = Tick::from_seconds_f64(start);
+ l.in_point = l.start_time;
+ l.out_point = Tick::from_seconds_f64(4.0);
+ l.stretch = stretch;
- p.comp_mut(cid).unwrap().layers = vec![l];
++ p.comp_mut(cid).unwrap().layers = vec![l].into();
+ (p, cid)
+ }
+
+ /// Total light in a frame.
+ fn light(img: &crate::Image) -> f32 {
+ img.data.iter().map(|p| p[0] + p[1] + p[2]).sum()
+ }
+
+ /// #1373: a Birth Rate keyed down to 0 stops new births; the particles already born live on
+ /// until their Longevity runs out (they vanished the frame the rate reached 0).
+ #[test]
+ fn particle_world_keeps_a_burst_after_birth_rate_drops_to_zero() {
+ let (keyed, cid) = burst_scene(Some(0.5), 0.0, 100.0);
+ let (constant, _) = burst_scene(None, 0.0, 100.0);
+ let at = Tick::from_seconds_f64;
+ assert_same(&render_cached(&keyed, cid, at(0.4), None), &render_cached(&constant, cid, at(0.4), None), "during the burst");
+ let (k, c) = (light(&render_cached(&keyed, cid, at(0.9), None)), light(&render_cached(&constant, cid, at(0.9), None)));
+ assert!(k > 1.0 && k < c, "the burst is still drawn, with no new births: {k} vs {c}");
+ // Longevity 1 s (± 20 %): gone by 1.9 s.
+ assert_eq!(light(&render_cached(&keyed, cid, at(1.9), None)), 0.0);
+ }
+
+ /// The burst's history: moving an earlier key changes a later frame whose Birth Rate is the
+ /// same (0), cached or not, and offset or stretched layers sample their own layer times.
+ #[test]
+ fn particle_world_birth_rate_history_with_the_layer_cache_and_time_mapping() {
+ let at = Tick::from_seconds_f64;
+ let (long, cid) = burst_scene(Some(0.5), 0.0, 100.0);
+ let (short, _) = burst_scene(Some(0.25), 0.0, 100.0);
+ let cache = crate::LayerCache::default();
+ let a = render_cached(&long, cid, at(0.9), Some(&cache));
+ let b = render_cached(&short, cid, at(0.9), Some(&cache));
+ assert_same(&b, &render_cached(&short, cid, at(0.9), None), "cached render after an earlier key moved");
+ assert!(light(&b) > 1.0 && light(&b) < light(&a), "a shorter burst: {} vs {}", light(&b), light(&a));
+ let plain = render_cached(&long, cid, at(0.9), None);
+ let (late, _) = burst_scene(Some(0.5), 0.5, 100.0);
+ assert_same(&render_cached(&late, cid, at(1.4), None), &plain, "a layer starting at 0.5 s");
+ let (slow, _) = burst_scene(Some(0.5), 0.0, 200.0);
+ assert_same(&render_cached(&slow, cid, at(1.8), None), &plain, "a layer stretched 200 %");
+ }
+
+ /// The Birth Rate history is read at exact layer times: under a fractional stretch, mapping a
+ /// layer time to comp time and back rounds down a tick, so a Hold cut at 0.5 s read its
+ /// previous value at the step starting there (six extra births).
+ #[test]
+ fn particle_world_reads_a_hold_cut_at_its_exact_layer_time_under_any_stretch() {
+ use crate::{EffectHost, FxHost, NoFootage, RenderOpts, Renderer};
+ let plain = {
+ let (p, cid) = burst_scene_keys(Some(0.5), 0.0, 100.0, true);
+ render_cached(&p, cid, Tick::from_seconds_f64(0.9), None)
+ };
+ assert!(light(&plain) > 1.0, "the burst is drawn");
+ for stretch in [33.3, 133.33, 200.0] {
+ let (p, cid) = burst_scene_keys(Some(0.5), 0.0, stretch, true);
+ let c = p.comp(cid).unwrap();
+ let l = &c.layers[0];
+ let ctx = crate::eval::EvalCtx::new(&p, cid, c, Tick::ZERO);
+ let renderer = Renderer::new(&p, &NoFootage, RenderOpts::default());
+ let host = FxHost { r: &renderer, ctx: &ctx, layer: l, origin: [0.0; 2], index: Default::default(), original: None };
+ let rate = |t: f64| host.param_at(0, "birthRate", t).unwrap().as_f64();
+ assert_eq!((rate(0.5 - 1.0 / 60.0), rate(0.5)), (3.0, 0.0), "stretch {stretch} %");
+ assert!(!host.param_is_static(0, "birthRate"));
+ let t = l.comp_time(Tick::from_seconds_f64(0.9));
+ assert_same(&render_cached(&p, cid, t, None), &plain, &format!("stretch {stretch} %"));
+ }
+ }
+
+ /// An expression that reads a Controller layer's Opacity (÷ 25) before layer time 0.5 s and is
+ /// 0 after (a stand-in for the expression engine).
+ struct ControllerRate;
+
+ impl crate::eval::ExprHost for ControllerRate {
+ fn eval(&self, ctx: &crate::eval::EvalCtx, layer: &effectcraft_project::Layer, _: &effectcraft_project::Property, _: &Value) -> Result<Value, String> {
+ if layer.layer_time(ctx.time).seconds() >= 0.5 {
+ return Ok(Value::Scalar(0.0));
+ }
+ let c = ctx.comp.layers.iter().find(|l| l.name == "Controller").ok_or("no controller")?;
+ let opacity = c.props.prop("transform/opacity").ok_or("no opacity")?;
+ Ok(Value::Scalar(ctx.value(c, opacity).as_f64() / 25.0))
+ }
+ }
+
+ /// A Birth Rate expression reading another layer: changing that layer changes the history but
+ /// not the current rate (0) nor this layer's properties, which is all the layer cache key sees.
+ /// CC Particle World layers with expressions therefore aren't layer cached.
+ #[test]
+ fn particle_world_with_a_birth_rate_expression_is_not_served_from_the_layer_cache() {
+ let scene = |opacity: f64| {
+ let (mut p, cid) = burst_scene(None, 0.0, 100.0);
+ let comp = p.comp(cid).unwrap().clone();
+ let mut ctl = solid(&mut p, &comp, [0.0, 0.0, 0.0], 10, 10);
+ ctl.name = "Controller".into();
+ ctl.switches.video = false;
+ ctl.props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(opacity);
+ let c = p.comp_mut(cid).unwrap();
+ let fx = c.layers[0].props.sub_mut("effects").unwrap().children.iter_mut().find_map(|n| match n {
+ effectcraft_project::Node::Group(g) => Some(g),
+ _ => None,
+ });
+ fx.unwrap().prop_mut("birthRate").unwrap().expr = Some(effectcraft_project::Expression { text: "controller".into(), enabled: true });
+ c.layers.push(ctl);
+ (p, cid)
+ };
+ let render = |p: &Project, cid: ItemId, cache: Option<&crate::LayerCache>| {
+ let mut r = crate::Renderer::new(p, &crate::NoFootage, crate::RenderOpts::default());
+ r.expr = Some(&ControllerRate);
+ r.cache = cache;
+ r.comp_frame(cid, Tick::from_seconds_f64(0.9))
+ };
+ let ((many, cid), (few, _)) = (scene(75.0), scene(25.0));
+ let cache = crate::LayerCache::default();
+ let a = render(&many, cid, Some(&cache));
+ let b = render(&few, cid, Some(&cache));
+ assert_same(&b, &render(&few, cid, None), "cached render after the controller changed");
+ assert!(light(&b) > 1.0 && light(&b) < light(&a), "fewer births: {} vs {}", light(&b), light(&a));
+ }
+
+ /// A grey solid (CC Mr. Mercury shades its blobs from it) with effect `id` (CC Particle Systems
+ /// II, CC Mr. Mercury): Birth Rate `rate`,
+ /// or keyed 3 until 0.5 s and 0 a frame later (`None`); Longevity 1 s.
+ fn system_scene(id: &str, rate: Option<f64>) -> (Project, ItemId) {
+ let (mut p, cid, comp) = setup();
+ let mut l = solid(&mut p, &comp, [0.5, 0.5, 0.5], 200, 100);
+ let vals = [("birthRate", Value::Scalar(rate.unwrap_or(3.0))), ("longevity", Value::Scalar(1.0))];
+ add_effect(&mut p, &mut l, id, [200.0, 100.0], &vals);
+ if rate.is_none() {
+ let fx = l.props.sub_mut("effects").unwrap().children.iter_mut().find_map(|c| match c {
+ effectcraft_project::Node::Group(g) => Some(g),
+ _ => None,
+ });
+ let key = |t: f64, v: f64| Keyframe::new(Tick::from_seconds_f64(t), Value::Scalar(v));
+ fx.unwrap().prop_mut("birthRate").unwrap().keys = vec![key(0.0, 3.0), key(0.5, 3.0), key(0.5 + 1.0 / 30.0, 0.0)];
+ }
- p.comp_mut(cid).unwrap().layers = vec![l];
++ p.comp_mut(cid).unwrap().layers = vec![l].into();
+ (p, cid)
+ }
+
+ /// #1373 for CC Particle Systems II and CC Mr. Mercury through the renderer: a keyed burst
+ /// draws as the constant rate during it, still draws after the rate drops to 0 (unlike a rate
+ /// of 0 throughout), and draws as nothing born once Longevity has run out.
+ #[test]
+ fn particle_systems_ii_and_mr_mercury_keep_a_burst_after_birth_rate_drops_to_zero() {
+ let at = Tick::from_seconds_f64;
+ for id in ["ec.sim.ccparticlesystems2", "ec.sim.ccmrmercury"] {
+ let (keyed, cid) = system_scene(id, None);
+ let (constant, _) = system_scene(id, Some(3.0));
+ let (none, _) = system_scene(id, Some(0.0));
+ assert_same(&render_cached(&keyed, cid, at(0.4), None), &render_cached(&constant, cid, at(0.4), None), &format!("{id}: during the burst"));
+ let (k, z) = (render_cached(&keyed, cid, at(0.9), None), render_cached(&none, cid, at(0.9), None));
+ assert!((light(&k) - light(&z)).abs() > 1.0, "{id}: the burst is still drawn: {} vs {}", light(&k), light(&z));
+ assert_same(&render_cached(&keyed, cid, at(1.9), None), &render_cached(&none, cid, at(1.9), None), &format!("{id}: gone after Longevity"));
+ }
+ }
+
+ /// A Birth Rate expression on any of the stepped particle systems keeps the layer out of the
+ /// layer cache (its key can't see what the expression returned at earlier times); without one
+ /// the layer is cached.
+ #[test]
+ fn stepped_particle_layers_with_a_birth_rate_expression_are_not_layer_cached() {
+ for id in ["ec.sim.ccparticleworld", "ec.sim.ccparticlesystems2", "ec.sim.ccmrmercury"] {
+ let (mut p, cid) = system_scene(id, Some(2.0));
+ let key = |p: &Project| {
+ let c = p.comp(cid).unwrap();
+ crate::cache::layer_key(&crate::eval::EvalCtx::new(p, cid, c, Tick::from_seconds_f64(0.9)), &c.layers[0], 1.0, false, false)
+ };
+ assert!(key(&p).is_some(), "{id}: cached without an expression");
+ let fx = p.comp_mut(cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.iter_mut().find_map(|c| match c {
+ effectcraft_project::Node::Group(g) => Some(g),
+ _ => None,
+ });
+ fx.unwrap().prop_mut("birthRate").unwrap().expr = Some(effectcraft_project::Expression { text: "time < 0.5 ? 3 : 0".into(), enabled: true });
+ assert!(key(&p).is_none(), "{id}: not cached with a Birth Rate expression");
+ }
+ }
+
// ------------------------------------------------------------------------------ layer cache
fn render_cached(p: &Project, cid: ItemId, t: Tick, cache: Option<&crate::LayerCache>) -> crate::Image {
@@@ -1073,3 -1276,194 +1276,194 @@@ fn workers_rendering_a_layer_at_once_sh
assert!(bufs.iter().all(|b| std::sync::Arc::ptr_eq(b, &bufs[0])), "one shared buffer");
assert_eq!(cache.stats().entries, 1);
}
+
+ // ------------------------------------------------------------------------------ 3D layer instances
+
+ /// A red triangle in the Z = 0 plane (the engine instance3d tests' glTF, buffer embedded as a
+ /// data URI), parsed in memory: a FootageSource that knows one model item.
+ const TRIANGLE: &str = r#"{
+ "asset": {"version": "2.0"},
+ "scenes": [{"nodes": [0]}],
+ "nodes": [{"mesh": 0}],
+ "meshes": [{"primitives": [{"attributes": {"POSITION": 0, "NORMAL": 1}, "material": 0}]}],
+ "materials": [{"pbrMetallicRoughness": {"baseColorFactor": [1.0, 0.0, 0.0, 1.0]}}],
+ "buffers": [{"byteLength": 72, "uri": "data:application/octet-stream;base64,AACAvwAAgL8AAAAAAACAPwAAgL8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/"}],
+ "bufferViews": [
+ {"buffer": 0, "byteOffset": 0, "byteLength": 36},
+ {"buffer": 0, "byteOffset": 36, "byteLength": 36}
+ ],
+ "accessors": [
+ {"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 3, "min": [-1.0, -1.0, 0.0], "max": [1.0, 1.0, 0.0]},
+ {"bufferView": 1, "componentType": 5126, "type": "VEC3", "count": 3}
+ ]
+ }"#;
+
+ struct SoloModels;
+
+ impl crate::FootageSource for SoloModels {
+ fn frame(&self, _: ItemId, _: &effectcraft_project::Footage, _: Tick) -> Option<std::sync::Arc<crate::Image>> {
+ None
+ }
+ fn model(&self, _: ItemId, f: &effectcraft_project::Footage) -> Option<std::sync::Arc<effectcraft_model::Model>> {
+ effectcraft_model::load(&f.path, TRIANGLE.as_bytes(), &|_| None).ok().map(std::sync::Arc::new)
+ }
+ }
+
+ /// A red triangle 3D model, a camera panned right by `pan` px and, on top of the stack, the
+ /// copy: a solid with a 3D Layer Instance effect referencing the model.
+ fn instance_scene(pan: f64) -> (Project, ItemId) {
+ let (mut p, cid, comp) = setup();
+ // Models are Advanced 3D (layer.newModel switches a Classic comp over).
+ p.comp_mut(cid).unwrap().renderer = effectcraft_project::Renderer::Advanced3D;
+ let mid = p.add_item(
+ "Triangle",
+ Label::Red,
+ None,
+ ItemKind::Footage(effectcraft_project::Footage {
+ path: "Triangle.gltf".into(),
+ kind: effectcraft_project::FootageKind::Model,
+ frame_rate: FrameRate::FPS_30,
+ codec: "glTF".into(),
+ ..Default::default()
+ }),
+ );
+ let model = build::layer(&mut p, &comp, "Triangle", LayerSource::Model { item: mid }, (200, 100), None);
+ let mut cam = build::layer(&mut p, &comp, "Camera", LayerSource::Camera, (200, 100), None);
+ for path in ["transform/position", "transform/poi"] {
+ let pr = cam.props.prop_mut(path).unwrap();
+ let Value::Vec3(v) = pr.value else { panic!("{path}: {:?}", pr.value) };
+ pr.value = Value::Vec3([v[0] + pan, v[1], v[2]]);
+ }
+ let mut owner = solid(&mut p, &comp, [1.0, 1.0, 1.0], 200, 100);
+ add_effect_200(&mut p, &mut owner, "ec.3d.instance3d", &[("layer", Value::Layer(Some(model.id.0)))]);
- p.comp_mut(cid).unwrap().layers = vec![cam, owner, model];
++ p.comp_mut(cid).unwrap().layers = vec![cam, owner, model].into();
+ (p, cid)
+ }
+
+ fn render_models(p: &Project, cid: ItemId, t: Tick, cache: Option<&crate::LayerCache>) -> crate::Image {
+ let mut r = crate::Renderer::new(p, &SoloModels, crate::RenderOpts::default());
+ r.cache = cache;
+ r.comp_frame(cid, t)
+ }
+
+ /// The coverage-weighted centre of the triangle (x): where its drawn pixels are, lit or not.
+ fn cover_x(img: &crate::Image) -> f32 {
+ let (mut m, mut sx) = (0.0, 0.0);
+ for y in 0..img.height {
+ for x in 0..img.width {
+ let a = img.get(x as i64, y as i64)[3];
+ m += a;
+ sx += a * x as f32;
+ }
+ }
+ assert!(m > 1.0, "the triangle is drawn");
+ sx / m
+ }
+
+ /// Create 3D Layer Instance: the copy (the owner) is cached, and its key covers what the
+ /// referenced model's solo render depends on — the camera, and the model's in/out gate.
+ #[test]
+ fn an_instance_of_a_model_is_cached_and_follows_the_camera() {
+ let t = Tick::from_seconds_f64(1.0);
+ let (p, cid) = instance_scene(0.0);
+ let (q, _) = instance_scene(40.0);
+ let key_at = |p: &Project, t: Tick| {
+ let c = p.comp(cid).unwrap();
+ crate::cache::layer_key(&crate::EvalCtx::new(p, cid, c, t), &c.layers[1], 1.0, false, false).unwrap()
+ };
+ assert_ne!(key_at(&p, t), key_at(&q, t), "moving the camera changes the copy's key");
+ // The same layer at a later time, outside its in/out range: the read turns transparent.
+ let mut out = p.clone();
+ out.comp_mut(cid).unwrap().layers[2].out_point = Tick::from_seconds_f64(0.5);
+ assert_ne!(key_at(&out, Tick::from_seconds_f64(0.25)), key_at(&out, t), "leaving the model's in/out range changes the copy's key");
+ // Two renders of one time: the second is served from the cache.
+ let cache = crate::LayerCache::default();
+ let here = render_models(&p, cid, t, Some(&cache));
+ let again = render_models(&p, cid, t, Some(&cache));
+ assert_same(&again, &render_models(&p, cid, t, None), "the second render is the cached one");
+ assert!(cache.stats().hits > 0, "the copy was served from the cache");
+ // Panning the camera: the copy (and the model) move with it.
+ let panned = render_models(&q, cid, t, Some(&cache));
+ assert_same(&panned, &render_models(&q, cid, t, None), "cached render after the camera moved");
+ let (a, b) = (cover_x(&here), cover_x(&panned));
+ assert!(b < a - 20.0, "panning the camera right moves the copy left: {a} -> {b}");
+ }
+
+ /// The camera's depth of field blurs the solo render (focus, aperture, iris, highlights) —
+ /// none of it is in the projection matrix: changing any of it must change the copy's key, or
+ /// it would serve the un-blurred pixels.
+ #[test]
+ fn the_copy_key_covers_the_cameras_depth_of_field() {
+ let t = Tick::from_seconds_f64(1.0);
+ let (p, cid) = instance_scene(0.0);
+ let set_dof = |p: &Project, on: bool, focus: f64, aperture: f64| {
+ let mut q = p.clone();
+ let cam = &mut q.comp_mut(cid).unwrap().layers[0];
+ for (path, v) in
+ [("cameraOptions/dof", Value::Bool(on)), ("cameraOptions/focusDistance", Value::Scalar(focus)), ("cameraOptions/aperture", Value::Scalar(aperture))]
+ {
+ cam.props.prop_mut(path).unwrap().value = v;
+ }
+ q
+ };
+ let key_at = |p: &Project| {
+ let c = p.comp(cid).unwrap();
+ crate::cache::layer_key(&crate::EvalCtx::new(p, cid, c, t), &c.layers[1], 1.0, false, false).unwrap()
+ };
+ let ka = key_at(&set_dof(&p, true, 500.0, 20.0));
+ let kb = key_at(&set_dof(&p, true, 500.0, 40.0)); // aperture
+ let kc = key_at(&set_dof(&p, true, 700.0, 20.0)); // focus
+ let kd = key_at(&set_dof(&p, false, 500.0, 20.0)); // the switch off
+ assert!(ka != kb && ka != kc && ka != kd && kb != kc, "aperture, focus and the switch each change the key");
+ }
+
+ /// A model with an animation clip selected is posed at its layer time: every time is its own
+ /// entry. Without one (the default) the pose is the same at every time: one key inside the
+ /// in/out range, as before.
+ #[test]
+ fn the_copy_key_covers_an_animated_models_layer_time() {
+ let (p, cid) = instance_scene(0.0);
+ let (t0, t1) = (Tick::from_seconds_f64(0.5), Tick::from_seconds_f64(1.0));
+ let key_at = |p: &Project, t: Tick| {
+ let c = p.comp(cid).unwrap();
+ crate::cache::layer_key(&crate::EvalCtx::new(p, cid, c, t), &c.layers[1], 1.0, false, false).unwrap()
+ };
+ assert_eq!(key_at(&p, t0), key_at(&p, t1), "a model without a clip keeps one key across times");
+ let mut q = p.clone();
+ q.comp_mut(cid).unwrap().layers[2].props.prop_mut("geometryOptions/animation").unwrap().value = Value::Enum(1);
+ assert_ne!(key_at(&q, t0), key_at(&q, t1), "an animated model's key follows its layer time");
+ // Two times and back: each is rendered fresh, none served another time's pixels.
+ let cache = crate::LayerCache::default();
+ let first = render_models(&q, cid, t0, Some(&cache));
+ let second = render_models(&q, cid, t1, Some(&cache));
+ assert_eq!(cache.stats().hits, 0, "the second time is not served the first time's pixels");
+ assert_same(&second, &render_models(&q, cid, t1, None), "the second time is a fresh render");
+ let again = render_models(&q, cid, t0, Some(&cache));
+ assert_eq!(cache.stats().hits, 1, "the first time is served on the way back");
+ assert_same(&again, &first, "the way back is the first time's pixels");
+ }
+
+ /// An Environment light's image lights Advanced 3D models — its source can be any layer's
+ /// render: with one on, the read is not cached, so changes to it cannot reuse stale pixels.
+ #[test]
+ fn an_environment_light_makes_the_copy_uncached() {
+ let (p, cid) = instance_scene(0.0);
+ let mut q = p.clone();
+ let comp = q.comp(cid).unwrap().clone();
+ let env = build::layer(&mut q, &comp, "Env", LayerSource::Light { kind: effectcraft_project::LightKind::Environment }, (200, 100), None);
+ q.comp_mut(cid).unwrap().layers.insert(0, env);
+ let t = Tick::from_seconds_f64(1.0);
+ let key_at = |p: &Project| {
+ let c = p.comp(cid).unwrap();
+ crate::cache::layer_key(&crate::EvalCtx::new(p, cid, c, t), &c.layers[2], 1.0, false, false)
+ };
+ assert!(key_at(&q).is_none(), "with an Environment light the read is not cached");
+ let cache = crate::LayerCache::default();
+ let a = render_models(&q, cid, t, Some(&cache));
+ let b = render_models(&q, cid, t, Some(&cache));
+ assert_same(&a, &b, "two fresh renders agree");
+ assert_eq!(cache.stats().hits, 0, "nothing is served from the cache");
+ // The light off (not lighting): the cache comes back.
+ q.comp_mut(cid).unwrap().layers[0].switches.video = false;
+ assert!(key_at(&q).is_some(), "the light off, the read is cached again");
+ }
diff --cc crates/render/src/tests/collapse.rs
index 6f2ad8441,eb340a4ca..30d2163c1
--- a/crates/render/src/tests/collapse.rs
+++ b/crates/render/src/tests/collapse.rs
@@@ -110,6 -110,66 +110,66 @@@ fn masks_on_a_collapsed_layer_force_a_f
assert!(max_diff(&a, &b) < 1e-4, "{}", max_diff(&a, &b));
}
+ /// #1747: the flattened path honours a composition proxy and frame blending; a collapsed layer
+ /// renders the nested comp directly and ignores both, so such a layer must not collapse.
+ #[test]
+ fn proxy_and_frame_blending_keep_a_precomp_on_the_flattened_path() {
+ let (mut p, cid) = scene(true, false);
+ let id = p.comp(cid).unwrap().layers[0].id;
+ let collapses = |p: &Project| {
+ let r = Renderer::new(p, &NoFootage, RenderOpts::default());
+ let ctx = EvalCtx::new(p, cid, p.comp(cid).unwrap(), Tick::ZERO);
+ r.collapsed(&ctx, ctx.comp.layer(id).unwrap()).is_some()
+ };
+ assert!(collapses(&p), "a plain precomp collapses");
+ // Frame blending needs both the comp's switch and the layer's.
+ p.comp_mut(cid).unwrap().enable_frame_blending = true;
+ p.comp_mut(cid).unwrap().layers[0].switches.frame_blend = FrameBlend::FrameMix;
+ assert!(!collapses(&p), "frame blending keeps the flattened path");
+ p.comp_mut(cid).unwrap().enable_frame_blending = false;
+ p.comp_mut(cid).unwrap().layers[0].switches.frame_blend = FrameBlend::Off;
+ // A composition proxy stands in for the precomp's pixels.
+ let LayerSource::Comp { item } = p.comp(cid).unwrap().layers[0].source else { panic!("a precomp layer") };
+ let proxy =
+ effectcraft_project::Proxy { footage: effectcraft_project::Footage { has_video: true, width: 32, height: 32, ..Default::default() }, enabled: true };
+ p.item_mut(item).unwrap().proxy = Some(Box::new(proxy));
+ assert!(!collapses(&p), "a proxy keeps the flattened path");
+ }
+
+ /// #1747: an effect on B that reads A must not reuse its cached buffer when two collapsed
+ /// instances give A different static overrides (`hash_values` only sees animated values).
+ #[test]
+ fn referenced_layer_overrides_change_the_owner_cache_key() {
+ let mut p = project();
+ let comp = Comp::new(100, 100, FrameRate::FPS_30, Tick::from_seconds_f64(2.0));
+ let a = solid(&mut p, &comp, [1.0, 1.0, 1.0], (40, 40), [30.0, 50.0, 0.0]);
+ let aid = a.id;
+ let mut b = solid(&mut p, &comp, [1.0, 1.0, 1.0], (100, 100), [50.0, 50.0, 0.0]);
+ let bid = b.id;
+ let mut next = p.next_id;
+ let mut ids = Ids(&mut next);
+ let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap();
+ let mut fx = effectcraft_effects::instantiate(spec, &mut ids, spec.name, [100.0, 100.0]);
+ fx.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(a.id.0));
+ p.next_id = next;
+ b.props.sub_mut("effects").unwrap().children.push(fx.into());
+ let mut c = comp;
- c.layers = vec![b, a];
++ c.layers = vec![b, a].into();
+ let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(c.into()));
+ let comp = p.comp(cid).unwrap();
+ let b = comp.layer(bid).unwrap();
+ let opacity = comp.layer(aid).unwrap().props.prop("transform/opacity").unwrap().uid;
+ let key = |value: f64| {
+ let overrides = [crate::PropertyOverride { layer: aid, prop: opacity, value: Value::Scalar(value), font: false }];
+ let ctx =
+ EvalCtx { project: &p, comp_id: cid, comp, time: Tick::ZERO, expr: None, footage: None, property_overrides: &overrides, media_overrides: &[] };
+ crate::cache::layer_key(&ctx, b, 1.0, false, false)
+ };
+ let (opaque, half) = (key(100.0), key(50.0));
+ assert!(opaque.is_some(), "the owner is cacheable");
+ assert_ne!(opaque, half, "A's static override changes B's key");
+ }
+
#[test]
fn collapsed_3d_layers_join_the_parent_scene() {
// Parent: a red 3D solid at z = 0 above (in the stack) a collapsed 3D precomp holding a