blob: 7763f86e42957b664c027a2931a0f8d3da44e807 [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
egdaniel2d721d32015-11-11 13:06:05 -08008#include "GrGLSLFragmentShaderBuilder.h"
egdaniel574a4c12015-11-02 06:22:44 -08009#include "GrRenderTarget.h"
cdalton28f45b92016-03-07 13:58:26 -080010#include "GrRenderTargetPriv.h"
ethannicholas22793252016-01-30 09:59:10 -080011#include "gl/GrGLGpu.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070012#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070013#include "glsl/GrGLSLCaps.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080014#include "glsl/GrGLSLProgramBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080016#include "glsl/GrGLSLVarying.h"
joshualitt47bb3822014-10-07 16:43:25 -070017
egdaniel138c2632016-08-17 10:59:00 -070018const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
joshualitt30ba4362014-08-21 20:18:45 -070019
cdalton28f45b92016-03-07 13:58:26 -080020static const char* sample_offset_array_name(GrGLSLFPFragmentBuilder::Coordinates coords) {
21 static const char* kArrayNames[] = {
22 "deviceSpaceSampleOffsets",
23 "windowSpaceSampleOffsets"
24 };
25 return kArrayNames[coords];
26
27 GR_STATIC_ASSERT(0 == GrGLSLFPFragmentBuilder::kSkiaDevice_Coordinates);
28 GR_STATIC_ASSERT(1 == GrGLSLFPFragmentBuilder::kGLSLWindow_Coordinates);
29 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kArrayNames) == GrGLSLFPFragmentBuilder::kLast_Coordinates + 1);
30}
31
cdalton8917d622015-05-06 13:40:21 -070032static const char* specific_layout_qualifier_name(GrBlendEquation equation) {
33 SkASSERT(GrBlendEquationIsAdvanced(equation));
34
35 static const char* kLayoutQualifierNames[] = {
36 "blend_support_screen",
37 "blend_support_overlay",
38 "blend_support_darken",
39 "blend_support_lighten",
40 "blend_support_colordodge",
41 "blend_support_colorburn",
42 "blend_support_hardlight",
43 "blend_support_softlight",
44 "blend_support_difference",
45 "blend_support_exclusion",
46 "blend_support_multiply",
47 "blend_support_hsl_hue",
48 "blend_support_hsl_saturation",
49 "blend_support_hsl_color",
50 "blend_support_hsl_luminosity"
51 };
52 return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation];
53
54 GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation);
55 GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation);
56 GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation);
57 GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation);
58 GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation);
59 GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation);
60 GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
61 GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
62 GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation);
63 GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation);
64 GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation);
65 GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation);
66 GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation);
67 GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation);
68 GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation);
69 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) ==
bsalomonf7cc8772015-05-11 11:21:14 -070070 kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation);
cdalton8917d622015-05-06 13:40:21 -070071}
72
cdalton28f45b92016-03-07 13:58:26 -080073uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) {
74 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
75 return origin;
76
77 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
78 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
joshualitt30ba4362014-08-21 20:18:45 -070079}
80
cdalton28f45b92016-03-07 13:58:26 -080081GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
cdalton85285412016-02-18 12:37:07 -080082 : GrGLSLFragmentBuilder(program)
joshualitt30ba4362014-08-21 20:18:45 -070083 , fSetupFragPosition(false)
cdalton85285412016-02-18 12:37:07 -080084 , fHasCustomColorOutput(false)
joshualittb4384b92014-10-21 12:53:15 -070085 , fCustomColorOutputIndex(-1)
cdalton85285412016-02-18 12:37:07 -080086 , fHasSecondaryOutput(false)
cdalton28f45b92016-03-07 13:58:26 -080087 , fUsedSampleOffsetArrays(0)
cdalton87332102016-02-26 12:22:02 -080088 , fHasInitializedSampleMask(false) {
cdalton85285412016-02-18 12:37:07 -080089 fSubstageIndices.push_back(0);
cdalton87332102016-02-26 12:22:02 -080090#ifdef SK_DEBUG
91 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
92 fHasReadDstColor = false;
93#endif
94}
95
egdaniel2d721d32015-11-11 13:06:05 -080096bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) {
cdalton4a98cdb2016-03-01 12:12:20 -080097 const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
joshualitt30ba4362014-08-21 20:18:45 -070098 switch (feature) {
cdalton4a98cdb2016-03-01 12:12:20 -080099 case kStandardDerivatives_GLSLFeature:
100 if (!glslCaps.shaderDerivativeSupport()) {
joshualitt30ba4362014-08-21 20:18:45 -0700101 return false;
102 }
cdalton4a98cdb2016-03-01 12:12:20 -0800103 if (const char* extension = glslCaps.shaderDerivativeExtensionString()) {
egdaniel574a4c12015-11-02 06:22:44 -0800104 this->addFeature(1 << kStandardDerivatives_GLSLFeature, extension);
joshualitt30ba4362014-08-21 20:18:45 -0700105 }
106 return true;
cdalton4a98cdb2016-03-01 12:12:20 -0800107 case kPixelLocalStorage_GLSLFeature:
108 if (glslCaps.pixelLocalStorageSize() <= 0) {
ethannicholas22793252016-01-30 09:59:10 -0800109 return false;
110 }
111 this->addFeature(1 << kPixelLocalStorage_GLSLFeature,
112 "GL_EXT_shader_pixel_local_storage");
113 return true;
cdalton4a98cdb2016-03-01 12:12:20 -0800114 case kMultisampleInterpolation_GLSLFeature:
115 if (!glslCaps.multisampleInterpolationSupport()) {
116 return false;
117 }
118 if (const char* extension = glslCaps.multisampleInterpolationExtensionString()) {
119 this->addFeature(1 << kMultisampleInterpolation_GLSLFeature, extension);
120 }
121 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700122 default:
123 SkFAIL("Unexpected GLSLFeature requested.");
124 return false;
125 }
126}
127
egdaniel2d721d32015-11-11 13:06:05 -0800128SkString GrGLSLFragmentShaderBuilder::ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
129 int index) {
joshualitt23e280d2014-09-18 12:26:38 -0700130 if (kVec3f_GrSLType != coords[index].getType()) {
131 SkASSERT(kVec2f_GrSLType == coords[index].getType());
joshualitt30ba4362014-08-21 20:18:45 -0700132 return coords[index].getName();
133 }
134
135 SkString coords2D("coords2D");
136 if (0 != index) {
137 coords2D.appendf("_%i", index);
138 }
139 this->codeAppendf("\tvec2 %s = %s.xy / %s.z;",
140 coords2D.c_str(), coords[index].c_str(), coords[index].c_str());
141 return coords2D;
142}
143
egdaniel2d721d32015-11-11 13:06:05 -0800144const char* GrGLSLFragmentShaderBuilder::fragmentPosition() {
cdalton87332102016-02-26 12:22:02 -0800145 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;)
joshualitt30ba4362014-08-21 20:18:45 -0700146
egdaniel8dcdedc2015-11-11 06:27:20 -0800147 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
joshualitt30ba4362014-08-21 20:18:45 -0700148 // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
149 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
150 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
cdalton28f45b92016-03-07 13:58:26 -0800151 if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
joshualitt30ba4362014-08-21 20:18:45 -0700152 fSetupFragPosition = true;
153 return "gl_FragCoord";
egdaniel8dcdedc2015-11-11 06:27:20 -0800154 } else if (const char* extension = glslCaps->fragCoordConventionsExtensionString()) {
joshualitt30ba4362014-08-21 20:18:45 -0700155 if (!fSetupFragPosition) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800156 if (glslCaps->generation() < k150_GrGLSLGeneration) {
joshualitt30ba4362014-08-21 20:18:45 -0700157 this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature,
egdaniel8dcdedc2015-11-11 06:27:20 -0800158 extension);
joshualitt30ba4362014-08-21 20:18:45 -0700159 }
160 fInputs.push_back().set(kVec4f_GrSLType,
egdaniel0d3f0612015-10-21 10:45:48 -0700161 GrGLSLShaderVar::kIn_TypeModifier,
joshualitt30ba4362014-08-21 20:18:45 -0700162 "gl_FragCoord",
bsalomonc0bd6482014-12-09 10:04:14 -0800163 kDefault_GrSLPrecision,
egdanielae474182016-01-21 11:19:52 -0800164 "origin_upper_left");
joshualitt30ba4362014-08-21 20:18:45 -0700165 fSetupFragPosition = true;
166 }
167 return "gl_FragCoord";
168 } else {
robertphillips18c58c72015-07-21 12:06:52 -0700169 static const char* kTempName = "tmpXYFragCoord";
joshualitt30ba4362014-08-21 20:18:45 -0700170 static const char* kCoordName = "fragCoordYDown";
171 if (!fSetupFragPosition) {
joshualitt30ba4362014-08-21 20:18:45 -0700172 const char* rtHeightName;
173
egdaniel7ea439b2015-12-03 09:20:44 -0800174 fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName);
joshualitt30ba4362014-08-21 20:18:45 -0700175
robertphillips18c58c72015-07-21 12:06:52 -0700176 // The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
177 // Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
178 // depending on the surrounding code, accessing .xy with a uniform involved can
halcanary9d524f22016-03-29 09:03:52 -0700179 // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand
robertphillips18c58c72015-07-21 12:06:52 -0700180 // (and only accessing .xy) seems to "fix" things.
ethannicholas1cbb5ea2015-12-14 11:37:55 -0800181 const char* precision = glslCaps->usesPrecisionModifiers() ? "highp " : "";
182 this->codePrependf("\t%svec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n",
183 precision, kCoordName, kTempName, rtHeightName, kTempName);
184 this->codePrependf("%svec2 %s = gl_FragCoord.xy;", precision, kTempName);
joshualitt30ba4362014-08-21 20:18:45 -0700185 fSetupFragPosition = true;
186 }
187 SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid());
188 return kCoordName;
189 }
190}
191
dvonbeck9b03e7b2016-08-01 11:01:56 -0700192const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const {
193 return "fsDistanceVector";
194}
195
cdalton28f45b92016-03-07 13:58:26 -0800196void GrGLSLFragmentShaderBuilder::appendOffsetToSample(const char* sampleIdx, Coordinates coords) {
197 SkASSERT(fProgramBuilder->header().fSamplePatternKey);
198 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kSampleLocations_RequiredFeature);
199 if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
200 // With a top left origin, device and window space are equal, so we only use device coords.
201 coords = kSkiaDevice_Coordinates;
202 }
203 this->codeAppendf("%s[%s]", sample_offset_array_name(coords), sampleIdx);
204 fUsedSampleOffsetArrays |= (1 << coords);
205}
206
cdalton33ad7012016-02-22 07:55:44 -0800207void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool invert) {
208 const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
209 if (!glslCaps.sampleVariablesSupport()) {
210 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
211 return;
212 }
213 if (const char* extension = glslCaps.sampleVariablesExtensionString()) {
214 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
215 }
216 if (!fHasInitializedSampleMask) {
217 this->codePrependf("gl_SampleMask[0] = -1;");
218 fHasInitializedSampleMask = true;
219 }
220 if (invert) {
221 this->codeAppendf("gl_SampleMask[0] &= ~(%s);", mask);
222 } else {
223 this->codeAppendf("gl_SampleMask[0] &= %s;", mask);
224 }
225}
226
227void GrGLSLFragmentShaderBuilder::overrideSampleCoverage(const char* mask) {
228 const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
229 if (!glslCaps.sampleMaskOverrideCoverageSupport()) {
230 SkDEBUGFAIL("Attempted to override sample coverage without support.");
231 return;
232 }
233 SkASSERT(glslCaps.sampleVariablesSupport());
234 if (const char* extension = glslCaps.sampleVariablesExtensionString()) {
235 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
236 }
237 if (this->addFeature(1 << kSampleMaskOverrideCoverage_GLSLPrivateFeature,
238 "GL_NV_sample_mask_override_coverage")) {
239 // Redeclare gl_SampleMask with layout(override_coverage) if we haven't already.
240 fOutputs.push_back().set(kInt_GrSLType, GrShaderVar::kOut_TypeModifier,
241 "gl_SampleMask", 1, kHigh_GrSLPrecision,
242 "override_coverage");
243 }
244 this->codeAppendf("gl_SampleMask[0] = %s;", mask);
245 fHasInitializedSampleMask = true;
246}
247
egdaniel2d721d32015-11-11 13:06:05 -0800248const char* GrGLSLFragmentShaderBuilder::dstColor() {
cdalton87332102016-02-26 12:22:02 -0800249 SkDEBUGCODE(fHasReadDstColor = true;)
joshualitt47bb3822014-10-07 16:43:25 -0700250
ethannicholas22793252016-01-30 09:59:10 -0800251 const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride();
252 if (override != nullptr) {
253 return override;
254 }
255
egdaniel472d44e2015-10-22 08:20:00 -0700256 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
257 if (glslCaps->fbFetchSupport()) {
cdaltonc08f1962016-02-12 12:14:06 -0800258 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
egdaniel472d44e2015-10-22 08:20:00 -0700259 glslCaps->fbFetchExtensionString());
joshualittb4384b92014-10-21 12:53:15 -0700260
joshualitt3c1096f2015-01-13 13:13:59 -0800261 // Some versions of this extension string require declaring custom color output on ES 3.0+
egdaniel472d44e2015-10-22 08:20:00 -0700262 const char* fbFetchColorName = glslCaps->fbFetchColorName();
263 if (glslCaps->fbFetchNeedsCustomOutput()) {
joshualittb4384b92014-10-21 12:53:15 -0700264 this->enableCustomOutput();
265 fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
egdaniel8dcdedc2015-11-11 06:27:20 -0800266 fbFetchColorName = DeclaredColorOutputName();
egdaniel138c2632016-08-17 10:59:00 -0700267 // Set the dstColor to an intermediate variable so we don't override it with the output
268 this->codeAppendf("vec4 %s = %s;", kDstColorName, fbFetchColorName);
egdanielbd4121a2016-08-17 12:55:30 -0700269 } else {
270 return fbFetchColorName;
joshualittb4384b92014-10-21 12:53:15 -0700271 }
halcanary9d524f22016-03-29 09:03:52 -0700272 }
egdaniel138c2632016-08-17 10:59:00 -0700273 return kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700274}
275
egdaniel2d721d32015-11-11 13:06:05 -0800276void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
cdalton8917d622015-05-06 13:40:21 -0700277 SkASSERT(GrBlendEquationIsAdvanced(equation));
278
egdaniel472d44e2015-10-22 08:20:00 -0700279 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
cdalton8917d622015-05-06 13:40:21 -0700280 if (!caps.mustEnableAdvBlendEqs()) {
281 return;
282 }
283
284 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
285 "GL_KHR_blend_equation_advanced");
286 if (caps.mustEnableSpecificAdvBlendEqs()) {
287 this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
288 } else {
289 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
290 }
291}
292
egdaniel2d721d32015-11-11 13:06:05 -0800293void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
joshualittb4384b92014-10-21 12:53:15 -0700294 if (!fHasCustomColorOutput) {
295 fHasCustomColorOutput = true;
296 fCustomColorOutputIndex = fOutputs.count();
297 fOutputs.push_back().set(kVec4f_GrSLType,
egdaniel0d3f0612015-10-21 10:45:48 -0700298 GrGLSLShaderVar::kOut_TypeModifier,
egdaniel8dcdedc2015-11-11 06:27:20 -0800299 DeclaredColorOutputName());
egdanielb80ec8b2016-02-09 09:54:43 -0800300 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
joshualittb4384b92014-10-21 12:53:15 -0700301 }
joshualitt47bb3822014-10-07 16:43:25 -0700302}
303
egdaniel2d721d32015-11-11 13:06:05 -0800304void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
joshualitt47bb3822014-10-07 16:43:25 -0700305 SkASSERT(!fHasSecondaryOutput);
306 fHasSecondaryOutput = true;
egdaniel8dcdedc2015-11-11 06:27:20 -0800307 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
308 if (const char* extension = caps.secondaryOutputExtensionString()) {
309 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
kkinnunend94708e2015-07-30 22:47:04 -0700310 }
311
312 // If the primary output is declared, we must declare also the secondary output
313 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
314 // output. The condition also co-incides with the condition in whici GLES SL 2.0
315 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
kkinnunend94708e2015-07-30 22:47:04 -0700316 if (caps.mustDeclareFragmentShaderOutput()) {
egdaniel0d3f0612015-10-21 10:45:48 -0700317 fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModifier,
egdaniel8dcdedc2015-11-11 06:27:20 -0800318 DeclaredSecondaryColorOutputName());
egdanielb80ec8b2016-02-09 09:54:43 -0800319 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
kkinnunend94708e2015-07-30 22:47:04 -0700320 }
joshualitt47bb3822014-10-07 16:43:25 -0700321}
322
egdaniel2d721d32015-11-11 13:06:05 -0800323const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
egdaniel8dcdedc2015-11-11 06:27:20 -0800324 return fHasCustomColorOutput ? DeclaredColorOutputName() : "gl_FragColor";
joshualitt47bb3822014-10-07 16:43:25 -0700325}
326
ethannicholas22793252016-01-30 09:59:10 -0800327void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
328 va_list argp;
329 va_start(argp, fmt);
330 inputs().appendVAList(fmt, argp);
halcanary9d524f22016-03-29 09:03:52 -0700331 va_end(argp);
ethannicholas22793252016-01-30 09:59:10 -0800332}
333
egdaniel2d721d32015-11-11 13:06:05 -0800334const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
egdaniel8dcdedc2015-11-11 06:27:20 -0800335 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
336 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
kkinnunend94708e2015-07-30 22:47:04 -0700337 : "gl_SecondaryFragColorEXT";
joshualitt47bb3822014-10-07 16:43:25 -0700338}
339
cdalton28f45b92016-03-07 13:58:26 -0800340GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
341 SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
342 return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey);
343
344 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
345 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
346}
347
egdaniel2d721d32015-11-11 13:06:05 -0800348void GrGLSLFragmentShaderBuilder::onFinalize() {
egdaniel0eafe792015-11-20 14:01:22 -0800349 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
egdanielcb7ba1e2015-10-26 08:38:25 -0700350 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
351 *fProgramBuilder->glslCaps(),
352 &this->precisionQualifier());
cdalton28f45b92016-03-07 13:58:26 -0800353 if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) {
354 this->defineSampleOffsetArray(sample_offset_array_name(kSkiaDevice_Coordinates),
355 SkMatrix::MakeTrans(-0.5f, -0.5f));
356 }
357 if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) {
358 // With a top left origin, device and window space are equal, so we only use device coords.
359 SkASSERT(kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin());
360 SkMatrix m;
361 m.setScale(1, -1);
362 m.preTranslate(-0.5f, -0.5f);
363 this->defineSampleOffsetArray(sample_offset_array_name(kGLSLWindow_Coordinates), m);
364 }
365}
366
367void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, const SkMatrix& m) {
368 SkASSERT(fProgramBuilder->caps()->sampleLocationsSupport());
369 const GrPipeline& pipeline = fProgramBuilder->pipeline();
370 const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv();
371 const GrGpu::MultisampleSpecs& specs = rtp.getMultisampleSpecs(pipeline.getStencil());
372 SkSTArray<16, SkPoint, true> offsets;
373 offsets.push_back_n(specs.fEffectiveSampleCnt);
csmartdalton0d28e572016-07-06 09:59:43 -0700374 m.mapPoints(offsets.begin(), specs.fSampleLocations, specs.fEffectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -0800375 this->definitions().append("const ");
376 if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) {
377 this->definitions().append("highp ");
378 }
379 this->definitions().appendf("vec2 %s[] = vec2[](", name);
380 for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
381 this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y());
382 this->definitions().append(i + 1 != specs.fEffectiveSampleCnt ? ", " : ");\n");
383 }
joshualitt30ba4362014-08-21 20:18:45 -0700384}
385
cdalton85285412016-02-18 12:37:07 -0800386void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700387 SkASSERT(fSubstageIndices.count() >= 1);
wangyix7ef45a12015-08-13 06:51:35 -0700388 fSubstageIndices.push_back(0);
wangyix54a6b1a2015-09-08 08:41:51 -0700389 // second-to-last value in the fSubstageIndices stack is the index of the child proc
390 // at that level which is currently emitting code.
391 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
wangyix7ef45a12015-08-13 06:51:35 -0700392}
393
cdalton85285412016-02-18 12:37:07 -0800394void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700395 SkASSERT(fSubstageIndices.count() >= 2);
wangyix7ef45a12015-08-13 06:51:35 -0700396 fSubstageIndices.pop_back();
wangyix54a6b1a2015-09-08 08:41:51 -0700397 fSubstageIndices.back()++;
wangyix7ef45a12015-08-13 06:51:35 -0700398 int removeAt = fMangleString.findLastOf('_');
399 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
400}