VideoBackends:Vulkan: Set up VMA

Co-authored-by: iwubcode <iwubcode@users.noreply.github.com>
This commit is contained in:
Robin Kertels
2022-10-07 23:36:16 +02:00
parent 5e96733e35
commit 3ffbf94b2a
7 changed files with 100 additions and 14 deletions

View File

@ -40,6 +40,8 @@ VulkanContext::VulkanContext(VkInstance instance, VkPhysicalDevice physical_devi
VulkanContext::~VulkanContext()
{
if (m_allocator != VK_NULL_HANDLE)
vmaDestroyAllocator(m_allocator);
if (m_device != VK_NULL_HANDLE)
vkDestroyDevice(m_device, nullptr);
@ -86,7 +88,8 @@ bool VulkanContext::CheckValidationLayerAvailablility()
}
VkInstance VulkanContext::CreateVulkanInstance(WindowSystemType wstype, bool enable_debug_report,
bool enable_validation_layer)
bool enable_validation_layer,
u32* out_vk_api_version)
{
std::vector<const char*> enabled_extensions;
if (!SelectInstanceExtensions(&enabled_extensions, wstype, enable_debug_report))
@ -114,6 +117,8 @@ VkInstance VulkanContext::CreateVulkanInstance(WindowSystemType wstype, bool ena
}
}
*out_vk_api_version = app_info.apiVersion;
VkInstanceCreateInfo instance_create_info = {};
instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instance_create_info.pNext = nullptr;
@ -429,10 +434,9 @@ void VulkanContext::PopulateBackendInfoMultisampleModes(
config->backend_info.AAModes.emplace_back(64);
}
std::unique_ptr<VulkanContext> VulkanContext::Create(VkInstance instance, VkPhysicalDevice gpu,
VkSurfaceKHR surface,
bool enable_debug_reports,
bool enable_validation_layer)
std::unique_ptr<VulkanContext>
VulkanContext::Create(VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface,
bool enable_debug_reports, bool enable_validation_layer, u32 vk_api_version)
{
std::unique_ptr<VulkanContext> context = std::make_unique<VulkanContext>(instance, gpu);
@ -445,7 +449,8 @@ std::unique_ptr<VulkanContext> VulkanContext::Create(VkInstance instance, VkPhys
context->EnableDebugReports();
// Attempt to create the device.
if (!context->CreateDevice(surface, enable_validation_layer))
if (!context->CreateDevice(surface, enable_validation_layer) ||
!context->CreateAllocator(vk_api_version))
{
// Since we are destroying the instance, we're also responsible for destroying the surface.
if (surface != VK_NULL_HANDLE)
@ -508,6 +513,9 @@ bool VulkanContext::SelectDeviceExtensions(bool enable_surface)
INFO_LOG_FMT(VIDEO, "Using VK_EXT_full_screen_exclusive for exclusive fullscreen.");
#endif
AddExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);
AddExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, false);
return true;
}
@ -695,6 +703,34 @@ bool VulkanContext::CreateDevice(VkSurfaceKHR surface, bool enable_validation_la
return true;
}
bool VulkanContext::CreateAllocator(u32 vk_api_version)
{
VmaAllocatorCreateInfo allocator_info = {};
allocator_info.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
allocator_info.physicalDevice = m_physical_device;
allocator_info.device = m_device;
allocator_info.preferredLargeHeapBlockSize = 64 << 20;
allocator_info.pAllocationCallbacks = nullptr;
allocator_info.pDeviceMemoryCallbacks = nullptr;
allocator_info.pHeapSizeLimit = nullptr;
allocator_info.pVulkanFunctions = nullptr;
allocator_info.instance = m_instance;
allocator_info.vulkanApiVersion = vk_api_version;
allocator_info.pTypeExternalMemoryHandleTypes = nullptr;
if (SupportsDeviceExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME))
allocator_info.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
VkResult res = vmaCreateAllocator(&allocator_info, &m_allocator);
if (res != VK_SUCCESS)
{
LOG_VULKAN_ERROR(res, "vmaCreateAllocator failed: ");
return false;
}
return true;
}
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT objectType,
uint64_t object, size_t location,