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

Louis-Francis Ratté-Boulianne lfrb at collabora.com
Thu Aug 31 04:24:25 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      | 25 +++++++++++++++----------
 src/intel/vulkan/anv_extensions.py |  1 +
 src/intel/vulkan/anv_formats.c     | 22 +++++++++++++++++++---
 src/intel/vulkan/anv_private.h     |  4 ++++
 4 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 2e0fa19b1a..8d5a42bcd1 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -1475,11 +1475,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_KHR);
+      assert((fd_info->handleType & ~ANV_SUPPORTED_MEMORY_HANDLE_TYPES) == 0);
 
       result = anv_bo_cache_import(device, &device->bo_cache,
                                    fd_info->fd, pAllocateInfo->allocationSize,
@@ -1521,9 +1517,7 @@ VkResult anv_GetMemoryFdKHR(
 
    assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
 
-   /* We support only one handle type. */
-   assert(pGetFdInfo->handleType ==
-          VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR);
+   assert((pGetFdInfo->handleType & ~ANV_SUPPORTED_MEMORY_HANDLE_TYPES) == 0);
 
    return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
 }
@@ -1534,13 +1528,24 @@ VkResult anv_GetMemoryFdPropertiesKHR(
     int                                         fd,
     VkMemoryFdPropertiesKHR*                    pMemoryFdProperties)
 {
+   ANV_FROM_HANDLE(anv_device, device, device_h);
+
+   if (fd == -1)
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
    /* 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_KHR;
+   if (handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_MESAX)
+      return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
+
+   /* 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_extensions.py b/src/intel/vulkan/anv_extensions.py
index 4377d4b985..3abadff2dc 100644
--- a/src/intel/vulkan/anv_extensions.py
+++ b/src/intel/vulkan/anv_extensions.py
@@ -76,6 +76,7 @@ EXTENSIONS = [
     Extension('VK_KHR_xcb_surface',                       6, 'VK_USE_PLATFORM_XCB_KHR'),
     Extension('VK_KHR_xlib_surface',                      6, 'VK_USE_PLATFORM_XLIB_KHR'),
     Extension('VK_KHX_multiview',                         1, True),
+    Extension('VK_MESAX_external_memory_dma_buf',         0, True),
 ]
 
 class VkVersion:
diff --git a/src/intel/vulkan/anv_formats.c b/src/intel/vulkan/anv_formats.c
index 57202f4aca..83a0b5ad68 100644
--- a/src/intel/vulkan/anv_formats.c
+++ b/src/intel/vulkan/anv_formats.c
@@ -658,7 +658,7 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties(
                                           pImageFormatProperties);
 }
 
-static const VkExternalMemoryPropertiesKHR prime_fd_props = {
+static const VkExternalMemoryPropertiesKHR opaque_fd_props = {
    /* If we can handle external, then we can both import and export it. */
    .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT_KHR |
                              VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR,
@@ -669,6 +669,17 @@ static const VkExternalMemoryPropertiesKHR prime_fd_props = {
       VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
 };
 
+static const VkExternalMemoryPropertiesKHR dma_buf_mem_props = {
+   /* If we can handle external, then we can both import and export it. */
+   .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT_KHR |
+                             VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR,
+   /* 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,
@@ -718,8 +729,10 @@ VkResult anv_GetPhysicalDeviceImageFormatProperties2KHR(
       switch (external_info->handleType) {
       case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
          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:
           *
@@ -798,7 +811,10 @@ void anv_GetPhysicalDeviceExternalBufferPropertiesKHR(
 
    switch (pExternalBufferInfo->handleType) {
    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
-      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 55840d5325..08f69a0c8c 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1024,6 +1024,10 @@ struct anv_device_memory {
    void *                                       map;
 };
 
+#define ANV_SUPPORTED_MEMORY_HANDLE_TYPES \
+   (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR | \
+    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