Shader: Handle type as expressions
This commit is contained in:
@@ -24,7 +24,8 @@ namespace Nz::ShaderAst
|
||||
AstCloner(AstCloner&&) = delete;
|
||||
~AstCloner() = default;
|
||||
|
||||
template<typename T> AttributeValue<T> Clone(const AttributeValue<T>& attribute);
|
||||
template<typename T> ExpressionValue<T> Clone(const ExpressionValue<T>& expressionValue);
|
||||
inline ExpressionValue<ExpressionType> Clone(const ExpressionValue<ExpressionType>& expressionValue);
|
||||
ExpressionPtr Clone(Expression& statement);
|
||||
StatementPtr Clone(Statement& statement);
|
||||
|
||||
@@ -37,6 +38,7 @@ namespace Nz::ShaderAst
|
||||
|
||||
virtual ExpressionPtr CloneExpression(Expression& expr);
|
||||
virtual StatementPtr CloneStatement(Statement& statement);
|
||||
virtual ExpressionValue<ExpressionType> CloneType(const ExpressionValue<ExpressionType>& exprType);
|
||||
|
||||
virtual ExpressionPtr Clone(AccessIdentifierExpression& node);
|
||||
virtual ExpressionPtr Clone(AccessIndexExpression& node);
|
||||
@@ -69,6 +71,7 @@ namespace Nz::ShaderAst
|
||||
virtual StatementPtr Clone(MultiStatement& node);
|
||||
virtual StatementPtr Clone(NoOpStatement& node);
|
||||
virtual StatementPtr Clone(ReturnStatement& node);
|
||||
virtual StatementPtr Clone(ScopedStatement& node);
|
||||
virtual StatementPtr Clone(WhileStatement& node);
|
||||
|
||||
#define NAZARA_SHADERAST_NODE(NodeType) void Visit(NodeType& node) override;
|
||||
@@ -85,7 +88,7 @@ namespace Nz::ShaderAst
|
||||
std::vector<StatementPtr> m_statementStack;
|
||||
};
|
||||
|
||||
template<typename T> AttributeValue<T> Clone(const AttributeValue<T>& attribute);
|
||||
template<typename T> ExpressionValue<T> Clone(const ExpressionValue<T>& attribute);
|
||||
inline ExpressionPtr Clone(Expression& node);
|
||||
inline StatementPtr Clone(Statement& node);
|
||||
}
|
||||
|
||||
@@ -8,20 +8,25 @@
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
template<typename T>
|
||||
AttributeValue<T> AstCloner::Clone(const AttributeValue<T>& attribute)
|
||||
ExpressionValue<T> AstCloner::Clone(const ExpressionValue<T>& expressionValue)
|
||||
{
|
||||
if (!attribute.HasValue())
|
||||
if (!expressionValue.HasValue())
|
||||
return {};
|
||||
|
||||
if (attribute.IsExpression())
|
||||
return CloneExpression(attribute.GetExpression());
|
||||
if (expressionValue.IsExpression())
|
||||
return CloneExpression(expressionValue.GetExpression());
|
||||
else
|
||||
{
|
||||
assert(attribute.IsResultingValue());
|
||||
return attribute.GetResultingValue();
|
||||
assert(expressionValue.IsResultingValue());
|
||||
return expressionValue.GetResultingValue();
|
||||
}
|
||||
}
|
||||
|
||||
inline ExpressionValue<ExpressionType> AstCloner::Clone(const ExpressionValue<ExpressionType>& expressionValue)
|
||||
{
|
||||
return CloneType(expressionValue);
|
||||
}
|
||||
|
||||
ExpressionPtr AstCloner::CloneExpression(const ExpressionPtr& expr)
|
||||
{
|
||||
if (!expr)
|
||||
@@ -40,7 +45,7 @@ namespace Nz::ShaderAst
|
||||
|
||||
|
||||
template<typename T>
|
||||
AttributeValue<T> Clone(const AttributeValue<T>& attribute)
|
||||
ExpressionValue<T> Clone(const ExpressionValue<T>& attribute)
|
||||
{
|
||||
AstCloner cloner;
|
||||
return cloner.Clone(attribute);
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Nz::ShaderAst
|
||||
template<typename T> bool Compare(const T& lhs, const T& rhs);
|
||||
template<typename T, std::size_t S> bool Compare(const std::array<T, S>& lhs, const std::array<T, S>& rhs);
|
||||
template<typename T> bool Compare(const std::vector<T>& lhs, const std::vector<T>& rhs);
|
||||
template<typename T> bool Compare(const AttributeValue<T>& lhs, const AttributeValue<T>& rhs);
|
||||
template<typename T> bool Compare(const ExpressionValue<T>& lhs, const ExpressionValue<T>& rhs);
|
||||
inline bool Compare(const BranchStatement::ConditionalStatement& lhs, const BranchStatement::ConditionalStatement& rhs);
|
||||
inline bool Compare(const DeclareExternalStatement::ExternalVar& lhs, const DeclareExternalStatement::ExternalVar& rhs);
|
||||
inline bool Compare(const DeclareFunctionStatement::Parameter& lhs, const DeclareFunctionStatement::Parameter& rhs);
|
||||
@@ -59,6 +59,7 @@ namespace Nz::ShaderAst
|
||||
inline bool Compare(const MultiStatement& lhs, const MultiStatement& rhs);
|
||||
inline bool Compare(const NoOpStatement& lhs, const NoOpStatement& rhs);
|
||||
inline bool Compare(const ReturnStatement& lhs, const ReturnStatement& rhs);
|
||||
inline bool Compare(const ScopedStatement& lhs, const ScopedStatement& rhs);
|
||||
inline bool Compare(const WhileStatement& lhs, const WhileStatement& rhs);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Compare(const AttributeValue<T>& lhs, const AttributeValue<T>& rhs)
|
||||
bool Compare(const ExpressionValue<T>& lhs, const ExpressionValue<T>& rhs)
|
||||
{
|
||||
if (!Compare(lhs.HasValue(), rhs.HasValue()))
|
||||
return false;
|
||||
@@ -519,6 +519,14 @@ namespace Nz::ShaderAst
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Compare(const ScopedStatement& lhs, const ScopedStatement& rhs)
|
||||
{
|
||||
if (!Compare(lhs.statement, rhs.statement))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool Compare(const WhileStatement& lhs, const WhileStatement& rhs)
|
||||
{
|
||||
if (!Compare(lhs.unroll, rhs.unroll))
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API ExpressionVisitorExcept : public AstExpressionVisitor
|
||||
class NAZARA_SHADER_API AstExpressionVisitorExcept : public AstExpressionVisitor
|
||||
{
|
||||
public:
|
||||
using AstExpressionVisitor::Visit;
|
||||
|
||||
@@ -58,6 +58,7 @@ NAZARA_SHADERAST_STATEMENT(ExpressionStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(MultiStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(NoOpStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(ReturnStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(ScopedStatement)
|
||||
NAZARA_SHADERAST_STATEMENT_LAST(WhileStatement)
|
||||
|
||||
#undef NAZARA_SHADERAST_EXPRESSION
|
||||
|
||||
@@ -57,6 +57,8 @@ namespace Nz::ShaderAst
|
||||
template<typename TargetType> ExpressionPtr PropagateVec3Cast(TargetType v1, TargetType v2, TargetType v3);
|
||||
template<typename TargetType> ExpressionPtr PropagateVec4Cast(TargetType v1, TargetType v2, TargetType v3, TargetType v4);
|
||||
|
||||
StatementPtr Unscope(StatementPtr node);
|
||||
|
||||
private:
|
||||
Options m_options;
|
||||
};
|
||||
|
||||
@@ -51,6 +51,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(MultiStatement& node) override;
|
||||
void Visit(NoOpStatement& node) override;
|
||||
void Visit(ReturnStatement& node) override;
|
||||
void Visit(ScopedStatement& node) override;
|
||||
void Visit(WhileStatement& node) override;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Nz::ShaderAst
|
||||
struct Callbacks
|
||||
{
|
||||
std::function<void(ShaderStageType stageType, const std::string& functionName)> onEntryPointDeclaration;
|
||||
std::function<void(const std::string& optionName, const ExpressionType& optionType)> onOptionDeclaration;
|
||||
std::function<void(const std::string& optionName, const ExpressionValue<ExpressionType>& optionType)> onOptionDeclaration;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
@@ -54,12 +54,13 @@ namespace Nz::ShaderAst
|
||||
void Serialize(MultiStatement& node);
|
||||
void Serialize(NoOpStatement& node);
|
||||
void Serialize(ReturnStatement& node);
|
||||
void Serialize(ScopedStatement& node);
|
||||
void Serialize(WhileStatement& node);
|
||||
|
||||
protected:
|
||||
template<typename T> void Attribute(AttributeValue<T>& attribute);
|
||||
template<typename T> void Container(T& container);
|
||||
template<typename T> void Enum(T& enumVal);
|
||||
template<typename T> void ExprValue(ExpressionValue<T>& attribute);
|
||||
template<typename T> void OptEnum(std::optional<T>& optVal);
|
||||
template<typename T> void OptVal(std::optional<T>& optVal);
|
||||
|
||||
|
||||
@@ -3,12 +3,42 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/Ast/AstSerializer.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
template<typename T>
|
||||
void AstSerializerBase::Attribute(AttributeValue<T>& attribute)
|
||||
void AstSerializerBase::Container(T& container)
|
||||
{
|
||||
bool isWriting = IsWriting();
|
||||
|
||||
UInt32 size;
|
||||
if (isWriting)
|
||||
size = SafeCast<UInt32>(container.size());
|
||||
|
||||
Value(size);
|
||||
if (!isWriting)
|
||||
container.resize(size);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void AstSerializerBase::Enum(T& enumVal)
|
||||
{
|
||||
bool isWriting = IsWriting();
|
||||
|
||||
UInt32 value;
|
||||
if (isWriting)
|
||||
value = SafeCast<UInt32>(enumVal);
|
||||
|
||||
Value(value);
|
||||
if (!isWriting)
|
||||
enumVal = static_cast<T>(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AstSerializerBase::ExprValue(ExpressionValue<T>& attribute)
|
||||
{
|
||||
UInt32 valueType;
|
||||
if (IsWriting())
|
||||
@@ -55,6 +85,8 @@ namespace Nz::ShaderAst
|
||||
T value;
|
||||
if constexpr (std::is_enum_v<T>)
|
||||
Enum(value);
|
||||
else if constexpr (std::is_same_v<T, ExpressionType>)
|
||||
Type(value);
|
||||
else
|
||||
Value(value);
|
||||
|
||||
@@ -65,6 +97,8 @@ namespace Nz::ShaderAst
|
||||
T& value = const_cast<T&>(attribute.GetResultingValue()); //< not used for writing
|
||||
if constexpr (std::is_enum_v<T>)
|
||||
Enum(value);
|
||||
else if constexpr (std::is_same_v<T, ExpressionType>)
|
||||
Type(value);
|
||||
else
|
||||
Value(value);
|
||||
}
|
||||
@@ -74,35 +108,6 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AstSerializerBase::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 AstSerializerBase::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>
|
||||
void AstSerializerBase::OptEnum(std::optional<T>& optVal)
|
||||
{
|
||||
@@ -150,12 +155,12 @@ namespace Nz::ShaderAst
|
||||
|
||||
UInt32 fixedVal;
|
||||
if (isWriting)
|
||||
fixedVal = static_cast<UInt32>(val);
|
||||
fixedVal = SafeCast<UInt32>(val);
|
||||
|
||||
Value(fixedVal);
|
||||
|
||||
if (!isWriting)
|
||||
val = static_cast<std::size_t>(fixedVal);
|
||||
val = SafeCast<std::size_t>(fixedVal);
|
||||
}
|
||||
|
||||
inline ShaderAstSerializer::ShaderAstSerializer(ByteStream& stream) :
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API StatementVisitorExcept : public AstStatementVisitor
|
||||
class NAZARA_SHADER_API AstStatementVisitorExcept : public AstStatementVisitor
|
||||
{
|
||||
public:
|
||||
using AstStatementVisitor::Visit;
|
||||
|
||||
36
include/Nazara/Shader/Ast/AstTypes.hpp
Normal file
36
include/Nazara/Shader/Ast/AstTypes.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Shader module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SHADER_AST_ASTTYPES_HPP
|
||||
#define NAZARA_SHADER_AST_ASTTYPES_HPP
|
||||
|
||||
#include <Nazara/Shader/Ast/ConstantValue.hpp>
|
||||
#include <Nazara/Shader/Ast/ExpressionType.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
enum class TypeParameterCategory
|
||||
{
|
||||
ConstantValue,
|
||||
FullType,
|
||||
PrimitiveType,
|
||||
StructType
|
||||
};
|
||||
|
||||
struct PartialType;
|
||||
|
||||
using TypeParameter = std::variant<ConstantValue, ExpressionType, PartialType>;
|
||||
|
||||
struct PartialType
|
||||
{
|
||||
std::vector<TypeParameterCategory> parameters;
|
||||
std::function<ExpressionType(const TypeParameter* parameters, std::size_t parameterCount)> buildFunc;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // NAZARA_SHADER_AST_ASTTYPES_HPP
|
||||
@@ -20,15 +20,15 @@ namespace Nz::ShaderAst
|
||||
using ExpressionPtr = std::unique_ptr<Expression>;
|
||||
|
||||
template<typename T>
|
||||
class AttributeValue
|
||||
class ExpressionValue
|
||||
{
|
||||
public:
|
||||
AttributeValue() = default;
|
||||
AttributeValue(T value);
|
||||
AttributeValue(ExpressionPtr expr);
|
||||
AttributeValue(const AttributeValue&) = default;
|
||||
AttributeValue(AttributeValue&&) = default;
|
||||
~AttributeValue() = default;
|
||||
ExpressionValue() = default;
|
||||
ExpressionValue(T value);
|
||||
ExpressionValue(ExpressionPtr expr);
|
||||
ExpressionValue(const ExpressionValue&) = default;
|
||||
ExpressionValue(ExpressionValue&&) noexcept = default;
|
||||
~ExpressionValue() = default;
|
||||
|
||||
ExpressionPtr&& GetExpression() &&;
|
||||
const ExpressionPtr& GetExpression() const &;
|
||||
@@ -39,14 +39,14 @@ namespace Nz::ShaderAst
|
||||
|
||||
bool HasValue() const;
|
||||
|
||||
AttributeValue& operator=(const AttributeValue&) = default;
|
||||
AttributeValue& operator=(AttributeValue&&) = default;
|
||||
ExpressionValue& operator=(const ExpressionValue&) = default;
|
||||
ExpressionValue& operator=(ExpressionValue&&) noexcept = default;
|
||||
|
||||
private:
|
||||
std::variant<std::monostate, T, ExpressionPtr> m_value;
|
||||
};
|
||||
|
||||
struct Attribute
|
||||
struct ExprValue
|
||||
{
|
||||
using Param = std::optional<ExpressionPtr>;
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
template<typename T>
|
||||
AttributeValue<T>::AttributeValue(T value) :
|
||||
ExpressionValue<T>::ExpressionValue(T value) :
|
||||
m_value(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
AttributeValue<T>::AttributeValue(ExpressionPtr expr)
|
||||
ExpressionValue<T>::ExpressionValue(ExpressionPtr expr)
|
||||
{
|
||||
assert(expr);
|
||||
m_value = std::move(expr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ExpressionPtr&& AttributeValue<T>::GetExpression() &&
|
||||
ExpressionPtr&& ExpressionValue<T>::GetExpression() &&
|
||||
{
|
||||
if (!IsExpression())
|
||||
throw std::runtime_error("excepted expression");
|
||||
@@ -32,7 +32,7 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const ExpressionPtr& AttributeValue<T>::GetExpression() const &
|
||||
const ExpressionPtr& ExpressionValue<T>::GetExpression() const &
|
||||
{
|
||||
if (!IsExpression())
|
||||
throw std::runtime_error("excepted expression");
|
||||
@@ -42,7 +42,7 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T& AttributeValue<T>::GetResultingValue() const
|
||||
const T& ExpressionValue<T>::GetResultingValue() const
|
||||
{
|
||||
if (!IsResultingValue())
|
||||
throw std::runtime_error("excepted resulting value");
|
||||
@@ -51,19 +51,19 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool AttributeValue<T>::IsExpression() const
|
||||
bool ExpressionValue<T>::IsExpression() const
|
||||
{
|
||||
return std::holds_alternative<ExpressionPtr>(m_value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool AttributeValue<T>::IsResultingValue() const
|
||||
bool ExpressionValue<T>::IsResultingValue() const
|
||||
{
|
||||
return std::holds_alternative<T>(m_value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool AttributeValue<T>::HasValue() const
|
||||
bool ExpressionValue<T>::HasValue() const
|
||||
{
|
||||
return !std::holds_alternative<std::monostate>(m_value);
|
||||
}
|
||||
|
||||
@@ -29,14 +29,22 @@ namespace Nz::ShaderAst
|
||||
ArrayType& operator=(const ArrayType& array);
|
||||
ArrayType& operator=(ArrayType&&) noexcept = default;
|
||||
|
||||
AttributeValue<UInt32> length;
|
||||
UInt32 length;
|
||||
std::unique_ptr<ContainedType> containedType;
|
||||
|
||||
bool operator==(const ArrayType& rhs) const;
|
||||
inline bool operator!=(const ArrayType& rhs) const;
|
||||
};
|
||||
|
||||
struct IdentifierType //< Alias or struct
|
||||
struct FunctionType
|
||||
{
|
||||
std::size_t funcIndex;
|
||||
|
||||
inline bool operator==(const FunctionType& rhs) const;
|
||||
inline bool operator!=(const FunctionType& rhs) const;
|
||||
};
|
||||
|
||||
struct IdentifierType
|
||||
{
|
||||
std::string name;
|
||||
|
||||
@@ -44,6 +52,14 @@ namespace Nz::ShaderAst
|
||||
inline bool operator!=(const IdentifierType& rhs) const;
|
||||
};
|
||||
|
||||
struct IntrinsicFunctionType
|
||||
{
|
||||
IntrinsicType intrinsic;
|
||||
|
||||
inline bool operator==(const IntrinsicFunctionType& rhs) const;
|
||||
inline bool operator!=(const IntrinsicFunctionType& rhs) const;
|
||||
};
|
||||
|
||||
struct MatrixType
|
||||
{
|
||||
std::size_t columnCount;
|
||||
@@ -54,6 +70,22 @@ namespace Nz::ShaderAst
|
||||
inline bool operator!=(const MatrixType& rhs) const;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API MethodType
|
||||
{
|
||||
MethodType() = default;
|
||||
MethodType(const MethodType& methodType);
|
||||
MethodType(MethodType&&) noexcept = default;
|
||||
|
||||
MethodType& operator=(const MethodType& methodType);
|
||||
MethodType& operator=(MethodType&&) noexcept = default;
|
||||
|
||||
std::unique_ptr<ContainedType> objectType;
|
||||
std::size_t methodIndex;
|
||||
|
||||
bool operator==(const MethodType& rhs) const;
|
||||
inline bool operator!=(const MethodType& rhs) const;
|
||||
};
|
||||
|
||||
struct NoType
|
||||
{
|
||||
inline bool operator==(const NoType& rhs) const;
|
||||
@@ -77,9 +109,17 @@ namespace Nz::ShaderAst
|
||||
inline bool operator!=(const StructType& rhs) const;
|
||||
};
|
||||
|
||||
struct Type
|
||||
{
|
||||
std::size_t typeIndex;
|
||||
|
||||
inline bool operator==(const Type& rhs) const;
|
||||
inline bool operator!=(const Type& rhs) const;
|
||||
};
|
||||
|
||||
struct UniformType
|
||||
{
|
||||
std::variant<IdentifierType, StructType> containedType;
|
||||
StructType containedType;
|
||||
|
||||
inline bool operator==(const UniformType& rhs) const;
|
||||
inline bool operator!=(const UniformType& rhs) const;
|
||||
@@ -94,7 +134,7 @@ namespace Nz::ShaderAst
|
||||
inline bool operator!=(const VectorType& rhs) const;
|
||||
};
|
||||
|
||||
using ExpressionType = std::variant<NoType, ArrayType, IdentifierType, PrimitiveType, MatrixType, SamplerType, StructType, UniformType, VectorType>;
|
||||
using ExpressionType = std::variant<NoType, ArrayType, FunctionType, IdentifierType, IntrinsicFunctionType, PrimitiveType, MatrixType, MethodType, SamplerType, StructType, Type, UniformType, VectorType>;
|
||||
|
||||
struct ContainedType
|
||||
{
|
||||
@@ -105,25 +145,29 @@ namespace Nz::ShaderAst
|
||||
{
|
||||
struct StructMember
|
||||
{
|
||||
AttributeValue<BuiltinEntry> builtin;
|
||||
AttributeValue<bool> cond;
|
||||
AttributeValue<UInt32> locationIndex;
|
||||
ExpressionValue<BuiltinEntry> builtin;
|
||||
ExpressionValue<bool> cond;
|
||||
ExpressionValue<UInt32> locationIndex;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::string name;
|
||||
ExpressionType type;
|
||||
};
|
||||
|
||||
AttributeValue<StructLayout> layout;
|
||||
ExpressionValue<StructLayout> layout;
|
||||
std::string name;
|
||||
std::vector<StructMember> members;
|
||||
};
|
||||
|
||||
inline bool IsArrayType(const ExpressionType& type);
|
||||
inline bool IsFunctionType(const ExpressionType& type);
|
||||
inline bool IsIdentifierType(const ExpressionType& type);
|
||||
inline bool IsIntrinsicFunctionType(const ExpressionType& type);
|
||||
inline bool IsMatrixType(const ExpressionType& type);
|
||||
inline bool IsMethodType(const ExpressionType& type);
|
||||
inline bool IsNoType(const ExpressionType& type);
|
||||
inline bool IsPrimitiveType(const ExpressionType& type);
|
||||
inline bool IsSamplerType(const ExpressionType& type);
|
||||
inline bool IsStructType(const ExpressionType& type);
|
||||
inline bool IsTypeExpression(const ExpressionType& type);
|
||||
inline bool IsUniformType(const ExpressionType& type);
|
||||
inline bool IsVectorType(const ExpressionType& type);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,17 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
|
||||
inline bool FunctionType::operator==(const FunctionType& rhs) const
|
||||
{
|
||||
return funcIndex == rhs.funcIndex;
|
||||
}
|
||||
|
||||
inline bool FunctionType::operator!=(const FunctionType& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
inline bool IdentifierType::operator==(const IdentifierType& rhs) const
|
||||
{
|
||||
return name == rhs.name;
|
||||
@@ -25,6 +36,17 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
|
||||
inline bool IntrinsicFunctionType::operator==(const IntrinsicFunctionType& rhs) const
|
||||
{
|
||||
return intrinsic == rhs.intrinsic;
|
||||
}
|
||||
|
||||
inline bool IntrinsicFunctionType::operator!=(const IntrinsicFunctionType& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
inline bool MatrixType::operator==(const MatrixType& rhs) const
|
||||
{
|
||||
return columnCount == rhs.columnCount && rowCount == rhs.rowCount && type == rhs.type;
|
||||
@@ -36,6 +58,12 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
|
||||
inline bool MethodType::operator!=(const MethodType& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
inline bool NoType::operator==(const NoType& /*rhs*/) const
|
||||
{
|
||||
return true;
|
||||
@@ -68,6 +96,18 @@ namespace Nz::ShaderAst
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
inline bool Type::operator==(const Type& rhs) const
|
||||
{
|
||||
return typeIndex == rhs.typeIndex;
|
||||
}
|
||||
|
||||
inline bool Type::operator!=(const Type& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
inline bool UniformType::operator==(const UniformType& rhs) const
|
||||
{
|
||||
return containedType == rhs.containedType;
|
||||
@@ -90,21 +130,36 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
|
||||
|
||||
bool IsArrayType(const ExpressionType& type)
|
||||
inline bool IsArrayType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<ArrayType>(type);
|
||||
}
|
||||
|
||||
inline bool IsFunctionType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<FunctionType>(type);
|
||||
}
|
||||
|
||||
inline bool IsIdentifierType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<IdentifierType>(type);
|
||||
}
|
||||
|
||||
inline bool IsIntrinsicFunctionType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<IntrinsicFunctionType>(type);
|
||||
}
|
||||
|
||||
inline bool IsMatrixType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<MatrixType>(type);
|
||||
}
|
||||
|
||||
inline bool IsMethodType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<MethodType>(type);
|
||||
}
|
||||
|
||||
inline bool IsNoType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<NoType>(type);
|
||||
@@ -125,6 +180,11 @@ namespace Nz::ShaderAst
|
||||
return std::holds_alternative<StructType>(type);
|
||||
}
|
||||
|
||||
bool IsTypeExpression(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<Type>(type);
|
||||
}
|
||||
|
||||
bool IsUniformType(const ExpressionType& type)
|
||||
{
|
||||
return std::holds_alternative<UniformType>(type);
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
std::variant<std::string, std::size_t> targetFunction;
|
||||
ExpressionPtr targetFunction;
|
||||
std::vector<ExpressionPtr> parameters;
|
||||
};
|
||||
|
||||
@@ -126,7 +126,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
ExpressionType targetType;
|
||||
ExpressionValue<ExpressionType> targetType;
|
||||
std::array<ExpressionPtr, 4> expressions;
|
||||
};
|
||||
|
||||
@@ -249,10 +249,10 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::optional<std::size_t> constIndex;
|
||||
std::string name;
|
||||
ExpressionPtr expression;
|
||||
ExpressionType type;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareExternalStatement : Statement
|
||||
@@ -262,13 +262,13 @@ namespace Nz::ShaderAst
|
||||
|
||||
struct ExternalVar
|
||||
{
|
||||
AttributeValue<UInt32> bindingIndex;
|
||||
AttributeValue<UInt32> bindingSet;
|
||||
ExpressionValue<UInt32> bindingIndex;
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::string name;
|
||||
ExpressionType type;
|
||||
};
|
||||
|
||||
AttributeValue<UInt32> bindingSet;
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::vector<ExternalVar> externalVars;
|
||||
};
|
||||
@@ -281,18 +281,18 @@ namespace Nz::ShaderAst
|
||||
struct Parameter
|
||||
{
|
||||
std::string name;
|
||||
ExpressionType type;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
};
|
||||
|
||||
AttributeValue<DepthWriteMode> depthWrite;
|
||||
AttributeValue<bool> earlyFragmentTests;
|
||||
AttributeValue<ShaderStageType> entryStage;
|
||||
ExpressionValue<DepthWriteMode> depthWrite;
|
||||
ExpressionValue<bool> earlyFragmentTests;
|
||||
ExpressionValue<ShaderStageType> entryStage;
|
||||
ExpressionValue<ExpressionType> returnType;
|
||||
std::optional<std::size_t> funcIndex;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string name;
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<StatementPtr> statements;
|
||||
ExpressionType returnType;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareOptionStatement : Statement
|
||||
@@ -303,7 +303,7 @@ namespace Nz::ShaderAst
|
||||
std::optional<std::size_t> optIndex;
|
||||
std::string optName;
|
||||
ExpressionPtr defaultValue;
|
||||
ExpressionType optType;
|
||||
ExpressionValue<ExpressionType> optType;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareStructStatement : Statement
|
||||
@@ -323,7 +323,7 @@ namespace Nz::ShaderAst
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string varName;
|
||||
ExpressionPtr initialExpression;
|
||||
ExpressionType varType;
|
||||
ExpressionValue<ExpressionType> varType;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DiscardStatement : Statement
|
||||
@@ -345,7 +345,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
AttributeValue<LoopUnroll> unroll;
|
||||
ExpressionValue<LoopUnroll> unroll;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string varName;
|
||||
ExpressionPtr fromExpr;
|
||||
@@ -359,7 +359,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
AttributeValue<LoopUnroll> unroll;
|
||||
ExpressionValue<LoopUnroll> unroll;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string varName;
|
||||
ExpressionPtr expression;
|
||||
@@ -388,12 +388,20 @@ namespace Nz::ShaderAst
|
||||
ExpressionPtr returnExpr;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API ScopedStatement : Statement
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
StatementPtr statement;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API WhileStatement : Statement
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
AttributeValue<LoopUnroll> unroll;
|
||||
ExpressionValue<LoopUnroll> unroll;
|
||||
ExpressionPtr condition;
|
||||
StatementPtr body;
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <Nazara/Shader/Ast/AstTypes.hpp>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
@@ -19,6 +20,8 @@ namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API SanitizeVisitor final : AstCloner
|
||||
{
|
||||
friend class AstTypeExpressionVisitor;
|
||||
|
||||
public:
|
||||
struct Options;
|
||||
|
||||
@@ -55,6 +58,7 @@ namespace Nz::ShaderAst
|
||||
struct Identifier;
|
||||
|
||||
using AstCloner::CloneExpression;
|
||||
ExpressionValue<ExpressionType> CloneType(const ExpressionValue<ExpressionType>& exprType) override;
|
||||
|
||||
ExpressionPtr Clone(AccessIdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(AccessIndexExpression& node) override;
|
||||
@@ -84,50 +88,58 @@ namespace Nz::ShaderAst
|
||||
StatementPtr Clone(ForStatement& node) override;
|
||||
StatementPtr Clone(ForEachStatement& node) override;
|
||||
StatementPtr Clone(MultiStatement& node) override;
|
||||
StatementPtr Clone(ScopedStatement& node) override;
|
||||
StatementPtr Clone(WhileStatement& node) override;
|
||||
|
||||
const Identifier* FindIdentifier(const std::string_view& identifierName) const;
|
||||
template<typename F> const Identifier* FindIdentifier(const std::string_view& identifierName, F&& functor) const;
|
||||
TypeParameter FindTypeParameter(const std::string_view& identifierName) const;
|
||||
|
||||
Expression& MandatoryExpr(const ExpressionPtr& node);
|
||||
Statement& MandatoryStatement(const StatementPtr& node);
|
||||
void TypeMustMatch(const ExpressionPtr& left, const ExpressionPtr& right);
|
||||
void TypeMustMatch(const ExpressionType& left, const ExpressionType& right);
|
||||
Expression& MandatoryExpr(const ExpressionPtr& node) const;
|
||||
Statement& MandatoryStatement(const StatementPtr& node) const;
|
||||
|
||||
void PushScope();
|
||||
void PopScope();
|
||||
|
||||
ExpressionPtr CacheResult(ExpressionPtr expression);
|
||||
|
||||
template<typename T> const T& ComputeAttributeValue(AttributeValue<T>& attribute);
|
||||
ConstantValue ComputeConstantValue(Expression& expr);
|
||||
template<typename T> std::unique_ptr<T> Optimize(T& node);
|
||||
|
||||
std::size_t DeclareFunction(DeclareFunctionStatement& funcDecl);
|
||||
ConstantValue ComputeConstantValue(Expression& expr) const;
|
||||
template<typename T> const T& ComputeExprValue(ExpressionValue<T>& attribute) const;
|
||||
template<typename T> std::unique_ptr<T> Optimize(T& node) const;
|
||||
|
||||
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
|
||||
|
||||
void RegisterBuiltin();
|
||||
std::size_t RegisterConstant(std::string name, ConstantValue value);
|
||||
FunctionData& RegisterFunction(std::size_t functionIndex);
|
||||
std::size_t RegisterFunction(std::string name, FunctionData funcData);
|
||||
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type);
|
||||
std::size_t RegisterStruct(std::string name, StructDescription* description);
|
||||
std::size_t RegisterType(std::string name, ExpressionType expressionType);
|
||||
std::size_t RegisterType(std::string name, PartialType partialType);
|
||||
std::size_t RegisterVariable(std::string name, ExpressionType type);
|
||||
|
||||
void ResolveFunctions();
|
||||
|
||||
const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node);
|
||||
std::size_t ResolveStruct(const ExpressionType& exprType);
|
||||
std::size_t ResolveStruct(const IdentifierType& identifierType);
|
||||
std::size_t ResolveStruct(const StructType& structType);
|
||||
std::size_t ResolveStruct(const UniformType& uniformType);
|
||||
ExpressionType ResolveType(const ExpressionType& exprType);
|
||||
ExpressionType ResolveType(const ExpressionValue<ExpressionType>& exprTypeValue);
|
||||
|
||||
void SanitizeIdentifier(std::string& identifier);
|
||||
|
||||
void TypeMustMatch(const ExpressionPtr& left, const ExpressionPtr& right) const;
|
||||
void TypeMustMatch(const ExpressionType& left, const ExpressionType& right) const;
|
||||
|
||||
StatementPtr Unscope(StatementPtr node);
|
||||
|
||||
void Validate(WhileStatement& node);
|
||||
|
||||
void Validate(AccessIndexExpression& node);
|
||||
void Validate(AssignExpression& node);
|
||||
void Validate(BinaryExpression& node);
|
||||
void Validate(CallFunctionExpression& node, const DeclareFunctionStatement* referenceDeclaration);
|
||||
void Validate(CallFunctionExpression& node);
|
||||
void Validate(CastExpression& node);
|
||||
void Validate(DeclareVariableStatement& node);
|
||||
void Validate(IntrinsicExpression& node);
|
||||
@@ -141,7 +153,6 @@ namespace Nz::ShaderAst
|
||||
Bitset<> calledByFunctions;
|
||||
DeclareFunctionStatement* node;
|
||||
FunctionFlags flags;
|
||||
bool defined = false;
|
||||
};
|
||||
|
||||
struct Identifier
|
||||
@@ -153,6 +164,7 @@ namespace Nz::ShaderAst
|
||||
Function,
|
||||
Intrinsic,
|
||||
Struct,
|
||||
Type,
|
||||
Variable
|
||||
};
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_SHADER_API GlslWriter : public ShaderWriter, public ShaderAst::ExpressionVisitorExcept, public ShaderAst::StatementVisitorExcept
|
||||
class NAZARA_SHADER_API GlslWriter : public ShaderWriter, public ShaderAst::AstExpressionVisitorExcept, public ShaderAst::AstStatementVisitorExcept
|
||||
{
|
||||
public:
|
||||
using BindingMapping = std::unordered_map<UInt64 /* set | binding */, unsigned /*glBinding*/>;
|
||||
@@ -51,15 +51,20 @@ namespace Nz
|
||||
|
||||
private:
|
||||
void Append(const ShaderAst::ArrayType& type);
|
||||
void Append(const ShaderAst::ExpressionType& type);
|
||||
void Append(ShaderAst::BuiltinEntry builtin);
|
||||
void Append(const ShaderAst::ExpressionType& type);
|
||||
void Append(const ShaderAst::ExpressionValue<ShaderAst::ExpressionType>& type);
|
||||
void Append(const ShaderAst::FunctionType& functionType);
|
||||
void Append(const ShaderAst::IdentifierType& identifierType);
|
||||
void Append(const ShaderAst::IntrinsicFunctionType& intrinsicFunctionType);
|
||||
void Append(const ShaderAst::MatrixType& matrixType);
|
||||
void Append(const ShaderAst::MethodType& methodType);
|
||||
void Append(ShaderAst::MemoryLayout layout);
|
||||
void Append(ShaderAst::NoType);
|
||||
void Append(ShaderAst::PrimitiveType type);
|
||||
void Append(const ShaderAst::SamplerType& samplerType);
|
||||
void Append(const ShaderAst::StructType& structType);
|
||||
void Append(const ShaderAst::Type& type);
|
||||
void Append(const ShaderAst::UniformType& uniformType);
|
||||
void Append(const ShaderAst::VectorType& vecType);
|
||||
template<typename T> void Append(const T& param);
|
||||
@@ -81,6 +86,8 @@ namespace Nz
|
||||
void RegisterStruct(std::size_t structIndex, ShaderAst::StructDescription* desc);
|
||||
void RegisterVariable(std::size_t varIndex, std::string varName);
|
||||
|
||||
void ScopeVisit(ShaderAst::Statement& node);
|
||||
|
||||
void Visit(ShaderAst::ExpressionPtr& expr, bool encloseIfRequired = false);
|
||||
|
||||
void Visit(ShaderAst::AccessIdentifierExpression& node) override;
|
||||
@@ -107,6 +114,7 @@ namespace Nz
|
||||
void Visit(ShaderAst::MultiStatement& node) override;
|
||||
void Visit(ShaderAst::NoOpStatement& node) override;
|
||||
void Visit(ShaderAst::ReturnStatement& node) override;
|
||||
void Visit(ShaderAst::ScopedStatement& node) override;
|
||||
void Visit(ShaderAst::WhileStatement& node) override;
|
||||
|
||||
static bool HasExplicitBinding(ShaderAst::StatementPtr& shader);
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_SHADER_API LangWriter : public ShaderWriter, public ShaderAst::ExpressionVisitorExcept, public ShaderAst::StatementVisitorExcept
|
||||
class NAZARA_SHADER_API LangWriter : public ShaderWriter, public ShaderAst::AstExpressionVisitorExcept, public ShaderAst::AstStatementVisitorExcept
|
||||
{
|
||||
public:
|
||||
struct Environment;
|
||||
@@ -49,12 +49,17 @@ namespace Nz
|
||||
|
||||
void Append(const ShaderAst::ArrayType& type);
|
||||
void Append(const ShaderAst::ExpressionType& type);
|
||||
void Append(const ShaderAst::ExpressionValue<ShaderAst::ExpressionType>& type);
|
||||
void Append(const ShaderAst::FunctionType& functionType);
|
||||
void Append(const ShaderAst::IdentifierType& identifierType);
|
||||
void Append(const ShaderAst::IntrinsicFunctionType& intrinsicFunctionType);
|
||||
void Append(const ShaderAst::MatrixType& matrixType);
|
||||
void Append(const ShaderAst::MethodType& methodType);
|
||||
void Append(ShaderAst::NoType);
|
||||
void Append(ShaderAst::PrimitiveType type);
|
||||
void Append(const ShaderAst::SamplerType& samplerType);
|
||||
void Append(const ShaderAst::StructType& structType);
|
||||
void Append(const ShaderAst::Type& type);
|
||||
void Append(const ShaderAst::UniformType& uniformType);
|
||||
void Append(const ShaderAst::VectorType& vecType);
|
||||
template<typename T> void Append(const T& param);
|
||||
@@ -84,6 +89,8 @@ namespace Nz
|
||||
void RegisterStruct(std::size_t structIndex, ShaderAst::StructDescription* desc);
|
||||
void RegisterVariable(std::size_t varIndex, std::string varName);
|
||||
|
||||
void ScopeVisit(ShaderAst::Statement& node);
|
||||
|
||||
void Visit(ShaderAst::ExpressionPtr& expr, bool encloseIfRequired = false);
|
||||
|
||||
void Visit(ShaderAst::AccessIdentifierExpression& node) override;
|
||||
@@ -114,6 +121,7 @@ namespace Nz
|
||||
void Visit(ShaderAst::MultiStatement& node) override;
|
||||
void Visit(ShaderAst::NoOpStatement& node) override;
|
||||
void Visit(ShaderAst::ReturnStatement& node) override;
|
||||
void Visit(ShaderAst::ScopedStatement& node) override;
|
||||
void Visit(ShaderAst::WhileStatement& node) override;
|
||||
|
||||
struct State;
|
||||
|
||||
@@ -50,13 +50,14 @@ namespace Nz::ShaderBuilder
|
||||
struct CallFunction
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::CallFunctionExpression> operator()(std::string functionName, std::vector<ShaderAst::ExpressionPtr> parameters) const;
|
||||
inline std::unique_ptr<ShaderAst::CallFunctionExpression> operator()(ShaderAst::ExpressionPtr functionExpr, std::vector<ShaderAst::ExpressionPtr> parameters) const;
|
||||
};
|
||||
|
||||
struct Cast
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, ShaderAst::ExpressionPtr expression) const;
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const;
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const;
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, ShaderAst::ExpressionPtr expression) const;
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const;
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const;
|
||||
};
|
||||
|
||||
struct ConditionalExpression
|
||||
@@ -78,20 +79,20 @@ namespace Nz::ShaderBuilder
|
||||
struct DeclareConst
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareConstStatement> operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareConstStatement> operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareConstStatement> operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
};
|
||||
|
||||
struct DeclareFunction
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::string name, ShaderAst::StatementPtr statement) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType = ShaderAst::NoType{}) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> returnType = ShaderAst::ExpressionType{ ShaderAst::NoType{} }) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::optional<ShaderStageType> entryStage, std::string name, ShaderAst::StatementPtr statement) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::optional<ShaderStageType> entryStage, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType = ShaderAst::NoType{}) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::optional<ShaderStageType> entryStage, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> returnType = ShaderAst::ExpressionType{ ShaderAst::NoType{} }) const;
|
||||
};
|
||||
|
||||
struct DeclareOption
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
};
|
||||
|
||||
struct DeclareStruct
|
||||
@@ -102,7 +103,7 @@ namespace Nz::ShaderBuilder
|
||||
struct DeclareVariable
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
};
|
||||
|
||||
struct ExpressionStatement
|
||||
@@ -147,6 +148,11 @@ namespace Nz::ShaderBuilder
|
||||
inline std::unique_ptr<ShaderAst::ReturnStatement> operator()(ShaderAst::ExpressionPtr expr = nullptr) const;
|
||||
};
|
||||
|
||||
struct Scoped
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::ScopedStatement> operator()(ShaderAst::StatementPtr statement) const;
|
||||
};
|
||||
|
||||
struct Swizzle
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::SwizzleExpression> operator()(ShaderAst::ExpressionPtr expression, std::array<UInt32, 4> swizzleComponents, std::size_t componentCount) const;
|
||||
@@ -194,6 +200,7 @@ namespace Nz::ShaderBuilder
|
||||
constexpr Impl::Multi MultiStatement;
|
||||
constexpr Impl::NoParam<ShaderAst::NoOpStatement> NoOp;
|
||||
constexpr Impl::Return Return;
|
||||
constexpr Impl::Scoped Scoped;
|
||||
constexpr Impl::Swizzle Swizzle;
|
||||
constexpr Impl::Unary Unary;
|
||||
constexpr Impl::Variable Variable;
|
||||
|
||||
@@ -105,13 +105,22 @@ namespace Nz::ShaderBuilder
|
||||
inline std::unique_ptr<ShaderAst::CallFunctionExpression> Impl::CallFunction::operator()(std::string functionName, std::vector<ShaderAst::ExpressionPtr> parameters) const
|
||||
{
|
||||
auto callFunctionExpression = std::make_unique<ShaderAst::CallFunctionExpression>();
|
||||
callFunctionExpression->targetFunction = std::move(functionName);
|
||||
callFunctionExpression->targetFunction = ShaderBuilder::Identifier(std::move(functionName));
|
||||
callFunctionExpression->parameters = std::move(parameters);
|
||||
|
||||
return callFunctionExpression;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, ShaderAst::ExpressionPtr expression) const
|
||||
inline std::unique_ptr<ShaderAst::CallFunctionExpression> Impl::CallFunction::operator()(ShaderAst::ExpressionPtr functionExpr, std::vector<ShaderAst::ExpressionPtr> parameters) const
|
||||
{
|
||||
auto callFunctionExpression = std::make_unique<ShaderAst::CallFunctionExpression>();
|
||||
callFunctionExpression->targetFunction = std::move(functionExpr);
|
||||
callFunctionExpression->parameters = std::move(parameters);
|
||||
|
||||
return callFunctionExpression;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, ShaderAst::ExpressionPtr expression) const
|
||||
{
|
||||
auto castNode = std::make_unique<ShaderAst::CastExpression>();
|
||||
castNode->targetType = std::move(targetType);
|
||||
@@ -120,7 +129,7 @@ namespace Nz::ShaderBuilder
|
||||
return castNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const
|
||||
{
|
||||
auto castNode = std::make_unique<ShaderAst::CastExpression>();
|
||||
castNode->expressions = std::move(expressions);
|
||||
@@ -129,7 +138,7 @@ namespace Nz::ShaderBuilder
|
||||
return castNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionValue<ShaderAst::ExpressionType> targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const
|
||||
{
|
||||
auto castNode = std::make_unique<ShaderAst::CastExpression>();
|
||||
castNode->targetType = std::move(targetType);
|
||||
@@ -194,7 +203,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareConstNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareConstStatement> Impl::DeclareConst::operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareConstStatement> Impl::DeclareConst::operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareConstNode = std::make_unique<ShaderAst::DeclareConstStatement>();
|
||||
declareConstNode->name = std::move(name);
|
||||
@@ -213,7 +222,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareFunctionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> returnType) const
|
||||
{
|
||||
auto declareFunctionNode = std::make_unique<ShaderAst::DeclareFunctionStatement>();
|
||||
declareFunctionNode->name = std::move(name);
|
||||
@@ -236,7 +245,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareFunctionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::optional<ShaderStageType> entryStage, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::optional<ShaderStageType> entryStage, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> returnType) const
|
||||
{
|
||||
auto declareFunctionNode = std::make_unique<ShaderAst::DeclareFunctionStatement>();
|
||||
declareFunctionNode->name = std::move(name);
|
||||
@@ -250,7 +259,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareFunctionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> Impl::DeclareOption::operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> Impl::DeclareOption::operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareOptionNode = std::make_unique<ShaderAst::DeclareOptionStatement>();
|
||||
declareOptionNode->optName = std::move(name);
|
||||
@@ -277,7 +286,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareVariableNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Impl::DeclareVariable::operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Impl::DeclareVariable::operator()(std::string name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType> type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareVariableNode = std::make_unique<ShaderAst::DeclareVariableStatement>();
|
||||
declareVariableNode->varName = std::move(name);
|
||||
@@ -367,6 +376,14 @@ namespace Nz::ShaderBuilder
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::ScopedStatement> Impl::Scoped::operator()(ShaderAst::StatementPtr statement) const
|
||||
{
|
||||
auto scopedNode = std::make_unique<ShaderAst::ScopedStatement>();
|
||||
scopedNode->statement = std::move(statement);
|
||||
|
||||
return scopedNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::SwizzleExpression> Impl::Swizzle::operator()(ShaderAst::ExpressionPtr expression, std::array<UInt32, 4> swizzleComponents, std::size_t componentCount) const
|
||||
{
|
||||
assert(componentCount > 0);
|
||||
|
||||
@@ -70,36 +70,31 @@ namespace Nz::ShaderLang
|
||||
// Flow control
|
||||
const Token& Advance();
|
||||
void Consume(std::size_t count = 1);
|
||||
std::optional<ShaderAst::ExpressionType> DecodeType(const std::string& identifier);
|
||||
void EnterScope();
|
||||
const Token& Expect(const Token& token, TokenType type);
|
||||
const Token& ExpectNot(const Token& token, TokenType type);
|
||||
const Token& Expect(TokenType type);
|
||||
void LeaveScope();
|
||||
bool IsVariableInScope(const std::string_view& identifier) const;
|
||||
void RegisterVariable(std::string identifier);
|
||||
const Token& Peek(std::size_t advance = 0);
|
||||
|
||||
std::vector<ShaderAst::Attribute> ParseAttributes();
|
||||
void ParseVariableDeclaration(std::string& name, ShaderAst::ExpressionType& type, ShaderAst::ExpressionPtr& initialValue);
|
||||
std::vector<ShaderAst::ExprValue> ParseAttributes();
|
||||
void ParseVariableDeclaration(std::string& name, ShaderAst::ExpressionValue<ShaderAst::ExpressionType>& type, ShaderAst::ExpressionPtr& initialValue);
|
||||
|
||||
// Statements
|
||||
ShaderAst::StatementPtr ParseBranchStatement();
|
||||
ShaderAst::StatementPtr ParseConstStatement();
|
||||
ShaderAst::StatementPtr ParseDiscardStatement();
|
||||
ShaderAst::StatementPtr ParseExternalBlock(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::StatementPtr ParseForDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::StatementPtr ParseExternalBlock(std::vector<ShaderAst::ExprValue> attributes = {});
|
||||
ShaderAst::StatementPtr ParseForDeclaration(std::vector<ShaderAst::ExprValue> attributes = {});
|
||||
std::vector<ShaderAst::StatementPtr> ParseFunctionBody();
|
||||
ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector<ShaderAst::ExprValue> attributes = {});
|
||||
ShaderAst::DeclareFunctionStatement::Parameter ParseFunctionParameter();
|
||||
ShaderAst::StatementPtr ParseOptionDeclaration();
|
||||
ShaderAst::StatementPtr ParseReturnStatement();
|
||||
ShaderAst::StatementPtr ParseSingleStatement();
|
||||
ShaderAst::StatementPtr ParseStatement();
|
||||
std::vector<ShaderAst::StatementPtr> ParseStatementList();
|
||||
ShaderAst::StatementPtr ParseStructDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::StatementPtr ParseStructDeclaration(std::vector<ShaderAst::ExprValue> attributes = {});
|
||||
ShaderAst::StatementPtr ParseVariableDeclaration();
|
||||
ShaderAst::StatementPtr ParseWhileStatement(std::vector<ShaderAst::Attribute> attributes);
|
||||
ShaderAst::StatementPtr ParseWhileStatement(std::vector<ShaderAst::ExprValue> attributes);
|
||||
|
||||
// Expressions
|
||||
ShaderAst::ExpressionPtr ParseBinOpRhs(int exprPrecedence, ShaderAst::ExpressionPtr lhs);
|
||||
@@ -115,8 +110,7 @@ namespace Nz::ShaderLang
|
||||
|
||||
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
|
||||
const std::string& ParseIdentifierAsName();
|
||||
ShaderAst::PrimitiveType ParsePrimitiveType();
|
||||
ShaderAst::ExpressionType ParseType();
|
||||
ShaderAst::ExpressionPtr ParseType();
|
||||
|
||||
static int GetTokenPrecedence(TokenType token);
|
||||
|
||||
@@ -124,8 +118,6 @@ namespace Nz::ShaderLang
|
||||
{
|
||||
std::size_t tokenCount;
|
||||
std::size_t tokenIndex = 0;
|
||||
std::vector<std::size_t> scopeSizes;
|
||||
std::vector<std::string> identifiersInScope;
|
||||
std::unique_ptr<ShaderAst::MultiStatement> root;
|
||||
const Token* tokens;
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Nz
|
||||
{
|
||||
class SpirvWriter;
|
||||
|
||||
class NAZARA_SHADER_API SpirvAstVisitor : public ShaderAst::ExpressionVisitorExcept, public ShaderAst::StatementVisitorExcept
|
||||
class NAZARA_SHADER_API SpirvAstVisitor : public ShaderAst::AstExpressionVisitorExcept, public ShaderAst::AstStatementVisitorExcept
|
||||
{
|
||||
public:
|
||||
struct EntryPoint;
|
||||
@@ -38,8 +38,8 @@ namespace Nz
|
||||
|
||||
const Variable& GetVariable(std::size_t varIndex) const;
|
||||
|
||||
using ExpressionVisitorExcept::Visit;
|
||||
using StatementVisitorExcept::Visit;
|
||||
using AstExpressionVisitorExcept::Visit;
|
||||
using AstStatementVisitorExcept::Visit;
|
||||
|
||||
void Visit(ShaderAst::AccessIndexExpression& node) override;
|
||||
void Visit(ShaderAst::AssignExpression& node) override;
|
||||
@@ -60,6 +60,7 @@ namespace Nz
|
||||
void Visit(ShaderAst::MultiStatement& node) override;
|
||||
void Visit(ShaderAst::NoOpStatement& node) override;
|
||||
void Visit(ShaderAst::ReturnStatement& node) override;
|
||||
void Visit(ShaderAst::ScopedStatement& node) override;
|
||||
void Visit(ShaderAst::SwizzleExpression& node) override;
|
||||
void Visit(ShaderAst::UnaryExpression& node) override;
|
||||
void Visit(ShaderAst::VariableExpression& node) override;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Nz
|
||||
class SpirvBlock;
|
||||
class SpirvWriter;
|
||||
|
||||
class NAZARA_SHADER_API SpirvExpressionLoad : public ShaderAst::ExpressionVisitorExcept
|
||||
class NAZARA_SHADER_API SpirvExpressionLoad : public ShaderAst::AstExpressionVisitorExcept
|
||||
{
|
||||
public:
|
||||
inline SpirvExpressionLoad(SpirvWriter& writer, SpirvAstVisitor& visitor, SpirvBlock& block);
|
||||
@@ -29,7 +29,7 @@ namespace Nz
|
||||
|
||||
UInt32 Evaluate(ShaderAst::Expression& node);
|
||||
|
||||
using ExpressionVisitorExcept::Visit;
|
||||
using AstExpressionVisitorExcept::Visit;
|
||||
void Visit(ShaderAst::AccessIndexExpression& node) override;
|
||||
void Visit(ShaderAst::VariableExpression& node) override;
|
||||
|
||||
@@ -37,6 +37,15 @@ namespace Nz
|
||||
SpirvExpressionLoad& operator=(SpirvExpressionLoad&&) = delete;
|
||||
|
||||
private:
|
||||
struct PointerChainAccess
|
||||
{
|
||||
std::vector<UInt32> indices;
|
||||
const ShaderAst::ExpressionType* exprType;
|
||||
SpirvStorageClass storage;
|
||||
UInt32 pointerId;
|
||||
UInt32 pointedTypeId;
|
||||
};
|
||||
|
||||
struct Pointer
|
||||
{
|
||||
SpirvStorageClass storage;
|
||||
@@ -46,13 +55,20 @@ namespace Nz
|
||||
|
||||
struct Value
|
||||
{
|
||||
UInt32 resultId;
|
||||
UInt32 valueId;
|
||||
};
|
||||
|
||||
struct ValueExtraction
|
||||
{
|
||||
std::vector<UInt32> indices;
|
||||
UInt32 typeId;
|
||||
UInt32 valueId;
|
||||
};
|
||||
|
||||
SpirvAstVisitor& m_visitor;
|
||||
SpirvBlock& m_block;
|
||||
SpirvWriter& m_writer;
|
||||
std::variant<std::monostate, Pointer, Value> m_value;
|
||||
std::variant<std::monostate, ValueExtraction, Pointer, PointerChainAccess, Value> m_value;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Nz
|
||||
class SpirvBlock;
|
||||
class SpirvWriter;
|
||||
|
||||
class NAZARA_SHADER_API SpirvExpressionStore : public ShaderAst::ExpressionVisitorExcept
|
||||
class NAZARA_SHADER_API SpirvExpressionStore : public ShaderAst::AstExpressionVisitorExcept
|
||||
{
|
||||
public:
|
||||
inline SpirvExpressionStore(SpirvWriter& writer, SpirvAstVisitor& visitor, SpirvBlock& block);
|
||||
@@ -29,7 +29,7 @@ namespace Nz
|
||||
|
||||
void Store(ShaderAst::ExpressionPtr& node, UInt32 resultId);
|
||||
|
||||
using ExpressionVisitorExcept::Visit;
|
||||
using AstExpressionVisitorExcept::Visit;
|
||||
void Visit(ShaderAst::AccessIndexExpression& node) override;
|
||||
void Visit(ShaderAst::SwizzleExpression& node) override;
|
||||
void Visit(ShaderAst::VariableExpression& node) override;
|
||||
|
||||
Reference in New Issue
Block a user