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