GrGLSLFragmentShaderBuilder.cpp revision 9b03e7b29d963ea333a66dc5353e94f6391eb899
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 "gl/GrGLGpu.h"
12#include "glsl/GrGLSL.h"
13#include "glsl/GrGLSLCaps.h"
14#include "glsl/GrGLSLProgramBuilder.h"
15#include "glsl/GrGLSLUniformHandler.h"
16#include "glsl/GrGLSLVarying.h"
17
18const char* GrGLSLFragmentShaderBuilder::kDstTextureColorName = "_dstColor";
19
20static 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
32static 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
73uint8_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
81GrGLSLFragmentShaderBuilder::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    fSubstageIndices.push_back(0);
90#ifdef SK_DEBUG
91    fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
92    fHasReadDstColor = false;
93#endif
94}
95
96bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) {
97    const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
98    switch (feature) {
99        case kStandardDerivatives_GLSLFeature:
100            if (!glslCaps.shaderDerivativeSupport()) {
101                return false;
102            }
103            if (const char* extension = glslCaps.shaderDerivativeExtensionString()) {
104                this->addFeature(1 << kStandardDerivatives_GLSLFeature, extension);
105            }
106            return true;
107        case kPixelLocalStorage_GLSLFeature:
108            if (glslCaps.pixelLocalStorageSize() <= 0) {
109                return false;
110            }
111            this->addFeature(1 << kPixelLocalStorage_GLSLFeature,
112                             "GL_EXT_shader_pixel_local_storage");
113            return true;
114        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;
122        default:
123            SkFAIL("Unexpected GLSLFeature requested.");
124            return false;
125    }
126}
127
128SkString GrGLSLFragmentShaderBuilder::ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
129                                                       int index) {
130    if (kVec3f_GrSLType != coords[index].getType()) {
131        SkASSERT(kVec2f_GrSLType == coords[index].getType());
132        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
144const char* GrGLSLFragmentShaderBuilder::fragmentPosition() {
145    SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;)
146
147    const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
148    // 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.
151    if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
152        fSetupFragPosition = true;
153        return "gl_FragCoord";
154    } else if (const char* extension = glslCaps->fragCoordConventionsExtensionString()) {
155        if (!fSetupFragPosition) {
156            if (glslCaps->generation() < k150_GrGLSLGeneration) {
157                this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature,
158                                 extension);
159            }
160            fInputs.push_back().set(kVec4f_GrSLType,
161                                    GrGLSLShaderVar::kIn_TypeModifier,
162                                    "gl_FragCoord",
163                                    kDefault_GrSLPrecision,
164                                    "origin_upper_left");
165            fSetupFragPosition = true;
166        }
167        return "gl_FragCoord";
168    } else {
169        static const char* kTempName = "tmpXYFragCoord";
170        static const char* kCoordName = "fragCoordYDown";
171        if (!fSetupFragPosition) {
172            const char* rtHeightName;
173
174            fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName);
175
176            // 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
179            // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand
180            // (and only accessing .xy) seems to "fix" things.
181            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);
185            fSetupFragPosition = true;
186        }
187        SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid());
188        return kCoordName;
189    }
190}
191
192const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const {
193    return "fsDistanceVector";
194}
195
196void 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
207void 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
248const char* GrGLSLFragmentShaderBuilder::dstColor() {
249    SkDEBUGCODE(fHasReadDstColor = true;)
250
251    const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride();
252    if (override != nullptr) {
253        return override;
254    }
255
256    const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
257    if (glslCaps->fbFetchSupport()) {
258        this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
259                         glslCaps->fbFetchExtensionString());
260
261        // Some versions of this extension string require declaring custom color output on ES 3.0+
262        const char* fbFetchColorName = glslCaps->fbFetchColorName();
263        if (glslCaps->fbFetchNeedsCustomOutput()) {
264            this->enableCustomOutput();
265            fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
266            fbFetchColorName = DeclaredColorOutputName();
267        }
268        return fbFetchColorName;
269    } else {
270        return kDstTextureColorName;
271    }
272}
273
274void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
275    SkASSERT(GrBlendEquationIsAdvanced(equation));
276
277    const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
278    if (!caps.mustEnableAdvBlendEqs()) {
279        return;
280    }
281
282    this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
283                     "GL_KHR_blend_equation_advanced");
284    if (caps.mustEnableSpecificAdvBlendEqs()) {
285        this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
286    } else {
287        this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
288    }
289}
290
291void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
292    if (!fHasCustomColorOutput) {
293        fHasCustomColorOutput = true;
294        fCustomColorOutputIndex = fOutputs.count();
295        fOutputs.push_back().set(kVec4f_GrSLType,
296                                 GrGLSLShaderVar::kOut_TypeModifier,
297                                 DeclaredColorOutputName());
298        fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
299    }
300}
301
302void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
303    SkASSERT(!fHasSecondaryOutput);
304    fHasSecondaryOutput = true;
305    const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
306    if (const char* extension = caps.secondaryOutputExtensionString()) {
307        this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
308    }
309
310    // If the primary output is declared, we must declare also the secondary output
311    // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
312    // output. The condition also co-incides with the condition in whici GLES SL 2.0
313    // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
314    if (caps.mustDeclareFragmentShaderOutput()) {
315        fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModifier,
316                                 DeclaredSecondaryColorOutputName());
317        fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
318    }
319}
320
321const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
322    return fHasCustomColorOutput ? DeclaredColorOutputName() : "gl_FragColor";
323}
324
325void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
326    va_list argp;
327    va_start(argp, fmt);
328    inputs().appendVAList(fmt, argp);
329    va_end(argp);
330}
331
332const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
333    const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
334    return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
335                                                  : "gl_SecondaryFragColorEXT";
336}
337
338GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
339    SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
340    return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey);
341
342    GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
343    GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
344}
345
346void GrGLSLFragmentShaderBuilder::onFinalize() {
347    fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
348    GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
349                                                 *fProgramBuilder->glslCaps(),
350                                                 &this->precisionQualifier());
351    if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) {
352        this->defineSampleOffsetArray(sample_offset_array_name(kSkiaDevice_Coordinates),
353                                      SkMatrix::MakeTrans(-0.5f, -0.5f));
354    }
355    if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) {
356        // With a top left origin, device and window space are equal, so we only use device coords.
357        SkASSERT(kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin());
358        SkMatrix m;
359        m.setScale(1, -1);
360        m.preTranslate(-0.5f, -0.5f);
361        this->defineSampleOffsetArray(sample_offset_array_name(kGLSLWindow_Coordinates), m);
362    }
363}
364
365void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, const SkMatrix& m) {
366    SkASSERT(fProgramBuilder->caps()->sampleLocationsSupport());
367    const GrPipeline& pipeline = fProgramBuilder->pipeline();
368    const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv();
369    const GrGpu::MultisampleSpecs& specs = rtp.getMultisampleSpecs(pipeline.getStencil());
370    SkSTArray<16, SkPoint, true> offsets;
371    offsets.push_back_n(specs.fEffectiveSampleCnt);
372    m.mapPoints(offsets.begin(), specs.fSampleLocations, specs.fEffectiveSampleCnt);
373    this->definitions().append("const ");
374    if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) {
375        this->definitions().append("highp ");
376    }
377    this->definitions().appendf("vec2 %s[] = vec2[](", name);
378    for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
379        this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y());
380        this->definitions().append(i + 1 != specs.fEffectiveSampleCnt ? ", " : ");\n");
381    }
382}
383
384void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
385    SkASSERT(fSubstageIndices.count() >= 1);
386    fSubstageIndices.push_back(0);
387    // second-to-last value in the fSubstageIndices stack is the index of the child proc
388    // at that level which is currently emitting code.
389    fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
390}
391
392void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
393    SkASSERT(fSubstageIndices.count() >= 2);
394    fSubstageIndices.pop_back();
395    fSubstageIndices.back()++;
396    int removeAt = fMangleString.findLastOf('_');
397    fMangleString.remove(removeAt, fMangleString.size() - removeAt);
398}
399