[Mesa-dev] [RFC v2 16/23] RFC: anv: Implement VK_MESAX_external_memory_dma_buf

Louis-Francis Ratté-Boulianne lfrb at collabora.com
Fri Jul 14 04:59:14 UTC 2017


From: Chad Versace <chadversary at chromium.org>

For now, we support dma_bufs only for VkBuffers.  The
VK_MESAX_external_memory_dma_buf spec allows us to support dma_buf
VkImages, but we choose to defer that support until
VK_MESAX_external_image_dma_buf.
---
 src/intel/vulkan/anv_device.c           | 28 +++++++++++++++++++---------
 src/intel/vulkan/anv_entrypoints_gen.py |  1 +
 src/intel/vulkan/anv_formats.c          | 22 +++++++++++++++++++---
 src/intel/vulkan/anv_private.h          |  4 ++++
 4 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 3dc55dbb8d..4dd54e9b17 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -496,6 +496,10 @@ static const VkExtensionProperties device_extensions[] = {
       .extensionName = VK_KHX_MULTIVIEW_EXTENSION_NAME,
       .specVersion = 1,
    },
+   {
+      .extensionName = VK_MESAX_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
+      .specVersion = 0,
+   },
 };
 
 static void *
@@ -1578,11 +1582,7 @@ VkResult anv_AllocateMemory(
     * 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);
+      assert((fd_info->handleType & ~ANV_SUPPORTED_MEMORY_HANDLE_TYPES) == 0);
 
       result = anv_bo_cache_import(device, &device->bo_cache,
                                    fd_info->fd, pAllocateInfo->allocationSize,
@@ -1623,8 +1623,7 @@ VkResult anv_GetMemoryFdKHX(
    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);
+   assert((handleType & ~ANV_SUPPORTED_MEMORY_HANDLE_TYPES) == 0);
 
    return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
 }
@@ -1635,13 +1634,24 @@ VkResult anv_GetMemoryFdPropertiesKHX(
     int                                         fd,
     VkMemoryFdPropertiesKHX*                    pMemoryFdProperties)
 {
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   if (fd == -1)
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX;
+
    /* 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.
+    * The only non-opaque fd type we support is dma_buf.
     */
-   return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX;
+   if (handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX)
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX;
+
+   /* We support exactly one memory type on LLC, two on non-LLC.  */
+   pMemoryFdProperties->memoryTypeBits = device->info.has_llc ? 1 : 3;
+
+   return VK_SUCCESS;
 }
 
 void anv_FreeMemory(
diff --git a/src/intel/vulkan/anv_entrypoints_gen.py b/src/intel/vulkan/anv_entrypoints_gen.py
index ecddaae818..c62a3bed64 100644
--- a/src/intel/vulkan/anv_entrypoints_gen.py
+++ b/src/intel/vulkan/anv_entrypoints_gen.py
@@ -54,6 +54,7 @@ SUPPORTED_EXTENSIONS = [
     'VK_KHX_external_semaphore_capabilities',
     'VK_KHX_external_semaphore_fd',
     'VK_KHX_multiview',
+    'VK_MESAX_external_memory_dma_buf',
 ]
 
 # 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 813f781927..2f5b7f2518 100644
--- a/src/intel/vulkan/anv_formats.c
+++ b/src/intel/vulkan/anv_formats.c
@@ -657,7 +657,7 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties(
                                           pImageFormatProperties);
 }
 
-static const VkExternalMemoryPropertiesKHX prime_fd_props = {
+static const VkExternalMemoryPropertiesKHX opaque_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,
@@ -668,6 +668,17 @@ static const VkExternalMemoryPropertiesKHX prime_fd_props = {
       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX,
 };
 
+static const VkExternalMemoryPropertiesKHX dma_buf_mem_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_DMA_BUF_BIT_MESAX,
+   .compatibleHandleTypes =
+      VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX,
+};
+
 VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
     VkPhysicalDevice                            physicalDevice,
     const VkPhysicalDeviceImageFormatInfo2KHR*  base_info,
@@ -717,8 +728,10 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
       switch (external_info->handleType) {
       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX:
          if (external_props)
-            external_props->externalMemoryProperties = prime_fd_props;
+            external_props->externalMemoryProperties = opaque_fd_props;
          break;
+      case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX:
+         /* Fallthrough. We support dma_buf for VkBuffer but not yet VkImage. */
       default:
          /* From the Vulkan 1.0.42 spec:
           *
@@ -797,7 +810,10 @@ void anv_GetPhysicalDeviceExternalBufferPropertiesKHX(
 
    switch (pExternalBufferInfo->handleType) {
    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX:
-      pExternalBufferProperties->externalMemoryProperties = prime_fd_props;
+      pExternalBufferProperties->externalMemoryProperties = opaque_fd_props;
+      return;
+   case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX:
+      pExternalBufferProperties->externalMemoryProperties = dma_buf_mem_props;
       return;
    default:
       goto unsupported;
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 0e9fd26348..15e14ec8df 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1037,6 +1037,10 @@ struct anv_device_memory {
    void *                                       map;
 };
 
+#define ANV_SUPPORTED_MEMORY_HANDLE_TYPES \
+   (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHX | \
+    VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX)
+
 /**
  * Header for Vertex URB Entry (VUE)
  */
-- 
2.13.0



More information about the mesa-dev mailing list