2019-08-28 20:58:10 +00:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVUTIL_HWCONTEXT_VULKAN_H
|
|
|
|
#define AVUTIL_HWCONTEXT_VULKAN_H
|
|
|
|
|
2021-11-13 17:59:07 +00:00
|
|
|
#if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
|
|
|
|
#define VK_USE_PLATFORM_WIN32_KHR
|
|
|
|
#endif
|
2019-08-28 20:58:10 +00:00
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
|
2020-05-30 12:41:31 +00:00
|
|
|
#include "pixfmt.h"
|
|
|
|
#include "frame.h"
|
|
|
|
|
2022-03-15 22:00:32 +00:00
|
|
|
typedef struct AVVkFrame AVVkFrame;
|
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* API-specific header for AV_HWDEVICE_TYPE_VULKAN.
|
|
|
|
*
|
|
|
|
* For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
|
|
|
|
* with the data pointer set to an AVVkFrame.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
|
|
|
|
* All of these can be set before init to change what the context uses
|
|
|
|
*/
|
|
|
|
typedef struct AVVulkanDeviceContext {
|
|
|
|
/**
|
|
|
|
* Custom memory allocator, else NULL
|
|
|
|
*/
|
|
|
|
const VkAllocationCallbacks *alloc;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2021-04-29 00:44:41 +00:00
|
|
|
/**
|
|
|
|
* Pointer to the instance-provided vkGetInstanceProcAddr loading function.
|
|
|
|
* If NULL, will pick either libvulkan or libvolk, depending on libavutil's
|
|
|
|
* compilation settings, and set this field.
|
|
|
|
*/
|
|
|
|
PFN_vkGetInstanceProcAddr get_proc_addr;
|
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2022-11-23 14:15:04 +00:00
|
|
|
* Vulkan instance. Must be at least version 1.3.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkInstance inst;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Physical device
|
|
|
|
*/
|
|
|
|
VkPhysicalDevice phys_dev;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Active device
|
|
|
|
*/
|
|
|
|
VkDevice act_dev;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This structure should be set to the set of features that present and enabled
|
|
|
|
* during device creation. When a device is created by FFmpeg, it will default to
|
|
|
|
* enabling all that are present of the shaderImageGatherExtended,
|
|
|
|
* fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
|
|
|
|
*/
|
|
|
|
VkPhysicalDeviceFeatures2 device_features;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enabled instance extensions.
|
|
|
|
* If supplying your own device context, set this to an array of strings, with
|
|
|
|
* each entry containing the specified Vulkan extension string to enable.
|
|
|
|
* Duplicates are possible and accepted.
|
|
|
|
* If no extensions are enabled, set these fields to NULL, and 0 respectively.
|
|
|
|
*/
|
|
|
|
const char * const *enabled_inst_extensions;
|
|
|
|
int nb_enabled_inst_extensions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enabled device extensions. By default, VK_KHR_external_memory_fd,
|
|
|
|
* VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
|
|
|
|
* VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
|
|
|
|
* If supplying your own device context, these fields takes the same format as
|
|
|
|
* the above fields, with the same conditions that duplicates are possible
|
|
|
|
* and accepted, and that NULL and 0 respectively means no extensions are enabled.
|
|
|
|
*/
|
|
|
|
const char * const *enabled_dev_extensions;
|
|
|
|
int nb_enabled_dev_extensions;
|
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2021-11-07 07:16:11 +00:00
|
|
|
* Queue family index for graphics operations, and the number of queues
|
|
|
|
* enabled for it. If unavaiable, will be set to -1. Not required.
|
|
|
|
* av_hwdevice_create() will attempt to find a dedicated queue for each
|
|
|
|
* queue family, or pick the one with the least unrelated flags set.
|
|
|
|
* Queue indices here may overlap if a queue has to share capabilities.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
int queue_family_index;
|
2020-05-13 15:20:15 +00:00
|
|
|
int nb_graphics_queues;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2021-11-07 07:16:11 +00:00
|
|
|
* Queue family index for transfer operations and the number of queues
|
|
|
|
* enabled. Required.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
int queue_family_tx_index;
|
2020-05-13 15:20:15 +00:00
|
|
|
int nb_tx_queues;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2021-11-07 07:16:11 +00:00
|
|
|
* Queue family index for compute operations and the number of queues
|
|
|
|
* enabled. Required.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
int queue_family_comp_index;
|
2020-05-13 15:20:15 +00:00
|
|
|
int nb_comp_queues;
|
2021-11-07 07:16:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue family index for video encode ops, and the amount of queues enabled.
|
|
|
|
* If the device doesn't support such, queue_family_encode_index will be -1.
|
|
|
|
* Not required.
|
|
|
|
*/
|
|
|
|
int queue_family_encode_index;
|
|
|
|
int nb_encode_queues;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue family index for video decode ops, and the amount of queues enabled.
|
|
|
|
* If the device doesn't support such, queue_family_decode_index will be -1.
|
|
|
|
* Not required.
|
|
|
|
*/
|
|
|
|
int queue_family_decode_index;
|
|
|
|
int nb_decode_queues;
|
2022-03-15 22:00:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Locks a queue, preventing other threads from submitting any command
|
|
|
|
* buffers to this queue.
|
|
|
|
* If set to NULL, will be set to lavu-internal functions that utilize a
|
|
|
|
* mutex.
|
|
|
|
*/
|
|
|
|
void (*lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to lock_queue(), unlocks a queue. Must only be called after locking.
|
|
|
|
*/
|
|
|
|
void (*unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
|
2019-08-28 20:58:10 +00:00
|
|
|
} AVVulkanDeviceContext;
|
|
|
|
|
2021-12-07 09:05:51 +00:00
|
|
|
/**
|
|
|
|
* Defines the behaviour of frame allocation.
|
|
|
|
*/
|
|
|
|
typedef enum AVVkFrameFlags {
|
|
|
|
/* Unless this flag is set, autodetected flags will be OR'd based on the
|
|
|
|
* device and tiling during av_hwframe_ctx_init(). */
|
|
|
|
AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
|
|
|
|
|
2022-11-23 19:32:49 +00:00
|
|
|
#if FF_API_VULKAN_CONTIGUOUS_MEMORY
|
|
|
|
/* DEPRECATED: does nothing. Replaced by multiplane images. */
|
2021-12-07 09:05:51 +00:00
|
|
|
AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY = (1ULL << 1),
|
2022-11-23 19:32:49 +00:00
|
|
|
#endif
|
2023-01-11 08:37:35 +00:00
|
|
|
|
|
|
|
/* Disables multiplane images.
|
|
|
|
* This is required to export/import images from CUDA. */
|
|
|
|
AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE = (1ULL << 2),
|
2021-12-07 09:05:51 +00:00
|
|
|
} AVVkFrameFlags;
|
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
|
|
|
|
*/
|
|
|
|
typedef struct AVVulkanFramesContext {
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Controls the tiling of allocated frames.
|
|
|
|
* If left as VK_IMAGE_TILING_OPTIMAL (0), will use optimal tiling.
|
|
|
|
* Can be set to VK_IMAGE_TILING_LINEAR to force linear images,
|
|
|
|
* or VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT to force DMABUF-backed
|
|
|
|
* images.
|
|
|
|
* @note Imported frames from other APIs ignore this.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkImageTiling tiling;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Defines extra usage of output frames. If non-zero, all flags MUST be
|
|
|
|
* supported by the VkFormat. Otherwise, will use supported flags amongst:
|
|
|
|
* - VK_IMAGE_USAGE_SAMPLED_BIT
|
|
|
|
* - VK_IMAGE_USAGE_STORAGE_BIT
|
|
|
|
* - VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
|
|
|
* - VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkImageUsageFlagBits usage;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
2020-05-22 18:54:00 +00:00
|
|
|
* Extension data for image creation.
|
2023-01-11 08:37:35 +00:00
|
|
|
* If DRM tiling is used, a VkImageDrmFormatModifierListCreateInfoEXT structure
|
|
|
|
* can be added to specify the exact modifier to use.
|
|
|
|
*
|
2021-12-07 09:05:53 +00:00
|
|
|
* Additional structures may be added at av_hwframe_ctx_init() time,
|
2023-01-11 08:37:35 +00:00
|
|
|
* which will be freed automatically on uninit(), so users must only free
|
2021-12-07 09:05:53 +00:00
|
|
|
* any structures they've allocated themselves.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
void *create_pnext;
|
2021-04-27 23:39:25 +00:00
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Extension data for memory allocation. Must have as many entries as
|
|
|
|
* the number of planes of the sw_format.
|
|
|
|
* This will be chained to VkExportMemoryAllocateInfo, which is used
|
2020-05-22 18:54:00 +00:00
|
|
|
* to make all pool images exportable to other APIs if the necessary
|
|
|
|
* extensions are present in enabled_dev_extensions.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
void *alloc_pnext[AV_NUM_DATA_POINTERS];
|
2021-12-07 09:05:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
|
|
|
|
* autodetected flags will be OR'd based on the device and tiling during
|
|
|
|
* av_hwframe_ctx_init().
|
|
|
|
*/
|
|
|
|
AVVkFrameFlags flags;
|
2022-03-15 22:00:32 +00:00
|
|
|
|
2023-01-11 08:37:35 +00:00
|
|
|
/**
|
|
|
|
* Flags to set during image creation. If unset, defaults to
|
|
|
|
* VK_IMAGE_CREATE_ALIAS_BIT.
|
|
|
|
*/
|
|
|
|
VkImageCreateFlags img_flags;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Vulkan format for each image. MUST be compatible with the pixel format.
|
|
|
|
* If unset, will be automatically set.
|
|
|
|
* There are at most two compatible formats for a frame - a multiplane
|
|
|
|
* format, and a single-plane multi-image format.
|
|
|
|
*/
|
|
|
|
VkFormat format[AV_NUM_DATA_POINTERS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of layers each image will have.
|
|
|
|
*/
|
|
|
|
int nb_layers;
|
|
|
|
|
2022-03-15 22:00:32 +00:00
|
|
|
/**
|
|
|
|
* Locks a frame, preventing other threads from changing frame properties.
|
|
|
|
* Users SHOULD only ever lock just before command submission in order
|
|
|
|
* to get accurate frame properties, and unlock immediately after command
|
|
|
|
* submission without waiting for it to finish.
|
|
|
|
*
|
|
|
|
* If unset, will be set to lavu-internal functions that utilize a mutex.
|
|
|
|
*/
|
|
|
|
void (*lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to lock_frame(), unlocks a frame. Must only be called after locking.
|
|
|
|
*/
|
|
|
|
void (*unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
|
2019-08-28 20:58:10 +00:00
|
|
|
} AVVulkanFramesContext;
|
|
|
|
|
|
|
|
/*
|
2023-01-11 08:37:35 +00:00
|
|
|
* Frame structure.
|
2020-05-10 20:53:39 +00:00
|
|
|
*
|
2019-08-28 20:58:10 +00:00
|
|
|
* @note the size of this structure is not part of the ABI, to allocate
|
|
|
|
* you must use @av_vk_frame_alloc().
|
|
|
|
*/
|
2022-03-15 22:00:32 +00:00
|
|
|
struct AVVkFrame {
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Vulkan images to which the memory is bound to.
|
2023-01-11 08:37:35 +00:00
|
|
|
* May be one for multiplane formats, or multiple.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkImage img[AV_NUM_DATA_POINTERS];
|
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Tiling for the frame.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkImageTiling tiling;
|
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Memory backing the images. Either one, or as many as there are planes
|
|
|
|
* in the sw_format.
|
|
|
|
* In case of having multiple VkImages, but one memory, the offset field
|
|
|
|
* will indicate the bound offset for each image.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
|
|
|
|
size_t size[AV_NUM_DATA_POINTERS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OR'd flags for all memory allocated
|
|
|
|
*/
|
|
|
|
VkMemoryPropertyFlagBits flags;
|
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Updated after every barrier. One per VkImage.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
|
|
|
|
VkImageLayout layout[AV_NUM_DATA_POINTERS];
|
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Synchronization timeline semaphores, one for each VkImage.
|
2021-12-10 15:29:03 +00:00
|
|
|
* Must not be freed manually. Must be waited on at every submission using
|
|
|
|
* the value in sem_value, and must be signalled at every submission,
|
|
|
|
* using an incremented value.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
2020-05-11 22:27:01 +00:00
|
|
|
VkSemaphore sem[AV_NUM_DATA_POINTERS];
|
2019-08-28 20:58:10 +00:00
|
|
|
|
2021-11-04 11:17:06 +00:00
|
|
|
/**
|
|
|
|
* Up to date semaphore value at which each image becomes accessible.
|
2023-01-11 08:37:35 +00:00
|
|
|
* One per VkImage.
|
2021-11-04 11:17:06 +00:00
|
|
|
* Clients must wait on this value when submitting a command queue,
|
|
|
|
* and increment it when signalling.
|
|
|
|
*/
|
|
|
|
uint64_t sem_value[AV_NUM_DATA_POINTERS];
|
|
|
|
|
2019-08-28 20:58:10 +00:00
|
|
|
/**
|
|
|
|
* Internal data.
|
|
|
|
*/
|
|
|
|
struct AVVkFrameInternal *internal;
|
2021-12-07 09:05:51 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Describes the binding offset of each image to the VkDeviceMemory.
|
|
|
|
* One per VkImage.
|
2021-12-07 09:05:51 +00:00
|
|
|
*/
|
|
|
|
ptrdiff_t offset[AV_NUM_DATA_POINTERS];
|
2022-03-15 22:00:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue family of the images. Must be VK_QUEUE_FAMILY_IGNORED if
|
|
|
|
* the image was allocated with the CONCURRENT concurrency option.
|
2023-01-11 08:37:35 +00:00
|
|
|
* One per VkImage.
|
2022-03-15 22:00:32 +00:00
|
|
|
*/
|
|
|
|
uint32_t queue_family[AV_NUM_DATA_POINTERS];
|
2023-01-11 08:37:35 +00:00
|
|
|
};
|
2019-08-28 20:58:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a single AVVkFrame and initializes everything as 0.
|
|
|
|
* @note Must be freed via av_free()
|
|
|
|
*/
|
|
|
|
AVVkFrame *av_vk_frame_alloc(void);
|
|
|
|
|
|
|
|
/**
|
2023-01-11 08:37:35 +00:00
|
|
|
* Returns the optimal per-plane Vulkan format for a given sw_format,
|
|
|
|
* one for each plane.
|
2020-05-22 18:54:00 +00:00
|
|
|
* Returns NULL on unsupported formats.
|
2019-08-28 20:58:10 +00:00
|
|
|
*/
|
|
|
|
const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
|
|
|
|
|
|
|
|
#endif /* AVUTIL_HWCONTEXT_VULKAN_H */
|