blob: 62e5fa72e5955f2ab8f74746c2691bb19ee9cae6 [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"
ethannicholasb3058bd2016-07-01 08:22:01 -070012#include "SkSLIRGenerator.h"
13#include "SkSLParser.h"
14#include "SkSLSPIRVCodeGenerator.h"
15#include "ir/SkSLExpression.h"
16#include "ir/SkSLIntLiteral.h"
ethannicholas5961bc92016-10-12 06:39:56 -070017#include "ir/SkSLModifiersDeclaration.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070018#include "ir/SkSLSymbolTable.h"
ethannicholasddb37d62016-10-20 09:54:00 -070019#include "ir/SkSLUnresolvedFunction.h"
ethannicholas22f939e2016-10-13 13:25:34 -070020#include "ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070021#include "SkMutex.h"
22
23#define STRINGIFY(x) #x
24
25// include the built-in shader symbols as static strings
26
ethannicholas5961bc92016-10-12 06:39:56 -070027static const char* SKSL_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070028#include "sksl.include"
29;
30
ethannicholas5961bc92016-10-12 06:39:56 -070031static const char* SKSL_VERT_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070032#include "sksl_vert.include"
33;
34
ethannicholas5961bc92016-10-12 06:39:56 -070035static const char* SKSL_FRAG_INCLUDE =
ethannicholasb3058bd2016-07-01 08:22:01 -070036#include "sksl_frag.include"
37;
38
39namespace SkSL {
40
Mike Klein6ad99092016-10-26 10:35:22 -040041Compiler::Compiler()
ethannicholasb3058bd2016-07-01 08:22:01 -070042: fErrorCount(0) {
43 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this));
44 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this));
ethannicholasd598f792016-07-25 10:08:54 -070045 fIRGenerator = new IRGenerator(&fContext, symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070046 fTypes = types;
ethannicholasd598f792016-07-25 10:08:54 -070047 #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
48 fContext.f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070049 ADD_TYPE(Void);
50 ADD_TYPE(Float);
51 ADD_TYPE(Vec2);
52 ADD_TYPE(Vec3);
53 ADD_TYPE(Vec4);
54 ADD_TYPE(Double);
55 ADD_TYPE(DVec2);
56 ADD_TYPE(DVec3);
57 ADD_TYPE(DVec4);
58 ADD_TYPE(Int);
59 ADD_TYPE(IVec2);
60 ADD_TYPE(IVec3);
61 ADD_TYPE(IVec4);
62 ADD_TYPE(UInt);
63 ADD_TYPE(UVec2);
64 ADD_TYPE(UVec3);
65 ADD_TYPE(UVec4);
66 ADD_TYPE(Bool);
67 ADD_TYPE(BVec2);
68 ADD_TYPE(BVec3);
69 ADD_TYPE(BVec4);
70 ADD_TYPE(Mat2x2);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050071 types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070072 ADD_TYPE(Mat2x3);
73 ADD_TYPE(Mat2x4);
74 ADD_TYPE(Mat3x2);
75 ADD_TYPE(Mat3x3);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050076 types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070077 ADD_TYPE(Mat3x4);
78 ADD_TYPE(Mat4x2);
79 ADD_TYPE(Mat4x3);
80 ADD_TYPE(Mat4x4);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050081 types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get());
ethannicholasb3058bd2016-07-01 08:22:01 -070082 ADD_TYPE(GenType);
83 ADD_TYPE(GenDType);
84 ADD_TYPE(GenIType);
85 ADD_TYPE(GenUType);
86 ADD_TYPE(GenBType);
87 ADD_TYPE(Mat);
88 ADD_TYPE(Vec);
89 ADD_TYPE(GVec);
90 ADD_TYPE(GVec2);
91 ADD_TYPE(GVec3);
92 ADD_TYPE(GVec4);
93 ADD_TYPE(DVec);
94 ADD_TYPE(IVec);
95 ADD_TYPE(UVec);
96 ADD_TYPE(BVec);
97
98 ADD_TYPE(Sampler1D);
99 ADD_TYPE(Sampler2D);
100 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700101 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700102 ADD_TYPE(SamplerCube);
103 ADD_TYPE(Sampler2DRect);
104 ADD_TYPE(Sampler1DArray);
105 ADD_TYPE(Sampler2DArray);
106 ADD_TYPE(SamplerCubeArray);
107 ADD_TYPE(SamplerBuffer);
108 ADD_TYPE(Sampler2DMS);
109 ADD_TYPE(Sampler2DMSArray);
110
Brian Salomonbf7b6202016-11-11 16:08:03 -0500111 ADD_TYPE(ISampler2D);
112
Brian Salomon2a51de82016-11-16 12:06:01 -0500113 ADD_TYPE(Image2D);
114 ADD_TYPE(IImage2D);
115
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 ADD_TYPE(GSampler1D);
117 ADD_TYPE(GSampler2D);
118 ADD_TYPE(GSampler3D);
119 ADD_TYPE(GSamplerCube);
120 ADD_TYPE(GSampler2DRect);
121 ADD_TYPE(GSampler1DArray);
122 ADD_TYPE(GSampler2DArray);
123 ADD_TYPE(GSamplerCubeArray);
124 ADD_TYPE(GSamplerBuffer);
125 ADD_TYPE(GSampler2DMS);
126 ADD_TYPE(GSampler2DMSArray);
127
128 ADD_TYPE(Sampler1DShadow);
129 ADD_TYPE(Sampler2DShadow);
130 ADD_TYPE(SamplerCubeShadow);
131 ADD_TYPE(Sampler2DRectShadow);
132 ADD_TYPE(Sampler1DArrayShadow);
133 ADD_TYPE(Sampler2DArrayShadow);
134 ADD_TYPE(SamplerCubeArrayShadow);
135 ADD_TYPE(GSampler2DArrayShadow);
136 ADD_TYPE(GSamplerCubeArrayShadow);
137
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500138 SkString skCapsName("sk_Caps");
139 Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName,
140 *fContext.fSkCaps_Type, Variable::kGlobal_Storage);
141 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
142
ethannicholas5961bc92016-10-12 06:39:56 -0700143 Modifiers::Flag ignored1;
144 std::vector<std::unique_ptr<ProgramElement>> ignored2;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500145 this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2);
ethannicholasddb37d62016-10-20 09:54:00 -0700146 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 ASSERT(!fErrorCount);
148}
149
150Compiler::~Compiler() {
151 delete fIRGenerator;
152}
153
ethannicholas22f939e2016-10-13 13:25:34 -0700154// add the definition created by assigning to the lvalue to the definition set
155void Compiler::addDefinition(const Expression* lvalue, const Expression* expr,
156 std::unordered_map<const Variable*, const Expression*>* definitions) {
157 switch (lvalue->fKind) {
158 case Expression::kVariableReference_Kind: {
159 const Variable& var = ((VariableReference*) lvalue)->fVariable;
160 if (var.fStorage == Variable::kLocal_Storage) {
161 (*definitions)[&var] = expr;
162 }
163 break;
164 }
165 case Expression::kSwizzle_Kind:
166 // We consider the variable written to as long as at least some of its components have
167 // been written to. This will lead to some false negatives (we won't catch it if you
168 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400169 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
170 // 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 -0700171 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400172 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
173 fContext.fDefined_Expression.get(),
ethannicholas22f939e2016-10-13 13:25:34 -0700174 definitions);
175 break;
176 case Expression::kIndex_Kind:
177 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400178 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
179 fContext.fDefined_Expression.get(),
ethannicholas22f939e2016-10-13 13:25:34 -0700180 definitions);
181 break;
182 case Expression::kFieldAccess_Kind:
183 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400184 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
185 fContext.fDefined_Expression.get(),
ethannicholas22f939e2016-10-13 13:25:34 -0700186 definitions);
187 break;
188 default:
189 // not an lvalue, can't happen
190 ASSERT(false);
191 }
192}
193
194// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400195void Compiler::addDefinitions(const BasicBlock::Node& node,
ethannicholas22f939e2016-10-13 13:25:34 -0700196 std::unordered_map<const Variable*, const Expression*>* definitions) {
197 switch (node.fKind) {
198 case BasicBlock::Node::kExpression_Kind: {
199 const Expression* expr = (Expression*) node.fNode;
200 if (expr->fKind == Expression::kBinary_Kind) {
201 const BinaryExpression* b = (BinaryExpression*) expr;
202 if (b->fOperator == Token::EQ) {
203 this->addDefinition(b->fLeft.get(), b->fRight.get(), definitions);
204 }
205 }
206 break;
207 }
208 case BasicBlock::Node::kStatement_Kind: {
209 const Statement* stmt = (Statement*) node.fNode;
210 if (stmt->fKind == Statement::kVarDeclarations_Kind) {
211 const VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
212 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
213 if (decl.fValue) {
214 (*definitions)[decl.fVar] = decl.fValue.get();
215 }
216 }
217 }
218 break;
219 }
220 }
221}
222
223void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
224 BasicBlock& block = cfg->fBlocks[blockId];
225
226 // compute definitions after this block
227 std::unordered_map<const Variable*, const Expression*> after = block.fBefore;
228 for (const BasicBlock::Node& n : block.fNodes) {
229 this->addDefinitions(n, &after);
230 }
231
232 // propagate definitions to exits
233 for (BlockId exitId : block.fExits) {
234 BasicBlock& exit = cfg->fBlocks[exitId];
235 for (const auto& pair : after) {
236 const Expression* e1 = pair.second;
237 if (exit.fBefore.find(pair.first) == exit.fBefore.end()) {
238 exit.fBefore[pair.first] = e1;
239 } else {
240 const Expression* e2 = exit.fBefore[pair.first];
241 if (e1 != e2) {
242 // definition has changed, merge and add exit block to worklist
243 workList->insert(exitId);
244 if (!e1 || !e2) {
245 exit.fBefore[pair.first] = nullptr;
246 } else {
247 exit.fBefore[pair.first] = fContext.fDefined_Expression.get();
248 }
249 }
250 }
251 }
252 }
253}
254
255// returns a map which maps all local variables in the function to null, indicating that their value
256// is initially unknown
257static std::unordered_map<const Variable*, const Expression*> compute_start_state(const CFG& cfg) {
258 std::unordered_map<const Variable*, const Expression*> result;
Mike Klein6ad99092016-10-26 10:35:22 -0400259 for (const auto& block : cfg.fBlocks) {
260 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700261 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
262 const Statement* s = (Statement*) node.fNode;
263 if (s->fKind == Statement::kVarDeclarations_Kind) {
264 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
265 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
266 result[decl.fVar] = nullptr;
Mike Klein6ad99092016-10-26 10:35:22 -0400267 }
ethannicholas22f939e2016-10-13 13:25:34 -0700268 }
269 }
270 }
271 }
272 return result;
273}
274
275void Compiler::scanCFG(const FunctionDefinition& f) {
276 CFG cfg = CFGGenerator().getCFG(f);
277
278 // compute the data flow
279 cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg);
280 std::set<BlockId> workList;
281 for (BlockId i = 0; i < cfg.fBlocks.size(); i++) {
282 workList.insert(i);
283 }
284 while (workList.size()) {
285 BlockId next = *workList.begin();
286 workList.erase(workList.begin());
287 this->scanCFG(&cfg, next, &workList);
288 }
289
290 // check for unreachable code
291 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -0400292 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -0700293 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500294 this->error(cfg.fBlocks[i].fNodes[0].fNode->fPosition, SkString("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -0700295 }
296 }
297 if (fErrorCount) {
298 return;
299 }
300
301 // check for undefined variables
302 for (const BasicBlock& b : cfg.fBlocks) {
303 std::unordered_map<const Variable*, const Expression*> definitions = b.fBefore;
304 for (const BasicBlock::Node& n : b.fNodes) {
305 if (n.fKind == BasicBlock::Node::kExpression_Kind) {
306 const Expression* expr = (const Expression*) n.fNode;
307 if (expr->fKind == Expression::kVariableReference_Kind) {
308 const Variable& var = ((VariableReference*) expr)->fVariable;
Mike Klein6ad99092016-10-26 10:35:22 -0400309 if (var.fStorage == Variable::kLocal_Storage &&
ethannicholas22f939e2016-10-13 13:25:34 -0700310 !definitions[&var]) {
311 this->error(expr->fPosition,
312 "'" + var.fName + "' has not been assigned");
Mike Klein6ad99092016-10-26 10:35:22 -0400313 }
ethannicholas22f939e2016-10-13 13:25:34 -0700314 }
315 }
316 this->addDefinitions(n, &definitions);
317 }
318 }
319
320 // check for missing return
321 if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
322 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500323 this->error(f.fPosition, SkString("function can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -0700324 }
325 }
326}
327
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500328void Compiler::internalConvertProgram(SkString text,
ethannicholas5961bc92016-10-12 06:39:56 -0700329 Modifiers::Flag* defaultPrecision,
ethannicholasb3058bd2016-07-01 08:22:01 -0700330 std::vector<std::unique_ptr<ProgramElement>>* result) {
331 Parser parser(text, *fTypes, *this);
332 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
333 if (fErrorCount) {
334 return;
335 }
ethannicholas5961bc92016-10-12 06:39:56 -0700336 *defaultPrecision = Modifiers::kHighp_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700337 for (size_t i = 0; i < parsed.size(); i++) {
338 ASTDeclaration& decl = *parsed[i];
339 switch (decl.fKind) {
340 case ASTDeclaration::kVar_Kind: {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700341 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations(
Mike Klein6ad99092016-10-26 10:35:22 -0400342 (ASTVarDeclarations&) decl,
ethannicholasb3058bd2016-07-01 08:22:01 -0700343 Variable::kGlobal_Storage);
344 if (s) {
345 result->push_back(std::move(s));
346 }
347 break;
348 }
349 case ASTDeclaration::kFunction_Kind: {
350 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
351 (ASTFunction&) decl);
ethannicholas22f939e2016-10-13 13:25:34 -0700352 if (!fErrorCount && f) {
353 this->scanCFG(*f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700354 result->push_back(std::move(f));
355 }
356 break;
357 }
ethannicholas5961bc92016-10-12 06:39:56 -0700358 case ASTDeclaration::kModifiers_Kind: {
359 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration(
360 (ASTModifiersDeclaration&) decl);
361 if (f) {
362 result->push_back(std::move(f));
363 }
364 break;
365 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700366 case ASTDeclaration::kInterfaceBlock_Kind: {
367 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
368 (ASTInterfaceBlock&) decl);
369 if (i) {
370 result->push_back(std::move(i));
371 }
372 break;
373 }
374 case ASTDeclaration::kExtension_Kind: {
375 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
376 if (e) {
377 result->push_back(std::move(e));
378 }
379 break;
380 }
ethannicholas5961bc92016-10-12 06:39:56 -0700381 case ASTDeclaration::kPrecision_Kind: {
382 *defaultPrecision = ((ASTPrecision&) decl).fPrecision;
383 break;
384 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700385 default:
386 ABORT("unsupported declaration: %s\n", decl.description().c_str());
387 }
388 }
389}
390
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500391std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text,
392 std::unordered_map<SkString, CapValue> caps) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700393 fErrorText = "";
394 fErrorCount = 0;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500395 fIRGenerator->start(&caps);
ethannicholasd598f792016-07-25 10:08:54 -0700396 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholas5961bc92016-10-12 06:39:56 -0700397 Modifiers::Flag ignored;
ethannicholasb3058bd2016-07-01 08:22:01 -0700398 switch (kind) {
399 case Program::kVertex_Kind:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500400 this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700401 break;
402 case Program::kFragment_Kind:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500403 this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements);
ethannicholasb3058bd2016-07-01 08:22:01 -0700404 break;
405 }
ethannicholasddb37d62016-10-20 09:54:00 -0700406 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
ethannicholas5961bc92016-10-12 06:39:56 -0700407 Modifiers::Flag defaultPrecision;
408 this->internalConvertProgram(text, &defaultPrecision, &elements);
Mike Klein6ad99092016-10-26 10:35:22 -0400409 auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, std::move(elements),
ethannicholasddb37d62016-10-20 09:54:00 -0700410 fIRGenerator->fSymbolTable));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500411 fIRGenerator->finish();
ethannicholasb3058bd2016-07-01 08:22:01 -0700412 this->writeErrorCount();
ethannicholasd598f792016-07-25 10:08:54 -0700413 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700414}
415
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500416void Compiler::error(Position position, SkString msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700417 fErrorCount++;
418 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
419}
420
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500421SkString Compiler::errorText() {
422 SkString result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700423 return result;
424}
425
426void Compiler::writeErrorCount() {
427 if (fErrorCount) {
428 fErrorText += to_string(fErrorCount) + " error";
429 if (fErrorCount > 1) {
430 fErrorText += "s";
431 }
432 fErrorText += "\n";
433 }
434}
435
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500436bool Compiler::toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out) {
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500437 std::unordered_map<SkString, CapValue> capsMap;
438 auto program = this->convertProgram(kind, text, capsMap);
ethannicholasb3058bd2016-07-01 08:22:01 -0700439 if (fErrorCount == 0) {
ethannicholasd598f792016-07-25 10:08:54 -0700440 SkSL::SPIRVCodeGenerator cg(&fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -0700441 cg.generateCode(*program.get(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700442 }
443 return fErrorCount == 0;
444}
445
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500446bool Compiler::toSPIRV(Program::Kind kind, const SkString& text, SkString* out) {
447 SkDynamicMemoryWStream buffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700448 bool result = this->toSPIRV(kind, text, buffer);
449 if (result) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500450 sk_sp<SkData> data(buffer.detachAsData());
451 *out = SkString((const char*) data->data(), data->size());
ethannicholasb3058bd2016-07-01 08:22:01 -0700452 }
ethannicholasf789b382016-08-03 12:43:36 -0700453 return result;
454}
455
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500456static void fill_caps(const GrGLSLCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) {
457#define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name())));
458 CAP(fbFetchSupport);
459 CAP(fbFetchNeedsCustomOutput);
460 CAP(bindlessTextureSupport);
461 CAP(dropsTileOnZeroDivide);
462 CAP(flatInterpolationSupport);
463 CAP(noperspectiveInterpolationSupport);
464 CAP(multisampleInterpolationSupport);
465 CAP(sampleVariablesSupport);
466 CAP(sampleMaskOverrideCoverageSupport);
467 CAP(externalTextureSupport);
468 CAP(texelFetchSupport);
469 CAP(imageLoadStoreSupport);
470 CAP(mustEnableAdvBlendEqs);
471 CAP(mustEnableSpecificAdvBlendEqs);
472 CAP(mustDeclareFragmentShaderOutput);
473 CAP(canUseAnyFunctionInShader);
474#undef CAP
475}
476
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500477bool Compiler::toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps,
478 SkWStream& out) {
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500479 std::unordered_map<SkString, CapValue> capsMap;
480 fill_caps(caps, &capsMap);
481 auto program = this->convertProgram(kind, text, capsMap);
ethannicholasf789b382016-08-03 12:43:36 -0700482 if (fErrorCount == 0) {
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500483 SkSL::GLSLCodeGenerator cg(&fContext, &caps);
ethannicholasf789b382016-08-03 12:43:36 -0700484 cg.generateCode(*program.get(), out);
ethannicholasf789b382016-08-03 12:43:36 -0700485 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700486 return fErrorCount == 0;
487}
488
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500489bool Compiler::toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps,
490 SkString* out) {
491 SkDynamicMemoryWStream buffer;
ethannicholasf789b382016-08-03 12:43:36 -0700492 bool result = this->toGLSL(kind, text, caps, buffer);
493 if (result) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500494 sk_sp<SkData> data(buffer.detachAsData());
495 *out = SkString((const char*) data->data(), data->size());
ethannicholasf789b382016-08-03 12:43:36 -0700496 }
497 return result;
498}
499
ethannicholasb3058bd2016-07-01 08:22:01 -0700500} // namespace