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