History

hwcontext_vulkan: check for dedicated allocation when mapping from drm/vaapi

Some vendors (AMD) require dedicated allocation to be used for all imported images.

Lynne committed 6 years ago

Commit bf056ca1 parent b6d4bed1 file · +70 −38Browse files

Unified · Split

libavutil/hwcontext_vulkan.c+70−38
@@ -1254,7 +1254,7 @@ static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx, · 1253 unmodified lines
12541254 }
12551255
12561256 static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req,
1257− VkMemoryPropertyFlagBits req_flags, void *alloc_extension,
1257+ VkMemoryPropertyFlagBits req_flags, const void *alloc_extension,
12581258 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
12591259 {
12601260 VkResult ret;
@@ -1987,49 +1987,12 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f · 726 unmodified lines
19871987 goto fail;
19881988 }
19891989
1990− for (int i = 0; i < desc->nb_objects; i++) {
1991− VkMemoryFdPropertiesKHR fdmp = {
1992− .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
1993− };
1994− VkMemoryRequirements req = {
1995− .size = desc->objects[i].size,
1996− };
1997− VkImportMemoryFdInfoKHR idesc = {
1998− .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
1999− .handleType = htype,
2000− .fd = dup(desc->objects[i].fd),
2001− };
2002−
2003− ret = pfn_vkGetMemoryFdPropertiesKHR(hwctx->act_dev, htype,
2004− idesc.fd, &fdmp);
2005− if (ret != VK_SUCCESS) {
2006− av_log(hwfc, AV_LOG_ERROR, "Failed to get FD properties: %s\n",
2007− vk_ret2str(ret));
2008− err = AVERROR_EXTERNAL;
2009− close(idesc.fd);
2010− goto fail;
2011− }
2012−
2013− req.memoryTypeBits = fdmp.memoryTypeBits;
2014−
2015− err = alloc_mem(ctx, &req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
2016− &idesc, &f->flags, &f->mem[i]);
2017− if (err) {
2018− close(idesc.fd);
2019− return err;
2020− }
2021−
2022− f->size[i] = desc->objects[i].size;
2023− }
2024−
20251990 f->tiling = has_modifiers ? VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT :
20261991 desc->objects[0].format_modifier == DRM_FORMAT_MOD_LINEAR ?
20271992 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
20281993
20291994 for (int i = 0; i < desc->nb_layers; i++) {
20301995 const int planes = desc->layers[i].nb_planes;
2031− const int signal_p = has_modifiers && (planes > 1);
2032−
20331996 VkImageDrmFormatModifierExplicitCreateInfoEXT drm_info = {
20341997 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
20351998 .drmFormatModifier = desc->objects[0].format_modifier,
@@ -2104,7 +2067,76 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f · 68 unmodified lines
21042067
21052068 f->layout[i] = image_create_info.initialLayout;
21062069 f->access[i] = 0x0;
2070+ }
2071+
2072+ for (int i = 0; i < desc->nb_objects; i++) {
2073+ int use_ded_mem = 0;
2074+ VkMemoryFdPropertiesKHR fdmp = {
2075+ .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
2076+ };
2077+ VkMemoryRequirements req = {
2078+ .size = desc->objects[i].size,
2079+ };
2080+ VkImportMemoryFdInfoKHR idesc = {
2081+ .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
2082+ .handleType = htype,
2083+ .fd = dup(desc->objects[i].fd),
2084+ };
2085+ VkMemoryDedicatedAllocateInfo ded_alloc = {
2086+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
2087+ .pNext = &idesc,
2088+ };
2089+
2090+ ret = pfn_vkGetMemoryFdPropertiesKHR(hwctx->act_dev, htype,
2091+ idesc.fd, &fdmp);
2092+ if (ret != VK_SUCCESS) {
2093+ av_log(hwfc, AV_LOG_ERROR, "Failed to get FD properties: %s\n",
2094+ vk_ret2str(ret));
2095+ err = AVERROR_EXTERNAL;
2096+ close(idesc.fd);
2097+ goto fail;
2098+ }
21072099
2100+ req.memoryTypeBits = fdmp.memoryTypeBits;
2101+
2102+ /* Dedicated allocation only makes sense if there's a one to one mapping
2103+ * between images and the memory backing them, so only check in this
2104+ * case. */
2105+ if (desc->nb_layers == desc->nb_objects) {
2106+ VkImageMemoryRequirementsInfo2 req_desc = {
2107+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
2108+ .image = f->img[i],
2109+ };
2110+ VkMemoryDedicatedRequirements ded_req = {
2111+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
2112+ };
2113+ VkMemoryRequirements2 req2 = {
2114+ .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
2115+ .pNext = &ded_req,
2116+ };
2117+
2118+ vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req2);
2119+
2120+ use_ded_mem = ded_req.prefersDedicatedAllocation |
2121+ ded_req.requiresDedicatedAllocation;
2122+ if (use_ded_mem)
2123+ ded_alloc.image = f->img[i];
2124+ }
2125+
2126+ err = alloc_mem(ctx, &req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
2127+ use_ded_mem ? &ded_alloc : ded_alloc.pNext,
2128+ &f->flags, &f->mem[i]);
2129+ if (err) {
2130+ close(idesc.fd);
2131+ return err;
2132+ }
2133+
2134+ f->size[i] = desc->objects[i].size;
2135+ }
2136+
2137+ for (int i = 0; i < desc->nb_layers; i++) {
2138+ const int planes = desc->layers[i].nb_planes;
2139+ const int signal_p = has_modifiers && (planes > 1);
21082140 for (int j = 0; j < planes; j++) {
21092141 VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
21102142 j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :

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