summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp
diff options
context:
space:
mode:
authorRobin Kertels <robin.kertels@gmail.com>2022-10-07 23:36:16 +0200
committerRobin Kertels <robin.kertels@gmail.com>2022-10-23 02:54:35 +0200
commit3ffbf94b2a7460e250ae81e3eb6a40dcc1024e36 (patch)
treef33b7a015b70e19269d26c07388864fd3ecca004 /Source/Core/VideoBackends/Vulkan/VulkanContext.cpp
parent5e96733e353c8cbca0b9d467332d073daed1952d (diff)
VideoBackends:Vulkan: Set up VMA
Co-authored-by: iwubcode <iwubcode@users.noreply.github.com>
Diffstat (limited to 'Source/Core/VideoBackends/Vulkan/VulkanContext.cpp')
-rw-r--r--Source/Core/VideoBackends/Vulkan/VulkanContext.cpp48
1 files changed, 42 insertions, 6 deletions
diff --git a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp
index bc9cc1169b..6df7ed3414 100644
--- a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp
+++ b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp
@@ -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,