blob: 46e9c187571eb2c03a57ee914801059908a9c46c [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
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 "stdio.h"
9#include <fstream>
10#include "SkSLCompiler.h"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050011#include "GrContextOptions.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070012
13/**
14 * Very simple standalone executable to facilitate testing.
15 */
16int main(int argc, const char** argv) {
17 if (argc != 3) {
18 printf("usage: skslc <input> <output>\n");
19 exit(1);
20 }
21 SkSL::Program::Kind kind;
22 size_t len = strlen(argv[1]);
23 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".vert")) {
24 kind = SkSL::Program::kVertex_Kind;
25 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".frag")) {
26 kind = SkSL::Program::kFragment_Kind;
Ethan Nicholas52cad152017-02-16 16:37:32 -050027 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".geom")) {
28 kind = SkSL::Program::kGeometry_Kind;
ethannicholasb3058bd2016-07-01 08:22:01 -070029 } else {
Ethan Nicholas52cad152017-02-16 16:37:32 -050030 printf("input filename must end in '.vert', '.frag', or '.geom'\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070031 exit(1);
32 }
33
34 std::ifstream in(argv[1]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050035 std::string stdText((std::istreambuf_iterator<char>(in)),
36 std::istreambuf_iterator<char>());
37 SkString text(stdText.c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -070038 if (in.rdstate()) {
39 printf("error reading '%s'\n", argv[1]);
40 exit(2);
41 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050042 SkSL::Program::Settings settings;
43 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
44 settings.fCaps = caps.get();
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050045 SkString name(argv[2]);
46 if (name.endsWith(".spirv")) {
47 SkFILEWStream out(argv[2]);
ethannicholas5961bc92016-10-12 06:39:56 -070048 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050049 if (!out.isValid()) {
50 printf("error writing '%s'\n", argv[2]);
51 exit(4);
52 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050053 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
54 if (!program || !compiler.toSPIRV(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070055 printf("%s", compiler.errorText().c_str());
56 exit(3);
57 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050058 } else if (name.endsWith(".glsl")) {
59 SkFILEWStream out(argv[2]);
60 SkSL::Compiler compiler;
61 if (!out.isValid()) {
ethannicholas5961bc92016-10-12 06:39:56 -070062 printf("error writing '%s'\n", argv[2]);
63 exit(4);
64 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050065 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
66 if (!program || !compiler.toGLSL(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070067 printf("%s", compiler.errorText().c_str());
68 exit(3);
69 }
ethannicholas5961bc92016-10-12 06:39:56 -070070 } else {
71 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070072 }
73}