Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge main (M12.2 GPU compositor) into perf-bpc

Brandon Thomas81c9a16592d3
diff --cc crates/render/src/lib.rs
index 1b65a0e24,aa9c763fd..14a81575f
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@@ -391,17 -464,9 +469,10 @@@ impl<'a> Renderer<'a>
}
/// Draw a comp's layers (bottom to top) into `canvas` (blending space).
- fn draw_comp(&self, ctx: &EvalCtx<'a>, canvas: &mut Image) {
+ /// `blank`: the canvas is fully transparent (lets the first layer skip re-quantising).
+ fn draw_comp(&self, ctx: &EvalCtx<'a>, canvas: &mut Image, mut blank: bool) {
- let (comp, t) = (ctx.comp, ctx.time);
- let any_solo = comp.layers.iter().any(|l| l.switches.solo && l.source.is_av() && l.is_active_at(t));
// Bottom-to-top, with runs of consecutive 3D layers depth-sorted (farthest first).
- let visible: Vec<&Layer> = comp
- .layers
- .iter()
- .rev()
- .filter(|l| l.is_active_at(t) && l.has_video() && (!any_solo || l.switches.solo) && (self.opts.guides || !l.switches.guide))
- .collect();
+ let visible = self.visible_layers(ctx);
let mut i = 0;
while i < visible.len() {
if visible[i].is_3d() {
@@@ -799,74 -888,41 +901,87 @@@
if self.opts.draft { 4 } else { ctx.comp.motion_blur_samples.clamp(2, 64) as usize }
} else {
1
- };
+ }
+ }
+
+ /// Canvas pixels that compositing `buf` for `layer` can change: the transformed buffer's
+ /// bounds plus the resampling footprint. Stencil/silhouette modes, motion blur and
+ /// perspective fall back to the whole canvas.
+ ///
+ /// A quantised buffer placed pixel-exactly (integer translation, Normal, 100%) over a blank
+ /// canvas leaves exactly its own (quantised) pixels: nothing to re-quantise.
+ fn footprint(&self, ctx: &EvalCtx, layer: &Layer, buf: &Buf, opacity: f32, blank: bool) -> Region {
+ if layer.blend_mode.is_stencil() || self.mb_samples(ctx, layer) > 1 {
+ return Region::Full;
+ }
+ let m = self.buf_matrix(ctx, layer, buf);
+ if !m.is_affine() {
+ return Region::Full;
+ }
+ let matte = layer.track_matte.is_some_and(|tm| tm.layer != layer.id && ctx.comp.layer(tm.layer).is_some());
+ let k = &m.0;
+ let integral = |v: f64| v == v.round();
+ if blank
+ && layer.blend_mode == BlendMode::Normal
+ && opacity == 1.0
+ && !matte
+ && !layer.preserve_transparency
+ && self.pipe.to_blend().is_none()
+ && k[0][0] == 1.0
+ && k[1][1] == 1.0
+ && k[0][1] == 0.0
+ && k[1][0] == 0.0
+ && integral(k[0][2])
+ && integral(k[1][2])
+ {
+ return Region::Empty;
+ }
+ let (w, h) = (buf.img.width as f64, buf.img.height as f64);
+ let c = [(0.0, 0.0), (w, 0.0), (0.0, h), (w, h)].map(|(x, y)| m.apply(vec2(x, y)));
+ let (x0, x1) = (c.iter().map(|p| p.x).fold(f64::INFINITY, f64::min), c.iter().map(|p| p.x).fold(f64::NEG_INFINITY, f64::max));
+ let (y0, y1) = (c.iter().map(|p| p.y).fold(f64::INFINITY, f64::min), c.iter().map(|p| p.y).fold(f64::NEG_INFINITY, f64::max));
+ color::PxRect::covering(x0, y0, x1, y1, 3.0).map_or(Region::Full, Region::Rect)
+ }
+
- /// Draw a processed layer into `target` (transform, motion blur) with `mode`/`opacity`.
- fn place(&self, ctx: &EvalCtx, layer: &Layer, buf: &Buf, target: &mut Image, mode: BlendMode, opacity: f32) {
++ /// How a processed layer buffer lands in the output: one buffer → output matrix, or one per
++ /// motion-blur sub-sample (averaged), with the layer's sampling filter and dissolve seed.
++ pub fn placement(&self, ctx: &EvalCtx, layer: &Layer, buf: &Buf) -> Placement {
+ let samples = self.mb_samples(ctx, layer);
- let opts = WarpOpts { sampling: self.sampling(layer), opacity, mode, seed: layer.id.0 as u32, clip: None };
+ let (sampling, seed) = (self.sampling(layer), layer.id.0 as u32);
if samples <= 1 {
- let m = self.buf_matrix(ctx, layer, buf);
- composite_warp(target, &buf.img, &m, &opts);
- return;
+ return Placement { matrices: vec![self.buf_matrix(ctx, layer, buf)], sampling, seed };
}
let fd = ctx.comp.frame_duration().seconds();
let angle = ctx.comp.shutter_angle / 360.0;
let phase = ctx.comp.shutter_phase / 360.0;
+ let matrices = (0..samples)
+ .map(|i| {
+ let f = phase + angle * i as f64 / (samples - 1) as f64;
+ self.buf_matrix(&ctx.at(ctx.time + Tick::from_seconds_f64(f * fd)), layer, buf)
+ })
+ .collect();
+ Placement { matrices, sampling, seed }
+ }
+
+ /// Draw a processed layer into `target` (transform, motion blur) with `mode`/`opacity`.
+ fn place(&self, ctx: &EvalCtx, layer: &Layer, buf: &Buf, target: &mut Image, mode: BlendMode, opacity: f32) {
+ let pl = self.placement(ctx, layer, buf);
+ let opts = WarpOpts { sampling: pl.sampling, opacity, mode, seed: pl.seed, clip: None };
+ if let [m] = pl.matrices.as_slice() {
+ composite_warp(target, &buf.img, m, &opts);
+ return;
+ }
// Sub-samples sum straight into one buffer (each warp is row-parallel).
let mut acc = Image::new(target.width, target.height);
- let k = 1.0 / samples as f32;
- for i in 0..samples {
- let f = phase + angle * i as f64 / (samples - 1) as f64;
- let sub = ctx.at(ctx.time + Tick::from_seconds_f64(f * fd));
- let m = self.buf_matrix(&sub, layer, buf);
- effectcraft_raster::accumulate_warp(&mut acc, &buf.img, &m, opts.sampling, k);
- }
- target.blend_from(&acc, mode, opacity, layer.id.0 as u32);
+ let k = 1.0 / pl.matrices.len() as f32;
+ for m in &pl.matrices {
+ effectcraft_raster::accumulate_warp(&mut acc, &buf.img, m, opts.sampling, k);
+ }
+ target.blend_from(&acc, mode, opacity, pl.seed);
}
- fn draw_layer(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
+ /// Draw a 2D layer into `canvas`; returns the region it may have changed.
+ fn draw_layer(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image, blank: bool) -> Region {
let opacity = ctx.opacity(layer) as f32 * self.opacity_mul;
if layer.switches.quality == Quality::Wireframe && !layer.switches.adjustment {
self.draw_wireframe(ctx, layer, canvas);
@@@ -1026,7 -1080,87 +1142,87 @@@ pub struct StyledLayer
/// Content cache key.
ckey: Option<u64>,
/// No layer styles: composite `body` the usual way.
- plain: bool,
+ pub plain: bool,
+ }
+
+ /// Where a layer buffer lands (see [`Renderer::placement`]).
+ #[derive(Clone, Debug)]
+ pub struct Placement {
+ /// Buffer pixel → output pixel; several = motion-blur sub-samples (equal weights).
+ pub matrices: Vec<Mat3>,
+ pub sampling: effectcraft_raster::Sampling,
+ /// Dissolve noise seed.
+ pub seed: u32,
+ }
+
+ /// Hooks for accelerated compositors (`effectcraft-gpu`): the pieces of the CPU walk they reuse
+ /// or fall back to. The CPU compositor itself is [`Renderer::comp_frame_cpu`].
+ impl<'a> Renderer<'a> {
+ /// The project's colour pipeline.
+ pub fn pipe(&self) -> color::Pipe {
+ self.pipe
+ }
+
+ /// Nesting depth (0 = the top-level comp).
+ pub fn depth(&self) -> usize {
+ self.depth
+ }
+
+ /// Evaluation context of comp `comp_id` at comp time `t`.
+ pub fn eval_ctx(&self, comp_id: ItemId, t: Tick) -> Option<EvalCtx<'a>> {
+ Some(self.ctx(comp_id, self.project.comp(comp_id)?, t))
+ }
+
+ /// The layers drawn at the context time, bottom to top (solo, guides and visibility
+ /// applied).
+ pub fn visible_layers(&self, ctx: &EvalCtx<'a>) -> Vec<&'a Layer> {
+ let (comp, t) = (ctx.comp, ctx.time);
+ let any_solo = comp.layers.iter().any(|l| l.switches.solo && l.source.is_av() && l.is_active_at(t));
+ comp.layers
+ .iter()
+ .rev()
+ .filter(|l| l.is_active_at(t) && l.has_video() && (!any_solo || l.switches.solo) && (self.opts.guides || !l.switches.guide))
+ .collect()
+ }
+
+ /// Draw a run of consecutive 3D layers into `canvas` (CPU).
+ pub fn draw_3d_run(&self, ctx: &EvalCtx<'a>, run: &[&Layer], canvas: &mut Image) {
+ three_d::compose::draw_run(self, ctx, run, canvas);
+ }
+
+ /// Draw one 2D layer into `canvas` on the CPU (any kind: adjustment, wireframe, styled…).
+ pub fn draw_layer_cpu(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
- self.draw_layer(ctx, layer, canvas);
++ self.draw_layer(ctx, layer, canvas, false);
+ }
+
+ /// Draw a collapsed precomp layer into `canvas` on the CPU.
+ pub fn draw_collapsed_cpu(&self, ctx: &EvalCtx<'a>, layer: &Layer, item: ItemId, canvas: &mut Image) {
- self.draw_collapsed(ctx, layer, item, canvas);
++ self.draw_collapsed(ctx, layer, item, canvas, false);
+ }
+
+ /// The layer's finished buffers (styles included) in the blending space.
+ pub fn styled_layer(&self, ctx: &EvalCtx, layer: &Layer) -> Option<StyledLayer> {
+ let mut timing = self.profile.map(|_| LayerTiming { depth: self.depth, layer: layer.name.clone(), ..Default::default() });
+ let t0 = web_time::Instant::now();
+ let st = self.styled(ctx, layer, timing.as_mut()).map(|st| self.styled_to_blend(st));
+ if let (Some(prof), Some(mut timing)) = (self.profile, timing) {
+ timing.process_ms = t0.elapsed().as_secs_f64() * 1e3;
+ if let Ok(mut v) = prof.lock() {
+ v.push(timing);
+ }
+ }
+ st
+ }
+
+ /// Opacity the layer composites with (its Opacity × collapsed precomps' opacity).
+ pub fn layer_opacity(&self, ctx: &EvalCtx, layer: &Layer) -> f32 {
+ ctx.opacity(layer) as f32 * self.opacity_mul
+ }
+
+ /// The layer's track matte layer and kind, if it has a valid one.
+ pub fn track_matte<'c>(&self, ctx: &EvalCtx<'c>, layer: &Layer) -> Option<(&'c Layer, MatteKind)> {
+ layer.track_matte.and_then(|tm| ctx.comp.layer(tm.layer).filter(|m| m.id != layer.id).map(|m| (m, tm.kind)))
+ }
}
/// The layer's frame blending mode, if the comp's Enable Frame Blending switch is on.