From patchwork Thu Apr 29 01:54:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lynne X-Patchwork-Id: 27469 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp991775pxb; Wed, 28 Apr 2021 18:54:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYvRTLSFhSCCUZ1kNRccD1LqPfqrPptWYg2blYc/j7mHIyxZpx54WqnvFoSvG7PvCrsRp6 X-Received: by 2002:a17:906:a887:: with SMTP id ha7mr26061049ejb.365.1619661250032; Wed, 28 Apr 2021 18:54:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619661250; cv=none; d=google.com; s=arc-20160816; b=WOPREMrus4/qkivaHWjuTqaEcY3qeJ8/O64OWZtF30UOyr2JTO7F1gvv1iMPCC1Bj3 w4bDyajws7u5QSV8CN4q6ZsvJD/4U9FsOfaKbonxynT0BjuYCIUMHp2Yi089+0TQXQUS ZyBNUZwBzXISuyG0XiikGsOePcU1PE0JfHXa/W+a6wSlZKuCf98+WgSgEfkgPWRo3u4u GpmnstrezJ2NdAnbnqJUB3t11HOeFCZRbVwkVq6DbRCqW5wfueiunfLiTANf0kEsh5zS /rH3n3jaihQp/TpvusuHWsRiU+Ddq1hz10afwrj+n+sNwWJiHQLjOs+uZIXTeao7uJDj EINg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:to:from:date :dkim-signature:delivered-to; bh=Sgx5R2Ur/rwu8Vec2hjmAc/d6Nz0G6g+f34iLP+Frso=; b=dohKhuzCcZynDPPRyEJEK8EiDaIa+hTHdZF+K2pL/VVrnag5IwpjruRU63zqwHbX8v 5uzOIfOOEibNHmqXkBMmb9cJZ74pLA1NQBJu1qFovcnaDHamrgTlZxlYazY/l0N2I6Rf cs3Hc4KkJr60sXfca5jdChcSjjE6JmVQCkDhBfS4mgIzJIafPaNNULRjEsbK224XdVYO JEgpS8s7BuDJBlXMWiLtlr7wG+ljtIOhp0LjU7Zjaq0nr3/jWmlCyZSPR+NEC1qfVz07 4lPFKPke5Ag8hQhzzGDcFTcUndzQlBxyQ+QR85a3X8xMrxQq6gFFFh5z8ee5c6KmDY5Q F4HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@lynne.ee header.s=s1 header.b=zhg1qSqK; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lynne.ee Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bi22si1442819edb.191.2021.04.28.18.54.09; Wed, 28 Apr 2021 18:54:10 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@lynne.ee header.s=s1 header.b=zhg1qSqK; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lynne.ee Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B98A668A154; Thu, 29 Apr 2021 04:54:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from w4.tutanota.de (w4.tutanota.de [81.3.6.165]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 381DD6800F2 for ; Thu, 29 Apr 2021 04:54:01 +0300 (EEST) Received: from w3.tutanota.de (unknown [192.168.1.164]) by w4.tutanota.de (Postfix) with ESMTP id D0AB2106019A for ; Thu, 29 Apr 2021 01:54:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1619661240; s=s1; d=lynne.ee; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=z6MWL4+i8LQTU8hFvXmo5q+l6WeKGqi/VZs8kk3IA1U=; b=zhg1qSqKXPhXGLQb7rKOCBLM286i298/4qybWonqgFs8usUCS/Z2eePE3ppXssSm IBWkeTXtSXYvObKkWz/TM9eJqfMAgnCS3laeK9244/72eYll9Wy8KelvClFwzf5txWW colZmEJTb+r/zwn6+ndUjE5aUoEXOdLIpCeLlY4spS0yJXEUrkBqTZeXEdnbeuEv8tx spWvU/UDnGrJxqbTxFqmTKygTuZ9A1XYbOKEVj2aY1a56oPVnOoef0F9G1Rz8PiqmZF gj1HuqYGiTgaVu46dcYspqnb8AjV2113aWBlnm8sLo3NtcSGIx+SxU+S1+3iA1HM8w8 7WrNsuItzA== Date: Thu, 29 Apr 2021 03:54:00 +0200 (CEST) From: Lynne To: FFmpeg development discussions and patches Message-ID: In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] hwcontext_vulkan: dlopen libvulkan X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 4jkij8JqZyZB While Vulkan itself went more or less the way it was expected to go, libvulkan didn't quite solve all of the opengl loader issues. It's multi-vendor, yes, but unfortunately, the code is Google/Khronos QUALITY, so suffers from big static linking issues (static linking on anything but OSX is unsupported), has bugs, and due to the prefix system used, there are 3 or so ways to type out functions. Just solve all of those problems by dlopening it. We even have nice emulation for it on Windows. Patch attached. Subject: [PATCH 2/2] hwcontext_vulkan: dlopen libvulkan While Vulkan itself went more or less the way it was expected to go, libvulkan didn't quite solve all of the opengl loader issues. It's multi-vendor, yes, but unfortunately, the code is Google/Khronos QUALITY, so suffers from big static linking issues (static linking on anything but OSX is unsupported), has bugs, and due to the prefix system used, there are 3 or so ways to type out functions. Just solve all of those problems by dlopening it. We even have nice emulation for it on Windows. --- configure | 3 ++- libavutil/hwcontext_vulkan.c | 51 +++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/configure b/configure index 820f719a32..40922f9ad6 100755 --- a/configure +++ b/configure @@ -2920,6 +2920,7 @@ nvdec_deps="ffnvcodec" vaapi_x11_deps="xlib" videotoolbox_hwaccel_deps="videotoolbox pthreads" videotoolbox_hwaccel_extralibs="-framework QuartzCore" +vulkan_deps_any="libdl LoadLibrary" xvmc_deps="X11_extensions_XvMClib_h" av1_d3d11va_hwaccel_deps="d3d11va DXVA_PicParams_AV1" @@ -6739,7 +6740,7 @@ enabled vdpau && enabled crystalhd && check_lib crystalhd "stdint.h libcrystalhd/libcrystalhd_if.h" DtsCrystalHDVersion -lcrystalhd enabled vulkan && - require_pkg_config vulkan "vulkan >= 1.1.97" "vulkan/vulkan.h" vkCreateInstance + check_cpp_condition vulkan vulkan/vulkan.h "defined VK_VERSION_1_1" if enabled x86; then case $target_os in diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c index cfaf554af9..209439d427 100644 --- a/libavutil/hwcontext_vulkan.c +++ b/libavutil/hwcontext_vulkan.c @@ -26,8 +26,11 @@ #include "hwcontext_internal.h" #include "hwcontext_vulkan.h" -VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, - const char *name); +#ifdef _WIN32 +#include "compat/w32dlfcn.h" +#else +#include +#endif #if CONFIG_LIBDRM #include @@ -186,7 +189,8 @@ typedef struct VulkanExecCtx { } VulkanExecCtx; typedef struct VulkanDevicePriv { - /* Vulkan loader functions */ + /* Vulkan library and loader functions */ + void *libvulkan; VulkanFunctions vkfn; /* Properties */ @@ -369,6 +373,40 @@ static int pixfmt_is_supported(AVHWDeviceContext *dev_ctx, enum AVPixelFormat p, return 1; } +static int load_libvulkan(AVHWDeviceContext *ctx) +{ + AVVulkanDeviceContext *hwctx = ctx->hwctx; + VulkanDevicePriv *p = ctx->internal->priv; + + static const char *lib_names[] = { +#if defined(_WIN32) + "vulkan-1.dll", +#elif defined(__APPLE__) + "libvulkan.dylib", + "libvulkan.1.dylib", + "libMoltenVK.dylib", +#else + "libvulkan.so.1", + "libvulkan.so", +#endif + }; + + for (int i = 0; i < FF_ARRAY_ELEMS(lib_names); i++) { + p->libvulkan = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL); + if (p->libvulkan) + break; + } + + if (!p->libvulkan) { + av_log(ctx, AV_LOG_ERROR, "Unable to open the libvulkan library!\n"); + return AVERROR_UNKNOWN; + } + + hwctx->get_proc_addr = (PFN_vkGetInstanceProcAddr)dlsym(p->libvulkan, "vkGetInstanceProcAddr"); + + return 0; +} + static int load_functions(AVHWDeviceContext *ctx, int has_inst, int has_dev) { AVVulkanDeviceContext *hwctx = ctx->hwctx; @@ -649,7 +687,9 @@ static int create_instance(AVHWDeviceContext *ctx, AVDictionary *opts) }; if (!hwctx->get_proc_addr) { - hwctx->get_proc_addr = vkGetInstanceProcAddr; + err = load_libvulkan(ctx); + if (err < 0) + return err; } err = load_functions(ctx, 0, 0); @@ -1188,6 +1228,9 @@ static void vulkan_device_free(AVHWDeviceContext *ctx) vk->DestroyInstance(hwctx->inst, hwctx->alloc); + if (p->libvulkan) + dlclose(p->libvulkan); + for (int i = 0; i < hwctx->nb_enabled_inst_extensions; i++) av_free((void *)hwctx->enabled_inst_extensions[i]); av_free((void *)hwctx->enabled_inst_extensions);