New guard, fixed ABI issues

This commit is contained in:
Ilya Doroshenko 2024-08-26 14:33:32 +02:00
parent c41e3fb5a6
commit 9402a6bef9

View File

@ -242,6 +242,15 @@ extern "C" {
#endif #endif
#endif #endif
// Defined to 1 when VK_KHR_external_memory device extension is defined in Vulkan headers.
#if !defined(VMA_EXTERNAL_MEMORY_WIN32)
#if VK_KHR_external_memory_win32
#define VMA_EXTERNAL_MEMORY_WIN32 1
#else
#define VMA_EXTERNAL_MEMORY_WIN32 0
#endif
#endif
// Define these macros to decorate all public functions with additional code, // Define these macros to decorate all public functions with additional code,
// before and after returned type, appropriately. This may be useful for // before and after returned type, appropriately. This may be useful for
// exporting the functions when compiling VMA as a separate library. Example: // exporting the functions when compiling VMA as a separate library. Example:
@ -467,7 +476,7 @@ extern "C" {
You should set this flag if you found available and enabled this device extension, You should set this flag if you found available and enabled this device extension,
while creating Vulkan device passed as VmaAllocatorCreateInfo::device. while creating Vulkan device passed as VmaAllocatorCreateInfo::device.
*/ */
VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32 = 0x00000200, VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32_BIT = 0x00000200,
VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaAllocatorCreateFlagBits; } VmaAllocatorCreateFlagBits;
@ -6082,7 +6091,7 @@ private:
#endif // _VMA_MAPPING_HYSTERESIS #endif // _VMA_MAPPING_HYSTERESIS
#if defined(VK_USE_PLATFORM_WIN32_KHR) #if VMA_EXTERNAL_MEMORY_WIN32
class VmaWin32Handle class VmaWin32Handle
{ {
public: public:
@ -6114,10 +6123,11 @@ public:
VmaMutexLockWrite lock(m_Mutex, useMutex); VmaMutexLockWrite lock(m_Mutex, useMutex);
if (m_hHandle.load(std::memory_order_relaxed) == VMA_NULL) if (m_hHandle.load(std::memory_order_relaxed) == VMA_NULL)
{ {
res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle); VkResult res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle);
m_hHandle.store(hCreatedHandle, std::memory_order_relaxed); m_hHandle.store(hCreatedHandle, std::memory_order_relaxed);
} }
} }
*pHandle = Duplicate(hTargetProcess); *pHandle = Duplicate(hTargetProcess);
return res; return res;
} }
@ -6161,8 +6171,9 @@ class VmaWin32Handle
{ {
// ABI compatibility // ABI compatibility
void* placeholder = VMA_NULL; void* placeholder = VMA_NULL;
VMA_RW_MUTEX placeholder2;
}; };
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
#ifndef _VMA_DEVICE_MEMORY_BLOCK #ifndef _VMA_DEVICE_MEMORY_BLOCK
@ -6231,13 +6242,13 @@ public:
VkDeviceSize allocationLocalOffset, VkDeviceSize allocationLocalOffset,
VkImage hImage, VkImage hImage,
const void* pNext); const void* pNext);
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VkResult CreateWin32Handle( VkResult CreateWin32Handle(
const VmaAllocator hAllocator, const VmaAllocator hAllocator,
decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR,
HANDLE hTargetProcess, HANDLE hTargetProcess,
HANDLE* pHandle)noexcept; HANDLE* pHandle)noexcept;
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
private: private:
VmaPool m_hParentPool; // VK_NULL_HANDLE if not belongs to custom pool. VmaPool m_hParentPool; // VK_NULL_HANDLE if not belongs to custom pool.
uint32_t m_MemoryTypeIndex; uint32_t m_MemoryTypeIndex;
@ -6340,9 +6351,9 @@ public:
void PrintParameters(class VmaJsonWriter& json) const; void PrintParameters(class VmaJsonWriter& json) const;
#endif #endif
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) const noexcept; VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) const noexcept;
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
private: private:
// Allocation out of VmaDeviceMemoryBlock. // Allocation out of VmaDeviceMemoryBlock.
@ -10789,13 +10800,13 @@ VkResult VmaDeviceMemoryBlock::BindImageMemory(
return hAllocator->BindVulkanImage(m_hMemory, memoryOffset, hImage, pNext); return hAllocator->BindVulkanImage(m_hMemory, memoryOffset, hImage, pNext);
} }
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
{ {
VMA_ASSERT(pHandle); VMA_ASSERT(pHandle);
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle); return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
} }
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
#endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS #endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS
#ifndef _VMA_ALLOCATION_T_FUNCTIONS #ifndef _VMA_ALLOCATION_T_FUNCTIONS
@ -11096,7 +11107,7 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
json.WriteString(m_pName); json.WriteString(m_pName);
} }
} }
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) const noexcept VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) const noexcept
{ {
// Where do we get this function from? // Where do we get this function from?
@ -11112,7 +11123,7 @@ VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTarget
return VK_ERROR_FEATURE_NOT_PRESENT; return VK_ERROR_FEATURE_NOT_PRESENT;
} }
} }
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
#endif // VMA_STATS_STRING_ENABLED #endif // VMA_STATS_STRING_ENABLED
void VmaAllocation_T::FreeName(VmaAllocator hAllocator) void VmaAllocation_T::FreeName(VmaAllocator hAllocator)
@ -12843,7 +12854,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
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_UseKhrMaintenance4((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT) != 0),
m_UseKhrMaintenance5((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE5_BIT) != 0), m_UseKhrMaintenance5((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE5_BIT) != 0),
m_UseKhrExternalMemoryWin32((pCreateInfo->flags& VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32) != 0), m_UseKhrExternalMemoryWin32((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32_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),
@ -12942,10 +12953,10 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
} }
#endif #endif
#if !defined(VK_USE_PLATFORM_WIN32_KHR) #if !(VMA_EXTERNAL_MEMORY_WIN32)
if(m_UseKhrExternalMemoryWin32) if(m_UseKhrExternalMemoryWin32)
{ {
VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE5_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_KHR_EXTERNAL_MEMORY_WIN32_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
@ -13123,7 +13134,7 @@ void VmaAllocator_T::ImportVulkanFunctions_Static()
m_VulkanFunctions.vkGetDeviceImageMemoryRequirements = (PFN_vkGetDeviceImageMemoryRequirements)vkGetDeviceImageMemoryRequirements; m_VulkanFunctions.vkGetDeviceImageMemoryRequirements = (PFN_vkGetDeviceImageMemoryRequirements)vkGetDeviceImageMemoryRequirements;
} }
#endif #endif
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = (PFN_vkGetMemoryWin32HandleKHR)vkGetMemoryWin32HandleKHR; m_VulkanFunctions.vkGetMemoryWin32HandleKHR = (PFN_vkGetMemoryWin32HandleKHR)vkGetMemoryWin32HandleKHR;
#else #else
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = VMA_NULL; m_VulkanFunctions.vkGetMemoryWin32HandleKHR = VMA_NULL;
@ -13177,7 +13188,7 @@ void VmaAllocator_T::ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVul
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
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VMA_COPY_IF_NOT_NULL(vkGetMemoryWin32HandleKHR); VMA_COPY_IF_NOT_NULL(vkGetMemoryWin32HandleKHR);
#endif #endif
#undef VMA_COPY_IF_NOT_NULL #undef VMA_COPY_IF_NOT_NULL
@ -13281,7 +13292,7 @@ void VmaAllocator_T::ImportVulkanFunctions_Dynamic()
VMA_FETCH_DEVICE_FUNC(vkGetDeviceImageMemoryRequirements, PFN_vkGetDeviceImageMemoryRequirementsKHR, "vkGetDeviceImageMemoryRequirementsKHR"); VMA_FETCH_DEVICE_FUNC(vkGetDeviceImageMemoryRequirements, PFN_vkGetDeviceImageMemoryRequirementsKHR, "vkGetDeviceImageMemoryRequirementsKHR");
} }
#endif #endif
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
if (m_UseKhrExternalMemoryWin32) if (m_UseKhrExternalMemoryWin32)
{ {
VMA_FETCH_DEVICE_FUNC(vkGetMemoryWin32HandleKHR, PFN_vkGetMemoryWin32HandleKHR, "vkGetMemoryWin32HandleKHR"); VMA_FETCH_DEVICE_FUNC(vkGetMemoryWin32HandleKHR, PFN_vkGetMemoryWin32HandleKHR, "vkGetMemoryWin32HandleKHR");
@ -13335,7 +13346,7 @@ void VmaAllocator_T::ValidateVulkanFunctions()
VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR != VMA_NULL);
} }
#endif #endif
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
if (m_UseKhrExternalMemoryWin32) if (m_UseKhrExternalMemoryWin32)
{ {
VMA_ASSERT(m_VulkanFunctions.vkGetMemoryWin32HandleKHR != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkGetMemoryWin32HandleKHR != VMA_NULL);
@ -16597,7 +16608,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeVirtualBlockStatsString(VmaVirtualBlock V
VmaFreeString(virtualBlock->GetAllocationCallbacks(), pStatsString); VmaFreeString(virtualBlock->GetAllocationCallbacks(), pStatsString);
} }
} }
#ifdef VK_USE_PLATFORM_WIN32_KHR #if VMA_EXTERNAL_MEMORY_WIN32
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator, VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator,
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* pHandle) VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* pHandle)
{ {
@ -16605,7 +16616,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_
VMA_DEBUG_GLOBAL_MUTEX_LOCK; VMA_DEBUG_GLOBAL_MUTEX_LOCK;
return allocation->GetWin32Handle(allocator, hTargetProcess, pHandle); return allocation->GetWin32Handle(allocator, hTargetProcess, pHandle);
} }
#endif // VK_USE_PLATFORM_WIN32_KHR #endif // VMA_EXTERNAL_MEMORY_WIN32
#endif // VMA_STATS_STRING_ENABLED #endif // VMA_STATS_STRING_ENABLED
#endif // _VMA_PUBLIC_INTERFACE #endif // _VMA_PUBLIC_INTERFACE
#endif // VMA_IMPLEMENTATION #endif // VMA_IMPLEMENTATION