From 2f26a1d9c797aef71e4c973750e3e6dc518c0321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sat, 12 Mar 2022 16:53:36 +0100 Subject: [PATCH] Graphics: Move LightData to a shader module and add hotreload in debug --- src/Nazara/Graphics/BasicMaterial.cpp | 21 +++++++++++++++- src/Nazara/Graphics/DepthMaterial.cpp | 21 +++++++++++++++- src/Nazara/Graphics/Graphics.cpp | 23 +++++++++++++++++ .../Shaders/Modules/Engine/LightData.nzsl | 25 +++++++++++++++++++ .../Resources/Shaders/phong_material.nzsl | 22 +--------------- src/Nazara/Shader/DirectoryModuleResolver.cpp | 2 +- 6 files changed, 90 insertions(+), 24 deletions(-) create mode 100644 src/Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl diff --git a/src/Nazara/Graphics/BasicMaterial.cpp b/src/Nazara/Graphics/BasicMaterial.cpp index 9c952f12d..01352dd08 100644 --- a/src/Nazara/Graphics/BasicMaterial.cpp +++ b/src/Nazara/Graphics/BasicMaterial.cpp @@ -236,7 +236,26 @@ namespace Nz std::vector> BasicMaterial::BuildShaders() { - ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast(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(r_shader), sizeof(r_shader))); + auto shader = std::make_shared(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule)); return { std::move(shader) }; diff --git a/src/Nazara/Graphics/DepthMaterial.cpp b/src/Nazara/Graphics/DepthMaterial.cpp index e8fbf6fa1..be2862009 100644 --- a/src/Nazara/Graphics/DepthMaterial.cpp +++ b/src/Nazara/Graphics/DepthMaterial.cpp @@ -18,7 +18,26 @@ namespace Nz std::vector> DepthMaterial::BuildShaders() { - ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast(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(r_shader), sizeof(r_shader))); + auto shader = std::make_shared(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule)); return { std::move(shader) }; diff --git a/src/Nazara/Graphics/Graphics.cpp b/src/Nazara/Graphics/Graphics.cpp index b1ec12a7f..957c59dfc 100644 --- a/src/Nazara/Graphics/Graphics.cpp +++ b/src/Nazara/Graphics/Graphics.cpp @@ -22,6 +22,10 @@ namespace Nz #include }; + const UInt8 r_lightDataModule[] = { + #include + }; + const UInt8 r_viewerDataModule[] = { #include }; @@ -219,7 +223,26 @@ namespace Nz { m_shaderModuleResolver = std::make_shared(); 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() diff --git a/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl b/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl new file mode 100644 index 000000000..f7220d00f --- /dev/null +++ b/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl @@ -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, +} diff --git a/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl b/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl index a81222766..e881fc61b 100644 --- a/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl @@ -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], diff --git a/src/Nazara/Shader/DirectoryModuleResolver.cpp b/src/Nazara/Shader/DirectoryModuleResolver.cpp index ccbd6ca39..4a6e80d6b 100644 --- a/src/Nazara/Shader/DirectoryModuleResolver.cpp +++ b/src/Nazara/Shader/DirectoryModuleResolver.cpp @@ -49,7 +49,7 @@ namespace Nz if (!shaderModule) return {}; - m_knownModules.emplace(fullPath, shaderModule); + m_knownModules.emplace(std::move(fullPath), shaderModule); return shaderModule; }