Added support for VK_KHR_maintenance4 extension

Added VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT..
See #397.
This commit is contained in:
Adam Sawicki 2024-01-20 22:48:20 +01:00
parent 14dfcd8fe3
commit a0a04a22e9

View File

@ -212,6 +212,16 @@ extern "C" {
#endif #endif
#endif #endif
// Defined to 1 when VK_KHR_maintenance4 device extension is defined in Vulkan headers.
#if !defined(VMA_KHR_MAINTENANCE4)
#if VK_KHR_maintenance4
#define VMA_KHR_MAINTENANCE4 1
#else
#define VMA_KHR_MAINTENANCE4 0
#endif
#endif
// Defined to 1 when VK_KHR_external_memory device extension is defined in Vulkan headers. // Defined to 1 when VK_KHR_external_memory device extension is defined in Vulkan headers.
#if !defined(VMA_EXTERNAL_MEMORY) #if !defined(VMA_EXTERNAL_MEMORY)
#if VK_KHR_external_memory #if VK_KHR_external_memory
@ -425,6 +435,13 @@ typedef enum VmaAllocatorCreateFlagBits
For more details, see the documentation of the VK_EXT_memory_priority extension. For more details, see the documentation of the VK_EXT_memory_priority extension.
*/ */
VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT = 0x00000040, VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT = 0x00000040,
/**
Enables usage of VK_KHR_maintenance4 extension in the library.
You may set this flag only if you found available and enabled this device extension,
while creating Vulkan device passed as VmaAllocatorCreateInfo::device.
*/
VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT = 0x00000080,
VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaAllocatorCreateFlagBits; } VmaAllocatorCreateFlagBits;
@ -993,11 +1010,11 @@ typedef struct VmaVulkanFunctions
#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000 #if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000
PFN_vkGetPhysicalDeviceMemoryProperties2KHR VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties2KHR; PFN_vkGetPhysicalDeviceMemoryProperties2KHR VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties2KHR;
#endif #endif
#if VMA_VULKAN_VERSION >= 1003000 #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
/// Fetch from "vkGetDeviceBufferMemoryRequirements" on Vulkan >= 1.3, but you can also fetch it from "vkGetDeviceBufferMemoryRequirementsKHR" if you enabled extension VK_KHR_maintenance4. /// Fetch from "vkGetDeviceBufferMemoryRequirements" on Vulkan >= 1.3, but you can also fetch it from "vkGetDeviceBufferMemoryRequirementsKHR" if you enabled extension VK_KHR_maintenance4.
PFN_vkGetDeviceBufferMemoryRequirements VMA_NULLABLE vkGetDeviceBufferMemoryRequirements; PFN_vkGetDeviceBufferMemoryRequirementsKHR VMA_NULLABLE vkGetDeviceBufferMemoryRequirements;
/// Fetch from "vkGetDeviceImageMemoryRequirements" on Vulkan >= 1.3, but you can also fetch it from "vkGetDeviceImageMemoryRequirementsKHR" if you enabled extension VK_KHR_maintenance4. /// Fetch from "vkGetDeviceImageMemoryRequirements" on Vulkan >= 1.3, but you can also fetch it from "vkGetDeviceImageMemoryRequirementsKHR" if you enabled extension VK_KHR_maintenance4.
PFN_vkGetDeviceImageMemoryRequirements VMA_NULLABLE vkGetDeviceImageMemoryRequirements; PFN_vkGetDeviceImageMemoryRequirementsKHR VMA_NULLABLE vkGetDeviceImageMemoryRequirements;
#endif #endif
} VmaVulkanFunctions; } VmaVulkanFunctions;
@ -11452,6 +11469,7 @@ public:
bool m_UseAmdDeviceCoherentMemory; bool m_UseAmdDeviceCoherentMemory;
bool m_UseKhrBufferDeviceAddress; bool m_UseKhrBufferDeviceAddress;
bool m_UseExtMemoryPriority; bool m_UseExtMemoryPriority;
bool m_UseKhrMaintenance4;
VkDevice m_hDevice; VkDevice m_hDevice;
VkInstance m_hInstance; VkInstance m_hInstance;
bool m_AllocationCallbacksSpecified; bool m_AllocationCallbacksSpecified;
@ -14083,6 +14101,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
m_UseAmdDeviceCoherentMemory((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT) != 0), m_UseAmdDeviceCoherentMemory((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT) != 0),
m_UseKhrBufferDeviceAddress((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT) != 0), m_UseKhrBufferDeviceAddress((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT) != 0),
m_UseExtMemoryPriority((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT) != 0), m_UseExtMemoryPriority((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT) != 0),
m_UseKhrMaintenance4((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT) != 0),
m_hDevice(pCreateInfo->device), m_hDevice(pCreateInfo->device),
m_hInstance(pCreateInfo->instance), m_hInstance(pCreateInfo->instance),
m_AllocationCallbacksSpecified(pCreateInfo->pAllocationCallbacks != VMA_NULL), m_AllocationCallbacksSpecified(pCreateInfo->pAllocationCallbacks != VMA_NULL),
@ -14162,6 +14181,12 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT is set but required extension is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro."); VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT is set but required extension is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro.");
} }
#endif #endif
#if !(VMA_KHR_MAINTENANCE4)
if(m_UseKhrMaintenance4)
{
VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT is set but required extension is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro.");
}
#endif
memset(&m_DeviceMemoryCallbacks, 0 ,sizeof(m_DeviceMemoryCallbacks)); memset(&m_DeviceMemoryCallbacks, 0 ,sizeof(m_DeviceMemoryCallbacks));
memset(&m_PhysicalDeviceProperties, 0, sizeof(m_PhysicalDeviceProperties)); memset(&m_PhysicalDeviceProperties, 0, sizeof(m_PhysicalDeviceProperties));
@ -14382,7 +14407,7 @@ void VmaAllocator_T::ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVul
VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties2KHR); VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties2KHR);
#endif #endif
#if VMA_VULKAN_VERSION >= 1003000 #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
VMA_COPY_IF_NOT_NULL(vkGetDeviceBufferMemoryRequirements); VMA_COPY_IF_NOT_NULL(vkGetDeviceBufferMemoryRequirements);
VMA_COPY_IF_NOT_NULL(vkGetDeviceImageMemoryRequirements); VMA_COPY_IF_NOT_NULL(vkGetDeviceImageMemoryRequirements);
#endif #endif
@ -14481,6 +14506,13 @@ void VmaAllocator_T::ImportVulkanFunctions_Dynamic()
VMA_FETCH_DEVICE_FUNC(vkGetDeviceImageMemoryRequirements, PFN_vkGetDeviceImageMemoryRequirements, "vkGetDeviceImageMemoryRequirements"); VMA_FETCH_DEVICE_FUNC(vkGetDeviceImageMemoryRequirements, PFN_vkGetDeviceImageMemoryRequirements, "vkGetDeviceImageMemoryRequirements");
} }
#endif #endif
#if VMA_KHR_MAINTENANCE4
if(m_UseKhrMaintenance4)
{
VMA_FETCH_DEVICE_FUNC(vkGetDeviceBufferMemoryRequirements, PFN_vkGetDeviceBufferMemoryRequirementsKHR, "vkGetDeviceBufferMemoryRequirementsKHR");
VMA_FETCH_DEVICE_FUNC(vkGetDeviceImageMemoryRequirements, PFN_vkGetDeviceImageMemoryRequirementsKHR, "vkGetDeviceImageMemoryRequirementsKHR");
}
#endif
#undef VMA_FETCH_DEVICE_FUNC #undef VMA_FETCH_DEVICE_FUNC
#undef VMA_FETCH_INSTANCE_FUNC #undef VMA_FETCH_INSTANCE_FUNC
@ -14531,13 +14563,12 @@ void VmaAllocator_T::ValidateVulkanFunctions()
} }
#endif #endif
#if VMA_VULKAN_VERSION >= 1003000 // Not validating these due to suspected driver bugs with these function
if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 3, 0)) // pointers being null despite correct extension or Vulkan version is enabled.
{ // See issue #397. Their usage in VMA is optional anyway.
VMA_ASSERT(m_VulkanFunctions.vkGetDeviceBufferMemoryRequirements != VMA_NULL); //
VMA_ASSERT(m_VulkanFunctions.vkGetDeviceImageMemoryRequirements != VMA_NULL); // VMA_ASSERT(m_VulkanFunctions.vkGetDeviceBufferMemoryRequirements != VMA_NULL);
} // VMA_ASSERT(m_VulkanFunctions.vkGetDeviceImageMemoryRequirements != VMA_NULL);
#endif
} }
VkDeviceSize VmaAllocator_T::CalcPreferredBlockSize(uint32_t memTypeIndex) VkDeviceSize VmaAllocator_T::CalcPreferredBlockSize(uint32_t memTypeIndex)
@ -16471,7 +16502,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo(
const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions(); const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions();
VkResult res; VkResult res;
#if VMA_VULKAN_VERSION >= 1003000 #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
if(funcs->vkGetDeviceBufferMemoryRequirements) if(funcs->vkGetDeviceBufferMemoryRequirements)
{ {
// Can query straight from VkBufferCreateInfo :) // Can query straight from VkBufferCreateInfo :)
@ -16485,7 +16516,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo(
memReq.memoryRequirements.memoryTypeBits, pAllocationCreateInfo, pBufferCreateInfo->usage, pMemoryTypeIndex); memReq.memoryRequirements.memoryTypeBits, pAllocationCreateInfo, pBufferCreateInfo->usage, pMemoryTypeIndex);
} }
else else
#endif // #if VMA_VULKAN_VERSION >= 1003000 #endif // VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
{ {
// Must create a dummy buffer to query :( // Must create a dummy buffer to query :(
VkBuffer hBuffer = VK_NULL_HANDLE; VkBuffer hBuffer = VK_NULL_HANDLE;
@ -16521,7 +16552,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo(
const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions(); const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions();
VkResult res; VkResult res;
#if VMA_VULKAN_VERSION >= 1003000 #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
if(funcs->vkGetDeviceImageMemoryRequirements) if(funcs->vkGetDeviceImageMemoryRequirements)
{ {
// Can query straight from VkImageCreateInfo :) // Can query straight from VkImageCreateInfo :)
@ -16537,7 +16568,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo(
memReq.memoryRequirements.memoryTypeBits, pAllocationCreateInfo, pImageCreateInfo->usage, pMemoryTypeIndex); memReq.memoryRequirements.memoryTypeBits, pAllocationCreateInfo, pImageCreateInfo->usage, pMemoryTypeIndex);
} }
else else
#endif // #if VMA_VULKAN_VERSION >= 1003000 #endif // VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000
{ {
// Must create a dummy image to query :( // Must create a dummy image to query :(
VkImage hImage = VK_NULL_HANDLE; VkImage hImage = VK_NULL_HANDLE;
@ -17815,7 +17846,23 @@ You may need to configure importing Vulkan functions. There are 3 ways to do thi
- Define `VMA_STATIC_VULKAN_FUNCTIONS` and `VMA_DYNAMIC_VULKAN_FUNCTIONS` to 0. - Define `VMA_STATIC_VULKAN_FUNCTIONS` and `VMA_DYNAMIC_VULKAN_FUNCTIONS` to 0.
- Pass these pointers via structure #VmaVulkanFunctions. - Pass these pointers via structure #VmaVulkanFunctions.
Example for case 2: \subsection quick_start_initialization_enabling_extensions Enabling extensions
VMA can automatically use following Vulkan extensions.
If you found them availeble on the selected physical device and you enabled them
while creating `VkInstance` / `VkDevice` object, inform VMA about their availability
by setting appropriate flags in VmaAllocatorCreateInfo::flags.
Vulkan extension | VMA flag
------------------------------|-----------------------------------------------------
VK_KHR_dedicated_allocation | #VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT
VK_KHR_bind_memory2 | #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT
VK_KHR_maintenance4 | #VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT
VK_EXT_memory_budget | #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT
VK_EXT_memory_priority | #VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT
VK_AMD_device_coherent_memory | #VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT
Example with fetching pointers to Vulkan functions dynamically:
\code \code
#define VMA_STATIC_VULKAN_FUNCTIONS 0 #define VMA_STATIC_VULKAN_FUNCTIONS 0
@ -17829,6 +17876,7 @@ vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr; vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
VmaAllocatorCreateInfo allocatorCreateInfo = {}; VmaAllocatorCreateInfo allocatorCreateInfo = {};
allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2; allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
allocatorCreateInfo.physicalDevice = physicalDevice; allocatorCreateInfo.physicalDevice = physicalDevice;
allocatorCreateInfo.device = device; allocatorCreateInfo.device = device;