Unverified Commit 727cac88 authored by Lynne's avatar Lynne

lavfi/vulkan: use all enabled queues in the queue family

This should significantly improve the performance with certain
filterchains.
parent fac17fd4
......@@ -97,6 +97,10 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
if (!sampler)
return AVERROR_EXTERNAL;
s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;
s->vkctx.queue_count = GET_QUEUE_COUNT(s->vkctx.hwctx, 0, 1, 0);
s->vkctx.cur_queue_idx = rand() % s->vkctx.queue_count;
{ /* Create shader for the horizontal pass */
desc_i[0].updater = s->input_images;
desc_i[1].updater = s->tmp_images;
......@@ -184,8 +188,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
}
/* Execution context */
RET(ff_vk_create_exec_ctx(ctx, &s->exec,
s->vkctx.hwctx->queue_family_comp_index));
RET(ff_vk_create_exec_ctx(ctx, &s->exec));
s->initialized = 1;
......@@ -198,22 +201,30 @@ fail:
static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f, AVFrame *in_f)
{
int err;
VkCommandBuffer cmd_buf;
AvgBlurVulkanContext *s = avctx->priv;
AVVkFrame *in = (AVVkFrame *)in_f->data[0];
AVVkFrame *tmp = (AVVkFrame *)tmp_f->data[0];
AVVkFrame *out = (AVVkFrame *)out_f->data[0];
int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
/* Update descriptors and init the exec context */
ff_vk_start_exec_recording(avctx, s->exec);
cmd_buf = ff_vk_get_exec_buf(avctx, s->exec);
for (int i = 0; i < planes; i++) {
RET(ff_vk_create_imageview(avctx, &s->input_images[i].imageView, in->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->input_images[i].imageView,
in->img[i],
av_vkfmt_from_pixfmt(s->vkctx.input_format)[i],
ff_comp_identity_map));
RET(ff_vk_create_imageview(avctx, &s->tmp_images[i].imageView, tmp->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->tmp_images[i].imageView,
tmp->img[i],
av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
ff_comp_identity_map));
RET(ff_vk_create_imageview(avctx, &s->output_images[i].imageView, out->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView,
out->img[i],
av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
ff_comp_identity_map));
......@@ -225,8 +236,6 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f
ff_vk_update_descriptor_set(avctx, s->pl_hor, 0);
ff_vk_update_descriptor_set(avctx, s->pl_ver, 0);
ff_vk_start_exec_recording(avctx, s->exec);
for (int i = 0; i < planes; i++) {
VkImageMemoryBarrier bar[] = {
{
......@@ -270,7 +279,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f
},
};
vkCmdPipelineBarrier(s->exec->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
0, NULL, 0, NULL, FF_ARRAY_ELEMS(bar), bar);
......@@ -286,12 +295,12 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f
ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl_hor);
vkCmdDispatch(s->exec->buf, FFALIGN(s->vkctx.output_width, CGS)/CGS,
vkCmdDispatch(cmd_buf, FFALIGN(s->vkctx.output_width, CGS)/CGS,
s->vkctx.output_height, 1);
ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl_ver);
vkCmdDispatch(s->exec->buf, s->vkctx.output_width,
vkCmdDispatch(cmd_buf, s->vkctx.output_width,
FFALIGN(s->vkctx.output_height, CGS)/CGS, 1);
ff_vk_add_exec_dep(avctx, s->exec, in_f, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
......@@ -301,14 +310,10 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f
if (err)
return err;
fail:
for (int i = 0; i < planes; i++) {
ff_vk_destroy_imageview(avctx, &s->input_images[i].imageView);
ff_vk_destroy_imageview(avctx, &s->tmp_images[i].imageView);
ff_vk_destroy_imageview(avctx, &s->output_images[i].imageView);
}
return err;
fail:
ff_vk_discard_exec_deps(avctx, s->exec);
return err;
}
......
......@@ -73,6 +73,10 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
if (!sampler)
return AVERROR_EXTERNAL;
s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;
s->vkctx.queue_count = GET_QUEUE_COUNT(s->vkctx.hwctx, 0, 1, 0);
s->vkctx.cur_queue_idx = rand() % s->vkctx.queue_count;
s->pl = ff_vk_create_pipeline(ctx);
if (!s->pl)
return AVERROR(ENOMEM);
......@@ -154,8 +158,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
RET(ff_vk_init_compute_pipeline(ctx, s->pl));
/* Execution context */
RET(ff_vk_create_exec_ctx(ctx, &s->exec,
s->vkctx.hwctx->queue_family_comp_index));
RET(ff_vk_create_exec_ctx(ctx, &s->exec));
s->initialized = 1;
......@@ -168,17 +171,24 @@ fail:
static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
{
int err = 0;
VkCommandBuffer cmd_buf;
ChromaticAberrationVulkanContext *s = avctx->priv;
AVVkFrame *in = (AVVkFrame *)in_f->data[0];
AVVkFrame *out = (AVVkFrame *)out_f->data[0];
int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
/* Update descriptors and init the exec context */
ff_vk_start_exec_recording(avctx, s->exec);
cmd_buf = ff_vk_get_exec_buf(avctx, s->exec);
for (int i = 0; i < planes; i++) {
RET(ff_vk_create_imageview(avctx, &s->input_images[i].imageView, in->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->input_images[i].imageView,
in->img[i],
av_vkfmt_from_pixfmt(s->vkctx.input_format)[i],
ff_comp_identity_map));
RET(ff_vk_create_imageview(avctx, &s->output_images[i].imageView, out->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView,
out->img[i],
av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
ff_comp_identity_map));
......@@ -188,8 +198,6 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
ff_vk_update_descriptor_set(avctx, s->pl, 0);
ff_vk_start_exec_recording(avctx, s->exec);
for (int i = 0; i < planes; i++) {
VkImageMemoryBarrier bar[2] = {
{
......@@ -220,7 +228,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
},
};
vkCmdPipelineBarrier(s->exec->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
0, NULL, 0, NULL, FF_ARRAY_ELEMS(bar), bar);
......@@ -236,7 +244,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
ff_vk_update_push_exec(avctx, s->exec, VK_SHADER_STAGE_COMPUTE_BIT,
0, sizeof(s->opts), &s->opts);
vkCmdDispatch(s->exec->buf,
vkCmdDispatch(cmd_buf,
FFALIGN(s->vkctx.output_width, CGROUPS[0])/CGROUPS[0],
FFALIGN(s->vkctx.output_height, CGROUPS[1])/CGROUPS[1], 1);
......@@ -247,12 +255,10 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
if (err)
return err;
for (int i = 0; i < planes; i++) {
ff_vk_destroy_imageview(avctx, &s->input_images[i].imageView);
ff_vk_destroy_imageview(avctx, &s->output_images[i].imageView);
}
return err;
fail:
ff_vk_discard_exec_deps(avctx, s->exec);
return err;
}
......
......@@ -87,6 +87,10 @@ static av_cold int init_filter(AVFilterContext *ctx)
if (!s->pl)
return AVERROR(ENOMEM);
s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;
s->vkctx.queue_count = GET_QUEUE_COUNT(s->vkctx.hwctx, 0, 1, 0);
s->vkctx.cur_queue_idx = rand() % s->vkctx.queue_count;
{ /* Create the shader */
const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
const int ialpha = av_pix_fmt_desc_get(s->vkctx.input_format)->flags & AV_PIX_FMT_FLAG_ALPHA;
......@@ -211,8 +215,7 @@ static av_cold int init_filter(AVFilterContext *ctx)
}
/* Execution context */
RET(ff_vk_create_exec_ctx(ctx, &s->exec,
s->vkctx.hwctx->queue_family_comp_index));
RET(ff_vk_create_exec_ctx(ctx, &s->exec));
s->initialized = 1;
......@@ -226,6 +229,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
AVFrame *main_f, AVFrame *overlay_f)
{
int err;
VkCommandBuffer cmd_buf;
OverlayVulkanContext *s = avctx->priv;
int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
......@@ -236,16 +240,23 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
AVHWFramesContext *main_fc = (AVHWFramesContext*)main_f->hw_frames_ctx->data;
AVHWFramesContext *overlay_fc = (AVHWFramesContext*)overlay_f->hw_frames_ctx->data;
/* Update descriptors and init the exec context */
ff_vk_start_exec_recording(avctx, s->exec);
cmd_buf = ff_vk_get_exec_buf(avctx, s->exec);
for (int i = 0; i < planes; i++) {
RET(ff_vk_create_imageview(avctx, &s->main_images[i].imageView, main->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->main_images[i].imageView,
main->img[i],
av_vkfmt_from_pixfmt(main_fc->sw_format)[i],
ff_comp_identity_map));
RET(ff_vk_create_imageview(avctx, &s->overlay_images[i].imageView, overlay->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->overlay_images[i].imageView,
overlay->img[i],
av_vkfmt_from_pixfmt(overlay_fc->sw_format)[i],
ff_comp_identity_map));
RET(ff_vk_create_imageview(avctx, &s->output_images[i].imageView, out->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView,
out->img[i],
av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
ff_comp_identity_map));
......@@ -256,8 +267,6 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
ff_vk_update_descriptor_set(avctx, s->pl, 0);
ff_vk_start_exec_recording(avctx, s->exec);
for (int i = 0; i < planes; i++) {
VkImageMemoryBarrier bar[3] = {
{
......@@ -301,7 +310,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
},
};
vkCmdPipelineBarrier(s->exec->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
0, NULL, 0, NULL, FF_ARRAY_ELEMS(bar), bar);
......@@ -317,7 +326,7 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl);
vkCmdDispatch(s->exec->buf,
vkCmdDispatch(cmd_buf,
FFALIGN(s->vkctx.output_width, CGROUPS[0])/CGROUPS[0],
FFALIGN(s->vkctx.output_height, CGROUPS[1])/CGROUPS[1], 1);
......@@ -329,14 +338,10 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f,
if (err)
return err;
fail:
for (int i = 0; i < planes; i++) {
ff_vk_destroy_imageview(avctx, &s->main_images[i].imageView);
ff_vk_destroy_imageview(avctx, &s->overlay_images[i].imageView);
ff_vk_destroy_imageview(avctx, &s->output_images[i].imageView);
}
return err;
fail:
ff_vk_discard_exec_deps(avctx, s->exec);
return err;
}
......
......@@ -115,6 +115,10 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
int crop_w = in->width - (in->crop_left + in->crop_right);
int crop_h = in->height - (in->crop_top + in->crop_bottom);
s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;
s->vkctx.queue_count = GET_QUEUE_COUNT(s->vkctx.hwctx, 0, 1, 0);
s->vkctx.cur_queue_idx = rand() % s->vkctx.queue_count;
switch (s->scaler) {
case F_NEAREST:
sampler_mode = VK_FILTER_NEAREST;
......@@ -276,8 +280,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
}
/* Execution context */
RET(ff_vk_create_exec_ctx(ctx, &s->exec,
s->vkctx.hwctx->queue_family_comp_index));
RET(ff_vk_create_exec_ctx(ctx, &s->exec));
s->initialized = 1;
......@@ -290,14 +293,20 @@ fail:
static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
{
int err = 0;
VkCommandBuffer cmd_buf;
ScaleVulkanContext *s = avctx->priv;
AVVkFrame *in = (AVVkFrame *)in_f->data[0];
AVVkFrame *out = (AVVkFrame *)out_f->data[0];
VkImageMemoryBarrier barriers[AV_NUM_DATA_POINTERS*2];
int barrier_count = 0;
/* Update descriptors and init the exec context */
ff_vk_start_exec_recording(avctx, s->exec);
cmd_buf = ff_vk_get_exec_buf(avctx, s->exec);
for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++) {
RET(ff_vk_create_imageview(avctx, &s->input_images[i].imageView, in->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->input_images[i].imageView,
in->img[i],
av_vkfmt_from_pixfmt(s->vkctx.input_format)[i],
ff_comp_identity_map));
......@@ -305,7 +314,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
}
for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++) {
RET(ff_vk_create_imageview(avctx, &s->output_images[i].imageView, out->img[i],
RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView,
out->img[i],
av_vkfmt_from_pixfmt(s->vkctx.output_format)[i],
ff_comp_identity_map));
......@@ -314,8 +324,6 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
ff_vk_update_descriptor_set(avctx, s->pl, 0);
ff_vk_start_exec_recording(avctx, s->exec);
for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++) {
VkImageMemoryBarrier bar = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
......@@ -358,13 +366,13 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
out->access[i] = bar.dstAccessMask;
}
vkCmdPipelineBarrier(s->exec->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0,
0, NULL, 0, NULL, barrier_count, barriers);
ff_vk_bind_pipeline_exec(avctx, s->exec, s->pl);
vkCmdDispatch(s->exec->buf,
vkCmdDispatch(cmd_buf,
FFALIGN(s->vkctx.output_width, CGROUPS[0])/CGROUPS[0],
FFALIGN(s->vkctx.output_height, CGROUPS[1])/CGROUPS[1], 1);
......@@ -375,12 +383,10 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f)
if (err)
return err;
for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++)
ff_vk_destroy_imageview(avctx, &s->input_images[i].imageView);
for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++)
ff_vk_destroy_imageview(avctx, &s->output_images[i].imageView);
return err;
fail:
ff_vk_discard_exec_deps(avctx, s->exec);
return err;
}
......
This diff is collapsed.
......@@ -49,6 +49,17 @@
goto fail; \
} while (0)
/* Gets the queues count for a single queue family */
#define GET_QUEUE_COUNT(hwctx, graph, comp, tx) ( \
graph ? hwctx->nb_graphics_queues : \
comp ? (hwctx->nb_comp_queues ? \
hwctx->nb_comp_queues : hwctx->nb_graphics_queues) : \
tx ? (hwctx->nb_tx_queues ? hwctx->nb_tx_queues : \
(hwctx->nb_comp_queues ? \
hwctx->nb_comp_queues : hwctx->nb_graphics_queues)) : \
0 \
)
/* Useful for attaching immutable samplers to arrays */
#define DUP_SAMPLER_ARRAY4(x) (VkSampler []){ x, x, x, x, }
......@@ -98,6 +109,7 @@ typedef struct VulkanPipeline {
VkDescriptorPool desc_pool;
VkDescriptorSet *desc_set;
VkDescriptorUpdateTemplate *desc_template;
int desc_layout_num;
int descriptor_sets_num;
int pool_size_desc_num;
......@@ -106,11 +118,29 @@ typedef struct VulkanPipeline {
VkDescriptorPoolSize *pool_size_desc;
} VulkanPipeline;
typedef struct FFVkQueueCtx {
VkFence fence;
VkQueue queue;
/* Buffer dependencies */
AVBufferRef **buf_deps;
int nb_buf_deps;
int buf_deps_alloc_size;
/* Frame dependencies */
AVFrame **frame_deps;
int nb_frame_deps;
int frame_deps_alloc_size;
} FFVkQueueCtx;
typedef struct FFVkExecContext {
VkCommandPool pool;
VkCommandBuffer buf;
VkQueue queue;
VkFence fence;
VkCommandBuffer *bufs;
FFVkQueueCtx *queues;
AVBufferRef ***deps;
int *nb_deps;
int *dep_alloc_size;
VulkanPipeline *bound_pl;
......@@ -134,6 +164,11 @@ typedef struct VulkanFilterContext {
AVHWDeviceContext *device;
AVVulkanDeviceContext *hwctx;
/* State - mirrored with the exec ctx */
int cur_queue_idx;
int queue_family_idx;
int queue_count;
/* Properties */
int output_width;
int output_height;
......@@ -192,15 +227,12 @@ VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,
/**
* Create an imageview.
* Guaranteed to remain alive until the queue submission has finished executing,
* and will be destroyed after that.
*/
int ff_vk_create_imageview(AVFilterContext *avctx, VkImageView *v, VkImage img,
VkFormat fmt, const VkComponentMapping map);
/**
* Destroy an imageview. Command buffer must have completed executing, which
* ff_vk_submit_exec_queue() will ensure
*/
void ff_vk_destroy_imageview(AVFilterContext *avctx, VkImageView *v);
int ff_vk_create_imageview(AVFilterContext *avctx, FFVkExecContext *e,
VkImageView *v, VkImage img, VkFormat fmt,
const VkComponentMapping map);
/**
* Define a push constant for a given stage into a pipeline.
......@@ -264,7 +296,7 @@ void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,
* Init an execution context for command recording and queue submission.
* WIll be auto-freed on uninit.
*/
int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx, int queue);
int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx);
/**
* Begin recording to the command buffer. Previous execution must have been
......@@ -288,7 +320,23 @@ void ff_vk_update_push_exec(AVFilterContext *avctx, FFVkExecContext *e,
size_t size, void *src);
/**
* Adds a frame as a queue dependency. This manages semaphore signalling.
* Gets the command buffer to use for this submission from the exe context.
*/
VkCommandBuffer ff_vk_get_exec_buf(AVFilterContext *avctx, FFVkExecContext *e);
/**
* Adds a generic AVBufferRef as a queue depenency.
*/
int ff_vk_add_dep_exec_ctx(AVFilterContext *avctx, FFVkExecContext *e,
AVBufferRef **deps, int nb_deps);
/**
* Discards all queue dependencies
*/
void ff_vk_discard_exec_deps(AVFilterContext *avctx, FFVkExecContext *e);
/**
* Adds a frame as a queue dependency. This also manages semaphore signalling.
* Must be called before submission.
*/
int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment