blob: b14fe08ab022160194ffbedfeb41fc96770db143 [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"
Ethan Nicholas570506d2021-02-11 13:19:38 -05009#include "include/effects/SkRuntimeEffect.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040010#include "src/core/SkCanvasPriv.h"
Robert Phillipsf3868622021-08-04 13:27:43 -040011#include "src/gpu/SurfaceFillContext.h"
Ethan Nicholas570506d2021-02-11 13:19:38 -050012#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholas570506d2021-02-11 13:19:38 -050013#include "src/sksl/dsl/priv/DSLFPs.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -050014#include "src/sksl/dsl/priv/DSLWriter.h"
15#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholas570506d2021-02-11 13:19:38 -050016
17class SimpleDSLEffect : public GrFragmentProcessor {
18public:
19 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 100;
20
21 SimpleDSLEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
22 }
23
24 const char* name() const override { return "DSLEffect"; }
Brian Salomon13b28732021-08-06 15:33:58 -040025 void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Ethan Nicholas570506d2021-02-11 13:19:38 -050026 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
27 std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
28
Brian Salomon3176e862021-08-09 11:23:04 -040029 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
30 class Impl : public ProgramImpl {
Brian Salomonb25560a2021-08-10 13:56:13 -040031 public:
Ethan Nicholas570506d2021-02-11 13:19:38 -050032 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
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040037 Var x(kInt_Type, 1);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050038 Declare(x);
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040039 SkASSERT(DSLWriter::Var(x)->initialValue()->description() == "1");
Ethan Nicholas41130572021-03-02 15:38:19 -050040
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040041 GlobalVar blueAlpha(kUniform_Modifier, kHalf2_Type, "blueAlpha");
42 Declare(blueAlpha);
Ethan Nicholas11a15b12021-02-11 15:56:27 -050043 fBlueAlphaUniform = VarUniformHandle(blueAlpha);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040044 Var coords(kFloat4_Type, sk_FragCoord());
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050045 Declare(coords);
Ethan Nicholas3af656b2021-02-17 11:17:56 -050046 Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha));
Ethan Nicholas570506d2021-02-11 13:19:38 -050047 EndFragmentProcessor();
48 }
Ethan Nicholas11a15b12021-02-11 15:56:27 -050049
Brian Salomonb25560a2021-08-10 13:56:13 -040050 private:
Ethan Nicholas11a15b12021-02-11 15:56:27 -050051 void onSetData(const GrGLSLProgramDataManager& pdman,
52 const GrFragmentProcessor& effect) override {
53 pdman.set2f(fBlueAlphaUniform, 0.0, 1.0);
54 }
55
56 GrGLSLProgramDataManager::UniformHandle fBlueAlphaUniform;
Ethan Nicholas570506d2021-02-11 13:19:38 -050057 };
Brian Salomon18ab2032021-02-23 10:07:05 -050058 return std::make_unique<Impl>();
Ethan Nicholas570506d2021-02-11 13:19:38 -050059 }
60};
61
Robert Phillips7a0d3c32021-07-21 15:39:51 -040062DEF_SIMPLE_GPU_GM(simple_dsl_test, rContext, canvas, 100, 100) {
63 auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
John Stilesab7ff172021-07-30 10:59:25 -040064 if (!sfc) {
65 return;
66 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -040067
68 sfc->fillWithFP(std::make_unique<SimpleDSLEffect>());
Ethan Nicholas570506d2021-02-11 13:19:38 -050069}