Shader: Add module ID (UUID)

This commit is contained in:
Jérôme Leclercq 2022-03-05 15:25:27 +01:00
parent 43ac86e85c
commit 53728200ac
9 changed files with 72 additions and 46 deletions

View File

@ -8,26 +8,43 @@
#define NAZARA_SHADER_AST_MODULE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Uuid.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <memory>
namespace Nz::ShaderAst
{
struct Module;
class Module;
using ModulePtr = std::shared_ptr<Module>;
struct Module
class Module
{
struct Metadata
{
UInt32 shaderLangVersion;
};
public:
struct Metadata;
std::shared_ptr<const Metadata> metadata;
MultiStatementPtr rootNode;
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;
MultiStatementPtr rootNode;
struct Metadata
{
UInt32 shaderLangVersion;
Uuid moduleId;
};
};
}
#include <Nazara/Shader/Ast/Module.inl>
#endif // NAZARA_SHADER_AST_MODULE_HPP

View File

@ -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>

View File

@ -48,15 +48,10 @@ namespace Nz
{
if (!stageFlags.Test(stage))
{
ShaderAst::Module dummyModule;
ShaderAst::Module dummyModule(100);
dummyModule.rootNode = ShaderBuilder::MultiStatement();
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);
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping());
}

View File

@ -737,20 +737,16 @@ namespace Nz::ShaderAst
ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule)
{
ModulePtr clone = std::make_shared<Module>();
clone->metadata = shaderModule.metadata;
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode));
auto 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 clone = std::make_shared<Module>();
clone->metadata = shaderModule.metadata;
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, options));
auto 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)

View File

@ -346,6 +346,7 @@ namespace Nz::ShaderAst
{
m_stream << s_magicNumber << s_currentVersion;
m_stream << module.metadata->moduleId;
m_stream << module.metadata->shaderLangVersion;
Serialize(*module.rootNode);
@ -542,14 +543,12 @@ namespace Nz::ShaderAst
if (version > s_currentVersion)
throw std::runtime_error("unsupported version");
ModulePtr module = std::make_shared<Module>();
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
m_stream >> metadata->moduleId;
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);
module->rootNode->Visit(visitor);

View File

@ -25,11 +25,9 @@ namespace Nz::ShaderAst
ModulePtr EliminateUnusedPassVisitor::Process(const Module& shaderModule, const DependencyCheckerVisitor::UsageSet& usageSet)
{
ModulePtr clone = std::make_shared<Module>();
clone->metadata = shaderModule.metadata;
clone->rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, usageSet));
return clone;
auto rootNode = static_unique_pointer_cast<MultiStatement>(Process(*shaderModule.rootNode, usageSet));
return std::make_shared<Module>(shaderModule.metadata, std::move(rootNode));
}
StatementPtr EliminateUnusedPassVisitor::Process(Statement& statement, const DependencyCheckerVisitor::UsageSet& usageSet)

View File

@ -141,8 +141,7 @@ namespace Nz::ShaderAst
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
{
ModulePtr clone = std::make_shared<Module>();
clone->metadata = module.metadata;
ModulePtr clone = std::make_shared<Module>(module.metadata);
Context currentContext;
currentContext.options = options;

View File

@ -304,13 +304,7 @@ namespace Nz::ShaderLang
if (!moduleVersion.has_value())
throw AttributeError{ "missing module version" };
m_context->module = std::make_shared<ShaderAst::Module>();
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);
m_context->module = std::make_shared<ShaderAst::Module>(*moduleVersion);
Expect(Advance(), TokenType::Semicolon);
}

View File

@ -459,12 +459,7 @@ QJsonObject ShaderGraph::Save()
Nz::ShaderAst::ModulePtr ShaderGraph::ToModule() const
{
Nz::ShaderAst::ModulePtr shaderModule = std::make_shared<Nz::ShaderAst::Module>();
std::shared_ptr<Nz::ShaderAst::Module::Metadata> moduleMetada = std::make_shared<Nz::ShaderAst::Module::Metadata>();
moduleMetada->shaderLangVersion = 100;
shaderModule->metadata = std::move(moduleMetada);
Nz::ShaderAst::ModulePtr shaderModule = std::make_shared<Nz::ShaderAst::Module>(100);
// Declare all options
for (const auto& option : m_options)