Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1 | /* |
| 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 Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 12 | #include "include/private/SkSLStatement.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 13 | #include "include/sksl/DSLExpression.h" |
| 14 | #include "include/sksl/DSLStatement.h" |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLMangler.h" |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLOperators.h" |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 17 | #include "src/sksl/SkSLParsedModule.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 19 | #include "src/sksl/ir/SkSLProgram.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 20 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 21 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 22 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
John Stiles | 3befaf2 | 2021-04-23 14:42:22 -0400 | [diff] [blame] | 23 | #include <list> |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 24 | #include <stack> |
| 25 | |
| 26 | class AutoDSLContext; |
| 27 | |
| 28 | namespace SkSL { |
| 29 | |
| 30 | class Compiler; |
| 31 | class Context; |
| 32 | class IRGenerator; |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 33 | class ProgramElement; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 34 | class SymbolTable; |
| 35 | class Type; |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 36 | class Variable; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 37 | |
| 38 | namespace dsl { |
| 39 | |
| 40 | class ErrorHandler; |
| 41 | |
| 42 | /** |
| 43 | * Thread-safe class that tracks per-thread state associated with DSL output. This class is for |
| 44 | * internal use only. |
| 45 | */ |
| 46 | class DSLWriter { |
| 47 | public: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 48 | DSLWriter(SkSL::Compiler* compiler, SkSL::ProgramKind kind, |
| 49 | const SkSL::ProgramSettings& settings, SkSL::ParsedModule module, bool isModule); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 50 | |
Ethan Nicholas | 0584ac0 | 2021-02-17 10:48:14 -0500 | [diff] [blame] | 51 | ~DSLWriter(); |
| 52 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 53 | /** |
| 54 | * Returns the Compiler used by DSL operations in the current thread. |
| 55 | */ |
| 56 | static SkSL::Compiler& Compiler() { |
| 57 | return *Instance().fCompiler; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the IRGenerator used by DSL operations in the current thread. |
| 62 | */ |
| 63 | static SkSL::IRGenerator& IRGenerator(); |
| 64 | |
| 65 | /** |
| 66 | * Returns the Context used by DSL operations in the current thread. |
| 67 | */ |
| 68 | static const SkSL::Context& Context(); |
| 69 | |
| 70 | /** |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 71 | * Returns the collection to which DSL program elements in this thread should be appended. |
| 72 | */ |
| 73 | static std::vector<std::unique_ptr<SkSL::ProgramElement>>& ProgramElements() { |
| 74 | return Instance().fProgramElements; |
| 75 | } |
| 76 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 77 | static std::vector<const ProgramElement*>& SharedElements() { |
| 78 | return Instance().fSharedElements; |
| 79 | } |
| 80 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 81 | /** |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 82 | * Returns the SymbolTable of the current thread's IRGenerator. |
| 83 | */ |
| 84 | static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable(); |
| 85 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 86 | /** |
| 87 | * Returns the current memory pool. |
| 88 | */ |
| 89 | static std::unique_ptr<Pool>& MemoryPool() { return Instance().fPool; } |
| 90 | |
| 91 | /** |
| 92 | * Returns the current modifiers pool. |
| 93 | */ |
| 94 | static std::unique_ptr<ModifiersPool>& GetModifiersPool() { return Instance().fModifiersPool; } |
| 95 | |
| 96 | /** |
| 97 | * Returns the current ProgramConfig. |
| 98 | */ |
| 99 | static std::unique_ptr<ProgramConfig>& GetProgramConfig() { return Instance().fConfig; } |
| 100 | |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 101 | static void Reset(); |
| 102 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 103 | /** |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 104 | * Returns the final pointer to a pooled Modifiers object that should be used to represent the |
| 105 | * given modifiers. |
| 106 | */ |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 107 | static const SkSL::Modifiers* Modifiers(const SkSL::Modifiers& modifiers); |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 108 | |
| 109 | /** |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 110 | * Returns the SkSL variable corresponding to a (non-parameter) DSLVar. |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 111 | */ |
Ethan Nicholas | 707d315 | 2021-03-25 17:49:08 -0400 | [diff] [blame] | 112 | static const SkSL::Variable& Var(DSLVar& var); |
| 113 | |
| 114 | /** |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 115 | * Creates an SkSL variable corresponding to a parameter DSLVar. |
| 116 | */ |
| 117 | static std::unique_ptr<SkSL::Variable> ParameterVar(DSLVar& var); |
| 118 | |
| 119 | /** |
Ethan Nicholas | 707d315 | 2021-03-25 17:49:08 -0400 | [diff] [blame] | 120 | * Returns the SkSL declaration corresponding to a DSLVar. |
| 121 | */ |
| 122 | static std::unique_ptr<SkSL::Statement> Declaration(DSLVar& var); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 123 | |
| 124 | /** |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 125 | * For use in testing only: marks the variable as having been declared, so that it can be |
| 126 | * destroyed without generating errors. |
| 127 | */ |
| 128 | static void MarkDeclared(DSLVar& var); |
| 129 | |
| 130 | /** |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 131 | * Returns the (possibly mangled) final name that should be used for an entity with the given |
| 132 | * raw name. |
| 133 | */ |
| 134 | static const char* Name(const char* name); |
| 135 | |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 136 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 137 | /** |
| 138 | * Returns the fragment processor for which DSL output is being generated for the current |
| 139 | * thread. |
| 140 | */ |
| 141 | static GrGLSLFragmentProcessor* CurrentProcessor() { |
| 142 | SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor"); |
| 143 | return Instance().fStack.top().fProcessor; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the EmitArgs for fragment processor output in the current thread. |
| 148 | */ |
| 149 | static GrGLSLFragmentProcessor::EmitArgs* CurrentEmitArgs() { |
| 150 | SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor"); |
| 151 | return Instance().fStack.top().fEmitArgs; |
| 152 | } |
| 153 | |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 154 | static bool InFragmentProcessor() { |
| 155 | return !Instance().fStack.empty(); |
| 156 | } |
| 157 | |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 158 | /** |
| 159 | * Pushes a new processor / emitArgs pair for the current thread. |
| 160 | */ |
| 161 | static void StartFragmentProcessor(GrGLSLFragmentProcessor* processor, |
| 162 | GrGLSLFragmentProcessor::EmitArgs* emitArgs); |
| 163 | |
| 164 | /** |
| 165 | * Pops the processor / emitArgs pair associated with the current thread. |
| 166 | */ |
| 167 | static void EndFragmentProcessor(); |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 168 | |
| 169 | static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var); |
Ethan Nicholas | ee49efc | 2021-04-09 15:33:53 -0400 | [diff] [blame] | 170 | #else |
| 171 | static bool InFragmentProcessor() { |
| 172 | return false; |
| 173 | } |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 174 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 175 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 176 | static std::unique_ptr<SkSL::Expression> Call(const FunctionDeclaration& function, |
| 177 | ExpressionArray arguments); |
| 178 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 179 | /** |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 180 | * Invokes expr(arguments), where expr is a function or type reference. |
| 181 | */ |
| 182 | static std::unique_ptr<SkSL::Expression> Call(std::unique_ptr<SkSL::Expression> expr, |
| 183 | ExpressionArray arguments); |
| 184 | |
| 185 | /** |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 186 | * Reports an error if the argument is null. Returns its argument unmodified. |
| 187 | */ |
| 188 | static std::unique_ptr<SkSL::Expression> Check(std::unique_ptr<SkSL::Expression> expr); |
| 189 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 190 | static DSLPossibleExpression Coerce(std::unique_ptr<Expression> left, const SkSL::Type& type); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 191 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 192 | static DSLPossibleExpression Construct(const SkSL::Type& type, |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 193 | SkTArray<DSLExpression> rawArgs); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 194 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 195 | static std::unique_ptr<Expression> ConvertBinary(std::unique_ptr<Expression> left, Operator op, |
| 196 | std::unique_ptr<Expression> right); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 197 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 198 | static std::unique_ptr<SkSL::Expression> ConvertField(std::unique_ptr<Expression> base, |
| 199 | const char* name); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 200 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 201 | static std::unique_ptr<Expression> ConvertIndex(std::unique_ptr<Expression> base, |
| 202 | std::unique_ptr<Expression> index); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 203 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 204 | static std::unique_ptr<Expression> ConvertPostfix(std::unique_ptr<Expression> expr, |
| 205 | Operator op); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 206 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 207 | static std::unique_ptr<Expression> ConvertPrefix(Operator op, std::unique_ptr<Expression> expr); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 208 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 209 | static DSLPossibleStatement ConvertSwitch(std::unique_ptr<Expression> value, |
| 210 | ExpressionArray caseValues, |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 211 | SkTArray<SkSL::StatementArray> caseStatements, |
| 212 | bool isStatic); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 213 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 214 | /** |
| 215 | * Sets the ErrorHandler associated with the current thread. This object will be notified when |
| 216 | * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to |
| 217 | * stderr and a fatal exception will be generated. |
| 218 | */ |
| 219 | static void SetErrorHandler(ErrorHandler* errorHandler) { |
| 220 | Instance().fErrorHandler = errorHandler; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler |
| 225 | * (the default), any errors will be dumped to stderr and a fatal exception will be generated. |
| 226 | */ |
Ethan Nicholas | b956304 | 2021-02-25 09:45:49 -0500 | [diff] [blame] | 227 | static void ReportError(const char* msg, PositionInfo* info = nullptr); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 228 | |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 229 | /** |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 230 | * Returns whether name mangling is enabled. Mangling is important for the DSL because its |
| 231 | * variables normally all go into the same symbol table; for instance if you were to translate |
| 232 | * this legal (albeit silly) GLSL code: |
| 233 | * int x; |
| 234 | * { |
| 235 | * int x; |
| 236 | * } |
| 237 | * |
| 238 | * into DSL, you'd end up with: |
| 239 | * DSLVar x1(kInt_Type, "x"); |
| 240 | * DSLVar x2(kInt_Type, "x"); |
| 241 | * Declare(x1); |
| 242 | * Block(Declare(x2)); |
| 243 | * |
| 244 | * with x1 and x2 ending up in the same symbol table. This is fine as long as their effective |
| 245 | * names are different, so mangling prevents this situation from causing problems. |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 246 | */ |
| 247 | static bool ManglingEnabled() { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 248 | return Instance().fSettings.fDSLMangling; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns whether DSLVars should automatically be marked declared upon creation. This is used |
| 253 | * to simplify testing. |
| 254 | */ |
| 255 | static bool MarkVarsDeclared() { |
| 256 | return Instance().fSettings.fDSLMarkVarsDeclared; |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 259 | static std::unique_ptr<SkSL::Program> ReleaseProgram(); |
| 260 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 261 | static DSLWriter& Instance(); |
| 262 | |
| 263 | static void SetInstance(std::unique_ptr<DSLWriter> instance); |
| 264 | |
| 265 | private: |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 266 | std::unique_ptr<SkSL::ProgramConfig> fConfig; |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 267 | std::unique_ptr<SkSL::ModifiersPool> fModifiersPool; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 268 | SkSL::Compiler* fCompiler; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 269 | std::unique_ptr<Pool> fPool; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 270 | SkSL::ProgramConfig* fOldConfig; |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 271 | SkSL::ModifiersPool* fOldModifiersPool; |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 272 | std::vector<std::unique_ptr<SkSL::ProgramElement>> fProgramElements; |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 273 | std::vector<const SkSL::ProgramElement*> fSharedElements; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 274 | ErrorHandler* fErrorHandler = nullptr; |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame^] | 275 | ProgramSettings fSettings; |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 276 | Mangler fMangler; |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 277 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 278 | struct StackFrame { |
| 279 | GrGLSLFragmentProcessor* fProcessor; |
| 280 | GrGLSLFragmentProcessor::EmitArgs* fEmitArgs; |
John Stiles | 3befaf2 | 2021-04-23 14:42:22 -0400 | [diff] [blame] | 281 | SkSL::StatementArray fSavedDeclarations; |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 282 | }; |
John Stiles | 3befaf2 | 2021-04-23 14:42:22 -0400 | [diff] [blame] | 283 | std::stack<StackFrame, std::list<StackFrame>> fStack; |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 284 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 285 | |
| 286 | friend class DSLCore; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 287 | friend class DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | } // namespace dsl |
| 291 | |
| 292 | } // namespace SkSL |
| 293 | |
| 294 | #endif |