hwcontext_vulkan.h 7.0 KB

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