ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #include "SkSLCompiler.h" |
| 9 | |
| 10 | #include <fstream> |
| 11 | #include <streambuf> |
| 12 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 13 | #include "ast/SkSLASTPrecision.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 14 | #include "SkSLCFGGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | #include "SkSLIRGenerator.h" |
| 16 | #include "SkSLParser.h" |
| 17 | #include "SkSLSPIRVCodeGenerator.h" |
| 18 | #include "ir/SkSLExpression.h" |
| 19 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 20 | #include "ir/SkSLModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 22 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 23 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | #include "SkMutex.h" |
| 25 | |
| 26 | #define STRINGIFY(x) #x |
| 27 | |
| 28 | // include the built-in shader symbols as static strings |
| 29 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 30 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | #include "sksl.include" |
| 32 | ; |
| 33 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 34 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | #include "sksl_vert.include" |
| 36 | ; |
| 37 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #include "sksl_frag.include" |
| 40 | ; |
| 41 | |
| 42 | namespace SkSL { |
| 43 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 44 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 45 | : fErrorCount(0) { |
| 46 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 47 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 48 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 50 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 51 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | ADD_TYPE(Void); |
| 53 | ADD_TYPE(Float); |
| 54 | ADD_TYPE(Vec2); |
| 55 | ADD_TYPE(Vec3); |
| 56 | ADD_TYPE(Vec4); |
| 57 | ADD_TYPE(Double); |
| 58 | ADD_TYPE(DVec2); |
| 59 | ADD_TYPE(DVec3); |
| 60 | ADD_TYPE(DVec4); |
| 61 | ADD_TYPE(Int); |
| 62 | ADD_TYPE(IVec2); |
| 63 | ADD_TYPE(IVec3); |
| 64 | ADD_TYPE(IVec4); |
| 65 | ADD_TYPE(UInt); |
| 66 | ADD_TYPE(UVec2); |
| 67 | ADD_TYPE(UVec3); |
| 68 | ADD_TYPE(UVec4); |
| 69 | ADD_TYPE(Bool); |
| 70 | ADD_TYPE(BVec2); |
| 71 | ADD_TYPE(BVec3); |
| 72 | ADD_TYPE(BVec4); |
| 73 | ADD_TYPE(Mat2x2); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 74 | types->addWithoutOwnership("mat2x2", fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | ADD_TYPE(Mat2x3); |
| 76 | ADD_TYPE(Mat2x4); |
| 77 | ADD_TYPE(Mat3x2); |
| 78 | ADD_TYPE(Mat3x3); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 79 | types->addWithoutOwnership("mat3x3", fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 80 | ADD_TYPE(Mat3x4); |
| 81 | ADD_TYPE(Mat4x2); |
| 82 | ADD_TYPE(Mat4x3); |
| 83 | ADD_TYPE(Mat4x4); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 84 | types->addWithoutOwnership("mat4x4", fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | ADD_TYPE(GenType); |
| 86 | ADD_TYPE(GenDType); |
| 87 | ADD_TYPE(GenIType); |
| 88 | ADD_TYPE(GenUType); |
| 89 | ADD_TYPE(GenBType); |
| 90 | ADD_TYPE(Mat); |
| 91 | ADD_TYPE(Vec); |
| 92 | ADD_TYPE(GVec); |
| 93 | ADD_TYPE(GVec2); |
| 94 | ADD_TYPE(GVec3); |
| 95 | ADD_TYPE(GVec4); |
| 96 | ADD_TYPE(DVec); |
| 97 | ADD_TYPE(IVec); |
| 98 | ADD_TYPE(UVec); |
| 99 | ADD_TYPE(BVec); |
| 100 | |
| 101 | ADD_TYPE(Sampler1D); |
| 102 | ADD_TYPE(Sampler2D); |
| 103 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 104 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 105 | ADD_TYPE(SamplerCube); |
| 106 | ADD_TYPE(Sampler2DRect); |
| 107 | ADD_TYPE(Sampler1DArray); |
| 108 | ADD_TYPE(Sampler2DArray); |
| 109 | ADD_TYPE(SamplerCubeArray); |
| 110 | ADD_TYPE(SamplerBuffer); |
| 111 | ADD_TYPE(Sampler2DMS); |
| 112 | ADD_TYPE(Sampler2DMSArray); |
| 113 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame^] | 114 | ADD_TYPE(ISampler2D); |
| 115 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 116 | 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 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 138 | Modifiers::Flag ignored1; |
| 139 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
| 140 | this->internalConvertProgram(SKSL_INCLUDE, &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 141 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 142 | ASSERT(!fErrorCount); |
| 143 | } |
| 144 | |
| 145 | Compiler::~Compiler() { |
| 146 | delete fIRGenerator; |
| 147 | } |
| 148 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 149 | // add the definition created by assigning to the lvalue to the definition set |
| 150 | void Compiler::addDefinition(const Expression* lvalue, const Expression* expr, |
| 151 | std::unordered_map<const Variable*, const Expression*>* definitions) { |
| 152 | switch (lvalue->fKind) { |
| 153 | case Expression::kVariableReference_Kind: { |
| 154 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 155 | if (var.fStorage == Variable::kLocal_Storage) { |
| 156 | (*definitions)[&var] = expr; |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | case Expression::kSwizzle_Kind: |
| 161 | // We consider the variable written to as long as at least some of its components have |
| 162 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 163 | // write to foo.x and then read foo.y), but being stricter could lead to false positives |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 164 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 165 | // but since we pass foo as a whole it is flagged as an error) unless we perform a much |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 166 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 167 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
| 168 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 169 | definitions); |
| 170 | break; |
| 171 | case Expression::kIndex_Kind: |
| 172 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 173 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
| 174 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 175 | definitions); |
| 176 | break; |
| 177 | case Expression::kFieldAccess_Kind: |
| 178 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 179 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
| 180 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 181 | definitions); |
| 182 | break; |
| 183 | default: |
| 184 | // not an lvalue, can't happen |
| 185 | ASSERT(false); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 190 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 191 | std::unordered_map<const Variable*, const Expression*>* definitions) { |
| 192 | switch (node.fKind) { |
| 193 | case BasicBlock::Node::kExpression_Kind: { |
| 194 | const Expression* expr = (Expression*) node.fNode; |
| 195 | if (expr->fKind == Expression::kBinary_Kind) { |
| 196 | const BinaryExpression* b = (BinaryExpression*) expr; |
| 197 | if (b->fOperator == Token::EQ) { |
| 198 | this->addDefinition(b->fLeft.get(), b->fRight.get(), definitions); |
| 199 | } |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | case BasicBlock::Node::kStatement_Kind: { |
| 204 | const Statement* stmt = (Statement*) node.fNode; |
| 205 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
| 206 | const VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
| 207 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 208 | if (decl.fValue) { |
| 209 | (*definitions)[decl.fVar] = decl.fValue.get(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 219 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 220 | |
| 221 | // compute definitions after this block |
| 222 | std::unordered_map<const Variable*, const Expression*> after = block.fBefore; |
| 223 | for (const BasicBlock::Node& n : block.fNodes) { |
| 224 | this->addDefinitions(n, &after); |
| 225 | } |
| 226 | |
| 227 | // propagate definitions to exits |
| 228 | for (BlockId exitId : block.fExits) { |
| 229 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 230 | for (const auto& pair : after) { |
| 231 | const Expression* e1 = pair.second; |
| 232 | if (exit.fBefore.find(pair.first) == exit.fBefore.end()) { |
| 233 | exit.fBefore[pair.first] = e1; |
| 234 | } else { |
| 235 | const Expression* e2 = exit.fBefore[pair.first]; |
| 236 | if (e1 != e2) { |
| 237 | // definition has changed, merge and add exit block to worklist |
| 238 | workList->insert(exitId); |
| 239 | if (!e1 || !e2) { |
| 240 | exit.fBefore[pair.first] = nullptr; |
| 241 | } else { |
| 242 | exit.fBefore[pair.first] = fContext.fDefined_Expression.get(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 251 | // is initially unknown |
| 252 | static std::unordered_map<const Variable*, const Expression*> compute_start_state(const CFG& cfg) { |
| 253 | std::unordered_map<const Variable*, const Expression*> result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 254 | for (const auto& block : cfg.fBlocks) { |
| 255 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 256 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
| 257 | const Statement* s = (Statement*) node.fNode; |
| 258 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 259 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
| 260 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 261 | result[decl.fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 262 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | void Compiler::scanCFG(const FunctionDefinition& f) { |
| 271 | CFG cfg = CFGGenerator().getCFG(f); |
| 272 | |
| 273 | // compute the data flow |
| 274 | cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg); |
| 275 | std::set<BlockId> workList; |
| 276 | for (BlockId i = 0; i < cfg.fBlocks.size(); i++) { |
| 277 | workList.insert(i); |
| 278 | } |
| 279 | while (workList.size()) { |
| 280 | BlockId next = *workList.begin(); |
| 281 | workList.erase(workList.begin()); |
| 282 | this->scanCFG(&cfg, next, &workList); |
| 283 | } |
| 284 | |
| 285 | // check for unreachable code |
| 286 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 287 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 288 | cfg.fBlocks[i].fNodes.size()) { |
| 289 | this->error(cfg.fBlocks[i].fNodes[0].fNode->fPosition, "unreachable"); |
| 290 | } |
| 291 | } |
| 292 | if (fErrorCount) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | // check for undefined variables |
| 297 | for (const BasicBlock& b : cfg.fBlocks) { |
| 298 | std::unordered_map<const Variable*, const Expression*> definitions = b.fBefore; |
| 299 | for (const BasicBlock::Node& n : b.fNodes) { |
| 300 | if (n.fKind == BasicBlock::Node::kExpression_Kind) { |
| 301 | const Expression* expr = (const Expression*) n.fNode; |
| 302 | if (expr->fKind == Expression::kVariableReference_Kind) { |
| 303 | const Variable& var = ((VariableReference*) expr)->fVariable; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 304 | if (var.fStorage == Variable::kLocal_Storage && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 305 | !definitions[&var]) { |
| 306 | this->error(expr->fPosition, |
| 307 | "'" + var.fName + "' has not been assigned"); |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 308 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | this->addDefinitions(n, &definitions); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // check for missing return |
| 316 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 317 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
| 318 | this->error(f.fPosition, "function can exit without returning a value"); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 323 | void Compiler::internalConvertProgram(std::string text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 324 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 325 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 326 | Parser parser(text, *fTypes, *this); |
| 327 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 328 | if (fErrorCount) { |
| 329 | return; |
| 330 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 331 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 332 | for (size_t i = 0; i < parsed.size(); i++) { |
| 333 | ASTDeclaration& decl = *parsed[i]; |
| 334 | switch (decl.fKind) { |
| 335 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 336 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 337 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 338 | Variable::kGlobal_Storage); |
| 339 | if (s) { |
| 340 | result->push_back(std::move(s)); |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | case ASTDeclaration::kFunction_Kind: { |
| 345 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 346 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 347 | if (!fErrorCount && f) { |
| 348 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 349 | result->push_back(std::move(f)); |
| 350 | } |
| 351 | break; |
| 352 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 353 | case ASTDeclaration::kModifiers_Kind: { |
| 354 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 355 | (ASTModifiersDeclaration&) decl); |
| 356 | if (f) { |
| 357 | result->push_back(std::move(f)); |
| 358 | } |
| 359 | break; |
| 360 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 361 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 362 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 363 | (ASTInterfaceBlock&) decl); |
| 364 | if (i) { |
| 365 | result->push_back(std::move(i)); |
| 366 | } |
| 367 | break; |
| 368 | } |
| 369 | case ASTDeclaration::kExtension_Kind: { |
| 370 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 371 | if (e) { |
| 372 | result->push_back(std::move(e)); |
| 373 | } |
| 374 | break; |
| 375 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 376 | case ASTDeclaration::kPrecision_Kind: { |
| 377 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 378 | break; |
| 379 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 380 | default: |
| 381 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::string text) { |
| 387 | fErrorText = ""; |
| 388 | fErrorCount = 0; |
| 389 | fIRGenerator->pushSymbolTable(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 390 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 391 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 392 | switch (kind) { |
| 393 | case Program::kVertex_Kind: |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 394 | this->internalConvertProgram(SKSL_VERT_INCLUDE, &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 395 | break; |
| 396 | case Program::kFragment_Kind: |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 397 | this->internalConvertProgram(SKSL_FRAG_INCLUDE, &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 398 | break; |
| 399 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 400 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 401 | Modifiers::Flag defaultPrecision; |
| 402 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 403 | auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, std::move(elements), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 404 | fIRGenerator->fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 405 | fIRGenerator->popSymbolTable(); |
| 406 | this->writeErrorCount(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 407 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void Compiler::error(Position position, std::string msg) { |
| 411 | fErrorCount++; |
| 412 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 413 | } |
| 414 | |
| 415 | std::string Compiler::errorText() { |
| 416 | std::string result = fErrorText; |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | void Compiler::writeErrorCount() { |
| 421 | if (fErrorCount) { |
| 422 | fErrorText += to_string(fErrorCount) + " error"; |
| 423 | if (fErrorCount > 1) { |
| 424 | fErrorText += "s"; |
| 425 | } |
| 426 | fErrorText += "\n"; |
| 427 | } |
| 428 | } |
| 429 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 430 | bool Compiler::toSPIRV(Program::Kind kind, const std::string& text, std::ostream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 431 | auto program = this->convertProgram(kind, text); |
| 432 | if (fErrorCount == 0) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 433 | SkSL::SPIRVCodeGenerator cg(&fContext); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 434 | cg.generateCode(*program.get(), out); |
| 435 | ASSERT(!out.rdstate()); |
| 436 | } |
| 437 | return fErrorCount == 0; |
| 438 | } |
| 439 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 440 | bool Compiler::toSPIRV(Program::Kind kind, const std::string& text, std::string* out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 441 | std::stringstream buffer; |
| 442 | bool result = this->toSPIRV(kind, text, buffer); |
| 443 | if (result) { |
| 444 | *out = buffer.str(); |
| 445 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 446 | return result; |
| 447 | } |
| 448 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 449 | bool Compiler::toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 450 | std::ostream& out) { |
| 451 | auto program = this->convertProgram(kind, text); |
| 452 | if (fErrorCount == 0) { |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 453 | SkSL::GLSLCodeGenerator cg(&fContext, &caps); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 454 | cg.generateCode(*program.get(), out); |
| 455 | ASSERT(!out.rdstate()); |
| 456 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 457 | return fErrorCount == 0; |
| 458 | } |
| 459 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 460 | bool Compiler::toGLSL(Program::Kind kind, const std::string& text, const GrGLSLCaps& caps, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 461 | std::string* out) { |
| 462 | std::stringstream buffer; |
| 463 | bool result = this->toGLSL(kind, text, caps, buffer); |
| 464 | if (result) { |
| 465 | *out = buffer.str(); |
| 466 | } |
| 467 | return result; |
| 468 | } |
| 469 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 470 | } // namespace |