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 
30 /**
31  * @file
32  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
33  *
34  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
35  * with the data pointer set to an AVVkFrame.
36  */
37 
38 /**
39  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
40  * All of these can be set before init to change what the context uses
41  */
42 typedef struct AVVulkanDeviceContext {
43  /**
44  * Custom memory allocator, else NULL
45  */
46  const VkAllocationCallbacks *alloc;
47 
48  /**
49  * Pointer to the instance-provided vkGetInstanceProcAddr loading function.
50  * If NULL, will pick either libvulkan or libvolk, depending on libavutil's
51  * compilation settings, and set this field.
52  */
53  PFN_vkGetInstanceProcAddr get_proc_addr;
54 
55  /**
56  * Vulkan instance. Must be at least version 1.2.
57  */
58  VkInstance inst;
59 
60  /**
61  * Physical device
62  */
63  VkPhysicalDevice phys_dev;
64 
65  /**
66  * Active device
67  */
68  VkDevice act_dev;
69 
70  /**
71  * This structure should be set to the set of features that present and enabled
72  * during device creation. When a device is created by FFmpeg, it will default to
73  * enabling all that are present of the shaderImageGatherExtended,
74  * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
75  */
76  VkPhysicalDeviceFeatures2 device_features;
77 
78  /**
79  * Enabled instance extensions.
80  * If supplying your own device context, set this to an array of strings, with
81  * each entry containing the specified Vulkan extension string to enable.
82  * Duplicates are possible and accepted.
83  * If no extensions are enabled, set these fields to NULL, and 0 respectively.
84  */
85  const char * const *enabled_inst_extensions;
87 
88  /**
89  * Enabled device extensions. By default, VK_KHR_external_memory_fd,
90  * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
91  * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
92  * If supplying your own device context, these fields takes the same format as
93  * the above fields, with the same conditions that duplicates are possible
94  * and accepted, and that NULL and 0 respectively means no extensions are enabled.
95  */
96  const char * const *enabled_dev_extensions;
98 
99  /**
100  * Queue family index for graphics operations, and the number of queues
101  * enabled for it. If unavaiable, will be set to -1. Not required.
102  * av_hwdevice_create() will attempt to find a dedicated queue for each
103  * queue family, or pick the one with the least unrelated flags set.
104  * Queue indices here may overlap if a queue has to share capabilities.
105  */
108 
109  /**
110  * Queue family index for transfer operations and the number of queues
111  * enabled. Required.
112  */
115 
116  /**
117  * Queue family index for compute operations and the number of queues
118  * enabled. Required.
119  */
122 
123  /**
124  * Queue family index for video encode ops, and the amount of queues enabled.
125  * If the device doesn't support such, queue_family_encode_index will be -1.
126  * Not required.
127  */
130 
131  /**
132  * Queue family index for video decode ops, and the amount of queues enabled.
133  * If the device doesn't support such, queue_family_decode_index will be -1.
134  * Not required.
135  */
139 
140 /**
141  * Defines the behaviour of frame allocation.
142  */
143 typedef enum AVVkFrameFlags {
144  /* Unless this flag is set, autodetected flags will be OR'd based on the
145  * device and tiling during av_hwframe_ctx_init(). */
146  AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
147 
148  /* Image planes will be allocated in a single VkDeviceMemory, rather
149  * than as per-plane VkDeviceMemory allocations. Required for exporting
150  * to VAAPI on Intel devices. */
153 
154 /**
155  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
156  */
157 typedef struct AVVulkanFramesContext {
158  /**
159  * Controls the tiling of allocated frames. If left as optimal tiling,
160  * then during av_hwframe_ctx_init() will decide based on whether the device
161  * supports DRM modifiers, or if the linear_images flag is set, otherwise
162  * will allocate optimally-tiled images.
163  */
164  VkImageTiling tiling;
165 
166  /**
167  * Defines extra usage of output frames. If left as 0, the following bits
168  * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
169  */
170  VkImageUsageFlagBits usage;
171 
172  /**
173  * Extension data for image creation.
174  * If VkImageDrmFormatModifierListCreateInfoEXT is present in the chain,
175  * and the device supports DRM modifiers, then images will be allocated
176  * with the specific requested DRM modifiers.
177  * Additional structures may be added at av_hwframe_ctx_init() time,
178  * which will be freed automatically on uninit(), so users need only free
179  * any structures they've allocated themselves.
180  */
182 
183  /**
184  * Extension data for memory allocation. Must have as many entries as
185  * the number of planes of the sw_format.
186  * This will be chained to VkExportMemoryAllocateInfo, which is used
187  * to make all pool images exportable to other APIs if the necessary
188  * extensions are present in enabled_dev_extensions.
189  */
191 
192  /**
193  * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
194  * autodetected flags will be OR'd based on the device and tiling during
195  * av_hwframe_ctx_init().
196  */
199 
200 /*
201  * Frame structure, the VkFormat of the image will always match
202  * the pool's sw_format.
203  * All frames, imported or allocated, will be created with the
204  * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
205  *
206  * If all queue family indices in the device context are the same,
207  * images will be created with the EXCLUSIVE sharing mode. Otherwise, all images
208  * will be created using the CONCURRENT sharing mode.
209  *
210  * @note the size of this structure is not part of the ABI, to allocate
211  * you must use @av_vk_frame_alloc().
212  */
213 typedef struct AVVkFrame {
214  /**
215  * Vulkan images to which the memory is bound to.
216  */
218 
219  /**
220  * The same tiling must be used for all images in the frame.
221  */
222  VkImageTiling tiling;
223 
224  /**
225  * Memory backing the images. Could be less than the amount of planes,
226  * in which case the offset value will indicate the binding offset of
227  * each plane in the memory.
228  */
229  VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
231 
232  /**
233  * OR'd flags for all memory allocated
234  */
235  VkMemoryPropertyFlagBits flags;
236 
237  /**
238  * Updated after every barrier
239  */
240  VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
241  VkImageLayout layout[AV_NUM_DATA_POINTERS];
242 
243  /**
244  * Synchronization timeline semaphores, one for each sw_format plane.
245  * Must not be freed manually. Must be waited on at every submission using
246  * the value in sem_value, and must be signalled at every submission,
247  * using an incremented value.
248  */
249  VkSemaphore sem[AV_NUM_DATA_POINTERS];
250 
251  /**
252  * Up to date semaphore value at which each image becomes accessible.
253  * Clients must wait on this value when submitting a command queue,
254  * and increment it when signalling.
255  */
257 
258  /**
259  * Internal data.
260  */
261  struct AVVkFrameInternal *internal;
262 
263  /**
264  * Describes the binding offset of each plane to the VkDeviceMemory.
265  */
267 } AVVkFrame;
268 
269 /**
270  * Allocates a single AVVkFrame and initializes everything as 0.
271  * @note Must be freed via av_free()
272  */
274 
275 /**
276  * Returns the format of each image up to the number of planes for a given sw_format.
277  * Returns NULL on unsupported formats.
278  */
280 
281 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */
AVVulkanDeviceContext::phys_dev
VkPhysicalDevice phys_dev
Physical device.
Definition: hwcontext_vulkan.h:63
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVVulkanDeviceContext::get_proc_addr
PFN_vkGetInstanceProcAddr get_proc_addr
Pointer to the instance-provided vkGetInstanceProcAddr loading function.
Definition: hwcontext_vulkan.h:53
AVVulkanFramesContext::create_pnext
void * create_pnext
Extension data for image creation.
Definition: hwcontext_vulkan.h:181
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:136
AVVulkanDeviceContext::inst
VkInstance inst
Vulkan instance.
Definition: hwcontext_vulkan.h:58
AVVulkanDeviceContext::nb_decode_queues
int nb_decode_queues
Definition: hwcontext_vulkan.h:137
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:106
AVVulkanDeviceContext::alloc
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
Definition: hwcontext_vulkan.h:46
AVVkFrame::img
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Definition: hwcontext_vulkan.h:217
AVVkFrame::offset
ptrdiff_t offset[AV_NUM_DATA_POINTERS]
Describes the binding offset of each plane to the VkDeviceMemory.
Definition: hwcontext_vulkan.h:266
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:120
AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY
@ AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY
Definition: hwcontext_vulkan.h:151
AVVulkanFramesContext::flags
AVVkFrameFlags flags
A combination of AVVkFrameFlags.
Definition: hwcontext_vulkan.h:197
AVVkFrame::mem
VkDeviceMemory mem[AV_NUM_DATA_POINTERS]
Memory backing the images.
Definition: hwcontext_vulkan.h:229
AVVulkanFramesContext
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
Definition: hwcontext_vulkan.h:157
AV_VK_FRAME_FLAG_NONE
@ AV_VK_FRAME_FLAG_NONE
Definition: hwcontext_vulkan.h:146
AVVulkanDeviceContext::nb_graphics_queues
int nb_graphics_queues
Definition: hwcontext_vulkan.h:107
AVVulkanDeviceContext
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_vulkan.h:42
AVVulkanDeviceContext::nb_enabled_dev_extensions
int nb_enabled_dev_extensions
Definition: hwcontext_vulkan.h:97
AVVkFrameInternal
Definition: hwcontext_vulkan.c:128
AVVkFrame::flags
VkMemoryPropertyFlagBits flags
OR'd flags for all memory allocated.
Definition: hwcontext_vulkan.h:235
AVVulkanFramesContext::alloc_pnext
void * alloc_pnext[AV_NUM_DATA_POINTERS]
Extension data for memory allocation.
Definition: hwcontext_vulkan.h:190
AVVulkanDeviceContext::enabled_inst_extensions
const char *const * enabled_inst_extensions
Enabled instance extensions.
Definition: hwcontext_vulkan.h:85
AVVulkanFramesContext::usage
VkImageUsageFlagBits usage
Defines extra usage of output frames.
Definition: hwcontext_vulkan.h:170
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:113
AVVkFrame::size
size_t size[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:230
AVVkFrame::access
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
Definition: hwcontext_vulkan.h:240
av_vkfmt_from_pixfmt
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the format of each image up to the number of planes for a given sw_format.
Definition: hwcontext_stub.c:30
AVVkFrame
Definition: hwcontext_vulkan.h:213
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:331
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
VkFormat
enum VkFormat VkFormat
Definition: hwcontext_stub.c:25
AVVulkanDeviceContext::nb_encode_queues
int nb_encode_queues
Definition: hwcontext_vulkan.h:129
AVVkFrameFlags
AVVkFrameFlags
Defines the behaviour of frame allocation.
Definition: hwcontext_vulkan.h:143
AVVkFrame::sem
VkSemaphore sem[AV_NUM_DATA_POINTERS]
Synchronization timeline semaphores, one for each sw_format plane.
Definition: hwcontext_vulkan.h:249
AVVkFrame::tiling
VkImageTiling tiling
The same tiling must be used for all images in the frame.
Definition: hwcontext_vulkan.h:222
pixfmt.h
AVVulkanDeviceContext::nb_comp_queues
int nb_comp_queues
Definition: hwcontext_vulkan.h:121
AVVulkanFramesContext::tiling
VkImageTiling tiling
Controls the tiling of allocated frames.
Definition: hwcontext_vulkan.h:164
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:256
AVVulkanDeviceContext::enabled_dev_extensions
const char *const * enabled_dev_extensions
Enabled device extensions.
Definition: hwcontext_vulkan.h:96
AVVkFrame::layout
VkImageLayout layout[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:241
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:68
AVVulkanDeviceContext::nb_enabled_inst_extensions
int nb_enabled_inst_extensions
Definition: hwcontext_vulkan.h:86
AVVulkanDeviceContext::nb_tx_queues
int nb_tx_queues
Definition: hwcontext_vulkan.h:114
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:76
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:128