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 | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 10 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 11 | #include "GrGLShaderBuilder.h" |
| 12 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | /* |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 14 | * 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] | 15 | */ |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 16 | class GrGLGPFragmentBuilder : public GrGLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | public: |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 18 | GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 19 | virtual ~GrGLGPFragmentBuilder() {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | /** |
| 21 | * 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 enableFeature() |
| 23 | */ |
| 24 | enum GLSLFeature { |
| 25 | kStandardDerivatives_GLSLFeature = 0, |
| 26 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 31 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 32 | */ |
| 33 | virtual bool enableFeature(GLSLFeature) = 0; |
| 34 | |
| 35 | /** |
| 36 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
| 37 | * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a |
| 38 | * perspective divide into the fragment shader (xy / z) to convert them to 2D. |
| 39 | */ |
| 40 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
| 41 | int index) = 0; |
| 42 | |
| 43 | |
| 44 | /** 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 half-integers). */ |
| 46 | virtual const char* fragmentPosition() = 0; |
| 47 | |
| 48 | private: |
| 49 | typedef GrGLShaderBuilder INHERITED; |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Fragment processor's, in addition to all of the above, may need to use dst color so they use |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 54 | * this builder to create their shader. Because this is the only shader builder the FP sees, we |
| 55 | * just call it FPShaderBuilder |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 56 | */ |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 57 | class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | public: |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 59 | GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 60 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 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. */ |
| 63 | virtual const char* dstColor() = 0; |
| 64 | |
| 65 | private: |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 66 | typedef GrGLGPFragmentBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 69 | // TODO rename to Fragment Builder |
| 70 | class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 71 | public: |
| 72 | typedef uint8_t DstReadKey; |
| 73 | typedef uint8_t FragPosKey; |
| 74 | |
| 75 | /** 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 there is no dst |
| 77 | copy read at all (in which case this function should not be called). */ |
| 78 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 79 | |
| 80 | /** Returns a key for reading the fragment location. This should only be called if there is an |
| 81 | effect that will requires the fragment position. If the fragment position is not required, |
| 82 | the key is 0. */ |
| 83 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
| 84 | |
| 85 | GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc); |
| 86 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 87 | // true public interface, defined explicitly in the abstract interfaces above |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
| 90 | int index) SK_OVERRIDE; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 91 | virtual const char* fragmentPosition() SK_OVERRIDE; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 92 | virtual const char* dstColor() SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 93 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 94 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
| 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 GrGLSLExpr4& inputCoverage); |
| 102 | bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const; |
| 103 | void bindFragmentShaderLocations(GrGLuint programID); |
| 104 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 105 | /* |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 106 | * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 107 | */ |
| 108 | void addVarying(GrSLType type, |
| 109 | const char* name, |
egdaniel | 6db9128 | 2014-09-02 08:02:38 -0700 | [diff] [blame] | 110 | const char** fsInName, |
| 111 | GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefault_Precision); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 112 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 113 | // As GLProcessors emit code, there are some conditions we need to verify. We use the below |
| 114 | // state to track this. The reset call is called per processor emitted. |
| 115 | bool hasReadDstColor() const { return fHasReadDstColor; } |
| 116 | bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } |
| 117 | void reset() { |
| 118 | fHasReadDstColor = false; |
| 119 | fHasReadFragmentPosition = false; |
| 120 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 121 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 122 | private: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 125 | */ |
| 126 | enum GLSLPrivateFeature { |
| 127 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 128 | kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
| 129 | }; |
| 130 | |
| 131 | // Interpretation of DstReadKey when generating code |
| 132 | enum { |
| 133 | kNoDstRead_DstReadKey = 0, |
| 134 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 135 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 136 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 137 | }; |
| 138 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 139 | // Interpretation of FragPosKey when generating code |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 140 | enum { |
| 141 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 142 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 143 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 144 | }; |
| 145 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame^] | 146 | static const char* kDstCopyColorName; |
| 147 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 148 | bool fHasCustomColorOutput; |
| 149 | bool fHasSecondaryOutput; |
| 150 | bool fSetupFragPosition; |
| 151 | bool fTopLeftFragPosRead; |
| 152 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -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 | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 157 | |
joshualitt | fe1233c | 2014-10-07 12:16:35 -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 |