Add support for shader hotreloading

This commit is contained in:
Jérôme Leclercq
2022-03-17 21:36:36 +01:00
parent 667a4a0c08
commit 615509d1ba
16 changed files with 285 additions and 40 deletions

View File

@@ -20,6 +20,8 @@ namespace Nz
// std::string is assumed to contains UTF-8
NAZARA_CORE_API std::size_t ComputeCharacterCount(const std::string_view& str);
inline bool EndsWith(const std::string_view& str, const std::string_view& s);
NAZARA_CORE_API std::string FromUtf16String(const std::u16string_view& u16str);
NAZARA_CORE_API std::string FromUtf32String(const std::u32string_view& u32str);
NAZARA_CORE_API std::string FromWideString(const std::wstring_view& str);

View File

@@ -10,6 +10,20 @@
namespace Nz
{
inline bool EndsWith(const std::string_view& str, const std::string_view& s)
{
//FIXME: Replace with proper C++20 value once it's available
#if __cplusplus > 201703L
// C++20
return str.ends_with(s);
#else
if (s.size() > str.size())
return false;
return str.compare(str.size() - s.size(), s.size(), s.data()) == 0;
#endif
}
inline bool IsNumber(std::string_view str)
{
if (str.empty())

View File

@@ -35,6 +35,8 @@ namespace Nz
{
public:
MaterialPass(std::shared_ptr<const MaterialSettings> settings);
MaterialPass(const MaterialPass&) = delete;
MaterialPass(MaterialPass&&) = delete;
inline ~MaterialPass();
inline void Configure(std::shared_ptr<MaterialPipeline> pipeline);
@@ -105,6 +107,9 @@ namespace Nz
void Update(RenderFrame& renderFrame, CommandBufferBuilder& builder);
MaterialPass& operator=(const MaterialPass&) = delete;
MaterialPass& operator=(MaterialPass&&) = delete;
// Signals:
NazaraSignal(OnMaterialPassInvalidated, const MaterialPass* /*materialPass*/);
NazaraSignal(OnMaterialPassPipelineInvalidated, const MaterialPass* /*materialPass*/);
@@ -125,6 +130,11 @@ namespace Nz
TextureSamplerInfo samplerInfo;
};
struct ShaderEntry
{
NazaraSlot(UberShader, OnShaderUpdated, onShaderUpdated);
};
struct UniformBuffer
{
std::shared_ptr<RenderBuffer> buffer;
@@ -135,6 +145,7 @@ namespace Nz
std::array<ShaderAst::ConstantValue, 64> m_optionValues;
std::shared_ptr<const MaterialSettings> m_settings;
std::vector<MaterialTexture> m_textures;
std::vector<ShaderEntry> m_shaders;
std::vector<UniformBuffer> m_uniformBuffers;
mutable std::shared_ptr<MaterialPipeline> m_pipeline;
mutable MaterialPipelineInfo m_pipelineInfo;

View File

@@ -67,7 +67,13 @@ namespace Nz
static bool Initialize();
static void Uninitialize();
struct UberShaderEntry
{
NazaraSlot(UberShader, OnShaderUpdated, onShaderUpdated);
};
mutable std::vector<std::shared_ptr<RenderPipeline>> m_renderPipelines;
std::vector<UberShaderEntry> m_uberShaderEntries;
MaterialPipelineInfo m_pipelineInfo;
using PipelineCache = std::unordered_map<MaterialPipelineInfo, std::shared_ptr<MaterialPipeline>>;

View File

@@ -12,6 +12,15 @@ namespace Nz
inline MaterialPipeline::MaterialPipeline(const MaterialPipelineInfo& pipelineInfo, Token) :
m_pipelineInfo(pipelineInfo)
{
m_uberShaderEntries.resize(m_pipelineInfo.shaders.size());
for (std::size_t i = 0; i < m_uberShaderEntries.size(); ++i)
{
m_uberShaderEntries[i].onShaderUpdated.Connect(m_pipelineInfo.shaders[i].uberShader->OnShaderUpdated, [this](UberShader*)
{
// Clear cache
m_renderPipelines.clear();
});
}
}
/*!

View File

@@ -10,8 +10,10 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/RenderPipeline.hpp>
#include <Nazara/Shader/ShaderModuleResolver.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <unordered_map>
@@ -26,6 +28,8 @@ namespace Nz
struct Option;
using ConfigCallback = std::function<void(Config& config, const std::vector<RenderPipelineInfo::VertexBufferData>& vertexBuffers)>;
UberShader(ShaderStageTypeFlags shaderStages, std::string moduleName);
UberShader(ShaderStageTypeFlags shaderStages, ShaderModuleResolver& moduleResolver, std::string moduleName);
UberShader(ShaderStageTypeFlags shaderStages, ShaderAst::ModulePtr shaderModule);
~UberShader() = default;
@@ -58,7 +62,13 @@ namespace Nz
UInt32 hash;
};
NazaraSignal(OnShaderUpdated, UberShader* /*uberShader*/);
private:
void Validate(ShaderAst::Module& module);
NazaraSlot(ShaderModuleResolver, OnModuleUpdated, m_onShaderModuleUpdated);
std::unordered_map<Config, std::shared_ptr<ShaderModule>, ConfigHasher, ConfigEqual> m_combinations;
std::unordered_map<std::string, Option> m_optionIndexByName;
ShaderAst::ModulePtr m_shaderModule;

View File

@@ -8,9 +8,11 @@
#define NAZARA_SHADER_FILESYSTEMMODULERESOLVER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderModuleResolver.hpp>
#include <filesystem>
#include <memory>
#include <string>
#include <unordered_map>
@@ -19,23 +21,32 @@ namespace Nz
class NAZARA_SHADER_API FilesystemModuleResolver : public ShaderModuleResolver
{
public:
FilesystemModuleResolver() = default;
FilesystemModuleResolver(const FilesystemModuleResolver&) = default;
FilesystemModuleResolver(FilesystemModuleResolver&&) = default;
~FilesystemModuleResolver() = default;
FilesystemModuleResolver();
FilesystemModuleResolver(const FilesystemModuleResolver&) = delete;
FilesystemModuleResolver(FilesystemModuleResolver&&) noexcept = delete;
~FilesystemModuleResolver();
void RegisterModule(const std::filesystem::path& realPath);
void RegisterModule(std::string_view moduleSource);
void RegisterModule(ShaderAst::ModulePtr module);
void RegisterModuleDirectory(const std::filesystem::path& realPath);
void RegisterModuleDirectory(const std::filesystem::path& realPath, bool watchDirectory = true);
ShaderAst::ModulePtr Resolve(const std::string& moduleName) override;
FilesystemModuleResolver& operator=(const FilesystemModuleResolver&) = default;
FilesystemModuleResolver& operator=(FilesystemModuleResolver&&) = default;
FilesystemModuleResolver& operator=(const FilesystemModuleResolver&) = delete;
FilesystemModuleResolver& operator=(FilesystemModuleResolver&&) noexcept = delete;
static constexpr const char* ModuleExtension = ".nzsl";
private:
void OnFileAdded(std::string_view directory, std::string_view filename);
void OnFileRemoved(std::string_view directory, std::string_view filename);
void OnFileMoved(std::string_view directory, std::string_view filename, std::string_view oldFilename);
void OnFileUpdated(std::string_view directory, std::string_view filename);
std::unordered_map<std::string, std::string> m_moduleByFilepath;
std::unordered_map<std::string, ShaderAst::ModulePtr> m_modules;
MovablePtr<void> m_fileWatcher;
};
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_SHADER_SHADERMODULERESOLVER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Shader/Config.hpp>
#include <memory>
#include <string>
@@ -32,6 +33,8 @@ namespace Nz
ShaderModuleResolver& operator=(const ShaderModuleResolver&) = default;
ShaderModuleResolver& operator=(ShaderModuleResolver&&) = default;
NazaraSignal(OnModuleUpdated, ShaderModuleResolver* /*resolver*/, const std::string& /*moduleName*/);
};
}