Refactor: GHOST/Vulkan: Wrap handles in a struct

Vulkan handles are currently only requested once. In the future OpenXR
also needs acces to these handles and additional handles will be needed
when introducing copy queues and async compute.

This PR will collect the handles in a struct to ensure we don't need to
alter the GHOST interface for every change.

Pull Request: https://projects.blender.org/blender/blender/pulls/135905
This commit is contained in:
Jeroen Bakker
2025-03-13 11:06:20 +01:00
parent 8f2f0520da
commit 1ea1f4c92c
8 changed files with 45 additions and 97 deletions

View File

@@ -1285,32 +1285,10 @@ int GHOST_XrGetControllerModelData(GHOST_XrContextHandle xr_context,
*
* \param context: GHOST context handle of a vulkan context to
* get the Vulkan handles from.
* \param r_instance: After calling this function the VkInstance
* referenced by this parameter will contain the VKInstance handle
* of the context associated with the `context` parameter.
* \param r_physical_device: After calling this function the VkPhysicalDevice
* referenced by this parameter will contain the VKPhysicalDevice handle
* of the context associated with the `context` parameter.
* \param r_device: After calling this function the VkDevice
* referenced by this parameter will contain the VKDevice handle
* of the context associated with the `context` parameter.
* \param r_graphic_queue_family: After calling this function the uint32_t
* referenced by this parameter will contain the graphic queue family id
* of the context associated with the `context` parameter.
* \param r_queue: After calling this function the VkQueue
* referenced by this parameter will contain the VKQueue handle
* of the context associated with the `context` parameter.
* \param r_queue_mutex: After calling this function the std::mutex referred
* by this parameter will contain the mutex of the context associated
* with the context parameter.
* \param r_handles: After calling this structure is filled with
* the vulkan handles of the context.
*/
void GHOST_GetVulkanHandles(GHOST_ContextHandle context,
void *r_instance,
void *r_physical_device,
void *r_device,
uint32_t *r_graphic_queue_family,
void *r_queue,
void **r_queue_mutex);
void GHOST_GetVulkanHandles(GHOST_ContextHandle context, GHOST_VulkanHandles *r_handles);
/**
* Set the pre and post callbacks for vulkan swap chain in the given context.

View File

@@ -53,33 +53,10 @@ class GHOST_IContext {
* Other contexts will not return any handles and leave the
* handles where the parameters are referring to unmodified.
*
* \param context: GHOST context handle of a vulkan context to
* get the Vulkan handles from.
* \param r_instance: After calling this function the VkInstance
* referenced by this parameter will contain the VKInstance handle
* of the context associated with the `context` parameter.
* \param r_physical_device: After calling this function the VkPhysicalDevice
* referenced by this parameter will contain the VKPhysicalDevice handle
* of the context associated with the `context` parameter.
* \param r_device: After calling this function the VkDevice
* referenced by this parameter will contain the VKDevice handle
* of the context associated with the `context` parameter.
* \param r_graphic_queue_family: After calling this function the uint32_t
* referenced by this parameter will contain the graphic queue family id
* of the context associated with the `context` parameter.
* \param r_queue: After calling this function the VkQueue
* referenced by this parameter will contain the VKQueue handle
* of the context associated with the `context` parameter.
* \param r_queue_mutex: After calling this function the std::mutex referred
* by this parameter will contain the mutex of the context associated
* with the context parameter.
* \param r_handles: After calling this structure is filled with
* the vulkan handles of the context.
*/
virtual GHOST_TSuccess getVulkanHandles(void *r_instance,
void *r_physical_device,
void *r_device,
uint32_t *r_graphic_queue_family,
void *r_queue,
void **r_queue_mutex) = 0;
virtual GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) = 0;
/**
* Acquire the current swap chain format.

View File

@@ -747,6 +747,16 @@ typedef struct {
/** Resolution of the image. */
VkExtent2D extent;
} GHOST_VulkanSwapChainData;
typedef struct {
VkInstance instance;
VkPhysicalDevice physical_device;
VkDevice device;
uint32_t graphic_queue_family;
VkQueue queue;
void *queue_mutex;
} GHOST_VulkanHandles;
#endif
typedef enum {

View File

@@ -1276,17 +1276,10 @@ int GHOST_XrGetControllerModelData(GHOST_XrContextHandle xr_contexthandle,
#ifdef WITH_VULKAN_BACKEND
void GHOST_GetVulkanHandles(GHOST_ContextHandle contexthandle,
void *r_instance,
void *r_physical_device,
void *r_device,
uint32_t *r_graphic_queue_family,
void *r_queue,
void **r_queue_mutex)
void GHOST_GetVulkanHandles(GHOST_ContextHandle contexthandle, GHOST_VulkanHandles *r_handles)
{
GHOST_IContext *context = (GHOST_IContext *)contexthandle;
context->getVulkanHandles(
r_instance, r_physical_device, r_device, r_graphic_queue_family, r_queue, r_queue_mutex);
context->getVulkanHandles(*r_handles);
}
void GHOST_SetVulkanSwapBuffersCallbacks(

View File

@@ -161,12 +161,7 @@ class GHOST_Context : public GHOST_IContext {
* GHOST_kSuccess when the context is a Vulkan context and the
* handles have been set.
*/
virtual GHOST_TSuccess getVulkanHandles(void * /*r_instance*/,
void * /*r_physical_device*/,
void * /*r_device*/,
uint32_t * /*r_graphic_queue_family*/,
void * /*r_queue*/,
void ** /*r_queue_mutex*/) override
virtual GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles & /* r_handles */) override
{
return GHOST_kFailure;
};

View File

@@ -632,28 +632,28 @@ GHOST_TSuccess GHOST_ContextVK::getVulkanSwapChainFormat(
return GHOST_kSuccess;
}
GHOST_TSuccess GHOST_ContextVK::getVulkanHandles(void *r_instance,
void *r_physical_device,
void *r_device,
uint32_t *r_graphic_queue_family,
void *r_queue,
void **r_queue_mutex)
GHOST_TSuccess GHOST_ContextVK::getVulkanHandles(GHOST_VulkanHandles &r_handles)
{
*((VkInstance *)r_instance) = VK_NULL_HANDLE;
*((VkPhysicalDevice *)r_physical_device) = VK_NULL_HANDLE;
*((VkDevice *)r_device) = VK_NULL_HANDLE;
r_handles = {
VK_NULL_HANDLE, /* instance */
VK_NULL_HANDLE, /* physical_device */
VK_NULL_HANDLE, /* device */
0, /* queue_family */
VK_NULL_HANDLE, /* queue */
nullptr, /* queue_mutex */
};
if (vulkan_device.has_value()) {
*((VkInstance *)r_instance) = vulkan_device->instance;
*((VkPhysicalDevice *)r_physical_device) = vulkan_device->physical_device;
*((VkDevice *)r_device) = vulkan_device->device;
*r_graphic_queue_family = vulkan_device->generic_queue_family;
std::mutex **queue_mutex = (std::mutex **)r_queue_mutex;
*queue_mutex = &vulkan_device->queue_mutex;
r_handles = {
vulkan_device->instance,
vulkan_device->physical_device,
vulkan_device->device,
vulkan_device->generic_queue_family,
m_graphic_queue,
&vulkan_device->queue_mutex,
};
}
*((VkQueue *)r_queue) = m_graphic_queue;
return GHOST_kSuccess;
}

View File

@@ -124,12 +124,7 @@ class GHOST_ContextVK : public GHOST_Context {
* Gets the Vulkan context related resource handles.
* \return A boolean success indicator.
*/
GHOST_TSuccess getVulkanHandles(void *r_instance,
void *r_physical_device,
void *r_device,
uint32_t *r_graphic_queue_family,
void *r_queue,
void **r_queue_mutex) override;
GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) override;
GHOST_TSuccess getVulkanSwapChainFormat(GHOST_VulkanSwapChainData *r_swap_chain_data) override;

View File

@@ -85,14 +85,14 @@ void VKDevice::init(void *ghost_context)
{
BLI_assert(!is_initialized());
void *queue_mutex = nullptr;
GHOST_GetVulkanHandles((GHOST_ContextHandle)ghost_context,
&vk_instance_,
&vk_physical_device_,
&vk_device_,
&vk_queue_family_,
&vk_queue_,
&queue_mutex);
queue_mutex_ = static_cast<std::mutex *>(queue_mutex);
GHOST_VulkanHandles handles = {};
GHOST_GetVulkanHandles((GHOST_ContextHandle)ghost_context, &handles);
vk_instance_ = handles.instance;
vk_physical_device_ = handles.physical_device;
vk_device_ = handles.device;
vk_queue_family_ = handles.graphic_queue_family;
vk_queue_ = handles.queue;
queue_mutex_ = static_cast<std::mutex *>(handles.queue_mutex);
init_physical_device_properties();
init_physical_device_memory_properties();