1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/glsl/GrGLSLXferProcessor.h"
9
10 #include "include/gpu/GrTexture.h"
11 #include "src/gpu/GrShaderCaps.h"
12 #include "src/gpu/GrXferProcessor.h"
13 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
15 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
16
17 // This is only called for cases where we are doing LCD coverage and not using in shader blending.
18 // For these cases we assume the the src alpha is 1, thus we can just use the max for the alpha
19 // coverage since src alpha will always be greater than or equal to dst alpha.
adjust_for_lcd_coverage(GrGLSLXPFragmentBuilder * fragBuilder,const char * srcCoverage,const GrXferProcessor & proc)20 static void adjust_for_lcd_coverage(GrGLSLXPFragmentBuilder* fragBuilder,
21 const char* srcCoverage,
22 const GrXferProcessor& proc) {
23 if (srcCoverage && proc.isLCD()) {
24 fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
25 srcCoverage, srcCoverage, srcCoverage, srcCoverage);
26 }
27 }
28
29
emitCode(const EmitArgs & args)30 void GrGLSLXferProcessor::emitCode(const EmitArgs& args) {
31 if (!args.fXP.willReadDstColor()) {
32 adjust_for_lcd_coverage(args.fXPFragBuilder, args.fInputCoverage, args.fXP);
33 this->emitOutputsForBlendState(args);
34 } else {
35 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
36 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
37 const char* dstColor = fragBuilder->dstColor();
38
39 bool needsLocalOutColor = false;
40
41 if (args.fDstTextureSamplerHandle.isValid()) {
42 bool flipY = kBottomLeft_GrSurfaceOrigin == args.fDstTextureOrigin;
43
44 if (args.fInputCoverage) {
45 // We don't think any shaders actually output negative coverage, but just as a
46 // safety check for floating point precision errors we compare with <= here. We just
47 // check the rgb values of the coverage since the alpha may not have been set when
48 // using lcd. If we are using single channel coverage alpha will equal to rgb
49 // anyways.
50 //
51 // The discard here also helps for batching text draws together which need to read
52 // from a dst copy for blends. Though this only helps the case where the outer
53 // bounding boxes of each letter overlap and not two actually parts of the text.
54 fragBuilder->codeAppendf("if (all(lessThanEqual(%s.rgb, half3(0)))) {"
55 " discard;"
56 "}", args.fInputCoverage);
57 }
58
59 const char* dstTopLeftName;
60 const char* dstCoordScaleName;
61
62 fDstTopLeftUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
63 kHalf2_GrSLType,
64 "DstTextureUpperLeft",
65 &dstTopLeftName);
66 fDstScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
67 kHalf2_GrSLType,
68 "DstTextureCoordScale",
69 &dstCoordScaleName);
70
71 fragBuilder->codeAppend("// Read color from copy of the destination.\n");
72 fragBuilder->codeAppendf("half2 _dstTexCoord = (half2(sk_FragCoord.xy) - %s) * %s;",
73 dstTopLeftName, dstCoordScaleName);
74
75 if (flipY) {
76 fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;");
77 }
78
79 fragBuilder->codeAppendf("half4 %s = ", dstColor);
80 fragBuilder->appendTextureLookup(args.fDstTextureSamplerHandle, "_dstTexCoord",
81 kHalf2_GrSLType);
82 fragBuilder->codeAppend(";");
83 } else {
84 needsLocalOutColor = args.fShaderCaps->requiresLocalOutputColorForFBFetch();
85 }
86
87 const char* outColor = "_localColorOut";
88 if (!needsLocalOutColor) {
89 outColor = args.fOutputPrimary;
90 } else {
91 fragBuilder->codeAppendf("half4 %s;", outColor);
92 }
93
94 this->emitBlendCodeForDstRead(fragBuilder,
95 uniformHandler,
96 args.fInputColor,
97 args.fInputCoverage,
98 dstColor,
99 outColor,
100 args.fOutputSecondary,
101 args.fXP);
102 if (needsLocalOutColor) {
103 fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, outColor);
104 }
105 }
106
107 // Swizzle the fragment shader outputs if necessary.
108 this->emitOutputSwizzle(
109 args.fXPFragBuilder, args.fOutputSwizzle, args.fOutputPrimary, args.fOutputSecondary);
110 }
111
emitOutputSwizzle(GrGLSLXPFragmentBuilder * x,const GrSwizzle & swizzle,const char * outColor,const char * outColorSecondary) const112 void GrGLSLXferProcessor::emitOutputSwizzle(
113 GrGLSLXPFragmentBuilder* x, const GrSwizzle& swizzle, const char* outColor,
114 const char* outColorSecondary) const {
115 if (GrSwizzle::RGBA() != swizzle) {
116 x->codeAppendf("%s = %s.%s;", outColor, outColor, swizzle.c_str());
117 if (outColorSecondary) {
118 x->codeAppendf("%s = %s.%s;", outColorSecondary, outColorSecondary, swizzle.c_str());
119 }
120 }
121 }
122
setData(const GrGLSLProgramDataManager & pdm,const GrXferProcessor & xp,const GrTexture * dstTexture,const SkIPoint & dstTextureOffset)123 void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp,
124 const GrTexture* dstTexture, const SkIPoint& dstTextureOffset) {
125 if (dstTexture) {
126 if (fDstTopLeftUni.isValid()) {
127 pdm.set2f(fDstTopLeftUni, static_cast<float>(dstTextureOffset.fX),
128 static_cast<float>(dstTextureOffset.fY));
129 pdm.set2f(fDstScaleUni, 1.f / dstTexture->width(), 1.f / dstTexture->height());
130 } else {
131 SkASSERT(!fDstScaleUni.isValid());
132 }
133 } else {
134 SkASSERT(!fDstTopLeftUni.isValid());
135 SkASSERT(!fDstScaleUni.isValid());
136 }
137 this->onSetData(pdm, xp);
138 }
139
DefaultCoverageModulation(GrGLSLXPFragmentBuilder * fragBuilder,const char * srcCoverage,const char * dstColor,const char * outColor,const char * outColorSecondary,const GrXferProcessor & proc)140 void GrGLSLXferProcessor::DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
141 const char* srcCoverage,
142 const char* dstColor,
143 const char* outColor,
144 const char* outColorSecondary,
145 const GrXferProcessor& proc) {
146 if (proc.dstReadUsesMixedSamples()) {
147 if (srcCoverage) {
148 // TODO: Once we are no longer using legacy mesh ops, it will not be possible to even
149 // create a mixed sample with lcd so we can uncomment the below assert. In practice
150 // today this never happens except for GLPrograms test which can make one. skia:6661
151 // SkASSERT(!proc.isLCD());
152 fragBuilder->codeAppendf("%s *= %s;", outColor, srcCoverage);
153 fragBuilder->codeAppendf("%s = %s;", outColorSecondary, srcCoverage);
154 } else {
155 fragBuilder->codeAppendf("%s = half4(1.0);", outColorSecondary);
156 }
157 } else if (srcCoverage) {
158 if (proc.isLCD()) {
159 fragBuilder->codeAppendf("half lerpRed = mix(%s.a, %s.a, %s.r);",
160 dstColor, outColor, srcCoverage);
161 fragBuilder->codeAppendf("half lerpBlue = mix(%s.a, %s.a, %s.g);",
162 dstColor, outColor, srcCoverage);
163 fragBuilder->codeAppendf("half lerpGreen = mix(%s.a, %s.a, %s.b);",
164 dstColor, outColor, srcCoverage);
165 }
166 fragBuilder->codeAppendf("%s = %s * %s + (half4(1.0) - %s) * %s;",
167 outColor, srcCoverage, outColor, srcCoverage, dstColor);
168 if (proc.isLCD()) {
169 fragBuilder->codeAppendf("%s.a = max(max(lerpRed, lerpBlue), lerpGreen);", outColor);
170 }
171 }
172 }
173
174