Renderer/ShaderNodes: Add support for accessing struct fields
This commit is contained in:
@@ -61,6 +61,7 @@ namespace Nz
|
||||
|
||||
using ShaderVarVisitor::Visit;
|
||||
using ShaderVisitor::Visit;
|
||||
void Visit(const ShaderNodes::AccessMember& node) override;
|
||||
void Visit(const ShaderNodes::AssignOp& node) override;
|
||||
void Visit(const ShaderNodes::Branch& node) override;
|
||||
void Visit(const ShaderNodes::BinaryOp& node) override;
|
||||
@@ -85,6 +86,7 @@ namespace Nz
|
||||
|
||||
struct Context
|
||||
{
|
||||
const ShaderAst* shader = nullptr;
|
||||
const ShaderAst::Function* currentFunction = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace Nz::ShaderBuilder
|
||||
template<typename... Args> std::shared_ptr<T> operator()(Args&&... args) const;
|
||||
};
|
||||
|
||||
constexpr GenBuilder<ShaderNodes::AccessMember> AccessMember;
|
||||
constexpr BinOpBuilder<ShaderNodes::BinaryType::Add> Add;
|
||||
constexpr AssignOpBuilder<ShaderNodes::AssignType::Simple> Assign;
|
||||
constexpr BuiltinBuilder Builtin;
|
||||
|
||||
@@ -65,6 +65,7 @@ namespace Nz::ShaderNodes
|
||||
{
|
||||
None = -1,
|
||||
|
||||
AccessMember,
|
||||
AssignOp,
|
||||
BinaryOp,
|
||||
Branch,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#define NAZARA_SHADER_EXPRESSIONTYPE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/ShaderNodes.hpp>
|
||||
#include <Nazara/Renderer/ShaderEnums.hpp>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <Nazara/Math/Vector4.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/ShaderEnums.hpp>
|
||||
#include <Nazara/Renderer/ShaderExpressionType.hpp>
|
||||
#include <Nazara/Renderer/ShaderVariables.hpp>
|
||||
#include <array>
|
||||
#include <optional>
|
||||
@@ -59,7 +60,7 @@ namespace Nz
|
||||
inline Expression(NodeType type);
|
||||
|
||||
virtual ExpressionCategory GetExpressionCategory() const;
|
||||
virtual BasicType GetExpressionType() const = 0;
|
||||
virtual ShaderExpressionType GetExpressionType() const = 0;
|
||||
};
|
||||
|
||||
class Statement;
|
||||
@@ -125,7 +126,7 @@ namespace Nz
|
||||
inline Identifier();
|
||||
|
||||
ExpressionCategory GetExpressionCategory() const override;
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
VariablePtr var;
|
||||
@@ -133,13 +134,28 @@ namespace Nz
|
||||
static inline std::shared_ptr<Identifier> Build(VariablePtr variable);
|
||||
};
|
||||
|
||||
struct NAZARA_RENDERER_API AccessMember : public Expression
|
||||
{
|
||||
inline AccessMember();
|
||||
|
||||
ExpressionCategory GetExpressionCategory() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
std::size_t memberIndex;
|
||||
ExpressionPtr structExpr;
|
||||
ShaderExpressionType exprType; //< FIXME: Use ShaderAst to automate
|
||||
|
||||
static inline std::shared_ptr<AccessMember> Build(ExpressionPtr structExpr, std::size_t memberIndex, ShaderExpressionType exprType);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct NAZARA_RENDERER_API AssignOp : public Expression
|
||||
{
|
||||
inline AssignOp();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
AssignType op;
|
||||
@@ -153,7 +169,7 @@ namespace Nz
|
||||
{
|
||||
inline BinaryOp();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
BinaryType op;
|
||||
@@ -187,7 +203,7 @@ namespace Nz
|
||||
{
|
||||
inline Cast();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
BasicType exprType;
|
||||
@@ -201,7 +217,7 @@ namespace Nz
|
||||
{
|
||||
inline Constant();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
BasicType exprType;
|
||||
@@ -227,7 +243,7 @@ namespace Nz
|
||||
inline SwizzleOp();
|
||||
|
||||
ExpressionCategory GetExpressionCategory() const override;
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
std::array<SwizzleComponent, 4> components;
|
||||
@@ -243,7 +259,7 @@ namespace Nz
|
||||
{
|
||||
inline Sample2D();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
ExpressionPtr sampler;
|
||||
@@ -258,7 +274,7 @@ namespace Nz
|
||||
{
|
||||
inline IntrinsicCall();
|
||||
|
||||
BasicType GetExpressionType() const override;
|
||||
ShaderExpressionType GetExpressionType() const override;
|
||||
void Visit(ShaderVisitor& visitor) override;
|
||||
|
||||
IntrinsicType intrinsic;
|
||||
|
||||
@@ -146,6 +146,22 @@ namespace Nz::ShaderNodes
|
||||
}
|
||||
|
||||
|
||||
inline AccessMember::AccessMember() :
|
||||
Expression(NodeType::AccessMember)
|
||||
{
|
||||
}
|
||||
|
||||
inline std::shared_ptr<AccessMember> AccessMember::Build(ExpressionPtr structExpr, std::size_t memberIndex, ShaderExpressionType exprType)
|
||||
{
|
||||
auto node = std::make_shared<AccessMember>();
|
||||
node->exprType = std::move(exprType);
|
||||
node->memberIndex = memberIndex;
|
||||
node->structExpr = std::move(structExpr);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
inline AssignOp::AssignOp() :
|
||||
Expression(NodeType::AssignOp)
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace Nz
|
||||
ShaderSerializerBase(ShaderSerializerBase&&) = delete;
|
||||
~ShaderSerializerBase() = default;
|
||||
|
||||
void Serialize(ShaderNodes::AccessMember& node);
|
||||
void Serialize(ShaderNodes::AssignOp& node);
|
||||
void Serialize(ShaderNodes::BinaryOp& node);
|
||||
void Serialize(ShaderNodes::BuiltinVariable& var);
|
||||
@@ -51,6 +52,8 @@ namespace Nz
|
||||
virtual void Node(ShaderNodes::NodePtr& node) = 0;
|
||||
template<typename T> void Node(std::shared_ptr<T>& node);
|
||||
|
||||
virtual void Type(ShaderExpressionType& type) = 0;
|
||||
|
||||
virtual void Value(bool& val) = 0;
|
||||
virtual void Value(float& val) = 0;
|
||||
virtual void Value(std::string& val) = 0;
|
||||
@@ -78,6 +81,7 @@ namespace Nz
|
||||
bool IsWriting() const override;
|
||||
void Node(const ShaderNodes::NodePtr& node);
|
||||
void Node(ShaderNodes::NodePtr& node) override;
|
||||
void Type(ShaderExpressionType& type) override;
|
||||
void Value(bool& val) override;
|
||||
void Value(float& val) override;
|
||||
void Value(std::string& val) override;
|
||||
@@ -103,7 +107,7 @@ namespace Nz
|
||||
private:
|
||||
bool IsWriting() const override;
|
||||
void Node(ShaderNodes::NodePtr& node) override;
|
||||
void Type(ShaderExpressionType& type);
|
||||
void Type(ShaderExpressionType& type) override;
|
||||
void Value(bool& val) override;
|
||||
void Value(float& val) override;
|
||||
void Value(std::string& val) override;
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace Nz
|
||||
void TypeMustMatch(const ShaderExpressionType& left, const ShaderExpressionType& right);
|
||||
|
||||
using ShaderVisitor::Visit;
|
||||
void Visit(const ShaderNodes::AccessMember& node) override;
|
||||
void Visit(const ShaderNodes::AssignOp& node) override;
|
||||
void Visit(const ShaderNodes::BinaryOp& node) override;
|
||||
void Visit(const ShaderNodes::Branch& node) override;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Math/Vector4.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/ShaderEnums.hpp>
|
||||
#include <Nazara/Renderer/ShaderExpressionType.hpp>
|
||||
#include <array>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -34,7 +34,7 @@ namespace Nz
|
||||
virtual VariableType GetType() const = 0;
|
||||
virtual void Visit(ShaderVarVisitor& visitor) = 0;
|
||||
|
||||
BasicType type;
|
||||
ShaderExpressionType type;
|
||||
};
|
||||
|
||||
struct BuiltinVariable;
|
||||
@@ -48,7 +48,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<BuiltinVariable> Build(BuiltinEntry entry, BasicType varType);
|
||||
static inline std::shared_ptr<BuiltinVariable> Build(BuiltinEntry entry, ShaderExpressionType varType);
|
||||
};
|
||||
|
||||
struct NamedVariable;
|
||||
@@ -69,7 +69,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<InputVariable> Build(std::string varName, BasicType varType);
|
||||
static inline std::shared_ptr<InputVariable> Build(std::string varName, ShaderExpressionType varType);
|
||||
};
|
||||
|
||||
struct LocalVariable;
|
||||
@@ -81,7 +81,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<LocalVariable> Build(std::string varName, BasicType varType);
|
||||
static inline std::shared_ptr<LocalVariable> Build(std::string varName, ShaderExpressionType varType);
|
||||
};
|
||||
|
||||
struct OutputVariable;
|
||||
@@ -93,7 +93,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<OutputVariable> Build(std::string varName, BasicType varType);
|
||||
static inline std::shared_ptr<OutputVariable> Build(std::string varName, ShaderExpressionType varType);
|
||||
};
|
||||
|
||||
struct ParameterVariable;
|
||||
@@ -105,7 +105,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<ParameterVariable> Build(std::string varName, BasicType varType);
|
||||
static inline std::shared_ptr<ParameterVariable> Build(std::string varName, ShaderExpressionType varType);
|
||||
};
|
||||
|
||||
struct UniformVariable;
|
||||
@@ -117,7 +117,7 @@ namespace Nz
|
||||
VariableType GetType() const override;
|
||||
void Visit(ShaderVarVisitor& visitor) override;
|
||||
|
||||
static inline std::shared_ptr<UniformVariable> Build(std::string varName, BasicType varType);
|
||||
static inline std::shared_ptr<UniformVariable> Build(std::string varName, ShaderExpressionType varType);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
namespace Nz::ShaderNodes
|
||||
{
|
||||
inline std::shared_ptr<BuiltinVariable> BuiltinVariable::Build(BuiltinEntry variable, BasicType varType)
|
||||
inline std::shared_ptr<BuiltinVariable> BuiltinVariable::Build(BuiltinEntry variable, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<BuiltinVariable>();
|
||||
node->entry = variable;
|
||||
@@ -16,7 +16,7 @@ namespace Nz::ShaderNodes
|
||||
return node;
|
||||
}
|
||||
|
||||
inline std::shared_ptr<InputVariable> InputVariable::Build(std::string varName, BasicType varType)
|
||||
inline std::shared_ptr<InputVariable> InputVariable::Build(std::string varName, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<InputVariable>();
|
||||
node->name = std::move(varName);
|
||||
@@ -25,7 +25,7 @@ namespace Nz::ShaderNodes
|
||||
return node;
|
||||
}
|
||||
|
||||
inline std::shared_ptr<LocalVariable> LocalVariable::Build(std::string varName, BasicType varType)
|
||||
inline std::shared_ptr<LocalVariable> LocalVariable::Build(std::string varName, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<LocalVariable>();
|
||||
node->name = std::move(varName);
|
||||
@@ -34,7 +34,7 @@ namespace Nz::ShaderNodes
|
||||
return node;
|
||||
}
|
||||
|
||||
inline std::shared_ptr<OutputVariable> OutputVariable::Build(std::string varName, BasicType varType)
|
||||
inline std::shared_ptr<OutputVariable> OutputVariable::Build(std::string varName, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<OutputVariable>();
|
||||
node->name = std::move(varName);
|
||||
@@ -43,7 +43,7 @@ namespace Nz::ShaderNodes
|
||||
return node;
|
||||
}
|
||||
|
||||
inline std::shared_ptr<ParameterVariable> ParameterVariable::Build(std::string varName, BasicType varType)
|
||||
inline std::shared_ptr<ParameterVariable> ParameterVariable::Build(std::string varName, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<ParameterVariable>();
|
||||
node->name = std::move(varName);
|
||||
@@ -52,7 +52,7 @@ namespace Nz::ShaderNodes
|
||||
return node;
|
||||
}
|
||||
|
||||
inline std::shared_ptr<UniformVariable> UniformVariable::Build(std::string varName, BasicType varType)
|
||||
inline std::shared_ptr<UniformVariable> UniformVariable::Build(std::string varName, ShaderExpressionType varType)
|
||||
{
|
||||
auto node = std::make_shared<UniformVariable>();
|
||||
node->name = std::move(varName);
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace Nz
|
||||
|
||||
bool IsConditionEnabled(const std::string& name) const;
|
||||
|
||||
virtual void Visit(const ShaderNodes::AccessMember& node) = 0;
|
||||
virtual void Visit(const ShaderNodes::AssignOp& node) = 0;
|
||||
virtual void Visit(const ShaderNodes::BinaryOp& node) = 0;
|
||||
virtual void Visit(const ShaderNodes::Branch& node) = 0;
|
||||
|
||||
Reference in New Issue
Block a user