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 | |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame^] | 13 | #include "glsl/GrGLSLProcessorTypes.h" |
| 14 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 15 | class GrGLVarying; |
| 16 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | /* |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 18 | * 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] | 19 | */ |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 20 | class GrGLFragmentBuilder : public GrGLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | public: |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 22 | GrGLFragmentBuilder(GrGLProgramBuilder* program) |
| 23 | : INHERITED(program) { |
| 24 | fSubstageIndices.push_back(0); |
| 25 | } |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 26 | virtual ~GrGLFragmentBuilder() {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 29 | * if code is added that uses one of these features without calling enableFeature() |
| 30 | */ |
| 31 | enum GLSLFeature { |
| 32 | kStandardDerivatives_GLSLFeature = 0, |
| 33 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 38 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 39 | */ |
| 40 | virtual bool enableFeature(GLSLFeature) = 0; |
| 41 | |
| 42 | /** |
| 43 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
| 44 | * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a |
| 45 | * perspective divide into the fragment shader (xy / z) to convert them to 2D. |
| 46 | */ |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame^] | 47 | virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, int index) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /** Returns a variable name that represents the position of the fragment in the FS. The position |
| 51 | is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */ |
| 52 | virtual const char* fragmentPosition() = 0; |
| 53 | |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Fragment procs with child procs should call these functions before/after calling emitCode |
| 56 | * on a child proc. |
| 57 | */ |
| 58 | void onBeforeChildProcEmitCode(); |
| 59 | void onAfterChildProcEmitCode(); |
| 60 | |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 61 | const SkString& getMangleString() const { return fMangleString; } |
| 62 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | private: |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 64 | /* |
| 65 | * State that tracks which child proc in the proc tree is currently emitting code. This is |
| 66 | * used to update the fMangleString, which is used to mangle the names of uniforms and functions |
| 67 | * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep |
| 68 | * we are in the tree, and its second-to-last value is the index of the child proc at that |
| 69 | * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that |
| 70 | * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child. |
| 71 | */ |
| 72 | SkTArray<int> fSubstageIndices; |
| 73 | |
| 74 | /* |
| 75 | * The mangle string is used to mangle the names of uniforms/functions emitted by the child |
| 76 | * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle |
| 77 | * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0], |
| 78 | * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will |
| 79 | * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's |
| 80 | * 1st child's 2nd child". |
| 81 | */ |
| 82 | SkString fMangleString; |
| 83 | |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 84 | friend class GrGLPathProcessor; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 85 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 86 | typedef GrGLShaderBuilder INHERITED; |
| 87 | }; |
| 88 | |
| 89 | /* |
| 90 | * 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] | 91 | * this builder to create their shader. Because this is the only shader builder the FP sees, we |
| 92 | * just call it FPShaderBuilder |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 93 | */ |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 94 | class GrGLXPFragmentBuilder : public GrGLFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 95 | public: |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 96 | GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 97 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 99 | no effect advertised that it will read the destination. */ |
| 100 | virtual const char* dstColor() = 0; |
| 101 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 102 | /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with |
| 103 | this shader. It is only legal to call this method with an advanced blend equation, and only |
| 104 | if these equations are supported. */ |
| 105 | virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; |
| 106 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | private: |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 108 | typedef GrGLFragmentBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 111 | // TODO rename to Fragment Builder |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 112 | class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 113 | public: |
| 114 | typedef uint8_t DstReadKey; |
| 115 | typedef uint8_t FragPosKey; |
| 116 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 117 | /** 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] | 118 | 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] | 119 | texture at all (in which case this function should not be called). */ |
| 120 | static DstReadKey KeyForDstRead(const GrTexture* dsttexture, const GrGLCaps&); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 121 | |
| 122 | /** Returns a key for reading the fragment location. This should only be called if there is an |
| 123 | effect that will requires the fragment position. If the fragment position is not required, |
| 124 | the key is 0. */ |
| 125 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
| 126 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 127 | GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 128 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 129 | // true public interface, defined explicitly in the abstract interfaces above |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | bool enableFeature(GLSLFeature) override; |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame^] | 131 | virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 132 | int index) override; |
| 133 | const char* fragmentPosition() override; |
| 134 | const char* dstColor() override; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 135 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 136 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 137 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 138 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 139 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 140 | void enableCustomOutput(); |
| 141 | void enableSecondaryOutput(); |
| 142 | const char* getPrimaryColorOutputName() const; |
| 143 | const char* getSecondaryColorOutputName() const; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 144 | bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 145 | void bindFragmentShaderLocations(GrGLuint programID); |
| 146 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 147 | // As GLProcessors emit code, there are some conditions we need to verify. We use the below |
| 148 | // state to track this. The reset call is called per processor emitted. |
| 149 | bool hasReadDstColor() const { return fHasReadDstColor; } |
| 150 | bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } |
| 151 | void reset() { |
| 152 | fHasReadDstColor = false; |
| 153 | fHasReadFragmentPosition = false; |
| 154 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 155 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 156 | /* |
| 157 | * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader |
| 158 | */ |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 159 | void addVarying(GrGLVarying*, GrSLPrecision); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 160 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 163 | */ |
| 164 | enum GLSLPrivateFeature { |
| 165 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 166 | kBlendEquationAdvanced_GLSLPrivateFeature, |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 167 | kBlendFuncExtended_GLSLPrivateFeature, |
| 168 | kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | // Interpretation of DstReadKey when generating code |
| 172 | enum { |
| 173 | kNoDstRead_DstReadKey = 0, |
| 174 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 175 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 176 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 177 | }; |
| 178 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 179 | // Interpretation of FragPosKey when generating code |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 180 | enum { |
| 181 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 182 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 183 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 184 | }; |
| 185 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 186 | static const char* kDstTextureColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 187 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 188 | bool fHasCustomColorOutput; |
| 189 | bool fHasSecondaryOutput; |
| 190 | bool fSetupFragPosition; |
| 191 | bool fTopLeftFragPosRead; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 192 | int fCustomColorOutputIndex; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 193 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 194 | // some state to verify shaders and effects are consistent, this is reset between effects by |
| 195 | // the program creator |
| 196 | bool fHasReadDstColor; |
| 197 | bool fHasReadFragmentPosition; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 198 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 199 | friend class GrGLProgramBuilder; |
| 200 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 201 | typedef GrGLXPFragmentBuilder INHERITED; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | #endif |