Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8a7c6aa

Browse files
committed
Cleanup and fix CPU blends.
1 parent 60a7072 commit 8a7c6aa

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

impeller/compiler/shader_lib/impeller/blending.glsl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
#include <impeller/constants.glsl>
1010
#include <impeller/types.glsl>
1111

12+
/// Composite a blended color onto the destination.
13+
///
14+
/// All three parameters are premultiplied, including `blend_result`, which
15+
/// is assumed to already be premultiplied with `src.a`.
1216
f16vec4 IPApplyBlendedColor(f16vec4 dst, f16vec4 src, f16vec3 blend_result) {
13-
// Mix the blended colors together, weighted by the destination alpha. This
14-
// color becomes the new source color for the alpha composite.
17+
// The destination alpha determines how blended the result looks. This
18+
// color becomes the new source color for the alpha composite step.
1519
f16vec3 blended = mix(src.rgb, blend_result, dst.a);
1620

1721
// Source-over blend atop the destination color.
18-
return f16vec4(blended, src.a) + dst * (1.0f - src.a);
22+
return f16vec4(blended, src.a) + dst * (1.0hf - src.a);
1923
}
2024

2125
//------------------------------------------------------------------------------

impeller/geometry/color.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,23 @@ static constexpr inline Color FromRGB(Vector3 color, Scalar alpha) {
193193
return {color.x, color.y, color.z, alpha};
194194
}
195195

196+
/// This routine is the same as `IPApplyBlendedColor` in the Impeller shader
197+
/// library, except all inputs are unpremultiplied.
196198
static constexpr inline Color DoColorBlend(
197-
Color d,
198-
Color s,
199+
Color dst,
200+
Color src,
199201
const std::function<Vector3(Vector3, Vector3)>& blend_rgb_func) {
200-
d = d.Premultiply();
201-
s = s.Premultiply();
202-
const Vector3 rgb = blend_rgb_func(ToRGB(d), ToRGB(s));
203-
const Color blended = Color::Lerp(s, FromRGB(rgb, d.alpha), d.alpha);
204-
return Color::Lerp(d, blended, s.alpha).Unpremultiply();
202+
dst = dst.Premultiply();
203+
src = src.Premultiply();
204+
205+
// The destination alpha determines how blended the result looks. This
206+
// color becomes the new source color for the alpha composite step.
207+
const Vector3 blend_result = blend_rgb_func(ToRGB(dst), ToRGB(src));
208+
209+
// Source-over blend atop the destination color.
210+
const Vector3 blended = ToRGB(src).Lerp(blend_result, dst.alpha);
211+
return (FromRGB(blended, src.alpha) + dst * (1.0f - src.alpha))
212+
.Unpremultiply();
205213
}
206214

207215
static constexpr inline Color DoColorBlendComponents(

0 commit comments

Comments
 (0)