Shader: Move attribute parsing to parser, simplifying writer code

This commit is contained in:
Jérôme Leclercq
2021-04-14 11:34:21 +02:00
parent bca1561f73
commit aababb205f
14 changed files with 910 additions and 1046 deletions

View File

@@ -11,6 +11,7 @@
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <optional>
#include <string>
#include <variant>
#include <vector>
@@ -81,11 +82,13 @@ namespace Nz::ShaderAst
{
struct StructMember
{
std::optional<BuiltinEntry> builtin;
std::optional<unsigned int> locationIndex;
std::string name;
std::vector<Attribute> attributes;
ExpressionType type;
};
std::optional<StructLayout> layout;
std::string name;
std::vector<StructMember> members;
};

View File

@@ -71,7 +71,7 @@ namespace Nz
void HandleEntryPoint(ShaderAst::DeclareFunctionStatement& node);
void HandleInOut();
void RegisterStruct(std::size_t structIndex, bool isStd140, ShaderAst::StructDescription desc);
void RegisterStruct(std::size_t structIndex, ShaderAst::StructDescription desc);
void RegisterVariable(std::size_t varIndex, std::string varName);
void Visit(ShaderAst::ExpressionPtr& expr, bool encloseIfRequired = false);

View File

@@ -48,7 +48,6 @@ 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);

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderNodes.hpp>
#include <memory>
#include <optional>
namespace Nz::ShaderBuilder
{
@@ -59,13 +60,12 @@ 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::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;
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;
};
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

View File

@@ -108,10 +108,10 @@ 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::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::ExpressionType returnType) const
{
auto declareFunctionNode = std::make_unique<ShaderAst::DeclareFunctionStatement>();
declareFunctionNode->attributes = std::move(attributes);
declareFunctionNode->entryStage = entryStage;
declareFunctionNode->name = std::move(name);
declareFunctionNode->parameters = std::move(parameters);
declareFunctionNode->returnType = std::move(returnType);
@@ -128,15 +128,6 @@ namespace Nz::ShaderBuilder
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>();

View File

@@ -25,14 +25,6 @@ namespace Nz::ShaderAst
Location //< Location (struct member only) - has argument index
};
enum class PrimitiveType
{
Boolean, //< bool
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
};
enum class BinaryType
{
Add, //< +
@@ -80,6 +72,14 @@ namespace Nz::ShaderAst
#include <Nazara/Shader/ShaderAstNodes.hpp>
};
enum class PrimitiveType
{
Boolean, //< bool
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
};
enum class SwizzleComponent
{
First,

View File

@@ -14,6 +14,12 @@
namespace Nz::ShaderLang
{
class AttributeError : public std::exception
{
public:
using exception::exception;
};
class ExpectedToken : public std::exception
{
public:

View File

@@ -210,13 +210,12 @@ namespace Nz::ShaderAst
struct ExternalVar
{
std::optional<unsigned int> bindingIndex;
std::string name;
std::vector<Attribute> attributes;
ExpressionType type;
};
std::optional<std::size_t> varIndex;
std::vector<Attribute> attributes;
std::vector<ExternalVar> externalVars;
};
@@ -231,10 +230,10 @@ namespace Nz::ShaderAst
ExpressionType type;
};
std::optional<ShaderStageType> entryStage;
std::optional<std::size_t> funcIndex;
std::optional<std::size_t> varIndex;
std::string name;
std::vector<Attribute> attributes;
std::vector<Parameter> parameters;
std::vector<StatementPtr> statements;
ExpressionType returnType;
@@ -246,7 +245,6 @@ namespace Nz::ShaderAst
void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> structIndex;
std::vector<Attribute> attributes;
StructDescription description;
};