blob: 381fcf08d48ce434040f857751ca44b9ed64db5e [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;
27 } else {
28 printf("input filename must end in '.vert' or '.frag'\n");
29 exit(1);
30 }
31
32 std::ifstream in(argv[1]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050033 std::string stdText((std::istreambuf_iterator<char>(in)),
34 std::istreambuf_iterator<char>());
35 SkString text(stdText.c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -070036 if (in.rdstate()) {
37 printf("error reading '%s'\n", argv[1]);
38 exit(2);
39 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050040 SkString name(argv[2]);
41 if (name.endsWith(".spirv")) {
42 SkFILEWStream out(argv[2]);
ethannicholas5961bc92016-10-12 06:39:56 -070043 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050044 if (!out.isValid()) {
45 printf("error writing '%s'\n", argv[2]);
46 exit(4);
47 }
Greg Daniele8e4a3e2016-12-12 17:20:42 +000048 if (!compiler.toSPIRV(kind, text, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070049 printf("%s", compiler.errorText().c_str());
50 exit(3);
51 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050052 } else if (name.endsWith(".glsl")) {
53 SkFILEWStream out(argv[2]);
54 SkSL::Compiler compiler;
55 if (!out.isValid()) {
ethannicholas5961bc92016-10-12 06:39:56 -070056 printf("error writing '%s'\n", argv[2]);
57 exit(4);
58 }
Greg Daniele8e4a3e2016-12-12 17:20:42 +000059 if (!compiler.toGLSL(kind, text, *SkSL::ShaderCapsFactory::Default(), out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070060 printf("%s", compiler.errorText().c_str());
61 exit(3);
62 }
ethannicholas5961bc92016-10-12 06:39:56 -070063 } else {
64 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070065 }
66}