FFmpeg
Data Structures | Macros | Functions | Variables
vulkan.c File Reference
#include "formats.h"
#include "vulkan.h"
#include "glslang.h"

Go to the source code of this file.

Data Structures

struct  ImageViewCtx
 
struct  descriptor_props
 

Macros

#define FN_CREATING(ctx, type, shortname, array, num)
 
#define CASE(VAL)   case VAL: return #VAL
 

Functions

const char * ff_vk_ret2str (VkResult res)
 Converts Vulkan return values to strings. More...
 
static int vk_alloc_mem (AVFilterContext *avctx, VkMemoryRequirements *req, VkMemoryPropertyFlagBits req_flags, void *alloc_extension, VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
 
int ff_vk_create_buf (AVFilterContext *avctx, FFVkBuffer *buf, size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
 Create a VkBuffer with the specified parameters. More...
 
int ff_vk_map_buffers (AVFilterContext *avctx, FFVkBuffer *buf, uint8_t *mem[], int nb_buffers, int invalidate)
 Maps the buffer to userspace. More...
 
int ff_vk_unmap_buffers (AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers, int flush)
 Unmaps the buffer from userspace. More...
 
void ff_vk_free_buf (AVFilterContext *avctx, FFVkBuffer *buf)
 Frees a buffer. More...
 
int ff_vk_add_push_constant (AVFilterContext *avctx, VulkanPipeline *pl, int offset, int size, VkShaderStageFlagBits stage)
 Define a push constant for a given stage into a pipeline. More...
 
int ff_vk_create_exec_ctx (AVFilterContext *avctx, FFVkExecContext **ctx)
 Init an execution context for command recording and queue submission. More...
 
void ff_vk_discard_exec_deps (AVFilterContext *avctx, FFVkExecContext *e)
 Discards all queue dependencies. More...
 
int ff_vk_start_exec_recording (AVFilterContext *avctx, FFVkExecContext *e)
 Begin recording to the command buffer. More...
 
VkCommandBuffer ff_vk_get_exec_buf (AVFilterContext *avctx, FFVkExecContext *e)
 Gets the command buffer to use for this submission from the exe context. More...
 
int ff_vk_add_exec_dep (AVFilterContext *avctx, FFVkExecContext *e, AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
 Adds a frame as a queue dependency. More...
 
int ff_vk_submit_exec_queue (AVFilterContext *avctx, FFVkExecContext *e)
 Submits a command buffer to the queue for execution. More...
 
int ff_vk_add_dep_exec_ctx (AVFilterContext *avctx, FFVkExecContext *e, AVBufferRef **deps, int nb_deps)
 Adds a generic AVBufferRef as a queue depenency. More...
 
int ff_vk_filter_query_formats (AVFilterContext *avctx)
 General lavfi IO functions. More...
 
static int vulkan_filter_set_device (AVFilterContext *avctx, AVBufferRef *device)
 
static int vulkan_filter_set_frames (AVFilterContext *avctx, AVBufferRef *frames)
 
int ff_vk_filter_config_input (AVFilterLink *inlink)
 
int ff_vk_filter_config_output_inplace (AVFilterLink *outlink)
 
int ff_vk_filter_config_output (AVFilterLink *outlink)
 
int ff_vk_filter_init (AVFilterContext *avctx)
 
VkSampler * ff_vk_init_sampler (AVFilterContext *avctx, int unnorm_coords, VkFilter filt)
 Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit() More...
 
int ff_vk_mt_is_np_rgb (enum AVPixelFormat pix_fmt)
 Returns 1 if the image is any sort of supported RGB. More...
 
const char * ff_vk_shader_rep_fmt (enum AVPixelFormat pixfmt)
 Gets the glsl format string for a pixel format. More...
 
static void destroy_imageview (void *opaque, uint8_t *data)
 
int ff_vk_create_imageview (AVFilterContext *avctx, FFVkExecContext *e, VkImageView *v, VkImage img, VkFormat fmt, const VkComponentMapping map)
 Create an imageview. More...
 
SPIRVShaderff_vk_init_shader (AVFilterContext *avctx, VulkanPipeline *pl, const char *name, VkShaderStageFlags stage)
 Inits a shader for a specific pipeline. More...
 
void ff_vk_set_compute_shader_sizes (AVFilterContext *avctx, SPIRVShader *shd, int local_size[3])
 Writes the workgroup size for a shader. More...
 
static void print_shader (AVFilterContext *avctx, SPIRVShader *shd, int prio)
 
int ff_vk_compile_shader (AVFilterContext *avctx, SPIRVShader *shd, const char *entrypoint)
 Compiles the shader, entrypoint must be set to "main". More...
 
int ff_vk_add_descriptor_set (AVFilterContext *avctx, VulkanPipeline *pl, SPIRVShader *shd, VulkanDescriptorSetBinding *desc, int num, int only_print_to_shader)
 Adds a descriptor set to the shader and registers them in the pipeline. More...
 
void ff_vk_update_descriptor_set (AVFilterContext *avctx, VulkanPipeline *pl, int set_id)
 Updates a descriptor set via the updaters defined. More...
 
void ff_vk_update_push_exec (AVFilterContext *avctx, FFVkExecContext *e, VkShaderStageFlagBits stage, int offset, size_t size, void *src)
 Updates push constants. More...
 
int ff_vk_init_pipeline_layout (AVFilterContext *avctx, VulkanPipeline *pl)
 Initializes the pipeline layout after all shaders and descriptor sets have been finished. More...
 
VulkanPipelineff_vk_create_pipeline (AVFilterContext *avctx)
 Inits a pipeline. More...
 
int ff_vk_init_compute_pipeline (AVFilterContext *avctx, VulkanPipeline *pl)
 Initializes a compute pipeline. More...
 
void ff_vk_bind_pipeline_exec (AVFilterContext *avctx, FFVkExecContext *e, VulkanPipeline *pl)
 Add a command to bind the completed pipeline and its descriptor sets. More...
 
static void free_exec_ctx (VulkanFilterContext *s, FFVkExecContext *e)
 
static void free_pipeline (VulkanFilterContext *s, VulkanPipeline *pl)
 
void ff_vk_filter_uninit (AVFilterContext *avctx)
 

Variables

const VkComponentMapping ff_comp_identity_map
 
static const struct descriptor_props descriptor_props []
 

Macro Definition Documentation

◆ FN_CREATING

#define FN_CREATING (   ctx,
  type,
  shortname,
  array,
  num 
)
Value:
static av_always_inline type *create_ ##shortname(ctx *dctx) \
{ \
type **array, *sctx = av_mallocz(sizeof(*sctx)); \
if (!sctx) \
return NULL; \
\
array = av_realloc_array(dctx->array, sizeof(*dctx->array), dctx->num + 1);\
if (!array) { \
av_free(sctx); \
return NULL; \
} \
\
dctx->array = array; \
dctx->array[dctx->num++] = sctx; \
\
return sctx; \
}

Definition at line 25 of file vulkan.c.

◆ CASE

#define CASE (   VAL)    case VAL: return #VAL

Function Documentation

◆ ff_vk_ret2str()

const char* ff_vk_ret2str ( VkResult  res)

◆ vk_alloc_mem()

static int vk_alloc_mem ( AVFilterContext avctx,
VkMemoryRequirements *  req,
VkMemoryPropertyFlagBits  req_flags,
void *  alloc_extension,
VkMemoryPropertyFlagBits *  mem_flags,
VkDeviceMemory *  mem 
)
static

Definition at line 89 of file vulkan.c.

Referenced by ff_vk_create_buf().

◆ ff_vk_create_buf()

int ff_vk_create_buf ( AVFilterContext avctx,
FFVkBuffer buf,
size_t  size,
VkBufferUsageFlags  usage,
VkMemoryPropertyFlagBits  flags 
)

Create a VkBuffer with the specified parameters.

Definition at line 150 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_map_buffers()

int ff_vk_map_buffers ( AVFilterContext avctx,
FFVkBuffer buf,
uint8_t mem[],
int  nb_buffers,
int  invalidate 
)

Maps the buffer to userspace.

Set invalidate to 1 if reading the contents is necessary.

Definition at line 215 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_unmap_buffers()

int ff_vk_unmap_buffers ( AVFilterContext avctx,
FFVkBuffer buf,
int  nb_buffers,
int  flush 
)

Unmaps the buffer from userspace.

Set flush to 1 to write and sync.

Definition at line 264 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_free_buf()

void ff_vk_free_buf ( AVFilterContext avctx,
FFVkBuffer buf 
)

Frees a buffer.

Definition at line 306 of file vulkan.c.

Referenced by overlay_vulkan_uninit(), and scale_vulkan_uninit().

◆ ff_vk_add_push_constant()

int ff_vk_add_push_constant ( AVFilterContext avctx,
VulkanPipeline pl,
int  offset,
int  size,
VkShaderStageFlagBits  stage 
)

Define a push constant for a given stage into a pipeline.

Must be called before the pipeline layout has been initialized.

Definition at line 318 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_create_exec_ctx()

int ff_vk_create_exec_ctx ( AVFilterContext avctx,
FFVkExecContext **  ctx 
)

Init an execution context for command recording and queue submission.

WIll be auto-freed on uninit.

Definition at line 339 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_discard_exec_deps()

void ff_vk_discard_exec_deps ( AVFilterContext avctx,
FFVkExecContext e 
)

Discards all queue dependencies.

Definition at line 400 of file vulkan.c.

Referenced by ff_vk_add_dep_exec_ctx(), ff_vk_add_exec_dep(), ff_vk_start_exec_recording(), and process_frames().

◆ ff_vk_start_exec_recording()

int ff_vk_start_exec_recording ( AVFilterContext avctx,
FFVkExecContext e 
)

Begin recording to the command buffer.

Previous execution must have been completed, which ff_vk_submit_exec_queue() will ensure.

Definition at line 417 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_get_exec_buf()

VkCommandBuffer ff_vk_get_exec_buf ( AVFilterContext avctx,
FFVkExecContext e 
)

Gets the command buffer to use for this submission from the exe context.

Definition at line 458 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_add_exec_dep()

int ff_vk_add_exec_dep ( AVFilterContext avctx,
FFVkExecContext e,
AVFrame frame,
VkPipelineStageFlagBits  in_wait_dst_flag 
)

Adds a frame as a queue dependency.

This also manages semaphore signalling. Must be called before submission.

Definition at line 464 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_submit_exec_queue()

int ff_vk_submit_exec_queue ( AVFilterContext avctx,
FFVkExecContext e 
)

Submits a command buffer to the queue for execution.

Will block until execution has finished in order to simplify resource management.

Definition at line 522 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_add_dep_exec_ctx()

int ff_vk_add_dep_exec_ctx ( AVFilterContext avctx,
FFVkExecContext e,
AVBufferRef **  deps,
int  nb_deps 
)

Adds a generic AVBufferRef as a queue depenency.

Definition at line 561 of file vulkan.c.

Referenced by ff_vk_create_imageview().

◆ ff_vk_filter_query_formats()

int ff_vk_filter_query_formats ( AVFilterContext avctx)

General lavfi IO functions.

Definition at line 592 of file vulkan.c.

◆ vulkan_filter_set_device()

static int vulkan_filter_set_device ( AVFilterContext avctx,
AVBufferRef device 
)
static

◆ vulkan_filter_set_frames()

static int vulkan_filter_set_frames ( AVFilterContext avctx,
AVBufferRef frames 
)
static

Definition at line 621 of file vulkan.c.

Referenced by ff_vk_filter_config_input().

◆ ff_vk_filter_config_input()

int ff_vk_filter_config_input ( AVFilterLink inlink)

Definition at line 635 of file vulkan.c.

◆ ff_vk_filter_config_output_inplace()

int ff_vk_filter_config_output_inplace ( AVFilterLink outlink)

Definition at line 675 of file vulkan.c.

◆ ff_vk_filter_config_output()

int ff_vk_filter_config_output ( AVFilterLink outlink)

Definition at line 705 of file vulkan.c.

Referenced by overlay_vulkan_config_output(), and scale_vulkan_config_output().

◆ ff_vk_filter_init()

int ff_vk_filter_init ( AVFilterContext avctx)

Definition at line 756 of file vulkan.c.

Referenced by overlay_vulkan_init().

◆ ff_vk_init_sampler()

VkSampler* ff_vk_init_sampler ( AVFilterContext avctx,
int  unnorm_coords,
VkFilter  filt 
)

Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()

Definition at line 769 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_mt_is_np_rgb()

int ff_vk_mt_is_np_rgb ( enum AVPixelFormat  pix_fmt)

Returns 1 if the image is any sort of supported RGB.

Definition at line 805 of file vulkan.c.

Referenced by scale_vulkan_config_output().

◆ ff_vk_shader_rep_fmt()

const char* ff_vk_shader_rep_fmt ( enum AVPixelFormat  pixfmt)

Gets the glsl format string for a pixel format.

Definition at line 817 of file vulkan.c.

Referenced by init_filter().

◆ destroy_imageview()

static void destroy_imageview ( void *  opaque,
uint8_t data 
)
static

Definition at line 828 of file vulkan.c.

Referenced by ff_vk_create_imageview().

◆ ff_vk_create_imageview()

int ff_vk_create_imageview ( AVFilterContext avctx,
FFVkExecContext e,
VkImageView *  v,
VkImage  img,
VkFormat  fmt,
const VkComponentMapping  map 
)

Create an imageview.

Guaranteed to remain alive until the queue submission has finished executing, and will be destroyed after that.

Definition at line 836 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_init_shader()

SPIRVShader* ff_vk_init_shader ( AVFilterContext avctx,
VulkanPipeline pl,
const char *  name,
VkShaderStageFlags  stage 
)

Inits a shader for a specific pipeline.

Will be auto-freed on uninit.

Definition at line 888 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_set_compute_shader_sizes()

void ff_vk_set_compute_shader_sizes ( AVFilterContext avctx,
SPIRVShader shd,
int  local_size[3] 
)

Writes the workgroup size for a shader.

Definition at line 909 of file vulkan.c.

Referenced by init_filter().

◆ print_shader()

static void print_shader ( AVFilterContext avctx,
SPIRVShader shd,
int  prio 
)
static

Definition at line 921 of file vulkan.c.

Referenced by ff_vk_compile_shader().

◆ ff_vk_compile_shader()

int ff_vk_compile_shader ( AVFilterContext avctx,
SPIRVShader shd,
const char *  entrypoint 
)

Compiles the shader, entrypoint must be set to "main".

Definition at line 942 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_add_descriptor_set()

int ff_vk_add_descriptor_set ( AVFilterContext avctx,
VulkanPipeline pl,
SPIRVShader shd,
VulkanDescriptorSetBinding desc,
int  num,
int  only_print_to_shader 
)

Adds a descriptor set to the shader and registers them in the pipeline.

Definition at line 1020 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_update_descriptor_set()

void ff_vk_update_descriptor_set ( AVFilterContext avctx,
VulkanPipeline pl,
int  set_id 
)

Updates a descriptor set via the updaters defined.

Can be called immediately after pipeline creation, but must be called at least once before queue submission.

Definition at line 1160 of file vulkan.c.

Referenced by init_filter(), and process_frames().

◆ ff_vk_update_push_exec()

void ff_vk_update_push_exec ( AVFilterContext avctx,
FFVkExecContext e,
VkShaderStageFlagBits  stage,
int  offset,
size_t  size,
void *  src 
)

Updates push constants.

Must be called after binding a pipeline if any push constants were defined.

Definition at line 1171 of file vulkan.c.

Referenced by process_frames().

◆ ff_vk_init_pipeline_layout()

int ff_vk_init_pipeline_layout ( AVFilterContext avctx,
VulkanPipeline pl 
)

Initializes the pipeline layout after all shaders and descriptor sets have been finished.

Definition at line 1180 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_create_pipeline()

VulkanPipeline* ff_vk_create_pipeline ( AVFilterContext avctx)

Inits a pipeline.

Everything in it will be auto-freed when calling ff_vk_filter_uninit().

Definition at line 1276 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_init_compute_pipeline()

int ff_vk_init_compute_pipeline ( AVFilterContext avctx,
VulkanPipeline pl 
)

Initializes a compute pipeline.

Will pick the first shader with the COMPUTE flag set.

Definition at line 1281 of file vulkan.c.

Referenced by init_filter().

◆ ff_vk_bind_pipeline_exec()

void ff_vk_bind_pipeline_exec ( AVFilterContext avctx,
FFVkExecContext e,
VulkanPipeline pl 
)

Add a command to bind the completed pipeline and its descriptor sets.

Must be called after ff_vk_start_exec_recording() and before submission.

Definition at line 1316 of file vulkan.c.

Referenced by process_frames().

◆ free_exec_ctx()

static void free_exec_ctx ( VulkanFilterContext s,
FFVkExecContext e 
)
static

Definition at line 1330 of file vulkan.c.

Referenced by ff_vk_filter_uninit().

◆ free_pipeline()

static void free_pipeline ( VulkanFilterContext s,
VulkanPipeline pl 
)
static

Definition at line 1369 of file vulkan.c.

Referenced by ff_vk_filter_uninit().

◆ ff_vk_filter_uninit()

void ff_vk_filter_uninit ( AVFilterContext avctx)

Variable Documentation

◆ ff_comp_identity_map

const VkComponentMapping ff_comp_identity_map
Initial value:
= {
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
.a = VK_COMPONENT_SWIZZLE_IDENTITY,
}

Definition at line 44 of file vulkan.c.

Referenced by process_frames().

◆ descriptor_props

const struct descriptor_props descriptor_props[]
static
Initial value:
= {
[VK_DESCRIPTOR_TYPE_SAMPLER] = { sizeof(VkDescriptorImageInfo), "sampler", 1, 0, 0, 0, },
[VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE] = { sizeof(VkDescriptorImageInfo), "texture", 1, 0, 1, 0, },
[VK_DESCRIPTOR_TYPE_STORAGE_IMAGE] = { sizeof(VkDescriptorImageInfo), "image", 1, 1, 1, 0, },
[VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT] = { sizeof(VkDescriptorImageInfo), "subpassInput", 1, 0, 0, 0, },
[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER] = { sizeof(VkDescriptorImageInfo), "sampler", 1, 0, 1, 0, },
[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER] = { sizeof(VkDescriptorBufferInfo), NULL, 1, 0, 0, 1, },
[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER] = { sizeof(VkDescriptorBufferInfo), "buffer", 0, 1, 0, 1, },
[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC] = { sizeof(VkDescriptorBufferInfo), NULL, 1, 0, 0, 1, },
[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC] = { sizeof(VkDescriptorBufferInfo), "buffer", 0, 1, 0, 1, },
[VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER] = { sizeof(VkBufferView), "samplerBuffer", 1, 0, 0, 0, },
[VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER] = { sizeof(VkBufferView), "imageBuffer", 1, 0, 0, 0, },
}
type
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
Definition: writing_filters.txt:86
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:198
ctx
AVFormatContext * ctx
Definition: movenc.c:48
NULL
#define NULL
Definition: coverity.c:32
av_always_inline
#define av_always_inline
Definition: attributes.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106