blob: a9cff5b507f343e2011464e052754abc5b625d0a [file] [log] [blame]
Ethan Nicholas570506d2021-02-11 13:19:38 -05001/*
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
18class SimpleDSLEffect : public GrFragmentProcessor {
19public:
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 Salomon18ab2032021-02-23 10:07:05 -050030 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
Ethan Nicholas570506d2021-02-11 13:19:38 -050031 class Impl : public GrGLSLFragmentProcessor {
32 void emitCode(EmitArgs& args) override {
33 using namespace SkSL::dsl;
34 StartFragmentProcessor(this, &args);
Ethan Nicholas41130572021-03-02 15:38:19 -050035
36 // Test for skbug.com/11384
37 Var x(kInt);
38 Declare(x, 1);
39 SkASSERT(DSLWriter::Var(x).initialValue()->description() == "1");
40
Ethan Nicholas11a15b12021-02-11 15:56:27 -050041 Var blueAlpha(kUniform_Modifier, kHalf2);
42 fBlueAlphaUniform = VarUniformHandle(blueAlpha);
Ethan Nicholas570506d2021-02-11 13:19:38 -050043 Var coords(kFloat4);
Ethan Nicholas3af656b2021-02-17 11:17:56 -050044 Declare(coords, sk_FragCoord());
45 Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha));
Ethan Nicholas570506d2021-02-11 13:19:38 -050046 EndFragmentProcessor();
47 }
Ethan Nicholas11a15b12021-02-11 15:56:27 -050048
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 Nicholas570506d2021-02-11 13:19:38 -050055 };
Brian Salomon18ab2032021-02-23 10:07:05 -050056 return std::make_unique<Impl>();
Ethan Nicholas570506d2021-02-11 13:19:38 -050057 }
58};
59
60DEF_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}