Skip to content

Commit

Permalink
Shader lazy load (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas authored Oct 24, 2023
1 parent f6044f8 commit efd2d33
Show file tree
Hide file tree
Showing 90 changed files with 1,549 additions and 1,103 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ Engine/ThirdPartyProjects
ARM64

# tmp
Projects/Shared
Projects/Shared/*
Projects/Test/*
!Projects/Test/*/
Projects/Test/Textures/*
!Projects/Test/Textures/*/

# Precompiled Shader
!Projects/Shared/BuiltInShaders
Projects/Shared/BuiltInShaders/*
!Projects/Shared/BuiltInShaders/Direct3D11
Projects/Shared/BuiltInShaders/Direct3D11/*
!Projects/Shared/BuiltInShaders/Direct3D11/vs_imgui.bin
!Projects/Shared/BuiltInShaders/Direct3D11/fs_imgui.bin

# Commercial SKD
Engine/Auto/commercial_sdk_locations.bat
21 changes: 0 additions & 21 deletions Engine/BuiltInShaders/atm/varying.def.sc

This file was deleted.

2 changes: 1 addition & 1 deletion Engine/BuiltInShaders/common/Envirnoment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#if defined(ATM)

#include "../atm/atm_functions.sh"
#include "atm_functions.sh"

uniform vec4 u_LightDir;
uniform vec4 u_HeightOffsetAndshadowLength;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

IMAGE2D_WR(s_delta_irradiance, rgba32f, 0);
IMAGE2D_WR(s_irradiance, rgba32f, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

uniform vec4 u_numScatteringOrders;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

IMAGE3D_WR(s_delta_multiple_scattering, rgba32f, 0);
IMAGE3D_RW(s_scattering, rgba32f, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

uniform vec4 u_numScatteringOrders;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

IMAGE3D_WR(s_delta_rayleigh_scattering, rgba32f, 0);
IMAGE3D_WR(s_delta_mie_scattering, rgba32f, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/bgfx_compute.sh"

#define COMPUTE
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

IMAGE2D_WR(s_transmittance, rgba32f, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $input v_worldPos

#include "../common/common.sh"
#include "../common/Camera.sh"
#include "atm_functions.sh"
#include "../common/atm_functions.sh"

uniform vec4 u_LightDir;
uniform vec4 u_HeightOffset;
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions Engine/EditorResources/Text.csv
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ TEXT_SAVE,保存,Save
TEXT_SAVE_AS,另存为,Save AS
TEXT_WINDOW,窗口,Window
TEXT_BUILD,编译,Build
TEXT_REBUILD_NONUBER_SHADERS,重新生成着色器,Rebuild Non-Uber shaders
TEXT_REBUILD_PBR_SHADERS,重新生成PBR着色器,Rebuild PBR shaders
TEXT_REBUILD_ANIMATION_SHADERS,重新生成动画着色器,Rebuild Animation shaders
TEXT_BUILD_PBR_VARIANT,编译所有PBR着色器变体,Build All PBR Shader Variant
TEXT_ABOUT,关于,About
TEXT_DOCUMENTS,文档,Documents
148 changes: 50 additions & 98 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 @@ -261,140 +261,92 @@ 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);
materialComponent.ActivateShaderFeature(engine::GetSkyTypeShaderFeature(m_pSceneWorld->GetSkyComponent(m_pSceneWorld->GetSkyEntity())->GetSkyType()));

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();
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.ActiveShaderFeature(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.
ResourceBuilder::Get().Update();

// TODO : create material component before ResourceBuilder done.
// Assign a special color for loading resource status.
materialComponent.SetMaterialType(pMaterialType);
materialComponent.SetMaterialData(pMaterial);
materialComponent.SetAlbedoColor(cd::MoveTemp(albedoColor));
materialComponent.SetSkyType(m_pSceneWorld->GetSkyComponent(m_pSceneWorld->GetSkyEntity())->GetSkyType());

// Textures.
for (const auto& [outputTextureFilePath, pTextureData] : outputTexturePathToData)
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 @@ -71,6 +71,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
Loading

0 comments on commit efd2d33

Please sign in to comment.