CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ExpressionConverter.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "VisitorTracker.hpp"
11
13
16
18
20
21#include <functional>
22
24
32 private:
35
36 public:
37 // Conversion flags enumeration.
38 enum : UInt32 {
39 ConvertVectorSubscripts = (1 << 0), // Converts certain vector subscripts to type constructors.
42 ConvertImplicitCasts = (1 << 3), // Converts implicit type casts to explicit type casts.
43 ConvertInitializerToCtor = (1 << 4), // Converts initializer expressions to type constructors (e.g. "{ 1, 2, 3 }" to "float3(1, 2, 3)").
44 ConvertLog10 = (1 << 5), // Converts "log10(x)" to "(log(x) / log(10))".
45 ConvertUnaryExpression = (1 << 6), // Wraps an unary expression if it's parent expression is also an unary expression (e.g. "-+x" to "-(+x)").
47 ConvertMatrixLayout = (1 << 8), // Converts expressions that depend on the matrix layout (e.g. the argument order of "mul" intrinsic calls).
48 ConvertTextureBracketOp = (1 << 9), // Converts Texture Operator[] accesses into "Load" intrinsic calls.
49 ConvertTextureIntrinsicVec4 = (1 << 10), // Converts Texture intrinsic calls whose return type is a non-4D-vector.
50 ConvertMatrixSubscripts = (1 << 11), // Converts matrix subscripts into function calls to the respective wrapper function.
51 ConvertCompatibleStructs = (1 << 12), // Converts type denoters and struct members when the underlying struct type has a compatible struct.
52 ConvertLiteralHalfToFloat = (1 << 13), // Converts all half literals to float literals (e.g. "1.5h to 1.5f").
53
54 // All conversion flags commonly used before visiting the sub nodes.
57
58 // All conversion flags commonly used after visiting the sub nodes.
60
61 // All conversion flags.
62 All = (~0u),
63 };
64
65 // Converts the expressions in the specified AST.
66 void convert(Program& program, const RawFlags& conversionFlags, const NameMangling& nameMangling);
67
70
71 // Returns the texture dimension of the specified expression.
72 static Int32 getTextureDimFromExpression(Expression* expression, const AST* ast = nullptr);
73
74 // Returns the identifier used for matrix subscript wrapper functions.
76
77 private: // Visitor implementation
79
81
89
100
101 private: // Conversion
102 // Converts the expression according to the specified flags (if enabled in the current conversion).
103 void convertExpression(ExpressionPtr& expression, const RawFlags& flags);
104
105 // Converts the list of expressions (see ConvertExpression).
107
108 // Converts the expression if a vector subscript is used on a scalar type expression.
111
112 // Converts the expression if a matrix subscript is used.
115
116 // Converts the expression from a vector comparison to the respective intrinsic call (e.g. "a < b" -> "lessThan(a, b)").
121
122 // Converts the expression from an image access to the respective intrinsic call (e.g. "image[int2(1, 2)]" -> "imageLoad(image, ivec2(1, 2))").
126
127 // Converts the expression from a sampler buffer access to the texelFetch intrinsic call (e.g. "buffer[2]" -> "texelFetch(buffer, 2)").
130
131 // Converts the expression by moving its sub expression into a bracket (e.g. "-+x" -> "-(+x)").
133
134 // Converts the expression if this is an intrinsic call to "log10" (e.g. "log10(x)" to "(log(x) / log(10))").
136
137 // Converts the expression to the specified target type and according to the specified flags (if enabled in the current conversion).
139
140 // Converts the expression from an initializer list to a type constructor.
142
143 // Converts the initializer into GLSL-ready form.
145
146 // Converts the expression from array access to texture object into a "Load" intrinsic call.
148
149 // Appends vector subscripts to a texture intrinsic call if the intrinsic return type is not a 4D-vector.
151
152 // Converts the specified expression when it refers to a member variable of a struct that has a compatible struct.
154 };
155
156} // namespace CeresEngine::ShaderCompiler
#define DECLARATION_VISIT_PROC(CLASS_NAME)
Definition Visitor.hpp:88
Definition Flags.hpp:235
Common AST expression converter.
Definition ExpressionConverter.hpp:31
void convertExpressionCompatibleStruct(const ExpressionPtr &expression)
static String getMatrixSubscriptWrapperIdent(const NameMangling &nameMangling, const MatrixSubscriptUsage &subscriptUsage)
void convertExpressionSamplerBufferAccess(ExpressionPtr &expression)
void convert(Program &program, const RawFlags &conversionFlags, const NameMangling &nameMangling)
void convertExpressionTargetType(ExpressionPtr &expression, const TypeDenoter &targetTypeDen, bool matchTypeSize=true)
NameMangling mNameMangling
Definition ExpressionConverter.hpp:34
void convertExpressionVectorSubscript(ExpressionPtr &expression)
void convertExpressionTargetTypeInitializer(ExpressionPtr &expression, const InitializerExpression *initExpression, const TypeDenoter &targetTypeDen)
void convertExpressionMatrixSubscriptObject(ExpressionPtr &expression, ObjectExpression *objectExpression)
void convertExpressionVectorCompareTernary(ExpressionPtr &expression, TernaryExpression *ternaryExpression)
RawFlags mConversionFlags
Definition ExpressionConverter.hpp:33
static void convertExpressionIfCastRequired(ExpressionPtr &expression, const DataType targetType, bool matchTypeSize=true)
void convertExpressionVectorCompareUnary(ExpressionPtr &expression, UnaryExpression *unaryExpression)
void convertExpressionTextureIntrinsicVec4(ExpressionPtr &expression)
void convertExpressionSamplerBufferAccessArray(ExpressionPtr &expression, ArrayExpression *arrayExpression)
void convertExpressionIntrinsicCallLog10(ExpressionPtr &expression)
void convertExpressionFormatInitializer(ExpressionPtr &expression, InitializerExpression *initExpression, const TypeDenoter &targetTypeDen)
void convertExpressionImageAccessAssign(ExpressionPtr &expression, const AssignExpression *assignExpression)
static void convertExpressionIfCastRequired(ExpressionPtr &expression, const TypeDenoter &targetTypeDen, bool matchTypeSize=true)
void convertExpressionIntoBracket(ExpressionPtr &expression)
@ ConvertCompatibleStructs
Definition ExpressionConverter.hpp:51
@ ConvertImageAccess
Definition ExpressionConverter.hpp:41
@ ConvertMatrixLayout
Definition ExpressionConverter.hpp:47
@ ConvertLog10
Definition ExpressionConverter.hpp:44
@ ConvertVectorSubscripts
Definition ExpressionConverter.hpp:39
@ ConvertLiteralHalfToFloat
Definition ExpressionConverter.hpp:52
@ ConvertTextureBracketOp
Definition ExpressionConverter.hpp:48
@ ConvertUnaryExpression
Definition ExpressionConverter.hpp:45
@ ConvertImplicitCasts
Definition ExpressionConverter.hpp:42
@ ConvertMatrixSubscripts
Definition ExpressionConverter.hpp:50
@ AllPreVisit
Definition ExpressionConverter.hpp:55
@ ConvertSamplerBufferAccess
Definition ExpressionConverter.hpp:46
@ ConvertTextureIntrinsicVec4
Definition ExpressionConverter.hpp:49
@ ConvertVectorCompare
Definition ExpressionConverter.hpp:40
@ ConvertInitializerToCtor
Definition ExpressionConverter.hpp:43
@ All
Definition ExpressionConverter.hpp:62
@ AllPostVisit
Definition ExpressionConverter.hpp:59
void convertExpressionVectorCompareBinary(ExpressionPtr &expression, BinaryExpression *binaryExpression)
void convertExpressionList(Vector< ExpressionPtr > &expressionList, const RawFlags &flags)
static Int32 getTextureDimFromExpression(Expression *expression, const AST *ast=nullptr)
void convertExpressionMatrixSubscript(ExpressionPtr &expression)
void convertExpressionVectorSubscriptObject(ExpressionPtr &expression, const ObjectExpression *objectExpression)
void convertExpressionTextureBracketOp(ExpressionPtr &expression)
void convertExpressionImageAccessArray(ExpressionPtr &expression, ArrayExpression *arrayExpression, const AssignExpression *assignExpression=nullptr)
void convertExpressionImageAccess(ExpressionPtr &expression)
void convertExpressionVectorCompare(ExpressionPtr &expression)
void convertExpression(ExpressionPtr &expression, const RawFlags &flags)
Definition VisitorTracker.hpp:18
Definition AST.hpp:33
SPtr< Expression > ExpressionPtr
Definition Visitor.hpp:26
DataType
Definition ASTEnums.hpp:159
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::int32_t Int32
Definition DataTypes.hpp:21
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Name mangling descriptor structure for shader input/output variables (also referred to as "varyings")...
Definition ShaderCompiler.hpp:185
Definition TypeDenoter.hpp:82