diff --git a/include/Nazara/Shader/Ast/AstCompare.inl b/include/Nazara/Shader/Ast/AstCompare.inl index aa9a6bd37..95e6d40e8 100644 --- a/include/Nazara/Shader/Ast/AstCompare.inl +++ b/include/Nazara/Shader/Ast/AstCompare.inl @@ -519,6 +519,9 @@ namespace Nz::ShaderAst inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs) { + if (!Compare(lhs.sectionName, rhs.sectionName)) + return false; + if (!Compare(lhs.statements, rhs.statements)) return false; diff --git a/include/Nazara/Shader/Ast/Nodes.hpp b/include/Nazara/Shader/Ast/Nodes.hpp index ebef534bc..2b74767c2 100644 --- a/include/Nazara/Shader/Ast/Nodes.hpp +++ b/include/Nazara/Shader/Ast/Nodes.hpp @@ -385,6 +385,7 @@ namespace Nz::ShaderAst NodeType GetType() const override; void Visit(AstStatementVisitor& visitor) override; + std::string sectionName; std::vector statements; }; diff --git a/include/Nazara/Shader/GlslWriter.hpp b/include/Nazara/Shader/GlslWriter.hpp index 8a1694bf4..19ee64969 100644 --- a/include/Nazara/Shader/GlslWriter.hpp +++ b/include/Nazara/Shader/GlslWriter.hpp @@ -70,6 +70,7 @@ namespace Nz void Append(const ShaderAst::VectorType& vecType); template void Append(const T& param); template void Append(const T1& firstParam, const T2& secondParam, Args&&... params); + void AppendComment(const std::string& section); void AppendCommentSection(const std::string& section); void AppendFunctionDeclaration(const ShaderAst::DeclareFunctionStatement& node, bool forward = false); void AppendHeader(); diff --git a/include/Nazara/Shader/LangWriter.hpp b/include/Nazara/Shader/LangWriter.hpp index fe4bdc05d..522023877 100644 --- a/include/Nazara/Shader/LangWriter.hpp +++ b/include/Nazara/Shader/LangWriter.hpp @@ -77,6 +77,7 @@ namespace Nz void AppendAttribute(LocationAttribute location); void AppendAttribute(SetAttribute set); void AppendAttribute(UnrollAttribute unroll); + void AppendComment(const std::string& section); void AppendCommentSection(const std::string& section); void AppendHeader(); void AppendLine(const std::string& txt = {}); diff --git a/src/Nazara/Shader/Ast/AstCloner.cpp b/src/Nazara/Shader/Ast/AstCloner.cpp index f5e95b163..353c1071e 100644 --- a/src/Nazara/Shader/Ast/AstCloner.cpp +++ b/src/Nazara/Shader/Ast/AstCloner.cpp @@ -231,6 +231,7 @@ namespace Nz::ShaderAst StatementPtr AstCloner::Clone(MultiStatement& node) { auto clone = std::make_unique(); + clone->sectionName = node.sectionName; clone->statements.reserve(node.statements.size()); for (auto& statement : node.statements) clone->statements.push_back(CloneStatement(statement)); diff --git a/src/Nazara/Shader/Ast/AstSerializer.cpp b/src/Nazara/Shader/Ast/AstSerializer.cpp index 7052cb6dd..8f3ad1861 100644 --- a/src/Nazara/Shader/Ast/AstSerializer.cpp +++ b/src/Nazara/Shader/Ast/AstSerializer.cpp @@ -315,6 +315,8 @@ namespace Nz::ShaderAst void AstSerializerBase::Serialize(MultiStatement& node) { + Value(node.sectionName); + Container(node.statements); for (auto& statement : node.statements) Node(statement); diff --git a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp index db241ea91..f6069d56b 100644 --- a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp +++ b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp @@ -1389,6 +1389,8 @@ namespace Nz::ShaderAst if (!targetModule) throw AstError{ "module " + ModulePathAsString() + " not found" }; + targetModule->rootNode->sectionName = "Module " + targetModule->metadata->moduleId.ToString(); + std::string error; ModulePtr sanitizedModule = ShaderAst::Sanitize(*targetModule, m_context->options, &error); if (!sanitizedModule) @@ -1462,6 +1464,7 @@ namespace Nz::ShaderAst StatementPtr SanitizeVisitor::Clone(MultiStatement& node) { auto clone = std::make_unique(); + clone->sectionName = node.sectionName; clone->statements.reserve(node.statements.size()); std::vector* previousList = m_context->currentStatementList; diff --git a/src/Nazara/Shader/GlslWriter.cpp b/src/Nazara/Shader/GlslWriter.cpp index 44879e42f..050a41583 100644 --- a/src/Nazara/Shader/GlslWriter.cpp +++ b/src/Nazara/Shader/GlslWriter.cpp @@ -403,6 +403,26 @@ namespace Nz Append(secondParam, std::forward(params)...); } + void GlslWriter::AppendComment(const std::string& section) + { + std::size_t lineFeed = section.find('\n'); + if (lineFeed != section.npos) + { + std::size_t previousCut = 0; + + AppendLine("/*"); + do + { + AppendLine(section.substr(previousCut, lineFeed - previousCut)); + previousCut = lineFeed + 1; + } while ((lineFeed = section.find('\n', previousCut)) != section.npos); + AppendLine(section.substr(previousCut)); + AppendLine("*/"); + } + else + AppendLine("// ", section); + } + void GlslWriter::AppendCommentSection(const std::string& section) { NazaraAssert(m_currentState, "This function should only be called while processing an AST"); @@ -1208,7 +1228,13 @@ namespace Nz void GlslWriter::Visit(ShaderAst::MultiStatement& node) { + if (!node.sectionName.empty()) + AppendComment(node.sectionName); + AppendStatementList(node.statements); + + if (!node.sectionName.empty()) + AppendComment("End: " + node.sectionName); } void GlslWriter::Visit(ShaderAst::NoOpStatement& /*node*/) diff --git a/src/Nazara/Shader/LangWriter.cpp b/src/Nazara/Shader/LangWriter.cpp index 19b776128..5371699c5 100644 --- a/src/Nazara/Shader/LangWriter.cpp +++ b/src/Nazara/Shader/LangWriter.cpp @@ -502,6 +502,27 @@ namespace Nz unroll.unroll.GetExpression()->Visit(*this); } + void LangWriter::AppendComment(const std::string& section) + { + std::size_t lineFeed = section.find('\n'); + if (lineFeed != section.npos) + { + std::size_t previousCut = 0; + + AppendLine("/*"); + do + { + AppendLine(section.substr(previousCut, lineFeed - previousCut)); + previousCut = lineFeed + 1; + } + while ((lineFeed = section.find('\n', previousCut)) != section.npos); + AppendLine(section.substr(previousCut)); + AppendLine("*/"); + } + else + AppendLine("// ", section); + } + void LangWriter::AppendCommentSection(const std::string& section) { NazaraAssert(m_currentState, "This function should only be called while processing an AST"); @@ -1032,7 +1053,13 @@ namespace Nz void LangWriter::Visit(ShaderAst::MultiStatement& node) { + if (!node.sectionName.empty()) + AppendComment(node.sectionName); + AppendStatementList(node.statements); + + if (!node.sectionName.empty()) + AppendComment("End: " + node.sectionName); } void LangWriter::Visit(ShaderAst::NoOpStatement& /*node*/)