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>
67 #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
92 VkPhysicalDeviceProperties2
props;
93 VkPhysicalDeviceMemoryProperties
mprops;
94 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
hprops;
156 #define ASPECT_2PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT)
157 #define ASPECT_3PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)
169 { VK_FORMAT_R8_UNORM,
AV_PIX_FMT_GRAY8, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8_UNORM } },
170 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GRAY16, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16_UNORM } },
171 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GRAYF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R32_SFLOAT } },
174 { VK_FORMAT_R16G16B16A16_UNORM,
AV_PIX_FMT_XV36, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
175 { VK_FORMAT_B8G8R8A8_UNORM,
AV_PIX_FMT_BGRA, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8A8_UNORM } },
176 { VK_FORMAT_R8G8B8A8_UNORM,
AV_PIX_FMT_RGBA, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
177 { VK_FORMAT_R8G8B8_UNORM,
AV_PIX_FMT_RGB24, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8_UNORM } },
178 { VK_FORMAT_B8G8R8_UNORM,
AV_PIX_FMT_BGR24, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8_UNORM } },
179 { VK_FORMAT_R16G16B16_UNORM,
AV_PIX_FMT_RGB48, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16_UNORM } },
180 { VK_FORMAT_R16G16B16A16_UNORM,
AV_PIX_FMT_RGBA64, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
181 { VK_FORMAT_R5G6B5_UNORM_PACK16,
AV_PIX_FMT_RGB565, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R5G6B5_UNORM_PACK16 } },
182 { VK_FORMAT_B5G6R5_UNORM_PACK16,
AV_PIX_FMT_BGR565, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B5G6R5_UNORM_PACK16 } },
183 { VK_FORMAT_B8G8R8A8_UNORM,
AV_PIX_FMT_BGR0, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_B8G8R8A8_UNORM } },
184 { VK_FORMAT_R8G8B8A8_UNORM,
AV_PIX_FMT_RGB0, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
185 { VK_FORMAT_A2R10G10B10_UNORM_PACK32,
AV_PIX_FMT_X2RGB10, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_A2R10G10B10_UNORM_PACK32 } },
188 { VK_FORMAT_R8_UNORM,
AV_PIX_FMT_GBRAP, VK_IMAGE_ASPECT_COLOR_BIT, 1, 4, 4, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
189 { VK_FORMAT_R16_UNORM,
AV_PIX_FMT_GBRAP16, VK_IMAGE_ASPECT_COLOR_BIT, 1, 4, 4, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
190 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GBRPF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 3, 3, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
191 { VK_FORMAT_R32_SFLOAT,
AV_PIX_FMT_GBRAPF32, VK_IMAGE_ASPECT_COLOR_BIT, 1, 4, 4, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
195 { 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 } },
196 { 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 } },
201 { 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 } },
202 { 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 } },
207 { 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 } },
208 { 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 } },
226 { VK_FORMAT_G8B8G8R8_422_UNORM,
AV_PIX_FMT_YUYV422, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
227 { VK_FORMAT_B8G8R8G8_422_UNORM,
AV_PIX_FMT_UYVY422, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R8G8B8A8_UNORM } },
228 { VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16,
AV_PIX_FMT_Y210, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
229 { VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16,
AV_PIX_FMT_Y212, VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 1, { VK_FORMAT_R16G16B16A16_UNORM } },
250 #define FN_MAP_TO(dst_t, dst_name, src_t, src_name) \
251 static av_unused dst_t map_ ##src_name## _to_ ##dst_name(src_t src) \
254 MAP_TO(VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT, \
255 VK_IMAGE_USAGE_SAMPLED_BIT); \
256 MAP_TO(VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT, \
257 VK_IMAGE_USAGE_TRANSFER_SRC_BIT); \
258 MAP_TO(VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT, \
259 VK_IMAGE_USAGE_TRANSFER_DST_BIT); \
260 MAP_TO(VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT, \
261 VK_IMAGE_USAGE_STORAGE_BIT); \
262 MAP_TO(VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT, \
263 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); \
264 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR, \
265 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR); \
266 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR, \
267 VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR); \
268 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_ENCODE_DPB_BIT_KHR, \
269 VK_IMAGE_USAGE_VIDEO_ENCODE_DPB_BIT_KHR); \
270 MAP_TO(VK_FORMAT_FEATURE_2_VIDEO_ENCODE_INPUT_BIT_KHR, \
271 VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR); \
275 #define MAP_TO(flag1, flag2) if (src & flag2) dst |= flag1;
276 FN_MAP_TO(VkFormatFeatureFlagBits2, feats, VkImageUsageFlags,
usage)
278 #define MAP_TO(flag1, flag2) if (src & flag1) dst |= flag2;
279 FN_MAP_TO(VkImageUsageFlags,
usage, VkFormatFeatureFlagBits2, feats)
284 VkImageTiling tiling,
287 VkImageAspectFlags *
aspect,
288 VkImageUsageFlags *supported_usage,
289 int disable_multiplane,
int need_storage)
295 const VkFormatFeatureFlagBits2 basic_flags = VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT |
296 VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
297 VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
301 VkFormatProperties3 fprops = {
302 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3,
304 VkFormatProperties2 prop = {
305 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
308 VkFormatFeatureFlagBits2 feats_primary, feats_secondary;
309 int basics_primary = 0, basics_secondary = 0;
310 int storage_primary = 0, storage_secondary = 0;
312 vk->GetPhysicalDeviceFormatProperties2(hwctx->
phys_dev,
316 feats_primary = tiling == VK_IMAGE_TILING_LINEAR ?
317 fprops.linearTilingFeatures : fprops.optimalTilingFeatures;
318 basics_primary = (feats_primary & basic_flags) == basic_flags;
319 storage_primary = !!(feats_primary & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT);
322 vk->GetPhysicalDeviceFormatProperties2(hwctx->
phys_dev,
325 feats_secondary = tiling == VK_IMAGE_TILING_LINEAR ?
326 fprops.linearTilingFeatures : fprops.optimalTilingFeatures;
327 basics_secondary = (feats_secondary & basic_flags) == basic_flags;
328 storage_secondary = !!(feats_secondary & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT);
330 basics_secondary = basics_primary;
331 storage_secondary = storage_primary;
334 if (basics_primary &&
336 (!need_storage || (need_storage && (storage_primary | storage_secondary)))) {
344 *supported_usage = map_feats_to_usage(feats_primary) |
345 ((need_storage && (storage_primary | storage_secondary)) ?
346 VK_IMAGE_USAGE_STORAGE_BIT : 0);
348 }
else if (basics_secondary &&
349 (!need_storage || (need_storage && storage_secondary))) {
359 *supported_usage = map_feats_to_usage(feats_secondary);
375 static const char *lib_names[] = {
378 #elif defined(__APPLE__)
389 p->
libvulkan = dlopen(lib_names[
i], RTLD_NOW | RTLD_LOCAL);
431 { VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_WIN32_MEMORY },
432 { VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME, FF_VK_EXT_EXTERNAL_WIN32_SEM },
443 static VkBool32 VKAPI_CALL
vk_dbg_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
444 VkDebugUtilsMessageTypeFlagsEXT messageType,
445 const VkDebugUtilsMessengerCallbackDataEXT *
data,
452 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: l =
AV_LOG_VERBOSE;
break;
453 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: l =
AV_LOG_INFO;
break;
454 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: l =
AV_LOG_WARNING;
break;
455 case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: l =
AV_LOG_ERROR;
break;
460 for (
int i = 0;
i <
data->cmdBufLabelCount;
i++)
466 #define ADD_VAL_TO_LIST(list, count, val) \
468 list = av_realloc_array(list, sizeof(*list), ++count); \
470 err = AVERROR(ENOMEM); \
473 list[count - 1] = av_strdup(val); \
474 if (!list[count - 1]) { \
475 err = AVERROR(ENOMEM); \
480 #define RELEASE_PROPS(props, count) \
482 for (int i = 0; i < count; i++) \
483 av_free((void *)((props)[i])); \
484 av_free((void *)props); \
488 const char *
const **dst, uint32_t *num,
int debug)
491 const char **extension_names =
NULL;
495 int err = 0, found, extensions_found = 0;
498 int optional_exts_num;
499 uint32_t sup_ext_count;
500 char *user_exts_str =
NULL;
502 VkExtensionProperties *sup_ext;
512 if (!user_exts_str) {
517 vk->EnumerateInstanceExtensionProperties(
NULL, &sup_ext_count,
NULL);
518 sup_ext =
av_malloc_array(sup_ext_count,
sizeof(VkExtensionProperties));
521 vk->EnumerateInstanceExtensionProperties(
NULL, &sup_ext_count, sup_ext);
529 if (!user_exts_str) {
534 vk->EnumerateDeviceExtensionProperties(hwctx->
phys_dev,
NULL,
535 &sup_ext_count,
NULL);
536 sup_ext =
av_malloc_array(sup_ext_count,
sizeof(VkExtensionProperties));
539 vk->EnumerateDeviceExtensionProperties(hwctx->
phys_dev,
NULL,
540 &sup_ext_count, sup_ext);
543 for (
int i = 0;
i < optional_exts_num;
i++) {
544 tstr = optional_exts[
i].
name;
546 for (
int j = 0; j < sup_ext_count; j++) {
547 if (!strcmp(tstr, sup_ext[j].extensionName)) {
561 tstr = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
563 for (
int j = 0; j < sup_ext_count; j++) {
564 if (!strcmp(tstr, sup_ext[j].extensionName)) {
582 char *save, *token =
av_strtok(user_exts_str,
"+", &save);
585 for (
int j = 0; j < sup_ext_count; j++) {
586 if (!strcmp(token, sup_ext[j].extensionName)) {
602 *dst = extension_names;
603 *num = extensions_found;
617 const char *
const **dst, uint32_t *num,
620 static const char default_layer[] = {
"VK_LAYER_KHRONOS_validation" };
622 int found = 0, err = 0;
626 uint32_t sup_layer_count;
627 VkLayerProperties *sup_layers;
630 char *user_layers_str =
NULL;
633 const char **enabled_layers =
NULL;
634 uint32_t enabled_layers_count = 0;
637 int debug = debug_opt && strtol(debug_opt->
value,
NULL, 10);
640 if (debug_opt && !debug)
643 vk->EnumerateInstanceLayerProperties(&sup_layer_count,
NULL);
644 sup_layers =
av_malloc_array(sup_layer_count,
sizeof(VkLayerProperties));
647 vk->EnumerateInstanceLayerProperties(&sup_layer_count, sup_layers);
650 for (
int i = 0;
i < sup_layer_count;
i++)
656 for (
int i = 0;
i < sup_layer_count;
i++) {
657 if (!strcmp(default_layer, sup_layers[
i].layerName)) {
672 if (!user_layers_str) {
677 token =
av_strtok(user_layers_str,
"+", &save);
680 if (!strcmp(default_layer, token)) {
690 for (
int j = 0; j < sup_layer_count; j++) {
691 if (!strcmp(token, sup_layers[j].layerName)) {
701 "Validation Layer \"%s\" not support.\n", token);
713 *dst = enabled_layers;
714 *num = enabled_layers_count;
728 int err = 0, debug_mode = 0;
733 VkApplicationInfo application_info = {
734 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
735 .pApplicationName =
"ffmpeg",
739 .pEngineName =
"libavutil",
740 .apiVersion = VK_API_VERSION_1_3,
745 VkValidationFeaturesEXT validation_features = {
746 .sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT,
748 VkInstanceCreateInfo inst_props = {
749 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
750 .pApplicationInfo = &application_info,
766 &inst_props.enabledLayerCount, &debug_mode);
772 &inst_props.enabledExtensionCount, debug_mode);
779 VkValidationFeatureEnableEXT feat_list[] = {
780 VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,
781 VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT,
782 VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT,
784 validation_features.pEnabledValidationFeatures = feat_list;
785 validation_features.enabledValidationFeatureCount =
FF_ARRAY_ELEMS(feat_list);
786 inst_props.pNext = &validation_features;
790 ret = vk->CreateInstance(&inst_props, hwctx->
alloc, &hwctx->
inst);
793 if (
ret != VK_SUCCESS) {
807 VkDebugUtilsMessengerCreateInfoEXT dbg = {
808 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
809 .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
810 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
811 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
812 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
813 .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
814 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
815 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
820 vk->CreateDebugUtilsMessengerEXT(hwctx->
inst, &dbg,
827 RELEASE_PROPS(inst_props.ppEnabledLayerNames, inst_props.enabledLayerCount);
846 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
return "integrated";
847 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
return "discrete";
848 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
return "virtual";
849 case VK_PHYSICAL_DEVICE_TYPE_CPU:
return "software";
850 default:
return "unknown";
857 int err = 0, choice = -1;
862 VkPhysicalDevice *devices =
NULL;
863 VkPhysicalDeviceIDProperties *idp =
NULL;
864 VkPhysicalDeviceProperties2 *prop =
NULL;
865 VkPhysicalDeviceDrmPropertiesEXT *drm_prop =
NULL;
868 ret = vk->EnumeratePhysicalDevices(hwctx->
inst, &num,
NULL);
869 if (
ret != VK_SUCCESS || !num) {
878 ret = vk->EnumeratePhysicalDevices(hwctx->
inst, &num, devices);
879 if (
ret != VK_SUCCESS) {
899 drm_prop =
av_calloc(num,
sizeof(*drm_prop));
907 for (
int i = 0;
i < num;
i++) {
909 drm_prop[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT;
910 idp[
i].pNext = &drm_prop[
i];
912 idp[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
913 prop[
i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
914 prop[
i].pNext = &idp[
i];
916 vk->GetPhysicalDeviceProperties2(devices[
i], &prop[
i]);
918 prop[
i].properties.deviceName,
920 prop[
i].properties.deviceID);
924 for (
int i = 0;
i < num;
i++) {
925 if (!strncmp(idp[
i].deviceUUID, select->
uuid, VK_UUID_SIZE)) {
934 for (
int i = 0;
i < num;
i++) {
935 if ((select->
drm_major == drm_prop[
i].primaryMajor &&
936 select->
drm_minor == drm_prop[
i].primaryMinor) ||
937 (select->
drm_major == drm_prop[
i].renderMajor &&
938 select->
drm_minor == drm_prop[
i].renderMinor)) {
947 }
else if (select->
name) {
949 for (
int i = 0;
i < num;
i++) {
950 if (strstr(prop[
i].properties.deviceName, select->
name)) {
961 for (
int i = 0;
i < num;
i++) {
962 if (select->
pci_device == prop[
i].properties.deviceID) {
973 for (
int i = 0;
i < num;
i++) {
974 if (select->
vendor_id == prop[
i].properties.vendorID) {
984 if (select->
index < num) {
985 choice = select->
index;
997 choice, prop[choice].properties.deviceName,
999 prop[choice].properties.deviceID);
1013 VkQueueFlagBits
flags)
1016 uint32_t min_score = UINT32_MAX;
1018 for (
int i = 0;
i < num_qf;
i++) {
1019 const VkQueueFlagBits qflags = qf[
i].queueFlags;
1020 if (qflags &
flags) {
1021 uint32_t score =
av_popcount(qflags) + qf[
i].timestampValidBits;
1022 if (score < min_score) {
1030 qf[
index].timestampValidBits++;
1039 VkQueueFamilyProperties *qf =
NULL;
1043 int graph_index, comp_index, tx_index, enc_index, dec_index;
1046 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &num,
NULL);
1058 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &num, qf);
1061 for (
int i = 0;
i < num;
i++) {
1063 ((qf[
i].queueFlags) & VK_QUEUE_GRAPHICS_BIT) ?
" graphics" :
"",
1064 ((qf[
i].queueFlags) & VK_QUEUE_COMPUTE_BIT) ?
" compute" :
"",
1065 ((qf[
i].queueFlags) & VK_QUEUE_TRANSFER_BIT) ?
" transfer" :
"",
1066 ((qf[
i].queueFlags) & VK_QUEUE_VIDEO_ENCODE_BIT_KHR) ?
" encode" :
"",
1067 ((qf[
i].queueFlags) & VK_QUEUE_VIDEO_DECODE_BIT_KHR) ?
" decode" :
"",
1068 ((qf[
i].queueFlags) & VK_QUEUE_SPARSE_BINDING_BIT) ?
" sparse" :
"",
1069 ((qf[
i].queueFlags) & VK_QUEUE_PROTECTED_BIT) ?
" protected" :
"",
1074 qf[
i].timestampValidBits = 0;
1097 #define SETUP_QUEUE(qf_idx) \
1098 if (qf_idx > -1) { \
1099 int fidx = qf_idx; \
1100 int qc = qf[fidx].queueCount; \
1101 VkDeviceQueueCreateInfo *pc; \
1103 if (fidx == graph_index) { \
1104 hwctx->queue_family_index = fidx; \
1105 hwctx->nb_graphics_queues = qc; \
1108 if (fidx == comp_index) { \
1109 hwctx->queue_family_comp_index = fidx; \
1110 hwctx->nb_comp_queues = qc; \
1113 if (fidx == tx_index) { \
1114 hwctx->queue_family_tx_index = fidx; \
1115 hwctx->nb_tx_queues = qc; \
1118 if (fidx == enc_index) { \
1119 hwctx->queue_family_encode_index = fidx; \
1120 hwctx->nb_encode_queues = qc; \
1123 if (fidx == dec_index) { \
1124 hwctx->queue_family_decode_index = fidx; \
1125 hwctx->nb_decode_queues = qc; \
1129 pc = av_realloc((void *)cd->pQueueCreateInfos, \
1130 sizeof(*pc) * (cd->queueCreateInfoCount + 1)); \
1133 return AVERROR(ENOMEM); \
1135 cd->pQueueCreateInfos = pc; \
1136 pc = &pc[cd->queueCreateInfoCount]; \
1138 weights = av_malloc(qc * sizeof(float)); \
1141 return AVERROR(ENOMEM); \
1144 memset(pc, 0, sizeof(*pc)); \
1145 pc->sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; \
1146 pc->queueFamilyIndex = fidx; \
1147 pc->queueCount = qc; \
1148 pc->pQueuePriorities = weights; \
1150 for (int i = 0; i < qc; i++) \
1151 weights[i] = 1.0f / qc; \
1153 cd->queueCreateInfoCount++; \
1184 vk->DestroyDebugUtilsMessengerEXT(hwctx->
inst, p->
debug_ctx,
1188 vk->DestroyInstance(hwctx->
inst, hwctx->
alloc);
1212 int disable_multiplane,
1227 VkPhysicalDeviceTimelineSemaphoreFeatures timeline_features = {
1228 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES,
1230 VkPhysicalDeviceCooperativeMatrixFeaturesKHR coop_matrix_features = {
1231 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR,
1232 .pNext = &timeline_features,
1234 VkPhysicalDeviceShaderAtomicFloatFeaturesEXT atomic_float_features = {
1235 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_FLOAT_FEATURES_EXT,
1236 .pNext = &coop_matrix_features,
1238 VkPhysicalDeviceDescriptorBufferFeaturesEXT desc_buf_features = {
1239 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_FEATURES_EXT,
1240 .pNext = &atomic_float_features,
1242 VkPhysicalDeviceVulkan13Features dev_features_1_3 = {
1243 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
1244 .pNext = &desc_buf_features,
1246 VkPhysicalDeviceVulkan12Features dev_features_1_2 = {
1247 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
1248 .pNext = &dev_features_1_3,
1250 VkPhysicalDeviceVulkan11Features dev_features_1_1 = {
1251 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES,
1252 .pNext = &dev_features_1_2,
1254 VkPhysicalDeviceFeatures2 dev_features = {
1255 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
1256 .pNext = &dev_features_1_1,
1259 VkDeviceCreateInfo dev_info = {
1260 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1263 hwctx->
device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1271 p->
desc_buf_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_FEATURES_EXT;
1273 p->
atomic_float_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_FLOAT_FEATURES_EXT;
1275 p->
coop_matrix_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
1288 vk->GetPhysicalDeviceFeatures2(hwctx->
phys_dev, &dev_features);
1291 #define COPY_FEATURE(DST, NAME) (DST).features.NAME = dev_features.features.NAME;
1303 if (!timeline_features.timelineSemaphore) {
1317 p->
device_features_1_2.storageBuffer8BitAccess = dev_features_1_2.storageBuffer8BitAccess;
1318 p->
device_features_1_2.uniformAndStorageBuffer8BitAccess = dev_features_1_2.uniformAndStorageBuffer8BitAccess;
1320 p->
device_features_1_2.shaderSharedInt64Atomics = dev_features_1_2.shaderSharedInt64Atomics;
1322 p->
device_features_1_2.vulkanMemoryModelDeviceScope = dev_features_1_2.vulkanMemoryModelDeviceScope;
1329 p->
device_features_1_3.shaderZeroInitializeWorkgroupMemory = dev_features_1_3.shaderZeroInitializeWorkgroupMemory;
1333 p->
desc_buf_features.descriptorBufferPushDescriptors = desc_buf_features.descriptorBufferPushDescriptors;
1335 p->
atomic_float_features.shaderBufferFloat32Atomics = atomic_float_features.shaderBufferFloat32Atomics;
1336 p->
atomic_float_features.shaderBufferFloat32AtomicAdd = atomic_float_features.shaderBufferFloat32AtomicAdd;
1347 &dev_info.enabledExtensionCount, 0))) {
1348 for (
int i = 0;
i < dev_info.queueCreateInfoCount;
i++)
1349 av_free((
void *)dev_info.pQueueCreateInfos[
i].pQueuePriorities);
1350 av_free((
void *)dev_info.pQueueCreateInfos);
1357 for (
int i = 0;
i < dev_info.queueCreateInfoCount;
i++)
1358 av_free((
void *)dev_info.pQueueCreateInfos[
i].pQueuePriorities);
1359 av_free((
void *)dev_info.pQueueCreateInfos);
1361 if (
ret != VK_SUCCESS) {
1364 for (
int i = 0;
i < dev_info.enabledExtensionCount;
i++)
1365 av_free((
void *)dev_info.ppEnabledExtensionNames[
i]);
1366 av_free((
void *)dev_info.ppEnabledExtensionNames);
1412 VkQueueFamilyProperties *qf;
1413 int graph_index, comp_index, tx_index, enc_index, dec_index;
1432 p->
props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
1434 p->
hprops.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT;
1436 vk->GetPhysicalDeviceProperties2(hwctx->
phys_dev, &p->
props);
1438 p->
props.properties.deviceName);
1441 p->
props.properties.limits.optimalBufferCopyRowPitchAlignment);
1443 p->
props.properties.limits.minMemoryMapAlignment);
1445 p->
props.properties.limits.nonCoherentAtomSize);
1448 p->
hprops.minImportedHostPointerAlignment);
1452 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &qf_num,
NULL);
1462 vk->GetPhysicalDeviceQueueFamilyProperties(hwctx->
phys_dev, &qf_num, qf);
1471 for (uint32_t
i = 0;
i < qf_num;
i++) {
1477 for (uint32_t j = 0; j < qf[
i].queueCount; j++) {
1496 #define CHECK_QUEUE(type, required, fidx, ctx_qf, qc) \
1498 if (ctx_qf < 0 && required) { \
1499 av_log(ctx, AV_LOG_ERROR, "%s queue family is required, but marked as missing" \
1500 " in the context!\n", type); \
1501 return AVERROR(EINVAL); \
1502 } else if (fidx < 0 || ctx_qf < 0) { \
1504 } else if (ctx_qf >= qf_num) { \
1505 av_log(ctx, AV_LOG_ERROR, "Invalid %s family index %i (device has %i families)!\n", \
1506 type, ctx_qf, qf_num); \
1507 return AVERROR(EINVAL); \
1510 av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i (queues: %i)" \
1511 " for%s%s%s%s%s\n", \
1513 ctx_qf == graph_index ? " graphics" : "", \
1514 ctx_qf == comp_index ? " compute" : "", \
1515 ctx_qf == tx_index ? " transfers" : "", \
1516 ctx_qf == enc_index ? " encode" : "", \
1517 ctx_qf == dec_index ? " decode" : ""); \
1518 graph_index = (ctx_qf == graph_index) ? -1 : graph_index; \
1519 comp_index = (ctx_qf == comp_index) ? -1 : comp_index; \
1520 tx_index = (ctx_qf == tx_index) ? -1 : tx_index; \
1521 enc_index = (ctx_qf == enc_index) ? -1 : enc_index; \
1522 dec_index = (ctx_qf == dec_index) ? -1 : dec_index; \
1523 p->img_qfs[p->nb_img_qfs++] = ctx_qf; \
1540 vk->GetPhysicalDeviceMemoryProperties(hwctx->
phys_dev, &p->
mprops);
1556 if (device && device[0]) {
1558 dev_select.
index = strtol(device, &end, 10);
1559 if (end == device) {
1560 dev_select.
index = 0;
1561 dev_select.
name = device;
1577 switch(src_ctx->
type) {
1582 const char *vendor = vaQueryVendorString(src_hwctx->
display);
1588 if (strstr(vendor,
"AMD"))
1589 dev_select.vendor_id = 0x1002;
1597 struct stat drm_node_info;
1598 drmDevice *drm_dev_info;
1601 err = fstat(src_hwctx->
fd, &drm_node_info);
1608 dev_select.drm_major = major(drm_node_info.st_dev);
1609 dev_select.drm_minor = minor(drm_node_info.st_dev);
1610 dev_select.has_drm = 1;
1612 err = drmGetDevice(src_hwctx->
fd, &drm_dev_info);
1619 if (drm_dev_info->bustype == DRM_BUS_PCI)
1620 dev_select.pci_device = drm_dev_info->deviceinfo.pci->device_id;
1622 drmFreeDevice(&drm_dev_info);
1632 CudaFunctions *cu = cu_internal->
cuda_dl;
1634 int ret =
CHECK_CU(cu->cuDeviceGetUuid((CUuuid *)&dev_select.uuid,
1641 dev_select.has_uuid = 1;
1656 const void *hwconfig,
1665 VK_IMAGE_TILING_OPTIMAL,
1678 VK_IMAGE_TILING_OPTIMAL,
1688 constraints->
max_width = p->
props.properties.limits.maxImageDimension2D;
1689 constraints->
max_height = p->
props.properties.limits.maxImageDimension2D;
1702 VkMemoryPropertyFlagBits req_flags,
const void *alloc_extension,
1703 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
1710 VkMemoryAllocateInfo alloc_info = {
1711 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1712 .pNext = alloc_extension,
1713 .allocationSize = req->size,
1718 for (
int i = 0;
i < p->
mprops.memoryTypeCount;
i++) {
1719 const VkMemoryType *
type = &p->
mprops.memoryTypes[
i];
1722 if (!(req->memoryTypeBits & (1 <<
i)))
1726 if ((
type->propertyFlags & req_flags) != req_flags)
1730 if (req->size > p->
mprops.memoryHeaps[
type->heapIndex].size)
1744 alloc_info.memoryTypeIndex =
index;
1746 ret = vk->AllocateMemory(dev_hwctx->
act_dev, &alloc_info,
1747 dev_hwctx->
alloc, mem);
1748 if (
ret != VK_SUCCESS) {
1754 *mem_flags |= p->
mprops.memoryTypes[
index].propertyFlags;
1764 if (internal->cuda_fc_ref) {
1770 CudaFunctions *cu = cu_internal->
cuda_dl;
1773 if (internal->cu_sem[
i])
1774 CHECK_CU(cu->cuDestroyExternalSemaphore(internal->cu_sem[
i]));
1775 if (internal->cu_mma[
i])
1776 CHECK_CU(cu->cuMipmappedArrayDestroy(internal->cu_mma[
i]));
1777 if (internal->ext_mem[
i])
1778 CHECK_CU(cu->cuDestroyExternalMemory(internal->ext_mem[
i]));
1780 if (internal->ext_sem_handle[
i])
1781 CloseHandle(internal->ext_sem_handle[
i]);
1782 if (internal->ext_mem_handle[
i])
1783 CloseHandle(internal->ext_mem_handle[
i]);
1803 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
1805 .pSemaphores =
f->sem,
1806 .pValues =
f->sem_value,
1807 .semaphoreCount = nb_images,
1814 for (
int i = 0;
i < nb_images;
i++) {
1829 void *alloc_pnext,
size_t alloc_pnext_stride)
1831 int img_cnt = 0, err;
1840 while (
f->img[img_cnt]) {
1842 VkImageMemoryRequirementsInfo2 req_desc = {
1843 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
1844 .image =
f->img[img_cnt],
1846 VkMemoryDedicatedAllocateInfo ded_alloc = {
1847 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
1848 .pNext = (
void *)(((uint8_t *)alloc_pnext) + img_cnt*alloc_pnext_stride),
1850 VkMemoryDedicatedRequirements ded_req = {
1851 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
1853 VkMemoryRequirements2 req = {
1854 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
1858 vk->GetImageMemoryRequirements2(hwctx->
act_dev, &req_desc, &req);
1860 if (
f->tiling == VK_IMAGE_TILING_LINEAR)
1861 req.memoryRequirements.size =
FFALIGN(req.memoryRequirements.size,
1862 p->
props.properties.limits.minMemoryMapAlignment);
1865 use_ded_mem = ded_req.prefersDedicatedAllocation |
1866 ded_req.requiresDedicatedAllocation;
1868 ded_alloc.image =
f->img[img_cnt];
1872 f->tiling == VK_IMAGE_TILING_LINEAR ?
1873 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
1874 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1875 use_ded_mem ? &ded_alloc : (
void *)ded_alloc.pNext,
1876 &
f->flags, &
f->mem[img_cnt])))
1879 f->size[img_cnt] = req.memoryRequirements.size;
1880 bind_info[img_cnt].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
1881 bind_info[img_cnt].image =
f->img[img_cnt];
1882 bind_info[img_cnt].memory =
f->mem[img_cnt];
1888 ret = vk->BindImageMemory2(hwctx->
act_dev, img_cnt, bind_info);
1889 if (
ret != VK_SUCCESS) {
1915 uint32_t dst_qf = VK_QUEUE_FAMILY_IGNORED;
1916 VkImageLayout new_layout;
1917 VkAccessFlags2 new_access;
1918 VkPipelineStageFlagBits2 src_stage = VK_PIPELINE_STAGE_2_NONE;
1924 .
data = (uint8_t *)hwfc,
1928 .hw_frames_ctx = &tmp_ref,
1931 VkCommandBuffer cmd_buf;
1933 cmd_buf = exec->
buf;
1937 VK_PIPELINE_STAGE_2_NONE,
1938 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
1944 new_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1945 new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
1948 new_layout = VK_IMAGE_LAYOUT_GENERAL;
1949 new_access = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
1952 new_layout = VK_IMAGE_LAYOUT_GENERAL;
1953 new_access = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
1954 dst_qf = VK_QUEUE_FAMILY_EXTERNAL_KHR;
1955 src_stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
1958 new_layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR;
1959 new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
1962 new_layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
1963 new_access = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
1969 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
1970 new_access, new_layout, dst_qf);
1972 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
1973 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
1974 .pImageMemoryBarriers = img_bar,
1975 .imageMemoryBarrierCount = nb_img_bar,
1989 int frame_w,
int frame_h,
int plane)
2006 VkImageTiling tiling, VkImageUsageFlagBits
usage,
2007 VkImageCreateFlags
flags,
int nb_layers,
2018 VkExportSemaphoreCreateInfo ext_sem_info = {
2019 .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
2021 .handleTypes = IsWindows8OrGreater()
2022 ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
2023 : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
2025 .handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
2029 VkSemaphoreTypeCreateInfo sem_type_info = {
2030 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
2036 .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
2040 VkSemaphoreCreateInfo sem_spawn = {
2041 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2042 .pNext = &sem_type_info,
2054 for (
int i = 0; (hwfc_vk->
format[
i] != VK_FORMAT_UNDEFINED);
i++) {
2055 VkImageCreateInfo create_info = {
2056 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
2057 .pNext = create_pnext,
2058 .imageType = VK_IMAGE_TYPE_2D,
2062 .arrayLayers = nb_layers,
2065 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
2067 .samples = VK_SAMPLE_COUNT_1_BIT,
2068 .pQueueFamilyIndices = p->
img_qfs,
2070 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2071 VK_SHARING_MODE_EXCLUSIVE,
2074 get_plane_wh(&create_info.extent.width, &create_info.extent.height,
2077 ret = vk->CreateImage(hwctx->
act_dev, &create_info,
2079 if (
ret != VK_SUCCESS) {
2087 ret = vk->CreateSemaphore(hwctx->
act_dev, &sem_spawn,
2089 if (
ret != VK_SUCCESS) {
2096 f->layout[
i] = create_info.initialLayout;
2098 f->sem_value[
i] = 0;
2114 VkExternalMemoryHandleTypeFlags *comp_handle_types,
2115 VkExternalMemoryHandleTypeFlagBits *iexp,
2116 VkExternalMemoryHandleTypeFlagBits
exp)
2124 const VkImageDrmFormatModifierListCreateInfoEXT *drm_mod_info =
2126 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
2127 int has_mods = hwctx->
tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT && drm_mod_info;
2130 VkExternalImageFormatProperties eprops = {
2131 .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
2133 VkImageFormatProperties2 props = {
2134 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
2137 VkPhysicalDeviceImageDrmFormatModifierInfoEXT phy_dev_mod_info = {
2138 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
2140 .pQueueFamilyIndices = p->
img_qfs,
2142 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2143 VK_SHARING_MODE_EXCLUSIVE,
2145 VkPhysicalDeviceExternalImageFormatInfo enext = {
2146 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
2148 .pNext = has_mods ? &phy_dev_mod_info :
NULL,
2150 VkPhysicalDeviceImageFormatInfo2 pinfo = {
2151 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
2152 .pNext = !
exp ?
NULL : &enext,
2154 .type = VK_IMAGE_TYPE_2D,
2156 .usage = hwctx->
usage,
2157 .flags = VK_IMAGE_CREATE_ALIAS_BIT,
2160 nb_mods = has_mods ? drm_mod_info->drmFormatModifierCount : 1;
2161 for (
int i = 0;
i < nb_mods;
i++) {
2163 phy_dev_mod_info.drmFormatModifier = drm_mod_info->pDrmFormatModifiers[
i];
2165 ret = vk->GetPhysicalDeviceImageFormatProperties2(dev_hwctx->
phys_dev,
2168 if (
ret == VK_SUCCESS) {
2170 *comp_handle_types |= eprops.externalMemoryProperties.compatibleHandleTypes;
2184 VkExternalMemoryHandleTypeFlags e = 0x0;
2187 VkExternalMemoryImageCreateInfo eiinfo = {
2188 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
2195 ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT
2196 : VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT);
2200 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
2204 eminfo[
i].sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
2206 eminfo[
i].handleTypes = e;
2219 if ( (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) &&
2220 !(hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR))
2222 else if (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)
2256 if (
fp->modifier_info) {
2257 if (
fp->modifier_info->pDrmFormatModifiers)
2258 av_freep(&
fp->modifier_info->pDrmFormatModifiers);
2274 VkImageUsageFlagBits supported_usage;
2285 (hwctx->
tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT))
2286 hwctx->
tiling = VK_IMAGE_TILING_LINEAR;
2296 if (hwctx->
format[0] != VK_FORMAT_UNDEFINED) {
2301 "for the current sw_format %s!\n",
2312 hwctx->
usage & VK_IMAGE_USAGE_STORAGE_BIT);
2321 NULL, &supported_usage,
2323 hwctx->
usage & VK_IMAGE_USAGE_STORAGE_BIT);
2329 if (!hwctx->
usage) {
2330 hwctx->
usage = supported_usage & (VK_BUFFER_USAGE_TRANSFER_DST_BIT |
2331 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
2332 VK_IMAGE_USAGE_STORAGE_BIT |
2333 VK_IMAGE_USAGE_SAMPLED_BIT);
2340 int is_lone_dpb = (hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) &&
2341 !(hwctx->
usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR);
2342 int sampleable = hwctx->
usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
2343 VK_IMAGE_USAGE_STORAGE_BIT);
2344 if (sampleable && !is_lone_dpb) {
2345 hwctx->
img_flags = VK_IMAGE_CREATE_ALIAS_BIT;
2347 hwctx->
img_flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT |
2348 VK_IMAGE_CREATE_EXTENDED_USAGE_BIT;
2439 static const struct {
2440 uint32_t drm_fourcc;
2442 } vulkan_drm_format_map[] = {
2443 { DRM_FORMAT_R8, VK_FORMAT_R8_UNORM },
2444 { DRM_FORMAT_R16, VK_FORMAT_R16_UNORM },
2445 { DRM_FORMAT_GR88, VK_FORMAT_R8G8_UNORM },
2446 { DRM_FORMAT_RG88, VK_FORMAT_R8G8_UNORM },
2447 { DRM_FORMAT_GR1616, VK_FORMAT_R16G16_UNORM },
2448 { DRM_FORMAT_RG1616, VK_FORMAT_R16G16_UNORM },
2449 { DRM_FORMAT_ARGB8888, VK_FORMAT_B8G8R8A8_UNORM },
2450 { DRM_FORMAT_XRGB8888, VK_FORMAT_B8G8R8A8_UNORM },
2451 { DRM_FORMAT_ABGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2452 { DRM_FORMAT_XBGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2455 #ifdef DRM_FORMAT_XYUV8888
2456 { DRM_FORMAT_XYUV8888, VK_FORMAT_R8G8B8A8_UNORM },
2457 { DRM_FORMAT_XVYU12_16161616, VK_FORMAT_R16G16B16A16_UNORM} ,
2460 { DRM_FORMAT_Y416, VK_FORMAT_R16G16B16A16_UNORM },
2464 static inline VkFormat drm_to_vulkan_fmt(uint32_t drm_fourcc)
2467 if (vulkan_drm_format_map[
i].drm_fourcc == drm_fourcc)
2468 return vulkan_drm_format_map[
i].vk_format;
2469 return VK_FORMAT_UNDEFINED;
2478 int bind_counts = 0;
2489 if (drm_to_vulkan_fmt(
desc->layers[
i].format) == VK_FORMAT_UNDEFINED) {
2491 desc->layers[
i].format);
2502 f->tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
2504 for (
int i = 0;
i <
desc->nb_layers;
i++) {
2508 VkSemaphoreTypeCreateInfo sem_type_info = {
2509 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
2510 .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
2513 VkSemaphoreCreateInfo sem_spawn = {
2514 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2515 .pNext = &sem_type_info,
2520 VkImageDrmFormatModifierExplicitCreateInfoEXT ext_img_mod_spec = {
2521 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
2522 .drmFormatModifier =
desc->objects[0].format_modifier,
2523 .drmFormatModifierPlaneCount =
planes,
2524 .pPlaneLayouts = (
const VkSubresourceLayout *)&ext_img_layouts,
2526 VkExternalMemoryImageCreateInfo ext_img_spec = {
2527 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
2528 .pNext = &ext_img_mod_spec,
2529 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
2531 VkImageCreateInfo create_info = {
2532 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
2533 .pNext = &ext_img_spec,
2534 .imageType = VK_IMAGE_TYPE_2D,
2535 .format = drm_to_vulkan_fmt(
desc->layers[
i].format),
2540 .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
2541 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
2542 .usage = VK_IMAGE_USAGE_SAMPLED_BIT |
2543 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
2544 .samples = VK_SAMPLE_COUNT_1_BIT,
2545 .pQueueFamilyIndices = p->
img_qfs,
2547 .sharingMode = p->
nb_img_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2548 VK_SHARING_MODE_EXCLUSIVE,
2552 VkExternalImageFormatProperties ext_props = {
2553 .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
2555 VkImageFormatProperties2 props_ret = {
2556 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
2557 .pNext = &ext_props,
2559 VkPhysicalDeviceImageDrmFormatModifierInfoEXT props_drm_mod = {
2560 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
2561 .drmFormatModifier = ext_img_mod_spec.drmFormatModifier,
2562 .pQueueFamilyIndices = create_info.pQueueFamilyIndices,
2563 .queueFamilyIndexCount = create_info.queueFamilyIndexCount,
2564 .sharingMode = create_info.sharingMode,
2566 VkPhysicalDeviceExternalImageFormatInfo props_ext = {
2567 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
2568 .pNext = &props_drm_mod,
2569 .handleType = ext_img_spec.handleTypes,
2571 VkPhysicalDeviceImageFormatInfo2 fmt_props = {
2572 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
2573 .pNext = &props_ext,
2574 .format = create_info.format,
2575 .type = create_info.imageType,
2576 .tiling = create_info.tiling,
2577 .usage = create_info.usage,
2578 .flags = create_info.flags,
2582 ret = vk->GetPhysicalDeviceImageFormatProperties2(hwctx->
phys_dev,
2583 &fmt_props, &props_ret);
2584 if (
ret != VK_SUCCESS) {
2592 get_plane_wh(&create_info.extent.width, &create_info.extent.height,
2596 for (
int j = 0; j <
planes; j++) {
2597 ext_img_layouts[j].offset =
desc->layers[
i].planes[j].offset;
2598 ext_img_layouts[j].rowPitch =
desc->layers[
i].planes[j].pitch;
2599 ext_img_layouts[j].size = 0;
2600 ext_img_layouts[j].arrayPitch = 0;
2601 ext_img_layouts[j].depthPitch = 0;
2605 ret = vk->CreateImage(hwctx->
act_dev, &create_info,
2607 if (
ret != VK_SUCCESS) {
2614 ret = vk->CreateSemaphore(hwctx->
act_dev, &sem_spawn,
2616 if (
ret != VK_SUCCESS) {
2628 f->layout[
i] = create_info.initialLayout;
2630 f->sem_value[
i] = 0;
2633 for (
int i = 0;
i <
desc->nb_layers;
i++) {
2635 VkImageMemoryRequirementsInfo2 req_desc = {
2636 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
2639 VkMemoryDedicatedRequirements ded_req = {
2640 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
2642 VkMemoryRequirements2 req2 = {
2643 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
2648 VkMemoryFdPropertiesKHR fdmp = {
2649 .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
2655 VkImportMemoryFdInfoKHR idesc = {
2656 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
2657 .fd = dup(
desc->objects[
desc->layers[
i].planes[0].object_index].fd),
2658 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
2660 VkMemoryDedicatedAllocateInfo ded_alloc = {
2661 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
2663 .image = req_desc.image,
2667 ret = vk->GetMemoryFdPropertiesKHR(hwctx->
act_dev,
2668 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
2670 if (
ret != VK_SUCCESS) {
2678 vk->GetImageMemoryRequirements2(hwctx->
act_dev, &req_desc, &req2);
2681 req2.memoryRequirements.memoryTypeBits = fdmp.memoryTypeBits;
2684 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
2685 (ded_req.prefersDedicatedAllocation ||
2686 ded_req.requiresDedicatedAllocation) ?
2687 &ded_alloc : ded_alloc.pNext,
2688 &
f->flags, &
f->mem[
i]);
2694 f->size[
i] = req2.memoryRequirements.size;
2697 for (
int i = 0;
i <
desc->nb_layers;
i++) {
2699 for (
int j = 0; j <
planes; j++) {
2700 VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
2701 j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :
2702 VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT;
2704 plane_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO;
2706 plane_info[bind_counts].planeAspect = aspect;
2708 bind_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
2710 bind_info[bind_counts].image =
f->img[
i];
2711 bind_info[bind_counts].memory =
f->mem[
i];
2714 bind_info[bind_counts].memoryOffset = 0;
2721 ret = vk->BindImageMemory2(hwctx->
act_dev, bind_counts, bind_info);
2722 if (
ret != VK_SUCCESS) {
2738 for (
int i = 0;
i <
desc->nb_layers;
i++) {
2742 for (
int i = 0;
i <
desc->nb_objects;
i++)
2756 if ((err = vulkan_map_from_drm_frame_desc(hwfc, &
f,
src)))
2760 dst->
data[0] = (uint8_t *)
f;
2765 &vulkan_unmap_from_drm,
f);
2788 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)
src->data[3];
2794 vaSyncSurface(vaapi_ctx->display, surface_id);
2802 err = vulkan_map_from_drm(dst_fc, dst,
tmp,
flags);
2835 CudaFunctions *cu = cu_internal->
cuda_dl;
2836 CUarray_format cufmt =
desc->comp[0].depth > 8 ? CU_AD_FORMAT_UNSIGNED_INT16 :
2837 CU_AD_FORMAT_UNSIGNED_INT8;
2842 if (!dst_int->cuda_fc_ref) {
2844 if (!dst_int->cuda_fc_ref)
2848 CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC tex_desc = {
2853 .NumChannels = 1 + ((
planes == 2) &&
i),
2861 CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
2862 .type = IsWindows8OrGreater()
2863 ? CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32
2864 : CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT,
2865 .size = dst_f->
size[
i],
2867 VkMemoryGetWin32HandleInfoKHR export_info = {
2868 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR,
2869 .memory = dst_f->
mem[
i],
2870 .handleType = IsWindows8OrGreater()
2871 ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT
2872 : VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
2874 VkSemaphoreGetWin32HandleInfoKHR sem_export = {
2875 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR,
2876 .semaphore = dst_f->
sem[
i],
2877 .handleType = IsWindows8OrGreater()
2878 ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
2879 : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
2881 CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
2885 ret = vk->GetMemoryWin32HandleKHR(hwctx->
act_dev, &export_info,
2886 &ext_desc.handle.win32.handle);
2887 if (
ret != VK_SUCCESS) {
2893 dst_int->ext_mem_handle[
i] = ext_desc.handle.win32.handle;
2895 CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
2896 .type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
2897 .size = dst_f->
size[
i],
2899 VkMemoryGetFdInfoKHR export_info = {
2900 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
2901 .memory = dst_f->
mem[
i],
2902 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
2904 VkSemaphoreGetFdInfoKHR sem_export = {
2905 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
2906 .semaphore = dst_f->
sem[
i],
2907 .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
2909 CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
2913 ret = vk->GetMemoryFdKHR(hwctx->
act_dev, &export_info,
2914 &ext_desc.handle.fd);
2915 if (
ret != VK_SUCCESS) {
2923 ret =
CHECK_CU(cu->cuImportExternalMemory(&dst_int->ext_mem[
i], &ext_desc));
2926 close(ext_desc.handle.fd);
2933 tex_desc.arrayDesc.Width = p_w;
2934 tex_desc.arrayDesc.Height = p_h;
2936 ret =
CHECK_CU(cu->cuExternalMemoryGetMappedMipmappedArray(&dst_int->cu_mma[
i],
2937 dst_int->ext_mem[
i],
2944 ret =
CHECK_CU(cu->cuMipmappedArrayGetLevel(&dst_int->cu_array[
i],
2945 dst_int->cu_mma[
i], 0));
2952 ret = vk->GetSemaphoreWin32HandleKHR(hwctx->
act_dev, &sem_export,
2953 &ext_sem_desc.handle.win32.handle);
2955 ret = vk->GetSemaphoreFdKHR(hwctx->
act_dev, &sem_export,
2956 &ext_sem_desc.handle.fd);
2958 if (
ret != VK_SUCCESS) {
2965 dst_int->ext_sem_handle[
i] = ext_sem_desc.handle.win32.handle;
2968 ret =
CHECK_CU(cu->cuImportExternalSemaphore(&dst_int->cu_sem[
i],
2972 close(ext_sem_desc.handle.fd);
3002 CudaFunctions *cu = cu_internal->
cuda_dl;
3012 err =
CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
3016 err = vulkan_export_to_cuda(hwfc,
src->hw_frames_ctx, dst);
3025 s_w_par[
i].params.fence.value = dst_f->
sem_value[
i] + 0;
3026 s_s_par[
i].params.fence.value = dst_f->
sem_value[
i] + 1;
3029 err =
CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
3030 planes, cuda_dev->stream));
3035 CUDA_MEMCPY2D cpy = {
3036 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
3037 .srcDevice = (CUdeviceptr)
src->data[
i],
3038 .srcPitch =
src->linesize[
i],
3041 .dstMemoryType = CU_MEMORYTYPE_ARRAY,
3042 .dstArray = dst_int->cu_array[
i],
3048 cpy.WidthInBytes = p_w *
desc->comp[
i].step;
3051 err =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
3056 err =
CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
3057 planes, cuda_dev->stream));
3083 switch (
src->format) {
3088 return vulkan_map_from_vaapi(hwfc, dst,
src,
flags);
3094 return vulkan_map_from_drm(hwfc, dst,
src,
flags);
3104 typedef struct VulkanDRMMapping {
3119 static inline uint32_t vulkan_fmt_to_drm(
VkFormat vkfmt)
3122 if (vulkan_drm_format_map[
i].vk_format == vkfmt)
3123 return vulkan_drm_format_map[
i].drm_fourcc;
3124 return DRM_FORMAT_INVALID;
3139 VkImageDrmFormatModifierPropertiesEXT drm_mod = {
3140 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
3142 VkSemaphoreWaitInfo wait_info = {
3143 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
3145 .semaphoreCount =
planes,
3157 wait_info.pSemaphores =
f->sem;
3158 wait_info.pValues =
f->sem_value;
3160 vk->WaitSemaphores(hwctx->
act_dev, &wait_info, UINT64_MAX);
3166 ret = vk->GetImageDrmFormatModifierPropertiesEXT(hwctx->
act_dev,
f->img[0],
3168 if (
ret != VK_SUCCESS) {
3174 for (
int i = 0; (
i <
planes) && (
f->mem[
i]);
i++) {
3175 VkMemoryGetFdInfoKHR export_info = {
3176 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
3177 .memory =
f->mem[
i],
3178 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
3181 ret = vk->GetMemoryFdKHR(hwctx->
act_dev, &export_info,
3183 if (
ret != VK_SUCCESS) {
3196 VkSubresourceLayout
layout;
3197 VkImageSubresource sub = {
3198 .aspectMask = VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT,
3202 drm_desc->
layers[
i].
format = vulkan_fmt_to_drm(plane_vkfmt);
3213 if (
f->tiling == VK_IMAGE_TILING_OPTIMAL)
3216 vk->GetImageSubresourceLayout(hwctx->
act_dev,
f->img[
i], &sub, &
layout);
3226 dst->
data[0] = (uint8_t *)drm_desc;
3274 return vulkan_map_to_drm(hwfc, dst,
src,
flags);
3280 return vulkan_map_to_vaapi(hwfc, dst,
src,
flags);
3302 const int *buf_stride,
int w,
3317 VkCommandBuffer cmd_buf;
3320 cmd_buf = exec->
buf;
3328 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
3329 VK_PIPELINE_STAGE_2_TRANSFER_BIT);
3334 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
3335 VK_PIPELINE_STAGE_2_TRANSFER_BIT_KHR,
3336 to_buf ? VK_ACCESS_TRANSFER_READ_BIT :
3337 VK_ACCESS_TRANSFER_WRITE_BIT,
3338 to_buf ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL :
3339 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
3340 VK_QUEUE_FAMILY_IGNORED);
3342 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
3343 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
3344 .pImageMemoryBarriers = img_bar,
3345 .imageMemoryBarrierCount = nb_img_bar,
3349 for (
int i = 0;
i < pixfmt_planes;
i++) {
3350 int idx =
FFMIN(
i, nb_images - 1);
3351 VkImageAspectFlags plane_aspect[] = { VK_IMAGE_ASPECT_COLOR_BIT,
3352 VK_IMAGE_ASPECT_PLANE_0_BIT,
3353 VK_IMAGE_ASPECT_PLANE_1_BIT,
3354 VK_IMAGE_ASPECT_PLANE_2_BIT, };
3357 VkBufferImageCopy buf_reg = {
3358 .bufferOffset = buf_offsets[
i],
3359 .bufferRowLength = buf_stride[
i] /
desc->comp[
i].step,
3360 .imageSubresource.layerCount = 1,
3361 .imageSubresource.aspectMask = plane_aspect[(pixfmt_planes != nb_images) +
3362 i*(pixfmt_planes != nb_images)],
3363 .imageOffset = { 0, 0, 0, },
3369 buf_reg.bufferImageHeight = p_h;
3370 buf_reg.imageExtent = (VkExtent3D){ p_w, p_h, 1, };
3373 vk->CmdCopyImageToBuffer(cmd_buf,
frame->img[idx],
3374 img_bar[0].newLayout,
3378 vk->CmdCopyBufferToImage(cmd_buf, vkbuf->
buf,
frame->img[idx],
3379 img_bar[0].newLayout,
3427 VkExternalMemoryBufferCreateInfo create_desc = {
3428 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
3429 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
3432 VkImportMemoryHostPointerInfoEXT import_desc = {
3433 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT,
3434 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
3437 VkMemoryHostPointerPropertiesEXT p_props = {
3438 .sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT,
3448 offs = (uintptr_t)swf->
data[
i] % p->
hprops.minImportedHostPointerAlignment;
3449 import_desc.pHostPointer = swf->
data[
i] - offs;
3454 p->
hprops.minImportedHostPointerAlignment);
3456 ret = vk->GetMemoryHostPointerPropertiesEXT(hwctx->
act_dev,
3457 import_desc.handleType,
3458 import_desc.pHostPointer,
3460 if (
ret == VK_SUCCESS && p_props.memoryTypeBits) {
3462 buf_offsets[
i] = offs;
3466 if (!host_mapped[
i])
3470 host_mapped[
i] ? &create_desc :
NULL,
3471 host_mapped[
i] ? &import_desc :
NULL,
3472 from ? VK_BUFFER_USAGE_TRANSFER_DST_BIT :
3473 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
3474 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
3476 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT : 0x0));
3521 (
const uint8_t *)
tmp.data[
i],
tmp.linesize[
i],
3542 switch (
src->format) {
3546 if ((p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_MEMORY) &&
3547 (p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_SEM))
3552 return vulkan_transfer_data_from_cuda(hwfc, dst,
src);
3555 if (
src->hw_frames_ctx)
3578 CudaFunctions *cu = cu_internal->
cuda_dl;
3588 err =
CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
3601 s_w_par[
i].params.fence.value = dst_f->
sem_value[
i] + 0;
3602 s_s_par[
i].params.fence.value = dst_f->
sem_value[
i] + 1;
3605 err =
CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
3606 planes, cuda_dev->stream));
3611 CUDA_MEMCPY2D cpy = {
3612 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
3613 .dstDevice = (CUdeviceptr)dst->
data[
i],
3617 .srcMemoryType = CU_MEMORYTYPE_ARRAY,
3618 .srcArray = dst_int->cu_array[
i],
3624 cpy.WidthInBytes =
w *
desc->comp[
i].step;
3627 err =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
3632 err =
CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
3633 planes, cuda_dev->stream));
3663 if ((p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_MEMORY) &&
3664 (p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_SEM))
3669 return vulkan_transfer_data_to_cuda(hwfc, dst,
src);