joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 11 | #include "GrGLShaderBuilder.h" |
| 12 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame^] | 13 | class GrGLVarying; |
| 14 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 15 | /* |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 16 | * This base class encapsulates the functionality which the GP uses to build fragment shaders |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | */ |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 18 | class GrGLGPFragmentBuilder : public GrGLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | public: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 20 | GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 21 | virtual ~GrGLGPFragmentBuilder() {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 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 | typedef GrGLShaderBuilder INHERITED; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * Fragment processor's, in addition to all of the above, may need to use dst color so they use |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 56 | * this builder to create their shader. Because this is the only shader builder the FP sees, we |
| 57 | * just call it FPShaderBuilder |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | */ |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 59 | class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | public: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 61 | GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 62 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | /** Returns the variable name that holds the color of the destination pixel. This may be NULL if |
| 64 | no effect advertised that it will read the destination. */ |
| 65 | virtual const char* dstColor() = 0; |
| 66 | |
| 67 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 68 | typedef GrGLGPFragmentBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 71 | // TODO rename to Fragment Builder |
| 72 | class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 73 | public: |
| 74 | typedef uint8_t DstReadKey; |
| 75 | typedef uint8_t FragPosKey; |
| 76 | |
| 77 | /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
| 78 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
| 79 | copy read at all (in which case this function should not be called). */ |
| 80 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 81 | |
| 82 | /** Returns a key for reading the fragment location. This should only be called if there is an |
| 83 | effect that will requires the fragment position. If the fragment position is not required, |
| 84 | the key is 0. */ |
| 85 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
| 86 | |
| 87 | GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc); |
| 88 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 89 | // true public interface, defined explicitly in the abstract interfaces above |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 90 | virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 91 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
| 92 | int index) SK_OVERRIDE; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 93 | virtual const char* fragmentPosition() SK_OVERRIDE; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 94 | virtual const char* dstColor() SK_OVERRIDE; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 95 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame^] | 96 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 97 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
| 98 | void emitCodeToReadDstTexture(); |
| 99 | void enableCustomOutput(); |
| 100 | void enableSecondaryOutput(); |
| 101 | const char* getPrimaryColorOutputName() const; |
| 102 | const char* getSecondaryColorOutputName() const; |
| 103 | void enableSecondaryOutput(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage); |
| 104 | void combineColorAndCoverage(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage); |
| 105 | bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const; |
| 106 | void bindFragmentShaderLocations(GrGLuint programID); |
| 107 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 108 | // As GLProcessors emit code, there are some conditions we need to verify. We use the below |
| 109 | // state to track this. The reset call is called per processor emitted. |
| 110 | bool hasReadDstColor() const { return fHasReadDstColor; } |
| 111 | bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } |
| 112 | void reset() { |
| 113 | fHasReadDstColor = false; |
| 114 | fHasReadFragmentPosition = false; |
| 115 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 116 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame^] | 117 | /* |
| 118 | * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader |
| 119 | */ |
| 120 | void addVarying(GrGLVarying*, GrGLShaderVar::Precision); |
| 121 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 124 | */ |
| 125 | enum GLSLPrivateFeature { |
| 126 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 127 | kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
| 128 | }; |
| 129 | |
| 130 | // Interpretation of DstReadKey when generating code |
| 131 | enum { |
| 132 | kNoDstRead_DstReadKey = 0, |
| 133 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 134 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 135 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 136 | }; |
| 137 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 138 | // Interpretation of FragPosKey when generating code |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 139 | enum { |
| 140 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 141 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 142 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 143 | }; |
| 144 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 145 | static const char* kDstCopyColorName; |
| 146 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 147 | bool fHasCustomColorOutput; |
| 148 | bool fHasSecondaryOutput; |
| 149 | bool fSetupFragPosition; |
| 150 | bool fTopLeftFragPosRead; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 151 | int fCustomColorOutputIndex; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 152 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 153 | // some state to verify shaders and effects are consistent, this is reset between effects by |
| 154 | // the program creator |
| 155 | bool fHasReadDstColor; |
| 156 | bool fHasReadFragmentPosition; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 157 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 158 | friend class GrGLNvprProgramBuilder; |
| 159 | friend class GrGLProgramBuilder; |
| 160 | |
| 161 | typedef GrGLFPFragmentBuilder INHERITED; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | #endif |