diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 5da2035ac..13bd8bf93 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -92,8 +92,8 @@ use effectcraft_project::build::Ids; use effectcraft_project::{GroupKind, ParamUi, PropGroup, Property}; pub use effectcraft_raster::{AuxChannels, Image}; pub use misc::{ - GLOW_GAIN, GLOW_KNEE, INVERT_ALPHA, INVERT_CHANNELS, glow_ab_t, glow_operation, glow_sigma, linear_wipe_axis, linear_wipe_sigma, radial_wipe_sigma, - radial_wipe_wedge, venetian_cover, venetian_origin, + GLOW_GAIN, GLOW_KNEE, INVERT_ALPHA, INVERT_CHANNELS, drop_shadow_pad, glow_ab_t, glow_operation, glow_sigma, linear_wipe_axis, linear_wipe_sigma, + radial_wipe_sigma, radial_wipe_wedge, venetian_cover, venetian_origin, }; pub use sim::particle_state; pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state}; diff --git a/crates/effects/src/misc.rs b/crates/effects/src/misc.rs index 9c7131657..7e5f5c1c6 100644 --- a/crates/effects/src/misc.rs +++ b/crates/effects/src/misc.rs @@ -3,7 +3,7 @@ use effectcraft_color::luminance; use effectcraft_keyframe::Value; use effectcraft_project::ParamUi; -use effectcraft_raster::{Image, gaussian_blur, hash_noise}; +use effectcraft_raster::{Image, gaussian_blur, gaussian_blur_unbounded, hash_noise}; use rayon::prelude::*; use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; @@ -283,6 +283,12 @@ fn glow(ctx: &EffectCtx, mut b: Buf) -> Buf { b } +/// The margin Drop Shadow asks for around a layer: its Distance and its blur's reach (three +/// boxes of radius about σ = Softness / 2), both in buffer pixels. Shared with the GPU path. +pub fn drop_shadow_pad(dist: f64, soft: f64) -> u32 { + ((dist + soft * 1.5).ceil() as u32).saturating_add(2) +} + fn drop_shadow(ctx: &EffectCtx, mut b: Buf) -> Buf { let color = ctx.params.color("color"); let opacity = ctx.params.f("opacity") as f32 / 100.0; @@ -290,9 +296,17 @@ fn drop_shadow(ctx: &EffectCtx, mut b: Buf) -> Buf { let dist = ctx.params.f("distance") * b.scale; let soft = ctx.params.f("softness") * b.scale; let only = ctx.params.b("shadowOnly"); - let pad = (dist + soft * 1.5).ceil() as u32 + 2; + let pad = drop_shadow_pad(dist, soft); + // Whether the buffer holds the whole blur. The pixel budget (#2261) cuts the margin of a + // large Softness (After Effects' range reaches 30000, #3464); the blur then goes on past the + // buffer's edge rather than dropping what reaches it, which would leave the shadow far too + // faint. An adjustment layer's buffer is the composition, as before. + let mut holds_blur = true; if !ctx.adjustment { + let (w0, h0) = (b.img.width, b.img.height); b.pad(pad); + let need = pad.saturating_mul(2); + holds_blur = b.img.width.saturating_sub(w0) >= need && b.img.height.saturating_sub(h0) >= need; } let (dx, dy) = (dir.sin() * dist, -dir.cos() * dist); let mut sh = Image::new(b.img.width, b.img.height); @@ -304,7 +318,7 @@ fn drop_shadow(ctx: &EffectCtx, mut b: Buf) -> Buf { } }); if soft > 0.0 { - sh = gaussian_blur(&sh, soft / 2.0, soft / 2.0, false); + sh = if holds_blur { gaussian_blur(&sh, soft / 2.0, soft / 2.0, false) } else { gaussian_blur_unbounded(&sh, soft / 2.0, soft / 2.0) }; } if !only { sh.data.par_iter_mut().zip(b.img.data.par_iter()).for_each(|(s, o)| { @@ -1036,6 +1050,30 @@ mod tests { assert_ne!(row(2.0), row(2.4)); } + /// #3464: past the padding budget (#2261) the shadow keeps its whole blur (σ = Softness / 2) + /// up to the buffer's edge. A 64 × 48 layer's shadow at its centre is the Gaussian's share + /// of the layer, less the 6 % three boxes' peak is under a Gaussian's; dropping what the + /// boxes spread past the buffer left it at 0.2 of that at 8000 and 0.001 at 30000. + #[test] + fn drop_shadow_keeps_its_blur_past_the_padding_budget() { + let (w, h) = (64u32, 48u32); + for softness in [500.0, 4000.0, 8000.0, 30000.0] { + let layer = effectcraft_raster::Image::filled(w, h, [1.0, 1.0, 1.0, 1.0]); + let vals = [("softness", num(softness)), ("opacity", num(100.0)), ("distance", num(0.0)), ("shadowOnly", Value::Bool(true))]; + let out = run_fx("ec.perspective.dropshadow", &vals, layer, 0.0, EffectEnv::default()); + assert!(out.img.data.iter().flatten().all(|v| v.is_finite()), "softness {softness}"); + let sigma = softness / 2.0; + let share = |len: u32| { + let sum: f64 = (0..len).map(|i| (-((f64::from(i) + 0.5 - f64::from(len) / 2.0) / sigma).powi(2) / 2.0).exp()).sum(); + sum / (sigma * (2.0 * std::f64::consts::PI).sqrt()) + }; + let want = share(w) * share(h); + let at = |v: f64, half: u32| (v + f64::from(half)) as i64; + let got = f64::from(out.img.get(at(out.offset[0], w / 2), at(out.offset[1], h / 2))[3]); + assert!((0.85..1.02).contains(&(got / want)), "softness {softness}: centre alpha {got}, Gaussian {want}"); + } + } + #[test] fn fractional_box_blur_radius_is_continuous() { let row = |v| blurred_row("ec.blur.fastbox", "radius", v); diff --git a/crates/gpu/src/effects.rs b/crates/gpu/src/effects.rs index fd5a97c38..8bbae69c4 100644 --- a/crates/gpu/src/effects.rs +++ b/crates/gpu/src/effects.rs @@ -411,8 +411,12 @@ fn drop_shadow(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option { let dist = ctx.params.f("distance") * b.scale; let soft = ctx.params.f("softness") * b.scale; let only = ctx.params.b("shadowOnly"); - let pad = (dist + soft * 1.5).ceil() as u32 + 2; + let pad = effectcraft_effects::drop_shadow_pad(dist, soft); if !ctx.adjustment { + // A margin cut to the pixel budget: the CPU blurs on past the buffer's edge (#3464). + if soft > 0.0 && effectcraft_effects::pad_within_budget(b.img.width, b.img.height, pad) < pad { + return None; + } b.pad(e, pad)?; } let (dx, dy) = (dir.sin() * dist, -dir.cos() * dist); diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index 1d2782262..0959aa4de 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -760,6 +760,39 @@ fn box_blur_horizontal_rows_match_cpu_beyond_256() { } } +/// #3464: Drop Shadow's Softness reaches 30000. While the padded buffer holds the blur the GPU +/// runs it (1000, the old maximum, on a 70 × 44 layer); once the pixel budget cuts the padding +/// the CPU blurs past the buffer's edge, so the GPU hands the effect over and the render still +/// matches, with and without Shadow Only. +#[test] +fn drop_shadow_large_softness_matches_cpu() { + effect_case("ec.perspective.dropshadow", &[("distance", n(6.0)), ("softness", n(1000.0)), ("opacity", n(100.0))]); + let Some(g) = gpu() else { return }; + for only in [false, true] { + let vals = [("distance", n(6.0)), ("softness", n(30000.0)), ("opacity", n(100.0)), ("shadowOnly", Value::Bool(only))]; + let spec = effectcraft_effects::find("ec.perspective.dropshadow").unwrap(); + let mut params = effectcraft_effects::Params { + values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, [70.0, 44.0]))).collect(), + }; + for (k, v) in &vals { + params.values.insert(k.to_string(), v.clone()); + } + let ctx = effectcraft_effects::EffectCtx { params: ¶ms, time: 0.0, layer_size: [70.0, 44.0], seed: 11, adjustment: false, env: Default::default() }; + let buf = effectcraft_effects::Buf { img: pattern(7, 70, 44), offset: [0.0, 0.0], scale: 1.0 }; + let step = effectcraft_render::FxStep { spec, ctx }; + assert!(effectcraft_render::Accelerator::effects(g, &[step], &buf, None).is_none(), "the GPU hands a cut margin to the CPU"); + for depth in [BitDepth::Bpc8, BitDepth::Bpc32] { + let mut s = Scene::new(depth); + let bg = s.solid([0.15, 0.1, 0.2], 97, 61); + s.push(bg); + let mut l = s.footage(70, 44); + s.effect(&mut l, "ec.perspective.dropshadow", &vals); + s.push(l); + check(&format!("drop shadow 30000 only {only} {depth:?}"), compare_at(&s, opts(), Tick::ZERO), 0.0); + } + } +} + #[test] fn gpu_effects_match_cpu() { // Sigma 3 px and up and even radii, so the half-scale pass stays on the GPU too (#1939).