Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

Merge branch 'main' of github.com:maubrickastuto/Concat

maubrickastuto331eccc5ac75
diff --cc src/crates/concat-effects/src/shader.rs
index 596045c4,cdf4d105..e37c01a9
--- a/src/crates/concat-effects/src/shader.rs
+++ b/src/crates/concat-effects/src/shader.rs
@@@ -401,12 -391,97 +402,13 @@@ impl Shader
/// Every declared parameter must be a field of the struct; a field the
/// manifest does not declare is allowed and stays zero.
pub fn compile(manifest: &Manifest, body: &str) -> Result<Shader, String> {
- let declares_params = body
- .split("struct")
- .skip(1)
- .any(|rest| rest.trim_start().starts_with("Params"));
- if !body.contains("fn effect") {
- return Err(
- "the shader declares no `fn effect(uv: vec2<f32>) -> vec4<f32>`".to_owned(),
- );
- }
- let mut source = String::with_capacity(PRELUDE.len() + body.len() + POSTLUDE.len() + 64);
- if !declares_params {
- // A package with no knobs still has to bind something.
- source.push_str("struct Params { _unused: f32 }\n");
- }
- source.push_str(body);
- source.push('\n');
- source.push_str(PRELUDE);
- source.push_str(POSTLUDE);
-
- let module =
- naga::front::wgsl::parse_str(&source).map_err(|error| error.emit_to_string(&source))?;
- let mut validator = naga::valid::Validator::new(
- naga::valid::ValidationFlags::all(),
- naga::valid::Capabilities::all(),
- );
- validator
- .validate(&module)
- .map_err(|error| error.emit_to_string(&source))?;
- if !module
- .functions
- .iter()
- .any(|(_, function)| function.name.as_deref() == Some("effect"))
- {
- return Err(
- "the shader declares no `fn effect(uv: vec2<f32>) -> vec4<f32>`".to_owned(),
- );
- }
- budget(&module)?;
-
- let (members, span) = module
- .types
- .iter()
- .find_map(|(_, ty)| match (&ty.name, &ty.inner) {
- (Some(name), naga::TypeInner::Struct { members, span }) if name == "Params" => {
- Some((members.clone(), *span as usize))
- }
- _ => None,
- })
- .ok_or_else(|| "the shader declares no `struct Params`".to_owned())?;
-
- let mut slots = Vec::new();
- for param in &manifest.params {
- let member = members
- .iter()
- .find(|member| member.name.as_deref() == Some(param.key.as_str()))
- .ok_or_else(|| format!("`Params` has no field `{}`", param.key))?;
- let inner = &module.types[member.ty].inner;
- let wanted = match param.kind {
- ParamType::Point => 2,
- ParamType::Color => 4,
- _ => 1,
- };
- let width = match inner {
- naga::TypeInner::Scalar(scalar) if is_f32(scalar) => 1,
- naga::TypeInner::Vector { size, scalar } if is_f32(scalar) => *size as usize,
- _ => 0,
- };
- if width != wanted {
- return Err(format!(
- "`Params.{}` must be {}",
- param.key,
- match wanted {
- 2 => "a vec2<f32>",
- 4 => "a vec4<f32>",
- _ => "an f32",
- }
- ));
- }
- slots.push(Slot {
- key: param.key.clone(),
- offset: member.offset as usize,
- kind: param.kind,
- });
- }
-
+ let (source, slots, span) = stitch(manifest, body, Entry::Effect, PRELUDE, POSTLUDE)?;
Ok(Shader {
+ package: manifest.effect.id.clone(),
key: format!("{}@{}", manifest.effect.id, manifest.effect.version),
- source: Arc::from(source),
+ source,
slots,
- span: span.max(ShaderPass::MIN_PARAMS).div_ceil(16) * 16,
+ span,
})
}
@@@ -419,375 -494,159 +421,471 @@@
/// struct. Every declared parameter is present in `values` by the time
/// the catalogue calls this; anything missing reads as zero.
pub fn params_bytes(&self, values: &BTreeMap<String, f64>, params: &[Param]) -> Vec<u8> {
- let mut bytes = vec![0u8; self.span];
- let mut put = |offset: usize, value: f64| {
- let at = offset..offset + 4;
- if at.end <= bytes.len() {
- bytes[at].copy_from_slice(&(value as f32).to_le_bytes());
- }
- };
- for slot in &self.slots {
- match slot.kind {
- ParamType::Point => {
- put(
- slot.offset,
- values
- .get(&format!("{}.x", slot.key))
- .copied()
- .unwrap_or(0.5),
- );
- put(
- slot.offset + 4,
- values
- .get(&format!("{}.y", slot.key))
- .copied()
- .unwrap_or(0.5),
- );
- }
- ParamType::Color => {
- // Packed RGBA in one number, as the document stores it.
- let packed = values.get(&slot.key).copied().unwrap_or(0.0).max(0.0) as u32;
- for (index, shift) in [24u32, 16, 8, 0].into_iter().enumerate() {
- put(
- slot.offset + index * 4,
- f64::from((packed >> shift) & 0xff) / 255.0,
- );
- }
- }
- _ => {
- let fallback = params
- .iter()
- .find(|param| param.key == slot.key)
- .map(|param| param.default)
- .unwrap_or(0.0);
- put(
- slot.offset,
- values.get(&slot.key).copied().unwrap_or(fallback),
- );
- }
- }
- }
- bytes
+ lay_params(&self.slots, self.span, values, params)
}
- /// A pass over a layer with these values.
+ /// A pass over a layer with these values: the uniform buffer written
+ /// from them, and the values themselves for a renderer that reads by
+ /// name.
pub fn pass(
&self,
values: &BTreeMap<String, f64>,
params: &[Param],
intensity: f32,
lut: Option<Arc<Lut>>,
+ reveal_map: Option<Arc<RevealMap>>,
) -> ShaderPass {
ShaderPass {
+ package: self.package.clone(),
key: self.key.clone(),
source: Arc::clone(&self.source),
params: self.params_bytes(values, params),
+ values: values.clone(),
intensity,
lut,
+ reveal_map,
}
}
}
-/// The bindings the prelude declares, and the only ones a package may
-/// use: the layer, the frame block and the parameters, the table.
-const BINDINGS: [(u32, u32); 6] = [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)];
++/// The bindings a prelude declares, and the only ones a package may use.
++/// The union of both contracts: an effect's layer at group 0 (0,0)-(0,1)
++/// and a transition's two pictures at group 0 (0,0)-(0,3); the frame block
++/// and parameters at group 1; the look-up table at group 2; an effect's
++/// reveal map at group 3 - a transition has no group 3, so nothing there
++/// ever validates against a transition's module.
++const BINDINGS: [(u32, u32); 10] = [
++ (0, 0),
++ (0, 1),
++ (0, 2),
++ (0, 3),
++ (1, 0),
++ (1, 1),
++ (2, 0),
++ (2, 1),
++ (3, 0),
++ (3, 1),
++];
+
+ /// What a package may not do, however well it parses: bind anything the
+ /// host did not declare, or loop without an end. A community shader runs
+ /// on the person's GPU with the host's rights, and a loop with no bound
+ /// hangs the device for every process on the machine; a binding the host
+ /// does not know is one it cannot serve. Caught at load, where a broken
+ /// package is a load error and not a black frame.
+ fn budget(module: &naga::Module) -> Result<(), String> {
+ for (_, global) in module.global_variables.iter() {
+ if let Some(binding) = &global.binding
+ && !BINDINGS.contains(&(binding.group, binding.binding))
+ {
+ return Err(format!(
+ "the shader binds @group({}) @binding({}), which the host does not provide",
+ binding.group, binding.binding
+ ));
+ }
+ }
+ for (_, function) in module.functions.iter() {
+ bounded(&function.body)?;
+ }
+ for entry in &module.entry_points {
+ bounded(&entry.function.body)?;
+ }
+ Ok(())
+ }
+
+ /// Every loop in `block` has a way out: a `break` in its body or a
+ /// `break if` in its continuing block.
+ fn bounded(block: &naga::Block) -> Result<(), String> {
+ for statement in block.iter() {
+ match statement {
+ naga::Statement::Loop {
+ body,
+ continuing,
+ break_if,
+ } => {
+ if break_if.is_none() && !breaks(body) {
+ return Err(
+ "the shader has a loop with no break: it would never end".to_owned()
+ );
+ }
+ bounded(body)?;
+ bounded(continuing)?;
+ }
+ naga::Statement::Block(inner) => bounded(inner)?,
+ naga::Statement::If { accept, reject, .. } => {
+ bounded(accept)?;
+ bounded(reject)?;
+ }
+ naga::Statement::Switch { cases, .. } => {
+ for case in cases {
+ bounded(&case.body)?;
+ }
+ }
+ _ => {}
+ }
+ }
+ Ok(())
+ }
+
+ /// Whether `block` breaks out of the loop it is the body of, at any
+ /// depth short of a nested loop, whose breaks are its own.
+ fn breaks(block: &naga::Block) -> bool {
+ block.iter().any(|statement| match statement {
+ naga::Statement::Break => true,
+ naga::Statement::Block(inner) => breaks(inner),
+ naga::Statement::If { accept, reject, .. } => breaks(accept) || breaks(reject),
+ naga::Statement::Switch { cases, .. } => cases.iter().any(|case| breaks(&case.body)),
+ _ => false,
+ })
+ }
+
fn is_f32(scalar: &naga::Scalar) -> bool {
scalar.kind == naga::ScalarKind::Float && scalar.width == 4
}
+/// Which entry a stitched module declares: the two shaders share everything
+/// but their entry function's name and signature.
+#[derive(Clone, Copy)]
+enum Entry {
+ Effect,
+ Transition,
+}
+
+impl Entry {
+ /// The function name the body must declare.
+ fn name(self) -> &'static str {
+ match self {
+ Entry::Effect => "effect",
+ Entry::Transition => "transition",
+ }
+ }
+
+ /// The signature named in the "no such function" error.
+ fn signature(self) -> &'static str {
+ match self {
+ Entry::Effect => "fn effect(uv: vec2<f32>) -> vec4<f32>",
+ Entry::Transition => "fn transition(uv: vec2<f32>, progress: f32) -> vec4<f32>",
+ }
+ }
+}
+
+/// Stitches a package `body` into the host's contract, parses and validates
+/// the result with naga, and reads its `Params` struct for where each declared
+/// parameter lands. Shared by [`Shader`] and [`TransitionShader`], which differ
+/// only in their prelude/postlude and entry function. Returns the finished
+/// module, the parameter slots in declaration order, and the padded uniform
+/// span.
+fn stitch(
+ manifest: &Manifest,
+ body: &str,
+ entry: Entry,
+ prelude: &str,
+ postlude: &str,
+) -> Result<(Arc<str>, Vec<Slot>, usize), String> {
+ let declares_params = body
+ .split("struct")
+ .skip(1)
+ .any(|rest| rest.trim_start().starts_with("Params"));
+ if !body.contains(&format!("fn {}", entry.name())) {
+ return Err(format!("the shader declares no `{}`", entry.signature()));
+ }
+ let mut source = String::with_capacity(prelude.len() + body.len() + postlude.len() + 64);
+ if !declares_params {
+ // A package with no knobs still has to bind something.
+ source.push_str("struct Params { _unused: f32 }\n");
+ }
+ source.push_str(body);
+ source.push('\n');
+ source.push_str(prelude);
+ source.push_str(postlude);
+
+ let module =
+ naga::front::wgsl::parse_str(&source).map_err(|error| error.emit_to_string(&source))?;
+ let mut validator = naga::valid::Validator::new(
+ naga::valid::ValidationFlags::all(),
+ naga::valid::Capabilities::all(),
+ );
+ validator
+ .validate(&module)
+ .map_err(|error| error.emit_to_string(&source))?;
+ if !module
+ .functions
+ .iter()
+ .any(|(_, function)| function.name.as_deref() == Some(entry.name()))
+ {
+ return Err(format!("the shader declares no `{}`", entry.signature()));
+ }
++ budget(&module)?;
+
+ let (members, span) = module
+ .types
+ .iter()
+ .find_map(|(_, ty)| match (&ty.name, &ty.inner) {
+ (Some(name), naga::TypeInner::Struct { members, span }) if name == "Params" => {
+ Some((members.clone(), *span as usize))
+ }
+ _ => None,
+ })
+ .ok_or_else(|| "the shader declares no `struct Params`".to_owned())?;
+
+ let mut slots = Vec::new();
+ for param in &manifest.params {
+ let member = members
+ .iter()
+ .find(|member| member.name.as_deref() == Some(param.key.as_str()))
+ .ok_or_else(|| format!("`Params` has no field `{}`", param.key))?;
+ let inner = &module.types[member.ty].inner;
+ let wanted = match param.kind {
+ ParamType::Point => 2,
+ ParamType::Color => 4,
+ _ => 1,
+ };
+ let width = match inner {
+ naga::TypeInner::Scalar(scalar) if is_f32(scalar) => 1,
+ naga::TypeInner::Vector { size, scalar } if is_f32(scalar) => *size as usize,
+ _ => 0,
+ };
+ if width != wanted {
+ return Err(format!(
+ "`Params.{}` must be {}",
+ param.key,
+ match wanted {
+ 2 => "a vec2<f32>",
+ 4 => "a vec4<f32>",
+ _ => "an f32",
+ }
+ ));
+ }
+ slots.push(Slot {
+ key: param.key.clone(),
+ offset: member.offset as usize,
+ kind: param.kind,
+ });
+ }
+
+ let span = span.max(ShaderPass::MIN_PARAMS).div_ceil(16) * 16;
+ Ok((Arc::from(source), slots, span))
+}
+
+/// The `Params` uniform for `values`, laid out to `slots` over a buffer of
+/// `span` bytes. Shared by both shaders, which store parameters identically.
+fn lay_params(
+ slots: &[Slot],
+ span: usize,
+ values: &BTreeMap<String, f64>,
+ params: &[Param],
+) -> Vec<u8> {
+ let mut bytes = vec![0u8; span];
+ let mut put = |offset: usize, value: f64| {
+ let at = offset..offset + 4;
+ if at.end <= bytes.len() {
+ bytes[at].copy_from_slice(&(value as f32).to_le_bytes());
+ }
+ };
+ for slot in slots {
+ match slot.kind {
+ ParamType::Point => {
+ put(
+ slot.offset,
+ values
+ .get(&format!("{}.x", slot.key))
+ .copied()
+ .unwrap_or(0.5),
+ );
+ put(
+ slot.offset + 4,
+ values
+ .get(&format!("{}.y", slot.key))
+ .copied()
+ .unwrap_or(0.5),
+ );
+ }
+ ParamType::Color => {
+ // Packed RGBA in one number, as the document stores it.
+ let packed = values.get(&slot.key).copied().unwrap_or(0.0).max(0.0) as u32;
+ for (index, shift) in [24u32, 16, 8, 0].into_iter().enumerate() {
+ put(
+ slot.offset + index * 4,
+ f64::from((packed >> shift) & 0xff) / 255.0,
+ );
+ }
+ }
+ _ => {
+ let fallback = params
+ .iter()
+ .find(|param| param.key == slot.key)
+ .map(|param| param.default)
+ .unwrap_or(0.0);
+ put(
+ slot.offset,
+ values.get(&slot.key).copied().unwrap_or(fallback),
+ );
+ }
+ }
+ }
+ bytes
+}
+
+/// The marker that opens the grading library inside [`PRELUDE`]. A transition
+/// reuses that same library by slicing it out here rather than copying it.
+const GRADING_MARKER: &str = "// ── the grading library ──";
+
+/// The shared grading library: the second half of [`PRELUDE`], from the
+/// grading marker to the end. It only reads `sample`, `texel`, `luma`, `hash`,
+/// `frame.size` and `frame.time`, all of which the transition head supplies,
+/// so it works unchanged over two inputs.
+fn grading() -> &'static str {
+ let at = PRELUDE
+ .find(GRADING_MARKER)
+ .expect("the prelude carries a grading library");
+ &PRELUDE[at..]
+}
+
+/// The transition head: the host's half of a two-input transition. It binds
+/// the outgoing picture and the incoming one at group 0, repurposes the
+/// frame's spare slot as `progress`, and re-declares the same basics the
+/// effect head does so the shared grading library (appended after this) works.
+/// `sample` reads the outgoing picture, so a helper like `soften` needs no
+/// wiring.
+const TRANSITION_HEAD: &str = r#"// ── the host's half of a transition; see concat-effects/src/shader.rs ──
+struct Frame {
+ /// The layer's size in pixels.
+ size: vec2<f32>,
+ /// Seconds into the timeline.
+ time: f32,
+ /// How far through the cut, 0 the outgoing picture, 1 the incoming one.
+ progress: f32,
+}
+
+@group(0) @binding(0) var from_texture: texture_2d<f32>;
+@group(0) @binding(1) var from_sampler: sampler;
+@group(0) @binding(2) var to_texture: texture_2d<f32>;
+@group(0) @binding(3) var to_sampler: sampler;
+@group(1) @binding(0) var<uniform> frame: Frame;
+@group(1) @binding(1) var<uniform> params: Params;
+@group(2) @binding(0) var lut_texture: texture_3d<f32>;
+@group(2) @binding(1) var lut_sampler: sampler;
+
+/// The outgoing picture's colour at `uv`, straight alpha.
+fn from_at(uv: vec2<f32>) -> vec4<f32> {
+ return textureSample(from_texture, from_sampler, uv);
+}
+
+/// The incoming picture's colour at `uv`, straight alpha.
+fn to_at(uv: vec2<f32>) -> vec4<f32> {
+ return textureSample(to_texture, to_sampler, uv);
+}
+
+/// What the shared grading helpers read: the outgoing picture, so `soften`
+/// and its like work on the from-side with no wiring by the author.
+fn sample(uv: vec2<f32>) -> vec4<f32> {
+ return from_at(uv);
+}
+
+/// One pixel, as a fraction of the layer.
+fn texel() -> vec2<f32> {
+ return vec2<f32>(1.0, 1.0) / frame.size;
+}
+
+/// Luminance, Rec. 709.
+fn luma(rgb: vec3<f32>) -> f32 {
+ return dot(rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
+}
+
+/// The package's look-up table applied to a colour - the identity when the
+/// package ships none. Sampled at the texel centres.
+fn lut(rgb: vec3<f32>) -> vec3<f32> {
+ let n = f32(textureDimensions(lut_texture).x);
+ let uvw = clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * (n - 1.0) / n + vec3<f32>(0.5 / n);
+ return textureSampleLevel(lut_texture, lut_sampler, uvw, 0.0).rgb;
+}
+
+/// A hash in 0..1 from a point and a seed, for grain and dither.
+fn hash(p: vec2<f32>, seed: f32) -> f32 {
+ let q = vec3<f32>(p, seed);
+ return fract(sin(dot(q, vec3<f32>(12.9898, 78.233, 37.719))) * 43758.5453);
+}
+
+"#;
+
+/// The transition's draw stages: a full-screen triangle, and a fragment that
+/// hands the whole result to the package's `transition` - it owns the blend,
+/// so the pipeline does no mixing of its own.
+const TRANSITION_POSTLUDE: &str = r#"
+struct VsOut {
+ @builtin(position) position: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+}
+
+@vertex
+fn vs_main(@builtin(vertex_index) index: u32) -> VsOut {
+ let x = f32(i32(index & 1u) * 4 - 1);
+ let y = f32(i32(index >> 1u) * 4 - 1);
+ var out: VsOut;
+ out.position = vec4<f32>(x, y, 0.0, 1.0);
+ out.uv = vec2<f32>((x + 1.0) * 0.5, 1.0 - (y + 1.0) * 0.5);
+ return out;
+}
+
+@fragment
+fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
+ return transition(in.uv, frame.progress);
+}
+"#;
+
+/// A transition package's shader, stitched, checked and laid out.
+#[derive(Clone, Debug)]
+pub struct TransitionShader {
+ key: String,
+ source: Arc<str>,
+ slots: Vec<Slot>,
+ span: usize,
+}
+
+impl TransitionShader {
+ /// Stitches `body` into the transition contract - two bound pictures and a
+ /// progress - checks it, and reads the `Params` struct for its parameters'
+ /// layout. Every declared parameter must be a field of the struct.
+ pub fn compile(manifest: &Manifest, body: &str) -> Result<TransitionShader, String> {
+ let prelude = format!("{TRANSITION_HEAD}{}", grading());
+ let (source, slots, span) =
+ stitch(manifest, body, Entry::Transition, &prelude, TRANSITION_POSTLUDE)?;
+ Ok(TransitionShader {
+ key: format!("{}@{}", manifest.effect.id, manifest.effect.version),
+ source,
+ slots,
+ span,
+ })
+ }
+
+ /// The stitched module.
+ pub fn source(&self) -> &str {
+ &self.source
+ }
+
+ /// The `Params` buffer for these resolved values, laid out to the struct.
+ pub fn params_bytes(&self, values: &BTreeMap<String, f64>, params: &[Param]) -> Vec<u8> {
+ lay_params(&self.slots, self.span, values, params)
+ }
+
+ /// A combine of two layers at `progress` with these values.
+ pub fn pass(
+ &self,
+ values: &BTreeMap<String, f64>,
+ params: &[Param],
+ progress: f32,
+ lut: Option<Arc<Lut>>,
+ ) -> TransitionPass {
+ TransitionPass {
+ key: self.key.clone(),
+ source: Arc::clone(&self.source),
+ params: self.params_bytes(values, params),
+ progress,
+ lut,
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@@ -859,6 -718,43 +957,43 @@@ fn effect(uv: vec2<f32>) -> vec4<f32>
assert_eq!(shader.params_bytes(&BTreeMap::new(), &[]).len(), 16);
}
+ /// A community shader may not bind what the host did not declare,
+ /// and may not loop without an end: both are refused at load, before
+ /// the package can reach a device. A loop with a way out is fine.
+ #[test]
+ fn a_hostile_shader_is_refused_at_load() {
+ let manifest = manifest("");
+ let unbounded = Shader::compile(
+ &manifest,
+ "fn effect(uv: vec2<f32>) -> vec4<f32> { var c = sample(uv); loop { c.r = c.r * 0.5; } return c; }",
+ );
+ assert!(
+ unbounded.unwrap_err().contains("no break"),
+ "an endless loop"
+ );
+ let nested = Shader::compile(
+ &manifest,
+ "fn effect(uv: vec2<f32>) -> vec4<f32> { var c = sample(uv); for (var i = 0; i < 4; i++) { loop { c.r = c.r * 0.5; } } return c; }",
+ );
+ assert!(
+ nested.unwrap_err().contains("no break"),
+ "an endless loop inside a bounded one"
+ );
+ let extra = Shader::compile(
+ &manifest,
- "@group(3) @binding(0) var other: texture_2d<f32>;\nfn effect(uv: vec2<f32>) -> vec4<f32> { return sample(uv) + textureSample(other, source_sampler, uv); }",
++ "@group(4) @binding(0) var other: texture_2d<f32>;\nfn effect(uv: vec2<f32>) -> vec4<f32> { return sample(uv) + textureSample(other, source_sampler, uv); }",
+ );
+ assert!(
- extra.unwrap_err().contains("@group(3)"),
++ extra.unwrap_err().contains("@group(4)"),
+ "a binding the host does not provide"
+ );
+ let bounded = Shader::compile(
+ &manifest,
+ "fn effect(uv: vec2<f32>) -> vec4<f32> { var c = sample(uv); var i = 0; loop { i++; if (i > 3) { break; } c.r = c.r * 0.5; } for (var j = 0; j < 2; j++) { c.g = c.g * 0.5; } return c; }",
+ );
+ assert!(bounded.is_ok(), "{:?}", bounded.err());
+ }
+
#[test]
fn a_missing_field_or_a_broken_body_is_refused() {
let manifest = manifest(
diff --cc src/crates/concat-export/src/lib.rs
index 9ba2bda7,248b1fcd..bb6edcac
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@@ -24,13 -24,10 +24,11 @@@ pub mod chains
pub mod flatten;
mod resolve;
- use resolve::{
- BuiltTimeline, RidingChain, TransitionSpan, Treatment, animation_of, build_timeline, quantise,
- };
-use resolve::{BuiltTimeline, Treatment, animation_of, build_timeline, quantise};
++use resolve::{BuiltTimeline, TransitionSpan, Treatment, animation_of, build_timeline, quantise};
-use std::collections::HashMap;
+use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
+use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use concat_core::SpeedCurve;
@@@ -1016,13 -960,11 +1017,13 @@@ fn render_picture
filter_chains,
tracks,
treatments,
+ transitions,
pre_chains,
- passes,
- riding,
+ chains,
++ reveal_maps,
cutouts,
highlight: _,
- } = build_timeline(request, rate, visible, gpu);
+ } = build_timeline(request, rate, visible, gpu, transitions);
let mut encoder = Encoder::create(
destination,
@@@ -1078,15 -1022,13 +1081,13 @@@
let frame = cutouts
.get(&layer.clip)
.and_then(|job| job.cut(&frame, layer.source_time))
- .unwrap_or_else(|| frame.as_ref().clone());
- sources.push(Source {
+ .map_or(frame, std::sync::Arc::new);
+ layers.push(filled(
+ layer,
frame,
- opacity: layer.opacity,
- transform: layer.transform,
- track: tracks.get(&layer.clip).copied().unwrap_or(0),
- blend: layer.blend,
- passes: passes_at(&passes, &riding, layer.clip, frame_seconds),
- });
+ tracks.get(&layer.clip).copied().unwrap_or(0),
- passes_at(&chains, &timeline, layer.clip, time),
++ passes_at(&chains, &reveal_maps, &timeline, layer.clip, time),
+ ));
}
continue;
}
@@@ -1152,45 -1094,25 +1153,26 @@@
Some(cut) => cut,
None => frame,
};
- sources.push(Source {
- frame,
- opacity: layer.opacity,
- transform: layer.transform,
- track: tracks.get(&layer.clip).copied().unwrap_or(0),
- blend: layer.blend,
- passes: passes_at(&passes, &riding, layer.clip, frame_seconds),
- });
+ layers.push(filled(
+ layer,
+ std::sync::Arc::new(frame),
+ tracks.get(&layer.clip).copied().unwrap_or(0),
- passes_at(&chains, &timeline, layer.clip, time),
++ passes_at(&chains, &reveal_maps, &timeline, layer.clip, time),
+ ));
}
}
- let seconds = time.as_f64() as f32;
- let layers: Vec<(Layer<'_>, usize)> = sources
- .iter()
- .map(|source| {
- (
- place_layer(
- &source.frame,
- source.opacity,
- &source.transform,
- request.width,
- request.height,
- )
- .with_blend(source.blend)
- .with_passes(&source.passes)
- .at_time(seconds),
- source.track,
- )
- })
- .collect();
-
let composed = composite_treated(
&mut *compositor,
- request.width,
- request.height,
- time,
- &layers,
+ FramePlan {
+ time,
+ width: request.width,
+ height: request.height,
+ layers,
+ treatments: Vec::new(),
+ },
&treatments,
+ &transitions,
);
encoder
.write_frame(&composed)
@@@ -1209,23 -1131,22 +1191,25 @@@
encoder.finish().map_err(|error| error.to_string())
}
- /// The shader passes for one clip at one frame: built for the instant when
- /// the chain rides, and the ones built once otherwise.
+ /// The shader passes for one clip at one instant: every keyed knob at its
+ /// value there, the rest at their settings, laid out as the uniforms the
-/// shaders read.
++/// shaders read. `reveal_maps` carries a title's baked per-word order
++/// alongside its chain - unlike an effect's knobs, it is fixed for the
++/// clip's whole life, so it is looked up rather than resolved.
fn passes_at(
- passes: &HashMap<ClipId, Vec<ShaderPass>>,
- riding: &HashMap<ClipId, RidingChain>,
+ chains: &HashMap<ClipId, Vec<AppliedFilter>>,
++ reveal_maps: &HashMap<ClipId, Arc<RevealMap>>,
+ timeline: &Timeline,
clip: ClipId,
- seconds: f64,
+ time: Rational,
) -> Vec<ShaderPass> {
- if let Some(chain) = riding.get(&clip) {
- let at = if chain.duration > 0.0 {
- ((seconds - chain.start) / chain.duration).clamp(0.0, 1.0)
- } else {
- 0.0
- };
- return Catalogue::builtin().shader_passes_at(&chain.effects, at, chain.reveal_map.clone());
- }
- passes.get(&clip).cloned().unwrap_or_default()
+ let Some(effects) = chains.get(&clip) else {
+ return Vec::new();
+ };
+ let at = timeline
+ .clip(clip)
+ .map_or(0.0, |engine_clip| engine_clip.fraction_at(time));
- Catalogue::builtin().shader_passes_at(effects, at)
++ Catalogue::builtin().shader_passes_at(effects, at, reveal_maps.get(&clip).cloned())
}
/// `a` towards `b` by `amount`, per channel.
@@@ -1239,33 -1160,18 +1223,32 @@@ fn mix(a: &Frame, b: &Frame, amount: f3
out
}
- /// Composites `sources` - each placed layer with the track it came from,
- /// bottom-most first - with every treatment live at `time` applied to the
- /// stack beneath its track. Without a live treatment this is one composite;
- /// with one, the stack is drawn up to the treatment's track, run through its
- /// chain, blended back by its strength, and used as the ground for the rest.
+ /// Draws `plan` with every treatment live at its instant applied to the
+ /// stack beneath its track. A treatment that is shaders alone goes into
+ /// the plan, and either compositor applies it where it draws; one that
+ /// needs FFmpeg for a package with no shader cannot, so the stack is
+ /// drawn up to its track, run through its chain here, blended back by
+ /// its strength, and used as the ground for the rest.
fn composite_treated(
compositor: &mut dyn Compositor,
- width: u32,
- height: u32,
- time: Rational,
- sources: &[(Layer<'_>, usize)],
+ mut plan: FramePlan,
treatments: &[Treatment],
+ transitions: &[TransitionSpan],
) -> Frame {
+ let time = plan.time;
+ // A packaged transition live at this instant combines the outgoing stack
+ // with the incoming picture through its shader. A live adjustment layer
+ // over the same frame is the one case we leave to the dissolve fallback,
+ // so the two never fight over the ground - a rare pairing, and the cut
+ // still transitions, just without its shader.
+ let treated_now = treatments.iter().any(|treatment| treatment.covers(time));
+ if !treated_now
+ && let Some(span) = transitions.iter().find(|span| span.covers(time))
- && let Some(frame) = combine_transition(compositor, width, height, time, sources, span)
++ && let Some(frame) = combine_transition(compositor, &plan, span)
+ {
+ return frame;
+ }
+
let mut live: Vec<&Treatment> = treatments
.iter()
.filter(|treatment| treatment.covers(time))
@@@ -1336,63 -1244,11 +1321,71 @@@
};
ground = Some(treated);
}
- let mut layers: Vec<Layer<'_>> = ground.as_ref().map(Layer::new).into_iter().collect();
- layers.extend(sources[next..].iter().map(|(layer, _)| *layer));
- compositor.composite(width, height, &layers)
+ let mut stack: Vec<PlannedLayer> = ground.map(ground_layer).into_iter().collect();
+ stack.extend(layers.into_iter().skip(next));
+ compositor.render(&stage(stack))
}
+/// The frame when a packaged transition is live: the outgoing stack
+/// (everything below the incoming lane) combined with the incoming picture
+/// (drawn over that stack at full opacity) through the transition's shader.
+/// `None` when the compositor cannot run the shader or the package is unknown,
+/// and the caller then shows the dissolve the incoming clip already carries.
+fn combine_transition(
+ compositor: &mut dyn Compositor,
- width: u32,
- height: u32,
- time: Rational,
- sources: &[(Layer<'_>, usize)],
++ plan: &FramePlan,
+ span: &TransitionSpan,
+) -> Option<Frame> {
+ let pass =
- Catalogue::builtin().transition_pass(&span.id, &span.params, span.progress(time))?;
++ Catalogue::builtin().transition_pass(&span.id, &span.params, span.progress(plan.time))?;
++ let (width, height, time) = (plan.width, plan.height, plan.time);
++ let stage = |layers: Vec<PlannedLayer>| FramePlan {
++ time,
++ width,
++ height,
++ layers,
++ treatments: Vec::new(),
++ };
+ // The outgoing picture: the stack strictly below the incoming lane.
- let from_layers: Vec<Layer<'_>> = sources
++ let from_layers: Vec<PlannedLayer> = plan
++ .layers
+ .iter()
- .filter(|(_, track)| *track < span.to_track)
- .map(|(layer, _)| *layer)
++ .filter(|layer| layer.track < span.to_track)
++ .cloned()
+ .collect();
+ // The incoming picture over that stack, its own layer forced to full
+ // opacity so the shader - not the dissolve ramp - owns the blend.
- let to_layers: Vec<Layer<'_>> = sources
++ let to_layers: Vec<PlannedLayer> = plan
++ .layers
+ .iter()
- .filter(|(_, track)| *track <= span.to_track)
- .map(|(layer, track)| {
- if *track == span.to_track {
- (*layer).with_opacity(1.0)
- } else {
- *layer
++ .filter(|layer| layer.track <= span.to_track)
++ .cloned()
++ .map(|mut layer| {
++ if layer.track == span.to_track {
++ layer.opacity = 1.0;
+ }
++ layer
+ })
+ .collect();
- let from = compositor.composite(width, height, &from_layers);
- let to = compositor.composite(width, height, &to_layers);
++ let from = compositor.render(&stage(from_layers));
++ let to = compositor.render(&stage(to_layers));
+ let combined = compositor.combine(width, height, time.as_f64() as f32, &from, &to, &pass)?;
+ // Anything above the incoming lane draws over the combined picture.
- let above: Vec<Layer<'_>> = sources
++ let above: Vec<PlannedLayer> = plan
++ .layers
+ .iter()
- .filter(|(_, track)| *track > span.to_track)
- .map(|(layer, _)| *layer)
++ .filter(|layer| layer.track > span.to_track)
++ .cloned()
+ .collect();
+ if above.is_empty() {
+ Some(combined)
+ } else {
- let mut layers = vec![Layer::new(&combined)];
++ let mut layers = vec![ground_layer(combined)];
+ layers.extend(above);
- Some(compositor.composite(width, height, &layers))
++ Some(compositor.render(&stage(layers)))
+ }
+}
+
/// One paused-monitor frame: the same clip list the exporter takes, one
/// timestamp, a preview resolution.
#[derive(Deserialize)]
@@@ -1431,136 -1287,41 +1424,53 @@@ pub fn preview_frame
Ok(sources.composite(&mut CpuCompositor).into_pixels())
}
- /// The decoded pictures under the playhead and where each goes: what a
- /// compositor needs to draw the paused monitor's frame, decoded but not yet
- /// drawn, so a caller with a GPU can draw them where they are shown.
+ /// The paused monitor's frame, described but not yet drawn: the plan
+ /// with every picture decoded and placed, so a caller with a GPU can draw
+ /// it where it is shown.
pub struct PreviewSources {
- sources: Vec<Source<std::sync::Arc<Frame>>>,
- width: u32,
- height: u32,
- time: Rational,
+ plan: FramePlan,
treatments: Vec<Treatment>,
+ transitions: Vec<TransitionSpan>,
}
impl PreviewSources {
- /// The layers, placed the exporter's way, bottom-most first. Bare: a
- /// caller drawing these itself is skipping the treatments, so check
- /// [`PreviewSources::has_treatments`] first.
- pub fn layers(&self) -> Vec<Layer<'_>> {
- let seconds = self.time.as_f64() as f32;
- self.sources
- .iter()
- .map(|source| {
- place_layer(
- source.frame.as_ref(),
- source.opacity,
- &source.transform,
- self.width,
- self.height,
- )
- .with_blend(source.blend)
- .with_passes(&source.passes)
- .at_time(seconds)
- })
- .collect()
+ /// The frame's description, with every treatment that is shaders
+ /// alone already in it; a compositor draws this and nothing else. A
+ /// treatment that needs FFmpeg is not in it: see
+ /// [`PreviewSources::needs_cpu`].
+ pub fn plan(&self) -> &FramePlan {
+ &self.plan
}
- /// Whether a layer clip or a transition is live at this instant, in which
- /// case the frame cannot be drawn from `layers` alone: see
- /// Whether a live treatment needs FFmpeg for a package with no
- /// shader, in which case the frame can only be drawn whole through
-- /// [`PreviewSources::composite`].
- pub fn has_treatments(&self) -> bool {
++ /// Whether the frame cannot be drawn from [`PreviewSources::plan`]
++ /// alone, and can only be drawn whole through
++ /// [`PreviewSources::composite`]: a live treatment needs FFmpeg for a
++ /// package with no shader, or a live transition is a two-input combine
++ /// a plain `FramePlan` has no way to describe.
+ pub fn needs_cpu(&self) -> bool {
self.treatments
.iter()
- .any(|treatment| treatment.covers(self.time))
+ .any(|treatment| treatment.covers(self.plan.time) && !treatment.chain.is_empty())
+ || self
+ .transitions
+ .iter()
- .any(|span| span.covers(self.time))
- }
-
- /// The layers as [`PreviewSources::layers`] gives them, each with the
- /// track it came from, for a compositor applying treatments itself.
- pub fn placed(&self) -> Vec<(Layer<'_>, usize)> {
- let seconds = self.seconds();
- self.sources
- .iter()
- .map(|source| {
- (
- place_layer(
- source.frame.as_ref(),
- source.opacity,
- &source.transform,
- self.width,
- self.height,
- )
- .with_blend(source.blend)
- .with_passes(&source.passes)
- .at_time(seconds),
- source.track,
- )
- })
- .collect()
++ .any(|span| span.covers(self.plan.time))
}
- /// The treatments live at this instant, in ascending track order, as
- /// a GPU compositor takes them - or None when one of them needs FFmpeg
- /// for a package with no shader, and only [`PreviewSources::composite`]
- /// can draw the frame.
- pub fn live_treatments(&self) -> Option<Vec<GpuTreatment<'_>>> {
- // A live transition is a two-input combine, not a treatment; it can
- // only be drawn through `composite`, so refuse the fast path here.
- if self.transitions.iter().any(|span| span.covers(self.time)) {
- return None;
- }
- let mut live: Vec<&Treatment> = self
- .treatments
- .iter()
- .filter(|treatment| treatment.covers(self.time))
- .collect();
- if live.iter().any(|treatment| !treatment.chain.is_empty()) {
- return None;
- }
- live.sort_by_key(|treatment| treatment.track);
- Some(
- live.iter()
- .map(|treatment| GpuTreatment {
- track: treatment.track,
- passes: &treatment.passes,
- strength: treatment.strength_at(self.time),
- })
- .collect(),
- )
- }
-
- /// The instant, in seconds, for passes that move.
+ /// The instant, in seconds.
pub fn seconds(&self) -> f32 {
- self.time.as_f64() as f32
+ self.plan.seconds()
}
- /// The frame, drawn by `compositor`, every treatment applied.
+ /// The frame, treatments and transitions included, drawn with `compositor`.
pub fn composite(&self, compositor: &mut dyn Compositor) -> Frame {
- let placed = self.placed();
- composite_treated(compositor, self.plan.clone(), &self.treatments)
+ composite_treated(
+ compositor,
- self.width,
- self.height,
- self.time,
- &placed,
++ self.plan.clone(),
+ &self.treatments,
+ &self.transitions,
+ )
}
-
- /// The output width the layers were placed for.
- pub fn width(&self) -> u32 {
- self.width
- }
-
- /// The output height the layers were placed for.
- pub fn height(&self) -> u32 {
- self.height
- }
}
/// Decodes the pictures the paused monitor's frame is made of, through the
@@@ -1588,10 -1373,8 +1522,10 @@@ pub fn preview_sources_of
filter_chains,
tracks,
treatments,
+ transitions,
pre_chains,
- passes,
- riding,
+ chains,
++ reveal_maps,
cutouts,
highlight,
} = &plan.built;
@@@ -1632,14 -1414,12 +1565,12 @@@
Some(drawn) => std::sync::Arc::new(drawn),
None => frame,
};
- sources.push(Source {
+ layers.push(filled(
+ layer,
frame,
- opacity: layer.opacity,
- transform: layer.transform,
- track: tracks.get(&layer.clip).copied().unwrap_or(0),
- blend: layer.blend,
- passes: passes_at(passes, riding, layer.clip, frame_seconds),
- })
+ tracks.get(&layer.clip).copied().unwrap_or(0),
- passes_at(chains, timeline, layer.clip, time),
++ passes_at(chains, reveal_maps, timeline, layer.clip, time),
+ ))
}
Err(error) => failures.push(format!("{}: {error}", layer.media.display())),
}
@@@ -1656,13 -1436,36 +1587,37 @@@
));
}
- Ok(PreviewSources {
- sources,
+ let mut frame_plan = FramePlan {
+ time,
width: plan.width,
height: plan.height,
- time,
+ layers,
+ treatments: Vec::new(),
+ };
+ // Every treatment that is shaders alone goes into the plan now, so a
+ // caller drawing the plan itself has them; see
+ // `PreviewSources::needs_cpu` for the one kind that cannot.
+ let live: Vec<&Treatment> = treatments
+ .iter()
+ .filter(|treatment| treatment.covers(time))
+ .collect();
+ if live.iter().all(|treatment| treatment.chain.is_empty()) {
+ frame_plan.treatments = live
+ .iter()
+ .map(|treatment| PlannedTreatment {
+ track: treatment.track,
+ effects: treatment.passes_at(time),
+ strength: treatment.strength_at(time),
+ })
+ .collect();
+ frame_plan
+ .treatments
+ .sort_by_key(|treatment| treatment.track);
+ }
+ Ok(PreviewSources {
+ plan: frame_plan,
treatments: treatments.clone(),
+ transitions: transitions.clone(),
})
}
@@@ -1823,12 -1673,8 +1825,9 @@@ mod tests
let mut compositor = CpuCompositor;
let out = composite_treated(
&mut compositor,
- 8,
- 8,
- Rational::from_int(1),
- &sources,
+ stack(Rational::from_int(1)),
std::slice::from_ref(&negate),
+ &[],
);
// Red negated is cyan where nothing sits on top...
let at = |x: usize, y: usize| &out.pixels()[(y * 8 + x) * 4..(y * 8 + x) * 4 + 3];
@@@ -1841,29 -1687,13 +1840,23 @@@
strength: 0.5,
..negate.clone()
};
- let out = composite_treated(&mut compositor, stack(Rational::from_int(1)), &[half]);
+ let out = composite_treated(
+ &mut compositor,
- 8,
- 8,
- Rational::from_int(1),
- &sources,
++ stack(Rational::from_int(1)),
+ &[half],
+ &[],
+ );
let pixel = &out.pixels()[(7 * 8 + 7) * 4..(7 * 8 + 7) * 4 + 3];
assert!(pixel[0] > 120 && pixel[0] < 136, "{pixel:?}");
assert!(pixel[1] > 120 && pixel[1] < 136, "{pixel:?}");
// Outside its span the treatment does nothing.
- let out = composite_treated(&mut compositor, stack(Rational::from_int(20)), &[negate]);
+ let out = composite_treated(
+ &mut compositor,
- 8,
- 8,
- Rational::from_int(20),
- &sources,
++ stack(Rational::from_int(20)),
+ &[negate],
+ &[],
+ );
assert_eq!(at_of(&out, 7, 7), [255, 0, 0]);
fn at_of(frame: &Frame, x: usize, y: usize) -> [u8; 3] {
let p = &frame.pixels()[(y * 8 + x) * 4..(y * 8 + x) * 4 + 3];
diff --cc src/crates/concat-export/src/resolve.rs
index fa3df0f9,ada2c296..3a98ca87
--- a/src/crates/concat-export/src/resolve.rs
+++ b/src/crates/concat-export/src/resolve.rs
@@@ -92,12 -92,10 +92,14 @@@ pub(crate) struct BuiltTimeline
pub(crate) tracks: HashMap<ClipId, usize>,
/// The clip's pre-fit chain - its crop - where it has one.
pub(crate) pre_chains: HashMap<ClipId, String>,
- /// The clip's shader passes, on a GPU renderer.
- pub(crate) passes: HashMap<ClipId, Vec<ShaderPass>>,
- /// The clips whose chains ride: a knob with keys is worth something
- /// different each frame, so their passes are built per frame from the
- /// chain and the clip's span rather than read from `passes`.
- pub(crate) riding: HashMap<ClipId, RidingChain>,
+ /// The clip's applied effects, on a GPU renderer: the passes are
+ /// resolved from them at each frame, because a knob with keys is worth
+ /// something different each frame and the resolution is cheap.
+ pub(crate) chains: HashMap<ClipId, Vec<AppliedFilter>>,
++ /// A title's per-word reveal order, for the clips that have one -
++ /// carried beside `chains` rather than inside it, since it is baked
++ /// once by the host and never resolved per frame the way effects are.
++ pub(crate) reveal_maps: HashMap<ClipId, Arc<RevealMap>>,
/// The clip whose cutout is drawn tinted rather than cut, if one is.
pub(crate) highlight: Option<ClipId>,
/// The layers: treatments over the stack, by span.
@@@ -108,47 -104,6 +110,36 @@@
pub(crate) cutouts: HashMap<ClipId, CutoutJob>,
}
+/// A packaged transition over a cut. The incoming clip has been overlapped
+/// onto the outgoing one and moved to `to_track`; over `[start, end)` the
+/// compositor combines the stack below `to_track` (the outgoing picture) with
+/// the incoming layer through the package's two-input shader. A machine with
+/// no GPU shows the dissolve the incoming clip already carries instead.
+#[derive(Clone, Debug)]
+pub(crate) struct TransitionSpan {
+ pub(crate) start: Rational,
+ pub(crate) end: Rational,
+ pub(crate) to_track: usize,
+ pub(crate) id: String,
+ pub(crate) params: BTreeMap<String, f64>,
+}
+
+impl TransitionSpan {
+ pub(crate) fn covers(&self, time: Rational) -> bool {
+ self.start <= time && time < self.end
+ }
+
+ /// How far through the cut `time` is, `0..=1`.
+ pub(crate) fn progress(&self, time: Rational) -> f64 {
+ let span = self.end.as_f64() - self.start.as_f64();
+ if span <= 0.0 {
+ return 1.0;
+ }
+ ((time.as_f64() - self.start.as_f64()) / span).clamp(0.0, 1.0)
+ }
+}
+
- /// A picture chain with keys on it, and where its clip sits, so a frame's
- /// passes can be built at the right point of the ride.
- pub(crate) struct RidingChain {
- pub(crate) effects: Vec<AppliedFilter>,
- pub(crate) start: f64,
- pub(crate) duration: f64,
- /// The clip's reveal map, carried the same way `shader_passes` carries
- /// it for the non-riding case - a title's word order does not change
- /// frame to frame even when one of its effect's knobs does.
- pub(crate) reveal_map: Option<Arc<RevealMap>>,
- }
+
/// A layer clip, as the compositor needs it: when, over which tracks, what
/// chain, and how hard.
#[derive(Clone, Debug)]
@@@ -170,6 -126,17 +162,18 @@@ impl Treatment
self.start <= time && time < self.end
}
- /// The shader passes at `time`, each keyed knob at its value there.
++ /// The shader passes at `time`, each keyed knob at its value there. A
++ /// layer is never a title, so it never carries a reveal map.
+ pub(crate) fn passes_at(&self, time: Rational) -> Vec<ShaderPass> {
+ let span = (self.end - self.start).as_f64();
+ let at = if span > 0.0 {
+ ((time - self.start).as_f64() / span).clamp(0.0, 1.0)
+ } else {
+ 0.0
+ };
- Catalogue::builtin().shader_passes_at(&self.effects, at)
++ Catalogue::builtin().shader_passes_at(&self.effects, at, None)
+ }
+
/// How hard the treatment is applied at `time`: the strength, eased in
/// and out over the ramps at either end.
pub(crate) fn strength_at(&self, time: Rational) -> f32 {
@@@ -201,8 -167,7 +205,8 @@@ pub(crate) fn build_timeline
let mut tracks_of: HashMap<ClipId, usize> = HashMap::new();
let mut treatments: Vec<Treatment> = Vec::new();
let mut pre_chains: HashMap<ClipId, String> = HashMap::new();
- let mut passes: HashMap<ClipId, Vec<ShaderPass>> = HashMap::new();
- let mut riding: HashMap<ClipId, RidingChain> = HashMap::new();
+ let mut chains: HashMap<ClipId, Vec<AppliedFilter>> = HashMap::new();
++ let mut reveal_maps: HashMap<ClipId, Arc<RevealMap>> = HashMap::new();
let mut cutouts: HashMap<ClipId, CutoutJob> = HashMap::new();
let mut highlight: Option<ClipId> = None;
@@@ -283,25 -252,10 +291,13 @@@
pre_chains.insert(id, pre);
}
if gpu {
- let clip_passes = shader_passes(clip);
- if !clip_passes.is_empty() {
- passes.insert(id, clip_passes);
+ let effects = shaded(&clip.effects);
+ if !effects.is_empty() {
+ chains.insert(id, effects);
}
- if clip
- .effects
- .iter()
- .any(|link| link.enabled && !link.keys.is_empty())
- {
- riding.insert(
- id,
- RidingChain {
- effects: clip.effects.clone(),
- start: clip.start,
- duration: clip.duration,
- reveal_map: clip.reveal_map.clone(),
- },
- );
++ if let Some(map) = &clip.reveal_map {
++ reveal_maps.insert(id, Arc::clone(map));
+ }
}
if let Some(job) = CutoutJob::of(clip) {
cutouts.insert(id, job);
@@@ -319,10 -273,8 +315,10 @@@
filter_chains,
tracks: tracks_of,
treatments,
+ transitions,
pre_chains,
- passes,
- riding,
+ chains,
++ reveal_maps,
cutouts,
highlight,
}
diff --cc src/crates/concat-host/src/media.rs
index 6523b3e2,c7feb251..cebfbd1a
--- a/src/crates/concat-host/src/media.rs
+++ b/src/crates/concat-host/src/media.rs
@@@ -829,11 -896,11 +896,11 @@@ mod tests
assert_eq!(fnv1a(b""), 0xcbf2_9ce4_8422_2325);
assert_eq!(
peaks_key("/a.mp4", None),
-- format!("{:016x}-b200.peaks", fnv1a(b"/a.mp4"))
++ format!("{:016x}-b1000.peaks", fnv1a(b"/a.mp4"))
);
assert_eq!(
peaks_key("/a.mp4", Some(2)),
-- format!("{:016x}-s2-b200.peaks", fnv1a(b"/a.mp4"))
++ format!("{:016x}-s2-b1000.peaks", fnv1a(b"/a.mp4"))
);
}
diff --cc src/crates/concat-host/src/proxy.rs
index 00000000,31565142..f295fe8f
mode 000000,100644..100644
--- a/src/crates/concat-host/src/proxy.rs
+++ b/src/crates/concat-host/src/proxy.rs
@@@ -1,0 -1,285 +1,287 @@@
+ // SPDX-License-Identifier: AGPL-3.0-or-later
+ // SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+ //! Proxies: a smaller copy of a large file, written once into the
+ //! project's cache, that playback and the filmstrips read instead of the
+ //! original.
+ //!
+ //! A 4K file decodes at a few frames a second on a laptop without
+ //! hardware decode; its quarter-size copy decodes at dozens. So a file
+ //! larger than HD gets one, written on the scheduler's proxy lane - one
+ //! at a time, on two threads, so the machine stays usable meanwhile - and
+ //! adopted into the reader pool as the file's stand-in. A request that
+ //! asks for the proxy reads it; the paused monitor and the export never
+ //! do, and see the original. The copy is named after the file's path,
+ //! size and modification time, so a file replaced on disk gets a fresh
+ //! one and the old copy is swept with the rest of the cache.
+
+ use std::collections::HashSet;
+ use std::hash::{Hash, Hasher};
+ use std::path::{Path, PathBuf};
+ use std::sync::{Mutex, OnceLock};
+
+ use concat_media::decode::{DecodeOptions, Decoder, FrameSource};
-use concat_media::{EncodeOptions, Encoder, FrameSink, Priority, VideoCodec};
++use concat_media::{EncodeOptions, Encoder, FrameSink, Priority, RateMode, VideoCodec};
+
+ /// A file with more pixels than this gets a proxy: anything larger than
+ /// full HD.
+ const PIXELS_WITHOUT_PROXY: u64 = 1920 * 1080;
+ /// The proxy's long side is the original's divided by this, and never
+ /// shorter than [`SHORTEST_LONG_SIDE`].
+ const DIVISOR: u32 = 4;
+ const SHORTEST_LONG_SIDE: u32 = 960;
+
+ /// Whether a `width` by `height` file is worth a proxy.
+ pub fn wanted(width: u32, height: u32) -> bool {
+ u64::from(width) * u64::from(height) > PIXELS_WITHOUT_PROXY
+ }
+
+ /// The proxy's size for a `width` by `height` original: the long side a
+ /// quarter of the original's, not under [`SHORTEST_LONG_SIDE`] and never
+ /// over the original, the aspect kept, both sides even.
+ pub fn size_for(width: u32, height: u32) -> (u32, u32) {
+ let (width, height) = (width.max(2), height.max(2));
+ let long = width.max(height);
+ let target = (long / DIVISOR).max(SHORTEST_LONG_SIDE).min(long);
+ let scale = f64::from(target) / f64::from(long);
+ let side = |px: u32| (((f64::from(px) * scale).round() as u32) & !1).max(2);
+ (side(width), side(height))
+ }
+
+ /// Where the proxy of `media` lives under `project`: named by the file's
+ /// path, size and modification time. None for a file that cannot be
+ /// stat'ed, which cannot be read either.
+ pub fn path_for(project: &Path, media: &str) -> Option<PathBuf> {
+ let meta = std::fs::metadata(media).ok()?;
+ let mut hasher = std::collections::hash_map::DefaultHasher::new();
+ media.hash(&mut hasher);
+ meta.len().hash(&mut hasher);
+ if let Ok(modified) = meta.modified()
+ && let Ok(since) = modified.duration_since(std::time::UNIX_EPOCH)
+ {
+ since.as_secs().hash(&mut hasher);
+ }
+ Some(
+ project
+ .join("cache")
+ .join("proxy")
+ .join(format!("{:016x}.mp4", hasher.finish())),
+ )
+ }
+
+ /// The proxy of `media` under `project`, when it has been written.
+ pub fn existing(project: &Path, media: &str) -> Option<PathBuf> {
+ path_for(project, media).filter(|path| path.is_file())
+ }
+
+ /// Sees to it that `media` - `width` by `height` - has a proxy when it
+ /// is worth one: an existing copy is adopted into the reader pool at
+ /// once; otherwise one is queued to be written on the proxy lane and
+ /// adopted when done. Asking again while it is being written is free.
+ /// Returns whether the file has, or will have, a proxy.
+ pub fn ensure(project: &Path, media: &str, width: u32, height: u32) -> bool {
+ if !wanted(width, height) {
+ return false;
+ }
+ let Some(target) = path_for(project, media) else {
+ return false;
+ };
+ let pool = crate::scheduler().pool();
+ if target.is_file() {
+ if pool.proxy_of(Path::new(media)).as_deref() != Some(target.as_path()) {
+ pool.adopt_proxy(Path::new(media), target);
+ }
+ return true;
+ }
+ static WRITING: OnceLock<Mutex<HashSet<PathBuf>>> = OnceLock::new();
+ let writing = WRITING.get_or_init(|| Mutex::new(HashSet::new()));
+ {
+ let mut writing = writing
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ if !writing.insert(target.clone()) {
+ return true;
+ }
+ }
+ let source = media.to_owned();
+ let size = size_for(width, height);
+ crate::scheduler().submit(Priority::Proxy, move || {
+ match write(&source, &target, size) {
+ Ok(()) => {
+ crate::scheduler()
+ .pool()
+ .adopt_proxy(Path::new(&source), target.clone());
+ log::info!("{source}: proxy written to {}", target.display());
+ }
+ Err(error) => log::warn!("{source}: no proxy: {error}"),
+ }
+ writing
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .remove(&target);
+ });
+ true
+ }
+
+ /// Writes the proxy of `source` at `size` to `target`: H.264, a fast
+ /// preset, two threads, in software, into a temporary name that is moved
+ /// into place only when whole, so a copy that is there is a copy that is
+ /// complete.
+ pub fn write(source: &str, target: &Path, (width, height): (u32, u32)) -> Result<(), String> {
+ let info = concat_media::probe(source).map_err(|error| error.to_string())?;
+ let rate = info
+ .video
+ .as_ref()
+ .map(|video| video.frame_rate)
+ .ok_or_else(|| "no video stream".to_owned())?;
+ if let Some(folder) = target.parent() {
+ std::fs::create_dir_all(folder)
+ .map_err(|error| format!("{}: {error}", folder.display()))?;
+ }
+ // Written beside the target under a name the muxer still knows the
+ // format of, and moved into place only when whole.
+ let partial = target.with_extension("part.mp4");
+ let options = DecodeOptions::default()
+ .scaled_to(width, height)
+ .in_software()
+ .threaded(2);
+ let mut decoder = Decoder::open(source, &options).map_err(|error| error.to_string())?;
+ let mut encoder = Encoder::create(
+ &partial,
+ width,
+ height,
+ rate,
+ &EncodeOptions {
+ codec: VideoCodec::H264,
+ preset: "veryfast".to_owned(),
+ crf: 23,
++ rate_mode: RateMode::Vbr,
++ bitrate_kbps: 0,
+ ten_bit: false,
+ hardware: false,
+ threads: 2,
+ },
+ )
+ .map_err(|error| error.to_string())?;
+ let result = (|| {
+ while let Some(frame) = decoder.next_frame().map_err(|error| error.to_string())? {
+ encoder
+ .write_frame(&frame)
+ .map_err(|error| error.to_string())?;
+ }
+ encoder.finish().map_err(|error| error.to_string())
+ })();
+ match result {
+ Ok(()) => std::fs::rename(&partial, target)
+ .map_err(|error| format!("{}: {error}", target.display())),
+ Err(error) => {
+ let _ = std::fs::remove_file(&partial);
+ Err(error)
+ }
+ }
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn only_files_larger_than_hd_get_a_proxy() {
+ assert!(!wanted(1920, 1080));
+ assert!(!wanted(1280, 720));
+ assert!(wanted(3840, 2160));
+ assert!(wanted(2560, 1440));
+ assert!(!wanted(0, 0));
+ }
+
+ #[test]
+ fn a_proxy_is_a_quarter_of_the_long_side_but_never_tiny_or_larger() {
+ assert_eq!(size_for(3840, 2160), (960, 540));
+ assert_eq!(size_for(7680, 4320), (1920, 1080));
+ // A tall file keeps its way up.
+ assert_eq!(size_for(2160, 3840), (540, 960));
+ // A file too small for a quarter gets the floor, never upscaled.
+ assert_eq!(size_for(2560, 1440), (960, 540));
+ assert_eq!(size_for(640, 480), (640, 480));
+ // Odd sizes come out even, and nothing is ever under two.
+ let (w, h) = size_for(3841, 2161);
+ assert!(w % 2 == 0 && h % 2 == 0);
+ assert_eq!(size_for(1, 1), (2, 2));
+ }
+
+ /// A proxy written from a file larger than HD is a quarter of it, is
+ /// adopted by the pool, and serves a moving request while the paused
+ /// one still reads the original.
+ #[test]
+ fn a_written_proxy_is_read_for_a_moving_picture_only() {
+ use concat_core::frame::Frame;
+ use concat_core::time::FrameRate;
+ let dir = std::env::temp_dir().join(format!("concat-proxy-write-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).expect("temp dir");
+ let media = dir.join("big.mp4");
+ {
+ let mut encoder = Encoder::create(
+ &media,
+ 2560,
+ 1440,
+ FrameRate::THIRTY,
+ &EncodeOptions {
+ preset: "ultrafast".to_owned(),
+ hardware: false,
+ ..EncodeOptions::default()
+ },
+ )
+ .expect("encodes");
+ for index in 0..6u8 {
+ let mut frame = Frame::black(2560, 1440);
+ frame.fill([index * 40, 20, 200, 255]);
+ encoder.write_frame(&frame).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+ }
+ let project = dir.join("project");
+ let source = media.to_string_lossy().into_owned();
+ assert!(ensure(&project, &source, 2560, 1440), "worth a proxy");
+ crate::scheduler().drain();
+ let proxy = existing(&project, &source).expect("the proxy was written");
+ let info = concat_media::probe(&proxy).expect("probes");
+ let video = info.video.expect("has pictures");
+ assert_eq!((video.width, video.height), (960, 540));
+ let pool = crate::scheduler().pool();
+ assert_eq!(
+ pool.proxy_of(Path::new(&source)).as_deref(),
+ Some(proxy.as_path())
+ );
+ // Second time round: nothing to write, still adopted.
+ assert!(ensure(&project, &source, 2560, 1440));
+ let time = FrameRate::THIRTY.time_of_frame(2);
+ let moving = pool
+ .frame(&concat_media::FrameRequest::new(&source, time, 480, 270).from_proxy(true))
+ .expect("reads the proxy");
+ assert_eq!((moving.width(), moving.height()), (480, 270));
+ let paused = pool
+ .frame(&concat_media::FrameRequest::new(&source, time, 480, 270))
+ .expect("reads the original");
+ assert_eq!((paused.width(), paused.height()), (480, 270));
+ // A file not worth a proxy is left alone.
+ assert!(!ensure(&project, &source, 1280, 720));
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn the_proxy_path_follows_the_file_and_changes_when_the_file_does() {
+ let dir = std::env::temp_dir().join(format!("concat-proxy-path-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).expect("temp dir");
+ let media = dir.join("clip.mp4");
+ std::fs::write(&media, b"one").expect("writes");
+ let project = dir.join("project");
+ let first = path_for(&project, &media.to_string_lossy()).expect("a path");
+ assert!(first.starts_with(project.join("cache").join("proxy")));
+ assert!(existing(&project, &media.to_string_lossy()).is_none());
+ std::fs::write(&media, b"one more byte").expect("writes");
+ let second = path_for(&project, &media.to_string_lossy()).expect("a path");
+ assert_ne!(first, second, "a changed file is another proxy");
+ assert!(path_for(&project, &dir.join("missing.mp4").to_string_lossy()).is_none());
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+ }
diff --cc src/crates/concat-media/src/encode.rs
index 6aff99f3,ab52c7b9..e7256f49
--- a/src/crates/concat-media/src/encode.rs
+++ b/src/crates/concat-media/src/encode.rs
@@@ -570,50 -637,6 +646,53 @@@ pub fn jpeg(frame: &Frame, quality: u8
mod tests {
use super::*;
+ /// Every written frame decodes back, at a frame rate an even number of
+ /// frames does not land on a round second at (25 fps, 6 seconds: every
+ /// packet but the last infers its duration from the one after it, so a
+ /// packet's duration was never set here and the file's own reported
+ /// length came up one frame short - which is exactly the gap a strict
+ /// reader, not just this crate's own decoder, counts by).
+ #[test]
+ fn every_written_frame_decodes_back_even_at_a_rate_with_no_last_neighbour() {
+ let dir = std::env::temp_dir().join(format!("concat-media-encode-test-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).expect("scratch dir");
+ let path = dir.join("150-at-25fps.mp4");
+
+ let options = EncodeOptions {
+ codec: VideoCodec::H264,
+ preset: "ultrafast".to_owned(),
+ crf: 16,
++ rate_mode: RateMode::Vbr,
++ bitrate_kbps: 0,
+ ten_bit: false,
+ hardware: false,
++ threads: 0,
+ };
+ let rate = FrameRate::new(concat_core::time::Rational::new(25, 1));
+ const FRAMES: u32 = 150;
+ {
+ let mut encoder = Encoder::create(&path, 64, 64, rate, &options).expect("encodes");
+ let mut frame = Frame::black(64, 64);
+ for i in 0..FRAMES {
+ frame.fill([(i % 256) as u8, 0, 0, 255]);
+ encoder.write_frame(&frame).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+ }
+
+ use crate::decode::{DecodeOptions, Decoder, FrameSource};
+ let mut decoder =
+ Decoder::open(&path, &DecodeOptions::default()).expect("opens what was just written");
+ let mut count = 0;
+ while decoder.next_frame().expect("decodes").is_some() {
+ count += 1;
+ }
+ assert_eq!(count, FRAMES, "every encoded frame should decode back");
+
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
fn a_frame_becomes_a_jpeg() {
let mut frame = Frame::black(32, 32);
diff --cc src/crates/concat-project/src/animation.rs
index 7c70a720,727309a3..ae645baf
--- a/src/crates/concat-project/src/animation.rs
+++ b/src/crates/concat-project/src/animation.rs
@@@ -687,43 -300,6 +687,44 @@@ mod tests
}
}
+ #[test]
+ fn a_loop_animation_repeats_across_the_clip() {
+ let mut editor = Editor::new();
+ let id = editor
+ .apply(Command::AddTextClip {
++ above: false,
+ track_id: None,
+ start: 0.0,
+ style: None,
+ duration: Some(4.0),
+ offset_y: None,
+ })
+ .unwrap()
+ .created_id
+ .unwrap();
+ editor
+ .apply(Command::SetClipAnimation {
+ clip_id: id.clone(),
+ slot: AnimationSlot::Loop,
+ animation: Some(ClipAnimation {
+ preset: "Pulse".to_owned(),
+ duration: 1.0, // 1 second per cycle -> 4 cycles over 4 seconds
+ }),
+ })
+ .unwrap();
+ let clip = editor.project().active().clip(&id).unwrap();
+ let animation = animation_of(clip).expect("a pulse loop is set");
+ // Peak of pulse at half of each cycle: 0.5s / 4s = 0.125, 1.5s / 4s = 0.375
+ let scale_0 = animation.transform_at(Default::default(), 0.0).scale;
+ let scale_peak1 = animation.transform_at(Default::default(), 0.125).scale;
+ let scale_mid = animation.transform_at(Default::default(), 0.25).scale;
+ let scale_peak2 = animation.transform_at(Default::default(), 0.375).scale;
+ assert_eq!(scale_0, 1.0);
+ assert!((scale_peak1 - 1.15).abs() < 1e-3);
+ assert_eq!(scale_mid, 1.0);
+ assert!((scale_peak2 - 1.15).abs() < 1e-3);
+ }
+
#[test]
fn a_fade_in_lands_over_its_seconds_at_the_head() {
let mut editor = Editor::new();
@@@ -757,41 -334,4 +759,42 @@@
assert!((animation.opacity_at(1.0, 0.9) - 1.0).abs() < 1e-9);
assert_eq!(animation.transform_at(Default::default(), 0.1).scale, 1.0);
}
+
+ /// Elastic In starts a clip at zero scale, overshoots past 1.0, and
+ /// settles - the spring shape a "Bounce In" alone does not have.
+ #[test]
+ fn an_elastic_in_overshoots_before_settling() {
+ let mut editor = Editor::new();
+ let id = editor
+ .apply(Command::AddTextClip {
++ above: false,
+ track_id: None,
+ start: 0.0,
+ style: None,
+ duration: Some(4.0),
+ offset_y: None,
+ })
+ .unwrap()
+ .created_id
+ .unwrap();
+ editor
+ .apply(Command::SetClipAnimation {
+ clip_id: id.clone(),
+ slot: AnimationSlot::In,
+ animation: Some(ClipAnimation {
+ preset: "Elastic In".to_owned(),
+ duration: 1.0,
+ }),
+ })
+ .unwrap();
+ let clip = editor.project().active().clip(&id).unwrap();
+ let animation = animation_of(clip).expect("an elastic-in is set");
+ // The In slot is 1s of a 4s clip: a quarter of the whole clip.
+ assert!(animation.transform_at(Default::default(), 0.0).scale < 0.01);
+ // The overshoot key sits at 40% of the slot, 10% of the clip.
+ assert!(animation.transform_at(Default::default(), 0.1).scale > 1.1);
+ // Settled by the slot's end, and held there for the rest of the clip.
+ assert!((animation.transform_at(Default::default(), 0.25).scale - 1.0).abs() < 1e-9);
+ assert!((animation.transform_at(Default::default(), 0.9).scale - 1.0).abs() < 1e-9);
+ }
}
diff --cc src/crates/concat-render/src/compositor.rs
index 63da2644,a447e043..3ca63a29
--- a/src/crates/concat-render/src/compositor.rs
+++ b/src/crates/concat-render/src/compositor.rs
@@@ -1,192 -1,39 +1,56 @@@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
- //! Blending layers into one frame.
+ //! Blending a plan's layers into one frame.
//!
- //! [`CpuCompositor`] is the reference implementation: obvious, dependency-free,
- //! and slow enough that it will eventually only be used for tests and for
- //! checking the GPU path's output. The `Compositor` trait is the seam that
- //! `wgpu` will slot into.
+ //! [`CpuCompositor`] is the reference implementation: obvious and
+ //! dependency-free, the one the tests read and the one a machine without
+ //! a GPU renders with. The [`Compositor`] trait is the seam the wgpu
+ //! compositor slots into, and a [`FramePlan`] is all either takes.
+ //!
+ //! The picture of a layer is made the way [`crate::plan`] describes: the
+ //! crop, the flips and the fit, then the effects, then the mask, the fades
+ //! and the wipes at the placement. Effects run through the CPU kernels in
+ //! [`crate::kernels`], keyed by the package; a package without one is drawn
+ //! untreated and said so once, which is an honest fallback where a second
+ //! implementation would be a drifting one.
+
+ use std::borrow::Cow;
use concat_core::frame::{BYTES_PER_PIXEL, Frame};
-use concat_core::shader::ShaderPass;
+use concat_core::shader::{ShaderPass, TransitionPass};
use concat_core::timeline::Blend;
- /// A layer's placement beyond its base position, in output pixels.
- ///
- /// Applied about the layer's centre: scale first, then rotation, then the
- /// translation. Pixel-space on purpose - the resolution-independent form
- /// lives in `concat-core::timeline::Transform`, and whoever builds layers
- /// converts exactly once.
- #[derive(Clone, Copy, PartialEq, Debug)]
- pub struct Placement {
- /// Multiplier over the layer's own size.
- pub scale: f32,
- /// Clockwise rotation about the layer's centre, in radians.
- pub rotation: f32,
- /// Horizontal offset of the layer's centre from its base position, pixels.
- pub translate_x: f32,
- /// Vertical offset of the layer's centre from its base position, pixels.
- pub translate_y: f32,
- /// A multiplier on the width beyond `scale`; 1 keeps the aspect.
- pub stretch_x: f32,
- /// The same for the height.
- pub stretch_y: f32,
- }
-
- impl Placement {
- /// Unscaled, unrotated, unmoved.
- pub const IDENTITY: Placement = Placement {
- scale: 1.0,
- rotation: 0.0,
- translate_x: 0.0,
- translate_y: 0.0,
- stretch_x: 1.0,
- stretch_y: 1.0,
- };
-
- /// True when applying this placement would change nothing.
- pub fn is_identity(&self) -> bool {
- *self == Self::IDENTITY
- }
- }
-
- impl Default for Placement {
- fn default() -> Self {
- Self::IDENTITY
- }
- }
-
- /// One source image, positioned and weighted.
- #[derive(Clone, Copy, Debug)]
- pub struct Layer<'a> {
- /// The pixels to draw.
- pub frame: &'a Frame,
- /// Blend strength over what is beneath, in `0.0..=1.0`.
- pub opacity: f32,
- /// Horizontal offset of the layer's top-left corner. May be negative.
- pub x: i32,
- /// Vertical offset of the layer's top-left corner. May be negative.
- pub y: i32,
- /// Scale, rotation and translation about the layer's centre.
- pub placement: Placement,
- /// How the layer's colour meets the ground.
- pub blend: Blend,
- /// Shader passes to run over the layer's pixels before it is placed, in
- /// order. The GPU compositor runs them; the CPU reference cannot, and
- /// draws the layer untreated.
- pub passes: &'a [ShaderPass],
- /// Seconds into the timeline, for passes that move.
- pub time: f32,
- }
-
- impl<'a> Layer<'a> {
- /// A layer drawn at the origin, fully opaque.
- pub fn new(frame: &'a Frame) -> Self {
- Self {
- frame,
- opacity: 1.0,
- x: 0,
- y: 0,
- placement: Placement::IDENTITY,
- blend: Blend::Normal,
- passes: &[],
- time: 0.0,
- }
- }
-
- /// Sets the shader passes to run over the layer first.
- pub fn with_passes(mut self, passes: &'a [ShaderPass]) -> Self {
- self.passes = passes;
- self
- }
-
- /// Sets the timeline instant the passes see.
- pub fn at_time(mut self, time: f32) -> Self {
- self.time = time;
- self
- }
+ use crate::kernels;
+ use crate::plan::{FramePlan, Geometry, PlannedLayer, PlannedTreatment, Shading};
- /// Sets the blend mode.
- pub fn with_blend(mut self, blend: Blend) -> Self {
- self.blend = blend;
- self
- }
-
- /// Sets the blend strength.
- pub fn with_opacity(mut self, opacity: f32) -> Self {
- self.opacity = opacity;
- self
- }
-
- /// Sets the top-left corner.
- pub fn at(mut self, x: i32, y: i32) -> Self {
- self.x = x;
- self.y = y;
- self
- }
-
- /// Sets the scale/rotation/translation.
- pub fn with_placement(mut self, placement: Placement) -> Self {
- self.placement = placement;
- self
- }
- }
-
- /// A layer clip at one instant, for a compositor that can apply it over
- /// the stack beneath its track without leaving the GPU: its passes, and how
- /// hard the result is blended back over the untreated stack.
- #[derive(Clone, Copy, Debug)]
- pub struct Treatment<'a> {
- /// The track the layer sits on; every layer on a lower track is under it.
- pub track: usize,
- /// The passes to run over the stack beneath, in order.
- pub passes: &'a [ShaderPass],
- /// How much of the treated stack to keep over the untreated, `0..=1`.
- pub strength: f32,
- }
-
- /// Blends layers into a single output frame.
+ /// Draws a plan into a frame.
pub trait Compositor {
- /// Draws `layers` bottom-most first over an opaque black background.
+ /// Draws `plan`'s layers bottom-most first over an opaque black
+ /// background, every treatment applied over the stack beneath its
+ /// track.
///
/// Layers may hang off any edge; anything outside the output is clipped.
/// The result is always fully opaque - it is what goes to screen or to an
/// encoder, and neither has anything to show through.
- fn composite(&mut self, width: u32, height: u32, layers: &[Layer<'_>]) -> Frame;
-
- /// Draws `layers`, each with the track it came from and bottom-most
- /// first, with every treatment applied over the stack beneath its
- /// track, the treatments in ascending track order. `None` from a
- /// compositor that cannot run passes, which the CPU reference cannot:
- /// the caller then applies the treatments its own way.
- fn composite_treated(
- &mut self,
- _width: u32,
- _height: u32,
- _time: f32,
- _layers: &[(Layer<'_>, usize)],
- _treatments: &[Treatment<'_>],
- ) -> Option<Frame> {
- None
- }
+ fn render(&mut self, plan: &FramePlan) -> Frame;
+
+ /// Combines two finished frames with a transition: the outgoing picture
+ /// `from` and the incoming one `to`, at the pass's `progress`. The shader
+ /// owns the blend. `None` from a compositor that cannot run shaders, which
+ /// the CPU reference cannot: the caller then shows the fallback dissolve
+ /// the incoming layer already carries.
+ fn combine(
+ &mut self,
+ _width: u32,
+ _height: u32,
+ _time: f32,
+ _from: &Frame,
+ _to: &Frame,
+ _pass: &TransitionPass,
+ ) -> Option<Frame> {
+ None
+ }
}
/// A straightforward CPU compositor.
@@@ -567,6 -591,114 +608,115 @@@ mod tests
assert_eq!(frame.pixel(1, 1), Some([255, 0, 0, 255]));
}
+ /// A flip mirrors the picture, with and without a resample.
+ #[test]
+ fn flips_mirror_the_picture() {
+ let mut strip = Frame::transparent(2, 2);
+ strip.set_pixel(0, 0, [255, 0, 0, 255]);
+ strip.set_pixel(1, 0, [0, 0, 255, 255]);
+ strip.set_pixel(0, 1, [0, 255, 0, 255]);
+ strip.set_pixel(1, 1, [255, 255, 255, 255]);
+ let mut flipped = layer(strip.clone());
+ flipped.flip_h = true;
+ let frame = render(2, 2, vec![flipped]);
+ assert_eq!(frame.pixel(0, 0), Some([0, 0, 255, 255]));
+ assert_eq!(frame.pixel(1, 0), Some([255, 0, 0, 255]));
+ let mut flipped = layer(strip);
+ flipped.flip_v = true;
+ let frame = render(2, 2, vec![flipped]);
+ assert_eq!(frame.pixel(0, 0), Some([0, 255, 0, 255]));
+ assert_eq!(frame.pixel(1, 1), Some([0, 0, 255, 255]));
+ }
+
+ /// A crop keeps the part it names and fits it to the output.
+ #[test]
+ fn a_crop_keeps_what_it_names() {
+ let mut strip = Frame::transparent(4, 2);
+ for x in 0..4 {
+ for y in 0..2 {
+ strip.set_pixel(
+ x,
+ y,
+ if x < 2 {
+ [255, 0, 0, 255]
+ } else {
+ [0, 0, 255, 255]
+ },
+ );
+ }
+ }
+ let mut cropped = layer(strip);
+ cropped.crop = Crop::of([0.5, 0.0, 0.0, 0.0]);
+ let frame = render(2, 2, vec![cropped]);
+ assert_eq!(frame.pixel(0, 0), Some([0, 0, 255, 255]));
+ assert_eq!(frame.pixel(1, 1), Some([0, 0, 255, 255]));
+ }
+
+ /// The matte's alpha weighs the picture, sampled across it whatever
+ /// its size; a fade pulls the colour; a wipe hides the far side.
+ #[test]
+ fn the_mask_the_fade_and_the_wipe_weigh_the_picture() {
+ let red = solid(4, 4, [255, 0, 0, 255]);
+ let mut matte = Frame::transparent(2, 1);
+ matte.set_pixel(0, 0, [0, 0, 0, 255]);
+ let mut masked = layer(red.clone());
+ masked.mask = Some(Arc::new(matte));
+ let frame = render(4, 4, vec![masked]);
+ assert_eq!(frame.pixel(0, 0), Some([255, 0, 0, 255]));
+ assert_eq!(frame.pixel(3, 3), Some([0, 0, 0, 255]));
+
+ let mut faded = layer(red.clone());
+ faded.transitions = vec![Transition::FadeTo {
+ colour: [1.0; 3],
+ amount: 0.5,
+ }];
+ let frame = render(4, 4, vec![faded]);
+ assert_eq!(frame.pixel(0, 0), Some([255, 128, 128, 255]));
+
+ let mut wiped = layer(red);
+ wiped.transitions = vec![Transition::Wipe {
+ uncovered: 0.5,
+ from_right: true,
+ }];
+ let frame = render(4, 4, vec![wiped]);
+ assert_eq!(frame.pixel(0, 0), Some([0, 0, 0, 255]));
+ assert_eq!(frame.pixel(3, 0), Some([255, 0, 0, 255]));
+ }
+
+ /// A treatment on track 1 runs over what track 0 drew and not over
+ /// what track 2 draws on top of it, and its strength blends the result
+ /// back. With no kernel for its package the ground is left as it was.
+ #[test]
+ fn a_treatment_without_a_kernel_leaves_the_stack_as_it_was() {
+ let red = layer(solid(8, 8, [255, 0, 0, 255]));
+ let mut blue = layer(solid(2, 2, [0, 0, 255, 255]));
+ blue.track = 2;
+ blue.transform = Transform {
+ offset_x: -3.0 / 8.0,
+ offset_y: -3.0 / 8.0,
+ ..Transform::default()
+ };
+ let unknown = ShaderPass {
+ package: "test.nothing".to_owned(),
+ key: "test.nothing@1".to_owned(),
+ source: Arc::from(""),
+ params: vec![0; 16],
+ values: Default::default(),
+ intensity: 1.0,
+ lut: None,
++ reveal_map: None,
+ };
+ let mut frame_plan = plan(8, 8, vec![red, blue]);
+ frame_plan.treatments = vec![PlannedTreatment {
+ track: 1,
+ effects: vec![unknown],
+ strength: 1.0,
+ }];
+ let out = CpuCompositor.render(&frame_plan);
+ assert_eq!(out.pixel(7, 7), Some([255, 0, 0, 255]));
+ assert_eq!(out.pixel(0, 0), Some([0, 0, 255, 255]));
+ }
+
#[test]
fn overlap_spans_are_correct() {
assert_eq!(overlap(0, 4, 4), Some((0, 0, 4)));
diff --cc src/crates/concat-render/src/gpu.rs
index e4fa959d,275c892b..c272f94b
--- a/src/crates/concat-render/src/gpu.rs
+++ b/src/crates/concat-render/src/gpu.rs
@@@ -29,10 -29,11 +29,11 @@@
use std::collections::HashMap;
use concat_core::frame::Frame;
-use concat_core::shader::{Lut, ShaderPass};
+use concat_core::shader::{Lut, RevealMap, ShaderPass, TransitionPass};
use concat_core::timeline::Blend;
- use crate::compositor::{Compositor, CpuCompositor, Layer, Treatment};
+ use crate::compositor::{Compositor, CpuCompositor};
+ use crate::plan::{FramePlan, Geometry, PlannedLayer, PlannedTreatment, Shading};
/// Bytes per row must be a multiple of this for a texture-to-buffer copy.
const ROW_ALIGN: usize = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize;
@@@ -274,62 -301,11 +312,64 @@@ impl WgpuCompositor
},
],
});
+ // Group 3: a title's reveal map. Every pass binds one - a map that
+ // reveals everything when the package has none, or the pass is not
+ // over a title at all - so the same pipeline layout serves every
+ // shader pass whether or not it reads `reveal_order()`.
+ let reveal_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("concat pass reveal map"),
+ entries: &[
+ wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 1,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ },
+ ],
+ });
+ // Group 0 of a transition: two pictures, each a texture and a
+ // sampler - the outgoing at 0/1, the incoming at 2/3.
+ let texture_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ };
+ let sampler_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ };
+ let transition_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("concat transition inputs"),
+ entries: &[
+ texture_entry(0),
+ sampler_entry(1),
+ texture_entry(2),
+ sampler_entry(3),
+ ],
+ });
+
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("concat compositor"),
- bind_group_layouts: &[Some(&bind_layout)],
+ // Group 0 is the layer, group 1 its mask: the same shape, a
+ // texture and a sampler.
+ bind_group_layouts: &[Some(&bind_layout), Some(&bind_layout)],
immediate_size: 0,
});
@@@ -1440,146 -1351,6 +1614,126 @@@ impl Compositor for WgpuCompositor
}
}
}
+
- fn composite_treated(
- &mut self,
- width: u32,
- height: u32,
- time: f32,
- layers: &[(Layer<'_>, usize)],
- treatments: &[Treatment<'_>],
- ) -> Option<Frame> {
- if self.dead {
- return None;
- }
- let (draws, vertices) = self.prepare_treated(width, height, time, layers, treatments);
- self.write_vertices(&vertices);
- let frame = self.render_and_read(width, height, &draws);
- if frame.is_none() {
- self.dead = true;
- }
- frame
- }
-
+ fn combine(
+ &mut self,
+ width: u32,
+ height: u32,
+ time: f32,
+ from: &Frame,
+ to: &Frame,
+ pass: &TransitionPass,
+ ) -> Option<Frame> {
+ if self.dead {
+ return None;
+ }
+ self.transition_shader(pass);
+ let lut_id = self.lut_group(pass.lut.as_deref());
+
+ // The two pictures, uploaded and bound at group 0.
+ let from_texture = self.input_texture(from);
+ let to_texture = self.input_texture(to);
+ let from_view = from_texture.create_view(&wgpu::TextureViewDescriptor::default());
+ let to_view = to_texture.create_view(&wgpu::TextureViewDescriptor::default());
+ let inputs = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("concat transition inputs"),
+ layout: &self.transition_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&from_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::Sampler(&self.sampler),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: wgpu::BindingResource::TextureView(&to_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 3,
+ resource: wgpu::BindingResource::Sampler(&self.sampler),
+ },
+ ],
+ });
+
+ // The frame block carries progress where a pass carries intensity.
+ {
+ let shader = &self.transitions[&pass.key];
+ let frame_block: [f32; 4] = [width as f32, height as f32, time, pass.progress];
+ let frame_bytes: Vec<u8> = frame_block.iter().flat_map(|v| v.to_le_bytes()).collect();
+ self.queue.write_buffer(&shader.frame, 0, &frame_bytes);
+ let mut params = pass.params.clone();
+ params.resize(shader.params.size() as usize, 0);
+ self.queue.write_buffer(&shader.params, 0, &params);
+ }
+
+ self.target(width, height);
+ {
+ let target = self.target.as_ref().expect("just ensured");
+ let view = target
+ .texture
+ .create_view(&wgpu::TextureViewDescriptor::default());
+ let shader = &self.transitions[&pass.key];
+ let lut_group = &self.luts[&lut_id];
+ let mut encoder = self
+ .device
+ .create_command_encoder(&wgpu::CommandEncoderDescriptor {
+ label: Some("concat transition"),
+ });
+ {
+ let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("concat transition"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: &view,
+ depth_slice: None,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ multiview_mask: None,
+ });
+ render.set_pipeline(&shader.pipeline);
+ render.set_bind_group(0, &inputs, &[]);
+ render.set_bind_group(1, &shader.bind_group, &[]);
+ render.set_bind_group(2, lut_group, &[]);
+ render.draw(0..3, 0..1);
+ }
+ encoder.copy_texture_to_buffer(
+ wgpu::TexelCopyTextureInfo {
+ texture: &target.texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::All,
+ },
+ wgpu::TexelCopyBufferInfo {
+ buffer: &target.staging,
+ layout: wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(target.padded_row as u32),
+ rows_per_image: Some(height),
+ },
+ },
+ wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ );
+ self.queue.submit([encoder.finish()]);
+ }
+ let frame = self.read_back();
+ if frame.is_none() {
+ self.dead = true;
+ }
+ frame
+ }
}
impl WgpuCompositor {
diff --cc src/crates/concat-render/src/gpu/tests.rs
index 00000000,3b7280ca..f9905607
mode 000000,100644..100644
--- a/src/crates/concat-render/src/gpu/tests.rs
+++ b/src/crates/concat-render/src/gpu/tests.rs
@@@ -1,0 -1,397 +1,502 @@@
+ // SPDX-License-Identifier: AGPL-3.0-or-later
+ // SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+ //! The GPU compositor against the CPU reference: the same plans, and the
+ //! pictures must agree. Every test skips on a machine without an adapter,
+ //! and says so.
+
+ use std::collections::BTreeMap;
+ use std::sync::Arc;
+
+ use concat_core::frame::Frame;
-use concat_core::shader::{Lut, ShaderPass};
++use concat_core::shader::{Lut, RevealMap, ShaderPass};
+ use concat_core::timeline::{Blend, Transform};
+
+ use super::*;
+ use crate::CpuCompositor;
+ use crate::metrics::ssim;
+ use crate::plan::{Crop, Transition, detached_clip};
+
+ fn gpu() -> Option<WgpuCompositor> {
+ let compositor = WgpuCompositor::new();
+ if compositor.is_none() {
+ eprintln!("no usable GPU adapter; skipping");
+ }
+ compositor
+ }
+
+ fn solid(width: u32, height: u32, rgba: [u8; 4]) -> Frame {
+ let mut frame = Frame::transparent(width, height);
+ frame.fill(rgba);
+ frame
+ }
+
+ /// A picture with something in it everywhere: a diagonal gradient and a
+ /// dark bar, so a misplaced or mirrored picture is caught.
+ fn gradient(width: u32, height: u32) -> Frame {
+ let mut frame = Frame::transparent(width, height);
+ for y in 0..height {
+ for x in 0..width {
+ let r = (x * 255 / width.max(1)) as u8;
+ let g = (y * 255 / height.max(1)) as u8;
+ let bar = x > width / 3 && x < width / 2 && y > height / 4;
+ let b = if bar { 20 } else { 200 };
+ frame.set_pixel(x, y, [r, g, b, 255]);
+ }
+ }
+ frame
+ }
+
+ fn layer(frame: Frame) -> PlannedLayer {
+ PlannedLayer::picture(detached_clip(), Arc::new(frame))
+ }
+
+ fn plan(width: u32, height: u32, layers: Vec<PlannedLayer>) -> FramePlan {
+ FramePlan {
+ layers,
+ ..FramePlan::empty(width, height)
+ }
+ }
+
+ /// A real package's pass: `id` with `body` as its shader and `params` as
+ /// the manifest's parameter table, resolved to `values`.
+ fn package(
+ id: &str,
+ body: &str,
+ params: &str,
+ values: &[(&str, f64)],
+ intensity: f32,
+ ) -> ShaderPass {
+ let manifest = concat_effects::Manifest::parse(&format!(
+ "[effect]\nid = \"{id}\"\nname = \"Test\"\nkind = \"effect\"\n{params}\n[wgsl]\nentry = \"effect.wgsl\"\n"
+ ))
+ .expect("a manifest");
+ let shader = concat_effects::Shader::compile(&manifest, body).expect("compiles");
+ let values: BTreeMap<String, f64> = values
+ .iter()
+ .map(|(key, value)| ((*key).to_owned(), *value))
+ .collect();
- shader.pass(&values, &manifest.params, intensity, None)
++ shader.pass(&values, &manifest.params, intensity, None, None)
+ }
+
+ const INVERT: &str = "fn effect(uv: vec2<f32>) -> vec4<f32> { let c = sample(uv); return vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a); }";
+
+ /// Both compositors draw `plan`; the pictures must agree to `least` by
+ /// SSIM, and every pixel's alpha must be opaque.
+ fn assert_parity(name: &str, plan: &FramePlan, least: f64) {
+ let Some(mut gpu) = gpu() else { return };
+ let expected = CpuCompositor.render(plan);
+ let actual = gpu.render(plan);
+ let score = ssim(&expected, &actual);
+ eprintln!("parity {name}: ssim {score:.4}");
+ assert!(
+ score >= least,
+ "{name}: cpu and gpu differ, ssim {score:.4} < {least}"
+ );
+ assert!(
+ actual.pixels().chunks_exact(4).all(|px| px[3] == 255),
+ "{name}: opaque"
+ );
+ }
+
+ #[test]
+ fn empty_output_is_opaque_black() {
+ let Some(mut gpu) = gpu() else { return };
+ let frame = gpu.render(&FramePlan::empty(4, 4));
+ assert_eq!(frame.pixel(0, 0), Some([0, 0, 0, 255]));
+ assert_eq!(frame.pixel(3, 3), Some([0, 0, 0, 255]));
+ }
+
+ #[test]
+ fn plain_layers_match_the_cpu_reference_to_the_pixel() {
+ let Some(mut gpu) = gpu() else { return };
+ let mut top = layer(solid(3, 3, [0, 0, 255, 160]));
+ top.transform = Transform {
+ offset_x: 2.0 / 8.0,
+ offset_y: 1.0 / 8.0,
+ ..Transform::default()
+ };
+ let plan = plan(8, 8, vec![layer(solid(8, 8, [255, 0, 0, 255])), top]);
+ let expected = CpuCompositor.render(&plan);
+ let actual = gpu.render(&plan);
+ for (index, (want, got)) in expected
+ .pixels()
+ .chunks_exact(4)
+ .zip(actual.pixels().chunks_exact(4))
+ .enumerate()
+ {
+ for channel in 0..4 {
+ let difference = (i32::from(want[channel]) - i32::from(got[channel])).abs();
+ assert!(
+ difference <= 1,
+ "pixel {index} channel {channel}: cpu {want:?} vs gpu {got:?}"
+ );
+ }
+ }
+ }
+
+ /// A pass runs over the layer before it is placed: an invert shader
+ /// over a red frame composites cyan, and at half intensity the mix.
+ #[test]
+ fn a_pass_treats_the_layer_before_it_is_placed() {
+ let Some(mut gpu) = gpu() else { return };
+ let red = solid(4, 4, [255, 0, 0, 255]);
+ let mut full = layer(red.clone());
+ full.effects = vec![package("test.invert", INVERT, "", &[], 1.0)];
+ let out = gpu.render(&plan(4, 4, vec![full]));
+ assert_eq!(&out.pixels()[..3], &[0, 255, 255]);
+ let mut half = layer(red);
+ half.effects = vec![package("test.invert", INVERT, "", &[], 0.5)];
+ let out = gpu.render(&plan(4, 4, vec![half]));
+ let p = &out.pixels()[..3];
+ assert!(
+ p[0] > 120 && p[0] < 136 && p[1] > 120 && p[1] < 136,
+ "{p:?}"
+ );
+ }
+
+ /// A pass reads its table through `lut()`: a table that answers green
+ /// to every colour turns a red frame green, and a pass without one is
+ /// handed the identity and changes nothing.
+ #[test]
+ fn a_pass_samples_its_table_and_the_identity_without_one() {
+ let Some(mut gpu) = gpu() else { return };
+ let body = "fn effect(uv: vec2<f32>) -> vec4<f32> { let c = sample(uv); return vec4<f32>(lut(c.rgb), c.a); }";
+ let green = Lut::from_rgb(2, &[0.0, 1.0, 0.0].repeat(8)).expect("a table");
+ let mut tabled = layer(solid(4, 4, [255, 0, 0, 255]));
+ let mut pass = package("test.table", body, "", &[], 1.0);
+ pass.lut = Some(Arc::new(green));
+ tabled.effects = vec![pass];
+ let out = gpu.render(&plan(4, 4, vec![tabled]));
+ assert_eq!(&out.pixels()[..3], &[0, 255, 0]);
+ let mut plain = layer(solid(4, 4, [255, 0, 0, 255]));
+ plain.effects = vec![package("test.table", body, "", &[], 1.0)];
+ let out = gpu.render(&plan(4, 4, vec![plain]));
+ assert_eq!(&out.pixels()[..3], &[255, 0, 0]);
+ }
+
+ /// A treatment runs its passes over the stack beneath its track and
+ /// nothing above it, blended back by its strength, without the frame
+ /// leaving the GPU: an invert over a red ground under a blue quarter
+ /// turns the ground cyan and leaves the blue alone.
+ #[test]
+ fn a_treatment_treats_the_stack_beneath_its_track_only() {
+ let Some(mut gpu) = gpu() else { return };
+ let ground = layer(solid(8, 8, [255, 0, 0, 255]));
+ let mut blue = layer(solid(4, 4, [0, 0, 255, 255]));
+ blue.track = 2;
+ blue.transform = Transform {
+ offset_x: -2.0 / 8.0,
+ offset_y: -2.0 / 8.0,
+ ..Transform::default()
+ };
+ let treated = |strength: f32| FramePlan {
+ treatments: vec![PlannedTreatment {
+ track: 1,
+ effects: vec![package("concat.invert", INVERT, "", &[], 1.0)],
+ strength,
+ }],
+ ..plan(8, 8, vec![ground.clone(), blue.clone()])
+ };
+ let out = gpu.render(&treated(1.0));
+ // Top-left is under the blue quarter; bottom-right is treated ground.
+ assert_eq!(&out.pixels()[..3], &[0, 0, 255]);
+ let last = out.pixels().len() - 4;
+ assert_eq!(&out.pixels()[last..last + 3], &[0, 255, 255]);
+ let out = gpu.render(&treated(0.5));
+ let p = &out.pixels()[last..last + 3];
+ assert!(
+ p[0] > 120 && p[0] < 136 && p[1] > 120 && p[1] < 136,
+ "{p:?}"
+ );
+ // The CPU reference agrees on the whole picture.
+ assert_parity("treatment", &treated(0.5), 0.99);
+ }
+
+ /// The parity suite: one plan per thing a frame can ask for, drawn by
+ /// both compositors, alike by SSIM.
+ #[test]
+ fn every_kind_of_layer_matches_the_cpu_reference() {
+ let sepia = include_str!("../../../concat-effects/packages/concat.sepia/effect.wgsl");
+ let blur = include_str!("../../../concat-effects/packages/concat.box-blur/effect.wgsl");
+ let radius =
+ "[[param]]\nkey = \"radius\"\nlabel = \"Radius\"\nmin = 0\nmax = 20\ndefault = 4\n";
+
+ assert_parity("plain", &plan(64, 48, vec![layer(gradient(64, 48))]), 0.999);
+
+ // Fitted: a wide picture inside a square, letterboxed.
+ assert_parity("fitted", &plan(64, 64, vec![layer(gradient(48, 16))]), 0.99);
+
+ let mut placed = layer(gradient(32, 24));
+ placed.transform = Transform {
+ scale: 1.4,
+ rotation: 30.0,
+ offset_x: 0.1,
+ offset_y: -0.05,
+ stretch_x: 1.2,
+ ..Transform::default()
+ };
+ assert_parity(
+ "placed",
+ &plan(
+ 64,
+ 64,
+ vec![layer(solid(64, 64, [40, 40, 40, 255])), placed],
+ ),
+ 0.98,
+ );
+
+ let mut cropped = layer(gradient(64, 48));
+ cropped.crop = Crop::of([0.25, 0.1, 0.2, 0.3]);
+ assert_parity("cropped", &plan(64, 48, vec![cropped]), 0.99);
+
+ let mut flipped = layer(gradient(64, 48));
+ flipped.flip_h = true;
+ flipped.flip_v = true;
+ assert_parity("flipped", &plan(64, 48, vec![flipped]), 0.999);
+
+ let mut blended = layer(gradient(64, 48));
+ blended.opacity = 0.5;
+ blended.blend = Blend::Multiply;
+ assert_parity(
+ "blended",
+ &plan(64, 48, vec![layer(gradient(64, 48)), blended]),
+ 0.99,
+ );
+
+ let mut masked = layer(gradient(64, 64));
+ let mut mask = Frame::transparent(32, 32);
+ for y in 0..32u32 {
+ for x in 0..32u32 {
+ let d = ((x as f32 - 16.0).powi(2) + (y as f32 - 16.0).powi(2)).sqrt();
+ let a = ((16.0 - d) / 8.0).clamp(0.0, 1.0);
+ mask.set_pixel(x, y, [255, 255, 255, (a * 255.0) as u8]);
+ }
+ }
+ masked.mask = Some(Arc::new(mask));
+ assert_parity(
+ "masked",
+ &plan(64, 64, vec![layer(solid(64, 64, [0, 60, 0, 255])), masked]),
+ 0.99,
+ );
+
+ let mut cut = layer(gradient(64, 48));
+ cut.transitions = vec![
+ Transition::FadeTo {
+ colour: [0.0, 0.0, 0.0],
+ amount: 0.5,
+ },
+ Transition::Wipe {
+ uncovered: 0.6,
+ from_right: false,
+ },
+ ];
+ assert_parity("faded and wiped", &plan(64, 48, vec![cut]), 0.99);
+
+ let mut toned = layer(gradient(64, 48));
+ toned.effects = vec![package("concat.sepia", sepia, "", &[], 1.0)];
+ assert_parity("sepia kernel", &plan(64, 48, vec![toned]), 0.99);
+
+ let mut keyed = layer(gradient(64, 48));
+ keyed.effects = vec![package("concat.sepia", sepia, "", &[], 0.4)];
+ assert_parity(
+ "sepia at a keyed intensity",
+ &plan(64, 48, vec![keyed]),
+ 0.99,
+ );
+
+ // A blur over a cropped, flipped picture: the effects have to see it
+ // made first, on both sides.
+ let mut prepared = layer(gradient(64, 48));
+ prepared.crop = Crop::of([0.1, 0.0, 0.1, 0.2]);
+ prepared.flip_h = true;
+ prepared.effects = vec![package(
+ "concat.box-blur",
+ blur,
+ radius,
+ &[("radius", 3.0)],
+ 1.0,
+ )];
+ assert_parity(
+ "blur over a made picture",
+ &plan(80, 60, vec![prepared]),
+ 0.98,
+ );
+
+ let mut under = layer(gradient(64, 48));
+ under.track = 0;
+ let mut over = layer(solid(16, 16, [0, 0, 255, 255]));
+ over.track = 2;
+ let treated = FramePlan {
+ treatments: vec![PlannedTreatment {
+ track: 1,
+ effects: vec![package("concat.sepia", sepia, "", &[], 1.0)],
+ strength: 0.6,
+ }],
+ ..plan(64, 48, vec![under, over])
+ };
+ assert_parity("treated stack", &treated, 0.99);
+ }
+
+ /// What cannot be drawn is not drawn, the same way on both sides: a
+ /// layer with a NaN opacity, one far outside the frame, one with no
+ /// picture, and an opacity past one is one.
+ #[test]
+ fn what_cannot_be_drawn_is_skipped_alike() {
+ let Some(mut gpu) = gpu() else { return };
+ let mut nan = layer(solid(8, 8, [255, 0, 0, 255]));
+ nan.opacity = f32::NAN;
+ let mut away = layer(solid(8, 8, [255, 0, 0, 255]));
+ away.transform = Transform {
+ offset_x: 40.0,
+ ..Transform::default()
+ };
+ let mut none = layer(solid(8, 8, [255, 0, 0, 255]));
+ none.source = None;
+ let plan = plan(8, 8, vec![nan, away, none]);
+ assert_eq!(gpu.render(&plan).pixel(4, 4), Some([0, 0, 0, 255]));
+ assert_eq!(
+ CpuCompositor.render(&plan).pixel(4, 4),
+ Some([0, 0, 0, 255])
+ );
+ let mut over = layer(solid(8, 8, [0, 200, 0, 255]));
+ over.opacity = 7.0;
+ let plan = FramePlan {
+ layers: vec![over],
+ ..FramePlan::empty(8, 8)
+ };
+ assert_eq!(gpu.render(&plan).pixel(1, 1), Some([0, 200, 0, 255]));
+ }
+
+ /// A package that runs within the budget passes its trial; a device that
+ /// answers in time is kept.
+ #[test]
+ fn a_benign_package_survives_its_trial() {
+ let Some(mut gpu) = gpu() else { return };
+ let pass = package("test.trial", INVERT, "", &[], 1.0);
+ gpu.trial(&pass, std::time::Duration::from_secs(5))
+ .expect("an invert is quick");
+ assert!(!gpu.is_dead());
+ // The compositor is still good for a frame afterwards.
+ let frame = gpu.render(&plan(4, 4, vec![layer(solid(4, 4, [0, 0, 255, 255]))]));
+ assert_eq!(&frame.pixels()[..3], &[0, 0, 255]);
+ }
+
+ #[test]
+ fn output_size_changes_are_handled() {
+ let Some(mut gpu) = gpu() else { return };
+ let small = gpu.render(&plan(4, 4, vec![layer(solid(4, 4, [255, 255, 255, 255]))]));
+ assert_eq!((small.width(), small.height()), (4, 4));
+ let large = gpu.render(&plan(
+ 16,
+ 8,
+ vec![layer(solid(16, 8, [255, 255, 255, 255]))],
+ ));
+ assert_eq!((large.width(), large.height()), (16, 8));
+ assert_eq!(large.pixel(15, 7), Some([255, 255, 255, 255]));
+ }
++
++/// A pass reads its title's reveal map through `reveal_order()`: the
++/// first word's half of the canvas reads 0, the second word's half
++/// reads 1, and a pass without one - the common case, any pass over
++/// anything that is not a title - reads the identity, 0 everywhere.
++#[test]
++fn a_pass_reads_its_reveal_map_and_the_identity_everywhere() {
++ let Some(mut gpu) = gpu() else { return };
++ let body =
++ "fn effect(uv: vec2<f32>) -> vec4<f32> { let r = reveal_order(uv); return vec4<f32>(r, r, r, 1.0); }";
++ let map = RevealMap::from_rects(4, 4, &[(0, 0, 2, 4), (2, 0, 2, 4)]);
++ let mut revealed = layer(solid(4, 4, [0, 0, 0, 255]));
++ let mut pass = package("test.reveal", body, "", &[], 1.0);
++ pass.reveal_map = Some(Arc::new(map));
++ revealed.effects = vec![pass];
++ let out = gpu.render(&plan(4, 4, vec![revealed]));
++ let pixels = out.pixels();
++ assert_eq!(pixels[0], 0, "{:?}", &pixels[..4]);
++ assert_eq!(pixels[2 * 4], 255, "{:?}", &pixels[8..12]);
++
++ let mut plain = layer(solid(4, 4, [0, 0, 0, 255]));
++ plain.effects = vec![package("test.reveal", body, "", &[], 1.0)];
++ let out = gpu.render(&plan(4, 4, vec![plain]));
++ assert_eq!(&out.pixels()[..4], &[0, 0, 0, 255]);
++}
++
++/// A transition combines its two inputs through its shader: a trivial
++/// dissolve over two solid colours must equal `from` at progress 0, `to`
++/// at progress 1, and the exact half-and-half mix at progress 0.5. The
++/// golden every packaged transition's own shader is measured against.
++#[test]
++fn a_transition_combines_its_two_inputs_by_progress() {
++ let Some(mut gpu) = gpu() else { return };
++ let manifest = concat_effects::Manifest::parse(
++ "[effect]\nid = \"test.dissolve\"\nname = \"Dissolve\"\nkind = \"transition\"\n[transition]\nentry = \"effect.wgsl\"\n",
++ )
++ .expect("a manifest");
++ let shader = concat_effects::TransitionShader::compile(
++ &manifest,
++ "fn transition(uv: vec2<f32>, progress: f32) -> vec4<f32> { return mix(from_at(uv), to_at(uv), progress); }",
++ )
++ .expect("compiles");
++ let red = solid(4, 4, [255, 0, 0, 255]);
++ let blue = solid(4, 4, [0, 0, 255, 255]);
++
++ let mut at = |progress: f32| {
++ let pass = shader.pass(&Default::default(), &[], progress, None);
++ gpu.combine(4, 4, 0.0, &red, &blue, &pass)
++ .expect("a GPU combine")
++ };
++ assert_eq!(&at(0.0).pixels()[..3], &[255, 0, 0], "all outgoing at 0");
++ assert_eq!(&at(1.0).pixels()[..3], &[0, 0, 255], "all incoming at 1");
++ assert_eq!(&at(0.5).pixels()[..3], &[128, 0, 128], "the exact half mix");
++}
++
++/// Every packaged effect and filter with a shader actually renders on
++/// the GPU at its default settings - naga's validation at load catches
++/// a broken shader's syntax and types, but only a real pipeline creation
++/// and draw catches a binding or layout mistake.
++#[test]
++fn every_shader_package_renders_at_its_defaults() {
++ let Some(mut gpu) = gpu() else { return };
++ let source = solid(4, 4, [200, 120, 60, 255]);
++ let catalogue = concat_effects::Catalogue::builtin();
++ for package in catalogue.packages() {
++ let Some(shader) = package.shader() else { continue };
++ let values = package.resolve(&Default::default());
++ let pass = shader.pass(
++ &values,
++ &package.manifest.params,
++ 1.0,
++ package.lut().cloned(),
++ None,
++ );
++ let mut treated = layer(source.clone());
++ treated.effects = vec![pass];
++ let out = gpu.render(&plan(4, 4, vec![treated]));
++ assert_eq!(out.pixels().len(), 4 * 4 * 4, "{}", package.id());
++ }
++}
++
++/// Every packaged transition's pipeline actually creates and runs on the
++/// GPU, across its whole progress range - naga's validation at load
++/// catches a broken shader's syntax and types, but only a real pipeline
++/// creation catches a binding or layout mistake.
++#[test]
++fn every_packaged_transition_combines_across_its_progress_range() {
++ let Some(mut gpu) = gpu() else { return };
++ let red = solid(4, 4, [255, 0, 0, 255]);
++ let blue = solid(4, 4, [0, 0, 255, 255]);
++ let catalogue = concat_effects::Catalogue::builtin();
++ for package in catalogue.packages() {
++ if package.kind() != concat_effects::Kind::Transition {
++ continue;
++ }
++ for progress in [0.0, 0.25, 0.5, 0.75, 1.0] {
++ let pass = catalogue
++ .transition_pass(package.id(), &Default::default(), progress)
++ .unwrap_or_else(|| panic!("{} has no transition pass", package.id()));
++ gpu.combine(4, 4, 0.0, &red, &blue, &pass).unwrap_or_else(|| {
++ panic!("{} failed to combine at progress {progress}", package.id())
++ });
++ }
++ }
++}
diff --cc src/crates/concat-render/src/kernels.rs
index 00000000,67ea0d99..3fe13f09
mode 000000,100644..100644
--- a/src/crates/concat-render/src/kernels.rs
+++ b/src/crates/concat-render/src/kernels.rs
@@@ -1,0 -1,297 +1,298 @@@
+ // SPDX-License-Identifier: AGPL-3.0-or-later
+ // SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+ //! The CPU's effects: one kernel per built-in package, keyed by its id.
+ //!
+ //! A package is a WGSL shader, and the GPU runs it. The CPU reference
+ //! cannot run WGSL at any useful speed, so it carries a kernel of its own
+ //! for each package it knows, written to the same arithmetic as the
+ //! shader and read from the same resolved values, and checked against the
+ //! GPU by the parity suite. A package it does not know - a community
+ //! package, or a built-in nobody has ported - is drawn untreated and said
+ //! so once per process, which is honest where a filter string that
+ //! silently differs was not.
+
+ use std::collections::HashSet;
+ use std::sync::{Mutex, OnceLock};
+
+ use concat_core::frame::{BYTES_PER_PIXEL, Frame};
+ use concat_core::shader::ShaderPass;
+
+ use crate::compositor::sample_bilinear;
+
+ /// Every package the CPU renders.
+ pub const KERNELS: &[&str] = &[
+ "concat.black-white",
+ "concat.invert",
+ "concat.sepia",
+ "concat.hue-shift",
+ "concat.vignette",
+ "concat.box-blur",
+ "concat.gaussian-blur",
+ ];
+
+ /// Whether the CPU has a kernel for the package.
+ pub fn has_kernel(package: &str) -> bool {
+ KERNELS.contains(&package)
+ }
+
+ /// Runs the kernel for `pass.package` over `picture` at full strength, the
+ /// intensity mix being the caller's; `None` when there is no kernel for
+ /// the package. `seconds` is the timeline instant, for a kernel that
+ /// moves; none of these do.
+ pub fn run(pass: &ShaderPass, picture: &Frame, seconds: f32) -> Option<Frame> {
+ let _ = seconds;
+ let knob = |key: &str| pass.values.get(key).copied().unwrap_or(0.0) as f32;
+ Some(match pass.package.as_str() {
+ "concat.black-white" => per_pixel(picture, |[r, g, b, a]| {
+ let y = luma(r, g, b);
+ [y, y, y, a]
+ }),
+ "concat.invert" => per_pixel(picture, |[r, g, b, a]| [1.0 - r, 1.0 - g, 1.0 - b, a]),
+ "concat.sepia" => per_pixel(picture, |[r, g, b, a]| {
+ [
+ (r * 0.393 + g * 0.769 + b * 0.189).clamp(0.0, 1.0),
+ (r * 0.349 + g * 0.686 + b * 0.168).clamp(0.0, 1.0),
+ (r * 0.272 + g * 0.534 + b * 0.131).clamp(0.0, 1.0),
+ a,
+ ]
+ }),
+ "concat.hue-shift" => {
+ let (sn, cs) = knob("angle").to_radians().sin_cos();
+ per_pixel(picture, |[r, g, b, a]| {
+ // A turn of the hue is a plain rotation in YIQ, where the
+ // two chroma axes are at right angles.
+ let y = 0.299 * r + 0.587 * g + 0.114 * b;
+ let i = 0.596 * r - 0.274 * g - 0.322 * b;
+ let q = 0.211 * r - 0.523 * g + 0.312 * b;
+ let (i, q) = (i * cs - q * sn, i * sn + q * cs);
+ [
+ (y + 0.956 * i + 0.621 * q).clamp(0.0, 1.0),
+ (y - 0.272 * i - 0.647 * q).clamp(0.0, 1.0),
+ (y - 1.106 * i + 1.703 * q).clamp(0.0, 1.0),
+ a,
+ ]
+ })
+ }
+ "concat.vignette" => {
+ let s = knob("strength") / 100.0;
+ per_point(picture, |[r, g, b, a], u, v| {
+ let d = ((u - 0.5) * 2.0).hypot((v - 0.5) * 2.0);
+ let fall = smoothstep(1.4 - s * 0.9, 1.4 + 0.2 - s * 0.3, d);
+ let keep = 1.0 - fall * (0.4 + 0.6 * s);
+ [r * keep, g * keep, b * keep, a]
+ })
+ }
+ "concat.box-blur" => {
+ let step = (knob("radius") / 4.0).max(0.5);
+ gathered(picture, step, |_, _| 1.0)
+ }
+ "concat.gaussian-blur" => {
+ let sigma = knob("radius").max(0.5);
+ let step = (sigma / 2.5).max(1.0);
+ gathered(picture, step, move |dx, dy| {
+ (-(dx * dx + dy * dy) / (2.0 * sigma * sigma)).exp()
+ })
+ }
+ _ => return None,
+ })
+ }
+
+ /// Says once, in the log, that `package` is drawn untreated on the CPU.
+ pub fn fallback_once(package: &str) {
+ static SEEN: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
+ let seen = SEEN.get_or_init(|| Mutex::new(HashSet::new()));
+ let mut seen = seen.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ if seen.insert(package.to_owned()) {
+ log::warn!("{package}: no CPU kernel for this package; the picture is drawn untreated");
+ }
+ }
+
+ /// Luminance, Rec. 709.
+ fn luma(r: f32, g: f32, b: f32) -> f32 {
+ 0.2126 * r + 0.7152 * g + 0.0722 * b
+ }
+
+ /// WGSL's `smoothstep`.
+ fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
+ let t = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
+ t * t * (3.0 - 2.0 * t)
+ }
+
+ /// Every pixel through `f`, straight colour and alpha in `0..=1`.
+ fn per_pixel(picture: &Frame, f: impl Fn([f32; 4]) -> [f32; 4]) -> Frame {
+ per_point(picture, |rgba, _, _| f(rgba))
+ }
+
+ /// Every pixel through `f` with where it is, `u` and `v` in `0..1` across
+ /// the picture at the pixel's centre.
+ fn per_point(picture: &Frame, f: impl Fn([f32; 4], f32, f32) -> [f32; 4]) -> Frame {
+ let (width, height) = (picture.width(), picture.height());
+ let mut out = picture.clone();
+ let stride = width as usize * BYTES_PER_PIXEL;
+ let pixels = out.pixels_mut();
+ for y in 0..height as usize {
+ let v = (y as f32 + 0.5) / height as f32;
+ for x in 0..width as usize {
+ let u = (x as f32 + 0.5) / width as f32;
+ let at = y * stride + x * BYTES_PER_PIXEL;
+ let rgba = f(
+ [
+ f32::from(pixels[at]) / 255.0,
+ f32::from(pixels[at + 1]) / 255.0,
+ f32::from(pixels[at + 2]) / 255.0,
+ f32::from(pixels[at + 3]) / 255.0,
+ ],
+ u,
+ v,
+ );
+ for (channel, value) in rgba.iter().enumerate() {
+ pixels[at + channel] = (value * 255.0).round().clamp(0.0, 255.0) as u8;
+ }
+ }
+ }
+ out
+ }
+
+ /// The blurs' 9×9 gather: every pixel becomes the weighted mean of the
+ /// samples `step` pixels apart around it, `weight` of the offset in
+ /// pixels, sampled bilinearly with the edge clamped the way the GPU's
+ /// sampler does.
+ fn gathered(picture: &Frame, step: f32, weight: impl Fn(f32, f32) -> f32) -> Frame {
+ let (width, height) = (picture.width(), picture.height());
+ let mut out = picture.clone();
+ let stride = width as usize * BYTES_PER_PIXEL;
+ let taps: Vec<(f32, f32, f32)> = (-4..=4)
+ .flat_map(|y| (-4..=4).map(move |x| (x as f32 * step, y as f32 * step)))
+ .map(|(dx, dy)| (dx, dy, weight(dx, dy)))
+ .collect();
+ let total: f32 = taps.iter().map(|(_, _, w)| *w).sum();
+ let pixels = out.pixels_mut();
+ for y in 0..height as usize {
+ for x in 0..width as usize {
+ let mut sum = [0.0f32; 4];
+ for (dx, dy, w) in &taps {
+ let sample = sample_bilinear(picture, x as f32 + dx, y as f32 + dy);
+ for (channel, value) in sample.iter().enumerate() {
+ sum[channel] += value * w;
+ }
+ }
+ let at = y * stride + x * BYTES_PER_PIXEL;
+ for (channel, value) in sum.iter().enumerate() {
+ pixels[at + channel] = (value / total).round().clamp(0.0, 255.0) as u8;
+ }
+ }
+ }
+ out
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use std::collections::BTreeMap;
+ use std::sync::Arc;
+
+ use super::*;
+
+ fn pass(package: &str, values: &[(&str, f64)]) -> ShaderPass {
+ ShaderPass {
+ package: package.to_owned(),
+ key: package.to_owned(),
+ source: Arc::from(""),
+ params: vec![0; ShaderPass::MIN_PARAMS],
+ values: values
+ .iter()
+ .map(|(key, value)| ((*key).to_owned(), *value))
+ .collect::<BTreeMap<_, _>>(),
+ intensity: 1.0,
+ lut: None,
++ reveal_map: None,
+ }
+ }
+
+ fn solid(rgba: [u8; 4]) -> Frame {
+ let mut frame = Frame::transparent(4, 4);
+ frame.fill(rgba);
+ frame
+ }
+
+ #[test]
+ fn every_listed_package_runs_and_an_unknown_one_does_not() {
+ let picture = solid([200, 100, 50, 255]);
+ for package in KERNELS {
+ assert!(has_kernel(package));
+ let knobs = [("radius", 2.0), ("strength", 50.0), ("angle", 90.0)];
+ let out = run(&pass(package, &knobs), &picture, 0.0)
+ .unwrap_or_else(|| panic!("{package} has a kernel"));
+ assert_eq!((out.width(), out.height()), (4, 4));
+ }
+ assert!(!has_kernel("someone.else"));
+ assert!(run(&pass("someone.else", &[]), &picture, 0.0).is_none());
+ fallback_once("someone.else");
+ fallback_once("someone.else");
+ }
+
+ #[test]
+ fn the_colour_kernels_do_their_arithmetic() {
+ let red = solid([255, 0, 0, 255]);
+ let bw = run(&pass("concat.black-white", &[]), &red, 0.0).expect("kernel");
+ let y = (0.2126f32 * 255.0).round() as u8;
+ assert_eq!(bw.pixel(0, 0), Some([y, y, y, 255]));
+ let inverted = run(&pass("concat.invert", &[]), &red, 0.0).expect("kernel");
+ assert_eq!(inverted.pixel(0, 0), Some([0, 255, 255, 255]));
+ let sepia = run(&pass("concat.sepia", &[]), &red, 0.0).expect("kernel");
+ assert_eq!(sepia.pixel(0, 0), Some([100, 89, 69, 255]));
+ // A full turn of the hue is the colour again, near enough.
+ let turned =
+ run(&pass("concat.hue-shift", &[("angle", 360.0)]), &red, 0.0).expect("kernel");
+ let [r, g, b, _] = turned.pixel(0, 0).expect("in");
+ assert!(r >= 250 && g <= 5 && b <= 5, "{r} {g} {b}");
+ // Alpha rides through every colour kernel untouched.
+ let half = solid([255, 0, 0, 128]);
+ assert_eq!(
+ run(&pass("concat.invert", &[]), &half, 0.0)
+ .expect("kernel")
+ .pixel(0, 0),
+ Some([0, 255, 255, 128])
+ );
+ }
+
+ #[test]
+ fn a_blur_of_a_flat_picture_is_the_picture_and_the_edges_stay_put() {
+ let flat = solid([90, 120, 30, 255]);
+ for package in ["concat.box-blur", "concat.gaussian-blur"] {
+ let out = run(&pass(package, &[("radius", 8.0)]), &flat, 0.0).expect("kernel");
+ assert_eq!(out.pixel(0, 0), Some([90, 120, 30, 255]), "{package}");
+ assert_eq!(out.pixel(3, 3), Some([90, 120, 30, 255]), "{package}");
+ }
+ // A negative radius is the floor, not a panic.
+ let out = run(&pass("concat.box-blur", &[("radius", -9.0)]), &flat, 0.0).expect("kernel");
+ assert_eq!(out.pixel(1, 1), Some([90, 120, 30, 255]));
+ let mut edge = Frame::transparent(4, 1);
+ edge.set_pixel(0, 0, [255, 255, 255, 255]);
+ let out = run(&pass("concat.box-blur", &[("radius", 4.0)]), &edge, 0.0).expect("kernel");
+ let [near, ..] = out.pixel(0, 0).expect("in");
+ let [far, ..] = out.pixel(3, 0).expect("in");
+ assert!(
+ near > far,
+ "the white spreads but stays brightest where it was: {near} vs {far}"
+ );
+ }
+
+ #[test]
+ fn a_vignette_darkens_the_corners_and_not_the_centre() {
+ let mut flat = Frame::transparent(64, 64);
+ flat.fill([200, 200, 200, 255]);
+ let out =
+ run(&pass("concat.vignette", &[("strength", 100.0)]), &flat, 0.0).expect("kernel");
+ let [centre, ..] = out.pixel(32, 32).expect("in");
+ let [corner, ..] = out.pixel(0, 0).expect("in");
+ assert!(centre >= 195, "{centre}");
+ assert!(corner < centre, "{corner} < {centre}");
+ let out = run(&pass("concat.vignette", &[("strength", 0.0)]), &flat, 0.0).expect("kernel");
+ assert_eq!(
+ out.pixel(0, 0),
+ Some([200, 200, 200, 255]),
+ "no strength, no fall"
+ );
+ }
+ }
diff --cc src/crates/concat-text/src/lib.rs
index a99d890f,16e3e36e..c6cb3ced
--- a/src/crates/concat-text/src/lib.rs
+++ b/src/crates/concat-text/src/lib.rs
@@@ -82,26 -82,16 +82,32 @@@ pub struct TitleStyle
/// Extra advance after every glyph, as a fraction of frame height.
pub tracking: f64,
/// The widest a line may run, as a fraction of frame width, before its
- /// words wrap; zero for no limit.
+ /// words wrap; zero for no limit. With a limit the block is a box that
+ /// wide - a word no line can hold still widens it - and the lines
+ /// align inside the box rather than against each other.
pub max_width: f64,
+ /// The box's height as a fraction of frame height; zero for the words'
+ /// own. With a height the block is a box that tall, at least, and the
+ /// words sit centred in it.
+ pub max_height: f64,
}
+/// One word's box in canvas pixels, top-left origin, in reading order
+/// (line by line, left to right within a line). A pixel-reveal effect
+/// keys off this order rather than the word itself, which is why it is
+/// exposed as rects and not text.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub struct WordRect {
+ /// The box's left edge.
+ pub x: i32,
+ /// The box's top edge.
+ pub y: i32,
+ /// The box's width.
+ pub width: u32,
+ /// The box's height.
+ pub height: u32,
+}
+
/// The finished title as pixels: the canvas, RGBA with straight alpha,
/// for a monitor that wants it now and not from a file.
#[derive(Clone, PartialEq, Debug)]
@@@ -557,13 -542,29 +587,29 @@@ fn paint
.flat_map(|line| wrap_line(&face, line, em, tracking, max_w))
.collect();
let rows = lines.len().max(1);
- let block_w = lines.iter().map(|line| line.width).fold(0.0, f32::max);
- let block_h = (rows as f32 - 1.0) * pitch + ascent + descent;
- if block_w <= 0.0 || lines.is_empty() {
+ let words_w = lines.iter().map(|line| line.width).fold(0.0, f32::max);
+ let words_h = (rows as f32 - 1.0) * pitch + ascent + descent;
+ if words_w <= 0.0 || lines.is_empty() {
// Nothing to paint: an empty, valid canvas.
- return Ok((canvas, (0, 0, 0, 0)));
+ return Ok((canvas, (0, 0, 0, 0), Vec::new()));
}
+ // The box the words sit in. Sized by the style where the style says,
+ // and by the words where it does not; never smaller than the words,
+ // so a word no line can hold is still whole. A sized axis is exact -
+ // a box asked for at 400 by 120 is 400 by 120, plate and all - so the
+ // plate's air is only added on an axis the words size themselves.
+ let max_h = (style.max_height as f32) * frame_h;
+ let box_w = if max_w > 0.0 {
+ max_w.max(words_w)
+ } else {
+ words_w
+ };
+ let box_h = if max_h > 0.0 {
+ max_h.max(words_h)
+ } else {
+ words_h
+ };
// The plate's padding is part of the block: it is what a monitor should
// outline, and what the title's neighbours should keep clear of.
let plate = colour(&style.background);
@@@ -589,10 -596,8 +641,10 @@@
let block_dx = (outer_left + outer_w / 2.0 - anchor_x).round() as i32;
// One path for all the words, placed. Each line is aligned within the
- // block's width and sits on its own baseline. A word's box rides the
- // box's width and sits on its own baseline.
++ // box's width and sits on its own baseline. A word's box rides the
+ // same indent and baseline, in reading order.
let mut words = PathBuilder::new();
+ let mut word_rects = Vec::new();
for (row, line) in lines.iter().enumerate() {
let Some(path) = &line.path else { continue };
let indent = match style.align {
diff --cc src/crates/concat/src/studio.rs
index 5b1b4a1b,08d06237..c0516f30
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@@ -191,80 -195,10 +197,15 @@@ pub struct LibraryView
}
/// Which library a shelf index names. The numbers are the panel's own -
-/// `PresetStack.shelf` - and the order the tabs sit in.
-pub const SHELF_KINDS: [PackageKind; 3] =
- [PackageKind::Filter, PackageKind::Effect, PackageKind::Audio];
+/// `PresetStack.shelf`, and `Library.view`'s shelf argument everywhere else -
+/// and the order the tabs sit in.
+pub const SHELF_KINDS: [PackageKind; 4] = [
+ PackageKind::Filter,
+ PackageKind::Effect,
+ PackageKind::Audio,
+ PackageKind::Transition,
+];
- /// The default Kokoro speaker: `af_heart`.
- const DEFAULT_VOICE: i32 = 3;
-
- /// The project sheet: the Details panel's Modify button, as a form.
- #[derive(Default)]
- pub struct ProjectSheet {
- pub open: bool,
- pub name: String,
- /// Row in `OUTPUTS`, or -1 for a frame the list does not carry.
- pub size: i32,
- /// Row in `START_RATES`.
- pub rate: usize,
- }
-
- /// The captions sheet: the tray's Captions tool, as a form and then as a
- /// progress report.
- #[derive(Default)]
- pub struct CaptionsSheet {
- pub open: bool,
- /// The clip being transcribed: the one selected when the sheet opened,
- /// when it had sound. None, and the sheet is a script instead.
- pub clip: Option<String>,
- /// The script's words.
- pub text: String,
- /// Row in the installed transcriber list.
- pub model: usize,
- /// 0 bottom, 1 centre, 2 top.
- pub placement: usize,
- /// 0 small, 1 medium, 2 large.
- pub size: usize,
- pub running: bool,
- pub progress: f32,
- /// Why the last run failed, when it did.
- pub message: String,
- }
-
- /// The speech sheet: a title's words, or any words, read aloud.
- #[derive(Default)]
- pub struct SpeechSheet {
- pub open: bool,
- /// The title the words came from, and where the sound lands. None
- /// reads a script of its own at the playhead.
- pub clip: Option<String>,
- pub text: String,
- /// Row in `Studio::speakers`.
- pub voice: usize,
- /// Row in the installed voice model list.
- pub model: usize,
- /// 0 slower, 1 natural, 2 faster.
- pub pace: usize,
- pub running: bool,
- pub progress: f32,
- pub message: String,
- }
-
- /// The paces the speech sheet offers, as the voice's rate multiplier.
- const PACES: [f32; 3] = [0.85, 1.0, 1.15];
- /// Where a caption sits, by the sheet's row: a frame-height fraction from
- /// the centre, positive down. Bottom, centre, top.
- const CAPTION_OFFSETS: [f64; 3] = [0.35, 0.0, -0.35];
- /// A caption's cap height by the sheet's row, as a fraction of the frame.
- const CAPTION_SIZES: [f64; 3] = [0.04, 0.05, 0.065];
- /// A rough speaking rate, for the estimate under the script.
- const CHARS_PER_SECOND: f32 = 14.0;
-
/// `name` under the home directory, as a path string; empty when there is
/// no home to speak of, and the form then asks for a folder outright.
pub(crate) fn home_folder(name: &str) -> String {
@@@ -1463,19 -1259,9 +1279,13 @@@ impl Studio
pending_stroke: None,
host,
};
- studio.settings.language = studio
- .languages
- .iter()
- .position(|language| Some(language.code.as_str()) == studio.prefs.locale.as_deref())
- .unwrap_or(0);
- studio.settings.playhead_stops = studio.prefs.playhead_stops_at_end;
- studio.settings.download_source = studio.download_source().0;
- studio.settings.download_base = studio.prefs.download_base.clone().unwrap_or_default();
- studio.apply_download_source();
- studio.apply_server();
+ studio.handle(crate::panes::Msg::Settings(
+ crate::panes::settings::SettingsMsg::Restore,
+ ));
++ // The effects shelf starts on "All", not whatever a stale default
++ // would leave it on.
+ studio.library[1].category = "All".to_owned();
+ studio.library[1].group = -1;
- studio.refresh_models();
studio
}
@@@ -3242,17 -2767,7 +2895,16 @@@
vec![id.to_owned()]
};
- self.flush_commit();
self.begin_echo();
+ if edge == 2 && self.selection.len() <= 1 {
+ if let Some(transition) = clip.transition_in.as_ref() {
+ self.gesture = Gesture::TransitionResize {
+ clip: id.to_owned(),
+ original_duration: transition.duration as f32,
+ };
+ return;
+ }
+ }
if edge >= 0 && self.selection.len() <= 1 {
self.gesture = Gesture::Trim {
clip: id.to_owned(),
@@@ -6165,35 -5017,44 +5199,48 @@@
timeline
.clips
.iter()
- .map(|clip| ClipData {
- id: clip.id.as_str().into(),
- name: clip.name.as_str().into(),
- kind: kind_of(clip),
- row: self.row_of(&clip.track_id),
- start: clip.start as f32,
- duration: clip.duration as f32,
- selected: self.selection.iter().any(|id| id == &clip.id),
- fx: clip.video_effects.iter().any(|effect| effect.enabled),
- transition_duration: clip
- .transition_in
- .as_ref()
- .map(|transition| transition.duration as f32)
- .unwrap_or(0.0),
- fade_in: clip.fade_in as f32,
- fade_out: clip.fade_out as f32,
- volume: clip.volume as f32,
- text_body: clip
- .text
- .as_ref()
- .map(|text| text.content.as_str())
- .unwrap_or_default()
- .into(),
- wave: if clip.kind == model::ClipKind::Audio {
- self.wave(clip)
- } else {
- SharedString::new()
- },
- strip: self.strip_of(clip),
+ .filter(|clip| {
+ crate::panes::timeline::TimelinePane::shows(
+ span,
+ clip.start as f32,
+ clip.duration as f32,
+ )
+ })
+ .map(|clip| {
+ let wave =
+ if matches!(clip.kind, model::ClipKind::Audio | model::ClipKind::Video) {
+ self.wave(clip)
+ } else {
+ Default::default()
+ };
+ ClipData {
+ id: clip.id.as_str().into(),
+ name: clip.name.as_str().into(),
+ kind: kind_of(clip),
+ row: self.row_of(&clip.track_id),
+ start: clip.start as f32,
+ duration: clip.duration as f32,
+ selected: self.selection.iter().any(|id| id == &clip.id),
+ fx: clip.video_effects.iter().any(|effect| effect.enabled),
- transition_in: clip.transition_in.is_some(),
++ transition_duration: clip
++ .transition_in
++ .as_ref()
++ .map(|transition| transition.duration as f32)
++ .unwrap_or(0.0),
+ fade_in: clip.fade_in as f32,
+ fade_out: clip.fade_out as f32,
+ volume: clip.volume as f32,
+ text_body: clip
+ .text
+ .as_ref()
+ .map(|text| text.content.as_str())
+ .unwrap_or_default()
+ .into(),
+ wave: wave.0,
+ wave_from: wave.1,
+ wave_span: wave.2,
+ strip: self.strip_of(clip),
+ }
})
.collect(),
);
diff --cc src/crates/concat/ui/timeline/model.slint
index 1db083df,b4803f4b..4b30944c
--- a/src/crates/concat/ui/timeline/model.slint
+++ b/src/crates/concat/ui/timeline/model.slint
@@@ -153,10 -155,11 +157,11 @@@ export enum TextAlignment { left, cente
export enum ClipField {
scale, offset-x, offset-y, rotation, stretch-x, stretch-y, opacity,
volume, speed, preserve-pitch, speed-curve, reverse, fade-in, fade-out,
- anim-in, anim-out, anim-combo, anim-in-duration, anim-out-duration,
+ anim-in, anim-out, anim-combo, anim-loop, anim-in-duration, anim-out-duration, anim-loop-duration,
flip-h, flip-v, blend, crop-left, crop-top, crop-right, crop-bottom,
font-size, font-weight, italic, text-opacity, align,
- stroke-width, shadow, line-height, tracking, text-width,
+ stroke-width, shadow, line-height, tracking, text-width, text-height,
+ stroke-opacity,
cutout-feather,
/// which of the media's audio tracks the clip plays, as a row of
/// Editor.audio-tracks