Skip to content

Commit

Permalink
Delete RequiredTextureType
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Sep 23, 2023
1 parent eef910a commit 7f8779a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 117 deletions.
146 changes: 49 additions & 97 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -225,131 +225,83 @@ void ECWorldConsumer::AddMaterial(engine::Entity entity, const cd::Material* pMa
std::set<uint8_t> compiledTextureSlot;
std::map<std::string, const cd::Texture*> outputTexturePathToData;

bool missRequiredTextures = false;
bool unknownTextureSlot = false;
engine::MaterialComponent& materialComponent = m_pSceneWorld->GetWorld()->CreateComponent<engine::MaterialComponent>(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<uint8_t> optTextureSlot = pMaterialType->GetTextureSlot(requiredTextureType);
std::optional<uint8_t> 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<engine::MaterialComponent>(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<uint8_t> 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<cd::BlendMode>(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<cd::BlendMode>(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.
Expand Down
1 change: 1 addition & 0 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions Engine/Source/Runtime/Material/MaterialType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> MaterialType::GetTextureSlot(cd::MaterialTextureType textureType) const
{
auto itTexture = m_textureTypeSlots.find(textureType);
Expand Down
4 changes: 0 additions & 4 deletions Engine/Source/Runtime/Material/MaterialType.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class MaterialType
void AddOptionalTextureType(cd::MaterialTextureType textureType, uint8_t slot);
const std::set<cd::MaterialTextureType>& GetOptionalTextureTypes() const { return m_optionalTextureTypes; }

void AddRequiredTextureType(cd::MaterialTextureType textureType, uint8_t slot);
const std::set<cd::MaterialTextureType>& GetRequiredTextureTypes() const { return m_requiredTextureTypes; }

std::optional<uint8_t> GetTextureSlot(cd::MaterialTextureType textureType) const;

private:
Expand All @@ -49,7 +46,6 @@ class MaterialType

cd::VertexFormat m_requiredVertexFormat;
std::set<cd::MaterialTextureType> m_optionalTextureTypes;
std::set<cd::MaterialTextureType> m_requiredTextureTypes;
std::map<cd::MaterialTextureType, uint8_t> m_textureTypeSlots;
};

Expand Down
12 changes: 6 additions & 6 deletions Engine/Source/Runtime/Rendering/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ uint16_t RenderContext::CreateView()

void RenderContext::RegisterShaderProgram(StringCrc programNameCrc, std::initializer_list<std::string> 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)))
{
Expand All @@ -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;
}
Expand All @@ -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())
{
Expand Down
6 changes: 3 additions & 3 deletions Engine/Source/Runtime/Rendering/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> names);
void AddShaderFeature(StringCrc programNameCrc, std::string combine);
Expand Down Expand Up @@ -131,7 +131,7 @@ class RenderContext
std::unordered_map<uint32_t, uint16_t> m_textureHandleCaches;
std::unordered_map<uint32_t, uint16_t> m_uniformHandleCaches;

ShaderCollections m_shaderVariantCollections;
ShaderCollections m_shaderCollections;

// Key : StringCrc(Program name), Value : Shader program handle
std::unordered_map<uint32_t, uint16_t> m_shaderProgramHandles;
Expand Down
1 change: 0 additions & 1 deletion Engine/Source/Runtime/Rendering/ShaderCollections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ void ShaderCollections::RegisterShaderProgram(StringCrc programNameCrc, std::ini
{
if (IsProgramValid(programNameCrc) || HasFeatureCombine(programNameCrc))
{
CD_ENGINE_WARN("Shader program already exists!");
return;
}

Expand Down

0 comments on commit 7f8779a

Please sign in to comment.