Home | History | Annotate | Download | only in builders
      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 #ifndef GrGLFragmentShaderBuilder_DEFINED
      9 #define GrGLFragmentShaderBuilder_DEFINED
     10 
     11 #include "GrGLShaderBuilder.h"
     12 
     13 class GrGLVarying;
     14 
     15 /*
     16  * This base class encapsulates the functionality which the GP uses to build fragment shaders
     17  */
     18 class GrGLFragmentBuilder : public GrGLShaderBuilder {
     19 public:
     20     GrGLFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
     21     virtual ~GrGLFragmentBuilder() {}
     22     /**
     23      * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
     24      * if code is added that uses one of these features without calling enableFeature()
     25      */
     26     enum GLSLFeature {
     27         kStandardDerivatives_GLSLFeature = 0,
     28         kLastGLSLFeature = kStandardDerivatives_GLSLFeature
     29     };
     30 
     31     /**
     32      * If the feature is supported then true is returned and any necessary #extension declarations
     33      * are added to the shaders. If the feature is not supported then false will be returned.
     34      */
     35     virtual bool enableFeature(GLSLFeature) = 0;
     36 
     37     /**
     38      * This returns a variable name to access the 2D, perspective correct version of the coords in
     39      * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
     40      * perspective divide into the fragment shader (xy / z) to convert them to 2D.
     41      */
     42     virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
     43                                       int index) = 0;
     44 
     45 
     46     /** Returns a variable name that represents the position of the fragment in the FS. The position
     47         is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
     48     virtual const char* fragmentPosition() = 0;
     49 
     50 private:
     51     friend class GrGLPathProcessor;
     52 
     53     typedef GrGLShaderBuilder INHERITED;
     54 };
     55 
     56 /*
     57  * Fragment processor's, in addition to all of the above, may need to use dst color so they use
     58  * this builder to create their shader.  Because this is the only shader builder the FP sees, we
     59  * just call it FPShaderBuilder
     60  */
     61 class GrGLXPFragmentBuilder : public GrGLFragmentBuilder {
     62 public:
     63     GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
     64 
     65     /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
     66         no effect advertised that it will read the destination. */
     67     virtual const char* dstColor() = 0;
     68 
     69     /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
     70         this shader. It is only legal to call this method with an advanced blend equation, and only
     71         if these equations are supported. */
     72     virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
     73 
     74 private:
     75     typedef GrGLFragmentBuilder INHERITED;
     76 };
     77 
     78 // TODO rename to Fragment Builder
     79 class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder {
     80 public:
     81     typedef uint8_t DstReadKey;
     82     typedef uint8_t FragPosKey;
     83 
     84     /**  Returns a key for adding code to read the copy-of-dst color in service of effects that
     85         require reading the dst. It must not return 0 because 0 indicates that there is no dst
     86         copy read at all (in which case this function should not be called). */
     87     static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
     88 
     89     /** Returns a key for reading the fragment location. This should only be called if there is an
     90        effect that will requires the fragment position. If the fragment position is not required,
     91        the key is 0. */
     92     static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
     93 
     94     GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey);
     95 
     96     // true public interface, defined explicitly in the abstract interfaces above
     97     bool enableFeature(GLSLFeature) override;
     98     virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
     99                                       int index) override;
    100     const char* fragmentPosition() override;
    101     const char* dstColor() override;
    102 
    103     void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
    104 
    105 private:
    106     // Private public interface, used by GrGLProgramBuilder to build a fragment shader
    107     void enableCustomOutput();
    108     void enableSecondaryOutput();
    109     const char* getPrimaryColorOutputName() const;
    110     const char* getSecondaryColorOutputName() const;
    111     bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds);
    112     void bindFragmentShaderLocations(GrGLuint programID);
    113 
    114     // As GLProcessors emit code, there are some conditions we need to verify.  We use the below
    115     // state to track this.  The reset call is called per processor emitted.
    116     bool hasReadDstColor() const { return fHasReadDstColor; }
    117     bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
    118     void reset() {
    119         fHasReadDstColor = false;
    120         fHasReadFragmentPosition = false;
    121     }
    122 
    123     /*
    124      * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
    125      */
    126     void addVarying(GrGLVarying*, GrSLPrecision);
    127 
    128     /**
    129      * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
    130      */
    131     enum GLSLPrivateFeature {
    132         kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
    133         kBlendEquationAdvanced_GLSLPrivateFeature,
    134         kLastGLSLPrivateFeature = kBlendEquationAdvanced_GLSLPrivateFeature
    135     };
    136 
    137     // Interpretation of DstReadKey when generating code
    138     enum {
    139         kNoDstRead_DstReadKey           = 0,
    140         kYesDstRead_DstReadKeyBit       = 0x1, // Set if we do a dst-copy-read.
    141         kUseAlphaConfig_DstReadKeyBit   = 0x2, // Set if dst-copy config is alpha only.
    142         kTopLeftOrigin_DstReadKeyBit    = 0x4, // Set if dst-copy origin is top-left.
    143     };
    144 
    145     // Interpretation of FragPosKey when generating code
    146     enum {
    147         kNoFragPosRead_FragPosKey           = 0,  // The fragment positition will not be needed.
    148         kTopLeftFragPosRead_FragPosKey      = 0x1,// Read frag pos relative to top-left.
    149         kBottomLeftFragPosRead_FragPosKey   = 0x2,// Read frag pos relative to bottom-left.
    150     };
    151 
    152     static const char* kDstCopyColorName;
    153 
    154     bool fHasCustomColorOutput;
    155     bool fHasSecondaryOutput;
    156     bool fSetupFragPosition;
    157     bool fTopLeftFragPosRead;
    158     int  fCustomColorOutputIndex;
    159 
    160     // some state to verify shaders and effects are consistent, this is reset between effects by
    161     // the program creator
    162     bool fHasReadDstColor;
    163     bool fHasReadFragmentPosition;
    164 
    165     friend class GrGLProgramBuilder;
    166 
    167     typedef GrGLXPFragmentBuilder INHERITED;
    168 };
    169 
    170 #endif
    171