Shader: Add comments to surround modules

This commit is contained in:
Jérôme Leclercq
2022-03-06 17:25:16 +01:00
parent 4bded2182c
commit a4858d6793
9 changed files with 65 additions and 0 deletions

View File

@@ -231,6 +231,7 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(MultiStatement& node)
{
auto clone = std::make_unique<MultiStatement>();
clone->sectionName = node.sectionName;
clone->statements.reserve(node.statements.size());
for (auto& statement : node.statements)
clone->statements.push_back(CloneStatement(statement));

View File

@@ -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);

View File

@@ -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<MultiStatement>();
clone->sectionName = node.sectionName;
clone->statements.reserve(node.statements.size());
std::vector<StatementPtr>* previousList = m_context->currentStatementList;

View File

@@ -403,6 +403,26 @@ namespace Nz
Append(secondParam, std::forward<Args>(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*/)

View File

@@ -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*/)