Added macro VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE Default to 1. Also changed VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT to default to 1. Fixes #338
diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index adf6b32..e4c21a5 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h
@@ -3276,7 +3276,18 @@ Set this to 1 to make VMA never exceed VkPhysicalDeviceLimits::maxMemoryAllocationCount and return error instead of leaving up to Vulkan implementation what to do in such cases. */ - #define VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT (0) + #define VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT (1) +#endif + +#ifndef VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE + /* + Set this to 1 to make VMA never exceed VkPhysicalDeviceMemoryProperties::memoryHeaps[i].size + with a single allocation size VkMemoryAllocateInfo::allocationSize + and return error instead of leaving up to Vulkan implementation what to do in such cases. + It protects agaist validation error VUID-vkAllocateMemory-pAllocateInfo-01713. + On the other hand, allowing exceeding this size may result in a successful allocation despite the validation error. + */ + #define VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE (1) #endif #ifndef VMA_SMALL_HEAP_MAX_SIZE @@ -14415,6 +14426,15 @@ VkResult VmaAllocator_T::AllocateVulkanMemory(const VkMemoryAllocateInfo* pAllocateInfo, VkDeviceMemory* pMemory) { + const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(pAllocateInfo->memoryTypeIndex); + +#if VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE + if (pAllocateInfo->allocationSize > m_MemProps.memoryHeaps[heapIndex].size) + { + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } +#endif + AtomicTransactionalIncrement<VMA_ATOMIC_UINT32> deviceMemoryCountIncrement; const uint64_t prevDeviceMemoryCount = deviceMemoryCountIncrement.Increment(&m_DeviceMemoryCount); #if VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT @@ -14424,8 +14444,6 @@ } #endif - const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(pAllocateInfo->memoryTypeIndex); - // HeapSizeLimit is in effect for this heap. if((m_HeapSizeLimitMask & (1u << heapIndex)) != 0) {