Some vendors (AMD) require dedicated allocation to be used for all imported images.
Unified · Split
| @@ -1254,7 +1254,7 @@ static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx, · 1253 unmodified lines | ||
| 1254 | 1254 | } |
| 1255 | 1255 | |
| 1256 | 1256 | static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req, |
| 1257 | − VkMemoryPropertyFlagBits req_flags, void *alloc_extension, | |
| 1257 | + VkMemoryPropertyFlagBits req_flags, const void *alloc_extension, | |
| 1258 | 1258 | VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem) |
| 1259 | 1259 | { |
| 1260 | 1260 | VkResult ret; |
| @@ -1987,49 +1987,12 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f · 726 unmodified lines | ||
| 1987 | 1987 | goto fail; |
| 1988 | 1988 | } |
| 1989 | 1989 | |
| 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 | − | |
| 2025 | 1990 | f->tiling = has_modifiers ? VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT : |
| 2026 | 1991 | desc->objects[0].format_modifier == DRM_FORMAT_MOD_LINEAR ? |
| 2027 | 1992 | VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 2028 | 1993 | |
| 2029 | 1994 | for (int i = 0; i < desc->nb_layers; i++) { |
| 2030 | 1995 | const int planes = desc->layers[i].nb_planes; |
| 2031 | − const int signal_p = has_modifiers && (planes > 1); | |
| 2032 | − | |
| 2033 | 1996 | VkImageDrmFormatModifierExplicitCreateInfoEXT drm_info = { |
| 2034 | 1997 | .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT, |
| 2035 | 1998 | .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 | ||
| 2104 | 2067 | |
| 2105 | 2068 | f->layout[i] = image_create_info.initialLayout; |
| 2106 | 2069 | 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 | + } | |
| 2107 | 2099 | |
| 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); | |
| 2108 | 2140 | for (int j = 0; j < planes; j++) { |
| 2109 | 2141 | VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT : |
| 2110 | 2142 | j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT : |