Backends: DX12: tidying up, added a ImGui_ImplDX12_Texture helper struct.

This commit is contained in:
ocornut 2024-11-15 19:14:04 +01:00
parent 40b2286d16
commit 08400f5be7

View File

@ -55,8 +55,16 @@
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
#endif #endif
// DirectX data // DirectX12 data
struct ImGui_ImplDX12_RenderBuffers; struct ImGui_ImplDX12_RenderBuffers;
struct ImGui_ImplDX12_Texture
{
ID3D12Resource* pTextureResource;
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle;
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle;
};
struct ImGui_ImplDX12_Data struct ImGui_ImplDX12_Data
{ {
ImGui_ImplDX12_InitInfo InitInfo; ImGui_ImplDX12_InitInfo InitInfo;
@ -64,9 +72,7 @@ struct ImGui_ImplDX12_Data
ID3D12RootSignature* pRootSignature; ID3D12RootSignature* pRootSignature;
ID3D12PipelineState* pPipelineState; ID3D12PipelineState* pPipelineState;
DXGI_FORMAT RTVFormat; DXGI_FORMAT RTVFormat;
ID3D12Resource* pFontTextureResource; ImGui_ImplDX12_Texture FontTexture;
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle;
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle;
ID3D12DescriptorHeap* pd3dSrvDescHeap; ID3D12DescriptorHeap* pd3dSrvDescHeap;
UINT numFramesInFlight; UINT numFramesInFlight;
@ -316,6 +322,7 @@ static void ImGui_ImplDX12_CreateFontsTexture()
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
// Upload texture to graphics system // Upload texture to graphics system
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
{ {
D3D12_HEAP_PROPERTIES props; D3D12_HEAP_PROPERTIES props;
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
@ -446,13 +453,13 @@ static void ImGui_ImplDX12_CreateFontsTexture()
srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, bd->hFontSrvCpuDescHandle); bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, font_tex->hFontSrvCpuDescHandle);
SafeRelease(bd->pFontTextureResource); SafeRelease(font_tex->pTextureResource);
bd->pFontTextureResource = pTexture; font_tex->pTextureResource = pTexture;
} }
// Store our identifier // Store our identifier
io.Fonts->SetTexID((ImTextureID)bd->hFontSrvGpuDescHandle.ptr); io.Fonts->SetTexID((ImTextureID)font_tex->hFontSrvGpuDescHandle.ptr);
} }
bool ImGui_ImplDX12_CreateDeviceObjects() bool ImGui_ImplDX12_CreateDeviceObjects()
@ -700,11 +707,12 @@ void ImGui_ImplDX12_InvalidateDeviceObjects()
SafeRelease(bd->pPipelineState); SafeRelease(bd->pPipelineState);
// Free SRV descriptor used by texture // Free SRV descriptor used by texture
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (bd->InitInfo.SrvDescriptorFreeFn != NULL) if (bd->InitInfo.SrvDescriptorFreeFn != NULL)
#endif #endif
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, bd->hFontSrvCpuDescHandle, bd->hFontSrvGpuDescHandle); bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle);
SafeRelease(bd->pFontTextureResource); SafeRelease(font_tex->pTextureResource);
io.Fonts->SetTexID(0); // We copied bd->hFontSrvGpuDescHandle to io.Fonts->TexID so let's clear that as well. io.Fonts->SetTexID(0); // We copied bd->hFontSrvGpuDescHandle to io.Fonts->TexID so let's clear that as well.
for (UINT i = 0; i < bd->numFramesInFlight; i++) for (UINT i = 0; i < bd->numFramesInFlight; i++)
@ -738,14 +746,14 @@ bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
if (init_info->SrvDescriptorAllocFn != NULL) if (init_info->SrvDescriptorAllocFn != NULL)
{ {
IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL); IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL);
init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->hFontSrvCpuDescHandle, &bd->hFontSrvGpuDescHandle); init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->FontTexture.hFontSrvCpuDescHandle, &bd->FontTexture.hFontSrvGpuDescHandle);
} }
else else
{ {
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0); IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0);
bd->hFontSrvCpuDescHandle = init_info->LegacySingleSrvCpuDescriptor; bd->FontTexture.hFontSrvCpuDescHandle = init_info->LegacySingleSrvCpuDescriptor;
bd->hFontSrvGpuDescHandle = init_info->LegacySingleSrvGpuDescriptor; bd->FontTexture.hFontSrvGpuDescHandle = init_info->LegacySingleSrvGpuDescriptor;
#else #else
IM_ASSERT(init_info->SrvDescriptorAllocFn != NULL); IM_ASSERT(init_info->SrvDescriptorAllocFn != NULL);
IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL); IM_ASSERT(init_info->SrvDescriptorFreeFn != NULL);