From 8f9ce03a8d61ea7e384e4a6ca403dc6ff522699a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 21 Nov 2021 23:08:47 +0100 Subject: [PATCH] Start integrating VMA --- Common/Common.vcxproj | 2 ++ Common/Common.vcxproj.filters | 9 +++++++++ Common/GPU/Vulkan/VulkanContext.cpp | 23 +++++++++++++++++++---- Common/GPU/Vulkan/VulkanContext.h | 4 ++++ Common/GPU/Vulkan/VulkanLoader.cpp | 13 +++++++++---- Common/GPU/Vulkan/VulkanLoader.h | 6 +++++- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 4bd66ab227..4e988922c5 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -376,6 +376,7 @@ + @@ -794,6 +795,7 @@ + diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 6de8798435..4cee6365b7 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -406,6 +406,9 @@ Thread + + ext\vma + @@ -777,6 +780,9 @@ Thread + + ext\vma + @@ -878,6 +884,9 @@ {9da96cce-c74a-4669-b755-c95defbe1e93} + + {7b17065c-729c-47c3-a02d-66dc383529dd} + diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index d3da842275..d416bb371f 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -13,14 +13,17 @@ #include "Common/StringUtils.h" #include "Core/Config.h" -// Change this to 1, 2, and 3 to fake failures in a few places, so that -// we can test our fallback-to-GL code. -#define SIMULATE_VULKAN_FAILURE 0 - #ifdef USE_CRT_DBG #undef new #endif +#include "ext/vma/vk_mem_alloc.h" + + +// Change this to 1, 2, and 3 to fake failures in a few places, so that +// we can test our fallback-to-GL code. +#define SIMULATE_VULKAN_FAILURE 0 + #include "ext/glslang/SPIRV/GlslangToSpv.h" #ifdef USE_CRT_DBG @@ -656,6 +659,15 @@ VkResult VulkanContext::CreateDevice() { } INFO_LOG(G3D, "Device created.\n"); VulkanSetAvailable(true); + + VmaAllocatorCreateInfo allocatorInfo = {}; + allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_0; + allocatorInfo.physicalDevice = physical_devices_[physical_device_]; + allocatorInfo.device = device_; + allocatorInfo.instance = instance_; + + VmaAllocator allocator; + vmaCreateAllocator(&allocatorInfo, &allocator); return res; } @@ -1103,6 +1115,9 @@ void VulkanContext::DestroyDevice() { INFO_LOG(G3D, "VulkanContext::DestroyDevice (performing deletes)"); PerformPendingDeletes(); + vmaDestroyAllocator(allocator_); + allocator_ = VK_NULL_HANDLE; + vkDestroyDevice(device_, nullptr); device_ = nullptr; } diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index a277561800..e0b2c7ee91 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -26,6 +26,8 @@ enum { VULKAN_VENDOR_IMGTEC = 0x00001010, // PowerVR }; +VK_DEFINE_HANDLE(VmaAllocator); + std::string VulkanVendorString(uint32_t vendorId); // Not all will be usable on all platforms, of course... @@ -359,6 +361,8 @@ private: VkSurfaceCapabilitiesKHR surfCapabilities_{}; std::vector cmdQueue_; + + VmaAllocator allocator_ = VK_NULL_HANDLE; }; // Detailed control. diff --git a/Common/GPU/Vulkan/VulkanLoader.cpp b/Common/GPU/Vulkan/VulkanLoader.cpp index 351668f0f8..26391ab62c 100644 --- a/Common/GPU/Vulkan/VulkanLoader.cpp +++ b/Common/GPU/Vulkan/VulkanLoader.cpp @@ -38,6 +38,7 @@ PFN_vkGetPhysicalDeviceImageFormatProperties vkGetPhysicalDeviceImageFormatPrope PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties; PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties; PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties; +PFN_vkGetPhysicalDeviceMemoryProperties2 vkGetPhysicalDeviceMemoryProperties2; PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; PFN_vkCreateDevice vkCreateDevice; @@ -58,11 +59,13 @@ PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges; PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges; PFN_vkGetDeviceMemoryCommitment vkGetDeviceMemoryCommitment; PFN_vkBindBufferMemory vkBindBufferMemory; +PFN_vkBindBufferMemory2 vkBindBufferMemory2; PFN_vkBindImageMemory vkBindImageMemory; +PFN_vkBindImageMemory2 vkBindImageMemory2; PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements; +PFN_vkGetBufferMemoryRequirements2 vkGetBufferMemoryRequirements2; PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; -PFN_vkGetImageSparseMemoryRequirements vkGetImageSparseMemoryRequirements; -PFN_vkQueueBindSparse vkQueueBindSparse; +PFN_vkGetImageMemoryRequirements2 vkGetImageMemoryRequirements2; PFN_vkCreateFence vkCreateFence; PFN_vkDestroyFence vkDestroyFence; PFN_vkGetFenceStatus vkGetFenceStatus; @@ -581,11 +584,13 @@ void VulkanLoadDeviceFunctions(VkDevice device, const VulkanExtensions &enabledE LOAD_DEVICE_FUNC(device, vkInvalidateMappedMemoryRanges); LOAD_DEVICE_FUNC(device, vkGetDeviceMemoryCommitment); LOAD_DEVICE_FUNC(device, vkBindBufferMemory); + LOAD_DEVICE_FUNC(device, vkBindBufferMemory2); LOAD_DEVICE_FUNC(device, vkBindImageMemory); + LOAD_DEVICE_FUNC(device, vkBindImageMemory2); LOAD_DEVICE_FUNC(device, vkGetBufferMemoryRequirements); + LOAD_DEVICE_FUNC(device, vkGetBufferMemoryRequirements2); LOAD_DEVICE_FUNC(device, vkGetImageMemoryRequirements); - LOAD_DEVICE_FUNC(device, vkGetImageSparseMemoryRequirements); - LOAD_DEVICE_FUNC(device, vkQueueBindSparse); + LOAD_DEVICE_FUNC(device, vkGetImageMemoryRequirements2); LOAD_DEVICE_FUNC(device, vkCreateFence); LOAD_DEVICE_FUNC(device, vkDestroyFence); LOAD_DEVICE_FUNC(device, vkResetFences); diff --git a/Common/GPU/Vulkan/VulkanLoader.h b/Common/GPU/Vulkan/VulkanLoader.h index c91c896a82..5b0cb93b0a 100644 --- a/Common/GPU/Vulkan/VulkanLoader.h +++ b/Common/GPU/Vulkan/VulkanLoader.h @@ -44,6 +44,7 @@ extern PFN_vkGetPhysicalDeviceImageFormatProperties vkGetPhysicalDeviceImageForm extern PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties; extern PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties; extern PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties; +extern PFN_vkGetPhysicalDeviceMemoryProperties2 vkGetPhysicalDeviceMemoryProperties2; extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; extern PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; extern PFN_vkCreateDevice vkCreateDevice; @@ -64,10 +65,13 @@ extern PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges; extern PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges; extern PFN_vkGetDeviceMemoryCommitment vkGetDeviceMemoryCommitment; extern PFN_vkBindBufferMemory vkBindBufferMemory; +extern PFN_vkBindBufferMemory2 vkBindBufferMemory2; extern PFN_vkBindImageMemory vkBindImageMemory; +extern PFN_vkBindImageMemory2 vkBindImageMemory2; extern PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements; +extern PFN_vkGetBufferMemoryRequirements2 vkGetBufferMemoryRequirements2; extern PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; -extern PFN_vkGetImageSparseMemoryRequirements vkGetImageSparseMemoryRequirements; +extern PFN_vkGetImageMemoryRequirements2 vkGetImageMemoryRequirements2; extern PFN_vkQueueBindSparse vkQueueBindSparse; extern PFN_vkCreateFence vkCreateFence; extern PFN_vkDestroyFence vkDestroyFence;