From b6cd85d6fee666315c5167e9d55e7318badc971e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Fri, 4 Mar 2022 18:27:37 +0100 Subject: [PATCH] Shader: Add import statement (not doing anything for now) --- include/Nazara/Shader/Ast/AstCloner.hpp | 1 + include/Nazara/Shader/Ast/AstCompare.hpp | 1 + include/Nazara/Shader/Ast/AstCompare.inl | 8 ++++++++ include/Nazara/Shader/Ast/AstNodeList.hpp | 1 + include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp | 1 + include/Nazara/Shader/Ast/AstSerializer.hpp | 1 + include/Nazara/Shader/Ast/Nodes.hpp | 8 ++++++++ include/Nazara/Shader/Ast/SanitizeVisitor.hpp | 1 + include/Nazara/Shader/GlslWriter.hpp | 1 + include/Nazara/Shader/LangWriter.hpp | 1 + include/Nazara/Shader/ShaderBuilder.hpp | 6 ++++++ include/Nazara/Shader/ShaderBuilder.inl | 8 ++++++++ include/Nazara/Shader/ShaderLangParser.hpp | 1 + include/Nazara/Shader/ShaderLangTokenList.hpp | 1 + include/Nazara/Shader/SpirvAstVisitor.hpp | 1 + src/Nazara/Shader/Ast/AstCloner.cpp | 8 ++++++++ src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp | 5 +++++ src/Nazara/Shader/Ast/AstSerializer.cpp | 7 +++++++ src/Nazara/Shader/Ast/SanitizeVisitor.cpp | 5 +++++ src/Nazara/Shader/LangWriter.cpp | 14 ++++++++++++++ src/Nazara/Shader/ShaderLangLexer.cpp | 1 + src/Nazara/Shader/ShaderLangParser.cpp | 15 +++++++++++++++ src/Nazara/Shader/SpirvAstVisitor.cpp | 5 +++++ 23 files changed, 101 insertions(+) diff --git a/include/Nazara/Shader/Ast/AstCloner.hpp b/include/Nazara/Shader/Ast/AstCloner.hpp index 1cf051d72..f4520c6d9 100644 --- a/include/Nazara/Shader/Ast/AstCloner.hpp +++ b/include/Nazara/Shader/Ast/AstCloner.hpp @@ -68,6 +68,7 @@ namespace Nz::ShaderAst virtual StatementPtr Clone(ExpressionStatement& node); virtual StatementPtr Clone(ForStatement& node); virtual StatementPtr Clone(ForEachStatement& node); + virtual StatementPtr Clone(ImportStatement& node); virtual StatementPtr Clone(MultiStatement& node); virtual StatementPtr Clone(NoOpStatement& node); virtual StatementPtr Clone(ReturnStatement& node); diff --git a/include/Nazara/Shader/Ast/AstCompare.hpp b/include/Nazara/Shader/Ast/AstCompare.hpp index a88a2dbbe..4d3094028 100644 --- a/include/Nazara/Shader/Ast/AstCompare.hpp +++ b/include/Nazara/Shader/Ast/AstCompare.hpp @@ -56,6 +56,7 @@ namespace Nz::ShaderAst inline bool Compare(const ExpressionStatement& lhs, const ExpressionStatement& rhs); inline bool Compare(const ForStatement& lhs, const ForStatement& rhs); inline bool Compare(const ForEachStatement& lhs, const ForEachStatement& rhs); + inline bool Compare(const ImportStatement& lhs, const ImportStatement& rhs); inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs); inline bool Compare(const NoOpStatement& lhs, const NoOpStatement& rhs); inline bool Compare(const ReturnStatement& lhs, const ReturnStatement& rhs); diff --git a/include/Nazara/Shader/Ast/AstCompare.inl b/include/Nazara/Shader/Ast/AstCompare.inl index 900ae3bdf..aa9a6bd37 100644 --- a/include/Nazara/Shader/Ast/AstCompare.inl +++ b/include/Nazara/Shader/Ast/AstCompare.inl @@ -509,6 +509,14 @@ namespace Nz::ShaderAst return true; } + bool Compare(const ImportStatement& lhs, const ImportStatement& rhs) + { + if (!Compare(lhs.modulePath, rhs.modulePath)) + return false; + + return true; + } + inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs) { if (!Compare(lhs.statements, rhs.statements)) diff --git a/include/Nazara/Shader/Ast/AstNodeList.hpp b/include/Nazara/Shader/Ast/AstNodeList.hpp index 84e162ac3..8714f3436 100644 --- a/include/Nazara/Shader/Ast/AstNodeList.hpp +++ b/include/Nazara/Shader/Ast/AstNodeList.hpp @@ -55,6 +55,7 @@ NAZARA_SHADERAST_STATEMENT(DiscardStatement) NAZARA_SHADERAST_STATEMENT(ForStatement) NAZARA_SHADERAST_STATEMENT(ForEachStatement) NAZARA_SHADERAST_STATEMENT(ExpressionStatement) +NAZARA_SHADERAST_STATEMENT(ImportStatement) NAZARA_SHADERAST_STATEMENT(MultiStatement) NAZARA_SHADERAST_STATEMENT(NoOpStatement) NAZARA_SHADERAST_STATEMENT(ReturnStatement) diff --git a/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp b/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp index f09269bd9..d85ca554e 100644 --- a/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp +++ b/include/Nazara/Shader/Ast/AstRecursiveVisitor.hpp @@ -48,6 +48,7 @@ namespace Nz::ShaderAst void Visit(ExpressionStatement& node) override; void Visit(ForStatement& node) override; void Visit(ForEachStatement& node) override; + void Visit(ImportStatement& node) override; void Visit(MultiStatement& node) override; void Visit(NoOpStatement& node) override; void Visit(ReturnStatement& node) override; diff --git a/include/Nazara/Shader/Ast/AstSerializer.hpp b/include/Nazara/Shader/Ast/AstSerializer.hpp index 117f6aa55..3170bf479 100644 --- a/include/Nazara/Shader/Ast/AstSerializer.hpp +++ b/include/Nazara/Shader/Ast/AstSerializer.hpp @@ -51,6 +51,7 @@ namespace Nz::ShaderAst void Serialize(ExpressionStatement& node); void Serialize(ForStatement& node); void Serialize(ForEachStatement& node); + void Serialize(ImportStatement& node); void Serialize(MultiStatement& node); void Serialize(NoOpStatement& node); void Serialize(ReturnStatement& node); diff --git a/include/Nazara/Shader/Ast/Nodes.hpp b/include/Nazara/Shader/Ast/Nodes.hpp index 6ffd24a6b..7486988f7 100644 --- a/include/Nazara/Shader/Ast/Nodes.hpp +++ b/include/Nazara/Shader/Ast/Nodes.hpp @@ -367,6 +367,14 @@ namespace Nz::ShaderAst StatementPtr statement; }; + struct NAZARA_SHADER_API ImportStatement : Statement + { + NodeType GetType() const override; + void Visit(AstStatementVisitor& visitor) override; + + std::vector modulePath; + }; + struct NAZARA_SHADER_API MultiStatement : Statement { NodeType GetType() const override; diff --git a/include/Nazara/Shader/Ast/SanitizeVisitor.hpp b/include/Nazara/Shader/Ast/SanitizeVisitor.hpp index 3f5736622..7e355dc48 100644 --- a/include/Nazara/Shader/Ast/SanitizeVisitor.hpp +++ b/include/Nazara/Shader/Ast/SanitizeVisitor.hpp @@ -88,6 +88,7 @@ namespace Nz::ShaderAst StatementPtr Clone(ExpressionStatement& node) override; StatementPtr Clone(ForStatement& node) override; StatementPtr Clone(ForEachStatement& node) override; + StatementPtr Clone(ImportStatement& node) override; StatementPtr Clone(MultiStatement& node) override; StatementPtr Clone(ScopedStatement& node) override; StatementPtr Clone(WhileStatement& node) override; diff --git a/include/Nazara/Shader/GlslWriter.hpp b/include/Nazara/Shader/GlslWriter.hpp index 3beebf2f8..1a4ecbe38 100644 --- a/include/Nazara/Shader/GlslWriter.hpp +++ b/include/Nazara/Shader/GlslWriter.hpp @@ -112,6 +112,7 @@ namespace Nz void Visit(ShaderAst::DeclareVariableStatement& node) override; void Visit(ShaderAst::DiscardStatement& node) override; void Visit(ShaderAst::ExpressionStatement& node) override; + void Visit(ShaderAst::ImportStatement& node) override; void Visit(ShaderAst::MultiStatement& node) override; void Visit(ShaderAst::NoOpStatement& node) override; void Visit(ShaderAst::ReturnStatement& node) override; diff --git a/include/Nazara/Shader/LangWriter.hpp b/include/Nazara/Shader/LangWriter.hpp index 50caae0d4..dded8b8a9 100644 --- a/include/Nazara/Shader/LangWriter.hpp +++ b/include/Nazara/Shader/LangWriter.hpp @@ -119,6 +119,7 @@ namespace Nz void Visit(ShaderAst::ExpressionStatement& node) override; void Visit(ShaderAst::ForStatement& node) override; void Visit(ShaderAst::ForEachStatement& node) override; + void Visit(ShaderAst::ImportStatement& node) override; void Visit(ShaderAst::MultiStatement& node) override; void Visit(ShaderAst::NoOpStatement& node) override; void Visit(ShaderAst::ReturnStatement& node) override; diff --git a/include/Nazara/Shader/ShaderBuilder.hpp b/include/Nazara/Shader/ShaderBuilder.hpp index a37389269..a8f130849 100644 --- a/include/Nazara/Shader/ShaderBuilder.hpp +++ b/include/Nazara/Shader/ShaderBuilder.hpp @@ -127,6 +127,11 @@ namespace Nz::ShaderBuilder inline std::unique_ptr operator()(std::string name) const; }; + struct Import + { + inline std::unique_ptr operator()(std::vector modulePath) const; + }; + struct Intrinsic { inline std::unique_ptr operator()(ShaderAst::IntrinsicType intrinsicType, std::vector parameters) const; @@ -196,6 +201,7 @@ namespace Nz::ShaderBuilder constexpr Impl::For For; constexpr Impl::ForEach ForEach; constexpr Impl::Identifier Identifier; + constexpr Impl::Import Import; constexpr Impl::Intrinsic Intrinsic; constexpr Impl::Multi MultiStatement; constexpr Impl::NoParam NoOp; diff --git a/include/Nazara/Shader/ShaderBuilder.inl b/include/Nazara/Shader/ShaderBuilder.inl index 482e624ff..f1f5dae2d 100644 --- a/include/Nazara/Shader/ShaderBuilder.inl +++ b/include/Nazara/Shader/ShaderBuilder.inl @@ -347,6 +347,14 @@ namespace Nz::ShaderBuilder return identifierNode; } + inline std::unique_ptr Impl::Import::operator()(std::vector modulePath) const + { + auto importNode = std::make_unique(); + importNode->modulePath = std::move(modulePath); + + return importNode; + } + inline std::unique_ptr Impl::Intrinsic::operator()(ShaderAst::IntrinsicType intrinsicType, std::vector parameters) const { auto intrinsicExpression = std::make_unique(); diff --git a/include/Nazara/Shader/ShaderLangParser.hpp b/include/Nazara/Shader/ShaderLangParser.hpp index 81360e7b0..1a7a6db44 100644 --- a/include/Nazara/Shader/ShaderLangParser.hpp +++ b/include/Nazara/Shader/ShaderLangParser.hpp @@ -94,6 +94,7 @@ namespace Nz::ShaderLang std::vector ParseFunctionBody(); ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector attributes = {}); ShaderAst::DeclareFunctionStatement::Parameter ParseFunctionParameter(); + ShaderAst::StatementPtr ParseImportStatement(); ShaderAst::StatementPtr ParseOptionDeclaration(); ShaderAst::StatementPtr ParseReturnStatement(); ShaderAst::StatementPtr ParseRootStatement(std::vector attributes = {}); diff --git a/include/Nazara/Shader/ShaderLangTokenList.hpp b/include/Nazara/Shader/ShaderLangTokenList.hpp index beb4cf17c..4596050ee 100644 --- a/include/Nazara/Shader/ShaderLangTokenList.hpp +++ b/include/Nazara/Shader/ShaderLangTokenList.hpp @@ -39,6 +39,7 @@ NAZARA_SHADERLANG_TOKEN(GreaterThanEqual) NAZARA_SHADERLANG_TOKEN(IntegerValue) NAZARA_SHADERLANG_TOKEN(Identifier) NAZARA_SHADERLANG_TOKEN(If) +NAZARA_SHADERLANG_TOKEN(Import) NAZARA_SHADERLANG_TOKEN(In) NAZARA_SHADERLANG_TOKEN(LessThan) NAZARA_SHADERLANG_TOKEN(LessThanEqual) diff --git a/include/Nazara/Shader/SpirvAstVisitor.hpp b/include/Nazara/Shader/SpirvAstVisitor.hpp index 7f71c0a51..d9768a1d5 100644 --- a/include/Nazara/Shader/SpirvAstVisitor.hpp +++ b/include/Nazara/Shader/SpirvAstVisitor.hpp @@ -56,6 +56,7 @@ namespace Nz void Visit(ShaderAst::DeclareVariableStatement& node) override; void Visit(ShaderAst::DiscardStatement& node) override; void Visit(ShaderAst::ExpressionStatement& node) override; + void Visit(ShaderAst::ImportStatement& node) override; void Visit(ShaderAst::IntrinsicExpression& node) override; void Visit(ShaderAst::MultiStatement& node) override; void Visit(ShaderAst::NoOpStatement& node) override; diff --git a/src/Nazara/Shader/Ast/AstCloner.cpp b/src/Nazara/Shader/Ast/AstCloner.cpp index 43ddda43e..b031d9805 100644 --- a/src/Nazara/Shader/Ast/AstCloner.cpp +++ b/src/Nazara/Shader/Ast/AstCloner.cpp @@ -215,6 +215,14 @@ namespace Nz::ShaderAst return clone; } + StatementPtr AstCloner::Clone(ImportStatement& node) + { + auto clone = std::make_unique(); + clone->modulePath = node.modulePath; + + return clone; + } + StatementPtr AstCloner::Clone(MultiStatement& node) { auto clone = std::make_unique(); diff --git a/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp b/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp index 73718cf6b..613dea9c0 100644 --- a/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp +++ b/src/Nazara/Shader/Ast/AstRecursiveVisitor.cpp @@ -185,6 +185,11 @@ namespace Nz::ShaderAst node.statement->Visit(*this); } + void AstRecursiveVisitor::Visit(ImportStatement& /*node*/) + { + /* nothing to do */ + } + void AstRecursiveVisitor::Visit(MultiStatement& node) { for (auto& statement : node.statements) diff --git a/src/Nazara/Shader/Ast/AstSerializer.cpp b/src/Nazara/Shader/Ast/AstSerializer.cpp index 1d587e2a1..95d5b949e 100644 --- a/src/Nazara/Shader/Ast/AstSerializer.cpp +++ b/src/Nazara/Shader/Ast/AstSerializer.cpp @@ -301,6 +301,13 @@ namespace Nz::ShaderAst Node(node.statement); } + void AstSerializerBase::Serialize(ImportStatement& node) + { + Container(node.modulePath); + for (auto& path : node.modulePath) + Value(path); + } + void AstSerializerBase::Serialize(MultiStatement& node) { Container(node.statements); diff --git a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp index f85a15dad..e6c5d45fe 100644 --- a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp +++ b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp @@ -1308,6 +1308,11 @@ namespace Nz::ShaderAst } } + StatementPtr SanitizeVisitor::Clone(ImportStatement& node) + { + return static_unique_pointer_cast(AstCloner::Clone(node)); + } + StatementPtr SanitizeVisitor::Clone(MultiStatement& node) { auto clone = std::make_unique(); diff --git a/src/Nazara/Shader/LangWriter.cpp b/src/Nazara/Shader/LangWriter.cpp index a935b1097..c2ed4e0f0 100644 --- a/src/Nazara/Shader/LangWriter.cpp +++ b/src/Nazara/Shader/LangWriter.cpp @@ -954,6 +954,20 @@ namespace Nz ScopeVisit(*node.statement); } + void LangWriter::Visit(ShaderAst::ImportStatement& node) + { + bool first = true; + for (const std::string& path : node.modulePath) + { + if (!first) + Append("/"); + + Append(path); + + first = false; + } + } + void LangWriter::Visit(ShaderAst::IntrinsicExpression& node) { bool method = false; diff --git a/src/Nazara/Shader/ShaderLangLexer.cpp b/src/Nazara/Shader/ShaderLangLexer.cpp index 798a92f3c..0523e47a8 100644 --- a/src/Nazara/Shader/ShaderLangLexer.cpp +++ b/src/Nazara/Shader/ShaderLangLexer.cpp @@ -49,6 +49,7 @@ namespace Nz::ShaderLang { "fn", TokenType::FunctionDeclaration }, { "for", TokenType::For }, { "if", TokenType::If }, + { "import", TokenType::Import }, { "in", TokenType::In }, { "let", TokenType::Let }, { "module", TokenType::Module }, diff --git a/src/Nazara/Shader/ShaderLangParser.cpp b/src/Nazara/Shader/ShaderLangParser.cpp index 114b53d79..0e4b590ae 100644 --- a/src/Nazara/Shader/ShaderLangParser.cpp +++ b/src/Nazara/Shader/ShaderLangParser.cpp @@ -649,14 +649,23 @@ namespace Nz::ShaderLang return { parameterName, std::move(parameterType) }; } + ShaderAst::StatementPtr Parser::ParseImportStatement() { + Expect(Advance(), TokenType::Import); + std::vector modulePath; + modulePath.push_back(ParseIdentifierAsName()); + while (Peek().type == TokenType::Divide) //< / { + Consume(); + modulePath.push_back(ParseIdentifierAsName()); } Expect(Advance(), TokenType::Semicolon); + + return ShaderBuilder::Import(std::move(modulePath)); } ShaderAst::StatementPtr Parser::ParseOptionDeclaration() @@ -715,6 +724,12 @@ namespace Nz::ShaderLang case TokenType::External: return ParseExternalBlock(std::move(attributes)); + case TokenType::Import: + if (!attributes.empty()) + throw UnexpectedToken{}; + + return ParseImportStatement(); + case TokenType::OpenSquareBracket: assert(attributes.empty()); return ParseRootStatement(ParseAttributes()); diff --git a/src/Nazara/Shader/SpirvAstVisitor.cpp b/src/Nazara/Shader/SpirvAstVisitor.cpp index b908c422d..89659d042 100644 --- a/src/Nazara/Shader/SpirvAstVisitor.cpp +++ b/src/Nazara/Shader/SpirvAstVisitor.cpp @@ -726,6 +726,11 @@ namespace Nz PopResultId(); } + void SpirvAstVisitor::Visit(ShaderAst::ImportStatement& node) + { + /* nothing to do */ + } + void SpirvAstVisitor::Visit(ShaderAst::IntrinsicExpression& node) { switch (node.intrinsic)