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