blob: ea87e999385aac562df23ca81aa7222161646d92 [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 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#include "SkSLCompiler.h"
9
ethannicholas5961bc92016-10-12 06:39:56 -070010#include "ast/SkSLASTPrecision.h"
ethannicholas22f939e2016-10-13 13:25:34 -070011#include "SkSLCFGGenerator.h"
Ethan Nicholas941e7e22016-12-12 15:33:30 -050012#include "SkSLGLSLCodeGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070013#include "SkSLIRGenerator.h"
14#include "SkSLParser.h"
15#include "SkSLSPIRVCodeGenerator.h"
16#include "ir/SkSLExpression.h"
17#include "ir/SkSLIntLiteral.h"
ethannicholas5961bc92016-10-12 06:39:56 -070018#include "ir/SkSLModifiersDeclaration.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070019#include "ir/SkSLSymbolTable.h"
ethannicholasddb37d62016-10-20 09:54:00 -070020#include "ir/SkSLUnresolvedFunction.h"
ethannicholas22f939e2016-10-13 13:25:34 -070021#include "ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070022
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040023#ifdef SK_ENABLE_SPIRV_VALIDATION
24#include "spirv-tools/libspirv.hpp"
25#endif
26
ethannicholasb3058bd2016-07-01 08:22:01 -070027#define STRINGIFY(x) #x
28
29// include the built-in shader symbols as static strings
30
ethannicholas5961bc92016-10-12 06:39:56 -070031static const char* SKSL_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070032#include "sksl.include"
33;
34
ethannicholas5961bc92016-10-12 06:39:56 -070035static const char* SKSL_VERT_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070036#include "sksl_vert.include"
37;
38
ethannicholas5961bc92016-10-12 06:39:56 -070039static const char* SKSL_FRAG_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070040#include "sksl_frag.include"
41;
42
Ethan Nicholas52cad152017-02-16 16:37:32 -050043static const char* SKSL_GEOM_INCLUDE =
44#include "sksl_geom.include"
45;
46
ethannicholasb3058bd2016-07-01 08:22:01 -070047namespace SkSL {
48
Mike Klein6ad99092016-10-26 10:35:22 -040049Compiler::Compiler()
ethannicholasb3058bd2016-07-01 08:22:01 -070050: fErrorCount(0) {
Ethan Nicholas8feeff92017-03-30 14:11:58 -040051 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
52 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
ethannicholasd598f792016-07-25 10:08:54 -070053 fIRGenerator = new IRGenerator(&fContext, symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070054 fTypes = types;
ethannicholasd598f792016-07-25 10:08:54 -070055 #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
56 fContext.f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070057 ADD_TYPE(Void);
58 ADD_TYPE(Float);
59 ADD_TYPE(Vec2);
60 ADD_TYPE(Vec3);
61 ADD_TYPE(Vec4);
62 ADD_TYPE(Double);
63 ADD_TYPE(DVec2);
64 ADD_TYPE(DVec3);
65 ADD_TYPE(DVec4);
66 ADD_TYPE(Int);
67 ADD_TYPE(IVec2);
68 ADD_TYPE(IVec3);
69 ADD_TYPE(IVec4);
70 ADD_TYPE(UInt);
71 ADD_TYPE(UVec2);
72 ADD_TYPE(UVec3);
73 ADD_TYPE(UVec4);
74 ADD_TYPE(Bool);
75 ADD_TYPE(BVec2);
76 ADD_TYPE(BVec3);
77 ADD_TYPE(BVec4);
78 ADD_TYPE(Mat2x2);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040079 types->addWithoutOwnership(String("mat2x2"), fContext.fMat2x2_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070080 ADD_TYPE(Mat2x3);
81 ADD_TYPE(Mat2x4);
82 ADD_TYPE(Mat3x2);
83 ADD_TYPE(Mat3x3);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040084 types->addWithoutOwnership(String("mat3x3"), fContext.fMat3x3_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070085 ADD_TYPE(Mat3x4);
86 ADD_TYPE(Mat4x2);
87 ADD_TYPE(Mat4x3);
88 ADD_TYPE(Mat4x4);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040089 types->addWithoutOwnership(String("mat4x4"), fContext.fMat4x4_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070090 ADD_TYPE(GenType);
91 ADD_TYPE(GenDType);
92 ADD_TYPE(GenIType);
93 ADD_TYPE(GenUType);
94 ADD_TYPE(GenBType);
95 ADD_TYPE(Mat);
96 ADD_TYPE(Vec);
97 ADD_TYPE(GVec);
98 ADD_TYPE(GVec2);
99 ADD_TYPE(GVec3);
100 ADD_TYPE(GVec4);
101 ADD_TYPE(DVec);
102 ADD_TYPE(IVec);
103 ADD_TYPE(UVec);
104 ADD_TYPE(BVec);
105
106 ADD_TYPE(Sampler1D);
107 ADD_TYPE(Sampler2D);
108 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700109 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 ADD_TYPE(SamplerCube);
111 ADD_TYPE(Sampler2DRect);
112 ADD_TYPE(Sampler1DArray);
113 ADD_TYPE(Sampler2DArray);
114 ADD_TYPE(SamplerCubeArray);
115 ADD_TYPE(SamplerBuffer);
116 ADD_TYPE(Sampler2DMS);
117 ADD_TYPE(Sampler2DMSArray);
118
Brian Salomonbf7b6202016-11-11 16:08:03 -0500119 ADD_TYPE(ISampler2D);
120
Brian Salomon2a51de82016-11-16 12:06:01 -0500121 ADD_TYPE(Image2D);
122 ADD_TYPE(IImage2D);
123
Greg Daniel64773e62016-11-22 09:44:03 -0500124 ADD_TYPE(SubpassInput);
125 ADD_TYPE(SubpassInputMS);
126
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 ADD_TYPE(GSampler1D);
128 ADD_TYPE(GSampler2D);
129 ADD_TYPE(GSampler3D);
130 ADD_TYPE(GSamplerCube);
131 ADD_TYPE(GSampler2DRect);
132 ADD_TYPE(GSampler1DArray);
133 ADD_TYPE(GSampler2DArray);
134 ADD_TYPE(GSamplerCubeArray);
135 ADD_TYPE(GSamplerBuffer);
136 ADD_TYPE(GSampler2DMS);
137 ADD_TYPE(GSampler2DMSArray);
138
139 ADD_TYPE(Sampler1DShadow);
140 ADD_TYPE(Sampler2DShadow);
141 ADD_TYPE(SamplerCubeShadow);
142 ADD_TYPE(Sampler2DRectShadow);
143 ADD_TYPE(Sampler1DArrayShadow);
144 ADD_TYPE(Sampler2DArrayShadow);
145 ADD_TYPE(SamplerCubeArrayShadow);
146 ADD_TYPE(GSampler2DArrayShadow);
147 ADD_TYPE(GSamplerCubeArrayShadow);
148
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400149 String skCapsName("sk_Caps");
150 Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName,
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500151 *fContext.fSkCaps_Type, Variable::kGlobal_Storage);
152 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
153
ethannicholas5961bc92016-10-12 06:39:56 -0700154 Modifiers::Flag ignored1;
155 std::vector<std::unique_ptr<ProgramElement>> ignored2;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400156 this->internalConvertProgram(String(SKSL_INCLUDE), &ignored1, &ignored2);
ethannicholasddb37d62016-10-20 09:54:00 -0700157 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholasb3058bd2016-07-01 08:22:01 -0700158 ASSERT(!fErrorCount);
159}
160
161Compiler::~Compiler() {
162 delete fIRGenerator;
163}
164
ethannicholas22f939e2016-10-13 13:25:34 -0700165// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500166void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
167 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700168 switch (lvalue->fKind) {
169 case Expression::kVariableReference_Kind: {
170 const Variable& var = ((VariableReference*) lvalue)->fVariable;
171 if (var.fStorage == Variable::kLocal_Storage) {
172 (*definitions)[&var] = expr;
173 }
174 break;
175 }
176 case Expression::kSwizzle_Kind:
177 // We consider the variable written to as long as at least some of its components have
178 // been written to. This will lead to some false negatives (we won't catch it if you
179 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400180 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
181 // but since we pass foo as a whole it is flagged as an error) unless we perform a much
ethannicholas22f939e2016-10-13 13:25:34 -0700182 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400183 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500184 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700185 definitions);
186 break;
187 case Expression::kIndex_Kind:
188 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400189 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500190 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700191 definitions);
192 break;
193 case Expression::kFieldAccess_Kind:
194 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400195 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500196 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700197 definitions);
198 break;
199 default:
200 // not an lvalue, can't happen
201 ASSERT(false);
202 }
203}
204
205// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400206void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500207 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700208 switch (node.fKind) {
209 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500210 ASSERT(node.fExpression);
211 const Expression* expr = (Expression*) node.fExpression->get();
212 switch (expr->fKind) {
213 case Expression::kBinary_Kind: {
214 BinaryExpression* b = (BinaryExpression*) expr;
215 if (b->fOperator == Token::EQ) {
216 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
217 } else if (Token::IsAssignment(b->fOperator)) {
218 this->addDefinition(
219 b->fLeft.get(),
220 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
221 definitions);
222
223 }
224 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700225 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500226 case Expression::kPrefix_Kind: {
227 const PrefixExpression* p = (PrefixExpression*) expr;
228 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
229 this->addDefinition(
230 p->fOperand.get(),
231 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
232 definitions);
233 }
234 break;
235 }
236 case Expression::kPostfix_Kind: {
237 const PostfixExpression* p = (PostfixExpression*) expr;
238 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
239 this->addDefinition(
240 p->fOperand.get(),
241 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
242 definitions);
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000243
Ethan Nicholas86a43402017-01-19 13:32:00 -0500244 }
245 break;
246 }
247 default:
248 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700249 }
250 break;
251 }
252 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000253 const Statement* stmt = (Statement*) node.fStatement;
ethannicholas22f939e2016-10-13 13:25:34 -0700254 if (stmt->fKind == Statement::kVarDeclarations_Kind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500255 VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000256 for (VarDeclaration& decl : vd->fDeclaration->fVars) {
257 if (decl.fValue) {
258 (*definitions)[decl.fVar] = &decl.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700259 }
260 }
261 }
262 break;
263 }
264 }
265}
266
267void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
268 BasicBlock& block = cfg->fBlocks[blockId];
269
270 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500271 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700272 for (const BasicBlock::Node& n : block.fNodes) {
273 this->addDefinitions(n, &after);
274 }
275
276 // propagate definitions to exits
277 for (BlockId exitId : block.fExits) {
278 BasicBlock& exit = cfg->fBlocks[exitId];
279 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500280 std::unique_ptr<Expression>* e1 = pair.second;
281 auto found = exit.fBefore.find(pair.first);
282 if (found == exit.fBefore.end()) {
283 // exit has no definition for it, just copy it
284 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700285 exit.fBefore[pair.first] = e1;
286 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500287 // exit has a (possibly different) value already defined
288 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700289 if (e1 != e2) {
290 // definition has changed, merge and add exit block to worklist
291 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500292 if (e1 && e2) {
293 exit.fBefore[pair.first] =
Ethan Nicholas86a43402017-01-19 13:32:00 -0500294 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500295 } else {
296 exit.fBefore[pair.first] = nullptr;
297 }
ethannicholas22f939e2016-10-13 13:25:34 -0700298 }
299 }
300 }
301 }
302}
303
304// returns a map which maps all local variables in the function to null, indicating that their value
305// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500306static DefinitionMap compute_start_state(const CFG& cfg) {
307 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400308 for (const auto& block : cfg.fBlocks) {
309 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700310 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500311 ASSERT(node.fStatement);
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000312 const Statement* s = node.fStatement;
ethannicholas22f939e2016-10-13 13:25:34 -0700313 if (s->fKind == Statement::kVarDeclarations_Kind) {
314 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000315 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
316 result[decl.fVar] = nullptr;
Mike Klein6ad99092016-10-26 10:35:22 -0400317 }
ethannicholas22f939e2016-10-13 13:25:34 -0700318 }
319 }
320 }
321 }
322 return result;
323}
324
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000325void Compiler::scanCFG(const FunctionDefinition& f) {
326 CFG cfg = CFGGenerator().getCFG(f);
ethannicholas22f939e2016-10-13 13:25:34 -0700327
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000328 // compute the data flow
329 cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700330 std::set<BlockId> workList;
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000331 for (BlockId i = 0; i < cfg.fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700332 workList.insert(i);
333 }
334 while (workList.size()) {
335 BlockId next = *workList.begin();
336 workList.erase(workList.begin());
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000337 this->scanCFG(&cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700338 }
339
340 // check for unreachable code
341 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -0400342 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -0700343 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500344 Position p;
345 switch (cfg.fBlocks[i].fNodes[0].fKind) {
346 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000347 p = cfg.fBlocks[i].fNodes[0].fStatement->fPosition;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500348 break;
349 case BasicBlock::Node::kExpression_Kind:
350 p = (*cfg.fBlocks[i].fNodes[0].fExpression)->fPosition;
351 break;
352 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400353 this->error(p, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -0700354 }
355 }
356 if (fErrorCount) {
357 return;
358 }
359
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000360 // check for undefined variables, perform constant propagation
361 for (BasicBlock& b : cfg.fBlocks) {
362 DefinitionMap definitions = b.fBefore;
363 for (BasicBlock::Node& n : b.fNodes) {
364 if (n.fKind == BasicBlock::Node::kExpression_Kind) {
365 ASSERT(n.fExpression);
366 Expression* expr = n.fExpression->get();
367 if (n.fConstantPropagation) {
368 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator,
369 definitions);
370 if (optimized) {
371 n.fExpression->reset(optimized.release());
372 expr = n.fExpression->get();
Ethan Nicholas113628d2017-02-02 16:11:39 -0500373 }
374 }
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000375 if (expr->fKind == Expression::kVariableReference_Kind) {
376 const Variable& var = ((VariableReference*) expr)->fVariable;
377 if (var.fStorage == Variable::kLocal_Storage &&
378 !definitions[&var]) {
379 this->error(expr->fPosition,
380 "'" + var.fName + "' has not been assigned");
381 }
382 }
Ethan Nicholas113628d2017-02-02 16:11:39 -0500383 }
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000384 this->addDefinitions(n, &definitions);
Ethan Nicholas113628d2017-02-02 16:11:39 -0500385 }
Ethan Nicholase1d9cb82017-02-06 18:53:07 +0000386 }
ethannicholas22f939e2016-10-13 13:25:34 -0700387
388 // check for missing return
389 if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
390 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400391 this->error(f.fPosition, String("function can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -0700392 }
393 }
394}
395
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400396void Compiler::internalConvertProgram(String text,
ethannicholas5961bc92016-10-12 06:39:56 -0700397 Modifiers::Flag* defaultPrecision,
ethannicholasb3058bd2016-07-01 08:22:01 -0700398 std::vector<std::unique_ptr<ProgramElement>>* result) {
399 Parser parser(text, *fTypes, *this);
400 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
401 if (fErrorCount) {
402 return;
403 }
ethannicholas5961bc92016-10-12 06:39:56 -0700404 *defaultPrecision = Modifiers::kHighp_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700405 for (size_t i = 0; i < parsed.size(); i++) {
406 ASTDeclaration& decl = *parsed[i];
407 switch (decl.fKind) {
408 case ASTDeclaration::kVar_Kind: {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700409 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations(
Mike Klein6ad99092016-10-26 10:35:22 -0400410 (ASTVarDeclarations&) decl,
ethannicholasb3058bd2016-07-01 08:22:01 -0700411 Variable::kGlobal_Storage);
412 if (s) {
413 result->push_back(std::move(s));
414 }
415 break;
416 }
417 case ASTDeclaration::kFunction_Kind: {
418 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
419 (ASTFunction&) decl);
ethannicholas22f939e2016-10-13 13:25:34 -0700420 if (!fErrorCount && f) {
421 this->scanCFG(*f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700422 result->push_back(std::move(f));
423 }
424 break;
425 }
ethannicholas5961bc92016-10-12 06:39:56 -0700426 case ASTDeclaration::kModifiers_Kind: {
427 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration(
428 (ASTModifiersDeclaration&) decl);
429 if (f) {
430 result->push_back(std::move(f));
431 }
432 break;
433 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700434 case ASTDeclaration::kInterfaceBlock_Kind: {
435 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
436 (ASTInterfaceBlock&) decl);
437 if (i) {
438 result->push_back(std::move(i));
439 }
440 break;
441 }
442 case ASTDeclaration::kExtension_Kind: {
443 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
444 if (e) {
445 result->push_back(std::move(e));
446 }
447 break;
448 }
ethannicholas5961bc92016-10-12 06:39:56 -0700449 case ASTDeclaration::kPrecision_Kind: {
450 *defaultPrecision = ((ASTPrecision&) decl).fPrecision;
451 break;
452 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700453 default:
454 ABORT("unsupported declaration: %s\n", decl.description().c_str());
455 }
456 }
457}
458
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400459std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500460 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700461 fErrorText = "";
462 fErrorCount = 0;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500463 fIRGenerator->start(&settings);
ethannicholasd598f792016-07-25 10:08:54 -0700464 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholas5961bc92016-10-12 06:39:56 -0700465 Modifiers::Flag ignored;
ethannicholasb3058bd2016-07-01 08:22:01 -0700466 switch (kind) {
467 case Program::kVertex_Kind:
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400468 this->internalConvertProgram(String(SKSL_VERT_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700469 break;
470 case Program::kFragment_Kind:
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400471 this->internalConvertProgram(String(SKSL_FRAG_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700472 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -0500473 case Program::kGeometry_Kind:
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400474 this->internalConvertProgram(String(SKSL_GEOM_INCLUDE), &ignored, &elements);
Ethan Nicholas52cad152017-02-16 16:37:32 -0500475 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700476 }
ethannicholasddb37d62016-10-20 09:54:00 -0700477 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholas5961bc92016-10-12 06:39:56 -0700478 Modifiers::Flag defaultPrecision;
479 this->internalConvertProgram(text, &defaultPrecision, &elements);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500480 auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext,
481 std::move(elements),
482 fIRGenerator->fSymbolTable,
483 fIRGenerator->fInputs));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500484 fIRGenerator->finish();
ethannicholasb3058bd2016-07-01 08:22:01 -0700485 this->writeErrorCount();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500486 if (fErrorCount) {
487 return nullptr;
488 }
489 return result;
490}
491
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400492bool Compiler::toSPIRV(const Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400493#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400494 StringStream buffer;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400495 SPIRVCodeGenerator cg(&fContext, &program, this, &buffer);
496 bool result = cg.generateCode();
497 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400498 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400499 ASSERT(0 == buffer.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400500 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
501 SkDebugf("SPIR-V validation error: %s\n", m);
502 };
503 tools.SetMessageConsumer(dumpmsg);
504 // Verify that the SPIR-V we produced is valid. If this assert fails, check the logs prior
505 // to the failure to see the validation errors.
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400506 ASSERT_RESULT(tools.Validate((const uint32_t*) buffer.data(), buffer.size() / 4));
507 out.write(buffer.data(), buffer.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400508 }
509#else
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500510 SPIRVCodeGenerator cg(&fContext, &program, this, &out);
511 bool result = cg.generateCode();
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400512#endif
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500513 this->writeErrorCount();
Ethan Nicholasce33f102016-12-09 17:22:59 -0500514 return result;
515}
516
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400517bool Compiler::toSPIRV(const Program& program, String* out) {
518 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500519 bool result = this->toSPIRV(program, buffer);
520 if (result) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400521 *out = String(buffer.data(), buffer.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500522 }
523 return result;
524}
525
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400526bool Compiler::toGLSL(const Program& program, OutputStream& out) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500527 GLSLCodeGenerator cg(&fContext, &program, this, &out);
528 bool result = cg.generateCode();
529 this->writeErrorCount();
530 return result;
531}
532
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400533bool Compiler::toGLSL(const Program& program, String* out) {
534 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500535 bool result = this->toGLSL(program, buffer);
536 if (result) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400537 *out = String(buffer.data(), buffer.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500538 }
539 return result;
540}
541
542
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400543void Compiler::error(Position position, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700544 fErrorCount++;
545 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
546}
547
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400548String Compiler::errorText() {
549 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700550 return result;
551}
552
553void Compiler::writeErrorCount() {
554 if (fErrorCount) {
555 fErrorText += to_string(fErrorCount) + " error";
556 if (fErrorCount > 1) {
557 fErrorText += "s";
558 }
559 fErrorText += "\n";
560 }
561}
562
ethannicholasb3058bd2016-07-01 08:22:01 -0700563} // namespace