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