FFmpeg
hwcontext_vulkan.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_HWCONTEXT_VULKAN_H
20 #define AVUTIL_HWCONTEXT_VULKAN_H
21 
22 #if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
23 #define VK_USE_PLATFORM_WIN32_KHR
24 #endif
25 #include <vulkan/vulkan.h>
26 
27 #include "pixfmt.h"
28 #include "frame.h"
29 #include "hwcontext.h"
30 
31 typedef struct AVVkFrame AVVkFrame;
32 
33 /**
34  * @file
35  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
36  *
37  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
38  * with the data pointer set to an AVVkFrame.
39  */
40 
41 /**
42  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
43  * All of these can be set before init to change what the context uses
44  */
45 typedef struct AVVulkanDeviceContext {
46  /**
47  * Custom memory allocator, else NULL
48  */
49  const VkAllocationCallbacks *alloc;
50 
51  /**
52  * Pointer to the instance-provided vkGetInstanceProcAddr loading function.
53  * If NULL, will pick either libvulkan or libvolk, depending on libavutil's
54  * compilation settings, and set this field.
55  */
56  PFN_vkGetInstanceProcAddr get_proc_addr;
57 
58  /**
59  * Vulkan instance. Must be at least version 1.3.
60  */
61  VkInstance inst;
62 
63  /**
64  * Physical device
65  */
66  VkPhysicalDevice phys_dev;
67 
68  /**
69  * Active device
70  */
71  VkDevice act_dev;
72 
73  /**
74  * This structure should be set to the set of features that present and enabled
75  * during device creation. When a device is created by FFmpeg, it will default to
76  * enabling all that are present of the shaderImageGatherExtended,
77  * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
78  */
79  VkPhysicalDeviceFeatures2 device_features;
80 
81  /**
82  * Enabled instance extensions.
83  * If supplying your own device context, set this to an array of strings, with
84  * each entry containing the specified Vulkan extension string to enable.
85  * Duplicates are possible and accepted.
86  * If no extensions are enabled, set these fields to NULL, and 0 respectively.
87  */
88  const char * const *enabled_inst_extensions;
90 
91  /**
92  * Enabled device extensions. By default, VK_KHR_external_memory_fd,
93  * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
94  * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
95  * If supplying your own device context, these fields takes the same format as
96  * the above fields, with the same conditions that duplicates are possible
97  * and accepted, and that NULL and 0 respectively means no extensions are enabled.
98  */
99  const char * const *enabled_dev_extensions;
101 
102  /**
103  * Queue family index for graphics operations, and the number of queues
104  * enabled for it. If unavaiable, will be set to -1. Not required.
105  * av_hwdevice_create() will attempt to find a dedicated queue for each
106  * queue family, or pick the one with the least unrelated flags set.
107  * Queue indices here may overlap if a queue has to share capabilities.
108  */
111 
112  /**
113  * Queue family index for transfer operations and the number of queues
114  * enabled. Required.
115  */
118 
119  /**
120  * Queue family index for compute operations and the number of queues
121  * enabled. Required.
122  */
125 
126  /**
127  * Queue family index for video encode ops, and the amount of queues enabled.
128  * If the device doesn't support such, queue_family_encode_index will be -1.
129  * Not required.
130  */
133 
134  /**
135  * Queue family index for video decode ops, and the amount of queues enabled.
136  * If the device doesn't support such, queue_family_decode_index will be -1.
137  * Not required.
138  */
141 
142  /**
143  * Locks a queue, preventing other threads from submitting any command
144  * buffers to this queue.
145  * If set to NULL, will be set to lavu-internal functions that utilize a
146  * mutex.
147  */
148  void (*lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
149 
150  /**
151  * Similar to lock_queue(), unlocks a queue. Must only be called after locking.
152  */
153  void (*unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
155 
156 /**
157  * Defines the behaviour of frame allocation.
158  */
159 typedef enum AVVkFrameFlags {
160  /* Unless this flag is set, autodetected flags will be OR'd based on the
161  * device and tiling during av_hwframe_ctx_init(). */
162  AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
163 
165  /* DEPRECATED: does nothing. Replaced by multiplane images. */
167 #endif
168 
169  /* Disables multiplane images.
170  * This is required to export/import images from CUDA. */
173 
174 /**
175  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
176  */
177 typedef struct AVVulkanFramesContext {
178  /**
179  * Controls the tiling of allocated frames.
180  * If left as VK_IMAGE_TILING_OPTIMAL (0), will use optimal tiling.
181  * Can be set to VK_IMAGE_TILING_LINEAR to force linear images,
182  * or VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT to force DMABUF-backed
183  * images.
184  * @note Imported frames from other APIs ignore this.
185  */
186  VkImageTiling tiling;
187 
188  /**
189  * Defines extra usage of output frames. If non-zero, all flags MUST be
190  * supported by the VkFormat. Otherwise, will use supported flags amongst:
191  * - VK_IMAGE_USAGE_SAMPLED_BIT
192  * - VK_IMAGE_USAGE_STORAGE_BIT
193  * - VK_IMAGE_USAGE_TRANSFER_SRC_BIT
194  * - VK_IMAGE_USAGE_TRANSFER_DST_BIT
195  */
196  VkImageUsageFlagBits usage;
197 
198  /**
199  * Extension data for image creation.
200  * If DRM tiling is used, a VkImageDrmFormatModifierListCreateInfoEXT structure
201  * can be added to specify the exact modifier to use.
202  *
203  * Additional structures may be added at av_hwframe_ctx_init() time,
204  * which will be freed automatically on uninit(), so users must only free
205  * any structures they've allocated themselves.
206  */
208 
209  /**
210  * Extension data for memory allocation. Must have as many entries as
211  * the number of planes of the sw_format.
212  * This will be chained to VkExportMemoryAllocateInfo, which is used
213  * to make all pool images exportable to other APIs if the necessary
214  * extensions are present in enabled_dev_extensions.
215  */
217 
218  /**
219  * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
220  * autodetected flags will be OR'd based on the device and tiling during
221  * av_hwframe_ctx_init().
222  */
224 
225  /**
226  * Flags to set during image creation. If unset, defaults to
227  * VK_IMAGE_CREATE_ALIAS_BIT.
228  */
229  VkImageCreateFlags img_flags;
230 
231  /**
232  * Vulkan format for each image. MUST be compatible with the pixel format.
233  * If unset, will be automatically set.
234  * There are at most two compatible formats for a frame - a multiplane
235  * format, and a single-plane multi-image format.
236  */
238 
239  /**
240  * Number of layers each image will have.
241  */
243 
244  /**
245  * Locks a frame, preventing other threads from changing frame properties.
246  * Users SHOULD only ever lock just before command submission in order
247  * to get accurate frame properties, and unlock immediately after command
248  * submission without waiting for it to finish.
249  *
250  * If unset, will be set to lavu-internal functions that utilize a mutex.
251  */
252  void (*lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
253 
254  /**
255  * Similar to lock_frame(), unlocks a frame. Must only be called after locking.
256  */
257  void (*unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
259 
260 /*
261  * Frame structure.
262  *
263  * @note the size of this structure is not part of the ABI, to allocate
264  * you must use @av_vk_frame_alloc().
265  */
266 struct AVVkFrame {
267  /**
268  * Vulkan images to which the memory is bound to.
269  * May be one for multiplane formats, or multiple.
270  */
272 
273  /**
274  * Tiling for the frame.
275  */
276  VkImageTiling tiling;
277 
278  /**
279  * Memory backing the images. Either one, or as many as there are planes
280  * in the sw_format.
281  * In case of having multiple VkImages, but one memory, the offset field
282  * will indicate the bound offset for each image.
283  */
284  VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
286 
287  /**
288  * OR'd flags for all memory allocated
289  */
290  VkMemoryPropertyFlagBits flags;
291 
292  /**
293  * Updated after every barrier. One per VkImage.
294  */
295  VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
296  VkImageLayout layout[AV_NUM_DATA_POINTERS];
297 
298  /**
299  * Synchronization timeline semaphores, one for each VkImage.
300  * Must not be freed manually. Must be waited on at every submission using
301  * the value in sem_value, and must be signalled at every submission,
302  * using an incremented value.
303  */
304  VkSemaphore sem[AV_NUM_DATA_POINTERS];
305 
306  /**
307  * Up to date semaphore value at which each image becomes accessible.
308  * One per VkImage.
309  * Clients must wait on this value when submitting a command queue,
310  * and increment it when signalling.
311  */
313 
314  /**
315  * Internal data.
316  */
317  struct AVVkFrameInternal *internal;
318 
319  /**
320  * Describes the binding offset of each image to the VkDeviceMemory.
321  * One per VkImage.
322  */
324 
325  /**
326  * Queue family of the images. Must be VK_QUEUE_FAMILY_IGNORED if
327  * the image was allocated with the CONCURRENT concurrency option.
328  * One per VkImage.
329  */
331 };
332 
333 /**
334  * Allocates a single AVVkFrame and initializes everything as 0.
335  * @note Must be freed via av_free()
336  */
338 
339 /**
340  * Returns the optimal per-plane Vulkan format for a given sw_format,
341  * one for each plane.
342  * Returns NULL on unsupported formats.
343  */
345 
346 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */
AVVulkanDeviceContext::phys_dev
VkPhysicalDevice phys_dev
Physical device.
Definition: hwcontext_vulkan.h:66
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE
@ AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE
Definition: hwcontext_vulkan.h:171
AVVulkanDeviceContext::get_proc_addr
PFN_vkGetInstanceProcAddr get_proc_addr
Pointer to the instance-provided vkGetInstanceProcAddr loading function.
Definition: hwcontext_vulkan.h:56
AVVulkanFramesContext::create_pnext
void * create_pnext
Extension data for image creation.
Definition: hwcontext_vulkan.h:207
AVVulkanDeviceContext::queue_family_decode_index
int queue_family_decode_index
Queue family index for video decode ops, and the amount of queues enabled.
Definition: hwcontext_vulkan.h:139
AVVulkanDeviceContext::inst
VkInstance inst
Vulkan instance.
Definition: hwcontext_vulkan.h:61
AVVulkanFramesContext::lock_frame
void(* lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf)
Locks a frame, preventing other threads from changing frame properties.
Definition: hwcontext_vulkan.h:252
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:464
AVVulkanDeviceContext::nb_decode_queues
int nb_decode_queues
Definition: hwcontext_vulkan.h:140
AVVulkanDeviceContext::queue_family_index
int queue_family_index
Queue family index for graphics operations, and the number of queues enabled for it.
Definition: hwcontext_vulkan.h:109
AVVulkanDeviceContext::alloc
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
Definition: hwcontext_vulkan.h:49
AVVkFrame::img
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Definition: hwcontext_vulkan.h:271
AVVkFrame::offset
ptrdiff_t offset[AV_NUM_DATA_POINTERS]
Describes the binding offset of each image to the VkDeviceMemory.
Definition: hwcontext_vulkan.h:323
AVVulkanDeviceContext::queue_family_comp_index
int queue_family_comp_index
Queue family index for compute operations and the number of queues enabled.
Definition: hwcontext_vulkan.h:123
AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY
@ AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY
Definition: hwcontext_vulkan.h:166
AVVulkanFramesContext::flags
AVVkFrameFlags flags
A combination of AVVkFrameFlags.
Definition: hwcontext_vulkan.h:223
AVVkFrame::mem
VkDeviceMemory mem[AV_NUM_DATA_POINTERS]
Memory backing the images.
Definition: hwcontext_vulkan.h:284
AVVulkanFramesContext
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
Definition: hwcontext_vulkan.h:177
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
AV_VK_FRAME_FLAG_NONE
@ AV_VK_FRAME_FLAG_NONE
Definition: hwcontext_vulkan.h:162
AVVulkanDeviceContext::nb_graphics_queues
int nb_graphics_queues
Definition: hwcontext_vulkan.h:110
AVVulkanFramesContext::unlock_frame
void(* unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf)
Similar to lock_frame(), unlocks a frame.
Definition: hwcontext_vulkan.h:257
AVVulkanFramesContext::img_flags
VkImageCreateFlags img_flags
Flags to set during image creation.
Definition: hwcontext_vulkan.h:229
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVVulkanDeviceContext
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_vulkan.h:45
AVVulkanDeviceContext::nb_enabled_dev_extensions
int nb_enabled_dev_extensions
Definition: hwcontext_vulkan.h:100
AVVkFrameInternal
Definition: hwcontext_vulkan.c:148
AVVkFrame::flags
VkMemoryPropertyFlagBits flags
OR'd flags for all memory allocated.
Definition: hwcontext_vulkan.h:290
AVVulkanFramesContext::alloc_pnext
void * alloc_pnext[AV_NUM_DATA_POINTERS]
Extension data for memory allocation.
Definition: hwcontext_vulkan.h:216
AVVulkanDeviceContext::unlock_queue
void(* unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index)
Similar to lock_queue(), unlocks a queue.
Definition: hwcontext_vulkan.h:153
AVVulkanDeviceContext::enabled_inst_extensions
const char *const * enabled_inst_extensions
Enabled instance extensions.
Definition: hwcontext_vulkan.h:88
AVVulkanFramesContext::format
VkFormat format[AV_NUM_DATA_POINTERS]
Vulkan format for each image.
Definition: hwcontext_vulkan.h:237
AVVulkanFramesContext::usage
VkImageUsageFlagBits usage
Defines extra usage of output frames.
Definition: hwcontext_vulkan.h:196
AVVulkanDeviceContext::queue_family_tx_index
int queue_family_tx_index
Queue family index for transfer operations and the number of queues enabled.
Definition: hwcontext_vulkan.h:116
index
int index
Definition: gxfenc.c:90
AVVkFrame::size
size_t size[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:285
AVVkFrame::access
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
Definition: hwcontext_vulkan.h:295
av_vkfmt_from_pixfmt
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the optimal per-plane Vulkan format for a given sw_format, one for each plane.
Definition: hwcontext_stub.c:30
AVVkFrame
Definition: hwcontext_vulkan.h:266
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:375
FF_API_VULKAN_CONTIGUOUS_MEMORY
#define FF_API_VULKAN_CONTIGUOUS_MEMORY
Definition: version.h:113
frame.h
av_vk_frame_alloc
AVVkFrame * av_vk_frame_alloc(void)
Allocates a single AVVkFrame and initializes everything as 0.
Definition: hwcontext_stub.c:35
AVVkFrame::queue_family
uint32_t queue_family[AV_NUM_DATA_POINTERS]
Queue family of the images.
Definition: hwcontext_vulkan.h:330
AVVulkanDeviceContext::lock_queue
void(* lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index)
Locks a queue, preventing other threads from submitting any command buffers to this queue.
Definition: hwcontext_vulkan.h:148
VkFormat
enum VkFormat VkFormat
Definition: hwcontext_stub.c:25
AVVulkanDeviceContext::nb_encode_queues
int nb_encode_queues
Definition: hwcontext_vulkan.h:132
AVVkFrameFlags
AVVkFrameFlags
Defines the behaviour of frame allocation.
Definition: hwcontext_vulkan.h:159
AVVkFrame::sem
VkSemaphore sem[AV_NUM_DATA_POINTERS]
Synchronization timeline semaphores, one for each VkImage.
Definition: hwcontext_vulkan.h:304
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
AVVkFrame::tiling
VkImageTiling tiling
Tiling for the frame.
Definition: hwcontext_vulkan.h:276
pixfmt.h
AVVulkanDeviceContext::nb_comp_queues
int nb_comp_queues
Definition: hwcontext_vulkan.h:124
AVVulkanFramesContext::tiling
VkImageTiling tiling
Controls the tiling of allocated frames.
Definition: hwcontext_vulkan.h:186
AVVkFrame::sem_value
uint64_t sem_value[AV_NUM_DATA_POINTERS]
Up to date semaphore value at which each image becomes accessible.
Definition: hwcontext_vulkan.h:312
AVVulkanDeviceContext::enabled_dev_extensions
const char *const * enabled_dev_extensions
Enabled device extensions.
Definition: hwcontext_vulkan.h:99
AVVulkanFramesContext::nb_layers
int nb_layers
Number of layers each image will have.
Definition: hwcontext_vulkan.h:242
AVVkFrame::layout
VkImageLayout layout[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:296
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:71
AVVulkanDeviceContext::nb_enabled_inst_extensions
int nb_enabled_inst_extensions
Definition: hwcontext_vulkan.h:89
AVVulkanDeviceContext::nb_tx_queues
int nb_tx_queues
Definition: hwcontext_vulkan.h:117
hwcontext.h
AVVulkanDeviceContext::device_features
VkPhysicalDeviceFeatures2 device_features
This structure should be set to the set of features that present and enabled during device creation.
Definition: hwcontext_vulkan.h:79
AVVulkanDeviceContext::queue_family_encode_index
int queue_family_encode_index
Queue family index for video encode ops, and the amount of queues enabled.
Definition: hwcontext_vulkan.h:131