From 4f91939dea2f09377b78c9e5bf2fb4327e13d32a Mon Sep 17 00:00:00 2001 From: Adam Sawicki Date: Tue, 3 Apr 2018 13:45:39 +0200 Subject: [PATCH] Added #if VMA_DEDICATED_ALLOCATION around every usage of VK_KHR_get_memory_requirements2 or VK_KHR_dedicated_allocation extension - for compatibility with Android. #23 Thanks @achienbsi ! --- src/VmaUsage.h | 2 ++ src/vk_mem_alloc.h | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/VmaUsage.h b/src/VmaUsage.h index 70c8849..7be6b2c 100644 --- a/src/VmaUsage.h +++ b/src/VmaUsage.h @@ -20,6 +20,8 @@ include all public interface declarations. Example: //#define VMA_HEAVY_ASSERT(expr) +//#define VMA_DEDICATED_ALLOCATION 0 + #pragma warning(push, 4) #pragma warning(disable: 4127) // conditional expression is constant #pragma warning(disable: 4100) // unreferenced formal parameter diff --git a/src/vk_mem_alloc.h b/src/vk_mem_alloc.h index 4a37227..fb29a17 100644 --- a/src/vk_mem_alloc.h +++ b/src/vk_mem_alloc.h @@ -1077,6 +1077,14 @@ Features deliberately excluded from the scope of this library: #include +#if !defined(VMA_DEDICATED_ALLOCATION) + #if VK_KHR_get_memory_requirements2 && VK_KHR_dedicated_allocation + #define VMA_DEDICATED_ALLOCATION 1 + #else + #define VMA_DEDICATED_ALLOCATION 0 + #endif +#endif + /** \struct VmaAllocator \brief Represents main object of this library initialized. @@ -1168,8 +1176,10 @@ typedef struct VmaVulkanFunctions { PFN_vkDestroyBuffer vkDestroyBuffer; PFN_vkCreateImage vkCreateImage; PFN_vkDestroyImage vkDestroyImage; +#if VMA_DEDICATED_ALLOCATION PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR; PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR; +#endif } VmaVulkanFunctions; /// Description of a Allocator to be created. @@ -7386,7 +7396,14 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : m_CurrentFrameIndex(0), m_Pools(VmaStlAllocator(GetAllocationCallbacks())) { - VMA_ASSERT(pCreateInfo->physicalDevice && pCreateInfo->device); + VMA_ASSERT(pCreateInfo->physicalDevice && pCreateInfo->device); + +#if !(VMA_DEDICATED_ALLOCATION) + if((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT) != 0) + { + VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT set but required extensions are disabled by preprocessor macros."); + } +#endif memset(&m_DeviceMemoryCallbacks, 0 ,sizeof(m_DeviceMemoryCallbacks)); memset(&m_MemProps, 0, sizeof(m_MemProps)); @@ -7477,6 +7494,7 @@ void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunc m_VulkanFunctions.vkDestroyBuffer = &vkDestroyBuffer; m_VulkanFunctions.vkCreateImage = &vkCreateImage; m_VulkanFunctions.vkDestroyImage = &vkDestroyImage; +#if VMA_DEDICATED_ALLOCATION if(m_UseKhrDedicatedAllocation) { m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR = @@ -7484,6 +7502,7 @@ void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunc m_VulkanFunctions.vkGetImageMemoryRequirements2KHR = (PFN_vkGetImageMemoryRequirements2KHR)vkGetDeviceProcAddr(m_hDevice, "vkGetImageMemoryRequirements2KHR"); } +#endif // #if VMA_DEDICATED_ALLOCATION #endif // #if VMA_STATIC_VULKAN_FUNCTIONS == 1 #define VMA_COPY_IF_NOT_NULL(funcName) \ @@ -7505,8 +7524,10 @@ void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunc VMA_COPY_IF_NOT_NULL(vkDestroyBuffer); VMA_COPY_IF_NOT_NULL(vkCreateImage); VMA_COPY_IF_NOT_NULL(vkDestroyImage); +#if VMA_DEDICATED_ALLOCATION VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements2KHR); VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements2KHR); +#endif } #undef VMA_COPY_IF_NOT_NULL @@ -7527,11 +7548,13 @@ void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunc VMA_ASSERT(m_VulkanFunctions.vkDestroyBuffer != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkCreateImage != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkDestroyImage != VMA_NULL); +#if VMA_DEDICATED_ALLOCATION if(m_UseKhrDedicatedAllocation) { VMA_ASSERT(m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkGetImageMemoryRequirements2KHR != VMA_NULL); } +#endif } VkDeviceSize VmaAllocator_T::CalcPreferredBlockSize(uint32_t memTypeIndex) @@ -7665,6 +7688,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( allocInfo.memoryTypeIndex = memTypeIndex; allocInfo.allocationSize = size; +#if VMA_DEDICATED_ALLOCATION VkMemoryDedicatedAllocateInfoKHR dedicatedAllocInfo = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR }; if(m_UseKhrDedicatedAllocation) { @@ -7680,6 +7704,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( allocInfo.pNext = &dedicatedAllocInfo; } } +#endif // #if VMA_DEDICATED_ALLOCATION // Allocate VkDeviceMemory. VkDeviceMemory hMemory = VK_NULL_HANDLE; @@ -7731,6 +7756,7 @@ void VmaAllocator_T::GetBufferMemoryRequirements( bool& requiresDedicatedAllocation, bool& prefersDedicatedAllocation) const { +#if VMA_DEDICATED_ALLOCATION if(m_UseKhrDedicatedAllocation) { VkBufferMemoryRequirementsInfo2KHR memReqInfo = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2_KHR }; @@ -7748,6 +7774,7 @@ void VmaAllocator_T::GetBufferMemoryRequirements( prefersDedicatedAllocation = (memDedicatedReq.prefersDedicatedAllocation != VK_FALSE); } else +#endif // #if VMA_DEDICATED_ALLOCATION { (*m_VulkanFunctions.vkGetBufferMemoryRequirements)(m_hDevice, hBuffer, &memReq); requiresDedicatedAllocation = false; @@ -7761,6 +7788,7 @@ void VmaAllocator_T::GetImageMemoryRequirements( bool& requiresDedicatedAllocation, bool& prefersDedicatedAllocation) const { +#if VMA_DEDICATED_ALLOCATION if(m_UseKhrDedicatedAllocation) { VkImageMemoryRequirementsInfo2KHR memReqInfo = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2_KHR }; @@ -7778,6 +7806,7 @@ void VmaAllocator_T::GetImageMemoryRequirements( prefersDedicatedAllocation = (memDedicatedReq.prefersDedicatedAllocation != VK_FALSE); } else +#endif // #if VMA_DEDICATED_ALLOCATION { (*m_VulkanFunctions.vkGetImageMemoryRequirements)(m_hDevice, hImage, &memReq); requiresDedicatedAllocation = false;