blob: 8355f1de7d50f59d1dc369885aae6026b685d00c [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"
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrShaderCaps.h"
ethannicholas22793252016-01-30 09:59:10 -080012#include "gl/GrGLGpu.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080013#include "glsl/GrGLSLProgramBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "../private/GrGLSL.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) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050097 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
joshualitt30ba4362014-08-21 20:18:45 -070098 switch (feature) {
cdalton4a98cdb2016-03-01 12:12:20 -080099 case kPixelLocalStorage_GLSLFeature:
Brian Salomon1edc5b92016-11-29 13:43:46 -0500100 if (shaderCaps.pixelLocalStorageSize() <= 0) {
ethannicholas22793252016-01-30 09:59:10 -0800101 return false;
102 }
103 this->addFeature(1 << kPixelLocalStorage_GLSLFeature,
104 "GL_EXT_shader_pixel_local_storage");
105 return true;
cdalton4a98cdb2016-03-01 12:12:20 -0800106 case kMultisampleInterpolation_GLSLFeature:
Brian Salomon1edc5b92016-11-29 13:43:46 -0500107 if (!shaderCaps.multisampleInterpolationSupport()) {
cdalton4a98cdb2016-03-01 12:12:20 -0800108 return false;
109 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500110 if (const char* extension = shaderCaps.multisampleInterpolationExtensionString()) {
cdalton4a98cdb2016-03-01 12:12:20 -0800111 this->addFeature(1 << kMultisampleInterpolation_GLSLFeature, extension);
112 }
113 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700114 default:
115 SkFAIL("Unexpected GLSLFeature requested.");
116 return false;
117 }
118}
119
bsalomon1a1aa932016-09-12 09:30:36 -0700120SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
121 if (kVec3f_GrSLType != coords.getType()) {
122 SkASSERT(kVec2f_GrSLType == coords.getType());
123 return coords.getName();
joshualitt30ba4362014-08-21 20:18:45 -0700124 }
125
bsalomon1a1aa932016-09-12 09:30:36 -0700126 SkString coords2D;
127 coords2D.printf("%s_ensure2D", coords.c_str());
128 this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
129 coords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700130 return coords2D;
131}
132
egdaniel2d721d32015-11-11 13:06:05 -0800133const char* GrGLSLFragmentShaderBuilder::fragmentPosition() {
cdalton87332102016-02-26 12:22:02 -0800134 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;)
joshualitt30ba4362014-08-21 20:18:45 -0700135
Brian Salomon1edc5b92016-11-29 13:43:46 -0500136 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
joshualitt30ba4362014-08-21 20:18:45 -0700137 // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
138 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
139 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
cdalton28f45b92016-03-07 13:58:26 -0800140 if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
joshualitt30ba4362014-08-21 20:18:45 -0700141 fSetupFragPosition = true;
142 return "gl_FragCoord";
Brian Salomon1edc5b92016-11-29 13:43:46 -0500143 } else if (const char* extension = shaderCaps->fragCoordConventionsExtensionString()) {
joshualitt30ba4362014-08-21 20:18:45 -0700144 if (!fSetupFragPosition) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500145 if (shaderCaps->generation() < k150_GrGLSLGeneration) {
joshualitt30ba4362014-08-21 20:18:45 -0700146 this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature,
egdaniel8dcdedc2015-11-11 06:27:20 -0800147 extension);
joshualitt30ba4362014-08-21 20:18:45 -0700148 }
149 fInputs.push_back().set(kVec4f_GrSLType,
joshualitt30ba4362014-08-21 20:18:45 -0700150 "gl_FragCoord",
Brian Salomon99938a82016-11-21 13:41:08 -0500151 GrShaderVar::kIn_TypeModifier,
bsalomonc0bd6482014-12-09 10:04:14 -0800152 kDefault_GrSLPrecision,
egdanielae474182016-01-21 11:19:52 -0800153 "origin_upper_left");
joshualitt30ba4362014-08-21 20:18:45 -0700154 fSetupFragPosition = true;
155 }
156 return "gl_FragCoord";
157 } else {
robertphillips18c58c72015-07-21 12:06:52 -0700158 static const char* kTempName = "tmpXYFragCoord";
joshualitt30ba4362014-08-21 20:18:45 -0700159 static const char* kCoordName = "fragCoordYDown";
160 if (!fSetupFragPosition) {
joshualitt30ba4362014-08-21 20:18:45 -0700161 const char* rtHeightName;
162
egdaniel7ea439b2015-12-03 09:20:44 -0800163 fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName);
joshualitt30ba4362014-08-21 20:18:45 -0700164
robertphillips18c58c72015-07-21 12:06:52 -0700165 // The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
166 // Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
167 // depending on the surrounding code, accessing .xy with a uniform involved can
halcanary9d524f22016-03-29 09:03:52 -0700168 // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand
robertphillips18c58c72015-07-21 12:06:52 -0700169 // (and only accessing .xy) seems to "fix" things.
Ethan Nicholas6762dd62016-11-22 15:40:27 -0500170 this->codePrependf("\thighp vec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n", kCoordName,
171 kTempName, rtHeightName, kTempName);
172 this->codePrependf("highp vec2 %s = gl_FragCoord.xy;", kTempName);
joshualitt30ba4362014-08-21 20:18:45 -0700173 fSetupFragPosition = true;
174 }
175 SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid());
176 return kCoordName;
177 }
178}
179
dvonbeck9b03e7b2016-08-01 11:01:56 -0700180const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const {
181 return "fsDistanceVector";
182}
183
cdalton28f45b92016-03-07 13:58:26 -0800184void GrGLSLFragmentShaderBuilder::appendOffsetToSample(const char* sampleIdx, Coordinates coords) {
185 SkASSERT(fProgramBuilder->header().fSamplePatternKey);
186 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kSampleLocations_RequiredFeature);
187 if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
188 // With a top left origin, device and window space are equal, so we only use device coords.
189 coords = kSkiaDevice_Coordinates;
190 }
191 this->codeAppendf("%s[%s]", sample_offset_array_name(coords), sampleIdx);
192 fUsedSampleOffsetArrays |= (1 << coords);
193}
194
cdalton33ad7012016-02-22 07:55:44 -0800195void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool invert) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500196 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
197 if (!shaderCaps.sampleVariablesSupport()) {
cdalton33ad7012016-02-22 07:55:44 -0800198 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
199 return;
200 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500201 if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
cdalton33ad7012016-02-22 07:55:44 -0800202 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
203 }
204 if (!fHasInitializedSampleMask) {
205 this->codePrependf("gl_SampleMask[0] = -1;");
206 fHasInitializedSampleMask = true;
207 }
208 if (invert) {
209 this->codeAppendf("gl_SampleMask[0] &= ~(%s);", mask);
210 } else {
211 this->codeAppendf("gl_SampleMask[0] &= %s;", mask);
212 }
213}
214
215void GrGLSLFragmentShaderBuilder::overrideSampleCoverage(const char* mask) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500216 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
217 if (!shaderCaps.sampleMaskOverrideCoverageSupport()) {
cdalton33ad7012016-02-22 07:55:44 -0800218 SkDEBUGFAIL("Attempted to override sample coverage without support.");
219 return;
220 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500221 SkASSERT(shaderCaps.sampleVariablesSupport());
222 if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
cdalton33ad7012016-02-22 07:55:44 -0800223 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
224 }
225 if (this->addFeature(1 << kSampleMaskOverrideCoverage_GLSLPrivateFeature,
226 "GL_NV_sample_mask_override_coverage")) {
227 // Redeclare gl_SampleMask with layout(override_coverage) if we haven't already.
Brian Salomon99938a82016-11-21 13:41:08 -0500228 fOutputs.push_back().set(kInt_GrSLType, "gl_SampleMask", 1, GrShaderVar::kOut_TypeModifier,
229 kHigh_GrSLPrecision, "override_coverage");
cdalton33ad7012016-02-22 07:55:44 -0800230 }
231 this->codeAppendf("gl_SampleMask[0] = %s;", mask);
232 fHasInitializedSampleMask = true;
233}
234
egdaniel2d721d32015-11-11 13:06:05 -0800235const char* GrGLSLFragmentShaderBuilder::dstColor() {
cdalton87332102016-02-26 12:22:02 -0800236 SkDEBUGCODE(fHasReadDstColor = true;)
joshualitt47bb3822014-10-07 16:43:25 -0700237
ethannicholas22793252016-01-30 09:59:10 -0800238 const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride();
239 if (override != nullptr) {
240 return override;
241 }
242
Brian Salomon1edc5b92016-11-29 13:43:46 -0500243 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
244 if (shaderCaps->fbFetchSupport()) {
cdaltonc08f1962016-02-12 12:14:06 -0800245 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500246 shaderCaps->fbFetchExtensionString());
joshualittb4384b92014-10-21 12:53:15 -0700247
joshualitt3c1096f2015-01-13 13:13:59 -0800248 // Some versions of this extension string require declaring custom color output on ES 3.0+
Brian Salomon1edc5b92016-11-29 13:43:46 -0500249 const char* fbFetchColorName = shaderCaps->fbFetchColorName();
250 if (shaderCaps->fbFetchNeedsCustomOutput()) {
joshualittb4384b92014-10-21 12:53:15 -0700251 this->enableCustomOutput();
252 fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
egdaniel8dcdedc2015-11-11 06:27:20 -0800253 fbFetchColorName = DeclaredColorOutputName();
egdaniel138c2632016-08-17 10:59:00 -0700254 // Set the dstColor to an intermediate variable so we don't override it with the output
255 this->codeAppendf("vec4 %s = %s;", kDstColorName, fbFetchColorName);
egdanielbd4121a2016-08-17 12:55:30 -0700256 } else {
257 return fbFetchColorName;
joshualittb4384b92014-10-21 12:53:15 -0700258 }
halcanary9d524f22016-03-29 09:03:52 -0700259 }
egdaniel138c2632016-08-17 10:59:00 -0700260 return kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700261}
262
egdaniel2d721d32015-11-11 13:06:05 -0800263void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
cdalton8917d622015-05-06 13:40:21 -0700264 SkASSERT(GrBlendEquationIsAdvanced(equation));
265
Brian Salomon94efbf52016-11-29 13:43:05 -0500266 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
cdalton8917d622015-05-06 13:40:21 -0700267 if (!caps.mustEnableAdvBlendEqs()) {
268 return;
269 }
270
271 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
272 "GL_KHR_blend_equation_advanced");
273 if (caps.mustEnableSpecificAdvBlendEqs()) {
274 this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
275 } else {
276 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
277 }
278}
279
egdaniel2d721d32015-11-11 13:06:05 -0800280void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
joshualittb4384b92014-10-21 12:53:15 -0700281 if (!fHasCustomColorOutput) {
282 fHasCustomColorOutput = true;
283 fCustomColorOutputIndex = fOutputs.count();
Brian Salomon99938a82016-11-21 13:41:08 -0500284 fOutputs.push_back().set(kVec4f_GrSLType, DeclaredColorOutputName(),
285 GrShaderVar::kOut_TypeModifier);
ethannicholas5961bc92016-10-12 06:39:56 -0700286 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
joshualittb4384b92014-10-21 12:53:15 -0700287 }
joshualitt47bb3822014-10-07 16:43:25 -0700288}
289
egdaniel2d721d32015-11-11 13:06:05 -0800290void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
joshualitt47bb3822014-10-07 16:43:25 -0700291 SkASSERT(!fHasSecondaryOutput);
292 fHasSecondaryOutput = true;
Brian Salomon94efbf52016-11-29 13:43:05 -0500293 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800294 if (const char* extension = caps.secondaryOutputExtensionString()) {
295 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
kkinnunend94708e2015-07-30 22:47:04 -0700296 }
297
298 // If the primary output is declared, we must declare also the secondary output
299 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
300 // output. The condition also co-incides with the condition in whici GLES SL 2.0
301 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
kkinnunend94708e2015-07-30 22:47:04 -0700302 if (caps.mustDeclareFragmentShaderOutput()) {
Brian Salomon99938a82016-11-21 13:41:08 -0500303 fOutputs.push_back().set(kVec4f_GrSLType, DeclaredSecondaryColorOutputName(),
304 GrShaderVar::kOut_TypeModifier);
egdanielb80ec8b2016-02-09 09:54:43 -0800305 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
kkinnunend94708e2015-07-30 22:47:04 -0700306 }
joshualitt47bb3822014-10-07 16:43:25 -0700307}
308
egdaniel2d721d32015-11-11 13:06:05 -0800309const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
ethannicholas5961bc92016-10-12 06:39:56 -0700310 return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor";
joshualitt47bb3822014-10-07 16:43:25 -0700311}
312
ethannicholas22793252016-01-30 09:59:10 -0800313void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
314 va_list argp;
315 va_start(argp, fmt);
316 inputs().appendVAList(fmt, argp);
halcanary9d524f22016-03-29 09:03:52 -0700317 va_end(argp);
ethannicholas22793252016-01-30 09:59:10 -0800318}
319
egdaniel2d721d32015-11-11 13:06:05 -0800320const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
Brian Salomon94efbf52016-11-29 13:43:05 -0500321 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800322 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
kkinnunend94708e2015-07-30 22:47:04 -0700323 : "gl_SecondaryFragColorEXT";
joshualitt47bb3822014-10-07 16:43:25 -0700324}
325
cdalton28f45b92016-03-07 13:58:26 -0800326GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
327 SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
328 return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey);
329
330 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
331 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
332}
333
egdaniel2d721d32015-11-11 13:06:05 -0800334void GrGLSLFragmentShaderBuilder::onFinalize() {
egdaniel0eafe792015-11-20 14:01:22 -0800335 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
egdanielcb7ba1e2015-10-26 08:38:25 -0700336 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
Brian Salomon94efbf52016-11-29 13:43:05 -0500337 *fProgramBuilder->shaderCaps(),
egdanielcb7ba1e2015-10-26 08:38:25 -0700338 &this->precisionQualifier());
cdalton28f45b92016-03-07 13:58:26 -0800339 if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) {
340 this->defineSampleOffsetArray(sample_offset_array_name(kSkiaDevice_Coordinates),
341 SkMatrix::MakeTrans(-0.5f, -0.5f));
342 }
343 if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) {
344 // With a top left origin, device and window space are equal, so we only use device coords.
345 SkASSERT(kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin());
346 SkMatrix m;
347 m.setScale(1, -1);
348 m.preTranslate(-0.5f, -0.5f);
349 this->defineSampleOffsetArray(sample_offset_array_name(kGLSLWindow_Coordinates), m);
350 }
351}
352
353void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, const SkMatrix& m) {
354 SkASSERT(fProgramBuilder->caps()->sampleLocationsSupport());
355 const GrPipeline& pipeline = fProgramBuilder->pipeline();
356 const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv();
csmartdaltonc633abb2016-11-01 08:55:55 -0700357 const GrGpu::MultisampleSpecs& specs = rtp.getMultisampleSpecs(pipeline);
cdalton28f45b92016-03-07 13:58:26 -0800358 SkSTArray<16, SkPoint, true> offsets;
359 offsets.push_back_n(specs.fEffectiveSampleCnt);
csmartdalton0d28e572016-07-06 09:59:43 -0700360 m.mapPoints(offsets.begin(), specs.fSampleLocations, specs.fEffectiveSampleCnt);
Ethan Nicholas6762dd62016-11-22 15:40:27 -0500361 this->definitions().appendf("const highp vec2 %s[] = vec2[](", name);
cdalton28f45b92016-03-07 13:58:26 -0800362 for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
363 this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y());
364 this->definitions().append(i + 1 != specs.fEffectiveSampleCnt ? ", " : ");\n");
365 }
joshualitt30ba4362014-08-21 20:18:45 -0700366}
367
cdalton85285412016-02-18 12:37:07 -0800368void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700369 SkASSERT(fSubstageIndices.count() >= 1);
wangyix7ef45a12015-08-13 06:51:35 -0700370 fSubstageIndices.push_back(0);
wangyix54a6b1a2015-09-08 08:41:51 -0700371 // second-to-last value in the fSubstageIndices stack is the index of the child proc
372 // at that level which is currently emitting code.
373 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
wangyix7ef45a12015-08-13 06:51:35 -0700374}
375
cdalton85285412016-02-18 12:37:07 -0800376void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700377 SkASSERT(fSubstageIndices.count() >= 2);
wangyix7ef45a12015-08-13 06:51:35 -0700378 fSubstageIndices.pop_back();
wangyix54a6b1a2015-09-08 08:41:51 -0700379 fSubstageIndices.back()++;
wangyix7ef45a12015-08-13 06:51:35 -0700380 int removeAt = fMangleString.findLastOf('_');
381 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
382}