• 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 
17 const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
18 
specific_layout_qualifier_name(GrBlendEquation equation)19 static 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 
41     GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation);
42     GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation);
43     GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation);
44     GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation);
45     GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation);
46     GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation);
47     GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
48     GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
49     GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation);
50     GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation);
51     GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation);
52     GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation);
53     GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation);
54     GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation);
55     GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation);
56     // There's an illegal GrBlendEquation at the end there, hence the -1.
57     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) ==
58                      kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation - 1);
59 }
60 
KeyForSurfaceOrigin(GrSurfaceOrigin origin)61 uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) {
62     SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
63     return origin + 1;
64 
65     GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
66     GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin);
67 }
68 
GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder * program)69 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
70     : GrGLSLFragmentBuilder(program)
71     , fSetupFragPosition(false)
72     , fHasCustomColorOutput(false)
73     , fCustomColorOutputIndex(-1)
74     , fHasSecondaryOutput(false)
75     , fForceHighPrecision(false) {
76     fSubstageIndices.push_back(0);
77 #ifdef SK_DEBUG
78     fHasReadDstColor = false;
79 #endif
80 }
81 
ensureCoords2D(const GrShaderVar & coords)82 SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
83     if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) {
84         SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType());
85         return coords.getName();
86     }
87 
88     SkString coords2D;
89     coords2D.printf("%s_ensure2D", coords.c_str());
90     this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
91                       coords.c_str());
92     return coords2D;
93 }
94 
dstColor()95 const char* GrGLSLFragmentShaderBuilder::dstColor() {
96     SkDEBUGCODE(fHasReadDstColor = true;)
97 
98     const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
99     if (shaderCaps->fbFetchSupport()) {
100         this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
101                          shaderCaps->fbFetchExtensionString());
102 
103         // Some versions of this extension string require declaring custom color output on ES 3.0+
104         const char* fbFetchColorName = "sk_LastFragColor";
105         if (shaderCaps->fbFetchNeedsCustomOutput()) {
106             this->enableCustomOutput();
107             fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
108             fbFetchColorName = DeclaredColorOutputName();
109             // Set the dstColor to an intermediate variable so we don't override it with the output
110             this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
111         } else {
112             return fbFetchColorName;
113         }
114     }
115     return kDstColorName;
116 }
117 
enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation)118 void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
119     SkASSERT(GrBlendEquationIsAdvanced(equation));
120 
121     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
122     if (!caps.mustEnableAdvBlendEqs()) {
123         return;
124     }
125 
126     this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
127                      "GL_KHR_blend_equation_advanced");
128     if (caps.mustEnableSpecificAdvBlendEqs()) {
129         this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
130     } else {
131         this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
132     }
133 }
134 
enableCustomOutput()135 void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
136     if (!fHasCustomColorOutput) {
137         fHasCustomColorOutput = true;
138         fCustomColorOutputIndex = fOutputs.count();
139         fOutputs.push_back().set(kHalf4_GrSLType, DeclaredColorOutputName(),
140                                  GrShaderVar::kOut_TypeModifier);
141         fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
142     }
143 }
144 
enableSecondaryOutput()145 void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
146     SkASSERT(!fHasSecondaryOutput);
147     fHasSecondaryOutput = true;
148     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
149     if (const char* extension = caps.secondaryOutputExtensionString()) {
150         this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
151     }
152 
153     // If the primary output is declared, we must declare also the secondary output
154     // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
155     // output. The condition also co-incides with the condition in whici GLES SL 2.0
156     // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
157     if (caps.mustDeclareFragmentShaderOutput()) {
158         fOutputs.push_back().set(kHalf4_GrSLType, DeclaredSecondaryColorOutputName(),
159                                  GrShaderVar::kOut_TypeModifier);
160         fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
161     }
162 }
163 
getPrimaryColorOutputName() const164 const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
165     return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor";
166 }
167 
primaryColorOutputIsInOut() const168 bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
169     return fHasCustomColorOutput &&
170            fOutputs[fCustomColorOutputIndex].getTypeModifier() == GrShaderVar::kInOut_TypeModifier;
171 }
172 
declAppendf(const char * fmt,...)173 void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
174     va_list argp;
175     va_start(argp, fmt);
176     inputs().appendVAList(fmt, argp);
177     va_end(argp);
178 }
179 
getSecondaryColorOutputName() const180 const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
181     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
182     return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
183                                                   : "gl_SecondaryFragColorEXT";
184 }
185 
getSurfaceOrigin() const186 GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
187     SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
188     return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1);
189 
190     GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
191     GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin);
192 }
193 
onFinalize()194 void GrGLSLFragmentShaderBuilder::onFinalize() {
195     fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
196 }
197 
onBeforeChildProcEmitCode()198 void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
199     SkASSERT(fSubstageIndices.count() >= 1);
200     fSubstageIndices.push_back(0);
201     // second-to-last value in the fSubstageIndices stack is the index of the child proc
202     // at that level which is currently emitting code.
203     fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
204 }
205 
onAfterChildProcEmitCode()206 void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
207     SkASSERT(fSubstageIndices.count() >= 2);
208     fSubstageIndices.pop_back();
209     fSubstageIndices.back()++;
210     int removeAt = fMangleString.findLastOf('_');
211     fMangleString.remove(removeAt, fMangleString.size() - removeAt);
212 }
213