Hash :
4963febf
Author :
Date :
2023-08-02T16:52:52
Vulkan: Remove type indices with host-visible bit
Currently, the memory type index for VMA image allocations are
selected and returned by the API. However, it could potentially
choose a type index with more flags than required or preferred,
and ignore the index with exactly the flags we want. For example,
it could pick a type index with the host-visible property flag,
even if is unnecessary and a type index with a device-local flag
would suffice.
Using memoryTypeBits during the allocation allows us to filter
the unwanted type indices out and use the other indices initially.
* Added a new function to RendererVk.cpp to try to remove the memory
type indices with the host-visible bit for VMA image allocations
if they should be device-local.
* GetMemoryTypeBitsExcludingHostVisible()
* It also removes the indices with the protected bit if it is not
required.
* If the allocation is unsuccessful, the fallback resets the field
for memoryTypeBits, allowing all available type indices to be used
for the allocation.
* Added memory type index to the pending allocation log during OOM.
Bug: b/294085818
Change-Id: Icc1b218df075170a6baa7ec57c837ed59cd4fa96
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4743604
Reviewed-by: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
Reviewed-by: Charlie Lao <cclao@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// vma_allocator_wrapper.cpp:
// Hides VMA functions so we can use separate warning sets.
//
#include "vk_mem_alloc_wrapper.h"
#include <vk_mem_alloc.h>
namespace vma
{
#define VALIDATE_BLOCK_CREATE_FLAG_BITS(x) \
static_assert(static_cast<uint32_t>(x) == \
static_cast<uint32_t>(VMA_VIRTUAL_BLOCK_CREATE_##x##_ALGORITHM_BIT), \
"VMA enum mismatch")
VALIDATE_BLOCK_CREATE_FLAG_BITS(LINEAR);
#if ANGLE_VMA_VERSION < 3000000
VALIDATE_BLOCK_CREATE_FLAG_BITS(BUDDY);
#endif // ANGLE_VMA_VERSION < 3000000
VkResult InitAllocator(VkPhysicalDevice physicalDevice,
VkDevice device,
VkInstance instance,
uint32_t apiVersion,
VkDeviceSize preferredLargeHeapBlockSize,
VmaAllocator *pAllocator)
{
VmaVulkanFunctions funcs = {};
funcs.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties;
funcs.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties;
funcs.vkAllocateMemory = vkAllocateMemory;
funcs.vkFreeMemory = vkFreeMemory;
funcs.vkMapMemory = vkMapMemory;
funcs.vkUnmapMemory = vkUnmapMemory;
funcs.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges;
funcs.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges;
funcs.vkBindBufferMemory = vkBindBufferMemory;
funcs.vkBindImageMemory = vkBindImageMemory;
funcs.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements;
funcs.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements;
funcs.vkCreateBuffer = vkCreateBuffer;
funcs.vkDestroyBuffer = vkDestroyBuffer;
funcs.vkCreateImage = vkCreateImage;
funcs.vkDestroyImage = vkDestroyImage;
funcs.vkCmdCopyBuffer = vkCmdCopyBuffer;
{
#if !defined(ANGLE_SHARED_LIBVULKAN)
// When the vulkan-loader is statically linked, we need to use the extension
// functions defined in ANGLE's rx namespace. When it's dynamically linked
// with volk, this will default to the function definitions with no namespace
using rx::vkBindBufferMemory2KHR;
using rx::vkBindImageMemory2KHR;
using rx::vkGetBufferMemoryRequirements2KHR;
using rx::vkGetImageMemoryRequirements2KHR;
using rx::vkGetPhysicalDeviceMemoryProperties2KHR;
#endif // !defined(ANGLE_SHARED_LIBVULKAN)
funcs.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2KHR;
funcs.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2KHR;
funcs.vkBindBufferMemory2KHR = vkBindBufferMemory2KHR;
funcs.vkBindImageMemory2KHR = vkBindImageMemory2KHR;
funcs.vkGetPhysicalDeviceMemoryProperties2KHR = vkGetPhysicalDeviceMemoryProperties2KHR;
}
VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.physicalDevice = physicalDevice;
allocatorInfo.device = device;
allocatorInfo.instance = instance;
allocatorInfo.pVulkanFunctions = &funcs;
allocatorInfo.vulkanApiVersion = apiVersion;
allocatorInfo.preferredLargeHeapBlockSize = preferredLargeHeapBlockSize;
return vmaCreateAllocator(&allocatorInfo, pAllocator);
}
void DestroyAllocator(VmaAllocator allocator)
{
vmaDestroyAllocator(allocator);
}
VkResult CreatePool(VmaAllocator allocator,
uint32_t memoryTypeIndex,
#if ANGLE_VMA_VERSION < 3000000
bool buddyAlgorithm,
#endif // ANGLE_VMA_VERSION < 3000000
VkDeviceSize blockSize,
VmaPool *pPool)
{
VmaPoolCreateInfo poolCreateInfo = {};
poolCreateInfo.memoryTypeIndex = memoryTypeIndex;
poolCreateInfo.flags = VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT;
#if ANGLE_VMA_VERSION < 3000000
if (buddyAlgorithm)
{
poolCreateInfo.flags |= VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT;
}
#endif
poolCreateInfo.blockSize = blockSize;
poolCreateInfo.maxBlockCount = -1; // unlimited
return vmaCreatePool(allocator, &poolCreateInfo, pPool);
}
void DestroyPool(VmaAllocator allocator, VmaPool pool)
{
vmaDestroyPool(allocator, pool);
}
void FreeMemory(VmaAllocator allocator, VmaAllocation allocation)
{
vmaFreeMemory(allocator, allocation);
}
VkResult CreateBuffer(VmaAllocator allocator,
const VkBufferCreateInfo *pBufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMapped,
uint32_t *pMemoryTypeIndexOut,
VkBuffer *pBuffer,
VmaAllocation *pAllocation)
{
VkResult result;
VmaAllocationCreateInfo allocationCreateInfo = {};
allocationCreateInfo.requiredFlags = requiredFlags;
allocationCreateInfo.preferredFlags = preferredFlags;
allocationCreateInfo.flags = (persistentlyMapped) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
VmaAllocationInfo allocationInfo = {};
result = vmaCreateBuffer(allocator, pBufferCreateInfo, &allocationCreateInfo, pBuffer,
pAllocation, &allocationInfo);
*pMemoryTypeIndexOut = allocationInfo.memoryType;
return result;
}
VkResult AllocateAndBindMemoryForImage(VmaAllocator allocator,
VkImage *pImage,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
uint32_t memoryTypeBits,
bool allocateDedicatedMemory,
VmaAllocation *pAllocationOut,
uint32_t *pMemoryTypeIndexOut,
VkDeviceSize *sizeOut)
{
VkResult result;
VmaAllocationCreateInfo allocationCreateInfo = {};
allocationCreateInfo.requiredFlags = requiredFlags;
allocationCreateInfo.preferredFlags = preferredFlags;
allocationCreateInfo.memoryTypeBits = memoryTypeBits;
allocationCreateInfo.flags =
allocateDedicatedMemory ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT : 0;
VmaAllocationInfo allocationInfo = {};
result = vmaAllocateMemoryForImage(allocator, *pImage, &allocationCreateInfo, pAllocationOut,
&allocationInfo);
if (result == VK_SUCCESS)
{
// If binding was unsuccessful, we should free the allocation.
result = vmaBindImageMemory(allocator, *pAllocationOut, *pImage);
if (result != VK_SUCCESS)
{
vmaFreeMemory(allocator, *pAllocationOut);
*pAllocationOut = VK_NULL_HANDLE;
return result;
}
*pMemoryTypeIndexOut = allocationInfo.memoryType;
*sizeOut = allocationInfo.size;
}
return result;
}
VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
const VkBufferCreateInfo *pBufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
uint32_t *pMemoryTypeIndexOut)
{
VmaAllocationCreateInfo allocationCreateInfo = {};
allocationCreateInfo.requiredFlags = requiredFlags;
allocationCreateInfo.preferredFlags = preferredFlags;
allocationCreateInfo.flags = (persistentlyMappedBuffers) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
return vmaFindMemoryTypeIndexForBufferInfo(allocator, pBufferCreateInfo, &allocationCreateInfo,
pMemoryTypeIndexOut);
}
VkResult FindMemoryTypeIndexForImageInfo(VmaAllocator allocator,
const VkImageCreateInfo *pImageCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool allocateDedicatedMemory,
uint32_t *pMemoryTypeIndexOut)
{
VmaAllocationCreateInfo allocationCreateInfo = {};
allocationCreateInfo.requiredFlags = requiredFlags;
allocationCreateInfo.preferredFlags = preferredFlags;
allocationCreateInfo.flags =
allocateDedicatedMemory ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT : 0;
return vmaFindMemoryTypeIndexForImageInfo(allocator, pImageCreateInfo, &allocationCreateInfo,
pMemoryTypeIndexOut);
}
void GetMemoryTypeProperties(VmaAllocator allocator,
uint32_t memoryTypeIndex,
VkMemoryPropertyFlags *pFlags)
{
vmaGetMemoryTypeProperties(allocator, memoryTypeIndex, pFlags);
}
VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData)
{
return vmaMapMemory(allocator, allocation, ppData);
}
void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation)
{
return vmaUnmapMemory(allocator, allocation);
}
void FlushAllocation(VmaAllocator allocator,
VmaAllocation allocation,
VkDeviceSize offset,
VkDeviceSize size)
{
vmaFlushAllocation(allocator, allocation, offset, size);
}
void InvalidateAllocation(VmaAllocator allocator,
VmaAllocation allocation,
VkDeviceSize offset,
VkDeviceSize size)
{
vmaInvalidateAllocation(allocator, allocation, offset, size);
}
void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap)
{
vmaBuildStatsString(allocator, statsString, detailedMap);
}
void FreeStatsString(VmaAllocator allocator, char *statsString)
{
vmaFreeStatsString(allocator, statsString);
}
// VmaVirtualBlock implementation
VkResult CreateVirtualBlock(VkDeviceSize size,
VirtualBlockCreateFlags flags,
VmaVirtualBlock *pVirtualBlock)
{
VmaVirtualBlockCreateInfo virtualBlockCreateInfo = {};
virtualBlockCreateInfo.size = size;
virtualBlockCreateInfo.flags = (VmaVirtualBlockCreateFlagBits)flags;
return vmaCreateVirtualBlock(&virtualBlockCreateInfo, pVirtualBlock);
}
void DestroyVirtualBlock(VmaVirtualBlock virtualBlock)
{
vmaDestroyVirtualBlock(virtualBlock);
}
VkResult VirtualAllocate(VmaVirtualBlock virtualBlock,
VkDeviceSize size,
VkDeviceSize alignment,
VmaVirtualAllocation *pAllocation,
VkDeviceSize *pOffset)
{
VmaVirtualAllocationCreateInfo createInfo = {};
createInfo.size = size;
createInfo.alignment = alignment;
createInfo.flags = 0;
#if ANGLE_VMA_VERSION < 3000000
return vmaVirtualAllocate(virtualBlock, &createInfo, pOffset);
#else
return vmaVirtualAllocate(virtualBlock, &createInfo, pAllocation, pOffset);
#endif // ANGLE_VMA_VERSION < 3000000
}
void VirtualFree(VmaVirtualBlock virtualBlock, VmaVirtualAllocation allocation, VkDeviceSize offset)
{
#if ANGLE_VMA_VERSION < 3000000
vmaVirtualFree(virtualBlock, offset);
#else
vmaVirtualFree(virtualBlock, allocation);
#endif // ANGLE_VMA_VERSION < 3000000
}
VkBool32 IsVirtualBlockEmpty(VmaVirtualBlock virtualBlock)
{
return vmaIsVirtualBlockEmpty(virtualBlock);
}
void GetVirtualAllocationInfo(VmaVirtualBlock virtualBlock,
VmaVirtualAllocation allocation,
VkDeviceSize offset,
VkDeviceSize *sizeOut,
void **pUserDataOut)
{
VmaVirtualAllocationInfo virtualAllocInfo = {};
#if ANGLE_VMA_VERSION < 3000000
vmaGetVirtualAllocationInfo(virtualBlock, offset, &virtualAllocInfo);
#else
vmaGetVirtualAllocationInfo(virtualBlock, allocation, &virtualAllocInfo);
#endif // ANGLE_VMA_VERSION < 3000000
*sizeOut = virtualAllocInfo.size;
*pUserDataOut = virtualAllocInfo.pUserData;
}
void ClearVirtualBlock(VmaVirtualBlock virtualBlock)
{
vmaClearVirtualBlock(virtualBlock);
}
void SetVirtualAllocationUserData(VmaVirtualBlock virtualBlock,
VmaVirtualAllocation allocation,
VkDeviceSize offset,
void *pUserData)
{
#if ANGLE_VMA_VERSION < 3000000
vmaSetVirtualAllocationUserData(virtualBlock, offset, pUserData);
#else
vmaSetVirtualAllocationUserData(virtualBlock, allocation, pUserData);
#endif // ANGLE_VMA_VERSION < 3000000
}
void CalculateVirtualBlockStats(VmaVirtualBlock virtualBlock, StatInfo *pStatInfo)
{
#if ANGLE_VMA_VERSION < 3000000
vmaCalculateVirtualBlockStats(virtualBlock, reinterpret_cast<VmaStatInfo *>(pStatInfo));
#else
vmaCalculateVirtualBlockStatistics(virtualBlock,
reinterpret_cast<VmaDetailedStatistics *>(pStatInfo));
#endif // ANGLE_VMA_VERSION < 3000000
}
void BuildVirtualBlockStatsString(VmaVirtualBlock virtualBlock,
char **ppStatsString,
VkBool32 detailedMap)
{
vmaBuildVirtualBlockStatsString(virtualBlock, ppStatsString, detailedMap);
}
void FreeVirtualBlockStatsString(VmaVirtualBlock virtualBlock, char *pStatsString)
{
vmaFreeVirtualBlockStatsString(virtualBlock, pStatsString);
}
} // namespace vma