Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/rendering/src/shaders/composite-video-frame-plain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn path_move_taps(
crop_bounds_uv: vec4<f32>,
shrink: ShrinkRing,
p: vec2<f32>,
partners: QuadPartners,
) -> vec4<f32> {
return vec4<f32>(0.0);
}
Expand All @@ -82,6 +83,7 @@ fn path_zoom_taps(
crop_bounds_uv: vec4<f32>,
shrink: ShrinkRing,
dither: f32,
partners: QuadPartners,
) -> vec4<f32> {
return vec4<f32>(0.0);
}
202 changes: 182 additions & 20 deletions crates/rendering/src/shaders/composite-video-frame.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,78 @@ fn interleaved_noise(p: vec2<f32>) -> f32 {
const PATH_MOVE_TAPS: i32 = 20;
const PATH_ZOOM_TAPS: i32 = 16;

// 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_step: vec2<f32>,
y_step: vec2<f32>,
}

fn quad_partners(p: vec2<f32>) -> QuadPartners {
let odd = fract(floor(p) * 0.5) > vec2<f32>(0.25);
let step = select(vec2<f32>(1.0), vec2<f32>(-1.0), odd) / uniforms.target_size;
return QuadPartners(vec2<f32>(step.x, 0.0), vec2<f32>(0.0, step.y));
}

// 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.
fn zoom_ray_uv(target_uv: vec2<f32>) -> vec2<f32> {
let dir = uniforms.motion_blur_zoom_center - target_uv;
let center_dist = length(dir);
let ray_dir = select(dir / center_dist, vec2<f32>(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;
}

// Where tap `i` of `taps` samples along `path_uv` for a pixel at
// `target_uv`. Every tap loop goes through here.
fn tap_uv(target_uv: vec2<f32>, path_uv: vec2<f32>, i: i32, phase: f32, taps: f32) -> vec2<f32> {
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 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<f32>, path_uv: vec2<f32>) -> f32 {
let reach = length(path_uv * uniforms.target_size);
let band = 4.0;
let centered = abs((target_uv - vec2<f32>(0.5)) * uniforms.target_size);
if any(centre_line_jumps() & (centered < vec2<f32>(reach + 2.0 * band))) {
return 3.0e38;
}
return band;
}

// 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<bool> {
let r = uniforms.rounding_px * uniforms.corner_radii;
let half = uniforms.target_size * 0.5 + vec2<f32>(0.5);
return vec2<bool>(
(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)
);
}

// 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<f32>, path_uv: vec2<f32>, 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
Expand Down Expand Up @@ -147,15 +219,26 @@ fn path_move_taps(
crop_bounds_uv: vec4<f32>,
shrink: ShrinkRing,
p: vec2<f32>,
partners: QuadPartners,
) -> vec4<f32> {
let phase = interleaved_noise(p);
let tap_total = f32(PATH_MOVE_TAPS);
let band = tap_band(target_uv, velocity_uv);
let all_inside = taps_all_inside(target_uv, velocity_uv, band);
var accum = vec3<f32>(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);
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;
}
Expand All @@ -170,14 +253,25 @@ fn path_zoom_taps(
crop_bounds_uv: vec4<f32>,
shrink: ShrinkRing,
dither: f32,
partners: QuadPartners,
) -> vec4<f32> {
let taps = f32(PATH_ZOOM_TAPS);
let band = tap_band(target_uv, scaled_dir);
let all_inside = taps_all_inside(target_uv, scaled_dir, band);
var accum = vec3<f32>(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);
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;
}
Expand Down Expand Up @@ -223,8 +317,17 @@ fn coverage_from_distance(distance: f32, anti_alias_width: f32) -> f32 {

fn rounded_rect_coverage(p: vec2<f32>, b: vec2<f32>, 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<f32>,
b: vec2<f32>,
r: f32,
rounding_type: f32,
distance: f32,
anti_alias_width: f32,
) -> f32 {
if distance <= -anti_alias_width {
return 1.0;
}
Expand Down Expand Up @@ -495,7 +598,14 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
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),
);
accum = sums.rgb;
alpha_sum = sums.a;
tap_total = f32(PATH_MOVE_TAPS);
Expand All @@ -504,11 +614,22 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
// 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);
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 = 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);
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;
Expand Down Expand Up @@ -544,11 +665,7 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
// 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);

Expand All @@ -557,18 +674,36 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
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),
);
accum = sums.rgb;
alpha_sum = sums.a;
weight_sum = f32(PATH_ZOOM_TAPS);
} else {
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 = 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);
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;
Expand Down Expand Up @@ -923,15 +1058,42 @@ fn sample_premultiplied(uv: vec2<f32>) -> vec4<f32> {
return mix(top, bottom, fraction.y);
}

fn apply_rounded_corners(current_color: vec4<f32>, target_uv: vec2<f32>) -> vec4<f32> {
fn card_distance(target_uv: vec2<f32>) -> f32 {
let centered_uv = (target_uv - vec2<f32>(0.5)) * uniforms.target_size;
let half_size = uniforms.target_size * 0.5;
let coverage = rounded_rect_coverage(
return sdf_rounded_rect(
centered_uv,
half_size,
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 tap moved
// to the pixel's quad partners (QuadPartners) inside `band` of the edge.
fn apply_rounded_corners(
current_color: vec4<f32>,
target_uv: vec2<f32>,
partner_x_uv: vec2<f32>,
partner_y_uv: vec2<f32>,
band: f32,
) -> vec4<f32> {
let centered_uv = (target_uv - vec2<f32>(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,
distance,
max(abs(step_x) + abs(step_y), 1.0)
);

return vec4(current_color.rgb, current_color.a * coverage);
}
Expand Down
Loading