blob: 9f1fec1f1ba2ff7de95296755cb3b57d38519885 [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
ethannicholasb3058bd2016-07-01 08:22:01 -07008#include <fstream>
9#include "SkSLCompiler.h"
Ethan Nicholas0df1b042017-03-31 13:56:23 -040010#include "SkSLFileOutputStream.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070011
Ethan Nicholas762466e2017-06-29 10:03:38 -040012// Given the path to a file (e.g. src/gpu/effects/GrFooFragmentProcessor.fp) and the expected
13// filename prefix and suffix (e.g. "Gr" and ".fp"), returns the "base name" of the
14// file (in this case, 'FooFragmentProcessor'). If no match, returns the empty string.
15static SkSL::String base_name(const char* fpPath, const char* prefix, const char* suffix) {
16 SkSL::String result;
17 const char* end = fpPath + strlen(fpPath);
18 const char* fileName = end;
19 // back up until we find a slash
20 while (fileName != fpPath && '/' != *(fileName - 1) && '\\' != *(fileName - 1)) {
21 --fileName;
22 }
23 if (!strncmp(fileName, prefix, strlen(prefix)) &&
24 !strncmp(end - strlen(suffix), suffix, strlen(suffix))) {
25 result.append(fileName + strlen(prefix), end - fileName - strlen(prefix) - strlen(suffix));
26 }
27 return result;
28}
29
ethannicholasb3058bd2016-07-01 08:22:01 -070030/**
31 * Very simple standalone executable to facilitate testing.
32 */
33int main(int argc, const char** argv) {
34 if (argc != 3) {
35 printf("usage: skslc <input> <output>\n");
36 exit(1);
37 }
38 SkSL::Program::Kind kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040039 SkSL::String input(argv[1]);
40 if (input.endsWith(".vert")) {
ethannicholasb3058bd2016-07-01 08:22:01 -070041 kind = SkSL::Program::kVertex_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040042 } else if (input.endsWith(".frag")) {
ethannicholasb3058bd2016-07-01 08:22:01 -070043 kind = SkSL::Program::kFragment_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040044 } else if (input.endsWith(".geom")) {
Ethan Nicholas52cad152017-02-16 16:37:32 -050045 kind = SkSL::Program::kGeometry_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040046 } else if (input.endsWith(".fp")) {
47 kind = SkSL::Program::kFragmentProcessor_Kind;
ethannicholasb3058bd2016-07-01 08:22:01 -070048 } else {
Ethan Nicholas762466e2017-06-29 10:03:38 -040049 printf("input filename must end in '.vert', '.frag', '.geom', or '.fp'\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070050 exit(1);
51 }
52
53 std::ifstream in(argv[1]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050054 std::string stdText((std::istreambuf_iterator<char>(in)),
55 std::istreambuf_iterator<char>());
Ethan Nicholas0df1b042017-03-31 13:56:23 -040056 SkSL::String text(stdText.c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -070057 if (in.rdstate()) {
58 printf("error reading '%s'\n", argv[1]);
59 exit(2);
60 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050061 SkSL::Program::Settings settings;
Ethan Nicholas762466e2017-06-29 10:03:38 -040062 settings.fArgs.insert(std::make_pair("gpImplementsDistanceVector", 1));
Ethan Nicholas0df1b042017-03-31 13:56:23 -040063 SkSL::String name(argv[2]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050064 if (name.endsWith(".spirv")) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040065 SkSL::FileOutputStream out(argv[2]);
ethannicholas5961bc92016-10-12 06:39:56 -070066 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050067 if (!out.isValid()) {
68 printf("error writing '%s'\n", argv[2]);
69 exit(4);
70 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050071 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
72 if (!program || !compiler.toSPIRV(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070073 printf("%s", compiler.errorText().c_str());
74 exit(3);
75 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040076 if (!out.close()) {
77 printf("error writing '%s'\n", argv[2]);
78 exit(4);
79 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050080 } else if (name.endsWith(".glsl")) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040081 SkSL::FileOutputStream out(argv[2]);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050082 SkSL::Compiler compiler;
83 if (!out.isValid()) {
ethannicholas5961bc92016-10-12 06:39:56 -070084 printf("error writing '%s'\n", argv[2]);
85 exit(4);
86 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050087 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
88 if (!program || !compiler.toGLSL(*program, out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070089 printf("%s", compiler.errorText().c_str());
90 exit(3);
91 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040092 if (!out.close()) {
93 printf("error writing '%s'\n", argv[2]);
94 exit(4);
95 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040096 } else if (name.endsWith(".h")) {
97 SkSL::FileOutputStream out(argv[2]);
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040098 SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
Ethan Nicholas762466e2017-06-29 10:03:38 -040099 if (!out.isValid()) {
100 printf("error writing '%s'\n", argv[2]);
101 exit(4);
102 }
103 settings.fReplaceSettings = false;
104 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
105 if (!program || !compiler.toH(*program, base_name(argv[1], "Gr", ".fp"), out)) {
106 printf("%s", compiler.errorText().c_str());
107 exit(3);
108 }
109 if (!out.close()) {
110 printf("error writing '%s'\n", argv[2]);
111 exit(4);
112 }
113 } else if (name.endsWith(".cpp")) {
114 SkSL::FileOutputStream out(argv[2]);
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400115 SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400116 if (!out.isValid()) {
117 printf("error writing '%s'\n", argv[2]);
118 exit(4);
119 }
120 settings.fReplaceSettings = false;
121 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
122 if (!program || !compiler.toCPP(*program, base_name(argv[1], "Gr", ".fp"), out)) {
123 printf("%s", compiler.errorText().c_str());
124 exit(3);
125 }
126 if (!out.close()) {
127 printf("error writing '%s'\n", argv[2]);
128 exit(4);
129 }
ethannicholas5961bc92016-10-12 06:39:56 -0700130 } else {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400131 printf("expected output filename to end with '.spirv', '.glsl', '.cpp', or '.h'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 }
133}