diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c index c2a3b5cfa3..4316886ca4 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, @@ -816,7 +817,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 +1869,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 */