blob: 680915e9d7009a23eb1871254cbb8ed2377f8a44 [file] [log] [blame]
Michael Ludwige88320b2020-06-24 09:04:56 -04001/*
2 * Copyright 2019 Google LLC.
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 "gm/gm.h"
9#include "include/core/SkFont.h"
Brian Osmanbe1b8372020-06-18 13:40:26 -040010#include "include/effects/SkRuntimeEffect.h"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon27c42022021-04-28 12:39:21 -040012#include "src/gpu/SkGr.h"
Michael Ludwige88320b2020-06-24 09:04:56 -040013#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/ops/GrFillRectOp.h"
15#include "tools/ToolUtils.h"
16
Michael Ludwige88320b2020-06-24 09:04:56 -040017// Samples child with a uniform matrix (functionally identical to GrMatrixEffect)
18// Scales along Y
19class UniformMatrixEffect : public GrFragmentProcessor {
20public:
21 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 4;
22
23 UniformMatrixEffect(std::unique_ptr<GrFragmentProcessor> child)
24 : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
Brian Osmanb1e9cb02021-05-21 12:03:51 -040025 this->registerChild(std::move(child),
26 SkSL::SampleUsage::UniformMatrix(/*hasPerspective=*/false));
Michael Ludwige88320b2020-06-24 09:04:56 -040027 }
28
29 const char* name() const override { return "UniformMatrixEffect"; }
30 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
31 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
32 std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
33
Brian Salomon18ab2032021-02-23 10:07:05 -050034 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
Michael Ludwige88320b2020-06-24 09:04:56 -040035 class Impl : public GrGLSLFragmentProcessor {
36 void emitCode(EmitArgs& args) override {
Brian Osmanb1e9cb02021-05-21 12:03:51 -040037 fMatrixVar =
38 args.fUniformHandler->addUniform(&args.fFp,
39 kFragment_GrShaderFlag,
40 kFloat3x3_GrSLType,
41 SkSL::SampleUsage::MatrixUniformName());
Michael Ludwige88320b2020-06-24 09:04:56 -040042 SkString sample = this->invokeChildWithMatrix(0, args);
John Stiles4bdc1212020-12-14 18:04:13 -050043 args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str());
Michael Ludwige88320b2020-06-24 09:04:56 -040044 }
45 void onSetData(const GrGLSLProgramDataManager& pdman,
46 const GrFragmentProcessor& proc) override {
47 pdman.setSkMatrix(fMatrixVar, SkMatrix::Scale(1, 0.5f));
48 }
49 UniformHandle fMatrixVar;
50 };
Brian Salomon18ab2032021-02-23 10:07:05 -050051 return std::make_unique<Impl>();
Michael Ludwige88320b2020-06-24 09:04:56 -040052 }
53};
54
Michael Ludwige88320b2020-06-24 09:04:56 -040055// Samples child with explicit coords
56// Translates along Y
57class ExplicitCoordEffect : public GrFragmentProcessor {
58public:
59 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 6;
60
61 ExplicitCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
62 : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
Brian Osman1298bc42020-06-30 13:39:35 -040063 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Michael Ludwige88320b2020-06-24 09:04:56 -040064 this->setUsesSampleCoordsDirectly();
65 }
66
67 const char* name() const override { return "ExplicitCoordEffect"; }
68 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
69 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
70 std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
71
Brian Salomon18ab2032021-02-23 10:07:05 -050072 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
Michael Ludwige88320b2020-06-24 09:04:56 -040073 class Impl : public GrGLSLFragmentProcessor {
74 void emitCode(EmitArgs& args) override {
75 args.fFragBuilder->codeAppendf("float2 coord = %s + float2(0, 8);",
76 args.fSampleCoord);
77 SkString sample = this->invokeChild(0, args, "coord");
John Stiles4bdc1212020-12-14 18:04:13 -050078 args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str());
Michael Ludwige88320b2020-06-24 09:04:56 -040079 }
80 };
Brian Salomon18ab2032021-02-23 10:07:05 -050081 return std::make_unique<Impl>();
Michael Ludwige88320b2020-06-24 09:04:56 -040082 }
83};
84
85// Generates test pattern
86class TestPatternEffect : public GrFragmentProcessor {
87public:
88 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 7;
89
90 TestPatternEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
Michael Ludwigfbe28592020-06-26 16:02:15 -040091 this->setUsesSampleCoordsDirectly();
Michael Ludwige88320b2020-06-24 09:04:56 -040092 }
93
94 const char* name() const override { return "TestPatternEffect"; }
95 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
96 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
97 std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
98
Brian Salomon18ab2032021-02-23 10:07:05 -050099 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
Michael Ludwige88320b2020-06-24 09:04:56 -0400100 class Impl : public GrGLSLFragmentProcessor {
101 void emitCode(EmitArgs& args) override {
102 auto fb = args.fFragBuilder;
103 fb->codeAppendf("float2 coord = %s / 64.0;", args.fSampleCoord);
104 fb->codeAppendf("coord = floor(coord * 4) / 3;");
John Stiles4bdc1212020-12-14 18:04:13 -0500105 fb->codeAppendf("return half2(coord).rg01;\n");
Michael Ludwige88320b2020-06-24 09:04:56 -0400106 }
107 };
Brian Salomon18ab2032021-02-23 10:07:05 -0500108 return std::make_unique<Impl>();
Michael Ludwige88320b2020-06-24 09:04:56 -0400109 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400110};
111
112SkBitmap make_test_bitmap() {
113 SkBitmap bitmap;
114 bitmap.allocN32Pixels(64, 64);
115 SkCanvas canvas(bitmap);
116
117 SkFont font(ToolUtils::create_portable_typeface());
118 const char* alpha = "ABCDEFGHIJKLMNOP";
119
120 for (int i = 0; i < 16; ++i) {
121 int tx = i % 4,
122 ty = i / 4;
123 int x = tx * 16,
124 y = ty * 16;
125 SkPaint paint;
126 paint.setColor4f({ tx / 3.0f, ty / 3.0f, 0.0f, 1.0f });
127 canvas.drawRect(SkRect::MakeXYWH(x, y, 16, 16), paint);
128 paint.setColor4f({ (3-tx) / 3.0f, (3-ty)/3.0f, 1.0f, 1.0f });
129 canvas.drawSimpleText(alpha + i, 1, SkTextEncoding::kUTF8, x + 3, y + 13, font, paint);
130 }
131
132 return bitmap;
133}
134
135enum EffectType {
Michael Ludwige88320b2020-06-24 09:04:56 -0400136 kUniform,
Michael Ludwige88320b2020-06-24 09:04:56 -0400137 kExplicit,
138};
139
140static std::unique_ptr<GrFragmentProcessor> wrap(std::unique_ptr<GrFragmentProcessor> fp,
141 EffectType effectType) {
142 switch (effectType) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400143 case kUniform:
John Stilesfbd050b2020-08-03 13:21:46 -0400144 return std::make_unique<UniformMatrixEffect>(std::move(fp));
Michael Ludwige88320b2020-06-24 09:04:56 -0400145 case kExplicit:
John Stilesfbd050b2020-08-03 13:21:46 -0400146 return std::make_unique<ExplicitCoordEffect>(std::move(fp));
Michael Ludwige88320b2020-06-24 09:04:56 -0400147 }
148 SkUNREACHABLE;
149}
150
Brian Osmanb1e9cb02021-05-21 12:03:51 -0400151DEF_SIMPLE_GPU_GM(fp_sample_chaining, ctx, rtCtx, canvas, 232, 232) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400152 SkBitmap bmp = make_test_bitmap();
153
Michael Ludwige88320b2020-06-24 09:04:56 -0400154 int x = 10, y = 10;
155
156 auto nextCol = [&] { x += (64 + 10); };
157 auto nextRow = [&] { x = 10; y += (64 + 10); };
158
159 auto draw = [&](std::initializer_list<EffectType> effects) {
160 // Enable TestPatternEffect to get a fully procedural inner effect. It's not quite as nice
161 // visually (no text labels in each box), but it avoids the extra GrMatrixEffect.
162 // Switching it on actually triggers *more* shader compilation failures.
163#if 0
164 auto fp = std::unique_ptr<GrFragmentProcessor>(new TestPatternEffect());
165#else
Brian Salomon27c42022021-04-28 12:39:21 -0400166 auto view = std::get<0>(GrMakeCachedBitmapProxyView(ctx, bmp, GrMipmapped::kNo));
167 auto fp = GrTextureEffect::Make(std::move(view), bmp.alphaType());
Michael Ludwige88320b2020-06-24 09:04:56 -0400168#endif
169 for (EffectType effectType : effects) {
170 fp = wrap(std::move(fp), effectType);
171 }
172 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -0400173 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwige88320b2020-06-24 09:04:56 -0400174 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(x, y),
175 SkRect::MakeIWH(64, 64));
176 nextCol();
177 };
178
179 // Reminder, in every case, the chain is more complicated than it seems, because the
180 // GrTextureEffect is wrapped in a GrMatrixEffect, which is subject to the same bugs that
181 // we're testing (particularly the bug about owner/base in UniformMatrixEffect).
182
183 // First row: no transform, then each one independently applied
184 draw({}); // Identity (4 rows and columns)
Michael Ludwige88320b2020-06-24 09:04:56 -0400185 draw({ kUniform }); // Scale Y axis by 2x (2 visible rows)
Michael Ludwige88320b2020-06-24 09:04:56 -0400186 draw({ kExplicit }); // Translate up by 8px
187 nextRow();
188
189 // Second row: transform duplicated
Michael Ludwige88320b2020-06-24 09:04:56 -0400190 draw({ kUniform, kUniform }); // Scale Y axis by 4x (1 visible row)
Michael Ludwige88320b2020-06-24 09:04:56 -0400191 draw({ kExplicit, kExplicit }); // Translate up by 16px
192 nextRow();
193
194 // Remember, these are applied inside out:
Michael Ludwige88320b2020-06-24 09:04:56 -0400195 draw({ kUniform, kExplicit }); // Scale Y by 2x and translate up by 8px
Brian Osmanb1e9cb02021-05-21 12:03:51 -0400196 draw({ kExplicit, kUniform }); // Scale Y by 2x and translate up by 16px
Michael Ludwige88320b2020-06-24 09:04:56 -0400197}