From 53728200acf0e32fdabe27c0a7f0965fbe4da4b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sat, 5 Mar 2022 15:25:27 +0100 Subject: [PATCH] Shader: Add module ID (UUID) --- include/Nazara/Shader/Ast/Module.hpp | 33 ++++++++++++++----- include/Nazara/Shader/Ast/Module.inl | 33 +++++++++++++++++++ .../OpenGLRenderer/OpenGLRenderPipeline.cpp | 7 +--- .../Ast/AstConstantPropagationVisitor.cpp | 12 +++---- src/Nazara/Shader/Ast/AstSerializer.cpp | 7 ++-- .../Shader/Ast/EliminateUnusedPassVisitor.cpp | 8 ++--- src/Nazara/Shader/Ast/SanitizeVisitor.cpp | 3 +- src/Nazara/Shader/ShaderLangParser.cpp | 8 +---- src/ShaderNode/ShaderGraph.cpp | 7 +--- 9 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 include/Nazara/Shader/Ast/Module.inl diff --git a/include/Nazara/Shader/Ast/Module.hpp b/include/Nazara/Shader/Ast/Module.hpp index c77c1dde6..d735ffb1b 100644 --- a/include/Nazara/Shader/Ast/Module.hpp +++ b/include/Nazara/Shader/Ast/Module.hpp @@ -8,26 +8,43 @@ #define NAZARA_SHADER_AST_MODULE_HPP #include +#include #include #include #include namespace Nz::ShaderAst { - struct Module; + class Module; using ModulePtr = std::shared_ptr; - struct Module + class Module { - struct Metadata - { - UInt32 shaderLangVersion; - }; + public: + struct Metadata; - std::shared_ptr metadata; - MultiStatementPtr rootNode; + inline Module(UInt32 shaderLangVersion); + inline Module(std::shared_ptr metadata); + inline Module(std::shared_ptr 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 metadata; + MultiStatementPtr rootNode; + + struct Metadata + { + UInt32 shaderLangVersion; + Uuid moduleId; + }; }; } +#include + #endif // NAZARA_SHADER_AST_MODULE_HPP diff --git a/include/Nazara/Shader/Ast/Module.inl b/include/Nazara/Shader/Ast/Module.inl new file mode 100644 index 000000000..79e9ab898 --- /dev/null +++ b/include/Nazara/Shader/Ast/Module.inl @@ -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 +#include +#include + +namespace Nz::ShaderAst +{ + inline Module::Module(UInt32 shaderLangVersion) + { + auto mutMetadata = std::make_shared(); + mutMetadata->moduleId = Uuid::Generate(); + mutMetadata->shaderLangVersion = shaderLangVersion; + + metadata = std::move(mutMetadata); + rootNode = ShaderBuilder::MultiStatement(); + } + + inline Module::Module(std::shared_ptr metadata) : + Module(std::move(metadata), ShaderBuilder::MultiStatement()) + { + } + + inline Module::Module(std::shared_ptr Metadata, MultiStatementPtr RootNode) : + metadata(std::move(Metadata)), + rootNode(std::move(RootNode)) + { + } +} + +#include diff --git a/src/Nazara/OpenGLRenderer/OpenGLRenderPipeline.cpp b/src/Nazara/OpenGLRenderer/OpenGLRenderPipeline.cpp index 6df6b9c9b..9d4399fda 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLRenderPipeline.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLRenderPipeline.cpp @@ -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 metadata = std::make_shared(); - metadata->shaderLangVersion = 100; - - dummyModule.metadata = std::move(metadata); - OpenGLShaderModule shaderModule(device, stage, dummyModule); stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping()); } diff --git a/src/Nazara/Shader/Ast/AstConstantPropagationVisitor.cpp b/src/Nazara/Shader/Ast/AstConstantPropagationVisitor.cpp index b9dff56b4..d8ed0bfe8 100644 --- a/src/Nazara/Shader/Ast/AstConstantPropagationVisitor.cpp +++ b/src/Nazara/Shader/Ast/AstConstantPropagationVisitor.cpp @@ -737,20 +737,16 @@ namespace Nz::ShaderAst ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule) { - ModulePtr clone = std::make_shared(); - clone->metadata = shaderModule.metadata; - clone->rootNode = static_unique_pointer_cast(Process(*shaderModule.rootNode)); + auto rootnode = static_unique_pointer_cast(Process(*shaderModule.rootNode)); - return clone; + return std::make_shared(shaderModule.metadata, std::move(rootnode)); } ModulePtr AstConstantPropagationVisitor::Process(const Module& shaderModule, const Options& options) { - ModulePtr clone = std::make_shared(); - clone->metadata = shaderModule.metadata; - clone->rootNode = static_unique_pointer_cast(Process(*shaderModule.rootNode, options)); + auto rootNode = static_unique_pointer_cast(Process(*shaderModule.rootNode, options)); - return clone; + return std::make_shared(shaderModule.metadata, std::move(rootNode)); } ExpressionPtr AstConstantPropagationVisitor::Clone(BinaryExpression& node) diff --git a/src/Nazara/Shader/Ast/AstSerializer.cpp b/src/Nazara/Shader/Ast/AstSerializer.cpp index 1899d93d3..7052cb6dd 100644 --- a/src/Nazara/Shader/Ast/AstSerializer.cpp +++ b/src/Nazara/Shader/Ast/AstSerializer.cpp @@ -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(); - std::shared_ptr metadata = std::make_shared(); + m_stream >> metadata->moduleId; m_stream >> metadata->shaderLangVersion; - module->metadata = std::move(metadata); + ModulePtr module = std::make_shared(std::move(metadata)); - module->rootNode = ShaderBuilder::MultiStatement(); ShaderSerializerVisitor visitor(*this); module->rootNode->Visit(visitor); diff --git a/src/Nazara/Shader/Ast/EliminateUnusedPassVisitor.cpp b/src/Nazara/Shader/Ast/EliminateUnusedPassVisitor.cpp index b48161e0a..671da02b1 100644 --- a/src/Nazara/Shader/Ast/EliminateUnusedPassVisitor.cpp +++ b/src/Nazara/Shader/Ast/EliminateUnusedPassVisitor.cpp @@ -25,11 +25,9 @@ namespace Nz::ShaderAst ModulePtr EliminateUnusedPassVisitor::Process(const Module& shaderModule, const DependencyCheckerVisitor::UsageSet& usageSet) { - ModulePtr clone = std::make_shared(); - clone->metadata = shaderModule.metadata; - clone->rootNode = static_unique_pointer_cast(Process(*shaderModule.rootNode, usageSet)); - - return clone; + auto rootNode = static_unique_pointer_cast(Process(*shaderModule.rootNode, usageSet)); + + return std::make_shared(shaderModule.metadata, std::move(rootNode)); } StatementPtr EliminateUnusedPassVisitor::Process(Statement& statement, const DependencyCheckerVisitor::UsageSet& usageSet) diff --git a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp index 8ac706f8b..db241ea91 100644 --- a/src/Nazara/Shader/Ast/SanitizeVisitor.cpp +++ b/src/Nazara/Shader/Ast/SanitizeVisitor.cpp @@ -141,8 +141,7 @@ namespace Nz::ShaderAst ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error) { - ModulePtr clone = std::make_shared(); - clone->metadata = module.metadata; + ModulePtr clone = std::make_shared(module.metadata); Context currentContext; currentContext.options = options; diff --git a/src/Nazara/Shader/ShaderLangParser.cpp b/src/Nazara/Shader/ShaderLangParser.cpp index 4e2cb0c0c..8bfaa4d76 100644 --- a/src/Nazara/Shader/ShaderLangParser.cpp +++ b/src/Nazara/Shader/ShaderLangParser.cpp @@ -304,13 +304,7 @@ namespace Nz::ShaderLang if (!moduleVersion.has_value()) throw AttributeError{ "missing module version" }; - m_context->module = std::make_shared(); - m_context->module->rootNode = ShaderBuilder::MultiStatement(); - - std::shared_ptr moduleMetadata = std::make_shared(); - moduleMetadata->shaderLangVersion = *moduleVersion; - - m_context->module->metadata = std::move(moduleMetadata); + m_context->module = std::make_shared(*moduleVersion); Expect(Advance(), TokenType::Semicolon); } diff --git a/src/ShaderNode/ShaderGraph.cpp b/src/ShaderNode/ShaderGraph.cpp index 47b4f1a59..bce32ca47 100644 --- a/src/ShaderNode/ShaderGraph.cpp +++ b/src/ShaderNode/ShaderGraph.cpp @@ -459,12 +459,7 @@ QJsonObject ShaderGraph::Save() Nz::ShaderAst::ModulePtr ShaderGraph::ToModule() const { - Nz::ShaderAst::ModulePtr shaderModule = std::make_shared(); - - std::shared_ptr moduleMetada = std::make_shared(); - moduleMetada->shaderLangVersion = 100; - - shaderModule->metadata = std::move(moduleMetada); + Nz::ShaderAst::ModulePtr shaderModule = std::make_shared(100); // Declare all options for (const auto& option : m_options)