ShaderLang: Proof of concept (add support for a lot of things)

This commit is contained in:
Jérôme Leclercq
2021-03-31 10:21:35 +02:00
parent 2a73005295
commit c1d1838336
37 changed files with 2259 additions and 908 deletions

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADERAST_ATTRIBUTES_HPP
#define NAZARA_SHADERAST_ATTRIBUTES_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
namespace Nz::ShaderAst
{
struct Attribute
{
using Param = std::variant<std::monostate, long long, std::string>;
AttributeType type;
Param args;
};
}
#endif

View File

@@ -0,0 +1,96 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_AST_EXPRESSIONTYPE_HPP
#define NAZARA_SHADER_AST_EXPRESSIONTYPE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <string>
#include <variant>
#include <vector>
namespace Nz::ShaderAst
{
struct IdentifierType //< Alias or struct
{
std::string name;
inline bool operator==(const IdentifierType& rhs) const;
inline bool operator!=(const IdentifierType& rhs) const;
};
struct MatrixType
{
std::size_t columnCount;
std::size_t rowCount;
PrimitiveType type;
inline bool operator==(const MatrixType& rhs) const;
inline bool operator!=(const MatrixType& rhs) const;
};
struct NoType
{
inline bool operator==(const NoType& rhs) const;
inline bool operator!=(const NoType& rhs) const;
};
struct SamplerType
{
ImageType dim;
PrimitiveType sampledType;
inline bool operator==(const SamplerType& rhs) const;
inline bool operator!=(const SamplerType& rhs) const;
};
struct UniformType
{
IdentifierType containedType;
inline bool operator==(const UniformType& rhs) const;
inline bool operator!=(const UniformType& rhs) const;
};
struct VectorType
{
std::size_t componentCount;
PrimitiveType type;
inline bool operator==(const VectorType& rhs) const;
inline bool operator!=(const VectorType& rhs) const;
};
using ExpressionType = std::variant<NoType, IdentifierType, PrimitiveType, MatrixType, SamplerType, UniformType, VectorType>;
struct StructDescription
{
struct StructMember
{
std::string name;
std::vector<Attribute> attributes;
ExpressionType type;
};
std::string name;
std::vector<StructMember> members;
};
inline bool IsIdentifierType(const ExpressionType& type);
inline bool IsMatrixType(const ExpressionType& type);
inline bool IsNoType(const ExpressionType& type);
inline bool IsPrimitiveType(const ExpressionType& type);
inline bool IsSamplerType(const ExpressionType& type);
inline bool IsUniformType(const ExpressionType& type);
inline bool IsVectorType(const ExpressionType& type);
}
#include <Nazara/Shader/Ast/ExpressionType.inl>
#endif

View File

@@ -0,0 +1,111 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/Ast/ExpressionType.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
inline bool IdentifierType::operator==(const IdentifierType& rhs) const
{
return name == rhs.name;
}
inline bool IdentifierType::operator!=(const IdentifierType& rhs) const
{
return !operator==(rhs);
}
inline bool MatrixType::operator==(const MatrixType& rhs) const
{
return columnCount == rhs.columnCount && rowCount == rhs.rowCount && type == rhs.type;
}
inline bool MatrixType::operator!=(const MatrixType& rhs) const
{
return !operator==(rhs);
}
inline bool NoType::operator==(const NoType& /*rhs*/) const
{
return true;
}
inline bool NoType::operator!=(const NoType& /*rhs*/) const
{
return false;
}
inline bool SamplerType::operator==(const SamplerType& rhs) const
{
return dim == rhs.dim && sampledType == rhs.sampledType;
}
inline bool SamplerType::operator!=(const SamplerType& rhs) const
{
return !operator==(rhs);
}
inline bool UniformType::operator==(const UniformType& rhs) const
{
return containedType == rhs.containedType;
}
inline bool UniformType::operator!=(const UniformType& rhs) const
{
return !operator==(rhs);
}
inline bool VectorType::operator==(const VectorType& rhs) const
{
return componentCount == rhs.componentCount && type == rhs.type;
}
inline bool VectorType::operator!=(const VectorType& rhs) const
{
return !operator==(rhs);
}
inline bool IsIdentifierType(const ExpressionType& type)
{
return std::holds_alternative<IdentifierType>(type);
}
inline bool IsMatrixType(const ExpressionType& type)
{
return std::holds_alternative<MatrixType>(type);
}
inline bool IsNoType(const ExpressionType& type)
{
return std::holds_alternative<NoType>(type);
}
inline bool IsPrimitiveType(const ExpressionType& type)
{
return std::holds_alternative<PrimitiveType>(type);
}
inline bool IsSamplerType(const ExpressionType& type)
{
return std::holds_alternative<SamplerType>(type);
}
bool IsUniformType(const ExpressionType& type)
{
return std::holds_alternative<UniformType>(type);
}
bool IsVectorType(const ExpressionType& type)
{
return std::holds_alternative<VectorType>(type);
}
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -28,7 +28,7 @@ namespace Nz
GlslWriter(GlslWriter&&) = delete;
~GlslWriter() = default;
std::string Generate(ShaderAst::StatementPtr& shader, const States& conditions = {});
std::string Generate(ShaderStageType shaderStage, ShaderAst::StatementPtr& shader, const States& conditions = {});
void SetEnv(Environment environment);
@@ -44,17 +44,26 @@ namespace Nz
static const char* GetFlipYUniformName();
private:
void Append(ShaderAst::ShaderExpressionType type);
void Append(const ShaderAst::ExpressionType& type);
void Append(ShaderAst::BuiltinEntry builtin);
void Append(ShaderAst::BasicType type);
void Append(const ShaderAst::IdentifierType& identifierType);
void Append(const ShaderAst::MatrixType& matrixType);
void Append(ShaderAst::MemoryLayout layout);
void Append(ShaderAst::NoType);
void Append(ShaderAst::PrimitiveType type);
void Append(const ShaderAst::SamplerType& samplerType);
void Append(const ShaderAst::UniformType& uniformType);
void Append(const ShaderAst::VectorType& vecType);
template<typename T> void Append(const T& param);
template<typename T1, typename T2, typename... Args> void Append(const T1& firstParam, const T2& secondParam, Args&&... params);
void AppendCommentSection(const std::string& section);
void AppendEntryPoint(ShaderStageType shaderStage);
void AppendField(std::size_t scopeId, const std::string& structName, const std::string* memberIdentifier, std::size_t remainingMembers);
void AppendLine(const std::string& txt = {});
template<typename... Args> void AppendLine(Args&&... params);
void EnterScope();
void LeaveScope();
void LeaveScope(bool skipLine = true);
void Visit(ShaderAst::ExpressionPtr& expr, bool encloseIfRequired = false);
@@ -70,7 +79,9 @@ namespace Nz
void Visit(ShaderAst::BranchStatement& node) override;
void Visit(ShaderAst::ConditionalStatement& node) override;
void Visit(ShaderAst::DeclareExternalStatement& node) override;
void Visit(ShaderAst::DeclareFunctionStatement& node) override;
void Visit(ShaderAst::DeclareStructStatement& node) override;
void Visit(ShaderAst::DeclareVariableStatement& node) override;
void Visit(ShaderAst::DiscardStatement& node) override;
void Visit(ShaderAst::ExpressionStatement& node) override;

View File

@@ -16,15 +16,22 @@ namespace Nz::ShaderAst
{
struct AstCache
{
struct Identifier;
struct Alias
{
std::variant<ExpressionType> value;
};
struct Variable
{
ShaderExpressionType type;
ExpressionType type;
};
struct Identifier
{
std::string name;
std::variant<Variable, StructDescription> value;
std::variant<Alias, Variable, StructDescription> value;
};
struct Scope
@@ -33,12 +40,12 @@ namespace Nz::ShaderAst
std::vector<Identifier> identifiers;
};
inline void Clear();
inline const Identifier* FindIdentifier(std::size_t startingScopeId, const std::string& identifierName) const;
inline std::size_t GetScopeId(const Node* node) const;
ShaderStageType stageType = ShaderStageType::Undefined;
std::array<DeclareFunctionStatement*, ShaderStageTypeCount> entryFunctions = {};
std::unordered_map<const Expression*, ShaderExpressionType> nodeExpressionType;
std::unordered_map<const Expression*, ExpressionType> nodeExpressionType;
std::unordered_map<const Node*, std::size_t> scopeIdByNode;
std::vector<Scope> scopes;
};

View File

@@ -7,6 +7,14 @@
namespace Nz::ShaderAst
{
inline void AstCache::Clear()
{
entryFunctions.fill(nullptr);
nodeExpressionType.clear();
scopeIdByNode.clear();
scopes.clear();
}
inline auto AstCache::FindIdentifier(std::size_t startingScopeId, const std::string& identifierName) const -> const Identifier*
{
assert(startingScopeId < scopes.size());
@@ -28,7 +36,7 @@ namespace Nz::ShaderAst
inline std::size_t AstCache::GetScopeId(const Node* node) const
{
auto it = scopeIdByNode.find(node);
assert(it == scopeIdByNode.end());
assert(it != scopeIdByNode.end());
return it->second;
}

View File

@@ -33,6 +33,8 @@ namespace Nz::ShaderAst
ExpressionPtr CloneExpression(ExpressionPtr& expr);
StatementPtr CloneStatement(StatementPtr& statement);
virtual std::unique_ptr<DeclareFunctionStatement> Clone(DeclareFunctionStatement& node);
using AstExpressionVisitor::Visit;
using AstStatementVisitor::Visit;
@@ -45,8 +47,10 @@ namespace Nz::ShaderAst
void Visit(IdentifierExpression& node) override;
void Visit(IntrinsicExpression& node) override;
void Visit(SwizzleExpression& node) override;
void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override;
void Visit(DeclareExternalStatement& node) override;
void Visit(DeclareFunctionStatement& node) override;
void Visit(DeclareStructStatement& node) override;
void Visit(DeclareVariableStatement& node) override;

View File

@@ -10,7 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderAstExpressionVisitor.hpp>
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <Nazara/Shader/Ast/ExpressionType.hpp>
#include <vector>
namespace Nz::ShaderAst
@@ -25,13 +25,14 @@ namespace Nz::ShaderAst
ExpressionTypeVisitor(ExpressionTypeVisitor&&) = delete;
~ExpressionTypeVisitor() = default;
ShaderExpressionType GetExpressionType(Expression& expression, AstCache* cache);
ExpressionType GetExpressionType(Expression& expression, AstCache* cache);
ExpressionTypeVisitor& operator=(const ExpressionTypeVisitor&) = delete;
ExpressionTypeVisitor& operator=(ExpressionTypeVisitor&&) = delete;
private:
ShaderExpressionType GetExpressionTypeInternal(Expression& expression);
ExpressionType GetExpressionTypeInternal(Expression& expression);
ExpressionType ResolveAlias(Expression& expression, ExpressionType expressionType);
void Visit(Expression& expression);
@@ -46,10 +47,10 @@ namespace Nz::ShaderAst
void Visit(SwizzleExpression& node) override;
AstCache* m_cache;
std::optional<ShaderExpressionType> m_lastExpressionType;
std::optional<ExpressionType> m_lastExpressionType;
};
inline ShaderExpressionType GetExpressionType(Expression& expression, AstCache* cache = nullptr);
inline ExpressionType GetExpressionType(Expression& expression, AstCache* cache = nullptr);
}
#include <Nazara/Shader/ShaderAstExpressionType.inl>

View File

@@ -7,7 +7,7 @@
namespace Nz::ShaderAst
{
inline ShaderExpressionType GetExpressionType(Expression& expression, AstCache* cache)
inline ExpressionType GetExpressionType(Expression& expression, AstCache* cache)
{
ExpressionTypeVisitor visitor;
return visitor.GetExpressionType(expression, cache);

View File

@@ -37,6 +37,7 @@ NAZARA_SHADERAST_EXPRESSION(IntrinsicExpression)
NAZARA_SHADERAST_EXPRESSION(SwizzleExpression)
NAZARA_SHADERAST_STATEMENT(BranchStatement)
NAZARA_SHADERAST_STATEMENT(ConditionalStatement)
NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement)
NAZARA_SHADERAST_STATEMENT(DeclareFunctionStatement)
NAZARA_SHADERAST_STATEMENT(DeclareStructStatement)
NAZARA_SHADERAST_STATEMENT(DeclareVariableStatement)

View File

@@ -32,6 +32,7 @@ namespace Nz::ShaderAst
void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override;
void Visit(DeclareExternalStatement& node) override;
void Visit(DeclareFunctionStatement& node) override;
void Visit(DeclareStructStatement& node) override;
void Visit(DeclareVariableStatement& node) override;

View File

@@ -35,6 +35,7 @@ namespace Nz::ShaderAst
void Serialize(BranchStatement& node);
void Serialize(ConditionalStatement& node);
void Serialize(DeclareExternalStatement& node);
void Serialize(DeclareFunctionStatement& node);
void Serialize(DeclareStructStatement& node);
void Serialize(DeclareVariableStatement& node);
@@ -45,6 +46,7 @@ namespace Nz::ShaderAst
void Serialize(ReturnStatement& node);
protected:
void Attributes(std::vector<Attribute>& attributes);
template<typename T> void Container(T& container);
template<typename T> void Enum(T& enumVal);
template<typename T> void OptEnum(std::optional<T>& optVal);
@@ -55,7 +57,7 @@ namespace Nz::ShaderAst
virtual void Node(ExpressionPtr& node) = 0;
virtual void Node(StatementPtr& node) = 0;
virtual void Type(ShaderExpressionType& type) = 0;
virtual void Type(ExpressionType& type) = 0;
virtual void Value(bool& val) = 0;
virtual void Value(float& val) = 0;
@@ -86,7 +88,7 @@ namespace Nz::ShaderAst
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
void Type(ShaderExpressionType& type) override;
void Type(ExpressionType& type) override;
void Value(bool& val) override;
void Value(float& val) override;
void Value(std::string& val) override;
@@ -117,7 +119,7 @@ namespace Nz::ShaderAst
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
void Type(ShaderExpressionType& type) override;
void Type(ExpressionType& type) override;
void Value(bool& val) override;
void Value(float& val) override;
void Value(std::string& val) override;

View File

@@ -1,40 +0,0 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_ASTTYPES_HPP
#define NAZARA_SHADER_ASTTYPES_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <string>
#include <variant>
#include <vector>
namespace Nz::ShaderAst
{
using ShaderExpressionType = std::variant<BasicType, std::string>;
struct StructDescription
{
struct StructMember
{
std::string name;
ShaderExpressionType type;
};
std::string name;
std::vector<StructMember> members;
};
inline bool IsBasicType(const ShaderExpressionType& type);
inline bool IsMatrixType(const ShaderExpressionType& type);
inline bool IsSamplerType(const ShaderExpressionType& type);
inline bool IsStructType(const ShaderExpressionType& type);
}
#include <Nazara/Shader/ShaderAstTypes.inl>
#endif // NAZARA_SHADER_ASTTYPES_HPP

View File

@@ -1,104 +0,0 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
inline bool IsBasicType(const ShaderExpressionType& type)
{
return std::visit([&](auto&& arg)
{
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, BasicType>)
return true;
else if constexpr (std::is_same_v<T, std::string>)
return false;
else
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
}, type);
}
inline bool IsMatrixType(const ShaderExpressionType& type)
{
if (!IsBasicType(type))
return false;
switch (std::get<BasicType>(type))
{
case BasicType::Mat4x4:
return true;
case BasicType::Boolean:
case BasicType::Float1:
case BasicType::Float2:
case BasicType::Float3:
case BasicType::Float4:
case BasicType::Int1:
case BasicType::Int2:
case BasicType::Int3:
case BasicType::Int4:
case BasicType::Sampler2D:
case BasicType::Void:
case BasicType::UInt1:
case BasicType::UInt2:
case BasicType::UInt3:
case BasicType::UInt4:
return false;
}
return false;
}
inline bool IsSamplerType(const ShaderExpressionType& type)
{
if (!IsBasicType(type))
return false;
switch (std::get<BasicType>(type))
{
case BasicType::Sampler2D:
return true;
case BasicType::Boolean:
case BasicType::Float1:
case BasicType::Float2:
case BasicType::Float3:
case BasicType::Float4:
case BasicType::Int1:
case BasicType::Int2:
case BasicType::Int3:
case BasicType::Int4:
case BasicType::Mat4x4:
case BasicType::Void:
case BasicType::UInt1:
case BasicType::UInt2:
case BasicType::UInt3:
case BasicType::UInt4:
return false;
}
return false;
}
inline bool IsStructType(const ShaderExpressionType& type)
{
return std::visit([&](auto&& arg)
{
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, BasicType>)
return false;
else if constexpr (std::is_same_v<T, std::string>)
return true;
else
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
}, type);
}
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -29,14 +29,14 @@ namespace Nz::ShaderAst
Expression& MandatoryExpr(ExpressionPtr& node);
Statement& MandatoryStatement(StatementPtr& node);
void TypeMustMatch(ExpressionPtr& left, ExpressionPtr& right);
void TypeMustMatch(const ShaderExpressionType& left, const ShaderExpressionType& right);
void TypeMustMatch(const ExpressionType& left, const ExpressionType& right);
ShaderExpressionType CheckField(const std::string& structName, const std::string* memberIdentifier, std::size_t remainingMembers);
ExpressionType CheckField(const std::string& structName, const std::string* memberIdentifier, std::size_t remainingMembers);
AstCache::Scope& EnterScope();
void ExitScope();
void RegisterExpressionType(Expression& node, ShaderExpressionType expressionType);
void RegisterExpressionType(Expression& node, ExpressionType expressionType);
void RegisterScope(Node& node);
void Visit(AccessMemberExpression& node) override;
@@ -51,6 +51,7 @@ namespace Nz::ShaderAst
void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override;
void Visit(DeclareExternalStatement& node) override;
void Visit(DeclareFunctionStatement& node) override;
void Visit(DeclareStructStatement& node) override;
void Visit(DeclareVariableStatement& node) override;

View File

@@ -15,6 +15,11 @@ namespace Nz::ShaderBuilder
{
namespace Impl
{
struct Assign
{
inline std::unique_ptr<ShaderAst::AssignExpression> operator()(ShaderAst::AssignType op, ShaderAst::ExpressionPtr left, ShaderAst::ExpressionPtr right) const;
};
struct Binary
{
inline std::unique_ptr<ShaderAst::BinaryExpression> operator()(ShaderAst::BinaryType op, ShaderAst::ExpressionPtr left, ShaderAst::ExpressionPtr right) const;
@@ -26,6 +31,11 @@ namespace Nz::ShaderBuilder
inline std::unique_ptr<ShaderAst::BranchStatement> operator()(std::vector<ShaderAst::BranchStatement::ConditionalStatement> condStatements, ShaderAst::StatementPtr elseStatement = nullptr) const;
};
struct Cast
{
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const;
};
struct Constant
{
inline std::unique_ptr<ShaderAst::ConstantExpression> operator()(ShaderConstantValue value) const;
@@ -33,13 +43,24 @@ namespace Nz::ShaderBuilder
struct DeclareFunction
{
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ShaderExpressionType returnType = ShaderAst::BasicType::Void) const;
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::vector<ShaderAst::Attribute> attributes, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ShaderExpressionType returnType = ShaderAst::BasicType::Void) 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::vector<ShaderAst::Attribute> attributes, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType = ShaderAst::NoType{}) const;
};
struct DeclareStruct
{
inline std::unique_ptr<ShaderAst::DeclareStructStatement> operator()(ShaderAst::StructDescription description) const;
inline std::unique_ptr<ShaderAst::DeclareStructStatement> operator()(std::vector<ShaderAst::Attribute> attributes, ShaderAst::StructDescription description) const;
};
struct DeclareVariable
{
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> operator()(std::string name, ShaderAst::ShaderExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
};
struct ExpressionStatement
{
inline std::unique_ptr<ShaderAst::ExpressionStatement> operator()(ShaderAst::ExpressionPtr expression) const;
};
struct Identifier
@@ -47,9 +68,9 @@ namespace Nz::ShaderBuilder
inline std::unique_ptr<ShaderAst::IdentifierExpression> operator()(std::string name) const;
};
struct Return
struct Intrinsic
{
inline std::unique_ptr<ShaderAst::ReturnStatement> operator()(ShaderAst::ExpressionPtr expr = nullptr) const;
inline std::unique_ptr<ShaderAst::IntrinsicExpression> operator()(ShaderAst::IntrinsicType intrinsicType, std::vector<ShaderAst::ExpressionPtr> parameters) const;
};
template<typename T>
@@ -57,15 +78,25 @@ namespace Nz::ShaderBuilder
{
std::unique_ptr<T> operator()() const;
};
struct Return
{
inline std::unique_ptr<ShaderAst::ReturnStatement> operator()(ShaderAst::ExpressionPtr expr = nullptr) const;
};
}
constexpr Impl::Assign Assign;
constexpr Impl::Binary Binary;
constexpr Impl::Branch Branch;
constexpr Impl::Cast Cast;
constexpr Impl::Constant Constant;
constexpr Impl::DeclareFunction DeclareFunction;
constexpr Impl::DeclareStruct DeclareStruct;
constexpr Impl::DeclareVariable DeclareVariable;
constexpr Impl::ExpressionStatement ExpressionStatement;
constexpr Impl::NoParam<ShaderAst::DiscardStatement> Discard;
constexpr Impl::Identifier Identifier;
constexpr Impl::Intrinsic Intrinsic;
constexpr Impl::NoParam<ShaderAst::NoOpStatement> NoOp;
constexpr Impl::Return Return;
}

View File

@@ -7,14 +7,24 @@
namespace Nz::ShaderBuilder
{
inline std::unique_ptr<ShaderAst::AssignExpression> Impl::Assign::operator()(ShaderAst::AssignType op, ShaderAst::ExpressionPtr left, ShaderAst::ExpressionPtr right) const
{
auto assignNode = std::make_unique<ShaderAst::AssignExpression>();
assignNode->op = op;
assignNode->left = std::move(left);
assignNode->right = std::move(right);
return assignNode;
}
inline std::unique_ptr<ShaderAst::BinaryExpression> Impl::Binary::operator()(ShaderAst::BinaryType op, ShaderAst::ExpressionPtr left, ShaderAst::ExpressionPtr right) const
{
auto constantNode = std::make_unique<ShaderAst::BinaryExpression>();
constantNode->op = op;
constantNode->left = std::move(left);
constantNode->right = std::move(right);
auto binaryNode = std::make_unique<ShaderAst::BinaryExpression>();
binaryNode->op = op;
binaryNode->left = std::move(left);
binaryNode->right = std::move(right);
return constantNode;
return binaryNode;
}
inline std::unique_ptr<ShaderAst::BranchStatement> Impl::Branch::operator()(ShaderAst::ExpressionPtr condition, ShaderAst::StatementPtr truePath, ShaderAst::StatementPtr falsePath) const
@@ -39,6 +49,18 @@ namespace Nz::ShaderBuilder
return branchNode;
}
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const
{
auto castNode = std::make_unique<ShaderAst::CastExpression>();
castNode->targetType = std::move(targetType);
assert(expressions.size() <= castNode->expressions.size());
for (std::size_t i = 0; i < expressions.size(); ++i)
castNode->expressions[i] = std::move(expressions[i]);
return castNode;
}
inline std::unique_ptr<ShaderAst::ConstantExpression> Impl::Constant::operator()(ShaderConstantValue value) const
{
auto constantNode = std::make_unique<ShaderAst::ConstantExpression>();
@@ -47,7 +69,7 @@ namespace Nz::ShaderBuilder
return constantNode;
}
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ShaderExpressionType 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::ExpressionType returnType) const
{
auto declareFunctionNode = std::make_unique<ShaderAst::DeclareFunctionStatement>();
declareFunctionNode->name = std::move(name);
@@ -58,7 +80,7 @@ namespace Nz::ShaderBuilder
return declareFunctionNode;
}
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::vector<ShaderAst::Attribute> attributes, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ShaderExpressionType returnType) const
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> Impl::DeclareFunction::operator()(std::vector<ShaderAst::Attribute> attributes, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType) const
{
auto declareFunctionNode = std::make_unique<ShaderAst::DeclareFunctionStatement>();
declareFunctionNode->attributes = std::move(attributes);
@@ -70,7 +92,24 @@ namespace Nz::ShaderBuilder
return declareFunctionNode;
}
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Nz::ShaderBuilder::Impl::DeclareVariable::operator()(std::string name, ShaderAst::ShaderExpressionType type, ShaderAst::ExpressionPtr initialValue) const
inline std::unique_ptr<ShaderAst::DeclareStructStatement> Impl::DeclareStruct::operator()(ShaderAst::StructDescription description) const
{
auto declareStructNode = std::make_unique<ShaderAst::DeclareStructStatement>();
declareStructNode->description = std::move(description);
return declareStructNode;
}
inline std::unique_ptr<ShaderAst::DeclareStructStatement> Impl::DeclareStruct::operator()(std::vector<ShaderAst::Attribute> attributes, ShaderAst::StructDescription description) const
{
auto declareStructNode = std::make_unique<ShaderAst::DeclareStructStatement>();
declareStructNode->attributes = std::move(attributes);
declareStructNode->description = std::move(description);
return declareStructNode;
}
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Nz::ShaderBuilder::Impl::DeclareVariable::operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
{
auto declareVariableNode = std::make_unique<ShaderAst::DeclareVariableStatement>();
declareVariableNode->varName = std::move(name);
@@ -80,6 +119,14 @@ namespace Nz::ShaderBuilder
return declareVariableNode;
}
inline std::unique_ptr<ShaderAst::ExpressionStatement> Impl::ExpressionStatement::operator()(ShaderAst::ExpressionPtr expression) const
{
auto expressionStatementNode = std::make_unique<ShaderAst::ExpressionStatement>();
expressionStatementNode->expression = std::move(expression);
return expressionStatementNode;
}
inline std::unique_ptr<ShaderAst::IdentifierExpression> Impl::Identifier::operator()(std::string name) const
{
auto identifierNode = std::make_unique<ShaderAst::IdentifierExpression>();
@@ -88,6 +135,15 @@ namespace Nz::ShaderBuilder
return identifierNode;
}
inline std::unique_ptr<ShaderAst::IntrinsicExpression> Impl::Intrinsic::operator()(ShaderAst::IntrinsicType intrinsicType, std::vector<ShaderAst::ExpressionPtr> parameters) const
{
auto intrinsicExpression = std::make_unique<ShaderAst::IntrinsicExpression>();
intrinsicExpression->intrinsic = intrinsicType;
intrinsicExpression->parameters = std::move(parameters);
return intrinsicExpression;
}
inline std::unique_ptr<ShaderAst::ReturnStatement> Impl::Return::operator()(ShaderAst::ExpressionPtr expr) const
{
auto returnNode = std::make_unique<ShaderAst::ReturnStatement>();

View File

@@ -18,28 +18,19 @@ namespace Nz::ShaderAst
enum class AttributeType
{
Entry, //< Entry point (function only) - has argument type
Layout //< Struct layout (struct only) - has argument style
Binding, //< Binding (external var only) - has argument index
Builtin, //< Builtin (struct member only) - has argument type
Entry, //< Entry point (function only) - has argument type
Layout, //< Struct layout (struct only) - has argument style
Location //< Location (struct member only) - has argument index
};
enum class BasicType
enum class PrimitiveType
{
Boolean, //< bool
Float1, //< float
Float2, //< vec2
Float3, //< vec3
Float4, //< vec4
Int1, //< int
Int2, //< ivec2
Int3, //< ivec3
Int4, //< ivec4
Mat4x4, //< mat4
Sampler2D, //< sampler2D
Void, //< void
UInt1, //< uint
UInt2, //< uvec2
UInt3, //< uvec3
UInt4 //< uvec4
Boolean, //< bool
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
};
enum class BinaryType
@@ -71,7 +62,8 @@ namespace Nz::ShaderAst
enum class IntrinsicType
{
CrossProduct,
DotProduct
DotProduct,
SampleTexture
};
enum class MemoryLayout
@@ -107,9 +99,6 @@ namespace Nz::ShaderAst
ParameterVariable,
UniformVariable
};
inline std::size_t GetComponentCount(BasicType type);
inline BasicType GetComponentType(BasicType type);
}
#include <Nazara/Shader/ShaderEnums.inl>

View File

@@ -7,51 +7,6 @@
namespace Nz::ShaderAst
{
inline std::size_t GetComponentCount(BasicType type)
{
switch (type)
{
case BasicType::Float2:
case BasicType::Int2:
return 2;
case BasicType::Float3:
case BasicType::Int3:
return 3;
case BasicType::Float4:
case BasicType::Int4:
return 4;
case BasicType::Mat4x4:
return 4;
default:
return 1;
}
}
inline BasicType GetComponentType(BasicType type)
{
switch (type)
{
case BasicType::Float2:
case BasicType::Float3:
case BasicType::Float4:
return BasicType::Float1;
case BasicType::Int2:
case BasicType::Int3:
case BasicType::Int4:
return BasicType::Int1;
case BasicType::Mat4x4:
return BasicType::Float4;
default:
return type;
}
}
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -19,6 +19,12 @@ namespace Nz::ShaderLang
public:
using exception::exception;
};
class DuplicateIdentifier : public std::exception
{
public:
using exception::exception;
};
class ReservedKeyword : public std::exception
{
@@ -56,17 +62,24 @@ namespace Nz::ShaderLang
// Flow control
const Token& Advance();
void Consume(std::size_t count = 1);
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);
void HandleAttributes();
std::vector<ShaderAst::Attribute> ParseAttributes();
// Statements
ShaderAst::StatementPtr ParseExternalBlock(std::vector<ShaderAst::Attribute> attributes = {});
std::vector<ShaderAst::StatementPtr> ParseFunctionBody();
ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
ShaderAst::DeclareFunctionStatement::Parameter ParseFunctionParameter();
ShaderAst::StatementPtr ParseStructDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
ShaderAst::StatementPtr ParseReturnStatement();
ShaderAst::StatementPtr ParseStatement();
std::vector<ShaderAst::StatementPtr> ParseStatementList();
@@ -75,22 +88,28 @@ namespace Nz::ShaderLang
// Expressions
ShaderAst::ExpressionPtr ParseBinOpRhs(int exprPrecedence, ShaderAst::ExpressionPtr lhs);
ShaderAst::ExpressionPtr ParseExpression();
ShaderAst::ExpressionPtr ParseFloatingPointExpression(bool minus = false);
ShaderAst::ExpressionPtr ParseIdentifier();
ShaderAst::ExpressionPtr ParseIntegerExpression();
ShaderAst::ExpressionPtr ParseIntegerExpression(bool minus = false);
std::vector<ShaderAst::ExpressionPtr> ParseParameters();
ShaderAst::ExpressionPtr ParseParenthesisExpression();
ShaderAst::ExpressionPtr ParsePrimaryExpression();
ShaderAst::ExpressionPtr ParseVariableAssignation();
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
const std::string& ParseIdentifierAsName();
ShaderAst::ShaderExpressionType ParseIdentifierAsType();
ShaderAst::PrimitiveType ParsePrimitiveType();
ShaderAst::ExpressionType ParseType();
static int GetTokenPrecedence(TokenType token);
struct Context
{
std::unique_ptr<ShaderAst::MultiStatement> root;
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;
};

View File

@@ -21,15 +21,22 @@ NAZARA_SHADERLANG_TOKEN(Colon)
NAZARA_SHADERLANG_TOKEN(Comma)
NAZARA_SHADERLANG_TOKEN(Divide)
NAZARA_SHADERLANG_TOKEN(Dot)
NAZARA_SHADERLANG_TOKEN(Equal)
NAZARA_SHADERLANG_TOKEN(External)
NAZARA_SHADERLANG_TOKEN(FloatingPointValue)
NAZARA_SHADERLANG_TOKEN(EndOfStream)
NAZARA_SHADERLANG_TOKEN(FunctionDeclaration)
NAZARA_SHADERLANG_TOKEN(FunctionReturn)
NAZARA_SHADERLANG_TOKEN(GreatherThan)
NAZARA_SHADERLANG_TOKEN(GreatherThanEqual)
NAZARA_SHADERLANG_TOKEN(IntegerValue)
NAZARA_SHADERLANG_TOKEN(Identifier)
NAZARA_SHADERLANG_TOKEN(LessThan)
NAZARA_SHADERLANG_TOKEN(LessThanEqual)
NAZARA_SHADERLANG_TOKEN(Let)
NAZARA_SHADERLANG_TOKEN(Multiply)
NAZARA_SHADERLANG_TOKEN(Minus)
NAZARA_SHADERLANG_TOKEN(NotEqual)
NAZARA_SHADERLANG_TOKEN(Plus)
NAZARA_SHADERLANG_TOKEN(OpenAttribute)
NAZARA_SHADERLANG_TOKEN(OpenCurlyBracket)
@@ -37,6 +44,7 @@ NAZARA_SHADERLANG_TOKEN(OpenSquareBracket)
NAZARA_SHADERLANG_TOKEN(OpenParenthesis)
NAZARA_SHADERLANG_TOKEN(Semicolon)
NAZARA_SHADERLANG_TOKEN(Return)
NAZARA_SHADERLANG_TOKEN(Struct)
#undef NAZARA_SHADERLANG_TOKEN
#undef NAZARA_SHADERLANG_TOKEN_LAST

View File

@@ -12,9 +12,10 @@
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <Nazara/Shader/Ast/ExpressionType.hpp>
#include <array>
#include <memory>
#include <optional>
@@ -25,12 +26,6 @@ namespace Nz::ShaderAst
class AstExpressionVisitor;
class AstStatementVisitor;
struct Attribute
{
AttributeType type;
std::string args;
};
struct NAZARA_SHADER_API Node
{
Node() = default;
@@ -97,7 +92,7 @@ namespace Nz::ShaderAst
NodeType GetType() const override;
void Visit(AstExpressionVisitor& visitor) override;
BasicType targetType;
ExpressionType targetType;
std::array<ExpressionPtr, 4> expressions;
};
@@ -189,6 +184,22 @@ namespace Nz::ShaderAst
StatementPtr statement;
};
struct NAZARA_SHADER_API DeclareExternalStatement : Statement
{
NodeType GetType() const override;
void Visit(AstStatementVisitor& visitor) override;
struct ExternalVar
{
std::vector<Attribute> attributes;
std::string name;
ExpressionType type;
};
std::vector<Attribute> attributes;
std::vector<ExternalVar> externalVars;
};
struct NAZARA_SHADER_API DeclareFunctionStatement : Statement
{
NodeType GetType() const override;
@@ -197,14 +208,14 @@ namespace Nz::ShaderAst
struct Parameter
{
std::string name;
ShaderExpressionType type;
ExpressionType type;
};
std::string name;
std::vector<Attribute> attributes;
std::vector<Parameter> parameters;
std::vector<StatementPtr> statements;
ShaderExpressionType returnType = BasicType::Void;
ExpressionType returnType;
};
struct NAZARA_SHADER_API DeclareStructStatement : Statement
@@ -212,6 +223,7 @@ namespace Nz::ShaderAst
NodeType GetType() const override;
void Visit(AstStatementVisitor& visitor) override;
std::vector<Attribute> attributes;
StructDescription description;
};
@@ -222,7 +234,7 @@ namespace Nz::ShaderAst
std::string varName;
ExpressionPtr initialExpression;
ShaderExpressionType varType;
ExpressionType varType;
};
struct NAZARA_SHADER_API DiscardStatement : Statement

View File

@@ -10,7 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <Nazara/Shader/Ast/ExpressionType.hpp>
#include <Nazara/Shader/SpirvData.hpp>
#include <memory>
#include <optional>
@@ -172,11 +172,16 @@ namespace Nz
SpirvConstantCache& operator=(SpirvConstantCache&& cache) noexcept;
static ConstantPtr BuildConstant(const ShaderConstantValue& value);
static TypePtr BuildFunctionType(const ShaderAst::ShaderExpressionType& retType, const std::vector<ShaderAst::ShaderExpressionType>& parameters);
static TypePtr BuildPointerType(const ShaderAst::BasicType& type, SpirvStorageClass storageClass);
static TypePtr BuildPointerType(const ShaderAst::ShaderExpressionType& type, SpirvStorageClass storageClass);
static TypePtr BuildType(const ShaderAst::BasicType& type);
static TypePtr BuildType(const ShaderAst::ShaderExpressionType& type);
static TypePtr BuildFunctionType(const ShaderAst::ExpressionType& retType, const std::vector<ShaderAst::ExpressionType>& parameters);
static TypePtr BuildPointerType(const ShaderAst::PrimitiveType& type, SpirvStorageClass storageClass);
static TypePtr BuildPointerType(const ShaderAst::ExpressionType& type, SpirvStorageClass storageClass);
static TypePtr BuildType(const ShaderAst::ExpressionType& type);
static TypePtr BuildType(const ShaderAst::IdentifierType& type);
static TypePtr BuildType(const ShaderAst::MatrixType& type);
static TypePtr BuildType(const ShaderAst::NoType& type);
static TypePtr BuildType(const ShaderAst::PrimitiveType& type);
static TypePtr BuildType(const ShaderAst::SamplerType& type);
static TypePtr BuildType(const ShaderAst::VectorType& type);
private:
struct DepRegisterer;

View File

@@ -62,8 +62,8 @@ namespace Nz
const ExtVar& GetInputVariable(const std::string& name) const;
const ExtVar& GetOutputVariable(const std::string& name) const;
const ExtVar& GetUniformVariable(const std::string& name) const;
UInt32 GetPointerTypeId(const ShaderAst::ShaderExpressionType& type, SpirvStorageClass storageClass) const;
UInt32 GetTypeId(const ShaderAst::ShaderExpressionType& type) const;
UInt32 GetPointerTypeId(const ShaderAst::ExpressionType& type, SpirvStorageClass storageClass) const;
UInt32 GetTypeId(const ShaderAst::ExpressionType& type) const;
inline bool IsConditionEnabled(const std::string& condition) const;
@@ -80,8 +80,8 @@ namespace Nz
UInt32 RegisterConstant(const ShaderConstantValue& value);
UInt32 RegisterFunctionType(const ShaderAst::DeclareFunctionStatement& functionNode);
UInt32 RegisterPointerType(ShaderAst::ShaderExpressionType type, SpirvStorageClass storageClass);
UInt32 RegisterType(ShaderAst::ShaderExpressionType type);
UInt32 RegisterPointerType(ShaderAst::ExpressionType type, SpirvStorageClass storageClass);
UInt32 RegisterType(ShaderAst::ExpressionType type);
void WriteLocalVariable(std::string name, UInt32 resultId);
@@ -106,7 +106,7 @@ namespace Nz
struct FunctionParameter
{
std::string name;
ShaderAst::ShaderExpressionType type;
ShaderAst::ExpressionType type;
};
struct State;