Shader: Add module ID (UUID)
This commit is contained in:
parent
43ac86e85c
commit
53728200ac
|
|
@ -8,26 +8,43 @@
|
||||||
#define NAZARA_SHADER_AST_MODULE_HPP
|
#define NAZARA_SHADER_AST_MODULE_HPP
|
||||||
|
|
||||||
#include <Nazara/Prerequisites.hpp>
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/Uuid.hpp>
|
||||||
#include <Nazara/Shader/Config.hpp>
|
#include <Nazara/Shader/Config.hpp>
|
||||||
#include <Nazara/Shader/Ast/Nodes.hpp>
|
#include <Nazara/Shader/Ast/Nodes.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace Nz::ShaderAst
|
namespace Nz::ShaderAst
|
||||||
{
|
{
|
||||||
struct Module;
|
class Module;
|
||||||
|
|
||||||
using ModulePtr = std::shared_ptr<Module>;
|
using ModulePtr = std::shared_ptr<Module>;
|
||||||
|
|
||||||
struct Module
|
class Module
|
||||||
{
|
{
|
||||||
struct Metadata
|
public:
|
||||||
{
|
struct Metadata;
|
||||||
UInt32 shaderLangVersion;
|
|
||||||
};
|
inline Module(UInt32 shaderLangVersion);
|
||||||
|
inline Module(std::shared_ptr<const Metadata> metadata);
|
||||||
|
inline Module(std::shared_ptr<const Metadata> metadata, MultiStatementPtr rootNode);
|
||||||
|
Module(const Module&) = default;
|
||||||
|
Module(Module&&) noexcept = default;
|
||||||
|
~Module() = default;
|
||||||
|
|
||||||
|
Module& operator=(const Module&) = default;
|
||||||
|
Module& operator=(Module&&) noexcept = default;
|
||||||
|
|
||||||
std::shared_ptr<const Metadata> metadata;
|
std::shared_ptr<const Metadata> metadata;
|
||||||
MultiStatementPtr rootNode;
|
MultiStatementPtr rootNode;
|
||||||
|
|
||||||
|
struct Metadata
|
||||||
|
{
|
||||||
|
UInt32 shaderLangVersion;
|
||||||
|
Uuid moduleId;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Shader/Ast/Module.inl>
|
||||||
|
|
||||||
#endif // NAZARA_SHADER_AST_MODULE_HPP
|
#endif // NAZARA_SHADER_AST_MODULE_HPP
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
// 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/Module.hpp>
|
||||||
|
#include <Nazara/Shader/ShaderBuilder.hpp>
|
||||||
|
#include <Nazara/Shader/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz::ShaderAst
|
||||||
|
{
|
||||||
|
inline Module::Module(UInt32 shaderLangVersion)
|
||||||
|
{
|
||||||
|
auto mutMetadata = std::make_shared<Metadata>();
|
||||||
|
mutMetadata->moduleId = Uuid::Generate();
|
||||||
|
mutMetadata->shaderLangVersion = shaderLangVersion;
|
||||||
|
|
||||||
|
metadata = std::move(mutMetadata);
|
||||||
|
rootNode = ShaderBuilder::MultiStatement();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Module::Module(std::shared_ptr<const Metadata> metadata) :
|
||||||
|
Module(std::move(metadata), ShaderBuilder::MultiStatement())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Module::Module(std::shared_ptr<const Metadata> Metadata, MultiStatementPtr RootNode) :
|
||||||
|
metadata(std::move(Metadata)),
|
||||||
|
rootNode(std::move(RootNode))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Shader/DebugOff.hpp>
|
||||||
|
|
@ -48,15 +48,10 @@ namespace Nz
|
||||||
{
|
{
|
||||||
if (!stageFlags.Test(stage))
|
if (!stageFlags.Test(stage))
|
||||||
{
|
{
|
||||||
ShaderAst::Module dummyModule;
|
ShaderAst::Module dummyModule(100);
|
||||||
dummyModule.rootNode = ShaderBuilder::MultiStatement();
|
dummyModule.rootNode = ShaderBuilder::MultiStatement();
|
||||||
dummyModule.rootNode->statements.push_back(ShaderBuilder::DeclareFunction(stage, "main", {}, {}));
|
dummyModule.rootNode->statements.push_back(ShaderBuilder::DeclareFunction(stage, "main", {}, {}));
|
||||||
|
|
||||||
std::shared_ptr<ShaderAst::Module::Metadata> metadata = std::make_shared<ShaderAst::Module::Metadata>();
|
|
||||||
metadata->shaderLangVersion = 100;
|
|
||||||
|
|
||||||
dummyModule.metadata = std::move(metadata);
|
|
||||||
|
|
||||||
OpenGLShaderModule shaderModule(device, stage, dummyModule);
|
OpenGLShaderModule shaderModule(device, stage, dummyModule);
|
||||||
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping());
|
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -737,20 +737,16 @@ namespace Nz::ShaderAst
|
||||||
|
|
||||||
ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule)
|
ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule)
|
||||||
{
|
{
|
||||||
ModulePtr clone = std::make_shared<Module>();
|
auto rootnode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode));
|
||||||
clone->metadata = shaderModule.metadata;
|
|
||||||
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode));
|
|
||||||
|
|
||||||
return clone;
|
return std::make_shared<Module>(shaderModule.metadata, std::move(rootnode));
|
||||||
}
|
}
|
||||||
|
|
||||||
ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule, const Options& options)
|
ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule, const Options& options)
|
||||||
{
|
{
|
||||||
ModulePtr clone = std::make_shared<Module>();
|
auto rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, options));
|
||||||
clone->metadata = shaderModule.metadata;
|
|
||||||
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, options));
|
|
||||||
|
|
||||||
return clone;
|
return std::make_shared<Module>(shaderModule.metadata, std::move(rootNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpressionPtr AstConstantPropagationVisitor::Clone(BinaryExpression& node)
|
ExpressionPtr AstConstantPropagationVisitor::Clone(BinaryExpression& node)
|
||||||
|
|
|
||||||
|
|
@ -346,6 +346,7 @@ namespace Nz::ShaderAst
|
||||||
{
|
{
|
||||||
m_stream << s_magicNumber << s_currentVersion;
|
m_stream << s_magicNumber << s_currentVersion;
|
||||||
|
|
||||||
|
m_stream << module.metadata->moduleId;
|
||||||
m_stream << module.metadata->shaderLangVersion;
|
m_stream << module.metadata->shaderLangVersion;
|
||||||
Serialize(*module.rootNode);
|
Serialize(*module.rootNode);
|
||||||
|
|
||||||
|
|
@ -542,14 +543,12 @@ namespace Nz::ShaderAst
|
||||||
if (version > s_currentVersion)
|
if (version > s_currentVersion)
|
||||||
throw std::runtime_error("unsupported version");
|
throw std::runtime_error("unsupported version");
|
||||||
|
|
||||||
ModulePtr module = std::make_shared<Module>();
|
|
||||||
|
|
||||||
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
|
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
|
||||||
|
m_stream >> metadata->moduleId;
|
||||||
m_stream >> metadata->shaderLangVersion;
|
m_stream >> metadata->shaderLangVersion;
|
||||||
|
|
||||||
module->metadata = std::move(metadata);
|
ModulePtr module = std::make_shared<Module>(std::move(metadata));
|
||||||
|
|
||||||
module->rootNode = ShaderBuilder::MultiStatement();
|
|
||||||
ShaderSerializerVisitor visitor(*this);
|
ShaderSerializerVisitor visitor(*this);
|
||||||
module->rootNode->Visit(visitor);
|
module->rootNode->Visit(visitor);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,9 @@ namespace Nz::ShaderAst
|
||||||
|
|
||||||
ModulePtr EliminateUnusedPassVisitor::Process(const Module& shaderModule, const DependencyCheckerVisitor::UsageSet& usageSet)
|
ModulePtr EliminateUnusedPassVisitor::Process(const Module& shaderModule, const DependencyCheckerVisitor::UsageSet& usageSet)
|
||||||
{
|
{
|
||||||
ModulePtr clone = std::make_shared<Module>();
|
auto rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, usageSet));
|
||||||
clone->metadata = shaderModule.metadata;
|
|
||||||
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, usageSet));
|
|
||||||
|
|
||||||
return clone;
|
return std::make_shared<Module>(shaderModule.metadata, std::move(rootNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
StatementPtr EliminateUnusedPassVisitor::Process(Statement& statement, const DependencyCheckerVisitor::UsageSet& usageSet)
|
StatementPtr EliminateUnusedPassVisitor::Process(Statement& statement, const DependencyCheckerVisitor::UsageSet& usageSet)
|
||||||
|
|
|
||||||
|
|
@ -141,8 +141,7 @@ namespace Nz::ShaderAst
|
||||||
|
|
||||||
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
|
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
|
||||||
{
|
{
|
||||||
ModulePtr clone = std::make_shared<Module>();
|
ModulePtr clone = std::make_shared<Module>(module.metadata);
|
||||||
clone->metadata = module.metadata;
|
|
||||||
|
|
||||||
Context currentContext;
|
Context currentContext;
|
||||||
currentContext.options = options;
|
currentContext.options = options;
|
||||||
|
|
|
||||||
|
|
@ -304,13 +304,7 @@ namespace Nz::ShaderLang
|
||||||
if (!moduleVersion.has_value())
|
if (!moduleVersion.has_value())
|
||||||
throw AttributeError{ "missing module version" };
|
throw AttributeError{ "missing module version" };
|
||||||
|
|
||||||
m_context->module = std::make_shared<ShaderAst::Module>();
|
m_context->module = std::make_shared<ShaderAst::Module>(*moduleVersion);
|
||||||
m_context->module->rootNode = ShaderBuilder::MultiStatement();
|
|
||||||
|
|
||||||
std::shared_ptr<ShaderAst::Module::Metadata> moduleMetadata = std::make_shared<ShaderAst::Module::Metadata>();
|
|
||||||
moduleMetadata->shaderLangVersion = *moduleVersion;
|
|
||||||
|
|
||||||
m_context->module->metadata = std::move(moduleMetadata);
|
|
||||||
|
|
||||||
Expect(Advance(), TokenType::Semicolon);
|
Expect(Advance(), TokenType::Semicolon);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -459,12 +459,7 @@ QJsonObject ShaderGraph::Save()
|
||||||
|
|
||||||
Nz::ShaderAst::ModulePtr ShaderGraph::ToModule() const
|
Nz::ShaderAst::ModulePtr ShaderGraph::ToModule() const
|
||||||
{
|
{
|
||||||
Nz::ShaderAst::ModulePtr shaderModule = std::make_shared<Nz::ShaderAst::Module>();
|
Nz::ShaderAst::ModulePtr shaderModule = std::make_shared<Nz::ShaderAst::Module>(100);
|
||||||
|
|
||||||
std::shared_ptr<Nz::ShaderAst::Module::Metadata> moduleMetada = std::make_shared<Nz::ShaderAst::Module::Metadata>();
|
|
||||||
moduleMetada->shaderLangVersion = 100;
|
|
||||||
|
|
||||||
shaderModule->metadata = std::move(moduleMetada);
|
|
||||||
|
|
||||||
// Declare all options
|
// Declare all options
|
||||||
for (const auto& option : m_options)
|
for (const auto& option : m_options)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue