You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
11KB

  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 AVFILTER_VULKAN_H
  19. #define AVFILTER_VULKAN_H
  20. #include "avfilter.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/bprint.h"
  23. #include "libavutil/hwcontext.h"
  24. #include "libavutil/hwcontext_vulkan.h"
  25. /* GLSL management macros */
  26. #define INDENT(N) INDENT_##N
  27. #define INDENT_0
  28. #define INDENT_1 INDENT_0 " "
  29. #define INDENT_2 INDENT_1 INDENT_1
  30. #define INDENT_3 INDENT_2 INDENT_1
  31. #define INDENT_4 INDENT_3 INDENT_1
  32. #define INDENT_5 INDENT_4 INDENT_1
  33. #define INDENT_6 INDENT_5 INDENT_1
  34. #define C(N, S) INDENT(N) #S "\n"
  35. #define GLSLC(N, S) av_bprintf(&shd->src, C(N, S))
  36. #define GLSLA(...) av_bprintf(&shd->src, __VA_ARGS__)
  37. #define GLSLF(N, S, ...) av_bprintf(&shd->src, C(N, S), __VA_ARGS__)
  38. #define GLSLD(D) GLSLC(0, ); \
  39. av_bprint_append_data(&shd->src, D, strlen(D)); \
  40. GLSLC(0, )
  41. /* Helper, pretty much every Vulkan return value needs to be checked */
  42. #define RET(x) \
  43. do { \
  44. if ((err = (x)) < 0) \
  45. goto fail; \
  46. } while (0)
  47. /* Useful for attaching immutable samplers to arrays */
  48. #define DUP_SAMPLER_ARRAY4(x) (VkSampler []){ x, x, x, x, }
  49. typedef struct SPIRVShader {
  50. const char *name; /* Name for id/debugging purposes */
  51. AVBPrint src;
  52. int local_size[3]; /* Compute shader workgroup sizes */
  53. VkPipelineShaderStageCreateInfo shader;
  54. } SPIRVShader;
  55. typedef struct VulkanDescriptorSetBinding {
  56. const char *name;
  57. VkDescriptorType type;
  58. const char *mem_layout; /* Storage images (rgba8, etc.) and buffers (std430, etc.) */
  59. const char *mem_quali; /* readonly, writeonly, etc. */
  60. const char *buf_content; /* For buffers */
  61. uint32_t dimensions; /* Needed for e.g. sampler%iD */
  62. uint32_t elems; /* 0 - scalar, 1 or more - vector */
  63. VkShaderStageFlags stages;
  64. const VkSampler *samplers; /* Immutable samplers, length - #elems */
  65. void *updater; /* Pointer to VkDescriptor*Info */
  66. } VulkanDescriptorSetBinding;
  67. typedef struct FFVkBuffer {
  68. VkBuffer buf;
  69. VkDeviceMemory mem;
  70. VkMemoryPropertyFlagBits flags;
  71. } FFVkBuffer;
  72. typedef struct VulkanPipeline {
  73. VkPipelineBindPoint bind_point;
  74. /* Contexts */
  75. VkPipelineLayout pipeline_layout;
  76. VkPipeline pipeline;
  77. /* Shaders */
  78. SPIRVShader **shaders;
  79. int shaders_num;
  80. /* Push consts */
  81. VkPushConstantRange *push_consts;
  82. int push_consts_num;
  83. /* Descriptors */
  84. VkDescriptorSetLayout *desc_layout;
  85. VkDescriptorPool desc_pool;
  86. VkDescriptorSet *desc_set;
  87. VkDescriptorUpdateTemplate *desc_template;
  88. int descriptor_sets_num;
  89. int pool_size_desc_num;
  90. /* Temporary, used to store data in between initialization stages */
  91. VkDescriptorUpdateTemplateCreateInfo *desc_template_info;
  92. VkDescriptorPoolSize *pool_size_desc;
  93. } VulkanPipeline;
  94. typedef struct FFVkExecContext {
  95. VkCommandPool pool;
  96. VkCommandBuffer buf;
  97. VkQueue queue;
  98. VkFence fence;
  99. VulkanPipeline *bound_pl;
  100. VkSemaphore *sem_wait;
  101. int sem_wait_alloc; /* Allocated sem_wait */
  102. int sem_wait_cnt;
  103. VkPipelineStageFlagBits *sem_wait_dst;
  104. int sem_wait_dst_alloc; /* Allocated sem_wait_dst */
  105. VkSemaphore *sem_sig;
  106. int sem_sig_alloc; /* Allocated sem_sig */
  107. int sem_sig_cnt;
  108. } FFVkExecContext;
  109. typedef struct VulkanFilterContext {
  110. const AVClass *class;
  111. AVBufferRef *device_ref;
  112. AVBufferRef *frames_ref; /* For in-place filtering */
  113. AVHWDeviceContext *device;
  114. AVVulkanDeviceContext *hwctx;
  115. /* Properties */
  116. int output_width;
  117. int output_height;
  118. enum AVPixelFormat output_format;
  119. enum AVPixelFormat input_format;
  120. /* Samplers */
  121. VkSampler **samplers;
  122. int samplers_num;
  123. /* Exec contexts */
  124. FFVkExecContext **exec_ctx;
  125. int exec_ctx_num;
  126. /* Pipelines (each can have 1 shader of each type) */
  127. VulkanPipeline **pipelines;
  128. int pipelines_num;
  129. void *scratch; /* Scratch memory used only in functions */
  130. unsigned int scratch_size;
  131. } VulkanFilterContext;
  132. /* Identity mapping - r = r, b = b, g = g, a = a */
  133. extern const VkComponentMapping ff_comp_identity_map;
  134. /**
  135. * General lavfi IO functions
  136. */
  137. int ff_vk_filter_query_formats (AVFilterContext *avctx);
  138. int ff_vk_filter_init (AVFilterContext *avctx);
  139. int ff_vk_filter_config_input (AVFilterLink *inlink);
  140. int ff_vk_filter_config_output (AVFilterLink *outlink);
  141. int ff_vk_filter_config_output_inplace(AVFilterLink *outlink);
  142. void ff_vk_filter_uninit (AVFilterContext *avctx);
  143. /**
  144. * Converts Vulkan return values to strings
  145. */
  146. const char *ff_vk_ret2str(VkResult res);
  147. /**
  148. * Gets the glsl format string for a pixel format
  149. */
  150. const char *ff_vk_shader_rep_fmt(enum AVPixelFormat pixfmt);
  151. /**
  152. * Create a Vulkan sampler, will be auto-freed in ff_vk_filter_uninit()
  153. */
  154. VkSampler *ff_vk_init_sampler(AVFilterContext *avctx, int unnorm_coords,
  155. VkFilter filt);
  156. /**
  157. * Create an imageview.
  158. */
  159. int ff_vk_create_imageview(AVFilterContext *avctx, VkImageView *v, VkImage img,
  160. VkFormat fmt, const VkComponentMapping map);
  161. /**
  162. * Destroy an imageview. Command buffer must have completed executing, which
  163. * ff_vk_submit_exec_queue() will ensure
  164. */
  165. void ff_vk_destroy_imageview(AVFilterContext *avctx, VkImageView *v);
  166. /**
  167. * Define a push constant for a given stage into a pipeline.
  168. * Must be called before the pipeline layout has been initialized.
  169. */
  170. int ff_vk_add_push_constant(AVFilterContext *avctx, VulkanPipeline *pl,
  171. int offset, int size, VkShaderStageFlagBits stage);
  172. /**
  173. * Inits a pipeline. Everything in it will be auto-freed when calling
  174. * ff_vk_filter_uninit().
  175. */
  176. VulkanPipeline *ff_vk_create_pipeline(AVFilterContext *avctx);
  177. /**
  178. * Inits a shader for a specific pipeline. Will be auto-freed on uninit.
  179. */
  180. SPIRVShader *ff_vk_init_shader(AVFilterContext *avctx, VulkanPipeline *pl,
  181. const char *name, VkShaderStageFlags stage);
  182. /**
  183. * Writes the workgroup size for a shader.
  184. */
  185. void ff_vk_set_compute_shader_sizes(AVFilterContext *avctx, SPIRVShader *shd,
  186. int local_size[3]);
  187. /**
  188. * Adds a descriptor set to the shader and registers them in the pipeline.
  189. */
  190. int ff_vk_add_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,
  191. SPIRVShader *shd, VulkanDescriptorSetBinding *desc,
  192. int num, int only_print_to_shader);
  193. /**
  194. * Compiles the shader, entrypoint must be set to "main".
  195. */
  196. int ff_vk_compile_shader(AVFilterContext *avctx, SPIRVShader *shd,
  197. const char *entrypoint);
  198. /**
  199. * Initializes the pipeline layout after all shaders and descriptor sets have
  200. * been finished.
  201. */
  202. int ff_vk_init_pipeline_layout(AVFilterContext *avctx, VulkanPipeline *pl);
  203. /**
  204. * Initializes a compute pipeline. Will pick the first shader with the
  205. * COMPUTE flag set.
  206. */
  207. int ff_vk_init_compute_pipeline(AVFilterContext *avctx, VulkanPipeline *pl);
  208. /**
  209. * Updates a descriptor set via the updaters defined.
  210. * Can be called immediately after pipeline creation, but must be called
  211. * at least once before queue submission.
  212. */
  213. void ff_vk_update_descriptor_set(AVFilterContext *avctx, VulkanPipeline *pl,
  214. int set_id);
  215. /**
  216. * Init an execution context for command recording and queue submission.
  217. * WIll be auto-freed on uninit.
  218. */
  219. int ff_vk_create_exec_ctx(AVFilterContext *avctx, FFVkExecContext **ctx, int queue);
  220. /**
  221. * Begin recording to the command buffer. Previous execution must have been
  222. * completed, which ff_vk_submit_exec_queue() will ensure.
  223. */
  224. int ff_vk_start_exec_recording(AVFilterContext *avctx, FFVkExecContext *e);
  225. /**
  226. * Add a command to bind the completed pipeline and its descriptor sets.
  227. * Must be called after ff_vk_start_exec_recording() and before submission.
  228. */
  229. void ff_vk_bind_pipeline_exec(AVFilterContext *avctx, FFVkExecContext *e,
  230. VulkanPipeline *pl);
  231. /**
  232. * Updates push constants.
  233. * Must be called after binding a pipeline if any push constants were defined.
  234. */
  235. void ff_vk_update_push_exec(AVFilterContext *avctx, FFVkExecContext *e,
  236. VkShaderStageFlagBits stage, int offset,
  237. size_t size, void *src);
  238. /**
  239. * Adds a frame as a queue dependency. This manages semaphore signalling.
  240. * Must be called before submission.
  241. */
  242. int ff_vk_add_exec_dep(AVFilterContext *avctx, FFVkExecContext *e,
  243. AVFrame *frame, VkPipelineStageFlagBits in_wait_dst_flag);
  244. /**
  245. * Submits a command buffer to the queue for execution.
  246. * Will block until execution has finished in order to simplify resource
  247. * management.
  248. */
  249. int ff_vk_submit_exec_queue(AVFilterContext *avctx, FFVkExecContext *e);
  250. /**
  251. * Create a VkBuffer with the specified parameters.
  252. */
  253. int ff_vk_create_buf(AVFilterContext *avctx, FFVkBuffer *buf, size_t size,
  254. VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags);
  255. /**
  256. * Maps the buffer to userspace. Set invalidate to 1 if reading the contents
  257. * is necessary.
  258. */
  259. int ff_vk_map_buffers(AVFilterContext *avctx, FFVkBuffer *buf, uint8_t *mem[],
  260. int nb_buffers, int invalidate);
  261. /**
  262. * Unmaps the buffer from userspace. Set flush to 1 to write and sync.
  263. */
  264. int ff_vk_unmap_buffers(AVFilterContext *avctx, FFVkBuffer *buf, int nb_buffers,
  265. int flush);
  266. /**
  267. * Frees a buffer.
  268. */
  269. void ff_vk_free_buf(AVFilterContext *avctx, FFVkBuffer *buf);
  270. #endif /* AVFILTER_VULKAN_H */