John Stiles | db62d08 | 2020-09-23 14:08:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "src/sksl/SkSLCompiler.h" |
| 9 | |
| 10 | #include "tests/Test.h" |
| 11 | |
| 12 | // Note that the optimizer will aggressively kill dead code and substitute constants in place of |
| 13 | // variables, so we have to jump through a few hoops to ensure that the code in these tests has the |
| 14 | // necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being |
| 15 | // smart enough to optimize around certain constructs; as the optimizer gets smarter it will |
| 16 | // undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is |
| 17 | // equivalent! |
| 18 | |
| 19 | static void test(skiatest::Reporter* r, const SkSL::Program::Settings& settings, const char* src, |
| 20 | SkSL::Program::Inputs* inputs, |
| 21 | SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
| 22 | SkSL::Compiler compiler; |
| 23 | SkSL::String output; |
| 24 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src), |
| 25 | settings); |
| 26 | if (!program) { |
| 27 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 28 | REPORTER_ASSERT(r, program); |
| 29 | } else { |
| 30 | *inputs = program->fInputs; |
| 31 | REPORTER_ASSERT(r, compiler.toGLSL(*program, &output)); |
| 32 | REPORTER_ASSERT(r, output != ""); |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 33 | //SkDebugf("GLSL output:\n\n%s", output.c_str()); |
John Stiles | db62d08 | 2020-09-23 14:08:40 -0400 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | static void test(skiatest::Reporter* r, const GrShaderCaps& caps, const char* src, |
| 38 | SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
| 39 | SkSL::Program::Settings settings; |
| 40 | settings.fCaps = ∩︀ |
| 41 | SkSL::Program::Inputs inputs; |
| 42 | test(r, settings, src, &inputs, kind); |
| 43 | } |
| 44 | |
| 45 | DEF_TEST(SkSLGLSLTestbed, r) { |
| 46 | // Add in your SkSL here. |
| 47 | test(r, |
| 48 | *SkSL::ShaderCapsFactory::Default(), |
| 49 | R"__SkSL__( |
| 50 | void main() { |
| 51 | sk_FragColor = half4(0); |
| 52 | } |
| 53 | )__SkSL__"); |
| 54 | } |