blob: f493b0577d652882bf912122c0d4ebea72da55dc [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas7ef4b742016-11-11 15:16:46 -050011#include "GrContextOptions.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070012
13/**
14 * Very simple standalone executable to facilitate testing.
15 */
16int 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 Nicholas9e1138d2016-11-21 10:39:35 -050033 std::string stdText((std::istreambuf_iterator<char>(in)),
34 std::istreambuf_iterator<char>());
35 SkString text(stdText.c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -070036 if (in.rdstate()) {
37 printf("error reading '%s'\n", argv[1]);
38 exit(2);
39 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050040 SkSL::Program::Settings settings;
41 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
42 settings.fCaps = caps.get();
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050043 SkString name(argv[2]);
44 if (name.endsWith(".spirv")) {
45 SkFILEWStream out(argv[2]);
ethannicholas5961bc92016-10-12 06:39:56 -070046 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050047 if (!out.isValid()) {
48 printf("error writing '%s'\n", argv[2]);
49 exit(4);
50 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050051 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
52 if (!program || !compiler.toSPIRV(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070053 printf("%s", compiler.errorText().c_str());
54 exit(3);
55 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050056 } else if (name.endsWith(".glsl")) {
57 SkFILEWStream out(argv[2]);
58 SkSL::Compiler compiler;
59 if (!out.isValid()) {
ethannicholas5961bc92016-10-12 06:39:56 -070060 printf("error writing '%s'\n", argv[2]);
61 exit(4);
62 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050063 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
64 if (!program || !compiler.toGLSL(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070065 printf("%s", compiler.errorText().c_str());
66 exit(3);
67 }
ethannicholas5961bc92016-10-12 06:39:56 -070068 } else {
69 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070070 }
71}