| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGLFragmentShaderBuilder_DEFINED | 8 #ifndef GrGLFragmentShaderBuilder_DEFINED |
| 9 #define GrGLFragmentShaderBuilder_DEFINED | 9 #define GrGLFragmentShaderBuilder_DEFINED |
| 10 | |
| 11 #include "GrGLShaderBuilder.h" | 10 #include "GrGLShaderBuilder.h" |
| 12 | 11 |
| 12 class GrGLProgramBuilder; |
| 13 |
| 13 /* | 14 /* |
| 14 * This base class encapsulates the functionality which the GP uses to build fra
gment shaders | 15 * This base class encapsulates the functionality which all GrProcessors are all
owed to use in their |
| 16 * fragment shader |
| 15 */ | 17 */ |
| 16 class GrGLGPFragmentBuilder : public GrGLShaderBuilder { | 18 class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder { |
| 17 public: | 19 public: |
| 18 GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} | 20 GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} |
| 19 virtual ~GrGLGPFragmentBuilder() {} | 21 virtual ~GrGLProcessorFragmentShaderBuilder() {} |
| 20 /** | 22 /** |
| 21 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile | 23 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile |
| 22 * if code is added that uses one of these features without calling enableFe
ature() | 24 * if code is added that uses one of these features without calling enableFe
ature() |
| 23 */ | 25 */ |
| 24 enum GLSLFeature { | 26 enum GLSLFeature { |
| 25 kStandardDerivatives_GLSLFeature = 0, | 27 kStandardDerivatives_GLSLFeature = 0, |
| 26 kLastGLSLFeature = kStandardDerivatives_GLSLFeature | 28 kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 27 }; | 29 }; |
| 28 | 30 |
| 29 /** | 31 /** |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 /** Returns a variable name that represents the position of the fragment in
the FS. The position | 46 /** Returns a variable name that represents the position of the fragment in
the FS. The position |
| 45 is in device space (e.g. 0,0 is the top left and pixel centers are at ha
lf-integers). */ | 47 is in device space (e.g. 0,0 is the top left and pixel centers are at ha
lf-integers). */ |
| 46 virtual const char* fragmentPosition() = 0; | 48 virtual const char* fragmentPosition() = 0; |
| 47 | 49 |
| 48 private: | 50 private: |
| 49 typedef GrGLShaderBuilder INHERITED; | 51 typedef GrGLShaderBuilder INHERITED; |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 /* | 54 /* |
| 53 * Fragment processor's, in addition to all of the above, may need to use dst co
lor so they use | 55 * Fragment processor's, in addition to all of the above, may need to use dst co
lor so they use |
| 54 * this builder to create their shader. Because this is the only shader builder
the FP sees, we | 56 * this builder to create their shader |
| 55 * just call it FPShaderBuilder | |
| 56 */ | 57 */ |
| 57 class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder { | 58 class GrGLFragmentProcessorShaderBuilder : public GrGLProcessorFragmentShaderBui
lder { |
| 58 public: | 59 public: |
| 59 GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} | 60 GrGLFragmentProcessorShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} |
| 60 | |
| 61 /** Returns the variable name that holds the color of the destination pixel.
This may be NULL if | 61 /** Returns the variable name that holds the color of the destination pixel.
This may be NULL if |
| 62 no effect advertised that it will read the destination. */ | 62 no effect advertised that it will read the destination. */ |
| 63 virtual const char* dstColor() = 0; | 63 virtual const char* dstColor() = 0; |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 typedef GrGLGPFragmentBuilder INHERITED; | 66 typedef GrGLProcessorFragmentShaderBuilder INHERITED; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 // TODO rename to Fragment Builder | 69 class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorShaderBuilder { |
| 70 class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder { | |
| 71 public: | 70 public: |
| 72 typedef uint8_t DstReadKey; | 71 typedef uint8_t DstReadKey; |
| 73 typedef uint8_t FragPosKey; | 72 typedef uint8_t FragPosKey; |
| 74 | 73 |
| 75 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that | 74 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that |
| 76 require reading the dst. It must not return 0 because 0 indicates that t
here is no dst | 75 require reading the dst. It must not return 0 because 0 indicates that t
here is no dst |
| 77 copy read at all (in which case this function should not be called). */ | 76 copy read at all (in which case this function should not be called). */ |
| 78 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); | 77 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 79 | 78 |
| 80 /** Returns a key for reading the fragment location. This should only be cal
led if there is an | 79 /** Returns a key for reading the fragment location. This should only be cal
led if there is an |
| 81 effect that will requires the fragment position. If the fragment position
is not required, | 80 effect that will requires the fragment position. If the fragment position
is not required, |
| 82 the key is 0. */ | 81 the key is 0. */ |
| 83 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); | 82 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); |
| 84 | 83 |
| 85 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); | 84 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); |
| 86 | 85 |
| 87 // true public interface, defined explicitly in the abstract interfaces abov
e | 86 virtual const char* dstColor() SK_OVERRIDE; |
| 87 |
| 88 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; | 88 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
| 89 |
| 89 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArra
y& coords, | 90 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArra
y& coords, |
| 90 int index) SK_OVERRIDE; | 91 int index) SK_OVERRIDE; |
| 92 |
| 91 virtual const char* fragmentPosition() SK_OVERRIDE; | 93 virtual const char* fragmentPosition() SK_OVERRIDE; |
| 92 virtual const char* dstColor() SK_OVERRIDE; | |
| 93 | 94 |
| 94 // Private public interface, used by GrGLProgramBuilder to build a fragment
shader | 95 private: |
| 95 void emitCodeToReadDstTexture(); | |
| 96 void enableCustomOutput(); | |
| 97 void enableSecondaryOutput(); | |
| 98 const char* getPrimaryColorOutputName() const; | |
| 99 const char* getSecondaryColorOutputName() const; | |
| 100 void enableSecondaryOutput(const GrGLSLExpr4& inputColor, const GrGLSLExpr4&
inputCoverage); | |
| 101 void combineColorAndCoverage(const GrGLSLExpr4& inputColor, const GrGLSLExpr
4& inputCoverage); | |
| 102 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; | |
| 103 void bindFragmentShaderLocations(GrGLuint programID); | |
| 104 | |
| 105 /* | 96 /* |
| 106 * An internal call for GrGLProgramBuilder to use to add varyings to the ver
tex shader | 97 * An internal call for GrGLFullProgramBuilder to use to add varyings to the
vertex shader |
| 107 */ | 98 */ |
| 108 void addVarying(GrSLType type, | 99 void addVarying(GrSLType type, |
| 109 const char* name, | 100 const char* name, |
| 110 const char** fsInName, | 101 const char** fsInName, |
| 111 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); | 102 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); |
| 112 | 103 |
| 113 // As GLProcessors emit code, there are some conditions we need to verify.
We use the below | 104 /* |
| 114 // state to track this. The reset call is called per processor emitted. | 105 * Private functions used by GrGLProgramBuilder for compilation |
| 115 bool hasReadDstColor() const { return fHasReadDstColor; } | 106 */ |
| 116 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } | 107 void bindProgramLocations(GrGLuint programId); |
| 117 void reset() { | 108 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; |
| 118 fHasReadDstColor = false; | 109 void emitCodeBeforeEffects(); |
| 119 fHasReadFragmentPosition = false; | 110 void emitCodeAfterEffects(const GrGLSLExpr4& inputColor, const GrGLSLExpr4&
inputCoverage); |
| 120 } | |
| 121 | 111 |
| 122 private: | 112 /** Enables using the secondary color output and returns the name of the var
in which it is |
| 113 to be stored */ |
| 114 const char* enableSecondaryOutput(); |
| 115 |
| 116 /** Gets the name of the primary color output. */ |
| 117 const char* getColorOutputName() const; |
| 118 |
| 123 /** | 119 /** |
| 124 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. | 120 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 125 */ | 121 */ |
| 126 enum GLSLPrivateFeature { | 122 enum GLSLPrivateFeature { |
| 127 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, | 123 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 128 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature | 124 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
| 129 }; | 125 }; |
| 130 | 126 |
| 131 // Interpretation of DstReadKey when generating code | 127 // Interpretation of DstReadKey when generating code |
| 132 enum { | 128 enum { |
| 133 kNoDstRead_DstReadKey = 0, | 129 kNoDstRead_DstReadKey = 0, |
| 134 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. | 130 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 135 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. | 131 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. |
| 136 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. | 132 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. |
| 137 }; | 133 }; |
| 138 | 134 |
| 139 // Interpretation of FragPosKey when generating code | |
| 140 enum { | 135 enum { |
| 141 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 136 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
| 142 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 137 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
| 143 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 138 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
| 144 }; | 139 }; |
| 145 | 140 |
| 146 static const char* kDstCopyColorName; | |
| 147 | |
| 148 bool fHasCustomColorOutput; | 141 bool fHasCustomColorOutput; |
| 149 bool fHasSecondaryOutput; | 142 bool fHasSecondaryOutput; |
| 150 bool fSetupFragPosition; | 143 bool fSetupFragPosition; |
| 151 bool fTopLeftFragPosRead; | 144 bool fTopLeftFragPosRead; |
| 152 | 145 |
| 153 // some state to verify shaders and effects are consistent, this is reset be
tween effects by | 146 friend class GrGLProgramBuilder; |
| 154 // the program creator | 147 friend class GrGLFullProgramBuilder; |
| 155 bool fHasReadDstColor; | |
| 156 bool fHasReadFragmentPosition; | |
| 157 | 148 |
| 158 friend class GrGLNvprProgramBuilder; | 149 typedef GrGLFragmentProcessorShaderBuilder INHERITED; |
| 159 friend class GrGLProgramBuilder; | |
| 160 | |
| 161 typedef GrGLFPFragmentBuilder INHERITED; | |
| 162 }; | 150 }; |
| 163 | 151 |
| 164 #endif | 152 #endif |
| OLD | NEW |