]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_vulkan.h
01d6d98b018f2a6bf9c391184690a91b0f450b23
[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 /**
25  * @file
26  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
27  *
28  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
29  * with the data pointer set to an AVVkFrame.
30  */
31
32 /**
33  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
34  * All of these can be set before init to change what the context uses
35  */
36 typedef struct AVVulkanDeviceContext {
37     /**
38      * Custom memory allocator, else NULL
39      */
40     const VkAllocationCallbacks *alloc;
41     /**
42      * Instance
43      */
44     VkInstance inst;
45     /**
46      * Physical device
47      */
48     VkPhysicalDevice phys_dev;
49     /**
50      * Active device
51      */
52     VkDevice act_dev;
53     /**
54      * Queue family index for graphics
55      * @note av_hwdevice_create() will set all 3 queue indices if unset
56      * If there is no dedicated queue for compute or transfer operations,
57      * they will be set to the graphics queue index which can handle both.
58      */
59     int queue_family_index;
60     /**
61      * Queue family index for transfer ops only
62      */
63     int queue_family_tx_index;
64     /**
65      * Queue family index for compute ops
66      */
67     int queue_family_comp_index;
68 } AVVulkanDeviceContext;
69
70 /**
71  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
72  */
73 typedef struct AVVulkanFramesContext {
74     /**
75      * Controls the tiling of output frames.
76      */
77     VkImageTiling tiling;
78     /**
79      * Defines extra usage of output frames. This is bitwise OR'd with the
80      * standard usage flags (SAMPLED, STORAGE, TRANSFER_SRC and TRANSFER_DST).
81      */
82     VkImageUsageFlagBits usage;
83     /**
84      * Extension data for image creation. By default, if the extension is
85      * available, this will be chained to a VkImageFormatListCreateInfoKHR.
86      */
87     void *create_pnext;
88     /**
89      * Extension data for memory allocation. Must have as many entries as
90      * the number of planes of the sw_format.
91      * This will be chained to VkExportMemoryAllocateInfo, which is used
92      * to make all pool images exportable to other APIs.
93      */
94     void *alloc_pnext[AV_NUM_DATA_POINTERS];
95 } AVVulkanFramesContext;
96
97 /*
98  * Frame structure, the VkFormat of the image will always match
99  * the pool's sw_format.
100  * All frames, imported or allocated, will be created with the
101  * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
102  *
103  * @note the size of this structure is not part of the ABI, to allocate
104  * you must use @av_vk_frame_alloc().
105  */
106 typedef struct AVVkFrame {
107     /**
108      * Vulkan images to which the memory is bound to.
109      */
110     VkImage img[AV_NUM_DATA_POINTERS];
111
112     /**
113      * Same tiling must be used for all images.
114      */
115     VkImageTiling tiling;
116
117     /**
118      * Memory backing the images. Could be less than the amount of images
119      * if importing from a DRM or VAAPI frame.
120      */
121     VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
122     size_t size[AV_NUM_DATA_POINTERS];
123
124     /**
125      * OR'd flags for all memory allocated
126      */
127     VkMemoryPropertyFlagBits flags;
128
129     /**
130      * Updated after every barrier
131      */
132     VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
133     VkImageLayout layout[AV_NUM_DATA_POINTERS];
134
135     /**
136      * Per-image semaphores. Must not be freed manually. Must be waited on
137      * and signalled at every queue submission.
138      */
139     VkSemaphore sem[AV_NUM_DATA_POINTERS];
140
141     /**
142      * Internal data.
143      */
144     struct AVVkFrameInternal *internal;
145 } AVVkFrame;
146
147 /**
148  * Allocates a single AVVkFrame and initializes everything as 0.
149  * @note Must be freed via av_free()
150  */
151 AVVkFrame *av_vk_frame_alloc(void);
152
153 /**
154  * Returns the format of each image up to the number of planes for a given sw_format.
155  */
156 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
157
158 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */