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

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
external
{

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
external
{

View File

@ -1,8 +1,8 @@
[nzsl_version("1.0")]
module;
import Engine/InstanceData;
import Engine/ViewerData;
import Engine.InstanceData;
import Engine.ViewerData;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;

View File

@ -1,8 +1,8 @@
[nzsl_version("1.0")]
module;
import Engine/InstanceData;
import Engine/ViewerData;
import Engine.InstanceData;
import Engine.ViewerData;
[layout(std140)]
struct BasicSettings

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
[layout(std140)]
struct BlurData

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
[layout(std140)]
struct Settings

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
[layout(std140)]
struct PointLight

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
external
{

View File

@ -1,7 +1,7 @@
[nzsl_version("1.0")]
module;
import Engine/ViewerData;
import Engine.ViewerData;
external
{

View File

@ -12,7 +12,7 @@ NAZARA_REQUEST_DEDICATED_GPU()
const char barModuleSource[] = R"(
[nzsl_version("1.0")]
[uuid("4BB09DEE-F70A-442E-859F-E8F2F3F8583D")]
module;
module Test.Bar;
fn dummy() {}
@ -26,9 +26,9 @@ struct Bar
const char dataModuleSource[] = R"(
[nzsl_version("1.0")]
[uuid("E49DC9AD-469C-462C-9719-A6F012372029")]
module;
module Test.Data;
import Test/Bar;
import Test.Bar;
struct Foo {}
@ -47,8 +47,8 @@ const char shaderSource[] = R"(
[nzsl_version("1.0")]
module;
import Test/Data;
import Test/Bar;
import Test.Data;
import Test.Bar;
option red: bool = false;
@ -140,9 +140,9 @@ int main()
return __LINE__;
}
auto directoryModuleResolver = std::make_shared<Nz::DirectoryModuleResolver>();
directoryModuleResolver->RegisterModuleFile("Test/Bar", barModuleSource, sizeof(barModuleSource));
directoryModuleResolver->RegisterModuleFile("Test/Data", dataModuleSource, sizeof(dataModuleSource));
auto directoryModuleResolver = std::make_shared<Nz::FilesystemModuleResolver>();
directoryModuleResolver->RegisterModule(std::string_view(barModuleSource));
directoryModuleResolver->RegisterModule(std::string_view(dataModuleSource));
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
sanitizeOpt.moduleResolver = directoryModuleResolver;

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;

View File

@ -221,27 +221,18 @@ namespace Nz
void Graphics::RegisterShaderModules()
{
m_shaderModuleResolver = std::make_shared<DirectoryModuleResolver>();
m_shaderModuleResolver->RegisterModuleFile("Engine/InstanceData", r_instanceDataModule, sizeof(r_instanceDataModule));
m_shaderModuleResolver->RegisterModuleFile("Engine/LightData", r_lightDataModule, sizeof(r_lightDataModule));
m_shaderModuleResolver->RegisterModuleFile("Engine/ViewerData", r_viewerDataModule, sizeof(r_viewerDataModule));
m_shaderModuleResolver = std::make_shared<FilesystemModuleResolver>();
m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast<const char*>(r_instanceDataModule), sizeof(r_instanceDataModule)));
m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast<const char*>(r_lightDataModule), sizeof(r_lightDataModule)));
m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast<const char*>(r_viewerDataModule), sizeof(r_viewerDataModule)));
if (std::filesystem::path shaderPath = "Shaders/Modules"; std::filesystem::is_directory(shaderPath))
m_shaderModuleResolver->RegisterModuleDirectory(shaderPath);
#ifdef NAZARA_DEBUG
// Override embed files with dev files in debug
std::filesystem::path modulePath = "../../src/Nazara/Graphics/Resources/Shaders/Modules";
if (std::filesystem::is_directory(modulePath))
{
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(modulePath))
{
if (!dirEntry.is_regular_file())
continue;
std::filesystem::path filePath = std::filesystem::relative(dirEntry.path(), modulePath);
filePath.replace_extension();
m_shaderModuleResolver->RegisterModuleFile(filePath.generic_u8string(), dirEntry.path());
}
}
if (std::filesystem::path modulePath = "../../src/Nazara/Graphics/Resources/Shaders/Modules"; std::filesystem::is_directory(modulePath))
m_shaderModuleResolver->RegisterModuleDirectory(modulePath);
#endif
}

View File

@ -1,6 +1,6 @@
[nzsl_version("1.0")]
module;
module Engine.InstanceData;
[export]
[layout(std140)]
struct InstanceData

View File

@ -1,5 +1,5 @@
[nzsl_version("1.0")]
module;
module Engine.LightData;
option MaxLightCount: u32 = u32(3); //< FIXME: Fix integral value types

View File

@ -1,5 +1,5 @@
[nzsl_version("1.0")]
module;
module Engine.ViewerData;
[export]
[layout(std140)]

View File

@ -1,8 +1,8 @@
[nzsl_version("1.0")]
module;
import Engine/InstanceData;
import Engine/ViewerData;
import Engine.InstanceData;
import Engine.ViewerData;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;

View File

@ -1,8 +1,8 @@
[nzsl_version("1.0")]
module;
import Engine/InstanceData;
import Engine/ViewerData;
import Engine.InstanceData;
import Engine.ViewerData;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;

View File

@ -1,9 +1,9 @@
[nzsl_version("1.0")]
module;
import Engine/InstanceData;
import Engine/LightData;
import Engine/ViewerData;
import Engine.InstanceData;
import Engine.LightData;
import Engine.ViewerData;
// Basic material options
option HasDiffuseTexture: bool = false;

View File

@ -228,7 +228,7 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(ImportStatement& node)
{
auto clone = std::make_unique<ImportStatement>();
clone->modulePath = node.modulePath;
clone->moduleName = node.moduleName;
return clone;
}

View File

@ -340,9 +340,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(ImportStatement& node)
{
Container(node.modulePath);
for (auto& path : node.modulePath)
Value(path);
Value(node.moduleName);
}
void AstSerializerBase::Serialize(MultiStatement& node)
@ -385,6 +383,7 @@ namespace Nz::ShaderAst
void ShaderAstSerializer::SerializeModule(ModulePtr& module)
{
m_stream << module->metadata->moduleName;
m_stream << module->metadata->moduleId;
m_stream << module->metadata->shaderLangVersion;
@ -604,6 +603,7 @@ namespace Nz::ShaderAst
void ShaderAstUnserializer::SerializeModule(ModulePtr& module)
{
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
m_stream >> metadata->moduleName;
m_stream >> metadata->moduleId;
m_stream >> metadata->shaderLangVersion;

View File

@ -1448,27 +1448,9 @@ namespace Nz::ShaderAst
if (!m_context->options.moduleResolver)
return static_unique_pointer_cast<ImportStatement>(AstCloner::Clone(node));
auto ModulePathAsString = [&]() -> std::string
{
std::ostringstream ss;
bool first = true;
for (const std::string& part : node.modulePath)
{
if (!first)
ss << "/";
ss << part;
first = false;
}
return ss.str();
};
ModulePtr targetModule = m_context->options.moduleResolver->Resolve(node.modulePath);
ModulePtr targetModule = m_context->options.moduleResolver->Resolve(node.moduleName);
if (!targetModule)
throw AstError{ "module " + ModulePathAsString() + " not found" };
throw AstError{ "module " + node.moduleName + " not found" };
std::size_t moduleIndex;
@ -1500,7 +1482,7 @@ namespace Nz::ShaderAst
std::string error;
sanitizedModule->rootNode = SanitizeInternal(*targetModule->rootNode, &error);
if (!sanitizedModule)
throw AstError{ "module " + ModulePathAsString() + " compilation failed: " + error };
throw AstError{ "module " + node.moduleName + " compilation failed: " + error };
moduleIndex = m_context->modules.size();

View File

@ -1,56 +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/ShaderLangParser.hpp>
#include <filesystem>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
ShaderAst::ModulePtr DirectoryModuleResolver::Resolve(const std::vector<std::string>& modulePath)
{
if (modulePath.empty())
return {};
std::string fullPath;
for (const auto& part : modulePath)
{
if (!fullPath.empty())
fullPath += '/';
fullPath += part;
}
if (auto it = m_knownModules.find(fullPath); it != m_knownModules.end())
return it->second;
ShaderAst::ModulePtr shaderModule;
m_searchDirectory->GetEntry(fullPath, [&](const VirtualDirectory::Entry& entry)
{
if (std::holds_alternative<VirtualDirectory::DataPointerEntry>(entry))
{
const auto& dataContent = std::get<VirtualDirectory::DataPointerEntry>(entry);
shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(dataContent.data), dataContent.size));
}
else if (std::holds_alternative<VirtualDirectory::FileContentEntry>(entry))
{
const auto& fileContent = std::get<VirtualDirectory::FileContentEntry>(entry);
shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(fileContent.data.data()), fileContent.data.size()));
}
else if (std::holds_alternative<VirtualDirectory::PhysicalFileEntry>(entry))
{
const auto& physicalEntry = std::get<VirtualDirectory::PhysicalFileEntry>(entry);
shaderModule = ShaderLang::ParseFromFile(physicalEntry.filePath);
}
});
if (!shaderModule)
return {};
m_knownModules.emplace(std::move(fullPath), shaderModule);
return shaderModule;
}
}

View File

@ -0,0 +1,61 @@
// 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/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <cassert>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
void FilesystemModuleResolver::RegisterModule(std::filesystem::path realPath)
{
return RegisterModule(ShaderLang::ParseFromFile(realPath));
}
void FilesystemModuleResolver::RegisterModule(std::string_view moduleSource)
{
return RegisterModule(ShaderLang::Parse(moduleSource));
}
void FilesystemModuleResolver::RegisterModule(ShaderAst::ModulePtr module)
{
assert(module);
std::string moduleName = module->metadata->moduleName;
if (moduleName.empty())
throw std::runtime_error("cannot register anonymous module");
m_modules.emplace(std::move(moduleName), std::move(module));
}
void FilesystemModuleResolver::RegisterModuleDirectory(std::filesystem::path realPath)
{
for (const auto& entry : std::filesystem::recursive_directory_iterator(realPath))
{
if (entry.is_regular_file() && StringEqual(entry.path().extension().generic_u8string(), "nzsl", Nz::CaseIndependent{}))
{
try
{
RegisterModule(entry.path());
}
catch (const std::exception& e)
{
NazaraWarning("failed to register module " + entry.path().generic_u8string() + ": " + e.what());
}
}
}
}
ShaderAst::ModulePtr FilesystemModuleResolver::Resolve(const std::string& moduleName)
{
auto it = m_modules.find(moduleName);
if (it == m_modules.end())
return {};
return it->second;
}
}

View File

@ -1144,20 +1144,7 @@ namespace Nz
void LangWriter::Visit(ShaderAst::ImportStatement& node)
{
Append("import ");
bool first = true;
for (const std::string& path : node.modulePath)
{
if (!first)
Append("/");
Append(path);
first = false;
}
AppendLine(";");
Append("import ", node.moduleName, ";");
}
void LangWriter::Visit(ShaderAst::IntrinsicExpression& node)

View File

@ -335,17 +335,12 @@ namespace Nz::ShaderLang
if (!moduleId)
moduleId = Uuid::Generate();
auto module = std::make_shared<ShaderAst::Module>(*moduleVersion, *moduleId);
if (Peek().type == TokenType::Identifier)
if (m_context->module)
{
std::string moduleName = ParseModuleName();
auto module = std::make_shared<ShaderAst::Module>(*moduleVersion, std::move(moduleName), *moduleId);
// Imported module
if (!m_context->module)
throw UnexpectedToken{}; //< "unexpected token before module declaration"
const std::string& identifier = std::get<std::string>(Peek().data);
Consume();
Expect(Advance(), TokenType::OpenCurlyBracket);
m_context->parsingImportedModule = true;
@ -364,10 +359,17 @@ namespace Nz::ShaderLang
auto& importedModule = m_context->module->importedModules.emplace_back();
importedModule.module = std::move(module);
importedModule.identifier = identifier;
importedModule.identifier = importedModule.module->metadata->moduleName;
}
else
{
std::string moduleName;
if (Peek().type == TokenType::Identifier)
moduleName = ParseModuleName();
auto module = std::make_shared<ShaderAst::Module>(*moduleVersion, std::move(moduleName), *moduleId);
// First declaration
Expect(Advance(), TokenType::Semicolon);
if (m_context->module)
@ -732,19 +734,11 @@ namespace Nz::ShaderLang
{
Expect(Advance(), TokenType::Import);
std::vector<std::string> modulePath;
modulePath.push_back(ParseIdentifierAsName());
while (Peek().type == TokenType::Divide) //< /
{
Consume();
modulePath.push_back(ParseIdentifierAsName());
}
std::string moduleName = ParseModuleName();
Expect(Advance(), TokenType::Semicolon);
return ShaderBuilder::Import(std::move(modulePath));
return ShaderBuilder::Import(std::move(moduleName));
}
ShaderAst::StatementPtr Parser::ParseOptionDeclaration()
@ -1371,6 +1365,19 @@ namespace Nz::ShaderLang
return std::get<std::string>(identifierToken.data);
}
std::string Parser::ParseModuleName()
{
std::string moduleName = ParseIdentifierAsName();
while (Peek().type == TokenType::Dot)
{
Consume();
moduleName += '.';
moduleName += ParseIdentifierAsName();
}
return moduleName;
}
ShaderAst::ExpressionPtr Parser::ParseType()
{
// Handle () as no type

View File

@ -1,7 +1,7 @@
#include <Engine/Shader/ShaderUtils.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Shader/DirectoryModuleResolver.hpp>
#include <Nazara/Shader/FilesystemModuleResolver.hpp>
#include <Nazara/Shader/LangWriter.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
@ -17,7 +17,7 @@ TEST_CASE("Modules", "[Shader]")
std::string_view importedSource = R"(
[nzsl_version("1.0")]
[uuid("ad3aed6e-0619-4a26-b5ce-abc2ec0836c4")]
module;
module SimpleModule;
[layout(std140)]
struct Data
@ -69,8 +69,8 @@ fn main(input: InputData) -> OutputData
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(shaderSource);
auto directoryModuleResolver = std::make_shared<Nz::DirectoryModuleResolver>();
directoryModuleResolver->RegisterModuleFile("SimpleModule", importedSource.data(), importedSource.size());
auto directoryModuleResolver = std::make_shared<Nz::FilesystemModuleResolver>();
directoryModuleResolver->RegisterModule(importedSource);
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
sanitizeOpt.moduleResolver = directoryModuleResolver;
@ -202,7 +202,7 @@ OpFunctionEnd)");
std::string_view dataModule = R"(
[nzsl_version("1.0")]
[uuid("ad3aed6e-0619-4a26-b5ce-abc2ec0836c4")]
module;
module Modules.Data;
fn dummy() {}
@ -217,9 +217,9 @@ struct Data
std::string_view blockModule = R"(
[nzsl_version("1.0")]
[uuid("7a548506-89e6-4944-897f-4f695a8bca01")]
module;
module Modules.Block;
import Modules/Data;
import Modules.Data;
[export]
[layout(std140)]
@ -234,7 +234,7 @@ struct Unused {}
std::string_view inputOutputModule = R"(
[nzsl_version("1.0")]
[uuid("e66c6e98-fc37-4390-a7e1-c81508ff8e49")]
module;
module Modules.InputOutput;
[export]
struct InputData
@ -253,8 +253,8 @@ struct OutputData
[nzsl_version("1.0")]
module;
import Modules/Block;
import Modules/InputOutput;
import Modules.Block;
import Modules.InputOutput;
external
{
@ -272,10 +272,10 @@ fn main(input: InputData) -> OutputData
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(shaderSource);
auto directoryModuleResolver = std::make_shared<Nz::DirectoryModuleResolver>();
directoryModuleResolver->RegisterModuleFile("Modules/Data", dataModule.data(), dataModule.size());
directoryModuleResolver->RegisterModuleFile("Modules/Block", blockModule.data(), blockModule.size());
directoryModuleResolver->RegisterModuleFile("Modules/InputOutput", inputOutputModule.data(), inputOutputModule.size());
auto directoryModuleResolver = std::make_shared<Nz::FilesystemModuleResolver>();
directoryModuleResolver->RegisterModule(dataModule);
directoryModuleResolver->RegisterModule(blockModule);
directoryModuleResolver->RegisterModule(inputOutputModule);
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
sanitizeOpt.moduleResolver = directoryModuleResolver;