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