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 GrGLSLFragmentShaderBuilder_DEFINED 9 #define GrGLSLFragmentShaderBuilder_DEFINED 10 11 #include "GrBlend.h" 12 #include "GrGLSLShaderBuilder.h" 13 #include "GrProcessor.h" 14 15 class GrRenderTarget; 16 class GrGLSLVarying; 17 18 /* 19 * This base class encapsulates the common functionality which all processors use to build fragment 20 * shaders. 21 */ 22 class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder { 23 public: GrGLSLFragmentBuilder(GrGLSLProgramBuilder * program)24 GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {} ~GrGLSLFragmentBuilder()25 virtual ~GrGLSLFragmentBuilder() {} 26 27 /** 28 * This returns a variable name to access the 2D, perspective correct version of the coords in 29 * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If 30 * the coordinates are 3-dimensional, it a perspective divide into is emitted into the 31 * fragment shader (xy / z) to convert them to 2D. 32 */ 33 virtual SkString ensureCoords2D(const GrShaderVar&) = 0; 34 35 // TODO: remove this method. 36 void declAppendf(const char* fmt, ...); 37 38 private: 39 typedef GrGLSLShaderBuilder INHERITED; 40 }; 41 42 /* 43 * This class is used by fragment processors to build their fragment code. 44 */ 45 class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder { 46 public: 47 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ GrGLSLFPFragmentBuilder()48 GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} 49 50 enum Coordinates { 51 kSkiaDevice_Coordinates, 52 kGLSLWindow_Coordinates, 53 54 kLast_Coordinates = kGLSLWindow_Coordinates 55 }; 56 57 /** 58 * Fragment procs with child procs should call these functions before/after calling emitCode 59 * on a child proc. 60 */ 61 virtual void onBeforeChildProcEmitCode() = 0; 62 virtual void onAfterChildProcEmitCode() = 0; 63 64 virtual const SkString& getMangleString() const = 0; 65 66 virtual void forceHighPrecision() = 0; 67 }; 68 69 /* 70 * This class is used by Xfer processors to build their fragment code. 71 */ 72 class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder { 73 public: 74 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ GrGLSLXPFragmentBuilder()75 GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} 76 77 virtual bool hasCustomColorOutput() const = 0; 78 virtual bool hasSecondaryOutput() const = 0; 79 80 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr 81 * if no effect advertised that it will read the destination. */ 82 virtual const char* dstColor() = 0; 83 84 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with 85 this shader. It is only legal to call this method with an advanced blend equation, and only 86 if these equations are supported. */ 87 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; 88 }; 89 90 /* 91 * This class implements the various fragment builder interfaces. 92 */ 93 class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder { 94 public: 95 /** Returns a nonzero key for a surface's origin. This should only be called if a processor will 96 use the fragment position and/or sample locations. */ 97 static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin); 98 99 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); 100 101 // Shared GrGLSLFragmentBuilder interface. 102 virtual SkString ensureCoords2D(const GrShaderVar&) override; 103 104 // GrGLSLFPFragmentBuilder interface. getMangleString()105 const SkString& getMangleString() const override { return fMangleString; } 106 void onBeforeChildProcEmitCode() override; 107 void onAfterChildProcEmitCode() override; forceHighPrecision()108 void forceHighPrecision() override { fForceHighPrecision = true; } 109 110 // GrGLSLXPFragmentBuilder interface. hasCustomColorOutput()111 bool hasCustomColorOutput() const override { return fHasCustomColorOutput; } hasSecondaryOutput()112 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } 113 const char* dstColor() override; 114 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; 115 116 private: 117 // Private public interface, used by GrGLProgramBuilder to build a fragment shader 118 void enableCustomOutput(); 119 void enableSecondaryOutput(); 120 const char* getPrimaryColorOutputName() const; 121 const char* getSecondaryColorOutputName() const; 122 bool primaryColorOutputIsInOut() const; 123 124 #ifdef SK_DEBUG 125 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below 126 // state to track this. The reset call is called per processor emitted. hasReadDstColor()127 bool hasReadDstColor() const { return fHasReadDstColor; } resetVerification()128 void resetVerification() { 129 fHasReadDstColor = false; 130 } 131 #endif 132 DeclaredColorOutputName()133 static const char* DeclaredColorOutputName() { return "sk_FragColor"; } DeclaredSecondaryColorOutputName()134 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } 135 136 GrSurfaceOrigin getSurfaceOrigin() const; 137 138 void onFinalize() override; 139 140 static const char* kDstColorName; 141 142 /* 143 * State that tracks which child proc in the proc tree is currently emitting code. This is 144 * used to update the fMangleString, which is used to mangle the names of uniforms and functions 145 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep 146 * we are in the tree, and its second-to-last value is the index of the child proc at that 147 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that 148 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child. 149 */ 150 SkTArray<int> fSubstageIndices; 151 152 /* 153 * The mangle string is used to mangle the names of uniforms/functions emitted by the child 154 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle 155 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0], 156 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will 157 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's 158 * 1st child's 2nd child". 159 */ 160 SkString fMangleString; 161 162 bool fSetupFragPosition; 163 bool fHasCustomColorOutput; 164 int fCustomColorOutputIndex; 165 bool fHasSecondaryOutput; 166 bool fForceHighPrecision; 167 168 #ifdef SK_DEBUG 169 // some state to verify shaders and effects are consistent, this is reset between effects by 170 // the program creator 171 bool fHasReadDstColor; 172 #endif 173 174 friend class GrGLSLProgramBuilder; 175 friend class GrGLProgramBuilder; 176 }; 177 178 #endif 179