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 12 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.

31 changes: 25 additions & 6 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)
AddWindow(cd::MoveTemp(pSplashWindow));

InitEditorRenderers();
CreateEditorRendererGraphicsResources();
InitEditorImGuiContext(m_initArgs.language);

InitECWorld();
m_pEditorImGuiContext->SetSceneWorld(m_pSceneWorld.get());

InitEngineRenderers();

// Add shader build tasks and create a thread to update tasks.
InitShaderPrograms();
m_pEditorImGuiContext->AddStaticLayer(std::make_unique<Splash>("Splash"));
Expand All @@ -107,6 +110,7 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)
resourceThread.detach();

m_pFileWatcher = std::make_unique<FileWatcher>(CDENGINE_BUILTIN_SHADER_PATH);
m_pFileWatcher->EnableWatchSubTree();
}

void EditorApp::Shutdown()
Expand Down Expand Up @@ -443,6 +447,22 @@ void EditorApp::InitEngineRenderers()
AddEngineRenderer(std::make_unique<engine::ImGuiRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget));
}

void EditorApp::CreateEditorRendererGraphicsResources()
{
for (std::unique_ptr<engine::Renderer>& pRenderer : m_pEditorRenderers)
{
pRenderer->CreateGraphicsResources();
}
}

void EditorApp::CreateEngineRendererGraphicsResources()
{
for (std::unique_ptr<engine::Renderer>& pRenderer : m_pEngineRenderers)
{
pRenderer->CreateGraphicsResources();
}
}

bool EditorApp::IsAtmosphericScatteringEnable() const
{
engine::GraphicsBackend backend = engine::Path::GetGraphicsBackend();
Expand All @@ -454,13 +474,10 @@ bool EditorApp::IsAtmosphericScatteringEnable() const

void EditorApp::InitShaderPrograms() const
{
ShaderBuilder::BuildShaders(m_pSceneWorld.get());

std::string nonUberBuildPath = CDENGINE_BUILTIN_SHADER_PATH;
ShaderBuilder::BuildNonUberShader(nonUberBuildPath + "shaders");
if (IsAtmosphericScatteringEnable())
{
std::string atmBuildPath = CDENGINE_BUILTIN_SHADER_PATH;
ShaderBuilder::BuildNonUberShader(atmBuildPath + "atm");
}

ShaderBuilder::BuildUberShader(m_pSceneWorld->GetPBRMaterialType());
ShaderBuilder::BuildUberShader(m_pSceneWorld->GetAnimationMaterialType());
Expand Down Expand Up @@ -498,12 +515,15 @@ bool EditorApp::Update(float deltaTime)
if (!m_bInitEditor && ResourceBuilder::Get().IsIdle())
{
m_bInitEditor = true;

engine::ShaderLoader::UploadUberShader(m_pSceneWorld->GetPBRMaterialType());
engine::ShaderLoader::UploadUberShader(m_pSceneWorld->GetAnimationMaterialType());
#ifdef ENABLE_DDGI
engine::ShaderLoader::UploadUberShader(m_pSceneWorld->GetDDGIMaterialType());
#endif

CreateEngineRendererGraphicsResources();

// Phase 2 - Project Manager
// * TODO : Show project selector
//GetMainWindow()->SetTitle("Project Manager");
Expand All @@ -521,7 +541,6 @@ bool EditorApp::Update(float deltaTime)
GetMainWindow()->SetBordedLess(false);
GetMainWindow()->SetResizeable(true);

InitEngineRenderers();
m_pEditorImGuiContext->ClearUILayers();
InitEditorUILayers();

Expand Down
4 changes: 4 additions & 0 deletions Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class EditorApp final : public engine::IApplication
void InitRenderContext(engine::GraphicsBackend backend, void* hwnd = nullptr);
void InitEditorRenderers();
void InitEngineRenderers();

void CreateEditorRendererGraphicsResources();
void CreateEngineRendererGraphicsResources();

void InitShaderPrograms() const;
void AddEditorRenderer(std::unique_ptr<engine::Renderer> pRenderer);
void AddEngineRenderer(std::unique_ptr<engine::Renderer> pRenderer);
Expand Down
22 changes: 21 additions & 1 deletion Engine/Source/Editor/Resources/ShaderBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ShaderBuilder.h"

#include "ECWorld/SceneWorld.h"
#include "Log/Log.h"
#include "Path/Path.h"
#include "Rendering/RenderContext.h"
Expand All @@ -12,7 +13,7 @@ void ShaderBuilder::BuildUberShader(engine::MaterialType* pMaterialType)
{
engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema();

// No shader feature support for VS now.
// No shader feature supports for VS now.
std::string outputVSFilePath = engine::Path::GetShaderOutputPath(shaderSchema.GetVertexShaderPath());
ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Vertex,
shaderSchema.GetVertexShaderPath(), outputVSFilePath.c_str());
Expand Down Expand Up @@ -50,6 +51,25 @@ void ShaderBuilder::BuildNonUberShader(std::string folderPath)
}
}

void ShaderBuilder::BuildShaders(engine::SceneWorld* pSceneWorld)
{
auto* pShaderVariantCollectionsComponent = pSceneWorld->GetShaderVariantCollectionsComponent(pSceneWorld->GetShaderVariantCollectionEntity());
const auto& shaderInfos = pShaderVariantCollectionsComponent->GetShaderInformations();

// for (const auto& info : shaderInfos)
// {
// if (info.second.empty())
// {
// // Non-Uber shader case
// BuildNonUberShader(info.first);
// }
// else
// {
// // Uber shader case
// }
// }
}

const ShaderType ShaderBuilder::GetShaderType(const std::string& fileName)
{
if (fileName._Starts_with("vs_") || fileName._Starts_with("VS_"))
Expand Down
9 changes: 9 additions & 0 deletions Engine/Source/Editor/Resources/ShaderBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include <map>
#include <string>

namespace engine
{

class SceneWorld;

}

namespace editor
{

Expand All @@ -15,6 +22,8 @@ class ShaderBuilder
static void BuildNonUberShader(std::string folderPath);
static void BuildUberShader(engine::MaterialType* pMaterialType);

static void BuildShaders(engine::SceneWorld* pSceneWorld);

private:
static const ShaderType GetShaderType(const std::string& fileName);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
#include "ShaderVariantCollectionsComponent.h"

#include "Log/Log.h"

#include <cassert>

namespace engine
{

void ShaderVariantCollectionsComponent::RegisterPragram(std::string programName, ShaderProgramPack pack)
{
if (IsValid(programName))
{
CD_ENGINE_WARN("Program pack {0} already exists in ShaderLibrary!", cd::MoveTemp(programName));
return;
}

m_shaderPrograms[cd::MoveTemp(programName)] = cd::MoveTemp(pack);
}

void ShaderVariantCollectionsComponent::ActivateShaderFeature(std::string programName, ShaderFeature feature)
{
assert(IsValid(programName) && "Program does not exist in ShaderLibrary!");

m_shaderPrograms[cd::MoveTemp(programName)].ActivateShaderFeature(cd::MoveTemp(feature));
}

void ShaderVariantCollectionsComponent::DeactiveShaderFeature(std::string programName, ShaderFeature feature)
{
assert(IsValid(programName) && "Shader information does not exist!");

m_shaderPrograms[cd::MoveTemp(programName)].DeactivateShaderFeature(cd::MoveTemp(feature));
}

void ShaderVariantCollectionsComponent::SetShaderPrograms(std::map<std::string, ShaderProgramPack> program)
{
m_shaderPrograms = cd::MoveTemp(program);
}

bool ShaderVariantCollectionsComponent::IsValid(std::string path) const
{
return (m_shaderPrograms.find(cd::MoveTemp(path)) != m_shaderPrograms.end());
}

}
27 changes: 27 additions & 0 deletions Engine/Source/Runtime/ECWorld/ShaderVariantCollectionsComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

#include "Base/Template.h"
#include "Core/StringCrc.h"
#include "Rendering/ShaderProgramPack.h"

#include <map>
#include <memory>
#include <string>

namespace engine
{

class ShaderVariantCollectionsComponent final
{
public:
using ShaderBlob = std::vector<std::byte>;

public:
static constexpr StringCrc GetClassName()
{
Expand All @@ -22,8 +30,27 @@ class ShaderVariantCollectionsComponent final
ShaderVariantCollectionsComponent& operator=(ShaderVariantCollectionsComponent&&) = default;
~ShaderVariantCollectionsComponent() = default;

void RegisterPragram(std::string programName, ShaderProgramPack pack);

void ActivateShaderFeature(std::string programName, ShaderFeature feature);
void DeactiveShaderFeature(std::string programName, ShaderFeature feature);

void SetShaderPrograms(std::map<std::string, ShaderProgramPack> program);
std::map<std::string, ShaderProgramPack>& GetShaderInformations() { return m_shaderPrograms; }
const std::map<std::string, ShaderProgramPack>& GetShaderInformations() const { return m_shaderPrograms; }

private:
inline bool IsValid(std::string path) const;

// Key : Shader source file path, Value : Shader feature set.
// Non-Uber shader has no features, in this case the value will be an empty std::set.
// std::map<std::string, ShaderFeatureSet> m_shaderInformations;

// Key : Program Name, Value : VS Path + FS Path + variant.
std::map<std::string, ShaderProgramPack> m_shaderPrograms;

// Key : Compiled Binary Shader File Name, Value : File Data.
// std::map<std::string, std::unique_ptr<ShaderBlob>> m_shaderData;
};

}
13 changes: 9 additions & 4 deletions Engine/Source/Runtime/ECWorld/SkyComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace engine
{

void SkyComponent::SetSkyType(SkyType crtType)
void SkyComponent::SetSkyType(SkyType type)
{
if (m_type == crtType)
if (m_type == type)
{
return;
}

m_type = crtType;
m_type = type;
static std::string preRadPath = m_radianceTexturePath;
if (SkyType::None == m_type)
{
preRadPath = cd::MoveTemp(m_radianceTexturePath);
m_radianceTexturePath = SkyComponent::PureGrayTexturePath;
m_radianceTexturePath = m_pureGrayTexturePath;
}
else if(preRadPath != m_radianceTexturePath)
{
Expand All @@ -38,4 +38,9 @@ void SkyComponent::SetRadianceTexturePath(std::string path)
m_radianceTexturePath = cd::MoveTemp(path);
}

void SkyComponent::SetPureGrayTexturePath(std::string path)
{
m_pureGrayTexturePath = cd::MoveTemp(path);
}

}
9 changes: 7 additions & 2 deletions Engine/Source/Runtime/ECWorld/SkyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SkyComponent final
public:
static constexpr const char* DefaultIrradainceTexturePath = "Textures/skybox/defaultSkybox_irr.dds";
static constexpr const char* DefaultRadianceTexturePath = "Textures/skybox/defaultSkybox_rad.dds";
static constexpr const char* PureGrayTexturePath = "Textures/skybox/PureGray.dds";
static constexpr const char* DefaultPureGrayTexturePath = "Textures/skybox/PureGray.dds";

public:
static constexpr StringCrc GetClassName()
Expand All @@ -39,7 +39,7 @@ class SkyComponent final
SkyComponent& operator=(SkyComponent&&) = default;
~SkyComponent() = default;

void SetSkyType(SkyType crtType);
void SetSkyType(SkyType type);
SkyType& GetSkyType() { return m_type; }
const SkyType& GetSkyType() const { return m_type; }

Expand Down Expand Up @@ -79,6 +79,10 @@ class SkyComponent final
std::string& GetRadianceTexturePath() { return m_radianceTexturePath; }
const std::string& GetRadianceTexturePath() const { return m_radianceTexturePath; }

void SetPureGrayTexturePath(std::string path);
std::string& GetPureGrayTexturePath() { return m_pureGrayTexturePath; }
const std::string& GetPureGrayTexturePath() const { return m_pureGrayTexturePath; }

private:
SkyType m_type = SkyType::SkyBox;
bool m_isAtmophericScatteringEnable = false;
Expand All @@ -94,6 +98,7 @@ class SkyComponent final

std::string m_irradianceTexturePath = DefaultIrradainceTexturePath;
std::string m_radianceTexturePath = DefaultRadianceTexturePath;
std::string m_pureGrayTexturePath = DefaultPureGrayTexturePath;
};

}
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/ECWorld/TerrainComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TerrainComponent final
uint16_t m_meshWidth = 129U;//uint32_t is too big for width
uint16_t m_meshDepth = 129U;

//height map input
//m_height map input
uint16_t m_texWidth = 129U;//uint32_t is too big for width
uint16_t m_texDepth = 129U;//
float m_roughness = 1.55f;
Expand Down
Loading