From 25c5adac1b3489863fb2c830d12b73b608ff18f8 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:16:10 +0000 Subject: [PATCH 1/6] fix(rendering): measure blur tap widths before any pixel returns --- .../shaders/composite-video-frame-plain.wgsl | 2 + .../src/shaders/composite-video-frame.wgsl | 91 +++++++++++++++++-- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl index 5a92cedbe8c..24855935149 100644 --- a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl @@ -66,6 +66,8 @@ fn path_shadow( return still; } +fn path_tap_widths(target_uv: vec2, blur_mode: f32, p: vec2) {} + fn path_move_taps( target_uv: vec2, velocity_uv: vec2, diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index 760a813e768..ee6909dd4de 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -82,6 +82,22 @@ fn interleaved_noise(p: vec2) -> f32 { const PATH_MOVE_TAPS: i32 = 20; const PATH_ZOOM_TAPS: i32 = 16; +// Each blur tap's anti-aliasing width, measured in fs_main before any pixel +// returns: fwidth is undefined in a quad where a neighbour has returned, and +// the taps run after the apron and border returns. +var tap_widths: array; + +fn tap_edge_width(sample_uv: vec2) -> f32 { + let centered_uv = (sample_uv - vec2(0.5)) * uniforms.target_size; + let distance = sdf_rounded_rect( + centered_uv, + uniforms.target_size * 0.5, + corner_radius_for(centered_uv), + uniforms.rounding_type + ); + return max(fwidth(distance), 1.0); +} + // The path kernel's code sits in its own layer filter region, so the plain // pipeline compiles none of it: software renderers run the plain module at // main's cost, and a frame that blurs with the path kernel takes the full @@ -138,6 +154,23 @@ fn path_shadow( return vec4(0.0, 0.0, 0.0, shadow_sum / f32(PATH_SHADOW_TAPS) * shadow_opacity); } +// tap_widths for path_move_taps or path_zoom_taps, at the same positions. +fn path_tap_widths(target_uv: vec2, blur_mode: f32, p: vec2) { + let path_uv = blur_path_uv(target_uv, blur_mode); + let phase = interleaved_noise(p); + if blur_mode < 1.5 { + let tap_total = f32(PATH_MOVE_TAPS); + for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { + tap_widths[i] = tap_edge_width(target_uv + path_uv * ((f32(i) + phase) / tap_total)); + } + } else { + let taps = f32(PATH_ZOOM_TAPS); + for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { + tap_widths[i] = tap_edge_width(target_uv + path_uv * ((f32(i) + phase) / taps)); + } + } +} + // Equal taps over the whole frame-to-frame path, each pixel starting at its // own phase so the streak reads as a smooth smear rather than repeated // copies. Returns the premultiplied colour sum and the alpha sum. @@ -155,7 +188,7 @@ fn path_move_taps( for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { let sample_uv = target_uv + velocity_uv * ((f32(i) + phase) / tap_total); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv); + tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -177,7 +210,7 @@ fn path_zoom_taps( for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { let sample_uv = target_uv + scaled_dir * ((f32(i) + dither) / taps); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv); + tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -223,8 +256,17 @@ fn coverage_from_distance(distance: f32, anti_alias_width: f32) -> f32 { fn rounded_rect_coverage(p: vec2, b: vec2, r: f32, rounding_type: f32) -> f32 { let distance = sdf_rounded_rect(p, b, r, rounding_type); - let anti_alias_width = max(fwidth(distance), 1.0); + return rounded_rect_coverage_within(p, b, r, rounding_type, distance, max(fwidth(distance), 1.0)); +} +fn rounded_rect_coverage_within( + p: vec2, + b: vec2, + r: f32, + rounding_type: f32, + distance: f32, + anti_alias_width: f32, +) -> f32 { if distance <= -anti_alias_width { return 1.0; } @@ -437,6 +479,30 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { border_coverage = clamp(border_outer_coverage - shape_coverage, 0.0, 1.0); } + // The taps' widths, while every pixel of the quad is still running. + if blur_active { + if path_kernel { + path_tap_widths(target_uv, blur_mode, p); + } else if blur_mode < 1.5 { + for (var i = 0; i < 20; i = i + 1) { + tap_widths[i] = tap_edge_width(target_uv + uniforms.motion_blur_vector * (f32(i) / 20.0)); + } + } else { + let dir = uniforms.motion_blur_zoom_center - target_uv; + let center_dist = length(dir); + // The pixel at the zoom centre returns before its taps; its ray + // only has to be finite here. + let ray_dir = select(dir / center_dist, vec2(0.0), center_dist < 1e-4); + let ray_sign = select(1.0, -1.0, uniforms.motion_blur_params.w > 0.5); + let scaled_dir = ray_dir * min(center_dist * min(uniforms.motion_blur_params.z, 1.0), MAX_ZOOM_RAY_UV) + * ray_sign; + let dither = interleaved_noise(p); + for (var i = 0; i < 13; i = i + 1) { + tap_widths[i] = tap_edge_width(target_uv + scaled_dir * ((f32(i) + dither) / 13.0)); + } + } + } + if target_uv.x < -edge_padding_uv.x - blur_reach_uv.x || target_uv.x > 1.0 + edge_padding_uv.x + blur_reach_uv.x || target_uv.y < -edge_padding_uv.y - blur_reach_uv.y || @@ -508,7 +574,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { for (var i = 0; i < 20; i = i + 1) { let sample_uv = target_uv + velocity_uv * (f32(i) / k); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv); + tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); let weight = select(1.0, 2.0, i == 0); accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; @@ -568,7 +634,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { let sample_uv = target_uv + scaled_dir * percent; var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv); + tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; weight_sum += weight; @@ -923,14 +989,21 @@ fn sample_premultiplied(uv: vec2) -> vec4 { return mix(top, bottom, fraction.y); } -fn apply_rounded_corners(current_color: vec4, target_uv: vec2) -> vec4 { +fn apply_rounded_corners( + current_color: vec4, + target_uv: vec2, + anti_alias_width: f32, +) -> vec4 { let centered_uv = (target_uv - vec2(0.5)) * uniforms.target_size; let half_size = uniforms.target_size * 0.5; - let coverage = rounded_rect_coverage( + let radius = corner_radius_for(centered_uv); + let coverage = rounded_rect_coverage_within( centered_uv, half_size, - corner_radius_for(centered_uv), - uniforms.rounding_type + radius, + uniforms.rounding_type, + sdf_rounded_rect(centered_uv, half_size, radius, uniforms.rounding_type), + anti_alias_width ); return vec4(current_color.rgb, current_color.a * coverage); From 01bf90c6f95e7ca7c06ad19b5e7a05007af1b879 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:21:12 +0000 Subject: [PATCH 2/6] refactor(rendering): share blur tap positions between the width pass and the taps --- .../shaders/composite-video-frame-plain.wgsl | 2 +- .../src/shaders/composite-video-frame.wgsl | 66 +++++++++++-------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl index 24855935149..42a873506bd 100644 --- a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl @@ -66,7 +66,7 @@ fn path_shadow( return still; } -fn path_tap_widths(target_uv: vec2, blur_mode: f32, p: vec2) {} +fn path_tap_widths(target_uv: vec2, path_uv: vec2, phase: f32, blur_mode: f32) {} fn path_move_taps( target_uv: vec2, diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index ee6909dd4de..6f22f2f7a5c 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -98,6 +98,26 @@ fn tap_edge_width(sample_uv: vec2) -> f32 { return max(fwidth(distance), 1.0); } +// Where tap `i` of `taps` samples along `path_uv`. The width hoist in fs_main +// and every tap loop take their positions from here, so the widths are always +// measured where the taps sample. +fn tap_uv(target_uv: vec2, path_uv: vec2, i: i32, phase: f32, taps: f32) -> vec2 { + return target_uv + path_uv * ((f32(i) + phase) / taps); +} + +// The zoom blur's ray for this pixel (card UV). Zero at the zoom centre, +// which returns before its taps. Taps run toward the centre while the frame +// shrinks and away from it while it grows, so the smear always trails where +// the content was. +fn zoom_ray_uv(target_uv: vec2) -> vec2 { + let dir = uniforms.motion_blur_zoom_center - target_uv; + let center_dist = length(dir); + let ray_dir = select(dir / center_dist, vec2(0.0), center_dist < 1e-4); + let ray_sign = select(1.0, -1.0, uniforms.motion_blur_params.w > 0.5); + return ray_dir * min(center_dist * min(uniforms.motion_blur_params.z, 1.0), MAX_ZOOM_RAY_UV) + * ray_sign; +} + // The path kernel's code sits in its own layer filter region, so the plain // pipeline compiles none of it: software renderers run the plain module at // main's cost, and a frame that blurs with the path kernel takes the full @@ -154,19 +174,15 @@ fn path_shadow( return vec4(0.0, 0.0, 0.0, shadow_sum / f32(PATH_SHADOW_TAPS) * shadow_opacity); } -// tap_widths for path_move_taps or path_zoom_taps, at the same positions. -fn path_tap_widths(target_uv: vec2, blur_mode: f32, p: vec2) { - let path_uv = blur_path_uv(target_uv, blur_mode); - let phase = interleaved_noise(p); +// tap_widths for path_move_taps or path_zoom_taps. +fn path_tap_widths(target_uv: vec2, path_uv: vec2, phase: f32, blur_mode: f32) { if blur_mode < 1.5 { - let tap_total = f32(PATH_MOVE_TAPS); for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { - tap_widths[i] = tap_edge_width(target_uv + path_uv * ((f32(i) + phase) / tap_total)); + tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, f32(PATH_MOVE_TAPS))); } } else { - let taps = f32(PATH_ZOOM_TAPS); for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { - tap_widths[i] = tap_edge_width(target_uv + path_uv * ((f32(i) + phase) / taps)); + tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, f32(PATH_ZOOM_TAPS))); } } } @@ -186,7 +202,7 @@ fn path_move_taps( var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { - let sample_uv = target_uv + velocity_uv * ((f32(i) + phase) / tap_total); + let sample_uv = tap_uv(target_uv, velocity_uv, i, phase, tap_total); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); accum += tap.rgb * tap.a; @@ -208,7 +224,7 @@ fn path_zoom_taps( var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { - let sample_uv = target_uv + scaled_dir * ((f32(i) + dither) / taps); + let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, taps); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); accum += tap.rgb * tap.a; @@ -481,24 +497,20 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { // The taps' widths, while every pixel of the quad is still running. if blur_active { + var path_uv = uniforms.motion_blur_vector; + if blur_mode >= 1.5 { + path_uv = zoom_ray_uv(target_uv); + } + let phase = interleaved_noise(p); if path_kernel { - path_tap_widths(target_uv, blur_mode, p); + path_tap_widths(target_uv, path_uv, phase, blur_mode); } else if blur_mode < 1.5 { for (var i = 0; i < 20; i = i + 1) { - tap_widths[i] = tap_edge_width(target_uv + uniforms.motion_blur_vector * (f32(i) / 20.0)); + tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, 0.0, 20.0)); } } else { - let dir = uniforms.motion_blur_zoom_center - target_uv; - let center_dist = length(dir); - // The pixel at the zoom centre returns before its taps; its ray - // only has to be finite here. - let ray_dir = select(dir / center_dist, vec2(0.0), center_dist < 1e-4); - let ray_sign = select(1.0, -1.0, uniforms.motion_blur_params.w > 0.5); - let scaled_dir = ray_dir * min(center_dist * min(uniforms.motion_blur_params.z, 1.0), MAX_ZOOM_RAY_UV) - * ray_sign; - let dither = interleaved_noise(p); for (var i = 0; i < 13; i = i + 1) { - tap_widths[i] = tap_edge_width(target_uv + scaled_dir * ((f32(i) + dither) / 13.0)); + tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, 13.0)); } } } @@ -572,7 +584,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { let k = 20.0; for (var i = 0; i < 20; i = i + 1) { - let sample_uv = target_uv + velocity_uv * (f32(i) / k); + let sample_uv = tap_uv(target_uv, velocity_uv, i, 0.0, k); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); let weight = select(1.0, 2.0, i == 0); @@ -610,11 +622,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { // from the center (ray = center_dist * amount), parabolic weights peaked // mid-ray, and a per-pixel random phase to dither the 13-tap banding — // all matching the reference zoom filter. - // Taps run toward the centre while the frame shrinks and away from it - // while it grows, so the smear always trails where the content was. - let ray_sign = select(1.0, -1.0, uniforms.motion_blur_params.w > 0.5); - let scaled_dir = dir / center_dist * min(center_dist * min(zoom_amount, 1.0), MAX_ZOOM_RAY_UV) - * ray_sign; + let scaled_dir = zoom_ray_uv(target_uv); let max_kernel = 13.0; let dither = interleaved_noise(p); @@ -631,7 +639,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { for (var i = 0; i < 13; i = i + 1) { let percent = (f32(i) + dither) / max_kernel; let weight = 4.0 * (percent - percent * percent); - let sample_uv = target_uv + scaled_dir * percent; + let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, max_kernel); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); From ac52c29af524743e8a6bc06bf56a939b7e478219 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 5 Dec 2026 23:20:24 +0000 Subject: [PATCH 3/6] fix(rendering): take blur tap edge widths from the quad partners Each motion blur tap now measures its edge width from the pixel's quad partners instead of fwidth, so the taps no longer depend on neighbours that returned early. Cards with different corner radii skip the edge shortcut only near their centre lines. --- .../shaders/composite-video-frame-plain.wgsl | 4 +- .../src/shaders/composite-video-frame.wgsl | 191 ++++++++++++------ 2 files changed, 131 insertions(+), 64 deletions(-) diff --git a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl index 42a873506bd..8381430d06d 100644 --- a/crates/rendering/src/shaders/composite-video-frame-plain.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame-plain.wgsl @@ -66,14 +66,13 @@ fn path_shadow( return still; } -fn path_tap_widths(target_uv: vec2, path_uv: vec2, phase: f32, blur_mode: f32) {} - fn path_move_taps( target_uv: vec2, velocity_uv: vec2, crop_bounds_uv: vec4, shrink: ShrinkRing, p: vec2, + partners: QuadPartners, ) -> vec4 { return vec4(0.0); } @@ -84,6 +83,7 @@ fn path_zoom_taps( crop_bounds_uv: vec4, shrink: ShrinkRing, dither: f32, + partners: QuadPartners, ) -> vec4 { return vec4(0.0); } diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index 6f22f2f7a5c..b4576d7c09c 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -82,30 +82,37 @@ fn interleaved_noise(p: vec2) -> f32 { const PATH_MOVE_TAPS: i32 = 20; const PATH_ZOOM_TAPS: i32 = 16; -// Each blur tap's anti-aliasing width, measured in fs_main before any pixel -// returns: fwidth is undefined in a quad where a neighbour has returned, and -// the taps run after the apron and border returns. -var tap_widths: array; - -fn tap_edge_width(sample_uv: vec2) -> f32 { - let centered_uv = (sample_uv - vec2(0.5)) * uniforms.target_size; - let distance = sdf_rounded_rect( - centered_uv, - uniforms.target_size * 0.5, - corner_radius_for(centered_uv), - uniforms.rounding_type - ); - return max(fwidth(distance), 1.0); +// The pixels a GPU differences against for this pixel's fwidth: its partners +// in its 2x2 quad (x+1 for an even x, x-1 for an odd x, the same in y), with +// the card UV, path and phase each would give its own blur taps. The taps +// run after per-pixel returns, where fwidth is undefined, so their edge +// width is taken from these instead. +struct QuadPartners { + x_uv: vec2, + x_path: vec2, + x_phase: f32, + y_uv: vec2, + y_path: vec2, + y_phase: f32, } -// Where tap `i` of `taps` samples along `path_uv`. The width hoist in fs_main -// and every tap loop take their positions from here, so the widths are always -// measured where the taps sample. -fn tap_uv(target_uv: vec2, path_uv: vec2, i: i32, phase: f32, taps: f32) -> vec2 { - return target_uv + path_uv * ((f32(i) + phase) / taps); +fn quad_partners(p: vec2, blur_mode: f32) -> QuadPartners { + let odd = fract(floor(p) * 0.5) > vec2(0.25); + let step = select(vec2(1.0), vec2(-1.0), odd); + let px = p + vec2(step.x, 0.0); + let py = p + vec2(0.0, step.y); + let x_uv = (px - uniforms.target_bounds.xy) / uniforms.target_size; + let y_uv = (py - uniforms.target_bounds.xy) / uniforms.target_size; + var x_path = uniforms.motion_blur_vector; + var y_path = uniforms.motion_blur_vector; + if blur_mode >= 1.5 { + x_path = zoom_ray_uv(x_uv); + y_path = zoom_ray_uv(y_uv); + } + return QuadPartners(x_uv, x_path, interleaved_noise(px), y_uv, y_path, interleaved_noise(py)); } -// The zoom blur's ray for this pixel (card UV). Zero at the zoom centre, +// The zoom blur's ray for a pixel at `target_uv`; zero at the zoom centre, // which returns before its taps. Taps run toward the centre while the frame // shrinks and away from it while it grows, so the smear always trails where // the content was. @@ -118,6 +125,33 @@ fn zoom_ray_uv(target_uv: vec2) -> vec2 { * ray_sign; } +// Where tap `i` of `taps` samples along `path_uv` for a pixel at +// `target_uv`. Every tap loop and its quad partners' taps go through here. +fn tap_uv(target_uv: vec2, path_uv: vec2, i: i32, phase: f32, taps: f32) -> vec2 { + return target_uv + path_uv * ((f32(i) + phase) / taps); +} + +// How far from the card edge a tap's anti-aliasing can matter for a pixel +// at `target_uv`. A partner's tap is at most a pixel plus its phase's share +// of the path away per axis, so beyond this the coverage is 0 or 1 whatever +// the width. On a card whose corners have different radii the distance +// jumps across the centre lines, so a pixel whose taps can come near one +// gets no shortcut at all. +fn tap_band(target_uv: vec2, path_uv: vec2, taps: f32) -> f32 { + let reach = length(path_uv * uniforms.target_size); + let band = 4.0 + 2.0 * reach / taps; + let centered = abs((target_uv - vec2(0.5)) * uniforms.target_size); + if corners_differ() && any(centered < vec2(reach + 2.0 * band)) { + return 3.0e38; + } + return band; +} + +fn corners_differ() -> bool { + let radii = uniforms.corner_radii; + return any(radii.xyz != radii.yzw); +} + // The path kernel's code sits in its own layer filter region, so the plain // pipeline compiles none of it: software renderers run the plain module at // main's cost, and a frame that blurs with the path kernel takes the full @@ -174,19 +208,6 @@ fn path_shadow( return vec4(0.0, 0.0, 0.0, shadow_sum / f32(PATH_SHADOW_TAPS) * shadow_opacity); } -// tap_widths for path_move_taps or path_zoom_taps. -fn path_tap_widths(target_uv: vec2, path_uv: vec2, phase: f32, blur_mode: f32) { - if blur_mode < 1.5 { - for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { - tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, f32(PATH_MOVE_TAPS))); - } - } else { - for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { - tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, f32(PATH_ZOOM_TAPS))); - } - } -} - // Equal taps over the whole frame-to-frame path, each pixel starting at its // own phase so the streak reads as a smooth smear rather than repeated // copies. Returns the premultiplied colour sum and the alpha sum. @@ -196,15 +217,23 @@ fn path_move_taps( crop_bounds_uv: vec4, shrink: ShrinkRing, p: vec2, + partners: QuadPartners, ) -> vec4 { let phase = interleaved_noise(p); let tap_total = f32(PATH_MOVE_TAPS); + let band = tap_band(target_uv, velocity_uv, tap_total); var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { let sample_uv = tap_uv(target_uv, velocity_uv, i, phase, tap_total); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); + tap = apply_rounded_corners( + tap, + sample_uv, + tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, tap_total), + tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, tap_total), + band + ); accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -219,14 +248,22 @@ fn path_zoom_taps( crop_bounds_uv: vec4, shrink: ShrinkRing, dither: f32, + partners: QuadPartners, ) -> vec4 { let taps = f32(PATH_ZOOM_TAPS); + let band = tap_band(target_uv, scaled_dir, taps); var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, taps); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); + tap = apply_rounded_corners( + tap, + sample_uv, + tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, taps), + tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, taps), + band + ); accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -495,26 +532,6 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { border_coverage = clamp(border_outer_coverage - shape_coverage, 0.0, 1.0); } - // The taps' widths, while every pixel of the quad is still running. - if blur_active { - var path_uv = uniforms.motion_blur_vector; - if blur_mode >= 1.5 { - path_uv = zoom_ray_uv(target_uv); - } - let phase = interleaved_noise(p); - if path_kernel { - path_tap_widths(target_uv, path_uv, phase, blur_mode); - } else if blur_mode < 1.5 { - for (var i = 0; i < 20; i = i + 1) { - tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, 0.0, 20.0)); - } - } else { - for (var i = 0; i < 13; i = i + 1) { - tap_widths[i] = tap_edge_width(tap_uv(target_uv, path_uv, i, phase, 13.0)); - } - } - } - if target_uv.x < -edge_padding_uv.x - blur_reach_uv.x || target_uv.x > 1.0 + edge_padding_uv.x + blur_reach_uv.x || target_uv.y < -edge_padding_uv.y - blur_reach_uv.y || @@ -573,7 +590,14 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { var tap_total = 21.0; if path_kernel { - let sums = path_move_taps(target_uv, velocity_uv, crop_bounds_uv, shrink, p); + let sums = path_move_taps( + target_uv, + velocity_uv, + crop_bounds_uv, + shrink, + p, + quad_partners(p, blur_mode), + ); accum = sums.rgb; alpha_sum = sums.a; tap_total = f32(PATH_MOVE_TAPS); @@ -582,11 +606,19 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { // t = 0, 0, 1/20 ... 19/20, divided by 21. The streak trails the // motion by just under a frame. let k = 20.0; + let partners = quad_partners(p, blur_mode); + let band = tap_band(target_uv, velocity_uv, k); for (var i = 0; i < 20; i = i + 1) { let sample_uv = tap_uv(target_uv, velocity_uv, i, 0.0, k); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); + tap = apply_rounded_corners( + tap, + sample_uv, + tap_uv(partners.x_uv, partners.x_path, i, 0.0, k), + tap_uv(partners.y_uv, partners.y_path, i, 0.0, k), + band + ); let weight = select(1.0, 2.0, i == 0); accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; @@ -631,18 +663,33 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { var weight_sum = 0.0; if path_kernel { - let sums = path_zoom_taps(target_uv, scaled_dir, crop_bounds_uv, shrink, dither); + let sums = path_zoom_taps( + target_uv, + scaled_dir, + crop_bounds_uv, + shrink, + dither, + quad_partners(p, blur_mode), + ); accum = sums.rgb; alpha_sum = sums.a; weight_sum = f32(PATH_ZOOM_TAPS); } else { + let partners = quad_partners(p, blur_mode); + let band = tap_band(target_uv, scaled_dir, max_kernel); for (var i = 0; i < 13; i = i + 1) { let percent = (f32(i) + dither) / max_kernel; let weight = 4.0 * (percent - percent * percent); let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, max_kernel); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners(tap, sample_uv, tap_widths[i]); + tap = apply_rounded_corners( + tap, + sample_uv, + tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, max_kernel), + tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, max_kernel), + band + ); accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; weight_sum += weight; @@ -997,21 +1044,41 @@ fn sample_premultiplied(uv: vec2) -> vec4 { return mix(top, bottom, fraction.y); } +fn card_distance(target_uv: vec2) -> f32 { + let centered_uv = (target_uv - vec2(0.5)) * uniforms.target_size; + return sdf_rounded_rect( + centered_uv, + uniforms.target_size * 0.5, + corner_radius_for(centered_uv), + uniforms.rounding_type + ); +} + +// The card's coverage of a blur tap, its width taken from the same taps of +// the pixel's quad partners (QuadPartners) inside `band` of the edge. fn apply_rounded_corners( current_color: vec4, target_uv: vec2, - anti_alias_width: f32, + partner_x_uv: vec2, + partner_y_uv: vec2, + band: f32, ) -> vec4 { let centered_uv = (target_uv - vec2(0.5)) * uniforms.target_size; let half_size = uniforms.target_size * 0.5; let radius = corner_radius_for(centered_uv); + let distance = sdf_rounded_rect(centered_uv, half_size, radius, uniforms.rounding_type); + if abs(distance) >= band { + return vec4(current_color.rgb, current_color.a * select(0.0, 1.0, distance < 0.0)); + } + let step_x = card_distance(partner_x_uv) - distance; + let step_y = card_distance(partner_y_uv) - distance; let coverage = rounded_rect_coverage_within( centered_uv, half_size, radius, uniforms.rounding_type, - sdf_rounded_rect(centered_uv, half_size, radius, uniforms.rounding_type), - anti_alias_width + distance, + max(abs(step_x) + abs(step_y), 1.0) ); return vec4(current_color.rgb, current_color.a * coverage); From 8ab7af947e94fe2feb2c406641427a8845078cf2 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 5 Dec 2026 23:24:11 +0000 Subject: [PATCH 4/6] fix(rendering): keep the blur edge shortcut on cards without rounding --- crates/rendering/src/shaders/composite-video-frame.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index b4576d7c09c..559e507e811 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -149,7 +149,7 @@ fn tap_band(target_uv: vec2, path_uv: vec2, taps: f32) -> f32 { fn corners_differ() -> bool { let radii = uniforms.corner_radii; - return any(radii.xyz != radii.yzw); + return uniforms.rounding_px > 0.0 && any(radii.xyz != radii.yzw); } // The path kernel's code sits in its own layer filter region, so the plain From 9350df7fa6f01c51f7f99dc1f202286f0ab56dfb Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 5 Dec 2026 23:28:38 +0000 Subject: [PATCH 5/6] fix(rendering): skip the blur edge shortcut only where the card distance jumps --- .../src/shaders/composite-video-frame.wgsl | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index 559e507e811..b3803991b44 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -134,22 +134,29 @@ fn tap_uv(target_uv: vec2, path_uv: vec2, i: i32, phase: f32, taps: f3 // How far from the card edge a tap's anti-aliasing can matter for a pixel // at `target_uv`. A partner's tap is at most a pixel plus its phase's share // of the path away per axis, so beyond this the coverage is 0 or 1 whatever -// the width. On a card whose corners have different radii the distance -// jumps across the centre lines, so a pixel whose taps can come near one -// gets no shortcut at all. +// the width. Where the distance jumps across a centre line, a pixel whose +// taps can come near that line gets no shortcut at all. fn tap_band(target_uv: vec2, path_uv: vec2, taps: f32) -> f32 { let reach = length(path_uv * uniforms.target_size); let band = 4.0 + 2.0 * reach / taps; let centered = abs((target_uv - vec2(0.5)) * uniforms.target_size); - if corners_differ() && any(centered < vec2(reach + 2.0 * band)) { + if any(centre_line_jumps() & (centered < vec2(reach + 2.0 * band))) { return 3.0e38; } return band; } -fn corners_differ() -> bool { - let radii = uniforms.corner_radii; - return uniforms.rounding_px > 0.0 && any(radii.xyz != radii.yzw); +// Whether the card's distance jumps across its vertical (x) and horizontal +// (y) centre lines. A corner's radius only changes the distance inside its +// own corner square, so it jumps only where two corners meeting at a line +// have different radii and the larger reaches past the half size across it. +fn centre_line_jumps() -> vec2 { + let r = uniforms.rounding_px * uniforms.corner_radii; + let half = uniforms.target_size * 0.5 + vec2(0.5); + return vec2( + (r.x != r.y && max(r.x, r.y) > half.x) || (r.z != r.w && max(r.z, r.w) > half.x), + (r.x != r.z && max(r.x, r.z) > half.y) || (r.y != r.w && max(r.y, r.w) > half.y) + ); } // The path kernel's code sits in its own layer filter region, so the plain From 703ec0115239d47c26316f3a44bfa8ad94227645 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 6 Dec 2026 01:01:09 +0000 Subject: [PATCH 6/6] perf(rendering): measure blur tap edge widths from the pixel's own taps --- .../src/shaders/composite-video-frame.wgsl | 145 +++++++++--------- 1 file changed, 76 insertions(+), 69 deletions(-) diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index b3803991b44..6fffc9b4a9a 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -82,34 +82,20 @@ fn interleaved_noise(p: vec2) -> f32 { const PATH_MOVE_TAPS: i32 = 20; const PATH_ZOOM_TAPS: i32 = 16; -// The pixels a GPU differences against for this pixel's fwidth: its partners -// in its 2x2 quad (x+1 for an even x, x-1 for an odd x, the same in y), with -// the card UV, path and phase each would give its own blur taps. The taps -// run after per-pixel returns, where fwidth is undefined, so their edge -// width is taken from these instead. +// The steps in card UV from this pixel to the pixels a GPU differences +// against for its fwidth: its partners in its 2x2 quad (x+1 for an even x, +// x-1 for an odd x, the same in y). The taps run after per-pixel returns, +// where fwidth is undefined, so a tap's edge width is taken from the same +// tap moved by these steps, keeping the pixel's own phase and path. struct QuadPartners { - x_uv: vec2, - x_path: vec2, - x_phase: f32, - y_uv: vec2, - y_path: vec2, - y_phase: f32, + x_step: vec2, + y_step: vec2, } -fn quad_partners(p: vec2, blur_mode: f32) -> QuadPartners { +fn quad_partners(p: vec2) -> QuadPartners { let odd = fract(floor(p) * 0.5) > vec2(0.25); - let step = select(vec2(1.0), vec2(-1.0), odd); - let px = p + vec2(step.x, 0.0); - let py = p + vec2(0.0, step.y); - let x_uv = (px - uniforms.target_bounds.xy) / uniforms.target_size; - let y_uv = (py - uniforms.target_bounds.xy) / uniforms.target_size; - var x_path = uniforms.motion_blur_vector; - var y_path = uniforms.motion_blur_vector; - if blur_mode >= 1.5 { - x_path = zoom_ray_uv(x_uv); - y_path = zoom_ray_uv(y_uv); - } - return QuadPartners(x_uv, x_path, interleaved_noise(px), y_uv, y_path, interleaved_noise(py)); + let step = select(vec2(1.0), vec2(-1.0), odd) / uniforms.target_size; + return QuadPartners(vec2(step.x, 0.0), vec2(0.0, step.y)); } // The zoom blur's ray for a pixel at `target_uv`; zero at the zoom centre, @@ -126,19 +112,19 @@ fn zoom_ray_uv(target_uv: vec2) -> vec2 { } // Where tap `i` of `taps` samples along `path_uv` for a pixel at -// `target_uv`. Every tap loop and its quad partners' taps go through here. +// `target_uv`. Every tap loop goes through here. fn tap_uv(target_uv: vec2, path_uv: vec2, i: i32, phase: f32, taps: f32) -> vec2 { return target_uv + path_uv * ((f32(i) + phase) / taps); } // How far from the card edge a tap's anti-aliasing can matter for a pixel -// at `target_uv`. A partner's tap is at most a pixel plus its phase's share -// of the path away per axis, so beyond this the coverage is 0 or 1 whatever -// the width. Where the distance jumps across a centre line, a pixel whose -// taps can come near that line gets no shortcut at all. -fn tap_band(target_uv: vec2, path_uv: vec2, taps: f32) -> f32 { +// at `target_uv`. A partner's tap is the same tap one pixel over, so its +// width is at most 2 and beyond this the coverage is 0 or 1. Where the +// distance jumps across a centre line, a pixel whose taps can come near that +// line gets no shortcut at all. +fn tap_band(target_uv: vec2, path_uv: vec2) -> f32 { let reach = length(path_uv * uniforms.target_size); - let band = 4.0 + 2.0 * reach / taps; + let band = 4.0; let centered = abs((target_uv - vec2(0.5)) * uniforms.target_size); if any(centre_line_jumps() & (centered < vec2(reach + 2.0 * band))) { return 3.0e38; @@ -159,6 +145,15 @@ fn centre_line_jumps() -> vec2 { ); } +// Whether every tap along `path_uv` from a pixel at `target_uv` lies at +// least `band` inside the card, where its coverage is 1. A tap is at most the +// path's length from the pixel and the distance changes by at most one per +// pixel, so this holds when the pixel itself is that far inside. Near a +// centre line the distance jumps, band is unbounded and this is false. +fn taps_all_inside(target_uv: vec2, path_uv: vec2, band: f32) -> bool { + return card_distance(target_uv) <= -(length(path_uv * uniforms.target_size) + band); +} + // The path kernel's code sits in its own layer filter region, so the plain // pipeline compiles none of it: software renderers run the plain module at // main's cost, and a frame that blurs with the path kernel takes the full @@ -228,19 +223,22 @@ fn path_move_taps( ) -> vec4 { let phase = interleaved_noise(p); let tap_total = f32(PATH_MOVE_TAPS); - let band = tap_band(target_uv, velocity_uv, tap_total); + let band = tap_band(target_uv, velocity_uv); + let all_inside = taps_all_inside(target_uv, velocity_uv, band); var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_MOVE_TAPS; i = i + 1) { let sample_uv = tap_uv(target_uv, velocity_uv, i, phase, tap_total); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners( - tap, - sample_uv, - tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, tap_total), - tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, tap_total), - band - ); + if !all_inside { + tap = apply_rounded_corners( + tap, + sample_uv, + sample_uv + partners.x_step, + sample_uv + partners.y_step, + band + ); + } accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -258,19 +256,22 @@ fn path_zoom_taps( partners: QuadPartners, ) -> vec4 { let taps = f32(PATH_ZOOM_TAPS); - let band = tap_band(target_uv, scaled_dir, taps); + let band = tap_band(target_uv, scaled_dir); + let all_inside = taps_all_inside(target_uv, scaled_dir, band); var accum = vec3(0.0); var alpha_sum = 0.0; for (var i = 0; i < PATH_ZOOM_TAPS; i = i + 1) { let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, taps); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners( - tap, - sample_uv, - tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, taps), - tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, taps), - band - ); + if !all_inside { + tap = apply_rounded_corners( + tap, + sample_uv, + sample_uv + partners.x_step, + sample_uv + partners.y_step, + band + ); + } accum += tap.rgb * tap.a; alpha_sum += tap.a; } @@ -603,7 +604,7 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { crop_bounds_uv, shrink, p, - quad_partners(p, blur_mode), + quad_partners(p), ); accum = sums.rgb; alpha_sum = sums.a; @@ -613,19 +614,22 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { // t = 0, 0, 1/20 ... 19/20, divided by 21. The streak trails the // motion by just under a frame. let k = 20.0; - let partners = quad_partners(p, blur_mode); - let band = tap_band(target_uv, velocity_uv, k); + let partners = quad_partners(p); + let band = tap_band(target_uv, velocity_uv); + let all_inside = taps_all_inside(target_uv, velocity_uv, band); for (var i = 0; i < 20; i = i + 1) { let sample_uv = tap_uv(target_uv, velocity_uv, i, 0.0, k); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners( - tap, - sample_uv, - tap_uv(partners.x_uv, partners.x_path, i, 0.0, k), - tap_uv(partners.y_uv, partners.y_path, i, 0.0, k), - band - ); + if !all_inside { + tap = apply_rounded_corners( + tap, + sample_uv, + sample_uv + partners.x_step, + sample_uv + partners.y_step, + band + ); + } let weight = select(1.0, 2.0, i == 0); accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; @@ -676,27 +680,30 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { crop_bounds_uv, shrink, dither, - quad_partners(p, blur_mode), + quad_partners(p), ); accum = sums.rgb; alpha_sum = sums.a; weight_sum = f32(PATH_ZOOM_TAPS); } else { - let partners = quad_partners(p, blur_mode); - let band = tap_band(target_uv, scaled_dir, max_kernel); + let partners = quad_partners(p); + let band = tap_band(target_uv, scaled_dir); + let all_inside = taps_all_inside(target_uv, scaled_dir, band); for (var i = 0; i < 13; i = i + 1) { let percent = (f32(i) + dither) / max_kernel; let weight = 4.0 * (percent - percent * percent); let sample_uv = tap_uv(target_uv, scaled_dir, i, dither, max_kernel); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); - tap = apply_rounded_corners( - tap, - sample_uv, - tap_uv(partners.x_uv, partners.x_path, i, partners.x_phase, max_kernel), - tap_uv(partners.y_uv, partners.y_path, i, partners.y_phase, max_kernel), - band - ); + if !all_inside { + tap = apply_rounded_corners( + tap, + sample_uv, + sample_uv + partners.x_step, + sample_uv + partners.y_step, + band + ); + } accum += tap.rgb * tap.a * weight; alpha_sum += tap.a * weight; weight_sum += weight; @@ -1061,8 +1068,8 @@ fn card_distance(target_uv: vec2) -> f32 { ); } -// The card's coverage of a blur tap, its width taken from the same taps of -// the pixel's quad partners (QuadPartners) inside `band` of the edge. +// The card's coverage of a blur tap, its width taken from the same tap moved +// to the pixel's quad partners (QuadPartners) inside `band` of the edge. fn apply_rounded_corners( current_color: vec4, target_uv: vec2,