diff mbox series

[FFmpeg-devel] hwcontext_vulkan: properly enable sync2 and make prepare_frame compatible

Message ID Mzk6ZCB--3-2@lynne.ee
State New
Headers show
Series [FFmpeg-devel] hwcontext_vulkan: properly enable sync2 and make prepare_frame compatible | expand

Checks

Context Check Description
andriy/configure_aarch64_jetson warning Failed to apply patch
andriy/configure_armv7_RPi4 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Lynne April 3, 2022, 3:49 p.m. UTC
This was meant to be present before the first patch.
I'll move the fixes to the first patch when I apply/resubmit.

Patch attached.
diff mbox series

Patch

From e09d026eedbd4c30f4bf3cfa9ffe547906781d3b Mon Sep 17 00:00:00 2001
From: Lynne <dev@lynne.ee>
Date: Sun, 3 Apr 2022 17:47:58 +0200
Subject: [PATCH] hwcontext_vulkan: properly enable sync2 and make
 prepare_frame compatible

---
 libavutil/hwcontext_vulkan.c | 52 +++++++++++++++++++++++++++---------
 libavutil/vulkan_functions.h |  4 +++
 2 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 5bd0cab7ef..c8de358fa3 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -347,7 +347,7 @@  static const VulkanOptExtension optional_device_exts[] = {
     /* Misc or required by other extensions */
     { VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,                  FF_VK_EXT_NO_FLAG                },
     { VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,         FF_VK_EXT_NO_FLAG                },
-    { VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME,                FF_VK_EXT_NO_FLAG                },
+    { VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME,                FF_VK_EXT_SYNC2                  },
 
     /* Imports/exports */
     { VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,               FF_VK_EXT_EXTERNAL_FD_MEMORY     },
@@ -1340,9 +1340,13 @@  static int vulkan_device_create_internal(AVHWDeviceContext *ctx,
     VkPhysicalDeviceTimelineSemaphoreFeatures timeline_features = {
         .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES,
     };
+    VkPhysicalDeviceSynchronization2Features sync2_features = {
+        .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SYNCHRONIZATION_2_FEATURES,
+        .pNext = &timeline_features,
+    };
     VkPhysicalDeviceVulkan12Features dev_features_1_2 = {
         .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
-        .pNext = &timeline_features,
+        .pNext = &sync2_features,
     };
     VkPhysicalDeviceVulkan11Features dev_features_1_1 = {
         .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES,
@@ -1352,10 +1356,8 @@  static int vulkan_device_create_internal(AVHWDeviceContext *ctx,
         .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
         .pNext = &dev_features_1_1,
     };
-
     VkDeviceCreateInfo dev_info = {
-        .sType                = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
-        .pNext                = &hwctx->device_features,
+        .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
     };
 
     hwctx->device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
@@ -1393,6 +1395,9 @@  static int vulkan_device_create_internal(AVHWDeviceContext *ctx,
     }
     p->device_features_1_2.timelineSemaphore = 1;
 
+    dev_info.pNext = &sync2_features;
+    sync2_features.pNext = &hwctx->device_features;
+
     /* Setup queue family */
     if ((err = setup_queue_families(ctx, &dev_info)))
         goto end;
@@ -2054,14 +2059,37 @@  static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx,
         frame->access[i] = img_bar[i].dstAccessMask;
     }
 
-    dep_info = (VkDependencyInfo) {
-        .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
-        .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT,
-        .pImageMemoryBarriers = img_bar,
-        .imageMemoryBarrierCount = planes,
-    };
+    if (p->extensions & FF_VK_EXT_SYNC2) {
+        dep_info = (VkDependencyInfo) {
+            .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
+            .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT,
+            .pImageMemoryBarriers = img_bar,
+            .imageMemoryBarrierCount = planes,
+        };
 
-    vk->CmdPipelineBarrier2KHR(get_buf_exec_ctx(hwfc, ectx), &dep_info);
+        vk->CmdPipelineBarrier2KHR(get_buf_exec_ctx(hwfc, ectx), &dep_info);
+    } else {
+        VkImageMemoryBarrier img_bar_old[AV_NUM_DATA_POINTERS] = { 0 };
+
+        for (int i = 0; i < planes; i++) {
+            img_bar_old[i] = (VkImageMemoryBarrier) {
+                .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
+                .srcAccessMask       = img_bar[i].srcAccessMask,
+                .dstAccessMask       = img_bar[i].dstAccessMask,
+                .oldLayout           = img_bar[i].oldLayout,
+                .newLayout           = img_bar[i].newLayout,
+                .srcQueueFamilyIndex = img_bar[i].srcQueueFamilyIndex,
+                .dstQueueFamilyIndex = img_bar[i].dstQueueFamilyIndex,
+                .image               = img_bar[i].image,
+                .subresourceRange    = img_bar[i].subresourceRange,
+            };
+        }
+
+        vk->CmdPipelineBarrier(get_buf_exec_ctx(hwfc, ectx),
+                               VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                               VK_PIPELINE_STAGE_TRANSFER_BIT,
+                               0, 0, NULL, 0, NULL, planes, img_bar_old);
+    }
 
     err = submit_exec_ctx(hwfc, ectx, &s_info, frame, 0);
     vkfc->unlock_frame(hwfc, &tmp);
diff --git a/libavutil/vulkan_functions.h b/libavutil/vulkan_functions.h
index d15a5d9a42..72d242b9a6 100644
--- a/libavutil/vulkan_functions.h
+++ b/libavutil/vulkan_functions.h
@@ -37,6 +37,7 @@  typedef enum FFVulkanExtensions {
     FF_VK_EXT_EXTERNAL_WIN32_MEMORY  = 1ULL <<  6, /* VK_KHR_external_memory_win32 */
     FF_VK_EXT_EXTERNAL_WIN32_SEM     = 1ULL <<  7, /* VK_KHR_external_semaphore_win32 */
 #endif
+    FF_VK_EXT_SYNC2                  = 1ULL <<  8, /* VK_KHR_synchronization2 */
 
     FF_VK_EXT_NO_FLAG                = 1ULL << 31,
 } FFVulkanExtensions;
@@ -145,6 +146,9 @@  typedef enum FFVulkanExtensions {
     MACRO(1, 1, FF_VK_EXT_NO_FLAG,              UpdateDescriptorSetWithTemplate)         \
     MACRO(1, 1, FF_VK_EXT_NO_FLAG,              CreateDescriptorUpdateTemplate)          \
     MACRO(1, 1, FF_VK_EXT_NO_FLAG,              DestroyDescriptorUpdateTemplate)         \
+                                                                                           \
+    /* sync2 */                                                                            \
+    MACRO(1, 1, FF_VK_EXT_SYNC2,                CmdPipelineBarrier2KHR)                    \
                                                                                          \
     /* Pipeline */                                                                       \
     MACRO(1, 1, FF_VK_EXT_NO_FLAG,              CreatePipelineLayout)                    \
-- 
2.35.1