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 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 10 | #include "ast/SkSLASTPrecision.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include "SkSLCFGGenerator.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 12 | #include "SkSLGLSLCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include "SkSLIRGenerator.h" |
| 14 | #include "SkSLParser.h" |
| 15 | #include "SkSLSPIRVCodeGenerator.h" |
| 16 | #include "ir/SkSLExpression.h" |
| 17 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 18 | #include "ir/SkSLModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 20 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 21 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | #include "SkMutex.h" |
| 23 | |
| 24 | #define STRINGIFY(x) #x |
| 25 | |
| 26 | // include the built-in shader symbols as static strings |
| 27 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 28 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | #include "sksl.include" |
| 30 | ; |
| 31 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 32 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | #include "sksl_vert.include" |
| 34 | ; |
| 35 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 36 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | #include "sksl_frag.include" |
| 38 | ; |
| 39 | |
| 40 | namespace SkSL { |
| 41 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 42 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | : fErrorCount(0) { |
| 44 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 45 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 46 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 48 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 49 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | 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 Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 72 | types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | ADD_TYPE(Mat2x3); |
| 74 | ADD_TYPE(Mat2x4); |
| 75 | ADD_TYPE(Mat3x2); |
| 76 | ADD_TYPE(Mat3x3); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 77 | types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | ADD_TYPE(Mat3x4); |
| 79 | ADD_TYPE(Mat4x2); |
| 80 | ADD_TYPE(Mat4x3); |
| 81 | ADD_TYPE(Mat4x4); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 82 | types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | 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); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 102 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 103 | 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 Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 112 | ADD_TYPE(ISampler2D); |
| 113 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 114 | ADD_TYPE(Image2D); |
| 115 | ADD_TYPE(IImage2D); |
| 116 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 117 | ADD_TYPE(SubpassInput); |
| 118 | ADD_TYPE(SubpassInputMS); |
| 119 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | 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 Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 142 | 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 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 147 | Modifiers::Flag ignored1; |
| 148 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 149 | this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 150 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | ASSERT(!fErrorCount); |
| 152 | } |
| 153 | |
| 154 | Compiler::~Compiler() { |
| 155 | delete fIRGenerator; |
| 156 | } |
| 157 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 158 | // add the definition created by assigning to the lvalue to the definition set |
| 159 | void Compiler::addDefinition(const Expression* lvalue, const Expression* expr, |
| 160 | std::unordered_map<const Variable*, const Expression*>* definitions) { |
| 161 | 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 Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 173 | // (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 |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 175 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 176 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
| 177 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 178 | definitions); |
| 179 | break; |
| 180 | case Expression::kIndex_Kind: |
| 181 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 182 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
| 183 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 184 | definitions); |
| 185 | break; |
| 186 | case Expression::kFieldAccess_Kind: |
| 187 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 188 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
| 189 | fContext.fDefined_Expression.get(), |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 190 | 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 Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 199 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 200 | std::unordered_map<const Variable*, const Expression*>* definitions) { |
| 201 | switch (node.fKind) { |
| 202 | case BasicBlock::Node::kExpression_Kind: { |
| 203 | const Expression* expr = (Expression*) node.fNode; |
| 204 | if (expr->fKind == Expression::kBinary_Kind) { |
| 205 | const BinaryExpression* b = (BinaryExpression*) expr; |
| 206 | if (b->fOperator == Token::EQ) { |
| 207 | this->addDefinition(b->fLeft.get(), b->fRight.get(), definitions); |
| 208 | } |
| 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case BasicBlock::Node::kStatement_Kind: { |
| 213 | const Statement* stmt = (Statement*) node.fNode; |
| 214 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
| 215 | const VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
| 216 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 217 | if (decl.fValue) { |
| 218 | (*definitions)[decl.fVar] = decl.fValue.get(); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 228 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 229 | |
| 230 | // compute definitions after this block |
| 231 | std::unordered_map<const Variable*, const Expression*> after = block.fBefore; |
| 232 | for (const BasicBlock::Node& n : block.fNodes) { |
| 233 | this->addDefinitions(n, &after); |
| 234 | } |
| 235 | |
| 236 | // propagate definitions to exits |
| 237 | for (BlockId exitId : block.fExits) { |
| 238 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 239 | for (const auto& pair : after) { |
| 240 | const Expression* e1 = pair.second; |
| 241 | if (exit.fBefore.find(pair.first) == exit.fBefore.end()) { |
| 242 | exit.fBefore[pair.first] = e1; |
| 243 | } else { |
| 244 | const Expression* e2 = exit.fBefore[pair.first]; |
| 245 | if (e1 != e2) { |
| 246 | // definition has changed, merge and add exit block to worklist |
| 247 | workList->insert(exitId); |
| 248 | if (!e1 || !e2) { |
| 249 | exit.fBefore[pair.first] = nullptr; |
| 250 | } else { |
| 251 | exit.fBefore[pair.first] = fContext.fDefined_Expression.get(); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 260 | // is initially unknown |
| 261 | static std::unordered_map<const Variable*, const Expression*> compute_start_state(const CFG& cfg) { |
| 262 | std::unordered_map<const Variable*, const Expression*> result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 263 | for (const auto& block : cfg.fBlocks) { |
| 264 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 265 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
| 266 | const Statement* s = (Statement*) node.fNode; |
| 267 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 268 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
| 269 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 270 | result[decl.fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 271 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return result; |
| 277 | } |
| 278 | |
| 279 | void Compiler::scanCFG(const FunctionDefinition& f) { |
| 280 | CFG cfg = CFGGenerator().getCFG(f); |
| 281 | |
| 282 | // compute the data flow |
| 283 | cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg); |
| 284 | std::set<BlockId> workList; |
| 285 | for (BlockId i = 0; i < cfg.fBlocks.size(); i++) { |
| 286 | workList.insert(i); |
| 287 | } |
| 288 | while (workList.size()) { |
| 289 | BlockId next = *workList.begin(); |
| 290 | workList.erase(workList.begin()); |
| 291 | this->scanCFG(&cfg, next, &workList); |
| 292 | } |
| 293 | |
| 294 | // check for unreachable code |
| 295 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 296 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 297 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 298 | this->error(cfg.fBlocks[i].fNodes[0].fNode->fPosition, SkString("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | if (fErrorCount) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | // check for undefined variables |
| 306 | for (const BasicBlock& b : cfg.fBlocks) { |
| 307 | std::unordered_map<const Variable*, const Expression*> definitions = b.fBefore; |
| 308 | for (const BasicBlock::Node& n : b.fNodes) { |
| 309 | if (n.fKind == BasicBlock::Node::kExpression_Kind) { |
| 310 | const Expression* expr = (const Expression*) n.fNode; |
| 311 | if (expr->fKind == Expression::kVariableReference_Kind) { |
| 312 | const Variable& var = ((VariableReference*) expr)->fVariable; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 313 | if (var.fStorage == Variable::kLocal_Storage && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 314 | !definitions[&var]) { |
| 315 | this->error(expr->fPosition, |
| 316 | "'" + var.fName + "' has not been assigned"); |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 317 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | this->addDefinitions(n, &definitions); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // check for missing return |
| 325 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 326 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 327 | this->error(f.fPosition, SkString("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 332 | void Compiler::internalConvertProgram(SkString text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 333 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 334 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 335 | Parser parser(text, *fTypes, *this); |
| 336 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 337 | if (fErrorCount) { |
| 338 | return; |
| 339 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 340 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 341 | for (size_t i = 0; i < parsed.size(); i++) { |
| 342 | ASTDeclaration& decl = *parsed[i]; |
| 343 | switch (decl.fKind) { |
| 344 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 345 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 346 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 347 | Variable::kGlobal_Storage); |
| 348 | if (s) { |
| 349 | result->push_back(std::move(s)); |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | case ASTDeclaration::kFunction_Kind: { |
| 354 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 355 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 356 | if (!fErrorCount && f) { |
| 357 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 358 | result->push_back(std::move(f)); |
| 359 | } |
| 360 | break; |
| 361 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 362 | case ASTDeclaration::kModifiers_Kind: { |
| 363 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 364 | (ASTModifiersDeclaration&) decl); |
| 365 | if (f) { |
| 366 | result->push_back(std::move(f)); |
| 367 | } |
| 368 | break; |
| 369 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 370 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 371 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 372 | (ASTInterfaceBlock&) decl); |
| 373 | if (i) { |
| 374 | result->push_back(std::move(i)); |
| 375 | } |
| 376 | break; |
| 377 | } |
| 378 | case ASTDeclaration::kExtension_Kind: { |
| 379 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 380 | if (e) { |
| 381 | result->push_back(std::move(e)); |
| 382 | } |
| 383 | break; |
| 384 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 385 | case ASTDeclaration::kPrecision_Kind: { |
| 386 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 387 | break; |
| 388 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | default: |
| 390 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 395 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 396 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 397 | fErrorText = ""; |
| 398 | fErrorCount = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 399 | fIRGenerator->start(&settings); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 400 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 401 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 402 | switch (kind) { |
| 403 | case Program::kVertex_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 404 | this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 405 | break; |
| 406 | case Program::kFragment_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 407 | this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 408 | break; |
| 409 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 410 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 411 | Modifiers::Flag defaultPrecision; |
| 412 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 413 | auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext, |
| 414 | std::move(elements), |
| 415 | fIRGenerator->fSymbolTable, |
| 416 | fIRGenerator->fInputs)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 417 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 418 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 419 | if (fErrorCount) { |
| 420 | return nullptr; |
| 421 | } |
| 422 | return result; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | bool Compiler::toSPIRV(const Program& program, SkWStream& out) { |
| 427 | SPIRVCodeGenerator cg(&fContext, &program, this, &out); |
| 428 | bool result = cg.generateCode(); |
| 429 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 430 | return result; |
| 431 | } |
| 432 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 433 | bool Compiler::toSPIRV(const Program& program, SkString* out) { |
| 434 | SkDynamicMemoryWStream buffer; |
| 435 | bool result = this->toSPIRV(program, buffer); |
| 436 | if (result) { |
| 437 | sk_sp<SkData> data(buffer.detachAsData()); |
| 438 | *out = SkString((const char*) data->data(), data->size()); |
| 439 | } |
| 440 | return result; |
| 441 | } |
| 442 | |
| 443 | bool Compiler::toGLSL(const Program& program, SkWStream& out) { |
| 444 | GLSLCodeGenerator cg(&fContext, &program, this, &out); |
| 445 | bool result = cg.generateCode(); |
| 446 | this->writeErrorCount(); |
| 447 | return result; |
| 448 | } |
| 449 | |
| 450 | bool Compiler::toGLSL(const Program& program, SkString* out) { |
| 451 | SkDynamicMemoryWStream buffer; |
| 452 | bool result = this->toGLSL(program, buffer); |
| 453 | if (result) { |
| 454 | sk_sp<SkData> data(buffer.detachAsData()); |
| 455 | *out = SkString((const char*) data->data(), data->size()); |
| 456 | } |
| 457 | return result; |
| 458 | } |
| 459 | |
| 460 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 461 | void Compiler::error(Position position, SkString msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 462 | fErrorCount++; |
| 463 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 464 | } |
| 465 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 466 | SkString Compiler::errorText() { |
| 467 | SkString result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 468 | return result; |
| 469 | } |
| 470 | |
| 471 | void Compiler::writeErrorCount() { |
| 472 | if (fErrorCount) { |
| 473 | fErrorText += to_string(fErrorCount) + " error"; |
| 474 | if (fErrorCount > 1) { |
| 475 | fErrorText += "s"; |
| 476 | } |
| 477 | fErrorText += "\n"; |
| 478 | } |
| 479 | } |
| 480 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 481 | } // namespace |