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 | */ |
| 7 | |
| 8 | #ifndef SKSL_COMPILER |
| 9 | #define SKSL_COMPILER |
| 10 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include <set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | #include "ir/SkSLProgram.h" |
| 14 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 15 | #include "SkSLCFGGenerator.h" |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 16 | #include "SkSLContext.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | #include "SkSLErrorReporter.h" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 18 | #include "SkSLIRGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 20 | #define SK_FRAGCOLOR_BUILTIN 10001 |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 21 | #define SK_IN_BUILTIN 10002 |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 22 | #define SK_FRAGCOORD_BUILTIN 15 |
| 23 | #define SK_VERTEXID_BUILTIN 5 |
| 24 | #define SK_CLIPDISTANCE_BUILTIN 3 |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 25 | #define SK_INVOCATIONID_BUILTIN 8 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 26 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 27 | namespace SkSL { |
| 28 | |
| 29 | class IRGenerator; |
| 30 | |
| 31 | /** |
| 32 | * Main compiler entry point. This is a traditional compiler design which first parses the .sksl |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 33 | * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 34 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 35 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 36 | * |
| 37 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | */ |
| 39 | class Compiler : public ErrorReporter { |
| 40 | public: |
| 41 | Compiler(); |
| 42 | |
| 43 | ~Compiler(); |
| 44 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 45 | std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text, |
| 46 | const Program::Settings& settings); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 48 | bool toSPIRV(const Program& program, SkWStream& out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 49 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 50 | bool toSPIRV(const Program& program, SkString* out); |
| 51 | |
| 52 | bool toGLSL(const Program& program, SkWStream& out); |
| 53 | |
| 54 | bool toGLSL(const Program& program, SkString* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 56 | void error(Position position, SkString msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 58 | SkString errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | |
| 60 | void writeErrorCount(); |
| 61 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 62 | int errorCount() override { |
| 63 | return fErrorCount; |
| 64 | } |
| 65 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | private: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 67 | void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 68 | DefinitionMap* definitions); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 69 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 70 | void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 71 | |
| 72 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 73 | |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 74 | void scanCFG(const FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 76 | void internalConvertProgram(SkString text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 77 | Modifiers::Flag* defaultPrecision, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 79 | |
| 80 | std::shared_ptr<SymbolTable> fTypes; |
| 81 | IRGenerator* fIRGenerator; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 82 | SkString fSkiaVertText; // FIXME store parsed version instead |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 84 | Context fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | int fErrorCount; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 86 | SkString fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | #endif |