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 | |
John Stiles | f08a82b | 2020-06-16 16:34:12 -0400 | [diff] [blame] | 11 | #include <tuple> |
| 12 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 13 | #include "include/private/SkSLSampleUsage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrProcessor.h" |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 15 | #include "src/gpu/ops/GrOp.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 16 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 17 | class GrGLSLFragmentProcessor; |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 18 | class GrPaint; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 19 | class GrPipeline; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 20 | class GrProcessorKeyBuilder; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 21 | class GrShaderCaps; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 22 | class GrSwizzle; |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 23 | class GrTextureEffect; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 24 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 25 | /** Provides custom fragment shader code. Fragment processors receive an input color (half4) and |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 26 | produce an output color. They may reference textures and uniforms. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 27 | */ |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 28 | class GrFragmentProcessor : public GrProcessor { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 29 | public: |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 30 | /** |
| 31 | * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to |
| 32 | * only consider the input color's alpha. However, there is a competing desire to have reusable |
| 33 | * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input |
| 34 | * color is considered. This function exists to filter the input color and pass it to a FP. It |
| 35 | * does so by returning a parent FP that multiplies the passed in FPs output by the parent's |
| 36 | * input alpha. The passed in FP will not receive an input color. |
| 37 | */ |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 38 | static std::unique_ptr<GrFragmentProcessor> MulChildByInputAlpha( |
| 39 | std::unique_ptr<GrFragmentProcessor> child); |
| 40 | |
| 41 | /** |
| 42 | * Like MulChildByInputAlpha(), but reverses the sense of src and dst. In this case, return |
| 43 | * the input modulated by the child's alpha. The passed in FP will not receive an input color. |
| 44 | * |
| 45 | * output = input * child.a |
| 46 | */ |
| 47 | static std::unique_ptr<GrFragmentProcessor> MulInputByChildAlpha( |
| 48 | std::unique_ptr<GrFragmentProcessor> child); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
John Stiles | 7bf2600 | 2020-07-13 11:30:12 -0400 | [diff] [blame] | 51 | * Returns a fragment processor that generates the passed-in color, modulated by the child's |
| 52 | * alpha channel. (Pass a null FP to use the alpha from sk_InColor instead of a child FP.) |
| 53 | */ |
| 54 | static std::unique_ptr<GrFragmentProcessor> ModulateAlpha( |
| 55 | std::unique_ptr<GrFragmentProcessor> child, const SkPMColor4f& color); |
| 56 | |
| 57 | /** |
John Stiles | 8589430 | 2020-07-13 11:39:52 -0400 | [diff] [blame] | 58 | * Returns a fragment processor that generates the passed-in color, modulated by the child's |
| 59 | * RGBA color. (Pass a null FP to use the color from sk_InColor instead of a child FP.) |
| 60 | */ |
| 61 | static std::unique_ptr<GrFragmentProcessor> ModulateRGBA( |
| 62 | std::unique_ptr<GrFragmentProcessor> child, const SkPMColor4f& color); |
| 63 | |
| 64 | /** |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 65 | * This assumes that the input color to the returned processor will be unpremul and that the |
| 66 | * passed processor (which becomes the returned processor's child) produces a premul output. |
| 67 | * The result of the returned processor is a premul of its input color modulated by the child |
| 68 | * processor's premul output. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 69 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 70 | static std::unique_ptr<GrFragmentProcessor> MakeInputPremulAndMulByOutput( |
| 71 | std::unique_ptr<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 74 | * Returns a parent fragment processor that adopts the passed fragment processor as a child. |
| 75 | * The parent will ignore its input color and instead feed the passed in color as input to the |
| 76 | * child. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 77 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 78 | static std::unique_ptr<GrFragmentProcessor> OverrideInput(std::unique_ptr<GrFragmentProcessor>, |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 79 | const SkPMColor4f&, |
| 80 | bool useUniform = true); |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 81 | |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 82 | /** |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 83 | * Returns a fragment processor that premuls the input before calling the passed in fragment |
| 84 | * processor. |
| 85 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 86 | static std::unique_ptr<GrFragmentProcessor> PremulInput(std::unique_ptr<GrFragmentProcessor>); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 89 | * Returns a fragment processor that calls the passed in fragment processor, and then swizzles |
| 90 | * the output. |
| 91 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 92 | static std::unique_ptr<GrFragmentProcessor> SwizzleOutput(std::unique_ptr<GrFragmentProcessor>, |
| 93 | const GrSwizzle&); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 94 | |
| 95 | /** |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 96 | * Returns a fragment processor that calls the passed in fragment processor, and then ensures |
| 97 | * the output is a valid premul color by clamping RGB to [0, A]. |
| 98 | */ |
| 99 | static std::unique_ptr<GrFragmentProcessor> ClampPremulOutput( |
| 100 | std::unique_ptr<GrFragmentProcessor>); |
| 101 | |
| 102 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 103 | * Returns a fragment processor that runs the passed in array of fragment processors in a |
| 104 | * series. The original input is passed to the first, the first's output is passed to the |
| 105 | * second, etc. The output of the returned processor is the output of the last processor of the |
| 106 | * series. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 107 | * |
| 108 | * The array elements with be moved. |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 109 | */ |
Brian Salomon | 64f4206 | 2020-02-14 10:42:45 -0500 | [diff] [blame] | 110 | static std::unique_ptr<GrFragmentProcessor> RunInSeries(std::unique_ptr<GrFragmentProcessor>[], |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 111 | int cnt); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 112 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 113 | /** |
| 114 | * 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] | 115 | * If the processor has child processors they are cloned as well. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 116 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 117 | virtual std::unique_ptr<GrFragmentProcessor> clone() const = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 118 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 119 | // The FP this was registered with as a child function. This will be null if this is a root. |
| 120 | const GrFragmentProcessor* parent() const { return fParent; } |
| 121 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 122 | GrGLSLFragmentProcessor* createGLSLInstance() const; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 123 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 124 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 125 | this->onGetGLSLProcessorKey(caps, b); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 126 | for (const auto& child : fChildProcessors) { |
| 127 | if (child) { |
| 128 | child->getGLSLProcessorKey(caps, b); |
| 129 | } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 133 | int numVaryingCoordsUsed() const { return this->usesVaryingCoordsDirectly() ? 1 : 0; } |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 134 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 135 | int numChildProcessors() const { return fChildProcessors.count(); } |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 136 | int numNonNullChildProcessors() const; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 137 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 138 | GrFragmentProcessor* childProcessor(int index) { return fChildProcessors[index].get(); } |
| 139 | const GrFragmentProcessor* childProcessor(int index) const { |
| 140 | return fChildProcessors[index].get(); |
| 141 | } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 142 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 143 | SkDEBUGCODE(bool isInstantiated() const;) |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 144 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 145 | /** |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 146 | * Does this FP require local coordinates to be produced by the primitive processor? This only |
| 147 | * returns true if this FP will directly read those local coordinates. FPs that are sampled |
| 148 | * explicitly do not require primitive-generated local coordinates (because the sample |
| 149 | * coordinates are supplied by the parent FP). |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 150 | * |
| 151 | * If the root of an FP tree does not provide explicit coordinates, the geometry processor |
| 152 | * provides the original local coordinates to start. This may be implicit as part of vertex |
| 153 | * shader-lifted varyings, or by providing the base local coordinate to the fragment shader. |
| 154 | */ |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 155 | bool usesVaryingCoordsDirectly() const { |
| 156 | return SkToBool(fFlags & kUsesSampleCoordsDirectly_Flag) && |
| 157 | !SkToBool(fFlags & kSampledWithExplicitCoords_Flag); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Do any of the FPs in this tree require local coordinates to be produced by the primitive |
| 162 | * processor? This can return true even if this FP does not refer to sample coordinates, but |
| 163 | * true if a descendant FP uses them. |
| 164 | */ |
| 165 | bool usesVaryingCoords() const { |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 166 | return (SkToBool(fFlags & kUsesSampleCoordsDirectly_Flag) || |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 167 | SkToBool(fFlags & kUsesSampleCoordsIndirectly_Flag)) && |
| 168 | !SkToBool(fFlags & kSampledWithExplicitCoords_Flag); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 171 | /** |
| 172 | * True if this FP refers directly to the sample coordinate parameter of its function |
| 173 | * (e.g. uses EmitArgs::fSampleCoord in emitCode()). This also returns true if the |
| 174 | * coordinate reference comes from autogenerated code invoking 'sample(matrix)' expressions. |
| 175 | * |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 176 | * Unlike usesVaryingCoords(), this can return true whether or not the FP is explicitly |
| 177 | * sampled, and does not change based on how the FP is composed. This property is specific to |
| 178 | * the FP's function and not the entire program. |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 179 | */ |
| 180 | bool referencesSampleCoords() const { |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 181 | return SkToBool(fFlags & kUsesSampleCoordsDirectly_Flag); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // True if this FP's parent invokes it with 'sample(float2)' or a variable 'sample(matrix)' |
Brian Salomon | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 185 | bool isSampledWithExplicitCoords() const { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 186 | return SkToBool(fFlags & kSampledWithExplicitCoords_Flag); |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 189 | // True if the transform chain from root to this FP introduces perspective into the local |
| 190 | // coordinate expression. |
| 191 | bool hasPerspectiveTransform() const { |
| 192 | return SkToBool(fFlags & kNetTransformHasPerspective_Flag); |
| 193 | } |
| 194 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 195 | // The SampleUsage describing how this FP is invoked by its parent using 'sample(matrix)' |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 196 | // This only reflects the immediate sampling from parent to this FP |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 197 | const SkSL::SampleUsage& sampleUsage() const { |
| 198 | return fUsage; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 201 | /** |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 202 | * A GrDrawOp may premultiply its antialiasing coverage into its GrGeometryProcessor's color |
| 203 | * output under the following scenario: |
| 204 | * * all the color fragment processors report true to this query, |
| 205 | * * all the coverage fragment processors report true to this query, |
| 206 | * * the blend mode arithmetic allows for it it. |
| 207 | * To be compatible a fragment processor's output must be a modulation of its input color or |
| 208 | * alpha with a computed premultiplied color or alpha that is in 0..1 range. The computed color |
| 209 | * or alpha that is modulated against the input cannot depend on the input's alpha. The computed |
| 210 | * value cannot depend on the input's color channels unless it unpremultiplies the input color |
| 211 | * channels by the input alpha. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 212 | */ |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 213 | bool compatibleWithCoverageAsAlpha() const { |
| 214 | return SkToBool(fFlags & kCompatibleWithCoverageAsAlpha_OptimizationFlag); |
| 215 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * If this is true then all opaque input colors to the processor produce opaque output colors. |
| 219 | */ |
| 220 | bool preservesOpaqueInput() const { |
| 221 | return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tests whether given a constant input color the processor produces a constant output color |
| 226 | * (for all fragments). If true outputColor will contain the constant color produces for |
| 227 | * inputColor. |
| 228 | */ |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 229 | bool hasConstantOutputForConstantInput(SkPMColor4f inputColor, SkPMColor4f* outputColor) const { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 230 | if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 231 | *outputColor = this->constantOutputForConstantInput(inputColor); |
| 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | bool hasConstantOutputForConstantInput() const { |
| 237 | return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag); |
| 238 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 239 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 240 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 241 | 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] | 242 | from getFactory()). |
| 243 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 244 | 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] | 245 | generate the same shader code. To test for identical code generation use getGLSLProcessorKey |
| 246 | */ |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 247 | bool isEqual(const GrFragmentProcessor& that) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 248 | |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 249 | void visitProxies(const GrOp::VisitProxyFunc& func) const; |
| 250 | |
| 251 | void visitTextureEffects(const std::function<void(const GrTextureEffect&)>&) const; |
| 252 | |
| 253 | GrTextureEffect* asTextureEffect(); |
| 254 | const GrTextureEffect* asTextureEffect() const; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 255 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 256 | // A pre-order traversal iterator over a hierarchy of FPs. It can also iterate over all the FP |
| 257 | // hierarchies rooted in a GrPaint, GrProcessorSet, or GrPipeline. For these collections it |
| 258 | // iterates the tree rooted at each color FP and then each coverage FP. |
| 259 | // |
| 260 | // An iterator is constructed from one of the srcs and used like this: |
| 261 | // for (GrFragmentProcessor::Iter iter(pipeline); iter; ++iter) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 262 | // GrFragmentProcessor& fp = *iter; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 263 | // } |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 264 | // The exit test for the loop is using CIter's operator bool(). |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 265 | // To use a range-for loop instead see CIterRange below. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 266 | class CIter; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 267 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 268 | // Used to implement a range-for loop using CIter. Src is one of GrFragmentProcessor, |
| 269 | // GrPaint, GrProcessorSet, or GrPipeline. Type aliases for these defined below. |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 270 | // Example usage: |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 271 | // for (const auto& fp : GrFragmentProcessor::PaintRange(paint)) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 272 | // if (fp.usesLocalCoords()) { |
| 273 | // ... |
| 274 | // } |
| 275 | // } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 276 | template <typename Src> class CIterRange; |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 277 | |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 278 | // We would use template deduction guides for CIter but for: |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 279 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79501 |
| 280 | // Instead we use these specialized type aliases to make it prettier |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 281 | // to construct CIters for particular sources of FPs. |
| 282 | using FPRange = CIterRange<GrFragmentProcessor>; |
| 283 | using PaintRange = CIterRange<GrPaint>; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 284 | |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 285 | // Sentinel type for range-for using CIter. |
| 286 | class EndCIter {}; |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 287 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 288 | protected: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 289 | enum OptimizationFlags : uint32_t { |
| 290 | kNone_OptimizationFlags, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 291 | kCompatibleWithCoverageAsAlpha_OptimizationFlag = 0x1, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 292 | kPreservesOpaqueInput_OptimizationFlag = 0x2, |
| 293 | kConstantOutputForConstantInput_OptimizationFlag = 0x4, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 294 | kAll_OptimizationFlags = kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 295 | kPreservesOpaqueInput_OptimizationFlag | |
| 296 | kConstantOutputForConstantInput_OptimizationFlag |
| 297 | }; |
| 298 | GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags) |
| 299 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 300 | /** |
| 301 | * Can be used as a helper to decide which fragment processor OptimizationFlags should be set. |
| 302 | * This assumes that the subclass output color will be a modulation of the input color with a |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 303 | * value read from a texture of the passed color type and that the texture contains |
| 304 | * premultiplied color or alpha values that are in range. |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 305 | * |
| 306 | * Since there are multiple ways in which a sampler may have its coordinates clamped or wrapped, |
| 307 | * callers must determine on their own if the sampling uses a decal strategy in any way, in |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 308 | * which case the texture may become transparent regardless of the color type. |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 309 | */ |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 310 | static OptimizationFlags ModulateForSamplerOptFlags(SkAlphaType alphaType, bool samplingDecal) { |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 311 | if (samplingDecal) { |
| 312 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 313 | } else { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 314 | return ModulateForClampedSamplerOptFlags(alphaType); |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | // As above, but callers should somehow ensure or assert their sampler still uses clamping |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 319 | static OptimizationFlags ModulateForClampedSamplerOptFlags(SkAlphaType alphaType) { |
| 320 | if (alphaType == kOpaque_SkAlphaType) { |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 321 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
| 322 | kPreservesOpaqueInput_OptimizationFlag; |
| 323 | } else { |
| 324 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 325 | } |
| 326 | } |
| 327 | |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 328 | GrFragmentProcessor(ClassID classID, OptimizationFlags optimizationFlags) |
Brian Salomon | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 329 | : INHERITED(classID), fFlags(optimizationFlags) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 330 | SkASSERT((optimizationFlags & ~kAll_OptimizationFlags) == 0); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | OptimizationFlags optimizationFlags() const { |
| 334 | return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags); |
| 335 | } |
| 336 | |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 337 | /** Useful when you can't call fp->optimizationFlags() on a base class object from a subclass.*/ |
| 338 | static OptimizationFlags ProcessorOptimizationFlags(const GrFragmentProcessor* fp) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 339 | return fp ? fp->optimizationFlags() : kAll_OptimizationFlags; |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 342 | /** |
| 343 | * This allows one subclass to access another subclass's implementation of |
| 344 | * constantOutputForConstantInput. It must only be called when |
| 345 | * hasConstantOutputForConstantInput() is known to be true. |
| 346 | */ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 347 | static SkPMColor4f ConstantOutputForConstantInput(const GrFragmentProcessor* fp, |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 348 | const SkPMColor4f& input) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 349 | if (fp) { |
| 350 | SkASSERT(fp->hasConstantOutputForConstantInput()); |
| 351 | return fp->constantOutputForConstantInput(input); |
| 352 | } else { |
| 353 | return input; |
| 354 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 355 | } |
| 356 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 357 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 358 | * FragmentProcessor subclasses call this from their constructor to register any child |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 359 | * FragmentProcessors they have. This must be called AFTER all texture accesses and coord |
| 360 | * transforms have been added. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 361 | * This is for processors whose shader code will be composed of nested processors whose output |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 362 | * colors will be combined somehow to produce its output color. Registering these child |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 363 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 364 | * texture accesses and mangle their uniform and output color names. |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 365 | * |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 366 | * The SampleUsage parameter describes all of the ways that the child is sampled by the parent. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 367 | */ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 368 | void registerChild(std::unique_ptr<GrFragmentProcessor> child, |
| 369 | SkSL::SampleUsage sampleUsage = SkSL::SampleUsage::PassThrough()); |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 370 | |
John Stiles | 9ec6b05 | 2020-06-15 12:06:10 -0400 | [diff] [blame] | 371 | /** |
| 372 | * This method takes an existing fragment processor, clones all of its children, and registers |
| 373 | * the clones as children of this fragment processor. |
| 374 | */ |
| 375 | void cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src); |
| 376 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 377 | // FP implementations must call this function if their matching GrGLSLFragmentProcessor's |
| 378 | // emitCode() function uses the EmitArgs::fSampleCoord variable in generated SkSL. |
| 379 | void setUsesSampleCoordsDirectly() { |
| 380 | fFlags |= kUsesSampleCoordsDirectly_Flag; |
| 381 | } |
| 382 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 383 | private: |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 384 | virtual SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& /* inputColor */) const { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 385 | SK_ABORT("Subclass must override this if advertising this optimization."); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 386 | } |
| 387 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 388 | /** Returns a new instance of the appropriate *GL* implementation class |
| 389 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 390 | the object. */ |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 391 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 392 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 393 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 394 | virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 395 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 396 | /** |
| 397 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 398 | * the two processors are of the same subclass (i.e. they return the same object from |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 399 | * getFactory()). |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 400 | */ |
| 401 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 402 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 403 | enum PrivateFlags { |
| 404 | kFirstPrivateFlag = kAll_OptimizationFlags + 1, |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 405 | |
| 406 | // Propagate up the FP tree to the root |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 407 | kUsesSampleCoordsIndirectly_Flag = kFirstPrivateFlag, |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 408 | |
| 409 | // Does not propagate at all |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 410 | kUsesSampleCoordsDirectly_Flag = kFirstPrivateFlag << 1, |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 411 | |
| 412 | // Propagates down the FP to all its leaves |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 413 | kSampledWithExplicitCoords_Flag = kFirstPrivateFlag << 2, |
| 414 | kNetTransformHasPerspective_Flag = kFirstPrivateFlag << 3, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 415 | }; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 416 | void addAndPushFlagToChildren(PrivateFlags flag); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 417 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 418 | SkSTArray<1, std::unique_ptr<GrFragmentProcessor>, true> fChildProcessors; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 419 | const GrFragmentProcessor* fParent = nullptr; |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 420 | uint32_t fFlags = 0; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 421 | SkSL::SampleUsage fUsage; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 422 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 423 | typedef GrProcessor INHERITED; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 426 | ////////////////////////////////////////////////////////////////////////////// |
| 427 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 428 | GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags) |
| 429 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 430 | ////////////////////////////////////////////////////////////////////////////// |
| 431 | |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 432 | class GrFragmentProcessor::CIter { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 433 | public: |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 434 | explicit CIter(const GrFragmentProcessor& fp) { fFPStack.push_back(&fp); } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 435 | explicit CIter(const GrPaint&); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 436 | explicit CIter(const GrPipeline&); |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 437 | |
| 438 | const GrFragmentProcessor& operator*() const { return *fFPStack.back(); } |
| 439 | const GrFragmentProcessor* operator->() const { return fFPStack.back(); } |
| 440 | |
| 441 | CIter& operator++(); |
| 442 | |
| 443 | operator bool() const { return !fFPStack.empty(); } |
| 444 | |
| 445 | bool operator!=(const EndCIter&) { return (bool)*this; } |
| 446 | |
| 447 | // Hopefully this does not actually get called because of RVO. |
| 448 | CIter(const CIter&) = default; |
| 449 | |
| 450 | // Because each iterator carries a stack we want to avoid copies. |
| 451 | CIter& operator=(const CIter&) = delete; |
| 452 | |
| 453 | protected: |
| 454 | CIter() = delete; |
| 455 | |
| 456 | SkSTArray<4, const GrFragmentProcessor*, true> fFPStack; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | ////////////////////////////////////////////////////////////////////////////// |
| 460 | |
| 461 | template <typename Src> class GrFragmentProcessor::CIterRange { |
| 462 | public: |
| 463 | explicit CIterRange(const Src& t) : fT(t) {} |
| 464 | CIter begin() const { return CIter(fT); } |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 465 | EndCIter end() const { return EndCIter(); } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 466 | |
| 467 | private: |
| 468 | const Src& fT; |
| 469 | }; |
| 470 | |
John Stiles | 72e5764 | 2020-06-24 10:42:35 -0400 | [diff] [blame] | 471 | /** |
| 472 | * Some fragment-processor creation methods have preconditions that might not be satisfied by the |
| 473 | * calling code. Those methods can return a `GrFPResult` from their factory methods. If creation |
| 474 | * succeeds, the new fragment processor is created and `success` is true. If a precondition is not |
| 475 | * met, `success` is set to false and the input FP is returned unchanged. |
| 476 | */ |
| 477 | using GrFPResult = std::tuple<bool /*success*/, std::unique_ptr<GrFragmentProcessor>>; |
| 478 | static inline GrFPResult GrFPFailure(std::unique_ptr<GrFragmentProcessor> fp) { |
| 479 | return {false, std::move(fp)}; |
| 480 | } |
| 481 | static inline GrFPResult GrFPSuccess(std::unique_ptr<GrFragmentProcessor> fp) { |
| 482 | return {true, std::move(fp)}; |
| 483 | } |
| 484 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 485 | #endif |