Shader/ShaderLang: Add support for Unary operators

This commit is contained in:
Lynix 2021-05-16 23:07:25 +02:00
parent 1f05e950e8
commit 525f24af2e
30 changed files with 566 additions and 208 deletions

View File

@ -48,6 +48,7 @@ namespace Nz::ShaderAst
virtual ExpressionPtr Clone(SelectOptionExpression& node); virtual ExpressionPtr Clone(SelectOptionExpression& node);
virtual ExpressionPtr Clone(SwizzleExpression& node); virtual ExpressionPtr Clone(SwizzleExpression& node);
virtual ExpressionPtr Clone(VariableExpression& node); virtual ExpressionPtr Clone(VariableExpression& node);
virtual ExpressionPtr Clone(UnaryExpression& node);
virtual StatementPtr Clone(BranchStatement& node); virtual StatementPtr Clone(BranchStatement& node);
virtual StatementPtr Clone(ConditionalStatement& node); virtual StatementPtr Clone(ConditionalStatement& node);

View File

@ -38,6 +38,7 @@ NAZARA_SHADERAST_EXPRESSION(IntrinsicExpression)
NAZARA_SHADERAST_EXPRESSION(SelectOptionExpression) NAZARA_SHADERAST_EXPRESSION(SelectOptionExpression)
NAZARA_SHADERAST_EXPRESSION(SwizzleExpression) NAZARA_SHADERAST_EXPRESSION(SwizzleExpression)
NAZARA_SHADERAST_EXPRESSION(VariableExpression) NAZARA_SHADERAST_EXPRESSION(VariableExpression)
NAZARA_SHADERAST_EXPRESSION(UnaryExpression)
NAZARA_SHADERAST_STATEMENT(BranchStatement) NAZARA_SHADERAST_STATEMENT(BranchStatement)
NAZARA_SHADERAST_STATEMENT(ConditionalStatement) NAZARA_SHADERAST_STATEMENT(ConditionalStatement)
NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement) NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement)

View File

@ -32,10 +32,12 @@ namespace Nz::ShaderAst
protected: protected:
ExpressionPtr Clone(BinaryExpression& node) override; ExpressionPtr Clone(BinaryExpression& node) override;
ExpressionPtr Clone(ConditionalExpression& node) override; ExpressionPtr Clone(ConditionalExpression& node) override;
ExpressionPtr Clone(UnaryExpression& node) override;
StatementPtr Clone(BranchStatement& node) override; StatementPtr Clone(BranchStatement& node) override;
StatementPtr Clone(ConditionalStatement& node) override; StatementPtr Clone(ConditionalStatement& node) override;
template<BinaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs); template<BinaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs);
template<UnaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& operand);
private: private:
std::optional<UInt64> m_enabledOptions; std::optional<UInt64> m_enabledOptions;

View File

@ -32,6 +32,7 @@ namespace Nz::ShaderAst
void Visit(SelectOptionExpression& node) override; void Visit(SelectOptionExpression& node) override;
void Visit(SwizzleExpression& node) override; void Visit(SwizzleExpression& node) override;
void Visit(VariableExpression& node) override; void Visit(VariableExpression& node) override;
void Visit(UnaryExpression& node) override;
void Visit(BranchStatement& node) override; void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override; void Visit(ConditionalStatement& node) override;

View File

@ -35,6 +35,7 @@ namespace Nz::ShaderAst
void Serialize(SelectOptionExpression& node); void Serialize(SelectOptionExpression& node);
void Serialize(SwizzleExpression& node); void Serialize(SwizzleExpression& node);
void Serialize(VariableExpression& node); void Serialize(VariableExpression& node);
void Serialize(UnaryExpression& node);
void Serialize(BranchStatement& node); void Serialize(BranchStatement& node);
void Serialize(ConditionalStatement& node); void Serialize(ConditionalStatement& node);

View File

@ -43,6 +43,7 @@ namespace Nz::ShaderAst
void Visit(SelectOptionExpression& node) override; void Visit(SelectOptionExpression& node) override;
void Visit(SwizzleExpression& node) override; void Visit(SwizzleExpression& node) override;
void Visit(VariableExpression& node) override; void Visit(VariableExpression& node) override;
void Visit(UnaryExpression& node) override;
ExpressionCategory m_expressionCategory; ExpressionCategory m_expressionCategory;
}; };

View File

@ -29,7 +29,7 @@ namespace Nz::ShaderAst
enum class BinaryType enum class BinaryType
{ {
Add, //< + Add, //< +
Subtract, //< - Subtract, //< -
Multiply, //< * Multiply, //< *
Divide, //< / Divide, //< /
@ -89,6 +89,13 @@ namespace Nz::ShaderAst
Third, Third,
Fourth Fourth
}; };
enum class UnaryType
{
LogicalNot, //< !v
Minus, //< -v
Plus, //< +v
};
} }
#endif // NAZARA_SHADER_ENUMS_HPP #endif // NAZARA_SHADER_ENUMS_HPP

View File

@ -176,6 +176,15 @@ namespace Nz::ShaderAst
std::size_t variableId; std::size_t variableId;
}; };
struct NAZARA_SHADER_API UnaryExpression : public Expression
{
NodeType GetType() const override;
void Visit(AstExpressionVisitor& visitor) override;
UnaryType op;
ExpressionPtr expression;
};
// Statements // Statements
struct Statement; struct Statement;

View File

@ -54,6 +54,7 @@ namespace Nz::ShaderAst
ExpressionPtr Clone(IntrinsicExpression& node) override; ExpressionPtr Clone(IntrinsicExpression& node) override;
ExpressionPtr Clone(SelectOptionExpression& node) override; ExpressionPtr Clone(SelectOptionExpression& node) override;
ExpressionPtr Clone(SwizzleExpression& node) override; ExpressionPtr Clone(SwizzleExpression& node) override;
ExpressionPtr Clone(UnaryExpression& node) override;
StatementPtr Clone(BranchStatement& node) override; StatementPtr Clone(BranchStatement& node) override;
StatementPtr Clone(ConditionalStatement& node) override; StatementPtr Clone(ConditionalStatement& node) override;

View File

@ -86,6 +86,7 @@ namespace Nz
void Visit(ShaderAst::IntrinsicExpression& node) override; void Visit(ShaderAst::IntrinsicExpression& node) override;
void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::SwizzleExpression& node) override;
void Visit(ShaderAst::VariableExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override;
void Visit(ShaderAst::UnaryExpression& node) override;
void Visit(ShaderAst::BranchStatement& node) override; void Visit(ShaderAst::BranchStatement& node) override;
void Visit(ShaderAst::ConditionalStatement& node) override; void Visit(ShaderAst::ConditionalStatement& node) override;

View File

@ -85,6 +85,7 @@ namespace Nz
void Visit(ShaderAst::IntrinsicExpression& node) override; void Visit(ShaderAst::IntrinsicExpression& node) override;
void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::SwizzleExpression& node) override;
void Visit(ShaderAst::VariableExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override;
void Visit(ShaderAst::UnaryExpression& node) override;
void Visit(ShaderAst::BranchStatement& node) override; void Visit(ShaderAst::BranchStatement& node) override;
void Visit(ShaderAst::ConditionalStatement& node) override; void Visit(ShaderAst::ConditionalStatement& node) override;

View File

@ -120,6 +120,11 @@ namespace Nz::ShaderBuilder
{ {
inline std::unique_ptr<ShaderAst::SwizzleExpression> operator()(ShaderAst::ExpressionPtr expression, std::vector<ShaderAst::SwizzleComponent> swizzleComponents) const; inline std::unique_ptr<ShaderAst::SwizzleExpression> operator()(ShaderAst::ExpressionPtr expression, std::vector<ShaderAst::SwizzleComponent> swizzleComponents) const;
}; };
struct Unary
{
inline std::unique_ptr<ShaderAst::UnaryExpression> operator()(ShaderAst::UnaryType op, ShaderAst::ExpressionPtr expression) const;
};
} }
constexpr Impl::AccessMember AccessMember; constexpr Impl::AccessMember AccessMember;
@ -143,6 +148,7 @@ namespace Nz::ShaderBuilder
constexpr Impl::Return Return; constexpr Impl::Return Return;
constexpr Impl::SelectOption SelectOption; constexpr Impl::SelectOption SelectOption;
constexpr Impl::Swizzle Swizzle; constexpr Impl::Swizzle Swizzle;
constexpr Impl::Unary Unary;
} }
#include <Nazara/Shader/ShaderBuilder.inl> #include <Nazara/Shader/ShaderBuilder.inl>

View File

@ -235,6 +235,15 @@ namespace Nz::ShaderBuilder
return swizzleNode; return swizzleNode;
} }
inline std::unique_ptr<ShaderAst::UnaryExpression> Impl::Unary::operator()(ShaderAst::UnaryType op, ShaderAst::ExpressionPtr expression) const
{
auto unaryNode = std::make_unique<ShaderAst::UnaryExpression>();
unaryNode->expression = std::move(expression);
unaryNode->op = op;
return unaryNode;
}
} }
#include <Nazara/Shader/DebugOff.hpp> #include <Nazara/Shader/DebugOff.hpp>

View File

@ -96,9 +96,9 @@ namespace Nz::ShaderLang
// Expressions // Expressions
ShaderAst::ExpressionPtr ParseBinOpRhs(int exprPrecedence, ShaderAst::ExpressionPtr lhs); ShaderAst::ExpressionPtr ParseBinOpRhs(int exprPrecedence, ShaderAst::ExpressionPtr lhs);
ShaderAst::ExpressionPtr ParseExpression(); ShaderAst::ExpressionPtr ParseExpression();
ShaderAst::ExpressionPtr ParseFloatingPointExpression(bool minus = false); ShaderAst::ExpressionPtr ParseFloatingPointExpression();
ShaderAst::ExpressionPtr ParseIdentifier(); ShaderAst::ExpressionPtr ParseIdentifier();
ShaderAst::ExpressionPtr ParseIntegerExpression(bool minus = false); ShaderAst::ExpressionPtr ParseIntegerExpression();
std::vector<ShaderAst::ExpressionPtr> ParseParameters(); std::vector<ShaderAst::ExpressionPtr> ParseParameters();
ShaderAst::ExpressionPtr ParseParenthesisExpression(); ShaderAst::ExpressionPtr ParseParenthesisExpression();
ShaderAst::ExpressionPtr ParsePrimaryExpression(); ShaderAst::ExpressionPtr ParsePrimaryExpression();

View File

@ -62,6 +62,7 @@ namespace Nz
void Visit(ShaderAst::ReturnStatement& node) override; void Visit(ShaderAst::ReturnStatement& node) override;
void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::SwizzleExpression& node) override;
void Visit(ShaderAst::VariableExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override;
void Visit(ShaderAst::UnaryExpression& node) override;
SpirvAstVisitor& operator=(const SpirvAstVisitor&) = delete; SpirvAstVisitor& operator=(const SpirvAstVisitor&) = delete;
SpirvAstVisitor& operator=(SpirvAstVisitor&&) = delete; SpirvAstVisitor& operator=(SpirvAstVisitor&&) = delete;

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
78,83,72,82,0,0,0,1,0,0,0,21,0,0,0,7,0,0,0,17,0,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,1,0,0,0,2,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,1,0,0,0,1,0,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,7,0,0,0,4,0,0,0,1,0,0,0,0,17,0,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,1,0,0,0,2,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,105,110,118,87,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,17,0,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,1,0,0,0,9,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,19,105,110,118,80,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,10,118,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,13,105,110,118,86,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,118,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,17,105,110,118,86,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,16,114,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,19,105,110,118,82,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,11,101,121,101,80,111,115,105,116,105,111,110,7,0,0,0,3,0,0,0,1,0,0,0,0,14,0,0,0,0,3,0,0,0,10,118,105,101,119,101,114,68,97,116,97,6,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,5,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,6,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,4,0,0,0,8,115,101,116,116,105,110,103,115,6,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,3,0,0,0,17,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,5,105,110,80,111,115,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,9,105,110,78,111,114,109,97,108,115,7,0,0,0,3,0,0,0,1,2,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,7,0,0,0,2,0,0,0,1,2,0,0,0,2,0,0,0,17,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,10,118,101,114,116,78,111,114,109,97,108,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,6,118,101,114,116,85,86,7,0,0,0,2,0,0,0,1,2,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,7,0,0,0,4,0,0,0,1,1,0,0,0,0,0,0,0,0,15,0,0,0,4,109,97,105,110,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,5,105,110,112,117,116,2,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,5,0,0,0,18,0,0,0,0,6,111,117,116,112,117,116,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,255,255,255,255,0,0,0,20,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,0,0,0,20,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,10,118,101,114,116,78,111,114,109,97,108,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,9,105,110,78,111,114,109,97,108,115,0,0,0,20,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,0,0,0,0,7,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,0,0,0,0,0,0,0,7,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,10,118,105,101,119,77,97,116,114,105,120,0,0,0,0,0,0,0,7,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,0,0,0,1,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,0,0,0,4,7,0,0,0,4,0,0,0,1,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,5,105,110,80,111,115,0,0,0,6,0,0,0,1,63,128,0,0,255,255,255,255,255,255,255,255,0,0,0,23,0,0,0,7,0,0,0,6,111,117,116,112,117,116, 78,83,72,82,0,0,0,1,0,0,0,22,0,0,0,7,0,0,0,18,0,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,1,0,0,0,2,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,1,0,0,0,1,0,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,7,0,0,0,4,0,0,0,1,0,0,0,0,18,0,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,1,0,0,0,2,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,105,110,118,87,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,18,0,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,1,0,0,0,9,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,19,105,110,118,80,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,10,118,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,13,105,110,118,86,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,118,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,17,105,110,118,86,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,16,114,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,19,105,110,118,82,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,11,101,121,101,80,111,115,105,116,105,111,110,7,0,0,0,3,0,0,0,1,0,0,0,0,15,0,0,0,0,3,0,0,0,10,118,105,101,119,101,114,68,97,116,97,6,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,5,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,6,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,4,0,0,0,8,115,101,116,116,105,110,103,115,6,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,3,0,0,0,18,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,5,105,110,80,111,115,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,9,105,110,78,111,114,109,97,108,115,7,0,0,0,3,0,0,0,1,2,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,7,0,0,0,2,0,0,0,1,2,0,0,0,2,0,0,0,18,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,10,118,101,114,116,78,111,114,109,97,108,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,6,118,101,114,116,85,86,7,0,0,0,2,0,0,0,1,2,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,7,0,0,0,4,0,0,0,1,1,0,0,0,0,0,0,0,0,16,0,0,0,4,109,97,105,110,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,5,105,110,112,117,116,2,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,5,0,0,0,19,0,0,0,0,6,111,117,116,112,117,116,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,255,255,255,255,0,0,0,21,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,0,0,0,21,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,10,118,101,114,116,78,111,114,109,97,108,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,9,105,110,78,111,114,109,97,108,115,0,0,0,21,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,0,0,0,0,7,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,0,0,0,0,0,0,0,7,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,10,118,105,101,119,77,97,116,114,105,120,0,0,0,0,0,0,0,7,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,0,0,0,1,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,0,0,0,4,7,0,0,0,4,0,0,0,1,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,5,105,110,80,111,115,0,0,0,6,0,0,0,1,63,128,0,0,255,255,255,255,255,255,255,255,0,0,0,24,0,0,0,7,0,0,0,6,111,117,116,112,117,116,

View File

@ -301,6 +301,17 @@ namespace Nz::ShaderAst
return clone; return clone;
} }
ExpressionPtr AstCloner::Clone(UnaryExpression& node)
{
auto clone = std::make_unique<UnaryExpression>();
clone->expression = CloneExpression(node.expression);
clone->op = node.op;
clone->cachedExpressionType = node.cachedExpressionType;
return clone;
}
#define NAZARA_SHADERAST_EXPRESSION(NodeType) void AstCloner::Visit(NodeType& node) \ #define NAZARA_SHADERAST_EXPRESSION(NodeType) void AstCloner::Visit(NodeType& node) \
{ \ { \
PushExpression(Clone(node)); \ PushExpression(Clone(node)); \

View File

@ -33,13 +33,14 @@ namespace Nz::ShaderAst
template<typename T> template<typename T>
inline constexpr bool is_complete_v = is_complete<T>::value; inline constexpr bool is_complete_v = is_complete<T>::value;
/*************************************************************************************************/
template<BinaryType Type, typename T1, typename T2> template<BinaryType Type, typename T1, typename T2>
struct PropagateConstantType; struct BinaryConstantPropagation;
// CompEq // CompEq
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompEqBase struct BinaryCompEqBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -48,17 +49,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompEq; struct BinaryCompEq;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompEq, T1, T2> struct BinaryConstantPropagation<BinaryType::CompEq, T1, T2>
{ {
using Op = CompEq<T1, T2>; using Op = BinaryCompEq<T1, T2>;
}; };
// CompGe // CompGe
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompGeBase struct BinaryCompGeBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -67,17 +68,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompGe; struct BinaryCompGe;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompGe, T1, T2> struct BinaryConstantPropagation<BinaryType::CompGe, T1, T2>
{ {
using Op = CompGe<T1, T2>; using Op = BinaryCompGe<T1, T2>;
}; };
// CompGt // CompGt
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompGtBase struct BinaryCompGtBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -86,17 +87,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompGt; struct BinaryCompGt;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompGt, T1, T2> struct BinaryConstantPropagation<BinaryType::CompGt, T1, T2>
{ {
using Op = CompGt<T1, T2>; using Op = BinaryCompGt<T1, T2>;
}; };
// CompLe // CompLe
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompLeBase struct BinaryCompLeBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -105,17 +106,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompLe; struct BinaryCompLe;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompLe, T1, T2> struct BinaryConstantPropagation<BinaryType::CompLe, T1, T2>
{ {
using Op = CompLe<T1, T2>; using Op = BinaryCompLe<T1, T2>;
}; };
// CompLt // CompLt
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompLtBase struct BinaryCompLtBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -124,17 +125,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompLt; struct BinaryCompLt;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompLt, T1, T2> struct BinaryConstantPropagation<BinaryType::CompLt, T1, T2>
{ {
using Op = CompLe<T1, T2>; using Op = BinaryCompLe<T1, T2>;
}; };
// CompNe // CompNe
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompNeBase struct BinaryCompNeBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -143,17 +144,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct CompNe; struct BinaryCompNe;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::CompNe, T1, T2> struct BinaryConstantPropagation<BinaryType::CompNe, T1, T2>
{ {
using Op = CompNe<T1, T2>; using Op = BinaryCompNe<T1, T2>;
}; };
// Addition // Addition
template<typename T1, typename T2> template<typename T1, typename T2>
struct AdditionBase struct BinaryAdditionBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -162,17 +163,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct Addition; struct BinaryAddition;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::Add, T1, T2> struct BinaryConstantPropagation<BinaryType::Add, T1, T2>
{ {
using Op = Addition<T1, T2>; using Op = BinaryAddition<T1, T2>;
}; };
// Division // Division
template<typename T1, typename T2> template<typename T1, typename T2>
struct DivisionBase struct BinaryDivisionBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -181,17 +182,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct Division; struct BinaryDivision;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::Divide, T1, T2> struct BinaryConstantPropagation<BinaryType::Divide, T1, T2>
{ {
using Op = Division<T1, T2>; using Op = BinaryDivision<T1, T2>;
}; };
// Multiplication // Multiplication
template<typename T1, typename T2> template<typename T1, typename T2>
struct MultiplicationBase struct BinaryMultiplicationBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -200,17 +201,17 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct Multiplication; struct BinaryMultiplication;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::Multiply, T1, T2> struct BinaryConstantPropagation<BinaryType::Multiply, T1, T2>
{ {
using Op = Multiplication<T1, T2>; using Op = BinaryMultiplication<T1, T2>;
}; };
// Subtraction // Subtraction
template<typename T1, typename T2> template<typename T1, typename T2>
struct SubtractionBase struct BinarySubtractionBase
{ {
std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs) std::unique_ptr<ConstantExpression> operator()(const T1& lhs, const T2& rhs)
{ {
@ -219,163 +220,251 @@ namespace Nz::ShaderAst
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct Subtraction; struct BinarySubtraction;
template<typename T1, typename T2> template<typename T1, typename T2>
struct PropagateConstantType<BinaryType::Subtract, T1, T2> struct BinaryConstantPropagation<BinaryType::Subtract, T1, T2>
{ {
using Op = Subtraction<T1, T2>; using Op = BinarySubtraction<T1, T2>;
};
/*************************************************************************************************/
template<UnaryType Type, typename T>
struct UnaryConstantPropagation;
// LogicalNot
template<typename T>
struct UnaryLogicalNotBase
{
std::unique_ptr<ConstantExpression> operator()(const T& arg)
{
return ShaderBuilder::Constant(!arg);
}
}; };
#define EnableOptimisation(Op, T1, T2) template<> struct Op<T1, T2> : Op##Base<T1, T2> {} template<typename T>
struct UnaryLogicalNot;
EnableOptimisation(CompEq, bool, bool); template<typename T>
EnableOptimisation(CompEq, double, double); struct UnaryConstantPropagation<UnaryType::LogicalNot, T>
EnableOptimisation(CompEq, float, float); {
EnableOptimisation(CompEq, Nz::Int32, Nz::Int32); using Op = UnaryLogicalNot<T>;
EnableOptimisation(CompEq, Nz::Vector2f, Nz::Vector2f); };
EnableOptimisation(CompEq, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(CompEq, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(CompEq, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(CompEq, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(CompEq, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(CompGe, bool, bool); // Minus
EnableOptimisation(CompGe, double, double); template<typename T>
EnableOptimisation(CompGe, float, float); struct UnaryMinusBase
EnableOptimisation(CompGe, Nz::Int32, Nz::Int32); {
EnableOptimisation(CompGe, Nz::Vector2f, Nz::Vector2f); std::unique_ptr<ConstantExpression> operator()(const T& arg)
EnableOptimisation(CompGe, Nz::Vector3f, Nz::Vector3f); {
EnableOptimisation(CompGe, Nz::Vector4f, Nz::Vector4f); return ShaderBuilder::Constant(-arg);
EnableOptimisation(CompGe, Nz::Vector2i32, Nz::Vector2i32); }
EnableOptimisation(CompGe, Nz::Vector3i32, Nz::Vector3i32); };
EnableOptimisation(CompGe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(CompGt, bool, bool); template<typename T>
EnableOptimisation(CompGt, double, double); struct UnaryMinus;
EnableOptimisation(CompGt, float, float);
EnableOptimisation(CompGt, Nz::Int32, Nz::Int32);
EnableOptimisation(CompGt, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(CompGt, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(CompGt, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(CompGt, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(CompGt, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(CompGt, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(CompLe, bool, bool); template<typename T>
EnableOptimisation(CompLe, double, double); struct UnaryConstantPropagation<UnaryType::Minus, T>
EnableOptimisation(CompLe, float, float); {
EnableOptimisation(CompLe, Nz::Int32, Nz::Int32); using Op = UnaryMinus<T>;
EnableOptimisation(CompLe, Nz::Vector2f, Nz::Vector2f); };
EnableOptimisation(CompLe, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(CompLe, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(CompLe, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(CompLe, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(CompLe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(CompLt, bool, bool); // Plus
EnableOptimisation(CompLt, double, double); template<typename T>
EnableOptimisation(CompLt, float, float); struct UnaryPlusBase
EnableOptimisation(CompLt, Nz::Int32, Nz::Int32); {
EnableOptimisation(CompLt, Nz::Vector2f, Nz::Vector2f); std::unique_ptr<ConstantExpression> operator()(const T& arg)
EnableOptimisation(CompLt, Nz::Vector3f, Nz::Vector3f); {
EnableOptimisation(CompLt, Nz::Vector4f, Nz::Vector4f); return ShaderBuilder::Constant(arg);
EnableOptimisation(CompLt, Nz::Vector2i32, Nz::Vector2i32); }
EnableOptimisation(CompLt, Nz::Vector3i32, Nz::Vector3i32); };
EnableOptimisation(CompLt, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(CompNe, bool, bool); template<typename T>
EnableOptimisation(CompNe, double, double); struct UnaryPlus;
EnableOptimisation(CompNe, float, float);
EnableOptimisation(CompNe, Nz::Int32, Nz::Int32);
EnableOptimisation(CompNe, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(CompNe, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(CompNe, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(CompNe, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(CompNe, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(CompNe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(Addition, double, double); template<typename T>
EnableOptimisation(Addition, float, float); struct UnaryConstantPropagation<UnaryType::Plus, T>
EnableOptimisation(Addition, Nz::Int32, Nz::Int32); {
EnableOptimisation(Addition, Nz::Vector2f, Nz::Vector2f); using Op = UnaryPlus<T>;
EnableOptimisation(Addition, Nz::Vector3f, Nz::Vector3f); };
EnableOptimisation(Addition, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(Addition, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(Addition, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(Addition, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(Division, double, double); #define EnableOptimisation(Op, ...) template<> struct Op<__VA_ARGS__> : Op##Base<__VA_ARGS__> {}
EnableOptimisation(Division, double, Nz::Vector2d);
EnableOptimisation(Division, double, Nz::Vector3d);
EnableOptimisation(Division, double, Nz::Vector4d);
EnableOptimisation(Division, float, float);
EnableOptimisation(Division, float, Nz::Vector2f);
EnableOptimisation(Division, float, Nz::Vector3f);
EnableOptimisation(Division, float, Nz::Vector4f);
EnableOptimisation(Division, Nz::Int32, Nz::Int32);
EnableOptimisation(Division, Nz::Int32, Nz::Vector2i32);
EnableOptimisation(Division, Nz::Int32, Nz::Vector3i32);
EnableOptimisation(Division, Nz::Int32, Nz::Vector4i32);
EnableOptimisation(Division, Nz::Vector2f, float);
EnableOptimisation(Division, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(Division, Nz::Vector3f, float);
EnableOptimisation(Division, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(Division, Nz::Vector4f, float);
EnableOptimisation(Division, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(Division, Nz::Vector2d, double);
EnableOptimisation(Division, Nz::Vector2d, Nz::Vector2d);
EnableOptimisation(Division, Nz::Vector3d, double);
EnableOptimisation(Division, Nz::Vector3d, Nz::Vector3d);
EnableOptimisation(Division, Nz::Vector4d, double);
EnableOptimisation(Division, Nz::Vector4d, Nz::Vector4d);
EnableOptimisation(Division, Nz::Vector2i32, Nz::Int32);
EnableOptimisation(Division, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(Division, Nz::Vector3i32, Nz::Int32);
EnableOptimisation(Division, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(Division, Nz::Vector4i32, Nz::Int32);
EnableOptimisation(Division, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(Multiplication, double, double); // Binary
EnableOptimisation(Multiplication, double, Nz::Vector2d);
EnableOptimisation(Multiplication, double, Nz::Vector3d);
EnableOptimisation(Multiplication, double, Nz::Vector4d);
EnableOptimisation(Multiplication, float, float);
EnableOptimisation(Multiplication, float, Nz::Vector2f);
EnableOptimisation(Multiplication, float, Nz::Vector3f);
EnableOptimisation(Multiplication, float, Nz::Vector4f);
EnableOptimisation(Multiplication, Nz::Int32, Nz::Int32);
EnableOptimisation(Multiplication, Nz::Int32, Nz::Vector2i32);
EnableOptimisation(Multiplication, Nz::Int32, Nz::Vector3i32);
EnableOptimisation(Multiplication, Nz::Int32, Nz::Vector4i32);
EnableOptimisation(Multiplication, Nz::Vector2f, float);
EnableOptimisation(Multiplication, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(Multiplication, Nz::Vector3f, float);
EnableOptimisation(Multiplication, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(Multiplication, Nz::Vector4f, float);
EnableOptimisation(Multiplication, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(Multiplication, Nz::Vector2d, double);
EnableOptimisation(Multiplication, Nz::Vector2d, Nz::Vector2d);
EnableOptimisation(Multiplication, Nz::Vector3d, double);
EnableOptimisation(Multiplication, Nz::Vector3d, Nz::Vector3d);
EnableOptimisation(Multiplication, Nz::Vector4d, double);
EnableOptimisation(Multiplication, Nz::Vector4d, Nz::Vector4d);
EnableOptimisation(Multiplication, Nz::Vector2i32, Nz::Int32);
EnableOptimisation(Multiplication, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(Multiplication, Nz::Vector3i32, Nz::Int32);
EnableOptimisation(Multiplication, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(Multiplication, Nz::Vector4i32, Nz::Int32);
EnableOptimisation(Multiplication, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(Subtraction, double, double); EnableOptimisation(BinaryCompEq, bool, bool);
EnableOptimisation(Subtraction, float, float); EnableOptimisation(BinaryCompEq, double, double);
EnableOptimisation(Subtraction, Nz::Int32, Nz::Int32); EnableOptimisation(BinaryCompEq, float, float);
EnableOptimisation(Subtraction, Nz::Vector2f, Nz::Vector2f); EnableOptimisation(BinaryCompEq, Nz::Int32, Nz::Int32);
EnableOptimisation(Subtraction, Nz::Vector3f, Nz::Vector3f); EnableOptimisation(BinaryCompEq, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(Subtraction, Nz::Vector4f, Nz::Vector4f); EnableOptimisation(BinaryCompEq, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(Subtraction, Nz::Vector2i32, Nz::Vector2i32); EnableOptimisation(BinaryCompEq, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(Subtraction, Nz::Vector3i32, Nz::Vector3i32); EnableOptimisation(BinaryCompEq, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(Subtraction, Nz::Vector4i32, Nz::Vector4i32); EnableOptimisation(BinaryCompEq, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompEq, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryCompGe, bool, bool);
EnableOptimisation(BinaryCompGe, double, double);
EnableOptimisation(BinaryCompGe, float, float);
EnableOptimisation(BinaryCompGe, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryCompGe, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryCompGe, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryCompGe, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryCompGe, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryCompGe, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompGe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryCompGt, bool, bool);
EnableOptimisation(BinaryCompGt, double, double);
EnableOptimisation(BinaryCompGt, float, float);
EnableOptimisation(BinaryCompGt, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryCompGt, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryCompGt, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryCompGt, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryCompGt, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryCompGt, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompGt, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryCompLe, bool, bool);
EnableOptimisation(BinaryCompLe, double, double);
EnableOptimisation(BinaryCompLe, float, float);
EnableOptimisation(BinaryCompLe, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryCompLe, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryCompLe, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryCompLe, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryCompLe, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryCompLe, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompLe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryCompLt, bool, bool);
EnableOptimisation(BinaryCompLt, double, double);
EnableOptimisation(BinaryCompLt, float, float);
EnableOptimisation(BinaryCompLt, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryCompLt, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryCompLt, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryCompLt, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryCompLt, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryCompLt, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompLt, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryCompNe, bool, bool);
EnableOptimisation(BinaryCompNe, double, double);
EnableOptimisation(BinaryCompNe, float, float);
EnableOptimisation(BinaryCompNe, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryCompNe, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryCompNe, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryCompNe, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryCompNe, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryCompNe, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryCompNe, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryAddition, double, double);
EnableOptimisation(BinaryAddition, float, float);
EnableOptimisation(BinaryAddition, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryAddition, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryAddition, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryAddition, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryAddition, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryAddition, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryAddition, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryDivision, double, double);
EnableOptimisation(BinaryDivision, double, Nz::Vector2d);
EnableOptimisation(BinaryDivision, double, Nz::Vector3d);
EnableOptimisation(BinaryDivision, double, Nz::Vector4d);
EnableOptimisation(BinaryDivision, float, float);
EnableOptimisation(BinaryDivision, float, Nz::Vector2f);
EnableOptimisation(BinaryDivision, float, Nz::Vector3f);
EnableOptimisation(BinaryDivision, float, Nz::Vector4f);
EnableOptimisation(BinaryDivision, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryDivision, Nz::Int32, Nz::Vector2i32);
EnableOptimisation(BinaryDivision, Nz::Int32, Nz::Vector3i32);
EnableOptimisation(BinaryDivision, Nz::Int32, Nz::Vector4i32);
EnableOptimisation(BinaryDivision, Nz::Vector2f, float);
EnableOptimisation(BinaryDivision, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryDivision, Nz::Vector3f, float);
EnableOptimisation(BinaryDivision, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryDivision, Nz::Vector4f, float);
EnableOptimisation(BinaryDivision, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryDivision, Nz::Vector2d, double);
EnableOptimisation(BinaryDivision, Nz::Vector2d, Nz::Vector2d);
EnableOptimisation(BinaryDivision, Nz::Vector3d, double);
EnableOptimisation(BinaryDivision, Nz::Vector3d, Nz::Vector3d);
EnableOptimisation(BinaryDivision, Nz::Vector4d, double);
EnableOptimisation(BinaryDivision, Nz::Vector4d, Nz::Vector4d);
EnableOptimisation(BinaryDivision, Nz::Vector2i32, Nz::Int32);
EnableOptimisation(BinaryDivision, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryDivision, Nz::Vector3i32, Nz::Int32);
EnableOptimisation(BinaryDivision, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryDivision, Nz::Vector4i32, Nz::Int32);
EnableOptimisation(BinaryDivision, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinaryMultiplication, double, double);
EnableOptimisation(BinaryMultiplication, double, Nz::Vector2d);
EnableOptimisation(BinaryMultiplication, double, Nz::Vector3d);
EnableOptimisation(BinaryMultiplication, double, Nz::Vector4d);
EnableOptimisation(BinaryMultiplication, float, float);
EnableOptimisation(BinaryMultiplication, float, Nz::Vector2f);
EnableOptimisation(BinaryMultiplication, float, Nz::Vector3f);
EnableOptimisation(BinaryMultiplication, float, Nz::Vector4f);
EnableOptimisation(BinaryMultiplication, Nz::Int32, Nz::Int32);
EnableOptimisation(BinaryMultiplication, Nz::Int32, Nz::Vector2i32);
EnableOptimisation(BinaryMultiplication, Nz::Int32, Nz::Vector3i32);
EnableOptimisation(BinaryMultiplication, Nz::Int32, Nz::Vector4i32);
EnableOptimisation(BinaryMultiplication, Nz::Vector2f, float);
EnableOptimisation(BinaryMultiplication, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinaryMultiplication, Nz::Vector3f, float);
EnableOptimisation(BinaryMultiplication, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinaryMultiplication, Nz::Vector4f, float);
EnableOptimisation(BinaryMultiplication, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinaryMultiplication, Nz::Vector2d, double);
EnableOptimisation(BinaryMultiplication, Nz::Vector2d, Nz::Vector2d);
EnableOptimisation(BinaryMultiplication, Nz::Vector3d, double);
EnableOptimisation(BinaryMultiplication, Nz::Vector3d, Nz::Vector3d);
EnableOptimisation(BinaryMultiplication, Nz::Vector4d, double);
EnableOptimisation(BinaryMultiplication, Nz::Vector4d, Nz::Vector4d);
EnableOptimisation(BinaryMultiplication, Nz::Vector2i32, Nz::Int32);
EnableOptimisation(BinaryMultiplication, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinaryMultiplication, Nz::Vector3i32, Nz::Int32);
EnableOptimisation(BinaryMultiplication, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinaryMultiplication, Nz::Vector4i32, Nz::Int32);
EnableOptimisation(BinaryMultiplication, Nz::Vector4i32, Nz::Vector4i32);
EnableOptimisation(BinarySubtraction, double, double);
EnableOptimisation(BinarySubtraction, float, float);
EnableOptimisation(BinarySubtraction, Nz::Int32, Nz::Int32);
EnableOptimisation(BinarySubtraction, Nz::Vector2f, Nz::Vector2f);
EnableOptimisation(BinarySubtraction, Nz::Vector3f, Nz::Vector3f);
EnableOptimisation(BinarySubtraction, Nz::Vector4f, Nz::Vector4f);
EnableOptimisation(BinarySubtraction, Nz::Vector2i32, Nz::Vector2i32);
EnableOptimisation(BinarySubtraction, Nz::Vector3i32, Nz::Vector3i32);
EnableOptimisation(BinarySubtraction, Nz::Vector4i32, Nz::Vector4i32);
// Unary
EnableOptimisation(UnaryLogicalNot, bool);
EnableOptimisation(UnaryMinus, double);
EnableOptimisation(UnaryMinus, float);
EnableOptimisation(UnaryMinus, Nz::Int32);
EnableOptimisation(UnaryMinus, Nz::Vector2f);
EnableOptimisation(UnaryMinus, Nz::Vector3f);
EnableOptimisation(UnaryMinus, Nz::Vector4f);
EnableOptimisation(UnaryMinus, Nz::Vector2i32);
EnableOptimisation(UnaryMinus, Nz::Vector3i32);
EnableOptimisation(UnaryMinus, Nz::Vector4i32);
EnableOptimisation(UnaryPlus, double);
EnableOptimisation(UnaryPlus, float);
EnableOptimisation(UnaryPlus, Nz::Int32);
EnableOptimisation(UnaryPlus, Nz::Vector2f);
EnableOptimisation(UnaryPlus, Nz::Vector3f);
EnableOptimisation(UnaryPlus, Nz::Vector4f);
EnableOptimisation(UnaryPlus, Nz::Vector2i32);
EnableOptimisation(UnaryPlus, Nz::Vector3i32);
EnableOptimisation(UnaryPlus, Nz::Vector4i32);
#undef EnableOptimisation #undef EnableOptimisation
} }
@ -525,6 +614,40 @@ namespace Nz::ShaderAst
return AstCloner::Clone(node.falsePath); return AstCloner::Clone(node.falsePath);
} }
ExpressionPtr AstOptimizer::Clone(UnaryExpression& node)
{
auto expr = CloneExpression(node.expression);
if (expr->GetType() == NodeType::ConstantExpression)
{
auto constantExpr = static_unique_pointer_cast<ConstantExpression>(std::move(expr));
ExpressionPtr optimized;
switch (node.op)
{
case UnaryType::LogicalNot:
optimized = PropagateConstant<UnaryType::LogicalNot>(std::move(constantExpr));
break;
case UnaryType::Minus:
optimized = PropagateConstant<UnaryType::Minus>(std::move(constantExpr));
break;
case UnaryType::Plus:
optimized = PropagateConstant<UnaryType::Plus>(std::move(constantExpr));
break;
}
if (optimized)
return optimized;
}
auto unary = ShaderBuilder::Unary(node.op, std::move(expr));
unary->cachedExpressionType = node.cachedExpressionType;
return unary;
}
StatementPtr AstOptimizer::Clone(ConditionalStatement& node) StatementPtr AstOptimizer::Clone(ConditionalStatement& node)
{ {
if (!m_enabledOptions) if (!m_enabledOptions)
@ -547,7 +670,7 @@ namespace Nz::ShaderAst
std::visit([&](auto&& arg2) std::visit([&](auto&& arg2)
{ {
using T2 = std::decay_t<decltype(arg2)>; using T2 = std::decay_t<decltype(arg2)>;
using PCType = PropagateConstantType<Type, T1, T2>; using PCType = BinaryConstantPropagation<Type, T1, T2>;
if constexpr (is_complete_v<PCType>) if constexpr (is_complete_v<PCType>)
{ {
@ -564,4 +687,27 @@ namespace Nz::ShaderAst
return optimized; return optimized;
} }
template<UnaryType Type>
ExpressionPtr AstOptimizer::PropagateConstant(std::unique_ptr<ConstantExpression>&& operand)
{
std::unique_ptr<ConstantExpression> optimized;
std::visit([&](auto&& arg)
{
using T = std::decay_t<decltype(arg)>;
using PCType = UnaryConstantPropagation<Type, T>;
if constexpr (is_complete_v<PCType>)
{
using Op = typename PCType::Op;
if constexpr (is_complete_v<Op>)
optimized = Op{}(arg);
}
}, operand->value);
if (optimized)
optimized->cachedExpressionType = optimized->GetExpressionType();
return optimized;
}
} }

View File

@ -73,11 +73,16 @@ namespace Nz::ShaderAst
node.expression->Visit(*this); node.expression->Visit(*this);
} }
void AstRecursiveVisitor::Visit(VariableExpression& node) void AstRecursiveVisitor::Visit(VariableExpression& /*node*/)
{ {
/* Nothing to do */ /* Nothing to do */
} }
void AstRecursiveVisitor::Visit(UnaryExpression& node)
{
node.expression->Visit(*this);
}
void AstRecursiveVisitor::Visit(BranchStatement& node) void AstRecursiveVisitor::Visit(BranchStatement& node)
{ {
for (auto& cond : node.condStatements) for (auto& cond : node.condStatements)
@ -95,7 +100,7 @@ namespace Nz::ShaderAst
node.statement->Visit(*this); node.statement->Visit(*this);
} }
void AstRecursiveVisitor::Visit(DeclareExternalStatement& node) void AstRecursiveVisitor::Visit(DeclareExternalStatement& /*node*/)
{ {
/* Nothing to do */ /* Nothing to do */
} }

View File

@ -147,6 +147,12 @@ namespace Nz::ShaderAst
SizeT(node.variableId); SizeT(node.variableId);
} }
void AstSerializerBase::Serialize(UnaryExpression& node)
{
Enum(node.op);
Node(node.expression);
}
void AstSerializerBase::Serialize(BranchStatement& node) void AstSerializerBase::Serialize(BranchStatement& node)
{ {

View File

@ -86,8 +86,13 @@ namespace Nz::ShaderAst
node.expression->Visit(*this); node.expression->Visit(*this);
} }
void ShaderAstValueCategory::Visit(VariableExpression& node) void ShaderAstValueCategory::Visit(VariableExpression& /*node*/)
{ {
m_expressionCategory = ExpressionCategory::LValue; m_expressionCategory = ExpressionCategory::LValue;
} }
void ShaderAstValueCategory::Visit(UnaryExpression& /*node*/)
{
m_expressionCategory = ExpressionCategory::RValue;
}
} }

View File

@ -592,6 +592,41 @@ namespace Nz::ShaderAst
return clone; return clone;
} }
ExpressionPtr SanitizeVisitor::Clone(UnaryExpression& node)
{
auto clone = static_unique_pointer_cast<UnaryExpression>(AstCloner::Clone(node));
const ExpressionType& exprType = GetExpressionType(MandatoryExpr(clone->expression));
if (!IsPrimitiveType(exprType))
throw AstError{ "unary expression operand type does not support unary operation" };
PrimitiveType primitiveType = std::get<PrimitiveType>(exprType);
switch (node.op)
{
case UnaryType::LogicalNot:
{
if (primitiveType != PrimitiveType::Boolean)
throw AstError{ "logical not is only supported on booleans" };
break;
}
case UnaryType::Minus:
case UnaryType::Plus:
{
if (primitiveType != PrimitiveType::Float32 && primitiveType != PrimitiveType::Int32 && primitiveType != PrimitiveType::UInt32)
throw AstError{ "plus and minus unary expressions are only supported on floating points and integers types" };
break;
}
}
clone->cachedExpressionType = primitiveType;
return clone;
}
StatementPtr SanitizeVisitor::Clone(BranchStatement& node) StatementPtr SanitizeVisitor::Clone(BranchStatement& node)
{ {
auto clone = std::make_unique<BranchStatement>(); auto clone = std::make_unique<BranchStatement>();

View File

@ -922,6 +922,26 @@ namespace Nz
Append(varName); Append(varName);
} }
void GlslWriter::Visit(ShaderAst::UnaryExpression& node)
{
switch (node.op)
{
case ShaderAst::UnaryType::LogicalNot:
Append("!");
break;
case ShaderAst::UnaryType::Minus:
Append("-");
break;
case ShaderAst::UnaryType::Plus:
Append("+");
break;
}
Visit(node.expression);
}
bool GlslWriter::HasExplicitBinding(ShaderAst::StatementPtr& shader) bool GlslWriter::HasExplicitBinding(ShaderAst::StatementPtr& shader)
{ {
/*for (const auto& uniform : shader.GetUniforms()) /*for (const auto& uniform : shader.GetUniforms())

View File

@ -107,7 +107,7 @@ namespace Nz
}, type); }, type);
} }
void LangWriter::Append(const ShaderAst::IdentifierType& identifierType) void LangWriter::Append(const ShaderAst::IdentifierType& /*identifierType*/)
{ {
throw std::runtime_error("unexpected identifier type"); throw std::runtime_error("unexpected identifier type");
} }
@ -730,6 +730,26 @@ namespace Nz
Append(varName); Append(varName);
} }
void LangWriter::Visit(ShaderAst::UnaryExpression& node)
{
switch (node.op)
{
case ShaderAst::UnaryType::LogicalNot:
Append("!");
break;
case ShaderAst::UnaryType::Minus:
Append("-");
break;
case ShaderAst::UnaryType::Plus:
Append("+");
break;
}
Append(node.expression);
}
void LangWriter::AppendHeader() void LangWriter::AppendHeader()
{ {
// Nothing yet // Nothing yet

View File

@ -802,10 +802,10 @@ namespace Nz::ShaderLang
return ParseBinOpRhs(0, ParsePrimaryExpression()); return ParseBinOpRhs(0, ParsePrimaryExpression());
} }
ShaderAst::ExpressionPtr Parser::ParseFloatingPointExpression(bool minus) ShaderAst::ExpressionPtr Parser::ParseFloatingPointExpression()
{ {
const Token& floatingPointToken = Expect(Advance(), TokenType::FloatingPointValue); const Token& floatingPointToken = Expect(Advance(), TokenType::FloatingPointValue);
return ShaderBuilder::Constant(((minus) ? -1.f : 1.f) * float(std::get<double>(floatingPointToken.data))); //< FIXME return ShaderBuilder::Constant(float(std::get<double>(floatingPointToken.data))); //< FIXME
} }
ShaderAst::ExpressionPtr Parser::ParseIdentifier() ShaderAst::ExpressionPtr Parser::ParseIdentifier()
@ -816,10 +816,10 @@ namespace Nz::ShaderLang
return ShaderBuilder::Identifier(identifier); return ShaderBuilder::Identifier(identifier);
} }
ShaderAst::ExpressionPtr Parser::ParseIntegerExpression(bool minus) ShaderAst::ExpressionPtr Parser::ParseIntegerExpression()
{ {
const Token& integerToken = Expect(Advance(), TokenType::IntegerValue); const Token& integerToken = Expect(Advance(), TokenType::IntegerValue);
return ShaderBuilder::Constant(((minus) ? -1 : 1) * static_cast<Nz::Int32>(std::get<long long>(integerToken.data))); return ShaderBuilder::Constant(static_cast<Nz::Int32>(std::get<long long>(integerToken.data))); //< FIXME
} }
std::vector<ShaderAst::ExpressionPtr> Parser::ParseParameters() std::vector<ShaderAst::ExpressionPtr> Parser::ParseParameters()
@ -894,21 +894,20 @@ namespace Nz::ShaderLang
return ParseIntegerExpression(); return ParseIntegerExpression();
case TokenType::Minus: case TokenType::Minus:
//< FIXME: Handle this with an unary node {
if (Peek(1).type == TokenType::FloatingPointValue) Consume();
{ ShaderAst::ExpressionPtr expr = ParsePrimaryExpression();
Consume();
return ParseFloatingPointExpression(true);
}
else if (Peek(1).type == TokenType::IntegerValue)
{
Consume();
return ParseIntegerExpression(true);
}
else
throw UnexpectedToken{};
break; return ShaderBuilder::Unary(ShaderAst::UnaryType::Minus, std::move(expr));
}
case TokenType::Plus:
{
Consume();
ShaderAst::ExpressionPtr expr = ParsePrimaryExpression();
return ShaderBuilder::Unary(ShaderAst::UnaryType::Plus, std::move(expr));
}
case TokenType::OpenParenthesis: case TokenType::OpenParenthesis:
return ParseParenthesisExpression(); return ParseParenthesisExpression();

View File

@ -815,6 +815,61 @@ namespace Nz
PushResultId(loadVisitor.Evaluate(node)); PushResultId(loadVisitor.Evaluate(node));
} }
void SpirvAstVisitor::Visit(ShaderAst::UnaryExpression& node)
{
const ShaderAst::ExpressionType& resultType = GetExpressionType(node);
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expression);
UInt32 operand = EvaluateExpression(node.expression);
UInt32 resultId = [&]
{
switch (node.op)
{
case ShaderAst::UnaryType::LogicalNot:
{
assert(IsPrimitiveType(exprType));
assert(std::get<ShaderAst::PrimitiveType>(resultType) == ShaderAst::PrimitiveType::Boolean);
UInt32 resultId = m_writer.AllocateResultId();
m_currentBlock->Append(SpirvOp::OpLogicalNot, m_writer.GetTypeId(resultType), resultId, operand);
return resultId;
}
case ShaderAst::UnaryType::Minus:
{
assert(IsPrimitiveType(exprType));
UInt32 resultId = m_writer.AllocateResultId();
switch (std::get<ShaderAst::PrimitiveType>(resultType))
{
case ShaderAst::PrimitiveType::Float32:
m_currentBlock->Append(SpirvOp::OpFNegate, m_writer.GetTypeId(resultType), resultId, operand);
return resultId;
case ShaderAst::PrimitiveType::Int32:
case ShaderAst::PrimitiveType::UInt32:
m_currentBlock->Append(SpirvOp::OpSNegate, m_writer.GetTypeId(resultType), resultId, operand);
return resultId;
default:
break;
}
}
case ShaderAst::UnaryType::Plus:
PushResultId(operand); //< No-op
break;
}
throw std::runtime_error("unexpected unary operation");
}();
PushResultId(resultId);
}
void SpirvAstVisitor::PushResultId(UInt32 value) void SpirvAstVisitor::PushResultId(UInt32 value)
{ {
m_resultIds.push_back(value); m_resultIds.push_back(value);

View File

@ -78,6 +78,13 @@ namespace Nz
m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value())); m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value()));
} }
void Visit(ShaderAst::BinaryExpression& node) override
{
AstRecursiveVisitor::Visit(node);
m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value()));
}
void Visit(ShaderAst::ConditionalExpression& node) override void Visit(ShaderAst::ConditionalExpression& node) override
{ {
if (TestBit<Nz::UInt64>(m_states.enabledOptions, node.optionIndex)) if (TestBit<Nz::UInt64>(m_states.enabledOptions, node.optionIndex))
@ -294,6 +301,13 @@ namespace Nz
m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value())); m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value()));
} }
void Visit(ShaderAst::UnaryExpression& node) override
{
AstRecursiveVisitor::Visit(node);
m_constantCache.Register(*m_constantCache.BuildType(node.cachedExpressionType.value()));
}
UInt32 HandleEntryInOutType(ShaderStageType entryPointType, std::size_t funcIndex, const ShaderAst::StructDescription::StructMember& member, SpirvStorageClass storageClass) UInt32 HandleEntryInOutType(ShaderStageType entryPointType, std::size_t funcIndex, const ShaderAst::StructDescription::StructMember& member, SpirvStorageClass storageClass)
{ {
if (member.builtin) if (member.builtin)