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 31 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
85 changes: 53 additions & 32 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#include "Rendering/RenderContext.h"
#include "Rendering/SkeletonRenderer.h"
#include "Rendering/SkyboxRenderer.h"
#include "Rendering/ShaderCollections.h"
#include "Rendering/TerrainRenderer.h"
#include "Rendering/WorldRenderer.h"
#include "Rendering/ParticleRenderer.h"
#include "Resources/FileWatcher.h"
#include "Resources/ResourceBuilder.h"
#include "Resources/ShaderBuilder.h"
#include "Resources/ShaderLoader.h"
#include "Scene/SceneDatabase.h"
#include "UILayers/AssetBrowser.h"
#include "UILayers/EntityList.h"
Expand Down Expand Up @@ -88,15 +88,19 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)

// Init graphics backend
InitRenderContext(m_initArgs.backend, pSplashWindow->GetNativeHandle());

pSplashWindow->OnResize.Bind<engine::RenderContext, &engine::RenderContext::OnResize>(m_pRenderContext.get());
AddWindow(cd::MoveTemp(pSplashWindow));

InitEditorRenderers();
EditorRenderersWarmup();
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 @@ -108,6 +112,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 @@ -245,7 +250,6 @@ void EditorApp::InitECWorld()
InitEditorCameraEntity();

InitSkyEntity();
InitShaderVariantCollectionEntity();

#ifdef ENABLE_DDGI
m_pSceneWorld->InitDDGISDK();
Expand Down Expand Up @@ -324,19 +328,6 @@ void EditorApp::InitSkyEntity()
meshComponent.Submit();
}

void EditorApp::InitShaderVariantCollectionEntity()
{
engine::World* pWorld = m_pSceneWorld->GetWorld();

engine::Entity shaderVariantCollectionEntity = pWorld->CreateEntity();
m_pSceneWorld->SetShaderVariantCollectionEntity(shaderVariantCollectionEntity);

auto& nameComponent = pWorld->CreateComponent<engine::NameComponent>(shaderVariantCollectionEntity);
nameComponent.SetName("ShaderVariantCollection");

auto& shaderVariantCollectionsComponent = pWorld->CreateComponent<engine::ShaderVariantCollectionsComponent>(shaderVariantCollectionEntity);
}

#ifdef ENABLE_DDGI
void EditorApp::InitDDGIEntity()
{
Expand All @@ -352,6 +343,29 @@ void EditorApp::InitDDGIEntity()
}
#endif

void EditorApp::LazyCompileAndLoadShaders()
{
bool doCompile = false;

for (const auto& info : m_pRenderContext->GetShaderCompileTasks())
{
ShaderBuilder::BuildShader(m_pRenderContext.get(), info);
doCompile = true;
}

if (doCompile)
{
ResourceBuilder::Get().Update(true);

for (auto& info : m_pRenderContext->GetShaderCompileTasks())
{
m_pRenderContext->UploadShaderProgram(info.m_programName, info.m_featuresCombine);
}

m_pRenderContext->ClearShaderCompileTasks();
}
}

void EditorApp::InitRenderContext(engine::GraphicsBackend backend, void* hwnd)
{
CD_INFO("Init graphics backend : {}", nameof::nameof_enum(backend));
Expand Down Expand Up @@ -455,6 +469,22 @@ void EditorApp::InitEngineRenderers()
AddEngineRenderer(std::make_unique<engine::ImGuiRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget));
}

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

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

bool EditorApp::IsAtmosphericScatteringEnable() const
{
engine::GraphicsBackend backend = engine::Path::GetGraphicsBackend();
Expand All @@ -466,18 +496,11 @@ bool EditorApp::IsAtmosphericScatteringEnable() const

void EditorApp::InitShaderPrograms() const
{
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::PreBuildNonUberShaders(m_pRenderContext.get());
ShaderBuilder::PreBuildUberShaders(m_pRenderContext.get(), m_pSceneWorld->GetPBRMaterialType());

ShaderBuilder::BuildUberShader(m_pSceneWorld->GetPBRMaterialType());
ShaderBuilder::BuildUberShader(m_pSceneWorld->GetAnimationMaterialType());
#ifdef ENABLE_DDGI
ShaderBuilder::BuildUberShader(m_pSceneWorld->GetDDGIMaterialType());
ShaderBuilder::PreBuildUberShaders(m_pSceneWorld->GetDDGIMaterialType());
#endif
}

Expand Down Expand Up @@ -510,11 +533,8 @@ 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

EngineRenderersWarmup();

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

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

Expand Down Expand Up @@ -591,7 +610,7 @@ bool EditorApp::Update(float deltaTime)

for (std::unique_ptr<engine::Renderer>& pRenderer : m_pEngineRenderers)
{
if (pRenderer->IsEnable())
if (pRenderer->IsEnable() && pRenderer->CheckResources())
{
const float* pViewMatrix = pMainCameraComponent->GetViewMatrix().Begin();
const float* pProjectionMatrix = pMainCameraComponent->GetProjectionMatrix().Begin();
Expand All @@ -603,6 +622,8 @@ bool EditorApp::Update(float deltaTime)

m_pRenderContext->EndFrame();

LazyCompileAndLoadShaders();

engine::Input::Get().FlushInputs();

return !GetMainWindow()->ShouldClose();
Expand Down
10 changes: 9 additions & 1 deletion Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Renderer;
class AABBRenderer;
class RenderTarget;
class SceneWorld;
class ShaderCollections;

}

Expand Down Expand Up @@ -50,8 +51,13 @@ class EditorApp final : public engine::IApplication
void RemoveWindow(size_t index);

void InitRenderContext(engine::GraphicsBackend backend, void* hwnd = nullptr);

void InitEditorRenderers();
void InitEngineRenderers();

void EditorRenderersWarmup();
void EngineRenderersWarmup();

void InitShaderPrograms() const;
void AddEditorRenderer(std::unique_ptr<engine::Renderer> pRenderer);
void AddEngineRenderer(std::unique_ptr<engine::Renderer> pRenderer);
Expand All @@ -71,12 +77,13 @@ class EditorApp final : public engine::IApplication
private:
void InitEditorCameraEntity();
void InitSkyEntity();
void InitShaderVariantCollectionEntity();

#ifdef ENABLE_DDGI
void InitDDGIEntity();
#endif

void LazyCompileAndLoadShaders();

bool m_bInitEditor = false;
engine::EngineInitArgs m_initArgs;

Expand All @@ -101,6 +108,7 @@ class EditorApp final : public engine::IApplication

// Rendering
std::unique_ptr<engine::RenderContext> m_pRenderContext;

std::vector<std::unique_ptr<engine::Renderer>> m_pEditorRenderers;
std::vector<std::unique_ptr<engine::Renderer>> m_pEngineRenderers;

Expand Down
8 changes: 4 additions & 4 deletions Engine/Source/Editor/Resources/ResourceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool ResourceBuilder::AddTask(Process process)
return true;
}

bool ResourceBuilder::AddShaderBuildTask(ShaderType shaderType, const char* pInputFilePath, const char* pOutputFilePath, const char* pShaderFeatures)
bool ResourceBuilder::AddShaderBuildTask(engine::ShaderType shaderType, const char* pInputFilePath, const char* pOutputFilePath, const char* pShaderFeatures)
{
if (s_SkipStatus & static_cast<uint8_t>(CheckFileStatus(pInputFilePath, pOutputFilePath)))
{
Expand Down Expand Up @@ -183,15 +183,15 @@ bool ResourceBuilder::AddShaderBuildTask(ShaderType shaderType, const char* pInp
#endif

commandArguments.push_back("--type");
if (ShaderType::Compute == shaderType)
if (engine::ShaderType::Compute == shaderType)
{
commandArguments.push_back("c");
}
else if (ShaderType::Fragment == shaderType)
else if (engine::ShaderType::Fragment == shaderType)
{
commandArguments.push_back("f");
}
else if (ShaderType::Vertex == shaderType)
else if (engine::ShaderType::Vertex == shaderType)
{
commandArguments.push_back("v");
}
Expand Down
Loading