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 | 9bd301d | 2017-03-31 16:04:34 +0000 | [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; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 27 | } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".geom")) { |
| 28 | kind = SkSL::Program::kGeometry_Kind; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | } else { |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 30 | printf("input filename must end in '.vert', '.frag', or '.geom'\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | exit(1); |
| 32 | } |
| 33 | |
| 34 | std::ifstream in(argv[1]); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 35 | std::string stdText((std::istreambuf_iterator<char>(in)), |
| 36 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | 9bd301d | 2017-03-31 16:04:34 +0000 | [diff] [blame] | 37 | SkString text(stdText.c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | if (in.rdstate()) { |
| 39 | printf("error reading '%s'\n", argv[1]); |
| 40 | exit(2); |
| 41 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 42 | SkSL::Program::Settings settings; |
Ethan Nicholas | 9bd301d | 2017-03-31 16:04:34 +0000 | [diff] [blame] | 43 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 44 | settings.fCaps = caps.get(); |
| 45 | SkString name(argv[2]); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 46 | if (name.endsWith(".spirv")) { |
Ethan Nicholas | 9bd301d | 2017-03-31 16:04:34 +0000 | [diff] [blame] | 47 | SkFILEWStream out(argv[2]); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 48 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 49 | if (!out.isValid()) { |
| 50 | printf("error writing '%s'\n", argv[2]); |
| 51 | exit(4); |
| 52 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 53 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); |
| 54 | if (!program || !compiler.toSPIRV(*program, out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 55 | printf("%s", compiler.errorText().c_str()); |
| 56 | exit(3); |
| 57 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 58 | } else if (name.endsWith(".glsl")) { |
Ethan Nicholas | 9bd301d | 2017-03-31 16:04:34 +0000 | [diff] [blame] | 59 | SkFILEWStream out(argv[2]); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 60 | SkSL::Compiler compiler; |
| 61 | if (!out.isValid()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 62 | printf("error writing '%s'\n", argv[2]); |
| 63 | exit(4); |
| 64 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 65 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); |
| 66 | if (!program || !compiler.toGLSL(*program, out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 67 | printf("%s", compiler.errorText().c_str()); |
| 68 | exit(3); |
| 69 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 70 | } else { |
| 71 | printf("expected output filename to end with '.spirv' or '.glsl'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 72 | } |
| 73 | } |