Shader/GlslWriter: Improve GLSL readability

This commit is contained in:
Jérôme Leclercq 2021-04-14 20:10:11 +02:00
parent a90937eb4f
commit 0860a0689f
2 changed files with 23 additions and 17 deletions

View File

@ -64,6 +64,7 @@ namespace Nz
void AppendHeader(); void AppendHeader();
void AppendLine(const std::string& txt = {}); void AppendLine(const std::string& txt = {});
template<typename... Args> void AppendLine(Args&&... params); template<typename... Args> void AppendLine(Args&&... params);
void AppendStatementList(std::vector<ShaderAst::StatementPtr>& statements);
void EnterScope(); void EnterScope();
void LeaveScope(bool skipLine = true); void LeaveScope(bool skipLine = true);

View File

@ -311,6 +311,20 @@ namespace Nz
AppendLine(); AppendLine();
} }
void GlslWriter::AppendStatementList(std::vector<ShaderAst::StatementPtr>& statements)
{
bool first = true;
for (const ShaderAst::StatementPtr& statement : statements)
{
if (!first && statement->GetType() != ShaderAst::NodeType::NoOpStatement)
AppendLine();
statement->Visit(*this);
first = false;
}
}
void GlslWriter::EnterScope() void GlslWriter::EnterScope()
{ {
NazaraAssert(m_currentState, "This function should only be called while processing an AST"); NazaraAssert(m_currentState, "This function should only be called while processing an AST");
@ -361,8 +375,7 @@ namespace Nz
// Output struct is handled on return node // Output struct is handled on return node
m_currentState->isInEntryPoint = true; m_currentState->isInEntryPoint = true;
for (auto& statement : node.statements) AppendStatementList(node.statements);
statement->Visit(*this);
m_currentState->isInEntryPoint = false; m_currentState->isInEntryPoint = false;
} }
@ -711,8 +724,7 @@ namespace Nz
EnterScope(); EnterScope();
{ {
for (auto& statement : node.statements) AppendStatementList(node.statements);
statement->Visit(*this);
} }
LeaveScope(); LeaveScope();
} }
@ -758,7 +770,7 @@ namespace Nz
node.initialExpression->Visit(*this); node.initialExpression->Visit(*this);
} }
AppendLine(";"); Append(";");
} }
void GlslWriter::Visit(ShaderAst::DiscardStatement& /*node*/) void GlslWriter::Visit(ShaderAst::DiscardStatement& /*node*/)
@ -769,7 +781,7 @@ namespace Nz
void GlslWriter::Visit(ShaderAst::ExpressionStatement& node) void GlslWriter::Visit(ShaderAst::ExpressionStatement& node)
{ {
node.expression->Visit(*this); node.expression->Visit(*this);
AppendLine(";"); Append(";");
} }
void GlslWriter::Visit(ShaderAst::IntrinsicExpression& node) void GlslWriter::Visit(ShaderAst::IntrinsicExpression& node)
@ -802,16 +814,7 @@ namespace Nz
void GlslWriter::Visit(ShaderAst::MultiStatement& node) void GlslWriter::Visit(ShaderAst::MultiStatement& node)
{ {
bool first = true; AppendStatementList(node.statements);
for (const ShaderAst::StatementPtr& statement : node.statements)
{
if (!first && statement->GetType() != ShaderAst::NodeType::NoOpStatement)
AppendLine();
statement->Visit(*this);
first = false;
}
} }
void GlslWriter::Visit(ShaderAst::NoOpStatement& /*node*/) void GlslWriter::Visit(ShaderAst::NoOpStatement& /*node*/)
@ -843,6 +846,8 @@ namespace Nz
outputStructVarName = s_outputVarName; outputStructVarName = s_outputVarName;
} }
AppendLine();
for (const auto& [name, targetName] : m_currentState->outputFields) for (const auto& [name, targetName] : m_currentState->outputFields)
{ {
bool isOutputPosition = (m_currentState->stage == ShaderStageType::Vertex && m_environment.flipYPosition && targetName == "gl_Position"); bool isOutputPosition = (m_currentState->stage == ShaderStageType::Vertex && m_environment.flipYPosition && targetName == "gl_Position");
@ -853,7 +858,7 @@ namespace Nz
if (isOutputPosition) if (isOutputPosition)
Append(" * vec4(1.0, ", s_flipYUniformName, ", 1.0, 1.0)"); Append(" * vec4(1.0, ", s_flipYUniformName, ", 1.0, 1.0)");
Append(";"); AppendLine(";");
} }
AppendLine(); AppendLine();