blob: 1461bf9aae215c407ae951aa40517e819aa8e9de [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 Nicholas0df1b042017-03-31 13:56:23 -040011#include "SkSLFileOutputStream.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>());
Ethan Nicholas0df1b042017-03-31 13:56:23 -040037 SkSL::String 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;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040043 SkSL::String name(argv[2]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050044 if (name.endsWith(".spirv")) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040045 SkSL::FileOutputStream out(argv[2]);
ethannicholas5961bc92016-10-12 06:39:56 -070046 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050047 if (!out.isValid()) {
48 printf("error writing '%s'\n", argv[2]);
49 exit(4);
50 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050051 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
52 if (!program || !compiler.toSPIRV(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070053 printf("%s", compiler.errorText().c_str());
54 exit(3);
55 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040056 if (!out.close()) {
57 printf("error writing '%s'\n", argv[2]);
58 exit(4);
59 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050060 } else if (name.endsWith(".glsl")) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040061 SkSL::FileOutputStream out(argv[2]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050062 SkSL::Compiler compiler;
63 if (!out.isValid()) {
ethannicholas5961bc92016-10-12 06:39:56 -070064 printf("error writing '%s'\n", argv[2]);
65 exit(4);
66 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050067 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
68 if (!program || !compiler.toGLSL(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070069 printf("%s", compiler.errorText().c_str());
70 exit(3);
71 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040072 if (!out.close()) {
73 printf("error writing '%s'\n", argv[2]);
74 exit(4);
75 }
ethannicholas5961bc92016-10-12 06:39:56 -070076 } else {
77 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070078 }
79}