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 | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 40 | SkSL::Program::Settings settings; |
| 41 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 42 | settings.fCaps = caps.get(); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 43 | SkString name(argv[2]); |
| 44 | if (name.endsWith(".spirv")) { |
| 45 | SkFILEWStream out(argv[2]); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 46 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 47 | if (!out.isValid()) { |
| 48 | printf("error writing '%s'\n", argv[2]); |
| 49 | exit(4); |
| 50 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 51 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); |
| 52 | if (!program || !compiler.toSPIRV(*program, out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 53 | printf("%s", compiler.errorText().c_str()); |
| 54 | exit(3); |
| 55 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 56 | } else if (name.endsWith(".glsl")) { |
| 57 | SkFILEWStream out(argv[2]); |
| 58 | SkSL::Compiler compiler; |
| 59 | if (!out.isValid()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 60 | printf("error writing '%s'\n", argv[2]); |
| 61 | exit(4); |
| 62 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 63 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); |
| 64 | if (!program || !compiler.toGLSL(*program, out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 65 | printf("%s", compiler.errorText().c_str()); |
| 66 | exit(3); |
| 67 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 68 | } else { |
| 69 | printf("expected output filename to end with '.spirv' or '.glsl'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 70 | } |
| 71 | } |