Skip to content

Commit

Permalink
[Vulkan] Improve VmaAllocatorCreateInfo
Browse files Browse the repository at this point in the history
- Set VmaAllocatorCreateInfo.vulkanApiVersion
- Fix setting Vulkan 1.1+ functions
  • Loading branch information
past-due committed Jun 10, 2024
1 parent 62fa5dd commit 47ce41d
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions lib/ivis_opengl/gfx_api_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4999,7 +4999,28 @@ bool VkRoot::createAllocator()
ASSERT(physicalDevice, "Physical device is null");
ASSERT(dev, "Logical device is null");

VmaVulkanFunctions vulkanFunctions;
VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
allocatorInfo.physicalDevice = physicalDevice;
allocatorInfo.device = dev;
allocatorInfo.instance = inst;
// According to vk_mem_alloc.h:
// vulkanApiVersion "must match the Vulkan version used by the application and supported on the selected physical device,
// so it must be no higher than `VkApplicationInfo::apiVersion` passed to `vkCreateInstance`
// and no higher than `VkPhysicalDeviceProperties::apiVersion` found on the physical device used."
allocatorInfo.vulkanApiVersion = std::min(instanceCreateInfo.pApplicationInfo->apiVersion, physDeviceProps.apiVersion);
debug(LOG_3D, "Using VMA allocator vulkanApiVersion: %s", VkhInfo::vulkan_apiversion_to_string(allocatorInfo.vulkanApiVersion).c_str());

bool enabled_VK_KHR_get_memory_requirements2 = std::find_if(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(),
[](const char *extensionName) { return (strcmp(extensionName, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME) == 0);}) != enabledDeviceExtensions.end();
bool enabled_VK_KHR_dedicated_allocation = std::find_if(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(),
[](const char *extensionName) { return (strcmp(extensionName, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME) == 0);}) != enabledDeviceExtensions.end();
if (enabled_VK_KHR_get_memory_requirements2 && enabled_VK_KHR_dedicated_allocation)
{
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
}

VmaVulkanFunctions vulkanFunctions = {};
vulkanFunctions.vkGetInstanceProcAddr = vkDynLoader.vkGetInstanceProcAddr;
vulkanFunctions.vkGetDeviceProcAddr = vkDynLoader.vkGetDeviceProcAddr;
vulkanFunctions.vkGetPhysicalDeviceProperties = vkDynLoader.vkGetPhysicalDeviceProperties;
Expand Down Expand Up @@ -5030,26 +5051,35 @@ bool VkRoot::createAllocator()
#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000
vulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = vkDynLoader.vkGetPhysicalDeviceMemoryProperties2KHR;
#endif
#if VMA_VULKAN_VERSION >= 1003000
vulkanFunctions.vkGetDeviceBufferMemoryRequirements = vkDynLoader.vkGetDeviceBufferMemoryRequirements;
vulkanFunctions.vkGetDeviceImageMemoryRequirements = vkDynLoader.vkGetDeviceImageMemoryRequirements;

// Vulkan 1.1
#if VMA_VULKAN_VERSION >= 1001000
if(allocatorInfo.vulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))
{
vulkanFunctions.vkGetBufferMemoryRequirements2KHR = vkDynLoader.vkGetBufferMemoryRequirements2;
vulkanFunctions.vkGetImageMemoryRequirements2KHR = vkDynLoader.vkGetImageMemoryRequirements2;
vulkanFunctions.vkBindBufferMemory2KHR = vkDynLoader.vkBindBufferMemory2;
vulkanFunctions.vkBindImageMemory2KHR = vkDynLoader.vkBindImageMemory2;
}
#endif

VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.physicalDevice = physicalDevice;
allocatorInfo.device = dev;
allocatorInfo.instance = inst;
allocatorInfo.pVulkanFunctions = &vulkanFunctions;
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
#if VMA_VULKAN_VERSION >= 1001000
if(allocatorInfo.vulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))
{
vulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = vkDynLoader.vkGetPhysicalDeviceMemoryProperties2;
}
#endif

bool enabled_VK_KHR_get_memory_requirements2 = std::find_if(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(),
[](const char *extensionName) { return (strcmp(extensionName, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME) == 0);}) != enabledDeviceExtensions.end();
bool enabled_VK_KHR_dedicated_allocation = std::find_if(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(),
[](const char *extensionName) { return (strcmp(extensionName, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME) == 0);}) != enabledDeviceExtensions.end();
if (enabled_VK_KHR_get_memory_requirements2 && enabled_VK_KHR_dedicated_allocation)
// Vulkan 1.3
#if VMA_VULKAN_VERSION >= 1003000
if(allocatorInfo.vulkanApiVersion >= VK_MAKE_VERSION(1, 3, 0))
{
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
vulkanFunctions.vkGetDeviceBufferMemoryRequirements = vkDynLoader.vkGetDeviceBufferMemoryRequirements;
vulkanFunctions.vkGetDeviceImageMemoryRequirements = vkDynLoader.vkGetDeviceImageMemoryRequirements;
}
#endif

allocatorInfo.pVulkanFunctions = &vulkanFunctions;

VkResult result = vmaCreateAllocator(&allocatorInfo, &allocator);
if (result != VK_SUCCESS)
Expand Down

0 comments on commit 47ce41d

Please sign in to comment.