Renderer/ShaderAst: Add input and outputs variables

This commit is contained in:
Lynix 2017-01-05 15:17:34 +01:00
parent f7c4c86934
commit 3ed661f387
4 changed files with 58 additions and 32 deletions

View File

@ -44,20 +44,24 @@ namespace Nz
private:
struct Function;
using VariableContainer = std::set<std::pair<ShaderAst::ExpressionType, String>>;
void Append(ShaderAst::Builtin builtin);
void Append(ShaderAst::ExpressionType type);
void Append(const String& txt);
void AppendCommentSection(const String& section);
void AppendFunction(Function& func);
void AppendLine(const Nz::String& txt = Nz::String());
void AppendLine(const String& txt = String());
void DeclareVariables(const VariableContainer& variables, const String& keyword = String(), const String& section = String());
void EnterScope();
void LeaveScope();
struct Function
{
std::set<std::pair<ShaderAst::ExpressionType, String>> variables;
VariableContainer variables;
std::vector<ShaderAst::NamedVariablePtr> parameters;
ShaderAst::ExpressionType retType;
ShaderAst::StatementPtr node;
@ -66,7 +70,9 @@ namespace Nz
struct State
{
std::set<std::pair<ShaderAst::ExpressionType, String>> m_uniforms;
VariableContainer inputs;
VariableContainer outputs;
VariableContainer uniforms;
StringStream stream;
unsigned int indentLevel = 0;
};

View File

@ -55,9 +55,11 @@ namespace Nz
enum class VariableType
{
Builtin,
Input,
Output,
Parameter,
Variable,
Uniform
Uniform,
Variable
};
//////////////////////////////////////////////////////////////////////////

View File

@ -50,6 +50,8 @@ namespace Nz { namespace ShaderBuilder
constexpr GenBuilder<ShaderAst::Branch> Branch;
constexpr GenBuilder<ShaderAst::Constant> Constant;
constexpr GenBuilder<ShaderAst::ExpressionStatement> ExprStatement;
constexpr VarBuilder<ShaderAst::VariableType::Input> Input;
constexpr VarBuilder<ShaderAst::VariableType::Output> Output;
constexpr VarBuilder<ShaderAst::VariableType::Parameter> Parameter;
constexpr VarBuilder<ShaderAst::VariableType::Uniform> Uniform;
constexpr VarBuilder<ShaderAst::VariableType::Variable> Variable;

View File

@ -31,23 +31,10 @@ namespace Nz
AppendLine(String::Number(m_glslVersion));
AppendLine();
// Uniforms
if (!state.m_uniforms.empty())
{
AppendCommentSection("Uniforms");
AppendLine();
for (const auto& pair : state.m_uniforms)
{
Append("uniform ");
Append(pair.first);
Append(" ");
Append(pair.second);
AppendLine(";");
}
AppendLine();
}
// Global variables (uniforms, input and outputs)
DeclareVariables(state.uniforms, "uniform", "Uniforms");
DeclareVariables(state.inputs, "in", "Inputs");
DeclareVariables(state.outputs, "out", "Outputs");
Function entryPoint;
entryPoint.name = "main"; //< GLSL has only one entry point name possible
@ -73,9 +60,18 @@ namespace Nz
void GlslWriter::RegisterVariable(ShaderAst::VariableType kind, const String& name, ShaderAst::ExpressionType type)
{
NazaraAssert(m_currentState, "This function should only be called while processing an AST");
NazaraAssert(kind != ShaderAst::VariableType::Builtin, "Builtin variables should not be registered");
switch (kind)
{
case ShaderAst::VariableType::Input:
m_currentState->inputs.insert(std::make_pair(type, name));
break;
case ShaderAst::VariableType::Output:
m_currentState->outputs.insert(std::make_pair(type, name));
break;
case ShaderAst::VariableType::Parameter:
{
if (m_currentFunction)
@ -105,7 +101,7 @@ namespace Nz
}
case ShaderAst::VariableType::Uniform:
m_currentState->m_uniforms.insert(std::make_pair(type, name));
m_currentState->uniforms.insert(std::make_pair(type, name));
break;
case ShaderAst::VariableType::Variable:
@ -320,15 +316,7 @@ namespace Nz
EnterScope();
{
for (const auto& pair : func.variables)
{
Append(pair.first);
Append(" ");
Append(pair.second);
AppendLine(";");
}
AppendLine();
DeclareVariables(func.variables);
Write(func.node);
}
@ -342,6 +330,34 @@ namespace Nz
m_currentState->stream << txt << '\n' << String(m_currentState->indentLevel, '\t');
}
void GlslWriter::DeclareVariables(const VariableContainer& variables, const String& keyword, const String& section)
{
if (!variables.empty())
{
if (!section.IsEmpty())
{
AppendCommentSection("Uniforms");
AppendLine();
}
for (const auto& pair : variables)
{
if (!keyword.IsEmpty())
{
Append(keyword);
Append(" ");
}
Append(pair.first);
Append(" ");
Append(pair.second);
AppendLine(";");
}
AppendLine();
}
}
void GlslWriter::EnterScope()
{
NazaraAssert(m_currentState, "This function should only be called while processing an AST");