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;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
@@ -60,8 +61,9 @@ namespace Nz
|
||||
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 AppendFunctionDeclaration(const ShaderAst::DeclareFunctionStatement& node, bool forward = false);
|
||||
void AppendField(std::size_t structIndex, const std::size_t* memberIndices, std::size_t remainingMembers);
|
||||
void AppendHeader();
|
||||
void AppendHeader(const std::vector<ShaderAst::DeclareFunctionStatement*>& forwardFunctionDeclarations);
|
||||
void AppendLine(const std::string& txt = {});
|
||||
template<typename... Args> void AppendLine(Args&&... params);
|
||||
void AppendStatementList(std::vector<ShaderAst::StatementPtr>& statements);
|
||||
@@ -72,6 +74,7 @@ namespace Nz
|
||||
void HandleEntryPoint(ShaderAst::DeclareFunctionStatement& node);
|
||||
void HandleInOut();
|
||||
|
||||
void RegisterFunction(std::size_t funcIndex, std::string funcName);
|
||||
void RegisterStruct(std::size_t structIndex, ShaderAst::StructDescription desc);
|
||||
void RegisterVariable(std::size_t varIndex, std::string varName);
|
||||
|
||||
@@ -80,6 +83,7 @@ namespace Nz
|
||||
void Visit(ShaderAst::AccessMemberIndexExpression& node) override;
|
||||
void Visit(ShaderAst::AssignExpression& node) override;
|
||||
void Visit(ShaderAst::BinaryExpression& node) override;
|
||||
void Visit(ShaderAst::CallFunctionExpression& node) override;
|
||||
void Visit(ShaderAst::CastExpression& node) override;
|
||||
void Visit(ShaderAst::ConditionalExpression& node) override;
|
||||
void Visit(ShaderAst::ConstantExpression& node) override;
|
||||
|
||||
@@ -38,6 +38,11 @@ namespace Nz::ShaderBuilder
|
||||
inline std::unique_ptr<ShaderAst::BranchStatement> operator()(std::vector<ShaderAst::BranchStatement::ConditionalStatement> condStatements, ShaderAst::StatementPtr elseStatement = nullptr) const;
|
||||
};
|
||||
|
||||
struct CallFunction
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::CallFunctionExpression> operator()(std::string functionName, std::vector<ShaderAst::ExpressionPtr> parameters) const;
|
||||
};
|
||||
|
||||
struct Cast
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const;
|
||||
@@ -133,6 +138,7 @@ namespace Nz::ShaderBuilder
|
||||
constexpr Impl::Assign Assign;
|
||||
constexpr Impl::Binary Binary;
|
||||
constexpr Impl::Branch Branch;
|
||||
constexpr Impl::CallFunction CallFunction;
|
||||
constexpr Impl::Cast Cast;
|
||||
constexpr Impl::ConditionalExpression ConditionalExpression;
|
||||
constexpr Impl::ConditionalStatement ConditionalStatement;
|
||||
|
||||
@@ -58,6 +58,15 @@ namespace Nz::ShaderBuilder
|
||||
return branchNode;
|
||||
}
|
||||
|
||||
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->parameters = std::move(parameters);
|
||||
|
||||
return callFunctionExpression;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const
|
||||
{
|
||||
auto castNode = std::make_unique<ShaderAst::CastExpression>();
|
||||
@@ -138,7 +147,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareFunctionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> Nz::ShaderBuilder::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::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareOptionNode = std::make_unique<ShaderAst::DeclareOptionStatement>();
|
||||
declareOptionNode->optName = std::move(name);
|
||||
@@ -156,7 +165,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareStructNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Nz::ShaderBuilder::Impl::DeclareVariable::operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Impl::DeclareVariable::operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareVariableNode = std::make_unique<ShaderAst::DeclareVariableStatement>();
|
||||
declareVariableNode->varName = std::move(name);
|
||||
@@ -165,7 +174,7 @@ namespace Nz::ShaderBuilder
|
||||
return declareVariableNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareVariableStatement> Nz::ShaderBuilder::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::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareVariableNode = std::make_unique<ShaderAst::DeclareVariableStatement>();
|
||||
declareVariableNode->varName = std::move(name);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <Nazara/Shader/ShaderLangLexer.hpp>
|
||||
#include <Nazara/Shader/Ast/Nodes.hpp>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
|
||||
namespace Nz::ShaderLang
|
||||
{
|
||||
@@ -69,7 +70,7 @@ namespace Nz::ShaderLang
|
||||
// Flow control
|
||||
const Token& Advance();
|
||||
void Consume(std::size_t count = 1);
|
||||
ShaderAst::ExpressionType DecodeType(const std::string& identifier);
|
||||
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);
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace Nz
|
||||
void Visit(ShaderAst::AssignExpression& node) override;
|
||||
void Visit(ShaderAst::BinaryExpression& node) override;
|
||||
void Visit(ShaderAst::BranchStatement& node) override;
|
||||
void Visit(ShaderAst::CallFunctionExpression& node) override;
|
||||
void Visit(ShaderAst::CastExpression& node) override;
|
||||
void Visit(ShaderAst::ConditionalExpression& node) override;
|
||||
void Visit(ShaderAst::ConditionalStatement& node) override;
|
||||
@@ -92,7 +93,6 @@ namespace Nz
|
||||
ShaderStageType stageType;
|
||||
std::optional<InputStruct> inputStruct;
|
||||
std::optional<UInt32> outputStructTypeId;
|
||||
std::size_t funcIndex;
|
||||
std::vector<Input> inputs;
|
||||
std::vector<Output> outputs;
|
||||
};
|
||||
@@ -101,6 +101,11 @@ namespace Nz
|
||||
{
|
||||
std::optional<EntryPoint> entryPointData;
|
||||
|
||||
struct FuncCall
|
||||
{
|
||||
std::size_t firstVarIndex;
|
||||
};
|
||||
|
||||
struct Parameter
|
||||
{
|
||||
UInt32 pointerTypeId;
|
||||
@@ -113,7 +118,9 @@ namespace Nz
|
||||
UInt32 varId;
|
||||
};
|
||||
|
||||
std::size_t funcIndex;
|
||||
std::string name;
|
||||
std::vector<FuncCall> funcCalls;
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<Variable> variables;
|
||||
std::unordered_map<std::size_t, std::size_t> varIndexToVarId;
|
||||
@@ -138,6 +145,7 @@ namespace Nz
|
||||
inline void RegisterVariable(std::size_t varIndex, UInt32 typeId, UInt32 pointerId, SpirvStorageClass storageClass);
|
||||
|
||||
std::size_t m_extVarIndex;
|
||||
std::size_t m_funcCallIndex;
|
||||
std::size_t m_funcIndex;
|
||||
std::vector<std::size_t> m_scopeSizes;
|
||||
std::vector<FuncData>& m_funcData;
|
||||
|
||||
Reference in New Issue
Block a user