ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 11 | #include "GrContextOptions.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * Very simple standalone executable to facilitate testing. |
| 15 | */ |
| 16 | int 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 Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 33 | std::string stdText((std::istreambuf_iterator<char>(in)), |
| 34 | std::istreambuf_iterator<char>()); |
| 35 | SkString text(stdText.c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | if (in.rdstate()) { |
| 37 | printf("error reading '%s'\n", argv[1]); |
| 38 | exit(2); |
| 39 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 40 | SkString name(argv[2]); |
| 41 | if (name.endsWith(".spirv")) { |
| 42 | SkFILEWStream out(argv[2]); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 43 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 44 | if (!out.isValid()) { |
| 45 | printf("error writing '%s'\n", argv[2]); |
| 46 | exit(4); |
| 47 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 48 | if (!compiler.toSPIRV(kind, text, out)) { |
| 49 | printf("%s", compiler.errorText().c_str()); |
| 50 | exit(3); |
| 51 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 52 | } else if (name.endsWith(".glsl")) { |
| 53 | SkFILEWStream out(argv[2]); |
| 54 | SkSL::Compiler compiler; |
| 55 | if (!out.isValid()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 56 | printf("error writing '%s'\n", argv[2]); |
| 57 | exit(4); |
| 58 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 59 | if (!compiler.toGLSL(kind, text, *SkSL::GLSLCapsFactory::Default(), out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 60 | printf("%s", compiler.errorText().c_str()); |
| 61 | exit(3); |
| 62 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 63 | } else { |
| 64 | printf("expected output filename to end with '.spirv' or '.glsl'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 65 | } |
| 66 | } |