| 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 | #include "hwcontext.h" |
| 30 |
| 31 | typedef struct AVVkFrame AVVkFrame; |
| 32 |
| 33 | typedef struct AVVulkanDeviceQueueFamily { |
| 34 | /* Queue family index */ |
| 35 | int idx; |
| 36 | /* Number of queues in the queue family in use */ |
| 37 | int num; |
| 38 | /* Queue family capabilities. Must be non-zero. |
| 39 | * Flags may be removed to indicate the queue family may not be used |
| 40 | * for a given purpose. */ |
| 41 | VkQueueFlagBits flags; |
| 42 | /* Vulkan implementations are allowed to list multiple video queues |
| 43 | * which differ in what they can encode or decode. */ |
| 44 | VkVideoCodecOperationFlagBitsKHR video_caps; |
| 45 | } AVVulkanDeviceQueueFamily; |
| 46 |
| 47 | /** |
| 48 | * @file |
| 49 | * API-specific header for AV_HWDEVICE_TYPE_VULKAN. |
| 50 | * |
| 51 | * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs |
| 52 | * with the data pointer set to an AVVkFrame. |
| 53 | */ |
| 54 |
| 55 | /** |
| 56 | * Main Vulkan context, allocated as AVHWDeviceContext.hwctx. |
| 57 | * All of these can be set before init to change what the context uses |
| 58 | */ |
| 59 | typedef struct AVVulkanDeviceContext { |
| 60 | /** |
| 61 | * Custom memory allocator, else NULL |
| 62 | */ |
| 63 | const VkAllocationCallbacks *alloc; |
| 64 |
| 65 | /** |
| 66 | * Pointer to a vkGetInstanceProcAddr loading function. |
| 67 | * If unset, will dynamically load and use libvulkan. |
| 68 | */ |
| 69 | PFN_vkGetInstanceProcAddr get_proc_addr; |
| 70 |
| 71 | /** |
| 72 | * Vulkan instance. Must be at least version 1.3. |
| 73 | */ |
| 74 | VkInstance inst; |
| 75 |
| 76 | /** |
| 77 | * Physical device |
| 78 | */ |
| 79 | VkPhysicalDevice phys_dev; |
| 80 |
| 81 | /** |
| 82 | * Active device |
| 83 | */ |
| 84 | VkDevice act_dev; |
| 85 |
| 86 | /** |
| 87 | * This structure should be set to the set of features that present and enabled |
| 88 | * during device creation. When a device is created by FFmpeg, it will default to |
| 89 | * enabling all that are present of the shaderImageGatherExtended, |
| 90 | * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features. |
| 91 | */ |
| 92 | VkPhysicalDeviceFeatures2 device_features; |
| 93 |
| 94 | /** |
| 95 | * Enabled instance extensions. |
| 96 | * If supplying your own device context, set this to an array of strings, with |
| 97 | * each entry containing the specified Vulkan extension string to enable. |
| 98 | * Duplicates are possible and accepted. |
| 99 | * If no extensions are enabled, set these fields to NULL, and 0 respectively. |
| 100 | * av_vk_get_optional_instance_extensions() can be used to enumerate extensions |
| 101 | * that FFmpeg may use if enabled. |
| 102 | */ |
| 103 | const char * const *enabled_inst_extensions; |
| 104 | int nb_enabled_inst_extensions; |
| 105 |
| 106 | /** |
| 107 | * Enabled device extensions. By default, VK_KHR_external_memory_fd, |
| 108 | * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier, |
| 109 | * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found. |
| 110 | * If supplying your own device context, these fields takes the same format as |
| 111 | * the above fields, with the same conditions that duplicates are possible |
| 112 | * and accepted, and that NULL and 0 respectively means no extensions are enabled. |
| 113 | * av_vk_get_optional_device_extensions() can be used to enumerate extensions |
| 114 | * that FFmpeg may use if enabled. |
| 115 | */ |
| 116 | const char * const *enabled_dev_extensions; |
| 117 | int nb_enabled_dev_extensions; |
| 118 |
| 119 | #if FF_API_VULKAN_FIXED_QUEUES |
| 120 | /** |
| 121 | * Queue family index for graphics operations, and the number of queues |
| 122 | * enabled for it. If unavailable, will be set to -1. Not required. |
| 123 | * av_hwdevice_create() will attempt to find a dedicated queue for each |
| 124 | * queue family, or pick the one with the least unrelated flags set. |
| 125 | * Queue indices here may overlap if a queue has to share capabilities. |
| 126 | */ |
| 127 | attribute_deprecated |
| 128 | int queue_family_index; |
| 129 | attribute_deprecated |
| 130 | int nb_graphics_queues; |
| 131 |
| 132 | /** |
| 133 | * Queue family index for transfer operations and the number of queues |
| 134 | * enabled. Required. |
| 135 | */ |
| 136 | attribute_deprecated |
| 137 | int queue_family_tx_index; |
| 138 | attribute_deprecated |
| 139 | int nb_tx_queues; |
| 140 |
| 141 | /** |
| 142 | * Queue family index for compute operations and the number of queues |
| 143 | * enabled. Required. |
| 144 | */ |
| 145 | attribute_deprecated |
| 146 | int queue_family_comp_index; |
| 147 | attribute_deprecated |
| 148 | int nb_comp_queues; |
| 149 |
| 150 | /** |
| 151 | * Queue family index for video encode ops, and the amount of queues enabled. |
| 152 | * If the device doesn't support such, queue_family_encode_index will be -1. |
| 153 | * Not required. |
| 154 | */ |
| 155 | attribute_deprecated |
| 156 | int queue_family_encode_index; |
| 157 | attribute_deprecated |
| 158 | int nb_encode_queues; |
| 159 |
| 160 | /** |
| 161 | * Queue family index for video decode ops, and the amount of queues enabled. |
| 162 | * If the device doesn't support such, queue_family_decode_index will be -1. |
| 163 | * Not required. |
| 164 | */ |
| 165 | attribute_deprecated |
| 166 | int queue_family_decode_index; |
| 167 | attribute_deprecated |
| 168 | int nb_decode_queues; |
| 169 | #endif |
| 170 |
| 171 | #if FF_API_VULKAN_SYNC_QUEUES |
| 172 | /** |
| 173 | * Locks a queue, preventing other threads from submitting any command |
| 174 | * buffers to this queue. |
| 175 | * If set to NULL, will be set to lavu-internal functions that utilize a |
| 176 | * mutex. |
| 177 | * |
| 178 | * Deprecated: use VK_KHR_internally_synchronized_queues. |
| 179 | */ |
| 180 | attribute_deprecated |
| 181 | void (*lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index); |
| 182 |
| 183 | /** |
| 184 | * Similar to lock_queue(), unlocks a queue. Must only be called after locking. |
| 185 | * |
| 186 | * Deprecated: use VK_KHR_internally_synchronized_queues. |
| 187 | */ |
| 188 | attribute_deprecated |
| 189 | void (*unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index); |
| 190 | #endif |
| 191 |
| 192 | /** |
| 193 | * Queue families used. Must be preferentially ordered. List may contain |
| 194 | * duplicates. |
| 195 | * |
| 196 | * For compatibility reasons, all the enabled queue families listed above |
| 197 | * (queue_family_(tx/comp/encode/decode)_index) must also be included in |
| 198 | * this list until they're removed after deprecation. |
| 199 | */ |
| 200 | AVVulkanDeviceQueueFamily qf[64]; |
| 201 | int nb_qf; |
| 202 | } AVVulkanDeviceContext; |
| 203 |
| 204 | /** |
| 205 | * Defines the behaviour of frame allocation. |
| 206 | */ |
| 207 | typedef enum AVVkFrameFlags { |
| 208 | /* Unless this flag is set, autodetected flags will be OR'd based on the |
| 209 | * device and tiling during av_hwframe_ctx_init(). */ |
| 210 | AV_VK_FRAME_FLAG_NONE = (1ULL << 0), |
| 211 |
| 212 | /* Disables multiplane images. |
| 213 | * This is required to export/import images from CUDA. */ |
| 214 | AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE = (1ULL << 2), |
| 215 | } AVVkFrameFlags; |
| 216 |
| 217 | /** |
| 218 | * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options |
| 219 | */ |
| 220 | typedef struct AVVulkanFramesContext { |
| 221 | /** |
| 222 | * Controls the tiling of allocated frames. |
| 223 | * If left as VK_IMAGE_TILING_OPTIMAL (0), will use optimal tiling. |
| 224 | * Can be set to VK_IMAGE_TILING_LINEAR to force linear images, |
| 225 | * or VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT to force DMABUF-backed |
| 226 | * images. |
| 227 | * @note Imported frames from other APIs ignore this. |
| 228 | */ |
| 229 | VkImageTiling tiling; |
| 230 |
| 231 | /** |
| 232 | * Defines extra usage of output frames. If non-zero, all flags MUST be |
| 233 | * supported by the VkFormat. Regardless, frames will always have the |
| 234 | * following usage flags enabled, if supported by the format: |
| 235 | * - VK_IMAGE_USAGE_SAMPLED_BIT |
| 236 | * - VK_IMAGE_USAGE_STORAGE_BIT |
| 237 | * - VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
| 238 | * - VK_IMAGE_USAGE_TRANSFER_DST_BIT |
| 239 | */ |
| 240 | VkImageUsageFlagBits usage; |
| 241 |
| 242 | /** |
| 243 | * Extension data for image creation. |
| 244 | * If DRM tiling is used, a VkImageDrmFormatModifierListCreateInfoEXT structure |
| 245 | * can be added to specify the exact modifier to use. |
| 246 | * |
| 247 | * Additional structures may be added at av_hwframe_ctx_init() time, |
| 248 | * which will be freed automatically on uninit(), so users must only free |
| 249 | * any structures they've allocated themselves. |
| 250 | */ |
| 251 | void *create_pnext; |
| 252 |
| 253 | /** |
| 254 | * Extension data for memory allocation. Must have as many entries as |
| 255 | * the number of planes of the sw_format. |
| 256 | * This will be chained to VkExportMemoryAllocateInfo, which is used |
| 257 | * to make all pool images exportable to other APIs if the necessary |
| 258 | * extensions are present in enabled_dev_extensions. |
| 259 | */ |
| 260 | void *alloc_pnext[AV_NUM_DATA_POINTERS]; |
| 261 |
| 262 | /** |
| 263 | * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set, |
| 264 | * autodetected flags will be OR'd based on the device and tiling during |
| 265 | * av_hwframe_ctx_init(). |
| 266 | */ |
| 267 | AVVkFrameFlags flags; |
| 268 |
| 269 | /** |
| 270 | * Flags to set during image creation. If unset, defaults to |
| 271 | * VK_IMAGE_CREATE_ALIAS_BIT. |
| 272 | */ |
| 273 | VkImageCreateFlags img_flags; |
| 274 |
| 275 | /** |
| 276 | * Vulkan format for each image. MUST be compatible with the pixel format. |
| 277 | * If unset, will be automatically set. |
| 278 | * There are at most two compatible formats for a frame - a multiplane |
| 279 | * format, and a single-plane multi-image format. |
| 280 | */ |
| 281 | VkFormat format[AV_NUM_DATA_POINTERS]; |
| 282 |
| 283 | /** |
| 284 | * Number of layers each image will have. |
| 285 | */ |
| 286 | int nb_layers; |
| 287 |
| 288 | /** |
| 289 | * Locks a frame, preventing other threads from changing frame properties. |
| 290 | * Users SHOULD only ever lock just before command submission in order |
| 291 | * to get accurate frame properties, and unlock immediately after command |
| 292 | * submission without waiting for it to finish. |
| 293 | * |
| 294 | * If unset, will be set to lavu-internal functions that utilize a mutex. |
| 295 | */ |
| 296 | void (*lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf); |
| 297 |
| 298 | /** |
| 299 | * Similar to lock_frame(), unlocks a frame. Must only be called after locking. |
| 300 | */ |
| 301 | void (*unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf); |
| 302 | } AVVulkanFramesContext; |
| 303 |
| 304 | /* |
| 305 | * Frame structure. |
| 306 | * |
| 307 | * @note the size of this structure is not part of the ABI, to allocate |
| 308 | * you must use @av_vk_frame_alloc(). |
| 309 | */ |
| 310 | struct AVVkFrame { |
| 311 | /** |
| 312 | * Vulkan images to which the memory is bound to. |
| 313 | * May be one for multiplane formats, or multiple. |
| 314 | */ |
| 315 | VkImage img[AV_NUM_DATA_POINTERS]; |
| 316 |
| 317 | /** |
| 318 | * Tiling for the frame. |
| 319 | */ |
| 320 | VkImageTiling tiling; |
| 321 |
| 322 | /** |
| 323 | * Memory backing the images. Either one, or as many as there are planes |
| 324 | * in the sw_format. |
| 325 | * In case of having multiple VkImages, but one memory, the offset field |
| 326 | * will indicate the bound offset for each image. |
| 327 | */ |
| 328 | VkDeviceMemory mem[AV_NUM_DATA_POINTERS]; |
| 329 | size_t size[AV_NUM_DATA_POINTERS]; |
| 330 |
| 331 | /** |
| 332 | * OR'd flags for all memory allocated |
| 333 | */ |
| 334 | VkMemoryPropertyFlagBits flags; |
| 335 |
| 336 | /** |
| 337 | * Updated after every barrier. One per VkImage. |
| 338 | */ |
| 339 | VkAccessFlagBits access[AV_NUM_DATA_POINTERS]; |
| 340 | VkImageLayout layout[AV_NUM_DATA_POINTERS]; |
| 341 |
| 342 | /** |
| 343 | * Synchronization timeline semaphores, one for each VkImage. |
| 344 | * Must not be freed manually. Must be waited on at every submission using |
| 345 | * the value in sem_value, and must be signalled at every submission, |
| 346 | * using an incremented value. |
| 347 | */ |
| 348 | VkSemaphore sem[AV_NUM_DATA_POINTERS]; |
| 349 |
| 350 | /** |
| 351 | * Up to date semaphore value at which each image becomes accessible. |
| 352 | * One per VkImage. |
| 353 | * Clients must wait on this value when submitting a command queue, |
| 354 | * and increment it when signalling. |
| 355 | */ |
| 356 | uint64_t sem_value[AV_NUM_DATA_POINTERS]; |
| 357 |
| 358 | /** |
| 359 | * Internal data. |
| 360 | */ |
| 361 | struct AVVkFrameInternal *internal; |
| 362 |
| 363 | /** |
| 364 | * Describes the binding offset of each image to the VkDeviceMemory. |
| 365 | * One per VkImage. |
| 366 | */ |
| 367 | ptrdiff_t offset[AV_NUM_DATA_POINTERS]; |
| 368 |
| 369 | /** |
| 370 | * Queue family of the images. Must be VK_QUEUE_FAMILY_IGNORED if |
| 371 | * the image was allocated with the CONCURRENT concurrency option. |
| 372 | * One per VkImage. |
| 373 | */ |
| 374 | uint32_t queue_family[AV_NUM_DATA_POINTERS]; |
| 375 | }; |
| 376 |
| 377 | /** |
| 378 | * Allocates a single AVVkFrame and initializes everything as 0. |
| 379 | * @note Must be freed via av_free() |
| 380 | */ |
| 381 | AVVkFrame *av_vk_frame_alloc(void); |
| 382 |
| 383 | /** |
| 384 | * Returns the optimal per-plane Vulkan format for a given sw_format, |
| 385 | * one for each plane. |
| 386 | * Returns NULL on unsupported formats. |
| 387 | */ |
| 388 | const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p); |
| 389 |
| 390 | /** |
| 391 | * Returns an array of optional Vulkan instance extensions that FFmpeg |
| 392 | * may use if enabled. |
| 393 | * @note Must be freed via av_free() |
| 394 | */ |
| 395 | const char **av_vk_get_optional_instance_extensions(int *count); |
| 396 |
| 397 | /** |
| 398 | * Returns an array of optional Vulkan device extensions that FFmpeg |
| 399 | * may use if enabled. |
| 400 | * @note Must be freed via av_free() |
| 401 | */ |
| 402 | const char **av_vk_get_optional_device_extensions(int *count); |
| 403 |
| 404 | #endif /* AVUTIL_HWCONTEXT_VULKAN_H */ |
| 405 |