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

Commit 12fb9cf

Browse files
Ethan NicholasSkia Commit-Bot
authored andcommitted
added SkSL saturate() function
Bug: skia:8220 Change-Id: Ib2e58ac77345a2aa53302c6c1484d52533556f93 Reviewed-on: https://skia-review.googlesource.com/145371 Reviewed-by: Greg Daniel <egdaniel@google.com> Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
1 parent 2e77f54 commit 12fb9cf

24 files changed

+261
-180
lines changed

src/core/SkColorMatrixFilterRowMajor255.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class ColorMatrixEffect : public GrFragmentProcessor {
221221
uniformHandler->getUniformCStr(fMatrixHandle),
222222
args.fInputColor,
223223
uniformHandler->getUniformCStr(fVectorHandle));
224-
fragBuilder->codeAppendf("\t%s = clamp(%s, 0.0, 1.0);\n",
224+
fragBuilder->codeAppendf("\t%s = saturate(%s);\n",
225225
args.fOutputColor, args.fOutputColor);
226226
fragBuilder->codeAppendf("\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
227227
}

src/effects/SkHighContrastFilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void GLHighContrastFilterEffect::emitCode(EmitArgs& args) {
343343
fragBuilder->codeAppendf("}");
344344

345345
// Clamp.
346-
fragBuilder->codeAppendf("color = clamp(color, 0, 1);");
346+
fragBuilder->codeAppendf("color = saturate(color);");
347347

348348
if (hcfe.linearize()) {
349349
fragBuilder->codeAppend("color.rgb = sqrt(color.rgb);");

src/effects/imagefilters/SkDisplacementMapEffect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) {
551551

552552
// Unpremultiply the displacement
553553
fragBuilder->codeAppendf(
554-
"\t\t%s.rgb = (%s.a < %s) ? half3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);",
554+
"\t\t%s.rgb = (%s.a < %s) ? half3(0.0) : saturate(%s.rgb / %s.a);",
555555
dColor, dColor, nearZero, dColor, dColor);
556556
SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
557557
fragBuilder->codeAppendf("\t\tfloat2 %s = %s + %s*(%s.",

src/effects/imagefilters/SkLightingImageFilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ void GrGLDiffuseLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHandl
19451945
};
19461946
SkString lightBody;
19471947
lightBody.appendf("\thalf colorScale = %s * dot(normal, surfaceToLight);\n", kd);
1948-
lightBody.appendf("\treturn half4(lightColor * clamp(colorScale, 0.0, 1.0), 1.0);\n");
1948+
lightBody.appendf("\treturn half4(lightColor * saturate(colorScale), 1.0);\n");
19491949
fragBuilder->emitFunction(kHalf4_GrSLType,
19501950
"light",
19511951
SK_ARRAY_COUNT(gLightArgs),
@@ -2043,7 +2043,7 @@ void GrGLSpecularLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHand
20432043
lightBody.appendf("\thalf3 halfDir = half3(normalize(surfaceToLight + half3(0, 0, 1)));\n");
20442044
lightBody.appendf("\tfloat colorScale = %s * pow(dot(normal, halfDir), %s);\n",
20452045
ks, shininess);
2046-
lightBody.appendf("\thalf3 color = lightColor * clamp(colorScale, 0.0, 1.0);\n");
2046+
lightBody.appendf("\thalf3 color = lightColor * saturate(colorScale);\n");
20472047
lightBody.appendf("\treturn half4(color, max(max(color.r, color.g), color.b));\n");
20482048
fragBuilder->emitFunction(kHalf4_GrSLType,
20492049
"light",

src/gpu/effects/GrArithmeticFP.fp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ uniform float4 k;
1616

1717
void main() {
1818
half4 dst = process(child);
19-
sk_OutColor = clamp(k.x * sk_InColor * dst + k.y * sk_InColor + k.z * dst + k.w, 0, 1);
19+
sk_OutColor = saturate(k.x * sk_InColor * dst + k.y * sk_InColor + k.z * dst + k.w);
2020
if (enforcePMColor) {
2121
sk_OutColor.rgb = min(sk_OutColor.rgb, sk_OutColor.a);
2222
}

src/gpu/effects/GrBezierEffect.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void GrGLConicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
175175
func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
176176
fragBuilder->codeAppendf("%s = %s / %s;",
177177
edgeAlpha.c_str(), func.c_str(), gFM.c_str());
178-
fragBuilder->codeAppendf("%s = clamp(0.5 - %s, 0.0, 1.0);",
178+
fragBuilder->codeAppendf("%s = saturate(0.5 - %s);",
179179
edgeAlpha.c_str(), edgeAlpha.c_str());
180180
// Add line below for smooth cubic ramp
181181
// fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
@@ -380,7 +380,7 @@ void GrGLQuadEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
380380
fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
381381
v.fsIn(), v.fsIn(), v.fsIn());
382382
fragBuilder->codeAppend("edgeAlpha = edgeAlpha / sqrt(dot(gF, gF));");
383-
fragBuilder->codeAppend("edgeAlpha = clamp(0.5 - edgeAlpha, 0.0, 1.0);");
383+
fragBuilder->codeAppend("edgeAlpha = saturate(0.5 - edgeAlpha);");
384384
// Add line below for smooth cubic ramp
385385
// fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
386386
break;
@@ -615,7 +615,7 @@ void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
615615
v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
616616
fragBuilder->codeAppendf("%s = %s * inversesqrt(dot(%s, %s));",
617617
edgeAlpha.c_str(), func.c_str(), gF.c_str(), gF.c_str());
618-
fragBuilder->codeAppendf("%s = clamp(0.5 - %s, 0.0, 1.0);",
618+
fragBuilder->codeAppendf("%s = saturate(0.5 - %s);",
619619
edgeAlpha.c_str(), edgeAlpha.c_str());
620620
// Add line below for smooth cubic ramp
621621
// fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",

src/gpu/effects/GrCircleEffect.fp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void main() {
6060
@if (edgeType == GrClipEdgeType::kFillAA ||
6161
edgeType == GrClipEdgeType::kInverseFillAA ||
6262
edgeType == GrClipEdgeType::kHairlineAA) {
63-
d = clamp(d, 0.0, 1.0);
63+
d = saturate(d);
6464
} else {
6565
d = d > 0.5 ? 1.0 : 0.0;
6666
}

src/gpu/effects/GrConvexPolyEffect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
5454
"1));\n",
5555
edgeArrayName, i);
5656
if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
57-
fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
57+
fragBuilder->codeAppend("\t\tedge = saturate(edge);\n");
5858
} else {
5959
fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
6060
}

src/gpu/effects/GrDistanceFieldGeoProc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
152152
// doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
153153
// distance mapped linearly to coverage, so use a linear step:
154154
fragBuilder->codeAppend(
155-
"half val = clamp((distance + afwidth) / (2.0 * afwidth), 0.0, 1.0);");
155+
"half val = saturate((distance + afwidth) / (2.0 * afwidth));");
156156
} else {
157157
fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
158158
}
@@ -447,7 +447,7 @@ class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
447447
// mapped linearly to coverage, so use a linear step:
448448
if (isGammaCorrect) {
449449
fragBuilder->codeAppend(
450-
"half val = clamp((distance + afwidth) / (2.0 * afwidth), 0.0, 1.0);");
450+
"half val = saturate((distance + afwidth) / (2.0 * afwidth));");
451451
} else {
452452
fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
453453
}
@@ -766,7 +766,7 @@ class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
766766
// mapped linearly to coverage, so use a linear step:
767767
if (isGammaCorrect) {
768768
fragBuilder->codeAppendf("%s = "
769-
"half4(clamp((distance + half3(afwidth)) / half3(2.0 * afwidth), 0.0, 1.0), 1.0);",
769+
"half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
770770
args.fOutputCoverage);
771771
} else {
772772
fragBuilder->codeAppendf(

src/gpu/effects/GrEllipseEffect.fp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ void main() {
9191
alpha = approx_dist > 0.0 ? 0.0 : 1.0;
9292
break;
9393
case GrClipEdgeType::kFillAA:
94-
alpha = clamp(0.5 - approx_dist, 0.0, 1.0);
94+
alpha = saturate(0.5 - approx_dist);
9595
break;
9696
case GrClipEdgeType::kInverseFillBW:
9797
alpha = approx_dist > 0.0 ? 1.0 : 0.0;
9898
break;
9999
case GrClipEdgeType::kInverseFillAA:
100-
alpha = clamp(0.5 + approx_dist, 0.0, 1.0);
100+
alpha = saturate(0.5 + approx_dist);
101101
break;
102102
default:
103103
// hairline not supported

0 commit comments

Comments
 (0)