]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_vulkan.h
ab075a74e0ce04d98bc314e403419eb332c75915
[ffmpeg] / libavutil / hwcontext_vulkan.h
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_HWCONTEXT_VULKAN_H
20 #define AVUTIL_HWCONTEXT_VULKAN_H
21
22 #include <vulkan/vulkan.h>
23
24 #include "pixfmt.h"
25 #include "frame.h"
26
27 /**
28  * @file
29  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
30  *
31  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
32  * with the data pointer set to an AVVkFrame.
33  */
34
35 /**
36  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
37  * All of these can be set before init to change what the context uses
38  */
39 typedef struct AVVulkanDeviceContext {
40     /**
41      * Custom memory allocator, else NULL
42      */
43     const VkAllocationCallbacks *alloc;
44
45     /**
46      * Vulkan instance. Must be at least version 1.1.
47      */
48     VkInstance inst;
49
50     /**
51      * Physical device
52      */
53     VkPhysicalDevice phys_dev;
54
55     /**
56      * Active device
57      */
58     VkDevice act_dev;
59
60     /**
61      * This structure should be set to the set of features that present and enabled
62      * during device creation. When a device is created by FFmpeg, it will default to
63      * enabling all that are present of the shaderImageGatherExtended,
64      * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
65      */
66     VkPhysicalDeviceFeatures2 device_features;
67
68     /**
69      * Enabled instance extensions.
70      * If supplying your own device context, set this to an array of strings, with
71      * each entry containing the specified Vulkan extension string to enable.
72      * Duplicates are possible and accepted.
73      * If no extensions are enabled, set these fields to NULL, and 0 respectively.
74      */
75     const char * const *enabled_inst_extensions;
76     int nb_enabled_inst_extensions;
77
78     /**
79      * Enabled device extensions. By default, VK_KHR_external_memory_fd,
80      * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
81      * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
82      * If supplying your own device context, these fields takes the same format as
83      * the above fields, with the same conditions that duplicates are possible
84      * and accepted, and that NULL and 0 respectively means no extensions are enabled.
85      */
86     const char * const *enabled_dev_extensions;
87     int nb_enabled_dev_extensions;
88
89     /**
90      * Queue family index for graphics
91      * @note av_hwdevice_create() will set all 3 queue indices if unset
92      * If there is no dedicated queue for compute or transfer operations,
93      * they will be set to the graphics queue index which can handle both.
94      * nb_graphics_queues indicates how many queues were enabled for the
95      * graphics queue (must be at least 1)
96      */
97     int queue_family_index;
98     int nb_graphics_queues;
99
100     /**
101      * Queue family index to use for transfer operations, and the amount of queues
102      * enabled. In case there is no dedicated transfer queue, nb_tx_queues
103      * must be 0 and queue_family_tx_index must be the same as either the graphics
104      * queue or the compute queue, if available.
105      */
106     int queue_family_tx_index;
107     int nb_tx_queues;
108
109     /**
110      * Queue family index for compute ops, and the amount of queues enabled.
111      * In case there are no dedicated compute queues, nb_comp_queues must be
112      * 0 and its queue family index must be set to the graphics queue.
113      */
114     int queue_family_comp_index;
115     int nb_comp_queues;
116 } AVVulkanDeviceContext;
117
118 /**
119  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
120  */
121 typedef struct AVVulkanFramesContext {
122     /**
123      * Controls the tiling of allocated frames.
124      */
125     VkImageTiling tiling;
126
127     /**
128      * Defines extra usage of output frames. If left as 0, the following bits
129      * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
130      */
131     VkImageUsageFlagBits usage;
132
133     /**
134      * Extension data for image creation.
135      */
136     void *create_pnext;
137
138     /**
139      * Extension data for memory allocation. Must have as many entries as
140      * the number of planes of the sw_format.
141      * This will be chained to VkExportMemoryAllocateInfo, which is used
142      * to make all pool images exportable to other APIs if the necessary
143      * extensions are present in enabled_dev_extensions.
144      */
145     void *alloc_pnext[AV_NUM_DATA_POINTERS];
146 } AVVulkanFramesContext;
147
148 /*
149  * Frame structure, the VkFormat of the image will always match
150  * the pool's sw_format.
151  * All frames, imported or allocated, will be created with the
152  * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
153  *
154  * If all three queue family indices in the device context are the same,
155  * images will be created with the EXCLUSIVE sharing mode. Otherwise, all images
156  * will be created using the CONCURRENT sharing mode.
157  *
158  * @note the size of this structure is not part of the ABI, to allocate
159  * you must use @av_vk_frame_alloc().
160  */
161 typedef struct AVVkFrame {
162     /**
163      * Vulkan images to which the memory is bound to.
164      */
165     VkImage img[AV_NUM_DATA_POINTERS];
166
167     /**
168      * The same tiling must be used for all images in the frame.
169      */
170     VkImageTiling tiling;
171
172     /**
173      * Memory backing the images. Could be less than the amount of images
174      * if importing from a DRM or VAAPI frame.
175      */
176     VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
177     size_t size[AV_NUM_DATA_POINTERS];
178
179     /**
180      * OR'd flags for all memory allocated
181      */
182     VkMemoryPropertyFlagBits flags;
183
184     /**
185      * Updated after every barrier
186      */
187     VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
188     VkImageLayout layout[AV_NUM_DATA_POINTERS];
189
190     /**
191      * Synchronization semaphores. Must not be freed manually. Must be waited on
192      * and signalled at every queue submission.
193      * Could be less than the amount of images: either one per VkDeviceMemory
194      * or one for the entire frame. All others will be set to VK_NULL_HANDLE.
195      */
196     VkSemaphore sem[AV_NUM_DATA_POINTERS];
197
198     /**
199      * Internal data.
200      */
201     struct AVVkFrameInternal *internal;
202 } AVVkFrame;
203
204 /**
205  * Allocates a single AVVkFrame and initializes everything as 0.
206  * @note Must be freed via av_free()
207  */
208 AVVkFrame *av_vk_frame_alloc(void);
209
210 /**
211  * Returns the format of each image up to the number of planes for a given sw_format.
212  * Returns NULL on unsupported formats.
213  */
214 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
215
216 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */