bsalomon | 6251d17 | 2014-10-15 10:50:36 -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 GrFragmentProcessor_DEFINED |
| 9 | #define GrFragmentProcessor_DEFINED |
| 10 | |
| 11 | #include "GrProcessor.h" |
| 12 | |
| 13 | class GrCoordTransform; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | class GrGLSLFragmentProcessor; |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 15 | class GrInvariantOutput; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 16 | class GrPipeline; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 17 | class GrProcessorKeyBuilder; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 18 | class GrShaderCaps; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 19 | class GrSwizzle; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 20 | |
Ethan Nicholas | 05d5a13 | 2017-09-15 11:42:17 -0400 | [diff] [blame] | 21 | /** Provides custom fragment shader code. Fragment processors receive an input color (half4) and |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 22 | produce an output color. They may reference textures and uniforms. They may use |
| 23 | GrCoordTransforms to receive a transformation of the local coordinates that map from local space |
| 24 | to the fragment being processed. |
| 25 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 26 | class GrFragmentProcessor : public GrResourceIOProcessor { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 27 | public: |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 28 | /** |
| 29 | * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to |
| 30 | * only consider the input color's alpha. However, there is a competing desire to have reusable |
| 31 | * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input |
| 32 | * color is considered. This function exists to filter the input color and pass it to a FP. It |
| 33 | * does so by returning a parent FP that multiplies the passed in FPs output by the parent's |
| 34 | * input alpha. The passed in FP will not receive an input color. |
| 35 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 36 | static std::unique_ptr<GrFragmentProcessor> MulOutputByInputAlpha( |
| 37 | std::unique_ptr<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 40 | * This assumes that the input color to the returned processor will be unpremul and that the |
| 41 | * passed processor (which becomes the returned processor's child) produces a premul output. |
| 42 | * The result of the returned processor is a premul of its input color modulated by the child |
| 43 | * processor's premul output. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 44 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 45 | static std::unique_ptr<GrFragmentProcessor> MakeInputPremulAndMulByOutput( |
| 46 | std::unique_ptr<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 49 | * Returns a parent fragment processor that adopts the passed fragment processor as a child. |
| 50 | * The parent will ignore its input color and instead feed the passed in color as input to the |
| 51 | * child. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 52 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 53 | static std::unique_ptr<GrFragmentProcessor> OverrideInput(std::unique_ptr<GrFragmentProcessor>, |
| 54 | GrColor4f); |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 55 | |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 56 | /** |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 57 | * Returns a fragment processor that premuls the input before calling the passed in fragment |
| 58 | * processor. |
| 59 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 60 | static std::unique_ptr<GrFragmentProcessor> PremulInput(std::unique_ptr<GrFragmentProcessor>); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 63 | * Returns a fragment processor that calls the passed in fragment processor, and then premuls |
| 64 | * the output. |
| 65 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 66 | static std::unique_ptr<GrFragmentProcessor> PremulOutput(std::unique_ptr<GrFragmentProcessor>); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Returns a fragment processor that calls the passed in fragment processor, and then unpremuls |
| 70 | * the output. |
| 71 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 72 | static std::unique_ptr<GrFragmentProcessor> UnpremulOutput( |
| 73 | std::unique_ptr<GrFragmentProcessor>); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 74 | |
| 75 | /** |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 76 | * Returns a fragment processor that calls the passed in fragment processor, and then swizzles |
| 77 | * the output. |
| 78 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 79 | static std::unique_ptr<GrFragmentProcessor> SwizzleOutput(std::unique_ptr<GrFragmentProcessor>, |
| 80 | const GrSwizzle&); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 81 | |
| 82 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 83 | * Returns a fragment processor that runs the passed in array of fragment processors in a |
| 84 | * series. The original input is passed to the first, the first's output is passed to the |
| 85 | * second, etc. The output of the returned processor is the output of the last processor of the |
| 86 | * series. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 87 | * |
| 88 | * The array elements with be moved. |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 89 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 90 | static std::unique_ptr<GrFragmentProcessor> RunInSeries(std::unique_ptr<GrFragmentProcessor>*, |
| 91 | int cnt); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 92 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 93 | /** |
| 94 | * Makes a copy of this fragment processor that draws equivalently to the original. |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 95 | * If the processor has child processors they are cloned as well. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 96 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 97 | virtual std::unique_ptr<GrFragmentProcessor> clone() const = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 98 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 99 | GrGLSLFragmentProcessor* createGLSLInstance() const; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 100 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 101 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 102 | this->onGetGLSLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 103 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 104 | fChildProcessors[i]->getGLSLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 108 | int numCoordTransforms() const { return fCoordTransforms.count(); } |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 109 | |
| 110 | /** Returns the coordinate transformation at index. index must be valid according to |
| 111 | numTransforms(). */ |
| 112 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 113 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 114 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 115 | return fCoordTransforms; |
| 116 | } |
| 117 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 118 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 119 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 120 | const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 121 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 122 | bool instantiate(GrResourceProvider*) const; |
| 123 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 124 | void markPendingExecution() const; |
| 125 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 126 | /** Do any of the coordtransforms for this processor require local coords? */ |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 127 | bool usesLocalCoords() const { return SkToBool(fFlags & kUsesLocalCoords_Flag); } |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 128 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 129 | /** |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 130 | * A GrDrawOp may premultiply its antialiasing coverage into its GrGeometryProcessor's color |
| 131 | * output under the following scenario: |
| 132 | * * all the color fragment processors report true to this query, |
| 133 | * * all the coverage fragment processors report true to this query, |
| 134 | * * the blend mode arithmetic allows for it it. |
| 135 | * To be compatible a fragment processor's output must be a modulation of its input color or |
| 136 | * alpha with a computed premultiplied color or alpha that is in 0..1 range. The computed color |
| 137 | * or alpha that is modulated against the input cannot depend on the input's alpha. The computed |
| 138 | * value cannot depend on the input's color channels unless it unpremultiplies the input color |
| 139 | * channels by the input alpha. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 140 | */ |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 141 | bool compatibleWithCoverageAsAlpha() const { |
| 142 | return SkToBool(fFlags & kCompatibleWithCoverageAsAlpha_OptimizationFlag); |
| 143 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * If this is true then all opaque input colors to the processor produce opaque output colors. |
| 147 | */ |
| 148 | bool preservesOpaqueInput() const { |
| 149 | return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Tests whether given a constant input color the processor produces a constant output color |
| 154 | * (for all fragments). If true outputColor will contain the constant color produces for |
| 155 | * inputColor. |
| 156 | */ |
| 157 | bool hasConstantOutputForConstantInput(GrColor4f inputColor, GrColor4f* outputColor) const { |
| 158 | if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 159 | *outputColor = this->constantOutputForConstantInput(inputColor); |
| 160 | return true; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | bool hasConstantOutputForConstantInput() const { |
| 165 | return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag); |
| 166 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 167 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 168 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 169 | true when the two processor are of the same subclass (i.e. they return the same object from |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 170 | from getFactory()). |
| 171 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 172 | A return value of true from isEqual() should not be used to test whether the processor would |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 173 | generate the same shader code. To test for identical code generation use getGLSLProcessorKey |
| 174 | */ |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 175 | bool isEqual(const GrFragmentProcessor& that) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 176 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 177 | /** |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 178 | * Pre-order traversal of a FP hierarchy, or of the forest of FPs in a GrPipeline. In the latter |
| 179 | * case the tree rooted at each FP in the GrPipeline is visited successively. |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 180 | */ |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 181 | class Iter : public SkNoncopyable { |
| 182 | public: |
| 183 | explicit Iter(const GrFragmentProcessor* fp) { fFPStack.push_back(fp); } |
| 184 | explicit Iter(const GrPipeline& pipeline); |
| 185 | const GrFragmentProcessor* next(); |
| 186 | |
| 187 | private: |
| 188 | SkSTArray<4, const GrFragmentProcessor*, true> fFPStack; |
| 189 | }; |
| 190 | |
| 191 | /** |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 192 | * Iterates over all the Ts owned by a GrFragmentProcessor and its children or over all the Ts |
| 193 | * owned by the forest of GrFragmentProcessors in a GrPipeline. FPs are visited in the same |
| 194 | * order as Iter and each of an FP's Ts are visited in order. |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 195 | */ |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 196 | template <typename T, typename BASE, |
| 197 | int (BASE::*COUNT)() const, |
| 198 | const T& (BASE::*GET)(int) const> |
| 199 | class FPItemIter : public SkNoncopyable { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 200 | public: |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 201 | explicit FPItemIter(const GrFragmentProcessor* fp) |
| 202 | : fCurrFP(nullptr) |
| 203 | , fCTIdx(0) |
| 204 | , fFPIter(fp) { |
| 205 | fCurrFP = fFPIter.next(); |
| 206 | } |
| 207 | explicit FPItemIter(const GrPipeline& pipeline) |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 208 | : fCurrFP(nullptr) |
| 209 | , fCTIdx(0) |
| 210 | , fFPIter(pipeline) { |
| 211 | fCurrFP = fFPIter.next(); |
| 212 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 213 | |
| 214 | const T* next() { |
| 215 | if (!fCurrFP) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | while (fCTIdx == (fCurrFP->*COUNT)()) { |
| 219 | fCTIdx = 0; |
| 220 | fCurrFP = fFPIter.next(); |
| 221 | if (!fCurrFP) { |
| 222 | return nullptr; |
| 223 | } |
| 224 | } |
| 225 | return &(fCurrFP->*GET)(fCTIdx++); |
| 226 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 227 | |
| 228 | private: |
| 229 | const GrFragmentProcessor* fCurrFP; |
| 230 | int fCTIdx; |
| 231 | GrFragmentProcessor::Iter fFPIter; |
| 232 | }; |
| 233 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 234 | using CoordTransformIter = FPItemIter<GrCoordTransform, |
| 235 | GrFragmentProcessor, |
| 236 | &GrFragmentProcessor::numCoordTransforms, |
| 237 | &GrFragmentProcessor::coordTransform>; |
| 238 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 239 | using TextureAccessIter = FPItemIter<TextureSampler, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 240 | GrResourceIOProcessor, |
| 241 | &GrResourceIOProcessor::numTextureSamplers, |
| 242 | &GrResourceIOProcessor::textureSampler>; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 243 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 244 | void visitProxies(const std::function<void(GrSurfaceProxy*)>& func) { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 245 | GrFragmentProcessor::TextureAccessIter iter(this); |
| 246 | while (const GrResourceIOProcessor::TextureSampler* sampler = iter.next()) { |
| 247 | func(sampler->proxy()); |
| 248 | } |
| 249 | } |
| 250 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 251 | protected: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 252 | enum OptimizationFlags : uint32_t { |
| 253 | kNone_OptimizationFlags, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 254 | kCompatibleWithCoverageAsAlpha_OptimizationFlag = 0x1, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 255 | kPreservesOpaqueInput_OptimizationFlag = 0x2, |
| 256 | kConstantOutputForConstantInput_OptimizationFlag = 0x4, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 257 | kAll_OptimizationFlags = kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 258 | kPreservesOpaqueInput_OptimizationFlag | |
| 259 | kConstantOutputForConstantInput_OptimizationFlag |
| 260 | }; |
| 261 | GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags) |
| 262 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 263 | /** |
| 264 | * Can be used as a helper to decide which fragment processor OptimizationFlags should be set. |
| 265 | * This assumes that the subclass output color will be a modulation of the input color with a |
| 266 | * value read from a texture of the passed config and that the texture contains premultiplied |
| 267 | * color or alpha values that are in range. |
| 268 | */ |
| 269 | static OptimizationFlags ModulateByConfigOptimizationFlags(GrPixelConfig config) { |
| 270 | if (GrPixelConfigIsOpaque(config)) { |
| 271 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
| 272 | kPreservesOpaqueInput_OptimizationFlag; |
| 273 | } else { |
| 274 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 275 | } |
| 276 | } |
| 277 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 278 | GrFragmentProcessor(OptimizationFlags optimizationFlags) : fFlags(optimizationFlags) { |
| 279 | SkASSERT((fFlags & ~kAll_OptimizationFlags) == 0); |
| 280 | } |
| 281 | |
| 282 | OptimizationFlags optimizationFlags() const { |
| 283 | return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * This allows one subclass to access another subclass's implementation of |
| 288 | * constantOutputForConstantInput. It must only be called when |
| 289 | * hasConstantOutputForConstantInput() is known to be true. |
| 290 | */ |
| 291 | static GrColor4f ConstantOutputForConstantInput(const GrFragmentProcessor& fp, |
| 292 | GrColor4f input) { |
| 293 | SkASSERT(fp.hasConstantOutputForConstantInput()); |
| 294 | return fp.constantOutputForConstantInput(input); |
| 295 | } |
| 296 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 299 | * transformations. Coord transforms provide a mechanism for a processor to receive coordinates |
| 300 | * in their FS code. The matrix expresses a transformation from local space. For a given |
| 301 | * fragment the matrix will be applied to the local coordinate that maps to the fragment. |
| 302 | * |
| 303 | * When the transformation has perspective, the transformed coordinates will have |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 304 | * 3 components. Otherwise they'll have 2. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 305 | * |
| 306 | * This must only be called from the constructor because GrProcessors are immutable. The |
| 307 | * processor subclass manages the lifetime of the transformations (this function only stores a |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 308 | * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 309 | * |
| 310 | * A processor subclass that has multiple methods of construction should always add its coord |
| 311 | * transforms in a consistent order. The non-virtual implementation of isEqual() automatically |
| 312 | * compares transforms and will assume they line up across the two processor instances. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 313 | */ |
| 314 | void addCoordTransform(const GrCoordTransform*); |
| 315 | |
| 316 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 317 | * FragmentProcessor subclasses call this from their constructor to register any child |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 318 | * FragmentProcessors they have. This must be called AFTER all texture accesses and coord |
| 319 | * transforms have been added. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 320 | * This is for processors whose shader code will be composed of nested processors whose output |
| 321 | * colors will be combined somehow to produce its output color. Registering these child |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 322 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 323 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 324 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 325 | int registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 326 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 327 | private: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 328 | virtual GrColor4f constantOutputForConstantInput(GrColor4f /* inputColor */) const { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 329 | SK_ABORT("Subclass must override this if advertising this optimization."); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 330 | return GrColor4f::TransparentBlack(); |
| 331 | } |
| 332 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 333 | /** Returns a new instance of the appropriate *GL* implementation class |
| 334 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 335 | the object. */ |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 336 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 337 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 338 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 339 | virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 340 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 341 | /** |
| 342 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 343 | * the two processors are of the same subclass (i.e. they return the same object from |
| 344 | * getFactory()). The processor subclass should not compare its coord transforms as that will |
| 345 | * be performed automatically in the non-virtual isEqual(). |
| 346 | */ |
| 347 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 348 | |
| 349 | bool hasSameTransforms(const GrFragmentProcessor&) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 350 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 351 | enum PrivateFlags { |
| 352 | kFirstPrivateFlag = kAll_OptimizationFlags + 1, |
| 353 | kUsesLocalCoords_Flag = kFirstPrivateFlag, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | mutable uint32_t fFlags = 0; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 357 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 358 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 359 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 360 | SkSTArray<1, std::unique_ptr<GrFragmentProcessor>, true> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 361 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 362 | typedef GrResourceIOProcessor INHERITED; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 365 | GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags) |
| 366 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 367 | #endif |