Skip to content

Commit 071ced0

Browse files
ImGui renderer: code cleanup
1 parent abd2394 commit 071ced0

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

Imgui/src/ImGuiDiligentRenderer.cpp

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,9 @@ void ImGuiDiligentRenderer::InvalidateDeviceObjects()
536536
{
537537
// Destroy ImGui textures that may still exist
538538
ImGuiPlatformIO& io = ImGui::GetPlatformIO();
539-
for (int n = 0; n < io.Textures.Size; ++n)
539+
for (ImTextureData* tex : io.Textures)
540540
{
541-
DestroyTexture(io.Textures[n]);
541+
DestroyTexture(tex);
542542
}
543543

544544
m_pVB.Release();
@@ -844,60 +844,77 @@ float4 ImGuiDiligentRenderer::TransformClipRect(const ImVec2& DisplaySize, const
844844
}
845845
}
846846

847-
void ImGuiDiligentRenderer::UpdateTexture(IDeviceContext* pCtx, ImTextureData* tex)
847+
inline TEXTURE_FORMAT ImTextureFormatToDiligentFormat(ImTextureFormat format)
848848
{
849-
auto* backend = static_cast<ITexture*>(tex->BackendUserData);
849+
switch (format)
850+
{
851+
case ImTextureFormat_Alpha8:
852+
return TEX_FORMAT_R8_UNORM;
853+
case ImTextureFormat_RGBA32:
854+
return TEX_FORMAT_RGBA8_UNORM;
855+
default:
856+
UNEXPECTED("Unknown texture format");
857+
return TEX_FORMAT_UNKNOWN;
858+
}
859+
}
850860

851-
if (tex->Status == ImTextureStatus_WantCreate)
861+
void ImGuiDiligentRenderer::UpdateTexture(IDeviceContext* pCtx, ImTextureData* pTexData)
862+
{
863+
ITexture* pTexture = static_cast<ITexture*>(pTexData->BackendUserData);
864+
if (pTexData->Status == ImTextureStatus_WantCreate)
852865
{
853-
IM_ASSERT(backend == nullptr && tex->TexID == ImTextureID_Invalid);
854-
855-
TextureDesc desc;
856-
desc.Name = "ImGuiTexture";
857-
desc.Type = RESOURCE_DIM_TEX_2D;
858-
desc.Width = static_cast<Uint32>(tex->Width);
859-
desc.Height = static_cast<Uint32>(tex->Height);
860-
desc.Format = tex->Format == ImTextureFormat_Alpha8 ? TEX_FORMAT_R8_UNORM : TEX_FORMAT_RGBA8_UNORM;
861-
desc.Usage = USAGE_DEFAULT; // allow future UpdateTexture()
862-
desc.BindFlags = BIND_SHADER_RESOURCE;
863-
864-
TextureSubResData mip0;
865-
mip0.pData = tex->GetPixels();
866-
mip0.Stride = tex->GetPitch();
867-
TextureData init(&mip0, 1);
868-
869-
ITexture* pTexture = nullptr;
870-
m_pDevice->CreateTexture(desc, &init, &pTexture);
866+
IM_ASSERT(pTexture == nullptr && pTexData->TexID == ImTextureID_Invalid);
867+
868+
TextureDesc Desc;
869+
Desc.Name = "ImGuiTexture";
870+
Desc.Type = RESOURCE_DIM_TEX_2D;
871+
Desc.Width = static_cast<Uint32>(pTexData->Width);
872+
Desc.Height = static_cast<Uint32>(pTexData->Height);
873+
Desc.Format = ImTextureFormatToDiligentFormat(pTexData->Format);
874+
Desc.Usage = USAGE_DEFAULT; // allow future UpdateTexture()
875+
Desc.BindFlags = BIND_SHADER_RESOURCE;
876+
877+
TextureSubResData mip0{
878+
pTexData->GetPixels(),
879+
static_cast<Uint64>(pTexData->GetPitch()),
880+
};
881+
TextureData init{&mip0, 1};
882+
883+
m_pDevice->CreateTexture(Desc, &init, &pTexture);
884+
VERIFY_EXPR(pTexture != nullptr);
871885
ITextureView* ptexView = pTexture->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
886+
VERIFY_EXPR(ptexView != nullptr);
872887

873888
// store texture view and texture pointers inside imgui and set texture state to ok
874-
tex->SetTexID(reinterpret_cast<ImTextureID>(ptexView));
875-
tex->BackendUserData = reinterpret_cast<void*>(pTexture);
876-
tex->SetStatus(ImTextureStatus_OK);
877-
return;
889+
pTexData->SetTexID(reinterpret_cast<ImTextureID>(ptexView));
890+
pTexData->BackendUserData = reinterpret_cast<void*>(pTexture);
891+
pTexData->SetStatus(ImTextureStatus_OK);
878892
}
879-
else if (tex->Status == ImTextureStatus_WantUpdates && backend != nullptr)
893+
else if (pTexData->Status == ImTextureStatus_WantUpdates && pTexture != nullptr)
880894
{
881-
Box dstBox{Uint32(tex->UpdateRect.x), Uint32(tex->UpdateRect.x + tex->UpdateRect.w),
882-
Uint32(tex->UpdateRect.y), Uint32(tex->UpdateRect.y + tex->UpdateRect.h)};
895+
Box dstBox{
896+
static_cast<Uint32>(pTexData->UpdateRect.x),
897+
static_cast<Uint32>(pTexData->UpdateRect.x + pTexData->UpdateRect.w),
898+
static_cast<Uint32>(pTexData->UpdateRect.y),
899+
static_cast<Uint32>(pTexData->UpdateRect.y + pTexData->UpdateRect.h),
900+
};
883901

884902
TextureSubResData SubresData;
885-
SubresData.pData = tex->GetPixelsAt(tex->UpdateRect.x, tex->UpdateRect.y);
886-
SubresData.Stride = tex->GetPitch();
903+
SubresData.pData = pTexData->GetPixelsAt(pTexData->UpdateRect.x, pTexData->UpdateRect.y);
904+
SubresData.Stride = pTexData->GetPitch();
887905

888-
pCtx->UpdateTexture(backend, 0, 0, dstBox, SubresData, RESOURCE_STATE_TRANSITION_MODE_VERIFY, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
889-
tex->SetStatus(ImTextureStatus_OK);
890-
return;
906+
pCtx->UpdateTexture(pTexture, 0, 0, dstBox, SubresData, RESOURCE_STATE_TRANSITION_MODE_VERIFY, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
907+
pTexData->SetStatus(ImTextureStatus_OK);
891908
}
892-
else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
909+
else if (pTexData->Status == ImTextureStatus_WantDestroy && pTexData->UnusedFrames > 0)
893910
{
894-
DestroyTexture(tex);
911+
DestroyTexture(pTexData);
895912
}
896913
}
897914

898915
void ImGuiDiligentRenderer::DestroyTexture(ImTextureData* tex)
899916
{
900-
if (auto* pTexture = static_cast<ITexture*>(tex->BackendUserData))
917+
if (ITexture* pTexture = static_cast<ITexture*>(tex->BackendUserData))
901918
{
902919
pTexture->Release();
903920
}
@@ -911,7 +928,7 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr
911928
{
912929
ScopedDebugGroup DebugGroup{pCtx, "ImGui"};
913930

914-
// Handle requested texture creates/updates/destroys -----------------
931+
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
915932
if (pDrawData->Textures != nullptr)
916933
{
917934
for (ImTextureData* tex : *pDrawData->Textures)
@@ -959,8 +976,8 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr
959976
}
960977

961978
{
962-
MapHelper<ImDrawVert> Vertices(pCtx, m_pVB, MAP_WRITE, MAP_FLAG_DISCARD);
963-
MapHelper<ImDrawIdx> Indices(pCtx, m_pIB, MAP_WRITE, MAP_FLAG_DISCARD);
979+
MapHelper<ImDrawVert> Vertices{pCtx, m_pVB, MAP_WRITE, MAP_FLAG_DISCARD};
980+
MapHelper<ImDrawIdx> Indices{pCtx, m_pIB, MAP_WRITE, MAP_FLAG_DISCARD};
964981
if (!Vertices || !Indices)
965982
return;
966983

@@ -1033,7 +1050,7 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr
10331050
UNEXPECTED("Unknown transform");
10341051
}
10351052

1036-
MapHelper<float4x4> CBData(pCtx, m_pVertexConstantBuffer, MAP_WRITE, MAP_FLAG_DISCARD);
1053+
MapHelper<float4x4> CBData{pCtx, m_pVertexConstantBuffer, MAP_WRITE, MAP_FLAG_DISCARD};
10371054
if (!CBData)
10381055
return;
10391056

0 commit comments

Comments
 (0)