[Mesa-dev] [PATCH v2 8/9] anv: Use DRM sync objects for external semaphores when available

Jason Ekstrand jason at jlekstrand.net
Fri Aug 4 01:25:27 UTC 2017


---
 src/intel/vulkan/anv_batch_chain.c | 78 ++++++++++++++++++++++++++++++++++++++
 src/intel/vulkan/anv_device.c      |  1 +
 src/intel/vulkan/anv_private.h     |  8 ++++
 src/intel/vulkan/anv_queue.c       | 76 ++++++++++++++++++++++++++-----------
 4 files changed, 140 insertions(+), 23 deletions(-)

diff --git a/src/intel/vulkan/anv_batch_chain.c b/src/intel/vulkan/anv_batch_chain.c
index 7a84bbd..092f8b7 100644
--- a/src/intel/vulkan/anv_batch_chain.c
+++ b/src/intel/vulkan/anv_batch_chain.c
@@ -957,6 +957,11 @@ struct anv_execbuf {
 
    /* Allocated length of the 'objects' and 'bos' arrays */
    uint32_t                                  array_length;
+
+   uint32_t                                  fence_count;
+   uint32_t                                  fence_array_length;
+   struct drm_i915_gem_exec_fence *          fences;
+   struct anv_syncobj **                     syncobjs;
 };
 
 static void
@@ -971,6 +976,8 @@ anv_execbuf_finish(struct anv_execbuf *exec,
 {
    vk_free(alloc, exec->objects);
    vk_free(alloc, exec->bos);
+   vk_free(alloc, exec->fences);
+   vk_free(alloc, exec->syncobjs);
 }
 
 static VkResult
@@ -1061,6 +1068,55 @@ anv_execbuf_add_bo(struct anv_execbuf *exec,
    return VK_SUCCESS;
 }
 
+static VkResult
+anv_execbuf_add_syncobj(struct anv_execbuf *exec,
+                        struct anv_syncobj *syncobj,
+                        uint32_t flags,
+                        const VkAllocationCallbacks *alloc)
+{
+   assert(flags != 0);
+
+   if (syncobj->index < exec->fence_count &&
+       exec->syncobjs[syncobj->index] == syncobj) {
+      /* We've already added this syncobj to the fence array.
+       *
+       * It's technically allowed by the Vulkan API to have a given submit
+       * wait on a semaphore and then immediately signal it again.  This will
+       * turn into a fence array entry with both WAIT and SIGNAL set.  The
+       * kernel will handle this just fine by waiting and then signaling.
+       */
+      exec->fences[syncobj->index].flags |= flags;
+   } else {
+      if (exec->fence_count >= exec->fence_array_length) {
+         uint32_t new_len = MAX2(exec->fence_array_length * 2, 64);
+
+         exec->fences = vk_realloc(alloc, exec->fences,
+                                   new_len * sizeof(*exec->fences),
+                                   8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+         if (exec->fences == NULL)
+            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
+
+         exec->syncobjs = vk_realloc(alloc, exec->syncobjs,
+                                     new_len * sizeof(*exec->syncobjs),
+                                     8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+         if (exec->syncobjs == NULL)
+            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
+
+         exec->fence_array_length = new_len;
+      }
+
+      exec->syncobjs[exec->fence_count] = syncobj;
+      exec->fences[exec->fence_count] = (struct drm_i915_gem_exec_fence) {
+         .handle = syncobj->handle,
+         .flags = flags,
+      };
+
+      exec->fence_count++;
+   }
+
+   return VK_SUCCESS;
+}
+
 static void
 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
                               struct anv_reloc_list *list)
@@ -1448,6 +1504,14 @@ anv_cmd_buffer_execbuf(struct anv_device *device,
          impl->fd = -1;
          break;
 
+      case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
+         result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
+                                          I915_EXEC_FENCE_WAIT,
+                                          &device->alloc);
+         if (result != VK_SUCCESS)
+            return result;
+         break;
+
       default:
          break;
       }
@@ -1484,6 +1548,14 @@ anv_cmd_buffer_execbuf(struct anv_device *device,
          need_out_fence = true;
          break;
 
+      case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
+         result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
+                                          I915_EXEC_FENCE_SIGNAL,
+                                          &device->alloc);
+         if (result != VK_SUCCESS)
+            return result;
+         break;
+
       default:
          break;
       }
@@ -1497,6 +1569,12 @@ anv_cmd_buffer_execbuf(struct anv_device *device,
       setup_empty_execbuf(&execbuf, device);
    }
 
+   if (execbuf.fence_count > 0) {
+      execbuf.execbuf.flags |= I915_EXEC_FENCE_ARRAY;
+      execbuf.execbuf.num_cliprects = execbuf.fence_count;
+      execbuf.execbuf.cliprects_ptr = (uintptr_t) execbuf.fences;
+   }
+
    if (in_fence != -1) {
       execbuf.execbuf.flags |= I915_EXEC_FENCE_IN;
       execbuf.execbuf.rsvd2 |= (uint32_t)in_fence;
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 252ca95..694825d 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -338,6 +338,7 @@ anv_physical_device_init(struct anv_physical_device *device,
 
    device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
    device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
+   device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
 
    bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
 
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index b51905f..2e1e1e6 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -692,6 +692,7 @@ struct anv_physical_device {
     int                                         cmd_parser_version;
     bool                                        has_exec_async;
     bool                                        has_exec_fence;
+    bool                                        has_syncobj;
 
     uint32_t                                    eu_total;
     uint32_t                                    subslice_total;
@@ -1782,6 +1783,7 @@ enum anv_semaphore_type {
    ANV_SEMAPHORE_TYPE_DUMMY,
    ANV_SEMAPHORE_TYPE_BO,
    ANV_SEMAPHORE_TYPE_SYNC_FILE,
+   ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ,
 };
 
 struct anv_semaphore_impl {
@@ -1800,6 +1802,12 @@ struct anv_semaphore_impl {
        * created or because it has been used for a wait, fd will be -1.
        */
       int fd;
+
+      /* Sync object handle when type == AKV_SEMAPHORE_TYPE_DRM_SYNCOBJ.
+       * Unlike GEM BOs, DRM sync objects aren't deduplicated by the kernel on
+       * import so we don't need to bother with a userspace cache.
+       */
+      struct anv_syncobj *syncobj;
    };
 };
 
diff --git a/src/intel/vulkan/anv_queue.c b/src/intel/vulkan/anv_queue.c
index 45c0466..d540617 100644
--- a/src/intel/vulkan/anv_queue.c
+++ b/src/intel/vulkan/anv_queue.c
@@ -558,19 +558,29 @@ VkResult anv_CreateSemaphore(
       semaphore->permanent.type = ANV_SEMAPHORE_TYPE_DUMMY;
    } else if (handleTypes & VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR) {
       assert(handleTypes == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR);
+      if (device->instance->physicalDevice.has_syncobj) {
+         semaphore->permanent.type = ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ;
+         VkResult result =
+            anv_syncobj_cache_create(device, &device->syncobj_cache,
+                                     &semaphore->permanent.syncobj);
+         if (result != VK_SUCCESS) {
+            vk_free2(&device->alloc, pAllocator, semaphore);
+            return result;
+         }
+      } else {
+         semaphore->permanent.type = ANV_SEMAPHORE_TYPE_BO;
+         VkResult result = anv_bo_cache_alloc(device, &device->bo_cache,
+                                              4096, &semaphore->permanent.bo);
+         if (result != VK_SUCCESS) {
+            vk_free2(&device->alloc, pAllocator, semaphore);
+            return result;
+         }
 
-      semaphore->permanent.type = ANV_SEMAPHORE_TYPE_BO;
-      VkResult result = anv_bo_cache_alloc(device, &device->bo_cache,
-                                           4096, &semaphore->permanent.bo);
-      if (result != VK_SUCCESS) {
-         vk_free2(&device->alloc, pAllocator, semaphore);
-         return result;
+         /* If we're going to use this as a fence, we need to *not* have the
+          * EXEC_OBJECT_ASYNC bit set.
+          */
+         assert(!(semaphore->permanent.bo->flags & EXEC_OBJECT_ASYNC));
       }
-
-      /* If we're going to use this as a fence, we need to *not* have the
-       * EXEC_OBJECT_ASYNC bit set.
-       */
-      assert(!(semaphore->permanent.bo->flags & EXEC_OBJECT_ASYNC));
    } else if (handleTypes & VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR) {
       assert(handleTypes == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR);
 
@@ -606,6 +616,10 @@ anv_semaphore_impl_cleanup(struct anv_device *device,
    case ANV_SEMAPHORE_TYPE_SYNC_FILE:
       close(impl->fd);
       return;
+
+   case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
+      anv_syncobj_cache_release(device, &device->syncobj_cache, impl->syncobj);
+      return;
    }
 
    unreachable("Invalid semaphore type");
@@ -690,22 +704,31 @@ VkResult anv_ImportSemaphoreFdKHR(
    };
 
    switch (pImportSemaphoreFdInfo->handleType) {
-   case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: {
-      new_impl.type = ANV_SEMAPHORE_TYPE_BO;
+   case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
+      if (device->instance->physicalDevice.has_syncobj) {
+         new_impl.type = ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ;
 
-      VkResult result = anv_bo_cache_import(device, &device->bo_cache,
-                                            pImportSemaphoreFdInfo->fd, 4096,
-                                            &new_impl.bo);
-      if (result != VK_SUCCESS)
-         return result;
+         VkResult result =
+            anv_syncobj_cache_import(device, &device->syncobj_cache,
+                                     pImportSemaphoreFdInfo->fd,
+                                     &new_impl.syncobj);
+         if (result != VK_SUCCESS)
+            return result;
+      } else {
+         new_impl.type = ANV_SEMAPHORE_TYPE_BO;
 
-      /* If we're going to use this as a fence, we need to *not* have the
-       * EXEC_OBJECT_ASYNC bit set.
-       */
-      assert(!(new_impl.bo->flags & EXEC_OBJECT_ASYNC));
+         VkResult result = anv_bo_cache_import(device, &device->bo_cache,
+                                               pImportSemaphoreFdInfo->fd, 4096,
+                                               &new_impl.bo);
+         if (result != VK_SUCCESS)
+            return result;
 
+         /* If we're going to use this as a fence, we need to *not* have the
+          * EXEC_OBJECT_ASYNC bit set.
+          */
+         assert(!(new_impl.bo->flags & EXEC_OBJECT_ASYNC));
+      }
       break;
-   }
 
    case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR:
       new_impl = (struct anv_semaphore_impl) {
@@ -782,6 +805,13 @@ VkResult anv_GetSemaphoreFdKHR(
       impl->fd = -1;
       return VK_SUCCESS;
 
+   case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
+      result = anv_syncobj_cache_export(device, &device->syncobj_cache,
+                                        impl->syncobj, pFd);
+      if (result != VK_SUCCESS)
+         return result;
+      break;
+
    default:
       return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
    }
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list