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 #include <vulkan/vulkan.h>
23 
24 #include "pixfmt.h"
25 #include "frame.h"
26 
27 /**
28  * @file
29  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
30  *
31  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
32  * with the data pointer set to an AVVkFrame.
33  */
34 
35 /**
36  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
37  * All of these can be set before init to change what the context uses
38  */
39 typedef struct AVVulkanDeviceContext {
40  /**
41  * Custom memory allocator, else NULL
42  */
43  const VkAllocationCallbacks *alloc;
44 
45  /**
46  * Pointer to the instance-provided vkGetInstanceProcAddr loading function.
47  * If NULL, will pick either libvulkan or libvolk, depending on libavutil's
48  * compilation settings, and set this field.
49  */
50  PFN_vkGetInstanceProcAddr get_proc_addr;
51 
52  /**
53  * Vulkan instance. Must be at least version 1.1.
54  */
55  VkInstance inst;
56 
57  /**
58  * Physical device
59  */
60  VkPhysicalDevice phys_dev;
61 
62  /**
63  * Active device
64  */
65  VkDevice act_dev;
66 
67  /**
68  * This structure should be set to the set of features that present and enabled
69  * during device creation. When a device is created by FFmpeg, it will default to
70  * enabling all that are present of the shaderImageGatherExtended,
71  * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
72  */
73  VkPhysicalDeviceFeatures2 device_features;
74 
75  /**
76  * Enabled instance extensions.
77  * If supplying your own device context, set this to an array of strings, with
78  * each entry containing the specified Vulkan extension string to enable.
79  * Duplicates are possible and accepted.
80  * If no extensions are enabled, set these fields to NULL, and 0 respectively.
81  */
82  const char * const *enabled_inst_extensions;
84 
85  /**
86  * Enabled device extensions. By default, VK_KHR_external_memory_fd,
87  * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
88  * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
89  * If supplying your own device context, these fields takes the same format as
90  * the above fields, with the same conditions that duplicates are possible
91  * and accepted, and that NULL and 0 respectively means no extensions are enabled.
92  */
93  const char * const *enabled_dev_extensions;
95 
96  /**
97  * Queue family index for graphics
98  * @note av_hwdevice_create() will set all 3 queue indices if unset
99  * If there is no dedicated queue for compute or transfer operations,
100  * they will be set to the graphics queue index which can handle both.
101  * nb_graphics_queues indicates how many queues were enabled for the
102  * graphics queue (must be at least 1)
103  */
106 
107  /**
108  * Queue family index to use for transfer operations, and the amount of queues
109  * enabled. In case there is no dedicated transfer queue, nb_tx_queues
110  * must be 0 and queue_family_tx_index must be the same as either the graphics
111  * queue or the compute queue, if available.
112  */
115 
116  /**
117  * Queue family index for compute ops, and the amount of queues enabled.
118  * In case there are no dedicated compute queues, nb_comp_queues must be
119  * 0 and its queue family index must be set to the graphics queue.
120  */
124 
125 /**
126  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
127  */
128 typedef struct AVVulkanFramesContext {
129  /**
130  * Controls the tiling of allocated frames.
131  */
132  VkImageTiling tiling;
133 
134  /**
135  * Defines extra usage of output frames. If left as 0, the following bits
136  * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
137  */
138  VkImageUsageFlagBits usage;
139 
140  /**
141  * Extension data for image creation.
142  */
144 
145  /**
146  * Extension data for memory allocation. Must have as many entries as
147  * the number of planes of the sw_format.
148  * This will be chained to VkExportMemoryAllocateInfo, which is used
149  * to make all pool images exportable to other APIs if the necessary
150  * extensions are present in enabled_dev_extensions.
151  */
154 
155 /*
156  * Frame structure, the VkFormat of the image will always match
157  * the pool's sw_format.
158  * All frames, imported or allocated, will be created with the
159  * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
160  *
161  * If all three queue family indices in the device context are the same,
162  * images will be created with the EXCLUSIVE sharing mode. Otherwise, all images
163  * will be created using the CONCURRENT sharing mode.
164  *
165  * @note the size of this structure is not part of the ABI, to allocate
166  * you must use @av_vk_frame_alloc().
167  */
168 typedef struct AVVkFrame {
169  /**
170  * Vulkan images to which the memory is bound to.
171  */
173 
174  /**
175  * The same tiling must be used for all images in the frame.
176  */
177  VkImageTiling tiling;
178 
179  /**
180  * Memory backing the images. Could be less than the amount of images
181  * if importing from a DRM or VAAPI frame.
182  */
183  VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
185 
186  /**
187  * OR'd flags for all memory allocated
188  */
189  VkMemoryPropertyFlagBits flags;
190 
191  /**
192  * Updated after every barrier
193  */
194  VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
195  VkImageLayout layout[AV_NUM_DATA_POINTERS];
196 
197  /**
198  * Synchronization semaphores. Must not be freed manually. Must be waited on
199  * and signalled at every queue submission.
200  * Could be less than the amount of images: either one per VkDeviceMemory
201  * or one for the entire frame. All others will be set to VK_NULL_HANDLE.
202  */
203  VkSemaphore sem[AV_NUM_DATA_POINTERS];
204 
205  /**
206  * Internal data.
207  */
208  struct AVVkFrameInternal *internal;
209 } AVVkFrame;
210 
211 /**
212  * Allocates a single AVVkFrame and initializes everything as 0.
213  * @note Must be freed via av_free()
214  */
216 
217 /**
218  * Returns the format of each image up to the number of planes for a given sw_format.
219  * Returns NULL on unsupported formats.
220  */
221 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
222 
223 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */
AVVulkanDeviceContext::phys_dev
VkPhysicalDevice phys_dev
Physical device.
Definition: hwcontext_vulkan.h:60
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:50
AVVulkanFramesContext::create_pnext
void * create_pnext
Extension data for image creation.
Definition: hwcontext_vulkan.h:143
AVVulkanDeviceContext::inst
VkInstance inst
Vulkan instance.
Definition: hwcontext_vulkan.h:55
AVVulkanDeviceContext::queue_family_index
int queue_family_index
Queue family index for graphics.
Definition: hwcontext_vulkan.h:104
AVVulkanDeviceContext::alloc
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
Definition: hwcontext_vulkan.h:43
AVVkFrame::img
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Definition: hwcontext_vulkan.h:172
AVVulkanDeviceContext::queue_family_comp_index
int queue_family_comp_index
Queue family index for compute ops, and the amount of queues enabled.
Definition: hwcontext_vulkan.h:121
AVVkFrame::mem
VkDeviceMemory mem[AV_NUM_DATA_POINTERS]
Memory backing the images.
Definition: hwcontext_vulkan.h:183
AVVulkanFramesContext
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
Definition: hwcontext_vulkan.h:128
AVVulkanDeviceContext::nb_graphics_queues
int nb_graphics_queues
Definition: hwcontext_vulkan.h:105
AVVulkanDeviceContext
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_vulkan.h:39
AVVulkanDeviceContext::nb_enabled_dev_extensions
int nb_enabled_dev_extensions
Definition: hwcontext_vulkan.h:94
AVVkFrameInternal
Definition: hwcontext_vulkan.c:227
AVVkFrame::flags
VkMemoryPropertyFlagBits flags
OR'd flags for all memory allocated.
Definition: hwcontext_vulkan.h:189
AVVulkanFramesContext::alloc_pnext
void * alloc_pnext[AV_NUM_DATA_POINTERS]
Extension data for memory allocation.
Definition: hwcontext_vulkan.h:152
AVVulkanDeviceContext::enabled_inst_extensions
const char *const * enabled_inst_extensions
Enabled instance extensions.
Definition: hwcontext_vulkan.h:82
AVVulkanFramesContext::usage
VkImageUsageFlagBits usage
Defines extra usage of output frames.
Definition: hwcontext_vulkan.h:138
AVVulkanDeviceContext::queue_family_tx_index
int queue_family_tx_index
Queue family index to use for transfer operations, and the amount of queues enabled.
Definition: hwcontext_vulkan.h:113
AVVkFrame::size
size_t size[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:184
AVVkFrame::access
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
Definition: hwcontext_vulkan.h:194
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_vulkan.c:341
AVVkFrame
Definition: hwcontext_vulkan.h:168
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:304
frame.h
av_vk_frame_alloc
AVVkFrame * av_vk_frame_alloc(void)
Allocates a single AVVkFrame and initializes everything as 0.
Definition: hwcontext_vulkan.c:3612
AVVkFrame::sem
VkSemaphore sem[AV_NUM_DATA_POINTERS]
Synchronization semaphores.
Definition: hwcontext_vulkan.h:203
AVVkFrame::tiling
VkImageTiling tiling
The same tiling must be used for all images in the frame.
Definition: hwcontext_vulkan.h:177
pixfmt.h
AVVulkanDeviceContext::nb_comp_queues
int nb_comp_queues
Definition: hwcontext_vulkan.h:122
AVVulkanFramesContext::tiling
VkImageTiling tiling
Controls the tiling of allocated frames.
Definition: hwcontext_vulkan.h:132
AVVulkanDeviceContext::enabled_dev_extensions
const char *const * enabled_dev_extensions
Enabled device extensions.
Definition: hwcontext_vulkan.h:93
AVVkFrame::layout
VkImageLayout layout[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:195
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:65
AVVulkanDeviceContext::nb_enabled_inst_extensions
int nb_enabled_inst_extensions
Definition: hwcontext_vulkan.h:83
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:73