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

Commit 87e9ddb

Browse files
bsalomonSkia Commit-Bot
authored andcommitted
GrGLSLShaderBuilder::appendTextureLookupAndBlend
Change-Id: Ifbc80e31a7a56cd40734cb174b77a5860a94a323 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/254682 Commit-Queue: Brian Salomon <[email protected]> Reviewed-by: Brian Osman <[email protected]>
1 parent e3f34a6 commit 87e9ddb

File tree

7 files changed

+89
-67
lines changed

7 files changed

+89
-67
lines changed

src/gpu/effects/GrTextureDomain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder,
111111
const char* inModulateColor) {
112112
auto appendTextureSample = [&sampler, inModulateColor, builder](const char* coord) {
113113
builder->codeAppend("half4 textureColor = ");
114-
builder->appendTextureLookupAndModulate(inModulateColor, sampler, coord);
114+
builder->appendTextureLookupAndBlend(inModulateColor, SkBlendMode::kModulate, sampler,
115+
coord);
115116
builder->codeAppend(";");
116117
return SkString("textureColor");
117118
};

src/gpu/glsl/GrGLSLBlend.cpp

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,49 @@
99
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
1010
#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
1111

12-
void GrGLSLBlend::AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor,
13-
const char* dstColor, const char* outColor,
14-
SkBlendMode mode) {
15-
// When and if the SkSL compiler supports inlining we could replace this with
16-
// out = blend(mode, src, dst) where mode is a literal.
17-
const char* name;
12+
namespace GrGLSLBlend {
13+
14+
const char* BlendFuncName(SkBlendMode mode) {
1815
switch (mode) {
19-
case SkBlendMode::kClear: name = "clear"; break;
20-
case SkBlendMode::kSrc: name = "src"; break;
21-
case SkBlendMode::kDst: name = "dst"; break;
22-
case SkBlendMode::kSrcOver: name = "src_over"; break;
23-
case SkBlendMode::kDstOver: name = "dst_over"; break;
24-
case SkBlendMode::kSrcIn: name = "src_in"; break;
25-
case SkBlendMode::kDstIn: name = "dst_in"; break;
26-
case SkBlendMode::kSrcOut: name = "src_out"; break;
27-
case SkBlendMode::kDstOut: name = "dst_out"; break;
28-
case SkBlendMode::kSrcATop: name = "src_atop"; break;
29-
case SkBlendMode::kDstATop: name = "dst_atop"; break;
30-
case SkBlendMode::kXor: name = "xor"; break;
31-
case SkBlendMode::kPlus: name = "plus"; break;
32-
case SkBlendMode::kModulate: name = "modulate"; break;
33-
case SkBlendMode::kScreen: name = "screen"; break;
34-
case SkBlendMode::kOverlay: name = "overlay"; break;
35-
case SkBlendMode::kDarken: name = "darken"; break;
36-
case SkBlendMode::kLighten: name = "lighten"; break;
37-
case SkBlendMode::kColorDodge: name = "color_dodge"; break;
38-
case SkBlendMode::kColorBurn: name = "color_burn"; break;
39-
case SkBlendMode::kHardLight: name = "hard_light"; break;
40-
case SkBlendMode::kSoftLight: name = "soft_light"; break;
41-
case SkBlendMode::kDifference: name = "difference"; break;
42-
case SkBlendMode::kExclusion: name = "exclusion"; break;
43-
case SkBlendMode::kMultiply: name = "multiply"; break;
44-
case SkBlendMode::kHue: name = "hue"; break;
45-
case SkBlendMode::kSaturation: name = "saturation"; break;
46-
case SkBlendMode::kColor: name = "color"; break;
47-
case SkBlendMode::kLuminosity: name = "luminosity"; break;
16+
case SkBlendMode::kClear: return "blend_clear";
17+
case SkBlendMode::kSrc: return "blend_src";
18+
case SkBlendMode::kDst: return "blend_dst";
19+
case SkBlendMode::kSrcOver: return "blend_src_over";
20+
case SkBlendMode::kDstOver: return "blend_dst_over";
21+
case SkBlendMode::kSrcIn: return "blend_src_in";
22+
case SkBlendMode::kDstIn: return "blend_dst_in";
23+
case SkBlendMode::kSrcOut: return "blend_src_out";
24+
case SkBlendMode::kDstOut: return "blend_dst_out";
25+
case SkBlendMode::kSrcATop: return "blend_src_atop";
26+
case SkBlendMode::kDstATop: return "blend_dst_atop";
27+
case SkBlendMode::kXor: return "blend_xor";
28+
case SkBlendMode::kPlus: return "blend_plus";
29+
case SkBlendMode::kModulate: return "blend_modulate";
30+
case SkBlendMode::kScreen: return "blend_screen";
31+
case SkBlendMode::kOverlay: return "blend_overlay";
32+
case SkBlendMode::kDarken: return "blend_darken";
33+
case SkBlendMode::kLighten: return "blend_lighten";
34+
case SkBlendMode::kColorDodge: return "blend_color_dodge";
35+
case SkBlendMode::kColorBurn: return "blend_color_burn";
36+
case SkBlendMode::kHardLight: return "blend_hard_light";
37+
case SkBlendMode::kSoftLight: return "blend_soft_light";
38+
case SkBlendMode::kDifference: return "blend_difference";
39+
case SkBlendMode::kExclusion: return "blend_exclusion";
40+
case SkBlendMode::kMultiply: return "blend_multiply";
41+
case SkBlendMode::kHue: return "blend_hue";
42+
case SkBlendMode::kSaturation: return "blend_saturation";
43+
case SkBlendMode::kColor: return "blend_color";
44+
case SkBlendMode::kLuminosity: return "blend_luminosity";
4845
}
49-
fsBuilder->codeAppendf("%s = blend_%s(%s, %s);", outColor, name, srcColor, dstColor);
46+
SkUNREACHABLE;
47+
}
48+
49+
void AppendMode(GrGLSLFragmentBuilder* fsBuilder,
50+
const char* srcColor,
51+
const char* dstColor,
52+
const char* outColor,
53+
SkBlendMode mode) {
54+
fsBuilder->codeAppendf("%s = %s(%s, %s);", outColor, BlendFuncName(mode), srcColor, dstColor);
5055
}
56+
57+
} // namespace GrGLSLBlend

src/gpu/glsl/GrGLSLBlend.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@
99
#define GrGLBlend_DEFINED
1010

1111
#include "include/core/SkBlendMode.h"
12-
#include "include/core/SkRegion.h"
1312

1413
class GrGLSLFragmentBuilder;
1514

1615
namespace GrGLSLBlend {
17-
/*
18-
* Appends GLSL code to fsBuilder that assigns a specified blend of the srcColor and dstColor
19-
* variables to the outColor variable.
20-
*/
21-
void AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor,
22-
const char* dstColor, const char* outColor, SkBlendMode mode);
16+
/*
17+
* Returns the name of the built in blend function for a SkBlendMode.
18+
* When and if the SkSL compiler supports inlining it'd be simpler to just call
19+
* blend(mode, src, dst) where mode is a literal when the desired blend mode is
20+
* not variable.
21+
*/
22+
const char* BlendFuncName(SkBlendMode mode);
23+
24+
/*
25+
* Appends GLSL code to fsBuilder that assigns a specified blend of the srcColor and dstColor
26+
* variables to the outColor variable.
27+
*/
28+
void AppendMode(GrGLSLFragmentBuilder* fsBuilder,
29+
const char* srcColor,
30+
const char* dstColor,
31+
const char* outColor,
32+
SkBlendMode mode);
2333
};
2434

2535
#endif

src/gpu/glsl/GrGLSLShaderBuilder.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "src/gpu/GrShaderCaps.h"
1111
#include "src/gpu/GrShaderVar.h"
1212
#include "src/gpu/GrSwizzle.h"
13+
#include "src/gpu/glsl/GrGLSLBlend.h"
1314
#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
1415
#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
1516

@@ -84,18 +85,21 @@ void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
8485
this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
8586
}
8687

87-
void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
88-
const char* modulation,
89-
SamplerHandle samplerHandle,
90-
const char* coordName,
91-
GrSLType varyingType,
92-
GrGLSLColorSpaceXformHelper* colorXformHelper) {
88+
void GrGLSLShaderBuilder::appendTextureLookupAndBlend(
89+
const char* dst,
90+
SkBlendMode mode,
91+
SamplerHandle samplerHandle,
92+
const char* coordName,
93+
GrSLType varyingType,
94+
GrGLSLColorSpaceXformHelper* colorXformHelper) {
95+
if (!dst) {
96+
dst = "half4(1)";
97+
}
9398
SkString lookup;
99+
this->codeAppendf("%s(", GrGLSLBlend::BlendFuncName(mode));
94100
this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
95101
this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
96-
if (modulation) {
97-
this->codeAppendf(" * %s", modulation);
98-
}
102+
this->codeAppendf(", %s)", dst);
99103
}
100104

101105
void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,

src/gpu/glsl/GrGLSLShaderBuilder.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ class GrGLSLShaderBuilder {
4444
GrSLType coordType = kHalf2_GrSLType,
4545
GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
4646

47-
48-
/** Does the work of appendTextureLookup and modulates the result by modulation. The result is
49-
always a half4. modulation and the swizzle specified by SamplerHandle must both be
50-
half4 or half. If modulation is "" or nullptr it this function acts as though
51-
appendTextureLookup were called. */
52-
void appendTextureLookupAndModulate(const char* modulation,
53-
SamplerHandle,
54-
const char* coordName,
55-
GrSLType coordType = kHalf2_GrSLType,
56-
GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
47+
/** Does the work of appendTextureLookup and blends the result by dst, treating the texture
48+
lookup a the src input to the blend. The dst is assumed to be half4 and the result is always
49+
a half4. If dst is nullptr we use half4(1) as the blend dst. */
50+
void appendTextureLookupAndBlend(const char* dst,
51+
SkBlendMode,
52+
SamplerHandle,
53+
const char* coordName,
54+
GrSLType coordType = kHalf2_GrSLType,
55+
GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
5756

5857
/** Adds a helper function to facilitate color gamut transformation, and produces code that
5958
returns the srcColor transformed into a new gamut (via multiplication by the xform from

src/gpu/ops/GrLatticeOp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ class LatticeGP : public GrGeometryProcessor {
7777
args.fOutputColor,
7878
Interpolation::kCanBeFlat);
7979
args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
80-
args.fFragBuilder->appendTextureLookupAndModulate(
80+
args.fFragBuilder->appendTextureLookupAndBlend(
8181
args.fOutputColor,
82+
SkBlendMode::kModulate,
8283
args.fTexSamplers[0],
8384
"clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
8485
kFloat2_GrSLType,

src/gpu/ops/GrQuadPerEdgeAA.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,9 @@ class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
664664

665665
// Now modulate the starting output color by the texture lookup
666666
args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
667-
args.fFragBuilder->appendTextureLookupAndModulate(
668-
args.fOutputColor, args.fTexSamplers[0], "texCoord", kFloat2_GrSLType,
669-
&fTextureColorSpaceXformHelper);
667+
args.fFragBuilder->appendTextureLookupAndBlend(
668+
args.fOutputColor, SkBlendMode::kModulate, args.fTexSamplers[0],
669+
"texCoord", kFloat2_GrSLType, &fTextureColorSpaceXformHelper);
670670
args.fFragBuilder->codeAppend(";");
671671
if (gp.fSaturate == Saturate::kYes) {
672672
args.fFragBuilder->codeAppendf("%s = saturate(%s);",

0 commit comments

Comments
 (0)