Skip to content

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

Render: effect stacks keep the cover's pixel-grid phase, only known-margin stacks skip the cover, and displacement and Repeat Edge Pixels get the room the cover gave them (#1205)

Matej Kozlovský9a202b133ae1
diff --git a/crates/effects/src/distort2.rs b/crates/effects/src/distort2.rs
index 10ec59735..4f09b95e2 100644
--- a/crates/effects/src/distort2.rs
+++ b/crates/effects/src/distort2.rs
@@ -93,6 +93,9 @@ fn turbulent_displace(ctx: &EffectCtx, mut b: Buf) -> Buf {
let size = (ctx.params.f("size") * b.scale).max(1.0);
if ctx.params.b("resizeLayer") && !ctx.adjustment {
b.pad(amount.abs().ceil() as u32 + 1);
+ } else {
+ // Room for pixels displaced past the content, up to the layer's edge (#1205).
+ b.pad(crate::cover_pad(ctx, [b.img.width, b.img.height], b.offset, b.scale, amount.abs().ceil() as u32 + 1));
}
let off = b.to_px(ctx.params.v2("offset"));
let oct = ctx.params.f("complexity").clamp(1.0, 10.0);
diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs
index 40ed79a7d..a968f1aad 100644
--- a/crates/effects/src/lib.rs
+++ b/crates/effects/src/lib.rs
@@ -286,6 +286,32 @@ impl Buf {
self.offset[0] += origin[0] * self.scale;
self.offset[1] += origin[1] * self.scale;
}
+ /// Grow by `left` and `top` transparent pixels on those sides only.
+ pub fn grow_left_top(&mut self, left: u32, top: u32) {
+ if left == 0 && top == 0 {
+ return;
+ }
+ let src = std::mem::replace(&mut self.img, Image::new(0, 0));
+ let (w, h) = (src.width + left, src.height + top);
+ let mut out = Image::new(w, h);
+ let sw = src.width as usize;
+ for y in 0..src.height as usize {
+ let d = (y + top as usize) * w as usize + left as usize;
+ out.data[d..d + sw].copy_from_slice(&src.data[y * sw..(y + 1) * sw]);
+ }
+ self.img = out;
+ self.offset[0] += left as f64;
+ self.offset[1] += top as f64;
+ }
+ /// Give the buffer the pixel-grid phase (modulo [`GRID_ALIGN`]) that [`Buf::cover`] of
+ /// `size` would, by growing left/top only ([`align_plan`]). A downscaled layer is resampled
+ /// through a pyramid whose 2x2 blocks follow the buffer's grid, so a buffer that starts at
+ /// its content's corner instead of the cover's resamples to different pixels (#1205).
+ pub fn align(&mut self, size: [f64; 2]) {
+ if let Some((l, t)) = align_plan(self.offset, self.scale, size, self.img.width, self.img.height) {
+ self.grow_left_top(l, t);
+ }
+ }
/// Grow by `pad` transparent pixels on every side.
pub fn pad(&mut self, pad: u32) {
if pad == 0 {
@@ -301,6 +327,33 @@ impl Buf {
/// `width` × `height` so it spans the `size`-layer-pixel region that starts at its offset — the
/// whole layer rectangle, which a sized source's buffer already is. `None` when the buffer
/// already covers that region unchanged (footage, solids, the comp below an adjustment).
+/// Effects that grow their own buffer by what they need (blur and glow margins, Repeat Edge
+/// Pixels through [`Buf::cover`], displacement through [`cover_pad`]): a stack of only these
+/// skips the renderer's cover of the whole layer rectangle (#1205).
+pub const SKIPS_COVER: &[&str] = &["ec.blur.gaussian", "ec.blur.fastbox", "ec.stylize.glow", "ec.distort.turbulentdisplace"];
+
+/// The grid phase [`Buf::align`] keeps and the step [`cover_pad`] grows in: covers the levels
+/// of the resampling pyramid used for downscaled layers.
+pub const GRID_ALIGN: u32 = 64;
+
+/// Left/top growth giving a buffer [`cover_plan`]'s grid phase modulo [`GRID_ALIGN`]; `None`
+/// when there is nothing to add.
+pub fn align_plan(offset: [f64; 2], scale: f64, size: [f64; 2], width: u32, height: u32) -> Option<(u32, u32)> {
+ let (dx, dy, _, _) = cover_plan(offset, scale, size, width, height)?;
+ let (l, t) = (dx % GRID_ALIGN, dy % GRID_ALIGN);
+ (l != 0 || t != 0).then_some((l, t))
+}
+
+/// Padding for an effect that moves pixels up to `want` past the content (not an adjustment
+/// layer): `want`, capped at what reaches the layer rectangle, in whole [`GRID_ALIGN`] steps so
+/// the grid phase stays. 0 once the buffer covers the layer.
+pub fn cover_pad(ctx: &EffectCtx, size: [u32; 2], offset: [f64; 2], scale: f64, want: u32) -> u32 {
+ if ctx.adjustment {
+ return 0;
+ }
+ want.min(util::layer_cover_pad(ctx, size, offset, scale)).div_ceil(GRID_ALIGN) * GRID_ALIGN
+}
+
pub fn cover_plan(offset: [f64; 2], scale: f64, size: [f64; 2], width: u32, height: u32) -> Option<(u32, u32, u32, u32)> {
if scale <= 0.0 || !scale.is_finite() || !offset[0].is_finite() || !offset[1].is_finite() || !size[0].is_finite() || !size[1].is_finite() {
return None;
diff --git a/crates/effects/src/misc.rs b/crates/effects/src/misc.rs
index c119118db..fed4ce45c 100644
--- a/crates/effects/src/misc.rs
+++ b/crates/effects/src/misc.rs
@@ -34,6 +34,9 @@ fn gaussian(ctx: &EffectCtx, mut b: Buf) -> Buf {
let repeat = ctx.params.b("repeatEdge");
if !repeat && !ctx.adjustment {
b.pad((s * 3.0).ceil() as u32);
+ } else if repeat && !ctx.adjustment {
+ // The edge it repeats is the layer's, not the painted content's (#1205).
+ b.cover(ctx.layer_size);
}
b.img = gaussian_blur(&b.img, s * kx, s * ky, repeat || ctx.adjustment);
b
@@ -49,6 +52,9 @@ fn box_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
let repeat = ctx.params.b("repeatEdge");
if !repeat && !ctx.adjustment {
b.pad((r * it) as u32 + 1);
+ } else if repeat && !ctx.adjustment {
+ // The edge it repeats is the layer's, not the painted content's (#1205).
+ b.cover(ctx.layer_size);
}
b.img = effectcraft_raster::box_blur(&b.img, r * kx as usize, r * ky as usize, it, repeat || ctx.adjustment);
b
diff --git a/crates/gpu/src/effects.rs b/crates/gpu/src/effects.rs
index 426931d78..4bf503940 100644
--- a/crates/gpu/src/effects.rs
+++ b/crates/gpu/src/effects.rs
@@ -23,6 +23,20 @@ pub(crate) struct GBuf {
}
impl GBuf {
+ /// [`effectcraft_effects::Buf::cover`] on the device; `None` when it can't hold it.
+ pub fn cover(&mut self, e: &mut Enc, size: [f64; 2]) -> Option<()> {
+ let Some((dx, dy, w, h)) = effectcraft_effects::cover_plan(self.offset, self.scale, size, self.img.width, self.img.height) else { return Some(()) };
+ if !e.g.fits(w, h) {
+ return None;
+ }
+ let out = e.image(w, h);
+ e.copy_into(&self.img, &out, dx, dy);
+ self.img = out;
+ self.offset[0] += dx as f64;
+ self.offset[1] += dy as f64;
+ Some(())
+ }
+
pub fn to_px(&self, p: [f64; 2]) -> (f64, f64) {
(p[0] * self.scale + self.offset[0], p[1] * self.scale + self.offset[1])
}
@@ -145,6 +159,32 @@ impl effectcraft_render::FxTarget for GpuFx<'_, '_> {
let out = f(Buf { img, offset: b.offset, scale: b.scale });
self.b = self.e.g.upload_image(&out.img).map(|img| GBuf { img, offset: out.offset, scale: out.scale });
}
+
+ /// GBuf::cover, like `pad`: a bigger texture with the old one copied into place, no
+ /// readback. A region the device can't allocate leaves the buffer as it is.
+ fn cover(&mut self, size: [f64; 2]) {
+ let Some(mut b) = self.b.take() else { return };
+ let keep = (b.img.clone(), b.offset);
+ if b.cover(self.e, size).is_none() {
+ (b.img, b.offset) = keep;
+ }
+ self.b = Some(b);
+ }
+
+ /// [`effectcraft_effects::Buf::align`] on the device: a texture grown left/top, the old one
+ /// copied in. A size the device can't allocate leaves the buffer as it is.
+ fn align(&mut self, size: [f64; 2]) {
+ let Some(b) = self.b.take() else { return };
+ let out = match effectcraft_effects::align_plan(b.offset, b.scale, size, b.img.width, b.img.height) {
+ Some((l, t)) if self.e.g.fits(b.img.width + l, b.img.height + t) => {
+ let img = self.e.image(b.img.width + l, b.img.height + t);
+ self.e.copy_into(&b.img, &img, l, t);
+ GBuf { img, offset: [b.offset[0] + l as f64, b.offset[1] + t as f64], scale: b.scale }
+ }
+ _ => b,
+ };
+ self.b = Some(out);
+ }
}
fn apply(e: &mut Enc, id: &str, ctx: &EffectCtx, b: GBuf) -> Option<GBuf> {
@@ -249,6 +289,9 @@ fn gaussian(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
let repeat = ctx.params.b("repeatEdge");
if !repeat && !ctx.adjustment {
b.pad(e, (s * 3.0).ceil() as u32)?;
+ } else if repeat && !ctx.adjustment {
+ // The edge it repeats is the layer's, not the painted content's (#1205).
+ b.cover(e, ctx.layer_size)?;
}
b.img = gaussian_blur(e, &b.img, s * kx, s * ky, repeat || ctx.adjustment);
Some(b)
@@ -264,6 +307,9 @@ fn box_blur(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
let repeat = ctx.params.b("repeatEdge");
if !repeat && !ctx.adjustment {
b.pad(e, (r * it) as u32 + 1)?;
+ } else if repeat && !ctx.adjustment {
+ // The edge it repeats is the layer's, not the painted content's (#1205).
+ b.cover(e, ctx.layer_size)?;
}
b.img = box_passes(e, &b.img, &vec![r * kx as usize; it], &vec![r * ky as usize; it], repeat || ctx.adjustment);
Some(b)
diff --git a/crates/gpu/src/fx_distort.rs b/crates/gpu/src/fx_distort.rs
index a62ad6831..e4e17c64c 100644
--- a/crates/gpu/src/fx_distort.rs
+++ b/crates/gpu/src/fx_distort.rs
@@ -177,6 +177,9 @@ fn turbulent(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
let size = (ctx.params.f("size") * b.scale).max(1.0);
if ctx.params.b("resizeLayer") && !ctx.adjustment {
b.pad(e, amount.abs().ceil() as u32 + 1)?;
+ } else {
+ // Room for pixels displaced past the content, up to the layer's edge (#1205).
+ b.pad(e, effectcraft_effects::cover_pad(ctx, [b.img.width, b.img.height], b.offset, b.scale, amount.abs().ceil() as u32 + 1))?;
}
let off = b.to_px(ctx.params.v2("offset"));
let oct = ctx.params.f("complexity").clamp(1.0, 10.0);
diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs
index d9c87af9f..041052b8e 100644
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@ -490,6 +490,24 @@ pub trait FxTarget {
fn gpu(&mut self, steps: &[FxStep]) -> bool;
/// Run one CPU effect: `f` maps the buffer.
fn cpu(&mut self, f: &mut dyn FnMut(Buf) -> Buf);
+ /// Grow the buffer to cover its whole layer rectangle (`size` layer pixels from its own
+ /// corner, the `layer_size` handed to effects), as [`effectcraft_effects::Buf::cover`].
+ /// Authored buffers (text, shapes) only span their painted content; this gives the stack
+ /// the room a sized source's buffer already has (#659).
+ fn cover(&mut self, size: [f64; 2]) {
+ self.cpu(&mut |mut buf| {
+ buf.cover(size);
+ buf
+ });
+ }
+ /// Give the buffer the pixel-grid phase the layer rectangle's cover would
+ /// ([`effectcraft_effects::Buf::align`]): grows left/top by less than 64 pixels.
+ fn align(&mut self, size: [f64; 2]) {
+ self.cpu(&mut |mut buf| {
+ buf.align(size);
+ buf
+ });
+ }
}
/// The CPU effect target (GPU chains go through [`Accelerator::effects`]: upload, readback).
@@ -1126,7 +1144,25 @@ impl<'a> Renderer<'a> {
// place pixels outside it grow the buffer themselves, by exactly what they need
// (`layer_cover_pad`: CC Tiler, CC Smear, CC Mosaic sharp) instead of a blanket cover of
// the whole layer rectangle on every stack — which paid a comp-sized texture per layer,
- // per frame in flight, for glow and blur that never use the extra room.
+ // per frame in flight, for glow and blur that never use the extra room. The buffer still
+ // takes the cover's pixel-grid phase: a downscaled layer resamples along that grid.
+ // Effects not known to stay inside their own margins (Twirl, Bulge, Transform, ...) still
+ // get the cover: they move content past the painted bounds without growing the buffer.
+ let video = || {
+ fx.groups().filter(|g| g.enabled).filter_map(|g| match &g.kind {
+ GroupKind::Effect { effect } if effectcraft_effects::find(effect).is_some_and(|s| !effectcraft_effects::audio_fx::is_audio_effect(s.id)) => {
+ Some(effect.as_str())
+ }
+ _ => None,
+ })
+ };
+ if !adjustment && video().next().is_some() {
+ if video().all(|id| effectcraft_effects::SKIPS_COVER.contains(&id)) {
+ target.align(layer_size);
+ } else {
+ target.cover(layer_size);
+ }
+ }
let mut mask_shapes = masks::shapes(ctx, layer);
if origin != [0.0; 2] {
for q in mask_shapes.iter_mut().flat_map(|m| m.points.iter_mut()) {