2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVUTIL_HWCONTEXT_VULKAN_H
20 #define AVUTIL_HWCONTEXT_VULKAN_H
22 #if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
23 #define VK_USE_PLATFORM_WIN32_KHR
25 #include <vulkan/vulkan.h>
32 * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
34 * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
35 * with the data pointer set to an AVVkFrame.
39 * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
40 * All of these can be set before init to change what the context uses
42 typedef struct AVVulkanDeviceContext
{
44 * Custom memory allocator, else NULL
46 const VkAllocationCallbacks
*alloc
;
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.
53 PFN_vkGetInstanceProcAddr get_proc_addr
;
56 * Vulkan instance. Must be at least version 1.2.
63 VkPhysicalDevice phys_dev
;
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.
76 VkPhysicalDeviceFeatures2 device_features
;
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.
85 const char * const *enabled_inst_extensions
;
86 int nb_enabled_inst_extensions
;
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.
96 const char * const *enabled_dev_extensions
;
97 int nb_enabled_dev_extensions
;
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.
106 int queue_family_index
;
107 int nb_graphics_queues
;
110 * Queue family index for transfer operations and the number of queues
113 int queue_family_tx_index
;
117 * Queue family index for compute operations and the number of queues
120 int queue_family_comp_index
;
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.
128 int queue_family_encode_index
;
129 int nb_encode_queues
;
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.
136 int queue_family_decode_index
;
137 int nb_decode_queues
;
138 } AVVulkanDeviceContext
;
141 * Defines the behaviour of frame allocation.
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),
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. */
151 AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY
= (1ULL << 1),
155 * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
157 typedef struct AVVulkanFramesContext
{
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.
164 VkImageTiling tiling
;
167 * Defines extra usage of output frames. If left as 0, the following bits
168 * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
170 VkImageUsageFlagBits usage
;
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.
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.
190 void *alloc_pnext
[AV_NUM_DATA_POINTERS
];
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().
197 AVVkFrameFlags flags
;
198 } AVVulkanFramesContext
;
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.
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.
210 * @note the size of this structure is not part of the ABI, to allocate
211 * you must use @av_vk_frame_alloc().
213 typedef struct AVVkFrame
{
215 * Vulkan images to which the memory is bound to.
217 VkImage img
[AV_NUM_DATA_POINTERS
];
220 * The same tiling must be used for all images in the frame.
222 VkImageTiling tiling
;
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.
229 VkDeviceMemory mem
[AV_NUM_DATA_POINTERS
];
230 size_t size
[AV_NUM_DATA_POINTERS
];
233 * OR'd flags for all memory allocated
235 VkMemoryPropertyFlagBits flags
;
238 * Updated after every barrier
240 VkAccessFlagBits access
[AV_NUM_DATA_POINTERS
];
241 VkImageLayout layout
[AV_NUM_DATA_POINTERS
];
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.
249 VkSemaphore sem
[AV_NUM_DATA_POINTERS
];
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.
256 uint64_t sem_value
[AV_NUM_DATA_POINTERS
];
261 struct AVVkFrameInternal
*internal
;
264 * Describes the binding offset of each plane to the VkDeviceMemory.
266 ptrdiff_t offset
[AV_NUM_DATA_POINTERS
];
270 * Allocates a single AVVkFrame and initializes everything as 0.
271 * @note Must be freed via av_free()
273 AVVkFrame
*av_vk_frame_alloc(void);
276 * Returns the format of each image up to the number of planes for a given sw_format.
277 * Returns NULL on unsupported formats.
279 const VkFormat
*av_vkfmt_from_pixfmt(enum AVPixelFormat p
);
281 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */