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 |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 35 | #define SK_MAIN_COORDS_BUILTIN 10009 |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 36 | #define SK_WIDTH_BUILTIN 10011 |
| 37 | #define SK_HEIGHT_BUILTIN 10012 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 38 | #define SK_FRAGCOORD_BUILTIN 15 |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 39 | #define SK_CLOCKWISE_BUILTIN 17 |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 40 | #define SK_SAMPLEMASK_BUILTIN 20 |
Ethan Nicholas | 9eded2c | 2018-03-22 10:10:44 -0400 | [diff] [blame] | 41 | #define SK_VERTEXID_BUILTIN 42 |
| 42 | #define SK_INSTANCEID_BUILTIN 43 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 43 | #define SK_CLIPDISTANCE_BUILTIN 3 |
| 44 | #define SK_INVOCATIONID_BUILTIN 8 |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 45 | #define SK_POSITION_BUILTIN 0 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 46 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | namespace SkSL { |
| 48 | |
Brian Osman | 9b8b455 | 2019-09-30 13:23:14 -0400 | [diff] [blame] | 49 | class ByteCode; |
Brian Osman | 3257dda | 2019-09-17 12:01:11 -0400 | [diff] [blame] | 50 | class ExternalValue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 51 | class IRGenerator; |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 52 | struct PipelineStageArgs; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * 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] | 56 | * 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] | 57 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 58 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 59 | * |
| 60 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 61 | */ |
Ethan Nicholas | 4100b7c | 2019-03-12 11:50:48 -0400 | [diff] [blame] | 62 | class SK_API Compiler : public ErrorReporter { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | public: |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 64 | static constexpr const char* RTADJUST_NAME = "sk_RTAdjust"; |
| 65 | static constexpr const char* PERVERTEX_NAME = "sk_PerVertex"; |
| 66 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 67 | enum Flags { |
| 68 | kNone_Flags = 0, |
| 69 | // permits static if/switch statements to be used with non-constant tests. This is used when |
| 70 | // producing H and CPP code; the static tests don't have to have constant values *yet*, but |
| 71 | // the generated code will contain a static test which then does have to be a constant. |
| 72 | kPermitInvalidStaticTests_Flag = 1, |
| 73 | }; |
| 74 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 75 | struct FormatArg { |
| 76 | enum class Kind { |
| 77 | kInput, |
| 78 | kOutput, |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 79 | kCoords, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 80 | kUniform, |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 81 | kChildProcessor, |
| 82 | kFunctionName |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | FormatArg(Kind kind) |
| 86 | : fKind(kind) {} |
| 87 | |
| 88 | FormatArg(Kind kind, int index) |
| 89 | : fKind(kind) |
| 90 | , fIndex(index) {} |
| 91 | |
| 92 | Kind fKind; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 93 | int fIndex; |
Brian Osman | 87e3bef | 2020-01-27 16:21:34 -0500 | [diff] [blame] | 94 | String fCoords; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 97 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 98 | /** |
| 99 | * Represents the arguments to GrGLSLShaderBuilder::emitFunction. |
| 100 | */ |
| 101 | struct GLSLFunction { |
| 102 | GrSLType fReturnType; |
| 103 | SkString fName; |
| 104 | std::vector<GrShaderVar> fParameters; |
| 105 | SkString fBody; |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 106 | std::vector<Compiler::FormatArg> fFormatArgs; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 107 | }; |
| 108 | #endif |
| 109 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 110 | Compiler(Flags flags = kNone_Flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 111 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 112 | ~Compiler() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | |
Brian Osman | 1400382 | 2019-04-10 11:47:26 -0400 | [diff] [blame] | 114 | Compiler(const Compiler&) = delete; |
| 115 | Compiler& operator=(const Compiler&) = delete; |
| 116 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 117 | /** |
| 118 | * Registers an ExternalValue as a top-level symbol which is visible in the global namespace. |
| 119 | */ |
| 120 | void registerExternalValue(ExternalValue* value); |
| 121 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 122 | std::unique_ptr<Program> convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 123 | const Program::Settings& settings); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 124 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 125 | bool optimize(Program& program); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 126 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 127 | std::unique_ptr<Program> specialize(Program& program, |
| 128 | const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 129 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 130 | bool toSPIRV(Program& program, OutputStream& out); |
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, String* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 133 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 134 | bool toGLSL(Program& program, OutputStream& out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 135 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 136 | bool toGLSL(Program& program, String* out); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 137 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 138 | bool toHLSL(Program& program, String* out); |
| 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 |