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 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrCoordTransform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProcessor.h" |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 13 | #include "src/gpu/ops/GrOp.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 14 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 15 | class GrGLSLFragmentProcessor; |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 16 | class GrPaint; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 17 | class GrPipeline; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 18 | class GrProcessorKeyBuilder; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 19 | class GrShaderCaps; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 20 | class GrSwizzle; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 21 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 22 | /** Provides custom fragment shader code. Fragment processors receive an input color (half4) and |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 23 | produce an output color. They may reference textures and uniforms. They may use |
| 24 | GrCoordTransforms to receive a transformation of the local coordinates that map from local space |
| 25 | to the fragment being processed. |
| 26 | */ |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 27 | class GrFragmentProcessor : public GrProcessor { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 28 | public: |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 29 | class TextureSampler; |
| 30 | |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 31 | /** |
| 32 | * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to |
| 33 | * only consider the input color's alpha. However, there is a competing desire to have reusable |
| 34 | * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input |
| 35 | * color is considered. This function exists to filter the input color and pass it to a FP. It |
| 36 | * does so by returning a parent FP that multiplies the passed in FPs output by the parent's |
| 37 | * input alpha. The passed in FP will not receive an input color. |
| 38 | */ |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 39 | static std::unique_ptr<GrFragmentProcessor> MulChildByInputAlpha( |
| 40 | std::unique_ptr<GrFragmentProcessor> child); |
| 41 | |
| 42 | /** |
| 43 | * Like MulChildByInputAlpha(), but reverses the sense of src and dst. In this case, return |
| 44 | * the input modulated by the child's alpha. The passed in FP will not receive an input color. |
| 45 | * |
| 46 | * output = input * child.a |
| 47 | */ |
| 48 | static std::unique_ptr<GrFragmentProcessor> MulInputByChildAlpha( |
| 49 | std::unique_ptr<GrFragmentProcessor> child); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 52 | * This assumes that the input color to the returned processor will be unpremul and that the |
| 53 | * passed processor (which becomes the returned processor's child) produces a premul output. |
| 54 | * The result of the returned processor is a premul of its input color modulated by the child |
| 55 | * processor's premul output. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 56 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 57 | static std::unique_ptr<GrFragmentProcessor> MakeInputPremulAndMulByOutput( |
| 58 | std::unique_ptr<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 61 | * Returns a parent fragment processor that adopts the passed fragment processor as a child. |
| 62 | * The parent will ignore its input color and instead feed the passed in color as input to the |
| 63 | * child. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 64 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 65 | static std::unique_ptr<GrFragmentProcessor> OverrideInput(std::unique_ptr<GrFragmentProcessor>, |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 66 | const SkPMColor4f&, |
| 67 | bool useUniform = true); |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 68 | |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 69 | /** |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 70 | * Returns a fragment processor that premuls the input before calling the passed in fragment |
| 71 | * processor. |
| 72 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 73 | static std::unique_ptr<GrFragmentProcessor> PremulInput(std::unique_ptr<GrFragmentProcessor>); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [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 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 108 | int numTextureSamplers() const { return fTextureSamplerCnt; } |
| 109 | const TextureSampler& textureSampler(int i) const; |
| 110 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 111 | int numCoordTransforms() const { return fCoordTransforms.count(); } |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 112 | |
| 113 | /** Returns the coordinate transformation at index. index must be valid according to |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 114 | numCoordTransforms(). */ |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 115 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 116 | GrCoordTransform& coordTransform(int index) { return *fCoordTransforms[index]; } |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 117 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 118 | const SkTArray<GrCoordTransform*, true>& coordTransforms() const { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 119 | return fCoordTransforms; |
| 120 | } |
| 121 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 122 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 123 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 124 | GrFragmentProcessor& childProcessor(int index) { return *fChildProcessors[index]; } |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 125 | const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 126 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 127 | SkDEBUGCODE(bool isInstantiated() const;) |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 128 | |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 129 | /** Do any of the coord transforms for this processor require local coords? */ |
| 130 | bool usesLocalCoords() const { |
| 131 | // If the processor is sampled with explicit coords then we do not need to apply the |
| 132 | // coord transforms in the vertex shader to the local coords. |
| 133 | return SkToBool(fFlags & kHasCoordTranforms_Flag) && |
| 134 | SkToBool(fFlags & kCoordTransformsApplyToLocalCoords_Flag); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 137 | bool coordTransformsApplyToLocalCoords() const { |
| 138 | return SkToBool(fFlags & kCoordTransformsApplyToLocalCoords_Flag); |
| 139 | } |
| 140 | |
| 141 | void setSampledWithExplicitCoords(bool value) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 142 | if (value) { |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 143 | fFlags &= ~kCoordTransformsApplyToLocalCoords_Flag; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 144 | } else { |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 145 | fFlags |= kCoordTransformsApplyToLocalCoords_Flag; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 146 | } |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 147 | for (auto& child : fChildProcessors) { |
| 148 | child->setSampledWithExplicitCoords(value); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 152 | /** |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 153 | * A GrDrawOp may premultiply its antialiasing coverage into its GrGeometryProcessor's color |
| 154 | * output under the following scenario: |
| 155 | * * all the color fragment processors report true to this query, |
| 156 | * * all the coverage fragment processors report true to this query, |
| 157 | * * the blend mode arithmetic allows for it it. |
| 158 | * To be compatible a fragment processor's output must be a modulation of its input color or |
| 159 | * alpha with a computed premultiplied color or alpha that is in 0..1 range. The computed color |
| 160 | * or alpha that is modulated against the input cannot depend on the input's alpha. The computed |
| 161 | * value cannot depend on the input's color channels unless it unpremultiplies the input color |
| 162 | * channels by the input alpha. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 163 | */ |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 164 | bool compatibleWithCoverageAsAlpha() const { |
| 165 | return SkToBool(fFlags & kCompatibleWithCoverageAsAlpha_OptimizationFlag); |
| 166 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * If this is true then all opaque input colors to the processor produce opaque output colors. |
| 170 | */ |
| 171 | bool preservesOpaqueInput() const { |
| 172 | return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Tests whether given a constant input color the processor produces a constant output color |
| 177 | * (for all fragments). If true outputColor will contain the constant color produces for |
| 178 | * inputColor. |
| 179 | */ |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 180 | bool hasConstantOutputForConstantInput(SkPMColor4f inputColor, SkPMColor4f* outputColor) const { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 181 | if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 182 | *outputColor = this->constantOutputForConstantInput(inputColor); |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | bool hasConstantOutputForConstantInput() const { |
| 188 | return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag); |
| 189 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 190 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 191 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 192 | 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] | 193 | from getFactory()). |
| 194 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 195 | 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] | 196 | generate the same shader code. To test for identical code generation use getGLSLProcessorKey |
| 197 | */ |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 198 | bool isEqual(const GrFragmentProcessor& that) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 199 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 200 | void visitProxies(const GrOp::VisitProxyFunc& func); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 201 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 202 | // A pre-order traversal iterator over a hierarchy of FPs. It can also iterate over all the FP |
| 203 | // hierarchies rooted in a GrPaint, GrProcessorSet, or GrPipeline. For these collections it |
| 204 | // iterates the tree rooted at each color FP and then each coverage FP. |
| 205 | // |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 206 | // Iter is the non-const version and CIter is the const version. |
| 207 | // |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 208 | // An iterator is constructed from one of the srcs and used like this: |
| 209 | // for (GrFragmentProcessor::Iter iter(pipeline); iter; ++iter) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 210 | // GrFragmentProcessor& fp = *iter; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 211 | // } |
| 212 | // The exit test for the loop is using Iter's operator bool(). |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 213 | // To use a range-for loop instead see CIterRange below. |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 214 | class Iter; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 215 | class CIter; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 216 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 217 | // Used to implement a range-for loop using CIter. Src is one of GrFragmentProcessor, |
| 218 | // GrPaint, GrProcessorSet, or GrPipeline. Type aliases for these defined below. |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 219 | // Example usage: |
| 220 | // for (const auto& fp : GrFragmentProcessor::PaintRange(paint)) { |
| 221 | // if (fp.usesLocalCoords()) { |
| 222 | // ... |
| 223 | // } |
| 224 | // } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 225 | template <typename Src> class CIterRange; |
| 226 | // Like CIterRange but non const and only constructable from GrFragmentProcessor. This could |
| 227 | // support GrPaint as it owns non-const FPs but no need for it as of now. |
| 228 | // for (auto& fp0 : GrFragmentProcessor::IterRange(fp)) { |
| 229 | // ... |
| 230 | // } |
| 231 | class IterRange; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 232 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 233 | // We would use template deduction guides for Iter/CIter but for: |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 234 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79501 |
| 235 | // Instead we use these specialized type aliases to make it prettier |
| 236 | // to construct Iters for particular sources of FPs. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 237 | using FPCRange = CIterRange<GrFragmentProcessor>; |
| 238 | using PaintCRange = CIterRange<GrPaint>; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 239 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 240 | // Implementation details for iterators that walk an array of Items owned by a set of FPs. |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 241 | using CountFn = int (GrFragmentProcessor::*)() const; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 242 | // Defined GetFn to be a member function that returns an Item by index. The function itself is |
| 243 | // const if Item is a const type and non-const if Item is non-const. |
| 244 | template <typename Item, bool IsConst = std::is_const<Item>::value> struct GetT; |
| 245 | template <typename Item> struct GetT<Item, false> { |
| 246 | using GetFn = Item& (GrFragmentProcessor::*)(int); |
| 247 | }; |
| 248 | template <typename Item> struct GetT<Item, true> { |
| 249 | using GetFn = const Item& (GrFragmentProcessor::*)(int) const; |
| 250 | }; |
| 251 | template <typename Item> using GetFn = typename GetT<Item>::GetFn; |
| 252 | // This is an iterator over the Items owned by a (collection of) FP. CountFn is a FP member that |
| 253 | // gets the number of Items owned by each FP and GetFn is a member that gets them by index. |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 254 | template <typename Item, CountFn Count, GetFn<Item> Get> class FPItemIter; |
| 255 | |
| 256 | // Loops over all the GrCoordTransforms owned by GrFragmentProcessors. The possible sources for |
| 257 | // the iteration are the same as those for Iter and the FPs are walked in the same order as |
| 258 | // Iter. This provides access to the coord transform and the FP that owns it. Example usage: |
| 259 | // for (GrFragmentProcessor::CoordTransformIter iter(pipeline); iter; ++iter) { |
| 260 | // // transform is const GrCoordTransform& and owningFP is const GrFragmentProcessor&. |
| 261 | // auto [transform, owningFP] = *iter; |
| 262 | // ... |
| 263 | // } |
| 264 | // See the ranges below to make this simpler a la range-for loops. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 265 | using CoordTransformIter = FPItemIter<const GrCoordTransform, |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 266 | &GrFragmentProcessor::numCoordTransforms, |
| 267 | &GrFragmentProcessor::coordTransform>; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 268 | // Same as CoordTransformIter but for TextureSamplers: |
| 269 | // for (GrFragmentProcessor::TextureSamplerIter iter(pipeline); iter; ++iter) { |
| 270 | // // TextureSamplerIter is const GrFragmentProcessor::TextureSampler& and |
| 271 | // // owningFP is const GrFragmentProcessor&. |
| 272 | // auto [sampler, owningFP] = *iter; |
| 273 | // ... |
| 274 | // } |
| 275 | // See the ranges below to make this simpler a la range-for loops. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 276 | using TextureSamplerIter = FPItemIter<const TextureSampler, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 277 | &GrFragmentProcessor::numTextureSamplers, |
| 278 | &GrFragmentProcessor::textureSampler>; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 279 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 280 | // Implementation detail for using CoordTransformIter and TextureSamplerIter in range-for loops. |
| 281 | template <typename Src, typename ItemIter> class FPItemRange; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 282 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 283 | // These allow iteration over coord transforms/texture samplers for various FP sources via |
| 284 | // range-for loops. An example usage for looping over the coord transforms in a pipeline: |
| 285 | // for (auto [transform, fp] : GrFragmentProcessor::PipelineCoordTransformRange(pipeline)) { |
| 286 | // ... |
| 287 | // } |
| 288 | // Only the combinations of FP sources and iterable things have been defined but it is easy |
| 289 | // to add more as they become useful. Maybe someday we'll have template argument deduction |
| 290 | // with guides for type aliases and the sources can be removed from the type aliases: |
| 291 | // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1021r5.html |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 292 | using PipelineCoordTransformRange = FPItemRange<const GrPipeline, CoordTransformIter>; |
| 293 | using PipelineTextureSamplerRange = FPItemRange<const GrPipeline, TextureSamplerIter>; |
| 294 | using FPTextureSamplerRange = FPItemRange<const GrFragmentProcessor, TextureSamplerIter>; |
| 295 | using ProcessorSetTextureSamplerRange = FPItemRange<const GrProcessorSet, TextureSamplerIter>; |
| 296 | |
| 297 | // Not used directly. |
| 298 | using NonConstCoordTransformIter = |
| 299 | FPItemIter<GrCoordTransform, &GrFragmentProcessor::numCoordTransforms, |
| 300 | &GrFragmentProcessor::coordTransform>; |
| 301 | // Iterator over non-const GrCoordTransforms owned by FP and its descendants. |
| 302 | using FPCoordTransformRange = FPItemRange<GrFragmentProcessor, NonConstCoordTransformIter>; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 303 | |
| 304 | // Sentinel type for range-for using Iter. |
| 305 | class EndIter {}; |
| 306 | // Sentinel type for range-for using FPItemIter. |
| 307 | class FPItemEndIter {}; |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 308 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 309 | protected: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 310 | enum OptimizationFlags : uint32_t { |
| 311 | kNone_OptimizationFlags, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 312 | kCompatibleWithCoverageAsAlpha_OptimizationFlag = 0x1, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 313 | kPreservesOpaqueInput_OptimizationFlag = 0x2, |
| 314 | kConstantOutputForConstantInput_OptimizationFlag = 0x4, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 315 | kAll_OptimizationFlags = kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 316 | kPreservesOpaqueInput_OptimizationFlag | |
| 317 | kConstantOutputForConstantInput_OptimizationFlag |
| 318 | }; |
| 319 | GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags) |
| 320 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 321 | /** |
| 322 | * Can be used as a helper to decide which fragment processor OptimizationFlags should be set. |
| 323 | * 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] | 324 | * value read from a texture of the passed color type and that the texture contains |
| 325 | * premultiplied color or alpha values that are in range. |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 326 | * |
| 327 | * Since there are multiple ways in which a sampler may have its coordinates clamped or wrapped, |
| 328 | * 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] | 329 | * which case the texture may become transparent regardless of the color type. |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 330 | */ |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 331 | static OptimizationFlags ModulateForSamplerOptFlags(SkAlphaType alphaType, bool samplingDecal) { |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 332 | if (samplingDecal) { |
| 333 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 334 | } else { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 335 | return ModulateForClampedSamplerOptFlags(alphaType); |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | // 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] | 340 | static OptimizationFlags ModulateForClampedSamplerOptFlags(SkAlphaType alphaType) { |
| 341 | if (alphaType == kOpaque_SkAlphaType) { |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 342 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
| 343 | kPreservesOpaqueInput_OptimizationFlag; |
| 344 | } else { |
| 345 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 346 | } |
| 347 | } |
| 348 | |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 349 | GrFragmentProcessor(ClassID classID, OptimizationFlags optimizationFlags) |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 350 | : INHERITED(classID) |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 351 | , fFlags(optimizationFlags | kCoordTransformsApplyToLocalCoords_Flag) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 352 | SkASSERT((optimizationFlags & ~kAll_OptimizationFlags) == 0); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | OptimizationFlags optimizationFlags() const { |
| 356 | return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags); |
| 357 | } |
| 358 | |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 359 | /** Useful when you can't call fp->optimizationFlags() on a base class object from a subclass.*/ |
| 360 | static OptimizationFlags ProcessorOptimizationFlags(const GrFragmentProcessor* fp) { |
| 361 | return fp->optimizationFlags(); |
| 362 | } |
| 363 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 364 | /** |
| 365 | * This allows one subclass to access another subclass's implementation of |
| 366 | * constantOutputForConstantInput. It must only be called when |
| 367 | * hasConstantOutputForConstantInput() is known to be true. |
| 368 | */ |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 369 | static SkPMColor4f ConstantOutputForConstantInput(const GrFragmentProcessor& fp, |
| 370 | const SkPMColor4f& input) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 371 | SkASSERT(fp.hasConstantOutputForConstantInput()); |
| 372 | return fp.constantOutputForConstantInput(input); |
| 373 | } |
| 374 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 377 | * transformations. Coord transforms provide a mechanism for a processor to receive coordinates |
| 378 | * in their FS code. The matrix expresses a transformation from local space. For a given |
| 379 | * fragment the matrix will be applied to the local coordinate that maps to the fragment. |
| 380 | * |
| 381 | * When the transformation has perspective, the transformed coordinates will have |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 382 | * 3 components. Otherwise they'll have 2. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 383 | * |
| 384 | * This must only be called from the constructor because GrProcessors are immutable. The |
| 385 | * processor subclass manages the lifetime of the transformations (this function only stores a |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 386 | * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 387 | * |
| 388 | * A processor subclass that has multiple methods of construction should always add its coord |
| 389 | * transforms in a consistent order. The non-virtual implementation of isEqual() automatically |
| 390 | * compares transforms and will assume they line up across the two processor instances. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 391 | */ |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 392 | void addCoordTransform(GrCoordTransform*); |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 393 | |
| 394 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 395 | * FragmentProcessor subclasses call this from their constructor to register any child |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 396 | * FragmentProcessors they have. This must be called AFTER all texture accesses and coord |
| 397 | * transforms have been added. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 398 | * This is for processors whose shader code will be composed of nested processors whose output |
| 399 | * colors will be combined somehow to produce its output color. Registering these child |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 400 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 401 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 402 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 403 | int registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 404 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 405 | void setTextureSamplerCnt(int cnt) { |
| 406 | SkASSERT(cnt >= 0); |
| 407 | fTextureSamplerCnt = cnt; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Helper for implementing onTextureSampler(). E.g.: |
| 412 | * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler); |
| 413 | */ |
| 414 | template <typename... Args> |
| 415 | static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0, |
| 416 | const Args&... samps) { |
| 417 | return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...); |
| 418 | } |
| 419 | inline static const TextureSampler& IthTextureSampler(int i); |
| 420 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 421 | private: |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 422 | // Implementation details of Iter and CIter. |
| 423 | template <typename> class IterBase; |
| 424 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 425 | virtual SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& /* inputColor */) const { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 426 | SK_ABORT("Subclass must override this if advertising this optimization."); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 427 | } |
| 428 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 429 | /** Returns a new instance of the appropriate *GL* implementation class |
| 430 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 431 | the object. */ |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 432 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 433 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 434 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 435 | virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 436 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 437 | /** |
| 438 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 439 | * the two processors are of the same subclass (i.e. they return the same object from |
| 440 | * getFactory()). The processor subclass should not compare its coord transforms as that will |
| 441 | * be performed automatically in the non-virtual isEqual(). |
| 442 | */ |
| 443 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 444 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 445 | virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); } |
| 446 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 447 | bool hasSameTransforms(const GrFragmentProcessor&) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 448 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 449 | enum PrivateFlags { |
| 450 | kFirstPrivateFlag = kAll_OptimizationFlags + 1, |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 451 | kHasCoordTranforms_Flag = kFirstPrivateFlag, |
| 452 | kCoordTransformsApplyToLocalCoords_Flag = kFirstPrivateFlag << 1, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 453 | }; |
| 454 | |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 455 | uint32_t fFlags = kCoordTransformsApplyToLocalCoords_Flag; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 456 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 457 | int fTextureSamplerCnt = 0; |
| 458 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 459 | SkSTArray<4, GrCoordTransform*, true> fCoordTransforms; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 460 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 461 | SkSTArray<1, std::unique_ptr<GrFragmentProcessor>, true> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 462 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 463 | typedef GrProcessor INHERITED; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 464 | }; |
| 465 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 466 | /** |
| 467 | * Used to represent a texture that is required by a GrFragmentProcessor. It holds a GrTextureProxy |
| 468 | * along with an associated GrSamplerState. TextureSamplers don't perform any coord manipulation to |
| 469 | * account for texture origin. |
| 470 | */ |
| 471 | class GrFragmentProcessor::TextureSampler { |
| 472 | public: |
| 473 | TextureSampler() = default; |
| 474 | |
| 475 | /** |
Robert Phillips | 7d7aaf4 | 2019-10-14 11:34:16 -0400 | [diff] [blame] | 476 | * This copy constructor is used by GrFragmentProcessor::clone() implementations. |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 477 | */ |
| 478 | explicit TextureSampler(const TextureSampler& that) |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 479 | : fProxy(that.fProxy) |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 480 | , fSamplerState(that.fSamplerState) {} |
| 481 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 482 | TextureSampler(sk_sp<GrSurfaceProxy>, const GrSamplerState& = GrSamplerState::ClampNearest()); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 483 | |
| 484 | TextureSampler& operator=(const TextureSampler&) = delete; |
| 485 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 486 | void reset(sk_sp<GrSurfaceProxy>, const GrSamplerState&); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 487 | |
| 488 | bool operator==(const TextureSampler& that) const { |
| 489 | return this->proxy()->underlyingUniqueID() == that.proxy()->underlyingUniqueID() && |
| 490 | fSamplerState == that.fSamplerState; |
| 491 | } |
| 492 | |
| 493 | bool operator!=(const TextureSampler& other) const { return !(*this == other); } |
| 494 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 495 | SkDEBUGCODE(bool isInstantiated() const { return fProxy->isInstantiated(); }) |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 496 | |
| 497 | // 'peekTexture' should only ever be called after a successful 'instantiate' call |
| 498 | GrTexture* peekTexture() const { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 499 | SkASSERT(fProxy->isInstantiated()); |
| 500 | return fProxy->peekTexture(); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 503 | GrSurfaceProxy* proxy() const { return fProxy.get(); } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 504 | const GrSamplerState& samplerState() const { return fSamplerState; } |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 505 | const GrSwizzle& swizzle() const { return this->proxy()->textureSwizzle(); } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 506 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 507 | bool isInitialized() const { return SkToBool(fProxy.get()); } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 508 | |
| 509 | private: |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 510 | sk_sp<GrSurfaceProxy> fProxy; |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 511 | GrSamplerState fSamplerState; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | ////////////////////////////////////////////////////////////////////////////// |
| 515 | |
| 516 | const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::IthTextureSampler(int i) { |
| 517 | SK_ABORT("Illegal texture sampler index"); |
| 518 | static const TextureSampler kBogus; |
| 519 | return kBogus; |
| 520 | } |
| 521 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 522 | GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags) |
| 523 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 524 | ////////////////////////////////////////////////////////////////////////////// |
| 525 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 526 | template <typename FP> class GrFragmentProcessor::IterBase { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 527 | public: |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 528 | FP& operator*() const { return *fFPStack.back(); } |
| 529 | FP* operator->() const { return fFPStack.back(); } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 530 | operator bool() const { return !fFPStack.empty(); } |
| 531 | bool operator!=(const EndIter&) { return (bool)*this; } |
| 532 | |
| 533 | // Because each iterator carries a stack we want to avoid copies. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 534 | IterBase(const IterBase&) = delete; |
| 535 | IterBase& operator=(const IterBase&) = delete; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 536 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 537 | protected: |
| 538 | void increment(); |
| 539 | |
| 540 | IterBase() = default; |
| 541 | explicit IterBase(FP& fp) { fFPStack.push_back(&fp); } |
| 542 | |
| 543 | SkSTArray<4, FP*, true> fFPStack; |
| 544 | }; |
| 545 | |
| 546 | template <typename FP> void GrFragmentProcessor::IterBase<FP>::increment() { |
| 547 | SkASSERT(!fFPStack.empty()); |
| 548 | FP* back = fFPStack.back(); |
| 549 | fFPStack.pop_back(); |
| 550 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 551 | fFPStack.push_back(&back->childProcessor(i)); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | ////////////////////////////////////////////////////////////////////////////// |
| 556 | |
| 557 | class GrFragmentProcessor::Iter : public IterBase<GrFragmentProcessor> { |
| 558 | public: |
| 559 | explicit Iter(GrFragmentProcessor& fp) : IterBase(fp) {} |
| 560 | Iter& operator++() { |
| 561 | this->increment(); |
| 562 | return *this; |
| 563 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | ////////////////////////////////////////////////////////////////////////////// |
| 567 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 568 | class GrFragmentProcessor::CIter : public IterBase<const GrFragmentProcessor> { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 569 | public: |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 570 | explicit CIter(const GrFragmentProcessor& fp) : IterBase(fp) {} |
| 571 | explicit CIter(const GrPaint&); |
| 572 | explicit CIter(const GrProcessorSet&); |
| 573 | explicit CIter(const GrPipeline&); |
| 574 | CIter& operator++() { |
| 575 | this->increment(); |
| 576 | return *this; |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | ////////////////////////////////////////////////////////////////////////////// |
| 581 | |
| 582 | template <typename Src> class GrFragmentProcessor::CIterRange { |
| 583 | public: |
| 584 | explicit CIterRange(const Src& t) : fT(t) {} |
| 585 | CIter begin() const { return CIter(fT); } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 586 | EndIter end() const { return EndIter(); } |
| 587 | |
| 588 | private: |
| 589 | const Src& fT; |
| 590 | }; |
| 591 | |
| 592 | ////////////////////////////////////////////////////////////////////////////// |
| 593 | |
| 594 | template <typename Item, GrFragmentProcessor::CountFn Count, GrFragmentProcessor::GetFn<Item> Get> |
| 595 | class GrFragmentProcessor::FPItemIter { |
| 596 | public: |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 597 | template <typename Src> explicit FPItemIter(Src& s); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 598 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 599 | std::pair<Item&, const GrFragmentProcessor&> operator*() const { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 600 | return {(*fFPIter.*Get)(fIndex), *fFPIter}; |
| 601 | } |
| 602 | FPItemIter& operator++(); |
| 603 | operator bool() const { return fFPIter; } |
| 604 | bool operator!=(const FPItemEndIter&) { return (bool)*this; } |
| 605 | |
| 606 | FPItemIter(const FPItemIter&) = delete; |
| 607 | FPItemIter& operator=(const FPItemIter&) = delete; |
| 608 | |
| 609 | private: |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 610 | typename std::conditional<std::is_const<Item>::value, CIter, Iter>::type fFPIter; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 611 | int fIndex; |
| 612 | }; |
| 613 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 614 | template <typename Item, GrFragmentProcessor::CountFn Count, GrFragmentProcessor::GetFn<Item> Get> |
| 615 | template <typename Src> |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 616 | GrFragmentProcessor::FPItemIter<Item, Count, Get>::FPItemIter(Src& s) : fFPIter(s), fIndex(-1) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 617 | if (fFPIter) { |
| 618 | ++*this; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | template <typename Item, GrFragmentProcessor::CountFn Count, GrFragmentProcessor::GetFn<Item> Get> |
| 623 | GrFragmentProcessor::FPItemIter<Item, Count, Get>& |
| 624 | GrFragmentProcessor::FPItemIter<Item, Count, Get>::operator++() { |
| 625 | ++fIndex; |
| 626 | if (fIndex < ((*fFPIter).*Count)()) { |
| 627 | return *this; |
| 628 | } |
| 629 | fIndex = 0; |
| 630 | do {} while (++fFPIter && !((*fFPIter).*Count)()); |
| 631 | return *this; |
| 632 | } |
| 633 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 634 | ////////////////////////////////////////////////////////////////////////////// |
| 635 | |
| 636 | template <typename Src, typename ItemIter> class GrFragmentProcessor::FPItemRange { |
| 637 | public: |
| 638 | FPItemRange(Src& src) : fSrc(src) {} |
| 639 | ItemIter begin() const { return ItemIter(fSrc); } |
| 640 | FPItemEndIter end() const { return FPItemEndIter(); } |
| 641 | |
| 642 | private: |
| 643 | Src& fSrc; |
| 644 | }; |
| 645 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 646 | #endif |