• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrGLSLFragmentShaderBuilder.h"
9 #include "GrRenderTarget.h"
10 #include "GrRenderTargetPriv.h"
11 #include "GrShaderCaps.h"
12 #include "gl/GrGLGpu.h"
13 #include "glsl/GrGLSLProgramBuilder.h"
14 #include "glsl/GrGLSLUniformHandler.h"
15 #include "glsl/GrGLSLVarying.h"
16 #include "../private/GrGLSL.h"
17 
18 const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
19 
sample_offset_array_name(GrGLSLFPFragmentBuilder::Coordinates coords)20 static 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 
specific_layout_qualifier_name(GrBlendEquation equation)32 static 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) ==
70                      kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation);
71 }
72 
KeyForSurfaceOrigin(GrSurfaceOrigin origin)73 uint8_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);
79 }
80 
GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder * program)81 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
82     : GrGLSLFragmentBuilder(program)
83     , fSetupFragPosition(false)
84     , fHasCustomColorOutput(false)
85     , fCustomColorOutputIndex(-1)
86     , fHasSecondaryOutput(false)
87     , fUsedSampleOffsetArrays(0)
88     , fHasInitializedSampleMask(false)
89     , fDefaultPrecision(kMedium_GrSLPrecision) {
90     fSubstageIndices.push_back(0);
91 #ifdef SK_DEBUG
92     fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
93     fHasReadDstColor = false;
94 #endif
95 }
96 
enableFeature(GLSLFeature feature)97 bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) {
98     const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
99     switch (feature) {
100         case kMultisampleInterpolation_GLSLFeature:
101             if (!shaderCaps.multisampleInterpolationSupport()) {
102                 return false;
103             }
104             if (const char* extension = shaderCaps.multisampleInterpolationExtensionString()) {
105                 this->addFeature(1 << kMultisampleInterpolation_GLSLFeature, extension);
106             }
107             return true;
108         default:
109             SkFAIL("Unexpected GLSLFeature requested.");
110             return false;
111     }
112 }
113 
ensureCoords2D(const GrShaderVar & coords)114 SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
115     if (kVec3f_GrSLType != coords.getType()) {
116         SkASSERT(kVec2f_GrSLType == coords.getType());
117         return coords.getName();
118     }
119 
120     SkString coords2D;
121     coords2D.printf("%s_ensure2D", coords.c_str());
122     this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
123                       coords.c_str());
124     return coords2D;
125 }
126 
appendOffsetToSample(const char * sampleIdx,Coordinates coords)127 void GrGLSLFragmentShaderBuilder::appendOffsetToSample(const char* sampleIdx, Coordinates coords) {
128     SkASSERT(fProgramBuilder->header().fSamplePatternKey);
129     SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kSampleLocations_RequiredFeature);
130     if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
131         // With a top left origin, device and window space are equal, so we only use device coords.
132         coords = kSkiaDevice_Coordinates;
133     }
134     this->codeAppendf("%s[%s]", sample_offset_array_name(coords), sampleIdx);
135     fUsedSampleOffsetArrays |= (1 << coords);
136 }
137 
maskSampleCoverage(const char * mask,bool invert)138 void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool invert) {
139     const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
140     if (!shaderCaps.sampleVariablesSupport()) {
141         SkDEBUGFAIL("Attempted to mask sample coverage without support.");
142         return;
143     }
144     if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
145         this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
146     }
147     if (!fHasInitializedSampleMask) {
148         this->codePrependf("gl_SampleMask[0] = -1;");
149         fHasInitializedSampleMask = true;
150     }
151     if (invert) {
152         this->codeAppendf("gl_SampleMask[0] &= ~(%s);", mask);
153     } else {
154         this->codeAppendf("gl_SampleMask[0] &= %s;", mask);
155     }
156 }
157 
overrideSampleCoverage(const char * mask)158 void GrGLSLFragmentShaderBuilder::overrideSampleCoverage(const char* mask) {
159     const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
160     if (!shaderCaps.sampleMaskOverrideCoverageSupport()) {
161         SkDEBUGFAIL("Attempted to override sample coverage without support.");
162         return;
163     }
164     SkASSERT(shaderCaps.sampleVariablesSupport());
165     if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
166         this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
167     }
168     if (this->addFeature(1 << kSampleMaskOverrideCoverage_GLSLPrivateFeature,
169                          "GL_NV_sample_mask_override_coverage")) {
170         // Redeclare gl_SampleMask with layout(override_coverage) if we haven't already.
171         fOutputs.push_back().set(kInt_GrSLType, "gl_SampleMask", 1, GrShaderVar::kOut_TypeModifier,
172                                  kHigh_GrSLPrecision, "override_coverage");
173     }
174     this->codeAppendf("gl_SampleMask[0] = %s;", mask);
175     fHasInitializedSampleMask = true;
176 }
177 
elevateDefaultPrecision(GrSLPrecision precision)178 void GrGLSLFragmentShaderBuilder::elevateDefaultPrecision(GrSLPrecision precision) {
179     fDefaultPrecision = SkTMax(fDefaultPrecision, precision);
180 }
181 
dstColor()182 const char* GrGLSLFragmentShaderBuilder::dstColor() {
183     SkDEBUGCODE(fHasReadDstColor = true;)
184 
185     const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride();
186     if (override != nullptr) {
187         return override;
188     }
189 
190     const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
191     if (shaderCaps->fbFetchSupport()) {
192         this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
193                          shaderCaps->fbFetchExtensionString());
194 
195         // Some versions of this extension string require declaring custom color output on ES 3.0+
196         const char* fbFetchColorName = shaderCaps->fbFetchColorName();
197         if (shaderCaps->fbFetchNeedsCustomOutput()) {
198             this->enableCustomOutput();
199             fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
200             fbFetchColorName = DeclaredColorOutputName();
201             // Set the dstColor to an intermediate variable so we don't override it with the output
202             this->codeAppendf("vec4 %s = %s;", kDstColorName, fbFetchColorName);
203         } else {
204             return fbFetchColorName;
205         }
206     }
207     return kDstColorName;
208 }
209 
enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation)210 void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
211     SkASSERT(GrBlendEquationIsAdvanced(equation));
212 
213     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
214     if (!caps.mustEnableAdvBlendEqs()) {
215         return;
216     }
217 
218     this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
219                      "GL_KHR_blend_equation_advanced");
220     if (caps.mustEnableSpecificAdvBlendEqs()) {
221         this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
222     } else {
223         this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
224     }
225 }
226 
enableCustomOutput()227 void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
228     if (!fHasCustomColorOutput) {
229         fHasCustomColorOutput = true;
230         fCustomColorOutputIndex = fOutputs.count();
231         fOutputs.push_back().set(kVec4f_GrSLType, DeclaredColorOutputName(),
232                                  GrShaderVar::kOut_TypeModifier);
233         fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
234     }
235 }
236 
enableSecondaryOutput()237 void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
238     SkASSERT(!fHasSecondaryOutput);
239     fHasSecondaryOutput = true;
240     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
241     if (const char* extension = caps.secondaryOutputExtensionString()) {
242         this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
243     }
244 
245     // If the primary output is declared, we must declare also the secondary output
246     // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
247     // output. The condition also co-incides with the condition in whici GLES SL 2.0
248     // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
249     if (caps.mustDeclareFragmentShaderOutput()) {
250         fOutputs.push_back().set(kVec4f_GrSLType, DeclaredSecondaryColorOutputName(),
251                                  GrShaderVar::kOut_TypeModifier);
252         fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
253     }
254 }
255 
getPrimaryColorOutputName() const256 const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
257     return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor";
258 }
259 
declAppendf(const char * fmt,...)260 void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
261     va_list argp;
262     va_start(argp, fmt);
263     inputs().appendVAList(fmt, argp);
264     va_end(argp);
265 }
266 
getSecondaryColorOutputName() const267 const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
268     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
269     return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
270                                                   : "gl_SecondaryFragColorEXT";
271 }
272 
getSurfaceOrigin() const273 GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
274     SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
275     return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey);
276 
277     GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
278     GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
279 }
280 
onFinalize()281 void GrGLSLFragmentShaderBuilder::onFinalize() {
282     fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
283     GrGLSLAppendDefaultFloatPrecisionDeclaration(fDefaultPrecision,
284                                                  *fProgramBuilder->shaderCaps(),
285                                                  &this->precisionQualifier());
286     if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) {
287         this->defineSampleOffsetArray(sample_offset_array_name(kSkiaDevice_Coordinates),
288                                       SkMatrix::MakeTrans(-0.5f, -0.5f));
289     }
290     if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) {
291         // With a top left origin, device and window space are equal, so we only use device coords.
292         SkASSERT(kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin());
293         SkMatrix m;
294         m.setScale(1, -1);
295         m.preTranslate(-0.5f, -0.5f);
296         this->defineSampleOffsetArray(sample_offset_array_name(kGLSLWindow_Coordinates), m);
297     }
298 }
299 
defineSampleOffsetArray(const char * name,const SkMatrix & m)300 void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, const SkMatrix& m) {
301     SkASSERT(fProgramBuilder->caps()->sampleLocationsSupport());
302     const GrPipeline& pipeline = fProgramBuilder->pipeline();
303     const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv();
304     const GrGpu::MultisampleSpecs& specs = rtp.getMultisampleSpecs(pipeline);
305     SkSTArray<16, SkPoint, true> offsets;
306     offsets.push_back_n(specs.fEffectiveSampleCnt);
307     m.mapPoints(offsets.begin(), specs.fSampleLocations, specs.fEffectiveSampleCnt);
308     this->definitions().appendf("const highp vec2 %s[] = vec2[](", name);
309     for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
310         this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y());
311         this->definitions().append(i + 1 != specs.fEffectiveSampleCnt ? ", " : ");\n");
312     }
313 }
314 
onBeforeChildProcEmitCode()315 void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
316     SkASSERT(fSubstageIndices.count() >= 1);
317     fSubstageIndices.push_back(0);
318     // second-to-last value in the fSubstageIndices stack is the index of the child proc
319     // at that level which is currently emitting code.
320     fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
321 }
322 
onAfterChildProcEmitCode()323 void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
324     SkASSERT(fSubstageIndices.count() >= 2);
325     fSubstageIndices.pop_back();
326     fSubstageIndices.back()++;
327     int removeAt = fMangleString.findLastOf('_');
328     fMangleString.remove(removeAt, fMangleString.size() - removeAt);
329 }
330