Plasma Engine  2.0
Loading...
Searching...
No Matches
ExpressionAST.h
1#pragma once
2
3#include <Foundation/CodeUtils/Expression/ExpressionDeclarations.h>
4#include <Foundation/Memory/LinearAllocator.h>
5
6class plDGMLGraph;
7
8class PL_FOUNDATION_DLL plExpressionAST
9{
10public:
11 struct NodeType
12 {
13 using StorageType = plUInt8;
14
15 enum Enum
16 {
17 Invalid,
18 Default = Invalid,
19
20 // Unary
21 FirstUnary,
22 Negate,
23 Absolute,
24 Saturate,
25 Sqrt,
26 Exp,
27 Ln,
28 Log2,
29 Log10,
30 Pow2,
31 Sin,
32 Cos,
33 Tan,
34 ASin,
35 ACos,
36 ATan,
37 RadToDeg,
38 DegToRad,
39 Round,
40 Floor,
41 Ceil,
42 Trunc,
43 Frac,
44 Length,
45 Normalize,
46 BitwiseNot,
47 LogicalNot,
48 All,
49 Any,
50 TypeConversion,
51 LastUnary,
52
53 // Binary
54 FirstBinary,
55 Add,
56 Subtract,
57 Multiply,
58 Divide,
59 Modulo,
60 Log,
61 Pow,
62 Min,
63 Max,
64 Dot,
65 Cross,
66 Reflect,
67 BitshiftLeft,
68 BitshiftRight,
69 BitwiseAnd,
70 BitwiseXor,
71 BitwiseOr,
72 Equal,
73 NotEqual,
74 Less,
75 LessEqual,
76 Greater,
77 GreaterEqual,
78 LogicalAnd,
79 LogicalOr,
80 LastBinary,
81
82 // Ternary
83 FirstTernary,
84 Clamp,
85 Select,
86 Lerp,
87 SmoothStep,
88 SmootherStep,
89 LastTernary,
90
92 Swizzle,
93 Input,
94 Output,
95
98
99 Count
100 };
101
102 static bool IsUnary(Enum nodeType);
103 static bool IsBinary(Enum nodeType);
104 static bool IsTernary(Enum nodeType);
105 static bool IsConstant(Enum nodeType);
106 static bool IsSwizzle(Enum nodeType);
107 static bool IsInput(Enum nodeType);
108 static bool IsOutput(Enum nodeType);
109 static bool IsFunctionCall(Enum nodeType);
110 static bool IsConstructorCall(Enum nodeType);
111
112 static bool IsCommutative(Enum nodeType);
113 static bool AlwaysReturnsSingleElement(Enum nodeType);
114
115 static const char* GetName(Enum nodeType);
116 };
117
118 struct DataType
119 {
120 using StorageType = plUInt8;
121
122 enum Enum
123 {
124 Unknown,
125 Unknown2,
126 Unknown3,
127 Unknown4,
128
129 Bool,
130 Bool2,
131 Bool3,
132 Bool4,
133
134 Int,
135 Int2,
136 Int3,
137 Int4,
138
139 Float,
140 Float2,
141 Float3,
142 Float4,
143
144 Count,
145
146 Default = Unknown,
147 };
148
149 static plVariantType::Enum GetVariantType(Enum dataType);
150
151 static Enum FromStreamType(plProcessingStream::DataType dataType);
152
153 PL_ALWAYS_INLINE static plExpression::RegisterType::Enum GetRegisterType(Enum dataType)
154 {
155 return static_cast<plExpression::RegisterType::Enum>(dataType >> 2);
156 }
157
158 PL_ALWAYS_INLINE static Enum FromRegisterType(plExpression::RegisterType::Enum registerType, plUInt32 uiElementCount = 1)
159 {
160 return static_cast<plExpressionAST::DataType::Enum>((registerType << 2) + uiElementCount - 1);
161 }
162
163 PL_ALWAYS_INLINE static plUInt32 GetElementCount(Enum dataType) { return (dataType & 0x3) + 1; }
164
165 static const char* GetName(Enum dataType);
166 };
167
169 {
170 using StorageType = plUInt8;
171
172 enum Enum
173 {
174 X,
175 Y,
176 Z,
177 W,
178
179 R = X,
180 G = Y,
181 B = Z,
182 A = W,
183
184 Count,
185
186 Default = X
187 };
188
189 static const char* GetName(Enum vectorComponent);
190
191 static Enum FromChar(plUInt32 uiChar);
192 };
193
194 struct Node
195 {
196 plEnum<NodeType> m_Type;
197 plEnum<DataType> m_ReturnType;
198 plUInt8 m_uiOverloadIndex = 0xFF;
199 plUInt8 m_uiNumInputElements = 0;
200
201 plUInt32 m_uiHash = 0;
202 };
203
204 struct UnaryOperator : public Node
205 {
206 Node* m_pOperand = nullptr;
207 };
208
209 struct BinaryOperator : public Node
210 {
211 Node* m_pLeftOperand = nullptr;
212 Node* m_pRightOperand = nullptr;
213 };
214
215 struct TernaryOperator : public Node
216 {
217 Node* m_pFirstOperand = nullptr;
218 Node* m_pSecondOperand = nullptr;
219 Node* m_pThirdOperand = nullptr;
220 };
221
222 struct Constant : public Node
223 {
224 plVariant m_Value;
225 };
226
227 struct Swizzle : public Node
228 {
229 plEnum<VectorComponent> m_Components[4];
230 plUInt32 m_NumComponents = 0;
231 Node* m_pExpression = nullptr;
232 };
233
234 struct Input : public Node
235 {
237 };
238
239 struct Output : public Node
240 {
242 Node* m_pExpression = nullptr;
243 };
244
245 struct FunctionCall : public Node
246 {
248 plSmallArray<Node*, 8> m_Arguments;
249 };
250
251 struct ConstructorCall : public Node
252 {
253 plSmallArray<Node*, 4> m_Arguments;
254 };
255
256public:
259
260 UnaryOperator* CreateUnaryOperator(NodeType::Enum type, Node* pOperand, DataType::Enum returnType = DataType::Unknown);
261 BinaryOperator* CreateBinaryOperator(NodeType::Enum type, Node* pLeftOperand, Node* pRightOperand);
262 TernaryOperator* CreateTernaryOperator(NodeType::Enum type, Node* pFirstOperand, Node* pSecondOperand, Node* pThirdOperand);
263 Constant* CreateConstant(const plVariant& value, DataType::Enum dataType = DataType::Float);
264 Swizzle* CreateSwizzle(plStringView sSwizzle, Node* pExpression);
265 Swizzle* CreateSwizzle(plEnum<VectorComponent> component, Node* pExpression);
266 Swizzle* CreateSwizzle(plArrayPtr<plEnum<VectorComponent>> swizzle, Node* pExpression);
267 Input* CreateInput(const plExpression::StreamDesc& desc);
268 Output* CreateOutput(const plExpression::StreamDesc& desc, Node* pExpression);
269 FunctionCall* CreateFunctionCall(const plExpression::FunctionDesc& desc, plArrayPtr<Node*> arguments);
271 ConstructorCall* CreateConstructorCall(DataType::Enum dataType, plArrayPtr<Node*> arguments);
272 ConstructorCall* CreateConstructorCall(Node* pOldValue, Node* pNewValue, plStringView sPartialAssignmentMask);
273
274 static plArrayPtr<Node*> GetChildren(Node* pNode);
275 static plArrayPtr<const Node*> GetChildren(const Node* pNode);
276
277 void PrintGraph(plDGMLGraph& inout_graph) const;
278
279 plSmallArray<Input*, 8> m_InputNodes;
280 plSmallArray<Output*, 8> m_OutputNodes;
281
282 // Transforms
283 Node* TypeDeductionAndConversion(Node* pNode);
284 Node* ReplaceVectorInstructions(Node* pNode);
285 Node* ScalarizeVectorInstructions(Node* pNode);
286 Node* ReplaceUnsupportedInstructions(Node* pNode);
287 Node* FoldConstants(Node* pNode);
288 Node* CommonSubexpressionElimination(Node* pNode);
289 Node* Validate(Node* pNode);
290
291 plResult ScalarizeInputs();
292 plResult ScalarizeOutputs();
293
294private:
295 void ResolveOverloads(Node* pNode);
296
297 static DataType::Enum GetExpectedChildDataType(const Node* pNode, plUInt32 uiChildIndex);
298
299 static void UpdateHash(Node* pNode);
300 static bool IsEqual(const Node* pNodeA, const Node* pNodeB);
301
302 plLinearAllocator<> m_Allocator;
303
304 plSet<plExpression::FunctionDesc> m_FunctionDescs;
305
306 plHashTable<plUInt32, plSmallArray<Node*, 1>> m_NodeDeduplicationTable;
307};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
This class encapsulates building a DGML compatible graph.
Definition DGMLWriter.h:10
Definition ExpressionAST.h:9
Definition HashTable.h:333
Definition LinearAllocator.h:12
DataType
The data types which can be stored in the stream. When adding new data types the GetDataTypeSize() of...
Definition ProcessingStream.h:13
Definition Set.h:238
Definition SmallArray.h:219
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Describes an expression function and its signature, e.g. how many input parameter it has and their ty...
Definition ExpressionDeclarations.h:72
Describes an input or output stream for a expression VM.
Definition ExpressionDeclarations.h:57
Definition ExpressionAST.h:210
Definition ExpressionAST.h:223
Definition ExpressionAST.h:252
Definition ExpressionAST.h:119
Definition ExpressionAST.h:246
Definition ExpressionAST.h:235
Definition ExpressionAST.h:195
Definition ExpressionAST.h:12
Definition ExpressionAST.h:240
Definition ExpressionAST.h:228
Definition ExpressionAST.h:216
Definition ExpressionAST.h:205
Definition ExpressionAST.h:169
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Enum
This enum describes the type of data that is currently stored inside the variant. Note that changes t...
Definition VariantType.h:26