Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader lazy load #382

Merged
merged 47 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
141d45e
Some optimize.
roeas Sep 11, 2023
3732d3c
Shader Variant Collections Component
roeas Sep 11, 2023
fd7c50b
Fix compile bug.
roeas Sep 11, 2023
0039034
Upload pre compiled imgui shader to github.
roeas Sep 11, 2023
23c934b
Renderer::LoadShaders
roeas Sep 11, 2023
87803fa
include cassert
roeas Sep 11, 2023
fccf8ab
Separating shader compiling and loading logic in the renderer.
roeas Sep 12, 2023
3404891
Renderer::CreateGraphicsResources
roeas Sep 12, 2023
782e884
typo
roeas Sep 12, 2023
3dedbb6
Merge branch 'main' into collection
roeas Sep 12, 2023
c39c479
Preparing to use SVC to guide shader compiling.
roeas Sep 13, 2023
660f9eb
ShaderVariantCollectionsComponent::RegisterPragram
roeas Sep 13, 2023
508a497
SVC guiding non-uber shader compiling.
roeas Sep 13, 2023
1353ac1
rename
roeas Sep 13, 2023
8b6d9a3
No more treating SVC as a component.
roeas Sep 14, 2023
7479561
SVC guiding uber shader compiling.
roeas Sep 15, 2023
c690cb1
SVC guiding shader loading.
roeas Sep 16, 2023
6a2f689
merge main
roeas Sep 16, 2023
099e885
Refactor RenderContext
roeas Sep 19, 2023
6b0fd02
merge main
roeas Sep 19, 2023
1ad81e9
Fix clang compile error.
roeas Sep 19, 2023
cf065d4
Preparing for lazy compiling and loading.
roeas Sep 19, 2023
69273bd
Lazy compile and load.
roeas Sep 19, 2023
c4535a6
fix clang compile error
roeas Sep 20, 2023
4bcf703
CheckResources::CheckResources
roeas Sep 20, 2023
5ff73c1
Renderer::Warmup
roeas Sep 21, 2023
e82be84
Trate uber shader without shader feature as non-uber shader.
roeas Sep 21, 2023
fcfb1f9
SVC store StringCrc as map key.
roeas Sep 21, 2023
fadc3f7
Merge branch 'main' into collection
roeas Sep 22, 2023
de15fed
RenderContext::AddShaderBlob return blob if exist.
roeas Sep 22, 2023
97ba310
Rename SVC to ShaderCollections.
roeas Sep 22, 2023
affc9af
optimize
roeas Sep 22, 2023
17005ba
RenderContext store uint16_t as handle.
roeas Sep 22, 2023
b500574
Cleanup ShaderSchema and MaterialComponent. Add compile variant button.
roeas Sep 22, 2023
2606d56
ResourceBuilder use pOutputFilePath as modifyTimeCache's key.
roeas Sep 22, 2023
f0ec269
rename
roeas Sep 23, 2023
eef910a
MaterialComponent::ActivateShaderFeature check shader feature comflict.
roeas Sep 23, 2023
7f8779a
Delete RequiredTextureType
roeas Sep 23, 2023
e94a304
EditorApp::UpdateMaterials
roeas Sep 25, 2023
8115cbe
merge main
roeas Sep 25, 2023
a8d3eda
Delete SkyType::Count
roeas Sep 25, 2023
3759347
Merge main
roeas Oct 23, 2023
773796c
Update BlendShapeRenderer and PostProcessRenderer
roeas Oct 23, 2023
3fcf2af
optimize
roeas Oct 23, 2023
029ae5b
Add a launch option to compile all uber shader variant.
roeas Oct 23, 2023
8bd62d5
Merge branch 'main' into collection
roeas Oct 24, 2023
a8251ec
Disable compileAllShaders in main
roeas Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -12 to +24
Copy link
Contributor Author

@roeas roeas Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upload vs_imgui.bin and fs_imgui.bin to github.
Seems that i cant just write something like

Projects/Shared/*
!Projects/Shared/BuiltInShaders/Direct3D11/vs_imgui.bin

to upload vs_imgui.bin but ignore other files under Shared.


# 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
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
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