Shader/Modules: proof of concept
This commit is contained in:
62
include/Nazara/Shader/Ast/IndexRemapperVisitor.hpp
Normal file
62
include/Nazara/Shader/Ast/IndexRemapperVisitor.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Shader module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SHADER_AST_INDEXREMAPPER_HPP
|
||||
#define NAZARA_SHADER_AST_INDEXREMAPPER_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API IndexRemapperVisitor : public AstCloner
|
||||
{
|
||||
public:
|
||||
struct Callbacks;
|
||||
|
||||
IndexRemapperVisitor() = default;
|
||||
IndexRemapperVisitor(const IndexRemapperVisitor&) = delete;
|
||||
IndexRemapperVisitor(IndexRemapperVisitor&&) = delete;
|
||||
~IndexRemapperVisitor() = default;
|
||||
|
||||
StatementPtr Clone(Statement& statement, const Callbacks& callbacks);
|
||||
|
||||
IndexRemapperVisitor& operator=(const IndexRemapperVisitor&) = delete;
|
||||
IndexRemapperVisitor& operator=(IndexRemapperVisitor&&) = delete;
|
||||
|
||||
struct Callbacks
|
||||
{
|
||||
std::function<std::size_t(std::size_t previousIndex)> constIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex)> funcIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex) > structIndexGenerator;
|
||||
//std::function<std::size_t()> typeIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex)> varIndexGenerator;
|
||||
};
|
||||
|
||||
private:
|
||||
StatementPtr Clone(DeclareConstStatement& node) override;
|
||||
StatementPtr Clone(DeclareExternalStatement& node) override;
|
||||
StatementPtr Clone(DeclareFunctionStatement& node) override;
|
||||
StatementPtr Clone(DeclareStructStatement& node) override;
|
||||
StatementPtr Clone(DeclareVariableStatement& node) override;
|
||||
|
||||
ExpressionPtr Clone(CallFunctionExpression& node) override;
|
||||
ExpressionPtr Clone(VariableExpression& node) override;
|
||||
|
||||
void HandleType(ExpressionValue<ExpressionType>& exprType);
|
||||
|
||||
struct Context;
|
||||
Context* m_context;
|
||||
};
|
||||
|
||||
inline StatementPtr RemapIndices(Statement& statement, const IndexRemapperVisitor::Callbacks& callbacks);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/IndexRemapperVisitor.inl>
|
||||
|
||||
#endif // NAZARA_SHADER_AST_INDEXREMAPPER_HPP
|
||||
17
include/Nazara/Shader/Ast/IndexRemapperVisitor.inl
Normal file
17
include/Nazara/Shader/Ast/IndexRemapperVisitor.inl
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Shader module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/Ast/IndexRemapperVisitor.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
StatementPtr RemapIndices(Statement& statement, const IndexRemapperVisitor::Callbacks& callbacks)
|
||||
{
|
||||
IndexRemapperVisitor visitor;
|
||||
return visitor.Clone(statement, callbacks);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
@@ -250,6 +250,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> constIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string name;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
ExpressionPtr expression;
|
||||
@@ -269,8 +270,9 @@ namespace Nz::ShaderAst
|
||||
ExpressionValue<ExpressionType> type;
|
||||
};
|
||||
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
std::vector<ExternalVar> externalVars;
|
||||
std::optional<bool> hidden;
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareFunctionStatement : Statement
|
||||
@@ -280,12 +282,13 @@ namespace Nz::ShaderAst
|
||||
|
||||
struct Parameter
|
||||
{
|
||||
std::string name;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
std::optional<std::size_t> funcIndex;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string name;
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<StatementPtr> statements;
|
||||
@@ -301,6 +304,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> optIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string optName;
|
||||
ExpressionPtr defaultValue;
|
||||
ExpressionValue<ExpressionType> optType;
|
||||
@@ -312,6 +316,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> structIndex;
|
||||
std::optional<bool> hidden;
|
||||
ExpressionValue<bool> isExported;
|
||||
StructDescription description;
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <Nazara/Shader/Ast/AstTypes.hpp>
|
||||
#include <Nazara/Shader/Ast/Module.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
@@ -41,6 +42,7 @@ namespace Nz::ShaderAst
|
||||
|
||||
struct Options
|
||||
{
|
||||
std::function<ModulePtr(const std::vector<std::string>& /*modulePath*/)> moduleCallback;
|
||||
std::unordered_set<std::string> reservedIdentifiers;
|
||||
std::unordered_map<std::size_t, ConstantValue> optionValues;
|
||||
bool makeVariableNameUnique = false;
|
||||
@@ -112,13 +114,13 @@ namespace Nz::ShaderAst
|
||||
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
|
||||
|
||||
void RegisterBuiltin();
|
||||
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, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterStruct(std::string name, StructDescription* description, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, ExpressionType expressionType, std::optional<std::size_t> index = {});
|
||||
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 = {});
|
||||
std::size_t RegisterConstant(std::string name, ConstantValue value, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterFunction(std::string name, FunctionData funcData, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterStruct(std::string name, StructDescription* description, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, ExpressionType expressionType, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, PartialType partialType, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterVariable(std::string name, ExpressionType type, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
|
||||
void ResolveFunctions();
|
||||
const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node);
|
||||
|
||||
Reference in New Issue
Block a user