Speeds up both use cases by 30%.
Unified · Split
| @@ -62,8 +62,9 @@ typedef struct VulkanExecCtx { · 61 unmodified lines | |||
| 62 | 62 | ||
| 63 | typedef struct VulkanDevicePriv { | 63 | typedef struct VulkanDevicePriv { |
| 64 | /* Properties */ | 64 | /* Properties */ |
| 65 | VkPhysicalDeviceProperties props; | 65 | VkPhysicalDeviceProperties2 props; |
| 66 | VkPhysicalDeviceMemoryProperties mprops; | 66 | VkPhysicalDeviceMemoryProperties mprops; |
| 67 | VkPhysicalDeviceExternalMemoryHostPropertiesEXT hprops; | ||
| 67 | 68 | ||
| 68 | /* Queues */ | 69 | /* Queues */ |
| 69 | uint32_t qfs[3]; | 70 | uint32_t qfs[3]; |
| @@ -208,6 +209,7 @@ enum VulkanExtensions { · 138 unmodified lines | |||
| 208 | EXT_DRM_MODIFIER_FLAGS = 1ULL << 1, /* VK_EXT_image_drm_format_modifier */ | 209 | EXT_DRM_MODIFIER_FLAGS = 1ULL << 1, /* VK_EXT_image_drm_format_modifier */ |
| 209 | EXT_EXTERNAL_FD_MEMORY = 1ULL << 2, /* VK_KHR_external_memory_fd */ | 210 | EXT_EXTERNAL_FD_MEMORY = 1ULL << 2, /* VK_KHR_external_memory_fd */ |
| 210 | EXT_EXTERNAL_FD_SEM = 1ULL << 3, /* VK_KHR_external_semaphore_fd */ | 211 | EXT_EXTERNAL_FD_SEM = 1ULL << 3, /* VK_KHR_external_semaphore_fd */ |
| 212 | EXT_EXTERNAL_HOST_MEMORY = 1ULL << 4, /* VK_EXT_external_memory_host */ | ||
| 211 | 213 | ||
| 212 | EXT_NO_FLAG = 1ULL << 63, | 214 | EXT_NO_FLAG = 1ULL << 63, |
| 213 | }; | 215 | }; |
| @@ -226,6 +228,7 @@ static const VulkanOptExtension optional_device_exts[] = { · 12 unmodified lines | |||
| 226 | { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME, EXT_EXTERNAL_DMABUF_MEMORY, }, | 228 | { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME, EXT_EXTERNAL_DMABUF_MEMORY, }, |
| 227 | { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, EXT_DRM_MODIFIER_FLAGS, }, | 229 | { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, EXT_DRM_MODIFIER_FLAGS, }, |
| 228 | { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, EXT_EXTERNAL_FD_SEM, }, | 230 | { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, EXT_EXTERNAL_FD_SEM, }, |
| 231 | { VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME, EXT_EXTERNAL_HOST_MEMORY, }, | ||
| 229 | }; | 232 | }; |
| 230 | 233 | ||
| 231 | /* Converts return values to strings */ | 234 | /* Converts return values to strings */ |
| @@ -1052,16 +1055,6 @@ static int vulkan_device_init(AVHWDeviceContext *ctx) · 820 unmodified lines | |||
| 1052 | AVVulkanDeviceContext *hwctx = ctx->hwctx; | 1055 | AVVulkanDeviceContext *hwctx = ctx->hwctx; |
| 1053 | VulkanDevicePriv *p = ctx->internal->priv; | 1056 | VulkanDevicePriv *p = ctx->internal->priv; |
| 1054 | 1057 | ||
| 1055 | vkGetPhysicalDeviceProperties(hwctx->phys_dev, &p->props); | ||
| 1056 | av_log(ctx, AV_LOG_VERBOSE, "Using device: %s\n", p->props.deviceName); | ||
| 1057 | av_log(ctx, AV_LOG_VERBOSE, "Alignments:\n"); | ||
| 1058 | av_log(ctx, AV_LOG_VERBOSE, " optimalBufferCopyOffsetAlignment: %li\n", | ||
| 1059 | p->props.limits.optimalBufferCopyOffsetAlignment); | ||
| 1060 | av_log(ctx, AV_LOG_VERBOSE, " optimalBufferCopyRowPitchAlignment: %li\n", | ||
| 1061 | p->props.limits.optimalBufferCopyRowPitchAlignment); | ||
| 1062 | av_log(ctx, AV_LOG_VERBOSE, " minMemoryMapAlignment: %li\n", | ||
| 1063 | p->props.limits.minMemoryMapAlignment); | ||
| 1064 | |||
| 1065 | /* Set device extension flags */ | 1058 | /* Set device extension flags */ |
| 1066 | for (int i = 0; i < hwctx->nb_enabled_dev_extensions; i++) { | 1059 | for (int i = 0; i < hwctx->nb_enabled_dev_extensions; i++) { |
| 1067 | for (int j = 0; j < FF_ARRAY_ELEMS(optional_device_exts); j++) { | 1060 | for (int j = 0; j < FF_ARRAY_ELEMS(optional_device_exts); j++) { |
| @@ -1075,7 +1068,23 @@ static int vulkan_device_init(AVHWDeviceContext *ctx) · 7 unmodified lines | |||
| 1075 | } | 1068 | } |
| 1076 | } | 1069 | } |
| 1077 | 1070 | ||
| 1078 | p->dev_is_nvidia = (p->props.vendorID == 0x10de); | 1071 | p->props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| 1072 | p->props.pNext = &p->hprops; | ||
| 1073 | p->hprops.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT; | ||
| 1074 | |||
| 1075 | vkGetPhysicalDeviceProperties2(hwctx->phys_dev, &p->props); | ||
| 1076 | av_log(ctx, AV_LOG_VERBOSE, "Using device: %s\n", | ||
| 1077 | p->props.properties.deviceName); | ||
| 1078 | av_log(ctx, AV_LOG_VERBOSE, "Alignments:\n"); | ||
| 1079 | av_log(ctx, AV_LOG_VERBOSE, " optimalBufferCopyRowPitchAlignment: %li\n", | ||
| 1080 | p->props.properties.limits.optimalBufferCopyRowPitchAlignment); | ||
| 1081 | av_log(ctx, AV_LOG_VERBOSE, " minMemoryMapAlignment: %li\n", | ||
| 1082 | p->props.properties.limits.minMemoryMapAlignment); | ||
| 1083 | if (p->extensions & EXT_EXTERNAL_HOST_MEMORY) | ||
| 1084 | av_log(ctx, AV_LOG_VERBOSE, " minImportedHostPointerAlignment: %li\n", | ||
| 1085 | p->hprops.minImportedHostPointerAlignment); | ||
| 1086 | |||
| 1087 | p->dev_is_nvidia = (p->props.properties.vendorID == 0x10de); | ||
| 1079 | 1088 | ||
| 1080 | vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &queue_num, NULL); | 1089 | vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &queue_num, NULL); |
| 1081 | if (!queue_num) { | 1090 | if (!queue_num) { |
| @@ -1231,8 +1240,8 @@ static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx, · 149 unmodified lines | |||
| 1231 | 1240 | ||
| 1232 | constraints->min_width = 0; | 1241 | constraints->min_width = 0; |
| 1233 | constraints->min_height = 0; | 1242 | constraints->min_height = 0; |
| 1234 | constraints->max_width = p->props.limits.maxImageDimension2D; | 1243 | constraints->max_width = p->props.properties.limits.maxImageDimension2D; |
| 1235 | constraints->max_height = p->props.limits.maxImageDimension2D; | 1244 | constraints->max_height = p->props.properties.limits.maxImageDimension2D; |
| 1236 | 1245 | ||
| 1237 | constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum AVPixelFormat)); | 1246 | constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum AVPixelFormat)); |
| 1238 | if (!constraints->valid_hw_formats) | 1247 | if (!constraints->valid_hw_formats) |
| @@ -1253,16 +1262,11 @@ static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req, · 14 unmodified lines | |||
| 1253 | VulkanDevicePriv *p = ctx->internal->priv; | 1262 | VulkanDevicePriv *p = ctx->internal->priv; |
| 1254 | AVVulkanDeviceContext *dev_hwctx = ctx->hwctx; | 1263 | AVVulkanDeviceContext *dev_hwctx = ctx->hwctx; |
| 1255 | VkMemoryAllocateInfo alloc_info = { | 1264 | VkMemoryAllocateInfo alloc_info = { |
| 1256 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | 1265 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 1257 | .pNext = alloc_extension, | 1266 | .pNext = alloc_extension, |
| 1267 | .allocationSize = req->size, | ||
| 1258 | }; | 1268 | }; |
| 1259 | 1269 | ||
| 1260 | /* Align if we need to */ | ||
| 1261 | if (req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) | ||
| 1262 | req->size = FFALIGN(req->size, p->props.limits.minMemoryMapAlignment); | ||
| 1263 | |||
| 1264 | alloc_info.allocationSize = req->size; | ||
| 1265 | |||
| 1266 | /* The vulkan spec requires memory types to be sorted in the "optimal" | 1270 | /* The vulkan spec requires memory types to be sorted in the "optimal" |
| 1267 | * order, so the first matching type we find will be the best/fastest one */ | 1271 | * order, so the first matching type we find will be the best/fastest one */ |
| 1268 | for (int i = 0; i < p->mprops.memoryTypeCount; i++) { | 1272 | for (int i = 0; i < p->mprops.memoryTypeCount; i++) { |
| @@ -1354,6 +1358,7 @@ static int alloc_bind_mem(AVHWFramesContext *hwfc, AVVkFrame *f, · 85 unmodified lines | |||
| 1354 | int err; | 1358 | int err; |
| 1355 | VkResult ret; | 1359 | VkResult ret; |
| 1356 | AVHWDeviceContext *ctx = hwfc->device_ctx; | 1360 | AVHWDeviceContext *ctx = hwfc->device_ctx; |
| 1361 | VulkanDevicePriv *p = ctx->internal->priv; | ||
| 1357 | const int planes = av_pix_fmt_count_planes(hwfc->sw_format); | 1362 | const int planes = av_pix_fmt_count_planes(hwfc->sw_format); |
| 1358 | VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { { 0 } }; | 1363 | VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { { 0 } }; |
| 1359 | 1364 | ||
| @@ -1379,6 +1384,10 @@ static int alloc_bind_mem(AVHWFramesContext *hwfc, AVVkFrame *f, · 19 unmodified lines | |||
| 1379 | 1384 | ||
| 1380 | vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req); | 1385 | vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req); |
| 1381 | 1386 | ||
| 1387 | if (f->tiling == VK_IMAGE_TILING_LINEAR) | ||
| 1388 | req.memoryRequirements.size = FFALIGN(req.memoryRequirements.size, | ||
| 1389 | p->props.properties.limits.minMemoryMapAlignment); | ||
| 1390 | |||
| 1382 | /* In case the implementation prefers/requires dedicated allocation */ | 1391 | /* In case the implementation prefers/requires dedicated allocation */ |
| 1383 | use_ded_mem = ded_req.prefersDedicatedAllocation | | 1392 | use_ded_mem = ded_req.prefersDedicatedAllocation | |
| 1384 | ded_req.requiresDedicatedAllocation; | 1393 | ded_req.requiresDedicatedAllocation; |
| @@ -2630,6 +2639,7 @@ typedef struct ImageBuffer { · 1245 unmodified lines | |||
| 2630 | VkBuffer buf; | 2639 | VkBuffer buf; |
| 2631 | VkDeviceMemory mem; | 2640 | VkDeviceMemory mem; |
| 2632 | VkMemoryPropertyFlagBits flags; | 2641 | VkMemoryPropertyFlagBits flags; |
| 2642 | int mapped_mem; | ||
| 2633 | } ImageBuffer; | 2643 | } ImageBuffer; |
| 2634 | 2644 | ||
| 2635 | static void free_buf(void *opaque, uint8_t *data) | 2645 | static void free_buf(void *opaque, uint8_t *data) |
| @@ -2646,7 +2656,7 @@ static void free_buf(void *opaque, uint8_t *data) · 10 unmodified lines | |||
| 2646 | av_free(data); | 2656 | av_free(data); |
| 2647 | } | 2657 | } |
| 2648 | 2658 | ||
| 2649 | static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, | 2659 | static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, size_t imp_size, |
| 2650 | int height, int *stride, VkBufferUsageFlags usage, | 2660 | int height, int *stride, VkBufferUsageFlags usage, |
| 2651 | VkMemoryPropertyFlagBits flags, void *create_pnext, | 2661 | VkMemoryPropertyFlagBits flags, void *create_pnext, |
| 2652 | void *alloc_pnext) | 2662 | void *alloc_pnext) |
| @@ -2668,8 +2678,15 @@ static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, · 15 unmodified lines | |||
| 2668 | if (!vkbuf) | 2678 | if (!vkbuf) |
| 2669 | return AVERROR(ENOMEM); | 2679 | return AVERROR(ENOMEM); |
| 2670 | 2680 | ||
| 2671 | *stride = FFALIGN(*stride, p->props.limits.optimalBufferCopyRowPitchAlignment); | 2681 | vkbuf->mapped_mem = !!imp_size; |
| 2672 | buf_spawn.size = height*(*stride); | 2682 | |
| 2683 | if (!vkbuf->mapped_mem) { | ||
| 2684 | *stride = FFALIGN(*stride, p->props.properties.limits.optimalBufferCopyRowPitchAlignment); | ||
| 2685 | buf_spawn.size = height*(*stride); | ||
| 2686 | buf_spawn.size = FFALIGN(buf_spawn.size, p->props.properties.limits.minMemoryMapAlignment); | ||
| 2687 | } else { | ||
| 2688 | buf_spawn.size = imp_size; | ||
| 2689 | } | ||
| 2673 | 2690 | ||
| 2674 | ret = vkCreateBuffer(hwctx->act_dev, &buf_spawn, NULL, &vkbuf->buf); | 2691 | ret = vkCreateBuffer(hwctx->act_dev, &buf_spawn, NULL, &vkbuf->buf); |
| 2675 | if (ret != VK_SUCCESS) { | 2692 | if (ret != VK_SUCCESS) { |
| @@ -2701,6 +2718,7 @@ static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, · 25 unmodified lines | |||
| 2701 | return 0; | 2718 | return 0; |
| 2702 | } | 2719 | } |
| 2703 | 2720 | ||
| 2721 | /* Skips mapping of host mapped buffers but still invalidates them */ | ||
| 2704 | static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[], | 2722 | static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[], |
| 2705 | int nb_buffers, int invalidate) | 2723 | int nb_buffers, int invalidate) |
| 2706 | { | 2724 | { |
| @@ -2711,6 +2729,9 @@ static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[ · 4 unmodified lines | |||
| 2711 | 2729 | ||
| 2712 | for (int i = 0; i < nb_buffers; i++) { | 2730 | for (int i = 0; i < nb_buffers; i++) { |
| 2713 | ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data; | 2731 | ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data; |
| 2732 | if (vkbuf->mapped_mem) | ||
| 2733 | continue; | ||
| 2734 | |||
| 2714 | ret = vkMapMemory(hwctx->act_dev, vkbuf->mem, 0, | 2735 | ret = vkMapMemory(hwctx->act_dev, vkbuf->mem, 0, |
| 2715 | VK_WHOLE_SIZE, 0, (void **)&mem[i]); | 2736 | VK_WHOLE_SIZE, 0, (void **)&mem[i]); |
| 2716 | if (ret != VK_SUCCESS) { | 2737 | if (ret != VK_SUCCESS) { |
| @@ -2780,6 +2801,9 @@ static int unmap_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, · 63 unmodified lines | |||
| 2780 | 2801 | ||
| 2781 | for (int i = 0; i < nb_buffers; i++) { | 2802 | for (int i = 0; i < nb_buffers; i++) { |
| 2782 | ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data; | 2803 | ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data; |
| 2804 | if (vkbuf->mapped_mem) | ||
| 2805 | continue; | ||
| 2806 | |||
| 2783 | vkUnmapMemory(hwctx->act_dev, vkbuf->mem); | 2807 | vkUnmapMemory(hwctx->act_dev, vkbuf->mem); |
| 2784 | } | 2808 | } |
| 2785 | 2809 | ||
| @@ -2901,11 +2925,6 @@ static int transfer_image_buf(AVHWFramesContext *hwfc, const AVFrame *f, · 115 unmodified lines | |||
| 2901 | } | 2925 | } |
| 2902 | } | 2926 | } |
| 2903 | 2927 | ||
| 2904 | /* Technically we can use VK_EXT_external_memory_host to upload and download, | ||
| 2905 | * however the alignment requirements make this unfeasible as both the pointer | ||
| 2906 | * and the size of each plane need to be aligned to the minimum alignment | ||
| 2907 | * requirement, which on all current implementations (anv, radv) is 4096. | ||
| 2908 | * If the requirement gets relaxed (unlikely) this can easily be implemented. */ | ||
| 2909 | static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, | 2928 | static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, |
| 2910 | const AVFrame *src) | 2929 | const AVFrame *src) |
| 2911 | { | 2930 | { |
| @@ -2916,6 +2935,9 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 4 unmodified lines | |||
| 2916 | AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 }; | 2935 | AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 }; |
| 2917 | const int planes = av_pix_fmt_count_planes(src->format); | 2936 | const int planes = av_pix_fmt_count_planes(src->format); |
| 2918 | int log2_chroma = av_pix_fmt_desc_get(src->format)->log2_chroma_h; | 2937 | int log2_chroma = av_pix_fmt_desc_get(src->format)->log2_chroma_h; |
| 2938 | VulkanDevicePriv *p = hwfc->device_ctx->internal->priv; | ||
| 2939 | int host_mapped[AV_NUM_DATA_POINTERS] = { 0 }; | ||
| 2940 | int map_host = p->extensions & EXT_EXTERNAL_HOST_MEMORY; | ||
| 2919 | 2941 | ||
| 2920 | if ((src->format != AV_PIX_FMT_NONE && !av_vkfmt_from_pixfmt(src->format))) { | 2942 | if ((src->format != AV_PIX_FMT_NONE && !av_vkfmt_from_pixfmt(src->format))) { |
| 2921 | av_log(hwfc, AV_LOG_ERROR, "Unsupported source pixel format!\n"); | 2943 | av_log(hwfc, AV_LOG_ERROR, "Unsupported source pixel format!\n"); |
| @@ -2946,11 +2968,27 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 24 unmodified lines | |||
| 2946 | for (int i = 0; i < planes; i++) { | 2968 | for (int i = 0; i < planes; i++) { |
| 2947 | int h = src->height; | 2969 | int h = src->height; |
| 2948 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; | 2970 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; |
| 2971 | size_t p_size = FFABS(src->linesize[i]) * p_height; | ||
| 2972 | |||
| 2973 | VkImportMemoryHostPointerInfoEXT import_desc = { | ||
| 2974 | .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT, | ||
| 2975 | .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, | ||
| 2976 | .pHostPointer = src->data[i], | ||
| 2977 | }; | ||
| 2978 | |||
| 2979 | /* We can only map images with positive stride and alignment appropriate | ||
| 2980 | * for the device. */ | ||
| 2981 | host_mapped[i] = map_host && src->linesize[i] > 0 && | ||
| 2982 | !(p_size % p->hprops.minImportedHostPointerAlignment) && | ||
| 2983 | !(((uintptr_t)import_desc.pHostPointer) % | ||
| 2984 | p->hprops.minImportedHostPointerAlignment); | ||
| 2985 | p_size = host_mapped[i] ? p_size : 0; | ||
| 2949 | 2986 | ||
| 2950 | tmp.linesize[i] = FFABS(src->linesize[i]); | 2987 | tmp.linesize[i] = FFABS(src->linesize[i]); |
| 2951 | err = create_buf(dev_ctx, &bufs[i], p_height, | 2988 | err = create_buf(dev_ctx, &bufs[i], p_size, p_height, &tmp.linesize[i], |
| 2952 | &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_SRC_BIT, | 2989 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, |
| 2953 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL); | 2990 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, |
| 2991 | host_mapped[i] ? &import_desc : NULL); | ||
| 2954 | if (err) | 2992 | if (err) |
| 2955 | goto end; | 2993 | goto end; |
| 2956 | } | 2994 | } |
| @@ -2959,8 +2997,17 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 2 unmodified lines | |||
| 2959 | if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 0))) | 2997 | if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 0))) |
| 2960 | goto end; | 2998 | goto end; |
| 2961 | 2999 | ||
| 2962 | av_image_copy(tmp.data, tmp.linesize, (const uint8_t **)src->data, | 3000 | for (int i = 0; i < planes; i++) { |
| 2963 | src->linesize, src->format, src->width, src->height); | 3001 | int h = src->height; |
| 3002 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; | ||
| 3003 | |||
| 3004 | if (host_mapped[i]) | ||
| 3005 | continue; | ||
| 3006 | |||
| 3007 | av_image_copy_plane(tmp.data[i], tmp.linesize[i], | ||
| 3008 | (const uint8_t *)src->data[i], src->linesize[i], | ||
| 3009 | FFMIN(tmp.linesize[i], src->linesize[i]), p_height); | ||
| 3010 | } | ||
| 2964 | 3011 | ||
| 2965 | if ((err = unmap_buffers(dev_ctx, bufs, planes, 1))) | 3012 | if ((err = unmap_buffers(dev_ctx, bufs, planes, 1))) |
| 2966 | goto end; | 3013 | goto end; |
| @@ -3076,6 +3123,9 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 109 unmodified lines | |||
| 3076 | AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 }; | 3123 | AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 }; |
| 3077 | const int planes = av_pix_fmt_count_planes(dst->format); | 3124 | const int planes = av_pix_fmt_count_planes(dst->format); |
| 3078 | int log2_chroma = av_pix_fmt_desc_get(dst->format)->log2_chroma_h; | 3125 | int log2_chroma = av_pix_fmt_desc_get(dst->format)->log2_chroma_h; |
| 3126 | VulkanDevicePriv *p = hwfc->device_ctx->internal->priv; | ||
| 3127 | int host_mapped[AV_NUM_DATA_POINTERS] = { 0 }; | ||
| 3128 | int map_host = p->extensions & EXT_EXTERNAL_HOST_MEMORY; | ||
| 3079 | 3129 | ||
| 3080 | if (dst->width > hwfc->width || dst->height > hwfc->height) | 3130 | if (dst->width > hwfc->width || dst->height > hwfc->height) |
| 3081 | return AVERROR(EINVAL); | 3131 | return AVERROR(EINVAL); |
| @@ -3101,11 +3151,27 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 19 unmodified lines | |||
| 3101 | for (int i = 0; i < planes; i++) { | 3151 | for (int i = 0; i < planes; i++) { |
| 3102 | int h = dst->height; | 3152 | int h = dst->height; |
| 3103 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; | 3153 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; |
| 3154 | size_t p_size = FFABS(dst->linesize[i]) * p_height; | ||
| 3155 | |||
| 3156 | VkImportMemoryHostPointerInfoEXT import_desc = { | ||
| 3157 | .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT, | ||
| 3158 | .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, | ||
| 3159 | .pHostPointer = dst->data[i], | ||
| 3160 | }; | ||
| 3161 | |||
| 3162 | /* We can only map images with positive stride and alignment appropriate | ||
| 3163 | * for the device. */ | ||
| 3164 | host_mapped[i] = map_host && dst->linesize[i] > 0 && | ||
| 3165 | !(p_size % p->hprops.minImportedHostPointerAlignment) && | ||
| 3166 | !(((uintptr_t)import_desc.pHostPointer) % | ||
| 3167 | p->hprops.minImportedHostPointerAlignment); | ||
| 3168 | p_size = host_mapped[i] ? p_size : 0; | ||
| 3104 | 3169 | ||
| 3105 | tmp.linesize[i] = FFABS(dst->linesize[i]); | 3170 | tmp.linesize[i] = FFABS(dst->linesize[i]); |
| 3106 | err = create_buf(dev_ctx, &bufs[i], p_height, | 3171 | err = create_buf(dev_ctx, &bufs[i], p_size, p_height, |
| 3107 | &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_DST_BIT, | 3172 | &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_DST_BIT, |
| 3108 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL); | 3173 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, |
| 3174 | host_mapped[i] ? &import_desc : NULL); | ||
| 3109 | if (err) | 3175 | if (err) |
| 3110 | goto end; | 3176 | goto end; |
| 3111 | } | 3177 | } |
| @@ -3119,8 +3185,17 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 7 unmodified lines | |||
| 3119 | if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 1))) | 3185 | if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 1))) |
| 3120 | goto end; | 3186 | goto end; |
| 3121 | 3187 | ||
| 3122 | av_image_copy(dst->data, dst->linesize, (const uint8_t **)tmp.data, | 3188 | for (int i = 0; i < planes; i++) { |
| 3123 | tmp.linesize, dst->format, dst->width, dst->height); | 3189 | int h = dst->height; |
| 3190 | int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h; | ||
| 3191 | |||
| 3192 | if (host_mapped[i]) | ||
| 3193 | continue; | ||
| 3194 | |||
| 3195 | av_image_copy_plane(dst->data[i], dst->linesize[i], | ||
| 3196 | (const uint8_t *)tmp.data[i], tmp.linesize[i], | ||
| 3197 | FFMIN(tmp.linesize[i], dst->linesize[i]), p_height); | ||
| 3198 | } | ||
| 3124 | 3199 | ||
| 3125 | err = unmap_buffers(dev_ctx, bufs, planes, 0); | 3200 | err = unmap_buffers(dev_ctx, bufs, planes, 0); |
| 3126 | 3201 | ||
| @@ -86,8 +86,8 @@ typedef struct AVVulkanDeviceContext { · 85 unmodified lines | |||
| 86 | int nb_enabled_inst_extensions; | 86 | int nb_enabled_inst_extensions; |
| 87 | /** | 87 | /** |
| 88 | * Enabled device extensions. By default, VK_KHR_external_memory_fd, | 88 | * Enabled device extensions. By default, VK_KHR_external_memory_fd, |
| 89 | * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier and | 89 | * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier, |
| 90 | * VK_KHR_external_semaphore_fd are enabled if found. | 90 | * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found. |
| 91 | * If supplying your own device context, these fields takes the same format as | 91 | * If supplying your own device context, these fields takes the same format as |
| 92 | * the above fields, with the same conditions that duplicates are possible | 92 | * the above fields, with the same conditions that duplicates are possible |
| 93 | * and accepted, and that NULL and 0 respectively means no extensions are enabled. | 93 | * and accepted, and that NULL and 0 respectively means no extensions are enabled. |