From 2dd374d2a77672e1b9810e8c7deb175f9ff94283 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 5 Feb 2023 13:02:31 +1000 Subject: [PATCH] Vulkan/Context: Fix query pool leak --- src/common/vulkan/context.cpp | 76 +++++++++++++++-------------------- src/common/vulkan/context.h | 11 +---- 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/src/common/vulkan/context.cpp b/src/common/vulkan/context.cpp index 0e0fddf680..bcdb6669a5 100644 --- a/src/common/vulkan/context.cpp +++ b/src/common/vulkan/context.cpp @@ -351,7 +351,8 @@ bool Vulkan::Context::Create(std::string_view gpu_name, const WindowInfo* wi, // Attempt to create the device. if (!g_vulkan_context->CreateDevice(surface, enable_validation_layer, nullptr, 0, nullptr, 0, nullptr) || !g_vulkan_context->CreateAllocator() || !g_vulkan_context->CreateGlobalDescriptorPool() || - !g_vulkan_context->CreateCommandBuffers() || !g_vulkan_context->CreateTextureStreamBuffer() || + !g_vulkan_context->CreateQueryPool() || !g_vulkan_context->CreateCommandBuffers() || + !g_vulkan_context->CreateTextureStreamBuffer() || (enable_surface && (*out_swap_chain = SwapChain::Create(wi_copy, surface, vsync)) == nullptr)) { // Since we are destroying the instance, we're also responsible for destroying the surface. @@ -368,31 +369,6 @@ bool Vulkan::Context::Create(std::string_view gpu_name, const WindowInfo* wi, return true; } -bool Vulkan::Context::CreateFromExistingInstance( - VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface, bool take_ownership, bool enable_validation_layer, - bool enable_debug_utils, const char** required_device_extensions /* = nullptr */, - u32 num_required_device_extensions /* = 0 */, const char** required_device_layers /* = nullptr */, - u32 num_required_device_layers /* = 0 */, const VkPhysicalDeviceFeatures* required_features /* = nullptr */) -{ - g_vulkan_context.reset(new Context(instance, gpu, take_ownership)); - - // Enable debug utils if the "Host GPU" log category is enabled. - if (enable_debug_utils) - g_vulkan_context->EnableDebugUtils(); - - // Attempt to create the device. - if (!g_vulkan_context->CreateDevice(surface, enable_validation_layer, required_device_extensions, - num_required_device_extensions, required_device_layers, - num_required_device_layers, required_features) || - !g_vulkan_context->CreateGlobalDescriptorPool() || !g_vulkan_context->CreateCommandBuffers()) - { - g_vulkan_context.reset(); - return false; - } - - return true; -} - void Vulkan::Context::Destroy() { AssertMsg(g_vulkan_context, "Has context"); @@ -405,6 +381,7 @@ void Vulkan::Context::Destroy() g_vulkan_context->m_texture_upload_buffer.Destroy(false); g_vulkan_context->DestroyRenderPassCache(); + g_vulkan_context->DestroyQueryPool(); g_vulkan_context->DestroyGlobalDescriptorPool(); g_vulkan_context->DestroyCommandBuffers(); g_vulkan_context->DestroyAllocator(); @@ -806,37 +783,50 @@ bool Vulkan::Context::CreateGlobalDescriptorPool() static_cast(countof(pool_sizes)), pool_sizes}; - VkResult res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &m_global_descriptor_pool); + const VkResult res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &m_global_descriptor_pool); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); return false; } Vulkan::Util::SetObjectName(g_vulkan_context->GetDevice(), m_global_descriptor_pool, "Global Descriptor Pool"); + return true; +} + +void Vulkan::Context::DestroyGlobalDescriptorPool() +{ + if (m_global_descriptor_pool == VK_NULL_HANDLE) + return; - if (m_gpu_timing_supported) + vkDestroyDescriptorPool(m_device, m_global_descriptor_pool, nullptr); + m_global_descriptor_pool = VK_NULL_HANDLE; +} + +bool Vulkan::Context::CreateQueryPool() +{ + if (!m_gpu_timing_supported) + return true; + + const VkQueryPoolCreateInfo query_create_info = { + VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, nullptr, 0, VK_QUERY_TYPE_TIMESTAMP, NUM_COMMAND_BUFFERS * 2, 0}; + const VkResult res = vkCreateQueryPool(m_device, &query_create_info, nullptr, &m_timestamp_query_pool); + if (res != VK_SUCCESS) { - const VkQueryPoolCreateInfo query_create_info = { - VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, nullptr, 0, VK_QUERY_TYPE_TIMESTAMP, NUM_COMMAND_BUFFERS * 2, 0}; - res = vkCreateQueryPool(m_device, &query_create_info, nullptr, &m_timestamp_query_pool); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: "); - m_gpu_timing_supported = false; - return false; - } + LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: "); + m_gpu_timing_supported = false; + return false; } return true; } -void Vulkan::Context::DestroyGlobalDescriptorPool() +void Vulkan::Context::DestroyQueryPool() { - if (m_global_descriptor_pool != VK_NULL_HANDLE) - { - vkDestroyDescriptorPool(m_device, m_global_descriptor_pool, nullptr); - m_global_descriptor_pool = VK_NULL_HANDLE; - } + if (!m_gpu_timing_supported) + return; + + vkDestroyQueryPool(m_device, m_timestamp_query_pool, nullptr); + m_timestamp_query_pool = VK_NULL_HANDLE; } bool Vulkan::Context::CreateTextureStreamBuffer() diff --git a/src/common/vulkan/context.h b/src/common/vulkan/context.h index 0ca273188a..bc9ebb8722 100644 --- a/src/common/vulkan/context.h +++ b/src/common/vulkan/context.h @@ -55,15 +55,6 @@ class Context static bool Create(std::string_view gpu_name, const WindowInfo* wi, std::unique_ptr* out_swap_chain, bool threaded_presentation, bool enable_debug_utils, bool enable_validation_layer, bool vsync); - // Creates a new context from a pre-existing instance. - static bool CreateFromExistingInstance(VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface, - bool take_ownership, bool enable_validation_layer, bool enable_debug_utils, - const char** required_device_extensions = nullptr, - u32 num_required_device_extensions = 0, - const char** required_device_layers = nullptr, - u32 num_required_device_layers = 0, - const VkPhysicalDeviceFeatures* required_features = nullptr); - // Destroys context. static void Destroy(); @@ -214,6 +205,8 @@ class Context void DestroyCommandBuffers(); bool CreateGlobalDescriptorPool(); void DestroyGlobalDescriptorPool(); + bool CreateQueryPool(); + void DestroyQueryPool(); bool CreateTextureStreamBuffer(); void DestroyRenderPassCache();