From 8a5cf7e23a7ce5797bb020a4b7be2ef1b7dc8f79 Mon Sep 17 00:00:00 2001 From: John Tur Date: Thu, 12 Nov 2026 08:19:32 +0000 Subject: [PATCH] gpui: Use Levien's shadow approximation (#66812) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GPUI draws shadows using Evan Wallace's technique: the blur along x is computed in closed form with the error function, and the blur along y is integrated numerically with four samples. Each pixel runs that loop, which evaluates the error function eight times, plus four square roots and four exponentials. We replace it with [Raph Levien's closed-form approximation](https://raphlinus.github.io/graphics/2020/04/21/blurred-rounded-rects.html) of a blurred rounded rectangle. The blurred shape is approximated as a superellipse whose radius and exponent are fitted to the blur, so each pixel needs one distance and two error function evaluations. Everything that depends only on the shadow is computed once per vertex and passed to the fragment shader through interpolators. The content in the `shadow` example differs from `main` by at most 5/255 per channel. On an Intel HD 4600, GPU time in the `shadow` example is reduced by 30% (2,008µs → 1,399µs). Release Notes: - Reduced GPU usage - [GPUI] Improved shadow rendering performance --- crates/gpui_apple/src/shaders.metal | 148 +++++++++++++++------------ crates/gpui_wgpu/src/shaders.wgsl | 120 ++++++++++++++-------- crates/gpui_windows/src/shaders.hlsl | 111 ++++++++++++-------- 3 files changed, 230 insertions(+), 149 deletions(-) diff --git a/crates/gpui_apple/src/shaders.metal b/crates/gpui_apple/src/shaders.metal index 4f1beb36325..ef9163cdacb 100644 --- a/crates/gpui_apple/src/shaders.metal +++ b/crates/gpui_apple/src/shaders.metal @@ -39,10 +39,6 @@ float pick_corner_radius(float2 center_to_point, Corners_ScaledPixels corner_rad float quad_sdf(float2 point, Bounds_ScaledPixels bounds, Corners_ScaledPixels corner_radii); float quad_sdf_impl(float2 center_to_point, float corner_radius); -float gaussian(float x, float sigma); -float2 erf(float2 x); -float blur_along_x(float x, float y, float sigma, float corner, - float2 half_size); float4 over(float4 below, float4 above); float radians(float degrees); @@ -483,12 +479,81 @@ fragment float4 opaque_quad_fragment(OpaqueQuadFragmentInput input return input.color; } +struct PreparedBlurredRoundedRect { + float4 shape; + float4 r1; + float4 exponent; + float4 scale; +}; + struct ShadowFragmentInput { float4 position [[position]]; float4 color [[flat]]; uint shadow_id [[flat]]; + float4 blur_shape [[flat]]; + float4 blur_r1 [[flat]]; + float4 blur_exponent [[flat]]; + float4 blur_scale [[flat]]; }; +float erf7(float x) { + x = clamp(x, -10., 10.) * (2. / sqrt(M_PI_F)); + float xx = x * x; + x = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx); + return x / sqrt(1. + x * x); +} + +// Raph Levien's blurred rounded rectangle approximation, whose blur s is sigma * sqrt(2): +// https://raphlinus.github.io/graphics/2020/04/21/blurred-rounded-rects.html +PreparedBlurredRoundedRect prepare_blurred_rounded_rect(float2 size, Corners_ScaledPixels corner_radii, float sigma) { + float s = max(sigma * sqrt(2.), 1e-6); + float s_inv = 1. / s; + float min_edge = min(size.x, size.y); + float r_max = 0.5 * min_edge; + float4 radii = float4(corner_radii.top_left, corner_radii.top_right, + corner_radii.bottom_right, corner_radii.bottom_left); + float4 r0 = min(sqrt(radii * radii + 1.15 * 1.15 * s * s), r_max); + float4 r1 = min(sqrt(radii * radii + 4. * s * s), r_max); + + float2 half_extent = 0.5 * s_inv * size; + float delta = 1.25 * s * (exp(-half_extent.x * half_extent.x) - exp(-half_extent.y * half_extent.y)); + float2 adjusted_size = float2(size.x + min(delta, 0.), size.y - max(delta, 0.)); + float4 peak = s_inv * 0.5 * (max(adjusted_size.x, adjusted_size.y) - 0.5 * radii); + + PreparedBlurredRoundedRect output; + output.shape = float4(0.5 * adjusted_size, s_inv, min_edge); + output.r1 = r1; + output.exponent = 2. * r1 / r0; + output.scale = 0.5 * float4(erf7(peak.x), erf7(peak.y), erf7(peak.z), erf7(peak.w)); + return output; +} + +float pick_corner(float2 center_to_point, float4 values) { + Corners_ScaledPixels corners = {values.x, values.y, values.z, values.w}; + return pick_corner_radius(center_to_point, corners); +} + +float blurred_rounded_rect(float2 center_to_point, PreparedBlurredRoundedRect prepared) { + float r1 = pick_corner(center_to_point, prepared.r1); + float exponent = pick_corner(center_to_point, prepared.exponent); + float scale = pick_corner(center_to_point, prepared.scale); + float s_inv = prepared.shape.z; + float min_edge = prepared.shape.w; + + float2 p0 = abs(center_to_point) - (prepared.shape.xy - r1); + float2 p1 = max(p0, 0.); + float d_pos; + if (p1.x > 0. && p1.y > 0.) { + d_pos = pow(pow(p1.x, exponent) + pow(p1.y, exponent), 1. / exponent); + } else { + d_pos = p1.x + p1.y; + } + + float d_neg = min(max(p0.x, p0.y), 0.); + float d = d_pos + d_neg - r1; + return scale * (erf7(s_inv * (min_edge + d)) - erf7(s_inv * d)); +} + vertex ShadowFragmentInput shadow_vertex( uint vertex_id [[vertex_id]], uint shadow_id [[instance_id]], constant Shadow *shadows [[buffer(ShadowInputIndex_Shadows)]], @@ -502,11 +567,18 @@ vertex ShadowFragmentInput shadow_vertex( float4 device_position = to_device_position_impl(clipped.position, viewport_size); float4 color = hsla_to_rgba(shadow.color); + PreparedBlurredRoundedRect blur = prepare_blurred_rounded_rect( + float2(shadow.shape_bounds.size.width, shadow.shape_bounds.size.height), + shadow.shape_corner_radii, shadow.blur_radius); return ShadowFragmentInput{ device_position, color, - shadow_id}; + shadow_id, + blur.shape, + blur.r1, + blur.exponent, + blur.scale}; } fragment float4 shadow_fragment(ShadowFragmentInput input [[stage_in]], @@ -514,48 +586,18 @@ fragment float4 shadow_fragment(ShadowFragmentInput input [[stage_in]], [[buffer(ShadowInputIndex_Shadows)]]) { Shadow shadow = shadows[input.shadow_id]; - float2 origin = float2(shadow.shape_bounds.origin.x, shadow.shape_bounds.origin.y); - float2 size = float2(shadow.shape_bounds.size.width, shadow.shape_bounds.size.height); - float2 half_size = size / 2.; - float2 center = origin + half_size; - float2 point = input.position.xy - center; - float corner_radius; - if (point.x < 0.) { - if (point.y < 0.) { - corner_radius = shadow.shape_corner_radii.top_left; - } else { - corner_radius = shadow.shape_corner_radii.bottom_left; - } - } else { - if (point.y < 0.) { - corner_radius = shadow.shape_corner_radii.top_right; - } else { - corner_radius = shadow.shape_corner_radii.bottom_right; - } - } - float alpha; if (shadow.blur_radius == 0.) { float distance = quad_sdf(input.position.xy, shadow.shape_bounds, shadow.shape_corner_radii); alpha = saturate(0.5 - distance); } else { - // The signal is only non-zero in a limited range, so don't waste samples - float low = point.y - half_size.y; - float high = point.y + half_size.y; - float start = clamp(-3. * shadow.blur_radius, low, high); - float end = clamp(3. * shadow.blur_radius, low, high); - - // Accumulate samples (we can get away with surprisingly few samples) - float step = (end - start) / 4.; - float y = start + step * 0.5; - alpha = 0.; - for (int i = 0; i < 4; i++) { - alpha += blur_along_x(point.x, point.y - y, shadow.blur_radius, - corner_radius, half_size) * - gaussian(y, shadow.blur_radius) * step; - y += step; - } + float2 origin = float2(shadow.shape_bounds.origin.x, shadow.shape_bounds.origin.y); + float2 size = float2(shadow.shape_bounds.size.width, shadow.shape_bounds.size.height); + float2 center = origin + size / 2.; + PreparedBlurredRoundedRect blur = {input.blur_shape, input.blur_r1, + input.blur_exponent, input.blur_scale}; + alpha = blurred_rounded_rect(input.position.xy - center, blur); } if (shadow.inset != 0u) { @@ -1124,30 +1166,6 @@ float quad_sdf_impl(float2 corner_center_to_point, float corner_radius) { } } -// A standard gaussian function, used for weighting samples -float gaussian(float x, float sigma) { - return exp(-(x * x) / (2. * sigma * sigma)) / (sqrt(2. * M_PI_F) * sigma); -} - -// This approximates the error function, needed for the gaussian integral -float2 erf(float2 x) { - float2 s = sign(x); - float2 a = abs(x); - float2 r1 = 1. + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a; - float2 r2 = r1 * r1; - return s - s / (r2 * r2); -} - -float blur_along_x(float x, float y, float sigma, float corner, - float2 half_size) { - float delta = min(half_size.y - corner - abs(y), 0.); - float curved = - half_size.x - corner + sqrt(max(0., corner * corner - delta * delta)); - float2 integral = - 0.5 + 0.5 * erf((x + float2(-curved, curved)) * (sqrt(0.5) / sigma)); - return integral.y - integral.x; -} - ClippedVertex clip_to_mask(float2 unit_vertex, Bounds_ScaledPixels bounds, Bounds_ScaledPixels mask) { float2 bounds_origin = float2(bounds.origin.x, bounds.origin.y); diff --git a/crates/gpui_wgpu/src/shaders.wgsl b/crates/gpui_wgpu/src/shaders.wgsl index b0c7b82237b..69a5338d3c5 100644 --- a/crates/gpui_wgpu/src/shaders.wgsl +++ b/crates/gpui_wgpu/src/shaders.wgsl @@ -335,27 +335,6 @@ fn over(below: vec4, above: vec4) -> vec4 { return vec4(color, alpha); } -// A standard gaussian function, used for weighting samples -fn gaussian(x: f32, sigma: f32) -> f32{ - return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * M_PI_F) * sigma); -} - -// This approximates the error function, needed for the gaussian integral -fn erf(v: vec2) -> vec2 { - let s = sign(v); - let a = abs(v); - let r1 = 1.0 + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a; - let r2 = r1 * r1; - return s - s / (r2 * r2); -} - -fn blur_along_x(x: f32, y: f32, sigma: f32, corner: f32, half_size: vec2) -> f32 { - let delta = min(half_size.y - corner - abs(y), 0.0); - let curved = half_size.x - corner + sqrt(max(0.0, corner * corner - delta * delta)); - let integral = 0.5 + 0.5 * erf((x + vec2(-curved, curved)) * (sqrt(0.5) / sigma)); - return integral.y - integral.x; -} - // Selects corner radius based on quadrant. fn pick_corner_radius(center_to_point: vec2, radii: Corners) -> f32 { if (center_to_point.x < 0.0) { @@ -1041,6 +1020,74 @@ struct ShadowVarying { @builtin(position) position: vec4, @location(0) @interpolate(flat) color: vec4, @location(1) @interpolate(flat) shadow_id: u32, + @location(2) @interpolate(flat) blur_shape: vec4, + @location(3) @interpolate(flat) blur_r1: vec4, + @location(4) @interpolate(flat) blur_exponent: vec4, + @location(5) @interpolate(flat) blur_scale: vec4, +} + +struct PreparedBlurredRoundedRect { + shape: vec4, + r1: vec4, + exponent: vec4, + scale: vec4, +} + +fn erf7(input: f32) -> f32 { + let x = clamp(input, -10.0, 10.0) * (2.0 / sqrt(M_PI_F)); + let xx = x * x; + let y = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx); + return y / sqrt(1.0 + y * y); +} + +// Raph Levien's blurred rounded rectangle approximation, whose blur s is sigma * sqrt(2): +// https://raphlinus.github.io/graphics/2020/04/21/blurred-rounded-rects.html +fn prepare_blurred_rounded_rect(size: vec2, corner_radii: Corners, sigma: f32) -> PreparedBlurredRoundedRect { + let s = max(sigma * sqrt(2.0), 1e-6); + let s_inv = 1.0 / s; + let min_edge = min(size.x, size.y); + let r_max = 0.5 * min_edge; + let radii = vec4(corner_radii.top_left, corner_radii.top_right, + corner_radii.bottom_right, corner_radii.bottom_left); + let r0 = min(sqrt(radii * radii + 1.15 * 1.15 * s * s), vec4(r_max)); + let r1 = min(sqrt(radii * radii + 4.0 * s * s), vec4(r_max)); + + let half_extent = 0.5 * s_inv * size; + let delta = 1.25 * s * (exp(-half_extent.x * half_extent.x) - exp(-half_extent.y * half_extent.y)); + let adjusted_size = vec2(size.x + min(delta, 0.0), size.y - max(delta, 0.0)); + let peak = s_inv * 0.5 * (max(adjusted_size.x, adjusted_size.y) - 0.5 * radii); + + var output: PreparedBlurredRoundedRect; + output.shape = vec4(0.5 * adjusted_size, s_inv, min_edge); + output.r1 = r1; + output.exponent = 2.0 * r1 / r0; + output.scale = 0.5 * vec4(erf7(peak.x), erf7(peak.y), erf7(peak.z), erf7(peak.w)); + return output; +} + +fn pick_corner(center_to_point: vec2, values: vec4) -> f32 { + return pick_corner_radius(center_to_point, Corners(values.x, values.y, values.z, values.w)); +} + +fn blurred_rounded_rect(center_to_point: vec2, prepared: PreparedBlurredRoundedRect) -> f32 { + let r1 = pick_corner(center_to_point, prepared.r1); + let exponent = pick_corner(center_to_point, prepared.exponent); + let scale = pick_corner(center_to_point, prepared.scale); + let s_inv = prepared.shape.z; + let min_edge = prepared.shape.w; + + let p0 = abs(center_to_point) - (prepared.shape.xy - r1); + let p1 = max(p0, vec2(0.0)); + var d_pos: f32; + if (p1.x > 0.0 && p1.y > 0.0) { + d_pos = pow(pow(p1.x, exponent) + pow(p1.y, exponent), 1.0 / exponent); + } else { + d_pos = p1.x + p1.y; + } + + let d_neg = min(max(p0.x, p0.y), 0.0); + let d = d_pos + d_neg - r1; + return scale * (erf7(s_inv * (min_edge + d)) - erf7(s_inv * d)); } @vertex @@ -1053,39 +1100,28 @@ fn vs_shadow(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) ins out.position = to_device_position_impl(clipped.position); out.color = hsla_to_rgba(shadow.color); out.shadow_id = instance_id; + let blur = prepare_blurred_rounded_rect(shadow.shape_bounds.size, shadow.shape_corner_radii, + shadow.blur_radius); + out.blur_shape = blur.shape; + out.blur_r1 = blur.r1; + out.blur_exponent = blur.exponent; + out.blur_scale = blur.scale; return out; } @fragment fn fs_shadow(input: ShadowVarying) -> @location(0) vec4 { let shadow = load_shadow(input.shadow_id); - let half_size = shadow.shape_bounds.size / 2.0; - let center = shadow.shape_bounds.origin + half_size; - let center_to_point = input.position.xy - center; - - let corner_radius = pick_corner_radius(center_to_point, shadow.shape_corner_radii); var alpha: f32; if (shadow.blur_radius == 0.0) { let distance = quad_sdf(input.position.xy, shadow.shape_bounds, shadow.shape_corner_radii); alpha = saturate(0.5 - distance); } else { - // The signal is only non-zero in a limited range, so don't waste samples - let low = center_to_point.y - half_size.y; - let high = center_to_point.y + half_size.y; - let start = clamp(-3.0 * shadow.blur_radius, low, high); - let end = clamp(3.0 * shadow.blur_radius, low, high); - - // Accumulate samples (we can get away with surprisingly few samples) - let step = (end - start) / 4.0; - var y = start + step * 0.5; - alpha = 0.0; - for (var i = 0; i < 4; i += 1) { - let blur = blur_along_x(center_to_point.x, center_to_point.y - y, - shadow.blur_radius, corner_radius, half_size); - alpha += blur * gaussian(y, shadow.blur_radius) * step; - y += step; - } + let center = shadow.shape_bounds.origin + shadow.shape_bounds.size / 2.0; + let blur = PreparedBlurredRoundedRect(input.blur_shape, input.blur_r1, + input.blur_exponent, input.blur_scale); + alpha = blurred_rounded_rect(input.position.xy - center, blur); } if (shadow.inset != 0u) { diff --git a/crates/gpui_windows/src/shaders.hlsl b/crates/gpui_windows/src/shaders.hlsl index 8d9f92a4f00..87882544fca 100644 --- a/crates/gpui_windows/src/shaders.hlsl +++ b/crates/gpui_windows/src/shaders.hlsl @@ -247,27 +247,6 @@ float4 oklab_to_srgb(float4 color) { return float4(linear_to_srgb(linear_rgb), color.a); } -// This approximates the error function, needed for the gaussian integral -float2 erf(float2 x) { - float2 s = sign(x); - float2 a = abs(x); - x = 1. + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a; - x *= x; - return s - s / (x * x); -} - -float blur_along_x(float x, float y, float sigma, float corner, float2 half_size) { - float delta = min(half_size.y - corner - abs(y), 0.); - float curved = half_size.x - corner + sqrt(max(0., corner * corner - delta * delta)); - float2 integral = 0.5 + 0.5 * erf((x + float2(-curved, curved)) * (sqrt(0.5) / sigma)); - return integral.y - integral.x; -} - -// A standard gaussian function, used for weighting samples -float gaussian(float x, float sigma) { - return exp(-(x * x) / (2. * sigma * sigma)) / (sqrt(2. * M_PI_F) * sigma); -} - float4 over(float4 below, float4 above) { float4 result; float alpha = above.a + below.a * (1.0 - above.a); @@ -915,14 +894,80 @@ struct Shadow { uint pad; // align to 8 bytes }; +struct PreparedBlurredRoundedRect { + nointerpolation float4 shape: TEXCOORD1; + nointerpolation float4 r1: TEXCOORD2; + nointerpolation float4 exponent: TEXCOORD3; + nointerpolation float4 scale: TEXCOORD4; +}; + struct ShadowFragmentInput { nointerpolation uint shadow_id: TEXCOORD0; float4 position: SV_Position; nointerpolation float4 color: COLOR; + PreparedBlurredRoundedRect blur; }; StructuredBuffer shadows: register(t1); +float erf7(float x) { + x = clamp(x, -10., 10.) * (2. / sqrt(M_PI_F)); + float xx = x * x; + x = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx); + return x / sqrt(1. + x * x); +} + +// Raph Levien's blurred rounded rectangle approximation, whose blur s is sigma * sqrt(2): +// https://raphlinus.github.io/graphics/2020/04/21/blurred-rounded-rects.html +PreparedBlurredRoundedRect prepare_blurred_rounded_rect(float2 size, Corners corner_radii, float sigma) { + float s = max(sigma * sqrt(2.), 1e-6); + float s_inv = 1. / s; + float min_edge = min(size.x, size.y); + float r_max = 0.5 * min_edge; + float4 radii = float4(corner_radii.top_left, corner_radii.top_right, + corner_radii.bottom_right, corner_radii.bottom_left); + float4 r0 = min(sqrt(radii * radii + 1.15 * 1.15 * s * s), r_max); + float4 r1 = min(sqrt(radii * radii + 4. * s * s), r_max); + + float2 half_extent = 0.5 * s_inv * size; + float delta = 1.25 * s * (exp(-half_extent.x * half_extent.x) - exp(-half_extent.y * half_extent.y)); + float2 adjusted_size = float2(size.x + min(delta, 0.), size.y - max(delta, 0.)); + float4 peak = s_inv * 0.5 * (max(adjusted_size.x, adjusted_size.y) - 0.5 * radii); + + PreparedBlurredRoundedRect output; + output.shape = float4(0.5 * adjusted_size, s_inv, min_edge); + output.r1 = r1; + output.exponent = 2. * r1 / r0; + output.scale = 0.5 * float4(erf7(peak.x), erf7(peak.y), erf7(peak.z), erf7(peak.w)); + return output; +} + +float pick_corner(float2 center_to_point, float4 values) { + Corners corners = { values.x, values.y, values.z, values.w }; + return pick_corner_radius(center_to_point, corners); +} + +float blurred_rounded_rect(float2 center_to_point, PreparedBlurredRoundedRect prepared) { + float r1 = pick_corner(center_to_point, prepared.r1); + float exponent = pick_corner(center_to_point, prepared.exponent); + float scale = pick_corner(center_to_point, prepared.scale); + float s_inv = prepared.shape.z; + float min_edge = prepared.shape.w; + + float2 p0 = abs(center_to_point) - (prepared.shape.xy - r1); + float2 p1 = max(p0, 0.); + float d_pos; + [branch] if (p1.x > 0. && p1.y > 0.) { + d_pos = pow(pow(p1.x, exponent) + pow(p1.y, exponent), 1. / exponent); + } else { + d_pos = p1.x + p1.y; + } + + float d_neg = min(max(p0.x, p0.y), 0.); + float d = d_pos + d_neg - r1; + return scale * (erf7(s_inv * (min_edge + d)) - erf7(s_inv * d)); +} + ShadowFragmentInput shadow_vertex(uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID) { float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u)); uint shadow_id = batch_start_index + instance_id; @@ -936,6 +981,7 @@ ShadowFragmentInput shadow_vertex(uint vertex_id: SV_VertexID, uint instance_id: output.position = device_position; output.color = color; output.shadow_id = shadow_id; + output.blur = prepare_blurred_rounded_rect(shadow.shape_bounds.size, shadow.shape_corner_radii, shadow.blur_radius); return output; } @@ -943,32 +989,13 @@ ShadowFragmentInput shadow_vertex(uint vertex_id: SV_VertexID, uint instance_id: float4 shadow_fragment(ShadowFragmentInput input): SV_TARGET { Shadow shadow = shadows[input.shadow_id]; - float2 half_size = shadow.shape_bounds.size / 2.; - float2 center = shadow.shape_bounds.origin + half_size; - float2 point0 = input.position.xy - center; - float corner_radius = pick_corner_radius(point0, shadow.shape_corner_radii); - float alpha; if (shadow.blur_radius == 0.) { float distance = quad_sdf(input.position.xy, shadow.shape_bounds, shadow.shape_corner_radii); alpha = saturate(0.5 - distance); } else { - // The signal is only non-zero in a limited range, so don't waste samples - float low = point0.y - half_size.y; - float high = point0.y + half_size.y; - float start = clamp(-3. * shadow.blur_radius, low, high); - float end = clamp(3. * shadow.blur_radius, low, high); - - // Accumulate samples (we can get away with surprisingly few samples) - float step = (end - start) / 4.; - float y = start + step * 0.5; - alpha = 0.; - for (int i = 0; i < 4; i++) { - alpha += blur_along_x(point0.x, point0.y - y, shadow.blur_radius, - corner_radius, half_size) * - gaussian(y, shadow.blur_radius) * step; - y += step; - } + float2 center = shadow.shape_bounds.origin + shadow.shape_bounds.size / 2.; + alpha = blurred_rounded_rect(input.position.xy - center, input.blur); } if (shadow.inset != 0u) {