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

Commit 1486cc5

Browse files
authored
[Impeller] Add shader library; reduce branching in advanced blends (#34349)
1 parent 45c3552 commit 1486cc5

File tree

15 files changed

+199
-74
lines changed

15 files changed

+199
-74
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ FILE: ../../../flutter/impeller/compiler/reflector.cc
484484
FILE: ../../../flutter/impeller/compiler/reflector.h
485485
FILE: ../../../flutter/impeller/compiler/runtime_stage_data.cc
486486
FILE: ../../../flutter/impeller/compiler/runtime_stage_data.h
487+
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/branching.glsl
488+
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/color.glsl
489+
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/constants.glsl
490+
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/texture.glsl
491+
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/types.glsl
487492
FILE: ../../../flutter/impeller/compiler/source_options.cc
488493
FILE: ../../../flutter/impeller/compiler/source_options.h
489494
FILE: ../../../flutter/impeller/compiler/switches.cc
@@ -611,7 +616,6 @@ FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.frag
611616
FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.vert
612617
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.frag
613618
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.vert
614-
FILE: ../../../flutter/impeller/entity/shaders/utils.sl.h
615619
FILE: ../../../flutter/impeller/entity/shaders/vertices.frag
616620
FILE: ../../../flutter/impeller/entity/shaders/vertices.vert
617621
FILE: ../../../flutter/impeller/geometry/color.cc
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef BRANCHING_GLSL_
6+
#define BRANCHING_GLSL_
7+
8+
#include <impeller/constants.glsl>
9+
#include <impeller/types.glsl>
10+
11+
/// Perform an equality check for each vec3 component.
12+
///
13+
/// Returns 1.0 if x == y, otherwise 0.0.
14+
BoolV3 IPVec3IsEqual(vec3 x, float y) {
15+
vec3 diff = abs(x - y);
16+
return vec3(diff.r < kEhCloseEnough, //
17+
diff.g < kEhCloseEnough, //
18+
diff.b < kEhCloseEnough);
19+
}
20+
21+
/// Perform a branchless greater than check.
22+
///
23+
/// Returns 1.0 if x > y, otherwise 0.0.
24+
BoolF IPFloatIsGreaterThan(float x, float y) {
25+
return max(sign(x - y), 0);
26+
}
27+
28+
/// Perform a branchless greater than check for each vec3 component.
29+
///
30+
/// Returns 1.0 if x > y, otherwise 0.0.
31+
BoolV3 IPVec3IsGreaterThan(vec3 x, vec3 y) {
32+
return max(sign(x - y), 0);
33+
}
34+
35+
/// Perform a branchless less than check.
36+
///
37+
/// Returns 1.0 if x < y, otherwise 0.0.
38+
BoolF IPFloatIsLessThan(float x, float y) {
39+
return max(sign(y - x), 0);
40+
}
41+
42+
/// For each vec3 component, if value > cutoff, return b, otherwise return a.
43+
vec3 IPVec3ChooseCutoff(vec3 a, vec3 b, vec3 value, float cutoff) {
44+
return mix(a, b, IPVec3IsGreaterThan(value, vec3(cutoff)));
45+
}
46+
47+
/// For each vec3 component, if value > 0.5, return b, otherwise return a.
48+
vec3 IPVec3Choose(vec3 a, vec3 b, vec3 value) {
49+
return IPVec3ChooseCutoff(a, b, value, 0.5);
50+
}
51+
52+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef COLOR_GLSL_
6+
#define COLOR_GLSL_
7+
8+
#include <impeller/branching.glsl>
9+
10+
/// Convert a premultiplied color (a color which has its color components
11+
/// multiplied with its alpha value) to an unpremultiplied color.
12+
///
13+
/// Returns (0, 0, 0, 0) if the alpha component is 0.
14+
vec4 IPUnpremultiply(vec4 color) {
15+
if (color.a == 0) {
16+
return vec4(0);
17+
}
18+
return vec4(color.rgb / color.a, color.a);
19+
}
20+
21+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CONSTANTS_GLSL_
6+
#define CONSTANTS_GLSL_
7+
8+
const float kEhCloseEnough = 0.000001;
9+
10+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef TEXTURE_GLSL_
6+
#define TEXTURE_GLSL_
7+
8+
#include <impeller/branching.glsl>
9+
10+
/// Sample from a texture.
11+
///
12+
/// If `y_coord_scale` < 0.0, the Y coordinate is flipped. This is useful
13+
/// for Impeller graphics backends that use a flipped framebuffer coordinate
14+
/// space.
15+
vec4 IPSample(sampler2D texture_sampler, vec2 coords, float y_coord_scale) {
16+
if (y_coord_scale < 0.0) {
17+
coords.y = 1.0 - coords.y;
18+
}
19+
return texture(texture_sampler, coords);
20+
}
21+
22+
/// Sample a texture, emulating SamplerAddressMode::ClampToBorder.
23+
///
24+
/// This is useful for Impeller graphics backend that don't support
25+
/// ClampToBorder.
26+
vec4 IPSampleClampToBorder(sampler2D tex, vec2 uv) {
27+
float within_bounds = float(uv.x > 0 && uv.y > 0 && uv.x < 1 && uv.y < 1);
28+
return texture(tex, uv) * within_bounds;
29+
}
30+
31+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef TYPES_GLSL_
6+
#define TYPES_GLSL_
7+
8+
#define BoolF float
9+
#define BoolV2 vec2
10+
#define BoolV3 vec3
11+
#define BoolV4 vec4
12+
13+
#endif

impeller/entity/shaders/blending/advanced_blend.glsl

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <impeller/branching.glsl>
6+
#include <impeller/color.glsl>
7+
#include <impeller/texture.glsl>
8+
59
uniform BlendInfo {
610
float color_factor;
711
vec4 color; // This color input is expected to be unpremultiplied.
@@ -16,28 +20,13 @@ in vec2 v_src_texture_coords;
1620

1721
out vec4 frag_color;
1822

19-
// Emulate SamplerAddressMode::ClampToBorder.
20-
vec4 SampleWithBorder(sampler2D tex, vec2 uv) {
21-
if (uv.x > 0 && uv.y > 0 && uv.x < 1 && uv.y < 1) {
22-
return texture(tex, uv);
23-
}
24-
return vec4(0);
25-
}
26-
27-
vec4 Unpremultiply(vec4 color) {
28-
if (color.a == 0) {
29-
return vec4(0);
30-
}
31-
return vec4(color.rgb / color.a, color.a);
32-
}
33-
3423
void main() {
35-
vec4 dst = Unpremultiply(
36-
SampleWithBorder(texture_sampler_dst, v_dst_texture_coords));
24+
vec4 dst = IPUnpremultiply(
25+
IPSampleClampToBorder(texture_sampler_dst, v_dst_texture_coords));
3726
vec4 src = blend_info.color_factor > 0
3827
? blend_info.color
39-
: Unpremultiply(SampleWithBorder(texture_sampler_src,
40-
v_src_texture_coords));
28+
: IPUnpremultiply(IPSampleClampToBorder(texture_sampler_src,
29+
v_src_texture_coords));
4130

4231
vec3 blended = Blend(dst.rgb, src.rgb);
4332

impeller/entity/shaders/blending/advanced_blend_colorburn.frag

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
vec3 Blend(vec3 dst, vec3 src) {
88
// https://www.w3.org/TR/compositing-1/#blendingcolorburn
99
vec3 color = 1 - min(vec3(1), (1 - dst) / src);
10-
color = mix(color, vec3(1), ComponentIsValue(dst, 1.0));
11-
color = mix(color, vec3(0), ComponentIsValue(src, 0.0));
10+
if (1 - dst.r < kEhCloseEnough) {
11+
color.r = 1;
12+
}
13+
if (1 - dst.g < kEhCloseEnough) {
14+
color.g = 1;
15+
}
16+
if (1 - dst.b < kEhCloseEnough) {
17+
color.b = 1;
18+
}
19+
if (src.r < kEhCloseEnough) {
20+
color.r = 0;
21+
}
22+
if (src.g < kEhCloseEnough) {
23+
color.g = 0;
24+
}
25+
if (src.b < kEhCloseEnough) {
26+
color.b = 0;
27+
}
1228
return color;
1329
}
1430

impeller/entity/shaders/blending/advanced_blend_colordodge.frag

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
vec3 Blend(vec3 dst, vec3 src) {
88
// https://www.w3.org/TR/compositing-1/#blendingcolordodge
99
vec3 color = min(vec3(1), dst / (1 - src));
10-
color = mix(color, vec3(0), ComponentIsValue(dst, 0.0));
11-
color = mix(color, vec3(1), ComponentIsValue(src, 1.0));
10+
if (dst.r < kEhCloseEnough) {
11+
color.r = 0;
12+
}
13+
if (dst.g < kEhCloseEnough) {
14+
color.g = 0;
15+
}
16+
if (dst.b < kEhCloseEnough) {
17+
color.b = 0;
18+
}
19+
if (1 - src.r < kEhCloseEnough) {
20+
color.r = 1;
21+
}
22+
if (1 - src.g < kEhCloseEnough) {
23+
color.g = 1;
24+
}
25+
if (1 - src.b < kEhCloseEnough) {
26+
color.b = 1;
27+
}
1228
return color;
1329
}
1430

impeller/entity/shaders/blending/advanced_blend_softlight.frag

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
vec3 Blend(vec3 dst, vec3 src) {
88
// https://www.w3.org/TR/compositing-1/#blendingsoftlight
99

10-
vec3 D = MixComponents(((16 * dst - 12) * dst + 4) * dst, //
11-
sqrt(dst), //
12-
dst, //
13-
0.25);
10+
vec3 D = IPVec3ChooseCutoff(((16 * dst - 12) * dst + 4) * dst, //
11+
sqrt(dst), //
12+
dst, //
13+
0.25);
1414

15-
return MixHalf(dst - (1 - 2 * src) * dst * (1 - dst), //
16-
dst + (2 * src - 1) * (D - dst), //
17-
src);
15+
return IPVec3Choose(dst - (1 - 2 * src) * dst * (1 - dst), //
16+
dst + (2 * src - 1) * (D - dst), //
17+
src);
1818
}
1919

2020
#include "advanced_blend.glsl"

0 commit comments

Comments
 (0)