Shader: Add module resolver + use modules for engine shaders

This commit is contained in:
Jérôme Leclercq
2022-03-10 21:00:10 +01:00
parent 98bd04e35a
commit db0c1e6e8c
30 changed files with 737 additions and 106 deletions

View File

@@ -4,8 +4,8 @@
#pragma once
#ifndef NAZARA_SHADER_AST_INDEXREMAPPER_HPP
#define NAZARA_SHADER_AST_INDEXREMAPPER_HPP
#ifndef NAZARA_SHADER_AST_INDEXREMAPPERVISITOR_HPP
#define NAZARA_SHADER_AST_INDEXREMAPPERVISITOR_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
@@ -60,4 +60,4 @@ namespace Nz::ShaderAst
#include <Nazara/Shader/Ast/IndexRemapperVisitor.inl>
#endif // NAZARA_SHADER_AST_INDEXREMAPPER_HPP
#endif // NAZARA_SHADER_AST_INDEXREMAPPERVISITOR_HPP

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderModuleResolver.hpp>
#include <Nazara/Shader/Ast/AstCloner.hpp>
#include <Nazara/Shader/Ast/AstTypes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
@@ -42,7 +43,7 @@ namespace Nz::ShaderAst
struct Options
{
std::function<ModulePtr(const std::vector<std::string>& /*modulePath*/)> moduleCallback;
std::shared_ptr<ShaderModuleResolver> moduleResolver;
std::unordered_set<std::string> reservedIdentifiers;
std::unordered_map<UInt32, ConstantValue> optionValues;
bool makeVariableNameUnique = false;

View File

@@ -0,0 +1,45 @@
// 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

@@ -0,0 +1,36 @@
// 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

@@ -13,6 +13,7 @@
#include <Nazara/Shader/Ast/AstExpressionVisitorExcept.hpp>
#include <Nazara/Shader/Ast/AstStatementVisitorExcept.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
#include <set>
#include <sstream>
#include <string>
@@ -48,7 +49,7 @@ namespace Nz
};
static const char* GetFlipYUniformName();
static ShaderAst::ModulePtr Sanitize(const ShaderAst::Module& module, std::unordered_map<UInt32, ShaderAst::ConstantValue> optionValues, std::string* error = nullptr);
static ShaderAst::SanitizeVisitor::Options GetSanitizeOptions();
private:
void Append(const ShaderAst::AliasType& aliasType);

View File

@@ -0,0 +1,40 @@
// 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_SHADERMODULERESOLVER_HPP
#define NAZARA_SHADER_SHADERMODULERESOLVER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <memory>
#include <string>
#include <vector>
namespace Nz
{
namespace ShaderAst
{
using ModulePtr = std::shared_ptr<class Module>;
}
class NAZARA_SHADER_API ShaderModuleResolver
{
public:
ShaderModuleResolver() = default;
ShaderModuleResolver(const ShaderModuleResolver&) = default;
ShaderModuleResolver(ShaderModuleResolver&&) = default;
virtual ~ShaderModuleResolver();
virtual ShaderAst::ModulePtr Resolve(const std::vector<std::string>& /*modulePath*/) = 0;
ShaderModuleResolver& operator=(const ShaderModuleResolver&) = default;
ShaderModuleResolver& operator=(ShaderModuleResolver&&) = default;
};
}
#include <Nazara/Shader/ShaderModuleResolver.inl>
#endif // NAZARA_SHADER_SHADERMODULERESOLVER_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/ShaderModuleResolver.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -10,11 +10,14 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/ConstantValue.hpp>
#include <memory>
#include <string>
#include <unordered_map>
namespace Nz
{
class ShaderModuleResolver;
class NAZARA_SHADER_API ShaderWriter
{
public:
@@ -27,6 +30,7 @@ namespace Nz
struct States
{
std::shared_ptr<ShaderModuleResolver> shaderModuleResolver;
std::unordered_map<UInt32, ShaderAst::ConstantValue> optionValues;
bool optimize = false;
bool sanitized = false;