blob: baffd8bcf8ae9c66f308918771babc017bd6fc43 [file] [log] [blame]
John Stilesdb62d082020-09-23 14:08:40 -04001/*
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
19static 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 != "");
33 }
34}
35
36static void test(skiatest::Reporter* r, const GrShaderCaps& caps, const char* src,
37 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
38 SkSL::Program::Settings settings;
39 settings.fCaps = &caps;
40 SkSL::Program::Inputs inputs;
41 test(r, settings, src, &inputs, kind);
42}
43
44DEF_TEST(SkSLGLSLTestbed, r) {
45 // Add in your SkSL here.
46 test(r,
47 *SkSL::ShaderCapsFactory::Default(),
48 R"__SkSL__(
49 void main() {
50 sk_FragColor = half4(0);
51 }
52 )__SkSL__");
53}