Skip to content

Commit

Permalink
change bgfx
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy176179 committed Jan 2, 2024
1 parent 05036d0 commit e386f7a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
3 changes: 3 additions & 0 deletions Engine/Source/Editor/UILayers/EntityList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)
auto& lightComponent = CreateLightComponents(entity, cd::LightType::Point, 1024.0f, cd::Vec3f(1.0f, 0.0f, 0.0f), true);
lightComponent.SetPosition(cd::Point(0.0f, 0.0f, -16.0f));
lightComponent.SetRange(1024.0f);
lightComponent.SetShadowMapTexture(BGFX_INVALID_HANDLE);
}
else if (ImGui::MenuItem("Add Spot Light"))
{
Expand All @@ -173,6 +174,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)
lightComponent.SetDirection(cd::Direction(0.0f, 0.0f, 1.0f));
lightComponent.SetRange(1024.0f);
lightComponent.SetInnerAndOuter(24.0f, 40.0f);
lightComponent.SetShadowMapTexture(BGFX_INVALID_HANDLE);
}
else if (ImGui::MenuItem("Add Directional Light"))
{
Expand All @@ -181,6 +183,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)
lightComponent.SetDirection(cd::Direction(0.0f, 0.0f, 1.0f));
lightComponent.SetCascadeNum(4);
lightComponent.SetFrustumClips(cd::Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
lightComponent.SetShadowMapTexture(BGFX_INVALID_HANDLE);
}

// ---------------------------------------- Add Area Light ---------------------------------------- //
Expand Down
21 changes: 20 additions & 1 deletion Engine/Source/Runtime/ECWorld/LightComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "LightComponent.h"

#include <bgfx/bgfx.h>

namespace engine
{

Expand Down Expand Up @@ -38,9 +40,26 @@ bool LightComponent::IsShadowMapFBsValid()

for (const auto& fb : m_shadowMapFBs)
{
if (!bgfx::isValid(fb)) return false;
if (!bgfx::isValid(static_cast<bgfx::TextureHandle>(fb))) return false;
}
return true;
}

void LightComponent::ClearShadowMapTexture()
{
bgfx::destroy(static_cast<bgfx::TextureHandle>(m_shadowMapTexture));
m_shadowMapTexture = BGFX_INVALID_HANDLE;
}

void LightComponent::ClearShadowMapFBs()
{
for (auto shadowMapFB : m_shadowMapFBs) bgfx::destroy(static_cast<bgfx::FrameBufferHandle>(shadowMapFB));
m_shadowMapFBs.clear();
}

bool LightComponent::IsShadowMapTextureValid()
{
return bgfx::isValid(static_cast<bgfx::FrameBufferHandle>(m_shadowMapTexture));
}

}
21 changes: 9 additions & 12 deletions Engine/Source/Runtime/ECWorld/LightComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include "Rendering/LightUniforms.h"
#include "Scene/LightType.h"

#include <bgfx/bgfx.h>


namespace engine
{

Expand Down Expand Up @@ -136,14 +133,14 @@ class LightComponent final
void ClearLightViewProjMatrix() { m_lightViewProjMatrices.clear(); }

bool IsShadowMapFBsValid();
void AddShadowMapFB(bgfx::FrameBufferHandle& shadowMapFB) { m_shadowMapFBs.push_back(std::move(shadowMapFB)); }
const std::vector<bgfx::FrameBufferHandle>& GetShadowMapFBs() const { return m_shadowMapFBs; }
void ClearShadowMapFBs() { for (auto shadowMapFB : m_shadowMapFBs) bgfx::destroy(shadowMapFB); m_shadowMapFBs.clear(); }
void AddShadowMapFB(uint16_t& shadowMapFB) { m_shadowMapFBs.push_back(std::move(shadowMapFB)); }
const std::vector<uint16_t>& GetShadowMapFBs() const { return m_shadowMapFBs; }
void ClearShadowMapFBs();

bool IsShadowMapTextureValid() { return bgfx::isValid(m_shadowMapTexture); }
void SetShadowMapTexture(bgfx::TextureHandle shadowMapTexture) { m_shadowMapTexture = shadowMapTexture; }
const bgfx::TextureHandle& GetShadowMapTexture() { return m_shadowMapTexture; }
void ClearShadowMapTexture() { bgfx::destroy(m_shadowMapTexture); m_shadowMapTexture = BGFX_INVALID_HANDLE; }
bool IsShadowMapTextureValid();
void SetShadowMapTexture(uint16_t shadowMapTexture) { m_shadowMapTexture = shadowMapTexture; }
const uint16_t& GetShadowMapTexture() { return m_shadowMapTexture; }
void ClearShadowMapTexture();

private:
U_Light m_lightUniformData;
Expand All @@ -157,9 +154,9 @@ class LightComponent final
float m_computedCascadeSplit[4] = { 0.0 }; // computed

// uniform
bgfx::TextureHandle m_shadowMapTexture = BGFX_INVALID_HANDLE;
uint16_t m_shadowMapTexture; // Texture Handle
std::vector<cd::Matrix4x4> m_lightViewProjMatrices;
std::vector<bgfx::FrameBufferHandle> m_shadowMapFBs;
std::vector<uint16_t> m_shadowMapFBs; // Framebuffer Handle

// Warning : We treat multiple light components as a complete and contiguous memory.
// any non-U_Light member of LightComponent will destroy this layout. --2023/6/21
Expand Down
18 changes: 9 additions & 9 deletions Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void ShadowMapRenderer::Render(float deltaTime)
lightComponent->ClearShadowMapFBs();
for (int i = 0; i < cascadeNum; ++i)
{
bgfx::FrameBufferHandle shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::D32F);
uint16_t shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::D32F).idx;
lightComponent->AddShadowMapFB(shadowMapFB);
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ void ShadowMapRenderer::Render(float deltaTime)

uint16_t viewId = m_renderPassID[shadowNum * shadowTexturePassMaxNum + cascadeIndex];
bgfx::setViewRect(viewId, 0, 0, lightComponent->GetShadowMapSize(), lightComponent->GetShadowMapSize());
bgfx::setViewFrameBuffer(viewId, lightComponent->GetShadowMapFBs().at(cascadeIndex));
bgfx::setViewFrameBuffer(viewId, static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(cascadeIndex)));
bgfx::setViewClear(viewId, BGFX_CLEAR_DEPTH, 0xffffffff, 1.0f, 0);
bgfx::setViewTransform(viewId, lightView.begin(), lightProjection.begin());

Expand Down Expand Up @@ -268,8 +268,8 @@ void ShadowMapRenderer::Render(float deltaTime)
-X +Z +X -Z
-Y
------------------------------------*/
bgfx::FrameBufferHandle shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::R32F);
uint16_t shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::R32F).idx;
lightComponent->AddShadowMapFB(shadowMapFB);
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ void ShadowMapRenderer::Render(float deltaTime)
// Settings
uint16_t viewId = m_renderPassID[shadowNum * shadowTexturePassMaxNum + i];
bgfx::setViewRect(viewId, 0, 0, lightComponent->GetShadowMapSize(), lightComponent->GetShadowMapSize());
bgfx::setViewFrameBuffer(viewId, lightComponent->GetShadowMapFBs().at(i));
bgfx::setViewFrameBuffer(viewId, static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(i)));
bgfx::setViewClear(viewId, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0xffffffff, 1.0f, 0);
bgfx::setViewTransform(viewId, lightView[i].begin(), lightProjection.begin());

Expand Down Expand Up @@ -332,8 +332,8 @@ void ShadowMapRenderer::Render(float deltaTime)
// Initialize(if not initialized) frame buffer
if (!lightComponent->IsShadowMapFBsValid())
{
bgfx::FrameBufferHandle shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::D32F);
uint16_t shadowMapFB = bgfx::createFrameBuffer(lightComponent->GetShadowMapSize(),
lightComponent->GetShadowMapSize(), bgfx::TextureFormat::D32F).idx;
lightComponent->AddShadowMapFB(shadowMapFB);
}

Expand All @@ -351,7 +351,7 @@ void ShadowMapRenderer::Render(float deltaTime)
bgfx::setState(state);
uint16_t viewId = m_renderPassID[shadowNum * shadowTexturePassMaxNum + 0];
bgfx::setViewRect(viewId, 0, 0, lightComponent->GetShadowMapSize(), lightComponent->GetShadowMapSize());
bgfx::setViewFrameBuffer(viewId, lightComponent->GetShadowMapFBs().at(0));
bgfx::setViewFrameBuffer(viewId, static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(0)));
bgfx::setViewClear(viewId, BGFX_CLEAR_DEPTH, 0xffffffff, 1.0f, 0);
bgfx::setViewTransform(viewId, lightView.begin(), lightProjection.begin());

Expand Down
24 changes: 12 additions & 12 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ void WorldRenderer::Render(float deltaTime)
bgfx::TextureHandle blitDstShadowMapTexture = bgfx::createTextureCube(lightComponent->GetShadowMapSize(),
false, 1, bgfx::TextureFormat::D32F, blitDstTextureFlags);
GetRenderContext()->SetTexture(engine::StringCrc(directionShadowMapTexture), blitDstShadowMapTexture);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture.idx);
}
// Blit RTV(FrameBuffer Texture) to SRV(Texture)
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
for (uint16_t cascadeIdx = 0; cascadeIdx < cascadeNum; ++cascadeIdx)
{
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(lightComponent->GetShadowMapFBs().at(cascadeIdx));
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(cascadeIdx)));
bgfx::blit(GetViewID(), blitDstShadowMapTexture, 0, 0, 0, cascadeIdx, blitSrcShadowMapTexture, 0, 0, 0, 0);
}
}
Expand All @@ -145,13 +145,13 @@ void WorldRenderer::Render(float deltaTime)
bgfx::TextureHandle blitDstShadowMapTexture = bgfx::createTextureCube(lightComponent->GetShadowMapSize(),
false, 1, bgfx::TextureFormat::R32F, blitDstTextureFlags);
GetRenderContext()->SetTexture(blitDstShadowMapTextureName, blitDstShadowMapTexture);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture.idx);
}
// Blit RTV(FrameBuffer Texture) to SRV(Texture)
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
for (uint16_t i = 0; i < 6; ++i)
{
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(lightComponent->GetShadowMapFBs().at(i));
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(i)));
bgfx::blit(GetViewID(), blitDstShadowMapTexture, 0, 0, 0, i, blitSrcShadowMapTexture, 0, 0, 0, 0);
}
}
Expand All @@ -162,11 +162,11 @@ void WorldRenderer::Render(float deltaTime)
{
bgfx::TextureHandle blitDstShadowMapTexture = bgfx::createTextureCube(lightComponent->GetShadowMapSize(),
false, 1, bgfx::TextureFormat::D32F, blitDstTextureFlags);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture);
lightComponent->SetShadowMapTexture(blitDstShadowMapTexture.idx);
}
// Blit RTV(FrameBuffer Texture) to SRV(Texture)
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(lightComponent->GetShadowMapFBs().at(0));
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
bgfx::TextureHandle blitSrcShadowMapTexture = bgfx::getTexture(static_cast<bgfx::FrameBufferHandle>(lightComponent->GetShadowMapFBs().at(0)));
bgfx::blit(GetViewID(), blitDstShadowMapTexture, 0, 0, 0, 0, blitSrcShadowMapTexture, 0, 0, 0, 0);
}
}
Expand Down Expand Up @@ -340,21 +340,21 @@ void WorldRenderer::Render(float deltaTime)
cd::LightType lightType = lightComponent->GetType();
if (cd::LightType::Directional == lightType)
{
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
bgfx::setTexture(SHADOW_MAP_CUBE_FIRST_SLOT+lightIndex, GetRenderContext()->GetUniform(shadowMapSamplerCrcs[lightIndex]), blitDstShadowMapTexture);
// TODO : manual
constexpr StringCrc clipFrustumDepthCrc(clipFrustumDepth);
GetRenderContext()->FillUniform(clipFrustumDepthCrc, lightComponent->GetComputedCascadeSplit(), 1);
}
else if (cd::LightType::Point == lightType)
{
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
bgfx::setTexture(SHADOW_MAP_CUBE_FIRST_SLOT+lightIndex, GetRenderContext()->GetUniform(shadowMapSamplerCrcs[lightIndex]), blitDstShadowMapTexture);
}
else if (cd::LightType::Spot == lightType)
{
// Blit RTV(FrameBuffer Texture) to SRV(Texture)
bgfx::TextureHandle blitDstShadowMapTexture = lightComponent->GetShadowMapTexture();
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
bgfx::setTexture(SHADOW_MAP_CUBE_FIRST_SLOT+lightIndex, GetRenderContext()->GetUniform(shadowMapSamplerCrcs[lightIndex]), blitDstShadowMapTexture);
}
}
Expand Down

0 comments on commit e386f7a

Please sign in to comment.