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 "src/gpu/GrFragmentProcessor.h"
9#include "src/gpu/GrPipeline.h"
10#include "src/gpu/GrProcessorAnalysis.h"
11#include "src/gpu/effects/GrBlendFragmentProcessor.h"
12#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
13#include "src/gpu/effects/generated/GrConstColorProcessor.h"
14#include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
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"
19
20bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
21 if (this->classID() != that.classID()) {
22 return false;
23 }
24 if (this->usesVaryingCoordsDirectly() != that.usesVaryingCoordsDirectly()) {
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) {
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)) {
40 return false;
41 }
42 }
43 return true;
44}
45
46void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) const {
47 this->visitTextureEffects([&func](const GrTextureEffect& te) {
48 func(te.view().proxy(), te.samplerState().mipmapped());
49 });
50}
51
52void GrFragmentProcessor::visitTextureEffects(
53 const std::function<void(const GrTextureEffect&)>& func) const {
54 if (auto* te = this->asTextureEffect()) {
55 func(*te);
56 }
57 for (auto& child : fChildProcessors) {
58 if (child) {
59 child->visitTextureEffects(func);
60 }
61 }
62}
63
64GrTextureEffect* GrFragmentProcessor::asTextureEffect() {
65 if (this->classID() == kGrTextureEffect_ClassID) {
66 return static_cast<GrTextureEffect*>(this);
67 }
68 return nullptr;
69}
70
71const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const {
72 if (this->classID() == kGrTextureEffect_ClassID) {
73 return static_cast<const GrTextureEffect*>(this);
74 }
75 return nullptr;
76}
77
78#if GR_TEST_UTILS
79static 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
94SkString 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
102GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
103 GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
104 glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
105 for (int i = 0; i < fChildProcessors.count(); ++i) {
106 glFragProc->fChildProcessors[i] =
107 fChildProcessors[i] ? fChildProcessors[i]->createGLSLInstance() : nullptr;
108 }
109 return glFragProc;
110}
111
112void 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) {
117 if (child) {
118 child->addAndPushFlagToChildren(flag);
119 }
120 }
121 }
122#ifdef SK_DEBUG
123 for (auto& child : fChildProcessors) {
124 SkASSERT(!child || (child->fFlags & flag));
125 }
126#endif
127}
128
129int GrFragmentProcessor::numNonNullChildProcessors() const {
130 return std::count_if(fChildProcessors.begin(), fChildProcessors.end(),
131 [](const auto& c) { return c != nullptr; });
132}
133
134#ifdef SK_DEBUG
135bool GrFragmentProcessor::isInstantiated() const {
136 bool result = true;
137 this->visitTextureEffects([&result](const GrTextureEffect& te) {
138 if (!te.texture()) {
139 result = false;
140 }
141 });
142 return result;
143}
144#endif
145
146void GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
147 SkSL::SampleUsage sampleUsage) {
148 if (!child) {
149 fChildProcessors.push_back(nullptr);
150 return;
151 }
152
153 // The child should not have been attached to another FP already and not had any sampling
154 // strategy set on it.
155 SkASSERT(!child->fParent && !child->sampleUsage().isSampled() &&
156 !child->isSampledWithExplicitCoords() && !child->hasPerspectiveTransform());
157
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
163 // Configure child's sampling state first
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) {
170 child->addAndPushFlagToChildren(kSampledWithExplicitCoords_Flag);
171 }
172
173 // Push perspective matrix type to children
174 if (sampleUsage.fHasPerspective) {
175 child->addAndPushFlagToChildren(kNetTransformHasPerspective_Flag);
176 }
177
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) {
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
196 fRequestedFeatures |= child->fRequestedFeatures;
197
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;
201 fChildProcessors.push_back(std::move(child));
202
203 // Validate: our sample strategy comes from a parent we shouldn't have yet.
204 SkASSERT(!this->isSampledWithExplicitCoords() && !this->hasPerspectiveTransform() &&
205 !fUsage.isSampled() && !fParent);
206}
207
208void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
209 for (int i = 0; i < src.numChildProcessors(); ++i) {
210 if (auto fp = src.childProcessor(i)) {
211 this->registerChild(fp->clone(), fp->sampleUsage());
212 } else {
213 this->registerChild(nullptr);
214 }
215 }
216}
217
218std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
219 std::unique_ptr<GrFragmentProcessor> fp) {
220 if (!fp) {
221 return nullptr;
222 }
223 return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kDstIn);
224}
225
226std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
227 std::unique_ptr<GrFragmentProcessor> fp) {
228 if (!fp) {
229 return nullptr;
230 }
231 return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn);
232}
233
234std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateAlpha(
235 std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
236 auto colorFP = GrConstColorProcessor::Make(color);
237 return GrBlendFragmentProcessor::Make(
238 std::move(colorFP), std::move(inputFP), SkBlendMode::kSrcIn,
239 GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior);
240}
241
242std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateRGBA(
243 std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
244 auto colorFP = GrConstColorProcessor::Make(color);
245 return GrBlendFragmentProcessor::Make(
246 std::move(colorFP), std::move(inputFP), SkBlendMode::kModulate,
247 GrBlendFragmentProcessor::BlendBehavior::kSkModeBehavior);
248}
249
250std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampPremulOutput(
251 std::unique_ptr<GrFragmentProcessor> fp) {
252 if (!fp) {
253 return nullptr;
254 }
255 return GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/true);
256}
257
258std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
259 std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
260 class SwizzleFragmentProcessor : public GrFragmentProcessor {
261 public:
262 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
263 const GrSwizzle& swizzle) {
264 return std::unique_ptr<GrFragmentProcessor>(
265 new SwizzleFragmentProcessor(std::move(fp), swizzle));
266 }
267
268 const char* name() const override { return "Swizzle"; }
269 const GrSwizzle& swizzle() const { return fSwizzle; }
270
271 std::unique_ptr<GrFragmentProcessor> clone() const override {
272 return Make(this->childProcessor(0)->clone(), fSwizzle);
273 }
274
275 private:
276 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle)
277 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
278 , fSwizzle(swizzle) {
279 this->registerChild(std::move(fp));
280 }
281
282 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
283 class GLFP : public GrGLSLFragmentProcessor {
284 public:
285 void emitCode(EmitArgs& args) override {
286 SkString childColor = this->invokeChild(0, args);
287
288 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
289 const GrSwizzle& swizzle = sfp.swizzle();
290 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
291
292 fragBuilder->codeAppendf("%s = %s.%s;",
293 args.fOutputColor, childColor.c_str(), swizzle.asString().c_str());
294 }
295 };
296 return new GLFP;
297 }
298
299 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
300 b->add32(fSwizzle.asKey());
301 }
302
303 bool onIsEqual(const GrFragmentProcessor& other) const override {
304 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
305 return fSwizzle == sfp.fSwizzle;
306 }
307
308 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
309 return fSwizzle.applyTo(input);
310 }
311
312 GrSwizzle fSwizzle;
313
314 typedef GrFragmentProcessor INHERITED;
315 };
316
317 if (!fp) {
318 return nullptr;
319 }
320 if (GrSwizzle::RGBA() == swizzle) {
321 return fp;
322 }
323 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
324}
325
326std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
327 std::unique_ptr<GrFragmentProcessor> fp) {
328 class PremulFragmentProcessor : public GrFragmentProcessor {
329 public:
330 static std::unique_ptr<GrFragmentProcessor> Make(
331 std::unique_ptr<GrFragmentProcessor> processor) {
332 return std::unique_ptr<GrFragmentProcessor>(
333 new PremulFragmentProcessor(std::move(processor)));
334 }
335
336 const char* name() const override { return "Premultiply"; }
337
338 std::unique_ptr<GrFragmentProcessor> clone() const override {
339 return Make(this->childProcessor(0)->clone());
340 }
341
342 private:
343 PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
344 : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
345 this->registerChild(std::move(processor));
346 }
347
348 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
349 class GLFP : public GrGLSLFragmentProcessor {
350 public:
351 void emitCode(EmitArgs& args) override {
352 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
353 SkString temp = this->invokeChild(0, "half4(1)", args);
354 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
355 fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
356 args.fInputColor);
357 fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
358 }
359 };
360 return new GLFP;
361 }
362
363 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
364
365 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
366
367 static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
368 OptimizationFlags flags = kNone_OptimizationFlags;
369 if (inner->preservesOpaqueInput()) {
370 flags |= kPreservesOpaqueInput_OptimizationFlag;
371 }
372 if (inner->hasConstantOutputForConstantInput()) {
373 flags |= kConstantOutputForConstantInput_OptimizationFlag;
374 }
375 return flags;
376 }
377
378 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
379 SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
380 SK_PMColor4fWHITE);
381 SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
382 return premulInput * childColor;
383 }
384
385 typedef GrFragmentProcessor INHERITED;
386 };
387 if (!fp) {
388 return nullptr;
389 }
390 return PremulFragmentProcessor::Make(std::move(fp));
391}
392
393//////////////////////////////////////////////////////////////////////////////
394
395std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
396 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
397 if (!fp) {
398 return nullptr;
399 }
400 return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
401}
402
403//////////////////////////////////////////////////////////////////////////////
404
405std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose(
406 std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) {
407 class ComposeProcessor : public GrFragmentProcessor {
408 public:
409 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f,
410 std::unique_ptr<GrFragmentProcessor> g) {
411 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f),
412 std::move(g)));
413 }
414
415 const char* name() const override { return "Compose"; }
416
417 std::unique_ptr<GrFragmentProcessor> clone() const override {
418 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this));
419 }
420
421 private:
422 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
423 class GLFP : public GrGLSLFragmentProcessor {
424 public:
425 void emitCode(EmitArgs& args) override {
426 SkString result = this->invokeChild(0, args);
427 result = this->invokeChild(1, result.c_str(), args);
428 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, result.c_str());
429 }
430 };
431 return new GLFP;
432 }
433
434 ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f,
435 std::unique_ptr<GrFragmentProcessor> g)
436 : INHERITED(kSeriesFragmentProcessor_ClassID,
437 f->optimizationFlags() & g->optimizationFlags()) {
438 this->registerChild(std::move(f));
439 this->registerChild(std::move(g));
440 }
441
442 ComposeProcessor(const ComposeProcessor& that)
443 : INHERITED(kSeriesFragmentProcessor_ClassID, that.optimizationFlags()) {
444 this->cloneAndRegisterAllChildProcessors(that);
445 }
446
447 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
448
449 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
450
451 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
452 SkPMColor4f color = inColor;
453 color = ConstantOutputForConstantInput(this->childProcessor(0), color);
454 color = ConstantOutputForConstantInput(this->childProcessor(1), color);
455 return color;
456 }
457
458 typedef GrFragmentProcessor INHERITED;
459 };
460
461 // Allow either of the composed functions to be null.
462 if (f == nullptr) {
463 return g;
464 }
465 if (g == nullptr) {
466 return f;
467 }
468
469 // Run an optimization pass on this composition.
470 GrProcessorAnalysisColor inputColor;
471 inputColor.setToUnknown();
472
473 std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(f), std::move(g)};
474 GrColorFragmentProcessorAnalysis info(inputColor, series, SK_ARRAY_COUNT(series));
475
476 SkPMColor4f knownColor;
477 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
478 switch (leadingFPsToEliminate) {
479 default:
480 // We shouldn't eliminate more than we started with.
481 SkASSERT(leadingFPsToEliminate <= 2);
482 [[fallthrough]];
483 case 0:
484 // Compose the two processors as requested.
485 return ComposeProcessor::Make(std::move(series[0]), std::move(series[1]));
486 case 1:
487 // Replace the first processor with a constant color.
488 return ComposeProcessor::Make(GrConstColorProcessor::Make(knownColor),
489 std::move(series[1]));
490 case 2:
491 // Replace the entire composition with a constant color.
492 return GrConstColorProcessor::Make(knownColor);
493 }
494}
495
496//////////////////////////////////////////////////////////////////////////////
497
498GrFragmentProcessor::CIter::CIter(const GrPaint& paint) {
499 if (paint.hasCoverageFragmentProcessor()) {
500 fFPStack.push_back(paint.getCoverageFragmentProcessor());
501 }
502 if (paint.hasColorFragmentProcessor()) {
503 fFPStack.push_back(paint.getColorFragmentProcessor());
504 }
505}
506
507GrFragmentProcessor::CIter::CIter(const GrPipeline& pipeline) {
508 for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
509 fFPStack.push_back(&pipeline.getFragmentProcessor(i));
510 }
511}
512
513GrFragmentProcessor::CIter& GrFragmentProcessor::CIter::operator++() {
514 SkASSERT(!fFPStack.empty());
515 const GrFragmentProcessor* back = fFPStack.back();
516 fFPStack.pop_back();
517 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
518 if (auto child = back->childProcessor(i)) {
519 fFPStack.push_back(child);
520 }
521 }
522 return *this;
523}
524
525