Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

GPU.EFFECT_SOURCE: preserve exact texel centers for effect inputs

sanyamsingh001892003f3d4aa
diff --git a/crates/gpu/src/composite.wgsl b/crates/gpu/src/composite.wgsl
index 5e897ba32..cc40e08b6 100644
--- a/crates/gpu/src/composite.wgsl
+++ b/crates/gpu/src/composite.wgsl
@@ -14,7 +14,7 @@ struct U {
p0: vec4<f32>, // opacity, kind (0 rgba8 srgb straight, 1 rgba16f premul linear, 2 yuv), taps, transfer (0 srgb, 1 linear, 2 pq, 3 hlg)
p1: vec4<f32>, // y_off y_scale c_off c_scale (code units)
p2: vec4<f32>, // kr kb code_scale footprint
- p3: vec4<f32>, // blend mode (index into filmcraft_render::Blend::ALL), alpha-plane scale (0: none), unused ×2
+ p3: vec4<f32>, // blend mode, alpha-plane scale (0: none), effect-source integer decimation, unused
};
@group(0) @binding(0) var<uniform> u: U;
@@ -274,6 +274,15 @@ fn linear_to_srgb(v: vec3<f32>) -> vec3<f32> {
return select(hi, v * 12.92, v <= vec3(0.0031308));
}
+// Effect sources are axis-aligned integer-decimated working images. Raster-interpolated source
+// coordinates can mix adjacent texels at a nominal pixel center, inventing alpha near zero.
+// Keep the regular transformed/minified layer path unchanged; only this source draw uses its
+// exact working-pixel position and the host's integer decimation.
+@fragment
+fn fs_fx_source(in: VOut) -> @location(0) vec4<f32> {
+ return layer_color(in.pos.xy * u.p3.z);
+}
+
// Blend modes that need the destination: written without fixed-function blending.
@fragment
fn fs_blend(in: VOut) -> @location(0) vec4<f32> {
diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs
index 930a43d87..660ee2c35 100644
--- a/crates/gpu/src/fx.rs
+++ b/crates/gpu/src/fx.rs
@@ -254,7 +254,7 @@ impl FxStage {
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: composite,
- entry_point: Some("fs"),
+ entry_point: Some("fs_fx_source"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState { format: FX_FORMAT, blend: None, write_mask: wgpu::ColorWrites::ALL })],
}),
diff --git a/crates/gpu/src/fx_tests.rs b/crates/gpu/src/fx_tests.rs
index 5e201d117..f47c0546a 100644
--- a/crates/gpu/src/fx_tests.rs
+++ b/crates/gpu/src/fx_tests.rs
@@ -489,3 +489,74 @@ fn layers_with_effects_fall_back_to_the_cpu_without_a_stage() {
assert!(p99 <= 6 && mean < 1.5, "stage {stage}: p99 {p99}, mean {mean}");
}
}
+
+/// The half-float upload is lossless for this input, so a 1:1 effect source draw must be too.
+/// Odd dimensions expose interpolation drift that can invent opacity before any effect runs.
+#[test]
+fn effect_source_identity_preserves_odd_sized_half_float_texels() {
+ let Some((dev, q)) = device() else {
+ eprintln!("no GPU adapter; skipping");
+ return;
+ };
+ let mut c = GpuCompositor::new(&dev, &q);
+ for (w, h) in [(67u32, 41u32), (96, 54), (64, 32)] {
+ let px = picture(w, h);
+ let frame = VideoFrame::rgba_f32(w, h, px.clone());
+ for n in [0, 1] {
+ let (ow, oh, gpu) = c.effect_image(&frame, &LayerFx { size: (w, h), decimation: n, ops: Vec::new() }).expect("source image");
+ assert_eq!((ow, oh, gpu.len()), (w, h, px.len()));
+ for (i, (expected, actual)) in px.iter().zip(&gpu).enumerate() {
+ assert_eq!(actual.to_bits(), expected.to_bits(), "{w}x{h} n{n} source component{i}: {actual} != {expected}");
+ }
+ }
+ }
+}
+
+#[test]
+fn alpha_invert_does_not_amplify_invented_opacity_at_odd_source_pixels() {
+ let Some((dev, q)) = device() else {
+ eprintln!("no GPU adapter; skipping");
+ return;
+ };
+ let mut c = GpuCompositor::new(&dev, &q);
+ let (w, h) = (67, 41);
+ let px = picture(w, h);
+ let frame = VideoFrame::rgba_f32(w, h, px.clone());
+ let op = FxOp::Invert { channel: 4, blend: 0.2 };
+ let mut cpu = filmcraft_render::Image { w: w as usize, h: h as usize, px: px.clone() };
+ op.apply(&mut cpu);
+ let (_, _, gpu) = c.effect_image(&frame, &LayerFx { size: (w, h), decimation: 1, ops: vec![op] }).expect("alpha invert image");
+ assert_eq!(gpu.len(), px.len());
+ let (worst, flips) = compare(&cpu.px, &gpu);
+ assert!(worst <= EXACT && flips == 0.0, "alpha invert: {worst}, {flips}");
+ let mut transparent = 0;
+ for (source, result) in px.as_chunks::<4>().0.iter().zip(gpu.as_chunks::<4>().0.iter()) {
+ if source[3] == 0.0 {
+ transparent += 1;
+ assert_eq!(&result[..3], &[0.0; 3], "a transparent source must not acquire neighboring RGB");
+ assert!((result[3] - 0.8).abs() <= f32::EPSILON);
+ }
+ }
+ assert_eq!(transparent, 402);
+}
+
+/// The exact source grid keeps the existing box-minification behavior at non-unit factors.
+#[test]
+fn effect_source_integer_decimation_preserves_cpu_box_oracle() {
+ let Some((dev, q)) = device() else {
+ eprintln!("no GPU adapter; skipping");
+ return;
+ };
+ let mut c = GpuCompositor::new(&dev, &q);
+ for (w, h) in [(67u32, 41u32), (96, 54)] {
+ let frame = VideoFrame::rgba_f32(w, h, picture(w, h));
+ for n in [2, 4, 8] {
+ let (ow, oh, cpu) = frame.to_linear_f32_decimated(n as usize);
+ let (gw, gh, gpu) =
+ c.effect_image(&frame, &LayerFx { size: (ow as u32, oh as u32), decimation: n, ops: Vec::new() }).expect("decimated source image");
+ assert_eq!((gw as usize, gh as usize, gpu.len()), (ow, oh, cpu.len()));
+ let (worst, flips) = compare(&cpu, &gpu);
+ assert!(worst <= EXACT && flips == 0.0, "{w}x{h} n{n} source box: {worst}, {flips}");
+ }
+ }
+}
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index f71db2b67..c8442d071 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -655,7 +655,10 @@ impl GpuCompositor {
Some(lfx) => {
// the source drawn onto the working image (box-decimated by n, as the CPU decodes)
let n = lfx.decimation.max(1) as f64;
- let su = Self::uniforms(&src, &filmcraft_geom::Affine::scale(1.0 / n, 1.0 / n), 1.0, Blend::Normal, (lfx.size.0.max(1), lfx.size.1.max(1)));
+ let mut su =
+ Self::uniforms(&src, &filmcraft_geom::Affine::scale(1.0 / n, 1.0 / n), 1.0, Blend::Normal, (lfx.size.0.max(1), lfx.size.1.max(1)));
+ // Effect sources use the working pixel center, avoiding interpolated vertex coordinates.
+ su[26] = n as f32;
let sbuf = self.uniform_buffer(&su);
let sbg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("fx-source"),
@@ -799,7 +802,9 @@ impl GpuCompositor {
let views = self.uploads.get(&key)?.views.clone();
let n = fx.decimation.max(1) as f64;
let (w, h) = (fx.size.0.max(1), fx.size.1.max(1));
- let su = Self::uniforms(&src, &filmcraft_geom::Affine::scale(1.0 / n, 1.0 / n), 1.0, Blend::Normal, (w, h));
+ let mut su = Self::uniforms(&src, &filmcraft_geom::Affine::scale(1.0 / n, 1.0 / n), 1.0, Blend::Normal, (w, h));
+ // Same integer source-grid mapping as the composited effect path.
+ su[26] = n as f32;
let sbuf = self.uniform_buffer(&su);
let sbg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("fx-source"),