Graphics: Move LightData to a shader module and add hotreload in debug

This commit is contained in:
Jérôme Leclercq 2022-03-12 16:53:36 +01:00
parent b92a9f8a1c
commit 2f26a1d9c7
6 changed files with 90 additions and 24 deletions

View File

@ -236,7 +236,26 @@ namespace Nz
std::vector<std::shared_ptr<UberShader>> BasicMaterial::BuildShaders()
{
ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
ShaderAst::ModulePtr shaderModule;
#ifdef NAZARA_DEBUG
std::filesystem::path shaderPath = "../../src/Nazara/Graphics/Resources/Shaders/basic_material.nzsl";
if (std::filesystem::exists(shaderPath))
{
try
{
shaderModule = ShaderLang::ParseFromFile(shaderPath);
}
catch (const std::exception& e)
{
NazaraError(std::string("failed to load shader from engine folder: ") + e.what());
}
}
#endif
if (!shaderModule)
shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule));
return { std::move(shader) };

View File

@ -18,7 +18,26 @@ namespace Nz
std::vector<std::shared_ptr<UberShader>> DepthMaterial::BuildShaders()
{
ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
ShaderAst::ModulePtr shaderModule;
#ifdef NAZARA_DEBUG
std::filesystem::path shaderPath = "../../src/Nazara/Graphics/Resources/Shaders/depth_material.nzsl";
if (std::filesystem::exists(shaderPath))
{
try
{
shaderModule = ShaderLang::ParseFromFile(shaderPath);
}
catch (const std::exception& e)
{
NazaraError(std::string("failed to load shader from engine folder: ") + e.what());
}
}
#endif
if (!shaderModule)
shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule));
return { std::move(shader) };

View File

@ -22,6 +22,10 @@ namespace Nz
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/InstanceData.nzsl.h>
};
const UInt8 r_lightDataModule[] = {
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl.h>
};
const UInt8 r_viewerDataModule[] = {
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl.h>
};
@ -219,7 +223,26 @@ namespace Nz
{
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));
#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());
}
}
#endif
}
void Graphics::SelectDepthStencilFormats()

View File

@ -0,0 +1,25 @@
[nzsl_version("1.0")]
module;
option MaxLightCount: u32 = u32(3); //< FIXME: Fix integral value types
[export]
[layout(std140)]
struct Light
{
type: i32,
color: vec4[f32],
factor: vec2[f32],
parameter1: vec4[f32],
parameter2: vec4[f32],
parameter3: vec4[f32],
hasShadowMapping: u32
}
[export]
[layout(std140)]
struct LightData
{
lights: array[Light, MaxLightCount],
lightCount: u32,
}

View File

@ -2,6 +2,7 @@
module;
import Engine/InstanceData;
import Engine/LightData;
import Engine/ViewerData;
// Basic material options
@ -15,8 +16,6 @@ option HasHeightTexture: bool = false;
option HasNormalTexture: bool = false;
option HasSpecularTexture: bool = false;
option MaxLightCount: u32 = u32(3); //< FIXME: Fix integral value types
// Billboard related options
option Billboard: bool = false;
option BillboardCenterLocation: i32 = -1;
@ -55,25 +54,6 @@ const DirectionalLight = 0;
const PointLight = 1;
const SpotLight = 2;
[layout(std140)]
struct Light
{
type: i32,
color: vec4[f32],
factor: vec2[f32],
parameter1: vec4[f32],
parameter2: vec4[f32],
parameter3: vec4[f32],
hasShadowMapping: u32
}
[layout(std140)]
struct LightData
{
lights: array[Light, MaxLightCount],
lightCount: u32,
}
external
{
[binding(0)] settings: uniform[MaterialSettings],

View File

@ -49,7 +49,7 @@ namespace Nz
if (!shaderModule)
return {};
m_knownModules.emplace(fullPath, shaderModule);
m_knownModules.emplace(std::move(fullPath), shaderModule);
return shaderModule;
}