1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "formats.h"
20 #include "vulkan.h"
21 #include "glslang.h"
22 
23 /* Generic macro for creating contexts which need to keep their addresses
24  * if another context is created. */
25 #define FN_CREATING(ctx, type, shortname, array, num)                          \
26 static av_always_inline type *create_ ##shortname(ctx *dctx)                   \
27 {                                                                              \
28     type **array, *sctx = av_mallocz(sizeof(*sctx));                           \
29     if (!sctx)                                                                 \
30         return NULL;                                                           \
31                                                                                \
32     array = av_realloc_array(dctx->array, sizeof(*dctx->array), dctx->num + 1);\
33     if (!array) {                                                              \
34         av_free(sctx);                                                         \
35         return NULL;                                                           \
36     }                                                                          \
37                                                                                \
38     dctx->array = array;                                                       \
39     dctx->array[dctx->num++] = sctx;                                           \
40                                                                                \
41     return sctx;                                                               \
42 }
43 
44 const VkComponentMapping ff_comp_identity_map = {
45     .r = VK_COMPONENT_SWIZZLE_IDENTITY,
46     .g = VK_COMPONENT_SWIZZLE_IDENTITY,
47     .b = VK_COMPONENT_SWIZZLE_IDENTITY,
48     .a = VK_COMPONENT_SWIZZLE_IDENTITY,
49 };
50 
51 /* Converts return values to strings */
ff_vk_ret2str(VkResult res)52 const char *ff_vk_ret2str(VkResult res)
53 {
54 #define CASE(VAL) case VAL: return #VAL
55     switch (res) {
56     CASE(VK_SUCCESS);
57     CASE(VK_NOT_READY);
58     CASE(VK_TIMEOUT);
59     CASE(VK_EVENT_SET);
60     CASE(VK_EVENT_RESET);
61     CASE(VK_INCOMPLETE);
62     CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
63     CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
64     CASE(VK_ERROR_INITIALIZATION_FAILED);
65     CASE(VK_ERROR_DEVICE_LOST);
66     CASE(VK_ERROR_MEMORY_MAP_FAILED);
67     CASE(VK_ERROR_LAYER_NOT_PRESENT);
68     CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
69     CASE(VK_ERROR_FEATURE_NOT_PRESENT);
70     CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
71     CASE(VK_ERROR_TOO_MANY_OBJECTS);
72     CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
73     CASE(VK_ERROR_FRAGMENTED_POOL);
74     CASE(VK_ERROR_SURFACE_LOST_KHR);
75     CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
76     CASE(VK_SUBOPTIMAL_KHR);
77     CASE(VK_ERROR_OUT_OF_DATE_KHR);
78     CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
79     CASE(VK_ERROR_VALIDATION_FAILED_EXT);
80     CASE(VK_ERROR_INVALID_SHADER_NV);
81     CASE(VK_ERROR_OUT_OF_POOL_MEMORY);
82     CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE);
83     CASE(VK_ERROR_NOT_PERMITTED_EXT);
84     default: return "Unknown error";
85     }
86 #undef CASE
87 }
88 
vk_alloc_mem(AVFilterContext * avctx,VkMemoryRequirements * req,VkMemoryPropertyFlagBits req_flags,void * alloc_extension,VkMemoryPropertyFlagBits * mem_flags,VkDeviceMemory * mem)89 static int vk_alloc_mem(AVFilterContext *avctx, VkMemoryRequirements *req,
90                         VkMemoryPropertyFlagBits req_flags, void *alloc_extension,
91                         VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
92 {
93     VkResult ret;
94     int index = -1;
95     VkPhysicalDeviceProperties props;
96     VkPhysicalDeviceMemoryProperties mprops;
97     VulkanFilterContext *s = avctx->priv;
98 
99     VkMemoryAllocateInfo alloc_info = {
100         .sType           = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
101         .pNext           = alloc_extension,
102     };
103 
104     vkGetPhysicalDeviceProperties(s->hwctx->phys_dev, &props);
105     vkGetPhysicalDeviceMemoryProperties(s->hwctx->phys_dev, &mprops);
106 
107     /* Align if we need to */
108     if (req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
109         req->size = FFALIGN(req->size, props.limits.minMemoryMapAlignment);
110 
111     alloc_info.allocationSize = req->size;
112 
113     /* The vulkan spec requires memory types to be sorted in the "optimal"
114      * order, so the first matching type we find will be the best/fastest one */
115     for (int i = 0; i < mprops.memoryTypeCount; i++) {
116         /* The memory type must be supported by the requirements (bitfield) */
117         if (!(req->memoryTypeBits & (1 << i)))
118             continue;
119 
120         /* The memory type flags must include our properties */
121         if ((mprops.memoryTypes[i].propertyFlags & req_flags) != req_flags)
122             continue;
123 
124         /* Found a suitable memory type */
125         index = i;
126         break;
127     }
128 
129     if (index < 0) {
130         av_log(avctx, AV_LOG_ERROR, "No memory type found for flags 0x%x\n",
131                req_flags);
132         return AVERROR(EINVAL);
133     }
134 
135     alloc_info.memoryTypeIndex = index;
136 
137     ret = vkAllocateMemory(s->hwctx->act_dev, &alloc_info,
138                            s->hwctx->alloc, mem);
139     if (ret != VK_SUCCESS) {
140         av_log(avctx, AV_LOG_ERROR, "Failed to allocate memory: %s\n",
141                ff_vk_ret2str(ret));
142         return AVERROR(ENOMEM);
143     }
144 
145     *mem_flags |= mprops.memoryTypes[index].propertyFlags;
146 
147     return 0;
148 }
149 
ff_vk_create_buf(AVFilterContext * avctx,FFVkBuffer * buf,size_t size,VkBufferUsageFlags usage,VkMemoryPropertyFlagBits flags)150 int ff_vk_create_buf(AVFilterContext *avctx, FFVkBuffer *buf, size_t size,
151                      VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
152 {
153     int err;
154     VkResult ret;
155     VkMemoryRequirements req;
156     VulkanFilterContext *s = avctx->priv;
157 
158     VkBufferCreateInfo buf_spawn = {
159         .sType       = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
160         .pNext       = NULL,
161         .usage       = usage,
162         .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
163         .size        = size, /* Gets FFALIGNED during alloc if host visible
164                                 but should be ok */
165     };
166 
167     ret = vkCreateBuffer(s->hwctx->act_dev, &buf_spawn, NULL, &buf->buf);
168     if (ret != VK_SUCCESS) {
169         av_log(avctx, AV_LOG_ERROR, "Failed to create buffer: %s\n",
170                ff_vk_ret2str(ret));
171         return AVERROR_EXTERNAL;
172     }
173 
174     vkGetBufferMemoryRequirements(s->hwctx->act_dev, buf->buf, &req);
175 
176     err = vk_alloc_mem(avctx, &req, flags, NULL, &buf->flags, &buf->mem);
177     if (err)
178         return err;
179 
180     ret = vkBindBufferMemory(s->hwctx->act_dev, buf->buf, buf->mem, 0);
181     if (ret != VK_SUCCESS) {
182         av_log(avctx, AV_LOG_ERROR, "Failed to bind memory to buffer: %s\n",
183                ff_vk_ret2str(ret));
184         return AVERROR_EXTERNAL;
185     }
186 
187     return 0;
188 }
189 
ff_vk_map_buffers(AVFilterContext * avctx,FFVkBuffer * buf,uint8_t * mem[],int nb_buffers,int invalidate)190 int ff_vk_map_buffers(AVFilterContext *avctx, FFVkBuffer *buf, uint8_t *mem[],
191                       int nb_buffers, int invalidate)
192 {
193     VkResult ret;
194     VulkanFilterContext *s = avctx->priv;
195     VkMappedMemoryRange *inval_list = NULL;
196     int inval_count = 0;
197 
198     for (int i = 0; i < nb_buffers; i++) {
199         ret = vkMapMemory(s->hwctx->act_dev, buf[i].mem, 0,
200                           VK_WHOLE_SIZE, 0, (void **)&mem[i]);
201         if (ret != VK_SUCCESS) {
202             av_log(avctx, AV_LOG_ERROR, "Failed to map buffer memory: %s\n",
203                    ff_vk_ret2str(ret));
204             return AVERROR_EXTERNAL;
205         }
206     }
207 
208     if (!invalidate)
209         return 0;
210 
211     for (int i = 0; i < nb_buffers; i++) {
212         const VkMappedMemoryRange ival_buf = {
213             .sType  = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
214             .memory = buf[i].mem,
215             .size   = VK_WHOLE_SIZE,
216         };
217         if (buf[i].flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
218             continue;
219         inval_list = av_fast_realloc(s->scratch, &s->scratch_size,
220                                      (++inval_count)*sizeof(*inval_list));
221         if (!inval_list)
222             return AVERROR(ENOMEM);
223         inval_list[inval_count - 1] = ival_buf;
224     }
225 
226     if (inval_count) {
227         ret = vkInvalidateMappedMemoryRanges(s->hwctx->act_dev, inval_count,
228                                              inval_list);
229         if (ret != VK_SUCCESS) {
230             av_log(avctx, AV_LOG_ERROR, "Failed to invalidate memory: %s\n",
231                    ff_vk_ret2str(ret));
232             return AVERROR_EXTERNAL;
233         }
234     }
235 
236     return 0;
237 }
238 
ff_vk_unmap_buffers(AVFilterContext * avctx,FFVkBuffer * buf,int nb_buffers,int flush)239 int ff_vk_unmap_buffers(AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers,
240                         int flush)
241 {
242     int err = 0;
243     VkResult ret;
244     VulkanFilterContext *s = avctx->priv;
245     VkMappedMemoryRange *flush_list = NULL;
246     int flush_count = 0;
247 
248     if (flush) {
249         for (int i = 0; i < nb_buffers; i++) {
250             const VkMappedMemoryRange flush_buf = {
251                 .sType  = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
252                 .memory = buf[i].mem,
253                 .size   = VK_WHOLE_SIZE,
254             };
255             if (buf[i].flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
256                 continue;
257             flush_list = av_fast_realloc(s->scratch, &s->scratch_size,
258                                          (++flush_count)*sizeof(*flush_list));
259             if (!flush_list)
260                 return AVERROR(ENOMEM);
261             flush_list[flush_count - 1] = flush_buf;
262         }
263     }
264 
265     if (flush_count) {
266         ret = vkFlushMappedMemoryRanges(s->hwctx->act_dev, flush_count,
267                                         flush_list);
268         if (ret != VK_SUCCESS) {
269             av_log(avctx, AV_LOG_ERROR, "Failed to flush memory: %s\n",
270                    ff_vk_ret2str(ret));
271             err = AVERROR_EXTERNAL; /* We still want to try to unmap them */
272         }
273     }
274 
275     for (int i = 0; i < nb_buffers; i++)
276         vkUnmapMemory(s->hwctx->act_dev, buf[i].mem);
277 
278     return err;
279 }
280 
ff_vk_free_buf(AVFilterContext * avctx,FFVkBuffer * buf)281 void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf)
282 {
283     VulkanFilterContext *s = avctx->priv;
284     if (!buf)
285         return;
286 
287     if (buf->buf != VK_NULL_HANDLE)
288         vkDestroyBuffer(s->hwctx->act_dev, buf->buf, s->hwctx->alloc);
289     if (buf->mem != VK_NULL_HANDLE)
290         vkFreeMemory(s->hwctx->act_dev, buf->mem, s->hwctx->alloc);
291 }
292 
ff_vk_add_push_constant(AVFilterContext * avctx,VulkanPipeline * pl,int offset,int size,VkShaderStageFlagBits stage)293 int ff_vk_add_push_constant(AVFilterContext *avctx, VulkanPipeline *pl,
294                             int offset, int size, VkShaderStageFlagBits stage)
295 {
296     VkPushConstantRange *pc;
297 
298     pl->push_consts = av_realloc_array(pl->push_consts, sizeof(*pl->push_consts),
299                                        pl->push_consts_num + 1);
300     if (!pl->push_consts)
301         return AVERROR(ENOMEM);
302 
303     pc = &pl->push_consts[pl->push_consts_num++];
304     memset(pc, 0, sizeof(*pc));
305 
306     pc->stageFlags = stage;
307     pc->offset = offset;
308     pc->size = size;
309 
310     return 0;
311 }
312 
FN_CREATING(VulkanFilterContext,FFVkExecContext,exec_ctx,exec_ctx,exec_ctx_num)313 FN_CREATING(VulkanFilterContext, FFVkExecContext, exec_ctx, exec_ctx, exec_ctx_num)
314 int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx, int queue)
315 {
316     VkResult ret;
317     FFVkExecContext *e;
318     VulkanFilterContext *s = avctx->priv;
319 
320     VkCommandPoolCreateInfo cqueue_create = {
321         .sType              = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
322         .flags              = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
323         .queueFamilyIndex   = queue,
324     };
325     VkCommandBufferAllocateInfo cbuf_create = {
326         .sType              = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
327         .level              = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
328         .commandBufferCount = 1,
329     };
330     VkFenceCreateInfo fence_spawn = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
331 
332     e = create_exec_ctx(s);
333     if (!e)
334         return AVERROR(ENOMEM);
335 
336     ret = vkCreateCommandPool(s->hwctx->act_dev, &cqueue_create,
337                               s->hwctx->alloc, &e->pool);
338     if (ret != VK_SUCCESS) {
339         av_log(avctx, AV_LOG_ERROR, "Command pool creation failure: %s\n",
340                ff_vk_ret2str(ret));
341         return 1;
342     }
343 
344     cbuf_create.commandPool = e->pool;
345 
346     ret = vkAllocateCommandBuffers(s->hwctx->act_dev, &cbuf_create, &e->buf);
347     if (ret != VK_SUCCESS) {
348         av_log(avctx, AV_LOG_ERROR, "Command buffer alloc failure: %s\n",
349                ff_vk_ret2str(ret));
350         return 1;
351     }
352 
353     ret = vkCreateFence(s->hwctx->act_dev, &fence_spawn,
354                         s->hwctx->alloc, &e->fence);
355     if (ret != VK_SUCCESS) {
356         av_log(avctx, AV_LOG_ERROR, "Failed to create frame fence: %s\n",
357                ff_vk_ret2str(ret));
358         return 1;
359     }
360 
361     vkGetDeviceQueue(s->hwctx->act_dev, queue, 0, &e->queue);
362 
363     *ctx = e;
364 
365     return 0;
366 }
367 
ff_vk_start_exec_recording(AVFilterContext * avctx,FFVkExecContext * e)368 int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e)
369 {
370     VkResult ret;
371     VkCommandBufferBeginInfo cmd_start = {
372         .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
373         .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
374     };
375 
376     e->sem_wait_cnt = 0;
377     e->sem_sig_cnt = 0;
378 
379     ret = vkBeginCommandBuffer(e->buf, &cmd_start);
380     if (ret != VK_SUCCESS) {
381         av_log(avctx, AV_LOG_ERROR, "Failed to start command recoding: %s\n",
382                ff_vk_ret2str(ret));
383         return AVERROR_EXTERNAL;
384     }
385 
386     return 0;
387 }
388 
ff_vk_add_exec_dep(AVFilterContext * avctx,FFVkExecContext * e,AVFrame * frame,VkPipelineStageFlagBits in_wait_dst_flag)389 int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e,
390                        AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag)
391 {
392     AVVkFrame *f = (AVVkFrame *)frame->data[0];
393     AVHWFramesContext *fc = (AVHWFramesContext *)frame->hw_frames_ctx->data;
394     int planes = av_pix_fmt_count_planes(fc->sw_format);
395 
396     for (int i = 0; i < planes; i++) {
397         e->sem_wait = av_fast_realloc(e->sem_wait, &e->sem_wait_alloc,
398                                       (e->sem_wait_cnt + 1)*sizeof(*e->sem_wait));
399         if (!e->sem_wait)
400             return AVERROR(ENOMEM);
401 
402         e->sem_wait_dst = av_fast_realloc(e->sem_wait_dst, &e->sem_wait_dst_alloc,
403                                           (e->sem_wait_cnt + 1)*sizeof(*e->sem_wait_dst));
404         if (!e->sem_wait_dst)
405             return AVERROR(ENOMEM);
406 
407         e->sem_sig = av_fast_realloc(e->sem_sig, &e->sem_sig_alloc,
408                                      (e->sem_sig_cnt + 1)*sizeof(*e->sem_sig));
409         if (!e->sem_sig)
410             return AVERROR(ENOMEM);
411 
412         e->sem_wait[e->sem_wait_cnt] = f->sem[i];
413         e->sem_wait_dst[e->sem_wait_cnt] = in_wait_dst_flag;
414         e->sem_wait_cnt++;
415 
416         e->sem_sig[e->sem_sig_cnt] = f->sem[i];
417         e->sem_sig_cnt++;
418     }
419 
420     return 0;
421 }
422 
ff_vk_submit_exec_queue(AVFilterContext * avctx,FFVkExecContext * e)423 int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e)
424 {
425     VkResult ret;
426     VulkanFilterContext *s = avctx->priv;
427 
428     VkSubmitInfo s_info = {
429         .sType                = VK_STRUCTURE_TYPE_SUBMIT_INFO,
430         .commandBufferCount   = 1,
431         .pCommandBuffers      = &e->buf,
432 
433         .pWaitSemaphores      = e->sem_wait,
434         .pWaitDstStageMask    = e->sem_wait_dst,
435         .waitSemaphoreCount   = e->sem_wait_cnt,
436 
437         .pSignalSemaphores    = e->sem_sig,
438         .signalSemaphoreCount = e->sem_sig_cnt,
439     };
440 
441     vkEndCommandBuffer(e->buf);
442 
443     ret = vkQueueSubmit(e->queue, 1, &s_info, e->fence);
444     if (ret != VK_SUCCESS) {
445         av_log(avctx, AV_LOG_ERROR, "Unable to submit command buffer: %s\n",
446                ff_vk_ret2str(ret));
447         return AVERROR_EXTERNAL;
448     }
449 
450     vkWaitForFences(s->hwctx->act_dev, 1, &e->fence, VK_TRUE, UINT64_MAX);
451     vkResetFences(s->hwctx->act_dev, 1, &e->fence);
452 
453     return 0;
454 }
455 
ff_vk_filter_query_formats(AVFilterContext * avctx)456 int ff_vk_filter_query_formats(AVFilterContext *avctx)
457 {
458     static const enum AVPixelFormat pixel_formats[] = {
459         AV_PIX_FMT_VULKAN, AV_PIX_FMT_NONE,
460     };
461     AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
462     if (!pix_fmts)
463         return AVERROR(ENOMEM);
464 
465     return ff_set_common_formats(avctx, pix_fmts);
466 }
467 
vulkan_filter_set_device(AVFilterContext * avctx,AVBufferRef * device)468 static int vulkan_filter_set_device(AVFilterContext *avctx,
469                                     AVBufferRef *device)
470 {
471     VulkanFilterContext *s = avctx->priv;
472 
473     av_buffer_unref(&s->device_ref);
474 
475     s->device_ref = av_buffer_ref(device);
476     if (!s->device_ref)
477         return AVERROR(ENOMEM);
478 
479     s->device = (AVHWDeviceContext*)s->device_ref->data;
480     s->hwctx  = s->device->hwctx;
481 
482     return 0;
483 }
484 
vulkan_filter_set_frames(AVFilterContext * avctx,AVBufferRef * frames)485 static int vulkan_filter_set_frames(AVFilterContext *avctx,
486                                     AVBufferRef *frames)
487 {
488     VulkanFilterContext *s = avctx->priv;
489 
490     av_buffer_unref(&s->frames_ref);
491 
492     s->frames_ref = av_buffer_ref(frames);
493     if (!s->frames_ref)
494         return AVERROR(ENOMEM);
495 
496     return 0;
497 }
498 
ff_vk_filter_config_input(AVFilterLink * inlink)499 int ff_vk_filter_config_input(AVFilterLink *inlink)
500 {
501     int err;
502     AVFilterContext *avctx = inlink->dst;
503     VulkanFilterContext *s = avctx->priv;
504     AVHWFramesContext *input_frames;
505 
506     if (!inlink->hw_frames_ctx) {
507         av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
508                "hardware frames context on the input.\n");
509         return AVERROR(EINVAL);
510     }
511 
512     /* Extract the device and default output format from the first input. */
513     if (avctx->inputs[0] != inlink)
514         return 0;
515 
516     input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
517     if (input_frames->format != AV_PIX_FMT_VULKAN)
518         return AVERROR(EINVAL);
519 
520     err = vulkan_filter_set_device(avctx, input_frames->device_ref);
521     if (err < 0)
522         return err;
523     err = vulkan_filter_set_frames(avctx, inlink->hw_frames_ctx);
524     if (err < 0)
525         return err;
526 
527     /* Default output parameters match input parameters. */
528     s->input_format = input_frames->sw_format;
529     if (s->output_format == AV_PIX_FMT_NONE)
530         s->output_format = input_frames->sw_format;
531     if (!s->output_width)
532         s->output_width  = inlink->w;
533     if (!s->output_height)
534         s->output_height = inlink->h;
535 
536     return 0;
537 }
538 
ff_vk_filter_config_output_inplace(AVFilterLink * outlink)539 int ff_vk_filter_config_output_inplace(AVFilterLink *outlink)
540 {
541     int err;
542     AVFilterContext *avctx = outlink->src;
543     VulkanFilterContext *s = avctx->priv;
544 
545     av_buffer_unref(&outlink->hw_frames_ctx);
546 
547     if (!s->device_ref) {
548         if (!avctx->hw_device_ctx) {
549             av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
550                    "Vulkan device.\n");
551             return AVERROR(EINVAL);
552         }
553 
554         err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
555         if (err < 0)
556             return err;
557     }
558 
559     outlink->hw_frames_ctx = av_buffer_ref(s->frames_ref);
560     if (!outlink->hw_frames_ctx)
561         return AVERROR(ENOMEM);
562 
563     outlink->w = s->output_width;
564     outlink->h = s->output_height;
565 
566     return 0;
567 }
568 
ff_vk_filter_config_output(AVFilterLink * outlink)569 int ff_vk_filter_config_output(AVFilterLink *outlink)
570 {
571     int err;
572     AVFilterContext *avctx = outlink->src;
573     VulkanFilterContext *s = avctx->priv;
574     AVBufferRef *output_frames_ref;
575     AVHWFramesContext *output_frames;
576 
577     av_buffer_unref(&outlink->hw_frames_ctx);
578 
579     if (!s->device_ref) {
580         if (!avctx->hw_device_ctx) {
581             av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
582                    "Vulkan device.\n");
583             return AVERROR(EINVAL);
584         }
585 
586         err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
587         if (err < 0)
588             return err;
589     }
590 
591     output_frames_ref = av_hwframe_ctx_alloc(s->device_ref);
592     if (!output_frames_ref) {
593         err = AVERROR(ENOMEM);
594         goto fail;
595     }
596     output_frames = (AVHWFramesContext*)output_frames_ref->data;
597 
598     output_frames->format    = AV_PIX_FMT_VULKAN;
599     output_frames->sw_format = s->output_format;
600     output_frames->width     = s->output_width;
601     output_frames->height    = s->output_height;
602 
603     err = av_hwframe_ctx_init(output_frames_ref);
604     if (err < 0) {
605         av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
606                "frames: %d.\n", err);
607         goto fail;
608     }
609 
610     outlink->hw_frames_ctx = output_frames_ref;
611     outlink->w = s->output_width;
612     outlink->h = s->output_height;
613 
614     return 0;
615 fail:
616     av_buffer_unref(&output_frames_ref);
617     return err;
618 }
619 
ff_vk_filter_init(AVFilterContext * avctx)620 int ff_vk_filter_init(AVFilterContext *avctx)
621 {
622     VulkanFilterContext *s = avctx->priv;
623 
624     s->output_format = AV_PIX_FMT_NONE;
625 
626     if (glslang_init())
627         return AVERROR_EXTERNAL;
628 
629     return 0;
630 }
631 
FN_CREATING(VulkanFilterContext,VkSampler,sampler,samplers,samplers_num)632 FN_CREATING(VulkanFilterContext, VkSampler, sampler, samplers, samplers_num)
633 VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,
634                               VkFilter filt)
635 {
636     VkResult ret;
637     VulkanFilterContext *s = avctx->priv;
638 
639     VkSamplerCreateInfo sampler_info = {
640         .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
641         .magFilter = filt,
642         .minFilter = sampler_info.magFilter,
643         .mipmapMode = unnorm_coords ? VK_SAMPLER_MIPMAP_MODE_NEAREST :
644                                       VK_SAMPLER_MIPMAP_MODE_LINEAR,
645         .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
646         .addressModeV = sampler_info.addressModeU,
647         .addressModeW = sampler_info.addressModeU,
648         .anisotropyEnable = VK_FALSE,
649         .compareOp = VK_COMPARE_OP_NEVER,
650         .borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
651         .unnormalizedCoordinates = unnorm_coords,
652     };
653 
654     VkSampler *sampler = create_sampler(s);
655     if (!sampler)
656         return NULL;
657 
658     ret = vkCreateSampler(s->hwctx->act_dev, &sampler_info,
659                           s->hwctx->alloc, sampler);
660     if (ret != VK_SUCCESS) {
661         av_log(avctx, AV_LOG_ERROR, "Unable to init sampler: %s\n",
662                ff_vk_ret2str(ret));
663         return NULL;
664     }
665 
666     return sampler;
667 }
668 
ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)669 const char *ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt)
670 {
671     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixfmt);
672     const int high = desc->comp[0].depth > 8;
673     return high ? "rgba16f" : "rgba8";
674 }
675 
ff_vk_create_imageview(AVFilterContext * avctx,VkImageView * v,VkImage img,VkFormat fmt,const VkComponentMapping map)676 int ff_vk_create_imageview(AVFilterContext *avctx, VkImageView *v, VkImage img,
677                            VkFormat fmt, const VkComponentMapping map)
678 {
679     VulkanFilterContext *s = avctx->priv;
680     VkImageViewCreateInfo imgview_spawn = {
681         .sType      = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
682         .pNext      = NULL,
683         .image      = img,
684         .viewType   = VK_IMAGE_VIEW_TYPE_2D,
685         .format     = fmt,
686         .components = map,
687         .subresourceRange = {
688             .aspectMask     = VK_IMAGE_ASPECT_COLOR_BIT,
689             .baseMipLevel   = 0,
690             .levelCount     = 1,
691             .baseArrayLayer = 0,
692             .layerCount     = 1,
693         },
694     };
695 
696     VkResult ret = vkCreateImageView(s->hwctx->act_dev, &imgview_spawn,
697                                      s->hwctx->alloc, v);
698     if (ret != VK_SUCCESS) {
699         av_log(s, AV_LOG_ERROR, "Failed to create imageview: %s\n",
700                ff_vk_ret2str(ret));
701         return AVERROR_EXTERNAL;
702     }
703 
704     return 0;
705 }
706 
ff_vk_destroy_imageview(AVFilterContext * avctx,VkImageView * v)707 void ff_vk_destroy_imageview(AVFilterContext *avctx, VkImageView *v)
708 {
709     VulkanFilterContext *s = avctx->priv;
710     if (v && *v) {
711         vkDestroyImageView(s->hwctx->act_dev, *v, s->hwctx->alloc);
712         *v = NULL;
713     }
714 }
715 
FN_CREATING(VulkanPipeline,SPIRVShader,shader,shaders,shaders_num)716 FN_CREATING(VulkanPipeline, SPIRVShader, shader, shaders, shaders_num)
717 SPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, VulkanPipeline *pl,
718                                const char *name, VkShaderStageFlags stage)
719 {
720     SPIRVShader *shd = create_shader(pl);
721     if (!shd)
722         return NULL;
723 
724     av_bprint_init(&shd->src, 0, AV_BPRINT_SIZE_UNLIMITED);
725 
726     shd->shader.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
727     shd->shader.stage = stage;
728 
729     shd->name = name;
730 
731     GLSLF(0, #version %i                                                  ,460);
732     GLSLC(0, #define IS_WITHIN(v1, v2) ((v1.x < v2.x) && (v1.y < v2.y))       );
733     GLSLC(0,                                                                  );
734 
735     return shd;
736 }
737 
ff_vk_set_compute_shader_sizes(AVFilterContext * avctx,SPIRVShader * shd,int local_size[3])738 void ff_vk_set_compute_shader_sizes(AVFilterContext *avctx, SPIRVShader *shd,
739                                         int local_size[3])
740 {
741     shd->local_size[0] = local_size[0];
742     shd->local_size[1] = local_size[1];
743     shd->local_size[2] = local_size[2];
744 
745     av_bprintf(&shd->src, "layout (local_size_x = %i, "
746                "local_size_y = %i, local_size_z = %i) in;\n\n",
747                shd->local_size[0], shd->local_size[1], shd->local_size[2]);
748 }
749 
print_shader(AVFilterContext * avctx,SPIRVShader * shd,int prio)750 static void print_shader(AVFilterContext *avctx, SPIRVShader *shd, int prio)
751 {
752     int line = 0;
753     const char *p = shd->src.str;
754     const char *start = p;
755 
756     AVBPrint buf;
757     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
758 
759     for (int i = 0; i < strlen(p); i++) {
760         if (p[i] == '\n') {
761             av_bprintf(&buf, "%i\t", ++line);
762             av_bprint_append_data(&buf, start, &p[i] - start + 1);
763             start = &p[i + 1];
764         }
765     }
766 
767     av_log(avctx, prio, "Shader %s: \n%s", shd->name, buf.str);
768     av_bprint_finalize(&buf, NULL);
769 }
770 
ff_vk_compile_shader(AVFilterContext * avctx,SPIRVShader * shd,const char * entrypoint)771 int ff_vk_compile_shader(AVFilterContext *avctx, SPIRVShader *shd,
772                          const char *entrypoint)
773 {
774     VkResult ret;
775     VulkanFilterContext *s = avctx->priv;
776     VkShaderModuleCreateInfo shader_create;
777     GLSlangResult *res;
778 
779     static const enum GLSlangStage emap[] = {
780         [VK_SHADER_STAGE_VERTEX_BIT]   = GLSLANG_VERTEX,
781         [VK_SHADER_STAGE_FRAGMENT_BIT] = GLSLANG_FRAGMENT,
782         [VK_SHADER_STAGE_COMPUTE_BIT]  = GLSLANG_COMPUTE,
783     };
784 
785     shd->shader.pName = entrypoint;
786 
787     res = glslang_compile(shd->src.str, emap[shd->shader.stage]);
788     if (!res)
789         return AVERROR(ENOMEM);
790 
791     if (res->rval) {
792         av_log(avctx, AV_LOG_ERROR, "Error compiling shader %s: %s!\n",
793                shd->name, av_err2str(res->rval));
794         print_shader(avctx, shd, AV_LOG_ERROR);
795         if (res->error_msg)
796             av_log(avctx, AV_LOG_ERROR, "%s", res->error_msg);
797         av_free(res->error_msg);
798         return res->rval;
799     }
800 
801     print_shader(avctx, shd, AV_LOG_VERBOSE);
802 
803     shader_create.sType    = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
804     shader_create.pNext    = NULL;
805     shader_create.codeSize = res->size;
806     shader_create.flags    = 0;
807     shader_create.pCode    = res->data;
808 
809     ret = vkCreateShaderModule(s->hwctx->act_dev, &shader_create, NULL,
810                                &shd->shader.module);
811 
812     /* Free the GLSlangResult struct */
813     av_free(res);
814 
815     if (ret != VK_SUCCESS) {
816         av_log(avctx, AV_LOG_ERROR, "Unable to create shader module: %s\n",
817                ff_vk_ret2str(ret));
818         return AVERROR_EXTERNAL;
819     }
820 
821     av_log(avctx, AV_LOG_VERBOSE, "Shader %s linked! Size: %zu bytes\n",
822            shd->name, shader_create.codeSize);
823 
824     return 0;
825 }
826 
827 static const struct descriptor_props {
828     size_t struct_size; /* Size of the opaque which updates the descriptor */
829     const char *type;
830     int is_uniform;
831     int mem_quali;      /* Can use a memory qualifier */
832     int dim_needed;     /* Must indicate dimension */
833     int buf_content;    /* Must indicate buffer contents */
834 } descriptor_props[] = {
835     [VK_DESCRIPTOR_TYPE_SAMPLER]                = { sizeof(VkDescriptorImageInfo),  "sampler",       1, 0, 0, 0, },
836     [VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE]          = { sizeof(VkDescriptorImageInfo),  "texture",       1, 0, 1, 0, },
837     [VK_DESCRIPTOR_TYPE_STORAGE_IMAGE]          = { sizeof(VkDescriptorImageInfo),  "image",         1, 1, 1, 0, },
838     [VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT]       = { sizeof(VkDescriptorImageInfo),  "subpassInput",  1, 0, 0, 0, },
839     [VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER] = { sizeof(VkDescriptorImageInfo),  "sampler",       1, 0, 1, 0, },
840     [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER]         = { sizeof(VkDescriptorBufferInfo),  NULL,           1, 0, 0, 1, },
841     [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER]         = { sizeof(VkDescriptorBufferInfo), "buffer",        0, 1, 0, 1, },
842     [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC] = { sizeof(VkDescriptorBufferInfo),  NULL,           1, 0, 0, 1, },
843     [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC] = { sizeof(VkDescriptorBufferInfo), "buffer",        0, 1, 0, 1, },
844     [VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER]   = { sizeof(VkBufferView),           "samplerBuffer", 1, 0, 0, 0, },
845     [VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER]   = { sizeof(VkBufferView),           "imageBuffer",   1, 0, 0, 0, },
846 };
847 
ff_vk_add_descriptor_set(AVFilterContext * avctx,VulkanPipeline * pl,SPIRVShader * shd,VulkanDescriptorSetBinding * desc,int num,int only_print_to_shader)848 int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,
849                              SPIRVShader *shd, VulkanDescriptorSetBinding *desc,
850                              int num, int only_print_to_shader)
851 {
852     VkResult ret;
853     VkDescriptorSetLayout *layout;
854     VulkanFilterContext *s = avctx->priv;
855 
856     if (only_print_to_shader)
857         goto print;
858 
859     pl->desc_layout = av_realloc_array(pl->desc_layout, sizeof(*pl->desc_layout),
860                                        pl->descriptor_sets_num + 1);
861     if (!pl->desc_layout)
862         return AVERROR(ENOMEM);
863 
864     layout = &pl->desc_layout[pl->descriptor_sets_num];
865     memset(layout, 0, sizeof(*layout));
866 
867     { /* Create descriptor set layout descriptions */
868         VkDescriptorSetLayoutCreateInfo desc_create_layout = { 0 };
869         VkDescriptorSetLayoutBinding *desc_binding;
870 
871         desc_binding = av_mallocz(sizeof(*desc_binding)*num);
872         if (!desc_binding)
873             return AVERROR(ENOMEM);
874 
875         for (int i = 0; i < num; i++) {
876             desc_binding[i].binding            = i;
877             desc_binding[i].descriptorType     = desc[i].type;
878             desc_binding[i].descriptorCount    = FFMAX(desc[i].elems, 1);
879             desc_binding[i].stageFlags         = desc[i].stages;
880             desc_binding[i].pImmutableSamplers = desc[i].samplers;
881         }
882 
883         desc_create_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
884         desc_create_layout.pBindings = desc_binding;
885         desc_create_layout.bindingCount = num;
886 
887         ret = vkCreateDescriptorSetLayout(s->hwctx->act_dev, &desc_create_layout,
888                                           s->hwctx->alloc, layout);
889         av_free(desc_binding);
890         if (ret != VK_SUCCESS) {
891             av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor set "
892                    "layout: %s\n", ff_vk_ret2str(ret));
893             return AVERROR_EXTERNAL;
894         }
895     }
896 
897     { /* Pool each descriptor by type and update pool counts */
898         for (int i = 0; i < num; i++) {
899             int j;
900             for (j = 0; j < pl->pool_size_desc_num; j++)
901                 if (pl->pool_size_desc[j].type == desc[i].type)
902                     break;
903             if (j >= pl->pool_size_desc_num) {
904                 pl->pool_size_desc = av_realloc_array(pl->pool_size_desc,
905                                                       sizeof(*pl->pool_size_desc),
906                                                       ++pl->pool_size_desc_num);
907                 if (!pl->pool_size_desc)
908                     return AVERROR(ENOMEM);
909                 memset(&pl->pool_size_desc[j], 0, sizeof(VkDescriptorPoolSize));
910             }
911             pl->pool_size_desc[j].type             = desc[i].type;
912             pl->pool_size_desc[j].descriptorCount += FFMAX(desc[i].elems, 1);
913         }
914     }
915 
916     { /* Create template creation struct */
917         VkDescriptorUpdateTemplateCreateInfo *dt;
918         VkDescriptorUpdateTemplateEntry *des_entries;
919 
920         /* Freed after descriptor set initialization */
921         des_entries = av_mallocz(num*sizeof(VkDescriptorUpdateTemplateEntry));
922         if (!des_entries)
923             return AVERROR(ENOMEM);
924 
925         for (int i = 0; i < num; i++) {
926             des_entries[i].dstBinding      = i;
927             des_entries[i].descriptorType  = desc[i].type;
928             des_entries[i].descriptorCount = FFMAX(desc[i].elems, 1);
929             des_entries[i].dstArrayElement = 0;
930             des_entries[i].offset          = ((uint8_t *)desc[i].updater) - (uint8_t *)s;
931             des_entries[i].stride          = descriptor_props[desc[i].type].struct_size;
932         }
933 
934         pl->desc_template_info = av_realloc_array(pl->desc_template_info,
935                                                   sizeof(*pl->desc_template_info),
936                                                   pl->descriptor_sets_num + 1);
937         if (!pl->desc_template_info)
938             return AVERROR(ENOMEM);
939 
940         dt = &pl->desc_template_info[pl->descriptor_sets_num];
941         memset(dt, 0, sizeof(*dt));
942 
943         dt->sType = VK_STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO;
944         dt->templateType = VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET;
945         dt->descriptorSetLayout = *layout;
946         dt->pDescriptorUpdateEntries = des_entries;
947         dt->descriptorUpdateEntryCount = num;
948     }
949 
950     pl->descriptor_sets_num++;
951 
952 print:
953     /* Write shader info */
954     for (int i = 0; i < num; i++) {
955         const struct descriptor_props *prop = &descriptor_props[desc[i].type];
956         GLSLA("layout (set = %i, binding = %i", pl->descriptor_sets_num - 1, i);
957 
958         if (desc[i].mem_layout)
959             GLSLA(", %s", desc[i].mem_layout);
960         GLSLA(")");
961 
962         if (prop->is_uniform)
963             GLSLA(" uniform");
964 
965         if (prop->mem_quali && desc[i].mem_quali)
966             GLSLA(" %s", desc[i].mem_quali);
967 
968         if (prop->type)
969             GLSLA(" %s", prop->type);
970 
971         if (prop->dim_needed)
972             GLSLA("%iD", desc[i].dimensions);
973 
974         GLSLA(" %s", desc[i].name);
975 
976         if (prop->buf_content)
977             GLSLA(" {\n    %s\n}", desc[i].buf_content);
978         else if (desc[i].elems > 0)
979             GLSLA("[%i]", desc[i].elems);
980 
981         GLSLA(";\n");
982     }
983     GLSLA("\n");
984 
985     return 0;
986 }
987 
ff_vk_update_descriptor_set(AVFilterContext * avctx,VulkanPipeline * pl,int set_id)988 void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,
989                                  int set_id)
990 {
991     VulkanFilterContext *s = avctx->priv;
992 
993     vkUpdateDescriptorSetWithTemplate(s->hwctx->act_dev,
994                                       pl->desc_set[set_id],
995                                       pl->desc_template[set_id], s);
996 }
997 
ff_vk_update_push_exec(AVFilterContext * avctx,FFVkExecContext * e,VkShaderStageFlagBits stage,int offset,size_t size,void * src)998 void ff_vk_update_push_exec(AVFilterContext *avctx, FFVkExecContext *e,
999                             VkShaderStageFlagBits stage, int offset,
1000                             size_t size, void *src)
1001 {
1002     vkCmdPushConstants(e->buf, e->bound_pl->pipeline_layout,
1003                        stage, offset, size, src);
1004 }
1005 
ff_vk_init_pipeline_layout(AVFilterContext * avctx,VulkanPipeline * pl)1006 int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl)
1007 {
1008     VkResult ret;
1009     VulkanFilterContext *s = avctx->priv;
1010 
1011     { /* Init descriptor set pool */
1012         VkDescriptorPoolCreateInfo pool_create_info = {
1013             .sType         = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
1014             .poolSizeCount = pl->pool_size_desc_num,
1015             .pPoolSizes    = pl->pool_size_desc,
1016             .maxSets       = pl->descriptor_sets_num,
1017         };
1018 
1019         ret = vkCreateDescriptorPool(s->hwctx->act_dev, &pool_create_info,
1020                                      s->hwctx->alloc, &pl->desc_pool);
1021         av_freep(&pl->pool_size_desc);
1022         if (ret != VK_SUCCESS) {
1023             av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor set "
1024                    "pool: %s\n", ff_vk_ret2str(ret));
1025             return AVERROR_EXTERNAL;
1026         }
1027     }
1028 
1029     { /* Allocate descriptor sets */
1030         VkDescriptorSetAllocateInfo alloc_info = {
1031             .sType              = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1032             .descriptorPool     = pl->desc_pool,
1033             .descriptorSetCount = pl->descriptor_sets_num,
1034             .pSetLayouts        = pl->desc_layout,
1035         };
1036 
1037         pl->desc_set = av_malloc(pl->descriptor_sets_num*sizeof(*pl->desc_set));
1038         if (!pl->desc_set)
1039             return AVERROR(ENOMEM);
1040 
1041         ret = vkAllocateDescriptorSets(s->hwctx->act_dev, &alloc_info,
1042                                        pl->desc_set);
1043         if (ret != VK_SUCCESS) {
1044             av_log(avctx, AV_LOG_ERROR, "Unable to allocate descriptor set: %s\n",
1045                    ff_vk_ret2str(ret));
1046             return AVERROR_EXTERNAL;
1047         }
1048     }
1049 
1050     { /* Finally create the pipeline layout */
1051         VkPipelineLayoutCreateInfo spawn_pipeline_layout = {
1052             .sType                  = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1053             .setLayoutCount         = pl->descriptor_sets_num,
1054             .pSetLayouts            = pl->desc_layout,
1055             .pushConstantRangeCount = pl->push_consts_num,
1056             .pPushConstantRanges    = pl->push_consts,
1057         };
1058 
1059         ret = vkCreatePipelineLayout(s->hwctx->act_dev, &spawn_pipeline_layout,
1060                                      s->hwctx->alloc, &pl->pipeline_layout);
1061         av_freep(&pl->push_consts);
1062         pl->push_consts_num = 0;
1063         if (ret != VK_SUCCESS) {
1064             av_log(avctx, AV_LOG_ERROR, "Unable to init pipeline layout: %s\n",
1065                    ff_vk_ret2str(ret));
1066             return AVERROR_EXTERNAL;
1067         }
1068     }
1069 
1070     { /* Descriptor template (for tightly packed descriptors) */
1071         VkDescriptorUpdateTemplateCreateInfo *desc_template_info;
1072 
1073         pl->desc_template = av_malloc(pl->descriptor_sets_num*sizeof(*pl->desc_template));
1074         if (!pl->desc_template)
1075             return AVERROR(ENOMEM);
1076 
1077         /* Create update templates for the descriptor sets */
1078         for (int i = 0; i < pl->descriptor_sets_num; i++) {
1079             desc_template_info = &pl->desc_template_info[i];
1080             desc_template_info->pipelineLayout = pl->pipeline_layout;
1081             ret = vkCreateDescriptorUpdateTemplate(s->hwctx->act_dev,
1082                                                    desc_template_info,
1083                                                    s->hwctx->alloc,
1084                                                    &pl->desc_template[i]);
1085             av_free((void *)desc_template_info->pDescriptorUpdateEntries);
1086             if (ret != VK_SUCCESS) {
1087                 av_log(avctx, AV_LOG_ERROR, "Unable to init descriptor "
1088                        "template: %s\n", ff_vk_ret2str(ret));
1089                 return AVERROR_EXTERNAL;
1090             }
1091         }
1092 
1093         av_freep(&pl->desc_template_info);
1094     }
1095 
1096     return 0;
1097 }
1098 
FN_CREATING(VulkanFilterContext,VulkanPipeline,pipeline,pipelines,pipelines_num)1099 FN_CREATING(VulkanFilterContext, VulkanPipeline, pipeline, pipelines, pipelines_num)
1100 VulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx)
1101 {
1102     return create_pipeline(avctx->priv);
1103 }
1104 
ff_vk_init_compute_pipeline(AVFilterContext * avctx,VulkanPipeline * pl)1105 int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl)
1106 {
1107     int i;
1108     VkResult ret;
1109     VulkanFilterContext *s = avctx->priv;
1110 
1111     VkComputePipelineCreateInfo pipe = {
1112         .sType  = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
1113         .layout = pl->pipeline_layout,
1114     };
1115 
1116     for (i = 0; i < pl->shaders_num; i++) {
1117         if (pl->shaders[i]->shader.stage & VK_SHADER_STAGE_COMPUTE_BIT) {
1118             pipe.stage = pl->shaders[i]->shader;
1119             break;
1120         }
1121     }
1122     if (i == pl->shaders_num) {
1123         av_log(avctx, AV_LOG_ERROR, "Can't init compute pipeline, no shader\n");
1124         return AVERROR(EINVAL);
1125     }
1126 
1127     ret = vkCreateComputePipelines(s->hwctx->act_dev, VK_NULL_HANDLE, 1, &pipe,
1128                                    s->hwctx->alloc, &pl->pipeline);
1129     if (ret != VK_SUCCESS) {
1130         av_log(avctx, AV_LOG_ERROR, "Unable to init compute pipeline: %s\n",
1131                ff_vk_ret2str(ret));
1132         return AVERROR_EXTERNAL;
1133     }
1134 
1135     pl->bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
1136 
1137     return 0;
1138 }
1139 
ff_vk_bind_pipeline_exec(AVFilterContext * avctx,FFVkExecContext * e,VulkanPipeline * pl)1140 void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,
1141                               VulkanPipeline *pl)
1142 {
1143     vkCmdBindPipeline(e->buf, pl->bind_point, pl->pipeline);
1144 
1145     vkCmdBindDescriptorSets(e->buf, pl->bind_point, pl->pipeline_layout, 0,
1146                             pl->descriptor_sets_num, pl->desc_set, 0, 0);
1147 
1148     e->bound_pl = pl;
1149 }
1150 
free_exec_ctx(VulkanFilterContext * s,FFVkExecContext * e)1151 static void free_exec_ctx(VulkanFilterContext *s, FFVkExecContext *e)
1152 {
1153     vkDestroyFence(s->hwctx->act_dev, e->fence, s->hwctx->alloc);
1154 
1155     if (e->buf   != VK_NULL_HANDLE)
1156         vkFreeCommandBuffers(s->hwctx->act_dev, e->pool, 1, &e->buf);
1157     if (e->pool  != VK_NULL_HANDLE)
1158         vkDestroyCommandPool(s->hwctx->act_dev, e->pool, s->hwctx->alloc);
1159 
1160     av_free(e->sem_wait);
1161     av_free(e->sem_wait_dst);
1162     av_free(e->sem_sig);
1163 
1164     av_free(e);
1165 }
1166 
free_pipeline(VulkanFilterContext * s,VulkanPipeline * pl)1167 static void free_pipeline(VulkanFilterContext *s, VulkanPipeline *pl)
1168 {
1169     for (int i = 0; i < pl->shaders_num; i++) {
1170         SPIRVShader *shd = pl->shaders[i];
1171         av_bprint_finalize(&shd->src, NULL);
1172         vkDestroyShaderModule(s->hwctx->act_dev, shd->shader.module,
1173                               s->hwctx->alloc);
1174         av_free(shd);
1175     }
1176 
1177     vkDestroyPipeline(s->hwctx->act_dev, pl->pipeline, s->hwctx->alloc);
1178     vkDestroyPipelineLayout(s->hwctx->act_dev, pl->pipeline_layout,
1179                             s->hwctx->alloc);
1180 
1181     for (int i = 0; i < pl->descriptor_sets_num; i++) {
1182         if (pl->desc_template && pl->desc_template[i])
1183             vkDestroyDescriptorUpdateTemplate(s->hwctx->act_dev, pl->desc_template[i],
1184                                               s->hwctx->alloc);
1185         if (pl->desc_layout && pl->desc_layout[i])
1186             vkDestroyDescriptorSetLayout(s->hwctx->act_dev, pl->desc_layout[i],
1187                                          s->hwctx->alloc);
1188     }
1189 
1190     /* Also frees the descriptor sets */
1191     if (pl->desc_pool)
1192         vkDestroyDescriptorPool(s->hwctx->act_dev, pl->desc_pool,
1193                                 s->hwctx->alloc);
1194 
1195     av_freep(&pl->desc_set);
1196     av_freep(&pl->shaders);
1197     av_freep(&pl->desc_layout);
1198     av_freep(&pl->desc_template);
1199     av_freep(&pl->push_consts);
1200     pl->push_consts_num = 0;
1201 
1202     /* Only freed in case of failure */
1203     av_freep(&pl->pool_size_desc);
1204     if (pl->desc_template_info) {
1205         for (int i = 0; i < pl->descriptor_sets_num; i++)
1206             av_free((void *)pl->desc_template_info[i].pDescriptorUpdateEntries);
1207         av_freep(&pl->desc_template_info);
1208     }
1209 
1210     av_free(pl);
1211 }
1212 
ff_vk_filter_uninit(AVFilterContext * avctx)1213 void ff_vk_filter_uninit(AVFilterContext *avctx)
1214 {
1215     VulkanFilterContext *s = avctx->priv;
1216 
1217     glslang_uninit();
1218 
1219     for (int i = 0; i < s->samplers_num; i++)
1220         vkDestroySampler(s->hwctx->act_dev, *s->samplers[i], s->hwctx->alloc);
1221     av_freep(&s->samplers);
1222 
1223     for (int i = 0; i < s->pipelines_num; i++)
1224         free_pipeline(s, s->pipelines[i]);
1225     av_freep(&s->pipelines);
1226 
1227     for (int i = 0; i < s->exec_ctx_num; i++)
1228         free_exec_ctx(s, s->exec_ctx[i]);
1229     av_freep(&s->exec_ctx);
1230 
1231     av_freep(&s->scratch);
1232     s->scratch_size = 0;
1233 
1234     av_buffer_unref(&s->device_ref);
1235     av_buffer_unref(&s->frames_ref);
1236 }
1237