Renderer/ShaderAst: Add Cast node
This commit is contained in:
parent
a84391cf08
commit
2a57af9896
|
|
@ -36,6 +36,7 @@ namespace Nz
|
||||||
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::BuiltinVariable& node) override;
|
||||||
|
void Write(const ShaderAst::Cast& 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::NamedVariable& node) override;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include <Nazara/Math/Vector4.hpp>
|
#include <Nazara/Math/Vector4.hpp>
|
||||||
#include <Nazara/Renderer/Config.hpp>
|
#include <Nazara/Renderer/Config.hpp>
|
||||||
#include <Nazara/Renderer/Enums.hpp>
|
#include <Nazara/Renderer/Enums.hpp>
|
||||||
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
@ -76,6 +77,9 @@ namespace Nz
|
||||||
|
|
||||||
virtual void Register(ShaderWriter& visitor) = 0;
|
virtual void Register(ShaderWriter& visitor) = 0;
|
||||||
virtual void Visit(ShaderWriter& visitor) = 0;
|
virtual void Visit(ShaderWriter& visitor) = 0;
|
||||||
|
|
||||||
|
static inline unsigned int GetComponentCount(ExpressionType type);
|
||||||
|
static inline ExpressionType GetComponentType(ExpressionType type);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Statement;
|
class Statement;
|
||||||
|
|
@ -209,6 +213,19 @@ namespace Nz
|
||||||
StatementPtr elseStatement;
|
StatementPtr elseStatement;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NAZARA_RENDERER_API Cast : public Expression
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline Cast(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second = nullptr, ExpressionPtr third = nullptr, ExpressionPtr fourth = nullptr);
|
||||||
|
|
||||||
|
ExpressionType GetExpressionType() const override;
|
||||||
|
void Register(ShaderWriter& visitor) override;
|
||||||
|
void Visit(ShaderWriter& visitor) override;
|
||||||
|
|
||||||
|
ExpressionType exprType;
|
||||||
|
std::array<ExpressionPtr, 4> expressions;
|
||||||
|
};
|
||||||
|
|
||||||
class NAZARA_RENDERER_API Constant : public Expression
|
class NAZARA_RENDERER_API Constant : public Expression
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,38 @@ namespace Nz
|
||||||
{
|
{
|
||||||
namespace ShaderAst
|
namespace ShaderAst
|
||||||
{
|
{
|
||||||
|
inline unsigned int Node::GetComponentCount(ExpressionType type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ExpressionType::Float2:
|
||||||
|
return 2;
|
||||||
|
|
||||||
|
case ExpressionType::Float3:
|
||||||
|
return 3;
|
||||||
|
|
||||||
|
case ExpressionType::Float4:
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ExpressionType Node::GetComponentType(ExpressionType type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ExpressionType::Float2:
|
||||||
|
case ExpressionType::Float3:
|
||||||
|
case ExpressionType::Float4:
|
||||||
|
return ExpressionType::Float1;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline ExpressionStatement::ExpressionStatement(ExpressionPtr expr) :
|
inline ExpressionStatement::ExpressionStatement(ExpressionPtr expr) :
|
||||||
expression(std::move(expr))
|
expression(std::move(expr))
|
||||||
{
|
{
|
||||||
|
|
@ -62,6 +94,25 @@ namespace Nz
|
||||||
elseStatement = std::move(falseStatement);
|
elseStatement = std::move(falseStatement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Cast::Cast(ExpressionType castTo, ExpressionPtr first, ExpressionPtr second, ExpressionPtr third, ExpressionPtr fourth) :
|
||||||
|
exprType(castTo),
|
||||||
|
expressions({first, second, third, fourth})
|
||||||
|
{
|
||||||
|
unsigned int componentCount = 0;
|
||||||
|
unsigned int requiredComponents = GetComponentCount(exprType);
|
||||||
|
for (const auto& exprPtr : expressions)
|
||||||
|
{
|
||||||
|
if (!exprPtr)
|
||||||
|
break;
|
||||||
|
|
||||||
|
componentCount += GetComponentCount(exprPtr->GetExpressionType());
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: AstParseError
|
||||||
|
if (componentCount != requiredComponents)
|
||||||
|
throw std::runtime_error("Component count doesn't match required component count");
|
||||||
|
}
|
||||||
|
|
||||||
inline Constant::Constant(float value) :
|
inline Constant::Constant(float value) :
|
||||||
exprType(ExpressionType::Float1)
|
exprType(ExpressionType::Float1)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,8 @@ namespace Nz { namespace ShaderBuilder
|
||||||
constexpr BinOpBuilder<ShaderAst::BinaryType::Substract> Substract;
|
constexpr BinOpBuilder<ShaderAst::BinaryType::Substract> Substract;
|
||||||
constexpr VarBuilder<ShaderAst::VariableType::Uniform> Uniform;
|
constexpr VarBuilder<ShaderAst::VariableType::Uniform> Uniform;
|
||||||
constexpr VarBuilder<ShaderAst::VariableType::Variable> Variable;
|
constexpr VarBuilder<ShaderAst::VariableType::Variable> Variable;
|
||||||
|
|
||||||
|
template<ShaderAst::ExpressionType Type, typename... Args> std::shared_ptr<ShaderAst::Cast> Cast(Args&&... args);
|
||||||
} }
|
} }
|
||||||
|
|
||||||
#include <Nazara/Renderer/ShaderBuilder.inl>
|
#include <Nazara/Renderer/ShaderBuilder.inl>
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,12 @@ namespace Nz { namespace ShaderBuilder
|
||||||
{
|
{
|
||||||
return std::make_shared<ShaderAst::NamedVariable>(type, std::forward<Args>(args)...);
|
return std::make_shared<ShaderAst::NamedVariable>(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)...);
|
||||||
|
}
|
||||||
} }
|
} }
|
||||||
|
|
||||||
#include <Nazara/Renderer/DebugOff.hpp>
|
#include <Nazara/Renderer/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ namespace Nz
|
||||||
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::BuiltinVariable& node) = 0;
|
||||||
|
virtual void Write(const ShaderAst::Cast& 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::NamedVariable& node) = 0;
|
||||||
|
|
|
||||||
|
|
@ -198,6 +198,28 @@ namespace Nz
|
||||||
Append(node.var);
|
Append(node.var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlslWriter::Write(const ShaderAst::Cast& node)
|
||||||
|
{
|
||||||
|
Append(node.exprType);
|
||||||
|
Append("(");
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
unsigned int requiredComponents = ShaderAst::Node::GetComponentCount(node.exprType);
|
||||||
|
while (requiredComponents > 0)
|
||||||
|
{
|
||||||
|
if (i != 0)
|
||||||
|
m_currentState->stream << ", ";
|
||||||
|
|
||||||
|
const auto& exprPtr = node.expressions[i++];
|
||||||
|
NazaraAssert(exprPtr, "Invalid expression");
|
||||||
|
|
||||||
|
Write(exprPtr);
|
||||||
|
requiredComponents -= ShaderAst::Node::GetComponentCount(exprPtr->GetExpressionType());
|
||||||
|
}
|
||||||
|
|
||||||
|
Append(")");
|
||||||
|
}
|
||||||
|
|
||||||
void GlslWriter::Write(const ShaderAst::Constant& node)
|
void GlslWriter::Write(const ShaderAst::Constant& node)
|
||||||
{
|
{
|
||||||
switch (node.exprType)
|
switch (node.exprType)
|
||||||
|
|
|
||||||
|
|
@ -139,5 +139,29 @@ namespace Nz { namespace ShaderAst
|
||||||
{
|
{
|
||||||
visitor.Write(*this);
|
visitor.Write(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExpressionType Cast::GetExpressionType() const
|
||||||
|
{
|
||||||
|
return exprType;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cast::Register(ShaderWriter& visitor)
|
||||||
|
{
|
||||||
|
auto it = expressions.begin();
|
||||||
|
(*it)->Register(visitor);
|
||||||
|
|
||||||
|
for (; it != expressions.end(); ++it)
|
||||||
|
{
|
||||||
|
if (!*it)
|
||||||
|
break;
|
||||||
|
|
||||||
|
(*it)->Register(visitor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cast::Visit(ShaderWriter& visitor)
|
||||||
|
{
|
||||||
|
visitor.Write(*this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue