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 #include <vulkan/vulkan.h>
26 * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
28 * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
29 * with the data pointer set to an AVVkFrame.
33 * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
34 * All of these can be set before init to change what the context uses
36 typedef struct AVVulkanDeviceContext {
38 * Custom memory allocator, else NULL
40 const VkAllocationCallbacks *alloc;
42 * Vulkan instance. Must be at least version 1.1.
48 VkPhysicalDevice phys_dev;
54 * Queue family index for graphics
55 * @note av_hwdevice_create() will set all 3 queue indices if unset
56 * If there is no dedicated queue for compute or transfer operations,
57 * they will be set to the graphics queue index which can handle both.
58 * nb_graphics_queues indicates how many queues were enabled for the
59 * graphics queue (must be at least 1)
61 int queue_family_index;
62 int nb_graphics_queues;
64 * Queue family index to use for transfer operations, and the amount of queues
65 * enabled. In case there is no dedicated transfer queue, nb_tx_queues
66 * must be 0 and queue_family_tx_index must be the same as either the graphics
67 * queue or the compute queue, if available.
69 int queue_family_tx_index;
72 * Queue family index for compute ops, and the amount of queues enabled.
73 * In case there are no dedicated compute queues, nb_comp_queues must be
74 * 0 and its queue family index must be set to the graphics queue.
76 int queue_family_comp_index;
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;
88 * Enabled device extensions. By default, VK_KHR_external_memory_fd,
89 * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
90 * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
91 * If supplying your own device context, these fields takes the same format as
92 * the above fields, with the same conditions that duplicates are possible
93 * and accepted, and that NULL and 0 respectively means no extensions are enabled.
95 const char * const *enabled_dev_extensions;
96 int nb_enabled_dev_extensions;
98 * This structure should be set to the set of features that present and enabled
99 * during device creation. When a device is created by FFmpeg, it will default to
100 * enabling all that are present of the shaderImageGatherExtended,
101 * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
103 VkPhysicalDeviceFeatures2 device_features;
104 } AVVulkanDeviceContext;
107 * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
109 typedef struct AVVulkanFramesContext {
111 * Controls the tiling of allocated frames.
113 VkImageTiling tiling;
115 * Defines extra usage of output frames. If left as 0, the following bits
116 * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
118 VkImageUsageFlagBits usage;
120 * Extension data for image creation.
124 * Extension data for memory allocation. Must have as many entries as
125 * the number of planes of the sw_format.
126 * This will be chained to VkExportMemoryAllocateInfo, which is used
127 * to make all pool images exportable to other APIs if the necessary
128 * extensions are present in enabled_dev_extensions.
130 void *alloc_pnext[AV_NUM_DATA_POINTERS];
131 } AVVulkanFramesContext;
134 * Frame structure, the VkFormat of the image will always match
135 * the pool's sw_format.
136 * All frames, imported or allocated, will be created with the
137 * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
139 * If all three queue family indices in the device context are the same,
140 * images will be created with the EXCLUSIVE sharing mode. Otherwise, all images
141 * will be created using the CONCURRENT sharing mode.
143 * @note the size of this structure is not part of the ABI, to allocate
144 * you must use @av_vk_frame_alloc().
146 typedef struct AVVkFrame {
148 * Vulkan images to which the memory is bound to.
150 VkImage img[AV_NUM_DATA_POINTERS];
153 * The same tiling must be used for all images in the frame.
155 VkImageTiling tiling;
158 * Memory backing the images. Could be less than the amount of images
159 * if importing from a DRM or VAAPI frame.
161 VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
162 size_t size[AV_NUM_DATA_POINTERS];
165 * OR'd flags for all memory allocated
167 VkMemoryPropertyFlagBits flags;
170 * Updated after every barrier
172 VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
173 VkImageLayout layout[AV_NUM_DATA_POINTERS];
176 * Synchronization semaphores. Must not be freed manually. Must be waited on
177 * and signalled at every queue submission.
178 * Could be less than the amount of images: either one per VkDeviceMemory
179 * or one for the entire frame. All others will be set to VK_NULL_HANDLE.
181 VkSemaphore sem[AV_NUM_DATA_POINTERS];
186 struct AVVkFrameInternal *internal;
190 * Allocates a single AVVkFrame and initializes everything as 0.
191 * @note Must be freed via av_free()
193 AVVkFrame *av_vk_frame_alloc(void);
196 * Returns the format of each image up to the number of planes for a given sw_format.
197 * Returns NULL on unsupported formats.
199 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
201 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */