Renderer: Add shader serialization

This commit is contained in:
Lynix
2020-06-05 19:47:29 +02:00
parent de1c64253e
commit 8467c79021
15 changed files with 1131 additions and 443 deletions

View File

@@ -31,20 +31,20 @@ namespace Nz
void SetGlslVersion(unsigned int version);
void Write(const ShaderAst::AssignOp& node) override;
void Write(const ShaderAst::Branch& node) override;
void Write(const ShaderAst::BinaryFunc& node) override;
void Write(const ShaderAst::BinaryOp& node) override;
void Write(const ShaderAst::BuiltinVariable& node) override;
void Write(const ShaderAst::Cast& node) override;
void Write(const ShaderAst::Constant& node) override;
void Write(const ShaderAst::DeclareVariable& 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::Sample2D& node) override;
void Write(const ShaderAst::StatementBlock& node) override;
void Write(const ShaderAst::SwizzleOp& node) override;
using ShaderWriter::Visit;
void Visit(const ShaderAst::AssignOp& node) override;
void Visit(const ShaderAst::Branch& node) override;
void Visit(const ShaderAst::BinaryFunc& node) override;
void Visit(const ShaderAst::BinaryOp& node) override;
void Visit(const ShaderAst::BuiltinVariable& node) override;
void Visit(const ShaderAst::Cast& node) override;
void Visit(const ShaderAst::Constant& node) override;
void Visit(const ShaderAst::DeclareVariable& node) override;
void Visit(const ShaderAst::ExpressionStatement& node) override;
void Visit(const ShaderAst::NamedVariable& node) override;
void Visit(const ShaderAst::Sample2D& node) override;
void Visit(const ShaderAst::StatementBlock& node) override;
void Visit(const ShaderAst::SwizzleOp& node) override;
private:
struct Function;

View File

@@ -17,6 +17,8 @@
namespace Nz
{
class ByteStream;
class ShaderVisitor;
class ShaderWriter;
namespace ShaderAst
@@ -66,6 +68,26 @@ namespace Nz
Void // void
};
enum class NodeType
{
None = -1,
AssignOp,
BinaryFunc,
BinaryOp,
Branch,
BuiltinVariable,
Cast,
Constant,
ConditionalStatement,
DeclareVariable,
ExpressionStatement,
NamedVariable,
Sample2D,
SwizzleOp,
StatementBlock
};
enum class SwizzleComponent
{
First,
@@ -93,13 +115,21 @@ namespace Nz
class NAZARA_RENDERER_API Node
{
public:
virtual ~Node() = default;
virtual ~Node();
inline NodeType GetType() const;
virtual void Register(ShaderWriter& visitor) = 0;
virtual void Visit(ShaderWriter& visitor) = 0;
virtual void Visit(ShaderVisitor& visitor) = 0;
static inline unsigned int GetComponentCount(ExpressionType type);
static inline ExpressionType GetComponentType(ExpressionType type);
protected:
inline Node(NodeType type);
private:
NodeType m_type;
};
class Statement;
@@ -108,6 +138,8 @@ namespace Nz
class NAZARA_RENDERER_API Statement : public Node
{
public:
using Node::Node;
};
class Expression;
@@ -117,236 +149,250 @@ namespace Nz
class NAZARA_RENDERER_API Expression : public Node
{
public:
using Node::Node;
virtual ExpressionCategory GetExpressionCategory() const;
virtual ExpressionType GetExpressionType() const = 0;
};
class NAZARA_RENDERER_API ExpressionStatement : public Statement
struct NAZARA_RENDERER_API ExpressionStatement : public Statement
{
public:
inline explicit ExpressionStatement(ExpressionPtr expr);
inline ExpressionStatement();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
ExpressionPtr expression;
ExpressionPtr expression;
static inline std::shared_ptr<ExpressionStatement> Build(ExpressionPtr expr);
};
//////////////////////////////////////////////////////////////////////////
class NAZARA_RENDERER_API ConditionalStatement : public Statement
struct NAZARA_RENDERER_API ConditionalStatement : public Statement
{
public:
inline ConditionalStatement(const String& condition, StatementPtr statementPtr);
inline ConditionalStatement();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
String conditionName;
StatementPtr statement;
std::string conditionName;
StatementPtr statement;
static inline std::shared_ptr<ConditionalStatement> Build(std::string condition, StatementPtr statementPtr);
};
class NAZARA_RENDERER_API StatementBlock : public Statement
struct NAZARA_RENDERER_API StatementBlock : public Statement
{
public:
template<typename... Args> explicit StatementBlock(Args&&... args);
inline StatementBlock();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
std::vector<StatementPtr> statements;
std::vector<StatementPtr> statements;
template<typename... Args> static std::shared_ptr<StatementBlock> Build(Args&&... args);
};
class Variable;
struct Variable;
using VariablePtr = std::shared_ptr<Variable>;
class NAZARA_RENDERER_API Variable : public Expression
struct NAZARA_RENDERER_API Variable : public Expression
{
public:
inline Variable(VariableType varKind, ExpressionType varType);
using Expression::Expression;
ExpressionCategory GetExpressionCategory() const override;
ExpressionType GetExpressionType() const override;
ExpressionCategory GetExpressionCategory() const override;
ExpressionType GetExpressionType() const override;
ExpressionType type;
VariableType kind;
ExpressionType type;
VariableType kind;
};
class NAZARA_RENDERER_API BuiltinVariable : public Variable
struct NAZARA_RENDERER_API BuiltinVariable : public Variable
{
public:
inline BuiltinVariable(BuiltinEntry variable, ExpressionType varType);
inline BuiltinVariable();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
BuiltinEntry var;
BuiltinEntry var;
static inline std::shared_ptr<BuiltinVariable> Build(BuiltinEntry variable, ExpressionType varType);
};
class NamedVariable;
struct NamedVariable;
using NamedVariablePtr = std::shared_ptr<NamedVariable>;
class NAZARA_RENDERER_API NamedVariable : public Variable
struct NAZARA_RENDERER_API NamedVariable : public Variable
{
public:
inline NamedVariable(VariableType varKind, const Nz::String& varName, ExpressionType varType);
inline NamedVariable();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
Nz::String name;
std::string name;
static inline std::shared_ptr<NamedVariable> Build(VariableType varType, std::string varName, ExpressionType expressionType);
};
class NAZARA_RENDERER_API DeclareVariable : public Statement
struct NAZARA_RENDERER_API DeclareVariable : public Statement
{
public:
inline DeclareVariable(NamedVariablePtr Variable, ExpressionPtr Expression = nullptr);
inline DeclareVariable();
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
NamedVariablePtr variable;
ExpressionPtr expression;
NamedVariablePtr variable;
ExpressionPtr expression;
static inline std::shared_ptr<DeclareVariable> Build(NamedVariablePtr variable, ExpressionPtr expression = nullptr);
};
//////////////////////////////////////////////////////////////////////////
class NAZARA_RENDERER_API AssignOp : public Expression
struct NAZARA_RENDERER_API AssignOp : public Expression
{
public:
inline AssignOp(AssignType Op, ExpressionPtr Left, ExpressionPtr Right);
inline AssignOp();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
AssignType op;
ExpressionPtr left;
ExpressionPtr right;
AssignType op;
ExpressionPtr left;
ExpressionPtr right;
static inline std::shared_ptr<AssignOp> Build(AssignType op, ExpressionPtr left, ExpressionPtr right);
};
class NAZARA_RENDERER_API BinaryOp : public Expression
struct NAZARA_RENDERER_API BinaryOp : public Expression
{
public:
inline BinaryOp(BinaryType Op, ExpressionPtr Left, ExpressionPtr Right);
inline BinaryOp();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
BinaryType op;
ExpressionPtr left;
ExpressionPtr right;
BinaryType op;
ExpressionPtr left;
ExpressionPtr right;
static inline std::shared_ptr<BinaryOp> Build(BinaryType op, ExpressionPtr left, ExpressionPtr right);
};
class NAZARA_RENDERER_API Branch : public Statement
struct NAZARA_RENDERER_API Branch : public Statement
{
public:
inline Branch(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement = nullptr);
struct ConditionalStatement;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
inline Branch();
struct ConditionalStatement
{
ExpressionPtr condition;
StatementPtr statement;
};
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
std::vector<ConditionalStatement> condStatements;
StatementPtr elseStatement;
std::vector<ConditionalStatement> condStatements;
StatementPtr elseStatement;
struct ConditionalStatement
{
ExpressionPtr condition;
StatementPtr statement;
};
inline std::shared_ptr<Branch> Build(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement = nullptr);
};
class NAZARA_RENDERER_API Cast : public Expression
struct NAZARA_RENDERER_API Cast : public Expression
{
public:
inline Cast(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second = nullptr, ExpressionPtr third = nullptr, ExpressionPtr fourth = nullptr);
inline Cast(ExpressionType castTo, ExpressionPtr* expressions, std::size_t expressionCount);
inline Cast();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
ExpressionType exprType;
std::array<ExpressionPtr, 4> expressions;
ExpressionType exprType;
std::array<ExpressionPtr, 4> expressions;
private:
void Validate() const;
static inline std::shared_ptr<Cast> Build(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second = nullptr, ExpressionPtr third = nullptr, ExpressionPtr fourth = nullptr);
static inline std::shared_ptr<Cast> Build(ExpressionType castTo, ExpressionPtr* expressions, std::size_t expressionCount);
};
class NAZARA_RENDERER_API Constant : public Expression
struct NAZARA_RENDERER_API Constant : public Expression
{
public:
inline explicit Constant(bool value);
inline explicit Constant(float value);
inline explicit Constant(const Vector2f& value);
inline explicit Constant(const Vector3f& value);
inline explicit Constant(const Vector4f& value);
inline Constant();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
ExpressionType exprType;
ExpressionType exprType;
union
{
bool bool1;
float vec1;
Vector2f vec2;
Vector3f vec3;
Vector4f vec4;
} values;
union
{
bool bool1;
float vec1;
Vector2f vec2;
Vector3f vec3;
Vector4f vec4;
} values;
static inline std::shared_ptr<Constant> Build(bool value);
static inline std::shared_ptr<Constant> Build(float value);
static inline std::shared_ptr<Constant> Build(const Vector2f& value);
static inline std::shared_ptr<Constant> Build(const Vector3f& value);
static inline std::shared_ptr<Constant> Build(const Vector4f& value);
};
class NAZARA_RENDERER_API SwizzleOp : public Expression
struct NAZARA_RENDERER_API SwizzleOp : public Expression
{
public:
inline SwizzleOp(ExpressionPtr expressionPtr, std::initializer_list<SwizzleComponent> swizzleComponents);
inline SwizzleOp();
ExpressionCategory GetExpressionCategory() const override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionCategory GetExpressionCategory() const override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
std::array<SwizzleComponent, 4> components;
std::size_t componentCount;
ExpressionPtr expression;
std::array<SwizzleComponent, 4> components;
std::size_t componentCount;
ExpressionPtr expression;
static inline std::shared_ptr<SwizzleOp> Build(ExpressionPtr expressionPtr, std::initializer_list<SwizzleComponent> swizzleComponents);
};
//////////////////////////////////////////////////////////////////////////
class NAZARA_RENDERER_API Sample2D : public Expression
struct NAZARA_RENDERER_API Sample2D : public Expression
{
public:
inline Sample2D(ExpressionPtr samplerPtr, ExpressionPtr coordinatesPtr);
inline Sample2D();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
ExpressionPtr sampler;
ExpressionPtr coordinates;
ExpressionPtr sampler;
ExpressionPtr coordinates;
static inline std::shared_ptr<Sample2D> Build(ExpressionPtr samplerPtr, ExpressionPtr coordinatesPtr);
};
//////////////////////////////////////////////////////////////////////////
class NAZARA_RENDERER_API BinaryFunc : public Expression
struct NAZARA_RENDERER_API BinaryFunc : public Expression
{
public:
inline BinaryFunc(BinaryIntrinsic Op, ExpressionPtr Left, ExpressionPtr Right);
inline BinaryFunc();
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderWriter& visitor) override;
ExpressionType GetExpressionType() const override;
void Register(ShaderWriter& visitor) override;
void Visit(ShaderVisitor& visitor) override;
BinaryIntrinsic intrinsic;
ExpressionPtr left;
ExpressionPtr right;
BinaryIntrinsic intrinsic;
ExpressionPtr left;
ExpressionPtr right;
static inline std::shared_ptr<BinaryFunc> Build(BinaryIntrinsic intrinsic, ExpressionPtr left, ExpressionPtr right);
};
}
}

View File

@@ -9,6 +9,16 @@ namespace Nz
{
namespace ShaderAst
{
inline Node::Node(NodeType type) :
m_type(type)
{
}
inline NodeType ShaderAst::Node::GetType() const
{
return m_type;
}
inline unsigned int Node::GetComponentCount(ExpressionType type)
{
switch (type)
@@ -47,224 +57,264 @@ namespace Nz
}
}
inline ExpressionStatement::ExpressionStatement(ExpressionPtr expr) :
expression(std::move(expr))
inline ExpressionStatement::ExpressionStatement() :
Statement(NodeType::ExpressionStatement)
{
}
inline std::shared_ptr<ExpressionStatement> ExpressionStatement::Build(ExpressionPtr expr)
{
auto node = std::make_shared<ExpressionStatement>();
node->expression = std::move(expr);
return node;
}
inline ConditionalStatement::ConditionalStatement() :
Statement(NodeType::ConditionalStatement)
{
}
inline ConditionalStatement::ConditionalStatement(const String& condition, StatementPtr statementPtr) :
conditionName(condition),
statement(std::move(statementPtr))
inline std::shared_ptr<ConditionalStatement> ConditionalStatement::Build(std::string condition, StatementPtr statementPtr)
{
auto node = std::make_shared<ConditionalStatement>();
node->conditionName = std::move(condition);
node->statement = std::move(statementPtr);
return node;
}
inline StatementBlock::StatementBlock() :
Statement(NodeType::StatementBlock)
{
}
template<typename... Args>
StatementBlock::StatementBlock(Args&& ...args) :
statements({std::forward<Args>(args)...})
std::shared_ptr<StatementBlock> StatementBlock::Build(Args&&... args)
{
auto node = std::make_shared<StatementBlock>();
node->statements = std::vector<StatementPtr>({ std::forward<Args>(args)... });
return node;
}
inline BuiltinVariable::BuiltinVariable() :
Variable(NodeType::BuiltinVariable)
{
kind = VariableType::Builtin;
}
inline std::shared_ptr<BuiltinVariable> BuiltinVariable::Build(BuiltinEntry variable, ExpressionType varType)
{
auto node = std::make_shared<BuiltinVariable>();
node->type = varType;
node->var = variable;
return node;
}
inline NamedVariable::NamedVariable() :
Variable(NodeType::NamedVariable)
{
}
inline Variable::Variable(VariableType varKind, ExpressionType varType) :
type(varType),
kind(varKind)
inline std::shared_ptr<NamedVariable> NamedVariable::Build(VariableType type, std::string varName, ExpressionType expressionType)
{
auto node = std::make_shared<NamedVariable>();
node->kind = type;
node->name = std::move(varName);
node->type = expressionType;
return node;
}
inline DeclareVariable::DeclareVariable() :
Statement(NodeType::DeclareVariable)
{
}
inline BuiltinVariable::BuiltinVariable(BuiltinEntry variable, ExpressionType varType) :
Variable(VariableType::Builtin, varType),
var(variable)
inline std::shared_ptr<DeclareVariable> DeclareVariable::Build(NamedVariablePtr variable, ExpressionPtr expression)
{
auto node = std::make_shared<DeclareVariable>();
node->expression = std::move(expression);
node->variable = std::move(variable);
return node;
}
inline AssignOp::AssignOp() :
Expression(NodeType::AssignOp)
{
}
inline NamedVariable::NamedVariable(VariableType varKind, const Nz::String& varName, ExpressionType varType) :
Variable(varKind, varType),
name(varName)
inline std::shared_ptr<AssignOp> AssignOp::Build(AssignType op, ExpressionPtr left, ExpressionPtr right)
{
auto node = std::make_shared<AssignOp>();
node->op = op;
node->left = std::move(left);
node->right = std::move(right);
return node;
}
inline BinaryOp::BinaryOp() :
Expression(NodeType::BinaryOp)
{
}
inline DeclareVariable::DeclareVariable(NamedVariablePtr Variable, ExpressionPtr Expression) :
expression(std::move(Expression)),
variable(std::move(Variable))
inline std::shared_ptr<BinaryOp> BinaryOp::Build(BinaryType op, ExpressionPtr left, ExpressionPtr right)
{
auto node = std::make_shared<BinaryOp>();
node->op = op;
node->left = std::move(left);
node->right = std::move(right);
return node;
}
inline Branch::Branch() :
Statement(NodeType::Branch)
{
}
inline AssignOp::AssignOp(AssignType Op, ExpressionPtr Left, ExpressionPtr Right) :
op(Op),
left(std::move(Left)),
right(std::move(Right))
inline std::shared_ptr<Branch> Branch::Build(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement)
{
if (left->GetExpressionCategory() != ExpressionCategory::LValue)
//TODO: AstParseError
throw std::runtime_error("Assignation is only possible with lvalues");
auto node = std::make_shared<Branch>();
node->condStatements.emplace_back(ConditionalStatement{ std::move(condition), std::move(trueStatement) });
node->elseStatement = std::move(falseStatement);
return node;
}
inline BinaryOp::BinaryOp(BinaryType Op, ExpressionPtr Left, ExpressionPtr Right) :
op(Op),
left(std::move(Left)),
right(std::move(Right))
inline Cast::Cast() :
Expression(NodeType::Cast)
{
ExpressionType leftType = left->GetExpressionType();
ExpressionType rightType = right->GetExpressionType();
if (leftType != rightType)
{
switch (op)
{
case BinaryType::Add:
case BinaryType::Equality:
case BinaryType::Substract:
{
//TODO: AstParseError
throw std::runtime_error("Left expression type must match right expression type");
}
case BinaryType::Multiply:
case BinaryType::Divide:
{
switch (leftType)
{
case ExpressionType::Float2:
case ExpressionType::Float3:
case ExpressionType::Float4:
{
if (rightType != ExpressionType::Float1)
throw std::runtime_error("Left expression type is not compatible with right expression type");
break;
}
case ExpressionType::Mat4x4:
{
switch (rightType)
{
case ExpressionType::Float1:
case ExpressionType::Float4:
case ExpressionType::Mat4x4:
break;
//TODO: AstParseError
default:
throw std::runtime_error("Left expression type is not compatible with right expression type");
}
break;
}
default:
//TODO: AstParseError
throw std::runtime_error("Left expression type must match right expression type");
}
}
}
}
}
inline Branch::Branch(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement)
inline std::shared_ptr<Cast> Cast::Build(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second, ExpressionPtr third, ExpressionPtr fourth)
{
condStatements.emplace_back(ConditionalStatement{ std::move(condition), std::move(trueStatement) });
elseStatement = std::move(falseStatement);
auto node = std::make_shared<Cast>();
node->exprType = castTo;
node->expressions = { {first, second, third, fourth} };
return node;
}
inline Cast::Cast(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second, ExpressionPtr third, ExpressionPtr fourth) :
exprType(castTo),
expressions({ {first, second, third, fourth} })
{
Validate();
}
inline Cast::Cast(ExpressionType castTo, ExpressionPtr* Expressions, std::size_t expressionCount) :
exprType(castTo)
inline std::shared_ptr<Cast> Cast::Build(ExpressionType castTo, ExpressionPtr* Expressions, std::size_t expressionCount)
{
auto node = std::make_shared<Cast>();
node->exprType = castTo;
for (std::size_t i = 0; i < expressionCount; ++i)
expressions[i] = Expressions[i];
node->expressions[i] = Expressions[i];
Validate();
return node;
}
inline Constant::Constant(bool value) :
exprType(ExpressionType::Boolean)
inline Constant::Constant() :
Expression(NodeType::Constant)
{
values.bool1 = value;
}
inline Constant::Constant(float value) :
exprType(ExpressionType::Float1)
inline std::shared_ptr<Constant> Constant::Build(bool value)
{
values.vec1 = value;
auto node = std::make_shared<Constant>();
node->exprType = ExpressionType::Boolean;
node->values.bool1 = value;
return node;
}
inline Constant::Constant(const Vector2f& value) :
exprType(ExpressionType::Float2)
inline std::shared_ptr<Constant> Constant::Build(float value)
{
values.vec2 = value;
auto node = std::make_shared<Constant>();
node->exprType = ExpressionType::Float1;
node->values.vec1 = value;
return node;
}
inline Constant::Constant(const Vector3f& value) :
exprType(ExpressionType::Float3)
inline std::shared_ptr<Constant> Constant::Build(const Vector2f& value)
{
values.vec3 = value;
auto node = std::make_shared<Constant>();
node->exprType = ExpressionType::Float2;
node->values.vec2 = value;
return node;
}
inline Constant::Constant(const Vector4f& value) :
exprType(ExpressionType::Float4)
inline std::shared_ptr<Constant> Constant::Build(const Vector3f& value)
{
values.vec4 = value;
auto node = std::make_shared<Constant>();
node->exprType = ExpressionType::Float3;
node->values.vec3 = value;
return node;
}
inline SwizzleOp::SwizzleOp(ExpressionPtr expressionPtr, std::initializer_list<SwizzleComponent> swizzleComponents) :
componentCount(swizzleComponents.size()),
expression(expressionPtr)
inline std::shared_ptr<Constant> Constant::Build(const Vector4f& value)
{
if (componentCount > 4)
throw std::runtime_error("Cannot swizzle more than four elements");
auto node = std::make_shared<Constant>();
node->exprType = ExpressionType::Float4;
node->values.vec4 = value;
switch (expressionPtr->GetExpressionType())
{
case ExpressionType::Float1:
case ExpressionType::Float2:
case ExpressionType::Float3:
case ExpressionType::Float4:
break;
default:
throw std::runtime_error("Cannot swizzle this type");
}
std::copy(swizzleComponents.begin(), swizzleComponents.end(), components.begin());
return node;
}
inline Sample2D::Sample2D(ExpressionPtr samplerPtr, ExpressionPtr coordinatesPtr) :
sampler(std::move(samplerPtr)),
coordinates(std::move(coordinatesPtr))
{
if (sampler->GetExpressionType() != ExpressionType::Sampler2D)
throw std::runtime_error("Sampler must be a Sampler2D");
if (coordinates->GetExpressionType() != ExpressionType::Float2)
throw std::runtime_error("Coordinates must be a Float2");
inline SwizzleOp::SwizzleOp() :
Expression(NodeType::SwizzleOp)
{
}
inline BinaryFunc::BinaryFunc(BinaryIntrinsic Op, ExpressionPtr Left, ExpressionPtr Right) :
intrinsic(Op),
left(Left),
right(Right)
inline std::shared_ptr<SwizzleOp> SwizzleOp::Build(ExpressionPtr expressionPtr, std::initializer_list<SwizzleComponent> swizzleComponents)
{
ExpressionType leftType = left->GetExpressionType();
ExpressionType rightType = right->GetExpressionType();
auto node = std::make_shared<SwizzleOp>();
node->componentCount = swizzleComponents.size();
node->expression = std::move(expressionPtr);
if (leftType != rightType)
//TODO: AstParseError
throw std::runtime_error("Left expression type must match right expression type");
std::copy(swizzleComponents.begin(), swizzleComponents.end(), node->components.begin());
switch (intrinsic)
{
case BinaryIntrinsic::CrossProduct:
{
if (leftType != ExpressionType::Float3)
//TODO: AstParseError
throw std::runtime_error("CrossProduct only works with Float3 expressions");
}
}
return node;
}
inline Sample2D::Sample2D() :
Expression(NodeType::Sample2D)
{
}
inline std::shared_ptr<Sample2D> Sample2D::Build(ExpressionPtr samplerPtr, ExpressionPtr coordinatesPtr)
{
auto node = std::make_shared<Sample2D>();
node->coordinates = std::move(coordinatesPtr);
node->sampler = std::move(samplerPtr);
return node;
}
inline BinaryFunc::BinaryFunc() :
Expression(NodeType::BinaryFunc)
{
}
inline std::shared_ptr<BinaryFunc> BinaryFunc::Build(BinaryIntrinsic intrinsic, ExpressionPtr left, ExpressionPtr right)
{
auto node = std::make_shared<BinaryFunc>();
node->intrinsic = intrinsic;
node->left = std::move(left);
node->right = std::move(right);
return node;
}
}
}

View File

@@ -5,25 +5,25 @@
#include <Nazara/Renderer/ShaderBuilder.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz { namespace ShaderBuilder
namespace Nz::ShaderBuilder
{
template<typename T>
template<typename... Args>
std::shared_ptr<T> GenBuilder<T>::operator()(Args&&... args) const
{
return std::make_shared<T>(std::forward<Args>(args)...);
return T::Build(std::forward<Args>(args)...);
}
template<ShaderAst::AssignType op>
std::shared_ptr<ShaderAst::AssignOp> AssignOpBuilder<op>::operator()(const ShaderAst::ExpressionPtr& left, const ShaderAst::ExpressionPtr& right) const
{
return std::make_shared<ShaderAst::AssignOp>(op, left, right);
return ShaderAst::AssignOp::Build(op, left, right);
}
template<ShaderAst::BinaryType op>
std::shared_ptr<ShaderAst::BinaryOp> BinOpBuilder<op>::operator()(const ShaderAst::ExpressionPtr& left, const ShaderAst::ExpressionPtr& right) const
{
return std::make_shared<ShaderAst::BinaryOp>(op, left, right);
return ShaderAst::BinaryOp::Build(op, left, right);
}
inline std::shared_ptr<ShaderAst::Variable> BuiltinBuilder::operator()(ShaderAst::BuiltinEntry builtin) const
@@ -39,21 +39,21 @@ namespace Nz { namespace ShaderBuilder
NazaraAssert(exprType != ShaderAst::ExpressionType::Void, "Unhandled builtin");
return std::make_shared<ShaderAst::BuiltinVariable>(builtin, exprType);
return ShaderAst::BuiltinVariable::Build(builtin, exprType);
}
template<ShaderAst::VariableType type>
template<typename... Args>
ShaderAst::NamedVariablePtr VarBuilder<type>::operator()(Args&&... args) const
{
return std::make_shared<ShaderAst::NamedVariable>(type, std::forward<Args>(args)...);
return ShaderAst::NamedVariable::Build(type, std::forward<Args>(args)...);
}
template<ShaderAst::ExpressionType Type, typename... Args>
std::shared_ptr<ShaderAst::Cast> Cast(Args&&... args)
{
return std::make_shared<ShaderAst::Cast>(Type, std::forward<Args>(args)...);
return ShaderAst::Cast::Build(Type, std::forward<Args>(args)...);
}
} }
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@@ -0,0 +1,111 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADERSERIALIZER_HPP
#define NAZARA_SHADERSERIALIZER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/ByteStream.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/ShaderVisitor.hpp>
namespace Nz::ShaderAst
{
class NAZARA_RENDERER_API ShaderSerializerBase
{
public:
ShaderSerializerBase() = default;
ShaderSerializerBase(const ShaderSerializerBase&) = delete;
ShaderSerializerBase(ShaderSerializerBase&&) = delete;
~ShaderSerializerBase() = default;
void Serialize(AssignOp& node);
void Serialize(BinaryFunc& node);
void Serialize(BinaryOp& node);
void Serialize(Branch& node);
void Serialize(BuiltinVariable& node);
void Serialize(Cast& node);
void Serialize(Constant& node);
void Serialize(DeclareVariable& node);
void Serialize(ExpressionStatement& node);
void Serialize(NamedVariable& node);
void Serialize(Sample2D& node);
void Serialize(StatementBlock& node);
void Serialize(SwizzleOp& node);
protected:
template<typename T> void Container(T& container);
template<typename T> void Enum(T& enumVal);
virtual bool IsWriting() const = 0;
virtual void Node(NodePtr& node) = 0;
template<typename T> void Node(std::shared_ptr<T>& node);
virtual void Value(bool& val) = 0;
virtual void Value(float& val) = 0;
virtual void Value(std::string& val) = 0;
virtual void Value(Vector2f& val) = 0;
virtual void Value(Vector3f& val) = 0;
virtual void Value(Vector4f& val) = 0;
virtual void Value(UInt32& val) = 0;
inline void Value(std::size_t& val);
};
class NAZARA_RENDERER_API ShaderSerializer final : public ShaderSerializerBase
{
public:
inline ShaderSerializer(ByteArray& byteArray);
~ShaderSerializer() = default;
void Serialize(const StatementPtr& shader);
private:
bool IsWriting() const override;
void Node(NodePtr& node) override;
void Value(bool& val) override;
void Value(float& val) override;
void Value(std::string& val) override;
void Value(Vector2f& val) override;
void Value(Vector3f& val) override;
void Value(Vector4f& val) override;
void Value(UInt32& val) override;
ByteArray& m_byteArray;
ByteStream m_stream;
};
class NAZARA_RENDERER_API ShaderUnserializer final : public ShaderSerializerBase
{
public:
ShaderUnserializer(const ByteArray& byteArray);
~ShaderUnserializer() = default;
StatementPtr Unserialize();
private:
bool IsWriting() const override;
void Node(NodePtr & node) override;
void Value(bool& val) override;
void Value(float& val) override;
void Value(std::string& val) override;
void Value(Vector2f& val) override;
void Value(Vector3f& val) override;
void Value(Vector4f& val) override;
void Value(UInt32& val) override;
const ByteArray& m_byteArray;
ByteStream m_stream;
};
NAZARA_RENDERER_API ByteArray Serialize(const StatementPtr& shader);
NAZARA_RENDERER_API StatementPtr Unserialize(const ByteArray& data);
}
#include <Nazara/Renderer/ShaderSerializer.inl>
#endif

View File

@@ -0,0 +1,79 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/ShaderSerializer.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz::ShaderAst
{
template<typename T>
void ShaderSerializerBase::Container(T& container)
{
bool isWriting = IsWriting();
UInt32 size;
if (isWriting)
size = UInt32(container.size());
Value(size);
if (!isWriting)
container.resize(size);
}
template<typename T>
void ShaderSerializerBase::Enum(T& enumVal)
{
bool isWriting = IsWriting();
UInt32 value;
if (isWriting)
value = static_cast<UInt32>(enumVal);
Value(value);
if (!isWriting)
enumVal = static_cast<T>(value);
}
template<typename T>
inline void ShaderSerializerBase::Node(std::shared_ptr<T>& node)
{
bool isWriting = IsWriting();
NodePtr value;
if (isWriting)
value = node;
Node(value);
if (!isWriting)
node = std::static_pointer_cast<T>(value);
}
inline void ShaderSerializerBase::Value(std::size_t& val)
{
bool isWriting = IsWriting();
UInt32 value;
if (isWriting)
value = static_cast<UInt32>(val);
Value(value);
if (!isWriting)
val = static_cast<std::size_t>(value);
}
inline ShaderSerializer::ShaderSerializer(ByteArray& byteArray) :
m_byteArray(byteArray),
m_stream(&m_byteArray, OpenModeFlags(OpenMode_WriteOnly))
{
}
inline ShaderUnserializer::ShaderUnserializer(const ByteArray& byteArray) :
m_byteArray(byteArray),
m_stream(const_cast<ByteArray*>(&m_byteArray), OpenModeFlags(OpenMode_ReadOnly))
{
}
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADERVISITOR_HPP
#define NAZARA_SHADERVISITOR_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/ShaderAst.hpp>
#include <unordered_set>
namespace Nz
{
class NAZARA_RENDERER_API ShaderVisitor
{
public:
ShaderVisitor() = default;
ShaderVisitor(const ShaderVisitor&) = delete;
ShaderVisitor(ShaderVisitor&&) = delete;
virtual ~ShaderVisitor();
void EnableCondition(const String& name, bool cond);
bool IsConditionEnabled(const String& name) const;
virtual void Visit(const ShaderAst::AssignOp& node) = 0;
virtual void Visit(const ShaderAst::BinaryFunc& node) = 0;
virtual void Visit(const ShaderAst::BinaryOp& node) = 0;
virtual void Visit(const ShaderAst::Branch& node) = 0;
virtual void Visit(const ShaderAst::BuiltinVariable& node) = 0;
virtual void Visit(const ShaderAst::Cast& node) = 0;
virtual void Visit(const ShaderAst::Constant& node) = 0;
virtual void Visit(const ShaderAst::DeclareVariable& node) = 0;
virtual void Visit(const ShaderAst::ExpressionStatement& node) = 0;
virtual void Visit(const ShaderAst::NamedVariable& node) = 0;
void Visit(const ShaderAst::NodePtr& node);
virtual void Visit(const ShaderAst::Sample2D& node) = 0;
virtual void Visit(const ShaderAst::StatementBlock& node) = 0;
virtual void Visit(const ShaderAst::SwizzleOp& node) = 0;
private:
std::unordered_set<String> m_conditions;
};
}
#endif

View File

@@ -10,45 +10,22 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/ShaderAst.hpp>
#include <unordered_set>
#include <Nazara/Renderer/ShaderVisitor.hpp>
namespace Nz
{
class NAZARA_RENDERER_API ShaderWriter
class NAZARA_RENDERER_API ShaderWriter : public ShaderVisitor
{
public:
ShaderWriter() = default;
ShaderWriter(const ShaderWriter&) = delete;
ShaderWriter(ShaderWriter&&) = delete;
virtual ~ShaderWriter();
void EnableCondition(const String& name, bool cond);
bool IsConditionEnabled(const String& name) const;
~ShaderWriter() = default;
virtual Nz::String Generate(const ShaderAst::StatementPtr& node) = 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 Write(const ShaderAst::AssignOp& node) = 0;
virtual void Write(const ShaderAst::Branch& node) = 0;
virtual void Write(const ShaderAst::BinaryFunc& node) = 0;
virtual void Write(const ShaderAst::BinaryOp& node) = 0;
virtual void Write(const ShaderAst::BuiltinVariable& node) = 0;
virtual void Write(const ShaderAst::Cast& node) = 0;
virtual void Write(const ShaderAst::Constant& node) = 0;
virtual void Write(const ShaderAst::DeclareVariable& 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::Sample2D& node) = 0;
virtual void Write(const ShaderAst::StatementBlock& node) = 0;
virtual void Write(const ShaderAst::SwizzleOp& node) = 0;
private:
std::unordered_set<String> m_conditions;
};
}