From 7f8779af95255827d22521430ccc9f5983a14c03 Mon Sep 17 00:00:00 2001 From: Hinageshi <2247001665@qq.com> Date: Sat, 23 Sep 2023 19:50:24 +0800 Subject: [PATCH] Delete RequiredTextureType --- .../Source/Editor/ECWorld/ECWorldConsumer.cpp | 146 ++++++------------ .../Source/Editor/ECWorld/ECWorldConsumer.h | 1 + .../Source/Runtime/Material/MaterialType.cpp | 6 - Engine/Source/Runtime/Material/MaterialType.h | 4 - .../Runtime/Rendering/RenderContext.cpp | 12 +- .../Source/Runtime/Rendering/RenderContext.h | 6 +- .../Runtime/Rendering/ShaderCollections.cpp | 1 - 7 files changed, 59 insertions(+), 117 deletions(-) diff --git a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp index 6835c109..604ac5fe 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp @@ -40,7 +40,7 @@ CD_FORCEINLINE bool IsMaterialTextureTypeValid(cd::MaterialTextureType type) } // namespace Detail ECWorldConsumer::ECWorldConsumer(engine::SceneWorld* pSceneWorld, engine::RenderContext* pRenderContext) : - m_pSceneWorld(pSceneWorld) + m_pSceneWorld(pSceneWorld), m_pRenderContext(pRenderContext) { } @@ -225,131 +225,83 @@ void ECWorldConsumer::AddMaterial(engine::Entity entity, const cd::Material* pMa std::set compiledTextureSlot; std::map outputTexturePathToData; - bool missRequiredTextures = false; - bool unknownTextureSlot = false; + engine::MaterialComponent& materialComponent = m_pSceneWorld->GetWorld()->CreateComponent(entity); + materialComponent.Init(); + materialComponent.SetMaterialType(pMaterialType); + materialComponent.SetMaterialData(pMaterial); + + cd::Vec3f albedoColor(1.0f); + engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema(); if (pMaterial) { - for (cd::MaterialTextureType requiredTextureType : pMaterialType->GetRequiredTextureTypes()) + // Expected textures are ready to build. Add more optional texture data. + for (cd::MaterialTextureType optionalTextureType : pMaterialType->GetOptionalTextureTypes()) { - cd::TextureID textureID = pMaterial->GetTextureID(requiredTextureType); + cd::TextureID textureID = pMaterial->GetTextureID(optionalTextureType); if (!textureID.IsValid()) { - missRequiredTextures = true; - CD_ENGINE_ERROR("Material {0} massing required texture {1}!", pMaterial->GetName(), - nameof::nameof_enum(requiredTextureType)); - break; + // TODO : Its ok to have a material factor instead of texture, remove factor case warning. + CD_WARN("Material {0} does not have optional texture type {1}!", pMaterial->GetName(), + nameof::nameof_enum(optionalTextureType)); + continue; } - std::optional optTextureSlot = pMaterialType->GetTextureSlot(requiredTextureType); + std::optional optTextureSlot = pMaterialType->GetTextureSlot(optionalTextureType); if (!optTextureSlot.has_value()) { - unknownTextureSlot = true; - CD_ENGINE_ERROR("Material {0} unknown texture slot of textuere type {1}!", pMaterial->GetName(), - nameof::nameof_enum(requiredTextureType)); + CD_ERROR("Unknow texture {0} slot!", nameof::nameof_enum(optionalTextureType)); break; } + if (Detail::IsMaterialTextureTypeValid(optionalTextureType)) + { + materialComponent.ActivateShaderFeature(Detail::materialTextureTypeToShaderFeature.at(optionalTextureType)); + } + uint8_t textureSlot = optTextureSlot.value(); - const cd::Texture& requiredTexture = pSceneDatabase->GetTexture(textureID.Data()); - std::string outputTexturePath = engine::Path::GetTextureOutputFilePath(requiredTexture.GetPath(), ".dds"); + const cd::Texture& optionalTexture = pSceneDatabase->GetTexture(textureID.Data()); + std::string outputTexturePath = engine::Path::GetTextureOutputFilePath(optionalTexture.GetPath(), ".dds"); if (compiledTextureSlot.find(textureSlot) == compiledTextureSlot.end()) { - // When multiple textures have the same texture slot, it implies that these textures are packed in one file. - // For example, AO + Metalness + Roughness are packed so they have same slots which mean we only need to build it once. - // Note that these texture types can only have same setting to build texture. compiledTextureSlot.insert(textureSlot); - ResourceBuilder::Get().AddTextureBuildTask(requiredTexture.GetType(), requiredTexture.GetPath(), outputTexturePath.c_str()); - outputTexturePathToData[cd::MoveTemp(outputTexturePath)] = &requiredTexture; + ResourceBuilder::Get().AddTextureBuildTask(optionalTexture.GetType(), optionalTexture.GetPath(), outputTexturePath.c_str()); + outputTexturePathToData[cd::MoveTemp(outputTexturePath)] = &optionalTexture; } } - } - // In any bad case, we should have a material component. - engine::MaterialComponent& materialComponent = m_pSceneWorld->GetWorld()->CreateComponent(entity); - materialComponent.Init(); - materialComponent.SetMaterialType(pMaterialType); - materialComponent.SetMaterialData(pMaterial); + if (auto optMetallic = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Metallic, cd::MaterialProperty::Factor); optMetallic.has_value()) + { + materialComponent.SetMetallicFactor(optMetallic.value()); + } - cd::Vec3f albedoColor(1.0f); - engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema(); - if (missRequiredTextures || unknownTextureSlot) - { - // Give a special red color to notify. - albedoColor = cd::Vec3f(1.0f, 0.0f, 0.0f); - } - else - { - if (pMaterial) + if (auto optRoughness = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Roughness, cd::MaterialProperty::Factor); optRoughness.has_value()) { - // Expected textures are ready to build. Add more optional texture data. - for (cd::MaterialTextureType optionalTextureType : pMaterialType->GetOptionalTextureTypes()) - { - cd::TextureID textureID = pMaterial->GetTextureID(optionalTextureType); - if (!textureID.IsValid()) - { - // TODO : Its ok to have a material factor instead of texture, remove factor case warning. - CD_WARN("Material {0} does not have optional texture type {1}!", pMaterial->GetName(), - nameof::nameof_enum(optionalTextureType)); - continue; - } - - std::optional optTextureSlot = pMaterialType->GetTextureSlot(optionalTextureType); - if (!optTextureSlot.has_value()) - { - CD_ERROR("Unknow texture {0} slot!", nameof::nameof_enum(optionalTextureType)); - break; - } - - if (Detail::IsMaterialTextureTypeValid(optionalTextureType)) - { - materialComponent.ActivateShaderFeature(Detail::materialTextureTypeToShaderFeature.at(optionalTextureType)); - } - - uint8_t textureSlot = optTextureSlot.value(); - const cd::Texture& optionalTexture = pSceneDatabase->GetTexture(textureID.Data()); - std::string outputTexturePath = engine::Path::GetTextureOutputFilePath(optionalTexture.GetPath(), ".dds"); - if (compiledTextureSlot.find(textureSlot) == compiledTextureSlot.end()) - { - compiledTextureSlot.insert(textureSlot); - ResourceBuilder::Get().AddTextureBuildTask(optionalTexture.GetType(), optionalTexture.GetPath(), outputTexturePath.c_str()); - outputTexturePathToData[cd::MoveTemp(outputTexturePath)] = &optionalTexture; - } - } + materialComponent.SetRoughnessFactor(optRoughness.value()); + } - if (auto optMetallic = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Metallic, cd::MaterialProperty::Factor); optMetallic.has_value()) - { - materialComponent.SetMetallicFactor(optMetallic.value()); - } + if (auto optTwoSided = pMaterial->GetBoolProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::TwoSided); optTwoSided.has_value()) + { + materialComponent.SetTwoSided(optTwoSided.value()); + } - if (auto optRoughness = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Roughness, cd::MaterialProperty::Factor); optRoughness.has_value()) + if (auto optBlendMode = pMaterial->GetI32Property(cd::MaterialPropertyGroup::General, cd::MaterialProperty::BlendMode); optBlendMode.has_value()) + { + cd::BlendMode blendMode = static_cast(optBlendMode.value()); + if (cd::BlendMode::Mask == blendMode) { - materialComponent.SetRoughnessFactor(optRoughness.value()); - } + auto optAlphaTestValue = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::OpacityMaskClipValue); + assert(optAlphaTestValue.has_value()); - if (auto optTwoSided = pMaterial->GetBoolProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::TwoSided); optTwoSided.has_value()) - { - materialComponent.SetTwoSided(optTwoSided.value()); + materialComponent.SetAlphaCutOff(optAlphaTestValue.value()); } - if (auto optBlendMode = pMaterial->GetI32Property(cd::MaterialPropertyGroup::General, cd::MaterialProperty::BlendMode); optBlendMode.has_value()) - { - cd::BlendMode blendMode = static_cast(optBlendMode.value()); - if (cd::BlendMode::Mask == blendMode) - { - auto optAlphaTestValue = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::OpacityMaskClipValue); - assert(optAlphaTestValue.has_value()); - - materialComponent.SetAlphaCutOff(optAlphaTestValue.value()); - } - - materialComponent.SetBlendMode(blendMode); - } - } - else - { - albedoColor = cd::Vec3f(0.2f); + materialComponent.SetBlendMode(blendMode); } } + else + { + albedoColor = cd::Vec3f(0.2f); + } // TODO : ResourceBuilder will move to EditorApp::Update in the future. // Now let's wait all resource build tasks done here. diff --git a/Engine/Source/Editor/ECWorld/ECWorldConsumer.h b/Engine/Source/Editor/ECWorld/ECWorldConsumer.h index 605a0373..634f688b 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.h +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.h @@ -69,6 +69,7 @@ class ECWorldConsumer final : public cdtools::IConsumer private: engine::MaterialType* m_pDefaultMaterialType = nullptr; engine::SceneWorld* m_pSceneWorld = nullptr; + engine::RenderContext* m_pRenderContext = nullptr; uint32_t m_nodeMinID; uint32_t m_meshMinID; diff --git a/Engine/Source/Runtime/Material/MaterialType.cpp b/Engine/Source/Runtime/Material/MaterialType.cpp index bb00b601..5cb08866 100644 --- a/Engine/Source/Runtime/Material/MaterialType.cpp +++ b/Engine/Source/Runtime/Material/MaterialType.cpp @@ -11,12 +11,6 @@ void MaterialType::AddOptionalTextureType(cd::MaterialTextureType textureType, u m_textureTypeSlots[textureType] = slot; } -void MaterialType::AddRequiredTextureType(cd::MaterialTextureType textureType, uint8_t slot) -{ - m_requiredTextureTypes.insert(textureType); - m_textureTypeSlots[textureType] = slot; -} - std::optional MaterialType::GetTextureSlot(cd::MaterialTextureType textureType) const { auto itTexture = m_textureTypeSlots.find(textureType); diff --git a/Engine/Source/Runtime/Material/MaterialType.h b/Engine/Source/Runtime/Material/MaterialType.h index fe151356..ab3bfcdf 100644 --- a/Engine/Source/Runtime/Material/MaterialType.h +++ b/Engine/Source/Runtime/Material/MaterialType.h @@ -38,9 +38,6 @@ class MaterialType void AddOptionalTextureType(cd::MaterialTextureType textureType, uint8_t slot); const std::set& GetOptionalTextureTypes() const { return m_optionalTextureTypes; } - void AddRequiredTextureType(cd::MaterialTextureType textureType, uint8_t slot); - const std::set& GetRequiredTextureTypes() const { return m_requiredTextureTypes; } - std::optional GetTextureSlot(cd::MaterialTextureType textureType) const; private: @@ -49,7 +46,6 @@ class MaterialType cd::VertexFormat m_requiredVertexFormat; std::set m_optionalTextureTypes; - std::set m_requiredTextureTypes; std::map m_textureTypeSlots; }; diff --git a/Engine/Source/Runtime/Rendering/RenderContext.cpp b/Engine/Source/Runtime/Rendering/RenderContext.cpp index 9c6612a9..9c815a51 100644 --- a/Engine/Source/Runtime/Rendering/RenderContext.cpp +++ b/Engine/Source/Runtime/Rendering/RenderContext.cpp @@ -179,17 +179,17 @@ uint16_t RenderContext::CreateView() void RenderContext::RegisterShaderProgram(StringCrc programNameCrc, std::initializer_list names) { - m_shaderVariantCollections.RegisterShaderProgram(programNameCrc, cd::MoveTemp(names)); + m_shaderCollections.RegisterShaderProgram(programNameCrc, cd::MoveTemp(names)); } void RenderContext::AddShaderFeature(StringCrc programNameCrc, std::string combine) { - m_shaderVariantCollections.AddFeatureCombine(programNameCrc, cd::MoveTemp(combine)); + m_shaderCollections.AddFeatureCombine(programNameCrc, cd::MoveTemp(combine)); } bool RenderContext::CheckShaderProgram(const std::string& programName, const std::string& featuresCombine) { - assert(m_shaderVariantCollections.IsProgramValid(StringCrc(programName))); + assert(m_shaderCollections.IsProgramValid(StringCrc(programName))); if (!bgfx::isValid(GetShaderProgramHandle(programName, featuresCombine))) { @@ -199,7 +199,7 @@ bool RenderContext::CheckShaderProgram(const std::string& programName, const std AddShaderCompileTask(ShaderCompileInfo(programName, featuresCombine)); if (!featuresCombine.empty()) { - m_shaderVariantCollections.AddFeatureCombine(StringCrc(programName), featuresCombine); + m_shaderCollections.AddFeatureCombine(StringCrc(programName), featuresCombine); } return false; } @@ -208,9 +208,9 @@ bool RenderContext::CheckShaderProgram(const std::string& programName, const std void RenderContext::UploadShaderProgram(const std::string& programName, const std::string& featuresCombine) { - assert(m_shaderVariantCollections.IsProgramValid(StringCrc(programName))); + assert(m_shaderCollections.IsProgramValid(StringCrc(programName))); - auto [vsName, fsName, csName] = IdentifyShaderTypes(m_shaderVariantCollections.GetShaders(StringCrc(programName))); + auto [vsName, fsName, csName] = IdentifyShaderTypes(m_shaderCollections.GetShaders(StringCrc(programName))); if (featuresCombine.empty()) { diff --git a/Engine/Source/Runtime/Rendering/RenderContext.h b/Engine/Source/Runtime/Rendering/RenderContext.h index 41266800..e3ced8db 100644 --- a/Engine/Source/Runtime/Rendering/RenderContext.h +++ b/Engine/Source/Runtime/Rendering/RenderContext.h @@ -60,8 +60,8 @@ class RenderContext ///////////////////////////////////////////////////////////////////// // Shader collections apis ///////////////////////////////////////////////////////////////////// - ShaderCollections& GetShaderVariantCollections() { return m_shaderVariantCollections; } - const ShaderCollections& GetShaderVariantCollections() const { return m_shaderVariantCollections; } + ShaderCollections& GetShaderVariantCollections() { return m_shaderCollections; } + const ShaderCollections& GetShaderVariantCollections() const { return m_shaderCollections; } void RegisterShaderProgram(StringCrc programNameCrc, std::initializer_list names); void AddShaderFeature(StringCrc programNameCrc, std::string combine); @@ -131,7 +131,7 @@ class RenderContext std::unordered_map m_textureHandleCaches; std::unordered_map m_uniformHandleCaches; - ShaderCollections m_shaderVariantCollections; + ShaderCollections m_shaderCollections; // Key : StringCrc(Program name), Value : Shader program handle std::unordered_map m_shaderProgramHandles; diff --git a/Engine/Source/Runtime/Rendering/ShaderCollections.cpp b/Engine/Source/Runtime/Rendering/ShaderCollections.cpp index 3879654e..89fa5a9e 100644 --- a/Engine/Source/Runtime/Rendering/ShaderCollections.cpp +++ b/Engine/Source/Runtime/Rendering/ShaderCollections.cpp @@ -11,7 +11,6 @@ void ShaderCollections::RegisterShaderProgram(StringCrc programNameCrc, std::ini { if (IsProgramValid(programNameCrc) || HasFeatureCombine(programNameCrc)) { - CD_ENGINE_WARN("Shader program already exists!"); return; }