Refactor: Vulkan: Move memory allocator ownership to GHOST.

This PR moves the ownership of vulkan memory allocator from gpu/device
to GHOST/context/device. This will allow in the future a cleaner control
flow between OpenXR and Vulkan. Currently as the ownership is in the gpu
module many objects would need to pass from GHOST to the GPU module to be
shared between the 2 allocators. Moving both (OpenXR/Context) allocator
to GHOST would reduce the complexity.

Pull Request: https://projects.blender.org/blender/blender/pulls/146819
This commit is contained in:
Jeroen Bakker
2025-09-26 09:34:48 +02:00
parent 73fcbaf7c4
commit be4a34d103
5 changed files with 37 additions and 28 deletions

View File

@@ -13,6 +13,7 @@
#ifdef WITH_VULKAN_BACKEND
# include <vulkan/vulkan_core.h>
VK_DEFINE_HANDLE(VmaAllocator)
#endif
/* This is used by `GHOST_C-api.h` too, cannot use C++ conventions. */
@@ -992,6 +993,8 @@ typedef struct {
VkQueue queue;
/** The #std::mutex mutex. */
void *queue_mutex;
/** Vulkan memory allocator of the device. */
VmaAllocator vma_allocator;
} GHOST_VulkanHandles;
#endif

View File

@@ -21,6 +21,8 @@
#include "vulkan/vk_ghost_api.hh"
#include "vk_mem_alloc.h"
#include "CLG_log.h"
#include <array>
@@ -222,6 +224,7 @@ class GHOST_DeviceVK {
uint32_t generic_queue_family = 0;
VkQueue generic_queue = VK_NULL_HANDLE;
VmaAllocator vma_allocator = VK_NULL_HANDLE;
VkPhysicalDeviceProperties2 properties = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
@@ -261,8 +264,13 @@ class GHOST_DeviceVK {
vkGetPhysicalDeviceFeatures2(vk_physical_device, &features);
init_extensions();
}
~GHOST_DeviceVK()
{
if (vma_allocator != VK_NULL_HANDLE) {
vmaDestroyAllocator(vma_allocator);
vma_allocator = VK_NULL_HANDLE;
}
if (vk_device != VK_NULL_HANDLE) {
vkDestroyDevice(vk_device, nullptr);
vk_device = VK_NULL_HANDLE;
@@ -316,6 +324,23 @@ class GHOST_DeviceVK {
{
vkGetDeviceQueue(vk_device, generic_queue_family, 0, &generic_queue);
}
void init_memory_allocator(VkInstance vk_instance)
{
VmaAllocatorCreateInfo vma_allocator_create_info = {};
vma_allocator_create_info.vulkanApiVersion = VK_API_VERSION_1_2;
vma_allocator_create_info.physicalDevice = vk_physical_device;
vma_allocator_create_info.device = vk_device;
vma_allocator_create_info.instance = vk_instance;
vma_allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
if (extensions.is_enabled(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME)) {
vma_allocator_create_info.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT;
}
if (extensions.is_enabled(VK_KHR_MAINTENANCE_4_EXTENSION_NAME)) {
vma_allocator_create_info.flags |= VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT;
}
vmaCreateAllocator(&vma_allocator_create_info, &vma_allocator);
}
};
/** \} */
@@ -615,6 +640,7 @@ struct GHOST_InstanceVK {
VK_CHECK(vkCreateDevice(vk_physical_device, &device_create_info, nullptr, &device.vk_device),
GHOST_kFailure);
device.init_generic_queue();
device.init_memory_allocator(vk_instance);
return true;
}
};
@@ -902,6 +928,7 @@ GHOST_TSuccess GHOST_ContextVK::getVulkanHandles(GHOST_VulkanHandles &r_handles)
0, /* queue_family */
VK_NULL_HANDLE, /* queue */
nullptr, /* queue_mutex */
VK_NULL_HANDLE, /* vma_allocator */
};
if (vulkan_instance.has_value() && vulkan_instance.value().device.has_value()) {
@@ -914,6 +941,7 @@ GHOST_TSuccess GHOST_ContextVK::getVulkanHandles(GHOST_VulkanHandles &r_handles)
device_vk.generic_queue_family,
device_vk.generic_queue,
&device_vk.queue_mutex,
device_vk.vma_allocator,
};
}