| 1 | //! `LayerMuxer`, `LayerRegex`, `LayerRemover` and the lossless passthroughs `Dummy`, |
| 2 | //! `Fuse.FLS_CopyMetadata`, `Fuse.SetMetaDataTC` (textures carry no metadata, so RGBA, DoD and aux pass through). |
| 3 | |
| 4 | use super::pattern::is_match; |
| 5 | use super::*; |
| 6 | use crate::cache::AuxChannels; |
| 7 | use std::sync::Arc; |
| 8 | |
| 9 | /// Aux channel names as matched by `LayerRegex` / `LayerRemover` patterns, with their `Kill*` input. |
| 10 | const AUX_LAYERS: [(&str, &str); 4] = [("Z", "KillZ"), ("Normal", "KillNormal"), ("Vector", "KillVector"), ("BackVector", "KillBackVector")]; |
| 11 | |
| 12 | fn aux_slots(aux: &mut AuxChannels) -> [&mut Option<crate::cache::AuxChannelTex>; 4] { |
| 13 | [&mut aux.z, &mut aux.normal, &mut aux.vector, &mut aux.back_vector] |
| 14 | } |
| 15 | |
| 16 | /// `tex` with `aux` swapped in under a content hash that reflects the change. |
| 17 | fn with_aux(tex: &CachedTexture, aux: AuxChannels, salt: &impl Hash) -> CachedTexture { |
| 18 | let mut out = tex.clone(); |
| 19 | out.set_aux(Some(Arc::new(aux))); |
| 20 | let mut h = DefaultHasher::new(); |
| 21 | tex.content_hash.hash(&mut h); |
| 22 | salt.hash(&mut h); |
| 23 | out.content_hash = h.finish(); |
| 24 | out |
| 25 | } |
| 26 | |
| 27 | fn mux_ports() -> impl Iterator<Item = String> { |
| 28 | ["Background", "Input", "Image"] |
| 29 | .into_iter() |
| 30 | .map(str::to_owned) |
| 31 | .chain((1..=16).flat_map(|n| [format!("Image{n}"), format!("Input{n}"), format!("Foreground{n}")])) |
| 32 | .chain(std::iter::once("Foreground".to_owned())) |
| 33 | } |
| 34 | |
| 35 | /// The connected `Image2` when `Conflicts` hands it the RGBA. |
| 36 | fn conflict_winner(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<NodeId> { |
| 37 | (comp.eval_text(node, "Conflicts", time, "", expr) == "Image2") |
| 38 | .then(|| node.connected_node("Image2").and_then(|s| comp.find_node(s))) |
| 39 | .flatten() |
| 40 | } |
| 41 | |
| 42 | impl Engine { |
| 43 | /// `Dummy`, `Fuse.FLS_CopyMetadata`, `Fuse.SetMetaDataTC`: the first connected input unchanged (a transparent |
| 44 | /// canvas when none is). `MetadataInput` and the timecode fields are not evaluated. |
| 45 | pub(in crate::eval) fn eval_passthrough( |
| 46 | &mut self, |
| 47 | comp: &Comp, |
| 48 | node: &Node, |
| 49 | time: f64, |
| 50 | enc: &mut wgpu::CommandEncoder, |
| 51 | visiting: &mut HashSet<(NodeId, i64)>, |
| 52 | stats: &mut EvalStats, |
| 53 | ) -> CachedTexture { |
| 54 | match conn_any(comp, node, &["Input", "Image", "Background", "Source"]) { |
| 55 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), |
| 56 | None => self.dummy.clone(), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// `LayerMuxer` — RGBA from the first connected input (`Background`, `Input1..`, `Foreground1..`), or from |
| 61 | /// `Image2` when `Conflicts` is `Image2`; each aux channel the main input lacks comes from the first later input of identical DoD that has it. |
| 62 | pub(in crate::eval) fn eval_layer_muxer( |
| 63 | &mut self, |
| 64 | comp: &Comp, |
| 65 | node: &Node, |
| 66 | time: f64, |
| 67 | enc: &mut wgpu::CommandEncoder, |
| 68 | visiting: &mut HashSet<(NodeId, i64)>, |
| 69 | stats: &mut EvalStats, |
| 70 | ) -> CachedTexture { |
| 71 | let mut inputs = mux_ports() |
| 72 | .filter_map(|p| node.connected_node(&p).and_then(|s| comp.find_node(s))) |
| 73 | .collect::<Vec<_>>(); |
| 74 | inputs.dedup(); |
| 75 | if let Some(winner) = conflict_winner(comp, node, time, &self.expr) { |
| 76 | inputs.retain(|&id| id != winner); |
| 77 | inputs.insert(0, winner); |
| 78 | } |
| 79 | let mut layers = inputs.into_iter().map(|id| self.eval_node(comp, id, time, enc, visiting, stats)); |
| 80 | let Some(main) = layers.next() else { |
| 81 | return self.dummy.clone(); |
| 82 | }; |
| 83 | let mut aux = main.aux_cloned(); |
| 84 | let mut donors = Vec::new(); |
| 85 | for layer in layers.filter(|l| l.size == main.size && l.origin == main.origin) { |
| 86 | let Some(other) = layer.aux.as_deref() else { continue }; |
| 87 | let mut other = other.clone(); |
| 88 | let mut taken = false; |
| 89 | for (slot, donor) in aux_slots(&mut aux).into_iter().zip(aux_slots(&mut other)) { |
| 90 | if slot.is_none() && donor.is_some() { |
| 91 | *slot = donor.take(); |
| 92 | taken = true; |
| 93 | } |
| 94 | } |
| 95 | if taken { |
| 96 | donors.push(layer.content_hash); |
| 97 | } |
| 98 | } |
| 99 | if donors.is_empty() { |
| 100 | return main; |
| 101 | } |
| 102 | with_aux(&main, aux, &("LayerMuxer", donors)) |
| 103 | } |
| 104 | |
| 105 | /// `LayerRegex` keeps the aux channels whose name matches `Pattern` (all when empty; `Invert` flips the |
| 106 | /// selection) and takes RGBA from `Image2` when `Conflicts` is `Image2`; `LayerRemover` drops those matching `Layers` plus any `Kill*` toggle. RGBA is never touched. |
| 107 | #[allow(clippy::too_many_arguments)] |
| 108 | pub(in crate::eval) fn eval_layer_filter( |
| 109 | &mut self, |
| 110 | comp: &Comp, |
| 111 | node: &Node, |
| 112 | time: f64, |
| 113 | remove: bool, |
| 114 | enc: &mut wgpu::CommandEncoder, |
| 115 | visiting: &mut HashSet<(NodeId, i64)>, |
| 116 | stats: &mut EvalStats, |
| 117 | ) -> CachedTexture { |
| 118 | let winner = if remove { None } else { conflict_winner(comp, node, time, &self.expr) }; |
| 119 | let Some(src_id) = winner.or_else(|| conn_any(comp, node, &["Input", "Image", "Image1", "Background"])) else { |
| 120 | return self.dummy.clone(); |
| 121 | }; |
| 122 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 123 | let Some(src_aux) = src.aux.as_deref() else { |
| 124 | return src; |
| 125 | }; |
| 126 | let pattern = if remove { |
| 127 | comp.eval_text(node, "Layers", time, "", &self.expr) |
| 128 | } else { |
| 129 | comp.eval_text(node, "Pattern", time, ".*", &self.expr) |
| 130 | }; |
| 131 | let invert = !remove && comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5; |
| 132 | let mut aux = src_aux.clone(); |
| 133 | let mut dropped = Vec::new(); |
| 134 | for ((name, kill), slot) in AUX_LAYERS.into_iter().zip(aux_slots(&mut aux)) { |
| 135 | let matched = !pattern.is_empty() && is_match(&pattern, name); |
| 136 | let killed = remove && (matched || comp.eval_number(node, kill, time, 0.0, &self.expr) > 0.5); |
| 137 | let keep = if remove { !killed } else { (pattern.is_empty() || matched) != invert }; |
| 138 | if !keep && slot.take().is_some() { |
| 139 | dropped.push(name); |
| 140 | } |
| 141 | } |
| 142 | if dropped.is_empty() { |
| 143 | return src; |
| 144 | } |
| 145 | with_aux(&src, aux, &(&node.kind, dropped)) |
| 146 | } |
| 147 | } |