Renderer/GlslWriter: Move variables to the function scope
This commit is contained in:
parent
5c6df52fbf
commit
5c3e67bb26
|
|
@ -44,7 +44,7 @@ namespace Nz
|
||||||
|
|
||||||
void Append(ShaderAst::ExpressionType type);
|
void Append(ShaderAst::ExpressionType type);
|
||||||
void Append(const String& txt);
|
void Append(const String& txt);
|
||||||
void AppendFunction(const Function& func);
|
void AppendFunction(Function& func);
|
||||||
void AppendLine(const Nz::String& txt = Nz::String());
|
void AppendLine(const Nz::String& txt = Nz::String());
|
||||||
|
|
||||||
void EnterScope();
|
void EnterScope();
|
||||||
|
|
@ -52,6 +52,7 @@ namespace Nz
|
||||||
|
|
||||||
struct Function
|
struct Function
|
||||||
{
|
{
|
||||||
|
std::set<std::pair<ShaderAst::ExpressionType, String>> variables;
|
||||||
std::vector<ShaderAst::VariablePtr> parameters;
|
std::vector<ShaderAst::VariablePtr> parameters;
|
||||||
ShaderAst::ExpressionType retType;
|
ShaderAst::ExpressionType retType;
|
||||||
ShaderAst::StatementPtr node;
|
ShaderAst::StatementPtr node;
|
||||||
|
|
@ -60,12 +61,13 @@ namespace Nz
|
||||||
|
|
||||||
struct State
|
struct State
|
||||||
{
|
{
|
||||||
std::set<std::pair<ShaderAst::ExpressionType, String>> m_variables;
|
std::set<std::pair<ShaderAst::ExpressionType, String>> m_uniforms;
|
||||||
StringStream stream;
|
StringStream stream;
|
||||||
unsigned int indentLevel = 0;
|
unsigned int indentLevel = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unordered_map<String, Function> m_functions;
|
std::unordered_map<String, Function> m_functions;
|
||||||
|
Function* m_currentFunction;
|
||||||
State* m_currentState;
|
State* m_currentState;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
GlslWriter::GlslWriter() :
|
GlslWriter::GlslWriter() :
|
||||||
|
m_currentFunction(nullptr),
|
||||||
m_currentState(nullptr)
|
m_currentState(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -24,7 +25,7 @@ namespace Nz
|
||||||
|
|
||||||
node->Register(*this);
|
node->Register(*this);
|
||||||
|
|
||||||
for (const auto& pair : state.m_variables)
|
for (const auto& pair : state.m_uniforms)
|
||||||
{
|
{
|
||||||
Append(pair.first);
|
Append(pair.first);
|
||||||
Append(" ");
|
Append(" ");
|
||||||
|
|
@ -59,7 +60,8 @@ namespace Nz
|
||||||
{
|
{
|
||||||
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");
|
||||||
|
|
||||||
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)
|
void GlslWriter::Write(const ShaderAst::NodePtr& node)
|
||||||
|
|
@ -200,10 +202,19 @@ namespace Nz
|
||||||
m_currentState->stream << txt;
|
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");
|
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);
|
Append(func.retType);
|
||||||
|
|
||||||
m_currentState->stream << ' ';
|
m_currentState->stream << ' ';
|
||||||
|
|
@ -222,7 +233,19 @@ namespace Nz
|
||||||
m_currentState->stream << ")\n";
|
m_currentState->stream << ")\n";
|
||||||
|
|
||||||
EnterScope();
|
EnterScope();
|
||||||
|
{
|
||||||
|
for (const auto& pair : func.variables)
|
||||||
|
{
|
||||||
|
Append(pair.first);
|
||||||
|
Append(" ");
|
||||||
|
Append(pair.second);
|
||||||
|
AppendLine(";");
|
||||||
|
}
|
||||||
|
|
||||||
|
AppendLine();
|
||||||
|
|
||||||
Write(func.node);
|
Write(func.node);
|
||||||
|
}
|
||||||
LeaveScope();
|
LeaveScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue