Shader: Change module system (no longer based on path)

This commit is contained in:
Jérôme Leclercq
2022-03-13 15:07:43 +01:00
parent 80f9556f8c
commit e40e8eb204
40 changed files with 224 additions and 271 deletions

View File

@@ -15,7 +15,7 @@
#include <Nazara/Renderer/RenderPassCache.hpp>
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Shader/DirectoryModuleResolver.hpp>
#include <Nazara/Shader/FilesystemModuleResolver.hpp>
#include <optional>
namespace Nz
@@ -46,7 +46,7 @@ namespace Nz
inline const std::shared_ptr<RenderDevice>& GetRenderDevice() const;
inline const RenderPassCache& GetRenderPassCache() const;
inline TextureSamplerCache& GetSamplerCache();
inline const std::shared_ptr<DirectoryModuleResolver>& GetShaderModuleResolver() const;
inline const std::shared_ptr<FilesystemModuleResolver>& GetShaderModuleResolver() const;
struct Config
{
@@ -69,7 +69,7 @@ namespace Nz
std::optional<RenderPassCache> m_renderPassCache;
std::optional<TextureSamplerCache> m_samplerCache;
std::shared_ptr<DirectoryModuleResolver> m_shaderModuleResolver;
std::shared_ptr<FilesystemModuleResolver> m_shaderModuleResolver;
std::shared_ptr<RenderBuffer> m_fullscreenVertexBuffer;
std::shared_ptr<RenderDevice> m_renderDevice;
std::shared_ptr<RenderPipeline> m_blitPipeline;

View File

@@ -63,7 +63,7 @@ namespace Nz
return *m_samplerCache;
}
inline const std::shared_ptr<DirectoryModuleResolver>& Graphics::GetShaderModuleResolver() const
inline const std::shared_ptr<FilesystemModuleResolver>& Graphics::GetShaderModuleResolver() const
{
return m_shaderModuleResolver;
}

View File

@@ -30,7 +30,7 @@
#define NAZARA_GLOBAL_SHADER_HPP
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/DirectoryModuleResolver.hpp>
#include <Nazara/Shader/FilesystemModuleResolver.hpp>
#include <Nazara/Shader/GlslWriter.hpp>
#include <Nazara/Shader/LangWriter.hpp>
#include <Nazara/Shader/Shader.hpp>

View File

@@ -56,6 +56,9 @@ namespace Nz::ShaderAst
if (!Compare(lhs.moduleId, rhs.moduleId))
return false;
if (!Compare(lhs.moduleName, rhs.moduleName))
return false;
if (!Compare(lhs.shaderLangVersion, rhs.shaderLangVersion))
return false;
@@ -601,7 +604,7 @@ namespace Nz::ShaderAst
bool Compare(const ImportStatement& lhs, const ImportStatement& rhs)
{
if (!Compare(lhs.modulePath, rhs.modulePath))
if (!Compare(lhs.moduleName, rhs.moduleName))
return false;
return true;

View File

@@ -25,7 +25,7 @@ namespace Nz::ShaderAst
struct ImportedModule;
struct Metadata;
inline Module(UInt32 shaderLangVersion, const Uuid& moduleId = Uuid::Generate());
inline Module(UInt32 shaderLangVersion, std::string moduleName, const Uuid& moduleId = Uuid::Generate());
inline Module(std::shared_ptr<const Metadata> metadata, std::vector<ImportedModule> importedModules = {});
inline Module(std::shared_ptr<const Metadata> metadata, MultiStatementPtr rootNode, std::vector<ImportedModule> importedModules = {});
Module(const Module&) = default;
@@ -43,6 +43,7 @@ namespace Nz::ShaderAst
struct Metadata
{
std::string moduleName;
UInt32 shaderLangVersion;
Uuid moduleId;
};

View File

@@ -8,10 +8,11 @@
namespace Nz::ShaderAst
{
inline Module::Module(UInt32 shaderLangVersion, const Uuid& uuid)
inline Module::Module(UInt32 shaderLangVersion, std::string moduleName, const Uuid& uuid)
{
auto mutMetadata = std::make_shared<Metadata>();
mutMetadata->moduleId = uuid;
mutMetadata->moduleName = std::move(moduleName);
mutMetadata->shaderLangVersion = shaderLangVersion;
metadata = std::move(mutMetadata);

View File

@@ -414,7 +414,7 @@ namespace Nz::ShaderAst
NodeType GetType() const override;
void Visit(AstStatementVisitor& visitor) override;
std::vector<std::string> modulePath;
std::string moduleName;
};
struct NAZARA_SHADER_API MultiStatement : Statement

View File

@@ -1,45 +0,0 @@
// 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_DIRECTORYMODULERESOLVER_HPP
#define NAZARA_SHADER_DIRECTORYMODULERESOLVER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/VirtualDirectory.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderModuleResolver.hpp>
#include <string>
#include <unordered_map>
namespace Nz
{
class NAZARA_SHADER_API DirectoryModuleResolver : public ShaderModuleResolver
{
public:
inline DirectoryModuleResolver();
DirectoryModuleResolver(const DirectoryModuleResolver&) = delete;
DirectoryModuleResolver(DirectoryModuleResolver&&) = delete;
~DirectoryModuleResolver() = default;
inline void RegisterModuleDirectory(std::string_view path, std::filesystem::path realPath);
inline void RegisterModuleFile(std::string_view path, std::filesystem::path realPath);
inline void RegisterModuleFile(std::string_view path, std::vector<UInt8> fileContent);
inline void RegisterModuleFile(std::string_view path, const void* staticData, std::size_t size);
ShaderAst::ModulePtr Resolve(const std::vector<std::string>& modulePath) override;
DirectoryModuleResolver& operator=(const DirectoryModuleResolver&) = delete;
DirectoryModuleResolver& operator=(DirectoryModuleResolver&&) = delete;
private:
std::shared_ptr<VirtualDirectory> m_searchDirectory;
std::unordered_map<std::string, ShaderAst::ModulePtr> m_knownModules;
};
}
#include <Nazara/Shader/DirectoryModuleResolver.inl>
#endif // NAZARA_SHADER_DIRECTORYMODULERESOLVER_HPP

View File

@@ -1,36 +0,0 @@
// 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/DirectoryModuleResolver.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
inline DirectoryModuleResolver::DirectoryModuleResolver() :
m_searchDirectory(std::make_shared<VirtualDirectory>())
{
}
inline void DirectoryModuleResolver::RegisterModuleDirectory(std::string_view path, std::filesystem::path realPath)
{
m_searchDirectory->StoreDirectory(path, realPath);
}
inline void DirectoryModuleResolver::RegisterModuleFile(std::string_view path, std::filesystem::path realPath)
{
m_searchDirectory->StoreFile(path, realPath);
}
inline void DirectoryModuleResolver::RegisterModuleFile(std::string_view path, std::vector<UInt8> fileContent)
{
m_searchDirectory->StoreFile(path, std::move(fileContent));
}
inline void DirectoryModuleResolver::RegisterModuleFile(std::string_view path, const void* staticData, std::size_t size)
{
m_searchDirectory->StoreFile(path, staticData, size);
}
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -0,0 +1,44 @@
// 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_FILESYSTEMMODULERESOLVER_HPP
#define NAZARA_SHADER_FILESYSTEMMODULERESOLVER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderModuleResolver.hpp>
#include <filesystem>
#include <string>
#include <unordered_map>
namespace Nz
{
class NAZARA_SHADER_API FilesystemModuleResolver : public ShaderModuleResolver
{
public:
FilesystemModuleResolver() = default;
FilesystemModuleResolver(const FilesystemModuleResolver&) = default;
FilesystemModuleResolver(FilesystemModuleResolver&&) = default;
~FilesystemModuleResolver() = default;
void RegisterModule(std::filesystem::path realPath);
void RegisterModule(std::string_view moduleSource);
void RegisterModule(ShaderAst::ModulePtr module);
void RegisterModuleDirectory(std::filesystem::path realPath);
ShaderAst::ModulePtr Resolve(const std::string& moduleName) override;
FilesystemModuleResolver& operator=(const FilesystemModuleResolver&) = default;
FilesystemModuleResolver& operator=(FilesystemModuleResolver&&) = default;
private:
std::unordered_map<std::string, ShaderAst::ModulePtr> m_modules;
};
}
#include <Nazara/Shader/FilesystemModuleResolver.inl>
#endif // NAZARA_SHADER_FILESYSTEMMODULERESOLVER_HPP

View File

@@ -0,0 +1,12 @@
// 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/FilesystemModuleResolver.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -139,7 +139,7 @@ namespace Nz::ShaderBuilder
struct Import
{
inline ShaderAst::ImportStatementPtr operator()(std::vector<std::string> modulePath) const;
inline ShaderAst::ImportStatementPtr operator()(std::string modulePath) const;
};
struct Intrinsic

View File

@@ -365,10 +365,10 @@ namespace Nz::ShaderBuilder
return identifierNode;
}
inline ShaderAst::ImportStatementPtr Impl::Import::operator()(std::vector<std::string> modulePath) const
inline ShaderAst::ImportStatementPtr Impl::Import::operator()(std::string moduleName) const
{
auto importNode = std::make_unique<ShaderAst::ImportStatement>();
importNode->modulePath = std::move(modulePath);
importNode->moduleName = std::move(moduleName);
return importNode;
}

View File

@@ -121,6 +121,7 @@ namespace Nz::ShaderLang
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
const std::string& ParseIdentifierAsName();
std::string ParseModuleName();
ShaderAst::ExpressionPtr ParseType();
static int GetTokenPrecedence(TokenType token);

View File

@@ -28,7 +28,7 @@ namespace Nz
ShaderModuleResolver(ShaderModuleResolver&&) = default;
virtual ~ShaderModuleResolver();
virtual ShaderAst::ModulePtr Resolve(const std::vector<std::string>& /*modulePath*/) = 0;
virtual ShaderAst::ModulePtr Resolve(const std::string& /*moduleName*/) = 0;
ShaderModuleResolver& operator=(const ShaderModuleResolver&) = default;
ShaderModuleResolver& operator=(ShaderModuleResolver&&) = default;