[Mesa-dev] [PATCH 7/7] anv: Implement VK_KHX_external_memory_fd
Jason Ekstrand
jason at jlekstrand.net
Mon Feb 27 18:25:05 UTC 2017
Co-authored-with: Chad Versace <chadversary at chromium.org>
---
src/intel/vulkan/anv_device.c | 90 +++++++++++++++++++++++++++++----
src/intel/vulkan/anv_entrypoints_gen.py | 1 +
src/intel/vulkan/anv_formats.c | 38 +++++++++++++-
3 files changed, 118 insertions(+), 11 deletions(-)
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 024e19f..ec88547 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -310,6 +310,10 @@ static const VkExtensionProperties device_extensions[] = {
.extensionName = VK_KHX_EXTERNAL_MEMORY_EXTENSION_NAME,
.specVersion = 1,
},
+ {
+ .extensionName = VK_KHX_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
+ .specVersion = 1,
+ },
};
static void *
@@ -1400,7 +1404,7 @@ VkResult anv_AllocateMemory(
{
ANV_FROM_HANDLE(anv_device, device, _device);
struct anv_device_memory *mem;
- VkResult result;
+ VkResult result = VK_SUCCESS;
assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
@@ -1418,18 +1422,50 @@ VkResult anv_AllocateMemory(
if (mem == NULL)
return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
- /* The kernel is going to give us whole pages anyway */
- uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
-
- result = anv_bo_init_new(&mem->bo, device, alloc_size);
- if (result != VK_SUCCESS)
- goto fail;
-
mem->type_index = pAllocateInfo->memoryTypeIndex;
-
mem->map = NULL;
mem->map_size = 0;
+ const VkImportMemoryFdInfoKHX *fd_info =
+ vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHX);
+
+ /* The Vulkan spec permits handleType to be 0, in which case the struct is
+ * ignored.
+ */
+ if (fd_info && fd_info->handleType) {
+ /* At the moment, we only support the OPAQUE_FD memory type which is
+ * just a GEM buffer.
+ */
+ assert(fd_info->handleType ==
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX);
+
+ uint32_t gem_handle = anv_gem_fd_to_handle(device, fd_info->fd);
+ if (!gem_handle) {
+ result = vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
+ goto fail;
+ }
+
+ /* From the Vulkan spec:
+ *
+ * "Importing memory from a file descriptor transfers ownership of
+ * the file descriptor from the application to the Vulkan
+ * implementation. The application must not perform any operations on
+ * the file descriptor after a successful import."
+ *
+ * If the import fails, we leave the file descriptor open.
+ */
+ close(fd_info->fd);
+
+ anv_bo_init(&mem->bo, gem_handle, pAllocateInfo->allocationSize);
+ } else {
+ /* The kernel is going to give us whole pages anyway */
+ uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
+
+ result = anv_bo_init_new(&mem->bo, device, alloc_size);
+ if (result != VK_SUCCESS)
+ goto fail;
+ }
+
*pMem = anv_device_memory_to_handle(mem);
return VK_SUCCESS;
@@ -1440,6 +1476,42 @@ VkResult anv_AllocateMemory(
return result;
}
+VkResult anv_GetMemoryFdKHX(
+ VkDevice device_h,
+ VkDeviceMemory memory_h,
+ VkExternalMemoryHandleTypeFlagBitsKHX handleType,
+ int* pFd)
+{
+ ANV_FROM_HANDLE(anv_device, dev, device_h);
+ ANV_FROM_HANDLE(anv_device_memory, mem, memory_h);
+
+ /* We support only one handle type. */
+ assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX);
+
+ int fd = anv_gem_handle_to_fd(dev, mem->bo.gem_handle);
+ if (fd == -1)
+ return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
+
+ *pFd = fd;
+
+ return VK_SUCCESS;
+}
+
+VkResult anv_GetMemoryFdPropertiesKHX(
+ VkDevice device_h,
+ VkExternalMemoryHandleTypeFlagBitsKHX handleType,
+ int fd,
+ VkMemoryFdPropertiesKHX* pMemoryFdProperties)
+{
+ /* The valid usage section for this function says:
+ *
+ * "handleType must not be one of the handle types defined as opaque."
+ *
+ * Since we only handle opaque handles for now, there are no FD properties.
+ */
+ return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX;
+}
+
void anv_FreeMemory(
VkDevice _device,
VkDeviceMemory _mem,
diff --git a/src/intel/vulkan/anv_entrypoints_gen.py b/src/intel/vulkan/anv_entrypoints_gen.py
index d8816a5..530163c 100644
--- a/src/intel/vulkan/anv_entrypoints_gen.py
+++ b/src/intel/vulkan/anv_entrypoints_gen.py
@@ -39,6 +39,7 @@ supported_extensions = [
'VK_KHR_xlib_surface',
'VK_KHX_external_memory',
'VK_KHX_external_memory_capabilities',
+ 'VK_KHX_external_memory_fd',
]
# We generate a static hash table for entry point lookup
diff --git a/src/intel/vulkan/anv_formats.c b/src/intel/vulkan/anv_formats.c
index 8f230ea..4c22f10 100644
--- a/src/intel/vulkan/anv_formats.c
+++ b/src/intel/vulkan/anv_formats.c
@@ -656,6 +656,17 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties(
pImageFormatProperties);
}
+static const VkExternalMemoryPropertiesKHX prime_fd_props = {
+ /* If we can handle external, then we can both import and export it. */
+ .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT_KHX |
+ VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHX,
+ /* For the moment, let's not support mixing and matching */
+ .exportFromImportedHandleTypes =
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX,
+ .compatibleHandleTypes =
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX,
+};
+
VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
VkPhysicalDevice physicalDevice,
const VkPhysicalDeviceImageFormatInfo2KHR* pImageFormatInfo,
@@ -672,9 +683,17 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
vk_foreach_struct(ext, pImageFormatProperties->pNext) {
switch (ext->sType) {
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO_KHX: {
+ struct VkExternalImageFormatPropertiesKHX *props =
+ vk_find_struct(pImageFormatProperties->pNext,
+ EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHX);
const VkPhysicalDeviceExternalImageFormatInfoKHX *ext_info =
(const VkPhysicalDeviceExternalImageFormatInfoKHX *)ext;
- if (ext_info->handleType != 0) {
+ switch (ext_info->handleType) {
+ case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX:
+ props->externalMemoryProperties = prime_fd_props;
+ break;
+
+ default:
/* FINISHME: Support at least one external memory type for images.
*
* From the Vulkan 1.x spec:
@@ -740,8 +759,23 @@ void anv_GetPhysicalDeviceExternalBufferPropertiesKHX(
const VkPhysicalDeviceExternalBufferInfoKHX* pExternalBufferInfo,
VkExternalBufferPropertiesKHX* pExternalBufferProperties)
{
- anv_finishme("Handle external buffers");
+ switch (pExternalBufferInfo->handleType) {
+ case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX:
+ /* All of the current flags are for sparse which we don't support yet.
+ * Even when we do support it, doing sparse on external memory sounds
+ * sketchy. Also, just disallowing flags is the safe option.
+ */
+ if (pExternalBufferInfo->flags)
+ goto unsupported;
+
+ pExternalBufferProperties->externalMemoryProperties = prime_fd_props;
+ return;
+ default:
+ break;
+ }
+
+unsupported:
pExternalBufferProperties->externalMemoryProperties =
(VkExternalMemoryPropertiesKHX) {0};
}
--
2.5.0.400.gff86faf
More information about the mesa-dev
mailing list