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" |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 9 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 10 | #include "src/core/SkCanvasPriv.h" |
Robert Phillips | f386862 | 2021-08-04 13:27:43 -0400 | [diff] [blame] | 11 | #include "src/gpu/SurfaceFillContext.h" |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 13 | #include "src/sksl/dsl/priv/DSLFPs.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 14 | #include "src/sksl/dsl/priv/DSLWriter.h" |
| 15 | #include "src/sksl/ir/SkSLVariable.h" |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 16 | |
| 17 | class SimpleDSLEffect : public GrFragmentProcessor { |
| 18 | public: |
| 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 Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 25 | void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 26 | bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
| 27 | std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; } |
| 28 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 29 | std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
| 30 | class Impl : public ProgramImpl { |
Brian Salomon | b25560a | 2021-08-10 13:56:13 -0400 | [diff] [blame] | 31 | public: |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 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 |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 37 | Var x(kInt_Type, 1); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 38 | Declare(x); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 39 | SkASSERT(DSLWriter::Var(x)->initialValue()->description() == "1"); |
Ethan Nicholas | 4113057 | 2021-03-02 15:38:19 -0500 | [diff] [blame] | 40 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 41 | GlobalVar blueAlpha(kUniform_Modifier, kHalf2_Type, "blueAlpha"); |
| 42 | Declare(blueAlpha); |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 43 | fBlueAlphaUniform = VarUniformHandle(blueAlpha); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 44 | Var coords(kFloat4_Type, sk_FragCoord()); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 45 | Declare(coords); |
Ethan Nicholas | 3af656b | 2021-02-17 11:17:56 -0500 | [diff] [blame] | 46 | Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha)); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 47 | EndFragmentProcessor(); |
| 48 | } |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 49 | |
Brian Salomon | b25560a | 2021-08-10 13:56:13 -0400 | [diff] [blame] | 50 | private: |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 51 | 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 Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 57 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 58 | return std::make_unique<Impl>(); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 59 | } |
| 60 | }; |
| 61 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 62 | DEF_SIMPLE_GPU_GM(simple_dsl_test, rContext, canvas, 100, 100) { |
| 63 | auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas); |
John Stiles | ab7ff17 | 2021-07-30 10:59:25 -0400 | [diff] [blame] | 64 | if (!sfc) { |
| 65 | return; |
| 66 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 67 | |
| 68 | sfc->fillWithFP(std::make_unique<SimpleDSLEffect>()); |
Ethan Nicholas | 570506d | 2021-02-11 13:19:38 -0500 | [diff] [blame] | 69 | } |