hwcontext_vulkan.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. typedef struct AVVkFrame AVVkFrame;
  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. * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
  36. * All of these can be set before init to change what the context uses
  37. */
  38. typedef struct AVVulkanDeviceContext {
  39. /**
  40. * Custom memory allocator, else NULL
  41. */
  42. const VkAllocationCallbacks *alloc;
  43. /**
  44. * Pointer to the instance-provided vkGetInstanceProcAddr loading function.
  45. * If NULL, will pick either libvulkan or libvolk, depending on libavutil's
  46. * compilation settings, and set this field.
  47. */
  48. PFN_vkGetInstanceProcAddr get_proc_addr;
  49. /**
  50. * Vulkan instance. Must be at least version 1.3.
  51. */
  52. VkInstance inst;
  53. /**
  54. * Physical device
  55. */
  56. VkPhysicalDevice phys_dev;
  57. /**
  58. * Active device
  59. */
  60. VkDevice act_dev;
  61. /**
  62. * This structure should be set to the set of features that present and enabled
  63. * during device creation. When a device is created by FFmpeg, it will default to
  64. * enabling all that are present of the shaderImageGatherExtended,
  65. * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
  66. */
  67. VkPhysicalDeviceFeatures2 device_features;
  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. * Enabled device extensions. By default, VK_KHR_external_memory_fd,
  79. * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
  80. * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
  81. * If supplying your own device context, these fields takes the same format as
  82. * the above fields, with the same conditions that duplicates are possible
  83. * and accepted, and that NULL and 0 respectively means no extensions are enabled.
  84. */
  85. const char * const *enabled_dev_extensions;
  86. int nb_enabled_dev_extensions;
  87. /**
  88. * Queue family index for graphics operations, and the number of queues
  89. * enabled for it. If unavaiable, will be set to -1. Not required.
  90. * av_hwdevice_create() will attempt to find a dedicated queue for each
  91. * queue family, or pick the one with the least unrelated flags set.
  92. * Queue indices here may overlap if a queue has to share capabilities.
  93. */
  94. int queue_family_index;
  95. int nb_graphics_queues;
  96. /**
  97. * Queue family index for transfer operations and the number of queues
  98. * enabled. Required.
  99. */
  100. int queue_family_tx_index;
  101. int nb_tx_queues;
  102. /**
  103. * Queue family index for compute operations and the number of queues
  104. * enabled. Required.
  105. */
  106. int queue_family_comp_index;
  107. int nb_comp_queues;
  108. /**
  109. * Queue family index for video encode ops, and the amount of queues enabled.
  110. * If the device doesn't support such, queue_family_encode_index will be -1.
  111. * Not required.
  112. */
  113. int queue_family_encode_index;
  114. int nb_encode_queues;
  115. /**
  116. * Queue family index for video decode ops, and the amount of queues enabled.
  117. * If the device doesn't support such, queue_family_decode_index will be -1.
  118. * Not required.
  119. */
  120. int queue_family_decode_index;
  121. int nb_decode_queues;
  122. /**
  123. * Locks a queue, preventing other threads from submitting any command
  124. * buffers to this queue.
  125. * If set to NULL, will be set to lavu-internal functions that utilize a
  126. * mutex.
  127. */
  128. void (*lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
  129. /**
  130. * Similar to lock_queue(), unlocks a queue. Must only be called after locking.
  131. */
  132. void (*unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
  133. } AVVulkanDeviceContext;
  134. /**
  135. * Defines the behaviour of frame allocation.
  136. */
  137. typedef enum AVVkFrameFlags {
  138. /* Unless this flag is set, autodetected flags will be OR'd based on the
  139. * device and tiling during av_hwframe_ctx_init(). */
  140. AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
  141. #if FF_API_VULKAN_CONTIGUOUS_MEMORY
  142. /* DEPRECATED: does nothing. Replaced by multiplane images. */
  143. AV_VK_FRAME_FLAG_CONTIGUOUS_MEMORY = (1ULL << 1),
  144. #endif
  145. /* Disables multiplane images.
  146. * This is required to export/import images from CUDA. */
  147. AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE = (1ULL << 2),
  148. } AVVkFrameFlags;
  149. /**
  150. * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
  151. */
  152. typedef struct AVVulkanFramesContext {
  153. /**
  154. * Controls the tiling of allocated frames.
  155. * If left as VK_IMAGE_TILING_OPTIMAL (0), will use optimal tiling.
  156. * Can be set to VK_IMAGE_TILING_LINEAR to force linear images,
  157. * or VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT to force DMABUF-backed
  158. * images.
  159. * @note Imported frames from other APIs ignore this.
  160. */
  161. VkImageTiling tiling;
  162. /**
  163. * Defines extra usage of output frames. If non-zero, all flags MUST be
  164. * supported by the VkFormat. Otherwise, will use supported flags amongst:
  165. * - VK_IMAGE_USAGE_SAMPLED_BIT
  166. * - VK_IMAGE_USAGE_STORAGE_BIT
  167. * - VK_IMAGE_USAGE_TRANSFER_SRC_BIT
  168. * - VK_IMAGE_USAGE_TRANSFER_DST_BIT
  169. */
  170. VkImageUsageFlagBits usage;
  171. /**
  172. * Extension data for image creation.
  173. * If DRM tiling is used, a VkImageDrmFormatModifierListCreateInfoEXT structure
  174. * can be added to specify the exact modifier to use.
  175. *
  176. * Additional structures may be added at av_hwframe_ctx_init() time,
  177. * which will be freed automatically on uninit(), so users must only free
  178. * any structures they've allocated themselves.
  179. */
  180. void *create_pnext;
  181. /**
  182. * Extension data for memory allocation. Must have as many entries as
  183. * the number of planes of the sw_format.
  184. * This will be chained to VkExportMemoryAllocateInfo, which is used
  185. * to make all pool images exportable to other APIs if the necessary
  186. * extensions are present in enabled_dev_extensions.
  187. */
  188. void *alloc_pnext[AV_NUM_DATA_POINTERS];
  189. /**
  190. * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
  191. * autodetected flags will be OR'd based on the device and tiling during
  192. * av_hwframe_ctx_init().
  193. */
  194. AVVkFrameFlags flags;
  195. /**
  196. * Flags to set during image creation. If unset, defaults to
  197. * VK_IMAGE_CREATE_ALIAS_BIT.
  198. */
  199. VkImageCreateFlags img_flags;
  200. /**
  201. * Vulkan format for each image. MUST be compatible with the pixel format.
  202. * If unset, will be automatically set.
  203. * There are at most two compatible formats for a frame - a multiplane
  204. * format, and a single-plane multi-image format.
  205. */
  206. VkFormat format[AV_NUM_DATA_POINTERS];
  207. /**
  208. * Number of layers each image will have.
  209. */
  210. int nb_layers;
  211. /**
  212. * Locks a frame, preventing other threads from changing frame properties.
  213. * Users SHOULD only ever lock just before command submission in order
  214. * to get accurate frame properties, and unlock immediately after command
  215. * submission without waiting for it to finish.
  216. *
  217. * If unset, will be set to lavu-internal functions that utilize a mutex.
  218. */
  219. void (*lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
  220. /**
  221. * Similar to lock_frame(), unlocks a frame. Must only be called after locking.
  222. */
  223. void (*unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
  224. } AVVulkanFramesContext;
  225. /*
  226. * Frame structure.
  227. *
  228. * @note the size of this structure is not part of the ABI, to allocate
  229. * you must use @av_vk_frame_alloc().
  230. */
  231. struct AVVkFrame {
  232. /**
  233. * Vulkan images to which the memory is bound to.
  234. * May be one for multiplane formats, or multiple.
  235. */
  236. VkImage img[AV_NUM_DATA_POINTERS];
  237. /**
  238. * Tiling for the frame.
  239. */
  240. VkImageTiling tiling;
  241. /**
  242. * Memory backing the images. Either one, or as many as there are planes
  243. * in the sw_format.
  244. * In case of having multiple VkImages, but one memory, the offset field
  245. * will indicate the bound offset for each image.
  246. */
  247. VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
  248. size_t size[AV_NUM_DATA_POINTERS];
  249. /**
  250. * OR'd flags for all memory allocated
  251. */
  252. VkMemoryPropertyFlagBits flags;
  253. /**
  254. * Updated after every barrier. One per VkImage.
  255. */
  256. VkAccessFlagBits access[AV_NUM_DATA_POINTERS];
  257. VkImageLayout layout[AV_NUM_DATA_POINTERS];
  258. /**
  259. * Synchronization timeline semaphores, one for each VkImage.
  260. * Must not be freed manually. Must be waited on at every submission using
  261. * the value in sem_value, and must be signalled at every submission,
  262. * using an incremented value.
  263. */
  264. VkSemaphore sem[AV_NUM_DATA_POINTERS];
  265. /**
  266. * Up to date semaphore value at which each image becomes accessible.
  267. * One per VkImage.
  268. * Clients must wait on this value when submitting a command queue,
  269. * and increment it when signalling.
  270. */
  271. uint64_t sem_value[AV_NUM_DATA_POINTERS];
  272. /**
  273. * Internal data.
  274. */
  275. struct AVVkFrameInternal *internal;
  276. /**
  277. * Describes the binding offset of each image to the VkDeviceMemory.
  278. * One per VkImage.
  279. */
  280. ptrdiff_t offset[AV_NUM_DATA_POINTERS];
  281. /**
  282. * Queue family of the images. Must be VK_QUEUE_FAMILY_IGNORED if
  283. * the image was allocated with the CONCURRENT concurrency option.
  284. * One per VkImage.
  285. */
  286. uint32_t queue_family[AV_NUM_DATA_POINTERS];
  287. };
  288. /**
  289. * Allocates a single AVVkFrame and initializes everything as 0.
  290. * @note Must be freed via av_free()
  291. */
  292. AVVkFrame *av_vk_frame_alloc(void);
  293. /**
  294. * Returns the optimal per-plane Vulkan format for a given sw_format,
  295. * one for each plane.
  296. * Returns NULL on unsupported formats.
  297. */
  298. const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p);
  299. #endif /* AVUTIL_HWCONTEXT_VULKAN_H */