<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jul 14, 2017 at 5:08 PM, Bas Nieuwenhuizen <span dir="ltr"><<a href="mailto:bas@basnieuwenhuizen.nl" target="_blank">bas@basnieuwenhuizen.nl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br>
<br>
This effectively reverts commit 43a171878bb4b5aedb36a.  Technically,<br>
VK_KHR_get_memory_<wbr>requirements2 and VK_KHR_dedicated_allocation are<br>
required for the KHR version but this at least restores the removed<br>
functionality.  This patch builds but has received zero testing.<br>
<br>
Cc: Dave Airlie <<a href="mailto:airlied@redhat.com">airlied@redhat.com</a>><br>
(Bas: added plumbing for the dedicated allocation)<br></blockquote><div><br></div><div>Those bits look good to me for what it's worth.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
---<br>
 src/amd/vulkan/radv_device.c           |  74 +++++++++++++++++++++-<br>
 src/amd/vulkan/radv_<wbr>entrypoints_gen.py |   3 +<br>
 src/amd/vulkan/radv_formats.c          | 112 ++++++++++++++++++++++++++++++<wbr>+++<br>
 src/amd/vulkan/radv_image.c            |  11 +++-<br>
 src/amd/vulkan/radv_private.h          |   2 +<br>
 5 files changed, 199 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/src/amd/vulkan/radv_device.c b/src/amd/vulkan/radv_device.c<br>
index 59c4ea8448e..630f35ff7a8 100644<br>
--- a/src/amd/vulkan/radv_device.c<br>
+++ b/src/amd/vulkan/radv_device.c<br>
@@ -98,6 +98,10 @@ static const VkExtensionProperties instance_extensions[] = {<br>
                .extensionName = VK_KHR_GET_PHYSICAL_DEVICE_<wbr>PROPERTIES_2_EXTENSION_NAME,<br>
                .specVersion = 1,<br>
        },<br>
+       {<br>
+               .extensionName = VK_KHR_EXTERNAL_MEMORY_<wbr>CAPABILITIES_EXTENSION_NAME,<br>
+               .specVersion = 1,<br>
+       },<br>
 };<br>
<br>
 static const VkExtensionProperties common_device_extensions[] = {<br>
@@ -145,6 +149,14 @@ static const VkExtensionProperties common_device_extensions[] = {<br>
                .extensionName = VK_KHR_DEDICATED_ALLOCATION_<wbr>EXTENSION_NAME,<br>
                .specVersion = 1,<br>
        },<br>
+       {<br>
+               .extensionName = VK_KHR_EXTERNAL_MEMORY_<wbr>EXTENSION_NAME,<br>
+               .specVersion = 1,<br>
+       },<br>
+       {<br>
+               .extensionName = VK_KHR_EXTERNAL_MEMORY_FD_<wbr>EXTENSION_NAME,<br>
+               .specVersion = 1,<br>
+       },<br>
 };<br>
<br>
 static VkResult<br>
@@ -731,6 +743,7 @@ void radv_<wbr>GetPhysicalDeviceProperties2KH<wbr>R(<br>
        VkPhysicalDevice                            physicalDevice,<br>
        VkPhysicalDeviceProperties2KHR             *pProperties)<br>
 {<br>
+       RADV_FROM_HANDLE(radv_<wbr>physical_device, pdevice, physicalDevice);<br>
        radv_<wbr>GetPhysicalDeviceProperties(<wbr>physicalDevice, &pProperties->properties);<br>
<br>
        vk_foreach_struct(ext, pProperties->pNext) {<br>
@@ -741,6 +754,13 @@ void radv_<wbr>GetPhysicalDeviceProperties2KH<wbr>R(<br>
                        properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;<br>
                        break;<br>
                }<br>
+               case VK_STRUCTURE_TYPE_PHYSICAL_<wbr>DEVICE_ID_PROPERTIES_KHR: {<br>
+                       VkPhysicalDeviceIDPropertiesKH<wbr>R *properties = (<wbr>VkPhysicalDeviceIDPropertiesKH<wbr>R*)ext;<br>
+                       radv_device_get_cache_uuid(0, properties->driverUUID);<br>
+                       memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);<br>
+                       properties->deviceLUIDValid = false;<br>
+                       break;<br>
+               }<br>
                default:<br>
                        break;<br>
                }<br>
@@ -2076,6 +2096,9 @@ VkResult radv_AllocateMemory(<br>
                *pMem = VK_NULL_HANDLE;<br>
                return VK_SUCCESS;<br>
        }<br>
+<br>
+       const VkImportMemoryFdInfoKHR *import_info =<br>
+               vk_find_struct_const(<wbr>pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);<br>
        const VkMemoryDedicatedAllocateInfoK<wbr>HR *dedicate_info =<br>
                vk_find_struct_const(<wbr>pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_<wbr>INFO_KHR);<br>
<br>
@@ -2092,6 +2115,18 @@ VkResult radv_AllocateMemory(<br>
                mem->buffer = NULL;<br>
        }<br>
<br>
+       if (import_info) {<br>
+               assert(import_info->handleType ==<br>
+                      VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR);<br>
+               mem->bo = device->ws->buffer_from_fd(<wbr>device->ws, import_info->fd,<br>
+                                                    NULL, NULL);<br>
+               if (!mem->bo) {<br>
+                       result = VK_ERROR_INVALID_EXTERNAL_<wbr>HANDLE_KHR;<br>
+                       goto fail;<br>
+               } else<br>
+                       goto out_success;<br>
+       }<br>
+<br>
        uint64_t alloc_size = align_u64(pAllocateInfo-><wbr>allocationSize, 4096);<br>
        if (pAllocateInfo-><wbr>memoryTypeIndex == RADV_MEM_TYPE_GTT_WRITE_<wbr>COMBINE ||<br>
            pAllocateInfo->memoryTypeIndex == RADV_MEM_TYPE_GTT_CACHED)<br>
@@ -2115,7 +2150,7 @@ VkResult radv_AllocateMemory(<br>
                goto fail;<br>
        }<br>
        mem->type_index = pAllocateInfo-><wbr>memoryTypeIndex;<br>
-<br>
+out_success:<br>
        *pMem = radv_device_memory_to_handle(<wbr>mem);<br>
<br>
        return VK_SUCCESS;<br>
@@ -2258,12 +2293,14 @@ void radv_<wbr>GetImageMemoryRequirements2KHR<wbr>(<br>
        radv_<wbr>GetImageMemoryRequirements(<wbr>device, pInfo->image,<br>
                                         &pMemoryRequirements-><wbr>memoryRequirements);<br>
<br>
+       RADV_FROM_HANDLE(radv_image, image, pInfo->image);<br>
+<br>
        vk_foreach_struct(ext, pMemoryRequirements->pNext) {<br>
                switch (ext->sType) {<br>
                case VK_STRUCTURE_TYPE_MEMORY_<wbr>DEDICATED_REQUIREMENTS_KHR: {<br>
                        VkMemoryDedicatedRequirementsK<wbr>HR *req =<br>
                                       (<wbr>VkMemoryDedicatedRequirementsK<wbr>HR *) ext;<br>
-                       req-><wbr>requiresDedicatedAllocation = false;<br>
+                       req-><wbr>requiresDedicatedAllocation = image->shareable;<br>
                        req-><wbr>prefersDedicatedAllocation = req-><wbr>requiresDedicatedAllocation;<br>
                        break;<br>
                }<br>
@@ -3318,3 +3355,36 @@ vk_<wbr>icdNegotiateLoaderICDInterface<wbr>Version(uint32_t *pSupportedVersion)<br>
        *pSupportedVersion = MIN2(*pSupportedVersion, 3u);<br>
        return VK_SUCCESS;<br>
 }<br>
+<br>
+VkResult radv_GetMemoryFdKHR(VkDevice _device,<br>
+                            const VkMemoryGetFdInfoKHR *pGetFdInfo,<br>
+                            int *pFD)<br>
+{<br>
+       RADV_FROM_HANDLE(radv_device, device, _device);<br>
+       RADV_FROM_HANDLE(radv_device_<wbr>memory, memory, pGetFdInfo->memory);<br>
+<br>
+       assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_<wbr>FD_INFO_KHR);<br>
+<br>
+       /* We support only one handle type. */<br>
+       assert(pGetFdInfo->handleType ==<br>
+              VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR);<br>
+<br>
+       bool ret = radv_get_memory_fd(device, memory, pFD);<br>
+       if (ret == false)<br>
+               return VK_ERROR_OUT_OF_DEVICE_MEMORY;<br>
+       return VK_SUCCESS;<br>
+}<br>
+<br>
+VkResult radv_GetMemoryFdPropertiesKHR(<wbr>VkDevice _device,<br>
+                                      VkExternalMemoryHandleTypeFlag<wbr>BitsKHR handleType,<br>
+                                      int fd,<br>
+                                      VkMemoryFdPropertiesKHR *pMemoryFdProperties)<br>
+{<br>
+   /* The valid usage section for this function says:<br>
+    *<br>
+    *    "handleType must not be one of the handle types defined as opaque."<br>
+    *<br>
+    * Since we only handle opaque handles for now, there are no FD properties.<br>
+    */<br>
+   return VK_ERROR_INVALID_EXTERNAL_<wbr>HANDLE_KHR;<br>
+}<br>
diff --git a/src/amd/vulkan/radv_<wbr>entrypoints_gen.py b/src/amd/vulkan/radv_<wbr>entrypoints_gen.py<br>
index d305a07e7d7..22068a5b02d 100644<br>
--- a/src/amd/vulkan/radv_<wbr>entrypoints_gen.py<br>
+++ b/src/amd/vulkan/radv_<wbr>entrypoints_gen.py<br>
@@ -44,6 +44,9 @@ supported_extensions = [<br>
    'VK_KHR_xlib_surface',<br>
    'VK_KHR_get_memory_<wbr>requirements2',<br>
    'VK_KHR_dedicated_allocation',<br>
+   'VK_KHR_external_memory_<wbr>capabilities',<br>
+   'VK_KHR_external_memory',<br>
+   'VK_KHR_external_memory_fd',<br>
 ]<br>
<br>
 # We generate a static hash table for entry point lookup<br>
diff --git a/src/amd/vulkan/radv_formats.<wbr>c b/src/amd/vulkan/radv_formats.<wbr>c<br>
index b740a1a5d9b..4d0aca1490d 100644<br>
--- a/src/amd/vulkan/radv_formats.<wbr>c<br>
+++ b/src/amd/vulkan/radv_formats.<wbr>c<br>
@@ -1142,12 +1142,37 @@ VkResult radv_<wbr>GetPhysicalDeviceImageFormatPr<wbr>operties(<br>
                                                pImageFormatProperties);<br>
 }<br>
<br>
+static void<br>
+get_external_image_format_<wbr>properties(const VkPhysicalDeviceImageFormatInf<wbr>o2KHR *pImageFormatInfo,<br>
+                                    VkExternalMemoryPropertiesKHR *external_properties)<br>
+{<br>
+       VkExternalMemoryFeatureFlagBit<wbr>sKHR flags = 0;<br>
+       VkExternalMemoryHandleTypeFlag<wbr>sKHR export_flags = 0;<br>
+       VkExternalMemoryHandleTypeFlag<wbr>sKHR compat_flags = 0;<br>
+       switch (pImageFormatInfo->type) {<br>
+       case VK_IMAGE_TYPE_2D:<br>
+               flags = VK_EXTERNAL_MEMORY_FEATURE_<wbr>DEDICATED_ONLY_BIT_KHR|VK_<wbr>EXTERNAL_MEMORY_FEATURE_<wbr>EXPORTABLE_BIT_KHR|VK_<wbr>EXTERNAL_MEMORY_FEATURE_<wbr>IMPORTABLE_BIT_KHR;<br>
+               compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR;<br>
+               break;<br>
+       default:<br>
+               break;<br>
+       }<br>
+<br>
+       *external_properties = (<wbr>VkExternalMemoryPropertiesKHR) {<br>
+               .externalMemoryFeatures = flags,<br>
+               .exportFromImportedHandleTypes = export_flags,<br>
+               .compatibleHandleTypes = compat_flags,<br>
+       };<br>
+}<br>
+<br>
 VkResult radv_<wbr>GetPhysicalDeviceImageFormatPr<wbr>operties2KHR(<br>
        VkPhysicalDevice                            physicalDevice,<br>
        const VkPhysicalDeviceImageFormatInf<wbr>o2KHR  *base_info,<br>
        VkImageFormatProperties2KHR                *base_props)<br>
 {<br>
        RADV_FROM_HANDLE(radv_<wbr>physical_device, physical_device, physicalDevice);<br>
+       const VkPhysicalDeviceExternalImageF<wbr>ormatInfoKHR *external_info = NULL;<br>
+       VkExternalImageFormatPropertie<wbr>sKHR *external_props = NULL;<br>
        VkResult result;<br>
<br>
        result = radv_get_image_format_<wbr>properties(physical_device, base_info,<br>
@@ -1155,7 +1180,69 @@ VkResult radv_<wbr>GetPhysicalDeviceImageFormatPr<wbr>operties2KHR(<br>
        if (result != VK_SUCCESS)<br>
                return result;<br>
<br>
+          /* Extract input structs */<br>
+       vk_foreach_struct_const(s, base_info->pNext) {<br>
+               switch (s->sType) {<br>
+               case VK_STRUCTURE_TYPE_PHYSICAL_<wbr>DEVICE_EXTERNAL_IMAGE_FORMAT_<wbr>INFO_KHR:<br>
+                       external_info = (const void *) s;<br>
+                       break;<br>
+               default:<br>
+                       break;<br>
+               }<br>
+       }<br>
+<br>
+       /* Extract output structs */<br>
+       vk_foreach_struct(s, base_props->pNext) {<br>
+               switch (s->sType) {<br>
+               case VK_STRUCTURE_TYPE_EXTERNAL_<wbr>IMAGE_FORMAT_PROPERTIES_KHR:<br>
+                       external_props = (void *) s;<br>
+                       break;<br>
+               default:<br>
+                       break;<br>
+               }<br>
+       }<br>
+<br>
+       /* From the Vulkan 1.0.42 spec:<br>
+        *<br>
+        *    If handleType is 0, vkGetPhysicalDeviceImageFormat<wbr>Properties2KHR will<br>
+        *    behave as if VkPhysicalDeviceExternalImageF<wbr>ormatInfoKHR was not<br>
+        *    present and VkExternalImageFormatPropertie<wbr>sKHR will be ignored.<br>
+        */<br>
+       if (external_info && external_info->handleType != 0) {<br>
+               switch (external_info->handleType) {<br>
+               case VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR:<br>
+                       get_external_image_format_<wbr>properties(base_info, &external_props-><wbr>externalMemoryProperties);<br>
+                       break;<br>
+               default:<br>
+                       /* From the Vulkan 1.0.42 spec:<br>
+                        *<br>
+                        *    If handleType is not compatible with the [parameters] specified<br>
+                        *    in VkPhysicalDeviceImageFormatInf<wbr>o2KHR, then<br>
+                        *    vkGetPhysicalDeviceImageFormat<wbr>Properties2KHR returns<br>
+                        *    VK_ERROR_FORMAT_NOT_SUPPORTED.<br>
+                        */<br>
+                       result = vk_errorf(VK_ERROR_FORMAT_NOT_<wbr>SUPPORTED,<br>
+                                          "unsupported VkExternalMemoryTypeFlagBitsKH<wbr>R 0x%x",<br>
+                                          external_info->handleType);<br>
+                       goto fail;<br>
+               }<br>
+       }<br>
+<br>
        return VK_SUCCESS;<br>
+<br>
+fail:<br>
+       if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {<br>
+               /* From the Vulkan 1.0.42 spec:<br>
+                *<br>
+                *    If the combination of parameters to<br>
+                *    vkGetPhysicalDeviceImageFormat<wbr>Properties2KHR is not supported by<br>
+                *    the implementation for use in vkCreateImage, then all members of<br>
+                *    imageFormatProperties will be filled with zero.<br>
+                */<br>
+               base_props-><wbr>imageFormatProperties = (VkImageFormatProperties) {0};<br>
+       }<br>
+<br>
+       return result;<br>
 }<br>
<br>
 void radv_<wbr>GetPhysicalDeviceSparseImageFo<wbr>rmatProperties(<br>
@@ -1181,3 +1268,28 @@ void radv_<wbr>GetPhysicalDeviceSparseImageFo<wbr>rmatProperties2KHR(<br>
        /* Sparse images are not yet supported. */<br>
        *pPropertyCount = 0;<br>
 }<br>
+<br>
+void radv_<wbr>GetPhysicalDeviceExternalBuffe<wbr>rPropertiesKHR(<br>
+       VkPhysicalDevice                            physicalDevice,<br>
+       const VkPhysicalDeviceExternalBuffer<wbr>InfoKHR *pExternalBufferInfo,<br>
+       VkExternalBufferPropertiesKHR               *pExternalBufferProperties)<br>
+{<br>
+       VkExternalMemoryFeatureFlagBit<wbr>sKHR flags = 0;<br>
+       VkExternalMemoryHandleTypeFlag<wbr>sKHR export_flags = 0;<br>
+       VkExternalMemoryHandleTypeFlag<wbr>sKHR compat_flags = 0;<br>
+       switch(pExternalBufferInfo-><wbr>handleType) {<br>
+       case VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR:<br>
+               flags = VK_EXTERNAL_MEMORY_FEATURE_<wbr>DEDICATED_ONLY_BIT_KHR |<br>
+                       VK_EXTERNAL_MEMORY_FEATURE_<wbr>EXPORTABLE_BIT_KHR |<br>
+                       VK_EXTERNAL_MEMORY_FEATURE_<wbr>IMPORTABLE_BIT_KHR;<br>
+               compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_<wbr>TYPE_OPAQUE_FD_BIT_KHR;<br>
+               break;<br>
+       default:<br>
+               break;<br>
+       }<br>
+       pExternalBufferProperties-><wbr>externalMemoryProperties = (<wbr>VkExternalMemoryPropertiesKHR) {<br>
+               .externalMemoryFeatures = flags,<br>
+               .exportFromImportedHandleTypes = export_flags,<br>
+               .compatibleHandleTypes = compat_flags,<br>
+       };<br>
+}<br>
diff --git a/src/amd/vulkan/radv_image.c b/src/amd/vulkan/radv_image.c<br>
index fc00d019d1d..17ee74b5f5b 100644<br>
--- a/src/amd/vulkan/radv_image.c<br>
+++ b/src/amd/vulkan/radv_image.c<br>
@@ -27,6 +27,7 @@<br>
<br>
 #include "radv_private.h"<br>
 #include "vk_format.h"<br>
+#include "vk_util.h"<br>
 #include "radv_radeon_winsys.h"<br>
 #include "sid.h"<br>
 #include "gfx9d.h"<br>
@@ -783,9 +784,15 @@ radv_image_create(VkDevice _device,<br>
        image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;<br>
        if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {<br>
                for (uint32_t i = 0; i < pCreateInfo-><wbr>queueFamilyIndexCount; ++i)<br>
-                       image->queue_family_mask |= 1u << pCreateInfo-><wbr>pQueueFamilyIndices[i];<br>
+                       if (pCreateInfo-><wbr>pQueueFamilyIndices[i] == VK_QUEUE_FAMILY_EXTERNAL_KHR)<br>
+                               image->queue_family_mask |= (1u << RADV_MAX_QUEUE_FAMILIES) - 1u;<br>
+                       else<br>
+                               image->queue_family_mask |= 1u << pCreateInfo-><wbr>pQueueFamilyIndices[i];<br>
        }<br>
<br>
+       image->shareable = vk_find_struct_const(<wbr>pCreateInfo->pNext,<br>
+                                               EXTERNAL_MEMORY_IMAGE_CREATE_<wbr>INFO_KHR) != NULL;<br>
+<br>
        radv_init_surface(device, &image->surface, create_info);<br>
<br>
        device->ws->surface_init(<wbr>device->ws, &image->info, &image->surface);<br>
@@ -962,6 +969,8 @@ unsigned radv_image_queue_family_mask(<wbr>const struct radv_image *image, uint32_t f<br>
 {<br>
        if (!image->exclusive)<br>
                return image->queue_family_mask;<br>
+       if (family == VK_QUEUE_FAMILY_EXTERNAL_KHR)<br>
+               return (1u << RADV_MAX_QUEUE_FAMILIES) - 1u;<br>
        if (family == VK_QUEUE_FAMILY_IGNORED)<br>
                return 1u << queue_family;<br>
        return 1u << family;<br>
diff --git a/src/amd/vulkan/radv_private.<wbr>h b/src/amd/vulkan/radv_private.<wbr>h<br>
index a8bc5c96586..2f633b4dbd9 100644<br>
--- a/src/amd/vulkan/radv_private.<wbr>h<br>
+++ b/src/amd/vulkan/radv_private.<wbr>h<br>
@@ -1205,6 +1205,8 @@ struct radv_image {<br>
        bool exclusive;<br>
        unsigned queue_family_mask;<br>
<br>
+       bool shareable;<br>
+<br>
        /* Set when bound */<br>
        struct radeon_winsys_bo *bo;<br>
        VkDeviceSize offset;<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.13.2<br>
<br>
</font></span></blockquote></div><br></div></div>