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