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 | |
Brian Salomon | 096b091 | 2019-08-14 16:56:13 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrBlend.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProcessor.h" |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLShaderBuilder.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 15 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 16 | class GrRenderTarget; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 17 | class GrGLSLVarying; |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 18 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 20 | * This base class encapsulates the common functionality which all processors use to build fragment |
| 21 | * shaders. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | */ |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 23 | class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 24 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 25 | GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {} |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 26 | virtual ~GrGLSLFragmentBuilder() {} |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 27 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 29 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 30 | * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 31 | * the coordinates are 3-dimensional, it a perspective divide into is emitted into the |
| 32 | * fragment shader (xy / z) to convert them to 2D. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 33 | */ |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 34 | virtual SkString ensureCoords2D(const GrShaderVar&) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 35 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 36 | // TODO: remove this method. |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 37 | void declAppendf(const char* fmt, ...); |
| 38 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 39 | private: |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 40 | typedef GrGLSLShaderBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 44 | * This class is used by fragment processors to build their fragment code. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 45 | */ |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 46 | class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 47 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 48 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 49 | GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) { |
| 50 | // Suppress unused warning error |
| 51 | (void) fDummyPadding; |
| 52 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 53 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Returns the variable name that holds the array of sample offsets from pixel center to each |
| 56 | * sample location. Before this is called, a processor must have advertised that it will use |
| 57 | * CustomFeatures::kSampleLocations. |
| 58 | */ |
| 59 | virtual const char* sampleOffsets() = 0; |
| 60 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 61 | enum class ScopeFlags { |
| 62 | // Every fragment will always execute this code, and will do it exactly once. |
| 63 | kTopLevel = 0, |
| 64 | // Either all fragments in a given primitive, or none, will execute this code. |
| 65 | kInsidePerPrimitiveBranch = (1 << 0), |
| 66 | // Any given fragment may or may not execute this code. |
| 67 | kInsidePerPixelBranch = (1 << 1), |
| 68 | // This code will be executed more than once. |
| 69 | kInsideLoop = (1 << 2) |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /** |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 73 | * Subtracts multisample coverage by AND-ing the sample mask with the provided "mask". |
| 74 | * Sample N corresponds to bit "1 << N". |
| 75 | * |
| 76 | * If the given scope is "kTopLevel" and the sample mask has not yet been modified, this method |
| 77 | * assigns the sample mask in place rather than pre-initializing it to ~0 then AND-ing it. |
| 78 | * |
| 79 | * Requires MSAA and GLSL support for sample variables. |
| 80 | */ |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 81 | virtual void maskOffMultisampleCoverage(const char* mask, ScopeFlags) = 0; |
| 82 | |
| 83 | /** |
| 84 | * Turns off coverage at each sample where the implicit function fn > 0. |
| 85 | * |
| 86 | * The provided "fn" value represents the implicit function at pixel center. We then approximate |
| 87 | * the implicit at each sample by riding the gradient, "grad", linearly from pixel center to |
| 88 | * each sample location. |
| 89 | * |
| 90 | * If "grad" is null, we approximate the gradient using HW derivatives. |
| 91 | * |
| 92 | * Requires MSAA and GLSL support for sample variables. Also requires HW derivatives if not |
| 93 | * providing a gradient. |
| 94 | */ |
| 95 | virtual void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) = 0; |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 98 | * Fragment procs with child procs should call these functions before/after calling emitCode |
| 99 | * on a child proc. |
| 100 | */ |
| 101 | virtual void onBeforeChildProcEmitCode() = 0; |
| 102 | virtual void onAfterChildProcEmitCode() = 0; |
| 103 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 104 | virtual SkString writeProcessorFunction(GrGLSLFragmentProcessor* fp, |
| 105 | GrGLSLFragmentProcessor::EmitArgs& args); |
| 106 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 107 | virtual const SkString& getMangleString() const = 0; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 108 | |
| 109 | virtual void forceHighPrecision() = 0; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 110 | |
| 111 | private: |
| 112 | // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused |
| 113 | // by GrGLSLProgramBuilder's GrTAllocators requiring 16 byte alignment, but since |
| 114 | // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers |
| 115 | // to start aligned, even though clang is already correctly offsetting the individual fields |
| 116 | // that require the larger alignment. In the current world, this extra padding is sufficient to |
| 117 | // correctly initialize GrGLSLXPFragmentBuilder second. |
| 118 | char fDummyPadding[4]; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 121 | GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags); |
| 122 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 123 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 124 | * This class is used by Xfer processors to build their fragment code. |
| 125 | */ |
| 126 | class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
| 127 | public: |
| 128 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 129 | GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
| 130 | |
| 131 | virtual bool hasCustomColorOutput() const = 0; |
| 132 | virtual bool hasSecondaryOutput() const = 0; |
| 133 | |
| 134 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr |
| 135 | * if no effect advertised that it will read the destination. */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 136 | virtual const char* dstColor() = 0; |
| 137 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 138 | /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with |
| 139 | this shader. It is only legal to call this method with an advanced blend equation, and only |
| 140 | if these equations are supported. */ |
| 141 | virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 144 | /* |
| 145 | * This class implements the various fragment builder interfaces. |
| 146 | */ |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 147 | class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 148 | public: |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 149 | /** Returns a nonzero key for a surface's origin. This should only be called if a processor will |
| 150 | use the fragment position and/or sample locations. */ |
| 151 | static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin); |
| 152 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 153 | GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 154 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 155 | // Shared GrGLSLFragmentBuilder interface. |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 156 | virtual SkString ensureCoords2D(const GrShaderVar&) override; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 157 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 158 | // GrGLSLFPFragmentBuilder interface. |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 159 | const char* sampleOffsets() override; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 160 | void maskOffMultisampleCoverage(const char* mask, ScopeFlags) override; |
| 161 | void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) override; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 162 | const SkString& getMangleString() const override { return fMangleString; } |
| 163 | void onBeforeChildProcEmitCode() override; |
| 164 | void onAfterChildProcEmitCode() override; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 165 | void forceHighPrecision() override { fForceHighPrecision = true; } |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 166 | |
| 167 | // GrGLSLXPFragmentBuilder interface. |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 168 | bool hasCustomColorOutput() const override { return SkToBool(fCustomColorOutput); } |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 169 | bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } |
| 170 | const char* dstColor() override; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 171 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 172 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 173 | private: |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 174 | using CustomFeatures = GrProcessor::CustomFeatures; |
| 175 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 176 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 177 | void enableCustomOutput(); |
| 178 | void enableSecondaryOutput(); |
| 179 | const char* getPrimaryColorOutputName() const; |
| 180 | const char* getSecondaryColorOutputName() const; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 181 | bool primaryColorOutputIsInOut() const; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 182 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 183 | #ifdef SK_DEBUG |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 184 | // 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] | 185 | // state to track this. The reset call is called per processor emitted. |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 186 | bool fHasReadDstColorThisStage_DebugOnly = false; |
| 187 | CustomFeatures fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone; |
| 188 | CustomFeatures fUsedProcessorFeaturesAllStages_DebugOnly = CustomFeatures::kNone; |
| 189 | |
| 190 | void debugOnly_resetPerStageVerification() { |
| 191 | fHasReadDstColorThisStage_DebugOnly = false; |
| 192 | fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 193 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 194 | #endif |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 195 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 196 | static const char* DeclaredColorOutputName() { return "sk_FragColor"; } |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 197 | static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } |
| 198 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 199 | GrSurfaceOrigin getSurfaceOrigin() const; |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 200 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 201 | void onFinalize() override; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 202 | |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 203 | static const char* kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 204 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 205 | /* |
| 206 | * State that tracks which child proc in the proc tree is currently emitting code. This is |
| 207 | * used to update the fMangleString, which is used to mangle the names of uniforms and functions |
| 208 | * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep |
| 209 | * we are in the tree, and its second-to-last value is the index of the child proc at that |
| 210 | * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that |
| 211 | * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child. |
| 212 | */ |
| 213 | SkTArray<int> fSubstageIndices; |
| 214 | |
| 215 | /* |
| 216 | * The mangle string is used to mangle the names of uniforms/functions emitted by the child |
| 217 | * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle |
| 218 | * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0], |
| 219 | * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will |
| 220 | * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's |
| 221 | * 1st child's 2nd child". |
| 222 | */ |
| 223 | SkString fMangleString; |
| 224 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 225 | GrShaderVar* fCustomColorOutput = nullptr; |
| 226 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 227 | bool fSetupFragPosition = false; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 228 | bool fHasSecondaryOutput = false; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 229 | bool fHasModifiedSampleMask = false; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 230 | bool fForceHighPrecision = false; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 231 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 232 | friend class GrGLSLProgramBuilder; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 233 | friend class GrGLProgramBuilder; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | #endif |