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 | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 18 | #include "src/sksl/ir/SkSLProgram.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 19 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 20 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 21 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 22 | #include <stack> |
| 23 | |
| 24 | class AutoDSLContext; |
| 25 | |
| 26 | namespace SkSL { |
| 27 | |
| 28 | class Compiler; |
| 29 | class Context; |
| 30 | class IRGenerator; |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 31 | class ProgramElement; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 32 | class SymbolTable; |
| 33 | class Type; |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 34 | class Variable; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 35 | |
| 36 | namespace dsl { |
| 37 | |
| 38 | class ErrorHandler; |
| 39 | |
| 40 | /** |
| 41 | * Thread-safe class that tracks per-thread state associated with DSL output. This class is for |
| 42 | * internal use only. |
| 43 | */ |
| 44 | class DSLWriter { |
| 45 | public: |
| 46 | DSLWriter(SkSL::Compiler* compiler); |
| 47 | |
Ethan Nicholas | 0584ac0 | 2021-02-17 10:48:14 -0500 | [diff] [blame] | 48 | ~DSLWriter(); |
| 49 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 50 | /** |
| 51 | * Returns the Compiler used by DSL operations in the current thread. |
| 52 | */ |
| 53 | static SkSL::Compiler& Compiler() { |
| 54 | return *Instance().fCompiler; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the IRGenerator used by DSL operations in the current thread. |
| 59 | */ |
| 60 | static SkSL::IRGenerator& IRGenerator(); |
| 61 | |
| 62 | /** |
| 63 | * Returns the Context used by DSL operations in the current thread. |
| 64 | */ |
| 65 | static const SkSL::Context& Context(); |
| 66 | |
| 67 | /** |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 68 | * Returns the collection to which DSL program elements in this thread should be appended. |
| 69 | */ |
| 70 | static std::vector<std::unique_ptr<SkSL::ProgramElement>>& ProgramElements() { |
| 71 | return Instance().fProgramElements; |
| 72 | } |
| 73 | |
| 74 | /** |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 75 | * Returns the SymbolTable of the current thread's IRGenerator. |
| 76 | */ |
| 77 | static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable(); |
| 78 | |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 79 | static void Reset(); |
| 80 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 81 | /** |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 82 | * Returns the final pointer to a pooled Modifiers object that should be used to represent the |
| 83 | * given modifiers. |
| 84 | */ |
| 85 | static const SkSL::Modifiers* Modifiers(SkSL::Modifiers modifiers); |
| 86 | |
| 87 | /** |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 88 | * Returns the SkSL variable corresponding to a DSLVar. |
| 89 | */ |
| 90 | static const SkSL::Variable& Var(const DSLVar& var); |
| 91 | |
| 92 | /** |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame^] | 93 | * For use in testing only: marks the variable as having been declared, so that it can be |
| 94 | * destroyed without generating errors. |
| 95 | */ |
| 96 | static void MarkDeclared(DSLVar& var); |
| 97 | |
| 98 | /** |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 99 | * Returns the (possibly mangled) final name that should be used for an entity with the given |
| 100 | * raw name. |
| 101 | */ |
| 102 | static const char* Name(const char* name); |
| 103 | |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 104 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 105 | /** |
| 106 | * Returns the fragment processor for which DSL output is being generated for the current |
| 107 | * thread. |
| 108 | */ |
| 109 | static GrGLSLFragmentProcessor* CurrentProcessor() { |
| 110 | SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor"); |
| 111 | return Instance().fStack.top().fProcessor; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the EmitArgs for fragment processor output in the current thread. |
| 116 | */ |
| 117 | static GrGLSLFragmentProcessor::EmitArgs* CurrentEmitArgs() { |
| 118 | SkASSERTF(!Instance().fStack.empty(), "This feature requires a FragmentProcessor"); |
| 119 | return Instance().fStack.top().fEmitArgs; |
| 120 | } |
| 121 | |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 122 | static bool InFragmentProcessor() { |
| 123 | return !Instance().fStack.empty(); |
| 124 | } |
| 125 | |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 126 | /** |
| 127 | * Pushes a new processor / emitArgs pair for the current thread. |
| 128 | */ |
| 129 | static void StartFragmentProcessor(GrGLSLFragmentProcessor* processor, |
| 130 | GrGLSLFragmentProcessor::EmitArgs* emitArgs); |
| 131 | |
| 132 | /** |
| 133 | * Pops the processor / emitArgs pair associated with the current thread. |
| 134 | */ |
| 135 | static void EndFragmentProcessor(); |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 136 | |
| 137 | static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var); |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 138 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 139 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 140 | static std::unique_ptr<SkSL::Expression> Call(const FunctionDeclaration& function, |
| 141 | ExpressionArray arguments); |
| 142 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 143 | /** |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 144 | * Reports an error if the argument is null. Returns its argument unmodified. |
| 145 | */ |
| 146 | static std::unique_ptr<SkSL::Expression> Check(std::unique_ptr<SkSL::Expression> expr); |
| 147 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 148 | static DSLPossibleExpression Coerce(std::unique_ptr<Expression> left, const SkSL::Type& type); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 149 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 150 | static DSLPossibleExpression Construct(const SkSL::Type& type, |
| 151 | std::vector<DSLExpression> rawArgs); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 152 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 153 | static std::unique_ptr<Expression> ConvertBinary(std::unique_ptr<Expression> left, Operator op, |
| 154 | std::unique_ptr<Expression> right); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 155 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 156 | static std::unique_ptr<SkSL::Expression> ConvertField(std::unique_ptr<Expression> base, |
| 157 | const char* name); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 158 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 159 | static std::unique_ptr<Expression> ConvertIndex(std::unique_ptr<Expression> base, |
| 160 | std::unique_ptr<Expression> index); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 161 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 162 | static std::unique_ptr<Expression> ConvertPostfix(std::unique_ptr<Expression> expr, |
| 163 | Operator op); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 164 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 165 | 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] | 166 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 167 | static DSLPossibleStatement ConvertSwitch(std::unique_ptr<Expression> value, |
| 168 | ExpressionArray caseValues, |
| 169 | SkTArray<SkSL::StatementArray> caseStatements); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 170 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 171 | /** |
| 172 | * Sets the ErrorHandler associated with the current thread. This object will be notified when |
| 173 | * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to |
| 174 | * stderr and a fatal exception will be generated. |
| 175 | */ |
| 176 | static void SetErrorHandler(ErrorHandler* errorHandler) { |
| 177 | Instance().fErrorHandler = errorHandler; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler |
| 182 | * (the default), any errors will be dumped to stderr and a fatal exception will be generated. |
| 183 | */ |
Ethan Nicholas | b956304 | 2021-02-25 09:45:49 -0500 | [diff] [blame] | 184 | static void ReportError(const char* msg, PositionInfo* info = nullptr); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 185 | |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 186 | /** |
| 187 | * Returns whether name mangling is enabled. This should always be enabled outside of tests. |
| 188 | */ |
| 189 | static bool ManglingEnabled() { |
| 190 | return Instance().fMangle; |
| 191 | } |
| 192 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 193 | static DSLWriter& Instance(); |
| 194 | |
| 195 | static void SetInstance(std::unique_ptr<DSLWriter> instance); |
| 196 | |
| 197 | private: |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 198 | SkSL::ProgramConfig fConfig; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 199 | SkSL::Compiler* fCompiler; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame^] | 200 | std::unique_ptr<Pool> fPool; |
Ethan Nicholas | 0584ac0 | 2021-02-17 10:48:14 -0500 | [diff] [blame] | 201 | std::shared_ptr<SkSL::SymbolTable> fOldSymbolTable; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 202 | SkSL::ProgramConfig* fOldConfig; |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 203 | std::vector<std::unique_ptr<SkSL::ProgramElement>> fProgramElements; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 204 | ErrorHandler* fErrorHandler = nullptr; |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 205 | bool fMangle = true; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame^] | 206 | bool fMarkVarsDeclared = false; |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 207 | Mangler fMangler; |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 208 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 209 | struct StackFrame { |
| 210 | GrGLSLFragmentProcessor* fProcessor; |
| 211 | GrGLSLFragmentProcessor::EmitArgs* fEmitArgs; |
| 212 | }; |
| 213 | std::stack<StackFrame> fStack; |
| 214 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 215 | |
| 216 | friend class DSLCore; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame^] | 217 | friend class DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 218 | friend class ::AutoDSLContext; |
| 219 | }; |
| 220 | |
| 221 | } // namespace dsl |
| 222 | |
| 223 | } // namespace SkSL |
| 224 | |
| 225 | #endif |