Skip to content

Commit 142827f

Browse files
committed
Backends: DX12: rework legacy path for handling ImGui_ImplDX12_Init() being called with space for a single descriptor.
1 parent 08400f5 commit 142827f

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

backends/imgui_impl_dx12.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ struct ImGui_ImplDX12_Data
7272
ID3D12RootSignature* pRootSignature;
7373
ID3D12PipelineState* pPipelineState;
7474
DXGI_FORMAT RTVFormat;
75-
ImGui_ImplDX12_Texture FontTexture;
7675
ID3D12DescriptorHeap* pd3dSrvDescHeap;
7776
UINT numFramesInFlight;
7877

7978
ImGui_ImplDX12_RenderBuffers* pFrameResources;
8079
UINT frameIndex;
8180

81+
ImGui_ImplDX12_Texture FontTexture;
82+
bool LegacySingleDescriptorUsed;
83+
8284
ImGui_ImplDX12_Data() { memset((void*)this, 0, sizeof(*this)); frameIndex = UINT_MAX; }
8385
};
8486

@@ -708,10 +710,7 @@ void ImGui_ImplDX12_InvalidateDeviceObjects()
708710

709711
// Free SRV descriptor used by texture
710712
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
711-
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
712-
if (bd->InitInfo.SrvDescriptorFreeFn != NULL)
713-
#endif
714-
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle);
713+
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle);
715714
SafeRelease(font_tex->pTextureResource);
716715
io.Fonts->SetTexID(0); // We copied bd->hFontSrvGpuDescHandle to io.Fonts->TexID so let's clear that as well.
717716

@@ -731,8 +730,9 @@ bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
731730

732731
// Setup backend capabilities flags
733732
ImGui_ImplDX12_Data* bd = IM_NEW(ImGui_ImplDX12_Data)();
734-
735733
bd->InitInfo = *init_info; // Deep copy
734+
init_info = &bd->InitInfo;
735+
736736
bd->pd3dDevice = init_info->Device;
737737
bd->RTVFormat = init_info->RTVFormat;
738738
bd->numFramesInFlight = init_info->NumFramesInFlight;
@@ -742,23 +742,31 @@ bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
742742
io.BackendRendererName = "imgui_impl_dx12";
743743
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
744744

745-
// Allocate 1 SRV descriptor for the font texture
746-
if (init_info->SrvDescriptorAllocFn != NULL)
747-
{
748-
IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL);
749-
init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->FontTexture.hFontSrvCpuDescHandle, &bd->FontTexture.hFontSrvGpuDescHandle);
750-
}
751-
else
752-
{
753745
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
746+
if (init_info->SrvDescriptorAllocFn == NULL)
747+
{
748+
// Wrap legacy behavior of passing space for a single descriptor
754749
IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0);
755-
bd->FontTexture.hFontSrvCpuDescHandle = init_info->LegacySingleSrvCpuDescriptor;
756-
bd->FontTexture.hFontSrvGpuDescHandle = init_info->LegacySingleSrvGpuDescriptor;
757-
#else
758-
IM_ASSERT(init_info->SrvDescriptorAllocFn != NULL);
759-
IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL);
760-
#endif
750+
init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle)
751+
{
752+
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
753+
IM_ASSERT(bd->LegacySingleDescriptorUsed == false);
754+
*out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor;
755+
*out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor;
756+
bd->LegacySingleDescriptorUsed = true;
757+
};
758+
init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE)
759+
{
760+
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
761+
IM_ASSERT(bd->LegacySingleDescriptorUsed == true);
762+
bd->LegacySingleDescriptorUsed = false;
763+
};
761764
}
765+
#endif
766+
767+
// Allocate 1 SRV descriptor for the font texture
768+
IM_ASSERT(init_info->SrvDescriptorAllocFn != NULL && init_info->SrvDescriptorFreeFn != NULL);
769+
init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->FontTexture.hFontSrvCpuDescHandle, &bd->FontTexture.hFontSrvGpuDescHandle);
762770

763771
// Create buffers with a default size (they will later be grown as needed)
764772
bd->frameIndex = UINT_MAX;

0 commit comments

Comments
 (0)