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 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 13 | bool 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 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Very simple standalone executable to facilitate testing. |
| 22 | */ |
| 23 | int 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 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 46 | 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 Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame^] | 61 | if (!compiler.toGLSL(kind, text, *SkSL::GLSLCapsFactory::Default(), out)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 62 | 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'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | } |
| 72 | } |