The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix: the document's maths - a ripple copies only what it moves, every command leaves a tidy clip, a late-starting stream reads from its first frame, Lighten and Darken weigh the layer in by its alpha
Jareer0a1f2fe380c0
diff --git a/src/crates/concat-media/fixtures/starts-at-1.4s.ts b/src/crates/concat-media/fixtures/starts-at-1.4s.tsnew file mode 100644index 00000000..794d4d0cBinary files /dev/null and b/src/crates/concat-media/fixtures/starts-at-1.4s.ts differdiff --git a/src/crates/concat-media/src/audio.rs b/src/crates/concat-media/src/audio.rsindex 2ca2a68f..71c446d2 100644--- a/src/crates/concat-media/src/audio.rs+++ b/src/crates/concat-media/src/audio.rs@@ -418,13 +418,18 @@ pub fn mix_to_file(clips: &[AudioClip], duration: f64, destination: &Path) -> Re})?;let stream = input.stream(stream_index).expect("just found");let time_base = stream.time_base();+ // Where the stream's clock starts: taken off what is read, put+ // back on the seek, so the in-point is measured from the sound's+ // own first sample. See `ffi::start_of`.+ let start = ffi::start_of(&stream).as_f64();let decoder = ffmpeg::codec::Context::from_parameters(stream.parameters()).and_then(|context| context.decoder().audio()).map_err(|error| ffi::fail("open decoder", path, error))?;// Near the in-point: the packet at or before it. The graph cuts the// rest, by the lead measured off the first frame below.if clip.source_start > 0.0 {- let target = (clip.source_start * f64::from(ffmpeg::sys::AV_TIME_BASE)) as i64;+ let target =+ ((clip.source_start + start) * f64::from(ffmpeg::sys::AV_TIME_BASE)) as i64;let _ = input.seek(target, ..=target);}let label = format!("{index}:a");@@ -448,7 +453,7 @@ pub fn mix_to_file(clips: &[AudioClip], duration: f64, destination: &Path) -> Relet landed = first.timestamp().and_then(|ticks| ffi::seconds(ticks, time_base))- .map_or(clip.source_start, |at| at.as_f64());+ .map_or(clip.source_start, |at| at.as_f64() - start);let lead = (clip.source_start - landed).max(0.0);leads.push(lead);// Our continuous PTS: sample count from the start of this clip.diff --git a/src/crates/concat-media/src/decode.rs b/src/crates/concat-media/src/decode.rsindex 75fa6019..c4c17c29 100644--- a/src/crates/concat-media/src/decode.rs+++ b/src/crates/concat-media/src/decode.rs@@ -491,6 +491,10 @@ pub struct Decoder {input: format::context::Input,stream: usize,time_base: ffmpeg::Rational,+ /// Where the stream's timestamps start; every timestamp read has this+ /// taken off and every seek has it put back, so the file reads from+ /// its own first frame. See `ffi::start_of`.+ start: Rational,decoder: decoder::Video,rotation: i64,options: DecodeOptions,@@ -545,6 +549,7 @@ impl Decoder {})?;let stream_index = stream.index();let time_base = stream.time_base();+ let start = ffi::start_of(&stream);let rotation = ffi::rotation(&stream);let coded = {let parameters = stream.parameters();@@ -598,6 +603,7 @@ impl Decoder {input,stream: stream_index,time_base,+ start,decoder,rotation,options: options.clone(),@@ -694,7 +700,7 @@ impl Decoder {Self::open_codec(&self.path, &stream, &self.options, None)?.0};let at = from.unwrap_or(self.origin);- let target = ffi::av_ticks(at);+ let target = ffi::av_ticks(at + self.start);self.input.seek(target, ..=target).map_err(|error| ffi::fail("seek", &self.path, error))?;@@ -712,7 +718,7 @@ impl Decoder {/// The container seek, and the state reset that goes with it.fn jump(&mut self, to: Rational) -> Result<()> {- let target = ffi::av_ticks(to);+ let target = ffi::av_ticks(to + self.start);self.input.seek(target, ..=target).map_err(|error| ffi::fail("seek", &self.path, error))?;@@ -742,7 +748,8 @@ impl Decoder {Ok(()) => {let pts = frame.timestamp()- .and_then(|ticks| ffi::seconds(ticks, self.time_base));+ .and_then(|ticks| ffi::seconds(ticks, self.time_base))+ .map(|pts| pts - self.start);if let Some((device, format)) = self.hardware {if frame.format() == format {frame = match hardware::download(&frame) {@@ -1046,6 +1053,26 @@ mod tests {use super::*;use crate::encode::RateMode;+ /// A stream that starts late - MPEG-TS starts at 1.4 s by default, and+ /// the MTS files cameras write likewise - is read from its own first+ /// frame, and a seek lands where the caller meant.+ #[test]+ fn a_stream_that_starts_late_is_read_from_its_first_frame() {+ let path = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/starts-at-1.4s.ts");+ let mut decoder = Decoder::open(path, &DecodeOptions::default()).expect("opens");+ let first = decoder.next_frame().expect("decodes").expect("a frame");+ assert_eq!((first.width(), first.height()), (64, 64));+ let at = decoder.position().expect("a position").as_f64();+ assert!(at < 0.05, "the first frame is at {at}, not 1.4 s in");+ decoder.seek(Rational::new(1, 2)).expect("seeks");+ let _ = decoder.next_frame().expect("decodes").expect("a frame");+ let at = decoder.position().expect("a position").as_f64();+ assert!(+ (0.45..0.65).contains(&at),+ "after a seek to 0.5 s the frame is at {at}"+ );+ }+#[test]fn options_build_up() {let options = DecodeOptions::default()diff --git a/src/crates/concat-media/src/ffi.rs b/src/crates/concat-media/src/ffi.rsindex e972938a..38c515d5 100644--- a/src/crates/concat-media/src/ffi.rs+++ b/src/crates/concat-media/src/ffi.rs@@ -156,6 +156,19 @@ pub(crate) fn seconds(ticks: i64, time_base: ffmpeg::Rational) -> Option<Rationa)}+/// Where a stream's timestamps start, in seconds: what its frames are+/// measured from. Zero for a stream that starts at zero or does not say;+/// 1.4 s for MPEG-TS by default and the MTS files cameras write, which+/// read as 1.4 s into the picture unless this is taken off every+/// timestamp and put back on every seek (audit 2026-09-23, #9).+pub(crate) fn start_of(stream: &ffmpeg::format::stream::Stream<'_>) -> Rational {+ let ticks = stream.start_time();+ if ticks == ffmpeg::sys::AV_NOPTS_VALUE || ticks <= 0 {+ return Rational::from_int(0);+ }+ seconds(ticks, stream.time_base()).unwrap_or_else(|| Rational::from_int(0))+}+/// Seconds in `AV_TIME_BASE` units, which is what container-level seeks take.pub(crate) fn av_ticks(seconds: Rational) -> i64 {let scaled = seconds * Rational::from_int(i64::from(ffmpeg::sys::AV_TIME_BASE));diff --git a/src/crates/concat-project/src/commands/clips.rs b/src/crates/concat-project/src/commands/clips.rsindex f0ae54bd..e24aa12d 100644--- a/src/crates/concat-project/src/commands/clips.rs+++ b/src/crates/concat-project/src/commands/clips.rs@@ -242,10 +242,10 @@ pub(super) fn apply(}};if ripple && applied && by != 0.0 {- for other in timeline.clips_mut() {- if other.id != clip_id && other.track_id == track_id && other.start >= behind {- other.start = (other.start + by).max(0.0);- }+ for other in timeline.clips_where(|other| {+ other.id != clip_id && other.track_id == track_id && other.start >= behind+ }) {+ other.start = (other.start + by).max(0.0);}}Ok(Outcome {@@ -465,10 +465,9 @@ pub(super) fn apply(// Ripple every later placement on this track (including the new// tail) so the freeze does not sit on top of the remainder.- for clip in timeline.clips_mut() {- if clip.track_id == track_id && clip.start >= time {- clip.start += hold;- }+ for clip in timeline.clips_where(|clip| clip.track_id == track_id && clip.start >= time)+ {+ clip.start += hold;}// The still is the source clip turned into a picture: cloning it@@ -593,7 +592,12 @@ const JOIN_EPSILON: f64 = 1e-6;/// zero./// https://github.com/jub0t/Concat/issues/106fn close_gaps(timeline: &mut Timeline, removed: &[(String, f64, f64)]) {- for clip in timeline.clips_mut() {+ let behind_a_span = |clip: &Clip| {+ removed+ .iter()+ .any(|(track, start, _)| *track == clip.track_id && *start < clip.start)+ };+ for clip in timeline.clips_where(behind_a_span) {let mut spans: Vec<(f64, f64)> = removed.iter().filter(|(track, start, _)| *track == clip.track_id && *start < clip.start)@@ -649,10 +653,7 @@ fn ripple_room_for(timeline: &mut Timeline, track_id: &str, start: f64, media: &if !overlaps {return;}- for clip in timeline- .clips_mut()- .filter(|clip| clip.track_id == track_id && clip.start >= start)- {+ for clip in timeline.clips_where(|clip| clip.track_id == track_id && clip.start >= start) {clip.start += duration;}}diff --git a/src/crates/concat-project/src/commands/media.rs b/src/crates/concat-project/src/commands/media.rsindex d0c7348b..621f7b8d 100644--- a/src/crates/concat-project/src/commands/media.rs+++ b/src/crates/concat-project/src/commands/media.rs@@ -101,9 +101,15 @@ pub(super) fn apply(// slot freeze-frames on its last frame downstream, which is the// renderer's existing behaviour for a trim past the media's end.// All timelines, like RemoveMedia: slots are not per-timeline.- for timeline in project.timelines.iter_mut().map(Arc::make_mut) {- for clip in timeline.clips_mut() {- if clip.media_id == media_id {+ for timeline in project.timelines.iter_mut() {+ // A timeline with no clip of this media stays the+ // snapshot's.+ if !timeline.clips.iter().any(|clip| clip.media_id == media_id) {+ continue;+ }+ let timeline = Arc::make_mut(timeline);+ for clip in timeline.clips_where(|clip| clip.media_id == media_id) {+ {clip.source_start = 0.0;clip.kind = kind;clip.name = item.name.clone();diff --git a/src/crates/concat-project/src/commands/mod.rs b/src/crates/concat-project/src/commands/mod.rsindex aa6b816a..9fc2bb64 100644--- a/src/crates/concat-project/src/commands/mod.rs+++ b/src/crates/concat-project/src/commands/mod.rs@@ -37,8 +37,8 @@ const DEFAULT_LAYER_DURATION: f64 = 5.0;/// Default hold for a freeze frame when the caller omits duration.const DEFAULT_FREEZE_DURATION: f64 = 1.0;pub(crate) use crate::model::ranges::{- MAX_OFFSET, MAX_SCALE, MAX_SPEED, MAX_STRETCH, MIN_CLIP_DURATION, MIN_SCALE, MIN_SPEED,- MIN_STRETCH, wrap_rotation,+ MAX_ANIMATION, MAX_OFFSET, MAX_SCALE, MAX_SPEED, MAX_STRETCH, MIN_ANIMATION, MIN_CLIP_DURATION,+ MIN_SCALE, MIN_SPEED, MIN_STRETCH, wrap_rotation,};/// Which end of a clip a trim drags. The two are not symmetric: seediff --git a/src/crates/concat-project/src/commands/properties.rs b/src/crates/concat-project/src/commands/properties.rsindex da902f0c..60c66cca 100644--- a/src/crates/concat-project/src/commands/properties.rs+++ b/src/crates/concat-project/src/commands/properties.rs@@ -155,7 +155,7 @@ pub(super) fn apply(.filter(|set| crate::animation::index_of(slot, &set.preset).is_some()).map(|set| ClipAnimation {preset: set.preset,- duration: set.duration.clamp(0.05, 60.0),+ duration: set.duration.clamp(MIN_ANIMATION, MAX_ANIMATION),});let field = match slot {AnimationSlot::In => &mut clip.animation_in,diff --git a/src/crates/concat-project/src/editor.rs b/src/crates/concat-project/src/editor.rsindex f731558e..64552764 100644--- a/src/crates/concat-project/src/editor.rs+++ b/src/crates/concat-project/src/editor.rs@@ -26,7 +26,7 @@ use serde_json::Value;use crate::commands::{Command, CommandError, IdMint, Outcome, apply};use crate::doc::{DocumentSettings, from_document, to_document};-use crate::model::Project;+use crate::model::{Clip, Project};const UNDO_DEPTH: usize = 200;@@ -118,6 +118,7 @@ impl Editor {if !outcome.applied {return Ok(outcome);}+ self.tidy_touched(&before);let continues = match (gesture, self.undo.back()) {(Some(gesture), Some(last)) => last.gesture.as_deref() == Some(gesture),_ => false,@@ -135,6 +136,40 @@ impl Editor {Ok(outcome)}+ /// Runs [`Clip::tidy`] over every clip the command wrote, so the clamps+ /// live in one place and a document is the same on screen as it is+ /// after a save and a reopen (audit 2026-09-23, #13). Only a clip+ /// whose `Arc` the command replaced is looked at: the rest are still+ /// the snapshot's, and were tidy already.+ fn tidy_touched(&mut self, before: &Project) {+ use std::collections::HashSet;+ use std::sync::Arc;+ let previous: std::collections::HashMap<&str, &Arc<crate::model::Timeline>> = before+ .timelines+ .iter()+ .map(|timeline| (timeline.id.as_str(), timeline))+ .collect();+ for timeline in &mut self.project.timelines {+ let old = previous.get(timeline.id.as_str()).copied();+ if old.is_some_and(|old| Arc::ptr_eq(old, timeline)) {+ continue;+ }+ let kept: HashSet<*const Clip> = old+ .map(|old| old.clips.iter().map(Arc::as_ptr).collect())+ .unwrap_or_default();+ let timeline = Arc::make_mut(timeline);+ for clip in &mut timeline.clips {+ if kept.contains(&Arc::as_ptr(clip)) {+ continue;+ }+ let tidied = (**clip).clone().tidy();+ if tidied != **clip {+ *clip = Arc::new(tidied);+ }+ }+ }+ }+/// Ends the gesture in progress, if any: the next command naming it/// starts a new step rather than folding into the last one. Called when/// the pointer goes up, or when enough time has passed that a seconddiff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rsindex 44b9eda7..0032af08 100644--- a/src/crates/concat-project/src/lib.rs+++ b/src/crates/concat-project/src/lib.rs@@ -1262,6 +1262,71 @@ mod tests {);}+ /// A ripple delete moves the clips behind the gap and copies those+ /// alone: the clip in front is still the snapshot's.+ #[test]+ fn a_ripple_copies_only_the_clips_it_moves() {+ let (mut editor, _, clip_id) = fixture();+ editor+ .apply(Command::SplitClips {+ clip_ids: vec![clip_id.clone()],+ time: 4.0,+ })+ .expect("splits");+ editor+ .apply(Command::SplitClips {+ clip_ids: vec![editor.project().active().clips[1].id.clone()],+ time: 7.0,+ })+ .expect("splits again");+ let head = std::sync::Arc::clone(&editor.project().active().clips[0]);+ let middle = editor.project().active().clips[1].id.clone();+ let last = std::sync::Arc::clone(&editor.project().active().clips[2]);+ editor+ .apply(Command::RemoveClips {+ clip_ids: vec![middle],+ ripple: true,+ })+ .expect("ripple deletes");+ let clips = &editor.project().active().clips;+ assert_eq!(clips.len(), 2);+ assert!(+ std::sync::Arc::ptr_eq(&head, &clips[0]),+ "the clip in front of the gap is still shared with the snapshot"+ );+ assert!(+ !std::sync::Arc::ptr_eq(&last, &clips[1]),+ "the moved clip was copied"+ );+ assert_eq!(clips[1].start, 4.0);+ }++ /// Every command's result goes through `Clip::tidy`: a key set past the+ /// field's range comes out clamped, the way the field itself would.+ #[test]+ fn a_command_leaves_a_tidy_clip_behind() {+ let (mut editor, _, clip_id) = fixture();+ editor+ .apply(Command::SetClipKey {+ clip_id: clip_id.clone(),+ property: crate::model::KeyProperty::OffsetX,+ at: 0.5,+ value: 99.0,+ ease: Default::default(),+ })+ .expect("sets a key");+ let clip = &editor.project().active().clips[0];+ let key = clip+ .keys_on(crate::model::KeyProperty::OffsetX)+ .next()+ .expect("the key");+ assert_eq!(+ key.value,+ crate::model::ranges::MAX_OFFSET,+ "clamped like the field"+ );+ }+#[test]fn a_command_copies_only_what_it_writes() {let (mut editor, _, clip_id) = fixture();diff --git a/src/crates/concat-project/src/model.rs b/src/crates/concat-project/src/model.rsindex f5d4c6dd..bf6ae6d9 100644--- a/src/crates/concat-project/src/model.rs+++ b/src/crates/concat-project/src/model.rs@@ -636,7 +636,8 @@ impl ClipAnimation {return None;}self.duration = if self.duration.is_finite() {- self.duration.max(0.0)+ self.duration+ .clamp(ranges::MIN_ANIMATION, ranges::MAX_ANIMATION)} else {default_animation_duration()};@@ -1103,6 +1104,10 @@ pub mod ranges {pub const MAX_OFFSET: f64 = 3.0;/// A transition can be no shorter than this, in seconds.pub const MIN_TRANSITION: f64 = 0.1;+ /// The shortest a clip animation runs, in seconds.+ pub const MIN_ANIMATION: f64 = 0.05;+ /// The longest a clip animation runs, in seconds.+ pub const MAX_ANIMATION: f64 = 60.0;/// A rotation kept in (-180, 180] so a full drag never accumulates/// turns.@@ -1401,6 +1406,20 @@ impl Clip {}self.keys.retain(|key| (0.0..=1.0).contains(&key.at) && key.value.is_finite());+ // A key holds what the field itself may hold. Rotation is the one+ // exception: a key past a full turn is a spin, and wrapping it+ // would take the spin away.+ for key in &mut self.keys {+ key.value = match key.property {+ KeyProperty::Scale => key.value.clamp(MIN_SCALE, MAX_SCALE),+ KeyProperty::Opacity => key.value.clamp(0.0, 1.0),+ KeyProperty::Volume => key.value.max(0.0),+ KeyProperty::OffsetX | KeyProperty::OffsetY => {+ key.value.clamp(-MAX_OFFSET, MAX_OFFSET)+ }+ KeyProperty::Rotation => key.value,+ };+ }self.sort_keys();for chain in [&mut self.filters, &mut self.video_effects] {for entry in chain.iter_mut() {@@ -1961,10 +1980,18 @@ impl Timeline {Arc::make_mut(&mut self.clips[index])}- /// Every clip, mutable, each copied out of any snapshot sharing it as- /// it is reached.- pub fn clips_mut(&mut self) -> impl Iterator<Item = &mut Clip> {- self.clips.iter_mut().map(Arc::make_mut)+ /// The clips `keep` picks, mutable, each copied out of any snapshot+ /// sharing it only once picked: a ripple that moves ten clips of five+ /// thousand copies ten, and the undo step stays the size of the edit+ /// (audit 2026-09-23, #8).+ pub fn clips_where(+ &mut self,+ mut keep: impl FnMut(&Clip) -> bool,+ ) -> impl Iterator<Item = &mut Clip> {+ self.clips+ .iter_mut()+ .filter(move |clip| keep(clip))+ .map(Arc::make_mut)}/// The track with this id, or None if it was removed.diff --git a/src/crates/concat-render/src/compositor.rs b/src/crates/concat-render/src/compositor.rsindex 44139eaa..b4bd24a8 100644--- a/src/crates/concat-render/src/compositor.rs+++ b/src/crates/concat-render/src/compositor.rs@@ -235,9 +235,8 @@ impl Weighing<'_> {}for channel in 0..3 {let colour = self.shading.colour(channel, sample[channel] / 255.0) * 255.0;- let over = colour * alpha;let ground = f32::from(under[channel]);- under[channel] = mix(self.blend, over, ground, alpha)+ under[channel] = mix(self.blend, colour, ground, alpha).round().clamp(0.0, 255.0) as u8;}@@ -245,18 +244,25 @@ impl Weighing<'_> {}}-/// One channel of `blend`: `over` is the layer's colour already times its-/// alpha, `under` the ground, both in `0..=255`. The formulas are the GPU's-/// fixed-function ones over premultiplied colour, so the two paths agree.+/// One channel of `blend`: `colour` is the layer's straight colour, `under`+/// the ground, both in `0..=255`, `alpha` how much of the layer is there.+/// Normal, Multiply, Screen and Add are the GPU's fixed-function blends+/// over premultiplied colour, spelled the same way so the two paths agree.+/// Lighten and Darken weigh the lighter (darker) of the two in by the+/// layer's alpha - a white layer at 30 % over mid grey lightens it 30 % of+/// the way to white, and darkens it not at all - which no fixed-function+/// blend expresses; the GPU samples a copy of the ground for these two+/// (audit 2026-09-23, #10).#[inline]-fn mix(blend: Blend, over: f32, under: f32, alpha: f32) -> f32 {+fn mix(blend: Blend, colour: f32, under: f32, alpha: f32) -> f32 {+ let over = colour * alpha;match blend {Blend::Normal => over + under * (1.0 - alpha),Blend::Multiply => over * under / 255.0 + under * (1.0 - alpha),Blend::Screen => over * (1.0 - under / 255.0) + under,Blend::Add => over + under,- Blend::Lighten => over.max(under),- Blend::Darken => over.min(under),+ Blend::Lighten => colour.max(under) * alpha + under * (1.0 - alpha),+ Blend::Darken => colour.min(under) * alpha + under * (1.0 - alpha),}}@@ -409,6 +415,20 @@ fn blend_aligned(output: &mut Frame, picture: &Frame, geometry: &Geometry, weigh#[cfg(test)]mod tests {+ #[test]+ fn lighten_and_darken_weigh_the_layer_in_by_its_alpha() {+ use super::{Blend, mix};+ // White at 30 % over mid grey: 30 % of the way to white, no darker.+ assert!((mix(Blend::Lighten, 255.0, 128.0, 0.3) - 166.1).abs() < 0.01);+ assert_eq!(mix(Blend::Darken, 255.0, 128.0, 0.3), 128.0);+ // Black at 30 % over mid grey: 30 % of the way to black, no lighter.+ assert!((mix(Blend::Darken, 0.0, 128.0, 0.3) - 89.6).abs() < 0.01);+ assert_eq!(mix(Blend::Lighten, 0.0, 128.0, 0.3), 128.0);+ // At full alpha the pair are the plain max and min.+ assert_eq!(mix(Blend::Lighten, 40.0, 128.0, 1.0), 128.0);+ assert_eq!(mix(Blend::Darken, 40.0, 128.0, 1.0), 40.0);+ }+use std::sync::Arc;use concat_core::time::FrameRate;diff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rsindex 13fb3be7..4b94ccad 100644--- a/src/crates/concat-render/src/gpu.rs+++ b/src/crates/concat-render/src/gpu.rs@@ -108,20 +108,53 @@ fn vs_main(in: VsIn) -> VsOut {@group(0) @binding(1) var layer_sampler: sampler;@group(1) @binding(0) var mask_texture: texture_2d<f32>;@group(1) @binding(1) var mask_sampler: sampler;--@fragment-fn fs_main(in: VsOut) -> @location(0) vec4<f32> {+// The ground as it stood before this layer, for the two blends that need+// to see it; bound only for their pipelines.+@group(2) @binding(0) var ground_texture: texture_2d<f32>;+@group(2) @binding(1) var ground_sampler: sampler;++// The layer's straight colour and its alpha at this fragment, before any+// blend: the same lines the CPU reference computes.+fn shade(in: VsOut) -> vec4<f32> {let colour = textureSample(layer_texture, layer_sampler, in.uv);let mask = textureSample(mask_texture, mask_sampler, in.pic);// The wipes: a pixel past the moving edge is not drawn at all.let kept = select(0.0, 1.0, in.pic.x < in.edges.x && in.pic.x >= in.edges.y);let alpha = colour.a * in.opacity * mask.a * kept;- // The fades: the colour scaled and offset, the same line the CPU- // reference computes.+ // The fades: the colour scaled and offset.let shaded = colour.rgb * in.scale + in.offset;+ return vec4<f32>(shaded, alpha);+}++@fragment+fn fs_main(in: VsOut) -> @location(0) vec4<f32> {+ let s = shade(in);// Premultiplied output; the pipeline blends ONE / ONE_MINUS_SRC_ALPHA,// which together is the same source-over the CPU path computes.- return vec4<f32>(shaded * alpha, alpha);+ return vec4<f32>(s.rgb * s.a, s.a);+}++fn ground_at(position: vec4<f32>) -> vec3<f32> {+ let size = vec2<f32>(textureDimensions(ground_texture));+ return textureSample(ground_texture, ground_sampler, position.xy / size).rgb;+}++// Lighten and Darken weigh the lighter (darker) of the layer and the+// ground in by the layer's alpha - a white layer at 30 % over mid grey+// lightens it 30 % of the way to white - which no fixed-function blend+// expresses. The ground is a copy taken just before this draw; the+// result is premultiplied and blended source-over, so what lands is+// max(colour, ground) * alpha + ground * (1 - alpha), the CPU's own line.+@fragment+fn fs_lighten(in: VsOut) -> @location(0) vec4<f32> {+ let s = shade(in);+ return vec4<f32>(max(s.rgb, ground_at(in.position)) * s.a, s.a);+}++@fragment+fn fs_darken(in: VsOut) -> @location(0) vec4<f32> {+ let s = shade(in);+ return vec4<f32>(min(s.rgb, ground_at(in.position)) * s.a, s.a);}"#;@@ -142,6 +175,10 @@ struct Draw {texture: usize,blend: Blend,mask: (u32, u32, usize),+ /// For a Lighten or Darken layer: the pooled texture, at the output+ /// size, that the ground is copied into just before the draw, for its+ /// fragment stage to sample. See `fs_lighten` in [`SHADER`].+ ground: Option<usize>,}/// A cached layer texture and its bind group, reusable for any layer of the@@ -183,6 +220,9 @@ pub struct WgpuCompositor {queue: wgpu::Queue,/// One pipeline per blend mode, indexed as `Blend::ALL` is.pipelines: Vec<wgpu::RenderPipeline>,+ /// Lighten, then Darken: the two blends that sample the ground, drawn+ /// with a third bind group and plain source-over.+ ground_pipelines: [wgpu::RenderPipeline; 2],bind_layout: wgpu::BindGroupLayout,/// Group 1 of a shader pass: the frame block and the package's params.uniform_layout: wgpu::BindGroupLayout,@@ -460,6 +500,49 @@ impl WgpuCompositor {}).collect();+ // Lighten and Darken: source-over of a fragment that has already+ // taken the max or min against a copy of the ground, bound as a+ // third group of the same shape as the layer and its mask.+ let ground_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ label: Some("concat compositor ground"),+ bind_group_layouts: &[Some(&bind_layout), Some(&bind_layout), Some(&bind_layout)],+ immediate_size: 0,+ });+ let ground_pipelines = ["fs_lighten", "fs_darken"].map(|entry| {+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ label: Some(entry),+ layout: Some(&ground_layout),+ vertex: wgpu::VertexState {+ module: &shader,+ entry_point: Some("vs_main"),+ compilation_options: Default::default(),+ buffers: std::slice::from_ref(&vertex_layout),+ },+ fragment: Some(wgpu::FragmentState {+ module: &shader,+ entry_point: Some(entry),+ compilation_options: Default::default(),+ targets: &[Some(wgpu::ColorTargetState {+ format: wgpu::TextureFormat::Rgba8Unorm,+ blend: Some(wgpu::BlendState {+ color: wgpu::BlendComponent {+ src_factor: wgpu::BlendFactor::One,+ dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,+ operation: wgpu::BlendOperation::Add,+ },+ alpha,+ }),+ write_mask: wgpu::ColorWrites::ALL,+ })],+ }),+ primitive: wgpu::PrimitiveState::default(),+ depth_stencil: None,+ multisample: wgpu::MultisampleState::default(),+ multiview_mask: None,+ cache: None,+ })+ });+let sampler = device.create_sampler(&wgpu::SamplerDescriptor {label: Some("concat layer"),address_mode_u: wgpu::AddressMode::ClampToEdge,@@ -480,6 +563,7 @@ impl WgpuCompositor {device,queue,pipelines,+ ground_pipelines,bind_layout,uniform_layout,lut_layout,@@ -676,11 +760,14 @@ impl WgpuCompositor {)};let mask = self.mask_of(layer.mask.as_deref());+ let ground = matches!(layer.blend, Blend::Lighten | Blend::Darken)+ .then(|| self.claim(plan.width, plan.height));draws.push(Draw {size,texture,blend: layer.blend,mask,+ ground,});vertices.extend_from_slice(&Self::quad(&geometry,@@ -723,6 +810,7 @@ impl WgpuCompositor {size: source_size,texture: source,blend: Blend::Normal,+ ground: None,mask,}];let corner = |x: f32, y: f32, u: f32, v: f32| {@@ -764,6 +852,7 @@ impl WgpuCompositor {size: (width, height),texture: index,blend: Blend::Normal,+ ground: None,mask,});let corner = |x: f32, y: f32, u: f32, v: f32| Vertex {@@ -817,10 +906,9 @@ impl WgpuCompositor {) -> usize {let target = self.claim(width, height);self.write_vertices(vertices);- let view = self.pool[&(width, height)][target]- .texture- .create_view(&wgpu::TextureViewDescriptor::default());- let encoder = self.encode(&view, draws, clear);+ let texture = &self.pool[&(width, height)][target].texture;+ let view = texture.create_view(&wgpu::TextureViewDescriptor::default());+ let encoder = self.encode(&view, texture, draws, clear);self.queue.submit([encoder.finish()]);target}@@ -839,7 +927,7 @@ impl WgpuCompositor {self.write_vertices(&vertices);let texture = self.presentable(plan.width, plan.height);let view = texture.create_view(&wgpu::TextureViewDescriptor::default());- let encoder = self.encode(&view, &draws, wgpu::Color::BLACK);+ let encoder = self.encode(&view, &texture, &draws, wgpu::Color::BLACK);self.queue.submit([encoder.finish()]);self.retire();Some(texture)@@ -887,7 +975,7 @@ impl WgpuCompositor {self.write_vertices(&vertices);let texture = self.presentable(side, side);let view = texture.create_view(&wgpu::TextureViewDescriptor::default());- let encoder = self.encode(&view, &draws, wgpu::Color::BLACK);+ let encoder = self.encode(&view, &texture, &draws, wgpu::Color::BLACK);self.queue.submit([encoder.finish()]);let waited = self.device.poll(wgpu::PollType::Wait {submission_index: None,@@ -909,11 +997,16 @@ impl WgpuCompositor {}}- /// The render pass: every draw over `clear` into `view`. Returns the- /// encoder so the caller can add a readback before submitting.+ /// The render passes: every draw over `clear` into `view`, which is a+ /// view of `target`. One pass, except that a Lighten or Darken layer+ /// needs the ground as it stands: the pass ends, the target is copied+ /// into the draw's ground texture, and a new pass loads what is there+ /// and carries on. Returns the encoder so the caller can add a readback+ /// before submitting.fn encode(&self,view: &wgpu::TextureView,+ target: &wgpu::Texture,draws: &[Draw],clear: wgpu::Color,) -> wgpu::CommandEncoder {@@ -922,40 +1015,84 @@ impl WgpuCompositor {.create_command_encoder(&wgpu::CommandEncoderDescriptor {label: Some("concat composite"),});- {- let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {- label: Some("concat composite"),- color_attachments: &[Some(wgpu::RenderPassColorAttachment {- view,- depth_slice: None,- resolve_target: None,- ops: wgpu::Operations {- load: wgpu::LoadOp::Clear(clear),- store: wgpu::StoreOp::Store,- },- })],- depth_stencil_attachment: None,- timestamp_writes: None,- occlusion_query_set: None,- multiview_mask: None,- });- pass.set_vertex_buffer(0, self.vertices.slice(..));- for (index, draw) in draws.iter().enumerate() {- let which = Blend::ALL- .iter()- .position(|mode| *mode == draw.blend)- .unwrap_or(0);- pass.set_pipeline(&self.pipelines[which]);- pass.set_bind_group(0, &self.pool[&draw.size][draw.texture].bind_group, &[]);- let (mask_w, mask_h, mask) = draw.mask;- pass.set_bind_group(1, &self.pool[&(mask_w, mask_h)][mask].bind_group, &[]);- let first = (index * 6) as u32;- pass.draw(first..first + 6, 0..1);- }+ let size = (target.width(), target.height());+ let mut load = wgpu::LoadOp::Clear(clear);+ let mut pending: Vec<(usize, &Draw)> = Vec::new();+ for (index, draw) in draws.iter().enumerate() {+ let Some(ground) = draw.ground else {+ pending.push((index, draw));+ continue;+ };+ self.draw_segment(&mut encoder, view, size, load, &pending);+ pending.clear();+ load = wgpu::LoadOp::Load;+ encoder.copy_texture_to_texture(+ target.as_image_copy(),+ self.pool[&size][ground].texture.as_image_copy(),+ wgpu::Extent3d {+ width: size.0,+ height: size.1,+ depth_or_array_layers: 1,+ },+ );+ self.draw_segment(&mut encoder, view, size, load, &[(index, draw)]);+ }+ if !pending.is_empty() || matches!(load, wgpu::LoadOp::Clear(_)) {+ self.draw_segment(&mut encoder, view, size, load, &pending);}encoder}+ /// One render pass over `view`, `size` pixels: these draws, in order,+ /// each with the pipeline its blend wants.+ fn draw_segment(+ &self,+ encoder: &mut wgpu::CommandEncoder,+ view: &wgpu::TextureView,+ size: (u32, u32),+ load: wgpu::LoadOp<wgpu::Color>,+ draws: &[(usize, &Draw)],+ ) {+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {+ label: Some("concat composite"),+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {+ view,+ depth_slice: None,+ resolve_target: None,+ ops: wgpu::Operations {+ load,+ store: wgpu::StoreOp::Store,+ },+ })],+ depth_stencil_attachment: None,+ timestamp_writes: None,+ occlusion_query_set: None,+ multiview_mask: None,+ });+ pass.set_vertex_buffer(0, self.vertices.slice(..));+ for &(index, draw) in draws {+ match draw.ground {+ Some(ground) => {+ let which = usize::from(draw.blend == Blend::Darken);+ pass.set_pipeline(&self.ground_pipelines[which]);+ pass.set_bind_group(2, &self.pool[&size][ground].bind_group, &[]);+ }+ None => {+ let which = Blend::ALL+ .iter()+ .position(|mode| *mode == draw.blend)+ .unwrap_or(0);+ pass.set_pipeline(&self.pipelines[which]);+ }+ }+ pass.set_bind_group(0, &self.pool[&draw.size][draw.texture].bind_group, &[]);+ let (mask_w, mask_h, mask) = draw.mask;+ pass.set_bind_group(1, &self.pool[&(mask_w, mask_h)][mask].bind_group, &[]);+ let first = (index * 6) as u32;+ pass.draw(first..first + 6, 0..1);+ }+ }+/// Retires texture sizes the timeline has moved past. 300 unclaimed/// composites (ten seconds of 30fps export) says a size is gone for/// good, not just between two clips of it.@@ -1810,7 +1947,7 @@ impl WgpuCompositor {let view = target.texture.create_view(&wgpu::TextureViewDescriptor::default());- let mut encoder = self.encode(&view, draws, wgpu::Color::BLACK);+ let mut encoder = self.encode(&view, &target.texture, draws, wgpu::Color::BLACK);encoder.copy_texture_to_buffer(wgpu::TexelCopyTextureInfo {texture: &target.texture,diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rsindex 199a00f1..0422106f 100644--- a/src/crates/concat-render/src/gpu/tests.rs+++ b/src/crates/concat-render/src/gpu/tests.rs@@ -264,6 +264,19 @@ fn every_kind_of_layer_matches_the_cpu_reference() {0.99,);+ // Lighten and Darken at partial opacity: the two blends the GPU draws+ // over a copy of the ground, held to the CPU's own line.+ for (name, blend) in [("lightened", Blend::Lighten), ("darkened", Blend::Darken)] {+ let mut over = layer(solid(64, 48, [200, 60, 140, 255]));+ over.opacity = 0.3;+ over.blend = blend;+ assert_parity(+ name,+ &plan(64, 48, vec![layer(gradient(64, 48)), over]),+ 0.99,+ );+ }+let mut masked = layer(gradient(64, 64));let mut mask = Frame::transparent(32, 32);for y in 0..32u32 {