diff mbox series

[FFmpeg-devel,1/2] vulkan: add a ff_vk_init function

Message ID 20240901024143.438584-1-dev@lynne.ee
State New
Headers show
Series [FFmpeg-devel,1/2] vulkan: add a ff_vk_init function | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Lynne Sept. 1, 2024, 2:41 a.m. UTC
This function sets the class correctly, and calls functions
that all users have to call anyway.
---
 libavutil/vulkan.c | 62 +++++++++++++++++++++++++++++++++++++++++++---
 libavutil/vulkan.h | 12 ++++++++-
 2 files changed, 69 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c
index c2a3b5cfa3..3e3c087aff 100644
--- a/libavutil/vulkan.c
+++ b/libavutil/vulkan.c
@@ -22,6 +22,7 @@ 
 #include "mem.h"
 
 #include "vulkan.h"
+#include "libavutil/vulkan_loader.h"
 
 const VkComponentMapping ff_comp_identity_map = {
     .r = VK_COMPONENT_SWIZZLE_IDENTITY,
@@ -443,14 +444,13 @@  VkResult ff_vk_exec_get_query(FFVulkanContext *s, FFVkExecContext *e,
     int64_t res = 0;
     VkQueryResultFlags qf = 0;
 
-    if (!e->had_submission)
-        return VK_INCOMPLETE;
-
     if (!e->query_data) {
         av_log(s, AV_LOG_ERROR, "Requested a query with a NULL query_data pointer!\n");
         return VK_INCOMPLETE;
     }
 
+    qf |= e->had_submission ?
+          VK_QUERY_RESULT_WAIT_BIT : 0x0;
     qf |= pool->query_64bit ?
           VK_QUERY_RESULT_64_BIT : 0x0;
     qf |= pool->query_statuses ?
@@ -816,7 +816,7 @@  int ff_vk_alloc_mem(FFVulkanContext *s, VkMemoryRequirements *req,
     }
 
     if (index < 0) {
-        av_log(s->device, AV_LOG_ERROR, "No memory type found for flags 0x%x\n",
+        av_log(s, AV_LOG_ERROR, "No memory type found for flags 0x%x\n",
                req_flags);
         return AVERROR(EINVAL);
     }
@@ -1868,5 +1868,59 @@  void ff_vk_uninit(FFVulkanContext *s)
     av_freep(&s->video_props);
     av_freep(&s->coop_mat_props);
 
+    av_buffer_unref(&s->device_ref);
     av_buffer_unref(&s->frames_ref);
 }
+
+int ff_vk_init(FFVulkanContext *s, void *log_parent,
+               AVBufferRef *device_ref, AVBufferRef *frames_ref)
+{
+    int err;
+
+    static const AVClass vulkan_context_class = {
+        .class_name       = "vk",
+        .version          = LIBAVUTIL_VERSION_INT,
+        .parent_log_context_offset = offsetof(FFVulkanContext, log_parent),
+    };
+
+    memset(s, 0, sizeof(*s));
+    s->log_parent = log_parent;
+    s->class      = &vulkan_context_class;
+
+    if (frames_ref) {
+        s->frames_ref = av_buffer_ref(frames_ref);
+        if (!s->frames_ref)
+            return AVERROR(ENOMEM);
+
+        s->frames = (AVHWFramesContext *)s->frames_ref->data;
+        s->hwfc = s->frames->hwctx;
+
+        device_ref = s->frames->device_ref;
+    }
+
+    s->device_ref = av_buffer_ref(device_ref);
+    if (!s->device_ref) {
+        ff_vk_uninit(s);
+        return AVERROR(ENOMEM);
+    }
+
+    s->device = (AVHWDeviceContext *)s->device_ref->data;
+    s->hwctx = s->device->hwctx;
+
+    s->extensions = ff_vk_extensions_to_mask(s->hwctx->enabled_dev_extensions,
+                                             s->hwctx->nb_enabled_dev_extensions);
+
+    err = ff_vk_load_functions(s->device, &s->vkfn, s->extensions, 1, 1);
+    if (err < 0) {
+        ff_vk_uninit(s);
+        return err;
+    }
+
+    err = ff_vk_load_props(s);
+    if (err < 0) {
+        ff_vk_uninit(s);
+        return err;
+    }
+
+    return 0;
+}
diff --git a/libavutil/vulkan.h b/libavutil/vulkan.h
index 05bd71ae45..371de8ab51 100644
--- a/libavutil/vulkan.h
+++ b/libavutil/vulkan.h
@@ -227,7 +227,8 @@  typedef struct FFVkExecPool {
 } FFVkExecPool;
 
 typedef struct FFVulkanContext {
-    const AVClass *class; /* Filters and encoders use this */
+    const AVClass *class;
+    void *log_parent;
 
     FFVulkanFunctions     vkfn;
     FFVulkanExtensions    extensions;
@@ -251,6 +252,7 @@  typedef struct FFVulkanContext {
     VkPhysicalDeviceVulkan12Features feats_12;
     VkPhysicalDeviceFeatures2 feats;
 
+    AVBufferRef           *device_ref;
     AVHWDeviceContext     *device;
     AVVulkanDeviceContext *hwctx;
 
@@ -303,6 +305,14 @@  static inline void ff_vk_link_struct(void *chain, const void *in)
 /* Identity mapping - r = r, b = b, g = g, a = a */
 extern const VkComponentMapping ff_comp_identity_map;
 
+/**
+ * Initializes the AVClass, in case this context is not used
+ * as the main user's context.
+ * May use either a frames context reference, or a device context reference.
+ */
+int ff_vk_init(FFVulkanContext *s, void *log_parent,
+               AVBufferRef *device_ref, AVBufferRef *frames_ref);
+
 /**
  * Converts Vulkan return values to strings
  */