blob: 3658992412ce54e301721c44d9bda14ad90b00e4 [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
ethannicholas5961bc92016-10-12 06:39:56 -070013bool endsWith(const std::string& s, const std::string& ending) {
14 if (s.length() >= ending.length()) {
15 return (0 == s.compare(s.length() - ending.length(), ending.length(), ending));
16 }
17 return false;
18}
19
ethannicholasb3058bd2016-07-01 08:22:01 -070020/**
21 * Very simple standalone executable to facilitate testing.
22 */
23int main(int argc, const char** argv) {
24 if (argc != 3) {
25 printf("usage: skslc <input> <output>\n");
26 exit(1);
27 }
28 SkSL::Program::Kind kind;
29 size_t len = strlen(argv[1]);
30 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".vert")) {
31 kind = SkSL::Program::kVertex_Kind;
32 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".frag")) {
33 kind = SkSL::Program::kFragment_Kind;
34 } else {
35 printf("input filename must end in '.vert' or '.frag'\n");
36 exit(1);
37 }
38
39 std::ifstream in(argv[1]);
40 std::string text((std::istreambuf_iterator<char>(in)),
41 std::istreambuf_iterator<char>());
42 if (in.rdstate()) {
43 printf("error reading '%s'\n", argv[1]);
44 exit(2);
45 }
ethannicholas5961bc92016-10-12 06:39:56 -070046 std::string name(argv[2]);
47 if (endsWith(name, ".spirv")) {
48 std::ofstream out(argv[2], std::ofstream::binary);
49 SkSL::Compiler compiler;
50 if (!compiler.toSPIRV(kind, text, out)) {
51 printf("%s", compiler.errorText().c_str());
52 exit(3);
53 }
54 if (out.rdstate()) {
55 printf("error writing '%s'\n", argv[2]);
56 exit(4);
57 }
58 } else if (endsWith(name, ".glsl")) {
59 std::ofstream out(argv[2], std::ofstream::binary);
60 SkSL::Compiler compiler;
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050061 if (!compiler.toGLSL(kind, text, *SkSL::GLSLCapsFactory::Default(), out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070062 printf("%s", compiler.errorText().c_str());
63 exit(3);
64 }
65 if (out.rdstate()) {
66 printf("error writing '%s'\n", argv[2]);
67 exit(4);
68 }
69 } else {
70 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070071 }
72}