blob: 81ab9abe5815f8cdbfc75288e5d66797e9488a9e [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
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_DSLWRITER
9#define SKSL_DSLWRITER
10
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040011#include "include/core/SkStringView.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -050012#include "include/private/SkSLModifiers.h"
Ethan Nicholas24c17722021-03-09 13:10:59 -050013#include "include/private/SkSLStatement.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -050014#include "include/sksl/DSLExpression.h"
15#include "include/sksl/DSLStatement.h"
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050016#include "src/sksl/SkSLMangler.h"
John Stiles45990502021-02-16 10:55:27 -050017#include "src/sksl/SkSLOperators.h"
Ethan Nicholas55a63af2021-05-18 10:12:58 -040018#include "src/sksl/SkSLParsedModule.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050019#include "src/sksl/ir/SkSLExpressionStatement.h"
20#include "src/sksl/ir/SkSLProgram.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050021#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
22#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
23#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
John Stiles3befaf22021-04-23 14:42:22 -040024#include <list>
Ethan Nicholas95046142021-01-07 10:57:27 -050025#include <stack>
26
27class AutoDSLContext;
28
29namespace SkSL {
30
31class Compiler;
32class Context;
33class IRGenerator;
Ethan Nicholasdaed2592021-03-04 14:30:25 -050034class ProgramElement;
Ethan Nicholas95046142021-01-07 10:57:27 -050035class SymbolTable;
36class Type;
Ethan Nicholasdaed2592021-03-04 14:30:25 -050037class Variable;
Ethan Nicholas95046142021-01-07 10:57:27 -050038
39namespace dsl {
40
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040041class DSLGlobalVar;
42class DSLParameter;
43class DSLVar;
Ethan Nicholas95046142021-01-07 10:57:27 -050044class ErrorHandler;
45
46/**
47 * Thread-safe class that tracks per-thread state associated with DSL output. This class is for
48 * internal use only.
49 */
50class DSLWriter {
51public:
Ethan Nicholas55a63af2021-05-18 10:12:58 -040052 DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind,
53 const SkSL::ProgramSettings& settings, SkSL::ParsedModule module, bool isModule);
Ethan Nicholas95046142021-01-07 10:57:27 -050054
Ethan Nicholas0584ac02021-02-17 10:48:14 -050055 ~DSLWriter();
56
Ethan Nicholas95046142021-01-07 10:57:27 -050057 /**
58 * Returns the Compiler used by DSL operations in the current thread.
59 */
60 static SkSL::Compiler& Compiler() {
61 return *Instance().fCompiler;
62 }
63
64 /**
65 * Returns the IRGenerator used by DSL operations in the current thread.
66 */
67 static SkSL::IRGenerator& IRGenerator();
68
69 /**
70 * Returns the Context used by DSL operations in the current thread.
71 */
72 static const SkSL::Context& Context();
73
74 /**
Ethan Nicholas459777a2021-07-16 11:16:27 -040075 * Returns the Settings used by DSL operations in the current thread.
76 */
77 static SkSL::ProgramSettings& Settings();
78
79 /**
Ethan Nicholas1ff76092021-01-28 10:02:43 -050080 * Returns the collection to which DSL program elements in this thread should be appended.
81 */
82 static std::vector<std::unique_ptr<SkSL::ProgramElement>>& ProgramElements() {
83 return Instance().fProgramElements;
84 }
85
Ethan Nicholas55a63af2021-05-18 10:12:58 -040086 static std::vector<const ProgramElement*>& SharedElements() {
87 return Instance().fSharedElements;
88 }
89
Ethan Nicholas1ff76092021-01-28 10:02:43 -050090 /**
Ethan Nicholas95046142021-01-07 10:57:27 -050091 * Returns the SymbolTable of the current thread's IRGenerator.
92 */
93 static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
94
Ethan Nicholas55a63af2021-05-18 10:12:58 -040095 /**
96 * Returns the current memory pool.
97 */
98 static std::unique_ptr<Pool>& MemoryPool() { return Instance().fPool; }
99
100 /**
101 * Returns the current modifiers pool.
102 */
103 static std::unique_ptr<ModifiersPool>& GetModifiersPool() { return Instance().fModifiersPool; }
104
105 /**
106 * Returns the current ProgramConfig.
107 */
108 static std::unique_ptr<ProgramConfig>& GetProgramConfig() { return Instance().fConfig; }
109
Ethan Nicholas292a09d2021-07-14 09:52:16 -0400110 static bool IsModule() { return Instance().fIsModule; }
111
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500112 static void Reset();
113
Ethan Nicholas95046142021-01-07 10:57:27 -0500114 /**
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500115 * Returns the final pointer to a pooled Modifiers object that should be used to represent the
116 * given modifiers.
117 */
John Stilesf2872e62021-05-04 11:38:43 -0400118 static const SkSL::Modifiers* Modifiers(const SkSL::Modifiers& modifiers);
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500119
120 /**
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400121 * Returns the SkSL variable corresponding to a DSL var.
Ethan Nicholasd6b26e52021-01-27 07:53:46 -0500122 */
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400123 static const SkSL::Variable* Var(DSLVarBase& var);
Ethan Nicholas707d3152021-03-25 17:49:08 -0400124
125 /**
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400126 * Creates an SkSL variable corresponding to a DSLParameter.
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400127 */
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400128 static std::unique_ptr<SkSL::Variable> CreateParameterVar(DSLParameter& var);
129
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400130
131 /**
Ethan Nicholas707d3152021-03-25 17:49:08 -0400132 * Returns the SkSL declaration corresponding to a DSLVar.
133 */
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400134 static std::unique_ptr<SkSL::Statement> Declaration(DSLVarBase& var);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -0500135
136 /**
Ethan Nicholas961d9442021-03-16 16:37:29 -0400137 * For use in testing only: marks the variable as having been declared, so that it can be
138 * destroyed without generating errors.
139 */
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400140 static void MarkDeclared(DSLVarBase& var);
Ethan Nicholas961d9442021-03-16 16:37:29 -0400141
142 /**
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500143 * Returns the (possibly mangled) final name that should be used for an entity with the given
144 * raw name.
145 */
Ethan Nicholasd0f4d0d2021-06-23 13:51:55 -0400146 static skstd::string_view Name(skstd::string_view name);
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500147
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500148#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
149 /**
150 * Returns the fragment processor for which DSL output is being generated for the current
151 * thread.
152 */
153 static GrGLSLFragmentProcessor* CurrentProcessor() {
154 SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor");
155 return Instance().fStack.top().fProcessor;
156 }
157
158 /**
159 * Returns the EmitArgs for fragment processor output in the current thread.
160 */
161 static GrGLSLFragmentProcessor::EmitArgs* CurrentEmitArgs() {
162 SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor");
163 return Instance().fStack.top().fEmitArgs;
164 }
165
Ethan Nicholas11a15b12021-02-11 15:56:27 -0500166 static bool InFragmentProcessor() {
167 return !Instance().fStack.empty();
168 }
169
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500170 /**
171 * Pushes a new processor / emitArgs pair for the current thread.
172 */
173 static void StartFragmentProcessor(GrGLSLFragmentProcessor* processor,
174 GrGLSLFragmentProcessor::EmitArgs* emitArgs);
175
176 /**
177 * Pops the processor / emitArgs pair associated with the current thread.
178 */
179 static void EndFragmentProcessor();
Ethan Nicholas11a15b12021-02-11 15:56:27 -0500180
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400181 static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLGlobalVar& var);
Ethan Nicholasee49efc2021-04-09 15:33:53 -0400182#else
183 static bool InFragmentProcessor() {
184 return false;
185 }
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500186#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
187
Ethan Nicholasdaed2592021-03-04 14:30:25 -0500188 static std::unique_ptr<SkSL::Expression> Call(const FunctionDeclaration& function,
189 ExpressionArray arguments);
190
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500191 /**
Ethan Nicholas722cb672021-05-06 10:47:06 -0400192 * Invokes expr(arguments), where expr is a function or type reference.
193 */
194 static std::unique_ptr<SkSL::Expression> Call(std::unique_ptr<SkSL::Expression> expr,
195 ExpressionArray arguments);
196
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -0400197 static DSLPossibleExpression Coerce(std::unique_ptr<Expression> expr, const SkSL::Type& type);
Ethan Nicholas92969f22021-01-13 10:38:59 -0500198
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500199 static DSLPossibleExpression Construct(const SkSL::Type& type,
Ethan Nicholas84558932021-04-12 16:56:37 -0400200 SkTArray<DSLExpression> rawArgs);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500201
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500202 static std::unique_ptr<Expression> ConvertBinary(std::unique_ptr<Expression> left, Operator op,
203 std::unique_ptr<Expression> right);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -0500204
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500205 static std::unique_ptr<SkSL::Expression> ConvertField(std::unique_ptr<Expression> base,
Ethan Nicholasd0f4d0d2021-06-23 13:51:55 -0400206 skstd::string_view name);
Ethan Nicholas92969f22021-01-13 10:38:59 -0500207
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500208 static std::unique_ptr<Expression> ConvertIndex(std::unique_ptr<Expression> base,
209 std::unique_ptr<Expression> index);
Ethan Nicholas92969f22021-01-13 10:38:59 -0500210
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500211 static std::unique_ptr<Expression> ConvertPostfix(std::unique_ptr<Expression> expr,
212 Operator op);
Ethan Nicholas92969f22021-01-13 10:38:59 -0500213
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500214 static std::unique_ptr<Expression> ConvertPrefix(Operator op, std::unique_ptr<Expression> expr);
Ethan Nicholas92969f22021-01-13 10:38:59 -0500215
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500216 static DSLPossibleStatement ConvertSwitch(std::unique_ptr<Expression> value,
217 ExpressionArray caseValues,
Ethan Nicholas8a6537d2021-04-30 12:44:00 -0400218 SkTArray<SkSL::StatementArray> caseStatements,
219 bool isStatic);
Ethan Nicholascfefec02021-02-09 15:22:57 -0500220
Ethan Nicholas95046142021-01-07 10:57:27 -0500221 /**
222 * Sets the ErrorHandler associated with the current thread. This object will be notified when
223 * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to
224 * stderr and a fatal exception will be generated.
225 */
226 static void SetErrorHandler(ErrorHandler* errorHandler) {
227 Instance().fErrorHandler = errorHandler;
228 }
229
230 /**
231 * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler
232 * (the default), any errors will be dumped to stderr and a fatal exception will be generated.
233 */
Ethan Nicholasb9563042021-02-25 09:45:49 -0500234 static void ReportError(const char* msg, PositionInfo* info = nullptr);
Ethan Nicholas95046142021-01-07 10:57:27 -0500235
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500236 /**
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400237 * Returns whether name mangling is enabled. Mangling is important for the DSL because its
238 * variables normally all go into the same symbol table; for instance if you were to translate
239 * this legal (albeit silly) GLSL code:
240 * int x;
241 * {
242 * int x;
243 * }
244 *
245 * into DSL, you'd end up with:
246 * DSLVar x1(kInt_Type, "x");
247 * DSLVar x2(kInt_Type, "x");
248 * Declare(x1);
249 * Block(Declare(x2));
250 *
251 * with x1 and x2 ending up in the same symbol table. This is fine as long as their effective
252 * names are different, so mangling prevents this situation from causing problems.
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500253 */
254 static bool ManglingEnabled() {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400255 return Instance().fSettings.fDSLMangling;
256 }
257
258 /**
259 * Returns whether DSLVars should automatically be marked declared upon creation. This is used
260 * to simplify testing.
261 */
262 static bool MarkVarsDeclared() {
263 return Instance().fSettings.fDSLMarkVarsDeclared;
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500264 }
265
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -0400266 /**
267 * Forwards any pending Compiler errors to the DSL ErrorHandler.
268 */
269 static void ReportErrors(PositionInfo pos = PositionInfo());
270
Ethan Nicholas95046142021-01-07 10:57:27 -0500271 static DSLWriter& Instance();
272
273 static void SetInstance(std::unique_ptr<DSLWriter> instance);
274
275private:
Ethan Nicholas624a5292021-04-16 14:54:43 -0400276 std::unique_ptr<SkSL::ProgramConfig> fConfig;
John Stiles10d39d92021-05-04 16:13:14 -0400277 std::unique_ptr<SkSL::ModifiersPool> fModifiersPool;
Ethan Nicholas95046142021-01-07 10:57:27 -0500278 SkSL::Compiler* fCompiler;
Ethan Nicholas961d9442021-03-16 16:37:29 -0400279 std::unique_ptr<Pool> fPool;
John Stilesd1204642021-02-17 16:30:02 -0500280 SkSL::ProgramConfig* fOldConfig;
John Stiles10d39d92021-05-04 16:13:14 -0400281 SkSL::ModifiersPool* fOldModifiersPool;
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500282 std::vector<std::unique_ptr<SkSL::ProgramElement>> fProgramElements;
Ethan Nicholas624a5292021-04-16 14:54:43 -0400283 std::vector<const SkSL::ProgramElement*> fSharedElements;
Ethan Nicholas95046142021-01-07 10:57:27 -0500284 ErrorHandler* fErrorHandler = nullptr;
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400285 ProgramSettings fSettings;
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500286 Mangler fMangler;
Ethan Nicholas292a09d2021-07-14 09:52:16 -0400287 bool fIsModule;
Ethan Nicholas4f3e6a22021-06-15 09:17:05 -0400288 bool fEncounteredErrors = false;
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500289#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
290 struct StackFrame {
291 GrGLSLFragmentProcessor* fProcessor;
292 GrGLSLFragmentProcessor::EmitArgs* fEmitArgs;
John Stiles3befaf22021-04-23 14:42:22 -0400293 SkSL::StatementArray fSavedDeclarations;
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500294 };
John Stiles3befaf22021-04-23 14:42:22 -0400295 std::stack<StackFrame, std::list<StackFrame>> fStack;
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -0500296#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Ethan Nicholas95046142021-01-07 10:57:27 -0500297
298 friend class DSLCore;
Ethan Nicholas961d9442021-03-16 16:37:29 -0400299 friend class DSLVar;
Ethan Nicholas95046142021-01-07 10:57:27 -0500300};
301
302} // namespace dsl
303
304} // namespace SkSL
305
306#endif