Shader: Add support for for-each statements and improve arrays

This commit is contained in:
Jérôme Leclercq
2022-01-02 22:02:11 +01:00
parent aac6e38da2
commit 4fe44339c5
30 changed files with 712 additions and 93 deletions

View File

@@ -64,6 +64,7 @@ namespace Nz::ShaderAst
virtual StatementPtr Clone(DeclareVariableStatement& node);
virtual StatementPtr Clone(DiscardStatement& node);
virtual StatementPtr Clone(ExpressionStatement& node);
virtual StatementPtr Clone(ForEachStatement& node);
virtual StatementPtr Clone(MultiStatement& node);
virtual StatementPtr Clone(NoOpStatement& node);
virtual StatementPtr Clone(ReturnStatement& node);

View File

@@ -54,6 +54,7 @@ namespace Nz::ShaderAst
inline bool Compare(const DeclareVariableStatement& lhs, const DeclareVariableStatement& rhs);
inline bool Compare(const DiscardStatement& lhs, const DiscardStatement& rhs);
inline bool Compare(const ExpressionStatement& lhs, const ExpressionStatement& rhs);
inline bool Compare(const ForEachStatement& lhs, const ForEachStatement& rhs);
inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs);
inline bool Compare(const NoOpStatement& lhs, const NoOpStatement& rhs);
inline bool Compare(const ReturnStatement& lhs, const ReturnStatement& rhs);

View File

@@ -458,6 +458,23 @@ namespace Nz::ShaderAst
return true;
}
bool Compare(const ForEachStatement& lhs, const ForEachStatement& rhs)
{
if (!Compare(lhs.isConst, rhs.isConst))
return false;
if (!Compare(lhs.varName, rhs.varName))
return false;
if (!Compare(lhs.expression, rhs.expression))
return false;
if (!Compare(lhs.statement, rhs.statement))
return false;
return true;
}
inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs)
{
if (!Compare(lhs.statements, rhs.statements))

View File

@@ -52,6 +52,7 @@ NAZARA_SHADERAST_STATEMENT(DeclareOptionStatement)
NAZARA_SHADERAST_STATEMENT(DeclareStructStatement)
NAZARA_SHADERAST_STATEMENT(DeclareVariableStatement)
NAZARA_SHADERAST_STATEMENT(DiscardStatement)
NAZARA_SHADERAST_STATEMENT(ForEachStatement)
NAZARA_SHADERAST_STATEMENT(ExpressionStatement)
NAZARA_SHADERAST_STATEMENT(MultiStatement)
NAZARA_SHADERAST_STATEMENT(NoOpStatement)

View File

@@ -46,6 +46,7 @@ namespace Nz::ShaderAst
void Visit(DeclareVariableStatement& node) override;
void Visit(DiscardStatement& node) override;
void Visit(ExpressionStatement& node) override;
void Visit(ForEachStatement& node) override;
void Visit(MultiStatement& node) override;
void Visit(NoOpStatement& node) override;
void Visit(ReturnStatement& node) override;

View File

@@ -49,6 +49,7 @@ namespace Nz::ShaderAst
void Serialize(DeclareVariableStatement& node);
void Serialize(DiscardStatement& node);
void Serialize(ExpressionStatement& node);
void Serialize(ForEachStatement& node);
void Serialize(MultiStatement& node);
void Serialize(NoOpStatement& node);
void Serialize(ReturnStatement& node);

View File

@@ -340,6 +340,18 @@ namespace Nz::ShaderAst
ExpressionPtr expression;
};
struct NAZARA_SHADER_API ForEachStatement : Statement
{
NodeType GetType() const override;
void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> varIndex;
std::string varName;
ExpressionPtr expression;
StatementPtr statement;
bool isConst = false;
};
struct NAZARA_SHADER_API MultiStatement : Statement
{
NodeType GetType() const override;
@@ -371,7 +383,12 @@ namespace Nz::ShaderAst
StatementPtr body;
};
#define NAZARA_SHADERAST_NODE(X) using X##Ptr = std::unique_ptr<X>;
#include <Nazara/Shader/Ast/AstNodeList.hpp>
inline const ShaderAst::ExpressionType& GetExpressionType(ShaderAst::Expression& expr);
inline ShaderAst::ExpressionType& GetExpressionTypeMut(ShaderAst::Expression& expr);
inline bool IsExpression(NodeType nodeType);
inline bool IsStatement(NodeType nodeType);
}

View File

@@ -13,6 +13,12 @@ namespace Nz::ShaderAst
return expr.cachedExpressionType.value();
}
ShaderAst::ExpressionType& GetExpressionTypeMut(ShaderAst::Expression& expr)
{
assert(expr.cachedExpressionType);
return expr.cachedExpressionType.value();
}
inline bool IsExpression(NodeType nodeType)
{
switch (nodeType)

View File

@@ -40,8 +40,9 @@ namespace Nz::ShaderAst
std::unordered_set<std::string> reservedIdentifiers;
std::unordered_map<std::size_t, ConstantValue> optionValues;
bool makeVariableNameUnique = false;
bool reduceLoopsToWhile = false;
bool removeCompoundAssignments = false;
bool removeOptionDeclaration = true;
bool removeOptionDeclaration = false;
bool removeScalarSwizzling = false;
bool splitMultipleBranches = false;
};
@@ -77,6 +78,7 @@ namespace Nz::ShaderAst
StatementPtr Clone(DeclareVariableStatement& node) override;
StatementPtr Clone(DiscardStatement& node) override;
StatementPtr Clone(ExpressionStatement& node) override;
StatementPtr Clone(ForEachStatement& node) override;
StatementPtr Clone(MultiStatement& node) override;
StatementPtr Clone(WhileStatement& node) override;
@@ -117,6 +119,8 @@ namespace Nz::ShaderAst
void SanitizeIdentifier(std::string& identifier);
void Validate(AccessIndexExpression& node);
void Validate(AssignExpression& node);
void Validate(BinaryExpression& node);
void Validate(CallFunctionExpression& node, const DeclareFunctionStatement* referenceDeclaration);
void Validate(CastExpression& node);
void Validate(DeclareVariableStatement& node);