Skip to content

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

Effects: padded buffers stay within a pixel budget (#2261)

bflatasticfb2fb169dc1f
diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs
index 32e662fec..56e56bb61 100644
--- a/crates/effects/src/lib.rs
+++ b/crates/effects/src/lib.rs
@@ -312,8 +312,9 @@ impl Buf {
self.offset[0] += l as f64;
self.offset[1] += t as f64;
}
- /// Grow by `pad` transparent pixels on every side.
+ /// Grow by `pad` transparent pixels on every side, within [`pad_within_budget`].
pub fn pad(&mut self, pad: u32) {
+ let pad = pad_within_budget(self.img.width, self.img.height, pad);
if pad == 0 {
return;
}
@@ -323,6 +324,21 @@ impl Buf {
}
}
+/// `pad` on every side of a `w` × `h` effect buffer, cut so the padded buffer stays within
+/// 16 Mpx (256 MiB of f32 RGBA) or four times its own area, whichever is larger: a small layer
+/// far from an effect's centre can't ask for gigabytes (#2261). Shared with the GPU path.
+pub fn pad_within_budget(w: u32, h: u32, pad: u32) -> u32 {
+ const MIN_BUDGET: f64 = 16_777_216.0;
+ let (w, h) = (f64::from(w), f64::from(h));
+ let budget = MIN_BUDGET.max(4.0 * w * h);
+ if (w + 2.0 * f64::from(pad)) * (h + 2.0 * f64::from(pad)) <= budget {
+ return pad;
+ }
+ // (w + 2p)(h + 2p) = budget, solved for p.
+ let p = (((w - h).powi(2) + 4.0 * budget).sqrt() - (w + h)) / 4.0;
+ (p.floor().max(0.0) as u32).min(pad)
+}
+
/// Motion Tile's output frame for a buffer with layer origin `offset` (pixels) at `scale`:
/// `(width, height, offset)`. Output Width / Height grow (or crop) the layer rectangle about its
/// centre, on adjustment layers too, where the rectangle is the comp's and starts at the comp
@@ -1344,6 +1360,23 @@ pub(crate) fn run_fx(id: &str, vals: &[(&str, Value)], img: Image, time: f64, en
mod tests {
use super::*;
+ /// #2261: a 20 × 20 layer with CC Radial Fast Blur's Center far away grows to at most 16 Mpx;
+ /// big layers may still grow to four times their area, and small pads are untouched.
+ #[test]
+ fn padded_buffers_stay_within_the_budget() {
+ let area = |w: u32, h: u32, p: u32| (u64::from(w) + 2 * u64::from(p)) * (u64::from(h) + 2 * u64::from(p));
+ let far = radial_fast_pad((6500.0, 6500.0), 20, 20, 0.8);
+ assert_eq!(far, 4096, "the effect asks for its full pad");
+ let mut b = Buf { img: Image::new(20, 20), offset: [0.0; 2], scale: 1.0 };
+ b.pad(far);
+ assert!(area(20, 20, 0) < area(b.img.width, b.img.height, 0) && area(b.img.width, b.img.height, 0) <= 1 << 24, "{}x{}", b.img.width, b.img.height);
+ assert_eq!(b.offset, [f64::from((b.img.width - 20) / 2); 2], "the content keeps its place");
+ let p = pad_within_budget(3840, 2160, 4096);
+ assert!(p > 1000 && area(3840, 2160, p) <= 4 * 3840 * 2160 && area(3840, 2160, p + 1) > 4 * 3840 * 2160);
+ assert_eq!(pad_within_budget(640, 360, 200), 200);
+ assert_eq!(pad_within_budget(20, 20, 0), 0);
+ }
+
#[test]
fn run_fx_passthrough() {
let img = Image::filled(4, 3, [0.5, 0.25, 0.125, 1.0]);
diff --git a/crates/gpu/src/effects.rs b/crates/gpu/src/effects.rs
index 1e97b7eab..645f1cc39 100644
--- a/crates/gpu/src/effects.rs
+++ b/crates/gpu/src/effects.rs
@@ -46,6 +46,7 @@ impl GBuf {
/// Buf::pad: grow by `pad` transparent pixels on every side.
pub fn pad(&mut self, e: &mut Enc, pad: u32) -> Option<()> {
+ let pad = effectcraft_effects::pad_within_budget(self.img.width, self.img.height, pad);
if pad == 0 {
return Some(());
}