diff --git a/crates/gpui_apple/src/shaders.metal b/crates/gpui_apple/src/shaders.metal index ef9163cdacb..680842f890d 100644 --- a/crates/gpui_apple/src/shaders.metal +++ b/crates/gpui_apple/src/shaders.metal @@ -386,11 +386,8 @@ fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]], } } - // Blend the border on top of the background and then linearly interpolate - // between the two as we slide inside the background. - float4 blended_border = over(background, border_color); - color = mix(background, blended_border, - saturate(antialias_threshold - inner_sdf)); + border_color.a *= saturate(antialias_threshold - inner_sdf); + color = over(background, border_color); } return color * float4(1.0, 1.0, 1.0, saturate(antialias_threshold - outer_sdf)); @@ -1200,12 +1197,10 @@ float4 distance_from_clip_rect_transformed(float2 unit_vertex, Bounds_ScaledPixe } float4 over(float4 below, float4 above) { - float4 result; float alpha = above.a + below.a * (1.0 - above.a); - result.rgb = - (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha; - result.a = alpha; - return result; + float3 premultiplied = + above.rgb * above.a + below.rgb * below.a * (1.0 - above.a); + return float4(premultiplied / (alpha == 0.0 ? 1.0 : alpha), alpha); } PreparedBackground prepare_background(Background background) { diff --git a/crates/gpui_wgpu/src/shaders.wgsl b/crates/gpui_wgpu/src/shaders.wgsl index 3bfa53fdbf6..8dd2d022329 100644 --- a/crates/gpui_wgpu/src/shaders.wgsl +++ b/crates/gpui_wgpu/src/shaders.wgsl @@ -330,8 +330,8 @@ fn oklab_to_linear_srgb(color: vec4) -> vec4 { fn over(below: vec4, above: vec4) -> vec4 { let alpha = above.a + below.a * (1.0 - above.a); - let color = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha; - return vec4(color, alpha); + let premultiplied = above.rgb * above.a + below.rgb * below.a * (1.0 - above.a); + return vec4(premultiplied / select(alpha, 1.0, alpha == 0.0), alpha); } // Selects corner radius based on quadrant. @@ -901,11 +901,8 @@ fn fs_quad(input: QuadVarying) -> @location(0) vec4 { } } - // Blend the border on top of the background and then linearly interpolate - // between the two as we slide inside the background. - let blended_border = over(background, border_color); - color = mix(background, blended_border, - saturate(antialias_threshold - inner_sdf)); + border_color.a *= saturate(antialias_threshold - inner_sdf); + color = over(background, border_color); } return blend_color(color, saturate(antialias_threshold - outer_sdf)); diff --git a/crates/gpui_windows/src/shaders.hlsl b/crates/gpui_windows/src/shaders.hlsl index 87882544fca..e849773b109 100644 --- a/crates/gpui_windows/src/shaders.hlsl +++ b/crates/gpui_windows/src/shaders.hlsl @@ -248,11 +248,9 @@ float4 oklab_to_srgb(float4 color) { } float4 over(float4 below, float4 above) { - float4 result; float alpha = above.a + below.a * (1.0 - above.a); - result.rgb = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha; - result.a = alpha; - return result; + float3 premultiplied = above.rgb * above.a + below.rgb * below.a * (1.0 - above.a); + return float4(premultiplied / (alpha == 0.0 ? 1.0 : alpha), alpha); } float2 to_tile_position(float2 unit_vertex, AtlasTile tile) { @@ -830,11 +828,8 @@ float4 quad_fragment(QuadFragmentInput input): SV_Target { } } - // Blend the border on top of the background and then linearly interpolate - // between the two as we slide inside the background. - float4 blended_border = over(background, border_color); - color = lerp(background, blended_border, - saturate(antialias_threshold - inner_sdf)); + border_color.a *= saturate(antialias_threshold - inner_sdf); + color = over(background, border_color); } return color * float4(1.0, 1.0, 1.0, saturate(antialias_threshold - outer_sdf));