This commit is contained in:
Lynix
2022-03-08 01:30:48 +01:00
committed by Jérôme Leclercq
parent 012712b8d0
commit 83d26e209e
22 changed files with 295 additions and 142 deletions

View File

@@ -58,6 +58,7 @@ namespace Nz::ShaderAst
virtual StatementPtr Clone(BranchStatement& node);
virtual StatementPtr Clone(ConditionalStatement& node);
virtual StatementPtr Clone(DeclareAliasStatement& node);
virtual StatementPtr Clone(DeclareConstStatement& node);
virtual StatementPtr Clone(DeclareExternalStatement& node);
virtual StatementPtr Clone(DeclareFunctionStatement& node);

View File

@@ -50,6 +50,7 @@ namespace Nz::ShaderAst
inline bool Compare(const BranchStatement& lhs, const BranchStatement& rhs);
inline bool Compare(const ConditionalStatement& lhs, const ConditionalStatement& rhs);
inline bool Compare(const DeclareAliasStatement& lhs, const DeclareAliasStatement& rhs);
inline bool Compare(const DeclareConstStatement& lhs, const DeclareConstStatement& rhs);
inline bool Compare(const DeclareExternalStatement& lhs, const DeclareExternalStatement& rhs);
inline bool Compare(const DeclareFunctionStatement& lhs, const DeclareFunctionStatement& rhs);

View File

@@ -419,6 +419,17 @@ namespace Nz::ShaderAst
return true;
}
bool Compare(const DeclareAliasStatement& lhs, const DeclareAliasStatement& rhs)
{
if (!Compare(lhs.name, rhs.name))
return false;
if (!Compare(lhs.expression, rhs.expression))
return false;
return true;
}
inline bool Compare(const DeclareConstStatement& lhs, const DeclareConstStatement& rhs)
{
if (!Compare(lhs.name, rhs.name))

View File

@@ -45,6 +45,7 @@ NAZARA_SHADERAST_EXPRESSION(VariableExpression)
NAZARA_SHADERAST_EXPRESSION(UnaryExpression)
NAZARA_SHADERAST_STATEMENT(BranchStatement)
NAZARA_SHADERAST_STATEMENT(ConditionalStatement)
NAZARA_SHADERAST_STATEMENT(DeclareAliasStatement)
NAZARA_SHADERAST_STATEMENT(DeclareConstStatement)
NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement)
NAZARA_SHADERAST_STATEMENT(DeclareFunctionStatement)

View File

@@ -38,6 +38,7 @@ namespace Nz::ShaderAst
void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override;
void Visit(DeclareAliasStatement& node) override;
void Visit(DeclareConstStatement& node) override;
void Visit(DeclareExternalStatement& node) override;
void Visit(DeclareFunctionStatement& node) override;

View File

@@ -41,6 +41,7 @@ namespace Nz::ShaderAst
void Serialize(BranchStatement& node);
void Serialize(ConditionalStatement& node);
void Serialize(DeclareAliasStatement& node);
void Serialize(DeclareConstStatement& node);
void Serialize(DeclareExternalStatement& node);
void Serialize(DeclareFunctionStatement& node);

View File

@@ -244,6 +244,16 @@ namespace Nz::ShaderAst
StatementPtr statement;
};
struct NAZARA_SHADER_API DeclareAliasStatement : Statement
{
NodeType GetType() const override;
void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> aliasIndex;
std::string name;
ExpressionPtr expression;
};
struct NAZARA_SHADER_API DeclareConstStatement : Statement
{
NodeType GetType() const override;

View File

@@ -57,11 +57,13 @@ namespace Nz::ShaderAst
};
private:
enum class IdentifierCategory;
struct CurrentFunctionData;
struct Environment;
struct FunctionData;
struct Identifier;
template<typename T> struct IdentifierData;
struct IdentifierData;
template<typename T> struct IdentifierList;
struct Scope;
using AstCloner::CloneExpression;
@@ -84,6 +86,7 @@ namespace Nz::ShaderAst
StatementPtr Clone(BranchStatement& node) override;
StatementPtr Clone(ConditionalStatement& node) override;
StatementPtr Clone(DeclareAliasStatement& node) override;
StatementPtr Clone(DeclareConstStatement& node) override;
StatementPtr Clone(DeclareExternalStatement& node) override;
StatementPtr Clone(DeclareFunctionStatement& node) override;
@@ -99,10 +102,10 @@ namespace Nz::ShaderAst
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;
const Identifier* FindIdentifier(const Environment& environment, const std::string_view& identifierName) const;
template<typename F> const Identifier* FindIdentifier(const Environment& environment, const std::string_view& identifierName, F&& functor) const;
const IdentifierData* FindIdentifier(const std::string_view& identifierName) const;
template<typename F> const IdentifierData* FindIdentifier(const std::string_view& identifierName, F&& functor) const;
const IdentifierData* FindIdentifier(const Environment& environment, const std::string_view& identifierName) const;
template<typename F> const IdentifierData* FindIdentifier(const Environment& environment, const std::string_view& identifierName, F&& functor) const;
TypeParameter FindTypeParameter(const std::string_view& identifierName) const;
Expression& MandatoryExpr(const ExpressionPtr& node) const;
@@ -120,6 +123,8 @@ namespace Nz::ShaderAst
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
void RegisterBuiltin();
std::size_t RegisterAlias(std::string name, IdentifierData aliasData, std::optional<std::size_t> index = {});
std::size_t RegisterConstant(std::string name, ConstantValue value, std::optional<std::size_t> index = {});
std::size_t RegisterFunction(std::string name, FunctionData funcData, std::optional<std::size_t> index = {});
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type);
@@ -129,6 +134,7 @@ namespace Nz::ShaderAst
std::size_t RegisterType(std::string name, PartialType partialType, std::optional<std::size_t> index = {});
std::size_t RegisterVariable(std::string name, ExpressionType type, std::optional<std::size_t> index = {});
const IdentifierData* ResolveAlias(const IdentifierData* identifier) const;
void ResolveFunctions();
const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node);
std::size_t ResolveStruct(const ExpressionType& exprType);
@@ -146,6 +152,7 @@ namespace Nz::ShaderAst
StatementPtr Unscope(StatementPtr node);
void Validate(DeclareAliasStatement& node);
void Validate(WhileStatement& node);
void Validate(AccessIndexExpression& node);
@@ -160,6 +167,18 @@ namespace Nz::ShaderAst
void Validate(VariableExpression& node);
ExpressionType ValidateBinaryOp(BinaryType op, const ExpressionPtr& leftExpr, const ExpressionPtr& rightExpr);
enum class IdentifierCategory
{
Alias,
Constant,
Function,
Intrinsic,
Module,
Struct,
Type,
Variable
};
struct FunctionData
{
Bitset<> calledByFunctions;
@@ -167,23 +186,16 @@ namespace Nz::ShaderAst
FunctionFlags flags;
};
struct IdentifierData
{
std::size_t index;
IdentifierCategory category;
};
struct Identifier
{
enum class Type
{
Alias,
Constant,
Function,
Intrinsic,
Module,
Struct,
Type,
Variable
};
std::string name;
std::size_t index;
Type type;
IdentifierData data;
};
struct Context;

View File

@@ -105,6 +105,7 @@ namespace Nz
void Visit(ShaderAst::UnaryExpression& node) override;
void Visit(ShaderAst::BranchStatement& node) override;
void Visit(ShaderAst::DeclareAliasStatement& node) override;
void Visit(ShaderAst::DeclareConstStatement& node) override;
void Visit(ShaderAst::DeclareExternalStatement& node) override;
void Visit(ShaderAst::DeclareFunctionStatement& node) override;

View File

@@ -110,6 +110,7 @@ namespace Nz
void Visit(ShaderAst::BranchStatement& node) override;
void Visit(ShaderAst::ConditionalStatement& node) override;
void Visit(ShaderAst::DeclareAliasStatement& node) override;
void Visit(ShaderAst::DeclareConstStatement& node) override;
void Visit(ShaderAst::DeclareExternalStatement& node) override;
void Visit(ShaderAst::DeclareFunctionStatement& node) override;

View File

@@ -76,6 +76,11 @@ namespace Nz::ShaderBuilder
template<typename T> std::unique_ptr<ShaderAst::ConstantValueExpression> operator()(ShaderAst::ExpressionType type, T value) const;
};
struct DeclareAlias
{
inline std::unique_ptr<ShaderAst::DeclareAliasStatement> operator()(std::string name, ShaderAst::ExpressionPtr expression) const;
};
struct DeclareConst
{
inline std::unique_ptr<ShaderAst::DeclareConstStatement> operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const;
@@ -191,6 +196,7 @@ namespace Nz::ShaderBuilder
constexpr Impl::ConditionalStatement ConditionalStatement;
constexpr Impl::Constant Constant;
constexpr Impl::Branch<true> ConstBranch;
constexpr Impl::DeclareAlias DeclareAlias;
constexpr Impl::DeclareConst DeclareConst;
constexpr Impl::DeclareFunction DeclareFunction;
constexpr Impl::DeclareOption DeclareOption;

View File

@@ -195,6 +195,15 @@ namespace Nz::ShaderBuilder
throw std::runtime_error("unexpected primitive type");
}
inline std::unique_ptr<ShaderAst::DeclareAliasStatement> Impl::DeclareAlias::operator()(std::string name, ShaderAst::ExpressionPtr expression) const
{
auto declareAliasNode = std::make_unique<ShaderAst::DeclareAliasStatement>();
declareAliasNode->name = std::move(name);
declareAliasNode->expression = std::move(expression);
return declareAliasNode;
}
inline std::unique_ptr<ShaderAst::DeclareConstStatement> Impl::DeclareConst::operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const
{
auto declareConstNode = std::make_unique<ShaderAst::DeclareConstStatement>();

View File

@@ -48,6 +48,7 @@ namespace Nz
void Visit(ShaderAst::CallFunctionExpression& node) override;
void Visit(ShaderAst::CastExpression& node) override;
void Visit(ShaderAst::ConstantValueExpression& node) override;
void Visit(ShaderAst::DeclareAliasStatement& node) override;
void Visit(ShaderAst::DeclareConstStatement& node) override;
void Visit(ShaderAst::DeclareExternalStatement& node) override;
void Visit(ShaderAst::DeclareFunctionStatement& node) override;