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