21 #define VK_NO_PROTOTYPES
22 #define VK_ENABLE_BETA_EXTENSIONS
26 #include <versionhelpers.h>
53 #include <va/va_drmcommon.h>
56 #include <sys/sysmacros.h>
60 #include <drm_fourcc.h>
64 #if HAVE_LINUX_DMA_BUF_H
65 #include <sys/ioctl.h>
66 #include <linux/dma-buf.h>
72 #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
84 #ifdef VK_KHR_video_maintenance2
85 VkPhysicalDeviceVideoMaintenance2FeaturesKHR video_maintenance_2;
95 #ifdef VK_KHR_shader_relaxed_extended_instruction
96 VkPhysicalDeviceShaderRelaxedExtendedInstructionFeaturesKHR relaxed_extended_instruction;
116 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
hprops;
189 #define OPT_CHAIN(STRUCT_P, EXT_FLAG, TYPE) \
191 if ((EXT_FLAG == FF_VK_EXT_NO_FLAG) || \
192 (p->vkctx.extensions & EXT_FLAG)) { \
193 (STRUCT_P)->sType = TYPE; \
194 ff_vk_link_struct(&feats->device, STRUCT_P); \
198 feats->
device = (VkPhysicalDeviceFeatures2) {
199 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
203 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES);
205 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES);
207 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES);
210 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES);
213 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VIDEO_MAINTENANCE_1_FEATURES_KHR);
214 #ifdef VK_KHR_video_maintenance2
216 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VIDEO_MAINTENANCE_2_FEATURES_KHR);
220 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_OBJECT_FEATURES_EXT);
222 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR);
224 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_FEATURES_EXT);
226 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_FLOAT_FEATURES_EXT);
228 #ifdef VK_KHR_shader_relaxed_extended_instruction
230 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_RELAXED_EXTENDED_INSTRUCTION_FEATURES_KHR);
234 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_OPTICAL_FLOW_FEATURES_NV);
241 #define COPY_VAL(VAL) \
243 dst->VAL = src->VAL; \
246 COPY_VAL(device.features.shaderImageGatherExtended);
247 COPY_VAL(device.features.shaderStorageImageReadWithoutFormat);
248 COPY_VAL(device.features.shaderStorageImageWriteWithoutFormat);
249 COPY_VAL(device.features.fragmentStoresAndAtomics);
250 COPY_VAL(device.features.vertexPipelineStoresAndAtomics);
251 COPY_VAL(device.features.shaderInt64);
252 COPY_VAL(device.features.shaderInt16);
253 COPY_VAL(device.features.shaderFloat64);
254 COPY_VAL(device.features.shaderStorageImageReadWithoutFormat);
255 COPY_VAL(device.features.shaderStorageImageWriteWithoutFormat);
257 COPY_VAL(vulkan_1_1.samplerYcbcrConversion);
258 COPY_VAL(vulkan_1_1.storagePushConstant16);
259 COPY_VAL(vulkan_1_1.storageBuffer16BitAccess);
260 COPY_VAL(vulkan_1_1.uniformAndStorageBuffer16BitAccess);
262 COPY_VAL(vulkan_1_2.timelineSemaphore);
263 COPY_VAL(vulkan_1_2.scalarBlockLayout);
264 COPY_VAL(vulkan_1_2.bufferDeviceAddress);
265 COPY_VAL(vulkan_1_2.hostQueryReset);
266 COPY_VAL(vulkan_1_2.storagePushConstant8);
268 COPY_VAL(vulkan_1_2.storageBuffer8BitAccess);
269 COPY_VAL(vulkan_1_2.uniformAndStorageBuffer8BitAccess);
271 COPY_VAL(vulkan_1_2.shaderBufferInt64Atomics);
272 COPY_VAL(vulkan_1_2.shaderSharedInt64Atomics);
273 COPY_VAL(vulkan_1_2.vulkanMemoryModel);
274 COPY_VAL(vulkan_1_2.vulkanMemoryModelDeviceScope);
276 COPY_VAL(vulkan_1_3.dynamicRendering);
278 COPY_VAL(vulkan_1_3.synchronization2);
279 COPY_VAL(vulkan_1_3.computeFullSubgroups);
280 COPY_VAL(vulkan_1_3.shaderZeroInitializeWorkgroupMemory);
281 COPY_VAL(vulkan_1_3.dynamicRendering);
283 COPY_VAL(timeline_semaphore.timelineSemaphore);
285 COPY_VAL(video_maintenance_1.videoMaintenance1);
286 #ifdef VK_KHR_video_maintenance2
287 COPY_VAL(video_maintenance_2.videoMaintenance2);
290 COPY_VAL(shader_object.shaderObject);
292 COPY_VAL(cooperative_matrix.cooperativeMatrix);
294 COPY_VAL(descriptor_buffer.descriptorBuffer);
295 COPY_VAL(descriptor_buffer.descriptorBufferPushDescriptors);
297 COPY_VAL(atomic_float.shaderBufferFloat32Atomics);
298 COPY_VAL(atomic_float.shaderBufferFloat32AtomicAdd);
300 #ifdef VK_KHR_shader_relaxed_extended_instruction
301 COPY_VAL(relaxed_extended_instruction.shaderRelaxedExtendedInstruction);
308 #define ASPECT_2PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT)
309 #define ASPECT_3PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)
321 { VK_FORMAT_R8_UNORM,
AV_PIX_FMT_GRAY8, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8_UNORM } },
322 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GRAY10, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16_UNORM } },
323 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GRAY12, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16_UNORM } },
324 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GRAY14, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16_UNORM } },
325 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GRAY16, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16_UNORM } },
326 { VK_FORMAT_R32_UINT,
AV_PIX_FMT_GRAY32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32_UINT } },
327 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GRAYF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32_SFLOAT } },
330 { VK_FORMAT_B8G8R8A8_UNORM,
AV_PIX_FMT_BGRA, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8A8_UNORM } },
331 { VK_FORMAT_R8G8B8A8_UNORM,
AV_PIX_FMT_RGBA, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
332 { VK_FORMAT_R8G8B8_UNORM,
AV_PIX_FMT_RGB24, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8_UNORM } },
333 { VK_FORMAT_B8G8R8_UNORM,
AV_PIX_FMT_BGR24, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8_UNORM } },
334 { VK_FORMAT_R16G16B16_UNORM,
AV_PIX_FMT_RGB48, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16_UNORM } },
335 { VK_FORMAT_R16G16B16A16_UNORM,
AV_PIX_FMT_RGBA64, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
336 { VK_FORMAT_R5G6B5_UNORM_PACK16,
AV_PIX_FMT_RGB565, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R5G6B5_UNORM_PACK16 } },
337 { VK_FORMAT_B5G6R5_UNORM_PACK16,
AV_PIX_FMT_BGR565, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B5G6R5_UNORM_PACK16 } },
338 { VK_FORMAT_B8G8R8A8_UNORM,
AV_PIX_FMT_BGR0, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8A8_UNORM } },
339 { VK_FORMAT_R8G8B8A8_UNORM,
AV_PIX_FMT_RGB0, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
340 { VK_FORMAT_A2R10G10B10_UNORM_PACK32,
AV_PIX_FMT_X2RGB10, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_A2R10G10B10_UNORM_PACK32 } },
341 { VK_FORMAT_A2B10G10R10_UNORM_PACK32,
AV_PIX_FMT_X2BGR10, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_A2B10G10R10_UNORM_PACK32 } },
342 { VK_FORMAT_R32G32B32_SFLOAT,
AV_PIX_FMT_RGBF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32G32B32_SFLOAT } },
343 { VK_FORMAT_R32G32B32A32_SFLOAT,
AV_PIX_FMT_RGBAF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32G32B32A32_SFLOAT } },
344 { VK_FORMAT_R32G32B32_UINT,
AV_PIX_FMT_RGB96, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32G32B32_UINT } },
345 { VK_FORMAT_R32G32B32A32_UINT,
AV_PIX_FMT_RGBA128, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32G32B32A32_UINT } },
348 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRP10, VK_IMAGE_ASPECT_COLOR_BIT, 3, 3, 3, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
349 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRP12, VK_IMAGE_ASPECT_COLOR_BIT, 3, 3, 3, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
350 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRP14, VK_IMAGE_ASPECT_COLOR_BIT, 3, 3, 3, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
351 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRP16, VK_IMAGE_ASPECT_COLOR_BIT, 3, 3, 3, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
352 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GBRPF32, VK_IMAGE_ASPECT_COLOR_BIT, 3, 3, 3, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
355 { VK_FORMAT_R8_UNORM,
AV_PIX_FMT_GBRAP, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
356 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRAP10, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
357 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRAP12, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
358 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRAP14, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
359 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRAP16, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
360 { VK_FORMAT_R32_UINT,
AV_PIX_FMT_GBRAP32, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R32_UINT, VK_FORMAT_R32_UINT, VK_FORMAT_R32_UINT, VK_FORMAT_R32_UINT } },
361 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GBRAPF32, VK_IMAGE_ASPECT_COLOR_BIT, 4, 4, 4, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
365 { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,
AV_PIX_FMT_P010,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
366 { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16,
AV_PIX_FMT_P012,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
371 { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16,
AV_PIX_FMT_P210,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
372 { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16,
AV_PIX_FMT_P212,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
377 { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16,
AV_PIX_FMT_P410,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
378 { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16,
AV_PIX_FMT_P412,
ASPECT_2PLANE, 2, 1, 2, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
396 { VK_FORMAT_G8B8G8R8_422_UNORM,
AV_PIX_FMT_YUYV422, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
397 { VK_FORMAT_B8G8R8G8_422_UNORM,
AV_PIX_FMT_UYVY422, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
398 { VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16,
AV_PIX_FMT_Y210, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
399 { VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16,
AV_PIX_FMT_Y212, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
400 { VK_FORMAT_G16B16G16R16_422_UNORM,
AV_PIX_FMT_Y216, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
403 { VK_FORMAT_B8G8R8A8_UNORM,
AV_PIX_FMT_UYVA, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8A8_UNORM } },
404 { VK_FORMAT_A2R10G10B10_UNORM_PACK32,
AV_PIX_FMT_XV30, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
405 { VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16,
AV_PIX_FMT_XV36, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
406 { VK_FORMAT_R16G16B16A16_UNORM,
AV_PIX_FMT_XV48, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
427 #define FN_MAP_TO(dst_t, dst_name, src_t, src_name) \
428 static av_unused dst_t map_ ##src_name## _to_ ##dst_name(src_t src) \
431 MAP_TO(VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT, \
432 VK_IMAGE_USAGE_SAMPLED_BIT); \
433 MAP_TO(VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT, \
434 VK_IMAGE_USAGE_TRANSFER_SRC_BIT); \
435 MAP_TO(VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT, \
436 VK_IMAGE_USAGE_TRANSFER_DST_BIT); \
437 MAP_TO(VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT, \
438 VK_IMAGE_USAGE_STORAGE_BIT); \
439 MAP_TO(VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT, \
440 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); \
441 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR, \
442 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR); \
443 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR, \
444 VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR); \
445 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_ENCODE_DPB_BIT_KHR, \
446 VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR); \
447 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_ENCODE_INPUT_BIT_KHR, \
448 VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR); \
452 #define MAP_TO(flag1, flag2) if (src & flag2) dst |= flag1;
453 FN_MAP_TO(VkFormatFeatureFlagBits2, feats, VkImageUsageFlags,
usage)
455 #define MAP_TO(flag1, flag2) if (src & flag1) dst |= flag2;
456 FN_MAP_TO(VkImageUsageFlags,
usage, VkFormatFeatureFlagBits2, feats)
461 VkImageTiling tiling,
464 VkImageAspectFlags *
aspect,
465 VkImageUsageFlags *supported_usage,
466 int disable_multiplane,
int need_storage)
472 const VkFormatFeatureFlagBits2 basic_flags = VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
473 VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
474 VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
478 VkFormatProperties3 fprops = {
479 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3,
481 VkFormatProperties2 prop = {
482 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
485 VkFormatFeatureFlagBits2 feats_primary, feats_secondary;
486 int basics_primary = 0, basics_secondary = 0;
487 int storage_primary = 0, storage_secondary = 0;
489 vk->GetPhysicalDeviceFormatProperties2(hwctx->
phys_dev,
493 feats_primary = tiling == VK_IMAGE_TILING_LINEAR ?
494 fprops.linearTilingFeatures : fprops.optimalTilingFeatures;
495 basics_primary = (feats_primary & basic_flags) == basic_flags;
496 storage_primary = !!(feats_primary & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT);
499 vk->GetPhysicalDeviceFormatProperties2(hwctx->
phys_dev,
502 feats_secondary = tiling == VK_IMAGE_TILING_LINEAR ?
503 fprops.linearTilingFeatures : fprops.optimalTilingFeatures;
504 basics_secondary = (feats_secondary & basic_flags) == basic_flags;
505 storage_secondary = !!(feats_secondary & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT);
507 basics_secondary = basics_primary;
508 storage_secondary = storage_primary;
511 if (basics_primary &&
513 (!need_storage || (need_storage && (storage_primary | storage_secondary)))) {
527 *supported_usage = map_feats_to_usage(feats_primary) |
528 ((need_storage && (storage_primary | storage_secondary)) ?
529 VK_IMAGE_USAGE_STORAGE_BIT : 0);
531 }
else if (basics_secondary &&
532 (!need_storage || (need_storage && storage_secondary))) {
542 *supported_usage = map_feats_to_usage(feats_secondary);
553 #if CONFIG_VULKAN_STATIC
554 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance,
563 #if CONFIG_VULKAN_STATIC
566 static const char *lib_names[] = {
569 #elif defined(__APPLE__)
580 p->
libvulkan = dlopen(lib_names[
i], RTLD_NOW | RTLD_LOCAL);
619 #ifdef VK_KHR_video_maintenance2
645 static VkBool32 VKAPI_CALL
vk_dbg_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
646 VkDebugUtilsMessageTypeFlagsEXT messageType,
647 const VkDebugUtilsMessengerCallbackDataEXT *
data,
654 switch (
data->messageIdNumber) {
666 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: l =
AV_LOG_VERBOSE;
break;
667 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: l =
AV_LOG_INFO;
break;
668 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: l =
AV_LOG_WARNING;
break;
669 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: l =
AV_LOG_ERROR;
break;
674 for (
int i = 0;
i <
data->cmdBufLabelCount;
i++)
680 #define ADD_VAL_TO_LIST(list, count, val) \
682 list = av_realloc_array(list, sizeof(*list), ++count); \
684 err = AVERROR(ENOMEM); \
687 list[count - 1] = av_strdup(val); \
688 if (!list[count - 1]) { \
689 err = AVERROR(ENOMEM); \
694 #define RELEASE_PROPS(props, count) \
696 for (int i = 0; i < count; i++) \
697 av_free((void *)((props)[i])); \
698 av_free((void *)props); \
716 const char *
const **
dst, uint32_t *num,
720 const char **extension_names =
NULL;
724 int err = 0, found, extensions_found = 0;
727 int optional_exts_num;
728 uint32_t sup_ext_count;
729 char *user_exts_str =
NULL;
731 VkExtensionProperties *sup_ext;
741 if (!user_exts_str) {
746 vk->EnumerateInstanceExtensionProperties(
NULL, &sup_ext_count,
NULL);
747 sup_ext =
av_malloc_array(sup_ext_count,
sizeof(VkExtensionProperties));
750 vk->EnumerateInstanceExtensionProperties(
NULL, &sup_ext_count, sup_ext);
758 if (!user_exts_str) {
763 vk->EnumerateDeviceExtensionProperties(hwctx->
phys_dev,
NULL,
764 &sup_ext_count,
NULL);
765 sup_ext =
av_malloc_array(sup_ext_count,
sizeof(VkExtensionProperties));
768 vk->EnumerateDeviceExtensionProperties(hwctx->
phys_dev,
NULL,
769 &sup_ext_count, sup_ext);
772 for (
int i = 0;
i < optional_exts_num;
i++) {
773 tstr = optional_exts[
i].
name;
780 !strcmp(tstr, VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME)) {
784 for (
int j = 0; j < sup_ext_count; j++) {
785 if (!strcmp(tstr, sup_ext[j].extensionName)) {
802 tstr = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
804 for (
int j = 0; j < sup_ext_count; j++) {
805 if (!strcmp(tstr, sup_ext[j].extensionName)) {
821 #ifdef VK_KHR_shader_relaxed_extended_instruction
824 tstr = VK_KHR_SHADER_RELAXED_EXTENDED_INSTRUCTION_EXTENSION_NAME;
826 for (
int j = 0; j < sup_ext_count; j++) {
827 if (!strcmp(tstr, sup_ext[j].extensionName)) {
845 char *save, *token =
av_strtok(user_exts_str,
"+", &save);
848 for (
int j = 0; j < sup_ext_count; j++) {
849 if (!strcmp(token, sup_ext[j].extensionName)) {
865 *
dst = extension_names;
866 *num = extensions_found;
880 const char *
const **
dst, uint32_t *num,
887 static const char layer_standard_validation[] = {
"VK_LAYER_KHRONOS_validation" };
888 int layer_standard_validation_found = 0;
890 uint32_t sup_layer_count;
891 VkLayerProperties *sup_layers;
894 char *user_layers_str =
NULL;
897 const char **enabled_layers =
NULL;
898 uint32_t enabled_layers_count = 0;
906 vk->EnumerateInstanceLayerProperties(&sup_layer_count,
NULL);
907 sup_layers =
av_malloc_array(sup_layer_count,
sizeof(VkLayerProperties));
910 vk->EnumerateInstanceLayerProperties(&sup_layer_count, sup_layers);
913 for (
int i = 0;
i < sup_layer_count;
i++)
917 if (!debug_opt && !user_layers)
922 if (!strcmp(debug_opt->
value,
"profile")) {
924 }
else if (!strcmp(debug_opt->
value,
"printf")) {
926 }
else if (!strcmp(debug_opt->
value,
"validate")) {
928 }
else if (!strcmp(debug_opt->
value,
"practices")) {
931 char *end_ptr =
NULL;
932 int idx = strtol(debug_opt->
value, &end_ptr, 10);
933 if (end_ptr == debug_opt->
value || end_ptr[0] !=
'\0' ||
948 for (
int i = 0;
i < sup_layer_count;
i++) {
949 if (!strcmp(layer_standard_validation, sup_layers[
i].layerName)) {
951 layer_standard_validation);
952 ADD_VAL_TO_LIST(enabled_layers, enabled_layers_count, layer_standard_validation);
954 layer_standard_validation_found = 1;
958 if (!layer_standard_validation_found) {
960 "Validation Layer \"%s\" not supported\n", layer_standard_validation);
973 if (!user_layers_str) {
978 token =
av_strtok(user_layers_str,
"+", &save);
983 if (!strcmp(layer_standard_validation, token) && layer_standard_validation_found) {
989 for (
int j = 0; j < sup_layer_count; j++) {
990 if (!strcmp(token, sup_layers[j].layerName)) {
1001 if (!strcmp(layer_standard_validation, token))
1005 "Layer \"%s\" not supported\n", token);
1022 *
dst = enabled_layers;
1023 *num = enabled_layers_count;
1038 VkApplicationInfo application_info = {
1039 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
1040 .pApplicationName =
"ffmpeg",
1044 .pEngineName =
"libavutil",
1045 .apiVersion = VK_API_VERSION_1_3,
1050 VkValidationFeaturesEXT validation_features = {
1051 .sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT,
1053 VkInstanceCreateInfo inst_props = {
1054 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
1055 .pApplicationInfo = &application_info,
1071 &inst_props.enabledLayerCount, debug_mode);
1077 &inst_props.enabledExtensionCount, *debug_mode);
1085 static const VkValidationFeatureEnableEXT feat_list_validate[] = {
1086 VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT,
1087 VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT,
1088 VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,
1090 validation_features.pEnabledValidationFeatures = feat_list_validate;
1091 validation_features.enabledValidationFeatureCount =
FF_ARRAY_ELEMS(feat_list_validate);
1092 inst_props.pNext = &validation_features;
1094 static const VkValidationFeatureEnableEXT feat_list_debug[] = {
1095 VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT,
1096 VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT,
1097 VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT,
1099 validation_features.pEnabledValidationFeatures = feat_list_debug;
1100 validation_features.enabledValidationFeatureCount =
FF_ARRAY_ELEMS(feat_list_debug);
1101 inst_props.pNext = &validation_features;
1103 static const VkValidationFeatureEnableEXT feat_list_practices[] = {
1104 VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT,
1105 VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT,
1107 validation_features.pEnabledValidationFeatures = feat_list_practices;
1108 validation_features.enabledValidationFeatureCount =
FF_ARRAY_ELEMS(feat_list_practices);
1109 inst_props.pNext = &validation_features;
1113 for (
int i = 0;
i < inst_props.enabledExtensionCount;
i++) {
1114 if (!strcmp(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
1115 inst_props.ppEnabledExtensionNames[
i])) {
1116 inst_props.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
1123 ret = vk->CreateInstance(&inst_props, hwctx->
alloc, &hwctx->
inst);
1126 if (
ret != VK_SUCCESS) {
1143 VkDebugUtilsMessengerCreateInfoEXT dbg = {
1144 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
1145 .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
1146 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
1147 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
1148 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
1149 .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
1150 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
1151 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
1156 vk->CreateDebugUtilsMessengerEXT(hwctx->
inst, &dbg,
1163 RELEASE_PROPS(inst_props.ppEnabledLayerNames, inst_props.enabledLayerCount);
1182 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
return "integrated";
1183 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
return "discrete";
1184 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
return "virtual";
1185 case VK_PHYSICAL_DEVICE_TYPE_CPU:
return "software";
1186 default:
return "unknown";
1193 int err = 0, choice = -1;
1199 VkPhysicalDevice *devices =
NULL;
1200 VkPhysicalDeviceIDProperties *idp =
NULL;
1201 VkPhysicalDeviceProperties2 *prop =
NULL;
1202 VkPhysicalDeviceDrmPropertiesEXT *drm_prop =
NULL;
1204 ret = vk->EnumeratePhysicalDevices(hwctx->
inst, &num,
NULL);
1205 if (
ret != VK_SUCCESS || !num) {
1214 ret = vk->EnumeratePhysicalDevices(hwctx->
inst, &num, devices);
1215 if (
ret != VK_SUCCESS) {
1235 drm_prop =
av_calloc(num,
sizeof(*drm_prop));
1243 for (
int i = 0;
i < num;
i++) {
1245 drm_prop[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT;
1246 idp[
i].pNext = &drm_prop[
i];
1248 idp[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
1249 prop[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
1250 prop[
i].pNext = &idp[
i];
1252 vk->GetPhysicalDeviceProperties2(devices[
i], &prop[
i]);
1254 prop[
i].properties.deviceName,
1256 prop[
i].properties.deviceID);
1260 for (
int i = 0;
i < num;
i++) {
1261 if (!strncmp(idp[
i].deviceUUID, select->
uuid, VK_UUID_SIZE)) {
1270 for (
int i = 0;
i < num;
i++) {
1271 if ((select->
drm_major == drm_prop[
i].primaryMajor &&
1272 select->
drm_minor == drm_prop[
i].primaryMinor) ||
1273 (select->
drm_major == drm_prop[
i].renderMajor &&
1274 select->
drm_minor == drm_prop[
i].renderMinor)) {
1283 }
else if (select->
name) {
1285 for (
int i = 0;
i < num;
i++) {
1286 if (strstr(prop[
i].properties.deviceName, select->
name)) {
1297 for (
int i = 0;
i < num;
i++) {
1298 if (select->
pci_device == prop[
i].properties.deviceID) {
1309 for (
int i = 0;
i < num;
i++) {
1310 if (select->
vendor_id == prop[
i].properties.vendorID) {
1320 if (select->
index < num) {
1321 choice = select->
index;
1333 choice, prop[choice].properties.deviceName,
1335 prop[choice].properties.deviceID);
1349 VkQueueFlagBits
flags)
1352 uint32_t min_score = UINT32_MAX;
1354 for (
int i = 0;
i < num_qf;
i++) {
1355 VkQueueFlagBits qflags = qf[
i].queueFamilyProperties.queueFlags;
1358 if ((
flags & VK_QUEUE_TRANSFER_BIT) &&
1359 (qflags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT)))
1360 qflags |= VK_QUEUE_TRANSFER_BIT;
1362 if (qflags &
flags) {
1363 uint32_t score =
av_popcount(qflags) + qf[
i].queueFamilyProperties.timestampValidBits;
1364 if (score < min_score) {
1372 qf[
index].queueFamilyProperties.timestampValidBits++;
1378 VkQueueFamilyVideoPropertiesKHR *qf_vid, uint32_t num_qf,
1379 VkVideoCodecOperationFlagBitsKHR
flags)
1382 uint32_t min_score = UINT32_MAX;
1384 for (
int i = 0;
i < num_qf;
i++) {
1385 const VkQueueFlagBits qflags = qf[
i].queueFamilyProperties.queueFlags;
1386 const VkQueueFlagBits vflags = qf_vid[
i].videoCodecOperations;
1388 if (!(qflags & (VK_QUEUE_VIDEO_ENCODE_BIT_KHR | VK_QUEUE_VIDEO_DECODE_BIT_KHR)))
1391 if (vflags &
flags) {
1392 uint32_t score =
av_popcount(vflags) + qf[
i].queueFamilyProperties.timestampValidBits;
1393 if (score < min_score) {
1401 qf[
index].queueFamilyProperties.timestampValidBits++;
1413 VkQueueFamilyProperties2 *qf =
NULL;
1414 VkQueueFamilyVideoPropertiesKHR *qf_vid =
NULL;
1417 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &num,
NULL);
1428 qf_vid =
av_malloc_array(num,
sizeof(VkQueueFamilyVideoPropertiesKHR));
1432 for (uint32_t
i = 0;
i < num;
i++) {
1433 qf_vid[
i] = (VkQueueFamilyVideoPropertiesKHR) {
1434 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_VIDEO_PROPERTIES_KHR,
1436 qf[
i] = (VkQueueFamilyProperties2) {
1437 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2,
1443 vk->GetPhysicalDeviceQueueFamilyProperties2(hwctx->
phys_dev, &num, qf);
1446 for (
int i = 0;
i < num;
i++) {
1448 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_GRAPHICS_BIT) ?
" graphics" :
"",
1449 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_COMPUTE_BIT) ?
" compute" :
"",
1450 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_TRANSFER_BIT) ?
" transfer" :
"",
1451 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_VIDEO_ENCODE_BIT_KHR) ?
" encode" :
"",
1452 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_VIDEO_DECODE_BIT_KHR) ?
" decode" :
"",
1453 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_SPARSE_BINDING_BIT) ?
" sparse" :
"",
1454 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_OPTICAL_FLOW_BIT_NV) ?
" optical_flow" :
"",
1455 ((qf[
i].queueFamilyProperties.queueFlags) & VK_QUEUE_PROTECTED_BIT) ?
" protected" :
"",
1456 qf[
i].queueFamilyProperties.queueCount);
1460 qf[
i].queueFamilyProperties.timestampValidBits = 0;
1466 #define PICK_QF(type, vid_op) \
1472 idx = pick_video_queue_family(qf, qf_vid, num, vid_op); \
1474 idx = pick_queue_family(qf, num, type); \
1479 for (i = 0; i < hwctx->nb_qf; i++) { \
1480 if (hwctx->qf[i].idx == idx) { \
1481 hwctx->qf[i].flags |= type; \
1482 hwctx->qf[i].video_caps |= vid_op; \
1486 if (i == hwctx->nb_qf) { \
1487 hwctx->qf[i].idx = idx; \
1488 hwctx->qf[i].num = qf[idx].queueFamilyProperties.queueCount; \
1489 hwctx->qf[i].flags = type; \
1490 hwctx->qf[i].video_caps = vid_op; \
1495 PICK_QF(VK_QUEUE_GRAPHICS_BIT, VK_VIDEO_CODEC_OPERATION_NONE_KHR);
1496 PICK_QF(VK_QUEUE_COMPUTE_BIT, VK_VIDEO_CODEC_OPERATION_NONE_KHR);
1497 PICK_QF(VK_QUEUE_TRANSFER_BIT, VK_VIDEO_CODEC_OPERATION_NONE_KHR);
1498 PICK_QF(VK_QUEUE_OPTICAL_FLOW_BIT_NV, VK_VIDEO_CODEC_OPERATION_NONE_KHR);
1500 PICK_QF(VK_QUEUE_VIDEO_ENCODE_BIT_KHR, VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR);
1501 PICK_QF(VK_QUEUE_VIDEO_DECODE_BIT_KHR, VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR);
1503 PICK_QF(VK_QUEUE_VIDEO_ENCODE_BIT_KHR, VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR);
1504 PICK_QF(VK_QUEUE_VIDEO_DECODE_BIT_KHR, VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR);
1506 PICK_QF(VK_QUEUE_VIDEO_DECODE_BIT_KHR, VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR);
1514 sizeof(VkDeviceQueueCreateInfo));
1515 if (!cd->pQueueCreateInfos)
1518 for (uint32_t
i = 0;
i < hwctx->
nb_qf;
i++) {
1521 VkDeviceQueueCreateInfo *pc;
1522 for (uint32_t j = 0; j < cd->queueCreateInfoCount; j++) {
1523 if (hwctx->
qf[
i].
idx == cd->pQueueCreateInfos[j].queueFamilyIndex) {
1533 for (uint32_t j = 0; j < cd->queueCreateInfoCount; j++)
1534 av_free((
void *)cd->pQueueCreateInfos[
i].pQueuePriorities);
1535 av_free((
void *)cd->pQueueCreateInfos);
1539 for (uint32_t j = 0; j < hwctx->
qf[
i].
num; j++)
1542 pc = (VkDeviceQueueCreateInfo *)cd->pQueueCreateInfos;
1543 pc[cd->queueCreateInfoCount++] = (VkDeviceQueueCreateInfo) {
1544 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1545 .queueFamilyIndex = hwctx->
qf[
i].
idx,
1546 .queueCount = hwctx->
qf[
i].
num,
1551 #if FF_API_VULKAN_FIXED_QUEUES
1560 #define SET_OLD_QF(field, nb_field, type) \
1562 if (field < 0 && hwctx->qf[i].flags & type) { \
1563 field = hwctx->qf[i].idx; \
1564 nb_field = hwctx->qf[i].num; \
1568 for (uint32_t
i = 0;
i < hwctx->
nb_qf;
i++) {
1598 vk->DestroyDebugUtilsMessengerEXT(hwctx->
inst, p->
debug_ctx,
1602 vk->DestroyInstance(hwctx->
inst, hwctx->
alloc);
1626 int disable_multiplane,
1637 VkDeviceCreateInfo dev_info = {
1638 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1651 &dev_info.enabledExtensionCount, debug_mode))) {
1652 for (
int i = 0;
i < dev_info.queueCreateInfoCount;
i++)
1653 av_free((
void *)dev_info.pQueueCreateInfos[
i].pQueuePriorities);
1654 av_free((
void *)dev_info.pQueueCreateInfos);
1660 vk->GetPhysicalDeviceFeatures2(hwctx->
phys_dev, &supported_feats.
device);
1666 dev_info.pEnabledFeatures = &p->
feats.
device.features;
1676 for (
int i = 0;
i < dev_info.queueCreateInfoCount;
i++)
1677 av_free((
void *)dev_info.pQueueCreateInfos[
i].pQueuePriorities);
1678 av_free((
void *)dev_info.pQueueCreateInfos);
1680 if (
ret != VK_SUCCESS) {
1683 for (
int i = 0;
i < dev_info.enabledExtensionCount;
i++)
1684 av_free((
void *)dev_info.ppEnabledExtensionNames[
i]);
1685 av_free((
void *)dev_info.ppEnabledExtensionNames);
1736 VkQueueFamilyProperties2 *qf;
1737 VkQueueFamilyVideoPropertiesKHR *qf_vid;
1738 VkPhysicalDeviceExternalSemaphoreInfo ext_sem_props_info;
1739 int graph_index, comp_index, tx_index, enc_index, dec_index;
1758 p->
props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
1760 p->
hprops.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT;
1762 vk->GetPhysicalDeviceProperties2(hwctx->
phys_dev, &p->
props);
1764 p->
props.properties.deviceName);
1767 p->
props.properties.limits.optimalBufferCopyRowPitchAlignment);
1769 p->
props.properties.limits.minMemoryMapAlignment);
1771 p->
props.properties.limits.nonCoherentAtomSize);
1774 p->
hprops.minImportedHostPointerAlignment);
1778 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &qf_num,
NULL);
1784 ext_sem_props_info = (VkPhysicalDeviceExternalSemaphoreInfo) {
1785 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO,
1789 ext_sem_props_info.handleType =
1791 IsWindows8OrGreater()
1792 ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
1793 : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT;
1795 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
1798 vk->GetPhysicalDeviceExternalSemaphoreProperties(hwctx->
phys_dev,
1799 &ext_sem_props_info,
1806 qf_vid =
av_malloc_array(qf_num,
sizeof(VkQueueFamilyVideoPropertiesKHR));
1812 for (uint32_t
i = 0;
i < qf_num;
i++) {
1813 qf_vid[
i] = (VkQueueFamilyVideoPropertiesKHR) {
1814 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_VIDEO_PROPERTIES_KHR,
1816 qf[
i] = (VkQueueFamilyProperties2) {
1817 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2,
1822 vk->GetPhysicalDeviceQueueFamilyProperties2(hwctx->
phys_dev, &qf_num, qf);
1831 for (uint32_t
i = 0;
i < qf_num;
i++) {
1838 for (uint32_t j = 0; j < qf[
i].queueFamilyProperties.queueCount; j++) {
1849 #if FF_API_VULKAN_FIXED_QUEUES
1857 #define CHECK_QUEUE(type, required, fidx, ctx_qf, qc) \
1859 if (ctx_qf < 0 && required) { \
1860 av_log(ctx, AV_LOG_ERROR, "%s queue family is required, but marked as missing" \
1861 " in the context!\n", type); \
1862 err = AVERROR(EINVAL); \
1864 } else if (fidx < 0 || ctx_qf < 0) { \
1866 } else if (ctx_qf >= qf_num) { \
1867 av_log(ctx, AV_LOG_ERROR, "Invalid %s family index %i (device has %i families)!\n", \
1868 type, ctx_qf, qf_num); \
1869 err = AVERROR(EINVAL); \
1873 av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i (queues: %i)" \
1874 " for%s%s%s%s%s\n", \
1876 ctx_qf == graph_index ? " graphics" : "", \
1877 ctx_qf == comp_index ? " compute" : "", \
1878 ctx_qf == tx_index ? " transfers" : "", \
1879 ctx_qf == enc_index ? " encode" : "", \
1880 ctx_qf == dec_index ? " decode" : ""); \
1881 graph_index = (ctx_qf == graph_index) ? -1 : graph_index; \
1882 comp_index = (ctx_qf == comp_index) ? -1 : comp_index; \
1883 tx_index = (ctx_qf == tx_index) ? -1 : tx_index; \
1884 enc_index = (ctx_qf == enc_index) ? -1 : enc_index; \
1885 dec_index = (ctx_qf == dec_index) ? -1 : dec_index; \
1898 if (!hwctx->
nb_qf) {
1899 #define ADD_QUEUE(ctx_qf, qc, flag) \
1901 if (ctx_qf != -1) { \
1902 hwctx->qf[hwctx->nb_qf++] = (AVVulkanDeviceQueueFamily) { \
1920 for (
int i = 0;
i < hwctx->
nb_qf;
i++) {
1922 hwctx->
qf[
i].
flags & (VK_QUEUE_VIDEO_DECODE_BIT_KHR |
1923 VK_QUEUE_VIDEO_ENCODE_BIT_KHR)) {
1930 for (
int i = 0;
i < hwctx->
nb_qf;
i++) {
1934 for (
int j = (
i - 1); j >= 0; j--) {
1950 vk->GetPhysicalDeviceMemoryProperties(hwctx->
phys_dev, &p->
mprops);
1969 if (device && device[0]) {
1971 dev_select.
index = strtol(device, &end, 10);
1972 if (end == device) {
1973 dev_select.
index = 0;
1974 dev_select.
name = device;
1990 switch(src_ctx->
type) {
1994 VADisplay dpy = src_hwctx->
display;
1995 #if VA_CHECK_VERSION(1, 15, 0)
1997 VADisplayAttribute attr = {
1998 .type = VADisplayPCIID,
2003 #if VA_CHECK_VERSION(1, 15, 0)
2004 vas = vaGetDisplayAttributes(dpy, &attr, 1);
2005 if (vas == VA_STATUS_SUCCESS && attr.flags != VA_DISPLAY_ATTRIB_NOT_SUPPORTED)
2006 dev_select.pci_device = (attr.value & 0xFFFF);
2009 if (!dev_select.pci_device) {
2010 vendor = vaQueryVendorString(dpy);
2016 if (strstr(vendor,
"AMD"))
2017 dev_select.vendor_id = 0x1002;
2026 struct stat drm_node_info;
2027 drmDevice *drm_dev_info;
2030 err = fstat(src_hwctx->
fd, &drm_node_info);
2037 dev_select.drm_major = major(drm_node_info.st_dev);
2038 dev_select.drm_minor = minor(drm_node_info.st_dev);
2039 dev_select.has_drm = 1;
2041 err = drmGetDevice(src_hwctx->
fd, &drm_dev_info);
2048 if (drm_dev_info->bustype == DRM_BUS_PCI)
2049 dev_select.pci_device = drm_dev_info->deviceinfo.pci->device_id;
2051 drmFreeDevice(&drm_dev_info);
2061 CudaFunctions *cu = cu_internal->
cuda_dl;
2063 int ret =
CHECK_CU(cu->cuDeviceGetUuid((CUuuid *)&dev_select.uuid,
2070 dev_select.has_uuid = 1;
2085 const void *hwconfig,
2094 VK_IMAGE_TILING_OPTIMAL,
2107 VK_IMAGE_TILING_OPTIMAL,
2117 constraints->
max_width = p->
props.properties.limits.maxImageDimension2D;
2118 constraints->
max_height = p->
props.properties.limits.maxImageDimension2D;
2131 VkMemoryPropertyFlagBits req_flags,
const void *alloc_extension,
2132 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
2139 VkMemoryAllocateInfo alloc_info = {
2140 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
2141 .pNext = alloc_extension,
2142 .allocationSize = req->size,
2147 for (
int i = 0;
i < p->
mprops.memoryTypeCount;
i++) {
2148 const VkMemoryType *
type = &p->
mprops.memoryTypes[
i];
2151 if (!(req->memoryTypeBits & (1 <<
i)))
2155 if ((
type->propertyFlags & req_flags) != req_flags)
2159 if (req->size > p->
mprops.memoryHeaps[
type->heapIndex].size)
2173 alloc_info.memoryTypeIndex =
index;
2175 ret = vk->AllocateMemory(dev_hwctx->
act_dev, &alloc_info,
2176 dev_hwctx->
alloc, mem);
2177 if (
ret != VK_SUCCESS) {
2183 *mem_flags |= p->
mprops.memoryTypes[
index].propertyFlags;
2193 if (internal->cuda_fc_ref) {
2199 CudaFunctions *cu = cu_internal->
cuda_dl;
2202 if (internal->cu_sem[
i])
2203 CHECK_CU(cu->cuDestroyExternalSemaphore(internal->cu_sem[
i]));
2204 if (internal->cu_mma[
i])
2205 CHECK_CU(cu->cuMipmappedArrayDestroy(internal->cu_mma[
i]));
2206 if (internal->ext_mem[
i])
2207 CHECK_CU(cu->cuDestroyExternalMemory(internal->ext_mem[
i]));
2209 if (internal->ext_sem_handle[
i])
2210 CloseHandle(internal->ext_sem_handle[
i]);
2211 if (internal->ext_mem_handle[
i])
2212 CloseHandle(internal->ext_mem_handle[
i]);
2237 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
2239 .pSemaphores =
f->sem,
2240 .pValues =
f->sem_value,
2241 .semaphoreCount = nb_sems,
2249 for (
int i = 0;
i < nb_images;
i++) {
2264 void *alloc_pnext,
size_t alloc_pnext_stride)
2266 int img_cnt = 0, err;
2274 while (
f->img[img_cnt]) {
2276 VkImageMemoryRequirementsInfo2 req_desc = {
2277 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
2278 .image =
f->img[img_cnt],
2280 VkMemoryDedicatedAllocateInfo ded_alloc = {
2281 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
2282 .pNext = (
void *)(((uint8_t *)alloc_pnext) + img_cnt*alloc_pnext_stride),
2284 VkMemoryDedicatedRequirements ded_req = {
2285 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
2287 VkMemoryRequirements2 req = {
2288 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
2292 vk->GetImageMemoryRequirements2(hwctx->
act_dev, &req_desc, &req);
2294 if (
f->tiling == VK_IMAGE_TILING_LINEAR)
2295 req.memoryRequirements.size =
FFALIGN(req.memoryRequirements.size,
2296 p->
props.properties.limits.minMemoryMapAlignment);
2299 use_ded_mem = ded_req.prefersDedicatedAllocation |
2300 ded_req.requiresDedicatedAllocation;
2302 ded_alloc.image =
f->img[img_cnt];
2306 f->tiling == VK_IMAGE_TILING_LINEAR ?
2307 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
2308 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
2309 use_ded_mem ? &ded_alloc : (
void *)ded_alloc.pNext,
2310 &
f->flags, &
f->mem[img_cnt])))
2313 f->size[img_cnt] = req.memoryRequirements.size;
2314 bind_info[img_cnt].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
2315 bind_info[img_cnt].image =
f->img[img_cnt];
2316 bind_info[img_cnt].memory =
f->mem[img_cnt];
2322 ret = vk->BindImageMemory2(hwctx->
act_dev, img_cnt, bind_info);
2323 if (
ret != VK_SUCCESS) {
2351 uint32_t dst_qf = VK_QUEUE_FAMILY_IGNORED;
2352 VkImageLayout new_layout;
2353 VkAccessFlags2 new_access;
2354 VkPipelineStageFlagBits2 src_stage = VK_PIPELINE_STAGE_2_NONE;
2360 .
data = (uint8_t *)hwfc,
2364 .hw_frames_ctx = &tmp_ref,
2367 VkCommandBuffer cmd_buf;
2369 cmd_buf = exec->
buf;
2373 VK_PIPELINE_STAGE_2_NONE,
2374 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
2380 new_layout = VK_IMAGE_LAYOUT_GENERAL;
2381 new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
2384 new_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
2385 new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
2388 new_layout = VK_IMAGE_LAYOUT_GENERAL;
2389 new_access = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
2392 new_layout = VK_IMAGE_LAYOUT_GENERAL;
2393 new_access = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
2394 dst_qf = VK_QUEUE_FAMILY_EXTERNAL_KHR;
2395 src_stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
2398 new_layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR;
2399 new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
2402 new_layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
2403 new_access = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
2406 new_layout = VK_IMAGE_LAYOUT_VIDEO_ENCODE_DPB_KHR;
2407 new_access = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
2413 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
2414 new_access, new_layout, dst_qf);
2416 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
2417 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
2418 .pImageMemoryBarriers = img_bar,
2419 .imageMemoryBarrierCount = nb_img_bar,
2433 int frame_w,
int frame_h,
int plane)
2450 VkImageTiling tiling, VkImageUsageFlagBits
usage,
2451 VkImageCreateFlags
flags,
int nb_layers,
2463 VkSemaphoreTypeCreateInfo sem_type_info = {
2464 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
2465 .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
2468 VkSemaphoreCreateInfo sem_spawn = {
2469 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2470 .pNext = &sem_type_info,
2473 VkExportSemaphoreCreateInfo ext_sem_info_opaque = {
2474 .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
2476 .handleTypes = IsWindows8OrGreater()
2477 ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
2478 : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
2480 .handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
2485 if (p->
ext_sem_props_opaque.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT) {
2499 for (
int i = 0; (hwfc_vk->
format[
i] != VK_FORMAT_UNDEFINED);
i++) {
2500 VkImageCreateInfo create_info = {
2501 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
2502 .pNext = create_pnext,
2503 .imageType = VK_IMAGE_TYPE_2D,
2507 .arrayLayers = nb_layers,
2510 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
2512 .samples = VK_SAMPLE_COUNT_1_BIT,
2513 .pQueueFamilyIndices = p->
img_qfs,
2515 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2516 VK_SHARING_MODE_EXCLUSIVE,
2519 get_plane_wh(&create_info.extent.width, &create_info.extent.height,
2522 ret = vk->CreateImage(hwctx->
act_dev, &create_info,
2524 if (
ret != VK_SUCCESS) {
2532 ret = vk->CreateSemaphore(hwctx->
act_dev, &sem_spawn,
2534 if (
ret != VK_SUCCESS) {
2542 f->layout[
i] = create_info.initialLayout;
2544 f->sem_value[
i] = 0;
2560 VkExternalMemoryHandleTypeFlags *comp_handle_types,
2561 VkExternalMemoryHandleTypeFlagBits *iexp,
2562 VkExternalMemoryHandleTypeFlagBits
exp)
2570 const VkImageDrmFormatModifierListCreateInfoEXT *drm_mod_info =
2572 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
2573 int has_mods = hwctx->
tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT && drm_mod_info;
2576 VkExternalImageFormatProperties eprops = {
2577 .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
2579 VkImageFormatProperties2 props = {
2580 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
2583 VkPhysicalDeviceImageDrmFormatModifierInfoEXT phy_dev_mod_info = {
2584 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
2586 .pQueueFamilyIndices = p->
img_qfs,
2588 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2589 VK_SHARING_MODE_EXCLUSIVE,
2591 VkPhysicalDeviceExternalImageFormatInfo enext = {
2592 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
2594 .pNext = has_mods ? &phy_dev_mod_info :
NULL,
2596 VkPhysicalDeviceImageFormatInfo2 pinfo = {
2597 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
2598 .pNext = !
exp ?
NULL : &enext,
2600 .type = VK_IMAGE_TYPE_2D,
2602 .usage = hwctx->
usage,
2603 .flags = VK_IMAGE_CREATE_ALIAS_BIT,
2606 nb_mods = has_mods ? drm_mod_info->drmFormatModifierCount : 1;
2607 for (
int i = 0;
i < nb_mods;
i++) {
2609 phy_dev_mod_info.drmFormatModifier = drm_mod_info->pDrmFormatModifiers[
i];
2611 ret = vk->GetPhysicalDeviceImageFormatProperties2(dev_hwctx->
phys_dev,
2614 if (
ret == VK_SUCCESS) {
2616 *comp_handle_types |= eprops.externalMemoryProperties.compatibleHandleTypes;
2630 VkExternalMemoryHandleTypeFlags e = 0x0;
2633 VkExternalMemoryImageCreateInfo eiinfo = {
2634 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
2641 ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT
2642 : VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT);
2646 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
2650 eminfo[
i].sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
2652 eminfo[
i].handleTypes = e;
2665 if ( (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) &&
2666 !(hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR))
2668 else if (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)
2670 else if (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR)
2672 else if (hwctx->
usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)
2726 VkImageUsageFlagBits supported_usage;
2737 (hwctx->
tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT))
2738 hwctx->
tiling = VK_IMAGE_TILING_LINEAR;
2748 if (hwctx->
format[0] != VK_FORMAT_UNDEFINED) {
2753 "for the current sw_format %s!\n",
2765 (hwctx->
usage & VK_IMAGE_USAGE_STORAGE_BIT));
2774 NULL, &supported_usage,
2777 (hwctx->
usage & VK_IMAGE_USAGE_STORAGE_BIT));
2783 if (!hwctx->
usage) {
2784 hwctx->
usage = supported_usage & (VK_BUFFER_USAGE_TRANSFER_DST_BIT |
2785 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
2786 VK_IMAGE_USAGE_STORAGE_BIT |
2787 VK_IMAGE_USAGE_SAMPLED_BIT);
2790 if ((supported_usage & VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR) &&
2793 hwctx->
usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
2800 int is_lone_dpb = ((hwctx->
usage & VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR) ||
2801 ((hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) &&
2802 !(hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)));
2803 int sampleable = hwctx->
usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
2804 VK_IMAGE_USAGE_STORAGE_BIT);
2805 hwctx->
img_flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
2806 if (sampleable && !is_lone_dpb) {
2807 hwctx->
img_flags |= VK_IMAGE_CREATE_ALIAS_BIT;
2809 hwctx->
img_flags |= VK_IMAGE_CREATE_EXTENDED_USAGE_BIT;
2817 if ((hwctx->
usage & VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR) &&
2820 const VkVideoProfileListInfoKHR *pl;
2823 hwctx->
img_flags |= VK_IMAGE_CREATE_VIDEO_PROFILE_INDEPENDENT_BIT_KHR;
2826 for (
i = 0;
i < pl->profileCount;
i++) {
2828 if (pl->pProfiles[
i].videoCodecOperation & 0xFFFF0000)
2831 if (
i == pl->profileCount)
2832 hwctx->
img_flags |= VK_IMAGE_CREATE_VIDEO_PROFILE_INDEPENDENT_BIT_KHR;
2923 static const struct {
2924 uint32_t drm_fourcc;
2926 } vulkan_drm_format_map[] = {
2927 { DRM_FORMAT_R8, VK_FORMAT_R8_UNORM },
2928 { DRM_FORMAT_R16, VK_FORMAT_R16_UNORM },
2929 { DRM_FORMAT_GR88, VK_FORMAT_R8G8_UNORM },
2930 { DRM_FORMAT_RG88, VK_FORMAT_R8G8_UNORM },
2931 { DRM_FORMAT_GR1616, VK_FORMAT_R16G16_UNORM },
2932 { DRM_FORMAT_RG1616, VK_FORMAT_R16G16_UNORM },
2933 { DRM_FORMAT_ARGB8888, VK_FORMAT_B8G8R8A8_UNORM },
2934 { DRM_FORMAT_XRGB8888, VK_FORMAT_B8G8R8A8_UNORM },
2935 { DRM_FORMAT_ABGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2936 { DRM_FORMAT_XBGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2937 { DRM_FORMAT_ARGB2101010, VK_FORMAT_A2B10G10R10_UNORM_PACK32 },
2938 { DRM_FORMAT_ABGR2101010, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
2939 { DRM_FORMAT_XRGB2101010, VK_FORMAT_A2B10G10R10_UNORM_PACK32 },
2940 { DRM_FORMAT_XBGR2101010, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
2943 #ifdef DRM_FORMAT_XYUV8888
2944 { DRM_FORMAT_XYUV8888, VK_FORMAT_R8G8B8A8_UNORM },
2945 { DRM_FORMAT_XVYU2101010, VK_FORMAT_A2R10G10B10_UNORM_PACK32 } ,
2946 { DRM_FORMAT_XVYU12_16161616, VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16 } ,
2947 { DRM_FORMAT_XVYU16161616, VK_FORMAT_R16G16B16A16_UNORM } ,
2951 static inline VkFormat drm_to_vulkan_fmt(uint32_t drm_fourcc)
2954 if (vulkan_drm_format_map[
i].drm_fourcc == drm_fourcc)
2955 return vulkan_drm_format_map[
i].vk_format;
2956 return VK_FORMAT_UNDEFINED;
2965 int bind_counts = 0;
2975 if (drm_to_vulkan_fmt(
desc->layers[
i].format) == VK_FORMAT_UNDEFINED) {
2977 desc->layers[
i].format);
2988 f->tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
2990 for (
int i = 0;
i <
desc->nb_layers;
i++) {
2994 VkSemaphoreTypeCreateInfo sem_type_info = {
2995 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
2996 .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
2999 VkSemaphoreCreateInfo sem_spawn = {
3000 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3001 .pNext = &sem_type_info,
3006 VkImageDrmFormatModifierExplicitCreateInfoEXT ext_img_mod_spec = {
3007 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
3008 .drmFormatModifier =
desc->objects[0].format_modifier,
3009 .drmFormatModifierPlaneCount =
planes,
3010 .pPlaneLayouts = (
const VkSubresourceLayout *)&ext_img_layouts,
3012 VkExternalMemoryImageCreateInfo ext_img_spec = {
3013 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
3014 .pNext = &ext_img_mod_spec,
3015 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
3017 VkImageCreateInfo create_info = {
3018 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
3019 .pNext = &ext_img_spec,
3020 .imageType = VK_IMAGE_TYPE_2D,
3021 .format = drm_to_vulkan_fmt(
desc->layers[
i].format),
3026 .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
3027 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
3029 .samples = VK_SAMPLE_COUNT_1_BIT,
3030 .pQueueFamilyIndices = p->
img_qfs,
3032 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
3033 VK_SHARING_MODE_EXCLUSIVE,
3037 VkExternalImageFormatProperties ext_props = {
3038 .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
3040 VkImageFormatProperties2 props_ret = {
3041 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
3042 .pNext = &ext_props,
3044 VkPhysicalDeviceImageDrmFormatModifierInfoEXT props_drm_mod = {
3045 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
3046 .drmFormatModifier = ext_img_mod_spec.drmFormatModifier,
3047 .pQueueFamilyIndices = create_info.pQueueFamilyIndices,
3048 .queueFamilyIndexCount = create_info.queueFamilyIndexCount,
3049 .sharingMode = create_info.sharingMode,
3051 VkPhysicalDeviceExternalImageFormatInfo props_ext = {
3052 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
3053 .pNext = &props_drm_mod,
3054 .handleType = ext_img_spec.handleTypes,
3056 VkPhysicalDeviceImageFormatInfo2 fmt_props;
3059 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT |
3060 VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
3062 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT |
3063 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
3065 fmt_props = (VkPhysicalDeviceImageFormatInfo2) {
3066 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
3067 .pNext = &props_ext,
3068 .format = create_info.format,
3069 .type = create_info.imageType,
3070 .tiling = create_info.tiling,
3071 .usage = create_info.usage,
3072 .flags = create_info.flags,
3076 ret = vk->GetPhysicalDeviceImageFormatProperties2(hwctx->
phys_dev,
3077 &fmt_props, &props_ret);
3078 if (
ret != VK_SUCCESS) {
3086 get_plane_wh(&create_info.extent.width, &create_info.extent.height,
3090 for (
int j = 0; j <
planes; j++) {
3091 ext_img_layouts[j].offset =
desc->layers[
i].planes[j].offset;
3092 ext_img_layouts[j].rowPitch =
desc->layers[
i].planes[j].pitch;
3093 ext_img_layouts[j].size = 0;
3094 ext_img_layouts[j].arrayPitch = 0;
3095 ext_img_layouts[j].depthPitch = 0;
3099 ret = vk->CreateImage(hwctx->
act_dev, &create_info,
3101 if (
ret != VK_SUCCESS) {
3108 ret = vk->CreateSemaphore(hwctx->
act_dev, &sem_spawn,
3110 if (
ret != VK_SUCCESS) {
3117 f->queue_family[
i] = VK_QUEUE_FAMILY_EXTERNAL;
3118 f->layout[
i] = create_info.initialLayout;
3120 f->sem_value[
i] = 0;
3123 for (
int i = 0;
i <
desc->nb_layers;
i++) {
3125 VkImageMemoryRequirementsInfo2 req_desc = {
3126 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
3129 VkMemoryDedicatedRequirements ded_req = {
3130 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
3132 VkMemoryRequirements2 req2 = {
3133 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
3138 VkMemoryFdPropertiesKHR fdmp = {
3139 .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
3145 VkImportMemoryFdInfoKHR idesc = {
3146 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
3147 .fd = dup(
desc->objects[
desc->layers[
i].planes[0].object_index].fd),
3148 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
3150 VkMemoryDedicatedAllocateInfo ded_alloc = {
3151 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
3153 .image = req_desc.image,
3157 ret = vk->GetMemoryFdPropertiesKHR(hwctx->
act_dev,
3158 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
3160 if (
ret != VK_SUCCESS) {
3168 vk->GetImageMemoryRequirements2(hwctx->
act_dev, &req_desc, &req2);
3171 req2.memoryRequirements.memoryTypeBits = fdmp.memoryTypeBits;
3174 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
3175 (ded_req.prefersDedicatedAllocation ||
3176 ded_req.requiresDedicatedAllocation) ?
3177 &ded_alloc : ded_alloc.pNext,
3178 &
f->flags, &
f->mem[
i]);
3184 f->size[
i] = req2.memoryRequirements.size;
3187 for (
int i = 0;
i <
desc->nb_layers;
i++) {
3189 for (
int j = 0; j <
planes; j++) {
3190 VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
3191 j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :
3192 VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT;
3194 plane_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO;
3196 plane_info[bind_counts].planeAspect = aspect;
3198 bind_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
3200 bind_info[bind_counts].image =
f->img[
i];
3201 bind_info[bind_counts].memory =
f->mem[
i];
3204 bind_info[bind_counts].memoryOffset = 0;
3211 ret = vk->BindImageMemory2(hwctx->
act_dev, bind_counts, bind_info);
3212 if (
ret != VK_SUCCESS) {
3242 #ifdef DMA_BUF_IOCTL_EXPORT_SYNC_FILE
3244 VkCommandBuffer cmd_buf;
3250 for (
int i = 0;
i <
desc->nb_objects;
i++) {
3251 VkSemaphoreTypeCreateInfo sem_type_info = {
3252 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
3253 .semaphoreType = VK_SEMAPHORE_TYPE_BINARY,
3255 VkSemaphoreCreateInfo sem_spawn = {
3256 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3257 .pNext = &sem_type_info,
3259 VkImportSemaphoreFdInfoKHR import_info;
3260 struct dma_buf_export_sync_file implicit_fd_info = {
3261 .flags = DMA_BUF_SYNC_READ,
3265 if (ioctl(
desc->objects[
i].fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE,
3266 &implicit_fd_info)) {
3271 vk->DestroySemaphore(hwctx->
act_dev, drm_sync_sem[
i], hwctx->
alloc);
3275 ret = vk->CreateSemaphore(hwctx->
act_dev, &sem_spawn,
3276 hwctx->
alloc, &drm_sync_sem[
i]);
3277 if (
ret != VK_SUCCESS) {
3282 vk->DestroySemaphore(hwctx->
act_dev, drm_sync_sem[
i], hwctx->
alloc);
3286 import_info = (VkImportSemaphoreFdInfoKHR) {
3287 .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
3288 .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT,
3289 .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT,
3290 .semaphore = drm_sync_sem[
i],
3291 .fd = implicit_fd_info.fd,
3294 ret = vk->ImportSemaphoreFdKHR(hwctx->
act_dev, &import_info);
3295 if (
ret != VK_SUCCESS) {
3300 vk->DestroySemaphore(hwctx->
act_dev, drm_sync_sem[
i], hwctx->
alloc);
3306 cmd_buf = exec->
buf;
3312 drm_sync_sem,
desc->nb_objects,
3313 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, 1);
3318 VK_PIPELINE_STAGE_2_NONE,
3319 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
3324 VK_PIPELINE_STAGE_2_NONE,
3325 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
3327 VK_ACCESS_2_SHADER_SAMPLED_READ_BIT : 0x0) |
3329 VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT : 0x0),
3330 VK_IMAGE_LAYOUT_GENERAL,
3331 VK_QUEUE_FAMILY_IGNORED);
3333 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
3334 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
3335 .pImageMemoryBarriers = img_bar,
3336 .imageMemoryBarrierCount = nb_img_bar,
3347 "image may be corrupted.\n");
3362 if ((err = vulkan_map_from_drm_frame_desc(hwfc, &
f,
src,
flags)))
3366 dst->data[0] = (uint8_t *)
f;
3368 dst->height =
src->height;
3371 &vulkan_unmap_from_drm,
f);
3375 err = vulkan_map_from_drm_frame_sync(hwfc,
dst,
src,
flags);
3398 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)
src->data[3];
3404 vaSyncSurface(vaapi_ctx->display, surface_id);
3412 err = vulkan_map_from_drm(dst_fc,
dst,
tmp,
flags);
3429 VkDeviceMemory mem,
size_t size)
3437 CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
3438 .type = IsWindows8OrGreater()
3439 ? CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32
3440 : CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT,
3443 VkMemoryGetWin32HandleInfoKHR export_info = {
3444 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR,
3446 .handleType = IsWindows8OrGreater()
3447 ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT
3448 : VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
3451 ret = vk->GetMemoryWin32HandleKHR(hwctx->
act_dev, &export_info,
3452 &ext_desc.handle.win32.handle);
3453 if (
ret != VK_SUCCESS) {
3458 dst_int->ext_mem_handle[idx] = ext_desc.handle.win32.handle;
3460 CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
3461 .type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
3464 VkMemoryGetFdInfoKHR export_info = {
3465 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
3467 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
3470 ret = vk->GetMemoryFdKHR(hwctx->
act_dev, &export_info,
3471 &ext_desc.handle.fd);
3472 if (
ret != VK_SUCCESS) {
3479 ret =
CHECK_CU(cu->cuImportExternalMemory(&dst_int->ext_mem[idx], &ext_desc));
3482 close(ext_desc.handle.fd);
3501 VkSemaphoreGetWin32HandleInfoKHR sem_export = {
3502 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR,
3504 .handleType = IsWindows8OrGreater()
3505 ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
3506 : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
3508 CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
3512 VkSemaphoreGetFdInfoKHR sem_export = {
3513 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
3515 .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
3517 CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
3523 ret = vk->GetSemaphoreWin32HandleKHR(hwctx->
act_dev, &sem_export,
3524 &ext_sem_desc.handle.win32.handle);
3526 ret = vk->GetSemaphoreFdKHR(hwctx->
act_dev, &sem_export,
3527 &ext_sem_desc.handle.fd);
3529 if (
ret != VK_SUCCESS) {
3535 dst_int->ext_sem_handle[idx] = ext_sem_desc.handle.win32.handle;
3538 ret =
CHECK_CU(cu->cuImportExternalSemaphore(&dst_int->cu_sem[idx],
3542 close(ext_sem_desc.handle.fd);
3570 CudaFunctions *cu = cu_internal->
cuda_dl;
3571 CUarray_format cufmt =
desc->comp[0].depth > 8 ? CU_AD_FORMAT_UNSIGNED_INT16 :
3572 CU_AD_FORMAT_UNSIGNED_INT8;
3577 if (!dst_int->cuda_fc_ref) {
3581 if (!dst_int->cuda_fc_ref)
3585 for (
int i = 0;
i < nb_images;
i++) {
3586 err = export_mem_to_cuda(
ctx, cuda_cu, cu, dst_int,
i,
3591 err = export_sem_to_cuda(
ctx, cuda_cu, cu, dst_int,
i,
3597 if (nb_images !=
planes) {
3599 VkImageSubresource subres = {
3600 .aspectMask =
i == 2 ? VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT :
3601 i == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :
3602 VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT
3604 VkSubresourceLayout
layout = { 0 };
3605 vk->GetImageSubresourceLayout(hwctx->
act_dev, dst_f->
img[
FFMIN(
i, nb_images - 1)],
3612 CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC tex_desc = {
3617 .NumChannels = 1 + ((
planes == 2) &&
i),
3625 tex_desc.arrayDesc.Width = p_w;
3626 tex_desc.arrayDesc.Height = p_h;
3628 ret =
CHECK_CU(cu->cuExternalMemoryGetMappedMipmappedArray(&dst_int->cu_mma[
i],
3629 dst_int->ext_mem[
FFMIN(
i, nb_images - 1)],
3636 ret =
CHECK_CU(cu->cuMipmappedArrayGetLevel(&dst_int->cu_array[
i],
3637 dst_int->cu_mma[
i], 0));
3668 CudaFunctions *cu = cu_internal->
cuda_dl;
3678 err =
CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
3682 err = vulkan_export_to_cuda(hwfc,
src->hw_frames_ctx,
dst);
3691 s_w_par[
i].params.fence.value = dst_f->
sem_value[
i] + 0;
3692 s_s_par[
i].params.fence.value = dst_f->
sem_value[
i] + 1;
3695 err =
CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
3696 planes, cuda_dev->stream));
3701 CUDA_MEMCPY2D cpy = {
3702 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
3703 .srcDevice = (CUdeviceptr)
src->data[
i],
3704 .srcPitch =
src->linesize[
i],
3707 .dstMemoryType = CU_MEMORYTYPE_ARRAY,
3708 .dstArray = dst_int->cu_array[
i],
3714 cpy.WidthInBytes = p_w *
desc->comp[
i].step;
3717 err =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
3722 err =
CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
3723 planes, cuda_dev->stream));
3749 switch (
src->format) {
3754 return vulkan_map_from_vaapi(hwfc,
dst,
src,
flags);
3760 return vulkan_map_from_drm(hwfc,
dst,
src,
flags);
3770 typedef struct VulkanDRMMapping {
3785 static inline uint32_t vulkan_fmt_to_drm(
VkFormat vkfmt)
3788 if (vulkan_drm_format_map[
i].vk_format == vkfmt)
3789 return vulkan_drm_format_map[
i].drm_fourcc;
3790 return DRM_FORMAT_INVALID;
3804 VkImageDrmFormatModifierPropertiesEXT drm_mod = {
3805 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
3807 VkSemaphoreWaitInfo wait_info = {
3808 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
3810 .semaphoreCount =
planes,
3822 wait_info.pSemaphores =
f->sem;
3823 wait_info.pValues =
f->sem_value;
3825 vk->WaitSemaphores(hwctx->
act_dev, &wait_info, UINT64_MAX);
3831 ret = vk->GetImageDrmFormatModifierPropertiesEXT(hwctx->
act_dev,
f->img[0],
3833 if (
ret != VK_SUCCESS) {
3839 for (
int i = 0; (
i <
planes) && (
f->mem[
i]);
i++) {
3840 VkMemoryGetFdInfoKHR export_info = {
3841 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
3842 .memory =
f->mem[
i],
3843 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
3846 ret = vk->GetMemoryFdKHR(hwctx->
act_dev, &export_info,
3848 if (
ret != VK_SUCCESS) {
3861 VkSubresourceLayout
layout;
3862 VkImageSubresource sub = {
3863 .aspectMask = VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT,
3867 drm_desc->
layers[
i].
format = vulkan_fmt_to_drm(plane_vkfmt);
3878 if (
f->tiling == VK_IMAGE_TILING_OPTIMAL)
3881 vk->GetImageSubresourceLayout(hwctx->
act_dev,
f->img[
i], &sub, &
layout);
3887 dst->height =
src->height;
3888 dst->data[0] = (uint8_t *)drm_desc;
3932 switch (
dst->format) {
3942 return vulkan_map_to_vaapi(hwfc,
dst,
src,
flags);
3954 AVFrame *swf, VkBufferImageCopy *region,
3964 const VkMappedMemoryRange flush_info = {
3965 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
3966 .memory = vkbuf->
mem,
3967 .size = VK_WHOLE_SIZE,
3970 if (!upload && !(vkbuf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
3971 ret = vk->InvalidateMappedMemoryRanges(hwctx->
act_dev, 1,
3973 if (
ret != VK_SUCCESS) {
3983 region[
i].bufferRowLength,
3987 region[
i].imageExtent.height);
3992 vkbuf->mapped_mem + region[
i].bufferOffset,
3993 region[
i].bufferRowLength,
3995 region[
i].imageExtent.height);
3998 if (upload && !(vkbuf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
3999 ret = vk->FlushMappedMemoryRanges(hwctx->
act_dev, 1,
4001 if (
ret != VK_SUCCESS) {
4012 AVFrame *swf, VkBufferImageCopy *region,
int upload)
4019 VkBufferUsageFlags buf_usage = upload ? VK_BUFFER_USAGE_TRANSFER_SRC_BIT :
4020 VK_BUFFER_USAGE_TRANSFER_DST_BIT;
4022 size_t buf_offset = 0;
4026 region[
i] = (VkBufferImageCopy) {
4027 .bufferOffset = buf_offset,
4029 p->
props.properties.limits.optimalBufferCopyRowPitchAlignment),
4030 .bufferImageHeight = p_h,
4031 .imageSubresource.layerCount = 1,
4032 .imageExtent = (VkExtent3D){ p_w, p_h, 1 },
4036 buf_offset +=
FFALIGN(p_h*region[
i].bufferRowLength,
4037 p->
props.properties.limits.optimalBufferCopyOffsetAlignment);
4042 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
4043 VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
4051 AVFrame *swf, VkBufferImageCopy *region,
int upload)
4058 VkBufferUsageFlags buf_usage = upload ? VK_BUFFER_USAGE_TRANSFER_SRC_BIT :
4059 VK_BUFFER_USAGE_TRANSFER_DST_BIT;
4068 while (swf->
buf[nb_src_bufs])
4072 if (nb_src_bufs == 1) {
4083 }
else if (nb_src_bufs ==
planes) {
4102 for (
int i = 0;
i < (*nb_bufs);
i++)
4116 int host_mapped = 0;
4131 VkCommandBuffer cmd_buf;
4148 region[
i] = (VkBufferImageCopy) {
4151 .bufferImageHeight = p_h,
4152 .imageSubresource.layerCount = 1,
4153 .imageExtent = (VkExtent3D){ p_w, p_h, 1 },
4179 cmd_buf = exec->
buf;
4185 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
4186 VK_PIPELINE_STAGE_2_TRANSFER_BIT);
4210 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
4211 VK_PIPELINE_STAGE_2_TRANSFER_BIT_KHR,
4212 upload ? VK_ACCESS_TRANSFER_WRITE_BIT :
4213 VK_ACCESS_TRANSFER_READ_BIT,
4214 upload ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL :
4215 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4216 VK_QUEUE_FAMILY_IGNORED);
4218 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
4219 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
4220 .pImageMemoryBarriers = img_bar,
4221 .imageMemoryBarrierCount = nb_img_bar,
4225 int buf_idx =
FFMIN(
i, (nb_bufs - 1));
4226 int img_idx =
FFMIN(
i, (nb_images - 1));
4229 uint32_t orig_stride = region[
i].bufferRowLength;
4230 region[
i].bufferRowLength /=
desc->comp[
i].step;
4234 vk->CmdCopyBufferToImage(cmd_buf, vkbuf->
buf,
4235 hwf_vk->
img[img_idx],
4236 img_bar[img_idx].newLayout,
4239 vk->CmdCopyImageToBuffer(cmd_buf, hwf_vk->
img[img_idx],
4240 img_bar[img_idx].newLayout,
4244 region[
i].bufferRowLength = orig_stride;
4250 }
else if (!upload) {
4257 for (
int i = 0;
i < nb_bufs;
i++)
4268 switch (
src->format) {
4278 return vulkan_transfer_data_from_cuda(hwfc,
dst,
src);
4281 if (
src->hw_frames_ctx)
4305 CudaFunctions *cu = cu_internal->
cuda_dl;
4316 err =
CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
4320 err = vulkan_export_to_cuda(hwfc,
dst->hw_frames_ctx,
src);
4329 s_w_par[
i].params.fence.value = dst_f->
sem_value[
i] + 0;
4330 s_s_par[
i].params.fence.value = dst_f->
sem_value[
i] + 1;
4333 err =
CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
4334 nb_images, cuda_dev->stream));
4339 CUDA_MEMCPY2D cpy = {
4340 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
4341 .dstDevice = (CUdeviceptr)
dst->data[
i],
4342 .dstPitch =
dst->linesize[
i],
4345 .srcMemoryType = CU_MEMORYTYPE_ARRAY,
4346 .srcArray = dst_int->cu_array[
i],
4352 cpy.WidthInBytes =
w *
desc->comp[
i].step;
4355 err =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
4360 err =
CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
4361 nb_images, cuda_dev->stream));
4387 switch (
dst->format) {
4397 return vulkan_transfer_data_to_cuda(hwfc,
dst,
src);
4400 if (
dst->hw_frames_ctx)