From 089f8e374d6cd25a2862545f7f1d92978d124ae5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 01:55:41 +0100 Subject: [PATCH] Footage cache (#3271): plan the aux channels before the render FootageSource gains aux_for (only what a need reads), aux_names (names from the headers) and set_aux_needs (per footage file, what its effects read, so the picture is decoded with those channels in one pass); the wrappers forward them. The renderer asks for the union of the layer's effects' needs; the engine sends each aux footage's needs with the aux footage set (all channels when a parameter is animated or an effect doesn't say); layer.channels and the popups read names only. --- crates/automation/src/confine.rs | 12 ++++++++ crates/engine/src/cmd/effect.rs | 8 +++-- crates/engine/src/cmd/watch_folder.rs | 1 + crates/engine/src/lib.rs | 4 +++ crates/engine/src/session/edit.rs | 1 + crates/engine/src/session/render.rs | 42 +++++++++++++++++++++++++++ crates/render/src/accel.rs | 25 +++++++++++++++- crates/render/src/footage.rs | 26 +++++++++++++++++ 8 files changed, 115 insertions(+), 4 deletions(-) diff --git a/crates/automation/src/confine.rs b/crates/automation/src/confine.rs index 570726932..8ecb31019 100644 --- a/crates/automation/src/confine.rs +++ b/crates/automation/src/confine.rs @@ -389,10 +389,22 @@ impl FootageSource for ConfinedFootage { fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { self.readable(footage).then(|| self.inner.aux(item, footage, t)).flatten() } + /// The channels `need` reads of the footage frame, or `None` when the footage lies outside the allowed roots. + fn aux_for(&self, item: ItemId, footage: &Footage, t: Tick, need: &effectcraft_engine::raster::channels3d::AuxNeed) -> Option> { + self.readable(footage).then(|| self.inner.aux_for(item, footage, t, need)).flatten() + } + /// The channel names of the footage frame, or `None` when the footage lies outside the allowed roots. + fn aux_names(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { + self.readable(footage).then(|| self.inner.aux_names(item, footage, t)).flatten() + } /// Forwards the auxiliary footage paths to the inner source. fn set_aux_footage(&self, paths: &[String]) { self.inner.set_aux_footage(paths) } + /// Forwards what effects read of each auxiliary footage to the inner source. + fn set_aux_needs(&self, needs: &[(String, effectcraft_engine::raster::channels3d::AuxNeed)]) { + self.inner.set_aux_needs(needs) + } /// The 3D model for the footage, or `None` when the footage lies outside the allowed roots. fn model(&self, item: ItemId, footage: &Footage) -> Option> { self.readable(footage).then(|| self.inner.model(item, footage)).flatten() diff --git a/crates/engine/src/cmd/effect.rs b/crates/engine/src/cmd/effect.rs index 1d352d192..a33301520 100644 --- a/crates/engine/src/cmd/effect.rs +++ b/crates/engine/src/cmd/effect.rs @@ -422,7 +422,7 @@ struct CryptoHit { impl CryptoHit { fn manifest(&self) -> Option<&[(String, u32)]> { - self.aux.manifests.iter().find(|(l, _)| *l == self.layer).map(|(_, m)| m.as_slice()) + self.aux.manifest(&self.layer) } fn reply(&self, sel: &str) -> Value { let selected = effectcraft_effects::selection_hashes(sel, self.manifest()).contains(&self.hash); @@ -1259,12 +1259,14 @@ fn list(_: &mut Session, p: &Value) -> Result { impl Session { /// The named channels of layer `lid` in the active comp at the current time: its /// multi-layer OpenEXR footage's layers and channels (`diffuse.R`, `depth.Z`, …), which - /// EXtractoR and the 3D Channel effects read. `None` for other layers. + /// EXtractoR and the 3D Channel effects read, and its Cryptomatte manifests. The names come + /// from the file's headers ([`effectcraft_render::FootageSource::aux_names`]): the planes + /// may be empty, so this is for lists and popups only. `None` for other layers. pub fn layer_channels(&self, lid: LayerId) -> Option> { let layer = self.active_comp()?.layer(lid)?; let effectcraft_project::LayerSource::Footage { item } = layer.source else { return None }; let effectcraft_project::ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None }; - self.footage.aux(item, f, layer.layer_time(self.time())) + self.footage.aux_names(item, f, layer.layer_time(self.time())) } } diff --git a/crates/engine/src/cmd/watch_folder.rs b/crates/engine/src/cmd/watch_folder.rs index 6e480a9e6..b8d476d0b 100644 --- a/crates/engine/src/cmd/watch_folder.rs +++ b/crates/engine/src/cmd/watch_folder.rs @@ -91,6 +91,7 @@ fn render_project(host: &Session, path: &Path) -> Value { // The project shared `host`'s footage source and gave it its own set of footage effects read // the channels of (#474): give back the host's. host.footage.set_aux_footage(host.aux_footage.as_deref().unwrap_or(&[])); + host.footage.set_aux_needs(host.aux_needs.as_deref().unwrap_or(&[])); let mut v = match result { Ok(v) => v, Err(e) => json!({"state": "failed", "error": e, "items": []}), diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 599aba1b7..9b7ef6a0b 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -260,6 +260,9 @@ pub struct Session { /// The footage files effects read the channels of, as last given to the footage source /// ([`Session::refresh_aux_footage`]); `None` = not given yet. pub aux_footage: Option>, + /// What the effects read of each of those files, as last given ([`Session::refresh_aux_footage`], + /// #3271); `None` = not given yet. + pub aux_needs: Option>, /// How many script-issued commands are running ([`Session::execute_for_script`]): while one /// is, settings that guard what scripts may do can't change (see /// [`prefs::Scripting::same_permissions`]). @@ -388,6 +391,7 @@ impl Default for Session { stored_paths: Default::default(), footage_stamps: Default::default(), aux_footage: None, + aux_needs: None, script_calls: ScriptCalls::default(), } } diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 67a8e4dcb..3870e6ebd 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -220,6 +220,7 @@ impl Session { self.project = Arc::new(p); // The set of footage effects read the channels of is sent again, even when it is empty. self.aux_footage = None; + self.aux_needs = None; self.history = History::default(); self.state = EditorState { snapping: true, view_layout: 1, ..Default::default() }; self.path = path; diff --git a/crates/engine/src/session/render.rs b/crates/engine/src/session/render.rs index 2dd8a3e7f..5c5a36d8a 100644 --- a/crates/engine/src/session/render.rs +++ b/crates/engine/src/session/render.rs @@ -155,9 +155,51 @@ impl Session { self.footage.set_aux_footage(&paths); self.aux_footage = Some(paths); } + let needs = aux_needs(&self.project); + if self.aux_needs.as_ref() != Some(&needs) { + self.footage.set_aux_needs(&needs); + self.aux_needs = Some(needs); + } } } +/// What the effects of [`aux_footage`]'s layers read of each file's channels (#3271), by +/// [`effectcraft_project::Footage::path`], sorted: the union of what each enabled effect reading +/// them declares ([`effectcraft_effects::aux_needs`]) with its parameters' values. An effect that +/// doesn't declare, or whose parameters change over time (keyframes, an expression), reads all +/// channels. Several layers of one file add up. +pub(crate) fn aux_needs(project: &effectcraft_project::Project) -> Vec<(String, effectcraft_raster::channels3d::AuxNeed)> { + use effectcraft_project::{GroupKind, ItemKind, LayerSource, Node}; + use effectcraft_raster::channels3d::AuxNeed; + let mut out: std::collections::BTreeMap = std::collections::BTreeMap::new(); + for l in project.comps().flat_map(|(_, c)| c.layers.iter()).filter(|l| l.switches.effects) { + let LayerSource::Footage { item } = &l.source else { continue }; + let Some(ItemKind::Footage(f)) = project.item(*item).map(|i| &i.kind) else { continue }; + if f.path.is_empty() { + continue; + } + for g in l.effects().into_iter().flat_map(|fx| fx.groups()).filter(|g| g.enabled) { + let GroupKind::Effect { effect } = &g.kind else { continue }; + if !effectcraft_effects::reads_aux(effect) { + continue; + } + let moving = g.children.iter().any(|c| matches!(c, Node::Prop(p) if p.is_animated() || p.has_expression())); + let need = if moving { + AuxNeed::All + } else { + let params = effectcraft_effects::flatten_params(g, &mut |p| p.value.clone()); + effectcraft_effects::aux_needs(effect, ¶ms).map_or(AuxNeed::All, AuxNeed::Lookups) + }; + let merged = match out.remove(&f.path) { + Some(e) => e.union(need), + None => need, + }; + out.insert(f.path.clone(), merged); + } + } + out.into_iter().collect() +} + /// The files (each footage item's [`effectcraft_project::Footage::path`], sorted, once each) of /// the footage layers that have an enabled effect reading their channels /// ([`effectcraft_effects::reads_aux`]), with the layer's Effects switch on. Several items can diff --git a/crates/render/src/accel.rs b/crates/render/src/accel.rs index 0f271c59d..a7954d263 100644 --- a/crates/render/src/accel.rs +++ b/crates/render/src/accel.rs @@ -224,7 +224,9 @@ impl<'a> Renderer<'a> { LayerSource::Footage { item } => { let item = ctx.media_item(layer, *item); let ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None }; - let a = self.read(|| self.footage.aux(item, f, ctx.source_time(layer)))?; + // Only what the layer's effects read (#3271). + let need = self.layer_aux_need(ctx, layer); + let a = self.read(|| self.footage.aux_for(item, f, ctx.source_time(layer), &need))?; // Aux pixels per layer pixel (the layer is the footage at its nominal size). The // planes are only copied when that changes the scale. let scale = a.width as f64 / f.width as f64; @@ -245,6 +247,27 @@ impl<'a> Renderer<'a> { } } + /// What the enabled effects of `layer` read of its auxiliary channels at the context time + /// ([`effectcraft_effects::aux_needs`], #3271): the union of their lookups, or all channels + /// when one doesn't say (or none reads them). + pub fn layer_aux_need(&self, ctx: &EvalCtx, layer: &Layer) -> effectcraft_raster::channels3d::AuxNeed { + use effectcraft_raster::channels3d::AuxNeed; + let mut need: Option = None; + for g in layer.effects().into_iter().flat_map(|fx| fx.groups()).filter(|g| g.enabled) { + let effectcraft_project::GroupKind::Effect { effect } = &g.kind else { continue }; + if !effectcraft_effects::reads_aux(effect) { + continue; + } + let params = effectcraft_effects::flatten_params(g, &mut |pr| ctx.value(layer, pr)); + let one = effectcraft_effects::aux_needs(effect, ¶ms).map_or(AuxNeed::All, AuxNeed::Lookups); + need = Some(match need { + Some(n) => n.union(one), + None => one, + }); + } + need.unwrap_or_default() + } + /// Evaluation context of comp `comp_id` at comp time `t`. pub fn eval_ctx(&self, comp_id: ItemId, t: Tick) -> Option> { Some(self.ctx(comp_id, self.project.comp(comp_id)?, t)) diff --git a/crates/render/src/footage.rs b/crates/render/src/footage.rs index 0e17f152d..e6a7015ec 100644 --- a/crates/render/src/footage.rs +++ b/crates/render/src/footage.rs @@ -49,12 +49,29 @@ pub trait FootageSource: Send + Sync { fn aux(&self, _item: ItemId, _footage: &Footage, _t: Tick) -> Option> { None } + /// [`FootageSource::aux`] of only what `need` reads (#3271): the channels its lookups find, + /// or more (sources may hand over a set that holds them; [`effectcraft_raster::AuxChannels::listed`] + /// then names every channel). Sources that read every channel anyway can leave the default. + fn aux_for(&self, item: ItemId, footage: &Footage, t: Tick, need: &effectcraft_raster::channels3d::AuxNeed) -> Option> { + let _ = need; + self.aux(item, footage, t) + } + /// The channel names of [`FootageSource::aux`] (planes may be empty: read from the file's + /// headers alone), for channel lists and popups. Never handed to an effect. + fn aux_names(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { + self.aux(item, footage, t) + } /// The footage files (by [`Footage::path`]) whose channels effects of the project read /// through [`FootageSource::aux`] (EXtractoR, Cryptomatte and the other 3D Channel effects), /// replacing the set given before. A source that decodes a file's picture and its channels /// separately can decode both at once for these from the first frame on. Sources that don't /// ignore it. fn set_aux_footage(&self, _paths: &[String]) {} + /// What the effects reading the channels of each footage file (by [`Footage::path`], as + /// [`FootageSource::set_aux_footage`] names them) read of them (#3271), replacing what was + /// given before: the picture of such footage is then decoded together with those channels + /// only. A file not named here reads every channel. Sources that don't care ignore it. + fn set_aux_needs(&self, _needs: &[(String, effectcraft_raster::channels3d::AuxNeed)]) {} /// The parsed 3D model of a [`effectcraft_project::FootageKind::Model`] item (Advanced 3D). fn model(&self, _item: ItemId, _footage: &Footage) -> Option> { None @@ -174,9 +191,18 @@ impl FootageSource for CancellableFootage<'_> { fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { self.inner.aux(item, footage, t) } + fn aux_for(&self, item: ItemId, footage: &Footage, t: Tick, need: &effectcraft_raster::channels3d::AuxNeed) -> Option> { + self.inner.aux_for(item, footage, t, need) + } + fn aux_names(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { + self.inner.aux_names(item, footage, t) + } fn set_aux_footage(&self, paths: &[String]) { self.inner.set_aux_footage(paths) } + fn set_aux_needs(&self, needs: &[(String, effectcraft_raster::channels3d::AuxNeed)]) { + self.inner.set_aux_needs(needs) + } fn model(&self, item: ItemId, footage: &Footage) -> Option> { self.inner.model(item, footage) }