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" 11 #include "GrContextOptions.h" 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 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".geom")) { 28 kind = SkSL::Program::kGeometry_Kind; 29 } else { 30 printf("input filename must end in '.vert', '.frag', or '.geom'\n"); 31 exit(1); 32 } 33 34 std::ifstream in(argv[1]); 35 std::string stdText((std::istreambuf_iterator<char>(in)), 36 std::istreambuf_iterator<char>()); 37 SkString text(stdText.c_str()); 38 if (in.rdstate()) { 39 printf("error reading '%s'\n", argv[1]); 40 exit(2); 41 } 42 SkSL::Program::Settings settings; 43 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); 44 settings.fCaps = caps.get(); 45 SkString name(argv[2]); 46 if (name.endsWith(".spirv")) { 47 SkFILEWStream out(argv[2]); 48 SkSL::Compiler compiler; 49 if (!out.isValid()) { 50 printf("error writing '%s'\n", argv[2]); 51 exit(4); 52 } 53 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); 54 if (!program || !compiler.toSPIRV(*program, out)) { 55 printf("%s", compiler.errorText().c_str()); 56 exit(3); 57 } 58 } else if (name.endsWith(".glsl")) { 59 SkFILEWStream out(argv[2]); 60 SkSL::Compiler compiler; 61 if (!out.isValid()) { 62 printf("error writing '%s'\n", argv[2]); 63 exit(4); 64 } 65 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings); 66 if (!program || !compiler.toGLSL(*program, out)) { 67 printf("%s", compiler.errorText().c_str()); 68 exit(3); 69 } 70 } else { 71 printf("expected output filename to end with '.spirv' or '.glsl'"); 72 } 73 } 74