History

lavfi/vulkan: refactor, fix and fully implement multiple queues

Lynne committed 5 years ago

Commit f705e9e1 parent 246f8416 files · +336 −216Browse files

Showing the changes to libavfilter (6 of 6 files). Show all

Unified · Split

6 files
  1. libavfilter/vf_avgblur_vulkan.c +14 −13
  2. libavfilter/vf_chromaber_vulkan.c +14 −11
  3. libavfilter/vf_overlay_vulkan.c +17 −13
  4. libavfilter/vf_scale_vulkan.c +15 −12
  5. libavfilter/vulkan.c +210 −130
  6. libavfilter/vulkan.h +66 −37
libavfilter/vf_avgblur_vulkan.c+14−13
@@ -24,12 +24,13 @@ · 23 unmodified lines
24#define CGS 3224#define CGS 32
25 25
26typedef struct AvgBlurVulkanContext {26typedef struct AvgBlurVulkanContext {
27 VulkanFilterContext vkctx;27 FFVulkanContext vkctx;
28 28
29 int initialized;29 int initialized;
30 FFVkQueueFamilyCtx qf;
30 FFVkExecContext *exec;31 FFVkExecContext *exec;
31 VulkanPipeline *pl_hor;32 FFVulkanPipeline *pl_hor;
32 VulkanPipeline *pl_ver;33 FFVulkanPipeline *pl_ver;
33 34
34 /* Shader updators, must be in the main filter struct */35 /* Shader updators, must be in the main filter struct */
35 VkDescriptorImageInfo input_images[3];36 VkDescriptorImageInfo input_images[3];
@@ -73,16 +74,14 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 37 unmodified lines
73 FFSPIRVShader *shd;74 FFSPIRVShader *shd;
74 AvgBlurVulkanContext *s = ctx->priv;75 AvgBlurVulkanContext *s = ctx->priv;
75 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);76 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
76 VkSampler *sampler = ff_vk_init_sampler(ctx, 1, VK_FILTER_LINEAR);
77 77
78 VulkanDescriptorSetBinding desc_i[2] = {78 FFVulkanDescriptorSetBinding desc_i[2] = {
79 {79 {
80 .name = "input_img",80 .name = "input_img",
81 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,81 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
82 .dimensions = 2,82 .dimensions = 2,
83 .elems = planes,83 .elems = planes,
84 .stages = VK_SHADER_STAGE_COMPUTE_BIT,84 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
85 .samplers = DUP_SAMPLER_ARRAY4(*sampler),
86 },85 },
87 {86 {
88 .name = "output_img",87 .name = "output_img",
@@ -95,17 +94,17 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 6 unmodified lines
95 },94 },
96 };95 };
97 96
98 if (!sampler)97 ff_vk_qf_init(ctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
99 return AVERROR_EXTERNAL;
100 98
101 s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;99 desc_i[0].sampler = ff_vk_init_sampler(ctx, 1, VK_FILTER_LINEAR);
102 s->vkctx.queue_count = s->vkctx.hwctx->nb_comp_queues;100 if (!desc_i[0].sampler)
101 return AVERROR_EXTERNAL;
103 102
104 { /* Create shader for the horizontal pass */103 { /* Create shader for the horizontal pass */
105 desc_i[0].updater = s->input_images;104 desc_i[0].updater = s->input_images;
106 desc_i[1].updater = s->tmp_images;105 desc_i[1].updater = s->tmp_images;
107 106
108 s->pl_hor = ff_vk_create_pipeline(ctx);107 s->pl_hor = ff_vk_create_pipeline(ctx, &s->qf);
109 if (!s->pl_hor)108 if (!s->pl_hor)
110 return AVERROR(ENOMEM);109 return AVERROR(ENOMEM);
111 110
@@ -148,7 +147,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 36 unmodified lines
148 desc_i[0].updater = s->tmp_images;147 desc_i[0].updater = s->tmp_images;
149 desc_i[1].updater = s->output_images;148 desc_i[1].updater = s->output_images;
150 149
151 s->pl_ver = ff_vk_create_pipeline(ctx);150 s->pl_ver = ff_vk_create_pipeline(ctx, &s->qf);
152 if (!s->pl_ver)151 if (!s->pl_ver)
153 return AVERROR(ENOMEM);152 return AVERROR(ENOMEM);
154 153
@@ -188,7 +187,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 33 unmodified lines
188 }187 }
189 188
190 /* Execution context */189 /* Execution context */
191 RET(ff_vk_create_exec_ctx(ctx, &s->exec));190 RET(ff_vk_create_exec_ctx(ctx, &s->exec, &s->qf));
192 191
193 s->initialized = 1;192 s->initialized = 1;
194 193
@@ -311,6 +310,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *tmp_f · 116 unmodified lines
311 if (err)310 if (err)
312 return err;311 return err;
313 312
313 ff_vk_qf_rotate(&s->qf);
314
314 return err;315 return err;
315 316
316fail:317fail:
libavfilter/vf_chromaber_vulkan.c+14−11
@@ -24,11 +24,12 @@ · 23 unmodified lines
24#define CGROUPS (int [3]){ 32, 32, 1 }24#define CGROUPS (int [3]){ 32, 32, 1 }
25 25
26typedef struct ChromaticAberrationVulkanContext {26typedef struct ChromaticAberrationVulkanContext {
27 VulkanFilterContext vkctx;27 FFVulkanContext vkctx;
28 28
29 int initialized;29 int initialized;
30 FFVkQueueFamilyCtx qf;
30 FFVkExecContext *exec;31 FFVkExecContext *exec;
31 VulkanPipeline *pl;32 FFVulkanPipeline *pl;
32 33
33 /* Shader updators, must be in the main filter struct */34 /* Shader updators, must be in the main filter struct */
34 VkDescriptorImageInfo input_images[3];35 VkDescriptorImageInfo input_images[3];
@@ -67,17 +68,18 @@ static const char distort_chroma_kernel[] = { · 32 unmodified lines
67static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)68static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
68{69{
69 int err;70 int err;
71 FFVkSampler *sampler;
70 ChromaticAberrationVulkanContext *s = ctx->priv;72 ChromaticAberrationVulkanContext *s = ctx->priv;
73 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
74
75 ff_vk_qf_init(ctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
71 76
72 /* Create a sampler */77 /* Create a sampler */
73 VkSampler *sampler = ff_vk_init_sampler(ctx, 0, VK_FILTER_LINEAR);78 sampler = ff_vk_init_sampler(ctx, 0, VK_FILTER_LINEAR);
74 if (!sampler)79 if (!sampler)
75 return AVERROR_EXTERNAL;80 return AVERROR_EXTERNAL;
76 81
77 s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;82 s->pl = ff_vk_create_pipeline(ctx, &s->qf);
78 s->vkctx.queue_count = s->vkctx.hwctx->nb_comp_queues;
79
80 s->pl = ff_vk_create_pipeline(ctx);
81 if (!s->pl)83 if (!s->pl)
82 return AVERROR(ENOMEM);84 return AVERROR(ENOMEM);
83 85
@@ -86,8 +88,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 2 unmodified lines
86 s->opts.dist[1] = (s->opts.dist[1] / 100.0f) + 1.0f;88 s->opts.dist[1] = (s->opts.dist[1] / 100.0f) + 1.0f;
87 89
88 { /* Create the shader */90 { /* Create the shader */
89 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);91 FFVulkanDescriptorSetBinding desc_i[2] = {
90 VulkanDescriptorSetBinding desc_i[2] = {
91 {92 {
92 .name = "input_img",93 .name = "input_img",
93 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,94 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
@@ -95,7 +96,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 1 unmodified line
95 .elems = planes,96 .elems = planes,
96 .stages = VK_SHADER_STAGE_COMPUTE_BIT,97 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
97 .updater = s->input_images,98 .updater = s->input_images,
98 .samplers = DUP_SAMPLER_ARRAY4(*sampler),99 .sampler = sampler,
99 },100 },
100 {101 {
101 .name = "output_img",102 .name = "output_img",
@@ -158,7 +159,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 56 unmodified lines
158 RET(ff_vk_init_compute_pipeline(ctx, s->pl));159 RET(ff_vk_init_compute_pipeline(ctx, s->pl));
159 160
160 /* Execution context */161 /* Execution context */
161 RET(ff_vk_create_exec_ctx(ctx, &s->exec));162 RET(ff_vk_create_exec_ctx(ctx, &s->exec, &s->qf));
162 163
163 s->initialized = 1;164 s->initialized = 1;
164 165
@@ -256,6 +257,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f) · 91 unmodified lines
256 if (err)257 if (err)
257 return err;258 return err;
258 259
260 ff_vk_qf_rotate(&s->qf);
261
259 return err;262 return err;
260 263
261fail:264fail:
libavfilter/vf_overlay_vulkan.c+17−13
@@ -25,11 +25,12 @@ · 24 unmodified lines
25#define CGROUPS (int [3]){ 32, 32, 1 }25#define CGROUPS (int [3]){ 32, 32, 1 }
26 26
27typedef struct OverlayVulkanContext {27typedef struct OverlayVulkanContext {
28 VulkanFilterContext vkctx;28 FFVulkanContext vkctx;
29 29
30 int initialized;30 int initialized;
31 VulkanPipeline *pl;31 FFVkQueueFamilyCtx qf;
32 FFVkExecContext *exec;32 FFVkExecContext *exec;
33 FFVulkanPipeline *pl;
33 FFFrameSync fs;34 FFFrameSync fs;
34 FFVkBuffer params_buf;35 FFVkBuffer params_buf;
35 36
@@ -79,23 +80,24 @@ static const char overlay_alpha[] = { · 43 unmodified lines
79static av_cold int init_filter(AVFilterContext *ctx)80static av_cold int init_filter(AVFilterContext *ctx)
80{81{
81 int err;82 int err;
83 FFVkSampler *sampler;
82 OverlayVulkanContext *s = ctx->priv;84 OverlayVulkanContext *s = ctx->priv;
83 VkSampler *sampler = ff_vk_init_sampler(ctx, 1, VK_FILTER_NEAREST);85 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
86
87 ff_vk_qf_init(ctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
88
89 sampler = ff_vk_init_sampler(ctx, 1, VK_FILTER_NEAREST);
84 if (!sampler)90 if (!sampler)
85 return AVERROR_EXTERNAL;91 return AVERROR_EXTERNAL;
86 92
87 s->pl = ff_vk_create_pipeline(ctx);93 s->pl = ff_vk_create_pipeline(ctx, &s->qf);
88 if (!s->pl)94 if (!s->pl)
89 return AVERROR(ENOMEM);95 return AVERROR(ENOMEM);
90 96
91 s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;
92 s->vkctx.queue_count = s->vkctx.hwctx->nb_comp_queues;
93
94 { /* Create the shader */97 { /* Create the shader */
95 const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
96 const int ialpha = av_pix_fmt_desc_get(s->vkctx.input_format)->flags & AV_PIX_FMT_FLAG_ALPHA;98 const int ialpha = av_pix_fmt_desc_get(s->vkctx.input_format)->flags & AV_PIX_FMT_FLAG_ALPHA;
97 99
98 VulkanDescriptorSetBinding desc_i[3] = {100 FFVulkanDescriptorSetBinding desc_i[3] = {
99 {101 {
100 .name = "main_img",102 .name = "main_img",
101 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,103 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
@@ -103,7 +105,7 @@ static av_cold int init_filter(AVFilterContext *ctx) · 1 unmodified line
103 .elems = planes,105 .elems = planes,
104 .stages = VK_SHADER_STAGE_COMPUTE_BIT,106 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
105 .updater = s->main_images,107 .updater = s->main_images,
106 .samplers = DUP_SAMPLER_ARRAY4(*sampler),108 .sampler = sampler,
107 },109 },
108 {110 {
109 .name = "overlay_img",111 .name = "overlay_img",
@@ -112,7 +114,7 @@ static av_cold int init_filter(AVFilterContext *ctx) · 2 unmodified lines
112 .elems = planes,114 .elems = planes,
113 .stages = VK_SHADER_STAGE_COMPUTE_BIT,115 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
114 .updater = s->overlay_images,116 .updater = s->overlay_images,
115 .samplers = DUP_SAMPLER_ARRAY4(*sampler),117 .sampler = sampler,
116 },118 },
117 {119 {
118 .name = "output_img",120 .name = "output_img",
@@ -126,7 +128,7 @@ static av_cold int init_filter(AVFilterContext *ctx) · 7 unmodified lines
126 },128 },
127 };129 };
128 130
129 VulkanDescriptorSetBinding desc_b = {131 FFVulkanDescriptorSetBinding desc_b = {
130 .name = "params",132 .name = "params",
131 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,133 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
132 .mem_quali = "readonly",134 .mem_quali = "readonly",
@@ -215,7 +217,7 @@ static av_cold int init_filter(AVFilterContext *ctx) · 82 unmodified lines
215 }217 }
216 218
217 /* Execution context */219 /* Execution context */
218 RET(ff_vk_create_exec_ctx(ctx, &s->exec));220 RET(ff_vk_create_exec_ctx(ctx, &s->exec, &s->qf));
219 221
220 s->initialized = 1;222 s->initialized = 1;
221 223
@@ -339,6 +341,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, · 117 unmodified lines
339 if (err)341 if (err)
340 return err;342 return err;
341 343
344 ff_vk_qf_rotate(&s->qf);
345
342 return err;346 return err;
343 347
344fail:348fail:
libavfilter/vf_scale_vulkan.c+15−12
@@ -33,11 +33,12 @@ enum ScalerFunc { · 32 unmodified lines
33};33};
34 34
35typedef struct ScaleVulkanContext {35typedef struct ScaleVulkanContext {
36 VulkanFilterContext vkctx;36 FFVulkanContext vkctx;
37 37
38 int initialized;38 int initialized;
39 FFVkQueueFamilyCtx qf;
39 FFVkExecContext *exec;40 FFVkExecContext *exec;
40 VulkanPipeline *pl;41 FFVulkanPipeline *pl;
41 FFVkBuffer params_buf;42 FFVkBuffer params_buf;
42 43
43 /* Shader updators, must be in the main filter struct */44 /* Shader updators, must be in the main filter struct */
@@ -107,7 +108,7 @@ static const char write_444[] = { · 63 unmodified lines
107static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)108static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
108{109{
109 int err;110 int err;
110 VkSampler *sampler;111 FFVkSampler *sampler;
111 VkFilter sampler_mode;112 VkFilter sampler_mode;
112 ScaleVulkanContext *s = ctx->priv;113 ScaleVulkanContext *s = ctx->priv;
113 114
@@ -115,9 +116,9 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 1 unmodified line
115 int crop_y = in->crop_top;116 int crop_y = in->crop_top;
116 int crop_w = in->width - (in->crop_left + in->crop_right);117 int crop_w = in->width - (in->crop_left + in->crop_right);
117 int crop_h = in->height - (in->crop_top + in->crop_bottom);118 int crop_h = in->height - (in->crop_top + in->crop_bottom);
119 int in_planes = av_pix_fmt_count_planes(s->vkctx.input_format);
118 120
119 s->vkctx.queue_family_idx = s->vkctx.hwctx->queue_family_comp_index;121 ff_vk_qf_init(ctx, &s->qf, VK_QUEUE_COMPUTE_BIT, 0);
120 s->vkctx.queue_count = s->vkctx.hwctx->nb_comp_queues;
121 122
122 switch (s->scaler) {123 switch (s->scaler) {
123 case F_NEAREST:124 case F_NEAREST:
@@ -133,20 +134,20 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 9 unmodified lines
133 if (!sampler)134 if (!sampler)
134 return AVERROR_EXTERNAL;135 return AVERROR_EXTERNAL;
135 136
136 s->pl = ff_vk_create_pipeline(ctx);137 s->pl = ff_vk_create_pipeline(ctx, &s->qf);
137 if (!s->pl)138 if (!s->pl)
138 return AVERROR(ENOMEM);139 return AVERROR(ENOMEM);
139 140
140 { /* Create the shader */141 { /* Create the shader */
141 VulkanDescriptorSetBinding desc_i[2] = {142 FFVulkanDescriptorSetBinding desc_i[2] = {
142 {143 {
143 .name = "input_img",144 .name = "input_img",
144 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,145 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
145 .dimensions = 2,146 .dimensions = 2,
146 .elems = av_pix_fmt_count_planes(s->vkctx.input_format),147 .elems = in_planes,
147 .stages = VK_SHADER_STAGE_COMPUTE_BIT,148 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
148 .updater = s->input_images,149 .updater = s->input_images,
149 .samplers = DUP_SAMPLER_ARRAY4(*sampler),150 .sampler = sampler,
150 },151 },
151 {152 {
152 .name = "output_img",153 .name = "output_img",
@@ -160,7 +161,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 7 unmodified lines
160 },161 },
161 };162 };
162 163
163 VulkanDescriptorSetBinding desc_b = {164 FFVulkanDescriptorSetBinding desc_b = {
164 .name = "params",165 .name = "params",
165 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,166 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
166 .mem_quali = "readonly",167 .mem_quali = "readonly",
@@ -178,7 +179,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 11 unmodified lines
178 ff_vk_set_compute_shader_sizes(ctx, shd, CGROUPS);179 ff_vk_set_compute_shader_sizes(ctx, shd, CGROUPS);
179 180
180 RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, desc_i, 2, 0)); /* set 0 */181 RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, desc_i, 2, 0)); /* set 0 */
181 RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, &desc_b, 1, 0)); /* set 0 */182 RET(ff_vk_add_descriptor_set(ctx, s->pl, shd, &desc_b, 1, 0)); /* set 1 */
182 183
183 GLSLD( scale_bilinear );184 GLSLD( scale_bilinear );
184 185
@@ -280,7 +281,7 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in) · 95 unmodified lines
280 }281 }
281 282
282 /* Execution context */283 /* Execution context */
283 RET(ff_vk_create_exec_ctx(ctx, &s->exec));284 RET(ff_vk_create_exec_ctx(ctx, &s->exec, &s->qf));
284 285
285 s->initialized = 1;286 s->initialized = 1;
286 287
@@ -384,6 +385,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out_f, AVFrame *in_f) · 97 unmodified lines
384 if (err)385 if (err)
385 return err;386 return err;
386 387
388 ff_vk_qf_rotate(&s->qf);
389
387 return err;390 return err;
388 391
389fail:392fail:
libavfilter/vulkan.c+210−130
@@ -20,6 +20,7 @@ · 19 unmodified lines
20#include "vulkan.h"20#include "vulkan.h"
21#include "glslang.h"21#include "glslang.h"
22 22
23#include "libavutil/avassert.h"
23#include "libavutil/vulkan_loader.h"24#include "libavutil/vulkan_loader.h"
24 25
25/* Generic macro for creating contexts which need to keep their addresses26/* Generic macro for creating contexts which need to keep their addresses
@@ -88,15 +89,54 @@ const char *ff_vk_ret2str(VkResult res) · 62 unmodified lines
88#undef CASE89#undef CASE
89}90}
90 91
92void ff_vk_qf_init(AVFilterContext *avctx, FFVkQueueFamilyCtx *qf,
93 VkQueueFlagBits dev_family, int queue_limit)
94{
95 FFVulkanContext *s = avctx->priv;
96
97 if (!queue_limit)
98 queue_limit = INT32_MAX;
99
100 switch (dev_family) {
101 case VK_QUEUE_GRAPHICS_BIT:
102 qf->queue_family = s->hwctx->queue_family_index;
103 qf->nb_queues = FFMIN(s->hwctx->nb_graphics_queues, queue_limit);
104 return;
105 case VK_QUEUE_COMPUTE_BIT:
106 qf->queue_family = s->hwctx->queue_family_comp_index;
107 qf->nb_queues = FFMIN(s->hwctx->nb_comp_queues, queue_limit);
108 return;
109 case VK_QUEUE_TRANSFER_BIT:
110 qf->queue_family = s->hwctx->queue_family_tx_index;
111 qf->nb_queues = FFMIN(s->hwctx->nb_tx_queues, queue_limit);
112 return;
113 case VK_QUEUE_VIDEO_ENCODE_BIT_KHR:
114 qf->queue_family = s->hwctx->queue_family_encode_index;
115 qf->nb_queues = FFMIN(s->hwctx->nb_encode_queues, queue_limit);
116 return;
117 case VK_QUEUE_VIDEO_DECODE_BIT_KHR:
118 qf->queue_family = s->hwctx->queue_family_decode_index;
119 qf->nb_queues = FFMIN(s->hwctx->nb_decode_queues, queue_limit);
120 return;
121 default:
122 av_assert0(0); /* Should never happen */
123 }
124
125 return;
126}
127
128void ff_vk_qf_rotate(FFVkQueueFamilyCtx *qf)
129{
130 qf->cur_queue = (qf->cur_queue + 1) % qf->nb_queues;
131}
132
91static int vk_alloc_mem(AVFilterContext *avctx, VkMemoryRequirements *req,133static int vk_alloc_mem(AVFilterContext *avctx, VkMemoryRequirements *req,
92 VkMemoryPropertyFlagBits req_flags, void *alloc_extension,134 VkMemoryPropertyFlagBits req_flags, void *alloc_extension,
93 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)135 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
94{136{
95 VkResult ret;137 VkResult ret;
96 int index = -1;138 int index = -1;
97 VkPhysicalDeviceProperties props;139 FFVulkanContext *s = avctx->priv;
98 VkPhysicalDeviceMemoryProperties mprops;
99 VulkanFilterContext *s = avctx->priv;
100 FFVulkanFunctions *vk = &s->vkfn;140 FFVulkanFunctions *vk = &s->vkfn;
101 141
102 VkMemoryAllocateInfo alloc_info = {142 VkMemoryAllocateInfo alloc_info = {
@@ -104,24 +144,21 @@ static int vk_alloc_mem(AVFilterContext *avctx, VkMemoryRequirements *req, · 1 unmodified line
104 .pNext = alloc_extension,144 .pNext = alloc_extension,
105 };145 };
106 146
107 vk->GetPhysicalDeviceProperties(s->hwctx->phys_dev, &props);
108 vk->GetPhysicalDeviceMemoryProperties(s->hwctx->phys_dev, &mprops);
109
110 /* Align if we need to */147 /* Align if we need to */
111 if (req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)148 if (req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
112 req->size = FFALIGN(req->size, props.limits.minMemoryMapAlignment);149 req->size = FFALIGN(req->size, s->props.limits.minMemoryMapAlignment);
113 150
114 alloc_info.allocationSize = req->size;151 alloc_info.allocationSize = req->size;
115 152
116 /* The vulkan spec requires memory types to be sorted in the "optimal"153 /* The vulkan spec requires memory types to be sorted in the "optimal"
117 * order, so the first matching type we find will be the best/fastest one */154 * order, so the first matching type we find will be the best/fastest one */
118 for (int i = 0; i < mprops.memoryTypeCount; i++) {155 for (int i = 0; i < s->mprops.memoryTypeCount; i++) {
119 /* The memory type must be supported by the requirements (bitfield) */156 /* The memory type must be supported by the requirements (bitfield) */
120 if (!(req->memoryTypeBits & (1 << i)))157 if (!(req->memoryTypeBits & (1 << i)))
121 continue;158 continue;
122 159
123 /* The memory type flags must include our properties */160 /* The memory type flags must include our properties */
124 if ((mprops.memoryTypes[i].propertyFlags & req_flags) != req_flags)161 if ((s->mprops.memoryTypes[i].propertyFlags & req_flags) != req_flags)
125 continue;162 continue;
126 163
127 /* Found a suitable memory type */164 /* Found a suitable memory type */
@@ -145,7 +182,7 @@ static int vk_alloc_mem(AVFilterContext *avctx, VkMemoryRequirements *req, · 17 unmodified lines
145 return AVERROR(ENOMEM);182 return AVERROR(ENOMEM);
146 }183 }
147 184
148 *mem_flags |= mprops.memoryTypes[index].propertyFlags;185 *mem_flags |= s->mprops.memoryTypes[index].propertyFlags;
149 186
150 return 0;187 return 0;
151}188}
@@ -156,7 +193,7 @@ int ff_vk_create_buf(AVFilterContext *avctx, FFVkBuffer *buf, size_t size, · 4 unmodified lines
156 int err;193 int err;
157 VkResult ret;194 VkResult ret;
158 int use_ded_mem;195 int use_ded_mem;
159 VulkanFilterContext *s = avctx->priv;196 FFVulkanContext *s = avctx->priv;
160 FFVulkanFunctions *vk = &s->vkfn;197 FFVulkanFunctions *vk = &s->vkfn;
161 198
162 VkBufferCreateInfo buf_spawn = {199 VkBufferCreateInfo buf_spawn = {
@@ -220,7 +257,7 @@ int ff_vk_map_buffers(AVFilterContext *avctx, FFVkBuffer *buf, uint8_t *mem[], · 57 unmodified lines
220 int nb_buffers, int invalidate)257 int nb_buffers, int invalidate)
221{258{
222 VkResult ret;259 VkResult ret;
223 VulkanFilterContext *s = avctx->priv;260 FFVulkanContext *s = avctx->priv;
224 FFVulkanFunctions *vk = &s->vkfn;261 FFVulkanFunctions *vk = &s->vkfn;
225 VkMappedMemoryRange *inval_list = NULL;262 VkMappedMemoryRange *inval_list = NULL;
226 int inval_count = 0;263 int inval_count = 0;
@@ -271,7 +308,7 @@ int ff_vk_unmap_buffers(AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers, · 44 unmodified lines
271{308{
272 int err = 0;309 int err = 0;
273 VkResult ret;310 VkResult ret;
274 VulkanFilterContext *s = avctx->priv;311 FFVulkanContext *s = avctx->priv;
275 FFVulkanFunctions *vk = &s->vkfn;312 FFVulkanFunctions *vk = &s->vkfn;
276 VkMappedMemoryRange *flush_list = NULL;313 VkMappedMemoryRange *flush_list = NULL;
277 int flush_count = 0;314 int flush_count = 0;
@@ -311,7 +348,7 @@ int ff_vk_unmap_buffers(AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers, · 33 unmodified lines
311 348
312void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf)349void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf)
313{350{
314 VulkanFilterContext *s = avctx->priv;351 FFVulkanContext *s = avctx->priv;
315 FFVulkanFunctions *vk = &s->vkfn;352 FFVulkanFunctions *vk = &s->vkfn;
316 353
317 if (!buf)354 if (!buf)
@@ -323,7 +360,7 @@ void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf) · 5 unmodified lines
323 vk->FreeMemory(s->hwctx->act_dev, buf->mem, s->hwctx->alloc);360 vk->FreeMemory(s->hwctx->act_dev, buf->mem, s->hwctx->alloc);
324}361}
325 362
326int ff_vk_add_push_constant(AVFilterContext *avctx, VulkanPipeline *pl,363int ff_vk_add_push_constant(AVFilterContext *avctx, FFVulkanPipeline *pl,
327 int offset, int size, VkShaderStageFlagBits stage)364 int offset, int size, VkShaderStageFlagBits stage)
328{365{
329 VkPushConstantRange *pc;366 VkPushConstantRange *pc;
@@ -343,37 +380,37 @@ int ff_vk_add_push_constant(AVFilterContext *avctx, VulkanPipeline *pl, · 13 unmodified lines
343 return 0;380 return 0;
344}381}
345 382
346FN_CREATING(VulkanFilterContext, FFVkExecContext, exec_ctx, exec_ctx, exec_ctx_num)383FN_CREATING(FFVulkanContext, FFVkExecContext, exec_ctx, exec_ctx, exec_ctx_num)
347int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx)384int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx,
385 FFVkQueueFamilyCtx *qf)
348{386{
349 VkResult ret;387 VkResult ret;
350 FFVkExecContext *e;388 FFVkExecContext *e;
351 VulkanFilterContext *s = avctx->priv;389 FFVulkanContext *s = avctx->priv;
352 FFVulkanFunctions *vk = &s->vkfn;390 FFVulkanFunctions *vk = &s->vkfn;
353 391
354 int queue_family = s->queue_family_idx;
355 int nb_queues = s->queue_count;
356
357 VkCommandPoolCreateInfo cqueue_create = {392 VkCommandPoolCreateInfo cqueue_create = {
358 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,393 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
359 .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,394 .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
360 .queueFamilyIndex = queue_family,395 .queueFamilyIndex = qf->queue_family,
361 };396 };
362 VkCommandBufferAllocateInfo cbuf_create = {397 VkCommandBufferAllocateInfo cbuf_create = {
363 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,398 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
364 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,399 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
365 .commandBufferCount = nb_queues,400 .commandBufferCount = qf->nb_queues,
366 };401 };
367 402
368 e = create_exec_ctx(s);403 e = create_exec_ctx(s);
369 if (!e)404 if (!e)
370 return AVERROR(ENOMEM);405 return AVERROR(ENOMEM);
371 406
372 e->queues = av_mallocz(nb_queues * sizeof(*e->queues));407 e->qf = qf;
408
409 e->queues = av_mallocz(qf->nb_queues * sizeof(*e->queues));
373 if (!e->queues)410 if (!e->queues)
374 return AVERROR(ENOMEM);411 return AVERROR(ENOMEM);
375 412
376 e->bufs = av_mallocz(nb_queues * sizeof(*e->bufs));413 e->bufs = av_mallocz(qf->nb_queues * sizeof(*e->bufs));
377 if (!e->bufs)414 if (!e->bufs)
378 return AVERROR(ENOMEM);415 return AVERROR(ENOMEM);
379 416
@@ -396,9 +433,9 @@ int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx) · 16 unmodified lines
396 return AVERROR_EXTERNAL;433 return AVERROR_EXTERNAL;
397 }434 }
398 435
399 for (int i = 0; i < nb_queues; i++) {436 for (int i = 0; i < qf->nb_queues; i++) {
400 FFVkQueueCtx *q = &e->queues[i];437 FFVkQueueCtx *q = &e->queues[i];
401 vk->GetDeviceQueue(s->hwctx->act_dev, queue_family, i, &q->queue);438 vk->GetDeviceQueue(s->hwctx->act_dev, qf->queue_family, i, &q->queue);
402 }439 }
403 440
404 *ctx = e;441 *ctx = e;
@@ -408,8 +445,7 @@ int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx) · 3 unmodified lines
408 445
409void ff_vk_discard_exec_deps(AVFilterContext *avctx, FFVkExecContext *e)446void ff_vk_discard_exec_deps(AVFilterContext *avctx, FFVkExecContext *e)
410{447{
411 VulkanFilterContext *s = avctx->priv;448 FFVkQueueCtx *q = &e->queues[e->qf->cur_queue];
412 FFVkQueueCtx *q = &e->queues[s->cur_queue_idx];
413 449
414 for (int j = 0; j < q->nb_buf_deps; j++)450 for (int j = 0; j < q->nb_buf_deps; j++)
415 av_buffer_unref(&q->buf_deps[j]);451 av_buffer_unref(&q->buf_deps[j]);
@@ -426,9 +462,9 @@ void ff_vk_discard_exec_deps(AVFilterContext *avctx, FFVkExecContext *e) · 10 unmodified lines
426int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e)462int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e)
427{463{
428 VkResult ret;464 VkResult ret;
429 VulkanFilterContext *s = avctx->priv;465 FFVulkanContext *s = avctx->priv;
430 FFVulkanFunctions *vk = &s->vkfn;466 FFVulkanFunctions *vk = &s->vkfn;
431 FFVkQueueCtx *q = &e->queues[s->cur_queue_idx];467 FFVkQueueCtx *q = &e->queues[e->qf->cur_queue];
432 468
433 VkCommandBufferBeginInfo cmd_start = {469 VkCommandBufferBeginInfo cmd_start = {
434 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,470 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
@@ -455,7 +491,7 @@ int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e) · 20 unmodified lines
455 /* Discard queue dependencies */491 /* Discard queue dependencies */
456 ff_vk_discard_exec_deps(avctx, e);492 ff_vk_discard_exec_deps(avctx, e);
457 493
458 ret = vk->BeginCommandBuffer(e->bufs[s->cur_queue_idx], &cmd_start);494 ret = vk->BeginCommandBuffer(e->bufs[e->qf->cur_queue], &cmd_start);
459 if (ret != VK_SUCCESS) {495 if (ret != VK_SUCCESS) {
460 av_log(avctx, AV_LOG_ERROR, "Failed to start command recoding: %s\n",496 av_log(avctx, AV_LOG_ERROR, "Failed to start command recoding: %s\n",
461 ff_vk_ret2str(ret));497 ff_vk_ret2str(ret));
@@ -467,17 +503,15 @@ int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e) · 5 unmodified lines
467 503
468VkCommandBuffer ff_vk_get_exec_buf(AVFilterContext *avctx, FFVkExecContext *e)504VkCommandBuffer ff_vk_get_exec_buf(AVFilterContext *avctx, FFVkExecContext *e)
469{505{
470 VulkanFilterContext *s = avctx->priv;506 return e->bufs[e->qf->cur_queue];
471 return e->bufs[s->cur_queue_idx];
472}507}
473 508
474int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e,509int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e,
475 AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)510 AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
476{511{
477 AVFrame **dst;512 AVFrame **dst;
478 VulkanFilterContext *s = avctx->priv;
479 AVVkFrame *f = (AVVkFrame *)frame->data[0];513 AVVkFrame *f = (AVVkFrame *)frame->data[0];
480 FFVkQueueCtx *q = &e->queues[s->cur_queue_idx];514 FFVkQueueCtx *q = &e->queues[e->qf->cur_queue];
481 AVHWFramesContext *fc = (AVHWFramesContext *)frame->hw_frames_ctx->data;515 AVHWFramesContext *fc = (AVHWFramesContext *)frame->hw_frames_ctx->data;
482 int planes = av_pix_fmt_count_planes(fc->sw_format);516 int planes = av_pix_fmt_count_planes(fc->sw_format);
483 517
@@ -517,16 +551,21 @@ int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e, · 33 unmodified lines
517 return AVERROR(ENOMEM);551 return AVERROR(ENOMEM);
518 }552 }
519 553
554 e->sem_sig_val_dst = av_fast_realloc(e->sem_sig_val_dst, &e->sem_sig_val_dst_alloc,
555 (e->sem_sig_cnt + 1)*sizeof(*e->sem_sig_val_dst));
556 if (!e->sem_sig_val_dst) {
557 ff_vk_discard_exec_deps(avctx, e);
558 return AVERROR(ENOMEM);
559 }
560
520 e->sem_wait[e->sem_wait_cnt] = f->sem[i];561 e->sem_wait[e->sem_wait_cnt] = f->sem[i];
521 e->sem_wait_dst[e->sem_wait_cnt] = in_wait_dst_flag;562 e->sem_wait_dst[e->sem_wait_cnt] = in_wait_dst_flag;
522 e->sem_wait_val[e->sem_wait_cnt] = f->sem_value[i];563 e->sem_wait_val[e->sem_wait_cnt] = f->sem_value[i];
523 e->sem_wait_cnt++;564 e->sem_wait_cnt++;
524 565
525 /* TODO: fix this in case execution fails */
526 f->sem_value[i]++;
527
528 e->sem_sig[e->sem_sig_cnt] = f->sem[i];566 e->sem_sig[e->sem_sig_cnt] = f->sem[i];
529 e->sem_sig_val[e->sem_sig_cnt] = f->sem_value[i];567 e->sem_sig_val[e->sem_sig_cnt] = f->sem_value[i] + 1;
568 e->sem_sig_val_dst[e->sem_sig_cnt] = &f->sem_value[i];
530 e->sem_sig_cnt++;569 e->sem_sig_cnt++;
531 }570 }
532 571
@@ -551,9 +590,9 @@ int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e, · 18 unmodified lines
551int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e)590int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e)
552{591{
553 VkResult ret;592 VkResult ret;
554 VulkanFilterContext *s = avctx->priv;593 FFVulkanContext *s = avctx->priv;
555 FFVulkanFunctions *vk = &s->vkfn;594 FFVulkanFunctions *vk = &s->vkfn;
556 FFVkQueueCtx *q = &e->queues[s->cur_queue_idx];595 FFVkQueueCtx *q = &e->queues[e->qf->cur_queue];
557 596
558 VkTimelineSemaphoreSubmitInfo s_timeline_sem_info = {597 VkTimelineSemaphoreSubmitInfo s_timeline_sem_info = {
559 .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO,598 .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO,
@@ -568,7 +607,7 @@ int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e) · 8 unmodified lines
568 .pNext = &s_timeline_sem_info,607 .pNext = &s_timeline_sem_info,
569 608
570 .commandBufferCount = 1,609 .commandBufferCount = 1,
571 .pCommandBuffers = &e->bufs[s->cur_queue_idx],610 .pCommandBuffers = &e->bufs[e->qf->cur_queue],
572 611
573 .pWaitSemaphores = e->sem_wait,612 .pWaitSemaphores = e->sem_wait,
574 .pWaitDstStageMask = e->sem_wait_dst,613 .pWaitDstStageMask = e->sem_wait_dst,
@@ -578,7 +617,7 @@ int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e) · 3 unmodified lines
578 .signalSemaphoreCount = e->sem_sig_cnt,617 .signalSemaphoreCount = e->sem_sig_cnt,
579 };618 };
580 619
581 ret = vk->EndCommandBuffer(e->bufs[s->cur_queue_idx]);620 ret = vk->EndCommandBuffer(e->bufs[e->qf->cur_queue]);
582 if (ret != VK_SUCCESS) {621 if (ret != VK_SUCCESS) {
583 av_log(avctx, AV_LOG_ERROR, "Unable to finish command buffer: %s\n",622 av_log(avctx, AV_LOG_ERROR, "Unable to finish command buffer: %s\n",
584 ff_vk_ret2str(ret));623 ff_vk_ret2str(ret));
@@ -592,8 +631,8 @@ int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e) · 7 unmodified lines
592 return AVERROR_EXTERNAL;631 return AVERROR_EXTERNAL;
593 }632 }
594 633
595 /* Rotate queues */634 for (int i = 0; i < e->sem_sig_cnt; i++)
596 s->cur_queue_idx = (s->cur_queue_idx + 1) % s->queue_count;635 *e->sem_sig_val_dst[i] += 1;
597 636
598 return 0;637 return 0;
599}638}
@@ -602,8 +641,7 @@ int ff_vk_add_dep_exec_ctx(AVFilterContext *avctx, FFVkExecContext *e, · 2 unmodified lines
602 AVBufferRef **deps, int nb_deps)641 AVBufferRef **deps, int nb_deps)
603{642{
604 AVBufferRef **dst;643 AVBufferRef **dst;
605 VulkanFilterContext *s = avctx->priv;644 FFVkQueueCtx *q = &e->queues[e->qf->cur_queue];
606 FFVkQueueCtx *q = &e->queues[s->cur_queue_idx];
607 645
608 if (!deps || !nb_deps)646 if (!deps || !nb_deps)
609 return 0;647 return 0;
@@ -632,7 +670,7 @@ err: · 22 unmodified lines
632static int vulkan_filter_set_device(AVFilterContext *avctx,670static int vulkan_filter_set_device(AVFilterContext *avctx,
633 AVBufferRef *device)671 AVBufferRef *device)
634{672{
635 VulkanFilterContext *s = avctx->priv;673 FFVulkanContext *s = avctx->priv;
636 674
637 av_buffer_unref(&s->device_ref);675 av_buffer_unref(&s->device_ref);
638 676
@@ -649,7 +687,7 @@ static int vulkan_filter_set_device(AVFilterContext *avctx, · 10 unmodified lines
649static int vulkan_filter_set_frames(AVFilterContext *avctx,687static int vulkan_filter_set_frames(AVFilterContext *avctx,
650 AVBufferRef *frames)688 AVBufferRef *frames)
651{689{
652 VulkanFilterContext *s = avctx->priv;690 FFVulkanContext *s = avctx->priv;
653 691
654 av_buffer_unref(&s->frames_ref);692 av_buffer_unref(&s->frames_ref);
655 693
@@ -664,7 +702,8 @@ int ff_vk_filter_config_input(AVFilterLink *inlink) · 8 unmodified lines
664{702{
665 int err;703 int err;
666 AVFilterContext *avctx = inlink->dst;704 AVFilterContext *avctx = inlink->dst;
667 VulkanFilterContext *s = avctx->priv;705 FFVulkanContext *s = avctx->priv;
706 FFVulkanFunctions *vk = &s->vkfn;
668 AVHWFramesContext *input_frames;707 AVHWFramesContext *input_frames;
669 708
670 if (!inlink->hw_frames_ctx) {709 if (!inlink->hw_frames_ctx) {
@@ -695,6 +734,9 @@ int ff_vk_filter_config_input(AVFilterLink *inlink) · 24 unmodified lines
695 if (err < 0)734 if (err < 0)
696 return err;735 return err;
697 736
737 vk->GetPhysicalDeviceProperties(s->hwctx->phys_dev, &s->props);
738 vk->GetPhysicalDeviceMemoryProperties(s->hwctx->phys_dev, &s->mprops);
739
698 /* Default output parameters match input parameters. */740 /* Default output parameters match input parameters. */
699 s->input_format = input_frames->sw_format;741 s->input_format = input_frames->sw_format;
700 if (s->output_format == AV_PIX_FMT_NONE)742 if (s->output_format == AV_PIX_FMT_NONE)
@@ -711,7 +753,7 @@ int ff_vk_filter_config_output_inplace(AVFilterLink *outlink) · 10 unmodified lines
711{753{
712 int err;754 int err;
713 AVFilterContext *avctx = outlink->src;755 AVFilterContext *avctx = outlink->src;
714 VulkanFilterContext *s = avctx->priv;756 FFVulkanContext *s = avctx->priv;
715 757
716 av_buffer_unref(&outlink->hw_frames_ctx);758 av_buffer_unref(&outlink->hw_frames_ctx);
717 759
@@ -741,7 +783,7 @@ int ff_vk_filter_config_output(AVFilterLink *outlink) · 23 unmodified lines
741{783{
742 int err;784 int err;
743 AVFilterContext *avctx = outlink->src;785 AVFilterContext *avctx = outlink->src;
744 VulkanFilterContext *s = avctx->priv;786 FFVulkanContext *s = avctx->priv;
745 AVBufferRef *output_frames_ref;787 AVBufferRef *output_frames_ref;
746 AVHWFramesContext *output_frames;788 AVHWFramesContext *output_frames;
747 789
@@ -790,7 +832,7 @@ fail: · 42 unmodified lines
790 832
791int ff_vk_filter_init(AVFilterContext *avctx)833int ff_vk_filter_init(AVFilterContext *avctx)
792{834{
793 VulkanFilterContext *s = avctx->priv;835 FFVulkanContext *s = avctx->priv;
794 836
795 s->output_format = AV_PIX_FMT_NONE;837 s->output_format = AV_PIX_FMT_NONE;
796 838
@@ -800,12 +842,12 @@ int ff_vk_filter_init(AVFilterContext *avctx) · 3 unmodified lines
800 return 0;842 return 0;
801}843}
802 844
803FN_CREATING(VulkanFilterContext, VkSampler, sampler, samplers, samplers_num)845FN_CREATING(FFVulkanContext, FFVkSampler, sampler, samplers, samplers_num)
804VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,846FFVkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,
805 VkFilter filt)847 VkFilter filt)
806{848{
807 VkResult ret;849 VkResult ret;
808 VulkanFilterContext *s = avctx->priv;850 FFVulkanContext *s = avctx->priv;
809 FFVulkanFunctions *vk = &s->vkfn;851 FFVulkanFunctions *vk = &s->vkfn;
810 852
811 VkSamplerCreateInfo sampler_info = {853 VkSamplerCreateInfo sampler_info = {
@@ -823,19 +865,22 @@ VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords, · 11 unmodified lines
823 .unnormalizedCoordinates = unnorm_coords,865 .unnormalizedCoordinates = unnorm_coords,
824 };866 };
825 867
826 VkSampler *sampler = create_sampler(s);868 FFVkSampler *sctx = create_sampler(s);
827 if (!sampler)869 if (!sctx)
828 return NULL;870 return NULL;
829 871
830 ret = vk->CreateSampler(s->hwctx->act_dev, &sampler_info,872 ret = vk->CreateSampler(s->hwctx->act_dev, &sampler_info,
831 s->hwctx->alloc, sampler);873 s->hwctx->alloc, &sctx->sampler[0]);
832 if (ret != VK_SUCCESS) {874 if (ret != VK_SUCCESS) {
833 av_log(avctx, AV_LOG_ERROR, "Unable to init sampler: %s\n",875 av_log(avctx, AV_LOG_ERROR, "Unable to init sampler: %s\n",
834 ff_vk_ret2str(ret));876 ff_vk_ret2str(ret));
835 return NULL;877 return NULL;
836 }878 }
837 879
838 return sampler;880 for (int i = 1; i < 4; i++)
881 sctx->sampler[i] = sctx->sampler[0];
882
883 return sctx;
839}884}
840 885
841int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)886int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
@@ -863,7 +908,7 @@ typedef struct ImageViewCtx { · 21 unmodified lines
863 908
864static void destroy_imageview(void *opaque, uint8_t *data)909static void destroy_imageview(void *opaque, uint8_t *data)
865{910{
866 VulkanFilterContext *s = opaque;911 FFVulkanContext *s = opaque;
867 FFVulkanFunctions *vk = &s->vkfn;912 FFVulkanFunctions *vk = &s->vkfn;
868 ImageViewCtx *iv = (ImageViewCtx *)data;913 ImageViewCtx *iv = (ImageViewCtx *)data;
869 914
@@ -877,7 +922,7 @@ int ff_vk_create_imageview(AVFilterContext *avctx, FFVkExecContext *e, · 7 unmodified lines
877{922{
878 int err;923 int err;
879 AVBufferRef *buf;924 AVBufferRef *buf;
880 VulkanFilterContext *s = avctx->priv;925 FFVulkanContext *s = avctx->priv;
881 FFVulkanFunctions *vk = &s->vkfn;926 FFVulkanFunctions *vk = &s->vkfn;
882 927
883 VkImageViewCreateInfo imgview_spawn = {928 VkImageViewCreateInfo imgview_spawn = {
@@ -924,8 +969,8 @@ int ff_vk_create_imageview(AVFilterContext *avctx, FFVkExecContext *e, · 40 unmodified lines
924 return 0;969 return 0;
925}970}
926 971
927FN_CREATING(VulkanPipeline, FFSPIRVShader, shader, shaders, shaders_num)972FN_CREATING(FFVulkanPipeline, FFSPIRVShader, shader, shaders, shaders_num)
928FFSPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, VulkanPipeline *pl,973FFSPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, FFVulkanPipeline *pl,
929 const char *name, VkShaderStageFlags stage)974 const char *name, VkShaderStageFlags stage)
930{975{
931 FFSPIRVShader *shd = create_shader(pl);976 FFSPIRVShader *shd = create_shader(pl);
@@ -984,7 +1029,7 @@ int ff_vk_compile_shader(AVFilterContext *avctx, FFSPIRVShader *shd, · 52 unmodified lines
984{1029{
985 int err;1030 int err;
986 VkResult ret;1031 VkResult ret;
987 VulkanFilterContext *s = avctx->priv;1032 FFVulkanContext *s = avctx->priv;
988 FFVulkanFunctions *vk = &s->vkfn;1033 FFVulkanFunctions *vk = &s->vkfn;
989 VkShaderModuleCreateInfo shader_create;1034 VkShaderModuleCreateInfo shader_create;
990 uint8_t *spirv;1035 uint8_t *spirv;
@@ -1043,25 +1088,24 @@ static const struct descriptor_props { · 52 unmodified lines
1043 [VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER] = { sizeof(VkBufferView), "imageBuffer", 1, 0, 0, 0, },1088 [VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER] = { sizeof(VkBufferView), "imageBuffer", 1, 0, 0, 0, },
1044};1089};
1045 1090
1046int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,1091int ff_vk_add_descriptor_set(AVFilterContext *avctx, FFVulkanPipeline *pl,
1047 FFSPIRVShader *shd, VulkanDescriptorSetBinding *desc,1092 FFSPIRVShader *shd, FFVulkanDescriptorSetBinding *desc,
1048 int num, int only_print_to_shader)1093 int num, int only_print_to_shader)
1049{1094{
1050 VkResult ret;1095 VkResult ret;
1051 VkDescriptorSetLayout *layout;1096 VkDescriptorSetLayout *layout;
1052 VulkanFilterContext *s = avctx->priv;1097 FFVulkanContext *s = avctx->priv;
1053 FFVulkanFunctions *vk = &s->vkfn;1098 FFVulkanFunctions *vk = &s->vkfn;
1054 1099
1055 if (only_print_to_shader)1100 if (only_print_to_shader)
1056 goto print;1101 goto print;
1057 1102
1058 pl->desc_layout = av_realloc_array(pl->desc_layout, sizeof(*pl->desc_layout),1103 pl->desc_layout = av_realloc_array(pl->desc_layout, sizeof(*pl->desc_layout),
1059 pl->desc_layout_num + 1);1104 pl->desc_layout_num + pl->qf->nb_queues);
1060 if (!pl->desc_layout)1105 if (!pl->desc_layout)
1061 return AVERROR(ENOMEM);1106 return AVERROR(ENOMEM);
1062 1107
1063 layout = &pl->desc_layout[pl->desc_layout_num];1108 layout = &pl->desc_layout[pl->desc_layout_num];
1064 memset(layout, 0, sizeof(*layout));
1065 1109
1066 { /* Create descriptor set layout descriptions */1110 { /* Create descriptor set layout descriptions */
1067 VkDescriptorSetLayoutCreateInfo desc_create_layout = { 0 };1111 VkDescriptorSetLayoutCreateInfo desc_create_layout = { 0 };
@@ -1076,21 +1120,27 @@ int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl, · 8 unmodified lines
1076 desc_binding[i].descriptorType = desc[i].type;1120 desc_binding[i].descriptorType = desc[i].type;
1077 desc_binding[i].descriptorCount = FFMAX(desc[i].elems, 1);1121 desc_binding[i].descriptorCount = FFMAX(desc[i].elems, 1);
1078 desc_binding[i].stageFlags = desc[i].stages;1122 desc_binding[i].stageFlags = desc[i].stages;
1079 desc_binding[i].pImmutableSamplers = desc[i].samplers;1123 desc_binding[i].pImmutableSamplers = desc[i].sampler ?
1124 desc[i].sampler->sampler :
1125 NULL;
1080 }1126 }
1081 1127
1082 desc_create_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;1128 desc_create_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
1083 desc_create_layout.pBindings = desc_binding;1129 desc_create_layout.pBindings = desc_binding;
1084 desc_create_layout.bindingCount = num;1130 desc_create_layout.bindingCount = num;
1085 1131
1086 ret = vk->CreateDescriptorSetLayout(s->hwctx->act_dev, &desc_create_layout,1132 for (int i = 0; i < pl->qf->nb_queues; i++) {
1087 s->hwctx->alloc, layout);1133 ret = vk->CreateDescriptorSetLayout(s->hwctx->act_dev, &desc_create_layout,
1088 av_free(desc_binding);1134 s->hwctx->alloc, &layout[i]);
1089 if (ret != VK_SUCCESS) {1135 if (ret != VK_SUCCESS) {
1090 av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor set "1136 av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor set "
1091 "layout: %s\n", ff_vk_ret2str(ret));1137 "layout: %s\n", ff_vk_ret2str(ret));
1092 return AVERROR_EXTERNAL;1138 av_free(desc_binding);
1139 return AVERROR_EXTERNAL;
1140 }
1093 }1141 }
1142
1143 av_free(desc_binding);
1094 }1144 }
1095 1145
1096 { /* Pool each descriptor by type and update pool counts */1146 { /* Pool each descriptor by type and update pool counts */
@@ -1108,7 +1158,7 @@ int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl, · 11 unmodified lines
1108 memset(&pl->pool_size_desc[j], 0, sizeof(VkDescriptorPoolSize));1158 memset(&pl->pool_size_desc[j], 0, sizeof(VkDescriptorPoolSize));
1109 }1159 }
1110 pl->pool_size_desc[j].type = desc[i].type;1160 pl->pool_size_desc[j].type = desc[i].type;
1111 pl->pool_size_desc[j].descriptorCount += FFMAX(desc[i].elems, 1);1161 pl->pool_size_desc[j].descriptorCount += FFMAX(desc[i].elems, 1)*pl->qf->nb_queues;
1112 }1162 }
1113 }1163 }
1114 1164
@@ -1132,27 +1182,32 @@ int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl, · 17 unmodified lines
1132 1182
1133 pl->desc_template_info = av_realloc_array(pl->desc_template_info,1183 pl->desc_template_info = av_realloc_array(pl->desc_template_info,
1134 sizeof(*pl->desc_template_info),1184 sizeof(*pl->desc_template_info),
1135 pl->desc_layout_num + 1);1185 pl->total_descriptor_sets + pl->qf->nb_queues);
1136 if (!pl->desc_template_info)1186 if (!pl->desc_template_info)
1137 return AVERROR(ENOMEM);1187 return AVERROR(ENOMEM);
1138 1188
1139 dt = &pl->desc_template_info[pl->desc_layout_num];1189 dt = &pl->desc_template_info[pl->total_descriptor_sets];
1140 memset(dt, 0, sizeof(*dt));1190 memset(dt, 0, sizeof(*dt)*pl->qf->nb_queues);
1141 1191
1142 dt->sType = VK_STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO;1192 for (int i = 0; i < pl->qf->nb_queues; i++) {
1143 dt->templateType = VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET;1193 dt[i].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO;
1144 dt->descriptorSetLayout = *layout;1194 dt[i].templateType = VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET;
1145 dt->pDescriptorUpdateEntries = des_entries;1195 dt[i].descriptorSetLayout = layout[i];
1146 dt->descriptorUpdateEntryCount = num;1196 dt[i].pDescriptorUpdateEntries = des_entries;
1197 dt[i].descriptorUpdateEntryCount = num;
1198 }
1147 }1199 }
1148 1200
1149 pl->desc_layout_num++;1201 pl->descriptor_sets_num++;
1202
1203 pl->desc_layout_num += pl->qf->nb_queues;
1204 pl->total_descriptor_sets += pl->qf->nb_queues;
1150 1205
1151print:1206print:
1152 /* Write shader info */1207 /* Write shader info */
1153 for (int i = 0; i < num; i++) {1208 for (int i = 0; i < num; i++) {
1154 const struct descriptor_props *prop = &descriptor_props[desc[i].type];1209 const struct descriptor_props *prop = &descriptor_props[desc[i].type];
1155 GLSLA("layout (set = %i, binding = %i", pl->desc_layout_num - 1, i);1210 GLSLA("layout (set = %i, binding = %i", pl->descriptor_sets_num - 1, i);
1156 1211
1157 if (desc[i].mem_layout)1212 if (desc[i].mem_layout)
1158 GLSLA(", %s", desc[i].mem_layout);1213 GLSLA(", %s", desc[i].mem_layout);
@@ -1184,12 +1239,14 @@ print: · 25 unmodified lines
1184 return 0;1239 return 0;
1185}1240}
1186 1241
1187void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,1242void ff_vk_update_descriptor_set(AVFilterContext *avctx, FFVulkanPipeline *pl,
1188 int set_id)1243 int set_id)
1189{1244{
1190 VulkanFilterContext *s = avctx->priv;1245 FFVulkanContext *s = avctx->priv;
1191 FFVulkanFunctions *vk = &s->vkfn;1246 FFVulkanFunctions *vk = &s->vkfn;
1192 1247
1248 set_id = set_id*pl->qf->nb_queues + pl->qf->cur_queue;
1249
1193 vk->UpdateDescriptorSetWithTemplate(s->hwctx->act_dev,1250 vk->UpdateDescriptorSetWithTemplate(s->hwctx->act_dev,
1194 pl->desc_set[set_id],1251 pl->desc_set[set_id],
1195 pl->desc_template[set_id],1252 pl->desc_template[set_id],
@@ -1200,27 +1257,29 @@ void ff_vk_update_push_exec(AVFilterContext *avctx, FFVkExecContext *e, · 4 unmodified lines
1200 VkShaderStageFlagBits stage, int offset,1257 VkShaderStageFlagBits stage, int offset,
1201 size_t size, void *src)1258 size_t size, void *src)
1202{1259{
1203 VulkanFilterContext *s = avctx->priv;1260 FFVulkanContext *s = avctx->priv;
1204 FFVulkanFunctions *vk = &s->vkfn;1261 FFVulkanFunctions *vk = &s->vkfn;
1205 1262
1206 vk->CmdPushConstants(e->bufs[s->cur_queue_idx], e->bound_pl->pipeline_layout,1263 vk->CmdPushConstants(e->bufs[e->qf->cur_queue], e->bound_pl->pipeline_layout,
1207 stage, offset, size, src);1264 stage, offset, size, src);
1208}1265}
1209 1266
1210int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl)1267int ff_vk_init_pipeline_layout(AVFilterContext *avctx, FFVulkanPipeline *pl)
1211{1268{
1212 VkResult ret;1269 VkResult ret;
1213 VulkanFilterContext *s = avctx->priv;1270 FFVulkanContext *s = avctx->priv;
1214 FFVulkanFunctions *vk = &s->vkfn;1271 FFVulkanFunctions *vk = &s->vkfn;
1215 1272
1216 pl->descriptor_sets_num = pl->desc_layout_num * s->queue_count;1273 pl->desc_staging = av_malloc(pl->descriptor_sets_num*sizeof(*pl->desc_staging));
1274 if (!pl->desc_staging)
1275 return AVERROR(ENOMEM);
1217 1276
1218 { /* Init descriptor set pool */1277 { /* Init descriptor set pool */
1219 VkDescriptorPoolCreateInfo pool_create_info = {1278 VkDescriptorPoolCreateInfo pool_create_info = {
1220 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,1279 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
1221 .poolSizeCount = pl->pool_size_desc_num,1280 .poolSizeCount = pl->pool_size_desc_num,
1222 .pPoolSizes = pl->pool_size_desc,1281 .pPoolSizes = pl->pool_size_desc,
1223 .maxSets = pl->descriptor_sets_num,1282 .maxSets = pl->total_descriptor_sets,
1224 };1283 };
1225 1284
1226 ret = vk->CreateDescriptorPool(s->hwctx->act_dev, &pool_create_info,1285 ret = vk->CreateDescriptorPool(s->hwctx->act_dev, &pool_create_info,
@@ -1237,11 +1296,11 @@ int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl) · 10 unmodified lines
1237 VkDescriptorSetAllocateInfo alloc_info = {1296 VkDescriptorSetAllocateInfo alloc_info = {
1238 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,1297 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1239 .descriptorPool = pl->desc_pool,1298 .descriptorPool = pl->desc_pool,
1240 .descriptorSetCount = pl->descriptor_sets_num,1299 .descriptorSetCount = pl->total_descriptor_sets,
1241 .pSetLayouts = pl->desc_layout,1300 .pSetLayouts = pl->desc_layout,
1242 };1301 };
1243 1302
1244 pl->desc_set = av_malloc(pl->descriptor_sets_num*sizeof(*pl->desc_set));1303 pl->desc_set = av_malloc(pl->total_descriptor_sets*sizeof(*pl->desc_set));
1245 if (!pl->desc_set)1304 if (!pl->desc_set)
1246 return AVERROR(ENOMEM);1305 return AVERROR(ENOMEM);
1247 1306
@@ -1257,12 +1316,14 @@ int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl) · 9 unmodified lines
1257 { /* Finally create the pipeline layout */1316 { /* Finally create the pipeline layout */
1258 VkPipelineLayoutCreateInfo spawn_pipeline_layout = {1317 VkPipelineLayoutCreateInfo spawn_pipeline_layout = {
1259 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,1318 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1260 .setLayoutCount = pl->desc_layout_num,1319 .pSetLayouts = (VkDescriptorSetLayout *)pl->desc_staging,
1261 .pSetLayouts = pl->desc_layout,
1262 .pushConstantRangeCount = pl->push_consts_num,1320 .pushConstantRangeCount = pl->push_consts_num,
1263 .pPushConstantRanges = pl->push_consts,1321 .pPushConstantRanges = pl->push_consts,
1264 };1322 };
1265 1323
1324 for (int i = 0; i < pl->total_descriptor_sets; i += pl->qf->nb_queues)
1325 pl->desc_staging[spawn_pipeline_layout.setLayoutCount++] = pl->desc_layout[i];
1326
1266 ret = vk->CreatePipelineLayout(s->hwctx->act_dev, &spawn_pipeline_layout,1327 ret = vk->CreatePipelineLayout(s->hwctx->act_dev, &spawn_pipeline_layout,
1267 s->hwctx->alloc, &pl->pipeline_layout);1328 s->hwctx->alloc, &pl->pipeline_layout);
1268 av_freep(&pl->push_consts);1329 av_freep(&pl->push_consts);
@@ -1275,21 +1336,19 @@ int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl) · 6 unmodified lines
1275 }1336 }
1276 1337
1277 { /* Descriptor template (for tightly packed descriptors) */1338 { /* Descriptor template (for tightly packed descriptors) */
1278 VkDescriptorUpdateTemplateCreateInfo *desc_template_info;1339 VkDescriptorUpdateTemplateCreateInfo *dt;
1279 1340
1280 pl->desc_template = av_malloc(pl->descriptor_sets_num*sizeof(*pl->desc_template));1341 pl->desc_template = av_malloc(pl->total_descriptor_sets*sizeof(*pl->desc_template));
1281 if (!pl->desc_template)1342 if (!pl->desc_template)
1282 return AVERROR(ENOMEM);1343 return AVERROR(ENOMEM);
1283 1344
1284 /* Create update templates for the descriptor sets */1345 /* Create update templates for the descriptor sets */
1285 for (int i = 0; i < pl->descriptor_sets_num; i++) {1346 for (int i = 0; i < pl->total_descriptor_sets; i++) {
1286 desc_template_info = &pl->desc_template_info[i % pl->desc_layout_num];1347 dt = &pl->desc_template_info[i];
1287 desc_template_info->pipelineLayout = pl->pipeline_layout;1348 dt->pipelineLayout = pl->pipeline_layout;
1288 ret = vk->CreateDescriptorUpdateTemplate(s->hwctx->act_dev,1349 ret = vk->CreateDescriptorUpdateTemplate(s->hwctx->act_dev,
1289 desc_template_info,1350 dt, s->hwctx->alloc,
1290 s->hwctx->alloc,
1291 &pl->desc_template[i]);1351 &pl->desc_template[i]);
1292 av_free((void *)desc_template_info->pDescriptorUpdateEntries);
1293 if (ret != VK_SUCCESS) {1352 if (ret != VK_SUCCESS) {
1294 av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor "1353 av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor "
1295 "template: %s\n", ff_vk_ret2str(ret));1354 "template: %s\n", ff_vk_ret2str(ret));
@@ -1297,23 +1356,34 @@ int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl) · 1 unmodified line
1297 }1356 }
1298 }1357 }
1299 1358
1359 /* Free the duplicated memory used for the template entries */
1360 for (int i = 0; i < pl->total_descriptor_sets; i += pl->qf->nb_queues) {
1361 dt = &pl->desc_template_info[i];
1362 av_free((void *)dt->pDescriptorUpdateEntries);
1363 }
1364
1300 av_freep(&pl->desc_template_info);1365 av_freep(&pl->desc_template_info);
1301 }1366 }
1302 1367
1303 return 0;1368 return 0;
1304}1369}
1305 1370
1306FN_CREATING(VulkanFilterContext, VulkanPipeline, pipeline, pipelines, pipelines_num)1371FN_CREATING(FFVulkanContext, FFVulkanPipeline, pipeline, pipelines, pipelines_num)
1307VulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx)1372FFVulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx,
1373 FFVkQueueFamilyCtx *qf)
1308{1374{
1309 return create_pipeline(avctx->priv);1375 FFVulkanPipeline *pl = create_pipeline(avctx->priv);
1376 if (pl)
1377 pl->qf = qf;
1378
1379 return pl;
1310}1380}
1311 1381
1312int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl)1382int ff_vk_init_compute_pipeline(AVFilterContext *avctx, FFVulkanPipeline *pl)
1313{1383{
1314 int i;1384 int i;
1315 VkResult ret;1385 VkResult ret;
1316 VulkanFilterContext *s = avctx->priv;1386 FFVulkanContext *s = avctx->priv;
1317 FFVulkanFunctions *vk = &s->vkfn;1387 FFVulkanFunctions *vk = &s->vkfn;
1318 1388
1319 VkComputePipelineCreateInfo pipe = {1389 VkComputePipelineCreateInfo pipe = {
@@ -1346,26 +1416,31 @@ int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl) · 26 unmodified lines
1346}1416}
1347 1417
1348void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,1418void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,
1349 VulkanPipeline *pl)1419 FFVulkanPipeline *pl)
1350{1420{
1351 VulkanFilterContext *s = avctx->priv;1421 FFVulkanContext *s = avctx->priv;
1352 FFVulkanFunctions *vk = &s->vkfn;1422 FFVulkanFunctions *vk = &s->vkfn;
1353 1423
1354 vk->CmdBindPipeline(e->bufs[s->cur_queue_idx], pl->bind_point, pl->pipeline);1424 vk->CmdBindPipeline(e->bufs[e->qf->cur_queue], pl->bind_point, pl->pipeline);
1355 1425
1356 vk->CmdBindDescriptorSets(e->bufs[s->cur_queue_idx], pl->bind_point,1426 for (int i = 0; i < pl->descriptor_sets_num; i++)
1357 pl->pipeline_layout, 0, pl->descriptor_sets_num,1427 pl->desc_staging[i] = pl->desc_set[i*pl->qf->nb_queues + pl->qf->cur_queue];
1358 pl->desc_set, 0, 0);1428
1429 vk->CmdBindDescriptorSets(e->bufs[e->qf->cur_queue], pl->bind_point,
1430 pl->pipeline_layout, 0,
1431 pl->descriptor_sets_num,
1432 (VkDescriptorSet *)pl->desc_staging,
1433 0, NULL);
1359 1434
1360 e->bound_pl = pl;1435 e->bound_pl = pl;
1361}1436}
1362 1437
1363static void free_exec_ctx(VulkanFilterContext *s, FFVkExecContext *e)1438static void free_exec_ctx(FFVulkanContext *s, FFVkExecContext *e)
1364{1439{
1365 FFVulkanFunctions *vk = &s->vkfn;1440 FFVulkanFunctions *vk = &s->vkfn;
1366 1441
1367 /* Make sure all queues have finished executing */1442 /* Make sure all queues have finished executing */
1368 for (int i = 0; i < s->queue_count; i++) {1443 for (int i = 0; i < e->qf->nb_queues; i++) {
1369 FFVkQueueCtx *q = &e->queues[i];1444 FFVkQueueCtx *q = &e->queues[i];
1370 1445
1371 if (q->fence) {1446 if (q->fence) {
@@ -1389,7 +1464,7 @@ static void free_exec_ctx(VulkanFilterContext *s, FFVkExecContext *e) · 17 unmodified lines
1389 }1464 }
1390 1465
1391 if (e->bufs)1466 if (e->bufs)
1392 vk->FreeCommandBuffers(s->hwctx->act_dev, e->pool, s->queue_count, e->bufs);1467 vk->FreeCommandBuffers(s->hwctx->act_dev, e->pool, e->qf->nb_queues, e->bufs);
1393 if (e->pool)1468 if (e->pool)
1394 vk->DestroyCommandPool(s->hwctx->act_dev, e->pool, s->hwctx->alloc);1469 vk->DestroyCommandPool(s->hwctx->act_dev, e->pool, s->hwctx->alloc);
1395 1470
@@ -1397,13 +1472,14 @@ static void free_exec_ctx(VulkanFilterContext *s, FFVkExecContext *e) · 1 unmodified line
1397 av_freep(&e->queues);1472 av_freep(&e->queues);
1398 av_freep(&e->sem_sig);1473 av_freep(&e->sem_sig);
1399 av_freep(&e->sem_sig_val);1474 av_freep(&e->sem_sig_val);
1475 av_freep(&e->sem_sig_val_dst);
1400 av_freep(&e->sem_wait);1476 av_freep(&e->sem_wait);
1401 av_freep(&e->sem_wait_dst);1477 av_freep(&e->sem_wait_dst);
1402 av_freep(&e->sem_wait_val);1478 av_freep(&e->sem_wait_val);
1403 av_free(e);1479 av_free(e);
1404}1480}
1405 1481
1406static void free_pipeline(VulkanFilterContext *s, VulkanPipeline *pl)1482static void free_pipeline(FFVulkanContext *s, FFVulkanPipeline *pl)
1407{1483{
1408 FFVulkanFunctions *vk = &s->vkfn;1484 FFVulkanFunctions *vk = &s->vkfn;
1409 1485
@@ -1433,6 +1509,7 @@ static void free_pipeline(VulkanFilterContext *s, VulkanPipeline *pl) · 23 unmodified lines
1433 vk->DestroyDescriptorPool(s->hwctx->act_dev, pl->desc_pool,1509 vk->DestroyDescriptorPool(s->hwctx->act_dev, pl->desc_pool,
1434 s->hwctx->alloc);1510 s->hwctx->alloc);
1435 1511
1512 av_freep(&pl->desc_staging);
1436 av_freep(&pl->desc_set);1513 av_freep(&pl->desc_set);
1437 av_freep(&pl->shaders);1514 av_freep(&pl->shaders);
1438 av_freep(&pl->desc_layout);1515 av_freep(&pl->desc_layout);
@@ -1443,8 +1520,10 @@ static void free_pipeline(VulkanFilterContext *s, VulkanPipeline *pl) · 4 unmodified lines
1443 /* Only freed in case of failure */1520 /* Only freed in case of failure */
1444 av_freep(&pl->pool_size_desc);1521 av_freep(&pl->pool_size_desc);
1445 if (pl->desc_template_info) {1522 if (pl->desc_template_info) {
1446 for (int i = 0; i < pl->descriptor_sets_num; i++)1523 for (int i = 0; i < pl->total_descriptor_sets; i += pl->qf->nb_queues) {
1447 av_free((void *)pl->desc_template_info[i].pDescriptorUpdateEntries);1524 VkDescriptorUpdateTemplateCreateInfo *dt = &pl->desc_template_info[i];
1525 av_free((void *)dt->pDescriptorUpdateEntries);
1526 }
1448 av_freep(&pl->desc_template_info);1527 av_freep(&pl->desc_template_info);
1449 }1528 }
1450 1529
@@ -1453,7 +1532,7 @@ static void free_pipeline(VulkanFilterContext *s, VulkanPipeline *pl) · 2 unmodified lines
1453 1532
1454void ff_vk_filter_uninit(AVFilterContext *avctx)1533void ff_vk_filter_uninit(AVFilterContext *avctx)
1455{1534{
1456 VulkanFilterContext *s = avctx->priv;1535 FFVulkanContext *s = avctx->priv;
1457 FFVulkanFunctions *vk = &s->vkfn;1536 FFVulkanFunctions *vk = &s->vkfn;
1458 1537
1459 ff_vk_glslang_uninit();1538 ff_vk_glslang_uninit();
@@ -1463,7 +1542,8 @@ void ff_vk_filter_uninit(AVFilterContext *avctx) · 3 unmodified lines
1463 av_freep(&s->exec_ctx);1542 av_freep(&s->exec_ctx);
1464 1543
1465 for (int i = 0; i < s->samplers_num; i++) {1544 for (int i = 0; i < s->samplers_num; i++) {
1466 vk->DestroySampler(s->hwctx->act_dev, *s->samplers[i], s->hwctx->alloc);1545 vk->DestroySampler(s->hwctx->act_dev, s->samplers[i]->sampler[0],
1546 s->hwctx->alloc);
1467 av_free(s->samplers[i]);1547 av_free(s->samplers[i]);
1468 }1548 }
1469 av_freep(&s->samplers);1549 av_freep(&s->samplers);
libavfilter/vulkan.h+66−37
@@ -20,6 +20,7 @@ · 19 unmodified lines
20#define AVFILTER_VULKAN_H20#define AVFILTER_VULKAN_H
21 21
22#define VK_NO_PROTOTYPES22#define VK_NO_PROTOTYPES
23#define VK_ENABLE_BETA_EXTENSIONS
23 24
24#include "avfilter.h"25#include "avfilter.h"
25#include "libavutil/pixdesc.h"26#include "libavutil/pixdesc.h"
@@ -52,9 +53,6 @@ · 26 unmodified lines
52 goto fail; \53 goto fail; \
53 } while (0)54 } while (0)
54 55
55/* Useful for attaching immutable samplers to arrays */
56#define DUP_SAMPLER_ARRAY4(x) (VkSampler []){ x, x, x, x, }
57
58typedef struct FFSPIRVShader {56typedef struct FFSPIRVShader {
59 const char *name; /* Name for id/debugging purposes */57 const char *name; /* Name for id/debugging purposes */
60 AVBPrint src;58 AVBPrint src;
@@ -62,7 +60,11 @@ typedef struct FFSPIRVShader { · 1 unmodified line
62 VkPipelineShaderStageCreateInfo shader;60 VkPipelineShaderStageCreateInfo shader;
63} FFSPIRVShader;61} FFSPIRVShader;
64 62
65typedef struct VulkanDescriptorSetBinding {63typedef struct FFVkSampler {
64 VkSampler sampler[4];
65} FFVkSampler;
66
67typedef struct FFVulkanDescriptorSetBinding {
66 const char *name;68 const char *name;
67 VkDescriptorType type;69 VkDescriptorType type;
68 const char *mem_layout; /* Storage images (rgba8, etc.) and buffers (std430, etc.) */70 const char *mem_layout; /* Storage images (rgba8, etc.) and buffers (std430, etc.) */
@@ -71,9 +73,9 @@ typedef struct VulkanDescriptorSetBinding { · 2 unmodified lines
71 uint32_t dimensions; /* Needed for e.g. sampler%iD */73 uint32_t dimensions; /* Needed for e.g. sampler%iD */
72 uint32_t elems; /* 0 - scalar, 1 or more - vector */74 uint32_t elems; /* 0 - scalar, 1 or more - vector */
73 VkShaderStageFlags stages;75 VkShaderStageFlags stages;
74 const VkSampler *samplers; /* Immutable samplers, length - #elems */76 FFVkSampler *sampler; /* Sampler to use for all elems */
75 void *updater; /* Pointer to VkDescriptor*Info */77 void *updater; /* Pointer to VkDescriptor*Info */
76} VulkanDescriptorSetBinding;78} FFVulkanDescriptorSetBinding;
77 79
78typedef struct FFVkBuffer {80typedef struct FFVkBuffer {
79 VkBuffer buf;81 VkBuffer buf;
@@ -81,7 +83,15 @@ typedef struct FFVkBuffer { · 1 unmodified line
81 VkMemoryPropertyFlagBits flags;83 VkMemoryPropertyFlagBits flags;
82} FFVkBuffer;84} FFVkBuffer;
83 85
84typedef struct VulkanPipeline {86typedef struct FFVkQueueFamilyCtx {
87 int queue_family;
88 int nb_queues;
89 int cur_queue;
90} FFVkQueueFamilyCtx;
91
92typedef struct FFVulkanPipeline {
93 FFVkQueueFamilyCtx *qf;
94
85 VkPipelineBindPoint bind_point;95 VkPipelineBindPoint bind_point;
86 96
87 /* Contexts */97 /* Contexts */
@@ -97,18 +107,21 @@ typedef struct VulkanPipeline { · 9 unmodified lines
97 int push_consts_num;107 int push_consts_num;
98 108
99 /* Descriptors */109 /* Descriptors */
100 VkDescriptorSetLayout *desc_layout;110 VkDescriptorSetLayout *desc_layout;
101 VkDescriptorPool desc_pool;111 VkDescriptorPool desc_pool;
102 VkDescriptorSet *desc_set;112 VkDescriptorSet *desc_set;
103 VkDescriptorUpdateTemplate *desc_template;113 void **desc_staging;
104 int desc_layout_num;114 VkDescriptorSetLayoutBinding **desc_binding;
105 int descriptor_sets_num;115 VkDescriptorUpdateTemplate *desc_template;
106 int pool_size_desc_num;116 int desc_layout_num;
117 int descriptor_sets_num;
118 int total_descriptor_sets;
119 int pool_size_desc_num;
107 120
108 /* Temporary, used to store data in between initialization stages */121 /* Temporary, used to store data in between initialization stages */
109 VkDescriptorUpdateTemplateCreateInfo *desc_template_info;122 VkDescriptorUpdateTemplateCreateInfo *desc_template_info;
110 VkDescriptorPoolSize *pool_size_desc;123 VkDescriptorPoolSize *pool_size_desc;
111} VulkanPipeline;124} FFVulkanPipeline;
112 125
113typedef struct FFVkQueueCtx {126typedef struct FFVkQueueCtx {
114 VkFence fence;127 VkFence fence;
@@ -126,6 +139,8 @@ typedef struct FFVkQueueCtx { · 11 unmodified lines
126} FFVkQueueCtx;139} FFVkQueueCtx;
127 140
128typedef struct FFVkExecContext {141typedef struct FFVkExecContext {
142 FFVkQueueFamilyCtx *qf;
143
129 VkCommandPool pool;144 VkCommandPool pool;
130 VkCommandBuffer *bufs;145 VkCommandBuffer *bufs;
131 FFVkQueueCtx *queues;146 FFVkQueueCtx *queues;
@@ -134,7 +149,7 @@ typedef struct FFVkExecContext { · 2 unmodified lines
134 int *nb_deps;149 int *nb_deps;
135 int *dep_alloc_size;150 int *dep_alloc_size;
136 151
137 VulkanPipeline *bound_pl;152 FFVulkanPipeline *bound_pl;
138 153
139 VkSemaphore *sem_wait;154 VkSemaphore *sem_wait;
140 int sem_wait_alloc; /* Allocated sem_wait */155 int sem_wait_alloc; /* Allocated sem_wait */
@@ -152,23 +167,23 @@ typedef struct FFVkExecContext { · 11 unmodified lines
152 167
153 uint64_t *sem_sig_val;168 uint64_t *sem_sig_val;
154 int sem_sig_val_alloc;169 int sem_sig_val_alloc;
170
171 uint64_t **sem_sig_val_dst;
172 int sem_sig_val_dst_alloc;
155} FFVkExecContext;173} FFVkExecContext;
156 174
157typedef struct VulkanFilterContext {175typedef struct FFVulkanContext {
158 const AVClass *class;176 const AVClass *class;
159 FFVulkanFunctions vkfn;177 FFVulkanFunctions vkfn;
160 FFVulkanExtensions extensions;178 FFVulkanExtensions extensions;
179 VkPhysicalDeviceProperties props;
180 VkPhysicalDeviceMemoryProperties mprops;
161 181
162 AVBufferRef *device_ref;182 AVBufferRef *device_ref;
163 AVBufferRef *frames_ref; /* For in-place filtering */183 AVBufferRef *frames_ref; /* For in-place filtering */
164 AVHWDeviceContext *device;184 AVHWDeviceContext *device;
165 AVVulkanDeviceContext *hwctx;185 AVVulkanDeviceContext *hwctx;
166 186
167 /* State - mirrored with the exec ctx */
168 int cur_queue_idx;
169 int queue_family_idx;
170 int queue_count;
171
172 /* Properties */187 /* Properties */
173 int output_width;188 int output_width;
174 int output_height;189 int output_height;
@@ -176,7 +191,7 @@ typedef struct VulkanFilterContext { · 1 unmodified line
176 enum AVPixelFormat input_format;191 enum AVPixelFormat input_format;
177 192
178 /* Samplers */193 /* Samplers */
179 VkSampler **samplers;194 FFVkSampler **samplers;
180 int samplers_num;195 int samplers_num;
181 196
182 /* Exec contexts */197 /* Exec contexts */
@@ -184,12 +199,12 @@ typedef struct VulkanFilterContext { · 1 unmodified line
184 int exec_ctx_num;199 int exec_ctx_num;
185 200
186 /* Pipelines (each can have 1 shader of each type) */201 /* Pipelines (each can have 1 shader of each type) */
187 VulkanPipeline **pipelines;202 FFVulkanPipeline **pipelines;
188 int pipelines_num;203 int pipelines_num;
189 204
190 void *scratch; /* Scratch memory used only in functions */205 void *scratch; /* Scratch memory used only in functions */
191 unsigned int scratch_size;206 unsigned int scratch_size;
192} VulkanFilterContext;207} FFVulkanContext;
193 208
194/* Identity mapping - r = r, b = b, g = g, a = a */209/* Identity mapping - r = r, b = b, g = g, a = a */
195extern const VkComponentMapping ff_comp_identity_map;210extern const VkComponentMapping ff_comp_identity_map;
@@ -218,11 +233,23 @@ int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt); · 22 unmodified lines
218 */233 */
219const char *ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt);234const char *ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt);
220 235
236/**
237 * Initialize a queue family.
238 * A queue limit of 0 means no limit.
239 */
240void ff_vk_qf_init(AVFilterContext *avctx, FFVkQueueFamilyCtx *qf,
241 VkQueueFlagBits dev_family, int queue_limit);
242
243/**
244 * Rotate through the queues in a queue family.
245 */
246void ff_vk_qf_rotate(FFVkQueueFamilyCtx *qf);
247
221/**248/**
222 * Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()249 * Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
223 */250 */
224VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,251FFVkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,
225 VkFilter filt);252 VkFilter filt);
226 253
227/**254/**
228 * Create an imageview.255 * Create an imageview.
@@ -237,19 +264,20 @@ int ff_vk_create_imageview(AVFilterContext *avctx, FFVkExecContext *e, · 8 unmodified lines
237 * Define a push constant for a given stage into a pipeline.264 * Define a push constant for a given stage into a pipeline.
238 * Must be called before the pipeline layout has been initialized.265 * Must be called before the pipeline layout has been initialized.
239 */266 */
240int ff_vk_add_push_constant(AVFilterContext *avctx, VulkanPipeline *pl,267int ff_vk_add_push_constant(AVFilterContext *avctx, FFVulkanPipeline *pl,
241 int offset, int size, VkShaderStageFlagBits stage);268 int offset, int size, VkShaderStageFlagBits stage);
242 269
243/**270/**
244 * Inits a pipeline. Everything in it will be auto-freed when calling271 * Inits a pipeline. Everything in it will be auto-freed when calling
245 * ff_vk_filter_uninit().272 * ff_vk_filter_uninit().
246 */273 */
247VulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx);274FFVulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx,
275 FFVkQueueFamilyCtx *qf);
248 276
249/**277/**
250 * Inits a shader for a specific pipeline. Will be auto-freed on uninit.278 * Inits a shader for a specific pipeline. Will be auto-freed on uninit.
251 */279 */
252FFSPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, VulkanPipeline *pl,280FFSPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, FFVulkanPipeline *pl,
253 const char *name, VkShaderStageFlags stage);281 const char *name, VkShaderStageFlags stage);
254 282
255/**283/**
@@ -261,8 +289,8 @@ void ff_vk_set_compute_shader_sizes(AVFilterContext *avctx, FFSPIRVShader *shd, · 5 unmodified lines
261/**289/**
262 * Adds a descriptor set to the shader and registers them in the pipeline.290 * Adds a descriptor set to the shader and registers them in the pipeline.
263 */291 */
264int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,292int ff_vk_add_descriptor_set(AVFilterContext *avctx, FFVulkanPipeline *pl,
265 FFSPIRVShader *shd, VulkanDescriptorSetBinding *desc,293 FFSPIRVShader *shd, FFVulkanDescriptorSetBinding *desc,
266 int num, int only_print_to_shader);294 int num, int only_print_to_shader);
267 295
268/**296/**
@@ -280,27 +308,28 @@ void ff_vk_print_shader(AVFilterContext *avctx, FFSPIRVShader *shd, int prio); · 11 unmodified lines
280 * Initializes the pipeline layout after all shaders and descriptor sets have308 * Initializes the pipeline layout after all shaders and descriptor sets have
281 * been finished.309 * been finished.
282 */310 */
283int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl);311int ff_vk_init_pipeline_layout(AVFilterContext *avctx, FFVulkanPipeline *pl);
284 312
285/**313/**
286 * Initializes a compute pipeline. Will pick the first shader with the314 * Initializes a compute pipeline. Will pick the first shader with the
287 * COMPUTE flag set.315 * COMPUTE flag set.
288 */316 */
289int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl);317int ff_vk_init_compute_pipeline(AVFilterContext *avctx, FFVulkanPipeline *pl);
290 318
291/**319/**
292 * Updates a descriptor set via the updaters defined.320 * Updates a descriptor set via the updaters defined.
293 * Can be called immediately after pipeline creation, but must be called321 * Can be called immediately after pipeline creation, but must be called
294 * at least once before queue submission.322 * at least once before queue submission.
295 */323 */
296void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,324void ff_vk_update_descriptor_set(AVFilterContext *avctx, FFVulkanPipeline *pl,
297 int set_id);325 int set_id);
298 326
299/**327/**
300 * Init an execution context for command recording and queue submission.328 * Init an execution context for command recording and queue submission.
301 * WIll be auto-freed on uninit.329 * WIll be auto-freed on uninit.
302 */330 */
303int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx);331int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx,
332 FFVkQueueFamilyCtx *qf);
304 333
305/**334/**
306 * Begin recording to the command buffer. Previous execution must have been335 * Begin recording to the command buffer. Previous execution must have been
@@ -313,7 +342,7 @@ int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e); · 6 unmodified lines
313 * Must be called after ff_vk_start_exec_recording() and before submission.342 * Must be called after ff_vk_start_exec_recording() and before submission.
314 */343 */
315void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,344void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,
316 VulkanPipeline *pl);345 FFVulkanPipeline *pl);
317 346
318/**347/**
319 * Updates push constants.348 * Updates push constants.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t