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 | */ |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 18 | class GrGLFragmentBuilder : public GrGLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | public: |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 20 | GrGLFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 21 | virtual ~GrGLFragmentBuilder() {} |
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: |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 51 | friend class GrGLPathProcessor; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 52 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 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 |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 58 | * this builder to create their shader. Because this is the only shader builder the FP sees, we |
| 59 | * just call it FPShaderBuilder |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | */ |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 61 | class GrGLXPFragmentBuilder : public GrGLFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 62 | public: |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 63 | GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 64 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 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 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 74 | private: |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 75 | typedef GrGLFragmentBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 78 | // TODO rename to Fragment Builder |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 79 | class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 80 | public: |
| 81 | typedef uint8_t DstReadKey; |
| 82 | typedef uint8_t FragPosKey; |
| 83 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 84 | /** Returns a key for adding code to read the dst texture color in service of effects that |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 85 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 86 | texture at all (in which case this function should not be called). */ |
| 87 | static DstReadKey KeyForDstRead(const GrTexture* dsttexture, const GrGLCaps&); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 94 | GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 95 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 96 | // true public interface, defined explicitly in the abstract interfaces above |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | bool enableFeature(GLSLFeature) override; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 98 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | int index) override; |
| 100 | const char* fragmentPosition() override; |
| 101 | const char* dstColor() override; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 102 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 103 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 104 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 105 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 106 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 107 | void enableCustomOutput(); |
| 108 | void enableSecondaryOutput(); |
| 109 | const char* getPrimaryColorOutputName() const; |
| 110 | const char* getSecondaryColorOutputName() const; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 111 | bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 112 | void bindFragmentShaderLocations(GrGLuint programID); |
| 113 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 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 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 122 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 123 | /* |
| 124 | * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader |
| 125 | */ |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 126 | void addVarying(GrGLVarying*, GrSLPrecision); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 127 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 130 | */ |
| 131 | enum GLSLPrivateFeature { |
| 132 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 133 | kBlendEquationAdvanced_GLSLPrivateFeature, |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame^] | 134 | kBlendFuncExtended_GLSLPrivateFeature, |
| 135 | kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | // Interpretation of DstReadKey when generating code |
| 139 | enum { |
| 140 | kNoDstRead_DstReadKey = 0, |
| 141 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 142 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 143 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 144 | }; |
| 145 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 146 | // Interpretation of FragPosKey when generating code |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 147 | enum { |
| 148 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 149 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 150 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 151 | }; |
| 152 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 153 | static const char* kDstTextureColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 154 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 155 | bool fHasCustomColorOutput; |
| 156 | bool fHasSecondaryOutput; |
| 157 | bool fSetupFragPosition; |
| 158 | bool fTopLeftFragPosRead; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 159 | int fCustomColorOutputIndex; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 160 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 161 | // some state to verify shaders and effects are consistent, this is reset between effects by |
| 162 | // the program creator |
| 163 | bool fHasReadDstColor; |
| 164 | bool fHasReadFragmentPosition; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 165 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 166 | friend class GrGLProgramBuilder; |
| 167 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 168 | typedef GrGLXPFragmentBuilder INHERITED; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | #endif |