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; 86 int nb_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; 97 int nb_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 */ 106 int queue_family_index; 107 int nb_graphics_queues; 108 109 /** 110 * Queue family index for transfer operations and the number of queues 111 * enabled. Required. 112 */ 113 int queue_family_tx_index; 114 int nb_tx_queues; 115 116 /** 117 * Queue family index for compute operations and the number of queues 118 * enabled. Required. 119 */ 120 int queue_family_comp_index; 121 int nb_comp_queues; 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 */ 128 int queue_family_encode_index; 129 int nb_encode_queues; 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 */ 136 int queue_family_decode_index; 137 int nb_decode_queues; 138 } AVVulkanDeviceContext; 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. */ 151 AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY = (1ULL << 1), 152 } AVVkFrameFlags; 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 */ 181 void *create_pnext; 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 */ 190 void *alloc_pnext[AV_NUM_DATA_POINTERS]; 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 */ 197 AVVkFrameFlags flags; 198 } AVVulkanFramesContext; 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 */ 217 VkImage img[AV_NUM_DATA_POINTERS]; 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]; 230 size_t size[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 */ 256 uint64_t sem_value[AV_NUM_DATA_POINTERS]; 257 258 /** 259 * Internal data. 260 */ 261 struct AVVkFrameInternal *internal; 262 263 /** 264 * Describes the binding offset of each plane to the VkDeviceMemory. 265 */ 266 ptrdiff_t offset[AV_NUM_DATA_POINTERS]; 267 } AVVkFrame; 268 269 /** 270 * Allocates a single AVVkFrame and initializes everything as 0. 271 * @note Must be freed via av_free() 272 */ 273 AVVkFrame *av_vk_frame_alloc(void); 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 */ 279 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p); 280 281 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */ 282