History

hwcontext_vulkan: use host mapped buffers when uploading and downloading

Speeds up both use cases by 30%.

Lynne committed 6 years ago

Commit 4dcb50c1 parent b8d9bc22 files · +116 −41Browse files

Unified · Split

2 files
  1. libavutil/hwcontext_vulkan.c +114 −39
  2. libavutil/hwcontext_vulkan.h +2 −2
libavutil/hwcontext_vulkan.c+114−39
@@ -62,8 +62,9 @@ typedef struct VulkanExecCtx { · 61 unmodified lines
6262
6363 typedef struct VulkanDevicePriv {
6464 /* Properties */
65− VkPhysicalDeviceProperties props;
65+ VkPhysicalDeviceProperties2 props;
6666 VkPhysicalDeviceMemoryProperties mprops;
67+ VkPhysicalDeviceExternalMemoryHostPropertiesEXT hprops;
6768
6869 /* Queues */
6970 uint32_t qfs[3];
@@ -208,6 +209,7 @@ enum VulkanExtensions { · 138 unmodified lines
208209 EXT_DRM_MODIFIER_FLAGS = 1ULL << 1, /* VK_EXT_image_drm_format_modifier */
209210 EXT_EXTERNAL_FD_MEMORY = 1ULL << 2, /* VK_KHR_external_memory_fd */
210211 EXT_EXTERNAL_FD_SEM = 1ULL << 3, /* VK_KHR_external_semaphore_fd */
212+ EXT_EXTERNAL_HOST_MEMORY = 1ULL << 4, /* VK_EXT_external_memory_host */
211213
212214 EXT_NO_FLAG = 1ULL << 63,
213215 };
@@ -226,6 +228,7 @@ static const VulkanOptExtension optional_device_exts[] = { · 12 unmodified lines
226228 { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME, EXT_EXTERNAL_DMABUF_MEMORY, },
227229 { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, EXT_DRM_MODIFIER_FLAGS, },
228230 { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, EXT_EXTERNAL_FD_SEM, },
231+ { VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME, EXT_EXTERNAL_HOST_MEMORY, },
229232 };
230233
231234 /* Converts return values to strings */
@@ -1052,16 +1055,6 @@ static int vulkan_device_init(AVHWDeviceContext *ctx) · 820 unmodified lines
10521055 AVVulkanDeviceContext *hwctx = ctx->hwctx;
10531056 VulkanDevicePriv *p = ctx->internal->priv;
10541057
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−
10651058 /* Set device extension flags */
10661059 for (int i = 0; i < hwctx->nb_enabled_dev_extensions; i++) {
10671060 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
10751068 }
10761069 }
10771070
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);
10791088
10801089 vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &queue_num, NULL);
10811090 if (!queue_num) {
@@ -1231,8 +1240,8 @@ static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx, · 149 unmodified lines
12311240
12321241 constraints->min_width = 0;
12331242 constraints->min_height = 0;
1234− constraints->max_width = p->props.limits.maxImageDimension2D;
1235− constraints->max_height = p->props.limits.maxImageDimension2D;
1243+ constraints->max_width = p->props.properties.limits.maxImageDimension2D;
1244+ constraints->max_height = p->props.properties.limits.maxImageDimension2D;
12361245
12371246 constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum AVPixelFormat));
12381247 if (!constraints->valid_hw_formats)
@@ -1253,16 +1262,11 @@ static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req, · 14 unmodified lines
12531262 VulkanDevicePriv *p = ctx->internal->priv;
12541263 AVVulkanDeviceContext *dev_hwctx = ctx->hwctx;
12551264 VkMemoryAllocateInfo alloc_info = {
1256− .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1257− .pNext = alloc_extension,
1265+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1266+ .pNext = alloc_extension,
1267+ .allocationSize = req->size,
12581268 };
12591269
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−
12661270 /* The vulkan spec requires memory types to be sorted in the "optimal"
12671271 * order, so the first matching type we find will be the best/fastest one */
12681272 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
13541358 int err;
13551359 VkResult ret;
13561360 AVHWDeviceContext *ctx = hwfc->device_ctx;
1361+ VulkanDevicePriv *p = ctx->internal->priv;
13571362 const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
13581363 VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { { 0 } };
13591364
@@ -1379,6 +1384,10 @@ static int alloc_bind_mem(AVHWFramesContext *hwfc, AVVkFrame *f, · 19 unmodified lines
13791384
13801385 vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req);
13811386
1387+ if (f->tiling == VK_IMAGE_TILING_LINEAR)
1388+ req.memoryRequirements.size = FFALIGN(req.memoryRequirements.size,
1389+ p->props.properties.limits.minMemoryMapAlignment);
1390+
13821391 /* In case the implementation prefers/requires dedicated allocation */
13831392 use_ded_mem = ded_req.prefersDedicatedAllocation |
13841393 ded_req.requiresDedicatedAllocation;
@@ -2630,6 +2639,7 @@ typedef struct ImageBuffer { · 1245 unmodified lines
26302639 VkBuffer buf;
26312640 VkDeviceMemory mem;
26322641 VkMemoryPropertyFlagBits flags;
2642+ int mapped_mem;
26332643 } ImageBuffer;
26342644
26352645 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
26462656 av_free(data);
26472657 }
26482658
2649−static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf,
2659+static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, size_t imp_size,
26502660 int height, int *stride, VkBufferUsageFlags usage,
26512661 VkMemoryPropertyFlagBits flags, void *create_pnext,
26522662 void *alloc_pnext)
@@ -2668,8 +2678,15 @@ static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, · 15 unmodified lines
26682678 if (!vkbuf)
26692679 return AVERROR(ENOMEM);
26702680
2671− *stride = FFALIGN(*stride, p->props.limits.optimalBufferCopyRowPitchAlignment);
2672− buf_spawn.size = height*(*stride);
2681+ vkbuf->mapped_mem = !!imp_size;
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+ }
26732690
26742691 ret = vkCreateBuffer(hwctx->act_dev, &buf_spawn, NULL, &vkbuf->buf);
26752692 if (ret != VK_SUCCESS) {
@@ -2701,6 +2718,7 @@ static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, · 25 unmodified lines
27012718 return 0;
27022719 }
27032720
2721+/* Skips mapping of host mapped buffers but still invalidates them */
27042722 static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[],
27052723 int nb_buffers, int invalidate)
27062724 {
@@ -2711,6 +2729,9 @@ static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[ · 4 unmodified lines
27112729
27122730 for (int i = 0; i < nb_buffers; i++) {
27132731 ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2732+ if (vkbuf->mapped_mem)
2733+ continue;
2734+
27142735 ret = vkMapMemory(hwctx->act_dev, vkbuf->mem, 0,
27152736 VK_WHOLE_SIZE, 0, (void **)&mem[i]);
27162737 if (ret != VK_SUCCESS) {
@@ -2780,6 +2801,9 @@ static int unmap_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, · 63 unmodified lines
27802801
27812802 for (int i = 0; i < nb_buffers; i++) {
27822803 ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2804+ if (vkbuf->mapped_mem)
2805+ continue;
2806+
27832807 vkUnmapMemory(hwctx->act_dev, vkbuf->mem);
27842808 }
27852809
@@ -2901,11 +2925,6 @@ static int transfer_image_buf(AVHWFramesContext *hwfc, const AVFrame *f, · 115 unmodified lines
29012925 }
29022926 }
29032927
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. */
29092928 static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst,
29102929 const AVFrame *src)
29112930 {
@@ -2916,6 +2935,9 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 4 unmodified lines
29162935 AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 };
29172936 const int planes = av_pix_fmt_count_planes(src->format);
29182937 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;
29192941
29202942 if ((src->format != AV_PIX_FMT_NONE && !av_vkfmt_from_pixfmt(src->format))) {
29212943 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
29462968 for (int i = 0; i < planes; i++) {
29472969 int h = src->height;
29482970 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;
29492986
29502987 tmp.linesize[i] = FFABS(src->linesize[i]);
2951− err = create_buf(dev_ctx, &bufs[i], p_height,
2952− &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
2953− VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL);
2988+ err = create_buf(dev_ctx, &bufs[i], p_size, p_height, &tmp.linesize[i],
2989+ VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
2990+ VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL,
2991+ host_mapped[i] ? &import_desc : NULL);
29542992 if (err)
29552993 goto end;
29562994 }
@@ -2959,8 +2997,17 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 2 unmodified lines
29592997 if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 0)))
29602998 goto end;
29612999
2962− av_image_copy(tmp.data, tmp.linesize, (const uint8_t **)src->data,
2963− src->linesize, src->format, src->width, src->height);
3000+ for (int i = 0; i < planes; i++) {
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+ }
29643011
29653012 if ((err = unmap_buffers(dev_ctx, bufs, planes, 1)))
29663013 goto end;
@@ -3076,6 +3123,9 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 109 unmodified lines
30763123 AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 };
30773124 const int planes = av_pix_fmt_count_planes(dst->format);
30783125 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;
30793129
30803130 if (dst->width > hwfc->width || dst->height > hwfc->height)
30813131 return AVERROR(EINVAL);
@@ -3101,11 +3151,27 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 19 unmodified lines
31013151 for (int i = 0; i < planes; i++) {
31023152 int h = dst->height;
31033153 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;
31043169
31053170 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,
31073172 &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);
31093175 if (err)
31103176 goto end;
31113177 }
@@ -3119,8 +3185,17 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 7 unmodified lines
31193185 if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 1)))
31203186 goto end;
31213187
3122− av_image_copy(dst->data, dst->linesize, (const uint8_t **)tmp.data,
3123− tmp.linesize, dst->format, dst->width, dst->height);
3188+ for (int i = 0; i < planes; i++) {
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+ }
31243199
31253200 err = unmap_buffers(dev_ctx, bufs, planes, 0);
31263201
libavutil/hwcontext_vulkan.h+2−2
@@ -86,8 +86,8 @@ typedef struct AVVulkanDeviceContext { · 85 unmodified lines
8686 int nb_enabled_inst_extensions;
8787 /**
8888 * Enabled device extensions. By default, VK_KHR_external_memory_fd,
89− * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier and
90− * VK_KHR_external_semaphore_fd are enabled if found.
89+ * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
90+ * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
9191 * If supplying your own device context, these fields takes the same format as
9292 * the above fields, with the same conditions that duplicates are possible
9393 * and accepted, and that NULL and 0 respectively means no extensions are enabled.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t