aux/vk: Refactor our sync primitives (NFC)

This commit is contained in:
Jakob Bornecrantz 2022-02-15 14:16:58 +00:00
parent d6e0c45855
commit 36025886ee
5 changed files with 168 additions and 141 deletions

View file

@ -256,6 +256,7 @@ if(XRT_HAVE_VULKAN)
vk/vk_image_readback_to_xf_pool.c
vk/vk_image_readback_to_xf_pool.h
vk/vk_state_creators.c
vk/vk_sync_objects.c
)
target_link_libraries(aux_vk PUBLIC aux_os aux_util)
target_link_libraries(aux_vk PUBLIC Vulkan::Vulkan)

View file

@ -296,6 +296,7 @@ lib_aux_vk = static_library(
'vk/vk_image_readback_to_xf_pool.h',
'vk/vk_image_readback_to_xf_pool.c',
'vk/vk_state_creators.c',
'vk/vk_sync_objects.c',
),
include_directories: [
xrt_include,

View file

@ -550,114 +550,6 @@ vk_create_image_from_native(struct vk_bundle *vk,
return ret;
}
VkResult
vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence)
{
VkFence fence = VK_NULL_HANDLE;
VkResult ret;
VkFenceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
};
ret = vk->vkCreateFence(vk->device, &create_info, NULL, &fence);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkCreateFence: %s", vk_result_string(ret));
return ret;
}
#ifdef XRT_GRAPHICS_SYNC_HANDLE_IS_FD
// This is what is used on Linux Mesa when importing fences from OpenGL.
VkExternalFenceHandleTypeFlagBits handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
VkImportFenceFdInfoKHR import_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
.fence = fence,
.handleType = handleType,
.fd = native,
};
ret = vk->vkImportFenceFdKHR(vk->device, &import_info);
if (ret != VK_SUCCESS) {
vk->vkDestroyFence(vk->device, fence, NULL);
VK_ERROR(vk, "vkImportFenceFdKHR: %s", vk_result_string(ret));
return ret;
}
#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE)
//! @todo make sure this is the right one
VkExternalFenceHandleTypeFlagBits handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_WIN32_BIT;
VkImportFenceWin32HandleInfoKHR import_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_WIN32_HANDLE_INFO_KHR,
.pNext = NULL,
.fence = fence,
.flags = 0, /** @todo do we want the temporary flag? */
.handleType = handleType,
.handle = native,
.name = NULL, /* not importing by name */
};
ret = vk->vkImportFenceWin32HandleKHR(vk->device, &import_info);
if (ret != VK_SUCCESS) {
vk->vkDestroyFence(vk->device, fence, NULL);
VK_ERROR(vk, "vkImportFenceFdKHR: %s", vk_result_string(ret));
return ret;
}
#else
#error "Need port to import fence sync handles"
#endif
*out_fence = fence;
return VK_SUCCESS;
}
VkResult
vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem)
{
VkResult ret;
VkSemaphoreCreateInfo semaphore_create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
ret = vk->vkCreateSemaphore(vk->device, &semaphore_create_info, NULL, out_sem);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkCreateSemaphore: %s", vk_result_string(ret));
// Nothing to cleanup
return ret;
}
#if defined(XRT_GRAPHICS_SYNC_HANDLE_IS_FD)
VkImportSemaphoreFdInfoKHR import_semaphore_fd_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
.semaphore = *out_sem,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
.fd = native,
};
ret = vk->vkImportSemaphoreFdKHR(vk->device, &import_semaphore_fd_info);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkImportSemaphoreFdKHR: %s", vk_result_string(ret));
vk->vkDestroySemaphore(vk->device, *out_sem, NULL);
return ret;
}
#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE)
VkImportSemaphoreWin32HandleInfoKHR import_semaphore_handle_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_WIN32_HANDLE_INFO_KHR,
.semaphore = *out_sem,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT,
.handle = native,
};
ret = vk->vkImportSemaphoreWin32HandleKHR(vk->device, &import_semaphore_handle_info);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkImportSemaphoreWin32HandleKHR: %s", vk_result_string(ret));
vk->vkDestroySemaphore(vk->device, *out_sem, NULL);
return ret;
}
#else
#error "Not implemented for this underlying handle type!"
#endif
return ret;
}
VkResult
vk_create_sampler(struct vk_bundle *vk, VkSamplerAddressMode clamp_mode, VkSampler *out_sampler)
{

View file

@ -560,39 +560,6 @@ vk_create_image_from_native(struct vk_bundle *vk,
VkImage *out_image,
VkDeviceMemory *out_mem);
/*!
* @brief Creates a Vulkan fence from a native graphics sync handle.
*
* In case of error, ownership is never transferred and the caller should close the handle themselves.
*
* In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
* handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
* native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
* handle type used.
*
* See the corresponding Vulkan specification text:
* https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#synchronization-fences-importing
*
* @ingroup aux_vk
*/
VkResult
vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence);
/*!
* @brief Creates a Vulkan semaphore from a native graphics sync handle.
*
* In case of error, ownership is never transferred and the caller should close the handle themselves.
*
* In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
* handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
* native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
* handle type used.
*
* @ingroup aux_vk
*/
VkResult
vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem);
/*!
* @ingroup aux_vk
* Helper to create a VkImage
@ -918,6 +885,46 @@ vk_insert_image_memory_barrier(struct vk_bundle *vk,
/*
*
* Sync objects, in the vk_sync_objects.c file.
*
*/
/*!
* @brief Creates a Vulkan fence from a native graphics sync handle.
*
* In case of error, ownership is never transferred and the caller should close the handle themselves.
*
* In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
* handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
* native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
* handle type used.
*
* See the corresponding Vulkan specification text:
* https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#synchronization-fences-importing
*
* @ingroup aux_vk
*/
VkResult
vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence);
/*!
* @brief Creates a Vulkan semaphore from a native graphics sync handle.
*
* In case of error, ownership is never transferred and the caller should close the handle themselves.
*
* In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
* handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
* native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
* handle type used.
*
* @ingroup aux_vk
*/
VkResult
vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,126 @@
// Copyright 2019-2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Vulkan sync primitives code.
*
* @author Jakob Bornecrantz <jakob@collabora.com>
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
* @ingroup aux_vk
*/
#include <xrt/xrt_handles.h>
#include "util/u_misc.h"
#include "util/u_debug.h"
#include "vk/vk_helpers.h"
VkResult
vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence)
{
VkFence fence = VK_NULL_HANDLE;
VkResult ret;
VkFenceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
};
ret = vk->vkCreateFence(vk->device, &create_info, NULL, &fence);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkCreateFence: %s", vk_result_string(ret));
return ret;
}
#ifdef XRT_GRAPHICS_SYNC_HANDLE_IS_FD
// This is what is used on Linux Mesa when importing fences from OpenGL.
VkExternalFenceHandleTypeFlagBits handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
VkImportFenceFdInfoKHR import_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
.fence = fence,
.handleType = handleType,
.fd = native,
};
ret = vk->vkImportFenceFdKHR(vk->device, &import_info);
if (ret != VK_SUCCESS) {
vk->vkDestroyFence(vk->device, fence, NULL);
VK_ERROR(vk, "vkImportFenceFdKHR: %s", vk_result_string(ret));
return ret;
}
#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE)
//! @todo make sure this is the right one
VkExternalFenceHandleTypeFlagBits handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_WIN32_BIT;
VkImportFenceWin32HandleInfoKHR import_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_WIN32_HANDLE_INFO_KHR,
.pNext = NULL,
.fence = fence,
.flags = 0, /** @todo do we want the temporary flag? */
.handleType = handleType,
.handle = native,
.name = NULL, /* not importing by name */
};
ret = vk->vkImportFenceWin32HandleKHR(vk->device, &import_info);
if (ret != VK_SUCCESS) {
vk->vkDestroyFence(vk->device, fence, NULL);
VK_ERROR(vk, "vkImportFenceFdKHR: %s", vk_result_string(ret));
return ret;
}
#else
#error "Need port to import fence sync handles"
#endif
*out_fence = fence;
return VK_SUCCESS;
}
VkResult
vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem)
{
VkResult ret;
VkSemaphoreCreateInfo semaphore_create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
ret = vk->vkCreateSemaphore(vk->device, &semaphore_create_info, NULL, out_sem);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkCreateSemaphore: %s", vk_result_string(ret));
// Nothing to cleanup
return ret;
}
#if defined(XRT_GRAPHICS_SYNC_HANDLE_IS_FD)
VkImportSemaphoreFdInfoKHR import_semaphore_fd_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
.semaphore = *out_sem,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
.fd = native,
};
ret = vk->vkImportSemaphoreFdKHR(vk->device, &import_semaphore_fd_info);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkImportSemaphoreFdKHR: %s", vk_result_string(ret));
vk->vkDestroySemaphore(vk->device, *out_sem, NULL);
return ret;
}
#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE)
VkImportSemaphoreWin32HandleInfoKHR import_semaphore_handle_info = {
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_WIN32_HANDLE_INFO_KHR,
.semaphore = *out_sem,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT,
.handle = native,
};
ret = vk->vkImportSemaphoreWin32HandleKHR(vk->device, &import_semaphore_handle_info);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vkImportSemaphoreWin32HandleKHR: %s", vk_result_string(ret));
vk->vkDestroySemaphore(vk->device, *out_sem, NULL);
return ret;
}
#else
#error "Not implemented for this underlying handle type!"
#endif
return ret;
}