blob: 68ba303dc33c39fa4a47f541eb6ada59ed1469a4 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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
Brian Salomon201cdbb2019-08-14 17:00:30 -04008#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrRenderTargetPriv.h"
10#include "src/gpu/GrShaderCaps.h"
11#include "src/gpu/gl/GrGLGpu.h"
12#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14#include "src/gpu/glsl/GrGLSLUniformHandler.h"
15#include "src/gpu/glsl/GrGLSLVarying.h"
joshualitt47bb3822014-10-07 16:43:25 -070016
egdaniel138c2632016-08-17 10:59:00 -070017const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
joshualitt30ba4362014-08-21 20:18:45 -070018
cdalton8917d622015-05-06 13:40:21 -070019static const char* specific_layout_qualifier_name(GrBlendEquation equation) {
20 SkASSERT(GrBlendEquationIsAdvanced(equation));
21
22 static const char* kLayoutQualifierNames[] = {
23 "blend_support_screen",
24 "blend_support_overlay",
25 "blend_support_darken",
26 "blend_support_lighten",
27 "blend_support_colordodge",
28 "blend_support_colorburn",
29 "blend_support_hardlight",
30 "blend_support_softlight",
31 "blend_support_difference",
32 "blend_support_exclusion",
33 "blend_support_multiply",
34 "blend_support_hsl_hue",
35 "blend_support_hsl_saturation",
36 "blend_support_hsl_color",
37 "blend_support_hsl_luminosity"
38 };
39 return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation];
40
Brian Salomon4dea72a2019-12-18 10:43:10 -050041 static_assert(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation);
42 static_assert(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation);
43 static_assert(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation);
44 static_assert(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation);
45 static_assert(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation);
46 static_assert(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation);
47 static_assert(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
48 static_assert(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
49 static_assert(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation);
50 static_assert(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation);
51 static_assert(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation);
52 static_assert(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation);
53 static_assert(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation);
54 static_assert(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation);
55 static_assert(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation);
Mike Klein36743362018-11-06 08:23:30 -050056 // There's an illegal GrBlendEquation at the end there, hence the -1.
Brian Salomon4dea72a2019-12-18 10:43:10 -050057 static_assert(SK_ARRAY_COUNT(kLayoutQualifierNames) ==
58 kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation - 1);
cdalton8917d622015-05-06 13:40:21 -070059}
60
Robert Phillips7f861922018-01-30 13:13:42 +000061uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) {
62 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
63 return origin + 1;
64
Brian Salomon4dea72a2019-12-18 10:43:10 -050065 static_assert(0 == kTopLeft_GrSurfaceOrigin);
66 static_assert(1 == kBottomLeft_GrSurfaceOrigin);
Robert Phillips7f861922018-01-30 13:13:42 +000067}
68
cdalton28f45b92016-03-07 13:58:26 -080069GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
Chris Daltond7291ba2019-03-07 14:17:03 -070070 : GrGLSLFragmentBuilder(program) {
cdalton85285412016-02-18 12:37:07 -080071 fSubstageIndices.push_back(0);
cdalton87332102016-02-26 12:22:02 -080072}
73
Ethan Nicholas58430122020-04-14 09:54:02 -040074SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords,
75 const SkSL::SampleMatrix& matrix) {
76 SkString result;
Brian Salomonbf5c0c02019-11-11 14:55:28 -050077 if (!coords.getName().size()) {
Ethan Nicholas58430122020-04-14 09:54:02 -040078 result = "_coords";
79 } else if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -040080 SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType());
Ethan Nicholas58430122020-04-14 09:54:02 -040081 result = coords.getName();
82 } else {
83 SkString coords2D;
84 coords2D.printf("%s_ensure2D", coords.c_str());
85 this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
86 coords.c_str());
87 result = coords2D;
joshualitt30ba4362014-08-21 20:18:45 -070088 }
Ethan Nicholas58430122020-04-14 09:54:02 -040089 switch (matrix.fKind) {
90 case SkSL::SampleMatrix::Kind::kMixed:
Michael Ludwig89dd4e72020-06-03 15:39:06 -040091 case SkSL::SampleMatrix::Kind::kVariable: {
92 SkString sampleCoords2D;
93 sampleCoords2D.printf("%s_sample", coords.c_str());
94 this->codeAppendf("\tfloat3 %s_3d = _matrix * %s.xy1;\n",
95 sampleCoords2D.c_str(), result.c_str());
96 this->codeAppendf("\tfloat2 %s = %s_3d.xy / %s_3d.z;\n",
97 sampleCoords2D.c_str(), sampleCoords2D.c_str(),
98 sampleCoords2D.c_str());
99 result = sampleCoords2D;
100 break; }
Ethan Nicholas58430122020-04-14 09:54:02 -0400101 default:
102 break;
103 }
104 return result;
joshualitt30ba4362014-08-21 20:18:45 -0700105}
106
Chris Daltond7291ba2019-03-07 14:17:03 -0700107const char* GrGLSLFragmentShaderBuilder::sampleOffsets() {
Robert Phillips7de13332019-10-09 15:44:54 -0400108 SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures());
Chris Daltond7291ba2019-03-07 14:17:03 -0700109 SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
110 SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
111 return "_sampleOffsets";
112}
113
Chris Dalton0dffbab2019-03-27 13:08:50 -0600114void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage(
115 const char* mask, ScopeFlags scopeFlags) {
Chris Daltond31b5e72019-02-26 18:02:16 -0700116 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
Chris Dalton8a64a442019-10-29 18:54:58 -0600117 if (!shaderCaps.sampleMaskSupport()) {
Chris Daltond31b5e72019-02-26 18:02:16 -0700118 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
119 return;
120 }
121 if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
122 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
123 }
124
Chris Dalton0dffbab2019-03-27 13:08:50 -0600125 if (!fHasModifiedSampleMask) {
126 fHasModifiedSampleMask = true;
127 if (ScopeFlags::kTopLevel != scopeFlags) {
Chris Daltonb0fd4b12019-10-29 13:41:22 -0600128 this->codePrependf("sk_SampleMask[0] = ~0;");
Chris Dalton0dffbab2019-03-27 13:08:50 -0600129 }
130 if (!(ScopeFlags::kInsideLoop & scopeFlags)) {
Chris Daltonb0fd4b12019-10-29 13:41:22 -0600131 this->codeAppendf("sk_SampleMask[0] = (%s);", mask);
Chris Dalton0dffbab2019-03-27 13:08:50 -0600132 return;
133 }
Chris Daltond31b5e72019-02-26 18:02:16 -0700134 }
Chris Dalton0dffbab2019-03-27 13:08:50 -0600135
Chris Daltonb0fd4b12019-10-29 13:41:22 -0600136 this->codeAppendf("sk_SampleMask[0] &= (%s);", mask);
Chris Daltond31b5e72019-02-26 18:02:16 -0700137}
138
Chris Dalton0dffbab2019-03-27 13:08:50 -0600139void GrGLSLFragmentShaderBuilder::applyFnToMultisampleMask(
140 const char* fn, const char* grad, ScopeFlags scopeFlags) {
Robert Phillips7de13332019-10-09 15:44:54 -0400141 SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures());
Chris Dalton0dffbab2019-03-27 13:08:50 -0600142 SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
143 SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
144
145 int sampleCnt = fProgramBuilder->effectiveSampleCnt();
146 SkASSERT(sampleCnt > 1);
147
148 this->codeAppendf("{");
149
150 if (!grad) {
151 SkASSERT(fProgramBuilder->shaderCaps()->shaderDerivativeSupport());
152 // In order to use HW derivatives, our neighbors within the same primitive must also be
153 // executing the same code. A per-pixel branch makes this pre-condition impossible to
154 // fulfill.
155 SkASSERT(!(ScopeFlags::kInsidePerPixelBranch & scopeFlags));
Chris Daltonc3318f02019-07-19 14:20:53 -0600156 this->codeAppendf("float2 grad = float2(dFdx(%s), dFdy(%s));", fn, fn);
157 this->codeAppendf("float fnwidth = fwidth(%s);", fn);
Chris Dalton0dffbab2019-03-27 13:08:50 -0600158 grad = "grad";
159 } else {
160 this->codeAppendf("float fnwidth = abs(%s.x) + abs(%s.y);", grad, grad);
161 }
162
163 this->codeAppendf("int mask = 0;");
164 this->codeAppendf("if (%s*2 < fnwidth) {", fn); // Are ANY samples inside the implicit fn?
165 this->codeAppendf( "if (%s*-2 >= fnwidth) {", fn); // Are ALL samples inside the implicit?
166 this->codeAppendf( "mask = ~0;");
167 this->codeAppendf( "} else for (int i = 0; i < %i; ++i) {", sampleCnt);
168 this->codeAppendf( "float fnsample = dot(%s, _sampleOffsets[i]) + %s;", grad, fn);
169 this->codeAppendf( "if (fnsample < 0) {");
170 this->codeAppendf( "mask |= (1 << i);");
171 this->codeAppendf( "}");
172 this->codeAppendf( "}");
173 this->codeAppendf("}");
174 this->maskOffMultisampleCoverage("mask", scopeFlags);
175
176 this->codeAppendf("}");
177}
178
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400179SkString GrGLSLFPFragmentBuilder::writeProcessorFunction(GrGLSLFragmentProcessor* fp,
180 GrGLSLFragmentProcessor::EmitArgs& args) {
181 this->onBeforeChildProcEmitCode();
182 this->nextStage();
Ethan Nicholas58430122020-04-14 09:54:02 -0400183 bool hasVariableMatrix = args.fFp.sampleMatrix().fKind == SkSL::SampleMatrix::Kind::kVariable ||
184 args.fFp.sampleMatrix().fKind == SkSL::SampleMatrix::Kind::kMixed;
Brian Salomonb10a6622020-02-20 13:36:01 -0500185 if (args.fFp.isSampledWithExplicitCoords() && args.fTransformedCoords.count() > 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400186 // we currently only support overriding a single coordinate pair
187 SkASSERT(args.fTransformedCoords.count() == 1);
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400188 const GrShaderVar& transform = args.fTransformedCoords[0].fTransform;
189 switch (transform.getType()) {
190 case kFloat4_GrSLType:
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400191 // This is a scale+translate, so there's no perspective division needed
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400192 this->codeAppendf("_coords = _coords * %s.xz + %s.yw;\n", transform.c_str(),
193 transform.c_str());
194 break;
195 case kFloat3x3_GrSLType:
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400196 this->codeAppend("{\n");
197 this->codeAppendf("float3 _coords3 = (%s * _coords.xy1);\n", transform.c_str());
198 this->codeAppend("_coords = _coords3.xy / _coords3.z;\n");
199 this->codeAppend("}\n");
Brian Salomon8d1dcd72020-03-20 21:06:41 -0400200 break;
201 default:
202 SkASSERT(transform.getType() == kVoid_GrSLType);
203 break;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400204 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400205 if (args.fFp.sampleMatrix().fKind != SkSL::SampleMatrix::Kind::kNone) {
206 SkASSERT(!hasVariableMatrix);
207 this->codeAppend("{\n");
208 args.fUniformHandler->writeUniformMappings(args.fFp.sampleMatrix().fOwner, this);
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400209 // FIXME This is not a variable matrix, we could key on the matrix type and skip
210 // perspective division; it may also be worth detecting if it was scale+translate and
211 // evaluating this similarly to the kFloat4 explicit coord case.
212 this->codeAppendf("float3 _coords3 = (%s * _coords.xy1);\n",
Ethan Nicholas58430122020-04-14 09:54:02 -0400213 args.fFp.sampleMatrix().fExpression.c_str());
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400214 this->codeAppend("_coords = _coords3.xy / _coords3.z;\n");
Ethan Nicholas58430122020-04-14 09:54:02 -0400215 this->codeAppend("}\n");
216 }
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400217 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400218
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400219 this->codeAppendf("half4 %s;\n", args.fOutputColor);
220 fp->emitCode(args);
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400221 this->codeAppendf("return %s;\n", args.fOutputColor);
222 GrShaderVar params[] = { GrShaderVar(args.fInputColor, kHalf4_GrSLType),
Ethan Nicholas58430122020-04-14 09:54:02 -0400223 hasVariableMatrix ? GrShaderVar("_matrix", kFloat3x3_GrSLType)
224 : GrShaderVar("_coords", kFloat2_GrSLType) };
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400225 SkString result;
226 this->emitFunction(kHalf4_GrSLType,
Ethan Nicholas7c752262020-04-01 14:18:28 -0400227 args.fFp.name(),
Ethan Nicholas58430122020-04-14 09:54:02 -0400228 args.fFp.isSampledWithExplicitCoords() || hasVariableMatrix ? 2
229 : 1,
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400230 params,
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400231 this->code().c_str(),
232 &result);
233 this->deleteStage();
234 this->onAfterChildProcEmitCode();
235 return result;
236}
237
egdaniel2d721d32015-11-11 13:06:05 -0800238const char* GrGLSLFragmentShaderBuilder::dstColor() {
Chris Daltond7291ba2019-03-07 14:17:03 -0700239 SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;)
joshualitt47bb3822014-10-07 16:43:25 -0700240
Brian Salomon1edc5b92016-11-29 13:43:46 -0500241 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
242 if (shaderCaps->fbFetchSupport()) {
cdaltonc08f1962016-02-12 12:14:06 -0800243 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500244 shaderCaps->fbFetchExtensionString());
joshualittb4384b92014-10-21 12:53:15 -0700245
joshualitt3c1096f2015-01-13 13:13:59 -0800246 // Some versions of this extension string require declaring custom color output on ES 3.0+
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400247 const char* fbFetchColorName = "sk_LastFragColor";
Brian Salomon1edc5b92016-11-29 13:43:46 -0500248 if (shaderCaps->fbFetchNeedsCustomOutput()) {
joshualittb4384b92014-10-21 12:53:15 -0700249 this->enableCustomOutput();
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400250 fCustomColorOutput->setTypeModifier(GrShaderVar::TypeModifier::InOut);
egdaniel8dcdedc2015-11-11 06:27:20 -0800251 fbFetchColorName = DeclaredColorOutputName();
egdaniel138c2632016-08-17 10:59:00 -0700252 // Set the dstColor to an intermediate variable so we don't override it with the output
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400253 this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
egdanielbd4121a2016-08-17 12:55:30 -0700254 } else {
255 return fbFetchColorName;
joshualittb4384b92014-10-21 12:53:15 -0700256 }
halcanary9d524f22016-03-29 09:03:52 -0700257 }
egdaniel138c2632016-08-17 10:59:00 -0700258 return kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700259}
260
egdaniel2d721d32015-11-11 13:06:05 -0800261void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
cdalton8917d622015-05-06 13:40:21 -0700262 SkASSERT(GrBlendEquationIsAdvanced(equation));
263
Brian Salomon94efbf52016-11-29 13:43:05 -0500264 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
cdalton8917d622015-05-06 13:40:21 -0700265 if (!caps.mustEnableAdvBlendEqs()) {
266 return;
267 }
268
269 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
270 "GL_KHR_blend_equation_advanced");
271 if (caps.mustEnableSpecificAdvBlendEqs()) {
272 this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
273 } else {
274 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
275 }
276}
277
egdaniel2d721d32015-11-11 13:06:05 -0800278void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
Michael Ludwig45191342020-03-24 12:29:39 -0400279 if (!fCustomColorOutput) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400280 fCustomColorOutput = &fOutputs.emplace_back(DeclaredColorOutputName(), kHalf4_GrSLType,
281 GrShaderVar::TypeModifier::Out);
Ben Wagner63fd7602017-10-09 15:45:33 -0400282 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
joshualittb4384b92014-10-21 12:53:15 -0700283 }
joshualitt47bb3822014-10-07 16:43:25 -0700284}
285
egdaniel2d721d32015-11-11 13:06:05 -0800286void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
joshualitt47bb3822014-10-07 16:43:25 -0700287 SkASSERT(!fHasSecondaryOutput);
288 fHasSecondaryOutput = true;
Brian Salomon94efbf52016-11-29 13:43:05 -0500289 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800290 if (const char* extension = caps.secondaryOutputExtensionString()) {
291 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
kkinnunend94708e2015-07-30 22:47:04 -0700292 }
293
294 // If the primary output is declared, we must declare also the secondary output
295 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
296 // output. The condition also co-incides with the condition in whici GLES SL 2.0
297 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
kkinnunend94708e2015-07-30 22:47:04 -0700298 if (caps.mustDeclareFragmentShaderOutput()) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400299 fOutputs.emplace_back(DeclaredSecondaryColorOutputName(), kHalf4_GrSLType,
300 GrShaderVar::TypeModifier::Out);
egdanielb80ec8b2016-02-09 09:54:43 -0800301 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
kkinnunend94708e2015-07-30 22:47:04 -0700302 }
joshualitt47bb3822014-10-07 16:43:25 -0700303}
304
egdaniel2d721d32015-11-11 13:06:05 -0800305const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
Michael Ludwig45191342020-03-24 12:29:39 -0400306 return this->hasCustomColorOutput() ? DeclaredColorOutputName() : "sk_FragColor";
joshualitt47bb3822014-10-07 16:43:25 -0700307}
308
Brian Salomondc092132018-04-04 10:14:16 -0400309bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
Michael Ludwig45191342020-03-24 12:29:39 -0400310 return fCustomColorOutput &&
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400311 fCustomColorOutput->getTypeModifier() == GrShaderVar::TypeModifier::InOut;
Brian Salomondc092132018-04-04 10:14:16 -0400312}
313
ethannicholas22793252016-01-30 09:59:10 -0800314void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
315 va_list argp;
316 va_start(argp, fmt);
317 inputs().appendVAList(fmt, argp);
halcanary9d524f22016-03-29 09:03:52 -0700318 va_end(argp);
ethannicholas22793252016-01-30 09:59:10 -0800319}
320
egdaniel2d721d32015-11-11 13:06:05 -0800321const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
Chris Dalton6b76df02019-04-08 11:01:34 -0600322 if (this->hasSecondaryOutput()) {
323 return (fProgramBuilder->shaderCaps()->mustDeclareFragmentShaderOutput())
324 ? DeclaredSecondaryColorOutputName()
325 : "gl_SecondaryFragColorEXT";
326 }
327 return nullptr;
joshualitt47bb3822014-10-07 16:43:25 -0700328}
329
cdalton28f45b92016-03-07 13:58:26 -0800330GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
Robert Phillips7de13332019-10-09 15:44:54 -0400331 return fProgramBuilder->origin();
cdalton28f45b92016-03-07 13:58:26 -0800332}
333
egdaniel2d721d32015-11-11 13:06:05 -0800334void GrGLSLFragmentShaderBuilder::onFinalize() {
Robert Phillips7de13332019-10-09 15:44:54 -0400335 SkASSERT(fProgramBuilder->processorFeatures() == fUsedProcessorFeaturesAllStages_DebugOnly);
Chris Daltond7291ba2019-03-07 14:17:03 -0700336
Robert Phillips7de13332019-10-09 15:44:54 -0400337 if (CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures()) {
Robert Phillips65a77752019-10-02 15:22:24 -0400338 const SkTArray<SkPoint>& sampleLocations = fProgramBuilder->getSampleLocations();
Chris Dalton2284aab2019-11-15 11:02:24 -0700339 this->definitions().appendf("const float2 _sampleOffsets[%i] = float2[%i](",
340 sampleLocations.count(), sampleLocations.count());
Chris Daltond7291ba2019-03-07 14:17:03 -0700341 for (int i = 0; i < sampleLocations.count(); ++i) {
342 SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f);
343 if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
344 offset.fY = -offset.fY;
345 }
346 this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y());
347 this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");");
348 }
349 }
350
egdaniel0eafe792015-11-20 14:01:22 -0800351 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
joshualitt30ba4362014-08-21 20:18:45 -0700352}
353
cdalton85285412016-02-18 12:37:07 -0800354void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700355 SkASSERT(fSubstageIndices.count() >= 1);
wangyix7ef45a12015-08-13 06:51:35 -0700356 fSubstageIndices.push_back(0);
wangyix54a6b1a2015-09-08 08:41:51 -0700357 // second-to-last value in the fSubstageIndices stack is the index of the child proc
358 // at that level which is currently emitting code.
359 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
wangyix7ef45a12015-08-13 06:51:35 -0700360}
361
cdalton85285412016-02-18 12:37:07 -0800362void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700363 SkASSERT(fSubstageIndices.count() >= 2);
wangyix7ef45a12015-08-13 06:51:35 -0700364 fSubstageIndices.pop_back();
wangyix54a6b1a2015-09-08 08:41:51 -0700365 fSubstageIndices.back()++;
wangyix7ef45a12015-08-13 06:51:35 -0700366 int removeAt = fMangleString.findLastOf('_');
367 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
368}