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 fSubstageIndices.push_back(0);
72 }
73
ensureCoords2D(const GrShaderVar & coords)74 SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
75 if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) {
76 SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType());
77 return coords.getName();
78 }
79
80 SkString coords2D;
81 coords2D.printf("%s_ensure2D", coords.c_str());
82 this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
83 coords.c_str());
84 return coords2D;
85 }
86
sampleOffsets()87 const char* GrGLSLFragmentShaderBuilder::sampleOffsets() {
88 SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures());
89 SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
90 SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
91 return "_sampleOffsets";
92 }
93
maskOffMultisampleCoverage(const char * mask,Scope scope)94 void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage(const char* mask, Scope scope) {
95 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
96 if (!shaderCaps.sampleVariablesSupport()) {
97 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
98 return;
99 }
100 if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
101 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
102 }
103
104 if (!fHasInitializedSampleMask && Scope::kTopLevel == scope) {
105 this->codeAppendf("gl_SampleMask[0] = (%s);", mask);
106 fHasInitializedSampleMask = true;
107 return;
108 }
109 if (!fHasInitializedSampleMask) {
110 this->codePrependf("gl_SampleMask[0] = ~0;");
111 fHasInitializedSampleMask = true;
112 }
113 this->codeAppendf("gl_SampleMask[0] &= (%s);", mask);
114 }
115
dstColor()116 const char* GrGLSLFragmentShaderBuilder::dstColor() {
117 SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;)
118
119 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
120 if (shaderCaps->fbFetchSupport()) {
121 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
122 shaderCaps->fbFetchExtensionString());
123
124 // Some versions of this extension string require declaring custom color output on ES 3.0+
125 const char* fbFetchColorName = "sk_LastFragColor";
126 if (shaderCaps->fbFetchNeedsCustomOutput()) {
127 this->enableCustomOutput();
128 fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
129 fbFetchColorName = DeclaredColorOutputName();
130 // Set the dstColor to an intermediate variable so we don't override it with the output
131 this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
132 } else {
133 return fbFetchColorName;
134 }
135 }
136 return kDstColorName;
137 }
138
enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation)139 void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
140 SkASSERT(GrBlendEquationIsAdvanced(equation));
141
142 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
143 if (!caps.mustEnableAdvBlendEqs()) {
144 return;
145 }
146
147 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
148 "GL_KHR_blend_equation_advanced");
149 if (caps.mustEnableSpecificAdvBlendEqs()) {
150 this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
151 } else {
152 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
153 }
154 }
155
enableCustomOutput()156 void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
157 if (!fHasCustomColorOutput) {
158 fHasCustomColorOutput = true;
159 fCustomColorOutputIndex = fOutputs.count();
160 fOutputs.push_back().set(kHalf4_GrSLType, DeclaredColorOutputName(),
161 GrShaderVar::kOut_TypeModifier);
162 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
163 }
164 }
165
enableSecondaryOutput()166 void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
167 SkASSERT(!fHasSecondaryOutput);
168 fHasSecondaryOutput = true;
169 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
170 if (const char* extension = caps.secondaryOutputExtensionString()) {
171 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
172 }
173
174 // If the primary output is declared, we must declare also the secondary output
175 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
176 // output. The condition also co-incides with the condition in whici GLES SL 2.0
177 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
178 if (caps.mustDeclareFragmentShaderOutput()) {
179 fOutputs.push_back().set(kHalf4_GrSLType, DeclaredSecondaryColorOutputName(),
180 GrShaderVar::kOut_TypeModifier);
181 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
182 }
183 }
184
getPrimaryColorOutputName() const185 const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
186 return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor";
187 }
188
primaryColorOutputIsInOut() const189 bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
190 return fHasCustomColorOutput &&
191 fOutputs[fCustomColorOutputIndex].getTypeModifier() == GrShaderVar::kInOut_TypeModifier;
192 }
193
declAppendf(const char * fmt,...)194 void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
195 va_list argp;
196 va_start(argp, fmt);
197 inputs().appendVAList(fmt, argp);
198 va_end(argp);
199 }
200
getSecondaryColorOutputName() const201 const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
202 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
203 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
204 : "gl_SecondaryFragColorEXT";
205 }
206
getSurfaceOrigin() const207 GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
208 SkASSERT(fProgramBuilder->header().hasSurfaceOriginKey());
209 return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1);
210
211 GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
212 GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin);
213 }
214
onFinalize()215 void GrGLSLFragmentShaderBuilder::onFinalize() {
216 SkASSERT(fProgramBuilder->header().processorFeatures()
217 == fUsedProcessorFeaturesAllStages_DebugOnly);
218
219 if (CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures()) {
220 this->definitions().append("const float2 _sampleOffsets[] = float2[](");
221 const GrPipeline& pipeline = fProgramBuilder->pipeline();
222 const SkTArray<SkPoint>& sampleLocations =
223 fProgramBuilder->renderTarget()->renderTargetPriv().getSampleLocations(pipeline);
224 for (int i = 0; i < sampleLocations.count(); ++i) {
225 SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f);
226 if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
227 offset.fY = -offset.fY;
228 }
229 this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y());
230 this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");");
231 }
232 }
233
234 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
235 }
236
onBeforeChildProcEmitCode()237 void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
238 SkASSERT(fSubstageIndices.count() >= 1);
239 fSubstageIndices.push_back(0);
240 // second-to-last value in the fSubstageIndices stack is the index of the child proc
241 // at that level which is currently emitting code.
242 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
243 }
244
onAfterChildProcEmitCode()245 void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
246 SkASSERT(fSubstageIndices.count() >= 2);
247 fSubstageIndices.pop_back();
248 fSubstageIndices.back()++;
249 int removeAt = fMangleString.findLastOf('_');
250 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
251 }
252