Go to the documentation of this file.
26 #define GBLUR_MAX_KERNEL_SIZE 127
53 C(0,
void gblur(
const ivec2
pos,
const int index) )
55 C(1, vec4 sum = texture(input_images[
index],
pos) * kernel[0]; )
57 C(1,
for(
int i = 1;
i < kernel.length();
i++) { )
62 C(1, imageStore(output_images[
index],
pos, sum); )
66 static inline float gaussian(
float sigma,
float x)
68 return 1.0 / (sqrt(2.0 *
M_PI) * sigma) *
69 exp(-(x * x) / (2.0 * sigma * sigma));
74 return (
b -
a) * (1.0 / 6.0) * ((
gaussian(sigma,
a) +
84 for (x = 0; x < kernel_size; x++) {
89 sum += kernel[x] * 2.0;
93 for (x = 0; x < kernel_size; x++) {
113 s->sigmaV =
s->sigma;
126 FFVkBuffer *params_buf, VkDescriptorBufferInfo *params_desc,
127 int ksize,
float sigma)
130 uint8_t *kernel_mapped;
136 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
137 .mem_quali =
"readonly",
138 .mem_layout =
"std430",
139 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
144 char *kernel_def =
av_asprintf(
"float kernel[%i];", ksize);
148 buf_desc.
updater = params_desc;
157 GLSLC(1,
const ivec2
pos = ivec2(gl_GlobalInvocationID.xy); );
162 if (
s->planes & (1 <<
i)) {
165 GLSLF(2, vec4 res = texture(input_images[%
i],
pos); ,
i);
166 GLSLF(2, imageStore(output_images[%
i],
pos, res); ,
i);
178 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
185 params_desc->buffer = params_buf->
buf;
186 params_desc->range = VK_WHOLE_SIZE;
204 .
name =
"input_images",
205 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
208 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
211 .name =
"output_images",
212 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
214 .mem_quali =
"writeonly",
217 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
222 if (!image_descs[0].sampler)
231 image_descs[0].
updater =
s->input_images;
232 image_descs[1].
updater =
s->tmp_images;
256 image_descs[0].
updater =
s->tmp_images;
257 image_descs[1].
updater =
s->output_images;
276 s->sizeV,
s->sigmaV));
303 VkCommandBuffer cmd_buf;
335 s->input_images[
i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
336 s->tmp_images[
i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
337 s->output_images[
i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
344 VkImageMemoryBarrier barriers[] = {
346 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
348 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
350 .newLayout =
s->input_images[
i].imageLayout,
351 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
352 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
354 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
355 .subresourceRange.levelCount = 1,
356 .subresourceRange.layerCount = 1,
359 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
361 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT,
362 .oldLayout =
tmp->layout[
i],
363 .newLayout =
s->tmp_images[
i].imageLayout,
364 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
365 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
366 .image =
tmp->img[
i],
367 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
368 .subresourceRange.levelCount = 1,
369 .subresourceRange.layerCount = 1,
372 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
374 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
375 .oldLayout =
out->layout[
i],
376 .newLayout =
s->output_images[
i].imageLayout,
377 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
378 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
379 .image =
out->img[
i],
380 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
381 .subresourceRange.levelCount = 1,
382 .subresourceRange.layerCount = 1,
386 vk->CmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
387 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
390 in->
layout[
i] = barriers[0].newLayout;
391 in->
access[
i] = barriers[0].dstAccessMask;
393 tmp->layout[
i] = barriers[1].newLayout;
394 tmp->access[
i] = barriers[1].dstAccessMask;
396 out->layout[
i] = barriers[2].newLayout;
397 out->access[
i] = barriers[2].dstAccessMask;
403 s->vkctx.output_height, 1);
407 vk->CmdDispatch(cmd_buf,
s->vkctx.output_width,
440 if (!
s->initialized) {
465 #define OFFSET(x) offsetof(GBlurVulkanContext, x)
466 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
496 .
name =
"gblur_vulkan",
504 .priv_class = &gblur_vulkan_class,
VkDescriptorImageInfo input_images[3]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_LOG_WARNING
Something somehow does not look correct.
static const struct @346 planes[]
AVFILTER_DEFINE_CLASS(gblur_vulkan)
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 sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const VkComponentMapping ff_comp_identity_map
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Create a VkBuffer with the specified parameters.
static float gaussian_simpson_integration(float sigma, float a, float b)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
void ff_vk_bind_pipeline_exec(FFVulkanContext *s, FFVkExecContext *e, FFVulkanPipeline *pl)
Add a command to bind the completed pipeline and its descriptor sets.
char * av_asprintf(const char *fmt,...)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
VkShaderStageFlags stages
int ff_vk_filter_init(AVFilterContext *avctx)
General lavfi IO functions.
int ff_vk_compile_shader(FFVulkanContext *s, FFVkSPIRVShader *shd, const char *entrypoint)
Compiles the shader, entrypoint must be set to "main".
FFVulkanPipeline * pl_hor
FFVulkanPipeline * pl_ver
void ff_vk_uninit(FFVulkanContext *s)
Frees the main Vulkan context.
const char * name
Filter name.
A link between two filters.
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
static const AVFilterPad gblur_vulkan_inputs[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void init_gaussian_kernel(float *kernel, float sigma, float kernel_size)
int ff_vk_add_exec_dep(FFVulkanContext *s, FFVkExecContext *e, AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
Adds a frame as a queue dependency.
VkCommandBuffer ff_vk_get_exec_buf(FFVkExecContext *e)
Gets the command buffer to use for this submission from the exe context.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
VkDescriptorImageInfo output_images[3]
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
void * priv
private data for use by the filter
FFVkBuffer params_buf_ver
static const AVFilterPad gblur_vulkan_outputs[]
A filter pad used for either input or output.
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
void ff_vk_qf_init(FFVulkanContext *s, FFVkQueueFamilyCtx *qf, VkQueueFlagBits dev_family, int nb_queues)
Initialize a queue family with a specific number of queues.
#define FF_ARRAY_ELEMS(a)
int ff_vk_create_imageview(FFVulkanContext *s, FFVkExecContext *e, VkImageView *v, VkImage img, VkFormat fmt, const VkComponentMapping map)
Create an imageview.
VkDescriptorBufferInfo params_desc_hor
int(* init)(AVBSFContext *ctx)
FFVkSPIRVShader * ff_vk_init_shader(FFVulkanPipeline *pl, const char *name, VkShaderStageFlags stage)
Inits a shader for a specific pipeline.
static const AVOption gblur_vulkan_options[]
VkDescriptorImageInfo tmp_images[3]
#define FILTER_INPUTS(array)
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 sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
void ff_vk_update_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, int set_id)
Updates a descriptor set via the updaters defined.
int ff_vk_start_exec_recording(FFVulkanContext *s, FFVkExecContext *e)
Begin recording to the command buffer.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
int ff_vk_create_exec_ctx(FFVulkanContext *s, FFVkExecContext **ctx, FFVkQueueFamilyCtx *qf)
Init an execution context for command recording and queue submission.
int ff_vk_filter_config_output(AVFilterLink *outlink)
void ff_vk_qf_rotate(FFVkQueueFamilyCtx *qf)
Rotate through the queues in a queue family.
#define GBLUR_MAX_KERNEL_SIZE
void ff_vk_discard_exec_deps(FFVkExecContext *e)
Discards all queue dependencies.
int main(int argc, char **argv)
int ff_vk_unmap_buffers(FFVulkanContext *s, FFVkBuffer *buf, int nb_buffers, int flush)
Unmaps the buffer from userspace.
VkAccessFlagBits access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
VkDescriptorBufferInfo params_desc_ver
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_vk_init_pipeline_layout(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes the pipeline layout after all shaders and descriptor sets have been finished.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static av_cold void gblur_vulkan_uninit(AVFilterContext *avctx)
int ff_vk_submit_exec_queue(FFVulkanContext *s, FFVkExecContext *e)
Submits a command buffer to the queue for execution.
#define AVERROR_EXTERNAL
Generic error in an external library.
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the format of each image up to the number of planes for a given sw_format.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
static void init_kernel_size(GBlurVulkanContext *s, int *out_size)
int ff_vk_init_compute_pipeline(FFVulkanContext *s, FFVulkanPipeline *pl)
Initializes a compute pipeline.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
const char * ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)
Gets the glsl format string for a pixel format.
const char * name
Pad name.
FFVulkanPipeline * ff_vk_create_pipeline(FFVulkanContext *s, FFVkQueueFamilyCtx *qf)
Inits a pipeline.
void ff_vk_free_buf(FFVulkanContext *s, FFVkBuffer *buf)
Frees a buffer.
static av_cold void init_gaussian_params(GBlurVulkanContext *s)
static int gblur_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
static int init_gblur_pipeline(GBlurVulkanContext *s, FFVulkanPipeline *pl, FFVkSPIRVShader *shd, FFVkBuffer *params_buf, VkDescriptorBufferInfo *params_desc, int ksize, float sigma)
int ff_vk_add_descriptor_set(FFVulkanContext *s, FFVulkanPipeline *pl, FFVkSPIRVShader *shd, FFVulkanDescriptorSetBinding *desc, int num, int only_print_to_shader)
Adds a descriptor set to the shader and registers them in the pipeline.
static const char gblur_func[]
int h
agreed upon image height
const AVFilter ff_vf_gblur_vulkan
FFVkSampler * ff_vk_init_sampler(FFVulkanContext *s, int unnorm_coords, VkFilter filt)
Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
int ff_vk_filter_config_input(AVFilterLink *inlink)
VkImageLayout layout[AV_NUM_DATA_POINTERS]
int ff_vk_map_buffers(FFVulkanContext *s, FFVkBuffer *buf, uint8_t *mem[], int nb_buffers, int invalidate)
Maps the buffer to userspace.
static int process_frames(AVFilterContext *avctx, AVFrame *outframe, AVFrame *inframe)
#define FILTER_OUTPUTS(array)
void ff_vk_set_compute_shader_sizes(FFVkSPIRVShader *shd, int local_size[3])
Writes the workgroup size for a shader.
static av_cold int uninit(AVCodecContext *avctx)
FFVkBuffer params_buf_hor
static float gaussian(float sigma, float x)