diff --git a/include/Nazara/Graphics/Graphics.hpp b/include/Nazara/Graphics/Graphics.hpp index e86320954..48e7de553 100644 --- a/include/Nazara/Graphics/Graphics.hpp +++ b/include/Nazara/Graphics/Graphics.hpp @@ -65,6 +65,7 @@ namespace Nz void BuildFullscreenVertexBuffer(); void RegisterMaterialPasses(); void RegisterShaderModules(); + template void RegisterEmbedShaderModule(const UInt8(&content)[N]); void SelectDepthStencilFormats(); std::optional m_renderPassCache; diff --git a/src/Nazara/Graphics/BasicMaterial.cpp b/src/Nazara/Graphics/BasicMaterial.cpp index 573ff2e0a..a02b53d55 100644 --- a/src/Nazara/Graphics/BasicMaterial.cpp +++ b/src/Nazara/Graphics/BasicMaterial.cpp @@ -17,13 +17,6 @@ namespace Nz { - namespace - { - const UInt8 r_basicMaterialShader[] = { - #include - }; - } - BasicMaterial::BasicMaterial(MaterialPass& material) : BasicMaterial(material, NoInit{}) { @@ -236,26 +229,7 @@ namespace Nz std::vector> BasicMaterial::BuildShaders() { - 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_basicMaterialShader), sizeof(r_basicMaterialShader))); - + ShaderAst::ModulePtr shaderModule = Graphics::Instance()->GetShaderModuleResolver()->Resolve("BasicMaterial"); 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 918ae2b59..1a0dccf55 100644 --- a/src/Nazara/Graphics/DepthMaterial.cpp +++ b/src/Nazara/Graphics/DepthMaterial.cpp @@ -9,35 +9,9 @@ namespace Nz { - namespace - { - const UInt8 r_depthMaterialShader[] = { - #include - }; - } - std::vector> DepthMaterial::BuildShaders() { - 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_depthMaterialShader), sizeof(r_depthMaterialShader))); - + ShaderAst::ModulePtr shaderModule = Graphics::Instance()->GetShaderModuleResolver()->Resolve("DepthMaterial"); 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 28c0c67bd..95cdb03d9 100644 --- a/src/Nazara/Graphics/Graphics.cpp +++ b/src/Nazara/Graphics/Graphics.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -15,7 +16,19 @@ namespace Nz namespace { const UInt8 r_blitShader[] = { - #include + #include + }; + + const UInt8 r_basicMaterialShader[] = { + #include + }; + + const UInt8 r_depthMaterialShader[] = { + #include + }; + + const UInt8 r_phongMaterialShader[] = { + #include }; const UInt8 r_instanceDataModule[] = { @@ -74,8 +87,6 @@ namespace Nz m_renderPassCache.emplace(*m_renderDevice); m_samplerCache.emplace(m_renderDevice); - MaterialPipeline::Initialize(); - BuildDefaultTextures(); BuildFullscreenVertexBuffer(); RegisterShaderModules(); @@ -83,6 +94,8 @@ namespace Nz RegisterMaterialPasses(); SelectDepthStencilFormats(); + MaterialPipeline::Initialize(); + Font::SetDefaultAtlas(std::make_shared(*m_renderDevice)); } @@ -222,18 +235,29 @@ namespace Nz void Graphics::RegisterShaderModules() { m_shaderModuleResolver = std::make_shared(); - m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast(r_instanceDataModule), sizeof(r_instanceDataModule))); - m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast(r_lightDataModule), sizeof(r_lightDataModule))); - m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast(r_viewerDataModule), sizeof(r_viewerDataModule))); - - if (std::filesystem::path shaderPath = "Shaders/Modules"; std::filesystem::is_directory(shaderPath)) - m_shaderModuleResolver->RegisterModuleDirectory(shaderPath); + RegisterEmbedShaderModule(r_basicMaterialShader); + RegisterEmbedShaderModule(r_depthMaterialShader); + RegisterEmbedShaderModule(r_phongMaterialShader); + RegisterEmbedShaderModule(r_blitShader); + RegisterEmbedShaderModule(r_instanceDataModule); + RegisterEmbedShaderModule(r_lightDataModule); + RegisterEmbedShaderModule(r_viewerDataModule); #ifdef NAZARA_DEBUG // Override embed files with dev files in debug - if (std::filesystem::path modulePath = "../../src/Nazara/Graphics/Resources/Shaders/Modules"; std::filesystem::is_directory(modulePath)) + if (std::filesystem::path modulePath = "../../src/Nazara/Graphics/Resources/Shaders"; std::filesystem::is_directory(modulePath)) m_shaderModuleResolver->RegisterModuleDirectory(modulePath); #endif + + // Let application register their own shaders + if (std::filesystem::path shaderPath = "Shaders"; std::filesystem::is_directory(shaderPath)) + m_shaderModuleResolver->RegisterModuleDirectory(shaderPath); + } + + template + void Graphics::RegisterEmbedShaderModule(const UInt8(&content)[N]) + { + m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast(content), N)); } void Graphics::SelectDepthStencilFormats() diff --git a/src/Nazara/Graphics/PhongLightingMaterial.cpp b/src/Nazara/Graphics/PhongLightingMaterial.cpp index 2ea04328e..213745ff5 100644 --- a/src/Nazara/Graphics/PhongLightingMaterial.cpp +++ b/src/Nazara/Graphics/PhongLightingMaterial.cpp @@ -17,13 +17,6 @@ namespace Nz { - namespace - { - const UInt8 r_phongMaterialShader[] = { - #include - }; - } - PhongLightingMaterial::PhongLightingMaterial(MaterialPass& material) : BasicMaterial(material, NoInit{}) { @@ -317,26 +310,7 @@ namespace Nz std::vector> PhongLightingMaterial::BuildShaders() { - ShaderAst::ModulePtr shaderModule; - -#ifdef NAZARA_DEBUG - std::filesystem::path shaderPath = "../../src/Nazara/Graphics/Resources/Shaders/phong_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_phongMaterialShader), sizeof(r_phongMaterialShader))); - + ShaderAst::ModulePtr shaderModule = Graphics::Instance()->GetShaderModuleResolver()->Resolve("PhongMaterial"); auto shader = std::make_shared(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule)); return { std::move(shader) }; diff --git a/src/Nazara/Graphics/Resources/Shaders/basic_material.nzsl b/src/Nazara/Graphics/Resources/Shaders/BasicMaterial.nzsl similarity index 99% rename from src/Nazara/Graphics/Resources/Shaders/basic_material.nzsl rename to src/Nazara/Graphics/Resources/Shaders/BasicMaterial.nzsl index 531929d8a..fc080d7f4 100644 --- a/src/Nazara/Graphics/Resources/Shaders/basic_material.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/BasicMaterial.nzsl @@ -1,5 +1,5 @@ [nzsl_version("1.0")] -module; +module BasicMaterial; import Engine.InstanceData; import Engine.ViewerData; diff --git a/src/Nazara/Graphics/Resources/Shaders/depth_material.nzsl b/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl similarity index 98% rename from src/Nazara/Graphics/Resources/Shaders/depth_material.nzsl rename to src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl index 247ae2f59..8575a97df 100644 --- a/src/Nazara/Graphics/Resources/Shaders/depth_material.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl @@ -1,5 +1,5 @@ [nzsl_version("1.0")] -module; +module DepthMaterial; import Engine.InstanceData; import Engine.ViewerData; diff --git a/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl b/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl similarity index 99% rename from src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl rename to src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl index 9065421ff..4d8f9e0c7 100644 --- a/src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl @@ -1,5 +1,5 @@ [nzsl_version("1.0")] -module; +module PhongMaterial; import Engine.InstanceData; import Engine.LightData; diff --git a/src/Nazara/Graphics/Resources/Shaders/blit.nzsl b/src/Nazara/Graphics/Resources/Shaders/blit.nzsl index 1be6c2d4b..02db0c35c 100644 --- a/src/Nazara/Graphics/Resources/Shaders/blit.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/blit.nzsl @@ -1,5 +1,5 @@ [nzsl_version("1.0")] -module; +module Blit; external {