FFmpeg
vulkan_loader.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVUTIL_VULKAN_LOADER_H
20 #define AVUTIL_VULKAN_LOADER_H
21 
22 #include "vulkan_functions.h"
23 
24 /* Macro to turn a function name into a loader struct */
25 #define PFN_LOAD_INFO(req_inst, req_dev, ext_flag, name) \
26  { \
27  req_inst, \
28  req_dev, \
29  offsetof(FFVulkanFunctions, name), \
30  ext_flag, \
31  { "vk"#name, "vk"#name"EXT", "vk"#name"KHR" } \
32  },
33 
34 static inline uint64_t ff_vk_extensions_to_mask(const char * const *extensions,
35  int nb_extensions)
36 {
37  static const struct ExtensionMap {
38  const char *name;
40  } extension_map[] = {
41  { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_DMABUF_MEMORY },
42  { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, FF_VK_EXT_DRM_MODIFIER_FLAGS },
43  { VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_FD_MEMORY },
44  { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_FD_SEM },
45  { VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_HOST_MEMORY },
46  { VK_EXT_DEBUG_UTILS_EXTENSION_NAME, FF_VK_EXT_DEBUG_UTILS },
47  { VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME, FF_VK_EXT_DEVICE_DRM },
48  { VK_EXT_SHADER_ATOMIC_FLOAT_EXTENSION_NAME, FF_VK_EXT_ATOMIC_FLOAT },
49  { VK_KHR_COOPERATIVE_MATRIX_EXTENSION_NAME, FF_VK_EXT_COOP_MATRIX },
50 #ifdef _WIN32
51  { VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_WIN32_MEMORY },
52  { VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_WIN32_SEM },
53 #endif
54  { VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME, FF_VK_EXT_DESCRIPTOR_BUFFER, },
55  { VK_KHR_VIDEO_QUEUE_EXTENSION_NAME, FF_VK_EXT_VIDEO_QUEUE },
56  { VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME, FF_VK_EXT_VIDEO_DECODE_QUEUE },
57  { VK_KHR_VIDEO_DECODE_H264_EXTENSION_NAME, FF_VK_EXT_VIDEO_DECODE_H264 },
58  { VK_KHR_VIDEO_DECODE_H265_EXTENSION_NAME, FF_VK_EXT_VIDEO_DECODE_H265 },
59  { "VK_MESA_video_decode_av1", FF_VK_EXT_VIDEO_DECODE_AV1 },
60  };
61 
63 
64  for (int i = 0; i < nb_extensions; i++) {
65  for (int j = 0; j < FF_ARRAY_ELEMS(extension_map); j++) {
66  if (!strcmp(extensions[i], extension_map[j].name)) {
67  mask |= extension_map[j].flag;
68  continue;
69  }
70  }
71  }
72 
73  return mask;
74 }
75 
76 /**
77  * Function loader.
78  * Vulkan function from scratch loading happens in 3 stages - the first one
79  * is before any initialization has happened, and you have neither an instance
80  * structure nor a device structure. At this stage, you can only get the bare
81  * minimals to initialize an instance.
82  * The second stage is when you have an instance. At this stage, you can
83  * initialize a VkDevice, and have an idea of what extensions each device
84  * supports.
85  * Finally, in the third stage, you can proceed and load all core functions,
86  * plus you can be sure that any extensions you've enabled during device
87  * initialization will be available.
88  */
91  uint64_t extensions_mask,
92  int has_inst, int has_dev)
93 {
94  AVVulkanDeviceContext *hwctx = ctx->hwctx;
95 
96  static const struct FunctionLoadInfo {
97  int req_inst;
98  int req_dev;
99  size_t struct_offset;
100  FFVulkanExtensions ext_flag;
101  const char *names[3];
102  } vk_load_info[] = {
104 #ifdef _WIN32
106 #endif
107  };
108 
109  for (int i = 0; i < FF_ARRAY_ELEMS(vk_load_info); i++) {
110  const struct FunctionLoadInfo *load = &vk_load_info[i];
111  PFN_vkVoidFunction fn;
112 
113  if (load->req_dev && !has_dev)
114  continue;
115  if (load->req_inst && !has_inst)
116  continue;
117 
118  for (int j = 0; j < FF_ARRAY_ELEMS(load->names); j++) {
119  const char *name = load->names[j];
120 
121  if (load->req_dev)
122  fn = vk->GetDeviceProcAddr(hwctx->act_dev, name);
123  else if (load->req_inst)
124  fn = hwctx->get_proc_addr(hwctx->inst, name);
125  else
126  fn = hwctx->get_proc_addr(NULL, name);
127 
128  if (fn)
129  break;
130  }
131 
132  if (!fn && ((extensions_mask &~ FF_VK_EXT_NO_FLAG) & load->ext_flag)) {
133  av_log(ctx, AV_LOG_ERROR, "Loader error, function \"%s\" indicated "
134  "as supported, but got NULL function pointer!\n", load->names[0]);
135  return AVERROR_EXTERNAL;
136  }
137 
138  *(PFN_vkVoidFunction *)((uint8_t *)vk + load->struct_offset) = fn;
139  }
140 
141  return 0;
142 }
143 
144 #endif /* AVUTIL_VULKAN_LOADER_H */
FF_VK_EXT_NO_FLAG
@ FF_VK_EXT_NO_FLAG
Definition: vulkan_functions.h:50
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
FF_VK_EXT_VIDEO_DECODE_H264
@ FF_VK_EXT_VIDEO_DECODE_H264
Definition: vulkan_functions.h:44
FF_VK_EXT_VIDEO_DECODE_AV1
@ FF_VK_EXT_VIDEO_DECODE_AV1
Definition: vulkan_functions.h:46
AVVulkanDeviceContext::get_proc_addr
PFN_vkGetInstanceProcAddr get_proc_addr
Pointer to the instance-provided vkGetInstanceProcAddr loading function.
Definition: hwcontext_vulkan.h:55
FF_VK_EXT_DESCRIPTOR_BUFFER
@ FF_VK_EXT_DESCRIPTOR_BUFFER
Definition: vulkan_functions.h:40
fn
#define fn(a)
Definition: adynamicequalizer_template.c:61
AVVulkanDeviceContext::inst
VkInstance inst
Vulkan instance.
Definition: hwcontext_vulkan.h:60
FF_VK_EXT_VIDEO_DECODE_H265
@ FF_VK_EXT_VIDEO_DECODE_H265
Definition: vulkan_functions.h:45
FF_VK_EXT_VIDEO_DECODE_QUEUE
@ FF_VK_EXT_VIDEO_DECODE_QUEUE
Definition: vulkan_functions.h:43
FF_VK_EXT_COOP_MATRIX
@ FF_VK_EXT_COOP_MATRIX
Definition: vulkan_functions.h:48
FN_LIST_WIN32
#define FN_LIST_WIN32(MACRO)
Definition: vulkan_functions.h:217
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
FF_VK_EXT_VIDEO_QUEUE
@ FF_VK_EXT_VIDEO_QUEUE
Definition: vulkan_functions.h:42
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
mask
static const uint16_t mask[17]
Definition: lzw.c:38
ff_vk_load_functions
static int ff_vk_load_functions(AVHWDeviceContext *ctx, FFVulkanFunctions *vk, uint64_t extensions_mask, int has_inst, int has_dev)
Function loader.
Definition: vulkan_loader.h:89
PFN_LOAD_INFO
#define PFN_LOAD_INFO(req_inst, req_dev, ext_flag, name)
Definition: vulkan_loader.h:25
FN_LIST
#define FN_LIST(MACRO)
Definition: vulkan_functions.h:54
FF_VK_EXT_EXTERNAL_HOST_MEMORY
@ FF_VK_EXT_EXTERNAL_HOST_MEMORY
Definition: vulkan_functions.h:34
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVVulkanDeviceContext
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_vulkan.h:44
NULL
#define NULL
Definition: coverity.c:32
FF_VK_EXT_DRM_MODIFIER_FLAGS
@ FF_VK_EXT_DRM_MODIFIER_FLAGS
Definition: vulkan_functions.h:31
FF_VK_EXT_EXTERNAL_DMABUF_MEMORY
@ FF_VK_EXT_EXTERNAL_DMABUF_MEMORY
Definition: vulkan_functions.h:30
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
flag
#define flag(name)
Definition: cbs_av1.c:466
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
FF_VK_EXT_EXTERNAL_FD_MEMORY
@ FF_VK_EXT_EXTERNAL_FD_MEMORY
Definition: vulkan_functions.h:32
vulkan_functions.h
FF_VK_EXT_DEBUG_UTILS
@ FF_VK_EXT_DEBUG_UTILS
Definition: vulkan_functions.h:35
FFVulkanExtensions
FFVulkanExtensions
Definition: vulkan_functions.h:29
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:70
FF_VK_EXT_EXTERNAL_FD_SEM
@ FF_VK_EXT_EXTERNAL_FD_SEM
Definition: vulkan_functions.h:33
FF_VK_EXT_DEVICE_DRM
@ FF_VK_EXT_DEVICE_DRM
Definition: vulkan_functions.h:41
FF_VK_EXT_ATOMIC_FLOAT
@ FF_VK_EXT_ATOMIC_FLOAT
Definition: vulkan_functions.h:47
ff_vk_extensions_to_mask
static uint64_t ff_vk_extensions_to_mask(const char *const *extensions, int nb_extensions)
Definition: vulkan_loader.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FFVulkanFunctions
Definition: vulkan_functions.h:226