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
10 #include "src/core/SkRuntimeEffectPriv.h"
11 #include "src/gpu/GrPipeline.h"
12 #include "src/gpu/GrProcessorAnalysis.h"
13 #include "src/gpu/GrShaderCaps.h"
14 #include "src/gpu/KeyBuilder.h"
15 #include "src/gpu/effects/GrBlendFragmentProcessor.h"
16 #include "src/gpu/effects/GrSkSLFP.h"
17 #include "src/gpu/effects/GrTextureEffect.h"
18 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
19 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
20 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
21 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
22
isEqual(const GrFragmentProcessor & that) const23 bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
24 if (this->classID() != that.classID()) {
25 return false;
26 }
27 if (this->sampleUsage() != that.sampleUsage()) {
28 return false;
29 }
30 if (!this->onIsEqual(that)) {
31 return false;
32 }
33 if (this->numChildProcessors() != that.numChildProcessors()) {
34 return false;
35 }
36 for (int i = 0; i < this->numChildProcessors(); ++i) {
37 auto thisChild = this->childProcessor(i),
38 thatChild = that .childProcessor(i);
39 if (SkToBool(thisChild) != SkToBool(thatChild)) {
40 return false;
41 }
42 if (thisChild && !thisChild->isEqual(*thatChild)) {
43 return false;
44 }
45 }
46 return true;
47 }
48
visitProxies(const GrVisitProxyFunc & func) const49 void GrFragmentProcessor::visitProxies(const GrVisitProxyFunc& func) const {
50 this->visitTextureEffects([&func](const GrTextureEffect& te) {
51 func(te.view().proxy(), te.samplerState().mipmapped());
52 });
53 }
54
visitTextureEffects(const std::function<void (const GrTextureEffect &)> & func) const55 void GrFragmentProcessor::visitTextureEffects(
56 const std::function<void(const GrTextureEffect&)>& func) const {
57 if (auto* te = this->asTextureEffect()) {
58 func(*te);
59 }
60 for (auto& child : fChildProcessors) {
61 if (child) {
62 child->visitTextureEffects(func);
63 }
64 }
65 }
66
visitWithImpls(const std::function<void (const GrFragmentProcessor &,ProgramImpl &)> & f,ProgramImpl & impl) const67 void GrFragmentProcessor::visitWithImpls(
68 const std::function<void(const GrFragmentProcessor&, ProgramImpl&)>& f,
69 ProgramImpl& impl) const {
70 f(*this, impl);
71 SkASSERT(impl.numChildProcessors() == this->numChildProcessors());
72 for (int i = 0; i < this->numChildProcessors(); ++i) {
73 if (const auto* child = this->childProcessor(i)) {
74 child->visitWithImpls(f, *impl.childProcessor(i));
75 }
76 }
77 }
78
asTextureEffect()79 GrTextureEffect* GrFragmentProcessor::asTextureEffect() {
80 if (this->classID() == kGrTextureEffect_ClassID) {
81 return static_cast<GrTextureEffect*>(this);
82 }
83 return nullptr;
84 }
85
asTextureEffect() const86 const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const {
87 if (this->classID() == kGrTextureEffect_ClassID) {
88 return static_cast<const GrTextureEffect*>(this);
89 }
90 return nullptr;
91 }
92
93 #if GR_TEST_UTILS
recursive_dump_tree_info(const GrFragmentProcessor & fp,SkString indent,SkString * text)94 static void recursive_dump_tree_info(const GrFragmentProcessor& fp,
95 SkString indent,
96 SkString* text) {
97 for (int index = 0; index < fp.numChildProcessors(); ++index) {
98 text->appendf("\n%s(#%d) -> ", indent.c_str(), index);
99 if (const GrFragmentProcessor* childFP = fp.childProcessor(index)) {
100 text->append(childFP->dumpInfo());
101 indent.append("\t");
102 recursive_dump_tree_info(*childFP, indent, text);
103 } else {
104 text->append("null");
105 }
106 }
107 }
108
dumpTreeInfo() const109 SkString GrFragmentProcessor::dumpTreeInfo() const {
110 SkString text = this->dumpInfo();
111 recursive_dump_tree_info(*this, SkString("\t"), &text);
112 text.append("\n");
113 return text;
114 }
115 #endif
116
makeProgramImpl() const117 std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrFragmentProcessor::makeProgramImpl() const {
118 std::unique_ptr<ProgramImpl> impl = this->onMakeProgramImpl();
119 impl->fChildProcessors.push_back_n(fChildProcessors.count());
120 for (int i = 0; i < fChildProcessors.count(); ++i) {
121 impl->fChildProcessors[i] = fChildProcessors[i] ? fChildProcessors[i]->makeProgramImpl()
122 : nullptr;
123 }
124 return impl;
125 }
126
numNonNullChildProcessors() const127 int GrFragmentProcessor::numNonNullChildProcessors() const {
128 return std::count_if(fChildProcessors.begin(), fChildProcessors.end(),
129 [](const auto& c) { return c != nullptr; });
130 }
131
132 #ifdef SK_DEBUG
isInstantiated() const133 bool GrFragmentProcessor::isInstantiated() const {
134 bool result = true;
135 this->visitTextureEffects([&result](const GrTextureEffect& te) {
136 if (!te.texture()) {
137 result = false;
138 }
139 });
140 return result;
141 }
142 #endif
143
registerChild(std::unique_ptr<GrFragmentProcessor> child,SkSL::SampleUsage sampleUsage)144 void GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
145 SkSL::SampleUsage sampleUsage) {
146 SkASSERT(sampleUsage.isSampled());
147
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
157 // Configure child's sampling state first
158 child->fUsage = sampleUsage;
159
160 // Propagate the "will read dest-color" flag up to parent FPs.
161 if (child->willReadDstColor()) {
162 this->setWillReadDstColor();
163 }
164
165 // If this child receives passthrough or matrix transformed coords from its parent then note
166 // that the parent's coords are used indirectly to ensure that they aren't omitted.
167 if ((sampleUsage.isPassThrough() || sampleUsage.isUniformMatrix()) &&
168 child->usesSampleCoords()) {
169 fFlags |= kUsesSampleCoordsIndirectly_Flag;
170 }
171
172 // Record that the child is attached to us; this FP is the source of any uniform data needed
173 // to evaluate the child sample matrix.
174 child->fParent = this;
175 fChildProcessors.push_back(std::move(child));
176
177 // Validate: our sample strategy comes from a parent we shouldn't have yet.
178 SkASSERT(!fUsage.isSampled() && !fParent);
179 }
180
cloneAndRegisterAllChildProcessors(const GrFragmentProcessor & src)181 void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
182 for (int i = 0; i < src.numChildProcessors(); ++i) {
183 if (auto fp = src.childProcessor(i)) {
184 this->registerChild(fp->clone(), fp->sampleUsage());
185 } else {
186 this->registerChild(nullptr);
187 }
188 }
189 }
190
MakeColor(SkPMColor4f color)191 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeColor(SkPMColor4f color) {
192 // Use ColorFilter signature/factory to get the constant output for constant input optimization
193 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
194 uniform half4 color;
195 half4 main(half4 inColor) { return color; }
196 )");
197 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
198 return GrSkSLFP::Make(effect, "color_fp", /*inputFP=*/nullptr,
199 color.isOpaque() ? GrSkSLFP::OptFlags::kPreservesOpaqueInput
200 : GrSkSLFP::OptFlags::kNone,
201 "color", color);
202 }
203
MulInputByChildAlpha(std::unique_ptr<GrFragmentProcessor> fp)204 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
205 std::unique_ptr<GrFragmentProcessor> fp) {
206 if (!fp) {
207 return nullptr;
208 }
209 return GrBlendFragmentProcessor::Make(/*src=*/nullptr, std::move(fp), SkBlendMode::kSrcIn);
210 }
211
ApplyPaintAlpha(std::unique_ptr<GrFragmentProcessor> child)212 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ApplyPaintAlpha(
213 std::unique_ptr<GrFragmentProcessor> child) {
214 SkASSERT(child);
215 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
216 uniform colorFilter fp;
217 half4 main(half4 inColor) {
218 return fp.eval(inColor.rgb1) * inColor.a;
219 }
220 )");
221 return GrSkSLFP::Make(effect, "ApplyPaintAlpha", /*inputFP=*/nullptr,
222 GrSkSLFP::OptFlags::kPreservesOpaqueInput |
223 GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
224 "fp", std::move(child));
225 }
226
ModulateRGBA(std::unique_ptr<GrFragmentProcessor> inputFP,const SkPMColor4f & color)227 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateRGBA(
228 std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
229 auto colorFP = MakeColor(color);
230 return GrBlendFragmentProcessor::Make(std::move(colorFP),
231 std::move(inputFP),
232 SkBlendMode::kModulate);
233 }
234
ClampOutput(std::unique_ptr<GrFragmentProcessor> fp)235 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampOutput(
236 std::unique_ptr<GrFragmentProcessor> fp) {
237 SkASSERT(fp);
238 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
239 half4 main(half4 inColor) {
240 return saturate(inColor);
241 }
242 )");
243 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
244 return GrSkSLFP::Make(
245 effect, "Clamp", std::move(fp), GrSkSLFP::OptFlags::kPreservesOpaqueInput);
246 }
247
SwizzleOutput(std::unique_ptr<GrFragmentProcessor> fp,const skgpu::Swizzle & swizzle)248 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
249 std::unique_ptr<GrFragmentProcessor> fp, const skgpu::Swizzle& swizzle) {
250 class SwizzleFragmentProcessor : public GrFragmentProcessor {
251 public:
252 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
253 const skgpu::Swizzle& swizzle) {
254 return std::unique_ptr<GrFragmentProcessor>(
255 new SwizzleFragmentProcessor(std::move(fp), swizzle));
256 }
257
258 const char* name() const override { return "Swizzle"; }
259
260 std::unique_ptr<GrFragmentProcessor> clone() const override {
261 return Make(this->childProcessor(0)->clone(), fSwizzle);
262 }
263
264 private:
265 SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp,
266 const skgpu::Swizzle& swizzle)
267 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
268 , fSwizzle(swizzle) {
269 this->registerChild(std::move(fp));
270 }
271
272 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
273 class Impl : public ProgramImpl {
274 public:
275 void emitCode(EmitArgs& args) override {
276 SkString childColor = this->invokeChild(0, args);
277
278 const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
279 const skgpu::Swizzle& swizzle = sfp.fSwizzle;
280 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
281
282 fragBuilder->codeAppendf("return %s.%s;",
283 childColor.c_str(), swizzle.asString().c_str());
284 }
285 };
286 return std::make_unique<Impl>();
287 }
288
289 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const override {
290 b->add32(fSwizzle.asKey());
291 }
292
293 bool onIsEqual(const GrFragmentProcessor& other) const override {
294 const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
295 return fSwizzle == sfp.fSwizzle;
296 }
297
298 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
299 return fSwizzle.applyTo(ConstantOutputForConstantInput(this->childProcessor(0), input));
300 }
301
302 skgpu::Swizzle fSwizzle;
303
304 using INHERITED = GrFragmentProcessor;
305 };
306
307 if (!fp) {
308 return nullptr;
309 }
310 if (skgpu::Swizzle::RGBA() == swizzle) {
311 return fp;
312 }
313 return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
314 }
315
316 //////////////////////////////////////////////////////////////////////////////
317
OverrideInput(std::unique_ptr<GrFragmentProcessor> fp,const SkPMColor4f & color)318 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
319 std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color) {
320 if (!fp) {
321 return nullptr;
322 }
323 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
324 uniform colorFilter fp; // Declared as colorFilter so we can pass a color
325 uniform half4 color;
326 half4 main(half4 inColor) {
327 return fp.eval(color);
328 }
329 )");
330 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
331 return GrSkSLFP::Make(effect, "OverrideInput", /*inputFP=*/nullptr,
332 color.isOpaque() ? GrSkSLFP::OptFlags::kPreservesOpaqueInput
333 : GrSkSLFP::OptFlags::kNone,
334 "fp", std::move(fp),
335 "color", color);
336 }
337
338 //////////////////////////////////////////////////////////////////////////////
339
DisableCoverageAsAlpha(std::unique_ptr<GrFragmentProcessor> fp)340 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::DisableCoverageAsAlpha(
341 std::unique_ptr<GrFragmentProcessor> fp) {
342 if (!fp || !fp->compatibleWithCoverageAsAlpha()) {
343 return fp;
344 }
345 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
346 half4 main(half4 inColor) { return inColor; }
347 )");
348 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
349 return GrSkSLFP::Make(effect, "DisableCoverageAsAlpha", std::move(fp),
350 GrSkSLFP::OptFlags::kPreservesOpaqueInput);
351 }
352
353 //////////////////////////////////////////////////////////////////////////////
354
UseDestColorAsInput(std::unique_ptr<GrFragmentProcessor> fp)355 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::UseDestColorAsInput(
356 std::unique_ptr<GrFragmentProcessor> fp) {
357 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForBlender, R"(
358 uniform colorFilter fp; // Declared as colorFilter so we can pass a color
359 half4 main(half4 src, half4 dst) {
360 return fp.eval(dst);
361 }
362 )");
363 return GrSkSLFP::Make(effect, "UseDestColorAsInput", /*inputFP=*/nullptr,
364 GrSkSLFP::OptFlags::kNone, "fp", std::move(fp));
365 }
366
367 //////////////////////////////////////////////////////////////////////////////
368
Compose(std::unique_ptr<GrFragmentProcessor> f,std::unique_ptr<GrFragmentProcessor> g)369 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose(
370 std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) {
371 class ComposeProcessor : public GrFragmentProcessor {
372 public:
373 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f,
374 std::unique_ptr<GrFragmentProcessor> g) {
375 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f),
376 std::move(g)));
377 }
378
379 const char* name() const override { return "Compose"; }
380
381 std::unique_ptr<GrFragmentProcessor> clone() const override {
382 return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this));
383 }
384
385 private:
386 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
387 class Impl : public ProgramImpl {
388 public:
389 void emitCode(EmitArgs& args) override {
390 SkString result = this->invokeChild(1, args); // g(x)
391 result = this->invokeChild(0, result.c_str(), args); // f(g(x))
392 args.fFragBuilder->codeAppendf("return %s;", result.c_str());
393 }
394 };
395 return std::make_unique<Impl>();
396 }
397
398 ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f,
399 std::unique_ptr<GrFragmentProcessor> g)
400 : INHERITED(kSeriesFragmentProcessor_ClassID,
401 f->optimizationFlags() & g->optimizationFlags()) {
402 this->registerChild(std::move(f));
403 this->registerChild(std::move(g));
404 }
405
406 ComposeProcessor(const ComposeProcessor& that) : INHERITED(that) {}
407
408 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
409
410 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
411
412 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
413 SkPMColor4f color = inColor;
414 color = ConstantOutputForConstantInput(this->childProcessor(1), color);
415 color = ConstantOutputForConstantInput(this->childProcessor(0), color);
416 return color;
417 }
418
419 using INHERITED = GrFragmentProcessor;
420 };
421
422 // Allow either of the composed functions to be null.
423 if (f == nullptr) {
424 return g;
425 }
426 if (g == nullptr) {
427 return f;
428 }
429
430 // Run an optimization pass on this composition.
431 GrProcessorAnalysisColor inputColor;
432 inputColor.setToUnknown();
433
434 std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(g), std::move(f)};
435 GrColorFragmentProcessorAnalysis info(inputColor, series, SK_ARRAY_COUNT(series));
436
437 SkPMColor4f knownColor;
438 int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
439 switch (leadingFPsToEliminate) {
440 default:
441 // We shouldn't eliminate more than we started with.
442 SkASSERT(leadingFPsToEliminate <= 2);
443 [[fallthrough]];
444 case 0:
445 // Compose the two processors as requested.
446 return ComposeProcessor::Make(/*f=*/std::move(series[1]), /*g=*/std::move(series[0]));
447 case 1:
448 // Replace the first processor with a constant color.
449 return ComposeProcessor::Make(/*f=*/std::move(series[1]),
450 /*g=*/MakeColor(knownColor));
451 case 2:
452 // Replace the entire composition with a constant color.
453 return MakeColor(knownColor);
454 }
455 }
456
457 //////////////////////////////////////////////////////////////////////////////
458
ColorMatrix(std::unique_ptr<GrFragmentProcessor> child,const float matrix[20],bool unpremulInput,bool clampRGBOutput,bool premulOutput)459 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ColorMatrix(
460 std::unique_ptr<GrFragmentProcessor> child,
461 const float matrix[20],
462 bool unpremulInput,
463 bool clampRGBOutput,
464 bool premulOutput) {
465 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
466 uniform half4x4 m;
467 uniform half4 v;
468 uniform int unpremulInput; // always specialized
469 uniform int clampRGBOutput; // always specialized
470 uniform int premulOutput; // always specialized
471 half4 main(half4 color) {
472 if (bool(unpremulInput)) {
473 color = unpremul(color);
474 }
475 color = m * color + v;
476 if (bool(clampRGBOutput)) {
477 color = saturate(color);
478 } else {
479 color.a = saturate(color.a);
480 }
481 if (bool(premulOutput)) {
482 color.rgb *= color.a;
483 }
484 return color;
485 }
486 )");
487 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
488
489 SkM44 m44(matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
490 matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8],
491 matrix[10], matrix[11], matrix[12], matrix[13],
492 matrix[15], matrix[16], matrix[17], matrix[18]);
493 SkV4 v4 = {matrix[4], matrix[9], matrix[14], matrix[19]};
494 return GrSkSLFP::Make(effect, "ColorMatrix", std::move(child), GrSkSLFP::OptFlags::kNone,
495 "m", m44,
496 "v", v4,
497 "unpremulInput", GrSkSLFP::Specialize(unpremulInput ? 1 : 0),
498 "clampRGBOutput", GrSkSLFP::Specialize(clampRGBOutput ? 1 : 0),
499 "premulOutput", GrSkSLFP::Specialize(premulOutput ? 1 : 0));
500 }
501
502 //////////////////////////////////////////////////////////////////////////////
503
SurfaceColor()504 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SurfaceColor() {
505 class SurfaceColorProcessor : public GrFragmentProcessor {
506 public:
507 static std::unique_ptr<GrFragmentProcessor> Make() {
508 return std::unique_ptr<GrFragmentProcessor>(new SurfaceColorProcessor());
509 }
510
511 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
512
513 const char* name() const override { return "SurfaceColor"; }
514
515 private:
516 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
517 class Impl : public ProgramImpl {
518 public:
519 void emitCode(EmitArgs& args) override {
520 const char* dstColor = args.fFragBuilder->dstColor();
521 args.fFragBuilder->codeAppendf("return %s;", dstColor);
522 }
523 };
524 return std::make_unique<Impl>();
525 }
526
527 SurfaceColorProcessor()
528 : INHERITED(kSurfaceColorProcessor_ClassID, kNone_OptimizationFlags) {
529 this->setWillReadDstColor();
530 }
531
532 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
533
534 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
535
536 using INHERITED = GrFragmentProcessor;
537 };
538
539 return SurfaceColorProcessor::Make();
540 }
541
542 //////////////////////////////////////////////////////////////////////////////
543
DeviceSpace(std::unique_ptr<GrFragmentProcessor> fp)544 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::DeviceSpace(
545 std::unique_ptr<GrFragmentProcessor> fp) {
546 if (!fp) {
547 return nullptr;
548 }
549
550 class DeviceSpace : GrFragmentProcessor {
551 public:
552 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
553 return std::unique_ptr<GrFragmentProcessor>(new DeviceSpace(std::move(fp)));
554 }
555
556 private:
557 DeviceSpace(std::unique_ptr<GrFragmentProcessor> fp)
558 : GrFragmentProcessor(kDeviceSpace_ClassID, fp->optimizationFlags()) {
559 // Passing FragCoord here is the reason this is a subclass and not a runtime-FP.
560 this->registerChild(std::move(fp), SkSL::SampleUsage::FragCoord());
561 }
562
563 std::unique_ptr<GrFragmentProcessor> clone() const override {
564 auto child = this->childProcessor(0)->clone();
565 return std::unique_ptr<GrFragmentProcessor>(new DeviceSpace(std::move(child)));
566 }
567
568 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& f) const override {
569 return this->childProcessor(0)->constantOutputForConstantInput(f);
570 }
571
572 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
573 class Impl : public ProgramImpl {
574 public:
575 Impl() = default;
576 void emitCode(ProgramImpl::EmitArgs& args) override {
577 auto child = this->invokeChild(0, args.fInputColor, args, "sk_FragCoord.xy");
578 args.fFragBuilder->codeAppendf("return %s;", child.c_str());
579 }
580 };
581 return std::make_unique<Impl>();
582 }
583
584 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
585
586 bool onIsEqual(const GrFragmentProcessor& processor) const override { return true; }
587
588 const char* name() const override { return "DeviceSpace"; }
589 };
590
591 return DeviceSpace::Make(std::move(fp));
592 }
593
594 //////////////////////////////////////////////////////////////////////////////
595
596 #define CLIP_EDGE_SKSL \
597 "const int kFillBW = 0;" \
598 "const int kFillAA = 1;" \
599 "const int kInverseFillBW = 2;" \
600 "const int kInverseFillAA = 3;"
601
602 static_assert(static_cast<int>(GrClipEdgeType::kFillBW) == 0);
603 static_assert(static_cast<int>(GrClipEdgeType::kFillAA) == 1);
604 static_assert(static_cast<int>(GrClipEdgeType::kInverseFillBW) == 2);
605 static_assert(static_cast<int>(GrClipEdgeType::kInverseFillAA) == 3);
606
Rect(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkRect rect)607 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Rect(
608 std::unique_ptr<GrFragmentProcessor> inputFP, GrClipEdgeType edgeType, SkRect rect) {
609 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, CLIP_EDGE_SKSL R"(
610 uniform int edgeType; // GrClipEdgeType, specialized
611 uniform float4 rectUniform;
612
613 half4 main(float2 xy, half4 inColor) {
614 half coverage;
615 if (edgeType == kFillBW || edgeType == kInverseFillBW) {
616 // non-AA
617 coverage = all(greaterThan(float4(sk_FragCoord.xy, rectUniform.zw),
618 float4(rectUniform.xy, sk_FragCoord.xy))) ? 1 : 0;
619 } else {
620 // compute coverage relative to left and right edges, add, then subtract 1 to
621 // account for double counting. And similar for top/bottom.
622 half4 dists4 = clamp(half4(1, 1, -1, -1) *
623 half4(sk_FragCoord.xyxy - rectUniform), 0, 1);
624 half2 dists2 = dists4.xy + dists4.zw - 1;
625 coverage = dists2.x * dists2.y;
626 }
627
628 if (edgeType == kInverseFillBW || edgeType == kInverseFillAA) {
629 coverage = 1.0 - coverage;
630 }
631
632 return inColor * coverage;
633 }
634 )");
635
636 SkASSERT(rect.isSorted());
637 // The AA math in the shader evaluates to 0 at the uploaded coordinates, so outset by 0.5
638 // to interpolate from 0 at a half pixel inset and 1 at a half pixel outset of rect.
639 SkRect rectUniform = GrClipEdgeTypeIsAA(edgeType) ? rect.makeOutset(.5f, .5f) : rect;
640
641 return GrSkSLFP::Make(effect, "Rect", std::move(inputFP),
642 GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
643 "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
644 "rectUniform", rectUniform);
645 }
646
Circle(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkPoint center,float radius)647 GrFPResult GrFragmentProcessor::Circle(std::unique_ptr<GrFragmentProcessor> inputFP,
648 GrClipEdgeType edgeType,
649 SkPoint center,
650 float radius) {
651 // A radius below half causes the implicit insetting done by this processor to become
652 // inverted. We could handle this case by making the processor code more complicated.
653 if (radius < .5f && GrClipEdgeTypeIsInverseFill(edgeType)) {
654 return GrFPFailure(std::move(inputFP));
655 }
656
657 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, CLIP_EDGE_SKSL R"(
658 uniform int edgeType; // GrClipEdgeType, specialized
659 // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular
660 // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills.
661 uniform float4 circle;
662
663 half4 main(float2 xy, half4 inColor) {
664 // TODO: Right now the distance to circle calculation is performed in a space normalized
665 // to the radius and then denormalized. This is to mitigate overflow on devices that
666 // don't have full float.
667 half d;
668 if (edgeType == kInverseFillBW || edgeType == kInverseFillAA) {
669 d = half((length((circle.xy - sk_FragCoord.xy) * circle.w) - 1.0) * circle.z);
670 } else {
671 d = half((1.0 - length((circle.xy - sk_FragCoord.xy) * circle.w)) * circle.z);
672 }
673 if (edgeType == kFillAA || edgeType == kInverseFillAA) {
674 return inColor * saturate(d);
675 } else {
676 return d > 0.5 ? inColor : half4(0);
677 }
678 }
679 )");
680
681 SkScalar effectiveRadius = radius;
682 if (GrClipEdgeTypeIsInverseFill(edgeType)) {
683 effectiveRadius -= 0.5f;
684 // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the shader.
685 effectiveRadius = std::max(0.001f, effectiveRadius);
686 } else {
687 effectiveRadius += 0.5f;
688 }
689 SkV4 circle = {center.fX, center.fY, effectiveRadius, SkScalarInvert(effectiveRadius)};
690
691 return GrFPSuccess(GrSkSLFP::Make(effect, "Circle", std::move(inputFP),
692 GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
693 "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
694 "circle", circle));
695 }
696
Ellipse(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkPoint center,SkPoint radii,const GrShaderCaps & caps)697 GrFPResult GrFragmentProcessor::Ellipse(std::unique_ptr<GrFragmentProcessor> inputFP,
698 GrClipEdgeType edgeType,
699 SkPoint center,
700 SkPoint radii,
701 const GrShaderCaps& caps) {
702 const bool medPrecision = !caps.floatIs32Bits();
703
704 // Small radii produce bad results on devices without full float.
705 if (medPrecision && (radii.fX < 0.5f || radii.fY < 0.5f)) {
706 return GrFPFailure(std::move(inputFP));
707 }
708 // Very narrow ellipses produce bad results on devices without full float
709 if (medPrecision && (radii.fX > 255*radii.fY || radii.fY > 255*radii.fX)) {
710 return GrFPFailure(std::move(inputFP));
711 }
712 // Very large ellipses produce bad results on devices without full float
713 if (medPrecision && (radii.fX > 16384 || radii.fY > 16384)) {
714 return GrFPFailure(std::move(inputFP));
715 }
716
717 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, CLIP_EDGE_SKSL R"(
718 uniform int edgeType; // GrClipEdgeType, specialized
719 uniform int medPrecision; // !sk_Caps.floatIs32Bits, specialized
720
721 uniform float4 ellipse;
722 uniform float2 scale; // only for medPrecision
723
724 half4 main(float2 xy, half4 inColor) {
725 // d is the offset to the ellipse center
726 float2 d = sk_FragCoord.xy - ellipse.xy;
727 // If we're on a device with a "real" mediump then we'll do the distance computation in
728 // a space that is normalized by the larger radius or 128, whichever is smaller. The
729 // scale uniform will be scale, 1/scale. The inverse squared radii uniform values are
730 // already in this normalized space. The center is not.
731 if (bool(medPrecision)) {
732 d *= scale.y;
733 }
734 float2 Z = d * ellipse.zw;
735 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1.
736 float implicit = dot(Z, d) - 1;
737 // grad_dot is the squared length of the gradient of the implicit.
738 float grad_dot = 4 * dot(Z, Z);
739 // Avoid calling inversesqrt on zero.
740 if (bool(medPrecision)) {
741 grad_dot = max(grad_dot, 6.1036e-5);
742 } else {
743 grad_dot = max(grad_dot, 1.1755e-38);
744 }
745 float approx_dist = implicit * inversesqrt(grad_dot);
746 if (bool(medPrecision)) {
747 approx_dist *= scale.x;
748 }
749
750 half alpha;
751 if (edgeType == kFillBW) {
752 alpha = approx_dist > 0.0 ? 0.0 : 1.0;
753 } else if (edgeType == kFillAA) {
754 alpha = saturate(0.5 - half(approx_dist));
755 } else if (edgeType == kInverseFillBW) {
756 alpha = approx_dist > 0.0 ? 1.0 : 0.0;
757 } else { // edgeType == kInverseFillAA
758 alpha = saturate(0.5 + half(approx_dist));
759 }
760 return inColor * alpha;
761 }
762 )");
763
764 float invRXSqd;
765 float invRYSqd;
766 SkV2 scale = {1, 1};
767 // If we're using a scale factor to work around precision issues, choose the larger radius as
768 // the scale factor. The inv radii need to be pre-adjusted by the scale factor.
769 if (medPrecision) {
770 if (radii.fX > radii.fY) {
771 invRXSqd = 1.f;
772 invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY);
773 scale = {radii.fX, 1.f / radii.fX};
774 } else {
775 invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX);
776 invRYSqd = 1.f;
777 scale = {radii.fY, 1.f / radii.fY};
778 }
779 } else {
780 invRXSqd = 1.f / (radii.fX * radii.fX);
781 invRYSqd = 1.f / (radii.fY * radii.fY);
782 }
783 SkV4 ellipse = {center.fX, center.fY, invRXSqd, invRYSqd};
784
785 return GrFPSuccess(GrSkSLFP::Make(effect, "Ellipse", std::move(inputFP),
786 GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
787 "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
788 "medPrecision", GrSkSLFP::Specialize<int>(medPrecision),
789 "ellipse", ellipse,
790 "scale", scale));
791 }
792
793 //////////////////////////////////////////////////////////////////////////////
794
HighPrecision(std::unique_ptr<GrFragmentProcessor> fp)795 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::HighPrecision(
796 std::unique_ptr<GrFragmentProcessor> fp) {
797 class HighPrecisionFragmentProcessor : public GrFragmentProcessor {
798 public:
799 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
800 return std::unique_ptr<GrFragmentProcessor>(
801 new HighPrecisionFragmentProcessor(std::move(fp)));
802 }
803
804 const char* name() const override { return "HighPrecision"; }
805
806 std::unique_ptr<GrFragmentProcessor> clone() const override {
807 return Make(this->childProcessor(0)->clone());
808 }
809
810 private:
811 HighPrecisionFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)
812 : INHERITED(kHighPrecisionFragmentProcessor_ClassID,
813 ProcessorOptimizationFlags(fp.get())) {
814 this->registerChild(std::move(fp));
815 }
816
817 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
818 class Impl : public ProgramImpl {
819 public:
820 void emitCode(EmitArgs& args) override {
821 SkString childColor = this->invokeChild(0, args);
822
823 args.fFragBuilder->forceHighPrecision();
824 args.fFragBuilder->codeAppendf("return %s;", childColor.c_str());
825 }
826 };
827 return std::make_unique<Impl>();
828 }
829
830 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
831 bool onIsEqual(const GrFragmentProcessor& other) const override { return true; }
832
833 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
834 return ConstantOutputForConstantInput(this->childProcessor(0), input);
835 }
836
837 using INHERITED = GrFragmentProcessor;
838 };
839
840 return HighPrecisionFragmentProcessor::Make(std::move(fp));
841 }
842
843 //////////////////////////////////////////////////////////////////////////////
844
845 using ProgramImpl = GrFragmentProcessor::ProgramImpl;
846
setData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)847 void ProgramImpl::setData(const GrGLSLProgramDataManager& pdman,
848 const GrFragmentProcessor& processor) {
849 this->onSetData(pdman, processor);
850 }
851
invokeChild(int childIndex,const char * inputColor,const char * destColor,EmitArgs & args,std::string_view skslCoords)852 SkString ProgramImpl::invokeChild(int childIndex,
853 const char* inputColor,
854 const char* destColor,
855 EmitArgs& args,
856 std::string_view skslCoords) {
857 SkASSERT(childIndex >= 0);
858
859 if (!inputColor) {
860 inputColor = args.fInputColor;
861 }
862
863 const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex);
864 if (!childProc) {
865 // If no child processor is provided, return the input color as-is.
866 return SkString(inputColor);
867 }
868
869 auto invocation = SkStringPrintf("%s(%s", this->childProcessor(childIndex)->functionName(),
870 inputColor);
871
872 if (childProc->isBlendFunction()) {
873 if (!destColor) {
874 destColor = args.fFp.isBlendFunction() ? args.fDestColor : "half4(1)";
875 }
876 invocation.appendf(", %s", destColor);
877 }
878
879 // Assert that the child has no sample matrix. A uniform matrix sample call would go through
880 // invokeChildWithMatrix, not here.
881 SkASSERT(!childProc->sampleUsage().isUniformMatrix());
882
883 if (args.fFragBuilder->getProgramBuilder()->fragmentProcessorHasCoordsParam(childProc)) {
884 SkASSERT(!childProc->sampleUsage().isFragCoord() || skslCoords == "sk_FragCoord.xy");
885 // The child's function takes a half4 color and a float2 coordinate
886 if (!skslCoords.empty()) {
887 invocation.appendf(", %.*s", (int)skslCoords.size(), skslCoords.data());
888 } else {
889 invocation.appendf(", %s", args.fSampleCoord);
890 }
891 }
892
893 invocation.append(")");
894 return invocation;
895 }
896
invokeChildWithMatrix(int childIndex,const char * inputColor,const char * destColor,EmitArgs & args)897 SkString ProgramImpl::invokeChildWithMatrix(int childIndex,
898 const char* inputColor,
899 const char* destColor,
900 EmitArgs& args) {
901 SkASSERT(childIndex >= 0);
902
903 if (!inputColor) {
904 inputColor = args.fInputColor;
905 }
906
907 const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex);
908 if (!childProc) {
909 // If no child processor is provided, return the input color as-is.
910 return SkString(inputColor);
911 }
912
913 SkASSERT(childProc->sampleUsage().isUniformMatrix());
914
915 // Every uniform matrix has the same (initial) name. Resolve that into the mangled name:
916 GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
917 args.fFp, SkString(SkSL::SampleUsage::MatrixUniformName()));
918 SkASSERT(uniform.getType() == SkSLType::kFloat3x3);
919 const SkString& matrixName(uniform.getName());
920
921 auto invocation = SkStringPrintf("%s(%s", this->childProcessor(childIndex)->functionName(),
922 inputColor);
923
924 if (childProc->isBlendFunction()) {
925 if (!destColor) {
926 destColor = args.fFp.isBlendFunction() ? args.fDestColor : "half4(1)";
927 }
928 invocation.appendf(", %s", destColor);
929 }
930
931 // Produce a string containing the call to the helper function. We have a uniform variable
932 // containing our transform (matrixName). If the parent coords were produced by uniform
933 // transforms, then the entire expression (matrixName * coords) is lifted to a vertex shader
934 // and is stored in a varying. In that case, childProc will not be sampled explicitly, so its
935 // function signature will not take in coords.
936 //
937 // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke
938 // the function.
939 if (args.fFragBuilder->getProgramBuilder()->fragmentProcessorHasCoordsParam(childProc)) {
940 // Only check perspective for this specific matrix transform, not the aggregate FP property.
941 // Any parent perspective will have already been applied when evaluated in the FS.
942 if (childProc->sampleUsage().hasPerspective()) {
943 invocation.appendf(", proj((%s) * %s.xy1)", matrixName.c_str(), args.fSampleCoord);
944 } else if (args.fShaderCaps->nonsquareMatrixSupport()) {
945 invocation.appendf(", float3x2(%s) * %s.xy1", matrixName.c_str(), args.fSampleCoord);
946 } else {
947 invocation.appendf(", ((%s) * %s.xy1).xy", matrixName.c_str(), args.fSampleCoord);
948 }
949 }
950
951 invocation.append(")");
952 return invocation;
953 }
954