HostDisplay: Move imgui context creation to base class

This commit is contained in:
Connor McLaughlin
2021-01-30 14:39:56 +10:00
parent e132cac0e5
commit e697d9aa33
24 changed files with 155 additions and 230 deletions

View File

@ -33,7 +33,6 @@ add_library(frontend-common
vulkan_host_display.h
)
target_compile_definitions(frontend-common PRIVATE "WITH_IMGUI=1")
target_link_libraries(frontend-common PUBLIC core common glad vulkan-loader cubeb imgui simpleini tinyxml2 scmversion)
if(WIN32)

View File

@ -359,21 +359,11 @@ bool D3D11HostDisplay::InitializeRenderDevice(std::string_view shader_cache_dire
if (!CreateResources())
return false;
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext() && !CreateImGuiContext())
return false;
#endif
return true;
}
void D3D11HostDisplay::DestroyRenderDevice()
{
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
DestroyImGuiContext();
#endif
DestroyResources();
DestroyRenderSurface();
m_context.Reset();
@ -675,12 +665,10 @@ bool D3D11HostDisplay::CreateImGuiContext()
#ifdef WITH_IMGUI
ImGui::GetIO().DisplaySize.x = static_cast<float>(m_window_info.surface_width);
ImGui::GetIO().DisplaySize.y = static_cast<float>(m_window_info.surface_height);
if (!ImGui_ImplDX11_Init(m_device.Get(), m_context.Get()))
return false;
ImGui_ImplDX11_NewFrame();
#endif
return true;
}
@ -691,16 +679,21 @@ void D3D11HostDisplay::DestroyImGuiContext()
#endif
}
bool D3D11HostDisplay::UpdateImGuiFontTexture()
{
#ifdef WITH_IMGUI
ImGui_ImplDX11_CreateFontsTexture();
#endif
return true;
}
bool D3D11HostDisplay::Render()
{
if (ShouldSkipDisplayingFrame())
{
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
{
ImGui::Render();
ImGui_ImplDX11_NewFrame();
}
#endif
return false;
@ -724,11 +717,6 @@ bool D3D11HostDisplay::Render()
else
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
ImGui_ImplDX11_NewFrame();
#endif
return true;
}

View File

@ -83,8 +83,11 @@ protected:
virtual bool CreateResources() override;
virtual void DestroyResources() override;
virtual bool CreateImGuiContext();
virtual void DestroyImGuiContext();
#ifdef WITH_IMGUI
virtual bool CreateImGuiContext() override;
virtual void DestroyImGuiContext() override;
virtual bool UpdateImGuiFontTexture() override;
#endif
bool CreateSwapChain(const DXGI_MODE_DESC* fullscreen_mode);
bool CreateSwapChainRTV();

View File

@ -36,7 +36,6 @@
// DirectX data
static ID3D11Device* g_pd3dDevice = NULL;
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
static IDXGIFactory* g_pFactory = NULL;
static ID3D11Buffer* g_pVB = NULL;
static ID3D11Buffer* g_pIB = NULL;
static ID3D10Blob* g_pVertexShaderBlob = NULL;
@ -257,7 +256,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
}
static void ImGui_ImplDX11_CreateFontsTexture()
void ImGui_ImplDX11_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
@ -266,6 +265,11 @@ static void ImGui_ImplDX11_CreateFontsTexture()
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
// Upload texture to graphics system
if (g_pFontTextureView)
{
g_pFontTextureView->Release();
g_pFontTextureView = nullptr;
}
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
@ -301,6 +305,7 @@ static void ImGui_ImplDX11_CreateFontsTexture()
io.Fonts->TexID = (ImTextureID)g_pFontTextureView;
// Create texture sampler
if (!g_pFontSampler)
{
D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(desc));
@ -455,8 +460,6 @@ bool ImGui_ImplDX11_CreateDeviceObjects()
g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
}
ImGui_ImplDX11_CreateFontsTexture();
return true;
}
@ -486,36 +489,15 @@ bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_co
// Setup back-end capabilities flags
ImGuiIO& io = ImGui::GetIO();
io.BackendRendererName = "imgui_impl_dx11";
g_pd3dDevice = device;
g_pd3dDeviceContext = device_context;
// Get factory from device
IDXGIDevice* pDXGIDevice = NULL;
IDXGIAdapter* pDXGIAdapter = NULL;
IDXGIFactory* pFactory = NULL;
if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
{
g_pd3dDevice = device;
g_pd3dDeviceContext = device_context;
g_pFactory = pFactory;
}
if (pDXGIDevice) pDXGIDevice->Release();
if (pDXGIAdapter) pDXGIAdapter->Release();
return true;
return ImGui_ImplDX11_CreateDeviceObjects();
}
void ImGui_ImplDX11_Shutdown()
{
ImGui_ImplDX11_InvalidateDeviceObjects();
if (g_pFactory) { g_pFactory->Release(); g_pFactory = NULL; }
g_pd3dDevice = NULL;
g_pd3dDeviceContext = NULL;
}
void ImGui_ImplDX11_NewFrame()
{
if (!g_pFontSampler)
ImGui_ImplDX11_CreateDeviceObjects();
}

View File

@ -16,9 +16,9 @@ struct ID3D11DeviceContext;
IMGUI_IMPL_API bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context);
IMGUI_IMPL_API void ImGui_ImplDX11_Shutdown();
IMGUI_IMPL_API void ImGui_ImplDX11_NewFrame();
IMGUI_IMPL_API void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data);
// Use if you want to reset your rendering device without losing ImGui state.
IMGUI_IMPL_API void ImGui_ImplDX11_InvalidateDeviceObjects();
IMGUI_IMPL_API bool ImGui_ImplDX11_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplDX11_CreateFontsTexture();

View File

@ -128,7 +128,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
strcpy(g_GlslVersionString, glsl_version);
strcat(g_GlslVersionString, "\n");
return true;
return ImGui_ImplOpenGL3_CreateDeviceObjects();
}
void ImGui_ImplOpenGL3_Shutdown()
@ -538,8 +538,6 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
(GLvoid*)IM_OFFSETOF(ImDrawVert, col));
}
ImGui_ImplOpenGL3_CreateFontsTexture();
// Restore modified GL state
glBindTexture(GL_TEXTURE_2D, last_texture);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);

View File

@ -27,7 +27,6 @@
// Backend API
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL);
IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
// (Optional) Called by Init/NewFrame/Shutdown

View File

@ -819,10 +819,6 @@ void ImGui_ImplVulkan_Shutdown()
ImGui_ImplVulkan_DestroyDeviceObjects();
}
void ImGui_ImplVulkan_NewFrame()
{
}
void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)
{
IM_ASSERT(min_image_count >= 2);

View File

@ -44,7 +44,6 @@ struct ImGui_ImplVulkan_InitInfo
// Called by user code
IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass);
IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer);
IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer);
IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects();

View File

@ -361,11 +361,6 @@ bool OpenGLHostDisplay::InitializeRenderDevice(std::string_view shader_cache_dir
if (!CreateResources())
return false;
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext() && !CreateImGuiContext())
return false;
#endif
// Start with vsync on.
SetVSync(true);
@ -393,11 +388,6 @@ void OpenGLHostDisplay::DestroyRenderDevice()
if (!m_gl_context)
return;
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
DestroyImGuiContext();
#endif
DestroyResources();
m_gl_context->DoneCurrent();
@ -477,12 +467,10 @@ bool OpenGLHostDisplay::CreateImGuiContext()
#ifdef WITH_IMGUI
ImGui::GetIO().DisplaySize.x = static_cast<float>(m_window_info.surface_width);
ImGui::GetIO().DisplaySize.y = static_cast<float>(m_window_info.surface_height);
if (!ImGui_ImplOpenGL3_Init(GetGLSLVersionString()))
return false;
ImGui_ImplOpenGL3_NewFrame();
#endif
return true;
}
@ -493,6 +481,16 @@ void OpenGLHostDisplay::DestroyImGuiContext()
#endif
}
bool OpenGLHostDisplay::UpdateImGuiFontTexture()
{
#ifdef WITH_IMGUI
ImGui_ImplOpenGL3_DestroyFontsTexture();
return ImGui_ImplOpenGL3_CreateFontsTexture();
#else
return true;
#endif
}
bool OpenGLHostDisplay::CreateResources()
{
if (!m_use_gles2_draw_path)
@ -683,10 +681,7 @@ bool OpenGLHostDisplay::Render()
{
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
{
ImGui::Render();
ImGui_ImplOpenGL3_NewFrame();
}
#endif
return false;
@ -707,12 +702,6 @@ bool OpenGLHostDisplay::Render()
RenderSoftwareCursor();
m_gl_context->SwapBuffers();
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
ImGui_ImplOpenGL3_NewFrame();
#endif
return true;
}

View File

@ -72,8 +72,11 @@ protected:
virtual bool CreateResources() override;
virtual void DestroyResources() override;
virtual bool CreateImGuiContext();
virtual void DestroyImGuiContext();
#ifdef WITH_IMGUI
virtual bool CreateImGuiContext() override;
virtual void DestroyImGuiContext() override;
virtual bool UpdateImGuiFontTexture() override;
#endif
void RenderDisplay();
void RenderImGui();

View File

@ -343,11 +343,6 @@ bool VulkanHostDisplay::InitializeRenderDevice(std::string_view shader_cache_dir
if (!CreateResources())
return false;
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext() && !CreateImGuiContext())
return false;
#endif
return true;
}
@ -528,13 +523,51 @@ void VulkanHostDisplay::DestroyResources()
Vulkan::Util::SafeDestroySampler(m_linear_sampler);
}
void VulkanHostDisplay::DestroyImGuiContext()
bool VulkanHostDisplay::CreateImGuiContext()
{
#ifdef WITH_IMGUI
ImGui::GetIO().DisplaySize.x = static_cast<float>(m_window_info.surface_width);
ImGui::GetIO().DisplaySize.y = static_cast<float>(m_window_info.surface_height);
ImGui_ImplVulkan_InitInfo vii = {};
vii.Instance = g_vulkan_context->GetVulkanInstance();
vii.PhysicalDevice = g_vulkan_context->GetPhysicalDevice();
vii.Device = g_vulkan_context->GetDevice();
vii.QueueFamily = g_vulkan_context->GetGraphicsQueueFamilyIndex();
vii.Queue = g_vulkan_context->GetGraphicsQueue();
vii.PipelineCache = g_vulkan_shader_cache->GetPipelineCache();
vii.MinImageCount = m_swap_chain->GetImageCount();
vii.ImageCount = m_swap_chain->GetImageCount();
vii.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
if (!ImGui_ImplVulkan_Init(&vii, m_swap_chain->GetClearRenderPass()))
return false;
#endif
return true;
}
void VulkanHostDisplay::DestroyImGuiContext()
{
g_vulkan_context->WaitForGPUIdle();
#ifdef WITH_IMGUI
ImGui_ImplVulkan_Shutdown();
#endif
}
bool VulkanHostDisplay::UpdateImGuiFontTexture()
{
#ifdef WITH_IMGUI
// Just in case we were drawing something.
g_vulkan_context->ExecuteCommandBuffer(true);
ImGui_ImplVulkan_DestroyFontUploadObjects();
return ImGui_ImplVulkan_CreateFontsTexture(g_vulkan_context->GetCurrentCommandBuffer());
#else
return true;
#endif
}
void VulkanHostDisplay::DestroyRenderDevice()
{
if (!g_vulkan_context)
@ -542,11 +575,6 @@ void VulkanHostDisplay::DestroyRenderDevice()
g_vulkan_context->WaitForGPUIdle();
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
DestroyImGuiContext();
#endif
DestroyResources();
Vulkan::ShaderCache::Destroy();
@ -564,45 +592,13 @@ bool VulkanHostDisplay::DoneRenderContextCurrent()
return true;
}
bool VulkanHostDisplay::CreateImGuiContext()
{
#ifdef WITH_IMGUI
ImGui::GetIO().DisplaySize.x = static_cast<float>(m_window_info.surface_width);
ImGui::GetIO().DisplaySize.y = static_cast<float>(m_window_info.surface_height);
ImGui_ImplVulkan_InitInfo vii = {};
vii.Instance = g_vulkan_context->GetVulkanInstance();
vii.PhysicalDevice = g_vulkan_context->GetPhysicalDevice();
vii.Device = g_vulkan_context->GetDevice();
vii.QueueFamily = g_vulkan_context->GetGraphicsQueueFamilyIndex();
vii.Queue = g_vulkan_context->GetGraphicsQueue();
vii.PipelineCache = g_vulkan_shader_cache->GetPipelineCache();
vii.MinImageCount = m_swap_chain->GetImageCount();
vii.ImageCount = m_swap_chain->GetImageCount();
vii.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
if (!ImGui_ImplVulkan_Init(&vii, m_swap_chain->GetClearRenderPass()) ||
!ImGui_ImplVulkan_CreateFontsTexture(g_vulkan_context->GetCurrentCommandBuffer()))
{
return false;
}
ImGui_ImplVulkan_NewFrame();
#endif
return true;
}
bool VulkanHostDisplay::Render()
{
if (ShouldSkipDisplayingFrame())
{
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
{
ImGui::Render();
ImGui_ImplVulkan_NewFrame();
}
#endif
return false;
@ -656,11 +652,6 @@ bool VulkanHostDisplay::Render()
m_swap_chain->GetCurrentImageIndex(), !m_swap_chain->IsVSyncEnabled());
g_vulkan_context->MoveToNextCommandBuffer();
#ifdef WITH_IMGUI
if (ImGui::GetCurrentContext())
ImGui_ImplVulkan_NewFrame();
#endif
return true;
}

View File

@ -98,8 +98,11 @@ protected:
virtual bool CreateResources() override;
virtual void DestroyResources() override;
virtual bool CreateImGuiContext();
virtual void DestroyImGuiContext();
#ifdef WITH_IMGUI
virtual bool CreateImGuiContext() override;
virtual void DestroyImGuiContext() override;
virtual bool UpdateImGuiFontTexture() override;
#endif
void BeginSwapChainRenderPass(VkFramebuffer framebuffer);
void RenderDisplay();