[FFmpeg-devel] [PATCH 3/3] avutil/hwcontext_vulkan: add frames_sync() implementation

Niklas Haas ffmpeg at haasn.xyz
Sat Mar 1 18:46:21 EET 2025


From: Niklas Haas <git at haasn.dev>

Using a new internal function to wait explicitly for all execution pools
to be done executing their internal commands.
---
 libavutil/hwcontext_vulkan.c | 15 +++++++++++++++
 libavutil/vulkan.c           | 10 ++++++++++
 libavutil/vulkan.h           |  5 +++++
 3 files changed, 30 insertions(+)

diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 10521ce685..91f2db44cb 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -2702,6 +2702,18 @@ static void vulkan_frames_uninit(AVHWFramesContext *hwfc)
     av_buffer_pool_uninit(&fp->tmp);
 }
 
+static int vulkan_frames_sync(AVHWFramesContext *hwfc)
+{
+    VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+    VulkanFramesPriv *fp = hwfc->hwctx;
+
+    ff_vk_exec_pool_wait(&p->vkctx, &fp->compute_exec);
+    ff_vk_exec_pool_wait(&p->vkctx, &fp->upload_exec);
+    ff_vk_exec_pool_wait(&p->vkctx, &fp->download_exec);
+
+    return 0;
+}
+
 static int vulkan_frames_init(AVHWFramesContext *hwfc)
 {
     int err;
@@ -4268,6 +4280,8 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc,
         ff_vk_exec_wait(&p->vkctx, exec);
         if (!host_mapped)
             err = copy_buffer_data(hwfc, bufs[0], swf, region, planes, 0);
+    } else if (upload) {
+        ff_vk_exec_wait(&p->vkctx, exec);
     }
 
 end:
@@ -4464,6 +4478,7 @@ const HWContextType ff_hwcontext_type_vulkan = {
     .frames_init            = vulkan_frames_init,
     .frames_get_buffer      = vulkan_get_buffer,
     .frames_uninit          = vulkan_frames_uninit,
+    .frames_sync            = vulkan_frames_sync,
 
     .transfer_get_formats   = vulkan_transfer_get_formats,
     .transfer_data_to       = vulkan_transfer_data_to,
diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c
index 1d18bf07c1..f4c51c92d6 100644
--- a/libavutil/vulkan.c
+++ b/libavutil/vulkan.c
@@ -230,6 +230,16 @@ AVVulkanDeviceQueueFamily *ff_vk_qf_find(FFVulkanContext *s,
     return NULL;
 }
 
+void ff_vk_exec_pool_wait(FFVulkanContext *s, FFVkExecPool *pool)
+{
+    FFVulkanFunctions *vk = &s->vkfn;
+    for (int i = 0; i < pool->pool_size; i++) {
+        FFVkExecContext *e = &pool->contexts[i];
+        if (e->had_submission)
+            ff_vk_exec_wait(s, e);
+    }
+}
+
 void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
 {
     FFVulkanFunctions *vk = &s->vkfn;
diff --git a/libavutil/vulkan.h b/libavutil/vulkan.h
index 617df952c4..7f4522bf16 100644
--- a/libavutil/vulkan.h
+++ b/libavutil/vulkan.h
@@ -408,6 +408,11 @@ int ff_vk_exec_pool_init(FFVulkanContext *s, AVVulkanDeviceQueueFamily *qf,
                          const void *query_create_pnext);
 void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool);
 
+/**
+ * Wait until all prior submissions have finished.
+ */
+void ff_vk_exec_pool_wait(FFVulkanContext *s, FFVkExecPool *pool);
+
 /**
  * Retrieve an execution pool. Threadsafe.
  */
-- 
2.47.0



More information about the ffmpeg-devel mailing list