Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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" |
| 10 | #include "include/effects/SkRuntimeEffect.h" |
| 11 | #include "src/gpu/GrBitmapTextureMaker.h" |
| 12 | #include "src/gpu/GrDirectContextPriv.h" |
| 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/ops/GrFillRectOp.h" |
| 15 | #include "src/sksl/dsl/priv/DSLFPs.h" |
| 16 | #include "tools/ToolUtils.h" |
| 17 | |
| 18 | class SimpleDSLEffect : public GrFragmentProcessor { |
| 19 | public: |
| 20 | static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 100; |
| 21 | |
| 22 | SimpleDSLEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) { |
| 23 | } |
| 24 | |
| 25 | const char* name() const override { return "DSLEffect"; } |
| 26 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 27 | bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
| 28 | std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; } |
| 29 | |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 30 | std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override { |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 31 | class Impl : public GrGLSLFragmentProcessor { |
| 32 | void emitCode(EmitArgs& args) override { |
| 33 | using namespace SkSL::dsl; |
| 34 | StartFragmentProcessor(this, &args); |
Ethan Nicholas | 4113057 | 2021-03-02 15:38:19 -0500 | [diff] [blame^] | 35 | |
| 36 | // Test for skbug.com/11384 |
| 37 | Var x(kInt); |
| 38 | Declare(x, 1); |
| 39 | SkASSERT(DSLWriter::Var(x).initialValue()->description() == "1"); |
| 40 | |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 41 | Var blueAlpha(kUniform_Modifier, kHalf2); |
| 42 | fBlueAlphaUniform = VarUniformHandle(blueAlpha); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 43 | Var coords(kFloat4); |
Ethan Nicholas | 3af656b | 2021-02-17 11:17:56 -0500 | [diff] [blame] | 44 | Declare(coords, sk_FragCoord()); |
| 45 | Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha)); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 46 | EndFragmentProcessor(); |
| 47 | } |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 48 | |
| 49 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 50 | const GrFragmentProcessor& effect) override { |
| 51 | pdman.set2f(fBlueAlphaUniform, 0.0, 1.0); |
| 52 | } |
| 53 | |
| 54 | GrGLSLProgramDataManager::UniformHandle fBlueAlphaUniform; |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 55 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 56 | return std::make_unique<Impl>(); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
| 60 | DEF_SIMPLE_GPU_GM(simple_dsl_test, ctx, rtCtx, canvas, 100, 100) { |
| 61 | auto fp = std::make_unique<SimpleDSLEffect>(); |
| 62 | GrPaint paint; |
| 63 | paint.setColorFragmentProcessor(std::move(fp)); |
| 64 | rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 65 | SkRect::MakeIWH(100, 100)); |
| 66 | } |