blob: cf657e5fc842c33bcaf7ce3919acad0488900e3a [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_COMPILER
9#define SKSL_COMPILER
10
Ethan Nicholasdb80f692019-11-22 14:06:12 -050011#include <map>
ethannicholas22f939e2016-10-13 13:25:34 -070012#include <set>
Ethan Nicholascb670962017-04-20 19:31:52 -040013#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070014#include <vector>
Ethan Nicholasdb80f692019-11-22 14:06:12 -050015#include "src/sksl/SkSLASTFile.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
ethannicholasb3058bd2016-07-01 08:22:01 -070022
Brian Osman2e29ab52019-09-20 12:19:11 -040023#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
24#include "src/gpu/GrShaderVar.h"
25#endif
26
Ethan Nicholas762466e2017-06-29 10:03:38 -040027#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 Nicholas16c11962018-03-16 12:20:54 -040033#define SK_OUT_BUILTIN 10007
Ethan Nicholaseab2baa2018-04-13 15:16:27 -040034#define SK_LASTFRAGCOLOR_BUILTIN 10008
Ethan Nicholas00543112018-07-31 09:44:36 -040035#define SK_MAIN_X_BUILTIN 10009
36#define SK_MAIN_Y_BUILTIN 10010
Ethan Nicholascd700e92018-08-24 16:43:57 -040037#define SK_WIDTH_BUILTIN 10011
38#define SK_HEIGHT_BUILTIN 10012
Ethan Nicholas762466e2017-06-29 10:03:38 -040039#define SK_FRAGCOORD_BUILTIN 15
Chris Dalton49d14e92018-07-27 12:38:35 -060040#define SK_CLOCKWISE_BUILTIN 17
Chris Daltonb0fd4b12019-10-29 13:41:22 -060041#define SK_SAMPLEMASK_BUILTIN 20
Ethan Nicholas9eded2c2018-03-22 10:10:44 -040042#define SK_VERTEXID_BUILTIN 42
43#define SK_INSTANCEID_BUILTIN 43
Ethan Nicholas762466e2017-06-29 10:03:38 -040044#define SK_CLIPDISTANCE_BUILTIN 3
45#define SK_INVOCATIONID_BUILTIN 8
Ethan Nicholascc305772017-10-13 16:17:45 -040046#define SK_POSITION_BUILTIN 0
ethannicholas5961bc92016-10-12 06:39:56 -070047
ethannicholasb3058bd2016-07-01 08:22:01 -070048namespace SkSL {
49
Brian Osman9b8b4552019-09-30 13:23:14 -040050class ByteCode;
Brian Osman3257dda2019-09-17 12:01:11 -040051class ExternalValue;
ethannicholasb3058bd2016-07-01 08:22:01 -070052class IRGenerator;
Brian Osman107c6662019-12-30 15:02:30 -050053struct PipelineStageArgs;
ethannicholasb3058bd2016-07-01 08:22:01 -070054
55/**
56 * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
Ethan Nicholas941e7e22016-12-12 15:33:30 -050057 * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
ethannicholasb3058bd2016-07-01 08:22:01 -070058 * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
59 * compiled output.
ethannicholas5961bc92016-10-12 06:39:56 -070060 *
61 * See the README for information about SkSL.
ethannicholasb3058bd2016-07-01 08:22:01 -070062 */
Ethan Nicholas4100b7c2019-03-12 11:50:48 -040063class SK_API Compiler : public ErrorReporter {
ethannicholasb3058bd2016-07-01 08:22:01 -070064public:
Robert Phillipsfe8da172018-01-24 14:52:02 +000065 static constexpr const char* RTADJUST_NAME = "sk_RTAdjust";
66 static constexpr const char* PERVERTEX_NAME = "sk_PerVertex";
67
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040068 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 Nicholasce008112018-08-30 09:19:50 -040076 struct FormatArg {
77 enum class Kind {
78 kInput,
79 kOutput,
Brian Osman3b1e4c22019-08-12 15:12:14 -040080 kCoordX,
81 kCoordY,
Ethan Nicholasce008112018-08-30 09:19:50 -040082 kUniform,
Brian Osman2e29ab52019-09-20 12:19:11 -040083 kChildProcessor,
84 kFunctionName
Ethan Nicholasce008112018-08-30 09:19:50 -040085 };
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;
Ethan Nicholasce008112018-08-30 09:19:50 -040095 int fIndex;
Brian Osman87e3bef2020-01-27 16:21:34 -050096 String fCoords;
Ethan Nicholas00543112018-07-31 09:44:36 -040097 };
98
Brian Osman2e29ab52019-09-20 12:19:11 -040099#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 Nicholasfc671ad2019-11-21 11:14:11 -0500108 std::vector<Compiler::FormatArg> fFormatArgs;
Brian Osman2e29ab52019-09-20 12:19:11 -0400109 };
110#endif
111
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400112 Compiler(Flags flags = kNone_Flags);
ethannicholasb3058bd2016-07-01 08:22:01 -0700113
Brian Salomond3b65972017-03-22 12:05:03 -0400114 ~Compiler() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700115
Brian Osman14003822019-04-10 11:47:26 -0400116 Compiler(const Compiler&) = delete;
117 Compiler& operator=(const Compiler&) = delete;
118
Ethan Nicholas91164d12019-05-15 15:29:54 -0400119 /**
120 * Registers an ExternalValue as a top-level symbol which is visible in the global namespace.
121 */
122 void registerExternalValue(ExternalValue* value);
123
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400124 std::unique_ptr<Program> convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500125 const Program::Settings& settings);
ethannicholasb3058bd2016-07-01 08:22:01 -0700126
Ethan Nicholas00543112018-07-31 09:44:36 -0400127 bool optimize(Program& program);
ethannicholasf789b382016-08-03 12:43:36 -0700128
Ethan Nicholas00543112018-07-31 09:44:36 -0400129 std::unique_ptr<Program> specialize(Program& program,
130 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500131
Ethan Nicholas00543112018-07-31 09:44:36 -0400132 bool toSPIRV(Program& program, OutputStream& out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500133
Ethan Nicholas00543112018-07-31 09:44:36 -0400134 bool toSPIRV(Program& program, String* out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700135
Ethan Nicholas00543112018-07-31 09:44:36 -0400136 bool toGLSL(Program& program, OutputStream& out);
Ethan Nicholascc305772017-10-13 16:17:45 -0400137
Ethan Nicholas00543112018-07-31 09:44:36 -0400138 bool toGLSL(Program& program, String* out);
Timothy Liangb8eeb802018-07-23 16:46:16 -0400139
Ethan Nicholas00543112018-07-31 09:44:36 -0400140 bool toMetal(Program& program, OutputStream& out);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400141
Ethan Nicholas00543112018-07-31 09:44:36 -0400142 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 Nicholas0e9401d2019-03-21 11:05:37 -0400148 std::unique_ptr<ByteCode> toByteCode(Program& program);
149
Brian Osman2e29ab52019-09-20 12:19:11 -0400150#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osman107c6662019-12-30 15:02:30 -0500151 bool toPipelineStage(const Program& program, PipelineStageArgs* outArgs);
Brian Osman2e29ab52019-09-20 12:19:11 -0400152#endif
Ethan Nicholas762466e2017-06-29 10:03:38 -0400153
Ethan Nicholas91164d12019-05-15 15:29:54 -0400154 /**
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 Nicholas5b5f0962017-09-11 13:50:14 -0700159 void error(int offset, String msg) override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700160
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400161 String errorText();
ethannicholasb3058bd2016-07-01 08:22:01 -0700162
163 void writeErrorCount();
164
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500165 int errorCount() override {
166 return fErrorCount;
167 }
168
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400169 Context& context() {
170 return *fContext;
171 }
172
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700173 static const char* OperatorName(Token::Kind token);
174
175 static bool IsAssignment(Token::Kind token);
176
ethannicholasb3058bd2016-07-01 08:22:01 -0700177private:
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400178 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 Nicholas86a43402017-01-19 13:32:00 -0500183 void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
184 DefinitionMap* definitions);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500185
Ethan Nicholas86a43402017-01-19 13:32:00 -0500186 void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
ethannicholas22f939e2016-10-13 13:25:34 -0700187
188 void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
189
Ethan Nicholascb670962017-04-20 19:31:52 -0400190 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);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700216 Position position(int offset);
217
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500218 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>> fGPUIntrinsics;
219 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>> fInterpreterIntrinsics;
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500220 std::unique_ptr<ASTFile> fGpuIncludeSource;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400221 std::shared_ptr<SymbolTable> fGpuSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400222 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 Nicholas8da1e652019-05-24 11:01:59 -0400228 std::vector<std::unique_ptr<ProgramElement>> fPipelineInclude;
229 std::shared_ptr<SymbolTable> fPipelineSymbolTable;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500230 std::unique_ptr<ASTFile> fInterpreterIncludeSource;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400231 std::vector<std::unique_ptr<ProgramElement>> fInterpreterInclude;
232 std::shared_ptr<SymbolTable> fInterpreterSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400233
ethannicholasb3058bd2016-07-01 08:22:01 -0700234 std::shared_ptr<SymbolTable> fTypes;
235 IRGenerator* fIRGenerator;
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400236 int fFlags;
ethannicholasb3058bd2016-07-01 08:22:01 -0700237
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700238 const String* fSource;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400239 std::shared_ptr<Context> fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -0700240 int fErrorCount;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400241 String fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700242};
243
Brian Osman107c6662019-12-30 15:02:30 -0500244#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
245struct PipelineStageArgs {
246 String fCode;
247 std::vector<Compiler::FormatArg> fFormatArgs;
248 std::vector<Compiler::GLSLFunction> fFunctions;
249};
250#endif
251
ethannicholasb3058bd2016-07-01 08:22:01 -0700252} // namespace
253
254#endif