Layer cache: buffers that read other layers are keyed by the switches passed down

A layer whose effect reads another layer (Gradient Wipe, Set Matte, Blend…) gets
that layer drawn by its own renderer. Through a precomp layer, Switches Affect
Nested Comps, the switches inherited from the precomp layers above and motion
blur on change those pixels, but the key held only the layers' own switches: a
buffer made with the setting on was reused (from memory or the disk cache) with
it off. Such layers now derive their key with what the renderer passes down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Opus 5.5 committed 2026-11-07 02:42:22 +01:00
1 parent 47d8acb694
commit a5e673dd6a
4 files changed
+72

No files matched your search

+15
View File
@@ -22,6 +22,7 @@ use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use effectcraft_effects::Buf;
use effectcraft_keyframe::Value;
use effectcraft_project::{FootageKind, ItemKind, Layer, LayerSource, Node, PropGroup};
use crate::disk_cache::{EntryFiles, SharedVirtualFileId, VirtualFileId, layer_disk_key};
@@ -790,6 +791,20 @@ fn key_any(ctx: &EvalCtx, layer: &Layer, scale: f64, draft: bool, blur: bool, fo
Some(h.finish())
}
/// Does the layer read another layer through an enabled effect's layer parameter (Gradient Wipe,
/// Set Matte…)? Those layers are drawn by the reading layer's renderer, so the switches it
/// passes down belong in the key (`Renderer::nested_look`).
pub(crate) fn reads_layers(ctx: &EvalCtx, layer: &Layer) -> bool {
fn reads(ctx: &EvalCtx, layer: &Layer, g: &PropGroup, depth: usize) -> bool {
depth < 32
&& g.children.iter().any(|n| match n {
Node::Prop(p) => matches!(p.value, Value::Layer(_)) && ctx.value(layer, p).as_layer().is_some_and(|id| id != layer.id.0),
Node::Group(sub) => sub.enabled && reads(ctx, layer, sub, depth + 1),
})
}
layer.switches.effects && layer.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled && reads(ctx, layer, g, 0)))
}
/// Does the layer run an effect that reads the comp camera or lights (Card Dance, CC Particle
/// World…)?
fn reads_comp_scene(layer: &Layer) -> bool {
+4
View File
@@ -250,6 +250,10 @@ impl<'a> Renderer<'a> {
/// depends on the content at every sub-sample).
pub(crate) fn content_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option<u64>) -> Option<u64> {
let k = key?;
// A layer that reads other layers through its effects' layer parameters gets them drawn
// by this renderer: the switches it passes down (Switches Affect Nested Comps, the
// precomp layers above, motion blur on) change those pixels, not just the layers' own.
let k = if cache::reads_layers(ctx, layer) { cache::derive(k, self.nested_look()) } else { k };
let Some(times) = self.content_times(ctx, layer) else { return Some(k) };
times.iter().try_fold(cache::derive(k, 0x6d62_6c72), |d, t| Some(cache::derive(d, cache::layer_key(&ctx.at(*t), layer, 1.0, false, false)?)))
}
+18
View File
@@ -205,6 +205,24 @@ impl<'a> Renderer<'a> {
}
}
/// What this renderer passes down to the layers it draws beyond their own switches
/// ([`RenderOpts::nested_switches`], the switches inherited from the precomp layers above,
/// motion blur on): part of the cache key of a buffer that reads other layers.
pub(crate) fn nested_look(&self) -> u64 {
let inherited = match self.inherited {
None => 0,
Some((q, mb)) => {
let q = match q {
Quality::Best => 0,
Quality::Draft => 1,
Quality::Wireframe => 2,
};
1 + q * 2 + u64::from(mb)
}
};
0x6e65_7374_0000_0000 | inherited << 2 | u64::from(self.opts.nested_switches) << 1 | u64::from(self.opts.motion_blur)
}
/// A layer's Motion Blur switch, limited by the precomp layers above it.
pub(crate) fn layer_motion_blur(&self, layer: &Layer) -> bool {
layer.switches.motion_blur && self.inherited.is_none_or(|(_, mb)| mb)
+35
View File
@@ -820,6 +820,41 @@ fn effects_read_layer_params() {
assert!(px[2] > 0.99 && px[0] < 0.01, "{px:?}");
}
/// A cached layer whose effect reads a precomp layer: how the precomp's layers render depends on
/// Switches Affect Nested Comps (the precomp layer's Draft quality passes down) and on the
/// switches the reading layer's own comp inherits, so the key must tell them apart (a buffer
/// made under the other setting, from memory or disk, is stale).
#[test]
fn a_cached_layer_reading_a_precomp_is_keyed_by_the_nested_switches() {
let (mut p, cid, comp) = setup();
let mut inner = Comp::new(200, 100, FrameRate::FPS_30, Tick::from_seconds_f64(2.0));
// A rotated square off the pixel grid: Draft (nearest) and Best (bilinear) sampling differ.
let mut dot = solid(&mut p, &inner, [1.0, 1.0, 1.0], 40, 40);
dot.props.prop_mut("transform/position").unwrap().value = Value::Vec3([100.3, 50.6, 0.0]);
dot.props.prop_mut("transform/rotation").unwrap().value = Value::Scalar(30.0);
inner.layers.push(dot);
let iid = p.add_item("Inner", Label::Sandstone, None, ItemKind::Comp(inner.into()));
let mut pre = build::layer(&mut p, &comp, "Inner", LayerSource::Comp { item: iid }, (200, 100), None);
pre.switches.video = false;
pre.switches.quality = effectcraft_project::Quality::Draft;
let mut top = solid(&mut p, &comp, [1.0, 0.0, 0.0], 200, 100);
add_effect_200(&mut p, &mut top, "ec.channel.blend", &[("blendWithLayer", Value::Layer(Some(pre.id.0)))]);
let c = p.comp_mut(cid).unwrap();
c.layers.push(top);
c.layers.push(pre);
let frame = |nested_switches: bool, cache: Option<&crate::LayerCache>| {
let mut r = crate::Renderer::new(&p, &crate::NoFootage, crate::RenderOpts { nested_switches, ..Default::default() });
r.cache = cache;
r.comp_frame(cid, Tick::ZERO).data
};
let (on, off) = (frame(true, None), frame(false, None));
assert!(on != off, "the precomp layer's Draft quality passes down");
let cache = crate::LayerCache::default();
assert!(frame(true, Some(&cache)) == on);
assert!(frame(false, Some(&cache)) == off, "not the buffer made with switches affecting nested comps");
assert!(frame(true, Some(&cache)) == on, "and back");
}
/// Records whether the GPU compositor was asked to draw a frame.
struct ProbeAccel(std::sync::atomic::AtomicBool);
impl crate::Accelerator for ProbeAccel {