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" |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 11 | #include "src/gpu/effects/GrBlendFragmentProcessor.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 Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 24 | if (this->usesVaryingCoordsDirectly() != that.usesVaryingCoordsDirectly()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 25 | return false; |
| 26 | } |
| 27 | if (!this->onIsEqual(that)) { |
| 28 | return false; |
| 29 | } |
| 30 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 31 | return false; |
| 32 | } |
| 33 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 34 | auto thisChild = this->childProcessor(i), |
| 35 | thatChild = that .childProcessor(i); |
| 36 | if (SkToBool(thisChild) != SkToBool(thatChild)) { |
| 37 | return false; |
| 38 | } |
| 39 | if (thisChild && !thisChild->isEqual(*thatChild)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 46 | void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) const { |
| 47 | this->visitTextureEffects([&func](const GrTextureEffect& te) { |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 48 | func(te.view().proxy(), te.samplerState().mipmapped()); |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 49 | }); |
| 50 | } |
| 51 | |
| 52 | void GrFragmentProcessor::visitTextureEffects( |
| 53 | const std::function<void(const GrTextureEffect&)>& func) const { |
| 54 | if (auto* te = this->asTextureEffect()) { |
| 55 | func(*te); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 56 | } |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 57 | for (auto& child : fChildProcessors) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 58 | if (child) { |
| 59 | child->visitTextureEffects(func); |
| 60 | } |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | GrTextureEffect* GrFragmentProcessor::asTextureEffect() { |
| 65 | if (this->classID() == kGrTextureEffect_ClassID) { |
| 66 | return static_cast<GrTextureEffect*>(this); |
| 67 | } |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const { |
| 72 | if (this->classID() == kGrTextureEffect_ClassID) { |
| 73 | return static_cast<const GrTextureEffect*>(this); |
| 74 | } |
| 75 | return nullptr; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 76 | } |
| 77 | |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 78 | #if GR_TEST_UTILS |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 79 | static void recursive_dump_tree_info(const GrFragmentProcessor& fp, |
| 80 | SkString indent, |
| 81 | SkString* text) { |
| 82 | for (int index = 0; index < fp.numChildProcessors(); ++index) { |
| 83 | text->appendf("\n%s(#%d) -> ", indent.c_str(), index); |
| 84 | if (const GrFragmentProcessor* childFP = fp.childProcessor(index)) { |
| 85 | text->append(childFP->dumpInfo()); |
| 86 | indent.append("\t"); |
| 87 | recursive_dump_tree_info(*childFP, indent, text); |
| 88 | } else { |
| 89 | text->append("null"); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | SkString GrFragmentProcessor::dumpTreeInfo() const { |
| 95 | SkString text = this->dumpInfo(); |
| 96 | recursive_dump_tree_info(*this, SkString("\t"), &text); |
| 97 | text.append("\n"); |
| 98 | return text; |
| 99 | } |
| 100 | #endif |
| 101 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 102 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 103 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 104 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 105 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 106 | glFragProc->fChildProcessors[i] = |
| 107 | fChildProcessors[i] ? fChildProcessors[i]->createGLSLInstance() : nullptr; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 108 | } |
| 109 | return glFragProc; |
| 110 | } |
| 111 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 112 | void GrFragmentProcessor::addAndPushFlagToChildren(PrivateFlags flag) { |
| 113 | // This propagates down, so if we've already marked it, all our children should have it too |
| 114 | if (!(fFlags & flag)) { |
| 115 | fFlags |= flag; |
| 116 | for (auto& child : fChildProcessors) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 117 | if (child) { |
| 118 | child->addAndPushFlagToChildren(flag); |
| 119 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 120 | } |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 121 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 122 | #ifdef SK_DEBUG |
| 123 | for (auto& child : fChildProcessors) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 124 | SkASSERT(!child || (child->fFlags & flag)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 125 | } |
| 126 | #endif |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 129 | int GrFragmentProcessor::numNonNullChildProcessors() const { |
| 130 | return std::count_if(fChildProcessors.begin(), fChildProcessors.end(), |
| 131 | [](const auto& c) { return c != nullptr; }); |
| 132 | } |
| 133 | |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 134 | #ifdef SK_DEBUG |
| 135 | bool GrFragmentProcessor::isInstantiated() const { |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 136 | bool result = true; |
| 137 | this->visitTextureEffects([&result](const GrTextureEffect& te) { |
| 138 | if (!te.texture()) { |
| 139 | result = false; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 140 | } |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 141 | }); |
| 142 | return result; |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 143 | } |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 144 | #endif |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 145 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 146 | void GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child, |
| 147 | SkSL::SampleUsage sampleUsage) { |
Brian Osman | 54867de | 2020-07-10 14:22:57 -0400 | [diff] [blame] | 148 | if (!child) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 149 | fChildProcessors.push_back(nullptr); |
| 150 | return; |
Brian Osman | 54867de | 2020-07-10 14:22:57 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 153 | // The child should not have been attached to another FP already and not had any sampling |
| 154 | // strategy set on it. |
Brian Osman | 54867de | 2020-07-10 14:22:57 -0400 | [diff] [blame] | 155 | SkASSERT(!child->fParent && !child->sampleUsage().isSampled() && |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 156 | !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform()); |
| 157 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 158 | // If a child is sampled directly (sample(child)), and with a single uniform matrix, we need to |
| 159 | // treat it as if it were sampled with multiple matrices (eg variable). |
| 160 | bool variableMatrix = sampleUsage.hasVariableMatrix() || |
| 161 | (sampleUsage.fPassThrough && sampleUsage.hasUniformMatrix()); |
| 162 | |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 163 | // Configure child's sampling state first |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 164 | child->fUsage = sampleUsage; |
| 165 | |
| 166 | // When an FP is sampled using variable matrix expressions, it is effectively being sampled |
| 167 | // explicitly, except that the call site will automatically evaluate the matrix expression to |
| 168 | // produce the float2 passed into this FP. |
| 169 | if (sampleUsage.fExplicitCoords || variableMatrix) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 170 | child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 171 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 172 | |
| 173 | // Push perspective matrix type to children |
| 174 | if (sampleUsage.fHasPerspective) { |
| 175 | child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 178 | // If the child is sampled with a variable matrix expression, auto-generated code in |
| 179 | // invokeChildWithMatrix() for this FP will refer to the local coordinates. |
| 180 | if (variableMatrix) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 181 | this->setUsesSampleCoordsDirectly(); |
| 182 | } |
| 183 | |
| 184 | // If the child is not sampled explicitly and not already accessing sample coords directly |
| 185 | // (through reference or variable matrix expansion), then mark that this FP tree relies on |
| 186 | // coordinates at a lower level. If the child is sampled with explicit coordinates and |
| 187 | // there isn't any other direct reference to the sample coords, we halt the upwards propagation |
| 188 | // because it means this FP is determining coordinates on its own. |
| 189 | if (!child->isSampledWithExplicitCoords()) { |
| 190 | if ((child->fFlags & kUsesSampleCoordsDirectly_Flag || |
| 191 | child->fFlags & kUsesSampleCoordsIndirectly_Flag)) { |
| 192 | fFlags |= kUsesSampleCoordsIndirectly_Flag; |
| 193 | } |
| 194 | } |
| 195 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 196 | fRequestedFeatures |= child->fRequestedFeatures; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 197 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 198 | // Record that the child is attached to us; this FP is the source of any uniform data needed |
| 199 | // to evaluate the child sample matrix. |
| 200 | child->fParent = this; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 201 | fChildProcessors.push_back(std::move(child)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 202 | |
Leon Scroggins III | 982fff2 | 2020-07-31 14:09:06 -0400 | [diff] [blame] | 203 | // Validate: our sample strategy comes from a parent we shouldn't have yet. |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 204 | SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() && |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 205 | !fUsage.isSampled() && !fParent); |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 206 | } |
| 207 | |
John Stiles | 9ec6b05 | 2020-06-15 12:06:10 -0400 | [diff] [blame] | 208 | void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) { |
| 209 | for (int i = 0; i < src.numChildProcessors(); ++i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 210 | if (auto fp = src.childProcessor(i)) { |
| 211 | this->registerChild(fp->clone(), fp->sampleUsage()); |
| 212 | } else { |
| 213 | this->registerChild(nullptr); |
| 214 | } |
John Stiles | 9ec6b05 | 2020-06-15 12:06:10 -0400 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 218 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha( |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 219 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 220 | if (!fp) { |
| 221 | return nullptr; |
| 222 | } |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 223 | return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 226 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha( |
| 227 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 228 | if (!fp) { |
| 229 | return nullptr; |
| 230 | } |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 231 | return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn); |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 232 | } |
| 233 | |
John Stiles | 7bf2600 | 2020-07-13 11:30:12 -0400 | [diff] [blame] | 234 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateAlpha( |
| 235 | std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) { |
| 236 | auto colorFP = GrConstColorProcessor::Make(color); |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 237 | return GrBlendFragmentProcessor::Make( |
John Stiles | 7bf2600 | 2020-07-13 11:30:12 -0400 | [diff] [blame] | 238 | std::move(colorFP), std::move(inputFP), SkBlendMode::kSrcIn, |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 239 | GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior); |
John Stiles | 7bf2600 | 2020-07-13 11:30:12 -0400 | [diff] [blame] | 240 | } |
| 241 | |
John Stiles | 8589430 | 2020-07-13 11:39:52 -0400 | [diff] [blame] | 242 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateRGBA( |
| 243 | std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) { |
| 244 | auto colorFP = GrConstColorProcessor::Make(color); |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 245 | return GrBlendFragmentProcessor::Make( |
John Stiles | 8589430 | 2020-07-13 11:39:52 -0400 | [diff] [blame] | 246 | std::move(colorFP), std::move(inputFP), SkBlendMode::kModulate, |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 247 | GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior); |
John Stiles | 8589430 | 2020-07-13 11:39:52 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 250 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput( |
| 251 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 252 | if (!fp) { |
| 253 | return nullptr; |
| 254 | } |
John Stiles | 5a2a7b3 | 2020-06-04 10:57:21 -0400 | [diff] [blame] | 255 | return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true); |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 258 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput( |
| 259 | std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 260 | class SwizzleFragmentProcessor : public GrFragmentProcessor { |
| 261 | public: |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 262 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 263 | const GrSwizzle& swizzle) { |
| 264 | return std::unique_ptr<GrFragmentProcessor>( |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 265 | new SwizzleFragmentProcessor(std::move(fp), swizzle)); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | const char* name() const override { return "Swizzle"; } |
| 269 | const GrSwizzle& swizzle() const { return fSwizzle; } |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 270 | bool usesExplicitReturn() const override { return true; } |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 271 | |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 272 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 273 | return Make(this->childProcessor(0)->clone(), fSwizzle); |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 274 | } |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 275 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 276 | private: |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 277 | SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) |
| 278 | : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get())) |
| 279 | , fSwizzle(swizzle) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 280 | this->registerChild(std::move(fp)); |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 281 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 282 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 283 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 284 | class GLFP : public GrGLSLFragmentProcessor { |
| 285 | public: |
| 286 | void emitCode(EmitArgs& args) override { |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 287 | SkString childColor = this->invokeChild(0, args); |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 288 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 289 | const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>(); |
| 290 | const GrSwizzle& swizzle = sfp.swizzle(); |
| 291 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 292 | |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 293 | fragBuilder->codeAppendf("return %s.%s;", |
| 294 | childColor.c_str(), swizzle.asString().c_str()); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 295 | } |
| 296 | }; |
| 297 | return new GLFP; |
| 298 | } |
| 299 | |
| 300 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 301 | b->add32(fSwizzle.asKey()); |
| 302 | } |
| 303 | |
| 304 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
| 305 | const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>(); |
| 306 | return fSwizzle == sfp.fSwizzle; |
| 307 | } |
| 308 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 309 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 310 | return fSwizzle.applyTo(input); |
| 311 | } |
| 312 | |
| 313 | GrSwizzle fSwizzle; |
| 314 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 315 | using INHERITED = GrFragmentProcessor; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | if (!fp) { |
| 319 | return nullptr; |
| 320 | } |
| 321 | if (GrSwizzle::RGBA() == swizzle) { |
| 322 | return fp; |
| 323 | } |
John Stiles | eed56f0 | 2020-06-04 13:30:51 -0400 | [diff] [blame] | 324 | return SwizzleFragmentProcessor::Make(std::move(fp), swizzle); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 327 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput( |
| 328 | std::unique_ptr<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 329 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 330 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 331 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 332 | std::unique_ptr<GrFragmentProcessor> processor) { |
| 333 | return std::unique_ptr<GrFragmentProcessor>( |
| 334 | new PremulFragmentProcessor(std::move(processor))); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | const char* name() const override { return "Premultiply"; } |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 338 | bool usesExplicitReturn() const override { return true; } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 339 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 340 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 341 | return Make(this->childProcessor(0)->clone()); |
Brian Salomon | 216f2e0 | 2017-07-25 15:52:51 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 344 | private: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 345 | PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 346 | : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 347 | this->registerChild(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 348 | } |
| 349 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 350 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 351 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 352 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 353 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 354 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 355 | SkString temp = this->invokeChild(/*childIndex=*/0, "half4(1)", args); |
| 356 | fragBuilder->codeAppendf("half4 color = %s;", temp.c_str()); |
| 357 | fragBuilder->codeAppendf("color.rgb *= %s.rgb;", args.fInputColor); |
| 358 | fragBuilder->codeAppendf("return color * %s.a;", args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 359 | } |
| 360 | }; |
| 361 | return new GLFP; |
| 362 | } |
| 363 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 364 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 365 | |
| 366 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 367 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 368 | static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) { |
| 369 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 370 | if (inner->preservesOpaqueInput()) { |
| 371 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 372 | } |
| 373 | if (inner->hasConstantOutputForConstantInput()) { |
| 374 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 375 | } |
| 376 | return flags; |
| 377 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 378 | |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 379 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { |
| 380 | SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0), |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 381 | SK_PMColor4fWHITE); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 382 | SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul(); |
| 383 | return premulInput * childColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 384 | } |
| 385 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 386 | using INHERITED = GrFragmentProcessor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 387 | }; |
| 388 | if (!fp) { |
| 389 | return nullptr; |
| 390 | } |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 391 | return PremulFragmentProcessor::Make(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | ////////////////////////////////////////////////////////////////////////////// |
| 395 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 396 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput( |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 397 | std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 398 | if (!fp) { |
| 399 | return nullptr; |
| 400 | } |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 401 | return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 402 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 403 | |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 404 | ////////////////////////////////////////////////////////////////////////////// |
| 405 | |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 406 | std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose( |
| 407 | std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) { |
| 408 | class ComposeProcessor : public GrFragmentProcessor { |
| 409 | public: |
| 410 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f, |
| 411 | std::unique_ptr<GrFragmentProcessor> g) { |
| 412 | return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f), |
| 413 | std::move(g))); |
| 414 | } |
| 415 | |
| 416 | const char* name() const override { return "Compose"; } |
| 417 | |
| 418 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 419 | return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this)); |
| 420 | } |
| 421 | |
| 422 | private: |
| 423 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 424 | class GLFP : public GrGLSLFragmentProcessor { |
| 425 | public: |
| 426 | void emitCode(EmitArgs& args) override { |
| 427 | SkString result = this->invokeChild(0, args); |
| 428 | result = this->invokeChild(1, result.c_str(), args); |
John Stiles | 4964300 | 2020-10-19 18:52:40 -0400 | [diff] [blame] | 429 | args.fFragBuilder->codeAppendf("return %s;", result.c_str()); |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 430 | } |
| 431 | }; |
| 432 | return new GLFP; |
| 433 | } |
| 434 | |
| 435 | ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f, |
| 436 | std::unique_ptr<GrFragmentProcessor> g) |
| 437 | : INHERITED(kSeriesFragmentProcessor_ClassID, |
| 438 | f->optimizationFlags() & g->optimizationFlags()) { |
| 439 | this->registerChild(std::move(f)); |
| 440 | this->registerChild(std::move(g)); |
| 441 | } |
| 442 | |
| 443 | ComposeProcessor(const ComposeProcessor& that) |
| 444 | : INHERITED(kSeriesFragmentProcessor_ClassID, that.optimizationFlags()) { |
| 445 | this->cloneAndRegisterAllChildProcessors(that); |
| 446 | } |
| 447 | |
| 448 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 449 | |
| 450 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
John Stiles | 4964300 | 2020-10-19 18:52:40 -0400 | [diff] [blame] | 451 | bool usesExplicitReturn() const override { return true; } |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 452 | |
| 453 | SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override { |
| 454 | SkPMColor4f color = inColor; |
| 455 | color = ConstantOutputForConstantInput(this->childProcessor(0), color); |
| 456 | color = ConstantOutputForConstantInput(this->childProcessor(1), color); |
| 457 | return color; |
| 458 | } |
| 459 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 460 | using INHERITED = GrFragmentProcessor; |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | // Allow either of the composed functions to be null. |
| 464 | if (f == nullptr) { |
| 465 | return g; |
| 466 | } |
| 467 | if (g == nullptr) { |
| 468 | return f; |
| 469 | } |
| 470 | |
| 471 | // Run an optimization pass on this composition. |
| 472 | GrProcessorAnalysisColor inputColor; |
| 473 | inputColor.setToUnknown(); |
| 474 | |
| 475 | std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(f), std::move(g)}; |
| 476 | GrColorFragmentProcessorAnalysis info(inputColor, series, SK_ARRAY_COUNT(series)); |
| 477 | |
| 478 | SkPMColor4f knownColor; |
| 479 | int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor); |
| 480 | switch (leadingFPsToEliminate) { |
| 481 | default: |
| 482 | // We shouldn't eliminate more than we started with. |
| 483 | SkASSERT(leadingFPsToEliminate <= 2); |
| 484 | [[fallthrough]]; |
| 485 | case 0: |
| 486 | // Compose the two processors as requested. |
| 487 | return ComposeProcessor::Make(std::move(series[0]), std::move(series[1])); |
| 488 | case 1: |
| 489 | // Replace the first processor with a constant color. |
| 490 | return ComposeProcessor::Make(GrConstColorProcessor::Make(knownColor), |
| 491 | std::move(series[1])); |
| 492 | case 2: |
| 493 | // Replace the entire composition with a constant color. |
| 494 | return GrConstColorProcessor::Make(knownColor); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | ////////////////////////////////////////////////////////////////////////////// |
| 499 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 500 | GrFragmentProcessor::CIter::CIter(const GrPaint& paint) { |
John Stiles | 41d91b6 | 2020-07-21 14:39:40 -0400 | [diff] [blame] | 501 | if (paint.hasCoverageFragmentProcessor()) { |
| 502 | fFPStack.push_back(paint.getCoverageFragmentProcessor()); |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 503 | } |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 504 | if (paint.hasColorFragmentProcessor()) { |
| 505 | fFPStack.push_back(paint.getColorFragmentProcessor()); |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 509 | GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 510 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 511 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 512 | } |
| 513 | } |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 514 | |
| 515 | GrFragmentProcessor::CIter& GrFragmentProcessor::CIter::operator++() { |
| 516 | SkASSERT(!fFPStack.empty()); |
| 517 | const GrFragmentProcessor* back = fFPStack.back(); |
| 518 | fFPStack.pop_back(); |
| 519 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 520 | if (auto child = back->childProcessor(i)) { |
| 521 | fFPStack.push_back(child); |
| 522 | } |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 523 | } |
| 524 | return *this; |
| 525 | } |
| 526 | |