diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index 25e7a942..1ce59395 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -469,10 +469,10 @@ bool EditorApp::IsAtmosphericScatteringEnable() const void EditorApp::InitShaderPrograms() const { - ShaderBuilder::BuildShaders(m_pShaderVariantCollections.get()); + ShaderBuilder::BuildNonUberShaders(m_pShaderVariantCollections.get()); - ShaderBuilder::BuildUberShader(m_pSceneWorld->GetPBRMaterialType()); - ShaderBuilder::BuildUberShader(m_pSceneWorld->GetAnimationMaterialType()); + ShaderBuilder::BuildUberShader(m_pShaderVariantCollections.get(), m_pSceneWorld->GetPBRMaterialType()); + ShaderBuilder::BuildUberShader(m_pShaderVariantCollections.get(), m_pSceneWorld->GetAnimationMaterialType()); #ifdef ENABLE_DDGI ShaderBuilder::BuildUberShader(m_pSceneWorld->GetDDGIMaterialType()); #endif diff --git a/Engine/Source/Editor/Resources/ShaderBuilder.cpp b/Engine/Source/Editor/Resources/ShaderBuilder.cpp index 78894f24..45ec3965 100644 --- a/Engine/Source/Editor/Resources/ShaderBuilder.cpp +++ b/Engine/Source/Editor/Resources/ShaderBuilder.cpp @@ -8,11 +8,40 @@ namespace editor { -void ShaderBuilder::BuildUberShader(engine::MaterialType* pMaterialType) +void ShaderBuilder::BuildUberShader(engine::ShaderVariantCollections* pCollections, engine::MaterialType* pMaterialType) { + const std::string& programName = pMaterialType->GetShaderSchema().GetProgramName(); + + const std::set& shaders = pCollections->GetUberShaders(programName); + const std::set& combines = pCollections->GetFeatureCombines(programName); + for (const std::string& shader : shaders) + { + ShaderType shaderType = GetShaderType(shader); + if (ShaderType::Vertex == shaderType) + { + std::string inputVSFilePath = engine::Path::GetBuiltinShaderInputPath(shader.c_str()); + std::string outputVSFilePath = engine::Path::GetShaderOutputPath(shader.c_str()); + ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Vertex, inputVSFilePath.c_str(), outputVSFilePath.c_str()); + } + else if (ShaderType::Fragment == shaderType) + { + for (const auto& combine : combines) + { + std::string inputVFSFilePath = engine::Path::GetBuiltinShaderInputPath(shader.c_str()); + std::string outputFSFilePath = engine::Path::GetShaderOutputPath(shader.c_str(), combine); + ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Fragment, inputVFSFilePath.c_str(), outputFSFilePath.c_str(), combine.c_str()); + } + } + else + { + // No shader feature support for vertex and compute shader. + continue; + } + } + engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema(); - // No shader feature supports for VS now. + // No shader feature support for VS now. std::string outputVSFilePath = engine::Path::GetShaderOutputPath(shaderSchema.GetVertexShaderPath()); ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Vertex, shaderSchema.GetVertexShaderPath(), outputVSFilePath.c_str()); @@ -28,38 +57,21 @@ void ShaderBuilder::BuildUberShader(engine::MaterialType* pMaterialType) CD_ENGINE_INFO("Shader variant count of material type {0} : {1}", pMaterialType->GetMaterialName(), shaderSchema.GetFeatureCombines().size()); } -void ShaderBuilder::BuildShaders(engine::ShaderVariantCollections* pCollections) +void ShaderBuilder::BuildNonUberShaders(engine::ShaderVariantCollections* pCollections) { - const auto& porgrams = pCollections->GetShaderPrograms(); - - for (const auto& [programName, pack] : porgrams) + for (const auto& shaders : pCollections->GetNonUberShaderPrograms()) { - if (pack.GetFeatureSet().empty()) + for (const auto& shader : shaders.second) { - // Non-Uber Shaders - - for (const auto& shaderName : pack.GetShaderNames()) + ShaderType shaderType = GetShaderType(shader); + if (ShaderType::None == shaderType) { - ShaderType shaderType = GetShaderType(shaderName); - if (ShaderType::None == shaderType) - { - continue; - } - - // TODO : GetBuiltinShaderInputPath() - std::filesystem::path inputShaderFullPath = CDENGINE_BUILTIN_SHADER_PATH; - inputShaderFullPath += "shaders/"; - inputShaderFullPath += shaderName; - inputShaderFullPath .replace_extension(ShaderExtension); - - std::string outputShaderPath = engine::Path::GetShaderOutputPath(shaderName.c_str()); - ResourceBuilder::Get().AddShaderBuildTask(shaderType, inputShaderFullPath.generic_string().c_str(), outputShaderPath.c_str()); + continue; } - } - else - { - // Uber Shaders + std::string inputShaderPath = engine::Path::GetBuiltinShaderInputPath(shader.c_str()); + std::string outputShaderPath = engine::Path::GetShaderOutputPath(shader.c_str()); + ResourceBuilder::Get().AddShaderBuildTask(shaderType, inputShaderPath.c_str(), outputShaderPath.c_str()); } } } diff --git a/Engine/Source/Editor/Resources/ShaderBuilder.h b/Engine/Source/Editor/Resources/ShaderBuilder.h index 01f618ef..72125148 100644 --- a/Engine/Source/Editor/Resources/ShaderBuilder.h +++ b/Engine/Source/Editor/Resources/ShaderBuilder.h @@ -19,12 +19,8 @@ namespace editor class ShaderBuilder { public: - static constexpr const char* ShaderExtension = ".sc"; - -public: - static void BuildUberShader(engine::MaterialType* pMaterialType); - - static void BuildShaders(engine::ShaderVariantCollections* pCollections); + static void BuildNonUberShaders(engine::ShaderVariantCollections* pCollections); + static void BuildUberShader(engine::ShaderVariantCollections* pCollections, engine::MaterialType* pMaterialType); private: static const ShaderType GetShaderType(const std::string& fileName); diff --git a/Engine/Source/Runtime/ECWorld/SceneWorld.cpp b/Engine/Source/Runtime/ECWorld/SceneWorld.cpp index 762e2a96..135476df 100644 --- a/Engine/Source/Runtime/ECWorld/SceneWorld.cpp +++ b/Engine/Source/Runtime/ECWorld/SceneWorld.cpp @@ -49,7 +49,7 @@ void SceneWorld::CreatePBRMaterialType(bool isAtmosphericScatteringEnable) m_pPBRMaterialType = std::make_unique(); m_pPBRMaterialType->SetMaterialName("CD_PBR"); - ShaderSchema shaderSchema(Path::GetBuiltinShaderInputPath("shaders/vs_PBR"), Path::GetBuiltinShaderInputPath("shaders/fs_PBR")); + ShaderSchema shaderSchema("WorldProgram", Path::GetBuiltinShaderInputPath("vs_PBR"), Path::GetBuiltinShaderInputPath("fs_PBR")); shaderSchema.AddFeatureSet({ ShaderFeature::ALBEDO_MAP }); shaderSchema.AddFeatureSet({ ShaderFeature::NORMAL_MAP }); shaderSchema.AddFeatureSet({ ShaderFeature::ORM_MAP }); @@ -86,7 +86,7 @@ void SceneWorld::CreateAnimationMaterialType() m_pAnimationMaterialType = std::make_unique(); m_pAnimationMaterialType->SetMaterialName("CD_Animation"); - ShaderSchema shaderSchema(Path::GetBuiltinShaderInputPath("shaders/vs_animation"), Path::GetBuiltinShaderInputPath("shaders/fs_animation")); + ShaderSchema shaderSchema("AnimationProgram", Path::GetBuiltinShaderInputPath("vs_animation"), Path::GetBuiltinShaderInputPath("fs_animation")); m_pAnimationMaterialType->SetShaderSchema(cd::MoveTemp(shaderSchema)); cd::VertexFormat animationVertexFormat; diff --git a/Engine/Source/Runtime/Material/ShaderSchema.cpp b/Engine/Source/Runtime/Material/ShaderSchema.cpp index f89fc20a..17003070 100644 --- a/Engine/Source/Runtime/Material/ShaderSchema.cpp +++ b/Engine/Source/Runtime/Material/ShaderSchema.cpp @@ -10,8 +10,9 @@ namespace engine { -ShaderSchema::ShaderSchema(std::string vsPath, std::string fsPath) +ShaderSchema::ShaderSchema(std::string progeamName, std::string vsPath, std::string fsPath) { + m_programName = cd::MoveTemp(progeamName); m_vertexShaderPath = cd::MoveTemp(vsPath); m_fragmentShaderPath = cd::MoveTemp(fsPath); @@ -181,7 +182,7 @@ void ShaderSchema::AddUberFSBlob(StringCrc shaderFeaturesCrc, ShaderBlob shaderB m_shaderFeaturesToFSBlobs[shaderFeaturesCrc.Value()] = std::make_unique(cd::MoveTemp(shaderBlob)); } -const ShaderBlob& ShaderSchema::GetFSBlob(StringCrc shaderFeaturesCrc) const +const ShaderSchema::ShaderBlob& ShaderSchema::GetFSBlob(StringCrc shaderFeaturesCrc) const { auto itBlob = m_shaderFeaturesToFSBlobs.find(shaderFeaturesCrc.Value()); assert(itBlob != m_shaderFeaturesToFSBlobs.end()); diff --git a/Engine/Source/Runtime/Material/ShaderSchema.h b/Engine/Source/Runtime/Material/ShaderSchema.h index c6ea7682..664d78fa 100644 --- a/Engine/Source/Runtime/Material/ShaderSchema.h +++ b/Engine/Source/Runtime/Material/ShaderSchema.h @@ -29,16 +29,18 @@ class ShaderSchema public: static constexpr uint16_t InvalidProgramHandle = UINT16_MAX; static constexpr StringCrc DefaultUberShaderCrc = StringCrc(""); + using ShaderBlob = std::vector; public: ShaderSchema() = default; - explicit ShaderSchema(std::string vsPath, std::string fsPath); + explicit ShaderSchema(std::string progeamName, std::string vsPath, std::string fsPath); ShaderSchema(const ShaderSchema&) = delete; ShaderSchema& operator=(const ShaderSchema&) = delete; ShaderSchema(ShaderSchema&&) = default; ShaderSchema& operator=(ShaderSchema&&) = default; ~ShaderSchema() = default; + const char* GetProgramName() const { return m_programName.c_str(); } const char* GetVertexShaderPath() const { return m_vertexShaderPath.c_str(); } const char* GetFragmentShaderPath() const { return m_fragmentShaderPath.c_str(); } @@ -68,6 +70,8 @@ class ShaderSchema const ShaderBlob& GetFSBlob(StringCrc shaderFeaturesCrc) const; private: + std::string m_programName; + std::string m_vertexShaderPath; std::string m_fragmentShaderPath; @@ -76,7 +80,6 @@ class ShaderSchema std::vector m_shaderFeatureSets; // Parameters to compile shaders. std::vector m_featureCombines; - // Key: StringCrc(feature combine), Value: shader handle. std::map m_compiledProgramHandles; diff --git a/Engine/Source/Runtime/Path/Path.cpp b/Engine/Source/Runtime/Path/Path.cpp index 59ea32d9..670e42d8 100644 --- a/Engine/Source/Runtime/Path/Path.cpp +++ b/Engine/Source/Runtime/Path/Path.cpp @@ -91,7 +91,7 @@ void Path::SetGraphicsBackend(engine::GraphicsBackend backend) std::string Path::GetBuiltinShaderInputPath(const char* pShaderName) { - return (GetEngineBuiltinShaderPath() / pShaderName).replace_extension(ShaderInputExtension).string(); + return (GetEngineBuiltinShaderPath() / "shaders" / pShaderName).replace_extension(ShaderInputExtension).generic_string(); } std::filesystem::path Path::GetShaderOutputDirectory() @@ -101,7 +101,7 @@ std::filesystem::path Path::GetShaderOutputDirectory() std::string Path::GetShaderOutputPath(const char* pInputFilePath, const std::string& options) { - std::string outputShaderFileName = std::filesystem::path(pInputFilePath).stem().string(); + std::string outputShaderFileName = std::filesystem::path(pInputFilePath).stem().generic_string(); if (!options.empty()) { @@ -117,17 +117,17 @@ std::string Path::GetShaderOutputPath(const char* pInputFilePath, const std::str } } - return (GetShaderOutputDirectory() / cd::MoveTemp(outputShaderFileName)).replace_extension(ShaderOutputExtension).string(); + return (GetShaderOutputDirectory() / cd::MoveTemp(outputShaderFileName)).replace_extension(ShaderOutputExtension).generic_string(); } std::string Path::GetTextureOutputFilePath(const char* pInputFilePath, const char* extension) { - return ((GetEngineResourcesPath() / "Textures" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).string(); + return ((GetEngineResourcesPath() / "Textures" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).generic_string(); } std::string Path::GetTerrainTextureOutputFilePath(const char* pInputFilePath, const char* extension) { - return ((GetEngineResourcesPath() / "Textures" / "Terrain" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).string(); + return ((GetEngineResourcesPath() / "Textures" / "Terrain" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).generic_string(); } } \ No newline at end of file diff --git a/Engine/Source/Runtime/Path/Path.h b/Engine/Source/Runtime/Path/Path.h index 1f03abc6..b8f44d52 100644 --- a/Engine/Source/Runtime/Path/Path.h +++ b/Engine/Source/Runtime/Path/Path.h @@ -20,11 +20,6 @@ class Path static std::optional GetApplicationDataPath(); - static std::filesystem::path GetEngineBuiltinShaderPath(); - static std::filesystem::path GetEngineResourcesPath(); - static std::filesystem::path GetEditorResourcesPath(); - static std::filesystem::path GetProjectsSharedPath(); - static engine::GraphicsBackend GetGraphicsBackend(); static void SetGraphicsBackend(engine::GraphicsBackend backend); @@ -35,6 +30,11 @@ class Path static std::string GetTerrainTextureOutputFilePath(const char* pInputFilePath, const char* extension); private: + static std::filesystem::path GetEngineBuiltinShaderPath(); + static std::filesystem::path GetEngineResourcesPath(); + static std::filesystem::path GetEditorResourcesPath(); + static std::filesystem::path GetProjectsSharedPath(); + static const char* GetPlatformPathKey(); static std::filesystem::path GetPlatformAppDataPath(const char* pRootPath); diff --git a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp index c9e91792..6f1b9e5f 100644 --- a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp @@ -13,7 +13,7 @@ namespace engine void AABBRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("AABBProgram", {"vs_AABB", "fs_AABB"}); + GetShaderVariantCollections()->RegisterNonUberShader("AABBProgram", {"vs_AABB", "fs_AABB"}); bgfx::setViewName(GetViewID(), "AABBRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/AnimationRenderer.cpp b/Engine/Source/Runtime/Rendering/AnimationRenderer.cpp index 137160bd..5a106dba 100644 --- a/Engine/Source/Runtime/Rendering/AnimationRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/AnimationRenderer.cpp @@ -136,7 +136,7 @@ void CalculateBoneTransform(std::vector& boneMatrices, const cd:: void AnimationRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("AnimationProgram", { "vs_animation", "fs_animation" }); + GetShaderVariantCollections()->RegisterNonUberShader("AnimationProgram", { "vs_animation", "fs_animation" }); bgfx::setViewName(GetViewID(), "AnimationRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/BloomRenderer.cpp b/Engine/Source/Runtime/Rendering/BloomRenderer.cpp index edaa7d86..69fa0b88 100644 --- a/Engine/Source/Runtime/Rendering/BloomRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/BloomRenderer.cpp @@ -7,13 +7,13 @@ namespace engine { void BloomRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("CapTureBrightnessProgram", { "vs_fullscreen", "fs_captureBrightness" }); - GetShaderVariantCollections()->RegisterPragram("DownSampleProgram", { "vs_fullscreen", "fs_dowmsample" }); - GetShaderVariantCollections()->RegisterPragram("BlurVerticalProgram", { "vs_fullscreen", "fs_blurvertical" }); - GetShaderVariantCollections()->RegisterPragram("BlurHorizontalProgram", { "vs_fullscreen", "fs_blurhorizontal" }); - GetShaderVariantCollections()->RegisterPragram("UpSampleProgram", { "vs_fullscreen", "fs_upsample" }); - GetShaderVariantCollections()->RegisterPragram("KawaseBlurProgram", { "vs_fullscreen", "fs_kawaseblur" }); - GetShaderVariantCollections()->RegisterPragram("CombineProgram", { "vs_fullscreen", "fs_bloom" }); + GetShaderVariantCollections()->RegisterNonUberShader("CapTureBrightnessProgram", { "vs_fullscreen", "fs_captureBrightness" }); + GetShaderVariantCollections()->RegisterNonUberShader("DownSampleProgram", { "vs_fullscreen", "fs_dowmsample" }); + GetShaderVariantCollections()->RegisterNonUberShader("BlurVerticalProgram", { "vs_fullscreen", "fs_blurvertical" }); + GetShaderVariantCollections()->RegisterNonUberShader("BlurHorizontalProgram", { "vs_fullscreen", "fs_blurhorizontal" }); + GetShaderVariantCollections()->RegisterNonUberShader("UpSampleProgram", { "vs_fullscreen", "fs_upsample" }); + GetShaderVariantCollections()->RegisterNonUberShader("KawaseBlurProgram", { "vs_fullscreen", "fs_kawaseblur" }); + GetShaderVariantCollections()->RegisterNonUberShader("CombineProgram", { "vs_fullscreen", "fs_bloom" }); bgfx::setViewName(GetViewID(), "BloomRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/DebugRenderer.cpp b/Engine/Source/Runtime/Rendering/DebugRenderer.cpp index f6cb32f8..960767f3 100644 --- a/Engine/Source/Runtime/Rendering/DebugRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/DebugRenderer.cpp @@ -13,7 +13,7 @@ namespace engine void DebugRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("DebugProgram", { "vs_debug", "fs_debug" }); + GetShaderVariantCollections()->RegisterNonUberShader("DebugProgram", { "vs_debug", "fs_debug" }); bgfx::setViewName(GetViewID(), "DebugRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp b/Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp index 9b54e12f..dc399b57 100644 --- a/Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp @@ -10,7 +10,7 @@ namespace engine void ImGuiRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("ImGuiProgram", { "vs_imgui", "fs_imgui" }); + GetShaderVariantCollections()->RegisterNonUberShader("ImGuiProgram", { "vs_imgui", "fs_imgui" }); bgfx::setViewName(GetViewID(), "ImGuiRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp b/Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp index 4962db31..acc4820b 100644 --- a/Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp @@ -50,14 +50,14 @@ constexpr uint16_t ScatteringOrders = 6; void PBRSkyRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram(ProgramAtmosphericScatteringLUT, { "vs_atmSkyBox", "fs_PrecomputedAtmosphericScattering_LUT" }); - GetShaderVariantCollections()->RegisterPragram(ProgramSingleScatteringRayMarching, { "vs_atmSkyBox", "fs_SingleScattering_RayMarching" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeTransmittance, { "cs_ComputeTransmittance" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeDirectIrradiance, { "cs_ComputeDirectIrradiance" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeSingleScattering, { "cs_ComputeSingleScattering" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeScatteringDensity, { "cs_ComputeScatteringDensity" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeIndirectIrradiance, { "cs_ComputeIndirectIrradiance" }); - GetShaderVariantCollections()->RegisterPragram(ProgramComputeMultipleScattering, { "cs_ComputeMultipleScattering" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramAtmosphericScatteringLUT, { "vs_atmSkyBox", "fs_PrecomputedAtmosphericScattering_LUT" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramSingleScatteringRayMarching, { "vs_atmSkyBox", "fs_SingleScattering_RayMarching" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeTransmittance, { "cs_ComputeTransmittance" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeDirectIrradiance, { "cs_ComputeDirectIrradiance" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeSingleScattering, { "cs_ComputeSingleScattering" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeScatteringDensity, { "cs_ComputeScatteringDensity" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeIndirectIrradiance, { "cs_ComputeIndirectIrradiance" }); + GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeMultipleScattering, { "cs_ComputeMultipleScattering" }); bgfx::setViewName(GetViewID(), "PBRSkyRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp b/Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp index ef5a6e65..9585e72d 100644 --- a/Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp @@ -8,7 +8,7 @@ namespace engine void PostProcessRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("PostProcessProgram", {"vs_fullscreen","fs_PBR_postProcessing"}); + GetShaderVariantCollections()->RegisterNonUberShader("PostProcessProgram", {"vs_fullscreen","fs_PBR_postProcessing"}); bgfx::setViewName(GetViewID(), "PostProcessRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/ShaderFeature.h b/Engine/Source/Runtime/Rendering/ShaderFeature.h index 89b5780b..ff0e6ff0 100644 --- a/Engine/Source/Runtime/Rendering/ShaderFeature.h +++ b/Engine/Source/Runtime/Rendering/ShaderFeature.h @@ -27,6 +27,7 @@ enum class ShaderFeature : uint32_t COUNT, }; +// These names are used as macro definition symbols in shaders. constexpr const char* ShaderFeatureNames[] = { "", // Use empty string to represent default shader option in the name so we can reuse non-uber built shader. @@ -48,6 +49,5 @@ CD_FORCEINLINE constexpr const char* GetFeatureName(ShaderFeature feature) } using ShaderFeatureSet = std::set; -using ShaderBlob = std::vector; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/ShaderProgramPack.cpp b/Engine/Source/Runtime/Rendering/ShaderProgramPack.cpp deleted file mode 100644 index 12bb45c3..00000000 --- a/Engine/Source/Runtime/Rendering/ShaderProgramPack.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "ShaderProgramPack.h" - -#include "Base/Template.h" - -#include - -namespace engine -{ - -ShaderProgramPack::ShaderProgramPack(std::initializer_list names) -{ - for (auto& name : names) - { - m_shaderNames.emplace_back(cd::MoveTemp(name)); - } -} - -void ShaderProgramPack::AddShader(std::string path) -{ - m_shaderNames.emplace_back(cd::MoveTemp(path)); -} - -void ShaderProgramPack::ActivateShaderFeature(ShaderFeature feature) -{ - assert(!IsFeatureValid(feature) && "Shader feature already exists in ShaderProgramPack!"); - m_featureSet.insert(cd::MoveTemp(feature)); -} - -void ShaderProgramPack::DeactivateShaderFeature(ShaderFeature feature) -{ - assert(IsFeatureValid(feature) && "Shader feature does not exist in ShaderProgramPack!"); - m_featureSet.erase(cd::MoveTemp(feature)); -} - -void ShaderProgramPack::SetShaderNames(std::vector names) -{ - m_shaderNames = cd::MoveTemp(names); -} - -void ShaderProgramPack::SetFeatureSet(ShaderFeatureSet featureSet) -{ - m_featureSet = cd::MoveTemp(featureSet); -} - -inline bool ShaderProgramPack::IsFeatureValid(ShaderFeature feature) const -{ - return (m_featureSet.find(feature) != m_featureSet.end()); -} - -} \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/ShaderProgramPack.h b/Engine/Source/Runtime/Rendering/ShaderProgramPack.h deleted file mode 100644 index b831d1b1..00000000 --- a/Engine/Source/Runtime/Rendering/ShaderProgramPack.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "Rendering/ShaderFeature.h" - -#include -#include - -namespace engine -{ - -class ShaderProgramPack -{ -public: - ShaderProgramPack(std::initializer_list names); - - ShaderProgramPack() = default; - ShaderProgramPack(const ShaderProgramPack&) = default; - ShaderProgramPack& operator=(const ShaderProgramPack&) = default; - ShaderProgramPack(ShaderProgramPack&&) = default; - ShaderProgramPack& operator=(ShaderProgramPack&&) = default; - ~ShaderProgramPack() = default; - - void AddShader(std::string path); - - void ActivateShaderFeature(ShaderFeature feature); - void DeactivateShaderFeature(ShaderFeature feature); - - void SetShaderNames(std::vector names); - std::vector& GetShaderNames() { return m_shaderNames; } - const std::vector& GetShaderNames() const { return m_shaderNames; } - - void SetFeatureSet(ShaderFeatureSet featureSet); - ShaderFeatureSet& GetFeatureSet() { return m_featureSet; } - const ShaderFeatureSet& GetFeatureSet() const { return m_featureSet; } - -private: - inline bool IsFeatureValid(ShaderFeature feature) const; - - // TODO : Check is program legal, VS + FS or only CS will be a legal program. - std::vector m_shaderNames; - ShaderFeatureSet m_featureSet; -}; - -} \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/ShaderVariantCollections.cpp b/Engine/Source/Runtime/Rendering/ShaderVariantCollections.cpp index d38f1b2e..13eb8eb1 100644 --- a/Engine/Source/Runtime/Rendering/ShaderVariantCollections.cpp +++ b/Engine/Source/Runtime/Rendering/ShaderVariantCollections.cpp @@ -7,39 +7,82 @@ namespace engine { -void ShaderVariantCollections::RegisterPragram(std::string programName, ShaderProgramPack pack) +void ShaderVariantCollections::RegisterNonUberShader(std::string programName, std::initializer_list names) { - if (IsProgramValid(programName)) + if (IsNonUberShaderProgramValid(programName)) { - CD_ENGINE_WARN("Program pack {0} already exists in SVC!", cd::MoveTemp(programName)); + CD_ENGINE_WARN("Non uber shader program {0} already exists!", programName); return; } - m_shaderPrograms[cd::MoveTemp(programName)] = cd::MoveTemp(pack); + m_nonUberShaderPrograms[cd::MoveTemp(programName)] = cd::MoveTemp(names); } -void ShaderVariantCollections::ActivateShaderFeature(std::string programName, ShaderFeature feature) +void ShaderVariantCollections::RegisterUberShader(std::string programName, std::initializer_list names, std::initializer_list combines) { - assert(IsProgramValid(programName) && "Program does not exist in SVC!"); + if (IsUberShaderProgramValid(programName)) + { + CD_ENGINE_WARN("Uber shader program {0} already exists!", programName); + return; + } + + m_uberShaderPrograms[cd::MoveTemp(programName)] = cd::MoveTemp(names); + m_featureCombinePrograms[cd::MoveTemp(programName)] = cd::MoveTemp(combines); +} + +void ShaderVariantCollections::AddFeatureCombine(std::string programName, std::string combine) +{ + assert(IsUberShaderProgramValid(programName) && "Uber shader does not exist!"); - m_shaderPrograms[cd::MoveTemp(programName)].ActivateShaderFeature(cd::MoveTemp(feature)); + m_featureCombinePrograms[cd::MoveTemp(programName)].insert(cd::MoveTemp(combine)); } -void ShaderVariantCollections::DeactiveShaderFeature(std::string programName, ShaderFeature feature) +void ShaderVariantCollections::DeleteFeatureCombine(std::string programName, std::string combine) { - assert(IsProgramValid(programName) && "Program does not exist in SVC!"); + assert(IsUberShaderProgramValid(programName) && "Uber shader does not exist!"); - m_shaderPrograms[cd::MoveTemp(programName)].DeactivateShaderFeature(cd::MoveTemp(feature)); + m_featureCombinePrograms[cd::MoveTemp(programName)].erase(cd::MoveTemp(combine)); +} + +void ShaderVariantCollections::SetNonUberShaders(const std::string& programName, std::set shaders) +{ + m_nonUberShaderPrograms[programName] = cd::MoveTemp(shaders); +} + +void ShaderVariantCollections::SetUberShaders(const std::string& programName, std::set shaders) +{ + m_uberShaderPrograms[programName] = cd::MoveTemp(shaders); +} + +void ShaderVariantCollections::SetFeatureCombines(const std::string& programName, std::set combine) +{ + m_featureCombinePrograms[programName] = cd::MoveTemp(combine); +} + +void ShaderVariantCollections::SetNonUberShaderPrograms(std::map> shaders) +{ + m_nonUberShaderPrograms = cd::MoveTemp(shaders); +} + +void ShaderVariantCollections::SetUberShaderPrograms(std::map> shaders) +{ + m_uberShaderPrograms = cd::MoveTemp(shaders); +} + +void ShaderVariantCollections::SetFeatureCombinePrograms(std::map> combines) +{ + m_featureCombinePrograms = cd::MoveTemp(combines); } -void ShaderVariantCollections::SetShaderPrograms(std::map program) +bool ShaderVariantCollections::IsNonUberShaderProgramValid(std::string programName) const { - m_shaderPrograms = cd::MoveTemp(program); + return (m_nonUberShaderPrograms.find(cd::MoveTemp(programName)) != m_nonUberShaderPrograms.end()); } -bool ShaderVariantCollections::IsProgramValid(std::string programName) const +bool ShaderVariantCollections::IsUberShaderProgramValid(std::string programName) const { - return (m_shaderPrograms.find(cd::MoveTemp(programName)) != m_shaderPrograms.end()); + return (m_uberShaderPrograms.find(cd::MoveTemp(programName)) != m_uberShaderPrograms.end() && + m_featureCombinePrograms.find(cd::MoveTemp(programName)) != m_featureCombinePrograms.end()); } } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/ShaderVariantCollections.h b/Engine/Source/Runtime/Rendering/ShaderVariantCollections.h index 20c84e1c..d62db4ee 100644 --- a/Engine/Source/Runtime/Rendering/ShaderVariantCollections.h +++ b/Engine/Source/Runtime/Rendering/ShaderVariantCollections.h @@ -2,9 +2,9 @@ #include "Base/Template.h" #include "Core/StringCrc.h" -#include "Rendering/ShaderProgramPack.h" #include +#include #include namespace engine @@ -20,21 +20,50 @@ class ShaderVariantCollections final ShaderVariantCollections& operator=(ShaderVariantCollections&&) = default; ~ShaderVariantCollections() = default; - void RegisterPragram(std::string programName, ShaderProgramPack pack); + void RegisterNonUberShader(std::string programName, std::initializer_list names); + void RegisterUberShader(std::string programName, std::initializer_list names, std::initializer_list combines); - void ActivateShaderFeature(std::string programName, ShaderFeature feature); - void DeactiveShaderFeature(std::string programName, ShaderFeature feature); + void AddFeatureCombine(std::string programName, std::string combine); + void DeleteFeatureCombine(std::string programName, std::string combine); - void SetShaderPrograms(std::map program); - std::map& GetShaderPrograms() { return m_shaderPrograms; } - const std::map& GetShaderPrograms() const { return m_shaderPrograms; } + void SetNonUberShaders(const std::string& programName, std::set shaders); + std::set& GetNonUberShaders(const std::string& programName) { return m_nonUberShaderPrograms[programName]; } + const std::set& GetNonUberShaders(const std::string& programName) const { return m_nonUberShaderPrograms.at(programName); } + + void SetUberShaders(const std::string& programName, std::set shaders); + std::set& GetUberShaders(const std::string& programName) { return m_uberShaderPrograms[programName]; } + const std::set& GetUberShaders(const std::string& programName) const { return m_uberShaderPrograms.at(programName); } + + void SetFeatureCombines(const std::string& programName, std::set combine); + std::set& GetFeatureCombines(const std::string& programName) { return m_featureCombinePrograms[programName]; } + const std::set& GetFeatureCombines(const std::string& programName) const { return m_featureCombinePrograms.at(programName); } + + void SetNonUberShaderPrograms(std::map> shaders); + std::map>& GetNonUberShaderPrograms() { return m_nonUberShaderPrograms; } + const std::map>& GetNonUberShaderPrograms() const { return m_nonUberShaderPrograms; } + + void SetUberShaderPrograms(std::map> shaders); + std::map>& GetUberShaderPrograms() { return m_uberShaderPrograms; } + const std::map>& GetUberShaderPrograms() const { return m_uberShaderPrograms; } + + void SetFeatureCombinePrograms(std::map> combines); + std::map>& GetFeatureCombinePrograms() { return m_featureCombinePrograms; } + const std::map>& GetFeatureCombinePrograms() const { return m_featureCombinePrograms; } private: - inline bool IsProgramValid(std::string programName) const; + inline bool IsNonUberShaderProgramValid(std::string programName) const; + inline bool IsUberShaderProgramValid(std::string programName) const; + + // Key : Program name, Value : Non-uber hader names + std::map> m_nonUberShaderPrograms; + + // Key : Program name, Value : Uber shader names + std::map> m_uberShaderPrograms; + // Key : Program name, Value : Feature combine used as a parameter for compiling shaders + std::map> m_featureCombinePrograms; - // Key : Program Name, Value : Shader Path + Variant. // TODO : StringCrc - std::map m_shaderPrograms; + // TODO : Check is program legal, VS + FS or only CS will be a legal program. }; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp b/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp index 5c20b724..0e6174bf 100644 --- a/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp @@ -57,7 +57,7 @@ void TraverseBone(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase, void SkeletonRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("SkeletonProgram", {"vs_AABB", "fs_AABB"}); + GetShaderVariantCollections()->RegisterNonUberShader("SkeletonProgram", {"vs_AABB", "fs_AABB"}); bgfx::setViewName(GetViewID(), "SkeletonRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp b/Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp index a0791a78..038d5116 100644 --- a/Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp @@ -22,7 +22,7 @@ constexpr uint64_t renderState = BGFX_STATE_WRITE_MASK | BGFX_STATE_CULL_CCW | B void SkyboxRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("SkyboxRenderer", {"vs_skybox", "fs_skybox"}); + GetShaderVariantCollections()->RegisterNonUberShader("SkyboxRenderer", {"vs_skybox", "fs_skybox"}); bgfx::setViewName(GetViewID(), "SkyboxRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp index 6f14b296..dfa15bb7 100644 --- a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp @@ -55,7 +55,7 @@ constexpr uint64_t defaultRenderingState = BGFX_STATE_WRITE_MASK | BGFX_STATE_MS void TerrainRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("TerrainRenderer", {"vs_terrain", "fs_terrain"}); + GetShaderVariantCollections()->RegisterNonUberShader("TerrainRenderer", {"vs_terrain", "fs_terrain"}); bgfx::setViewName(GetViewID(), "TerrainRenderer"); } diff --git a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp index 6f804172..8f9f8eca 100644 --- a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp @@ -48,7 +48,7 @@ constexpr uint64_t defaultRenderingState = BGFX_STATE_WRITE_MASK | BGFX_STATE_MS void WorldRenderer::Init() { - GetShaderVariantCollections()->RegisterPragram("WorldRenderer", {"vs_PBR", "fs_PBR"}); + GetShaderVariantCollections()->RegisterUberShader("WorldProgram", { "vs_PBR", "fs_PBR" }, {}); bgfx::setViewName(GetViewID(), "WorldRenderer"); }