mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 00:55:43 +00:00
Footage cache (#3271): 3D Channel effects declare the aux channels they read
AuxNeed / AuxLookup describe what an effect reads as the lookups it makes on the planes (get, find, a Cryptomatte layer by Layer Name and slot); effects::aux_needs gives them for EXtractoR, Cryptomatte and the 3D Channel effects, resolved against a file's channel names. AuxChannels gains aliases (a multi-part file's stored channel names, Crypto_Material00.R in part Crypto_Material) and listed (every name when only some planes are held); the Cryptomatte layer slot rules move to raster so a declared need resolves the layer as the effect does.
This commit is contained in:
1 parent
404a62268d
commit
14e0665ef4
5 files changed
+416
-93
No files matched your search
@@ -19,7 +19,7 @@ use std::sync::Arc;
|
||||
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::ParamUi;
|
||||
use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, crypto_hash};
|
||||
use effectcraft_raster::channels3d::{AuxLookup, BACKGROUND_DEPTH, crypto_hash};
|
||||
use effectcraft_raster::{AuxChannels, Image, Px, gaussian_blur};
|
||||
use rayon::prelude::*;
|
||||
|
||||
@@ -49,6 +49,45 @@ pub fn reads_aux(id: &str) -> bool {
|
||||
READS_AUX.contains(&id)
|
||||
}
|
||||
|
||||
/// What effect `id` reads of its layer's auxiliary channels with `params`, declared before the
|
||||
/// render (#3271): the lookups it makes on the planes, which the footage resolves against the
|
||||
/// file's channel names to read only those channels (with the picture's, in one pass). `None`
|
||||
/// for an effect that doesn't say: all channels are read.
|
||||
pub fn aux_needs(id: &str, params: &crate::Params) -> Option<Vec<AuxLookup>> {
|
||||
use effectcraft_raster::channels3d::{COVERAGE_NAMES, DEPTH_NAMES, MATERIAL_ID_NAMES, OBJECT_ID_NAMES};
|
||||
let find = AuxLookup::find;
|
||||
// (as `id_channel` picks)
|
||||
let ids = |which: u32| if which == 1 { find(&MATERIAL_ID_NAMES) } else { find(&OBJECT_ID_NAMES) };
|
||||
Some(match id {
|
||||
// (as `channel_extract_at` looks them up, fallbacks included)
|
||||
"ec.3d.channelextract" => match params.e("channel") {
|
||||
0 => vec![find(&DEPTH_NAMES)],
|
||||
1 => vec![find(&OBJECT_ID_NAMES)],
|
||||
7 => vec![find(&MATERIAL_ID_NAMES)],
|
||||
4 => vec![find(&COVERAGE_NAMES)],
|
||||
2 => vec![find(&["UV.U", "U", "uv.x"]), find(&["UV.V", "V", "uv.y"]), find(&["UV.W", "W", "uv.z"])],
|
||||
3 => vec![find(&["N.X", "Normal.X", "normals.x"]), find(&["N.Y", "Normal.Y", "normals.y"]), find(&["N.Z", "Normal.Z", "normals.z"])],
|
||||
5 => vec![find(&["BG.R", "Background.R"]), find(&["BG.G", "Background.G"]), find(&["BG.B", "Background.B"])],
|
||||
_ => vec![find(&["R"]), find(&["G"]), find(&["B"])],
|
||||
},
|
||||
"ec.3d.depthmatte" | "ec.3d.depthoffield" | "ec.3d.fog3d" => vec![find(&DEPTH_NAMES)],
|
||||
"ec.3d.idmatte" => {
|
||||
let mut v = vec![ids(params.e("auxChannel"))];
|
||||
if params.b("useCoverage") {
|
||||
v.push(find(&COVERAGE_NAMES));
|
||||
}
|
||||
v
|
||||
}
|
||||
"ec.3d.identifier" => vec![ids(params.e("channelType"))],
|
||||
"ec.3d.cryptomatte" => vec![AuxLookup::Crypto { name: params.s("layerName").to_string(), index: params.e("layer") as usize }],
|
||||
// (an empty name reads the layer's own channel)
|
||||
"ec.3d.extractor" => {
|
||||
["red", "green", "blue", "alpha"].iter().map(|k| params.s(k).trim()).filter(|n| !n.is_empty()).map(|n| AuxLookup::Name(n.to_string())).collect()
|
||||
}
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn aux(ctx: &EffectCtx) -> Option<Arc<AuxChannels>> {
|
||||
ctx.env.host?.aux()
|
||||
}
|
||||
@@ -409,67 +448,9 @@ pub fn selection_hashes(sel: &str, manifest: Option<&[(String, u32)]>) -> Vec<u3
|
||||
out
|
||||
}
|
||||
|
||||
/// The Cryptomatte layers every renderer names, and the Layer popup's options when the footage
|
||||
/// has no Cryptomatte metadata. The stored Layer value is an index: 0-2 are these (an exact
|
||||
/// match or a suffix such as `ViewLayer.CryptoObject`), 3 and up are the file's other Cryptomatte
|
||||
/// layers in name order, so a project saved before the popup followed the footage loads unchanged.
|
||||
pub const CRYPTO_LAYERS: [&str; 3] = ["CryptoObject", "CryptoMaterial", "CryptoAsset"];
|
||||
|
||||
/// The Layer popup's entries for Cryptomatte `layers` (sorted, as [`AuxChannels::crypto_layers`]
|
||||
/// returns them): the stored value and the layer's name, in value order. Of several layers
|
||||
/// that fit one standard slot (`A.CryptoObject`, `B.CryptoObject`) the first one keeps the standard
|
||||
/// slot, so existing projects load unchanged, and the others get the next custom slots.
|
||||
fn crypto_layer_slots(layers: &[String]) -> Vec<(usize, &str)> {
|
||||
let mut out: Vec<(usize, &str)> = vec![];
|
||||
let mut custom = CRYPTO_LAYERS.len();
|
||||
for l in layers {
|
||||
match CRYPTO_LAYERS.iter().position(|w| l == w || l.ends_with(w)) {
|
||||
Some(i) if !out.iter().any(|(j, _)| *j == i) => out.push((i, l)),
|
||||
_ => {
|
||||
out.push((custom, l));
|
||||
custom += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.sort_by_key(|(i, _)| *i);
|
||||
out
|
||||
}
|
||||
|
||||
/// What the Layer popup lists for the footage `aux`: the Cryptomatte layers the file has, as
|
||||
/// (stored value, name), or the three standard ones when there is none (no footage, not an
|
||||
/// OpenEXR, no Cryptomatte channels).
|
||||
pub fn crypto_layer_choices(aux: Option<&AuxChannels>) -> Vec<(usize, String)> {
|
||||
crypto_layer_menu(aux, 0).0
|
||||
}
|
||||
|
||||
/// The Layer popup for the stored value `index`: [`crypto_layer_choices`], and the name of the
|
||||
/// entry the effect reads (the first when `index` names none, as the effect does).
|
||||
pub fn crypto_layer_menu(aux: Option<&AuxChannels>, index: usize) -> (Vec<(usize, String)>, Option<String>) {
|
||||
let layers = aux.map(AuxChannels::crypto_layers).unwrap_or_default();
|
||||
if layers.is_empty() {
|
||||
let all: Vec<(usize, String)> = CRYPTO_LAYERS.iter().enumerate().map(|(i, n)| (i, n.to_string())).collect();
|
||||
let shown = all.iter().find(|c| c.0 == index).or(all.first()).map(|c| c.1.clone());
|
||||
return (all, shown);
|
||||
}
|
||||
let slots = crypto_layer_slots(&layers);
|
||||
let shown = slots.iter().find(|(i, _)| *i == index).map(|(_, n)| n.to_string()).or_else(|| layers.first().cloned());
|
||||
(slots.into_iter().map(|(i, n)| (i, n.to_string())).collect(), shown)
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer the effect reads: the one called `name` when the footage has it (Layer
|
||||
/// Name, the layer an imported After Effects project names), else [`crypto_layer_name`]'s.
|
||||
pub fn crypto_layer_named(aux: &AuxChannels, name: &str, index: usize) -> Option<String> {
|
||||
aux.resolve_crypto_layer_named(name, &crypto_layer_name(aux, index)?)
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer of `aux` that the stored Layer value `index` reads: its slot's layer, else
|
||||
/// the first layer (what a file without that layer has always shown). `None` without any.
|
||||
pub fn crypto_layer_name(aux: &AuxChannels, index: usize) -> Option<String> {
|
||||
if aux.crypto_layers().is_empty() {
|
||||
return None;
|
||||
}
|
||||
crypto_layer_menu(Some(aux), index).1
|
||||
}
|
||||
/// The Cryptomatte Layer popup and the layer the effect reads (the slot rules live with
|
||||
/// [`AuxChannels`], so a declared need resolves the layer as the effect does, #3271).
|
||||
pub use effectcraft_raster::channels3d::{CRYPTO_LAYERS, crypto_layer_choices, crypto_layer_menu, crypto_layer_name, crypto_layer_named};
|
||||
|
||||
/// Rank planes as [`AuxChannels::crypto_rank_planes`] returns them.
|
||||
type RankPlanes<'a> = [(&'a [f32], &'a [f32])];
|
||||
@@ -585,7 +566,7 @@ pub fn matte_only_px(m: f32, in_alpha: bool) -> Px {
|
||||
fn cryptomatte(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let Some(a) = aux(ctx) else { return b };
|
||||
let Some(layer) = crypto_layer_named(&a, ctx.params.s("layerName"), ctx.params.e("layer") as usize) else { return b };
|
||||
let manifest = a.manifests.iter().find(|(l, _)| *l == layer).map(|(_, m)| m.as_slice());
|
||||
let manifest = a.manifest(&layer);
|
||||
let sel = selection_hashes(ctx.params.s("selection"), manifest);
|
||||
let idx = index_map(&b, &a);
|
||||
let display = ctx.params.e("display");
|
||||
|
||||
@@ -628,3 +628,72 @@ fn reads_aux_names_the_effects_that_read_channels() {
|
||||
}
|
||||
assert!(READS_AUX.iter().all(|id| specs().iter().any(|s| s.id == *id)), "every listed effect exists");
|
||||
}
|
||||
|
||||
/// [`scene`] with more channels: IDs, coverage, normals, UVs, a background, a colour layer and
|
||||
/// two more Cryptomatte layers, each with its own values.
|
||||
fn rich_scene() -> AuxHost {
|
||||
let mut a = (*scene().0).clone();
|
||||
let n = (a.width * a.height) as usize;
|
||||
let plane = |k: usize| (0..n).map(|i| ((i * 7 + k * 13) % 23) as f32 / 11.0 - 0.5).collect::<Vec<f32>>();
|
||||
let (ha, hc) = (crypto_hash("asset"), crypto_hash("custom"));
|
||||
let ids = |h: u32| (0..n).map(|i| if i % 3 == 0 { crypto_float(h) } else { 0.0 }).collect::<Vec<f32>>();
|
||||
let names = ["MaterialID", "Coverage", "N.X", "N.Y", "N.Z", "UV.U", "UV.V", "BG.R", "BG.G", "BG.B", "diffuse.R", "diffuse.G", "diffuse.B"];
|
||||
for (k, name) in names.iter().enumerate() {
|
||||
a.channels.push((name.to_string(), plane(k)));
|
||||
}
|
||||
a.channels.push(("A.CryptoAsset00.R".into(), ids(ha)));
|
||||
a.channels.push(("A.CryptoAsset00.G".into(), plane(40)));
|
||||
a.channels.push(("zz.Custom00.R".into(), ids(hc)));
|
||||
a.channels.push(("zz.Custom00.G".into(), plane(41)));
|
||||
a.manifests.push(("A.CryptoAsset".into(), vec![("asset".into(), ha)]));
|
||||
AuxHost(Arc::new(a))
|
||||
}
|
||||
|
||||
/// The parameters [`run_fx`] gives effect `id` with `vals` (on [`img`]).
|
||||
fn params_of(id: &str, vals: &[(&str, Value)]) -> crate::Params {
|
||||
let s = crate::find(id).expect("an effect");
|
||||
let mut p = crate::Params { values: s.params.iter().map(|p| (p.id.to_string(), crate::default_value(p, [20.0, 10.0]))).collect() };
|
||||
for (k, v) in vals {
|
||||
p.values.insert(k.to_string(), v.clone());
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// #3271: each 3D Channel effect, given only the channels it declares ([`aux_needs`], with every
|
||||
/// channel name listed), renders bit for bit what it renders with all channels, and reads fewer.
|
||||
#[test]
|
||||
fn declared_aux_needs_render_as_all_channels() {
|
||||
use effectcraft_raster::channels3d::AuxNeed;
|
||||
let full = rich_scene();
|
||||
let mut names = (*full.0).clone();
|
||||
for (_, p) in &mut names.channels {
|
||||
p.clear();
|
||||
}
|
||||
let s = |v: &str| Value::Str(v.into());
|
||||
let mut cases: Vec<(&str, Vec<(&str, Value)>)> = (0..8).map(|k| ("ec.3d.channelextract", vec![("channel", Value::Enum(k))])).collect();
|
||||
cases.extend([
|
||||
("ec.3d.depthmatte", vec![("depth", num(300.0))]),
|
||||
("ec.3d.depthoffield", vec![("focalPlane", num(300.0)), ("maximumRadius", num(4.0))]),
|
||||
("ec.3d.fog3d", vec![]),
|
||||
("ec.3d.idmatte", vec![("idSelection", num(1.0))]),
|
||||
("ec.3d.idmatte", vec![("auxChannel", Value::Enum(1)), ("useCoverage", Value::Bool(true))]),
|
||||
("ec.3d.identifier", vec![("id", num(2.0))]),
|
||||
("ec.3d.identifier", vec![("channelType", Value::Enum(1)), ("display", Value::Enum(3))]),
|
||||
("ec.3d.cryptomatte", vec![("selection", s("left")), ("display", Value::Enum(0))]),
|
||||
("ec.3d.cryptomatte", vec![("layer", Value::Enum(3)), ("display", Value::Enum(1)), ("selection", s("<*>"))]),
|
||||
("ec.3d.cryptomatte", vec![("layerName", s("A.CryptoAsset")), ("selection", s("asset"))]),
|
||||
("ec.3d.extractor", vec![("red", s("diffuse.R")), ("green", s(" N.X ")), ("blue", s("")), ("alpha", s("Coverage"))]),
|
||||
]);
|
||||
let bits = |i: &Image| i.data.iter().flat_map(|p| p.map(f32::to_bits)).collect::<Vec<_>>();
|
||||
for (id, vals) in cases {
|
||||
let need = aux_needs(id, ¶ms_of(id, &vals)).map_or(AuxNeed::All, AuxNeed::Lookups);
|
||||
let keep = need.resolve(&names).expect("declared");
|
||||
assert!(!keep.is_empty() && keep.len() < full.0.channels.len(), "{id} {vals:?}: {keep:?}");
|
||||
let mut some = (*full.0).clone();
|
||||
some.channels.retain(|(n, _)| keep.contains(n));
|
||||
some.listed = full.0.names().iter().map(|n| n.to_string()).collect();
|
||||
let (want, got) = (run(id, &vals, &full), run(id, &vals, &AuxHost(Arc::new(some))));
|
||||
assert_eq!(bits(&got), bits(&want), "{id} {vals:?}: {keep:?}");
|
||||
}
|
||||
assert_eq!(aux_needs("ec.blur.gaussian", &crate::Params::default()), None, "not declared: all");
|
||||
}
|
||||
@@ -117,7 +117,7 @@ pub use ocio::color_stabilizer_maps;
|
||||
// GPU effects, part B (3D Channel, Immersive Video, colour management, simulation render passes).
|
||||
pub use channel3d::{
|
||||
CRYPTO_LAYERS, CryptoAux, MATTE_ONLY_IN_ALPHA, aux_point, crypto_aux_image, crypto_aux_image_with, crypto_layer_choices, crypto_layer_menu,
|
||||
crypto_layer_name, crypto_layer_named, id_color, matte_only_px, reads_aux, selection_hashes,
|
||||
aux_needs, crypto_layer_name, crypto_layer_named, id_color, matte_only_px, reads_aux, selection_hashes,
|
||||
};
|
||||
pub use ocio::{ColorOp, Straight, Tf, color_program};
|
||||
pub use sim::{Acc, Blob, Bubble, Post, Shape, Sprite, SpritePlan, Tint, bubble_list, bubble_sample_bounds, drizzle_drops, mercury_blobs, sprite_plan};
|
||||
|
||||
@@ -269,7 +269,7 @@ fn identifier(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option<
|
||||
fn cryptomatte(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option<GBuf> {
|
||||
let pr = ctx.params;
|
||||
let Some(layer) = effectcraft_effects::crypto_layer_named(a, pr.s("layerName"), pr.e("layer") as usize) else { return Some(b) };
|
||||
let manifest = a.manifests.iter().find(|(l, _)| *l == layer).map(|(_, m)| m.as_slice());
|
||||
let manifest = a.manifest(&layer);
|
||||
let sel = effectcraft_effects::selection_hashes(pr.s("selection"), manifest);
|
||||
// Per aux pixel: coverage-weighted ID colours (RGB) and the selection's coverage (A), on all
|
||||
// cores with the rank planes resolved once.
|
||||
|
||||
+301
-28
@@ -17,9 +17,19 @@
|
||||
//! the object name reinterpreted as a float with the exponent nudged off 0 and 255, and the
|
||||
//! manifest maps names to hashes.
|
||||
|
||||
|
||||
/// Depth written where nothing was rendered.
|
||||
pub const BACKGROUND_DEPTH: f32 = 1.0e10;
|
||||
|
||||
/// The names [`AuxChannels::depth`] looks for, in order (also as `*.name` suffixes).
|
||||
pub const DEPTH_NAMES: [&str; 5] = ["Z", "depth", "Depth", "zDepth", "Z-Depth"];
|
||||
/// The names [`AuxChannels::object_id`] looks for.
|
||||
pub const OBJECT_ID_NAMES: [&str; 4] = ["ObjectID", "object_id", "objectId", "id"];
|
||||
/// The names [`AuxChannels::material_id`] looks for.
|
||||
pub const MATERIAL_ID_NAMES: [&str; 3] = ["MaterialID", "material_id", "materialId"];
|
||||
/// The names [`AuxChannels::coverage`] looks for.
|
||||
pub const COVERAGE_NAMES: [&str; 2] = ["Coverage", "coverage"];
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct AuxChannels {
|
||||
pub width: u32,
|
||||
@@ -30,39 +40,147 @@ pub struct AuxChannels {
|
||||
pub channels: Vec<(String, Vec<f32>)>,
|
||||
/// Cryptomatte manifests: (layer name, [(object name, hash)]).
|
||||
pub manifests: Vec<(String, Vec<(String, u32)>)>,
|
||||
/// Channels of a multi-part OpenEXR file whose stored name is itself a `layer.channel` name
|
||||
/// but doesn't start with its part's name (`Crypto_Material00.R` in part `Crypto_Material`):
|
||||
/// (the name as stored, the plane's full name `Crypto_Material.Crypto_Material00.R`).
|
||||
/// Lookups by the stored name find the plane; the full name is what is shown (#3271).
|
||||
pub aliases: Vec<(String, String)>,
|
||||
/// Every channel name of the source in order, when `channels` holds only some of them (the
|
||||
/// channels a layer's effects read, #3271); empty when `channels` holds them all. Lists
|
||||
/// that depend on every channel (the Cryptomatte layers) read it.
|
||||
pub listed: Vec<String>,
|
||||
}
|
||||
|
||||
/// What an effect reads of its layer's auxiliary channels, declared before the render
|
||||
/// ([`AuxNeed`]): each lookup is resolved against the source's channel names exactly as the
|
||||
/// effect resolves it on the planes, so the planes read are the ones it looks up (#3271).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum AuxLookup {
|
||||
/// [`AuxChannels::get`] of this name.
|
||||
Name(String),
|
||||
/// [`AuxChannels::find`] of these names.
|
||||
Find(Vec<String>),
|
||||
/// The rank planes of the Cryptomatte layer the effect reads: Layer Name `name`, else the
|
||||
/// layer of the Layer popup's stored value `index` ([`crypto_layer_named`]).
|
||||
Crypto { name: String, index: usize },
|
||||
}
|
||||
|
||||
impl AuxLookup {
|
||||
/// [`AuxLookup::Find`] of `names`.
|
||||
pub fn find(names: &[&str]) -> AuxLookup {
|
||||
AuxLookup::Find(names.iter().map(|n| n.to_string()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// The auxiliary channels a layer's effects read (#3271): all of them (an effect that doesn't
|
||||
/// declare, or parameters that change over time), or what its lookups find.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum AuxNeed {
|
||||
#[default]
|
||||
All,
|
||||
Lookups(Vec<AuxLookup>),
|
||||
}
|
||||
|
||||
impl AuxNeed {
|
||||
/// What both need.
|
||||
pub fn union(self, other: AuxNeed) -> AuxNeed {
|
||||
match (self, other) {
|
||||
(AuxNeed::Lookups(mut a), AuxNeed::Lookups(b)) => {
|
||||
for l in b {
|
||||
if !a.contains(&l) {
|
||||
a.push(l);
|
||||
}
|
||||
}
|
||||
AuxNeed::Lookups(a)
|
||||
}
|
||||
_ => AuxNeed::All,
|
||||
}
|
||||
}
|
||||
|
||||
/// The channel names of `names` (a source's channels; planes not needed) these lookups
|
||||
/// find, in channel order, once each. `None`: all of them.
|
||||
pub fn resolve(&self, names: &AuxChannels) -> Option<Vec<String>> {
|
||||
let AuxNeed::Lookups(lookups) = self else { return None };
|
||||
let mut picked = vec![false; names.channels.len()];
|
||||
let mut mark = |i: Option<usize>| {
|
||||
if let Some(slot) = i.and_then(|i| picked.get_mut(i)) {
|
||||
*slot = true;
|
||||
}
|
||||
};
|
||||
for l in lookups {
|
||||
match l {
|
||||
AuxLookup::Name(n) => mark(names.get_index(n.trim())),
|
||||
AuxLookup::Find(list) => {
|
||||
let list: Vec<&str> = list.iter().map(String::as_str).collect();
|
||||
mark(names.find_index(&list));
|
||||
}
|
||||
AuxLookup::Crypto { name, index } => {
|
||||
if let Some(layer) = crypto_layer_named(names, name, *index) {
|
||||
for (id, cov) in names.crypto_rank_indices(&layer) {
|
||||
mark(Some(id));
|
||||
mark(Some(cov));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(names.channels.iter().zip(picked).filter(|(_, p)| *p).map(|((n, _), _)| n.clone()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl AuxChannels {
|
||||
pub fn new(width: u32, height: u32, scale: f64) -> AuxChannels {
|
||||
AuxChannels { width, height, scale, channels: vec![], manifests: vec![] }
|
||||
AuxChannels { width, height, scale, channels: vec![], manifests: vec![], aliases: vec![], listed: vec![] }
|
||||
}
|
||||
|
||||
/// A channel by exact name (case-insensitive).
|
||||
/// The position in [`AuxChannels::channels`] of a channel by exact name (case-insensitive),
|
||||
/// or by the name a multi-part file stores it under ([`AuxChannels::aliases`]).
|
||||
pub fn get_index(&self, name: &str) -> Option<usize> {
|
||||
if let Some(i) = self.channels.iter().position(|(n, _)| n.eq_ignore_ascii_case(name)) {
|
||||
return Some(i);
|
||||
}
|
||||
let full = self.aliases.iter().find(|(stored, _)| stored.eq_ignore_ascii_case(name)).map(|(_, full)| full)?;
|
||||
self.channels.iter().position(|(n, _)| n == full)
|
||||
}
|
||||
|
||||
/// A channel by exact name (case-insensitive), or by the name a multi-part file stores it
|
||||
/// under (`Crypto_Material00.R` for `Crypto_Material.Crypto_Material00.R`).
|
||||
pub fn get(&self, name: &str) -> Option<&[f32]> {
|
||||
self.channels.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_slice())
|
||||
self.get_index(name).and_then(|i| self.channels.get(i)).map(|(_, v)| v.as_slice())
|
||||
}
|
||||
|
||||
/// The first channel present among `names` (also matching `*.name` suffixes, so `Z` finds
|
||||
/// `depth.Z`).
|
||||
pub fn find(&self, names: &[&str]) -> Option<&[f32]> {
|
||||
/// The position of the first channel present among `names` ([`AuxChannels::find`]).
|
||||
pub fn find_index(&self, names: &[&str]) -> Option<usize> {
|
||||
for n in names {
|
||||
if let Some(v) = self.get(n) {
|
||||
return Some(v);
|
||||
if let Some(i) = self.get_index(n) {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
for n in names {
|
||||
let suffix = format!(".{}", n.to_ascii_lowercase());
|
||||
if let Some((_, v)) = self.channels.iter().find(|(c, _)| c.to_ascii_lowercase().ends_with(&suffix)) {
|
||||
return Some(v);
|
||||
if let Some(i) = self.channels.iter().position(|(c, _)| c.to_ascii_lowercase().ends_with(&suffix)) {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The first channel present among `names` (also matching `*.name` suffixes, so `Z` finds
|
||||
/// `depth.Z`).
|
||||
pub fn find(&self, names: &[&str]) -> Option<&[f32]> {
|
||||
self.find_index(names).and_then(|i| self.channels.get(i)).map(|(_, v)| v.as_slice())
|
||||
}
|
||||
|
||||
pub fn names(&self) -> Vec<&str> {
|
||||
self.channels.iter().map(|(n, _)| n.as_str()).collect()
|
||||
}
|
||||
|
||||
/// Every channel name of the source: [`AuxChannels::listed`] when `channels` holds only
|
||||
/// some, else the channels'.
|
||||
fn all_names(&self) -> Vec<&str> {
|
||||
if self.listed.is_empty() { self.names() } else { self.listed.iter().map(String::as_str).collect() }
|
||||
}
|
||||
|
||||
/// The layers of the channels (OpenEXR layers: the name before the last `.`; `""` for the
|
||||
/// unnamed layer), in channel order.
|
||||
pub fn layers(&self) -> Vec<&str> {
|
||||
@@ -97,34 +215,38 @@ pub fn split_channel(name: &str) -> (&str, &str) {
|
||||
name.rsplit_once('.').unwrap_or(("", name))
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer a channel name's first rank `R` belongs to (`CryptoObject00.R` →
|
||||
/// `CryptoObject`), any case of the component letter.
|
||||
fn crypto_layer_of(name: &str) -> Option<&str> {
|
||||
name.strip_suffix("00.R").or_else(|| name.strip_suffix("00.r"))
|
||||
}
|
||||
|
||||
impl AuxChannels {
|
||||
pub fn depth(&self) -> Option<&[f32]> {
|
||||
self.find(&["Z", "depth", "Depth", "zDepth", "Z-Depth"])
|
||||
self.find(&DEPTH_NAMES)
|
||||
}
|
||||
pub fn object_id(&self) -> Option<&[f32]> {
|
||||
self.find(&["ObjectID", "object_id", "objectId", "id"])
|
||||
self.find(&OBJECT_ID_NAMES)
|
||||
}
|
||||
pub fn material_id(&self) -> Option<&[f32]> {
|
||||
self.find(&["MaterialID", "material_id", "materialId"])
|
||||
self.find(&MATERIAL_ID_NAMES)
|
||||
}
|
||||
pub fn coverage(&self) -> Option<&[f32]> {
|
||||
self.find(&["Coverage", "coverage"])
|
||||
self.find(&COVERAGE_NAMES)
|
||||
}
|
||||
|
||||
/// Cryptomatte layer names present (prefixes with a `00.R` channel).
|
||||
/// Cryptomatte layer names present (prefixes with a `00.R` channel), of every channel of the
|
||||
/// source ([`AuxChannels::listed`]).
|
||||
pub fn crypto_layers(&self) -> Vec<String> {
|
||||
let mut v: Vec<String> =
|
||||
self.channels.iter().filter_map(|(n, _)| n.strip_suffix("00.R").or_else(|| n.strip_suffix("00.r")).map(str::to_string)).collect();
|
||||
let mut v: Vec<String> = self.all_names().into_iter().filter_map(crypto_layer_of).map(str::to_string).collect();
|
||||
v.sort();
|
||||
v.dedup();
|
||||
v
|
||||
}
|
||||
|
||||
/// The rank planes of Cryptomatte layer `layer`, in rank order: (ID plane, coverage plane)
|
||||
/// for `layer00.R/G`, `layer00.B/A`, `layer01.R/G`, ... Resolve them once per evaluation and
|
||||
/// index them per pixel; looking the channels up by name for every pixel is what made the
|
||||
/// effect slow.
|
||||
pub fn crypto_rank_planes(&self, layer: &str) -> Vec<(&[f32], &[f32])> {
|
||||
/// The positions in [`AuxChannels::channels`] of the rank planes of Cryptomatte layer
|
||||
/// `layer`, paired as [`AuxChannels::crypto_rank_planes`] pairs them.
|
||||
pub fn crypto_rank_indices(&self, layer: &str) -> Vec<(usize, usize)> {
|
||||
let mut out = vec![];
|
||||
for k in 0.. {
|
||||
let (r, g, b, a) = (self.rank(layer, k, 'R'), self.rank(layer, k, 'G'), self.rank(layer, k, 'B'), self.rank(layer, k, 'A'));
|
||||
@@ -137,17 +259,32 @@ impl AuxChannels {
|
||||
out
|
||||
}
|
||||
|
||||
/// The plane `{layer}{k:02}.{comp}`: the layer name matches case-sensitively (layers
|
||||
/// `Crypto_A` and `crypto_a` are different layers), the component letter either case.
|
||||
fn rank(&self, layer: &str, k: usize, comp: char) -> Option<&[f32]> {
|
||||
/// The rank planes of Cryptomatte layer `layer`, in rank order: (ID plane, coverage plane)
|
||||
/// for `layer00.R/G`, `layer00.B/A`, `layer01.R/G`, ... Resolve them once per evaluation and
|
||||
/// index them per pixel; looking the channels up by name for every pixel is what made the
|
||||
/// effect slow.
|
||||
pub fn crypto_rank_planes(&self, layer: &str) -> Vec<(&[f32], &[f32])> {
|
||||
let plane = |i: usize| self.channels.get(i).map(|(_, v)| v.as_slice());
|
||||
self.crypto_rank_indices(layer).into_iter().filter_map(|(id, cov)| Some((plane(id)?, plane(cov)?))).collect()
|
||||
}
|
||||
|
||||
/// The position of plane `{layer}{k:02}.{comp}`: the layer name matches case-sensitively
|
||||
/// (layers `Crypto_A` and `crypto_a` are different layers), the component letter either
|
||||
/// case; also by the name a multi-part file stores it under ([`AuxChannels::aliases`]).
|
||||
fn rank(&self, layer: &str, k: usize, comp: char) -> Option<usize> {
|
||||
let tail = format!("{k:02}.{comp}");
|
||||
self.channels.iter().find(|(n, _)| n.strip_prefix(layer).is_some_and(|rest| rest.eq_ignore_ascii_case(&tail))).map(|(_, v)| v.as_slice())
|
||||
let is_rank = |n: &str| n.strip_prefix(layer).is_some_and(|rest| rest.eq_ignore_ascii_case(&tail));
|
||||
if let Some(i) = self.channels.iter().position(|(n, _)| is_rank(n)) {
|
||||
return Some(i);
|
||||
}
|
||||
let full = self.aliases.iter().find(|(stored, _)| is_rank(stored)).map(|(_, full)| full)?;
|
||||
self.channels.iter().position(|(n, _)| n == full)
|
||||
}
|
||||
|
||||
/// Rank pairs (id, coverage) of Cryptomatte layer `layer` at aux pixel index `i`. For a
|
||||
/// loop over pixels, use [`AuxChannels::crypto_rank_planes`] once instead.
|
||||
pub fn crypto_ranks(&self, layer: &str, i: usize) -> Vec<(f32, f32)> {
|
||||
self.crypto_rank_planes(layer).iter().map(|(id, c)| (id[i], c[i])).collect()
|
||||
self.crypto_rank_planes(layer).iter().filter_map(|(id, c)| Some((*id.get(i)?, *c.get(i)?))).collect()
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer an effect asking for `want` (`CryptoObject`, …) reads: an exact
|
||||
@@ -161,6 +298,8 @@ impl AuxChannels {
|
||||
/// exactly `name` when it is not empty and the footage has it (`Crypto_Material`, a name an
|
||||
/// imported After Effects project carries), else the layer for `want`.
|
||||
/// The name as given is tried first (EXR channel names may carry spaces), then trimmed.
|
||||
/// A multi-part file's layer is also found by the name its part stores it under
|
||||
/// (`Crypto_Material` for `Crypto_Material.Crypto_Material`, [`AuxChannels::aliases`]).
|
||||
pub fn resolve_crypto_layer_named(&self, name: &str, want: &str) -> Option<String> {
|
||||
let layers = self.crypto_layers();
|
||||
for name in [name, name.trim()] {
|
||||
@@ -168,9 +307,29 @@ impl AuxChannels {
|
||||
return Some(name.to_string());
|
||||
}
|
||||
}
|
||||
for name in [name, name.trim()] {
|
||||
if name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let full = self.aliases.iter().filter(|(stored, _)| crypto_layer_of(stored) == Some(name)).find_map(|(_, full)| crypto_layer_of(full));
|
||||
if let Some(full) = full.filter(|f| layers.iter().any(|l| l == f)) {
|
||||
return Some(full.to_string());
|
||||
}
|
||||
}
|
||||
self.resolve_crypto_layer(want)
|
||||
}
|
||||
|
||||
/// The manifest of Cryptomatte layer `layer`: the one named as the layer, else the one named
|
||||
/// as a multi-part file's part stores the layer (the manifest `Crypto_Material` for the layer
|
||||
/// `Crypto_Material.Crypto_Material`).
|
||||
pub fn manifest(&self, layer: &str) -> Option<&[(String, u32)]> {
|
||||
if let Some((_, m)) = self.manifests.iter().find(|(l, _)| l == layer) {
|
||||
return Some(m.as_slice());
|
||||
}
|
||||
let stored = self.aliases.iter().filter(|(_, full)| crypto_layer_of(full) == Some(layer)).find_map(|(stored, _)| crypto_layer_of(stored))?;
|
||||
self.manifests.iter().find(|(l, _)| l == stored).map(|(_, m)| m.as_slice())
|
||||
}
|
||||
|
||||
/// The ID (its hash, `f32::to_bits`) with the most coverage at aux pixel `i` of Cryptomatte
|
||||
/// layer `layer`, summed over the ranks it appears in. Ties go to the lower rank; IDs with
|
||||
/// no positive coverage don't count. `None` when nothing covers the pixel.
|
||||
@@ -202,10 +361,72 @@ impl AuxChannels {
|
||||
|
||||
/// Manifest entry name for a hash in Cryptomatte layer `layer`.
|
||||
pub fn manifest_name(&self, layer: &str, hash: u32) -> Option<&str> {
|
||||
self.manifests.iter().find(|(l, _)| l == layer)?.1.iter().find(|(_, h)| *h == hash).map(|(n, _)| n.as_str())
|
||||
self.manifest(layer)?.iter().find(|(_, h)| *h == hash).map(|(n, _)| n.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// The Cryptomatte layers every renderer names, and the Layer popup's options when the footage
|
||||
/// has no Cryptomatte metadata. The stored Layer value is an index: 0-2 are these (an exact
|
||||
/// match or a suffix such as `ViewLayer.CryptoObject`), 3 and up are the file's other Cryptomatte
|
||||
/// layers in name order, so a project saved before the popup followed the footage loads unchanged.
|
||||
pub const CRYPTO_LAYERS: [&str; 3] = ["CryptoObject", "CryptoMaterial", "CryptoAsset"];
|
||||
|
||||
/// The Layer popup's entries for Cryptomatte `layers` (sorted, as [`AuxChannels::crypto_layers`]
|
||||
/// returns them): the stored value and the layer's name, in value order. Of several layers
|
||||
/// that fit one standard slot (`A.CryptoObject`, `B.CryptoObject`) the first one keeps the standard
|
||||
/// slot, so existing projects load unchanged, and the others get the next custom slots.
|
||||
fn crypto_layer_slots(layers: &[String]) -> Vec<(usize, &str)> {
|
||||
let mut out: Vec<(usize, &str)> = vec![];
|
||||
let mut custom = CRYPTO_LAYERS.len();
|
||||
for l in layers {
|
||||
match CRYPTO_LAYERS.iter().position(|w| l == w || l.ends_with(w)) {
|
||||
Some(i) if !out.iter().any(|(j, _)| *j == i) => out.push((i, l)),
|
||||
_ => {
|
||||
out.push((custom, l));
|
||||
custom += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.sort_by_key(|(i, _)| *i);
|
||||
out
|
||||
}
|
||||
|
||||
/// What the Layer popup lists for the footage `aux`: the Cryptomatte layers the file has, as
|
||||
/// (stored value, name), or the three standard ones when there is none (no footage, not an
|
||||
/// OpenEXR, no Cryptomatte channels).
|
||||
pub fn crypto_layer_choices(aux: Option<&AuxChannels>) -> Vec<(usize, String)> {
|
||||
crypto_layer_menu(aux, 0).0
|
||||
}
|
||||
|
||||
/// The Layer popup for the stored value `index`: [`crypto_layer_choices`], and the name of the
|
||||
/// entry the effect reads (the first when `index` names none, as the effect does).
|
||||
pub fn crypto_layer_menu(aux: Option<&AuxChannels>, index: usize) -> (Vec<(usize, String)>, Option<String>) {
|
||||
let layers = aux.map(AuxChannels::crypto_layers).unwrap_or_default();
|
||||
if layers.is_empty() {
|
||||
let all: Vec<(usize, String)> = CRYPTO_LAYERS.iter().enumerate().map(|(i, n)| (i, n.to_string())).collect();
|
||||
let shown = all.iter().find(|c| c.0 == index).or(all.first()).map(|c| c.1.clone());
|
||||
return (all, shown);
|
||||
}
|
||||
let slots = crypto_layer_slots(&layers);
|
||||
let shown = slots.iter().find(|(i, _)| *i == index).map(|(_, n)| n.to_string()).or_else(|| layers.first().cloned());
|
||||
(slots.into_iter().map(|(i, n)| (i, n.to_string())).collect(), shown)
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer the effect reads: the one called `name` when the footage has it (Layer
|
||||
/// Name, the layer an imported After Effects project names), else [`crypto_layer_name`]'s.
|
||||
pub fn crypto_layer_named(aux: &AuxChannels, name: &str, index: usize) -> Option<String> {
|
||||
aux.resolve_crypto_layer_named(name, &crypto_layer_name(aux, index)?)
|
||||
}
|
||||
|
||||
/// The Cryptomatte layer of `aux` that the stored Layer value `index` reads: its slot's layer, else
|
||||
/// the first layer (what a file without that layer has always shown). `None` without any.
|
||||
pub fn crypto_layer_name(aux: &AuxChannels, index: usize) -> Option<String> {
|
||||
if aux.crypto_layers().is_empty() {
|
||||
return None;
|
||||
}
|
||||
crypto_layer_menu(Some(aux), index).1
|
||||
}
|
||||
|
||||
/// MurmurHash3 x86 32-bit (public-domain algorithm by Austin Appleby).
|
||||
pub fn murmur3_32(data: &[u8], seed: u32) -> u32 {
|
||||
const C1: u32 = 0xcc9e_2d51;
|
||||
@@ -368,4 +589,56 @@ mod tests {
|
||||
assert_eq!(a.crypto_id_at("Crypto_A", 0), Some(crypto_hash("upper")));
|
||||
assert!(a.crypto_rank_planes("CRYPTO_A").is_empty());
|
||||
}
|
||||
|
||||
/// A names-only set of channels (planes of one sample) named `names`.
|
||||
fn named(names: &[&str]) -> AuxChannels {
|
||||
let mut a = AuxChannels::new(1, 1, 1.0);
|
||||
a.channels = names.iter().map(|n| (n.to_string(), vec![0.0])).collect();
|
||||
a
|
||||
}
|
||||
|
||||
/// #3271: a multi-part file stores `Crypto_Material00.R` in part `Crypto_Material`; the plane
|
||||
/// is shown as `Crypto_Material.Crypto_Material00.R` (as today) and found by its stored name,
|
||||
/// its layer by the part's name, and the manifest named after the part. Bare stored names
|
||||
/// (`R` of part `Image`) don't stand for the part's channel.
|
||||
#[test]
|
||||
fn multipart_channels_are_found_by_their_stored_names() {
|
||||
let mut a = named(&["Crypto_Material.Crypto_Material00.R", "Crypto_Material.Crypto_Material00.G", "Image.R"]);
|
||||
for c in ["R", "G"] {
|
||||
a.aliases.push((format!("Crypto_Material00.{c}"), format!("Crypto_Material.Crypto_Material00.{c}")));
|
||||
}
|
||||
a.manifests = vec![("Crypto_Material".into(), vec![("bunny".into(), 7)])];
|
||||
assert_eq!(a.get_index("Crypto_Material00.R"), Some(0));
|
||||
assert_eq!(a.get_index("crypto_material00.g"), Some(1), "any case, as full names");
|
||||
assert!(a.get("R").is_none(), "a bare name is not an alias");
|
||||
assert_eq!(a.crypto_layers(), ["Crypto_Material.Crypto_Material"], "the shown name is unchanged");
|
||||
let layer = a.resolve_crypto_layer_named("Crypto_Material", "CryptoObject");
|
||||
assert_eq!(layer.as_deref(), Some("Crypto_Material.Crypto_Material"));
|
||||
assert_eq!(a.crypto_rank_indices("Crypto_Material.Crypto_Material"), [(0, 1)]);
|
||||
assert_eq!(a.crypto_rank_indices("Crypto_Material"), [(0, 1)], "by the stored layer name too");
|
||||
assert_eq!(a.manifest_name("Crypto_Material.Crypto_Material", 7), Some("bunny"));
|
||||
assert_eq!(crypto_layer_named(&a, "Crypto_Material", 0).as_deref(), Some("Crypto_Material.Crypto_Material"));
|
||||
}
|
||||
|
||||
/// #3271: declared lookups resolve against the channel names as the effects resolve them on
|
||||
/// the planes; a set holding only some channels keeps listing every Cryptomatte layer.
|
||||
#[test]
|
||||
fn needs_resolve_as_the_effects_look_up() {
|
||||
let all = named(&["A.CryptoAsset00.R", "A.CryptoAsset00.G", "B.CryptoObject00.R", "B.CryptoObject00.G", "Image.R", "depth.Z", "zz.Custom00.R", "zz.Custom00.G"]);
|
||||
let need = AuxNeed::Lookups(vec![
|
||||
AuxLookup::Name(" image.r ".into()),
|
||||
AuxLookup::find(&DEPTH_NAMES),
|
||||
AuxLookup::Crypto { name: String::new(), index: 3 },
|
||||
AuxLookup::Name("missing".into()),
|
||||
]);
|
||||
assert_eq!(need.resolve(&all).unwrap(), ["Image.R", "depth.Z", "zz.Custom00.R", "zz.Custom00.G"]);
|
||||
assert_eq!(AuxNeed::All.resolve(&all), None);
|
||||
assert_eq!(AuxNeed::All.union(need.clone()), AuxNeed::All);
|
||||
assert_eq!(need.clone().union(need.clone()), need, "once each");
|
||||
// The planes read, with every name listed: the custom slot still names the same layer.
|
||||
let mut some = named(&["zz.Custom00.R", "zz.Custom00.G"]);
|
||||
some.listed = all.names().iter().map(|n| n.to_string()).collect();
|
||||
assert_eq!(some.crypto_layers(), all.crypto_layers());
|
||||
assert_eq!(crypto_layer_name(&some, 3), crypto_layer_name(&all, 3));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user