ShaderNodes: Replace union by std::variant
This commit is contained in:
parent
50bd150345
commit
7736312c2f
|
|
@ -220,22 +220,17 @@ namespace Nz
|
||||||
ShaderExpressionType GetExpressionType() const override;
|
ShaderExpressionType GetExpressionType() const override;
|
||||||
void Visit(ShaderVisitor& visitor) override;
|
void Visit(ShaderVisitor& visitor) override;
|
||||||
|
|
||||||
BasicType exprType;
|
using Variant = std::variant<
|
||||||
|
bool,
|
||||||
|
float,
|
||||||
|
Vector2f,
|
||||||
|
Vector3f,
|
||||||
|
Vector4f
|
||||||
|
>;
|
||||||
|
|
||||||
union
|
Variant value;
|
||||||
{
|
|
||||||
bool bool1;
|
|
||||||
float vec1;
|
|
||||||
Vector2f vec2;
|
|
||||||
Vector3f vec3;
|
|
||||||
Vector4f vec4;
|
|
||||||
} values;
|
|
||||||
|
|
||||||
static inline std::shared_ptr<Constant> Build(bool value);
|
template<typename T> static std::shared_ptr<Constant> Build(const T& 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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NAZARA_RENDERER_API SwizzleOp : public Expression
|
struct NAZARA_RENDERER_API SwizzleOp : public Expression
|
||||||
|
|
|
||||||
|
|
@ -239,47 +239,11 @@ namespace Nz::ShaderNodes
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Constant> Constant::Build(bool value)
|
template<typename T>
|
||||||
|
std::shared_ptr<Constant> Nz::ShaderNodes::Constant::Build(const T& value)
|
||||||
{
|
{
|
||||||
auto node = std::make_shared<Constant>();
|
auto node = std::make_shared<Constant>();
|
||||||
node->exprType = BasicType::Boolean;
|
node->value = value;
|
||||||
node->values.bool1 = value;
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::shared_ptr<Constant> Constant::Build(float value)
|
|
||||||
{
|
|
||||||
auto node = std::make_shared<Constant>();
|
|
||||||
node->exprType = BasicType::Float1;
|
|
||||||
node->values.vec1 = value;
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::shared_ptr<Constant> Constant::Build(const Vector2f& value)
|
|
||||||
{
|
|
||||||
auto node = std::make_shared<Constant>();
|
|
||||||
node->exprType = BasicType::Float2;
|
|
||||||
node->values.vec2 = value;
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::shared_ptr<Constant> Constant::Build(const Vector3f& value)
|
|
||||||
{
|
|
||||||
auto node = std::make_shared<Constant>();
|
|
||||||
node->exprType = BasicType::Float3;
|
|
||||||
node->values.vec3 = value;
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::shared_ptr<Constant> Constant::Build(const Vector4f& value)
|
|
||||||
{
|
|
||||||
auto node = std::make_shared<Constant>();
|
|
||||||
node->exprType = BasicType::Float4;
|
|
||||||
node->values.vec4 = value;
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Renderer/GlslWriter.hpp>
|
#include <Nazara/Renderer/GlslWriter.hpp>
|
||||||
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
#include <Nazara/Core/CallOnExit.hpp>
|
#include <Nazara/Core/CallOnExit.hpp>
|
||||||
#include <Nazara/Renderer/ShaderValidator.hpp>
|
#include <Nazara/Renderer/ShaderValidator.hpp>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
@ -426,31 +427,23 @@ namespace Nz
|
||||||
|
|
||||||
void GlslWriter::Visit(const ShaderNodes::Constant& node)
|
void GlslWriter::Visit(const ShaderNodes::Constant& node)
|
||||||
{
|
{
|
||||||
switch (node.exprType)
|
std::visit([&](auto&& arg)
|
||||||
{
|
{
|
||||||
case ShaderNodes::BasicType::Boolean:
|
using T = std::decay_t<decltype(arg)>;
|
||||||
Append((node.values.bool1) ? "true" : "false");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ShaderNodes::BasicType::Float1:
|
if constexpr (std::is_same_v<T, bool>)
|
||||||
Append(std::to_string(node.values.vec1));
|
Append((arg) ? "true" : "false");
|
||||||
break;
|
else if constexpr (std::is_same_v<T, float>)
|
||||||
|
Append(std::to_string(arg));
|
||||||
case ShaderNodes::BasicType::Float2:
|
else if constexpr (std::is_same_v<T, Vector2f>)
|
||||||
Append("vec2(" + std::to_string(node.values.vec2.x) + ", " + std::to_string(node.values.vec2.y) + ")");
|
Append("vec2(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ")");
|
||||||
break;
|
else if constexpr (std::is_same_v<T, Vector3f>)
|
||||||
|
Append("vec3(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ")");
|
||||||
case ShaderNodes::BasicType::Float3:
|
else if constexpr (std::is_same_v<T, Vector4f>)
|
||||||
Append("vec3(" + std::to_string(node.values.vec3.x) + ", " + std::to_string(node.values.vec3.y) + ", " + std::to_string(node.values.vec3.z) + ")");
|
Append("vec4(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ", " + std::to_string(arg.w) + ")");
|
||||||
break;
|
else
|
||||||
|
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
|
||||||
case ShaderNodes::BasicType::Float4:
|
}, node.value);
|
||||||
Append("vec4(" + std::to_string(node.values.vec4.x) + ", " + std::to_string(node.values.vec4.y) + ", " + std::to_string(node.values.vec4.z) + ", " + std::to_string(node.values.vec4.w) + ")");
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw std::runtime_error("Unhandled expression type");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Visit(const ShaderNodes::DeclareVariable& node)
|
void GlslWriter::Visit(const ShaderNodes::DeclareVariable& node)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Renderer/ShaderNodes.hpp>
|
#include <Nazara/Renderer/ShaderNodes.hpp>
|
||||||
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
#include <Nazara/Renderer/ShaderSerializer.hpp>
|
#include <Nazara/Renderer/ShaderSerializer.hpp>
|
||||||
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
||||||
#include <Nazara/Renderer/ShaderWriter.hpp>
|
#include <Nazara/Renderer/ShaderWriter.hpp>
|
||||||
|
|
@ -150,7 +151,23 @@ namespace Nz::ShaderNodes
|
||||||
|
|
||||||
ShaderExpressionType Constant::GetExpressionType() const
|
ShaderExpressionType Constant::GetExpressionType() const
|
||||||
{
|
{
|
||||||
return exprType;
|
return std::visit([&](auto&& arg)
|
||||||
|
{
|
||||||
|
using T = std::decay_t<decltype(arg)>;
|
||||||
|
|
||||||
|
if constexpr (std::is_same_v<T, bool>)
|
||||||
|
return ShaderNodes::BasicType::Boolean;
|
||||||
|
else if constexpr (std::is_same_v<T, float>)
|
||||||
|
return ShaderNodes::BasicType::Float1;
|
||||||
|
else if constexpr (std::is_same_v<T, Vector2f>)
|
||||||
|
return ShaderNodes::BasicType::Float2;
|
||||||
|
else if constexpr (std::is_same_v<T, Vector3f>)
|
||||||
|
return ShaderNodes::BasicType::Float3;
|
||||||
|
else if constexpr (std::is_same_v<T, Vector4f>)
|
||||||
|
return ShaderNodes::BasicType::Float4;
|
||||||
|
else
|
||||||
|
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
|
||||||
|
}, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Constant::Visit(ShaderVisitor& visitor)
|
void Constant::Visit(ShaderVisitor& visitor)
|
||||||
|
|
|
||||||
|
|
@ -178,29 +178,30 @@ namespace Nz
|
||||||
|
|
||||||
void ShaderSerializerBase::Serialize(ShaderNodes::Constant& node)
|
void ShaderSerializerBase::Serialize(ShaderNodes::Constant& node)
|
||||||
{
|
{
|
||||||
Enum(node.exprType);
|
UInt32 typeIndex;
|
||||||
|
if (IsWriting())
|
||||||
|
typeIndex = UInt32(node.value.index());
|
||||||
|
|
||||||
switch (node.exprType)
|
Value(typeIndex);
|
||||||
|
|
||||||
|
// Waiting for template lambda in C++20
|
||||||
|
auto SerializeValue = [&](auto dummyType)
|
||||||
{
|
{
|
||||||
case ShaderNodes::BasicType::Boolean:
|
using T = std::decay_t<decltype(dummyType)>;
|
||||||
Value(node.values.bool1);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ShaderNodes::BasicType::Float1:
|
auto& value = (IsWriting()) ? std::get<T>(node.value) : node.value.emplace<T>();
|
||||||
Value(node.values.vec1);
|
Value(value);
|
||||||
break;
|
};
|
||||||
|
|
||||||
case ShaderNodes::BasicType::Float2:
|
static_assert(std::variant_size_v<decltype(node.value)> == 5);
|
||||||
Value(node.values.vec2);
|
switch (typeIndex)
|
||||||
break;
|
{
|
||||||
|
case 0: SerializeValue(bool()); break;
|
||||||
case ShaderNodes::BasicType::Float3:
|
case 1: SerializeValue(float()); break;
|
||||||
Value(node.values.vec3);
|
case 2: SerializeValue(Vector2f()); break;
|
||||||
break;
|
case 3: SerializeValue(Vector3f()); break;
|
||||||
|
case 4: SerializeValue(Vector4f()); break;
|
||||||
case ShaderNodes::BasicType::Float4:
|
default: throw std::runtime_error("unexpected data type");
|
||||||
Value(node.values.vec4);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue