diff --git a/include/Nazara/Shader/Ast/AstCloner.hpp b/include/Nazara/Shader/Ast/AstCloner.hpp index 8c77768f6..d4f5fc2c9 100644 --- a/include/Nazara/Shader/Ast/AstCloner.hpp +++ b/include/Nazara/Shader/Ast/AstCloner.hpp @@ -48,6 +48,7 @@ namespace Nz::ShaderAst virtual ExpressionPtr Clone(SelectOptionExpression& node); virtual ExpressionPtr Clone(SwizzleExpression& node); virtual ExpressionPtr Clone(VariableExpression& node); + virtual ExpressionPtr Clone(UnaryExpression& node); virtual StatementPtr Clone(BranchStatement& node); virtual StatementPtr Clone(ConditionalStatement& node); diff --git a/include/Nazara/Shader/Ast/AstNodeList.hpp b/include/Nazara/Shader/Ast/AstNodeList.hpp index f16cdd9b5..47aa20c68 100644 --- a/include/Nazara/Shader/Ast/AstNodeList.hpp +++ b/include/Nazara/Shader/Ast/AstNodeList.hpp @@ -38,6 +38,7 @@ NAZARA_SHADERAST_EXPRESSION(IntrinsicExpression) NAZARA_SHADERAST_EXPRESSION(SelectOptionExpression) NAZARA_SHADERAST_EXPRESSION(SwizzleExpression) NAZARA_SHADERAST_EXPRESSION(VariableExpression) +NAZARA_SHADERAST_EXPRESSION(UnaryExpression) NAZARA_SHADERAST_STATEMENT(BranchStatement) NAZARA_SHADERAST_STATEMENT(ConditionalStatement) NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement) diff --git a/include/Nazara/Shader/Ast/AstOptimizer.hpp b/include/Nazara/Shader/Ast/AstOptimizer.hpp index 69092dcbf..8a60bce05 100644 --- a/include/Nazara/Shader/Ast/AstOptimizer.hpp +++ b/include/Nazara/Shader/Ast/AstOptimizer.hpp @@ -32,10 +32,12 @@ namespace Nz::ShaderAst protected: ExpressionPtr Clone(BinaryExpression& node) override; ExpressionPtr Clone(ConditionalExpression& node) override; + ExpressionPtr Clone(UnaryExpression& node) override; StatementPtr Clone(BranchStatement& node) override; StatementPtr Clone(ConditionalStatement& node) override; template ExpressionPtr PropagateConstant(std::unique_ptr&& lhs, std::unique_ptr&& rhs); + template ExpressionPtr PropagateConstant(std::unique_ptr&& operand); private: std::optional m_enabledOptions; diff --git a/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp b/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp index ea605ff6a..c82c98ab6 100644 --- a/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp +++ b/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp @@ -32,6 +32,7 @@ namespace Nz::ShaderAst void Visit(SelectOptionExpression& node) override; void Visit(SwizzleExpression& node) override; void Visit(VariableExpression& node) override; + void Visit(UnaryExpression& node) override; void Visit(BranchStatement& node) override; void Visit(ConditionalStatement& node) override; diff --git a/include/Nazara/Shader/Ast/AstSerializer.hpp b/include/Nazara/Shader/Ast/AstSerializer.hpp index 193f2f971..cbae0043f 100644 --- a/include/Nazara/Shader/Ast/AstSerializer.hpp +++ b/include/Nazara/Shader/Ast/AstSerializer.hpp @@ -35,6 +35,7 @@ namespace Nz::ShaderAst void Serialize(SelectOptionExpression& node); void Serialize(SwizzleExpression& node); void Serialize(VariableExpression& node); + void Serialize(UnaryExpression& node); void Serialize(BranchStatement& node); void Serialize(ConditionalStatement& node); diff --git a/include/Nazara/Shader/Ast/AstUtils.hpp b/include/Nazara/Shader/Ast/AstUtils.hpp index 42d7254ab..501f6fb45 100644 --- a/include/Nazara/Shader/Ast/AstUtils.hpp +++ b/include/Nazara/Shader/Ast/AstUtils.hpp @@ -43,6 +43,7 @@ namespace Nz::ShaderAst void Visit(SelectOptionExpression& node) override; void Visit(SwizzleExpression& node) override; void Visit(VariableExpression& node) override; + void Visit(UnaryExpression& node) override; ExpressionCategory m_expressionCategory; }; diff --git a/include/Nazara/Shader/Ast/Enums.hpp b/include/Nazara/Shader/Ast/Enums.hpp index b4a7597a6..7e64589a8 100644 --- a/include/Nazara/Shader/Ast/Enums.hpp +++ b/include/Nazara/Shader/Ast/Enums.hpp @@ -29,7 +29,7 @@ namespace Nz::ShaderAst enum class BinaryType { Add, //< + - Subtract, //< - + Subtract, //< - Multiply, //< * Divide, //< / @@ -89,6 +89,13 @@ namespace Nz::ShaderAst Third, Fourth }; + + enum class UnaryType + { + LogicalNot, //< !v + Minus, //< -v + Plus, //< +v + }; } #endif // NAZARA_SHADER_ENUMS_HPP diff --git a/include/Nazara/Shader/Ast/Nodes.hpp b/include/Nazara/Shader/Ast/Nodes.hpp index 5fc836bef..37d80a9bd 100644 --- a/include/Nazara/Shader/Ast/Nodes.hpp +++ b/include/Nazara/Shader/Ast/Nodes.hpp @@ -176,6 +176,15 @@ namespace Nz::ShaderAst 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 struct Statement; diff --git a/include/Nazara/Shader/Ast/SanitizeVisitor.hpp b/include/Nazara/Shader/Ast/SanitizeVisitor.hpp index da66ff277..9c9f79202 100644 --- a/include/Nazara/Shader/Ast/SanitizeVisitor.hpp +++ b/include/Nazara/Shader/Ast/SanitizeVisitor.hpp @@ -54,6 +54,7 @@ namespace Nz::ShaderAst ExpressionPtr Clone(IntrinsicExpression& node) override; ExpressionPtr Clone(SelectOptionExpression& node) override; ExpressionPtr Clone(SwizzleExpression& node) override; + ExpressionPtr Clone(UnaryExpression& node) override; StatementPtr Clone(BranchStatement& node) override; StatementPtr Clone(ConditionalStatement& node) override; diff --git a/include/Nazara/Shader/GlslWriter.hpp b/include/Nazara/Shader/GlslWriter.hpp index ca6ca8112..6aa4c5117 100644 --- a/include/Nazara/Shader/GlslWriter.hpp +++ b/include/Nazara/Shader/GlslWriter.hpp @@ -86,6 +86,7 @@ namespace Nz void Visit(ShaderAst::IntrinsicExpression& node) override; void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override; + void Visit(ShaderAst::UnaryExpression& node) override; void Visit(ShaderAst::BranchStatement& node) override; void Visit(ShaderAst::ConditionalStatement& node) override; diff --git a/include/Nazara/Shader/LangWriter.hpp b/include/Nazara/Shader/LangWriter.hpp index 2a111f47b..dbc0644de 100644 --- a/include/Nazara/Shader/LangWriter.hpp +++ b/include/Nazara/Shader/LangWriter.hpp @@ -85,6 +85,7 @@ namespace Nz void Visit(ShaderAst::IntrinsicExpression& node) override; void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override; + void Visit(ShaderAst::UnaryExpression& node) override; void Visit(ShaderAst::BranchStatement& node) override; void Visit(ShaderAst::ConditionalStatement& node) override; diff --git a/include/Nazara/Shader/ShaderBuilder.hpp b/include/Nazara/Shader/ShaderBuilder.hpp index 434e05cfe..498dfa861 100644 --- a/include/Nazara/Shader/ShaderBuilder.hpp +++ b/include/Nazara/Shader/ShaderBuilder.hpp @@ -120,6 +120,11 @@ namespace Nz::ShaderBuilder { inline std::unique_ptr operator()(ShaderAst::ExpressionPtr expression, std::vector swizzleComponents) const; }; + + struct Unary + { + inline std::unique_ptr operator()(ShaderAst::UnaryType op, ShaderAst::ExpressionPtr expression) const; + }; } constexpr Impl::AccessMember AccessMember; @@ -143,6 +148,7 @@ namespace Nz::ShaderBuilder constexpr Impl::Return Return; constexpr Impl::SelectOption SelectOption; constexpr Impl::Swizzle Swizzle; + constexpr Impl::Unary Unary; } #include diff --git a/include/Nazara/Shader/ShaderBuilder.inl b/include/Nazara/Shader/ShaderBuilder.inl index 93287e2b6..194040b0e 100644 --- a/include/Nazara/Shader/ShaderBuilder.inl +++ b/include/Nazara/Shader/ShaderBuilder.inl @@ -235,6 +235,15 @@ namespace Nz::ShaderBuilder return swizzleNode; } + + inline std::unique_ptr Impl::Unary::operator()(ShaderAst::UnaryType op, ShaderAst::ExpressionPtr expression) const + { + auto unaryNode = std::make_unique(); + unaryNode->expression = std::move(expression); + unaryNode->op = op; + + return unaryNode; + } } #include diff --git a/include/Nazara/Shader/ShaderLangParser.hpp b/include/Nazara/Shader/ShaderLangParser.hpp index 5baaf813a..4733b9eb0 100644 --- a/include/Nazara/Shader/ShaderLangParser.hpp +++ b/include/Nazara/Shader/ShaderLangParser.hpp @@ -96,9 +96,9 @@ namespace Nz::ShaderLang // Expressions ShaderAst::ExpressionPtr ParseBinOpRhs(int exprPrecedence, ShaderAst::ExpressionPtr lhs); ShaderAst::ExpressionPtr ParseExpression(); - ShaderAst::ExpressionPtr ParseFloatingPointExpression(bool minus = false); + ShaderAst::ExpressionPtr ParseFloatingPointExpression(); ShaderAst::ExpressionPtr ParseIdentifier(); - ShaderAst::ExpressionPtr ParseIntegerExpression(bool minus = false); + ShaderAst::ExpressionPtr ParseIntegerExpression(); std::vector ParseParameters(); ShaderAst::ExpressionPtr ParseParenthesisExpression(); ShaderAst::ExpressionPtr ParsePrimaryExpression(); diff --git a/include/Nazara/Shader/SpirvAstVisitor.hpp b/include/Nazara/Shader/SpirvAstVisitor.hpp index b4dd9b8a7..5f52ac640 100644 --- a/include/Nazara/Shader/SpirvAstVisitor.hpp +++ b/include/Nazara/Shader/SpirvAstVisitor.hpp @@ -62,6 +62,7 @@ namespace Nz void Visit(ShaderAst::ReturnStatement& node) override; void Visit(ShaderAst::SwizzleExpression& node) override; void Visit(ShaderAst::VariableExpression& node) override; + void Visit(ShaderAst::UnaryExpression& node) override; SpirvAstVisitor& operator=(const SpirvAstVisitor&) = delete; SpirvAstVisitor& operator=(SpirvAstVisitor&&) = delete; diff --git a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader index dd4e62b97..90f866d0b 100644 Binary files a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader and b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader differ diff --git a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader.h b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader.h index edc400886..d0446fabc 100644 --- a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader.h +++ b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.frag.shader.h @@ -1 +1 @@ -78,83,72,82,0,0,0,1,0,0,0,21,0,0,0,10,0,0,0,16,0,0,0,0,19,72,65,83,95,68,73,70,70,85,83,69,95,84,69,88,84,85,82,69,1,0,0,0,0,255,255,255,255,0,0,0,16,0,0,0,0,17,72,65,83,95,65,76,80,72,65,95,84,69,88,84,85,82,69,1,0,0,0,0,255,255,255,255,0,0,0,16,0,0,0,0,10,65,76,80,72,65,95,84,69,83,84,1,0,0,0,0,255,255,255,255,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,6,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,16,77,97,116,101,114,105,97,108,65,108,112,104,97,77,97,112,4,0,0,0,2,0,0,0,1,1,0,0,0,0,0,0,0,18,77,97,116,101,114,105,97,108,68,105,102,102,117,115,101,77,97,112,4,0,0,0,2,0,0,0,1,1,0,0,0,1,0,0,0,14,84,101,120,116,117,114,101,79,118,101,114,108,97,121,4,0,0,0,2,0,0,0,1,1,0,0,0,2,0,0,0,17,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,2,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,17,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,1,0,0,0,13,82,101,110,100,101,114,84,97,114,103,101,116,48,7,0,0,0,4,0,0,0,1,2,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,0,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,8,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,18,0,0,0,0,8,108,105,103,104,116,68,105,114,0,0,0,0,6,0,0,0,5,0,0,0,0,191,52,253,244,63,52,253,244,0,0,0,18,0,0,0,0,11,108,105,103,104,116,70,97,99,116,111,114,0,0,0,0,8,0,0,0,1,0,0,0,2,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,118,101,114,116,78,111,114,109,97,108,0,0,0,7,0,0,0,8,108,105,103,104,116,68,105,114,0,0,0,18,0,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,0,3,0,0,0,2,0,0,0,7,0,0,0,11,108,105,103,104,116,70,97,99,116,111,114,0,0,0,9,0,0,0,19,72,65,83,95,68,73,70,70,85,83,69,95,84,69,88,84,85,82,69,0,0,0,3,0,0,0,2,0,0,0,8,0,0,0,2,0,0,0,2,0,0,0,7,0,0,0,18,77,97,116,101,114,105,97,108,68,105,102,102,117,115,101,77,97,112,0,0,0,0,0,0,0,7,0,0,0,5,105,110,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,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,0,0,0,0,0,0,0,7,0,0,0,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,0,0,0,18,0,0,0,0,4,118,97,114,48,0,0,0,0,9,0,0,0,17,72,65,83,95,65,76,80,72,65,95,84,69,88,84,85,82,69,0,0,0,4,7,0,0,0,4,0,0,0,1,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,0,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,1,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,10,0,0,0,1,0,0,0,8,0,0,0,2,0,0,0,2,0,0,0,7,0,0,0,16,77,97,116,101,114,105,97,108,65,108,112,104,97,77,97,112,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,3,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,12,0,0,0,1,0,0,0,3,0,0,0,4,0,0,0,9,0,0,0,10,65,76,80,72,65,95,84,69,83,84,0,0,0,3,0,0,0,8,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,4,118,97,114,48,0,0,0,3,0,0,0,0,0,0,0,7,0,0,0,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,0,0,0,6,0,0,0,0,0,0,0,0,6,0,0,0,0,1,0,0,0,19,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,13,82,101,110,100,101,114,84,97,114,103,101,116,48,0,0,0,7,0,0,0,4,118,97,114,48,0,0,0,23,0,0,0,7,0,0,0,6,111,117,116,112,117,116, \ No newline at end of file +78,83,72,82,0,0,0,1,0,0,0,22,0,0,0,10,0,0,0,17,0,0,0,0,19,72,65,83,95,68,73,70,70,85,83,69,95,84,69,88,84,85,82,69,1,0,0,0,0,255,255,255,255,0,0,0,17,0,0,0,0,17,72,65,83,95,65,76,80,72,65,95,84,69,88,84,85,82,69,1,0,0,0,0,255,255,255,255,0,0,0,17,0,0,0,0,10,65,76,80,72,65,95,84,69,83,84,1,0,0,0,0,255,255,255,255,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,6,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,16,77,97,116,101,114,105,97,108,65,108,112,104,97,77,97,112,4,0,0,0,2,0,0,0,1,1,0,0,0,0,0,0,0,18,77,97,116,101,114,105,97,108,68,105,102,102,117,115,101,77,97,112,4,0,0,0,2,0,0,0,1,1,0,0,0,1,0,0,0,14,84,101,120,116,117,114,101,79,118,101,114,108,97,121,4,0,0,0,2,0,0,0,1,1,0,0,0,2,0,0,0,18,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,2,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,18,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,1,0,0,0,13,82,101,110,100,101,114,84,97,114,103,101,116,48,7,0,0,0,4,0,0,0,1,2,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,0,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,8,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,19,0,0,0,0,8,108,105,103,104,116,68,105,114,0,0,0,0,6,0,0,0,5,0,0,0,0,191,52,253,244,63,52,253,244,0,0,0,19,0,0,0,0,11,108,105,103,104,116,70,97,99,116,111,114,0,0,0,0,8,0,0,0,1,0,0,0,2,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,118,101,114,116,78,111,114,109,97,108,0,0,0,7,0,0,0,8,108,105,103,104,116,68,105,114,0,0,0,19,0,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,0,3,0,0,0,2,0,0,0,7,0,0,0,11,108,105,103,104,116,70,97,99,116,111,114,0,0,0,9,0,0,0,19,72,65,83,95,68,73,70,70,85,83,69,95,84,69,88,84,85,82,69,0,0,0,3,0,0,0,2,0,0,0,8,0,0,0,2,0,0,0,2,0,0,0,7,0,0,0,18,77,97,116,101,114,105,97,108,68,105,102,102,117,115,101,77,97,112,0,0,0,0,0,0,0,7,0,0,0,5,105,110,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,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,0,0,0,0,0,0,0,7,0,0,0,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,0,0,0,19,0,0,0,0,4,118,97,114,48,0,0,0,0,9,0,0,0,17,72,65,83,95,65,76,80,72,65,95,84,69,88,84,85,82,69,0,0,0,4,7,0,0,0,4,0,0,0,1,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,0,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,1,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,10,0,0,0,1,0,0,0,8,0,0,0,2,0,0,0,2,0,0,0,7,0,0,0,16,77,97,116,101,114,105,97,108,65,108,112,104,97,77,97,112,0,0,0,0,0,0,0,7,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,3,0,0,0,7,0,0,0,12,116,101,120,116,117,114,101,67,111,108,111,114,0,0,0,13,0,0,0,1,0,0,0,3,0,0,0,4,0,0,0,9,0,0,0,10,65,76,80,72,65,95,84,69,83,84,0,0,0,3,0,0,0,8,0,0,0,10,0,0,0,1,0,0,0,7,0,0,0,4,118,97,114,48,0,0,0,3,0,0,0,0,0,0,0,7,0,0,0,8,115,101,116,116,105,110,103,115,0,0,0,1,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,0,0,0,6,0,0,0,0,0,0,0,0,6,0,0,0,0,1,0,0,0,20,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,13,82,101,110,100,101,114,84,97,114,103,101,116,48,0,0,0,7,0,0,0,4,118,97,114,48,0,0,0,24,0,0,0,7,0,0,0,6,111,117,116,112,117,116, \ No newline at end of file diff --git a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader index 7d3e4d2b8..d5e37d6b6 100644 Binary files a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader and b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader differ diff --git a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader.h b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader.h index 07d92a859..49b59e099 100644 --- a/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader.h +++ b/src/Nazara/Graphics/Resources/Shaders/basicmaterial.vert.shader.h @@ -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, \ No newline at end of file +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, \ No newline at end of file diff --git a/src/Nazara/Shader/Ast/AstCloner.cpp b/src/Nazara/Shader/Ast/AstCloner.cpp index 496b9d14e..999d445e0 100644 --- a/src/Nazara/Shader/Ast/AstCloner.cpp +++ b/src/Nazara/Shader/Ast/AstCloner.cpp @@ -301,6 +301,17 @@ namespace Nz::ShaderAst return clone; } + ExpressionPtr AstCloner::Clone(UnaryExpression& node) + { + auto clone = std::make_unique(); + 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) \ { \ PushExpression(Clone(node)); \ diff --git a/src/Nazara/Shader/Ast/AstOptimizer.cpp b/src/Nazara/Shader/Ast/AstOptimizer.cpp index 8ec3fe3ab..e5d869a21 100644 --- a/src/Nazara/Shader/Ast/AstOptimizer.cpp +++ b/src/Nazara/Shader/Ast/AstOptimizer.cpp @@ -33,13 +33,14 @@ namespace Nz::ShaderAst template inline constexpr bool is_complete_v = is_complete::value; + /*************************************************************************************************/ template - struct PropagateConstantType; + struct BinaryConstantPropagation; // CompEq template - struct CompEqBase + struct BinaryCompEqBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -48,17 +49,17 @@ namespace Nz::ShaderAst }; template - struct CompEq; + struct BinaryCompEq; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompEq; + using Op = BinaryCompEq; }; // CompGe template - struct CompGeBase + struct BinaryCompGeBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -67,17 +68,17 @@ namespace Nz::ShaderAst }; template - struct CompGe; + struct BinaryCompGe; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompGe; + using Op = BinaryCompGe; }; // CompGt template - struct CompGtBase + struct BinaryCompGtBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -86,17 +87,17 @@ namespace Nz::ShaderAst }; template - struct CompGt; + struct BinaryCompGt; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompGt; + using Op = BinaryCompGt; }; // CompLe template - struct CompLeBase + struct BinaryCompLeBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -105,17 +106,17 @@ namespace Nz::ShaderAst }; template - struct CompLe; + struct BinaryCompLe; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompLe; + using Op = BinaryCompLe; }; // CompLt template - struct CompLtBase + struct BinaryCompLtBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -124,17 +125,17 @@ namespace Nz::ShaderAst }; template - struct CompLt; + struct BinaryCompLt; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompLe; + using Op = BinaryCompLe; }; // CompNe template - struct CompNeBase + struct BinaryCompNeBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -143,17 +144,17 @@ namespace Nz::ShaderAst }; template - struct CompNe; + struct BinaryCompNe; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = CompNe; + using Op = BinaryCompNe; }; // Addition template - struct AdditionBase + struct BinaryAdditionBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -162,17 +163,17 @@ namespace Nz::ShaderAst }; template - struct Addition; + struct BinaryAddition; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = Addition; + using Op = BinaryAddition; }; // Division template - struct DivisionBase + struct BinaryDivisionBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -181,17 +182,17 @@ namespace Nz::ShaderAst }; template - struct Division; + struct BinaryDivision; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = Division; + using Op = BinaryDivision; }; // Multiplication template - struct MultiplicationBase + struct BinaryMultiplicationBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -200,17 +201,17 @@ namespace Nz::ShaderAst }; template - struct Multiplication; + struct BinaryMultiplication; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = Multiplication; + using Op = BinaryMultiplication; }; // Subtraction template - struct SubtractionBase + struct BinarySubtractionBase { std::unique_ptr operator()(const T1& lhs, const T2& rhs) { @@ -219,163 +220,251 @@ namespace Nz::ShaderAst }; template - struct Subtraction; + struct BinarySubtraction; template - struct PropagateConstantType + struct BinaryConstantPropagation { - using Op = Subtraction; + using Op = BinarySubtraction; + }; + + /*************************************************************************************************/ + + template + struct UnaryConstantPropagation; + + // LogicalNot + template + struct UnaryLogicalNotBase + { + std::unique_ptr operator()(const T& arg) + { + return ShaderBuilder::Constant(!arg); + } }; -#define EnableOptimisation(Op, T1, T2) template<> struct Op : Op##Base {} + template + struct UnaryLogicalNot; - EnableOptimisation(CompEq, bool, bool); - EnableOptimisation(CompEq, double, double); - EnableOptimisation(CompEq, float, float); - EnableOptimisation(CompEq, Nz::Int32, Nz::Int32); - 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); + template + struct UnaryConstantPropagation + { + using Op = UnaryLogicalNot; + }; - EnableOptimisation(CompGe, bool, bool); - EnableOptimisation(CompGe, double, double); - EnableOptimisation(CompGe, float, float); - EnableOptimisation(CompGe, Nz::Int32, Nz::Int32); - EnableOptimisation(CompGe, Nz::Vector2f, Nz::Vector2f); - EnableOptimisation(CompGe, Nz::Vector3f, Nz::Vector3f); - EnableOptimisation(CompGe, Nz::Vector4f, Nz::Vector4f); - EnableOptimisation(CompGe, Nz::Vector2i32, Nz::Vector2i32); - EnableOptimisation(CompGe, Nz::Vector3i32, Nz::Vector3i32); - EnableOptimisation(CompGe, Nz::Vector4i32, Nz::Vector4i32); + // Minus + template + struct UnaryMinusBase + { + std::unique_ptr operator()(const T& arg) + { + return ShaderBuilder::Constant(-arg); + } + }; - EnableOptimisation(CompGt, bool, bool); - EnableOptimisation(CompGt, double, double); - 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); + template + struct UnaryMinus; - EnableOptimisation(CompLe, bool, bool); - EnableOptimisation(CompLe, double, double); - EnableOptimisation(CompLe, float, float); - EnableOptimisation(CompLe, Nz::Int32, Nz::Int32); - 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); + template + struct UnaryConstantPropagation + { + using Op = UnaryMinus; + }; - EnableOptimisation(CompLt, bool, bool); - EnableOptimisation(CompLt, double, double); - EnableOptimisation(CompLt, float, float); - EnableOptimisation(CompLt, Nz::Int32, Nz::Int32); - EnableOptimisation(CompLt, Nz::Vector2f, Nz::Vector2f); - EnableOptimisation(CompLt, Nz::Vector3f, Nz::Vector3f); - EnableOptimisation(CompLt, Nz::Vector4f, Nz::Vector4f); - EnableOptimisation(CompLt, Nz::Vector2i32, Nz::Vector2i32); - EnableOptimisation(CompLt, Nz::Vector3i32, Nz::Vector3i32); - EnableOptimisation(CompLt, Nz::Vector4i32, Nz::Vector4i32); + // Plus + template + struct UnaryPlusBase + { + std::unique_ptr operator()(const T& arg) + { + return ShaderBuilder::Constant(arg); + } + }; - EnableOptimisation(CompNe, bool, bool); - EnableOptimisation(CompNe, double, double); - 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); + template + struct UnaryPlus; - EnableOptimisation(Addition, double, double); - EnableOptimisation(Addition, float, float); - EnableOptimisation(Addition, Nz::Int32, Nz::Int32); - EnableOptimisation(Addition, Nz::Vector2f, Nz::Vector2f); - 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); + template + struct UnaryConstantPropagation + { + using Op = UnaryPlus; + }; - EnableOptimisation(Division, double, double); - 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); +#define EnableOptimisation(Op, ...) template<> struct Op<__VA_ARGS__> : Op##Base<__VA_ARGS__> {} - EnableOptimisation(Multiplication, double, double); - 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); + // Binary - EnableOptimisation(Subtraction, double, double); - EnableOptimisation(Subtraction, float, float); - EnableOptimisation(Subtraction, Nz::Int32, Nz::Int32); - EnableOptimisation(Subtraction, Nz::Vector2f, Nz::Vector2f); - EnableOptimisation(Subtraction, Nz::Vector3f, Nz::Vector3f); - EnableOptimisation(Subtraction, Nz::Vector4f, Nz::Vector4f); - EnableOptimisation(Subtraction, Nz::Vector2i32, Nz::Vector2i32); - EnableOptimisation(Subtraction, Nz::Vector3i32, Nz::Vector3i32); - EnableOptimisation(Subtraction, Nz::Vector4i32, Nz::Vector4i32); + EnableOptimisation(BinaryCompEq, bool, bool); + EnableOptimisation(BinaryCompEq, double, double); + EnableOptimisation(BinaryCompEq, float, float); + EnableOptimisation(BinaryCompEq, Nz::Int32, Nz::Int32); + EnableOptimisation(BinaryCompEq, Nz::Vector2f, Nz::Vector2f); + EnableOptimisation(BinaryCompEq, Nz::Vector3f, Nz::Vector3f); + EnableOptimisation(BinaryCompEq, Nz::Vector4f, Nz::Vector4f); + EnableOptimisation(BinaryCompEq, Nz::Vector2i32, Nz::Vector2i32); + 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 } @@ -525,6 +614,40 @@ namespace Nz::ShaderAst 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(std::move(expr)); + + ExpressionPtr optimized; + switch (node.op) + { + case UnaryType::LogicalNot: + optimized = PropagateConstant(std::move(constantExpr)); + break; + + case UnaryType::Minus: + optimized = PropagateConstant(std::move(constantExpr)); + break; + + case UnaryType::Plus: + optimized = PropagateConstant(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) { if (!m_enabledOptions) @@ -547,7 +670,7 @@ namespace Nz::ShaderAst std::visit([&](auto&& arg2) { using T2 = std::decay_t; - using PCType = PropagateConstantType; + using PCType = BinaryConstantPropagation; if constexpr (is_complete_v) { @@ -564,4 +687,27 @@ namespace Nz::ShaderAst return optimized; } + + template + ExpressionPtr AstOptimizer::PropagateConstant(std::unique_ptr&& operand) + { + std::unique_ptr optimized; + std::visit([&](auto&& arg) + { + using T = std::decay_t; + using PCType = UnaryConstantPropagation; + + if constexpr (is_complete_v) + { + using Op = typename PCType::Op; + if constexpr (is_complete_v) + optimized = Op{}(arg); + } + }, operand->value); + + if (optimized) + optimized->cachedExpressionType = optimized->GetExpressionType(); + + return optimized; + } } diff --git a/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp b/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp index 85e8b2507..0e646be65 100644 --- a/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp +++ b/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp @@ -73,11 +73,16 @@ namespace Nz::ShaderAst node.expression->Visit(*this); } - void AstRecursiveVisitor::Visit(VariableExpression& node) + void AstRecursiveVisitor::Visit(VariableExpression& /*node*/) { /* Nothing to do */ } + void AstRecursiveVisitor::Visit(UnaryExpression& node) + { + node.expression->Visit(*this); + } + void AstRecursiveVisitor::Visit(BranchStatement& node) { for (auto& cond : node.condStatements) @@ -95,7 +100,7 @@ namespace Nz::ShaderAst node.statement->Visit(*this); } - void AstRecursiveVisitor::Visit(DeclareExternalStatement& node) + void AstRecursiveVisitor::Visit(DeclareExternalStatement& /*node*/) { /* Nothing to do */ } diff --git a/src/Nazara/Shader/Ast/AstSerializer.cpp b/src/Nazara/Shader/Ast/AstSerializer.cpp index fee7d53fd..78b1647ea 100644 --- a/src/Nazara/Shader/Ast/AstSerializer.cpp +++ b/src/Nazara/Shader/Ast/AstSerializer.cpp @@ -147,6 +147,12 @@ namespace Nz::ShaderAst SizeT(node.variableId); } + void AstSerializerBase::Serialize(UnaryExpression& node) + { + Enum(node.op); + Node(node.expression); + } + void AstSerializerBase::Serialize(BranchStatement& node) { diff --git a/src/Nazara/Shader/Ast/AstUtils.cpp b/src/Nazara/Shader/Ast/AstUtils.cpp index 76496ddf0..ae21847dd 100644 --- a/src/Nazara/Shader/Ast/AstUtils.cpp +++ b/src/Nazara/Shader/Ast/AstUtils.cpp @@ -86,8 +86,13 @@ namespace Nz::ShaderAst node.expression->Visit(*this); } - void ShaderAstValueCategory::Visit(VariableExpression& node) + void ShaderAstValueCategory::Visit(VariableExpression& /*node*/) { m_expressionCategory = ExpressionCategory::LValue; } + + void ShaderAstValueCategory::Visit(UnaryExpression& /*node*/) + { + m_expressionCategory = ExpressionCategory::RValue; + } } diff --git a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp index 50f86f59e..54fb5f8eb 100644 --- a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp +++ b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp @@ -592,6 +592,41 @@ namespace Nz::ShaderAst return clone; } + ExpressionPtr SanitizeVisitor::Clone(UnaryExpression& node) + { + auto clone = static_unique_pointer_cast(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(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) { auto clone = std::make_unique(); diff --git a/src/Nazara/Shader/GlslWriter.cpp b/src/Nazara/Shader/GlslWriter.cpp index 3d355f4a6..c3a419fd1 100644 --- a/src/Nazara/Shader/GlslWriter.cpp +++ b/src/Nazara/Shader/GlslWriter.cpp @@ -922,6 +922,26 @@ namespace Nz 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) { /*for (const auto& uniform : shader.GetUniforms()) diff --git a/src/Nazara/Shader/LangWriter.cpp b/src/Nazara/Shader/LangWriter.cpp index d836214de..13031737b 100644 --- a/src/Nazara/Shader/LangWriter.cpp +++ b/src/Nazara/Shader/LangWriter.cpp @@ -107,7 +107,7 @@ namespace Nz }, type); } - void LangWriter::Append(const ShaderAst::IdentifierType& identifierType) + void LangWriter::Append(const ShaderAst::IdentifierType& /*identifierType*/) { throw std::runtime_error("unexpected identifier type"); } @@ -730,6 +730,26 @@ namespace Nz 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() { // Nothing yet diff --git a/src/Nazara/Shader/ShaderLangParser.cpp b/src/Nazara/Shader/ShaderLangParser.cpp index d3978f8bb..66cf22c3c 100644 --- a/src/Nazara/Shader/ShaderLangParser.cpp +++ b/src/Nazara/Shader/ShaderLangParser.cpp @@ -802,10 +802,10 @@ namespace Nz::ShaderLang return ParseBinOpRhs(0, ParsePrimaryExpression()); } - ShaderAst::ExpressionPtr Parser::ParseFloatingPointExpression(bool minus) + ShaderAst::ExpressionPtr Parser::ParseFloatingPointExpression() { const Token& floatingPointToken = Expect(Advance(), TokenType::FloatingPointValue); - return ShaderBuilder::Constant(((minus) ? -1.f : 1.f) * float(std::get(floatingPointToken.data))); //< FIXME + return ShaderBuilder::Constant(float(std::get(floatingPointToken.data))); //< FIXME } ShaderAst::ExpressionPtr Parser::ParseIdentifier() @@ -816,10 +816,10 @@ namespace Nz::ShaderLang return ShaderBuilder::Identifier(identifier); } - ShaderAst::ExpressionPtr Parser::ParseIntegerExpression(bool minus) + ShaderAst::ExpressionPtr Parser::ParseIntegerExpression() { const Token& integerToken = Expect(Advance(), TokenType::IntegerValue); - return ShaderBuilder::Constant(((minus) ? -1 : 1) * static_cast(std::get(integerToken.data))); + return ShaderBuilder::Constant(static_cast(std::get(integerToken.data))); //< FIXME } std::vector Parser::ParseParameters() @@ -894,21 +894,20 @@ namespace Nz::ShaderLang return ParseIntegerExpression(); case TokenType::Minus: - //< FIXME: Handle this with an unary node - if (Peek(1).type == TokenType::FloatingPointValue) - { - Consume(); - return ParseFloatingPointExpression(true); - } - else if (Peek(1).type == TokenType::IntegerValue) - { - Consume(); - return ParseIntegerExpression(true); - } - else - throw UnexpectedToken{}; + { + Consume(); + ShaderAst::ExpressionPtr expr = ParsePrimaryExpression(); - 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: return ParseParenthesisExpression(); diff --git a/src/Nazara/Shader/SpirvAstVisitor.cpp b/src/Nazara/Shader/SpirvAstVisitor.cpp index 9cab9ced4..f731cb7c2 100644 --- a/src/Nazara/Shader/SpirvAstVisitor.cpp +++ b/src/Nazara/Shader/SpirvAstVisitor.cpp @@ -815,6 +815,61 @@ namespace Nz 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(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(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) { m_resultIds.push_back(value); diff --git a/src/Nazara/Shader/SpirvWriter.cpp b/src/Nazara/Shader/SpirvWriter.cpp index 30e9c9b43..5bed57c67 100644 --- a/src/Nazara/Shader/SpirvWriter.cpp +++ b/src/Nazara/Shader/SpirvWriter.cpp @@ -78,6 +78,13 @@ namespace Nz 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 { if (TestBit(m_states.enabledOptions, node.optionIndex)) @@ -294,6 +301,13 @@ namespace Nz 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) { if (member.builtin)