hwcontext_vulkan.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #ifndef AVUTIL_HWCONTEXT_VULKAN_H
  19. #define AVUTIL_HWCONTEXT_VULKAN_H
  20. #if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
  21. #define VK_USE_PLATFORM_WIN32_KHR
  22. #endif
  23. #include <vulkan/vulkan.h>
  24. #include "pixfmt.h"
  25. #include "frame.h"
  26. /**
  27. * @file
  28. * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
  29. *
  30. * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
  31. * with the data pointer set to an AVVkFrame.
  32. */
  33. /**
  34. * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
  35. * All of these can be set before init to change what the context uses
  36. */
  37. typedef struct AVVulkanDeviceContext {
  38. /**
  39. * Custom memory allocator, else NULL
  40. */
  41. const VkAllocationCallbacks *alloc;
  42. /**
  43. * Pointer to the instance-provided vkGetInstanceProcAddr loading function.
  44. * If NULL, will pick either libvulkan or libvolk, depending on libavutil's
  45. * compilation settings, and set this field.
  46. */
  47. PFN_vkGetInstanceProcAddr get_proc_addr;
  48. /**
  49. * Vulkan instance. Must be at least version 1.2.
  50. */
  51. VkInstance inst;
  52. /**
  53. * Physical device
  54. */
  55. VkPhysicalDevice phys_dev;
  56. /**
  57. * Active device
  58. */
  59. VkDevice act_dev;
  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. * Enabled instance extensions.
  69. * If supplying your own device context, set this to an array of strings, with
  70. * each entry containing the specified Vulkan extension string to enable.
  71. * Duplicates are possible and accepted.
  72. * If no extensions are enabled, set these fields to NULL, and 0 respectively.
  73. */
  74. const char * const *enabled_inst_extensions;
  75. int nb_enabled_inst_extensions;
  76. /**
  77. * Enabled device extensions. By default, VK_KHR_external_memory_fd,
  78. * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
  79. * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
  80. * If supplying your own device context, these fields takes the same format as
  81. * the above fields, with the same conditions that duplicates are possible
  82. * and accepted, and that NULL and 0 respectively means no extensions are enabled.
  83. */
  84. const char * const *enabled_dev_extensions;
  85. int nb_enabled_dev_extensions;
  86. /**
  87. * Queue family index for graphics operations, and the number of queues
  88. * enabled for it. If unavaiable, will be set to -1. Not required.
  89. * av_hwdevice_create() will attempt to find a dedicated queue for each
  90. * queue family, or pick the one with the least unrelated flags set.
  91. * Queue indices here may overlap if a queue has to share capabilities.
  92. */
  93. int queue_family_index;
  94. int nb_graphics_queues;
  95. /**
  96. * Queue family index for transfer operations and the number of queues
  97. * enabled. Required.
  98. */
  99. int queue_family_tx_index;
  100. int nb_tx_queues;
  101. /**
  102. * Queue family index for compute operations and the number of queues
  103. * enabled. Required.
  104. */
  105. int queue_family_comp_index;
  106. int nb_comp_queues;
  107. /**
  108. * Queue family index for video encode ops, and the amount of queues enabled.
  109. * If the device doesn't support such, queue_family_encode_index will be -1.
  110. * Not required.
  111. */
  112. int queue_family_encode_index;
  113. int nb_encode_queues;
  114. /**
  115. * Queue family index for video decode ops, and the amount of queues enabled.
  116. * If the device doesn't support such, queue_family_decode_index will be -1.
  117. * Not required.
  118. */
  119. int queue_family_decode_index;
  120. int nb_decode_queues;
  121. } AVVulkanDeviceContext;
  122. /**
  123. * Defines the behaviour of frame allocation.
  124. */
  125. typedef enum AVVkFrameFlags {
  126. /* Unless this flag is set, autodetected flags will be OR'd based on the
  127. * device and tiling during av_hwframe_ctx_init(). */
  128. AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
  129. /* Image planes will be allocated in a single VkDeviceMemory, rather
  130. * than as per-plane VkDeviceMemory allocations. Required for exporting
  131. * to VAAPI on Intel devices. */
  132. AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY = (1ULL << 1),
  133. } AVVkFrameFlags;
  134. /**
  135. * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
  136. */
  137. typedef struct AVVulkanFramesContext {
  138. /**
  139. * Controls the tiling of allocated frames. If left as optimal tiling,
  140. * then during av_hwframe_ctx_init() will decide based on whether the device
  141. * supports DRM modifiers, or if the linear_images flag is set, otherwise
  142. * will allocate optimally-tiled images.
  143. */
  144. VkImageTiling tiling;
  145. /**
  146. * Defines extra usage of output frames. If left as 0, the following bits
  147. * are set: TRANSFER_SRC, TRANSFER_DST. SAMPLED and STORAGE.
  148. */
  149. VkImageUsageFlagBits usage;
  150. /**
  151. * Extension data for image creation.
  152. * If VkImageDrmFormatModifierListCreateInfoEXT is present in the chain,
  153. * and the device supports DRM modifiers, then images will be allocated
  154. * with the specific requested DRM modifiers.
  155. * Additional structures may be added at av_hwframe_ctx_init() time,
  156. * which will be freed automatically on uninit(), so users need only free
  157. * any structures they've allocated themselves.
  158. */
  159. void *create_pnext;
  160. /**
  161. * Extension data for memory allocation. Must have as many entries as
  162. * the number of planes of the sw_format.
  163. * This will be chained to VkExportMemoryAllocateInfo, which is used
  164. * to make all pool images exportable to other APIs if the necessary
  165. * extensions are present in enabled_dev_extensions.
  166. */
  167. void *alloc_pnext[AV_NUM_DATA_POINTERS];
  168. /**
  169. * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
  170. * autodetected flags will be OR'd based on the device and tiling during
  171. * av_hwframe_ctx_init().
  172. */
  173. AVVkFrameFlags flags;
  174. } AVVulkanFramesContext;
  175. /*
  176. * Frame structure, the VkFormat of the image will always match
  177. * the pool's sw_format.
  178. * All frames, imported or allocated, will be created with the
  179. * VK_IMAGE_CREATE_ALIAS_BIT flag set, so the memory may be aliased if needed.
  180. *
  181. * If all queue family indices in the device context are the same,
  182. * images will be created with the EXCLUSIVE sharing mode. Otherwise, all images
  183. * will be created using the CONCURRENT sharing mode.
  184. *
  185. * @note the size of this structure is not part of the ABI, to allocate
  186. * you must use @av_vk_frame_alloc().
  187. */
  188. typedef struct AVVkFrame {
  189. /**
  190. * Vulkan images to which the memory is bound to.
  191. */
  192. VkImage img[AV_NUM_DATA_POINTERS];
  193. /**
  194. * The same tiling must be used for all images in the frame.
  195. */
  196. VkImageTiling tiling;
  197. /**
  198. * Memory backing the images. Could be less than the amount of planes,
  199. * in which case the offset value will indicate the binding offset of
  200. * each plane in the memory.
  201. */
  202. VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
  203. size_t size[AV_NUM_DATA_POINTERS];
  204. /**
  205. * OR'd flags for all memory allocated
  206. */
  207. VkMemoryPropertyFlagBits flags;
  208. /**
  209. * Updated after every barrier
  210. */
  211. VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
  212. VkImageLayout layout[AV_NUM_DATA_POINTERS];
  213. /**
  214. * Synchronization timeline semaphores, one for each sw_format plane.
  215. * Must not be freed manually. Must be waited on at every submission using
  216. * the value in sem_value, and must be signalled at every submission,
  217. * using an incremented value.
  218. */
  219. VkSemaphore sem[AV_NUM_DATA_POINTERS];
  220. /**
  221. * Up to date semaphore value at which each image becomes accessible.
  222. * Clients must wait on this value when submitting a command queue,
  223. * and increment it when signalling.
  224. */
  225. uint64_t sem_value[AV_NUM_DATA_POINTERS];
  226. /**
  227. * Internal data.
  228. */
  229. struct AVVkFrameInternal *internal;
  230. /**
  231. * Describes the binding offset of each plane to the VkDeviceMemory.
  232. */
  233. ptrdiff_t offset[AV_NUM_DATA_POINTERS];
  234. } AVVkFrame;
  235. /**
  236. * Allocates a single AVVkFrame and initializes everything as 0.
  237. * @note Must be freed via av_free()
  238. */
  239. AVVkFrame *av_vk_frame_alloc(void);
  240. /**
  241. * Returns the format of each image up to the number of planes for a given sw_format.
  242. * Returns NULL on unsupported formats.
  243. */
  244. const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
  245. #endif /* AVUTIL_HWCONTEXT_VULKAN_H */