From 8d603d9db8bd1b97a6effb0d793f33cc7d6f3c24 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 16:06:36 +0530 Subject: [PATCH] #3460: GPU box passes count repeated edge pixels instead of visiting them Each box-pass thread started its running sum by reading all 2r+1 pixels of its window, even past the end of the line. At Gaussian Blur's Blurriness 30000 the radius is about 15000 px, so most of those reads were the same edge pixel or zero. Sum the pixels inside the line once and add the repeated edges times their count, as the CPU pass does. Apple M5 (Metal), 4096 x 4096, three passes per axis, repeated edges: r = 15000 went from about 185 ms to 75 ms; r = 1500 is unchanged (80 ms). --- crates/gpu/src/shaders/kernels.wgsl | 18 +++++++++++++++++- crates/gpu/src/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/gpu/src/shaders/kernels.wgsl b/crates/gpu/src/shaders/kernels.wgsl index a123b70ca..2f52b3b48 100644 --- a/crates/gpu/src/shaders/kernels.wgsl +++ b/crates/gpu/src/shaders/kernels.wgsl @@ -289,9 +289,25 @@ fn box_pass(gid: vec3, horizontal: bool) { } let norm = 1.0 / f32(2 * r + 1); var acc = vec4(0.0); - for (var i = b0 - r; i <= b0 + r; i++) { + // The starting window, as the CPU sums it: the pixels inside the line once each, and the + // repeated edge pixels counted rather than visited (a radius far past the line, such as + // Gaussian Blur's at Blurriness 30000, would otherwise loop over it once per pixel). + let lo = b0 - r; + let hi = b0 + r; + for (var i = max(lo, 0); i <= min(hi, n - 1); i++) { acc += line_get(horizontal, line, i, n, repeat); } + if (repeat) { + let before = f32(max(-lo, 0)); + let after = f32(max(hi - (n - 1), 0)); + // A zero count adds nothing (0 * NaN would be NaN). + if (before > 0.0) { + acc += line_get(horizontal, line, 0, n, repeat) * before; + } + if (after > 0.0) { + acc += line_get(horizontal, line, n - 1, n, repeat) * after; + } + } let b1 = min(b0 + block, n); for (var i = b0; i < b1; i++) { if (horizontal) { diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index 1d2782262..23e67d722 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -760,6 +760,27 @@ fn box_blur_horizontal_rows_match_cpu_beyond_256() { } } +/// #3460: a box radius far past the image (Gaussian Blur's at Blurriness 30000 is about 15000 px) +/// starts each running sum as the CPU does, the pixels inside once and the repeated edges +/// counted, so the GPU matches it along rows and columns, with and without repeated edges. +#[test] +fn box_passes_with_a_radius_far_past_the_image_match_cpu() { + let Some(g) = gpu() else { return }; + let img = pattern(5, 96, 40); + let up = g.ctx.upload_image(&img).expect("upload"); + let tol = tolerance(BitDepth::Bpc32); + for repeat in [true, false] { + let mut e = crate::context::Enc::new(&g.ctx); + let r = [15_000usize; 3]; + let out = crate::effects::box_passes(&mut e, &up, &r, &r, repeat); + let got = e.download(&out).expect("download"); + let want = effectcraft_raster::box_blur(&img, 15_000, 15_000, 3, repeat); + assert_eq!((got.width, got.height), (want.width, want.height)); + let d = diff(&want, &got, tol); + assert_eq!(d.over, 0, "repeat {repeat}: max {} at {:?} on {}", d.max, d.worst, g.ctx.name); + } +} + #[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).