Renderer/GlslWriter: Move variables to the function scope

This commit is contained in:
Lynix 2017-01-04 11:39:57 +01:00
parent 5c6df52fbf
commit 5c3e67bb26
2 changed files with 30 additions and 5 deletions

View File

@ -44,7 +44,7 @@ namespace Nz
void Append(ShaderAst::ExpressionType type);
void Append(const String& txt);
void AppendFunction(const Function& func);
void AppendFunction(Function& func);
void AppendLine(const Nz::String& txt = Nz::String());
void EnterScope();
@ -52,6 +52,7 @@ namespace Nz
struct Function
{
std::set<std::pair<ShaderAst::ExpressionType, String>> variables;
std::vector<ShaderAst::VariablePtr> parameters;
ShaderAst::ExpressionType retType;
ShaderAst::StatementPtr node;
@ -60,12 +61,13 @@ namespace Nz
struct State
{
std::set<std::pair<ShaderAst::ExpressionType, String>> m_variables;
std::set<std::pair<ShaderAst::ExpressionType, String>> m_uniforms;
StringStream stream;
unsigned int indentLevel = 0;
};
std::unordered_map<String, Function> m_functions;
Function* m_currentFunction;
State* m_currentState;
};
}

View File

@ -9,6 +9,7 @@
namespace Nz
{
GlslWriter::GlslWriter() :
m_currentFunction(nullptr),
m_currentState(nullptr)
{
}
@ -24,7 +25,7 @@ namespace Nz
node->Register(*this);
for (const auto& pair : state.m_variables)
for (const auto& pair : state.m_uniforms)
{
Append(pair.first);
Append(" ");
@ -59,7 +60,8 @@ namespace Nz
{
NazaraAssert(m_currentState, "This function should only be called while processing an AST");
m_currentState->m_variables.insert(std::make_pair(type, name));
if (m_currentFunction)
m_currentFunction->variables.insert(std::make_pair(type, name));
}
void GlslWriter::Write(const ShaderAst::NodePtr& node)
@ -200,10 +202,19 @@ namespace Nz
m_currentState->stream << txt;
}
void GlslWriter::AppendFunction(const Function& func)
void GlslWriter::AppendFunction(Function& func)
{
NazaraAssert(!m_currentFunction, "A function is already being processed");
NazaraAssert(m_currentState, "This function should only be called while processing an AST");
m_currentFunction = &func;
CallOnExit onExit([this] ()
{
m_currentFunction = nullptr;
});
func.node->Register(*this);
Append(func.retType);
m_currentState->stream << ' ';
@ -222,7 +233,19 @@ namespace Nz
m_currentState->stream << ")\n";
EnterScope();
{
for (const auto& pair : func.variables)
{
Append(pair.first);
Append(" ");
Append(pair.second);
AppendLine(";");
}
AppendLine();
Write(func.node);
}
LeaveScope();
}