Skip to content

Commit

Permalink
Separating shader compiling and loading logic in the renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Sep 12, 2023
1 parent 87803fa commit fccf8ab
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 65 deletions.
10 changes: 9 additions & 1 deletion Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)
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 Down Expand Up @@ -499,6 +501,13 @@ bool EditorApp::Update(float deltaTime)
if (!m_bInitEditor && ResourceBuilder::Get().IsIdle())
{
m_bInitEditor = true;

// Load compiled shaders.
for (std::unique_ptr<engine::Renderer>& pRenderer : m_pEngineRenderers)
{
pRenderer->LoadShaders();
}

engine::ShaderLoader::UploadUberShader(m_pSceneWorld->GetPBRMaterialType());
engine::ShaderLoader::UploadUberShader(m_pSceneWorld->GetAnimationMaterialType());
#ifdef ENABLE_DDGI
Expand All @@ -522,7 +531,6 @@ bool EditorApp::Update(float deltaTime)
GetMainWindow()->SetBordedLess(false);
GetMainWindow()->SetResizeable(true);

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

Expand Down
7 changes: 2 additions & 5 deletions Engine/Source/Runtime/Rendering/AABBRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ namespace engine

void AABBRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_AABB");
pShaderVariantCollectionsComponent->AddShader("fs_AABB");

GetRenderContext()->CreateProgram("AABBProgram", "vs_AABB.bin", "fs_AABB.bin");

bgfx::setViewName(GetViewID(), "AABBRenderer");
}

void AABBRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("AABBProgram", "vs_AABB.bin", "fs_AABB.bin");
}

void AABBRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
15 changes: 6 additions & 9 deletions Engine/Source/Runtime/Rendering/AnimationRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,22 @@ void CalculateBoneTransform(std::vector<cd::Matrix4x4>& boneMatrices, const cd::

void AnimationRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_animation");
pShaderVariantCollectionsComponent->AddShader("fs_animation");

bgfx::setViewName(GetViewID(), "AnimationRenderer");
}

void AnimationRenderer::LoadShaders()
{
#ifdef VISUALIZE_BONE_WEIGHTS
m_pRenderContext->CreateUniform("u_debugBoneIndex", bgfx::UniformType::Vec4, 1);
m_pRenderContext->CreateProgram("AnimationProgram", "vs_visualize_bone_weight.bin", "fs_visualize_bone_weight.bin");
#else
GetRenderContext()->CreateProgram("AnimationProgram", "vs_animation.bin", "fs_animation.bin");
#endif

bgfx::setViewName(GetViewID(), "AnimationRenderer");
}

void AnimationRenderer::LoadShaders()
{

}

void AnimationRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
18 changes: 8 additions & 10 deletions Engine/Source/Runtime/Rendering/BloomRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace engine
{
void BloomRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_fullscreen");
pShaderVariantCollectionsComponent->AddShader("fs_captureBrightness");
Expand All @@ -24,13 +23,6 @@ namespace engine
GetRenderContext()->CreateUniform("u_textureSize", bgfx::UniformType::Vec4);
GetRenderContext()->CreateUniform("u_bloomIntensity", bgfx::UniformType::Vec4);
GetRenderContext()->CreateUniform("u_luminanceThreshold", bgfx::UniformType::Vec4);
GetRenderContext()->CreateProgram("CapTureBrightnessProgram", "vs_fullscreen.bin", "fs_captureBrightness.bin");
GetRenderContext()->CreateProgram("DownSampleProgram", "vs_fullscreen.bin", "fs_dowmsample.bin");
GetRenderContext()->CreateProgram("BlurVerticalProgram", "vs_fullscreen.bin", "fs_blurvertical.bin");
GetRenderContext()->CreateProgram("BlurHorizontalProgram", "vs_fullscreen.bin", "fs_blurhorizontal.bin");
GetRenderContext()->CreateProgram("UpSampleProgram", "vs_fullscreen.bin", "fs_upsample.bin");
GetRenderContext()->CreateProgram("KawaseBlurProgram", "vs_fullscreen.bin", "fs_kawaseblur.bin");
GetRenderContext()->CreateProgram("CombineProgram", "vs_fullscreen.bin", "fs_bloom.bin");

bgfx::setViewName(GetViewID(), "BloomRenderer");

Expand All @@ -51,7 +43,13 @@ namespace engine

void BloomRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("CapTureBrightnessProgram", "vs_fullscreen.bin", "fs_captureBrightness.bin");
GetRenderContext()->CreateProgram("DownSampleProgram", "vs_fullscreen.bin", "fs_dowmsample.bin");
GetRenderContext()->CreateProgram("BlurVerticalProgram", "vs_fullscreen.bin", "fs_blurvertical.bin");
GetRenderContext()->CreateProgram("BlurHorizontalProgram", "vs_fullscreen.bin", "fs_blurhorizontal.bin");
GetRenderContext()->CreateProgram("UpSampleProgram", "vs_fullscreen.bin", "fs_upsample.bin");
GetRenderContext()->CreateProgram("KawaseBlurProgram", "vs_fullscreen.bin", "fs_kawaseblur.bin");
GetRenderContext()->CreateProgram("CombineProgram", "vs_fullscreen.bin", "fs_bloom.bin");
}

BloomRenderer::~BloomRenderer()
Expand Down
6 changes: 2 additions & 4 deletions Engine/Source/Runtime/Rendering/DebugRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ namespace engine

void DebugRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_debug");
pShaderVariantCollectionsComponent->AddShader("fs_debug");

GetRenderContext()->CreateProgram("DebugProgram", "vs_debug.bin", "fs_debug.bin");
bgfx::setViewName(GetViewID(), "DebugRenderer");
}

void DebugRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("DebugProgram", "vs_debug.bin", "fs_debug.bin");
}

void DebugRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void ImGuiRenderer::Init()
}

GetRenderContext()->CreateUniform("s_tex", bgfx::UniformType::Sampler);
GetRenderContext()->CreateProgram("ImGuiProgram", "vs_imgui.bin", "fs_imgui.bin");

bgfx::setViewName(GetViewID(), "ImGuiRenderer");
}

void ImGuiRenderer::LoadShaders()
{
GetRenderContext()->CreateProgram("ImGuiProgram", "vs_imgui.bin", "fs_imgui.bin");
}

ImGuiRenderer::~ImGuiRenderer()
Expand Down
23 changes: 9 additions & 14 deletions Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ constexpr uint16_t ScatteringOrders = 6;

void PBRSkyRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_atmSkyBox");
pShaderVariantCollectionsComponent->AddShader("fs_PrecomputedAtmosphericScattering_LUT");
Expand All @@ -62,18 +61,6 @@ void PBRSkyRenderer::Init()
pShaderVariantCollectionsComponent->AddShader("cs_ComputeIndirectIrradiance");
pShaderVariantCollectionsComponent->AddShader("cs_ComputeMultipleScattering");

SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());

GetRenderContext()->CreateProgram(ProgramAtmosphericScatteringLUT, "vs_atmSkyBox.bin", "fs_PrecomputedAtmosphericScattering_LUT.bin");
GetRenderContext()->CreateProgram(ProgramSingleScatteringRayMarching, "vs_atmSkyBox.bin", "fs_SingleScattering_RayMarching.bin");

GetRenderContext()->CreateProgram(ProgramComputeTransmittance, "cs_ComputeTransmittance.bin");
GetRenderContext()->CreateProgram(ProgramComputeDirectIrradiance, "cs_ComputeDirectIrradiance.bin");
GetRenderContext()->CreateProgram(ProgramComputeSingleScattering, "cs_ComputeSingleScattering.bin");
GetRenderContext()->CreateProgram(ProgramComputeScatteringDensity, "cs_ComputeScatteringDensity.bin");
GetRenderContext()->CreateProgram(ProgramComputeIndirectIrradiance, "cs_ComputeIndirectIrradiance.bin");
GetRenderContext()->CreateProgram(ProgramComputeMultipleScattering, "cs_ComputeMultipleScattering.bin");

GetRenderContext()->CreateTexture(TextureTransmittance, TRANSMITTANCE_TEXTURE_WIDTH, TRANSMITTANCE_TEXTURE_HEIGHT, 1,
bgfx::TextureFormat::RGBA32F, FlagTexture2D);
GetRenderContext()->CreateTexture(TextureIrradiance, IRRADIANCE_TEXTURE_WIDTH, IRRADIANCE_TEXTURE_HEIGHT, 1,
Expand Down Expand Up @@ -101,7 +88,15 @@ void PBRSkyRenderer::Init()

void PBRSkyRenderer::LoadShaders()
{
GetRenderContext()->CreateProgram(ProgramAtmosphericScatteringLUT, "vs_atmSkyBox.bin", "fs_PrecomputedAtmosphericScattering_LUT.bin");
GetRenderContext()->CreateProgram(ProgramSingleScatteringRayMarching, "vs_atmSkyBox.bin", "fs_SingleScattering_RayMarching.bin");

GetRenderContext()->CreateProgram(ProgramComputeTransmittance, "cs_ComputeTransmittance.bin");
GetRenderContext()->CreateProgram(ProgramComputeDirectIrradiance, "cs_ComputeDirectIrradiance.bin");
GetRenderContext()->CreateProgram(ProgramComputeSingleScattering, "cs_ComputeSingleScattering.bin");
GetRenderContext()->CreateProgram(ProgramComputeScatteringDensity, "cs_ComputeScatteringDensity.bin");
GetRenderContext()->CreateProgram(ProgramComputeIndirectIrradiance, "cs_ComputeIndirectIrradiance.bin");
GetRenderContext()->CreateProgram(ProgramComputeMultipleScattering, "cs_ComputeMultipleScattering.bin");
}

void PBRSkyRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
3 changes: 1 addition & 2 deletions Engine/Source/Runtime/Rendering/ParticleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace engine {

void ParticleRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

// pShaderVariantCollectionsComponent->AddShader("");
// pShaderVariantCollectionsComponent->AddShader("");
Expand Down
6 changes: 2 additions & 4 deletions Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ namespace engine

void PostProcessRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_fullscreen");
pShaderVariantCollectionsComponent->AddShader("fs_PBR_postProcessing");

GetRenderContext()->CreateUniform("s_lightingColor", bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform("u_gamma", bgfx::UniformType::Vec4);
GetRenderContext()->CreateProgram("PostProcessProgram", "vs_fullscreen.bin", "fs_PBR_postProcessing.bin");

bgfx::setViewName(GetViewID(), "PostProcessRenderer");
}

void PostProcessRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("PostProcessProgram", "vs_fullscreen.bin", "fs_PBR_postProcessing.bin");
}

PostProcessRenderer::~PostProcessRenderer()
Expand Down
6 changes: 2 additions & 4 deletions Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ void TraverseBone(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase,

void SkeletonRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_AABB");
pShaderVariantCollectionsComponent->AddShader("fs_AABB");

GetRenderContext()->CreateProgram("SkeletonProgram", "vs_AABB.bin", "fs_AABB.bin");
bgfx::setViewName(GetViewID(), "SkeletonRenderer");
}

void SkeletonRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("SkeletonProgram", "vs_AABB.bin", "fs_AABB.bin");
}

void SkeletonRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
6 changes: 2 additions & 4 deletions Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ constexpr uint64_t renderState = BGFX_STATE_WRITE_MASK | BGFX_STATE_CULL_CCW | B

void SkyboxRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_skybox");
pShaderVariantCollectionsComponent->AddShader("fs_skybox");
Expand All @@ -31,14 +30,13 @@ void SkyboxRenderer::Init()

GetRenderContext()->CreateUniform(skyboxSampler, bgfx::UniformType::Sampler);
GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), sampleFalg);
GetRenderContext()->CreateProgram(skyboxShader, "vs_skybox.bin", "fs_skybox.bin");

bgfx::setViewName(GetViewID(), "SkyboxRenderer");
}

void SkyboxRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram(skyboxShader, "vs_skybox.bin", "fs_skybox.bin");
}

void SkyboxRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
6 changes: 2 additions & 4 deletions Engine/Source/Runtime/Rendering/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ constexpr uint64_t defaultRenderingState = BGFX_STATE_WRITE_MASK | BGFX_STATE_MS

void TerrainRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_terrain");
pShaderVariantCollectionsComponent->AddShader("fs_terrain");

SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());

GetRenderContext()->CreateProgram("TerrainProgram", "vs_terrain.bin", "fs_terrain.bin");
GetRenderContext()->CreateUniform(snowSampler, bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform(rockSampler, bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform(grassSampler, bgfx::UniformType::Sampler);
Expand Down Expand Up @@ -94,7 +92,7 @@ void TerrainRenderer::Init()

void TerrainRenderer::LoadShaders()
{

GetRenderContext()->CreateProgram("TerrainProgram", "vs_terrain.bin", "fs_terrain.bin");
}

void TerrainRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down
5 changes: 2 additions & 3 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ constexpr uint64_t defaultRenderingState = BGFX_STATE_WRITE_MASK | BGFX_STATE_MS

void WorldRenderer::Init()
{
const auto& shaderVariantCollectionsEntity = m_pCurrentSceneWorld->GetShaderVariantCollectionEntity();
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(shaderVariantCollectionsEntity);
auto* pShaderVariantCollectionsComponent = m_pCurrentSceneWorld->GetShaderVariantCollectionsComponent(m_pCurrentSceneWorld->GetShaderVariantCollectionEntity());

pShaderVariantCollectionsComponent->AddShader("vs_PBR");
pShaderVariantCollectionsComponent->AddShader("fs_PBR");
Expand Down Expand Up @@ -81,7 +80,7 @@ void WorldRenderer::Init()

void WorldRenderer::LoadShaders()
{

// TODO : Transfer controling of shader in material to SVC.
}

void WorldRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix)
Expand Down

0 comments on commit fccf8ab

Please sign in to comment.