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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_COMPILER |
| 9 | #define SKSL_COMPILER |
| 10 | |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 11 | #include <map> |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 12 | #include <set> |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 13 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | #include <vector> |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLASTFile.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCFGGenerator.h" |
| 17 | #include "src/sksl/SkSLContext.h" |
| 18 | #include "src/sksl/SkSLErrorReporter.h" |
| 19 | #include "src/sksl/SkSLLexer.h" |
| 20 | #include "src/sksl/ir/SkSLProgram.h" |
| 21 | #include "src/sksl/ir/SkSLSymbolTable.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 23 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 24 | #include "src/gpu/GrShaderVar.h" |
| 25 | #endif |
| 26 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 27 | #define SK_FRAGCOLOR_BUILTIN 10001 |
| 28 | #define SK_IN_BUILTIN 10002 |
| 29 | #define SK_INCOLOR_BUILTIN 10003 |
| 30 | #define SK_OUTCOLOR_BUILTIN 10004 |
| 31 | #define SK_TRANSFORMEDCOORDS2D_BUILTIN 10005 |
| 32 | #define SK_TEXTURESAMPLERS_BUILTIN 10006 |
Ethan Nicholas | 16c1196 | 2018-03-16 12:20:54 -0400 | [diff] [blame] | 33 | #define SK_OUT_BUILTIN 10007 |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 34 | #define SK_LASTFRAGCOLOR_BUILTIN 10008 |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 35 | #define SK_MAIN_X_BUILTIN 10009 |
| 36 | #define SK_MAIN_Y_BUILTIN 10010 |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 37 | #define SK_WIDTH_BUILTIN 10011 |
| 38 | #define SK_HEIGHT_BUILTIN 10012 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 39 | #define SK_FRAGCOORD_BUILTIN 15 |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 40 | #define SK_CLOCKWISE_BUILTIN 17 |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 41 | #define SK_SAMPLEMASK_BUILTIN 20 |
Ethan Nicholas | 9eded2c | 2018-03-22 10:10:44 -0400 | [diff] [blame] | 42 | #define SK_VERTEXID_BUILTIN 42 |
| 43 | #define SK_INSTANCEID_BUILTIN 43 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 44 | #define SK_CLIPDISTANCE_BUILTIN 3 |
| 45 | #define SK_INVOCATIONID_BUILTIN 8 |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 46 | #define SK_POSITION_BUILTIN 0 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 47 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | namespace SkSL { |
| 49 | |
Brian Osman | 9b8b455 | 2019-09-30 13:23:14 -0400 | [diff] [blame] | 50 | class ByteCode; |
Brian Osman | 3257dda | 2019-09-17 12:01:11 -0400 | [diff] [blame] | 51 | class ExternalValue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | class IRGenerator; |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 53 | struct PipelineStageArgs; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * 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] | 57 | * 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] | 58 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 59 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 60 | * |
| 61 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 62 | */ |
Ethan Nicholas | 4100b7c | 2019-03-12 11:50:48 -0400 | [diff] [blame] | 63 | class SK_API Compiler : public ErrorReporter { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 64 | public: |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 65 | static constexpr const char* RTADJUST_NAME = "sk_RTAdjust"; |
| 66 | static constexpr const char* PERVERTEX_NAME = "sk_PerVertex"; |
| 67 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 68 | enum Flags { |
| 69 | kNone_Flags = 0, |
| 70 | // permits static if/switch statements to be used with non-constant tests. This is used when |
| 71 | // producing H and CPP code; the static tests don't have to have constant values *yet*, but |
| 72 | // the generated code will contain a static test which then does have to be a constant. |
| 73 | kPermitInvalidStaticTests_Flag = 1, |
| 74 | }; |
| 75 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 76 | struct FormatArg { |
| 77 | enum class Kind { |
| 78 | kInput, |
| 79 | kOutput, |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 80 | kCoordX, |
| 81 | kCoordY, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 82 | kUniform, |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 83 | kChildProcessor, |
| 84 | kFunctionName |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | FormatArg(Kind kind) |
| 88 | : fKind(kind) {} |
| 89 | |
| 90 | FormatArg(Kind kind, int index) |
| 91 | : fKind(kind) |
| 92 | , fIndex(index) {} |
| 93 | |
| 94 | Kind fKind; |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 95 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 96 | int fIndex; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 97 | }; |
| 98 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 99 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 100 | /** |
| 101 | * Represents the arguments to GrGLSLShaderBuilder::emitFunction. |
| 102 | */ |
| 103 | struct GLSLFunction { |
| 104 | GrSLType fReturnType; |
| 105 | SkString fName; |
| 106 | std::vector<GrShaderVar> fParameters; |
| 107 | SkString fBody; |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 108 | std::vector<Compiler::FormatArg> fFormatArgs; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 109 | }; |
| 110 | #endif |
| 111 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 112 | Compiler(Flags flags = kNone_Flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 114 | ~Compiler() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 115 | |
Brian Osman | 1400382 | 2019-04-10 11:47:26 -0400 | [diff] [blame] | 116 | Compiler(const Compiler&) = delete; |
| 117 | Compiler& operator=(const Compiler&) = delete; |
| 118 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 119 | /** |
| 120 | * Registers an ExternalValue as a top-level symbol which is visible in the global namespace. |
| 121 | */ |
| 122 | void registerExternalValue(ExternalValue* value); |
| 123 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 124 | std::unique_ptr<Program> convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 125 | const Program::Settings& settings); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 126 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 127 | bool optimize(Program& program); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 128 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 129 | std::unique_ptr<Program> specialize(Program& program, |
| 130 | const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 131 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 132 | bool toSPIRV(Program& program, OutputStream& out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 133 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 134 | bool toSPIRV(Program& program, String* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 136 | bool toGLSL(Program& program, OutputStream& out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 137 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 138 | bool toGLSL(Program& program, String* out); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 139 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 140 | bool toMetal(Program& program, OutputStream& out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 141 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 142 | bool toMetal(Program& program, String* out); |
| 143 | |
| 144 | bool toCPP(Program& program, String name, OutputStream& out); |
| 145 | |
| 146 | bool toH(Program& program, String name, OutputStream& out); |
| 147 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 148 | std::unique_ptr<ByteCode> toByteCode(Program& program); |
| 149 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 150 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 151 | bool toPipelineStage(const Program& program, PipelineStageArgs* outArgs); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 152 | #endif |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 153 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 154 | /** |
| 155 | * Takes ownership of the given symbol. It will be destroyed when the compiler is destroyed. |
| 156 | */ |
| 157 | Symbol* takeOwnership(std::unique_ptr<Symbol> symbol); |
| 158 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 159 | void error(int offset, String msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 161 | String errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | |
| 163 | void writeErrorCount(); |
| 164 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 165 | int errorCount() override { |
| 166 | return fErrorCount; |
| 167 | } |
| 168 | |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 169 | Context& context() { |
| 170 | return *fContext; |
| 171 | } |
| 172 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 173 | static const char* OperatorName(Token::Kind token); |
| 174 | |
| 175 | static bool IsAssignment(Token::Kind token); |
| 176 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | private: |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 178 | void processIncludeFile(Program::Kind kind, const char* src, size_t length, |
| 179 | std::shared_ptr<SymbolTable> base, |
| 180 | std::vector<std::unique_ptr<ProgramElement>>* outElements, |
| 181 | std::shared_ptr<SymbolTable>* outSymbolTable); |
| 182 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 183 | void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 184 | DefinitionMap* definitions); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 185 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 186 | void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 187 | |
| 188 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 189 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 190 | void computeDataFlow(CFG* cfg); |
| 191 | |
| 192 | /** |
| 193 | * Simplifies the expression pointed to by iter (in both the IR and CFG structures), if |
| 194 | * possible. |
| 195 | */ |
| 196 | void simplifyExpression(DefinitionMap& definitions, |
| 197 | BasicBlock& b, |
| 198 | std::vector<BasicBlock::Node>::iterator* iter, |
| 199 | std::unordered_set<const Variable*>* undefinedVariables, |
| 200 | bool* outUpdated, |
| 201 | bool* outNeedsRescan); |
| 202 | |
| 203 | /** |
| 204 | * Simplifies the statement pointed to by iter (in both the IR and CFG structures), if |
| 205 | * possible. |
| 206 | */ |
| 207 | void simplifyStatement(DefinitionMap& definitions, |
| 208 | BasicBlock& b, |
| 209 | std::vector<BasicBlock::Node>::iterator* iter, |
| 210 | std::unordered_set<const Variable*>* undefinedVariables, |
| 211 | bool* outUpdated, |
| 212 | bool* outNeedsRescan); |
| 213 | |
| 214 | void scanCFG(FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 215 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 216 | Position position(int offset); |
| 217 | |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 218 | std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>> fGPUIntrinsics; |
| 219 | std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>> fInterpreterIntrinsics; |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 220 | std::unique_ptr<ASTFile> fGpuIncludeSource; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 221 | std::shared_ptr<SymbolTable> fGpuSymbolTable; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 222 | std::vector<std::unique_ptr<ProgramElement>> fVertexInclude; |
| 223 | std::shared_ptr<SymbolTable> fVertexSymbolTable; |
| 224 | std::vector<std::unique_ptr<ProgramElement>> fFragmentInclude; |
| 225 | std::shared_ptr<SymbolTable> fFragmentSymbolTable; |
| 226 | std::vector<std::unique_ptr<ProgramElement>> fGeometryInclude; |
| 227 | std::shared_ptr<SymbolTable> fGeometrySymbolTable; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 228 | std::vector<std::unique_ptr<ProgramElement>> fPipelineInclude; |
| 229 | std::shared_ptr<SymbolTable> fPipelineSymbolTable; |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 230 | std::unique_ptr<ASTFile> fInterpreterIncludeSource; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 231 | std::vector<std::unique_ptr<ProgramElement>> fInterpreterInclude; |
| 232 | std::shared_ptr<SymbolTable> fInterpreterSymbolTable; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 233 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | std::shared_ptr<SymbolTable> fTypes; |
| 235 | IRGenerator* fIRGenerator; |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 236 | int fFlags; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 237 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 238 | const String* fSource; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 239 | std::shared_ptr<Context> fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 240 | int fErrorCount; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 241 | String fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 244 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 245 | struct PipelineStageArgs { |
| 246 | String fCode; |
| 247 | std::vector<Compiler::FormatArg> fFormatArgs; |
| 248 | std::vector<Compiler::GLSLFunction> fFunctions; |
| 249 | }; |
| 250 | #endif |
| 251 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 252 | } // namespace |
| 253 | |
| 254 | #endif |