bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrFragmentProcessor.h" |
| 9 | #include "src/gpu/GrPipeline.h" |
| 10 | #include "src/gpu/GrProcessorAnalysis.h" |
| 11 | #include "src/gpu/effects/GrXfermodeFragmentProcessor.h" |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 12 | #include "src/gpu/effects/generated/GrClampFragmentProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
| 14 | #include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 18 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 19 | |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 20 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 21 | if (this->classID() != that.classID()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 22 | return false; |
| 23 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 24 | if (this->numTextureSamplers() != that.numTextureSamplers()) { |
| 25 | return false; |
| 26 | } |
| 27 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 28 | if (this->textureSampler(i) != that.textureSampler(i)) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 32 | if (this->usesVaryingCoordsDirectly() != that.usesVaryingCoordsDirectly()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 33 | return false; |
| 34 | } |
| 35 | if (!this->onIsEqual(that)) { |
| 36 | return false; |
| 37 | } |
| 38 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 39 | return false; |
| 40 | } |
| 41 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 42 | if (!this->childProcessor(i).isEqual(that.childProcessor(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 49 | void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 50 | for (auto [sampler, fp] : FPTextureSamplerRange(*this)) { |
| 51 | bool mipped = (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter()); |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 52 | func(sampler.view().proxy(), GrMipMapped(mipped)); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 56 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 57 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 58 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 59 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 60 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 61 | } |
| 62 | return glFragProc; |
| 63 | } |
| 64 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 65 | const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const { |
| 66 | SkASSERT(i >= 0 && i < fTextureSamplerCnt); |
| 67 | return this->onTextureSampler(i); |
| 68 | } |
| 69 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 70 | void GrFragmentProcessor::addAndPushFlagToChildren(PrivateFlags flag) { |
| 71 | // This propagates down, so if we've already marked it, all our children should have it too |
| 72 | if (!(fFlags & flag)) { |
| 73 | fFlags |= flag; |
| 74 | for (auto& child : fChildProcessors) { |
| 75 | child->addAndPushFlagToChildren(flag); |
| 76 | } |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 77 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 78 | #ifdef SK_DEBUG |
| 79 | for (auto& child : fChildProcessors) { |
| 80 | SkASSERT(child->fFlags & flag); |
| 81 | } |
| 82 | #endif |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 85 | #ifdef SK_DEBUG |
| 86 | bool GrFragmentProcessor::isInstantiated() const { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 87 | for (int i = 0; i < fTextureSamplerCnt; ++i) { |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 88 | if (!this->textureSampler(i).isInstantiated()) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 89 | return false; |
| 90 | } |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 93 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 94 | if (!this->childProcessor(i).isInstantiated()) { |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 101 | #endif |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 102 | |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 103 | int GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child, |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 104 | SkSL::SampleUsage sampleUsage) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 105 | // The child should not have been attached to another FP already and not had any sampling |
| 106 | // strategy set on it. |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 107 | SkASSERT(child && !child->fParent && !child->sampleUsage().isSampled() && |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 108 | !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform()); |
| 109 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 110 | // If a child is sampled directly (sample(child)), and with a single uniform matrix, we need to |
| 111 | // treat it as if it were sampled with multiple matrices (eg variable). |
| 112 | bool variableMatrix = sampleUsage.hasVariableMatrix() || |
| 113 | (sampleUsage.fPassThrough && sampleUsage.hasUniformMatrix()); |
| 114 | |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 115 | // Configure child's sampling state first |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 116 | child->fUsage = sampleUsage; |
| 117 | |
| 118 | // When an FP is sampled using variable matrix expressions, it is effectively being sampled |
| 119 | // explicitly, except that the call site will automatically evaluate the matrix expression to |
| 120 | // produce the float2 passed into this FP. |
| 121 | if (sampleUsage.fExplicitCoords || variableMatrix) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 122 | child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 123 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 124 | |
| 125 | // Push perspective matrix type to children |
| 126 | if (sampleUsage.fHasPerspective) { |
| 127 | child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 130 | // If the child is sampled with a variable matrix expression, auto-generated code in |
| 131 | // invokeChildWithMatrix() for this FP will refer to the local coordinates. |
| 132 | if (variableMatrix) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 133 | this->setUsesSampleCoordsDirectly(); |
| 134 | } |
| 135 | |
| 136 | // If the child is not sampled explicitly and not already accessing sample coords directly |
| 137 | // (through reference or variable matrix expansion), then mark that this FP tree relies on |
| 138 | // coordinates at a lower level. If the child is sampled with explicit coordinates and |
| 139 | // there isn't any other direct reference to the sample coords, we halt the upwards propagation |
| 140 | // because it means this FP is determining coordinates on its own. |
| 141 | if (!child->isSampledWithExplicitCoords()) { |
| 142 | if ((child->fFlags & kUsesSampleCoordsDirectly_Flag || |
| 143 | child->fFlags & kUsesSampleCoordsIndirectly_Flag)) { |
| 144 | fFlags |= kUsesSampleCoordsIndirectly_Flag; |
| 145 | } |
| 146 | } |
| 147 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 148 | fRequestedFeatures |= child->fRequestedFeatures; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 149 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 150 | int index = fChildProcessors.count(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 151 | // Record that the child is attached to us; this FP is the source of any uniform data needed |
| 152 | // to evaluate the child sample matrix. |
| 153 | child->fParent = this; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 154 | fChildProcessors.push_back(std::move(child)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 155 | |
| 156 | // Sanity check: our sample strategy comes from a parent we shouldn't have yet. |
| 157 | SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() && |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 158 | !fUsage.isSampled() && !fParent); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 159 | return index; |
| 160 | } |
| 161 | |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 162 | int GrFragmentProcessor::cloneAndRegisterChildProcessor(const GrFragmentProcessor& fp) { |
| 163 | std::unique_ptr<GrFragmentProcessor> clone = fp.clone(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 164 | return this->registerChild(std::move(clone), fp.sampleUsage()); |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 165 | } |
| 166 | |
John Stiles | 9ec6b05 | 2020-06-15 12:06:10 -0400 | [diff] [blame] | 167 | void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) { |
| 168 | for (int i = 0; i < src.numChildProcessors(); ++i) { |
| 169 | this->cloneAndRegisterChildProcessor(src.childProcessor(i)); |
| 170 | } |
| 171 | } |
| 172 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 173 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha( |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 174 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 175 | if (!fp) { |
| 176 | return nullptr; |
| 177 | } |
John Stiles | 5c57e88 | 2020-06-30 14:05:03 -0400 | [diff] [blame] | 178 | return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 181 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha( |
| 182 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 183 | if (!fp) { |
| 184 | return nullptr; |
| 185 | } |
John Stiles | 5c57e88 | 2020-06-30 14:05:03 -0400 | [diff] [blame] | 186 | return GrXfermodeFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn); |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 189 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput( |
| 190 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 191 | if (!fp) { |
| 192 | return nullptr; |
| 193 | } |
John Stiles | 5a2a7b3 | 2020-06-04 10:57:21 -0400 | [diff] [blame] | 194 | return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true); |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 197 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput( |
| 198 | std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 199 | class SwizzleFragmentProcessor : public GrFragmentProcessor { |
| 200 | public: |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 201 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 202 | const GrSwizzle& swizzle) { |
| 203 | return std::unique_ptr<GrFragmentProcessor>( |
| 204 | new SwizzleFragmentProcessor(std::move(fp), swizzle)); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | const char* name() const override { return "Swizzle"; } |
| 208 | const GrSwizzle& swizzle() const { return fSwizzle; } |
| 209 | |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 210 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 211 | return Make(this->childProcessor(0).clone(), fSwizzle); |
| 212 | } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 213 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 214 | private: |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 215 | SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) |
| 216 | : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get())) |
| 217 | , fSwizzle(swizzle) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 218 | this->registerChild(std::move(fp)); |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 219 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 220 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 221 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 222 | class GLFP : public GrGLSLFragmentProcessor { |
| 223 | public: |
| 224 | void emitCode(EmitArgs& args) override { |
John Stiles | 0af87fe | 2020-06-05 13:33:09 -0400 | [diff] [blame] | 225 | SkString childColor = this->invokeChild(0, args.fInputColor, args); |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 226 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 227 | const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>(); |
| 228 | const GrSwizzle& swizzle = sfp.swizzle(); |
| 229 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 230 | |
| 231 | fragBuilder->codeAppendf("%s = %s.%s;", |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 232 | args.fOutputColor, childColor.c_str(), swizzle.asString().c_str()); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 233 | } |
| 234 | }; |
| 235 | return new GLFP; |
| 236 | } |
| 237 | |
| 238 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 239 | b->add32(fSwizzle.asKey()); |
| 240 | } |
| 241 | |
| 242 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
| 243 | const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>(); |
| 244 | return fSwizzle == sfp.fSwizzle; |
| 245 | } |
| 246 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 247 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 248 | return fSwizzle.applyTo(input); |
| 249 | } |
| 250 | |
| 251 | GrSwizzle fSwizzle; |
| 252 | |
| 253 | typedef GrFragmentProcessor INHERITED; |
| 254 | }; |
| 255 | |
| 256 | if (!fp) { |
| 257 | return nullptr; |
| 258 | } |
| 259 | if (GrSwizzle::RGBA() == swizzle) { |
| 260 | return fp; |
| 261 | } |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 262 | return SwizzleFragmentProcessor::Make(std::move(fp), swizzle); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 265 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput( |
| 266 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 267 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 268 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 269 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 270 | std::unique_ptr<GrFragmentProcessor> processor) { |
| 271 | return std::unique_ptr<GrFragmentProcessor>( |
| 272 | new PremulFragmentProcessor(std::move(processor))); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | const char* name() const override { return "Premultiply"; } |
| 276 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 277 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 278 | return Make(this->childProcessor(0).clone()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 281 | private: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 282 | PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 283 | : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 284 | this->registerChild(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 285 | } |
| 286 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 287 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 288 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 289 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 290 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 291 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 292 | SkString temp = this->invokeChild(0, args); |
Brian Osman | cddfc5e | 2020-01-24 10:24:27 -0500 | [diff] [blame] | 293 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 294 | fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor, |
Brian Osman | cddfc5e | 2020-01-24 10:24:27 -0500 | [diff] [blame] | 295 | args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 296 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 297 | } |
| 298 | }; |
| 299 | return new GLFP; |
| 300 | } |
| 301 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 302 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 303 | |
| 304 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 305 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 306 | static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) { |
| 307 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 308 | if (inner->preservesOpaqueInput()) { |
| 309 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 310 | } |
| 311 | if (inner->hasConstantOutputForConstantInput()) { |
| 312 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 313 | } |
| 314 | return flags; |
| 315 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 316 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 317 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
| 318 | SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0), |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 319 | SK_PMColor4fWHITE); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 320 | SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul(); |
| 321 | return premulInput * childColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 325 | }; |
| 326 | if (!fp) { |
| 327 | return nullptr; |
| 328 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 329 | return PremulFragmentProcessor::Make(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | ////////////////////////////////////////////////////////////////////////////// |
| 333 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 334 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput( |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 335 | std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 336 | if (!fp) { |
| 337 | return nullptr; |
| 338 | } |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 339 | return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 340 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 341 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 342 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries( |
Brian Salomon | 64f4206 | 2020-02-14 10:42:45 -0500 | [diff] [blame] | 343 | std::unique_ptr<GrFragmentProcessor> series[], int cnt) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 344 | class SeriesFragmentProcessor : public GrFragmentProcessor { |
| 345 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 346 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 347 | std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
| 348 | return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | const char* name() const override { return "Series"; } |
| 352 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 353 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 354 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 355 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 356 | if (!children.push_back(this->childProcessor(i).clone())) { |
| 357 | return nullptr; |
| 358 | } |
| 359 | } |
| 360 | return Make(children.begin(), this->numChildProcessors()); |
| 361 | } |
| 362 | |
| 363 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 364 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 365 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 366 | public: |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 367 | void emitCode(EmitArgs& args) override { |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 368 | // First guy's input might be nil. |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 369 | SkString result = this->invokeChild(0, args.fInputColor, args); |
Brian Osman | cddfc5e | 2020-01-24 10:24:27 -0500 | [diff] [blame] | 370 | for (int i = 1; i < this->numChildProcessors(); ++i) { |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 371 | result = this->invokeChild(i, result.c_str(), args); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 372 | } |
Brian Osman | cddfc5e | 2020-01-24 10:24:27 -0500 | [diff] [blame] | 373 | // Copy last output to our output variable |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 374 | args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str()); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 375 | } |
| 376 | }; |
| 377 | return new GLFP; |
| 378 | } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 379 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 380 | SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 381 | : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 382 | SkASSERT(cnt > 1); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 383 | for (int i = 0; i < cnt; ++i) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 384 | this->registerChild(std::move(children[i])); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 388 | static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 389 | OptimizationFlags flags = kAll_OptimizationFlags; |
| 390 | for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) { |
| 391 | flags &= children[i]->optimizationFlags(); |
| 392 | } |
| 393 | return flags; |
| 394 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 395 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 396 | |
| 397 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 398 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 399 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override { |
| 400 | SkPMColor4f color = inColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 401 | int childCnt = this->numChildProcessors(); |
| 402 | for (int i = 0; i < childCnt; ++i) { |
| 403 | color = ConstantOutputForConstantInput(this->childProcessor(i), color); |
| 404 | } |
| 405 | return color; |
| 406 | } |
| 407 | |
| 408 | typedef GrFragmentProcessor INHERITED; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | if (!cnt) { |
| 412 | return nullptr; |
| 413 | } |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 414 | if (1 == cnt) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 415 | return std::move(series[0]); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 416 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 417 | // Run the through the series, do the invariant output processing, and look for eliminations. |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 418 | GrProcessorAnalysisColor inputColor; |
| 419 | inputColor.setToUnknown(); |
Brian Salomon | 64f4206 | 2020-02-14 10:42:45 -0500 | [diff] [blame] | 420 | GrColorFragmentProcessorAnalysis info(inputColor, series, cnt); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 421 | SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries; |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 422 | SkPMColor4f knownColor; |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 423 | int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor); |
| 424 | if (leadingFPsToEliminate) { |
John Stiles | e3a39f7 | 2020-06-15 13:58:48 -0400 | [diff] [blame] | 425 | std::unique_ptr<GrFragmentProcessor> colorFP = GrConstColorProcessor::Make( |
| 426 | /*inputFP=*/nullptr, knownColor, GrConstColorProcessor::InputMode::kIgnore); |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 427 | if (leadingFPsToEliminate == cnt) { |
| 428 | return colorFP; |
| 429 | } |
| 430 | cnt = cnt - leadingFPsToEliminate + 1; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 431 | replacementSeries.reserve(cnt); |
| 432 | replacementSeries.emplace_back(std::move(colorFP)); |
| 433 | for (int i = 0; i < cnt - 1; ++i) { |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 434 | replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 435 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 436 | series = replacementSeries.begin(); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 437 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 438 | return SeriesFragmentProcessor::Make(series, cnt); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 439 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 440 | |
| 441 | ////////////////////////////////////////////////////////////////////////////// |
| 442 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 443 | GrFragmentProcessor::CIter::CIter(const GrPaint& paint) { |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 444 | for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) { |
| 445 | fFPStack.push_back(paint.getCoverageFragmentProcessor(i)); |
| 446 | } |
| 447 | for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) { |
| 448 | fFPStack.push_back(paint.getColorFragmentProcessor(i)); |
| 449 | } |
| 450 | } |
| 451 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 452 | GrFragmentProcessor::CIter::CIter(const GrProcessorSet& set) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 453 | for (int i = set.numCoverageFragmentProcessors() - 1; i >= 0; --i) { |
| 454 | fFPStack.push_back(set.coverageFragmentProcessor(i)); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 455 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 456 | for (int i = set.numColorFragmentProcessors() - 1; i >= 0; --i) { |
| 457 | fFPStack.push_back(set.colorFragmentProcessor(i)); |
| 458 | } |
| 459 | } |
| 460 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 461 | GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 462 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 463 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 464 | } |
| 465 | } |
| 466 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 467 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 468 | |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 469 | GrFragmentProcessor::TextureSampler::TextureSampler(GrSurfaceProxyView view, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 470 | GrSamplerState samplerState) |
| 471 | : fView(std::move(view)), fSamplerState(samplerState) { |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 472 | GrSurfaceProxy* proxy = this->proxy(); |
Greg Daniel | acf5929 | 2019-12-11 13:45:17 -0500 | [diff] [blame] | 473 | fSamplerState.setFilterMode( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 474 | std::min(samplerState.filter(), |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 475 | GrTextureProxy::HighestFilterMode(proxy->backendFormat().textureType()))); |
Greg Daniel | acf5929 | 2019-12-11 13:45:17 -0500 | [diff] [blame] | 476 | } |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 477 | |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 478 | #if GR_TEST_UTILS |
| 479 | void GrFragmentProcessor::TextureSampler::set(GrSurfaceProxyView view, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 480 | GrSamplerState samplerState) { |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 481 | SkASSERT(view.proxy()->asTextureProxy()); |
| 482 | fView = std::move(view); |
| 483 | fSamplerState = samplerState; |
| 484 | |
| 485 | fSamplerState.setFilterMode( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 486 | std::min(samplerState.filter(), |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 487 | GrTextureProxy::HighestFilterMode(this->proxy()->backendFormat().textureType()))); |
| 488 | } |
| 489 | #endif |