Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 1 | /* |
| 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 Osman | be1b837 | 2020-06-18 13:40:26 -0400 | [diff] [blame] | 10 | #include "include/effects/SkRuntimeEffect.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 12 | #include "src/gpu/SkGr.h" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/ops/GrFillRectOp.h" |
| 15 | #include "tools/ToolUtils.h" |
| 16 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 17 | // Samples child with a uniform matrix (functionally identical to GrMatrixEffect) |
| 18 | // Scales along Y |
| 19 | class UniformMatrixEffect : public GrFragmentProcessor { |
| 20 | public: |
| 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 Osman | b1e9cb0 | 2021-05-21 12:03:51 -0400 | [diff] [blame] | 25 | this->registerChild(std::move(child), |
| 26 | SkSL::SampleUsage::UniformMatrix(/*hasPerspective=*/false)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 27 | } |
| 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 Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 34 | std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 35 | class Impl : public GrGLSLFragmentProcessor { |
| 36 | void emitCode(EmitArgs& args) override { |
Brian Osman | b1e9cb0 | 2021-05-21 12:03:51 -0400 | [diff] [blame] | 37 | fMatrixVar = |
| 38 | args.fUniformHandler->addUniform(&args.fFp, |
| 39 | kFragment_GrShaderFlag, |
| 40 | kFloat3x3_GrSLType, |
| 41 | SkSL::SampleUsage::MatrixUniformName()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 42 | SkString sample = this->invokeChildWithMatrix(0, args); |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame] | 43 | args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 44 | } |
| 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 Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 51 | return std::make_unique<Impl>(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 55 | // Samples child with explicit coords |
| 56 | // Translates along Y |
| 57 | class ExplicitCoordEffect : public GrFragmentProcessor { |
| 58 | public: |
| 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 Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 63 | this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 64 | 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 Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 72 | std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 73 | 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 Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame] | 78 | args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 79 | } |
| 80 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 81 | return std::make_unique<Impl>(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | // Generates test pattern |
| 86 | class TestPatternEffect : public GrFragmentProcessor { |
| 87 | public: |
| 88 | static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 7; |
| 89 | |
| 90 | TestPatternEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) { |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 91 | this->setUsesSampleCoordsDirectly(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 92 | } |
| 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 Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 99 | std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 100 | 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 Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame] | 105 | fb->codeAppendf("return half2(coord).rg01;\n"); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 106 | } |
| 107 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 108 | return std::make_unique<Impl>(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 109 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | SkBitmap 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 | |
| 135 | enum EffectType { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 136 | kUniform, |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 137 | kExplicit, |
| 138 | }; |
| 139 | |
| 140 | static std::unique_ptr<GrFragmentProcessor> wrap(std::unique_ptr<GrFragmentProcessor> fp, |
| 141 | EffectType effectType) { |
| 142 | switch (effectType) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 143 | case kUniform: |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 144 | return std::make_unique<UniformMatrixEffect>(std::move(fp)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 145 | case kExplicit: |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 146 | return std::make_unique<ExplicitCoordEffect>(std::move(fp)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 147 | } |
| 148 | SkUNREACHABLE; |
| 149 | } |
| 150 | |
Brian Osman | b1e9cb0 | 2021-05-21 12:03:51 -0400 | [diff] [blame] | 151 | DEF_SIMPLE_GPU_GM(fp_sample_chaining, ctx, rtCtx, canvas, 232, 232) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 152 | SkBitmap bmp = make_test_bitmap(); |
| 153 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 154 | 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 Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 166 | auto view = std::get<0>(GrMakeCachedBitmapProxyView(ctx, bmp, GrMipmapped::kNo)); |
| 167 | auto fp = GrTextureEffect::Make(std::move(view), bmp.alphaType()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 168 | #endif |
| 169 | for (EffectType effectType : effects) { |
| 170 | fp = wrap(std::move(fp), effectType); |
| 171 | } |
| 172 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 173 | paint.setColorFragmentProcessor(std::move(fp)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 174 | 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 Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 185 | draw({ kUniform }); // Scale Y axis by 2x (2 visible rows) |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 186 | draw({ kExplicit }); // Translate up by 8px |
| 187 | nextRow(); |
| 188 | |
| 189 | // Second row: transform duplicated |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 190 | draw({ kUniform, kUniform }); // Scale Y axis by 4x (1 visible row) |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 191 | draw({ kExplicit, kExplicit }); // Translate up by 16px |
| 192 | nextRow(); |
| 193 | |
| 194 | // Remember, these are applied inside out: |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 195 | draw({ kUniform, kExplicit }); // Scale Y by 2x and translate up by 8px |
Brian Osman | b1e9cb0 | 2021-05-21 12:03:51 -0400 | [diff] [blame] | 196 | draw({ kExplicit, kUniform }); // Scale Y by 2x and translate up by 16px |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 197 | } |