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 #ifndef AVUTIL_VULKAN_H
20 #define AVUTIL_VULKAN_H
21
22 #define VK_NO_PROTOTYPES
23
24 #include <stdatomic.h>
25
31
32 /* GLSL management macros */
33 #define INDENT(N) INDENT_##N
35 #define INDENT_1 INDENT_0 " "
36 #define INDENT_2 INDENT_1 INDENT_1
37 #define INDENT_3 INDENT_2 INDENT_1
38 #define INDENT_4 INDENT_3 INDENT_1
39 #define INDENT_5 INDENT_4 INDENT_1
40 #define INDENT_6 INDENT_5 INDENT_1
41 #define C(N, S) INDENT(N) #S "\n"
42
44 do { \
45 av_bprintf(&shd->src, C(N, S)); \
46 } while (0)
47
49 do { \
50 av_bprintf(&shd->src, __VA_ARGS__); \
51 } while (0)
52
53 #define GLSLF(N, S, ...) \
54 do { \
55 av_bprintf(&shd->src, C(N, S), __VA_ARGS__); \
56 } while (0)
57
59 do { \
60 av_bprintf(&shd->src, "\n"); \
61 av_bprint_append_data(&shd->src, D, strlen(D)); \
62 av_bprintf(&shd->src, "\n"); \
63 } while (0)
64
65 /* Helper, pretty much every Vulkan return value needs to be checked */
67 do { \
68 if ((err = (x)) < 0) \
69 goto fail; \
70 } while (0)
71
72 #define DUP_SAMPLER(x) { x, x, x, x }
73
77 const char *
mem_layout;
/* Storage images (rgba8, etc.) and buffers (std430, etc.) */
78 const char *
mem_quali;
/* readonly, writeonly, etc. */
81 uint32_t
elems;
/* 0 - scalar, 1 or more - vector */
83 uint32_t
buf_elems;
/* Appends [buf_elems] to the contents. Avoids manually printing to a string. */
84 VkSampler
samplers[4];
/* Sampler to use for all elems */
86
93
94 /* Local use only */
97
98 /* Only valid when allocated via ff_vk_get_pooled_buffer with HOST_VISIBLE or
99 * via ff_vk_host_map_buffer */
101
102 /* Set by ff_vk_host_map_buffer. This is the offset at which the buffer data
103 * actually begins at.
104 * The address and mapped_mem fields will be offset by this amount. */
106
107 /* If host mapping, reference to the backing host memory buffer */
110
115
116 /* Queue for the execution context */
120
121 /* Command buffer for the context */
123
124 /* Fence for the command buffer */
126
127 /* Opaque data, untouched, free to use by users */
129
132
133 /* Buffer dependencies */
137
138 /* Frame dependencies */
142
143 /* Software frame dependencies */
147
151
155
159
162
165
168
171
175
177 /* Descriptor buffer */
181
185
186 /* Descriptor set is shared between all submissions */
189
191 /* Name for id/debugging purposes */
193
194 /* Shader text */
196
197 /* Compute shader local group sizes */
199
200 /* Shader bind point/type */
203
204 /* Creation info */
206
207 /* Base shader object */
210
211 /* Pipeline layout */
213
214 /* Push consts */
217
218 /* Descriptor sets */
221
222 /* Descriptor buffer */
225
226 /* Descriptor pool */
231
233 /* Descriptor buffer */
237
239 /* Shader to which this data belongs to */
242
243 /* Descriptor buffer */
246
247 /* Descriptor pools */
251
255
259
268
269 /* Registered shaders' data */
273
277
284 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
hprops;
296
299
303
307
312
315
316 /* Properties */
322
324 {
325 int cnt = 0;
327 cnt++;
328
329 return cnt;
330 }
331
333 {
334 const VkBaseInStructure *in = chain;
335 while (in) {
336 if (in->sType == stype)
337 return in;
338
339 in = in->pNext;
340 }
341
343 }
344
346 {
347 VkBaseOutStructure *
out = chain;
350
351 out->pNext = (
void *)in;
352 }
353
354 #define FF_VK_STRUCT_EXT(CTX, BASE, STRUCT_P, EXT_FLAG, TYPE) \
355 do { \
356 if ((EXT_FLAG == FF_VK_EXT_NO_FLAG) || \
357 ((CTX)->extensions & EXT_FLAG)) { \
358 (STRUCT_P)->sType = TYPE; \
359 ff_vk_link_struct(BASE, STRUCT_P); \
360 } \
361 } while (0)
362
363 /* Identity mapping - r = r, b = b, g = g, a = a */
365
366 /**
367 * Initializes the AVClass, in case this context is not used
368 * as the main user's context.
369 * May use either a frames context reference, or a device context reference.
370 */
373
374 /**
375 * Converts Vulkan return values to strings
376 */
378
379 /**
380 * Map between usage and features.
381 */
384
385 /**
386 * Returns 1 if pixfmt is a usable RGB format.
387 */
389
390 /**
391 * Since storage images may not be swizzled, we have to do this in the
392 * shader itself. This fills in a lookup table to do it.
393 */
395
396 /**
397 * Get the aspect flag for a plane from an image.
398 */
400
401 /**
402 * Returns the format to use for images in shaders.
403 */
405 /* Native format with no conversion. May require casting. */
407 /* Float conversion of the native format. */
409 /* Signed integer version of the native format */
411 /* Unsigned integer version of the native format */
413 };
416
417 /**
418 * Loads props/mprops/driver_props
419 */
421
422 /**
423 * Chooses an appropriate QF.
424 */
426 VkQueueFlagBits dev_family,
427 VkVideoCodecOperationFlagBitsKHR vid_ops);
428
429 /**
430 * Allocates/frees an execution pool.
431 * If used in a multi-threaded context, there must be at least as many contexts
432 * as there are threads.
433 * ff_vk_exec_pool_init_desc() MUST be called if ff_vk_exec_descriptor_set_add()
434 * has been called.
435 */
438 int nb_queries, VkQueryType query_type, int query_64bit,
439 const void *query_create_pnext);
441
442 /**
443 * Retrieve an execution pool. Threadsafe.
444 */
446
447 /**
448 * Performs nb_queries queries and returns their results and statuses.
449 * 64_BIT and WITH_STATUS flags are ignored as 64_BIT must be specified via
450 * query_64bit in ff_vk_exec_pool_init() and WITH_STATUS is always enabled.
451 */
453 void **
data, VkQueryResultFlagBits
flags);
454
455 /**
456 * Start/submit/wait an execution.
457 * ff_vk_exec_start() always waits on a submission, so using ff_vk_exec_wait()
458 * is not necessary (unless using it is just better).
459 */
463
464 /**
465 * Execution dependency management.
466 * Can attach buffers to executions that will only be unref'd once the
467 * buffer has finished executing.
468 * Adding a frame dep will *lock the frame*, until either the dependencies
469 * are discarded, the execution is submitted, or a failure happens.
470 * update_frame will update the frame's properties before it is unlocked,
471 * only if submission was successful.
472 */
476 VkSemaphore sem, uint64_t
val,
477 VkPipelineStageFlagBits2 stage);
479 VkSemaphore *sem, int nb,
480 VkPipelineStageFlagBits2 stage,
481 int wait); /* Ownership transferred if !wait */
483 VkPipelineStageFlagBits2 wait_stage,
484 VkPipelineStageFlagBits2 signal_stage);
488 VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar);
490 VkSemaphore *
dst, uint64_t *dst_val,
493
494 /**
495 * Create a single imageview for a given plane.
496 */
498 VkImageView *img_view, VkImageAspectFlags *aspect,
500
501 /**
502 * Create an imageview and add it as a dependency to an execution.
503 */
507
509 AVFrame *pic, VkImageMemoryBarrier2 *bar,
int *nb_bar,
510 VkPipelineStageFlags src_stage,
511 VkPipelineStageFlags dst_stage,
512 VkAccessFlagBits new_access,
513 VkImageLayout new_layout,
514 uint32_t new_qf);
515
516 /**
517 * Memory/buffer/image allocation helpers.
518 */
520 VkMemoryPropertyFlagBits req_flags, void *alloc_extension,
521 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem);
523 void *pNext, void *alloc_pNext,
524 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags);
525
526 /**
527 * Buffer management code.
528 */
530 int nb_buffers, int invalidate);
533
535 int invalidate)
536 {
538 1, invalidate);
539 }
540
542 {
544 }
545
547
548 /** Initialize a pool and create AVBufferRefs containing FFVkBuffer.
549 * Threadsafe to use. Buffers are automatically mapped on creation if
550 * VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT is set in mem_props. Users should
551 * synchronize access themselvesd. Mainly meant for device-local buffers. */
554 void *create_pNext,
size_t size,
555 VkMemoryPropertyFlagBits mem_props);
556
557 /** Maps a system RAM buffer into a Vulkan buffer.
558 * References the source buffer.
559 */
562 VkBufferUsageFlags
usage);
563
564 /**
565 * Create a sampler.
566 */
568 int unnorm_coords, VkFilter
filt);
569
570 /**
571 * Initialize a shader object, with a specific set of extensions, type+bind,
572 * local group size, and subgroup requirements.
573 */
575 VkPipelineStageFlags stage,
576 const char *extensions[], int nb_extensions,
577 int lg_x, int lg_y, int lg_z,
578 uint32_t required_subgroup_size);
579
580 /**
581 * Output the shader code as logging data, with a specific
582 * priority.
583 */
585
586 /**
587 * Link a shader into an executable.
588 */
590 uint8_t *spirv, size_t spirv_len,
591 const char *entrypoint);
592
593 /**
594 * Add/update push constants for execution.
595 */
597 VkShaderStageFlagBits stage);
598
599 /**
600 * Add descriptor to a shader. Must be called before shader init.
601 */
604 int singular, int print_to_shader_only);
605
606 /**
607 * Register a shader with an exec pool.
608 * Pool may be NULL if all descriptor sets are read-only.
609 */
612
613 /**
614 * Bind a shader.
615 */
618
619 /**
620 * Update push constant in a shader.
621 * Must be called before binding the shader.
622 */
625 VkShaderStageFlagBits stage,
627
628 /**
629 * Update a descriptor in a buffer with a buffer.
630 * Must be called before binding the shader.
631 */
634 int set,
int bind,
int elem,
637
638 /**
639 * Sets an image descriptor for specified shader and binding.
640 */
643 VkImageView view, VkImageLayout
layout,
644 VkSampler sampler);
645
646 /**
647 * Update a descriptor in a buffer with an image array..
648 * Must be called before binding the shader.
649 */
652 VkImageView *views,
int set,
int binding,
653 VkImageLayout
layout, VkSampler sampler);
654
655 /**
656 * Free a shader.
657 */
659
660 /**
661 * Frees main context.
662 */
664
665 #endif /* AVUTIL_VULKAN_H */