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 | |
| 8 | #include "GrFragmentProcessor.h" |
| 9 | #include "GrCoordTransform.h" |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 10 | #include "GrPipeline.h" |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 11 | #include "GrProcessorAnalysis.h" |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 12 | #include "effects/GrConstColorProcessor.h" |
| 13 | #include "effects/GrXfermodeFragmentProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 16 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 18 | |
| 19 | GrFragmentProcessor::~GrFragmentProcessor() { |
| 20 | // If we got here then our ref count must have reached zero, so we will have converted refs |
| 21 | // to pending executions for all children. |
| 22 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 23 | fChildProcessors[i]->completedExecution(); |
| 24 | } |
| 25 | } |
| 26 | |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 27 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 28 | if (this->classID() != that.classID() || |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 29 | !this->hasSameSamplersAndAccesses(that)) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 30 | return false; |
| 31 | } |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 32 | if (!this->hasSameTransforms(that)) { |
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 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 49 | GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const { |
| 50 | GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 51 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 52 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 53 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 54 | } |
| 55 | return glFragProc; |
| 56 | } |
| 57 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 58 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 59 | fCoordTransforms.push_back(transform); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 60 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 61 | SkDEBUGCODE(transform->setInProcessor();) |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 62 | } |
| 63 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 64 | int GrFragmentProcessor::registerChildProcessor(sk_sp<GrFragmentProcessor> child) { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 65 | if (child->isBad()) { |
| 66 | this->markAsBad(); |
| 67 | } |
| 68 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 69 | this->combineRequiredFeatures(*child); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 70 | |
| 71 | if (child->usesLocalCoords()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 72 | fFlags |= kUsesLocalCoords_Flag; |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 73 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 74 | if (child->usesDistanceVectorField()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 75 | fFlags |= kUsesDistanceVectorField_Flag; |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 76 | } |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 77 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 78 | int index = fChildProcessors.count(); |
| 79 | fChildProcessors.push_back(child.release()); |
| 80 | |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 81 | return index; |
| 82 | } |
| 83 | |
| 84 | void GrFragmentProcessor::notifyRefCntIsZero() const { |
| 85 | // See comment above GrProgramElement for a detailed explanation of why we do this. |
| 86 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 87 | fChildProcessors[i]->addPendingExecution(); |
| 88 | fChildProcessors[i]->unref(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 93 | if (this->numCoordTransforms() != that.numCoordTransforms()) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 96 | int count = this->numCoordTransforms(); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 97 | for (int i = 0; i < count; ++i) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 98 | if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) { |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 105 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::MulOutputByInputAlpha( |
| 106 | sk_sp<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 107 | if (!fp) { |
| 108 | return nullptr; |
| 109 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 110 | return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn); |
bsalomon | bf87730 | 2015-09-22 09:06:13 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 113 | namespace { |
| 114 | |
| 115 | class PremulInputFragmentProcessor : public GrFragmentProcessor { |
| 116 | public: |
| 117 | PremulInputFragmentProcessor() |
| 118 | : INHERITED(kPreservesOpaqueInput_OptimizationFlag | |
| 119 | kConstantOutputForConstantInput_OptimizationFlag) { |
| 120 | this->initClassID<PremulInputFragmentProcessor>(); |
| 121 | } |
| 122 | |
| 123 | const char* name() const override { return "PremultiplyInput"; } |
| 124 | |
| 125 | private: |
| 126 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 127 | class GLFP : public GrGLSLFragmentProcessor { |
| 128 | public: |
| 129 | void emitCode(EmitArgs& args) override { |
| 130 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 131 | |
| 132 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 133 | fragBuilder->codeAppendf("%s.rgb *= %s.a;", |
| 134 | args.fOutputColor, args.fInputColor); |
| 135 | } |
| 136 | }; |
| 137 | return new GLFP; |
| 138 | } |
| 139 | |
| 140 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 141 | |
| 142 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 143 | |
| 144 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 145 | return input.premul(); |
| 146 | } |
| 147 | |
| 148 | typedef GrFragmentProcessor INHERITED; |
| 149 | }; |
| 150 | |
| 151 | class UnpremulInputFragmentProcessor : public GrFragmentProcessor { |
| 152 | public: |
| 153 | UnpremulInputFragmentProcessor() |
| 154 | : INHERITED(kPreservesOpaqueInput_OptimizationFlag | |
| 155 | kConstantOutputForConstantInput_OptimizationFlag) { |
| 156 | this->initClassID<UnpremulInputFragmentProcessor>(); |
| 157 | } |
| 158 | |
| 159 | const char* name() const override { return "UnpremultiplyInput"; } |
| 160 | |
| 161 | private: |
| 162 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 163 | class GLFP : public GrGLSLFragmentProcessor { |
| 164 | public: |
| 165 | void emitCode(EmitArgs& args) override { |
| 166 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 167 | |
| 168 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 169 | fragBuilder->codeAppendf("float invAlpha = %s.a <= 0.0 ? 0.0 : 1.0 / %s.a;", |
| 170 | args.fInputColor, args.fInputColor); |
| 171 | fragBuilder->codeAppendf("%s.rgb *= invAlpha;", args.fOutputColor); |
| 172 | } |
| 173 | }; |
| 174 | return new GLFP; |
| 175 | } |
| 176 | |
| 177 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 178 | |
| 179 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 180 | |
| 181 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 182 | return input.unpremul(); |
| 183 | } |
| 184 | |
| 185 | typedef GrFragmentProcessor INHERITED; |
| 186 | }; |
| 187 | |
| 188 | } |
| 189 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 190 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::PremulInput(sk_sp<GrFragmentProcessor> fp) { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 191 | if (!fp) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | sk_sp<GrFragmentProcessor> fpPipeline[] = { sk_make_sp<PremulInputFragmentProcessor>(), fp}; |
| 195 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 196 | } |
| 197 | |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 198 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::PremulOutput(sk_sp<GrFragmentProcessor> fp) { |
| 199 | if (!fp) { |
| 200 | return nullptr; |
| 201 | } |
| 202 | sk_sp<GrFragmentProcessor> fpPipeline[] = { fp, sk_make_sp<PremulInputFragmentProcessor>() }; |
| 203 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 204 | } |
| 205 | |
| 206 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::UnpremulOutput(sk_sp<GrFragmentProcessor> fp) { |
| 207 | if (!fp) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | sk_sp<GrFragmentProcessor> fpPipeline[] = { fp, sk_make_sp<UnpremulInputFragmentProcessor>() }; |
| 211 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 212 | } |
| 213 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 214 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(sk_sp<GrFragmentProcessor> fp, |
| 215 | const GrSwizzle& swizzle) { |
| 216 | class SwizzleFragmentProcessor : public GrFragmentProcessor { |
| 217 | public: |
| 218 | SwizzleFragmentProcessor(const GrSwizzle& swizzle) |
| 219 | : INHERITED(kAll_OptimizationFlags) |
| 220 | , fSwizzle(swizzle) { |
| 221 | this->initClassID<SwizzleFragmentProcessor>(); |
| 222 | } |
| 223 | |
| 224 | const char* name() const override { return "Swizzle"; } |
| 225 | const GrSwizzle& swizzle() const { return fSwizzle; } |
| 226 | |
| 227 | private: |
| 228 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 229 | class GLFP : public GrGLSLFragmentProcessor { |
| 230 | public: |
| 231 | void emitCode(EmitArgs& args) override { |
| 232 | const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>(); |
| 233 | const GrSwizzle& swizzle = sfp.swizzle(); |
| 234 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 235 | |
| 236 | fragBuilder->codeAppendf("%s = %s.%s;", |
| 237 | args.fOutputColor, args.fInputColor, swizzle.c_str()); |
| 238 | } |
| 239 | }; |
| 240 | return new GLFP; |
| 241 | } |
| 242 | |
| 243 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 244 | b->add32(fSwizzle.asKey()); |
| 245 | } |
| 246 | |
| 247 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
| 248 | const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>(); |
| 249 | return fSwizzle == sfp.fSwizzle; |
| 250 | } |
| 251 | |
| 252 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 253 | return fSwizzle.applyTo(input); |
| 254 | } |
| 255 | |
| 256 | GrSwizzle fSwizzle; |
| 257 | |
| 258 | typedef GrFragmentProcessor INHERITED; |
| 259 | }; |
| 260 | |
| 261 | if (!fp) { |
| 262 | return nullptr; |
| 263 | } |
| 264 | if (GrSwizzle::RGBA() == swizzle) { |
| 265 | return fp; |
| 266 | } |
| 267 | sk_sp<GrFragmentProcessor> fpPipeline[] = { fp, sk_make_sp<SwizzleFragmentProcessor>(swizzle) }; |
| 268 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 269 | } |
| 270 | |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 271 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput( |
| 272 | sk_sp<GrFragmentProcessor> fp) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 273 | |
| 274 | class PremulFragmentProcessor : public GrFragmentProcessor { |
| 275 | public: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 276 | PremulFragmentProcessor(sk_sp<GrFragmentProcessor> processor) |
| 277 | : INHERITED(OptFlags(processor.get())) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 278 | this->initClassID<PremulFragmentProcessor>(); |
| 279 | this->registerChildProcessor(processor); |
| 280 | } |
| 281 | |
| 282 | const char* name() const override { return "Premultiply"; } |
| 283 | |
| 284 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 285 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 286 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 287 | public: |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 288 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 289 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 290 | this->emitChild(0, args); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 291 | fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 292 | args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 293 | fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 294 | } |
| 295 | }; |
| 296 | return new GLFP; |
| 297 | } |
| 298 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 299 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 300 | |
| 301 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 302 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 303 | static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) { |
| 304 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 305 | if (inner->preservesOpaqueInput()) { |
| 306 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 307 | } |
| 308 | if (inner->hasConstantOutputForConstantInput()) { |
| 309 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 310 | } |
| 311 | return flags; |
| 312 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 313 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 314 | GrColor4f constantOutputForConstantInput(GrColor4f input) const override { |
| 315 | GrColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0), |
| 316 | GrColor4f::OpaqueWhite()); |
| 317 | return GrColor4f(input.fRGBA[3] * input.fRGBA[0] * childColor.fRGBA[0], |
| 318 | input.fRGBA[3] * input.fRGBA[1] * childColor.fRGBA[1], |
| 319 | input.fRGBA[3] * input.fRGBA[2] * childColor.fRGBA[2], |
| 320 | input.fRGBA[3] * childColor.fRGBA[3]); |
| 321 | } |
| 322 | |
| 323 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 324 | }; |
| 325 | if (!fp) { |
| 326 | return nullptr; |
| 327 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 328 | return sk_sp<GrFragmentProcessor>(new PremulFragmentProcessor(std::move(fp))); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | ////////////////////////////////////////////////////////////////////////////// |
| 332 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 333 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(sk_sp<GrFragmentProcessor> fp, |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 334 | GrColor4f color) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 335 | class ReplaceInputFragmentProcessor : public GrFragmentProcessor { |
| 336 | public: |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 337 | ReplaceInputFragmentProcessor(sk_sp<GrFragmentProcessor> child, GrColor4f color) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 338 | : INHERITED(OptFlags(child.get(), color)), fColor(color) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 339 | this->initClassID<ReplaceInputFragmentProcessor>(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 340 | this->registerChildProcessor(std::move(child)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | const char* name() const override { return "Replace Color"; } |
| 344 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 345 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 346 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 347 | public: |
| 348 | GLFP() : fHaveSetColor(false) {} |
| 349 | void emitCode(EmitArgs& args) override { |
| 350 | const char* colorName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 351 | fColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
| 352 | kVec4f_GrSLType, |
| 353 | kDefault_GrSLPrecision, |
| 354 | "Color", &colorName); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 355 | this->emitChild(0, colorName, args); |
| 356 | } |
| 357 | |
| 358 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 359 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 360 | const GrFragmentProcessor& fp) override { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 361 | GrColor4f color = fp.cast<ReplaceInputFragmentProcessor>().fColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 362 | if (!fHaveSetColor || color != fPreviousColor) { |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 363 | pdman.set4fv(fColorUni, 1, color.fRGBA); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 364 | fPreviousColor = color; |
| 365 | fHaveSetColor = true; |
| 366 | } |
| 367 | } |
| 368 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 369 | GrGLSLProgramDataManager::UniformHandle fColorUni; |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 370 | bool fHaveSetColor; |
| 371 | GrColor4f fPreviousColor; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | return new GLFP; |
| 375 | } |
| 376 | |
| 377 | private: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 378 | static OptimizationFlags OptFlags(const GrFragmentProcessor* child, GrColor4f color) { |
| 379 | OptimizationFlags childFlags = child->optimizationFlags(); |
| 380 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 381 | if (childFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 382 | flags |= kConstantOutputForConstantInput_OptimizationFlag; |
| 383 | } |
| 384 | if ((childFlags & kPreservesOpaqueInput_OptimizationFlag) && color.isOpaque()) { |
| 385 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 386 | } |
| 387 | return flags; |
| 388 | } |
| 389 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 390 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 391 | {} |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 392 | |
| 393 | bool onIsEqual(const GrFragmentProcessor& that) const override { |
| 394 | return fColor == that.cast<ReplaceInputFragmentProcessor>().fColor; |
| 395 | } |
| 396 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 397 | GrColor4f constantOutputForConstantInput(GrColor4f) const override { |
| 398 | return ConstantOutputForConstantInput(this->childProcessor(0), fColor); |
| 399 | } |
| 400 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 401 | GrColor4f fColor; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 402 | |
| 403 | typedef GrFragmentProcessor INHERITED; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
Brian Salomon | 5f13fba | 2017-01-23 14:35:25 -0500 | [diff] [blame] | 406 | return sk_sp<GrFragmentProcessor>(new ReplaceInputFragmentProcessor(std::move(fp), color)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 407 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 408 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 409 | sk_sp<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(sk_sp<GrFragmentProcessor>* series, |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 410 | int cnt) { |
| 411 | class SeriesFragmentProcessor : public GrFragmentProcessor { |
| 412 | public: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 413 | SeriesFragmentProcessor(sk_sp<GrFragmentProcessor>* children, int cnt) |
| 414 | : INHERITED(OptFlags(children, cnt)) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 415 | SkASSERT(cnt > 1); |
| 416 | this->initClassID<SeriesFragmentProcessor>(); |
| 417 | for (int i = 0; i < cnt; ++i) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 418 | this->registerChildProcessor(std::move(children[i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | const char* name() const override { return "Series"; } |
| 423 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 424 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 425 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 426 | public: |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 427 | void emitCode(EmitArgs& args) override { |
dvonbeck | ca9eeab | 2016-07-06 12:00:06 -0700 | [diff] [blame] | 428 | // First guy's input might be nil. |
| 429 | SkString temp("out0"); |
| 430 | this->emitChild(0, args.fInputColor, &temp, args); |
| 431 | SkString input = temp; |
| 432 | for (int i = 1; i < this->numChildProcessors() - 1; ++i) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 433 | temp.printf("out%d", i); |
| 434 | this->emitChild(i, input.c_str(), &temp, args); |
| 435 | input = temp; |
| 436 | } |
| 437 | // Last guy writes to our output variable. |
| 438 | this->emitChild(this->numChildProcessors() - 1, input.c_str(), args); |
| 439 | } |
| 440 | }; |
| 441 | return new GLFP; |
| 442 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 443 | private: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 444 | static OptimizationFlags OptFlags(sk_sp<GrFragmentProcessor>* children, int cnt) { |
| 445 | OptimizationFlags flags = kAll_OptimizationFlags; |
| 446 | for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) { |
| 447 | flags &= children[i]->optimizationFlags(); |
| 448 | } |
| 449 | return flags; |
| 450 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 451 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 452 | |
| 453 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 454 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 455 | GrColor4f constantOutputForConstantInput(GrColor4f color) const override { |
| 456 | int childCnt = this->numChildProcessors(); |
| 457 | for (int i = 0; i < childCnt; ++i) { |
| 458 | color = ConstantOutputForConstantInput(this->childProcessor(i), color); |
| 459 | } |
| 460 | return color; |
| 461 | } |
| 462 | |
| 463 | typedef GrFragmentProcessor INHERITED; |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | if (!cnt) { |
| 467 | return nullptr; |
| 468 | } |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 469 | if (1 == cnt) { |
| 470 | return series[0]; |
| 471 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 472 | // Run the through the series, do the invariant output processing, and look for eliminations. |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 473 | GrColorFragmentProcessorAnalysis info; |
Brian Salomon | 0831f1b | 2017-01-18 11:08:41 -0500 | [diff] [blame] | 474 | info.analyzeProcessors(sk_sp_address_as_pointer_address(series), cnt); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 475 | SkTArray<sk_sp<GrFragmentProcessor>> replacementSeries; |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 476 | GrColor4f knownColor; |
| 477 | int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor); |
| 478 | if (leadingFPsToEliminate) { |
| 479 | sk_sp<GrFragmentProcessor> colorFP( |
| 480 | GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::kIgnore_InputMode)); |
| 481 | if (leadingFPsToEliminate == cnt) { |
| 482 | return colorFP; |
| 483 | } |
| 484 | cnt = cnt - leadingFPsToEliminate + 1; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 485 | replacementSeries.reserve(cnt); |
| 486 | replacementSeries.emplace_back(std::move(colorFP)); |
| 487 | for (int i = 0; i < cnt - 1; ++i) { |
Brian Salomon | eec6f7b | 2017-02-10 14:29:38 -0500 | [diff] [blame] | 488 | replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i])); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 489 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 490 | series = replacementSeries.begin(); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 491 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 492 | return sk_sp<GrFragmentProcessor>(new SeriesFragmentProcessor(series, cnt)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 493 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 494 | |
| 495 | ////////////////////////////////////////////////////////////////////////////// |
| 496 | |
| 497 | GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) { |
| 498 | for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) { |
| 499 | fFPStack.push_back(&pipeline.getFragmentProcessor(i)); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | const GrFragmentProcessor* GrFragmentProcessor::Iter::next() { |
| 504 | if (fFPStack.empty()) { |
| 505 | return nullptr; |
| 506 | } |
| 507 | const GrFragmentProcessor* back = fFPStack.back(); |
| 508 | fFPStack.pop_back(); |
| 509 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 510 | fFPStack.push_back(&back->childProcessor(i)); |
| 511 | } |
| 512 | return back; |
| 513 | } |
| 514 | |