Shader: Add support for custom functions calls (and better handle intrinsics)
This commit is contained in:
@@ -40,6 +40,8 @@ namespace Nz::ShaderAst
|
||||
virtual ExpressionPtr Clone(AccessMemberIndexExpression& node);
|
||||
virtual ExpressionPtr Clone(AssignExpression& node);
|
||||
virtual ExpressionPtr Clone(BinaryExpression& node);
|
||||
virtual ExpressionPtr Clone(CallFunctionExpression& node);
|
||||
virtual ExpressionPtr Clone(CallMethodExpression& node);
|
||||
virtual ExpressionPtr Clone(CastExpression& node);
|
||||
virtual ExpressionPtr Clone(ConditionalExpression& node);
|
||||
virtual ExpressionPtr Clone(ConstantExpression& node);
|
||||
|
||||
@@ -30,6 +30,8 @@ NAZARA_SHADERAST_EXPRESSION(AccessMemberIdentifierExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(AccessMemberIndexExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(AssignExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(BinaryExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(CallFunctionExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(CallMethodExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(CastExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(ConditionalExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(ConstantExpression)
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace Nz::ShaderAst
|
||||
void Visit(AccessMemberIndexExpression& node) override;
|
||||
void Visit(AssignExpression& node) override;
|
||||
void Visit(BinaryExpression& node) override;
|
||||
void Visit(CallFunctionExpression& node) override;
|
||||
void Visit(CallMethodExpression& node) override;
|
||||
void Visit(CastExpression& node) override;
|
||||
void Visit(ConditionalExpression& node) override;
|
||||
void Visit(ConstantExpression& node) override;
|
||||
|
||||
@@ -27,6 +27,8 @@ namespace Nz::ShaderAst
|
||||
void Serialize(AccessMemberIndexExpression& node);
|
||||
void Serialize(AssignExpression& node);
|
||||
void Serialize(BinaryExpression& node);
|
||||
void Serialize(CallFunctionExpression& node);
|
||||
void Serialize(CallMethodExpression& node);
|
||||
void Serialize(CastExpression& node);
|
||||
void Serialize(ConditionalExpression& node);
|
||||
void Serialize(ConstantExpression& node);
|
||||
|
||||
@@ -35,6 +35,8 @@ namespace Nz::ShaderAst
|
||||
void Visit(AccessMemberIndexExpression& node) override;
|
||||
void Visit(AssignExpression& node) override;
|
||||
void Visit(BinaryExpression& node) override;
|
||||
void Visit(CallFunctionExpression& node) override;
|
||||
void Visit(CallMethodExpression& node) override;
|
||||
void Visit(CastExpression& node) override;
|
||||
void Visit(ConditionalExpression& node) override;
|
||||
void Visit(ConstantExpression& node) override;
|
||||
|
||||
@@ -102,6 +102,25 @@ namespace Nz::ShaderAst
|
||||
ExpressionPtr right;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API CallFunctionExpression : public Expression
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
std::variant<std::string, std::size_t> targetFunction;
|
||||
std::vector<ExpressionPtr> parameters;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API CallMethodExpression : public Expression
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
ExpressionPtr object;
|
||||
std::string methodName;
|
||||
std::vector<ExpressionPtr> parameters;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API CastExpression : public Expression
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
@@ -20,7 +21,7 @@ namespace Nz::ShaderAst
|
||||
public:
|
||||
struct Options;
|
||||
|
||||
inline SanitizeVisitor();
|
||||
SanitizeVisitor() = default;
|
||||
SanitizeVisitor(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor(SanitizeVisitor&&) = delete;
|
||||
~SanitizeVisitor() = default;
|
||||
@@ -47,6 +48,7 @@ namespace Nz::ShaderAst
|
||||
ExpressionPtr Clone(AccessMemberIdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(AssignExpression& node) override;
|
||||
ExpressionPtr Clone(BinaryExpression& node) override;
|
||||
ExpressionPtr Clone(CallFunctionExpression& node) override;
|
||||
ExpressionPtr Clone(CastExpression& node) override;
|
||||
ExpressionPtr Clone(ConditionalExpression& node) override;
|
||||
ExpressionPtr Clone(ConstantExpression& node) override;
|
||||
@@ -76,10 +78,11 @@ namespace Nz::ShaderAst
|
||||
void PushScope();
|
||||
void PopScope();
|
||||
|
||||
inline std::size_t RegisterFunction(std::string name);
|
||||
inline std::size_t RegisterOption(std::string name, ExpressionType type);
|
||||
inline std::size_t RegisterStruct(std::string name, StructDescription description);
|
||||
inline std::size_t RegisterVariable(std::string name, ExpressionType type);
|
||||
std::size_t RegisterFunction(DeclareFunctionStatement* funcDecl);
|
||||
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type);
|
||||
std::size_t RegisterOption(std::string name, ExpressionType type);
|
||||
std::size_t RegisterStruct(std::string name, StructDescription description);
|
||||
std::size_t RegisterVariable(std::string name, ExpressionType type);
|
||||
|
||||
std::size_t ResolveStruct(const ExpressionType& exprType);
|
||||
std::size_t ResolveStruct(const IdentifierType& identifierType);
|
||||
@@ -89,37 +92,33 @@ namespace Nz::ShaderAst
|
||||
|
||||
void SanitizeIdentifier(std::string& identifier);
|
||||
|
||||
struct Alias
|
||||
{
|
||||
std::variant<ExpressionType> value;
|
||||
};
|
||||
|
||||
struct Option
|
||||
{
|
||||
std::size_t optionIndex;
|
||||
};
|
||||
|
||||
struct Struct
|
||||
{
|
||||
std::size_t structIndex;
|
||||
};
|
||||
|
||||
struct Variable
|
||||
{
|
||||
std::size_t varIndex;
|
||||
};
|
||||
void Validate(CallFunctionExpression& node, const DeclareFunctionStatement* referenceDeclaration);
|
||||
void Validate(IntrinsicExpression& node);
|
||||
|
||||
struct Identifier
|
||||
{
|
||||
enum class Type
|
||||
{
|
||||
Alias,
|
||||
Function,
|
||||
Intrinsic,
|
||||
Option,
|
||||
Struct,
|
||||
Variable
|
||||
};
|
||||
|
||||
std::string name;
|
||||
std::variant<Alias, Option, Struct, Variable> value;
|
||||
std::size_t index;
|
||||
Type type;
|
||||
};
|
||||
|
||||
std::size_t m_nextFuncIndex;
|
||||
std::unordered_map<std::string /*functionName*/, std::pair<const DeclareFunctionStatement*, std::size_t>> m_functionDeclarations;
|
||||
std::vector<Identifier> m_identifiersInScope;
|
||||
std::vector<DeclareFunctionStatement*> m_functions;
|
||||
std::vector<IntrinsicType> m_intrinsics;
|
||||
std::vector<ExpressionType> m_options;
|
||||
std::vector<StructDescription> m_structs;
|
||||
std::vector<ExpressionType> m_variables;
|
||||
std::vector<ExpressionType> m_variableTypes;
|
||||
std::vector<std::size_t> m_scopeSizes;
|
||||
|
||||
struct Context;
|
||||
|
||||
@@ -7,11 +7,6 @@
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
inline SanitizeVisitor::SanitizeVisitor() :
|
||||
m_nextFuncIndex(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline StatementPtr SanitizeVisitor::Sanitize(const StatementPtr& statement, std::string* error)
|
||||
{
|
||||
return Sanitize(statement, {}, error);
|
||||
@@ -26,56 +21,6 @@ namespace Nz::ShaderAst
|
||||
return &*it;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterFunction(std::string name)
|
||||
{
|
||||
return m_nextFuncIndex++;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterOption(std::string name, ExpressionType type)
|
||||
{
|
||||
std::size_t optionIndex = m_options.size();
|
||||
m_options.emplace_back(std::move(type));
|
||||
|
||||
m_identifiersInScope.push_back({
|
||||
std::move(name),
|
||||
Option {
|
||||
optionIndex
|
||||
}
|
||||
});
|
||||
|
||||
return optionIndex;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription description)
|
||||
{
|
||||
std::size_t structIndex = m_structs.size();
|
||||
m_structs.emplace_back(std::move(description));
|
||||
|
||||
m_identifiersInScope.push_back({
|
||||
std::move(name),
|
||||
Struct {
|
||||
structIndex
|
||||
}
|
||||
});
|
||||
|
||||
return structIndex;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type)
|
||||
{
|
||||
std::size_t varIndex = m_variables.size();
|
||||
m_variables.emplace_back(std::move(type));
|
||||
|
||||
m_identifiersInScope.push_back({
|
||||
std::move(name),
|
||||
Variable {
|
||||
varIndex
|
||||
}
|
||||
});
|
||||
|
||||
return varIndex;
|
||||
}
|
||||
|
||||
inline StatementPtr Sanitize(const StatementPtr& ast, std::string* error)
|
||||
{
|
||||
SanitizeVisitor sanitizer;
|
||||
|
||||
Reference in New Issue
Block a user