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 "src/gpu/GrShaderCaps.h"
11#include "src/gpu/GrTexture.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.
20static 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
30void 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(nullptr,
63 kFragment_GrShaderFlag,
64 kHalf2_GrSLType,
65 "DstTextureUpperLeft",
66 &dstTopLeftName);
67 fDstScaleUni = uniformHandler->addUniform(nullptr,
68 kFragment_GrShaderFlag,
69 kHalf2_GrSLType,
70 "DstTextureCoordScale",
71 &dstCoordScaleName);
72
73 fragBuilder->codeAppend("// Read color from copy of the destination.\n");
74 fragBuilder->codeAppendf("half2 _dstTexCoord = (half2(sk_FragCoord.xy) - %s) * %s;",
75 dstTopLeftName, dstCoordScaleName);
76
77 if (flipY) {
78 fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;");
79 }
80
81 fragBuilder->codeAppendf("half4 %s = ", dstColor);
82 fragBuilder->appendTextureLookup(args.fDstTextureSamplerHandle, "_dstTexCoord");
83 fragBuilder->codeAppend(";");
84 } else {
85 needsLocalOutColor = args.fShaderCaps->requiresLocalOutputColorForFBFetch();
86 }
87
88 const char* outColor = "_localColorOut";
89 if (!needsLocalOutColor) {
90 outColor = args.fOutputPrimary;
91 } else {
92 fragBuilder->codeAppendf("half4 %s;", outColor);
93 }
94
95 this->emitBlendCodeForDstRead(fragBuilder,
96 uniformHandler,
97 args.fInputColor,
98 args.fInputCoverage,
99 dstColor,
100 outColor,
101 args.fOutputSecondary,
102 args.fXP);
103 if (needsLocalOutColor) {
104 fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, outColor);
105 }
106 }
107
108 // Swizzle the fragment shader outputs if necessary.
109 this->emitWriteSwizzle(args.fXPFragBuilder, args.fWriteSwizzle, args.fOutputPrimary,
110 args.fOutputSecondary);
111}
112
113void GrGLSLXferProcessor::emitWriteSwizzle(GrGLSLXPFragmentBuilder* x,
114 const GrSwizzle& swizzle,
115 const char* outColor,
116 const char* outColorSecondary) const {
117 if (GrSwizzle::RGBA() != swizzle) {
118 x->codeAppendf("%s = %s.%s;", outColor, outColor, swizzle.asString().c_str());
119 if (outColorSecondary) {
120 x->codeAppendf("%s = %s.%s;", outColorSecondary, outColorSecondary,
121 swizzle.asString().c_str());
122 }
123 }
124}
125
126void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp,
127 const GrTexture* dstTexture, const SkIPoint& dstTextureOffset) {
128 if (dstTexture) {
129 if (fDstTopLeftUni.isValid()) {
130 pdm.set2f(fDstTopLeftUni, static_cast<float>(dstTextureOffset.fX),
131 static_cast<float>(dstTextureOffset.fY));
132 pdm.set2f(fDstScaleUni, 1.f / dstTexture->width(), 1.f / dstTexture->height());
133 } else {
134 SkASSERT(!fDstScaleUni.isValid());
135 }
136 } else {
137 SkASSERT(!fDstTopLeftUni.isValid());
138 SkASSERT(!fDstScaleUni.isValid());
139 }
140 this->onSetData(pdm, xp);
141}
142
143void GrGLSLXferProcessor::DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
144 const char* srcCoverage,
145 const char* dstColor,
146 const char* outColor,
147 const char* outColorSecondary,
148 const GrXferProcessor& proc) {
149 if (proc.dstReadUsesMixedSamples()) {
150 if (srcCoverage) {
151 // TODO: Once we are no longer using legacy mesh ops, it will not be possible to even
152 // create a mixed sample with lcd so we can uncomment the below assert. In practice
153 // today this never happens except for GLPrograms test which can make one. skia:6661
154 // SkASSERT(!proc.isLCD());
155 fragBuilder->codeAppendf("%s *= %s;", outColor, srcCoverage);
156 fragBuilder->codeAppendf("%s = %s;", outColorSecondary, srcCoverage);
157 } else {
158 fragBuilder->codeAppendf("%s = half4(1.0);", outColorSecondary);
159 }
160 } else if (srcCoverage) {
161 if (proc.isLCD()) {
162 fragBuilder->codeAppendf("half lerpRed = mix(%s.a, %s.a, %s.r);",
163 dstColor, outColor, srcCoverage);
164 fragBuilder->codeAppendf("half lerpBlue = mix(%s.a, %s.a, %s.g);",
165 dstColor, outColor, srcCoverage);
166 fragBuilder->codeAppendf("half lerpGreen = mix(%s.a, %s.a, %s.b);",
167 dstColor, outColor, srcCoverage);
168 }
169 fragBuilder->codeAppendf("%s = %s * %s + (half4(1.0) - %s) * %s;",
170 outColor, srcCoverage, outColor, srcCoverage, dstColor);
171 if (proc.isLCD()) {
172 fragBuilder->codeAppendf("%s.a = max(max(lerpRed, lerpBlue), lerpGreen);", outColor);
173 }
174 }
175}
176
177