Renderer/ShaderAst: Add NamedVariable and BuiltinVariable classes
This commit is contained in:
parent
43e23fea47
commit
e82fb7fef4
|
|
@ -27,7 +27,7 @@ namespace Nz
|
||||||
|
|
||||||
Nz::String Generate(const ShaderAst::StatementPtr& node) override;
|
Nz::String Generate(const ShaderAst::StatementPtr& node) override;
|
||||||
|
|
||||||
void RegisterFunction(const String& name, ShaderAst::StatementPtr statement, std::initializer_list<ShaderAst::VariablePtr> parameters, ShaderAst::ExpressionType ret) override;
|
void RegisterFunction(const String& name, ShaderAst::StatementPtr statement, std::initializer_list<ShaderAst::NamedVariablePtr> parameters, ShaderAst::ExpressionType ret) override;
|
||||||
void RegisterVariable(ShaderAst::VariableType kind, const String& name, ShaderAst::ExpressionType type) override;
|
void RegisterVariable(ShaderAst::VariableType kind, const String& name, ShaderAst::ExpressionType type) override;
|
||||||
|
|
||||||
void SetGlslVersion(unsigned int version);
|
void SetGlslVersion(unsigned int version);
|
||||||
|
|
@ -35,15 +35,17 @@ namespace Nz
|
||||||
void Write(const ShaderAst::AssignOp& node) override;
|
void Write(const ShaderAst::AssignOp& node) override;
|
||||||
void Write(const ShaderAst::Branch& node) override;
|
void Write(const ShaderAst::Branch& node) override;
|
||||||
void Write(const ShaderAst::BinaryOp& node) override;
|
void Write(const ShaderAst::BinaryOp& node) override;
|
||||||
|
void Write(const ShaderAst::BuiltinVariable& node) override;
|
||||||
void Write(const ShaderAst::Constant& node) override;
|
void Write(const ShaderAst::Constant& node) override;
|
||||||
void Write(const ShaderAst::ExpressionStatement& node) override;
|
void Write(const ShaderAst::ExpressionStatement& node) override;
|
||||||
|
void Write(const ShaderAst::NamedVariable& node) override;
|
||||||
void Write(const ShaderAst::NodePtr& node) override;
|
void Write(const ShaderAst::NodePtr& node) override;
|
||||||
void Write(const ShaderAst::StatementBlock& node) override;
|
void Write(const ShaderAst::StatementBlock& node) override;
|
||||||
void Write(const ShaderAst::Variable& node) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Function;
|
struct Function;
|
||||||
|
|
||||||
|
void Append(ShaderAst::Builtin builtin);
|
||||||
void Append(ShaderAst::ExpressionType type);
|
void Append(ShaderAst::ExpressionType type);
|
||||||
void Append(const String& txt);
|
void Append(const String& txt);
|
||||||
void AppendCommentSection(const String& section);
|
void AppendCommentSection(const String& section);
|
||||||
|
|
@ -56,7 +58,7 @@ namespace Nz
|
||||||
struct Function
|
struct Function
|
||||||
{
|
{
|
||||||
std::set<std::pair<ShaderAst::ExpressionType, String>> variables;
|
std::set<std::pair<ShaderAst::ExpressionType, String>> variables;
|
||||||
std::vector<ShaderAst::VariablePtr> parameters;
|
std::vector<ShaderAst::NamedVariablePtr> parameters;
|
||||||
ShaderAst::ExpressionType retType;
|
ShaderAst::ExpressionType retType;
|
||||||
ShaderAst::StatementPtr node;
|
ShaderAst::StatementPtr node;
|
||||||
String name;
|
String name;
|
||||||
|
|
|
||||||
|
|
@ -36,18 +36,25 @@ namespace Nz
|
||||||
Equality //< ==
|
Equality //< ==
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class Builtin
|
||||||
|
{
|
||||||
|
VertexPosition, // gl_Position
|
||||||
|
};
|
||||||
|
|
||||||
enum class ExpressionType
|
enum class ExpressionType
|
||||||
{
|
{
|
||||||
Float1, // float
|
Float1, // float
|
||||||
Float2, // vec2
|
Float2, // vec2
|
||||||
Float3, // vec3
|
Float3, // vec3
|
||||||
Float4, // vec4
|
Float4, // vec4
|
||||||
|
Mat4x4, // mat4
|
||||||
|
|
||||||
None // void
|
None // void
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class VariableType
|
enum class VariableType
|
||||||
{
|
{
|
||||||
|
Builtin,
|
||||||
Parameter,
|
Parameter,
|
||||||
Variable,
|
Variable,
|
||||||
Uniform
|
Uniform
|
||||||
|
|
@ -115,14 +122,36 @@ namespace Nz
|
||||||
class NAZARA_RENDERER_API Variable : public Expression
|
class NAZARA_RENDERER_API Variable : public Expression
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline Variable(VariableType varKind, const Nz::String& varName, ExpressionType varType);
|
inline Variable(VariableType varKind, ExpressionType varType);
|
||||||
|
|
||||||
|
ExpressionType type;
|
||||||
|
VariableType kind;
|
||||||
|
};
|
||||||
|
|
||||||
|
class NamedVariable;
|
||||||
|
|
||||||
|
using NamedVariablePtr = std::shared_ptr<NamedVariable>;
|
||||||
|
|
||||||
|
class NAZARA_RENDERER_API NamedVariable : public Variable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline NamedVariable(VariableType varKind, const Nz::String& varName, ExpressionType varType);
|
||||||
|
|
||||||
void Register(ShaderWriter& visitor) override;
|
void Register(ShaderWriter& visitor) override;
|
||||||
void Visit(ShaderWriter& visitor) override;
|
void Visit(ShaderWriter& visitor) override;
|
||||||
|
|
||||||
ExpressionType type;
|
Nz::String name;
|
||||||
VariableType kind;
|
};
|
||||||
Nz::String name;
|
|
||||||
|
class NAZARA_RENDERER_API BuiltinVariable : public Variable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline BuiltinVariable(Builtin variable, ExpressionType varType);
|
||||||
|
|
||||||
|
void Register(ShaderWriter& visitor) override;
|
||||||
|
void Visit(ShaderWriter& visitor) override;
|
||||||
|
|
||||||
|
Builtin var;
|
||||||
};
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,24 @@ namespace Nz
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Variable::Variable(VariableType varKind, const Nz::String& varName, ExpressionType varType) :
|
inline Variable::Variable(VariableType varKind, ExpressionType varType) :
|
||||||
kind(varKind),
|
kind(varKind),
|
||||||
name(varName),
|
|
||||||
type(varType)
|
type(varType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline NamedVariable::NamedVariable(VariableType varKind, const Nz::String& varName, ExpressionType varType) :
|
||||||
|
Variable(varKind, varType),
|
||||||
|
name(varName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline BuiltinVariable::BuiltinVariable(Builtin variable, ExpressionType varType) :
|
||||||
|
Variable(VariableType::Builtin, varType),
|
||||||
|
var(variable)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
inline AssignOp::AssignOp(AssignType Op, VariablePtr Var, ExpressionPtr Right) :
|
inline AssignOp::AssignOp(AssignType Op, VariablePtr Var, ExpressionPtr Right) :
|
||||||
op(Op),
|
op(Op),
|
||||||
variable(std::move(Var)),
|
variable(std::move(Var)),
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ namespace Nz { namespace ShaderBuilder
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
std::shared_ptr<Nz::ShaderAst::Variable> operator()(Args&&... args) const
|
std::shared_ptr<Nz::ShaderAst::Variable> operator()(Args&&... args) const
|
||||||
{
|
{
|
||||||
return std::make_shared<Nz::ShaderAst::Variable>(type, std::forward<Args>(args)...);
|
return std::make_shared<Nz::ShaderAst::NamedVariable>(type, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,17 +23,18 @@ namespace Nz
|
||||||
|
|
||||||
virtual Nz::String Generate(const ShaderAst::StatementPtr& node) = 0;
|
virtual Nz::String Generate(const ShaderAst::StatementPtr& node) = 0;
|
||||||
|
|
||||||
virtual void RegisterFunction(const String& name, ShaderAst::StatementPtr node, std::initializer_list<ShaderAst::VariablePtr> parameters, ShaderAst::ExpressionType ret) = 0;
|
virtual void RegisterFunction(const String& name, ShaderAst::StatementPtr node, std::initializer_list<ShaderAst::NamedVariablePtr> parameters, ShaderAst::ExpressionType ret) = 0;
|
||||||
virtual void RegisterVariable(ShaderAst::VariableType kind, const String& name, ShaderAst::ExpressionType type) = 0;
|
virtual void RegisterVariable(ShaderAst::VariableType kind, const String& name, ShaderAst::ExpressionType type) = 0;
|
||||||
|
|
||||||
virtual void Write(const ShaderAst::AssignOp& node) = 0;
|
virtual void Write(const ShaderAst::AssignOp& node) = 0;
|
||||||
virtual void Write(const ShaderAst::Branch& node) = 0;
|
virtual void Write(const ShaderAst::Branch& node) = 0;
|
||||||
virtual void Write(const ShaderAst::BinaryOp& node) = 0;
|
virtual void Write(const ShaderAst::BinaryOp& node) = 0;
|
||||||
|
virtual void Write(const ShaderAst::BuiltinVariable& node) = 0;
|
||||||
virtual void Write(const ShaderAst::Constant& node) = 0;
|
virtual void Write(const ShaderAst::Constant& node) = 0;
|
||||||
virtual void Write(const ShaderAst::ExpressionStatement& node) = 0;
|
virtual void Write(const ShaderAst::ExpressionStatement& node) = 0;
|
||||||
|
virtual void Write(const ShaderAst::NamedVariable& node) = 0;
|
||||||
virtual void Write(const ShaderAst::NodePtr& node) = 0;
|
virtual void Write(const ShaderAst::NodePtr& node) = 0;
|
||||||
virtual void Write(const ShaderAst::StatementBlock& node) = 0;
|
virtual void Write(const ShaderAst::StatementBlock& node) = 0;
|
||||||
virtual void Write(const ShaderAst::Variable& node) = 0;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ namespace Nz
|
||||||
return state.stream;
|
return state.stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::RegisterFunction(const String& name, ShaderAst::StatementPtr statement, std::initializer_list<ShaderAst::VariablePtr> parameters, ShaderAst::ExpressionType retType)
|
void GlslWriter::RegisterFunction(const String& name, ShaderAst::StatementPtr statement, std::initializer_list<ShaderAst::NamedVariablePtr> parameters, ShaderAst::ExpressionType retType)
|
||||||
{
|
{
|
||||||
Function func;
|
Function func;
|
||||||
func.retType = retType;
|
func.retType = retType;
|
||||||
|
|
@ -197,6 +197,11 @@ namespace Nz
|
||||||
Write(node.right);
|
Write(node.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlslWriter::Write(const ShaderAst::BuiltinVariable& node)
|
||||||
|
{
|
||||||
|
Append(node.var);
|
||||||
|
}
|
||||||
|
|
||||||
void GlslWriter::Write(const ShaderAst::Constant& node)
|
void GlslWriter::Write(const ShaderAst::Constant& node)
|
||||||
{
|
{
|
||||||
switch (node.exprType)
|
switch (node.exprType)
|
||||||
|
|
@ -213,6 +218,11 @@ namespace Nz
|
||||||
Append(";");
|
Append(";");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlslWriter::Write(const ShaderAst::NamedVariable& node)
|
||||||
|
{
|
||||||
|
Append(node.name);
|
||||||
|
}
|
||||||
|
|
||||||
void GlslWriter::Write(const ShaderAst::StatementBlock& node)
|
void GlslWriter::Write(const ShaderAst::StatementBlock& node)
|
||||||
{
|
{
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
@ -227,9 +237,14 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Write(const ShaderAst::Variable& node)
|
void GlslWriter::Append(ShaderAst::Builtin builtin)
|
||||||
{
|
{
|
||||||
Append(node.name);
|
switch (builtin)
|
||||||
|
{
|
||||||
|
case ShaderAst::Builtin::VertexPosition:
|
||||||
|
Append("gl_Position");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Append(ShaderAst::ExpressionType type)
|
void GlslWriter::Append(ShaderAst::ExpressionType type)
|
||||||
|
|
@ -248,6 +263,9 @@ namespace Nz
|
||||||
case ShaderAst::ExpressionType::Float4:
|
case ShaderAst::ExpressionType::Float4:
|
||||||
Append("vec4");
|
Append("vec4");
|
||||||
break;
|
break;
|
||||||
|
case ShaderAst::ExpressionType::Mat4x4:
|
||||||
|
Append("mat4");
|
||||||
|
break;
|
||||||
case ShaderAst::ExpressionType::None:
|
case ShaderAst::ExpressionType::None:
|
||||||
Append("void");
|
Append("void");
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,22 @@ namespace Nz { namespace ShaderAst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Variable::Register(ShaderWriter& visitor)
|
void NamedVariable::Register(ShaderWriter& visitor)
|
||||||
{
|
{
|
||||||
visitor.RegisterVariable(kind, name, type);
|
visitor.RegisterVariable(kind, name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Variable::Visit(ShaderWriter& visitor)
|
void NamedVariable::Visit(ShaderWriter& visitor)
|
||||||
|
{
|
||||||
|
visitor.Write(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BuiltinVariable::Register(ShaderWriter& visitor)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuiltinVariable::Visit(ShaderWriter& visitor)
|
||||||
{
|
{
|
||||||
visitor.Write(*this);
|
visitor.Write(*this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue