From 96da1f3eed7c2834bd05518df3024020560db81e Mon Sep 17 00:00:00 2001 From: Stephen Saunders Date: Sat, 29 Jan 2022 13:27:36 -0500 Subject: [PATCH] Fix heap flags check in Vulkan Allocator and add heap flags check to VMA Allocator (cherry picked from commit 488f8d27616570029de9429cf4794886f6d209e1) --- neo/renderer/Vulkan/Allocator_VK.cpp | 19 +++++++++++++++---- neo/renderer/Vulkan/vma.h | 10 +++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/neo/renderer/Vulkan/Allocator_VK.cpp b/neo/renderer/Vulkan/Allocator_VK.cpp index 0980c5bc..20ab3379 100644 --- a/neo/renderer/Vulkan/Allocator_VK.cpp +++ b/neo/renderer/Vulkan/Allocator_VK.cpp @@ -78,13 +78,13 @@ uint32 FindMemoryTypeIndex( const uint32 memoryTypeBits, const vulkanMemoryUsage case VULKAN_MEMORY_USAGE_CPU_TO_GPU: required |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; preferred |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; - // SRS - Make sure preferred memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory + // SRS - Make sure memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory avoid |= VK_MEMORY_HEAP_MULTI_INSTANCE_BIT; break; case VULKAN_MEMORY_USAGE_GPU_TO_CPU: required |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; preferred |= VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT; - // SRS - Make sure preferred memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory + // SRS - Make sure memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory avoid |= VK_MEMORY_HEAP_MULTI_INSTANCE_BIT; break; default: @@ -98,14 +98,19 @@ uint32 FindMemoryTypeIndex( const uint32 memoryTypeBits, const vulkanMemoryUsage continue; } + // SRS - Make sure memory type does not have any avoid heap flags set + if( ( physicalMemoryProperties.memoryHeaps[ physicalMemoryProperties.memoryTypes[ i ].heapIndex ].flags & avoid ) != 0 ) + { + continue; + } + const VkMemoryPropertyFlags properties = physicalMemoryProperties.memoryTypes[ i ].propertyFlags; if( ( properties & required ) != required ) { continue; } - // SRS - Make sure preferred memory type does not have any avoid heap flags set - if( ( properties & preferred ) != preferred || ( physicalMemoryProperties.memoryHeaps[ i ].flags & avoid ) != 0 ) + if( ( properties & preferred ) != preferred ) { continue; } @@ -120,6 +125,12 @@ uint32 FindMemoryTypeIndex( const uint32 memoryTypeBits, const vulkanMemoryUsage continue; } + // SRS - Make sure memory type does not have any avoid heap flags set + if( ( physicalMemoryProperties.memoryHeaps[ physicalMemoryProperties.memoryTypes[ i ].heapIndex ].flags & avoid ) != 0 ) + { + continue; + } + const VkMemoryPropertyFlags properties = physicalMemoryProperties.memoryTypes[ i ].propertyFlags; if( ( properties & required ) != required ) { diff --git a/neo/renderer/Vulkan/vma.h b/neo/renderer/Vulkan/vma.h index a6654d3e..916939c2 100644 --- a/neo/renderer/Vulkan/vma.h +++ b/neo/renderer/Vulkan/vma.h @@ -5231,6 +5231,7 @@ VkResult vmaFindMemoryTypeIndex( uint32_t requiredFlags = pMemoryRequirements->requiredFlags; uint32_t preferredFlags = pMemoryRequirements->preferredFlags; + uint32_t avoidFlags = 0; if( preferredFlags == 0 ) { preferredFlags = requiredFlags; @@ -5252,10 +5253,14 @@ VkResult vmaFindMemoryTypeIndex( case VMA_MEMORY_USAGE_CPU_TO_GPU: requiredFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; preferredFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + // SRS - Make sure memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory + avoidFlags |= VK_MEMORY_HEAP_MULTI_INSTANCE_BIT; break; case VMA_MEMORY_USAGE_GPU_TO_CPU: requiredFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; preferredFlags |= VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT; + // SRS - Make sure memory type does not have VK_MEMORY_HEAP_MULTI_INSTANCE_BIT set, otherwise get validation errors when mapping memory + avoidFlags |= VK_MEMORY_HEAP_MULTI_INSTANCE_BIT; break; default: break; @@ -5277,8 +5282,11 @@ VkResult vmaFindMemoryTypeIndex( { const VkMemoryPropertyFlags currFlags = allocator->m_MemProps.memoryTypes[memTypeIndex].propertyFlags; + const VkMemoryHeapFlags heapFlags = + allocator->m_MemProps.memoryHeaps[allocator->m_MemProps.memoryTypes[memTypeIndex].heapIndex].flags; // This memory type contains requiredFlags. - if( ( requiredFlags & ~currFlags ) == 0 ) + // SRS - and does not contain any heap avoidFlags + if( ( requiredFlags & ~currFlags ) == 0 && ( avoidFlags & heapFlags ) == 0 ) { // Calculate cost as number of bits from preferredFlags not present in this memory type. uint32_t currCost = CountBitsSet( preferredFlags & ~currFlags );