blob: 0d65b107ecf28bc587bf03e1b9ee332eeb3bac1d [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 "SkSLCompiler.h"
9
10#include <fstream>
11#include <streambuf>
12
13#include "SkSLIRGenerator.h"
14#include "SkSLParser.h"
15#include "SkSLSPIRVCodeGenerator.h"
16#include "ir/SkSLExpression.h"
17#include "ir/SkSLIntLiteral.h"
18#include "ir/SkSLSymbolTable.h"
19#include "ir/SkSLVarDeclaration.h"
20#include "SkMutex.h"
21
22#define STRINGIFY(x) #x
23
24// include the built-in shader symbols as static strings
25
26static std::string SKSL_INCLUDE =
27#include "sksl.include"
28;
29
30static std::string SKSL_VERT_INCLUDE =
31#include "sksl_vert.include"
32;
33
34static std::string SKSL_FRAG_INCLUDE =
35#include "sksl_frag.include"
36;
37
38namespace SkSL {
39
40Compiler::Compiler()
41: fErrorCount(0) {
42 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this));
43 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this));
ethannicholasd598f792016-07-25 10:08:54 -070044 fIRGenerator = new IRGenerator(&fContext, symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070045 fTypes = types;
ethannicholasd598f792016-07-25 10:08:54 -070046 #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
47 fContext.f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070048 ADD_TYPE(Void);
49 ADD_TYPE(Float);
50 ADD_TYPE(Vec2);
51 ADD_TYPE(Vec3);
52 ADD_TYPE(Vec4);
53 ADD_TYPE(Double);
54 ADD_TYPE(DVec2);
55 ADD_TYPE(DVec3);
56 ADD_TYPE(DVec4);
57 ADD_TYPE(Int);
58 ADD_TYPE(IVec2);
59 ADD_TYPE(IVec3);
60 ADD_TYPE(IVec4);
61 ADD_TYPE(UInt);
62 ADD_TYPE(UVec2);
63 ADD_TYPE(UVec3);
64 ADD_TYPE(UVec4);
65 ADD_TYPE(Bool);
66 ADD_TYPE(BVec2);
67 ADD_TYPE(BVec3);
68 ADD_TYPE(BVec4);
69 ADD_TYPE(Mat2x2);
70 ADD_TYPE(Mat2x3);
71 ADD_TYPE(Mat2x4);
72 ADD_TYPE(Mat3x2);
73 ADD_TYPE(Mat3x3);
74 ADD_TYPE(Mat3x4);
75 ADD_TYPE(Mat4x2);
76 ADD_TYPE(Mat4x3);
77 ADD_TYPE(Mat4x4);
78 ADD_TYPE(GenType);
79 ADD_TYPE(GenDType);
80 ADD_TYPE(GenIType);
81 ADD_TYPE(GenUType);
82 ADD_TYPE(GenBType);
83 ADD_TYPE(Mat);
84 ADD_TYPE(Vec);
85 ADD_TYPE(GVec);
86 ADD_TYPE(GVec2);
87 ADD_TYPE(GVec3);
88 ADD_TYPE(GVec4);
89 ADD_TYPE(DVec);
90 ADD_TYPE(IVec);
91 ADD_TYPE(UVec);
92 ADD_TYPE(BVec);
93
94 ADD_TYPE(Sampler1D);
95 ADD_TYPE(Sampler2D);
96 ADD_TYPE(Sampler3D);
97 ADD_TYPE(SamplerCube);
98 ADD_TYPE(Sampler2DRect);
99 ADD_TYPE(Sampler1DArray);
100 ADD_TYPE(Sampler2DArray);
101 ADD_TYPE(SamplerCubeArray);
102 ADD_TYPE(SamplerBuffer);
103 ADD_TYPE(Sampler2DMS);
104 ADD_TYPE(Sampler2DMSArray);
105
106 ADD_TYPE(GSampler1D);
107 ADD_TYPE(GSampler2D);
108 ADD_TYPE(GSampler3D);
109 ADD_TYPE(GSamplerCube);
110 ADD_TYPE(GSampler2DRect);
111 ADD_TYPE(GSampler1DArray);
112 ADD_TYPE(GSampler2DArray);
113 ADD_TYPE(GSamplerCubeArray);
114 ADD_TYPE(GSamplerBuffer);
115 ADD_TYPE(GSampler2DMS);
116 ADD_TYPE(GSampler2DMSArray);
117
118 ADD_TYPE(Sampler1DShadow);
119 ADD_TYPE(Sampler2DShadow);
120 ADD_TYPE(SamplerCubeShadow);
121 ADD_TYPE(Sampler2DRectShadow);
122 ADD_TYPE(Sampler1DArrayShadow);
123 ADD_TYPE(Sampler2DArrayShadow);
124 ADD_TYPE(SamplerCubeArrayShadow);
125 ADD_TYPE(GSampler2DArrayShadow);
126 ADD_TYPE(GSamplerCubeArrayShadow);
127
128 std::vector<std::unique_ptr<ProgramElement>> ignored;
129 this->internalConvertProgram(SKSL_INCLUDE, &ignored);
130 ASSERT(!fErrorCount);
131}
132
133Compiler::~Compiler() {
134 delete fIRGenerator;
135}
136
137void Compiler::internalConvertProgram(std::string text,
138 std::vector<std::unique_ptr<ProgramElement>>* result) {
139 Parser parser(text, *fTypes, *this);
140 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
141 if (fErrorCount) {
142 return;
143 }
144 for (size_t i = 0; i < parsed.size(); i++) {
145 ASTDeclaration& decl = *parsed[i];
146 switch (decl.fKind) {
147 case ASTDeclaration::kVar_Kind: {
148 std::unique_ptr<VarDeclaration> s = fIRGenerator->convertVarDeclaration(
149 (ASTVarDeclaration&) decl,
150 Variable::kGlobal_Storage);
151 if (s) {
152 result->push_back(std::move(s));
153 }
154 break;
155 }
156 case ASTDeclaration::kFunction_Kind: {
157 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
158 (ASTFunction&) decl);
159 if (f) {
160 result->push_back(std::move(f));
161 }
162 break;
163 }
164 case ASTDeclaration::kInterfaceBlock_Kind: {
165 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
166 (ASTInterfaceBlock&) decl);
167 if (i) {
168 result->push_back(std::move(i));
169 }
170 break;
171 }
172 case ASTDeclaration::kExtension_Kind: {
173 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
174 if (e) {
175 result->push_back(std::move(e));
176 }
177 break;
178 }
179 default:
180 ABORT("unsupported declaration: %s\n", decl.description().c_str());
181 }
182 }
183}
184
185std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::string text) {
186 fErrorText = "";
187 fErrorCount = 0;
188 fIRGenerator->pushSymbolTable();
ethannicholasd598f792016-07-25 10:08:54 -0700189 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -0700190 switch (kind) {
191 case Program::kVertex_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700192 this->internalConvertProgram(SKSL_VERT_INCLUDE, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700193 break;
194 case Program::kFragment_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700195 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196 break;
197 }
ethannicholasd598f792016-07-25 10:08:54 -0700198 this->internalConvertProgram(text, &elements);
199 auto result = std::unique_ptr<Program>(new Program(kind, std::move(elements),
200 fIRGenerator->fSymbolTable));;
ethannicholasb3058bd2016-07-01 08:22:01 -0700201 fIRGenerator->popSymbolTable();
202 this->writeErrorCount();
ethannicholasd598f792016-07-25 10:08:54 -0700203 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700204}
205
206void Compiler::error(Position position, std::string msg) {
207 fErrorCount++;
208 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
209}
210
211std::string Compiler::errorText() {
212 std::string result = fErrorText;
213 return result;
214}
215
216void Compiler::writeErrorCount() {
217 if (fErrorCount) {
218 fErrorText += to_string(fErrorCount) + " error";
219 if (fErrorCount > 1) {
220 fErrorText += "s";
221 }
222 fErrorText += "\n";
223 }
224}
225
226#include <fstream>
227bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out) {
228 auto program = this->convertProgram(kind, text);
229 if (fErrorCount == 0) {
ethannicholasd598f792016-07-25 10:08:54 -0700230 SkSL::SPIRVCodeGenerator cg(&fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -0700231 cg.generateCode(*program.get(), out);
232 ASSERT(!out.rdstate());
233 }
234 return fErrorCount == 0;
235}
236
237bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) {
238 std::stringstream buffer;
239 bool result = this->toSPIRV(kind, text, buffer);
240 if (result) {
241 *out = buffer.str();
242 }
243 return fErrorCount == 0;
244}
245
246} // namespace