blob: 743745ad14365cf83a842273c25e553cdf65cd59 [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#include "SkMutex.h"
23
24#define STRINGIFY(x) #x
25
26// include the built-in shader symbols as static strings
27
ethannicholas5961bc92016-10-12 06:39:56 -070028static const char* SKSL_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070029#include "sksl.include"
30;
31
ethannicholas5961bc92016-10-12 06:39:56 -070032static const char* SKSL_VERT_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070033#include "sksl_vert.include"
34;
35
ethannicholas5961bc92016-10-12 06:39:56 -070036static const char* SKSL_FRAG_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070037#include "sksl_frag.include"
38;
39
40namespace SkSL {
41
Mike Klein6ad99092016-10-26 10:35:22 -040042Compiler::Compiler()
ethannicholasb3058bd2016-07-01 08:22:01 -070043: fErrorCount(0) {
44 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this));
45 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this));
ethannicholasd598f792016-07-25 10:08:54 -070046 fIRGenerator = new IRGenerator(&fContext, symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070047 fTypes = types;
ethannicholasd598f792016-07-25 10:08:54 -070048 #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
49 fContext.f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070050 ADD_TYPE(Void);
51 ADD_TYPE(Float);
52 ADD_TYPE(Vec2);
53 ADD_TYPE(Vec3);
54 ADD_TYPE(Vec4);
55 ADD_TYPE(Double);
56 ADD_TYPE(DVec2);
57 ADD_TYPE(DVec3);
58 ADD_TYPE(DVec4);
59 ADD_TYPE(Int);
60 ADD_TYPE(IVec2);
61 ADD_TYPE(IVec3);
62 ADD_TYPE(IVec4);
63 ADD_TYPE(UInt);
64 ADD_TYPE(UVec2);
65 ADD_TYPE(UVec3);
66 ADD_TYPE(UVec4);
67 ADD_TYPE(Bool);
68 ADD_TYPE(BVec2);
69 ADD_TYPE(BVec3);
70 ADD_TYPE(BVec4);
71 ADD_TYPE(Mat2x2);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050072 types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070073 ADD_TYPE(Mat2x3);
74 ADD_TYPE(Mat2x4);
75 ADD_TYPE(Mat3x2);
76 ADD_TYPE(Mat3x3);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050077 types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070078 ADD_TYPE(Mat3x4);
79 ADD_TYPE(Mat4x2);
80 ADD_TYPE(Mat4x3);
81 ADD_TYPE(Mat4x4);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050082 types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070083 ADD_TYPE(GenType);
84 ADD_TYPE(GenDType);
85 ADD_TYPE(GenIType);
86 ADD_TYPE(GenUType);
87 ADD_TYPE(GenBType);
88 ADD_TYPE(Mat);
89 ADD_TYPE(Vec);
90 ADD_TYPE(GVec);
91 ADD_TYPE(GVec2);
92 ADD_TYPE(GVec3);
93 ADD_TYPE(GVec4);
94 ADD_TYPE(DVec);
95 ADD_TYPE(IVec);
96 ADD_TYPE(UVec);
97 ADD_TYPE(BVec);
98
99 ADD_TYPE(Sampler1D);
100 ADD_TYPE(Sampler2D);
101 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700102 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700103 ADD_TYPE(SamplerCube);
104 ADD_TYPE(Sampler2DRect);
105 ADD_TYPE(Sampler1DArray);
106 ADD_TYPE(Sampler2DArray);
107 ADD_TYPE(SamplerCubeArray);
108 ADD_TYPE(SamplerBuffer);
109 ADD_TYPE(Sampler2DMS);
110 ADD_TYPE(Sampler2DMSArray);
111
Brian Salomonbf7b6202016-11-11 16:08:03 -0500112 ADD_TYPE(ISampler2D);
113
Brian Salomon2a51de82016-11-16 12:06:01 -0500114 ADD_TYPE(Image2D);
115 ADD_TYPE(IImage2D);
116
Greg Daniel64773e62016-11-22 09:44:03 -0500117 ADD_TYPE(SubpassInput);
118 ADD_TYPE(SubpassInputMS);
119
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 ADD_TYPE(GSampler1D);
121 ADD_TYPE(GSampler2D);
122 ADD_TYPE(GSampler3D);
123 ADD_TYPE(GSamplerCube);
124 ADD_TYPE(GSampler2DRect);
125 ADD_TYPE(GSampler1DArray);
126 ADD_TYPE(GSampler2DArray);
127 ADD_TYPE(GSamplerCubeArray);
128 ADD_TYPE(GSamplerBuffer);
129 ADD_TYPE(GSampler2DMS);
130 ADD_TYPE(GSampler2DMSArray);
131
132 ADD_TYPE(Sampler1DShadow);
133 ADD_TYPE(Sampler2DShadow);
134 ADD_TYPE(SamplerCubeShadow);
135 ADD_TYPE(Sampler2DRectShadow);
136 ADD_TYPE(Sampler1DArrayShadow);
137 ADD_TYPE(Sampler2DArrayShadow);
138 ADD_TYPE(SamplerCubeArrayShadow);
139 ADD_TYPE(GSampler2DArrayShadow);
140 ADD_TYPE(GSamplerCubeArrayShadow);
141
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500142 SkString skCapsName("sk_Caps");
143 Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName,
144 *fContext.fSkCaps_Type, Variable::kGlobal_Storage);
145 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
146
ethannicholas5961bc92016-10-12 06:39:56 -0700147 Modifiers::Flag ignored1;
148 std::vector<std::unique_ptr<ProgramElement>> ignored2;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500149 this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2);
ethannicholasddb37d62016-10-20 09:54:00 -0700150 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholasb3058bd2016-07-01 08:22:01 -0700151 ASSERT(!fErrorCount);
152}
153
154Compiler::~Compiler() {
155 delete fIRGenerator;
156}
157
ethannicholas22f939e2016-10-13 13:25:34 -0700158// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500159void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
160 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700161 switch (lvalue->fKind) {
162 case Expression::kVariableReference_Kind: {
163 const Variable& var = ((VariableReference*) lvalue)->fVariable;
164 if (var.fStorage == Variable::kLocal_Storage) {
165 (*definitions)[&var] = expr;
166 }
167 break;
168 }
169 case Expression::kSwizzle_Kind:
170 // We consider the variable written to as long as at least some of its components have
171 // been written to. This will lead to some false negatives (we won't catch it if you
172 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400173 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
174 // 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 -0700175 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400176 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500177 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700178 definitions);
179 break;
180 case Expression::kIndex_Kind:
181 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400182 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500183 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700184 definitions);
185 break;
186 case Expression::kFieldAccess_Kind:
187 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400188 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas86a43402017-01-19 13:32:00 -0500189 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700190 definitions);
191 break;
192 default:
193 // not an lvalue, can't happen
194 ASSERT(false);
195 }
196}
197
198// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400199void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500200 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700201 switch (node.fKind) {
202 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500203 ASSERT(node.fExpression);
204 const Expression* expr = (Expression*) node.fExpression->get();
205 switch (expr->fKind) {
206 case Expression::kBinary_Kind: {
207 BinaryExpression* b = (BinaryExpression*) expr;
208 if (b->fOperator == Token::EQ) {
209 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
210 } else if (Token::IsAssignment(b->fOperator)) {
211 this->addDefinition(
212 b->fLeft.get(),
213 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
214 definitions);
215
216 }
217 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700218 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500219 case Expression::kPrefix_Kind: {
220 const PrefixExpression* p = (PrefixExpression*) expr;
221 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
222 this->addDefinition(
223 p->fOperand.get(),
224 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
225 definitions);
226 }
227 break;
228 }
229 case Expression::kPostfix_Kind: {
230 const PostfixExpression* p = (PostfixExpression*) expr;
231 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
232 this->addDefinition(
233 p->fOperand.get(),
234 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
235 definitions);
236
237 }
238 break;
239 }
240 default:
241 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700242 }
243 break;
244 }
245 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500246 const Statement* stmt = (Statement*) node.fStatement;
ethannicholas22f939e2016-10-13 13:25:34 -0700247 if (stmt->fKind == Statement::kVarDeclarations_Kind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500248 VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
249 for (VarDeclaration& decl : vd->fDeclaration->fVars) {
ethannicholas22f939e2016-10-13 13:25:34 -0700250 if (decl.fValue) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500251 (*definitions)[decl.fVar] = &decl.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700252 }
253 }
254 }
255 break;
256 }
257 }
258}
259
260void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
261 BasicBlock& block = cfg->fBlocks[blockId];
262
263 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500264 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700265 for (const BasicBlock::Node& n : block.fNodes) {
266 this->addDefinitions(n, &after);
267 }
268
269 // propagate definitions to exits
270 for (BlockId exitId : block.fExits) {
271 BasicBlock& exit = cfg->fBlocks[exitId];
272 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500273 std::unique_ptr<Expression>* e1 = pair.second;
274 auto found = exit.fBefore.find(pair.first);
275 if (found == exit.fBefore.end()) {
276 // exit has no definition for it, just copy it
277 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700278 exit.fBefore[pair.first] = e1;
279 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500280 // exit has a (possibly different) value already defined
281 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700282 if (e1 != e2) {
283 // definition has changed, merge and add exit block to worklist
284 workList->insert(exitId);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500285 exit.fBefore[pair.first] =
286 (std::unique_ptr<Expression>*) &fContext.fDefined_Expression;
ethannicholas22f939e2016-10-13 13:25:34 -0700287 }
288 }
289 }
290 }
291}
292
293// returns a map which maps all local variables in the function to null, indicating that their value
294// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500295static DefinitionMap compute_start_state(const CFG& cfg) {
296 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400297 for (const auto& block : cfg.fBlocks) {
298 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700299 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500300 ASSERT(node.fStatement);
301 const Statement* s = node.fStatement;
ethannicholas22f939e2016-10-13 13:25:34 -0700302 if (s->fKind == Statement::kVarDeclarations_Kind) {
303 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
304 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
305 result[decl.fVar] = nullptr;
Mike Klein6ad99092016-10-26 10:35:22 -0400306 }
ethannicholas22f939e2016-10-13 13:25:34 -0700307 }
308 }
309 }
310 }
311 return result;
312}
313
314void Compiler::scanCFG(const FunctionDefinition& f) {
315 CFG cfg = CFGGenerator().getCFG(f);
316
317 // compute the data flow
318 cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg);
319 std::set<BlockId> workList;
320 for (BlockId i = 0; i < cfg.fBlocks.size(); i++) {
321 workList.insert(i);
322 }
323 while (workList.size()) {
324 BlockId next = *workList.begin();
325 workList.erase(workList.begin());
326 this->scanCFG(&cfg, next, &workList);
327 }
328
329 // check for unreachable code
330 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -0400331 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -0700332 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500333 Position p;
334 switch (cfg.fBlocks[i].fNodes[0].fKind) {
335 case BasicBlock::Node::kStatement_Kind:
336 p = cfg.fBlocks[i].fNodes[0].fStatement->fPosition;
337 break;
338 case BasicBlock::Node::kExpression_Kind:
339 p = (*cfg.fBlocks[i].fNodes[0].fExpression)->fPosition;
340 break;
341 }
342 this->error(p, SkString("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -0700343 }
344 }
345 if (fErrorCount) {
346 return;
347 }
348
Ethan Nicholas86a43402017-01-19 13:32:00 -0500349 // check for undefined variables, perform constant propagation
350 for (BasicBlock& b : cfg.fBlocks) {
351 DefinitionMap definitions = b.fBefore;
352 for (BasicBlock::Node& n : b.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700353 if (n.fKind == BasicBlock::Node::kExpression_Kind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500354 ASSERT(n.fExpression);
355 Expression* expr = n.fExpression->get();
356 if (n.fConstantPropagation) {
357 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator,
358 definitions);
359 if (optimized) {
360 n.fExpression->reset(optimized.release());
361 expr = n.fExpression->get();
362 }
363 }
ethannicholas22f939e2016-10-13 13:25:34 -0700364 if (expr->fKind == Expression::kVariableReference_Kind) {
365 const Variable& var = ((VariableReference*) expr)->fVariable;
Mike Klein6ad99092016-10-26 10:35:22 -0400366 if (var.fStorage == Variable::kLocal_Storage &&
ethannicholas22f939e2016-10-13 13:25:34 -0700367 !definitions[&var]) {
368 this->error(expr->fPosition,
369 "'" + var.fName + "' has not been assigned");
Mike Klein6ad99092016-10-26 10:35:22 -0400370 }
ethannicholas22f939e2016-10-13 13:25:34 -0700371 }
372 }
373 this->addDefinitions(n, &definitions);
374 }
375 }
376
377 // check for missing return
378 if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
379 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500380 this->error(f.fPosition, SkString("function can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -0700381 }
382 }
383}
384
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500385void Compiler::internalConvertProgram(SkString text,
ethannicholas5961bc92016-10-12 06:39:56 -0700386 Modifiers::Flag* defaultPrecision,
ethannicholasb3058bd2016-07-01 08:22:01 -0700387 std::vector<std::unique_ptr<ProgramElement>>* result) {
388 Parser parser(text, *fTypes, *this);
389 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
390 if (fErrorCount) {
391 return;
392 }
ethannicholas5961bc92016-10-12 06:39:56 -0700393 *defaultPrecision = Modifiers::kHighp_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700394 for (size_t i = 0; i < parsed.size(); i++) {
395 ASTDeclaration& decl = *parsed[i];
396 switch (decl.fKind) {
397 case ASTDeclaration::kVar_Kind: {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700398 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations(
Mike Klein6ad99092016-10-26 10:35:22 -0400399 (ASTVarDeclarations&) decl,
ethannicholasb3058bd2016-07-01 08:22:01 -0700400 Variable::kGlobal_Storage);
401 if (s) {
402 result->push_back(std::move(s));
403 }
404 break;
405 }
406 case ASTDeclaration::kFunction_Kind: {
407 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
408 (ASTFunction&) decl);
ethannicholas22f939e2016-10-13 13:25:34 -0700409 if (!fErrorCount && f) {
410 this->scanCFG(*f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700411 result->push_back(std::move(f));
412 }
413 break;
414 }
ethannicholas5961bc92016-10-12 06:39:56 -0700415 case ASTDeclaration::kModifiers_Kind: {
416 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration(
417 (ASTModifiersDeclaration&) decl);
418 if (f) {
419 result->push_back(std::move(f));
420 }
421 break;
422 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700423 case ASTDeclaration::kInterfaceBlock_Kind: {
424 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
425 (ASTInterfaceBlock&) decl);
426 if (i) {
427 result->push_back(std::move(i));
428 }
429 break;
430 }
431 case ASTDeclaration::kExtension_Kind: {
432 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
433 if (e) {
434 result->push_back(std::move(e));
435 }
436 break;
437 }
ethannicholas5961bc92016-10-12 06:39:56 -0700438 case ASTDeclaration::kPrecision_Kind: {
439 *defaultPrecision = ((ASTPrecision&) decl).fPrecision;
440 break;
441 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700442 default:
443 ABORT("unsupported declaration: %s\n", decl.description().c_str());
444 }
445 }
446}
447
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500448std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500449 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700450 fErrorText = "";
451 fErrorCount = 0;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500452 fIRGenerator->start(&settings);
ethannicholasd598f792016-07-25 10:08:54 -0700453 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholas5961bc92016-10-12 06:39:56 -0700454 Modifiers::Flag ignored;
ethannicholasb3058bd2016-07-01 08:22:01 -0700455 switch (kind) {
456 case Program::kVertex_Kind:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500457 this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700458 break;
459 case Program::kFragment_Kind:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500460 this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700461 break;
462 }
ethannicholasddb37d62016-10-20 09:54:00 -0700463 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholas5961bc92016-10-12 06:39:56 -0700464 Modifiers::Flag defaultPrecision;
465 this->internalConvertProgram(text, &defaultPrecision, &elements);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500466 auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext,
467 std::move(elements),
468 fIRGenerator->fSymbolTable,
469 fIRGenerator->fInputs));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500470 fIRGenerator->finish();
ethannicholasb3058bd2016-07-01 08:22:01 -0700471 this->writeErrorCount();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500472 if (fErrorCount) {
473 return nullptr;
474 }
475 return result;
476}
477
478
479bool Compiler::toSPIRV(const Program& program, SkWStream& out) {
480 SPIRVCodeGenerator cg(&fContext, &program, this, &out);
481 bool result = cg.generateCode();
482 this->writeErrorCount();
Ethan Nicholasce33f102016-12-09 17:22:59 -0500483 return result;
484}
485
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500486bool Compiler::toSPIRV(const Program& program, SkString* out) {
487 SkDynamicMemoryWStream buffer;
488 bool result = this->toSPIRV(program, buffer);
489 if (result) {
490 sk_sp<SkData> data(buffer.detachAsData());
491 *out = SkString((const char*) data->data(), data->size());
492 }
493 return result;
494}
495
496bool Compiler::toGLSL(const Program& program, SkWStream& out) {
497 GLSLCodeGenerator cg(&fContext, &program, this, &out);
498 bool result = cg.generateCode();
499 this->writeErrorCount();
500 return result;
501}
502
503bool Compiler::toGLSL(const Program& program, SkString* out) {
504 SkDynamicMemoryWStream buffer;
505 bool result = this->toGLSL(program, buffer);
506 if (result) {
507 sk_sp<SkData> data(buffer.detachAsData());
508 *out = SkString((const char*) data->data(), data->size());
509 }
510 return result;
511}
512
513
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500514void Compiler::error(Position position, SkString msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700515 fErrorCount++;
516 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
517}
518
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500519SkString Compiler::errorText() {
520 SkString result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700521 return result;
522}
523
524void Compiler::writeErrorCount() {
525 if (fErrorCount) {
526 fErrorText += to_string(fErrorCount) + " error";
527 if (fErrorCount > 1) {
528 fErrorText += "s";
529 }
530 fErrorText += "\n";
531 }
532}
533
ethannicholasb3058bd2016-07-01 08:22:01 -0700534} // namespace