Go to the documentation of this file.
19 #ifndef AVUTIL_VULKAN_H
20 #define AVUTIL_VULKAN_H
22 #define VK_NO_PROTOTYPES
24 #include <stdatomic.h>
39 if ((err = (x)) < 0) \
44 #define SPEC_LIST_MAX 256
45 #define SPEC_LIST_CREATE(name, max_length, max_size) \
46 av_assert1((max_length) < (SPEC_LIST_MAX - 3)); \
47 uint8_t name##_data[(max_size) + 3*sizeof(uint32_t)]; \
48 VkSpecializationMapEntry name##_entries[(max_length) + 3]; \
49 VkSpecializationInfo name##_info = { \
50 .pMapEntries = name##_entries, \
51 .pData = name##_data, \
53 VkSpecializationInfo *name = &name##_info;
55 #define SPEC_LIST_ADD(name, idx, val_bits, val) \
57 unsigned int name##_cnt = name->mapEntryCount; \
58 size_t name##_off = name->dataSize; \
59 uint8_t *name##_dp = (uint8_t *)name->pData; \
60 void *name##_ep = (void *)&name->pMapEntries[name##_cnt]; \
61 AV_WN(val_bits, name##_dp + name##_off, (val)); \
62 VkSpecializationMapEntry name##_new_entry = { \
63 .constantID = (idx), \
64 .offset = name##_off, \
65 .size = val_bits >> 3, \
67 memcpy(name##_ep, &name##_new_entry, \
68 sizeof(VkSpecializationMapEntry)); \
69 name->dataSize = name##_off + (val_bits >> 3); \
70 name->mapEntryCount = name##_cnt + 1; \
73 #define DUP_SAMPLER(x) { x, x, x, x }
75 #define FF_VK_MAX_DESCRIPTOR_SETS 4
76 #define FF_VK_MAX_DESCRIPTOR_BINDINGS 16
77 #define FF_VK_MAX_DESCRIPTOR_TYPES 16
78 #define FF_VK_MAX_PUSH_CONSTS 4
79 #define FF_VK_MAX_SHADERS 16
115 #define FF_VK_EXEC_MAX_FRAME_DEPS 64
116 #define FF_VK_EXEC_MAX_BUF_DEPS 256
117 #define FF_VK_EXEC_MAX_SW_FRAME_DEPS 16
118 #define FF_VK_EXEC_MAX_SEM_OPS (FF_VK_EXEC_MAX_FRAME_DEPS*AV_NUM_DATA_POINTERS)
121 #define FF_VK_DEFAULT_EXEC_CONTEXTS 4
300 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
hprops;
306 #ifdef VK_EXT_shader_long_vector
307 VkPhysicalDeviceShaderLongVectorPropertiesEXT long_vector_props;
357 const VkBaseInStructure *in = chain;
359 if (in->sType == stype)
370 VkBaseOutStructure *
out = chain;
374 out->pNext = (
void *)in;
377 #define FF_VK_STRUCT_EXT(CTX, BASE, STRUCT_P, EXT_FLAG, TYPE) \
379 if ((EXT_FLAG == FF_VK_EXT_NO_FLAG) || \
380 ((CTX)->extensions & EXT_FLAG)) { \
381 (STRUCT_P)->sType = TYPE; \
382 ff_vk_link_struct(BASE, STRUCT_P); \
449 VkQueueFlagBits dev_family,
450 VkVideoCodecOperationFlagBitsKHR vid_ops);
461 int nb_queries, VkQueryType query_type,
int query_64bit,
462 const void *query_create_pnext);
476 void **
data, VkQueryResultFlagBits
flags);
510 VkObjectType
type, uint64_t obj);
513 VkSemaphore sem, uint64_t
val,
514 VkPipelineStageFlagBits2 stage);
516 VkSemaphore *sem,
int nb,
517 VkPipelineStageFlagBits2 stage,
520 VkPipelineStageFlagBits2 wait_stage,
521 VkPipelineStageFlagBits2 signal_stage);
525 VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar);
527 VkSemaphore *
dst, uint64_t *dst_val,
535 VkImageView *img_view, VkImageAspectFlags *aspect,
562 #define ff_vk_buf_barrier(dst, vkb, s_stage, s_access, s_access2, \
563 d_stage, d_access, d_access2, offs, bsz) \
565 dst = (VkBufferMemoryBarrier2) { \
566 .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2, \
567 .srcStageMask = VK_PIPELINE_STAGE_2_ ##s_stage, \
568 .srcAccessMask = VK_ACCESS_2_ ##s_access | \
569 VK_ACCESS_2_ ##s_access2, \
570 .dstStageMask = VK_PIPELINE_STAGE_2_ ##d_stage, \
571 .dstAccessMask = VK_ACCESS_2_ ##d_access | \
572 VK_ACCESS_2_ ##d_access2, \
573 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, \
574 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, \
575 .buffer = vkb->buf, \
582 AVFrame *pic, VkImageMemoryBarrier2 *bar,
int *nb_bar,
583 VkPipelineStageFlags2 src_stage,
584 VkPipelineStageFlags2 dst_stage,
585 VkAccessFlagBits2 new_access,
586 VkImageLayout new_layout,
593 VkMemoryPropertyFlagBits req_flags,
void *alloc_extension,
594 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem);
596 void *pNext,
void *alloc_pNext,
597 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags);
603 VkDeviceSize
offset, VkDeviceSize mem_size,
610 int nb_buffers,
int invalidate);
634 void *create_pNext,
size_t size,
635 VkMemoryPropertyFlagBits mem_props);
642 uint8_t *src_data, VkDeviceSize
size,
644 VkBufferUsageFlags
usage);
650 int unnorm_coords, VkFilter
filt);
658 VkPipelineStageFlags stage, VkSpecializationInfo *spec,
659 uint32_t wg_size[3], uint32_t required_subgroup_size);
665 const char *spirv,
size_t spirv_len,
666 const char *entrypoint);
672 VkShaderStageFlagBits stage);
700 VkShaderStageFlagBits stage,
709 int set,
int bind,
int elem,
718 VkImageView view, VkImageLayout
layout,
727 VkImageView *views,
int set,
int binding,
728 VkImageLayout
layout, VkSampler sampler);
VkPipelineBindPoint bind_point
const char * ff_vk_ret2str(VkResult res)
Converts Vulkan return values to strings.
int ff_vk_map_buffers(FFVulkanContext *s, FFVkBuffer **buf, uint8_t *mem[], int nb_buffers, int invalidate)
Buffer management code.
int ff_vk_unmap_buffers(FFVulkanContext *s, FFVkBuffer **buf, int nb_buffers, int flush)
AVPixelFormat
Pixel format.
void ff_vk_exec_discard_deps(FFVulkanContext *s, FFVkExecContext *e)
VkFormatFeatureFlagBits2 ff_vk_map_usage_to_feats(VkImageUsageFlags usage)
AVVulkanFramesContext * hwfc
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
int ff_vk_exec_add_dep_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkPipelineStageFlagBits2 wait_stage, VkPipelineStageFlagBits2 signal_stage)
#define FF_VK_EXEC_MAX_BUF_DEPS
#define FF_VK_EXEC_MAX_FRAME_DEPS
VkPhysicalDeviceVulkan11Properties props_11
uint64_t FFVulkanExtensions
int ff_vk_load_props(FFVulkanContext *s)
Loads props/mprops/driver_props.
int ff_vk_exec_pool_init(FFVulkanContext *s, AVVulkanDeviceQueueFamily *qf, FFVkExecPool *pool, int nb_contexts, int nb_queries, VkQueryType query_type, int query_64bit, const void *query_create_pnext)
Allocates/frees an execution pool.
int ff_vk_exec_add_dep_sw_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f)
FFVkExecContext * contexts
atomic_uint_least64_t idx
int ff_vk_get_pooled_buffer(FFVulkanContext *ctx, AVRefStructPool **buf_pool, FFVkBuffer **buf, VkBufferUsageFlags usage, void *create_pNext, size_t size, VkMemoryPropertyFlagBits mem_props)
Initialize a pool and create AVBufferRefs containing FFVkBuffer.
VkDeviceSize aligned_size
int ff_vk_init(FFVulkanContext *s, void *log_parent, AVBufferRef *device_ref, AVBufferRef *frames_ref)
Initializes the AVClass, in case this context is not used as the main user's context.
VkDescriptorPoolSize desc_pool_size[FF_VK_MAX_DESCRIPTOR_TYPES]
FFVulkanDescriptorSetData desc_set_buf[FF_VK_MAX_DESCRIPTOR_SETS]
const VkAllocationCallbacks * alloc
This structure describes decoded (raw) audio or video data.
VkShaderStageFlags stages
static int ff_vk_map_buffer(FFVulkanContext *s, FFVkBuffer *buf, uint8_t **mem, int invalidate)
VkPipelineShaderStageRequiredSubgroupSizeCreateInfo subgroup_info
static const void * ff_vk_find_struct(const void *chain, VkStructureType stype)
#define FF_VK_MAX_SHADERS
AVFrame * frame_deps[FF_VK_EXEC_MAX_FRAME_DEPS]
int ff_vk_host_map_buffer(FFVulkanContext *s, FFVkBuffer **dst, uint8_t *src_data, VkDeviceSize size, const AVBufferRef *src_buf, VkBufferUsageFlags usage)
Maps a system RAM buffer into a Vulkan buffer.
VkDescriptorSet * desc_sets
FFVkShaderRepFormat
Returns the format to use for images in shaders.
void * refstruct_deps[FF_VK_EXEC_MAX_BUF_DEPS]
void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd)
Free a shader.
int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, FFVulkanShader *shd)
Register a shader with an exec pool.
const char * ff_vk_shader_rep_fmt(enum AVPixelFormat pix_fmt, enum FFVkShaderRepFormat rep_fmt)
int ff_vk_flush_buffer(FFVulkanContext *s, FFVkBuffer *buf, VkDeviceSize offset, VkDeviceSize mem_size, int flush)
Flush or invalidate a single buffer, with a given size and offset.
VkPhysicalDeviceFeatures2 feats
void ff_vk_exec_add_dep_obj(FFVulkanContext *s, FFVkExecContext *e, VkObjectType type, uint64_t obj)
int ff_vk_exec_mirror_sem_value(FFVulkanContext *s, FFVkExecContext *e, VkSemaphore *dst, uint64_t *dst_val, AVFrame *f)
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
void ff_vk_exec_add_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Execution dependency management.
VkPhysicalDeviceSubgroupSizeControlProperties subgroup_props
static double val(void *priv, double ch)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
VkPhysicalDeviceShaderAtomicFloatFeaturesEXT atomic_float_feats
int ff_vk_shader_link(FFVulkanContext *s, FFVulkanShader *shd, const char *spirv, size_t spirv_len, const char *entrypoint)
Link a shader into an executable.
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
VkResult ff_vk_exec_get_query(FFVulkanContext *s, FFVkExecContext *e, void **data, VkQueryResultFlagBits flags)
Performs nb_queries queries and returns their results and statuses.
VkSpecializationInfo * specialization_info
VkDescriptorBufferBindingInfoEXT desc_bind[FF_VK_MAX_DESCRIPTOR_SETS]
#define FF_ARRAY_ELEMS(a)
static void ff_vk_link_struct(void *chain, const void *in)
static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v)
PFN_vkDestroyImageView destroy_image_view
#define flags(name, subs,...)
int ff_vk_shader_update_desc_buffer(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, int set, int bind, int elem, FFVkBuffer *buf, VkDeviceSize offset, VkDeviceSize len, VkFormat fmt)
Update a descriptor in a buffer with a buffer.
void ff_vk_exec_bind_shader(FFVulkanContext *s, FFVkExecContext *e, const FFVulkanShader *shd)
Bind a shader.
VkPhysicalDeviceDriverProperties driver_props
static enum AVPixelFormat pix_fmt
FFVulkanDescriptorSet desc_set[FF_VK_MAX_DESCRIPTOR_SETS]
void ff_vk_free_buf(FFVulkanContext *s, FFVkBuffer *buf)
VkMemoryPropertyFlagBits host_cached_flag
VkImageView views[AV_NUM_DATA_POINTERS]
VkPipelineStageFlags stage
AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.
static AVFormatContext * ctx
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, void *pNext, void *alloc_pNext, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
#define FF_VK_MAX_DESCRIPTOR_BINDINGS
const struct FFVkExecPool * parent
void ff_vk_shader_update_push_const(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, VkShaderStageFlagBits stage, int offset, size_t size, void *src)
Update push constant in a shader.
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
VkDeviceSize binding_offset[FF_VK_MAX_DESCRIPTOR_BINDINGS]
Describe the class of an AVClass context structure.
int ff_vk_create_imageviews(FFVulkanContext *s, FFVkExecContext *e, VkImageView views[AV_NUM_DATA_POINTERS], AVFrame *f, enum FFVkShaderRepFormat rep_fmt)
Create an imageview and add it as a dependency to an execution.
void ff_vk_frame_barrier(FFVulkanContext *s, FFVkExecContext *e, AVFrame *pic, VkImageMemoryBarrier2 *bar, int *nb_bar, VkPipelineStageFlags2 src_stage, VkPipelineStageFlags2 dst_stage, VkAccessFlagBits2 new_access, VkImageLayout new_layout, uint32_t new_qf)
FFVkImageViews * ff_vk_imageviews_alloc(FFVulkanContext *s, int nb_views)
Allocate a reference-counted set of image views, zero-initialized for the caller to create.
VkImageLayout layout_dst[FF_VK_EXEC_MAX_FRAME_DEPS]
void ff_vk_exec_add_dep_wait_sem(FFVulkanContext *s, FFVkExecContext *e, VkSemaphore sem, uint64_t val, VkPipelineStageFlagBits2 stage)
VkPhysicalDeviceCooperativeMatrixPropertiesKHR coop_matrix_props
VkQueueFamilyProperties2 * qf_props
#define FF_VK_EXEC_MAX_SEM_OPS
void(* flush)(AVBSFContext *ctx)
int ff_vk_init_sampler(FFVulkanContext *s, VkSampler *sampler, int unnorm_coords, VkFilter filt)
Create a sampler.
uint64_t * sem_sig_val_dst[FF_VK_EXEC_MAX_SEM_OPS]
int ff_vk_shader_add_push_const(FFVulkanShader *shd, int offset, int size, VkShaderStageFlagBits stage)
Add/update push constants for execution.
void ff_vk_exec_wait(FFVulkanContext *s, FFVkExecContext *e)
AVHWDeviceContext * device
int ff_vk_exec_start(FFVulkanContext *s, FFVkExecContext *e)
Start/submit/wait an execution.
void ff_vk_exec_add_dep_bool_sem(FFVulkanContext *s, FFVkExecContext *e, VkSemaphore *sem, int nb, VkPipelineStageFlagBits2 stage, int wait)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
VkPhysicalDeviceHostImageCopyPropertiesEXT host_image_props
AVRefStructPool * imageviews_pool
#define AV_NUM_DATA_POINTERS
int ff_vk_create_imageview(FFVulkanContext *s, VkImageView *img_view, VkImageAspectFlags *aspect, AVFrame *f, int plane, enum FFVkShaderRepFormat rep_fmt)
Create a single imageview for a given plane.
VkAccessFlagBits access_dst[FF_VK_EXEC_MAX_FRAME_DEPS]
VkPipelineLayout pipeline_layout
enum AVPixelFormat output_format
VkDescriptorSetLayout desc_layout[FF_VK_MAX_DESCRIPTOR_SETS]
VkMemoryPropertyFlagBits flags
VkImageAspectFlags ff_vk_aspect_flag(AVFrame *f, int p)
Get the aspect flag for a plane from an image.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
VkDescriptorSetLayoutBinding binding[FF_VK_MAX_DESCRIPTOR_BINDINGS]
int ff_vk_shader_load(FFVulkanShader *shd, VkPipelineStageFlags stage, VkSpecializationInfo *spec, uint32_t wg_size[3], uint32_t required_subgroup_size)
Initialize a shader object.
AVBufferRef * input_frames_ref
VkSemaphoreSubmitInfo sem_sig[FF_VK_EXEC_MAX_SEM_OPS]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
int ff_vk_shader_update_img(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, int set, int bind, int offs, VkImageView view, VkImageLayout layout, VkSampler sampler)
Sets an image descriptor for specified shader and binding.
int ff_vk_exec_submit(FFVulkanContext *s, FFVkExecContext *e)
VkCommandPool * cmd_buf_pools
VkDescriptorPool desc_pool
FFVkExecContext * ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool)
Retrieve an execution pool.
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if pixfmt is a usable RGB format.
static int ff_vk_unmap_buffer(FFVulkanContext *s, FFVkBuffer *buf, int flush)
AVVulkanDeviceQueueFamily * ff_vk_qf_find(FFVulkanContext *s, VkQueueFlagBits dev_family, VkVideoCodecOperationFlagBitsKHR vid_ops)
Chooses an appropriate QF.
VkPhysicalDeviceExternalMemoryHostPropertiesEXT hprops
VkPhysicalDeviceProperties2 props
static const int8_t filt[NUMTAPS *2]
FFVulkanExtensions extensions
VkPhysicalDeviceMemoryProperties mprops
AVFrame * sw_frame_deps[FF_VK_EXEC_MAX_SW_FRAME_DEPS]
This struct describes a set or pool of "hardware" frames (i.e.
void ff_vk_shader_update_img_array(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, AVFrame *f, VkImageView *views, int set, int binding, VkImageLayout layout, VkSampler sampler)
Update a descriptor in a buffer with an image array.
VkPhysicalDeviceDescriptorBufferPropertiesEXT desc_buf_props
AVBufferRef * buf_deps[FF_VK_EXEC_MAX_BUF_DEPS]
const VkComponentMapping ff_comp_identity_map
#define FF_VK_MAX_DESCRIPTOR_TYPES
int ff_vk_alloc_mem(FFVulkanContext *s, VkMemoryRequirements *req, VkMemoryPropertyFlagBits req_flags, void *alloc_extension, VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
Memory/buffer/image allocation helpers.
enum AVPixelFormat input_format
uint32_t coop_mat_props_nb
#define FF_VK_EXEC_MAX_SW_FRAME_DEPS
void ff_vk_exec_update_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar)
VkSemaphoreSubmitInfo sem_wait[FF_VK_EXEC_MAX_SEM_OPS]
FFVkExecObjDep obj_deps[FF_VK_EXEC_MAX_BUF_DEPS]
uint32_t queue_family_dst[FF_VK_EXEC_MAX_FRAME_DEPS]
VkQueueFamilyVideoPropertiesKHR * video_props
VkImageUsageFlags ff_vk_map_feats_to_usage(VkFormatFeatureFlagBits2 feats)
Map between usage and features.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
AVVulkanDeviceContext * hwctx
void ff_vk_exec_move_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
A reference to a data buffer.
VkPushConstantRange push_consts[FF_VK_MAX_PUSH_CONSTS]
VkCommandBuffer * cmd_bufs
uint8_t frame_update[FF_VK_EXEC_MAX_FRAME_DEPS]
VkPhysicalDevicePushDescriptorPropertiesKHR push_desc_props
VkPhysicalDeviceVulkan12Features feats_12
static int ff_vk_count_images(AVVkFrame *f)
void ff_vk_shader_add_descriptor_set(FFVulkanContext *s, FFVulkanShader *shd, const FFVulkanDescriptorSetBinding *desc, int nb, int singular)
Add descriptor to a shader.
AVHWFramesContext * frames
VkPhysicalDeviceOpticalFlowPropertiesNV optical_flow_props
void ff_vk_set_perm(enum AVPixelFormat pix_fmt, int lut[4], int inv)
Since storage images may not be swizzled, we have to do this in the shader itself.
FFVulkanShaderData reg_shd[FF_VK_MAX_SHADERS]
VkQueueFamilyQueryResultStatusPropertiesKHR * query_props
VkImageLayout * host_image_copy_layouts
uint8_t frame_locked[FF_VK_EXEC_MAX_FRAME_DEPS]
VkCooperativeMatrixPropertiesKHR * coop_mat_props
intptr_t atomic_uint_least64_t
#define FF_VK_MAX_PUSH_CONSTS
#define FF_VK_MAX_DESCRIPTOR_SETS