| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "stdio.h" | 8 #include "stdio.h" |
| 9 #include <fstream> | 9 #include <fstream> |
| 10 #include "SkSLCompiler.h" | 10 #include "SkSLCompiler.h" |
| 11 | 11 |
| 12 bool 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(), en
ding)); | |
| 15 } | |
| 16 return false; | |
| 17 } | |
| 18 | |
| 19 static 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 | |
| 30 /** | 12 /** |
| 31 * Very simple standalone executable to facilitate testing. | 13 * Very simple standalone executable to facilitate testing. |
| 32 */ | 14 */ |
| 33 int main(int argc, const char** argv) { | 15 int main(int argc, const char** argv) { |
| 34 if (argc != 3) { | 16 if (argc != 3) { |
| 35 printf("usage: skslc <input> <output>\n"); | 17 printf("usage: skslc <input> <output>\n"); |
| 36 exit(1); | 18 exit(1); |
| 37 } | 19 } |
| 38 SkSL::Program::Kind kind; | 20 SkSL::Program::Kind kind; |
| 39 size_t len = strlen(argv[1]); | 21 size_t len = strlen(argv[1]); |
| 40 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".vert")) { | 22 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".vert")) { |
| 41 kind = SkSL::Program::kVertex_Kind; | 23 kind = SkSL::Program::kVertex_Kind; |
| 42 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".frag")) { | 24 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".frag")) { |
| 43 kind = SkSL::Program::kFragment_Kind; | 25 kind = SkSL::Program::kFragment_Kind; |
| 44 } else { | 26 } else { |
| 45 printf("input filename must end in '.vert' or '.frag'\n"); | 27 printf("input filename must end in '.vert' or '.frag'\n"); |
| 46 exit(1); | 28 exit(1); |
| 47 } | 29 } |
| 48 | 30 |
| 49 std::ifstream in(argv[1]); | 31 std::ifstream in(argv[1]); |
| 50 std::string text((std::istreambuf_iterator<char>(in)), | 32 std::string text((std::istreambuf_iterator<char>(in)), |
| 51 std::istreambuf_iterator<char>()); | 33 std::istreambuf_iterator<char>()); |
| 52 if (in.rdstate()) { | 34 if (in.rdstate()) { |
| 53 printf("error reading '%s'\n", argv[1]); | 35 printf("error reading '%s'\n", argv[1]); |
| 54 exit(2); | 36 exit(2); |
| 55 } | 37 } |
| 56 std::string name(argv[2]); | 38 std::ofstream out(argv[2], std::ofstream::binary); |
| 57 if (endsWith(name, ".spirv")) { | 39 SkSL::Compiler compiler; |
| 58 std::ofstream out(argv[2], std::ofstream::binary); | 40 if (!compiler.toSPIRV(kind, text, out)) { |
| 59 SkSL::Compiler compiler; | 41 printf("%s", compiler.errorText().c_str()); |
| 60 if (!compiler.toSPIRV(kind, text, out)) { | 42 exit(3); |
| 61 printf("%s", compiler.errorText().c_str()); | 43 } |
| 62 exit(3); | 44 if (out.rdstate()) { |
| 63 } | 45 printf("error writing '%s'\n", argv[2]); |
| 64 if (out.rdstate()) { | 46 exit(4); |
| 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'"); | |
| 81 } | 47 } |
| 82 } | 48 } |
| OLD | NEW |