Clean ExternalMemoryUtil
authorPiotr Byszewski <piotr.byszewski@mobica.com>
Thu, 9 Apr 2020 11:41:09 +0000 (13:41 +0200)
committerAlexander Galazin <Alexander.Galazin@arm.com>
Mon, 20 Apr 2020 07:55:44 +0000 (03:55 -0400)
This change separates choosing of memory type from
allocateExportableMemory utility function. This makes
use of this functions more clear.
Previous implementation needed to be done ASAP and
involved passing of non-const reference to
allocateExportableMemory which was then filled.
This workaround made code unnecessarily hard to follow.

Components: Vulkan

VK-GL-CTS issue: 587

Affects:
dEQP-VK.api.external.memory.*
dEQP-VK.synchronization.signal_order.*
dEQP-VK.synchronization.cross_instance.*

Change-Id: I781388db390f13c7c03785dd0ae34ae0128ec31c

external/vulkancts/modules/vulkan/api/vktApiExternalMemoryTests.cpp
external/vulkancts/modules/vulkan/synchronization/vktSynchronizationCrossInstanceSharingTests.cpp
external/vulkancts/modules/vulkan/synchronization/vktSynchronizationSignalOrderTests.cpp
external/vulkancts/modules/vulkan/util/vktExternalMemoryUtil.cpp
external/vulkancts/modules/vulkan/util/vktExternalMemoryUtil.hpp

index 06845fd..2bbded1 100644 (file)
@@ -2839,7 +2839,7 @@ struct MemoryTestConfig
 };
 
 #if (DE_OS == DE_OS_WIN32)
-deUint32 chooseMemoryType (deUint32 bits)
+deUint32 chooseWin32MemoryType(deUint32 bits)
 {
        if (bits == 0)
                TCU_THROW(NotSupportedError, "No compatible memory type found");
@@ -2869,7 +2869,6 @@ tcu::TestStatus testMemoryWin32Create (Context& context, MemoryTestConfig config
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
        // \note Buffer is only allocated to get memory requirements
-       deUint32                                                                        exportedMemoryTypeIndex = ~0U;
        const vk::Unique<vk::VkBuffer>                          buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                          requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
        const vk::VkExportMemoryWin32HandleInfoKHR      win32Info                               =
@@ -2888,15 +2887,15 @@ tcu::TestStatus testMemoryWin32Create (Context& context, MemoryTestConfig config
                (vk::VkExternalMemoryHandleTypeFlags)config.externalType
        };
 
-       exportedMemoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits & compatibleMemTypes);
-       const vk::VkMemoryAllocateInfo                          info                            =
+       const deUint32                                                          exportedMemoryTypeIndex = chooseWin32MemoryType(requirements.memoryTypeBits & compatibleMemTypes);
+       const vk::VkMemoryAllocateInfo                          info                                    =
        {
                vk::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
                &exportInfo,
                requirements.size,
                exportedMemoryTypeIndex
        };
-       const vk::Unique<vk::VkDeviceMemory>            memory                          (vk::allocateMemory(vkd, *device, &info));
+       const vk::Unique<vk::VkDeviceMemory>            memory                                  (vk::allocateMemory(vkd, *device, &info));
        NativeHandle                                                            handleA;
 
        if (config.hostVisible)
@@ -2931,6 +2930,17 @@ tcu::TestStatus testMemoryWin32Create (Context& context, MemoryTestConfig config
 #endif
 }
 
+deUint32 getExportedMemoryTypeIndex(const vk::InstanceDriver& vki, const vk::VkPhysicalDevice physicalDevice, bool hostVisible, deUint32 memoryBits)
+{
+       if (hostVisible)
+       {
+               const vk::VkPhysicalDeviceMemoryProperties properties(vk::getPhysicalDeviceMemoryProperties(vki, physicalDevice));
+               return chooseHostVisibleMemoryType(memoryBits, properties);
+       }
+
+       return chooseMemoryType(memoryBits);
+}
+
 tcu::TestStatus testMemoryImportTwice (Context& context, MemoryTestConfig config)
 {
        const vk::PlatformInterface&                    vkp                                     (context.getPlatformInterface());
@@ -2947,12 +2957,12 @@ tcu::TestStatus testMemoryImportTwice (Context& context, MemoryTestConfig config
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
-       const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
-       const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
-       NativeHandle                                                    handleA;
+       const vk::Unique<vk::VkBuffer>                          buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
+       const vk::VkMemoryRequirements                          requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
+       const deUint32                                                          exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>            memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
+       NativeHandle                                                            handleA;
 
        if (config.hostVisible)
                writeHostMemory(vkd, *device, *memory, testData.size(), &testData[0]);
@@ -3004,11 +3014,11 @@ tcu::TestStatus testMemoryMultipleImports (Context& context, MemoryTestConfig co
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
        NativeHandle                                                    handleA;
 
        getMemoryNative(vkd, *device, *memory, config.externalType, handleA);
@@ -3040,11 +3050,11 @@ tcu::TestStatus testMemoryMultipleExports (Context& context, MemoryTestConfig co
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
        for (size_t ndx = 0; ndx < count; ndx++)
        {
@@ -3069,11 +3079,11 @@ tcu::TestStatus testMemoryFdProperties (Context& context, MemoryTestConfig confi
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
        vk::VkMemoryFdPropertiesKHR     properties;
        NativeHandle                            handle;
@@ -3117,11 +3127,11 @@ tcu::TestStatus testMemoryFdDup (Context& context, MemoryTestConfig config)
 
                checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                // \note Buffer is only allocated to get memory requirements
                const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
                const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+               const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
                if (config.hostVisible)
                        writeHostMemory(vkd, *device, *memory, testData.size(), &testData[0]);
@@ -3181,11 +3191,11 @@ tcu::TestStatus testMemoryFdDup2 (Context& context, MemoryTestConfig config)
 
                checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                // \note Buffer is only allocated to get memory requirements
                const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
                const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+               const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
                if (config.hostVisible)
                        writeHostMemory(vkd, *device, *memory, testData.size(), &testData[0]);
@@ -3246,11 +3256,11 @@ tcu::TestStatus testMemoryFdDup3 (Context& context, MemoryTestConfig config)
 
                checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                // \note Buffer is only allocated to get memory requirements
                const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
                const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+               const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
                if (config.hostVisible)
                        writeHostMemory(vkd, *device, *memory, testData.size(), &testData[0]);
@@ -3311,11 +3321,11 @@ tcu::TestStatus testMemoryFdSendOverSocket (Context& context, MemoryTestConfig c
 
                checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                // \note Buffer is only allocated to get memory requirements
                const vk::Unique<vk::VkBuffer>                  buffer                                  (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
                const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *buffer));
-               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vki, physicalDevice, vkd, *device, requirements, config.externalType, config.hostVisible, config.dedicated ? *buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+               const deUint32                                                  exportedMemoryTypeIndex (getExportedMemoryTypeIndex(vki, physicalDevice, config.hostVisible, requirements.memoryTypeBits));
+               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *buffer : (vk::VkBuffer)0));
 
                if (config.hostVisible)
                        writeHostMemory(vkd, *device, *memory, testData.size(), &testData[0]);
@@ -3463,11 +3473,11 @@ tcu::TestStatus testBufferBindExportImportBind (Context&                                context,
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  bufferA                                 (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *bufferA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0));
        NativeHandle                                                    handle;
 
        VK_CHECK(vkd.bindBufferMemory(*device, *bufferA, *memoryA, 0u));
@@ -3501,11 +3511,11 @@ tcu::TestStatus testBufferExportBindImportBind (Context&                                context,
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  bufferA                                 (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *bufferA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0));
        NativeHandle                                                    handle;
 
        getMemoryNative(vkd, *device, *memoryA, config.externalType, handle);
@@ -3538,11 +3548,11 @@ tcu::TestStatus testBufferExportImportBindBind (Context&                                context,
 
        checkBufferSupport(vki, physicalDevice, config.externalType, 0u, usage, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Buffer is only allocated to get memory requirements
        const vk::Unique<vk::VkBuffer>                  bufferA                                 (createExternalBuffer(vkd, *device, queueFamilyIndex, config.externalType, bufferSize, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getBufferMemoryRequirements(vkd, *device, *bufferA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *bufferA : (vk::VkBuffer)0));
        NativeHandle                                                    handle;
 
        getMemoryNative(vkd, *device, *memoryA, config.externalType, handle);
@@ -3742,10 +3752,10 @@ tcu::TestStatus testImageBindExportImportBind (Context&                                 context,
 
        checkImageSupport(vki, physicalDevice, config.externalType, 0u, usage, format, tiling, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        const vk::Unique<vk::VkImage>                   imageA                                  (createExternalImage(vkd, *device, queueFamilyIndex, config.externalType, format, width, height, tiling, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *imageA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0));
        NativeHandle                                                    handle;
 
        VK_CHECK(vkd.bindImageMemory(*device, *imageA, *memoryA, 0u));
@@ -3782,10 +3792,10 @@ tcu::TestStatus testImageExportBindImportBind (Context&                                 context,
 
        checkImageSupport(vki, physicalDevice, config.externalType, 0u, usage, format, tiling, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        const vk::Unique<vk::VkImage>                   imageA                                  (createExternalImage(vkd, *device, queueFamilyIndex, config.externalType, format, width, height, tiling, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *imageA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0));
        NativeHandle                                                    handle;
 
        getMemoryNative(vkd, *device, *memoryA, config.externalType, handle);
@@ -3821,11 +3831,11 @@ tcu::TestStatus testImageExportImportBindBind (Context&                                 context,
 
        checkImageSupport(vki, physicalDevice, config.externalType, 0u, usage, format, tiling, config.dedicated);
 
-       deUint32                                                                exportedMemoryTypeIndex = ~0U;
        // \note Image is only allocated to get memory requirements
        const vk::Unique<vk::VkImage>                   imageA                                  (createExternalImage(vkd, *device, queueFamilyIndex, config.externalType, format, width, height, tiling, 0u, usage));
        const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *imageA));
-       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0, exportedMemoryTypeIndex));
+       const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+       const vk::Unique<vk::VkDeviceMemory>    memoryA                                 (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, config.externalType, config.dedicated ? *imageA : (vk::VkImage)0));
        NativeHandle                                                    handle;
 
        getMemoryNative(vkd, *device, *memoryA, config.externalType, handle);
@@ -4133,10 +4143,10 @@ tcu::TestStatus testAndroidHardwareBufferImageFormat  (Context& context, vk::VkF
                        };
                        for (size_t i = 0; i < DE_LENGTH_OF_ARRAY(sizes); i++)
                        {
-                               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                                const vk::Unique<vk::VkImage>                   image                                   (createExternalImage(vkd, *device, queueFamilyIndex, externalMemoryType, format, sizes[i].width, sizes[i].height, tiling, createFlag, usage));
                                const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *image));
-                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements, externalMemoryType, *image, exportedMemoryTypeIndex));
+                               const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, externalMemoryType, *image));
                                NativeHandle                                                    handle;
 
                                VK_CHECK(vkd.bindImageMemory(*device, *image, *memory, 0u));
@@ -4154,10 +4164,10 @@ tcu::TestStatus testAndroidHardwareBufferImageFormat  (Context& context, vk::VkF
 
                        if (properties.imageFormatProperties.maxMipLevels > 1u)
                        {
-                               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                                const vk::Unique<vk::VkImage>                   image                                   (createExternalImage(vkd, *device, queueFamilyIndex, externalMemoryType, format, 64u, 64u, tiling, createFlag, usage, properties.imageFormatProperties.maxMipLevels));
                                const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *image));
-                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements, externalMemoryType, *image, exportedMemoryTypeIndex));
+                               const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, externalMemoryType, *image));
                                NativeHandle                                                    handle;
 
                                VK_CHECK(vkd.bindImageMemory(*device, *image, *memory, 0u));
@@ -4172,10 +4182,10 @@ tcu::TestStatus testAndroidHardwareBufferImageFormat  (Context& context, vk::VkF
 
                        if ((properties.imageFormatProperties.maxArrayLayers > 1u) && enableMaxLayerTest)
                        {
-                               deUint32                                                                exportedMemoryTypeIndex = ~0U;
                                const vk::Unique<vk::VkImage>                   image                                   (createExternalImage(vkd, *device, queueFamilyIndex, externalMemoryType, format, 64u, 64u, tiling, createFlag, usage, 1u, properties.imageFormatProperties.maxArrayLayers));
                                const vk::VkMemoryRequirements                  requirements                    (getImageMemoryRequirements(vkd, *device, *image));
-                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements, externalMemoryType, *image, exportedMemoryTypeIndex));
+                               const deUint32                                                  exportedMemoryTypeIndex (chooseMemoryType(requirements.memoryTypeBits));
+                               const vk::Unique<vk::VkDeviceMemory>    memory                                  (allocateExportableMemory(vkd, *device, requirements.size, exportedMemoryTypeIndex, externalMemoryType, *image));
                                NativeHandle                                                    handle;
 
                                VK_CHECK(vkd.bindImageMemory(*device, *image, *memory, 0u));
index 8ccd654..8cf418a 100644 (file)
@@ -538,23 +538,21 @@ vk::Move<vk::VkCommandBuffer> createCommandBuffer (const vk::DeviceInterface&     vk
        return vk::allocateCommandBuffer(vkd, device, &allocateInfo);
 }
 
-de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&                                  vkd,
-                                                                                                  vk::VkDevice                                                                 device,
-                                                                                                  vk::VkBuffer                                                                 buffer,
-                                                                                                  vk::VkExternalMemoryHandleTypeFlagBits       externalType,
-                                                                                                  deUint32&                                                                    exportedMemoryTypeIndex,
-                                                                                                  bool                                                                                 dedicated,
-                                                                                                  bool                                                                                 getMemReq2Supported)
+vk::VkMemoryRequirements getMemoryRequirements(const vk::DeviceInterface&                              vkd,
+                                                                                          vk::VkDevice                                                         device,
+                                                                                          vk::VkImage                                                          image,
+                                                                                          bool                                                                         dedicated,
+                                                                                          bool                                                                         getMemReq2Supported)
 {
        vk::VkMemoryRequirements memoryRequirements = { 0u, 0u, 0u, };
 
        if (getMemReq2Supported)
        {
-               const vk::VkBufferMemoryRequirementsInfo2       requirementInfo =
+               const vk::VkImageMemoryRequirementsInfo2        requirementInfo =
                {
-                       vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
+                       vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
                        DE_NULL,
-                       buffer
+                       image
                };
                vk::VkMemoryDedicatedRequirements                       dedicatedRequirements =
                {
@@ -569,7 +567,7 @@ de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&
                        &dedicatedRequirements,
                        { 0u, 0u, 0u, }
                };
-               vkd.getBufferMemoryRequirements2(device, &requirementInfo, &requirements);
+               vkd.getImageMemoryRequirements2(device, &requirementInfo, &requirements);
 
                if (!dedicated && dedicatedRequirements.requiresDedicatedAllocation)
                        TCU_THROW(NotSupportedError, "Memory requires dedicated allocation");
@@ -578,33 +576,27 @@ de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&
        }
        else
        {
-               vkd.getBufferMemoryRequirements(device, buffer, &memoryRequirements);
+               vkd.getImageMemoryRequirements(device, image, &memoryRequirements);
        }
 
-
-       vk::Move<vk::VkDeviceMemory> memory = allocateExportableMemory(vkd, device, memoryRequirements, externalType, dedicated ? buffer : (vk::VkBuffer)0, exportedMemoryTypeIndex);
-       VK_CHECK(vkd.bindBufferMemory(device, buffer, *memory, 0u));
-
-       return de::MovePtr<vk::Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
+       return memoryRequirements;
 }
 
-de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&                                  vkd,
-                                                                                                  vk::VkDevice                                                                 device,
-                                                                                                  vk::VkImage                                                                  image,
-                                                                                                  vk::VkExternalMemoryHandleTypeFlagBits       externalType,
-                                                                                                  deUint32&                                                                    exportedMemoryTypeIndex,
-                                                                                                  bool                                                                                 dedicated,
-                                                                                                  bool                                                                                 getMemReq2Supported)
+vk::VkMemoryRequirements getMemoryRequirements(const vk::DeviceInterface&                              vkd,
+                                                                                          vk::VkDevice                                                         device,
+                                                                                          vk::VkBuffer                                                         buffer,
+                                                                                          bool                                                                         dedicated,
+                                                                                          bool                                                                         getMemReq2Supported)
 {
        vk::VkMemoryRequirements memoryRequirements = { 0u, 0u, 0u, };
 
        if (getMemReq2Supported)
        {
-               const vk::VkImageMemoryRequirementsInfo2        requirementInfo =
+               const vk::VkBufferMemoryRequirementsInfo2       requirementInfo =
                {
-                       vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
+                       vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
                        DE_NULL,
-                       image
+                       buffer
                };
                vk::VkMemoryDedicatedRequirements                       dedicatedRequirements =
                {
@@ -619,7 +611,7 @@ de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&
                        &dedicatedRequirements,
                        { 0u, 0u, 0u, }
                };
-               vkd.getImageMemoryRequirements2(device, &requirementInfo, &requirements);
+               vkd.getBufferMemoryRequirements2(device, &requirementInfo, &requirements);
 
                if (!dedicated && dedicatedRequirements.requiresDedicatedAllocation)
                        TCU_THROW(NotSupportedError, "Memory requires dedicated allocation");
@@ -628,109 +620,77 @@ de::MovePtr<vk::Allocation> allocateAndBindMemory (const vk::DeviceInterface&
        }
        else
        {
-               vkd.getImageMemoryRequirements(device, image, &memoryRequirements);
+               vkd.getBufferMemoryRequirements(device, buffer, &memoryRequirements);
        }
 
-       vk::Move<vk::VkDeviceMemory> memory = allocateExportableMemory(vkd, device, memoryRequirements, externalType, dedicated ? image : (vk::VkImage)0, exportedMemoryTypeIndex);
-       VK_CHECK(vkd.bindImageMemory(device, image, *memory, 0u));
-
-       return de::MovePtr<vk::Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
+       return memoryRequirements;
 }
 
-de::MovePtr<Resource> createResource (const vk::DeviceInterface&                               vkd,
-                                                                         vk::VkDevice                                                          device,
-                                                                         const ResourceDescription&                            resourceDesc,
-                                                                         const std::vector<deUint32>&                          queueFamilyIndices,
-                                                                         const OperationSupport&                                       readOp,
-                                                                         const OperationSupport&                                       writeOp,
-                                                                         vk::VkExternalMemoryHandleTypeFlagBits        externalType,
-                                                                         deUint32&                                                                     exportedMemoryTypeIndex,
-                                                                         bool                                                                          dedicated,
-                                                                         bool                                                                          getMemReq2Supported)
+Move<VkImage> createImage(const vk::DeviceInterface&                           vkd,
+                                                 vk::VkDevice                                                          device,
+                                                 const ResourceDescription&                            resourceDesc,
+                                                 const vk::VkExtent3D                                          extent,
+                                                 const std::vector<deUint32>&                          queueFamilyIndices,
+                                                 const OperationSupport&                                       readOp,
+                                                 const OperationSupport&                                       writeOp,
+                                                 vk::VkExternalMemoryHandleTypeFlagBits        externalType)
 {
-       if (resourceDesc.type == RESOURCE_TYPE_IMAGE)
+       const vk::VkExternalMemoryImageCreateInfo externalInfo =
        {
-               const vk::VkExtent3D                            extent                                  =
-               {
-                       (deUint32)resourceDesc.size.x(),
-                       de::max(1u, (deUint32)resourceDesc.size.y()),
-                       de::max(1u, (deUint32)resourceDesc.size.z())
-               };
-               const vk::VkImageSubresourceRange       subresourceRange                =
-               {
-                       resourceDesc.imageAspect,
-                       0u,
-                       1u,
-                       0u,
-                       1u
-               };
-               const vk::VkImageSubresourceLayers      subresourceLayers               =
-               {
-                       resourceDesc.imageAspect,
-                       0u,
-                       0u,
-                       1u
-               };
-               const vk::VkExternalMemoryImageCreateInfo externalInfo =
-               {
-                       vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
-                       DE_NULL,
-                       (vk::VkExternalMemoryHandleTypeFlags)externalType
-               };
-               const vk::VkImageCreateInfo                     createInfo                              =
-               {
-                       vk::VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
-                       &externalInfo,
-                       0u,
-
-                       resourceDesc.imageType,
-                       resourceDesc.imageFormat,
-                       extent,
-                       1u,
-                       1u,
-                       vk::VK_SAMPLE_COUNT_1_BIT,
-                       vk::VK_IMAGE_TILING_OPTIMAL,
-                       readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags(),
-                       vk::VK_SHARING_MODE_EXCLUSIVE,
+               vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
+               DE_NULL,
+               (vk::VkExternalMemoryHandleTypeFlags)externalType
+       };
+       const vk::VkImageCreateInfo                     createInfo =
+       {
+               vk::VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
+               &externalInfo,
+               0u,
 
-                       (deUint32)queueFamilyIndices.size(),
-                       &queueFamilyIndices[0],
-                       vk::VK_IMAGE_LAYOUT_UNDEFINED
-               };
+               resourceDesc.imageType,
+               resourceDesc.imageFormat,
+               extent,
+               1u,
+               1u,
+               vk::VK_SAMPLE_COUNT_1_BIT,
+               vk::VK_IMAGE_TILING_OPTIMAL,
+               readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags(),
+               vk::VK_SHARING_MODE_EXCLUSIVE,
+
+               (deUint32)queueFamilyIndices.size(),
+               &queueFamilyIndices[0],
+               vk::VK_IMAGE_LAYOUT_UNDEFINED
+       };
 
-               vk::Move<vk::VkImage>                   image           = vk::createImage(vkd, device, &createInfo);
-               de::MovePtr<vk::Allocation>             allocation      = allocateAndBindMemory(vkd, device, *image, externalType, exportedMemoryTypeIndex, dedicated, getMemReq2Supported);
+       return vk::createImage(vkd, device, &createInfo);
+}
 
-               return de::MovePtr<Resource>(new Resource(image, allocation, extent, resourceDesc.imageType, resourceDesc.imageFormat, subresourceRange, subresourceLayers));
-       }
-       else
+Move<VkBuffer> createBuffer(const vk::DeviceInterface&                                         vkd,
+                                                       vk::VkDevice                                                                    device,
+                                                       const vk::VkDeviceSize                                                  size,
+                                                       const vk::VkBufferUsageFlags                                    usage,
+                                                       const vk::VkExternalMemoryHandleTypeFlagBits    memoryHandleType,
+                                                       const std::vector<deUint32>&                                    queueFamilyIndices)
+{
+       const vk::VkExternalMemoryBufferCreateInfo      externalInfo =
        {
-               const vk::VkDeviceSize                                                  offset                  = 0u;
-               const vk::VkDeviceSize                                                  size                    = static_cast<vk::VkDeviceSize>(resourceDesc.size.x());
-               const vk::VkBufferUsageFlags                                    usage                   = readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags();
-               const vk:: VkExternalMemoryBufferCreateInfo     externalInfo    =
-               {
-                       vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
-                       DE_NULL,
-                       (vk::VkExternalMemoryHandleTypeFlags)externalType
-               };
-               const vk::VkBufferCreateInfo                                    createInfo              =
-               {
-                       vk::VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
-                       &externalInfo,
-                       0u,
-
-                       size,
-                       usage,
-                       vk::VK_SHARING_MODE_EXCLUSIVE,
-                       (deUint32)queueFamilyIndices.size(),
-                       &queueFamilyIndices[0]
-               };
-               vk::Move<vk::VkBuffer>          buffer          = vk::createBuffer(vkd, device, &createInfo);
-               de::MovePtr<vk::Allocation>     allocation      = allocateAndBindMemory(vkd, device, *buffer, externalType, exportedMemoryTypeIndex, dedicated, getMemReq2Supported);
+               vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
+               DE_NULL,
+               (vk::VkExternalMemoryHandleTypeFlags)memoryHandleType
+       };
+       const vk::VkBufferCreateInfo                            createInfo =
+       {
+               vk::VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+               &externalInfo,
+               0u,
 
-               return de::MovePtr<Resource>(new Resource(resourceDesc.type, buffer, allocation, offset, size));
-       }
+               size,
+               usage,
+               vk::VK_SHARING_MODE_EXCLUSIVE,
+               (deUint32)queueFamilyIndices.size(),
+               &queueFamilyIndices[0]
+       };
+       return vk::createBuffer(vkd, device, &createInfo);
 }
 
 de::MovePtr<vk::Allocation> importAndBindMemory (const vk::DeviceInterface&                                    vkd,
@@ -1085,13 +1045,65 @@ tcu::TestStatus SharingTestInstance::iterate (void)
                const vk::Unique<vk::VkSemaphore>               semaphoreA                      (createExportableSemaphoreType(m_vkdA, *m_deviceA, m_config.semaphoreType, m_semaphoreHandleType));
                const vk::Unique<vk::VkSemaphore>               semaphoreB                      (createSemaphoreType(m_vkdB, *m_deviceB, m_config.semaphoreType));
 
-               deUint32                                                                exportedMemoryTypeIndex = ~0U;
-               const de::UniquePtr<Resource>                   resourceA                       (createResource(m_vkdA, *m_deviceA, m_config.resource, m_queueFamilyIndicesA, *m_supportReadOp, *m_supportWriteOp, m_memoryHandleType, exportedMemoryTypeIndex, m_config.dedicated, m_getMemReq2Supported));
+               const ResourceDescription&                              resourceDesc            = m_config.resource;
+               de::MovePtr<Resource>                                   resourceA;
+
+               deUint32 exportedMemoryTypeIndex = ~0U;
+               if (resourceDesc.type == RESOURCE_TYPE_IMAGE)
+               {
+                       const vk::VkExtent3D                            extent =
+                       {
+                               (deUint32)resourceDesc.size.x(),
+                               de::max(1u, (deUint32)resourceDesc.size.y()),
+                               de::max(1u, (deUint32)resourceDesc.size.z())
+                       };
+                       const vk::VkImageSubresourceRange       subresourceRange =
+                       {
+                               resourceDesc.imageAspect,
+                               0u,
+                               1u,
+                               0u,
+                               1u
+                       };
+                       const vk::VkImageSubresourceLayers      subresourceLayers =
+                       {
+                               resourceDesc.imageAspect,
+                               0u,
+                               0u,
+                               1u
+                       };
+
+                       vk::Move<vk::VkImage>                   image                                   = createImage(m_vkdA, *m_deviceA, resourceDesc, extent, m_queueFamilyIndicesA,
+                                                                                                                                                                 *m_supportReadOp, *m_supportWriteOp, m_memoryHandleType);
+                       const vk::VkMemoryRequirements  requirements                    = getMemoryRequirements(m_vkdA, *m_deviceA, *image, m_config.dedicated, m_getMemReq2Supported);
+                                                                                       exportedMemoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
+                       vk::Move<vk::VkDeviceMemory>    memory                                  = allocateExportableMemory(m_vkdA, *m_deviceA, requirements.size, exportedMemoryTypeIndex, m_memoryHandleType, m_config.dedicated ? *image : (vk::VkImage)0);
+
+                       VK_CHECK(m_vkdA.bindImageMemory(*m_deviceA, *image, *memory, 0u));
+
+                       de::MovePtr<vk::Allocation> allocation = de::MovePtr<vk::Allocation>(new SimpleAllocation(m_vkdA, *m_deviceA, memory.disown()));
+                       resourceA = de::MovePtr<Resource>(new Resource(image, allocation, extent, resourceDesc.imageType, resourceDesc.imageFormat, subresourceRange, subresourceLayers));
+               }
+               else
+               {
+                       const vk::VkDeviceSize                          offset                                  = 0u;
+                       const vk::VkDeviceSize                          size                                    = static_cast<vk::VkDeviceSize>(resourceDesc.size.x());
+                       const vk::VkBufferUsageFlags            usage                                   = m_supportReadOp->getInResourceUsageFlags() | m_supportWriteOp->getOutResourceUsageFlags();
+                       vk::Move<vk::VkBuffer>                          buffer                                  = createBuffer(m_vkdA, *m_deviceA, size, usage, m_memoryHandleType, m_queueFamilyIndicesA);
+                       const vk::VkMemoryRequirements          requirements                    = getMemoryRequirements(m_vkdA, *m_deviceA, *buffer, m_config.dedicated, m_getMemReq2Supported);
+                                                                                               exportedMemoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
+                       vk::Move<vk::VkDeviceMemory>            memory                                  = allocateExportableMemory(m_vkdA, *m_deviceA, requirements.size, exportedMemoryTypeIndex, m_memoryHandleType, m_config.dedicated ? *buffer : (vk::VkBuffer)0);
+
+                       VK_CHECK(m_vkdA.bindBufferMemory(*m_deviceA, *buffer, *memory, 0u));
+
+                       de::MovePtr<vk::Allocation> allocation = de::MovePtr<vk::Allocation>(new SimpleAllocation(m_vkdA, *m_deviceA, memory.disown()));
+                       resourceA = de::MovePtr<Resource>(new Resource(resourceDesc.type, buffer, allocation, offset, size));
+               }
 
                NativeHandle                                                    nativeMemoryHandle;
                getMemoryNative(m_vkdA, *m_deviceA, resourceA->getMemory(), m_memoryHandleType, nativeMemoryHandle);
 
-               const de::UniquePtr<Resource>                   resourceB                       (importResource(m_vkdB, *m_deviceB, m_config.resource, m_queueFamilyIndicesB, *m_supportReadOp, *m_supportWriteOp, nativeMemoryHandle, m_memoryHandleType, exportedMemoryTypeIndex, m_config.dedicated));
+               const de::UniquePtr<Resource>                   resourceB                       (importResource(m_vkdB, *m_deviceB, resourceDesc, m_queueFamilyIndicesB, *m_supportReadOp, *m_supportWriteOp, nativeMemoryHandle, m_memoryHandleType, exportedMemoryTypeIndex, m_config.dedicated));
 
 
                const vk::VkQueue                                               queueA                          (getQueue(m_vkdA, *m_deviceA, queueFamilyA));
index 0d95a19..8b1135b 100644 (file)
@@ -232,11 +232,9 @@ SimpleAllocation::~SimpleAllocation (void)
        m_vkd.freeMemory(m_device, getMemory(), DE_NULL);
 }
 
-MovePtr<Allocation> allocateAndBindMemory (const DeviceInterface&                              vkd,
-                                                                                  VkDevice                                                             device,
-                                                                                  VkBuffer                                                             buffer,
-                                                                                  VkExternalMemoryHandleTypeFlagBits   externalType,
-                                                                                  deUint32&                                                    memoryIndex)
+vk::VkMemoryRequirements getMemoryRequirements (const DeviceInterface&                         vkd,
+                                                                                                VkDevice                                                       device,
+                                                                                                VkBuffer                                                       buffer)
 {
        const VkBufferMemoryRequirementsInfo2   requirementInfo =
        {
@@ -251,20 +249,13 @@ MovePtr<Allocation> allocateAndBindMemory (const DeviceInterface&                         vkd,
                { 0u, 0u, 0u, }
        };
        vkd.getBufferMemoryRequirements2(device, &requirementInfo, &requirements);
-
-       Move<VkDeviceMemory>                                    memory                  = allocateExportableMemory(vkd, device, requirements.memoryRequirements, externalType, buffer, memoryIndex);
-       VK_CHECK(vkd.bindBufferMemory(device, buffer, *memory, 0u));
-
-       return MovePtr<Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
+       return requirements.memoryRequirements;
 }
 
-MovePtr<Allocation> allocateAndBindMemory (const DeviceInterface&                              vkd,
-                                                                                  VkDevice                                                             device,
-                                                                                  VkImage                                                              image,
-                                                                                  VkExternalMemoryHandleTypeFlagBits   externalType,
-                                                                                  deUint32&                                                    exportedMemoryTypeIndex)
+vk::VkMemoryRequirements getMemoryRequirements(const DeviceInterface&                          vkd,
+                                                                                               VkDevice                                                        device,
+                                                                                               VkImage                                                         image)
 {
-       VkMemoryRequirements memoryRequirements = { 0u, 0u, 0u, };
        const VkImageMemoryRequirementsInfo2    requirementInfo =
        {
                VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
@@ -279,15 +270,9 @@ MovePtr<Allocation> allocateAndBindMemory (const DeviceInterface&                          vkd,
        };
        vkd.getImageMemoryRequirements2(device, &requirementInfo, &requirements);
 
-       memoryRequirements = requirements.memoryRequirements;
-
-       Move<VkDeviceMemory> memory = allocateExportableMemory(vkd, device, memoryRequirements, externalType, image, exportedMemoryTypeIndex);
-       VK_CHECK(vkd.bindImageMemory(device, image, *memory, 0u));
-
-       return MovePtr<Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
+       return requirements.memoryRequirements;
 }
 
-
 MovePtr<Allocation> importAndBindMemory (const DeviceInterface&                                        vkd,
                                                                                 VkDevice                                                               device,
                                                                                 VkBuffer                                                               buffer,
@@ -350,100 +335,6 @@ struct QueueTimelineIteration
        SharedPtr<Operation>            op;
 };
 
-de::MovePtr<Resource> createResource (const DeviceInterface&                           vkd,
-                                                                         VkDevice                                                              device,
-                                                                         const ResourceDescription&                    resourceDesc,
-                                                                         const deUint32                                                queueFamilyIndex,
-                                                                         const OperationSupport&                               readOp,
-                                                                         const OperationSupport&                               writeOp,
-                                                                         VkExternalMemoryHandleTypeFlagBits    externalType,
-                                                                         deUint32&                                                             exportedMemoryTypeIndex)
-{
-       if (resourceDesc.type == RESOURCE_TYPE_IMAGE)
-       {
-               const VkExtent3D                                extent                                  =
-               {
-                       (deUint32)resourceDesc.size.x(),
-                       de::max(1u, (deUint32)resourceDesc.size.y()),
-                       de::max(1u, (deUint32)resourceDesc.size.z())
-               };
-               const VkImageSubresourceRange   subresourceRange                =
-               {
-                       resourceDesc.imageAspect,
-                       0u,
-                       1u,
-                       0u,
-                       1u
-               };
-               const VkImageSubresourceLayers  subresourceLayers               =
-               {
-                       resourceDesc.imageAspect,
-                       0u,
-                       0u,
-                       1u
-               };
-               const VkExternalMemoryImageCreateInfo externalInfo              =
-               {
-                       VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
-                       DE_NULL,
-                       (VkExternalMemoryHandleTypeFlags)externalType
-               };
-               const VkImageCreateInfo                 createInfo                              =
-               {
-                       VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
-                       &externalInfo,
-                       0u,
-
-                       resourceDesc.imageType,
-                       resourceDesc.imageFormat,
-                       extent,
-                       1u,
-                       1u,
-                       VK_SAMPLE_COUNT_1_BIT,
-                       VK_IMAGE_TILING_OPTIMAL,
-                       readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags(),
-                       VK_SHARING_MODE_EXCLUSIVE,
-
-                       1u,
-                       &queueFamilyIndex,
-                       VK_IMAGE_LAYOUT_UNDEFINED
-               };
-
-               Move<VkImage>                   image           = createImage(vkd, device, &createInfo);
-               MovePtr<Allocation>             allocation      = allocateAndBindMemory(vkd, device, *image, externalType, exportedMemoryTypeIndex);
-
-               return MovePtr<Resource>(new Resource(image, allocation, extent, resourceDesc.imageType, resourceDesc.imageFormat, subresourceRange, subresourceLayers));
-       }
-       else
-       {
-               const VkDeviceSize                                              offset                  = 0u;
-               const VkDeviceSize                                              size                    = static_cast<VkDeviceSize>(resourceDesc.size.x());
-               const VkBufferUsageFlags                                usage                   = readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags();
-               const VkExternalMemoryBufferCreateInfo  externalInfo    =
-               {
-                       VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
-                       DE_NULL,
-                       (VkExternalMemoryHandleTypeFlags)externalType
-               };
-               const VkBufferCreateInfo                                createInfo              =
-               {
-                       VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
-                       &externalInfo,
-                       0u,
-
-                       size,
-                       usage,
-                       VK_SHARING_MODE_EXCLUSIVE,
-                       1u,
-                       &queueFamilyIndex
-               };
-               Move<VkBuffer>                                                  buffer          = createBuffer(vkd, device, &createInfo);
-               MovePtr<Allocation>                                             allocation      = allocateAndBindMemory(vkd, device, *buffer, externalType, exportedMemoryTypeIndex);
-
-               return MovePtr<Resource>(new Resource(resourceDesc.type, buffer, allocation, offset, size));
-       }
-}
-
 de::MovePtr<Resource> importResource (const DeviceInterface&                           vkd,
                                                                          VkDevice                                                              device,
                                                                          const ResourceDescription&                    resourceDesc,
@@ -626,6 +517,67 @@ public:
 
        }
 
+       Move<VkImage> createImage (const vk::DeviceInterface&   vkd,
+                                                          vk::VkDevice                                 device,
+                                                          const vk::VkExtent3D&                extent,
+                                                          deUint32                                             queueFamilyIndex)
+       {
+               const VkExternalMemoryImageCreateInfo externalInfo =
+               {
+                       VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
+                       DE_NULL,
+                       (VkExternalMemoryHandleTypeFlags)m_memoryHandleType
+               };
+               const VkImageCreateInfo createInfo =
+               {
+                       VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
+                       &externalInfo,
+                       0u,
+
+                       m_resourceDesc.imageType,
+                       m_resourceDesc.imageFormat,
+                       extent,
+                       1u,
+                       1u,
+                       VK_SAMPLE_COUNT_1_BIT,
+                       VK_IMAGE_TILING_OPTIMAL,
+                       m_readOpSupport->getInResourceUsageFlags() | m_writeOpSupport->getOutResourceUsageFlags(),
+                       VK_SHARING_MODE_EXCLUSIVE,
+
+                       1u,
+                       &queueFamilyIndex,
+                       VK_IMAGE_LAYOUT_UNDEFINED
+               };
+
+               return vk::createImage(vkd, device, &createInfo);
+       }
+
+       Move<VkBuffer> createBuffer (const vk::DeviceInterface&         vkd,
+                                                                vk::VkDevice                                   device,
+                                                                const vk::VkDeviceSize&                size,
+                                                                deUint32                                               queueFamilyIndex)
+       {
+               const VkExternalMemoryBufferCreateInfo  externalInfo =
+               {
+                       VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
+                       DE_NULL,
+                       (VkExternalMemoryHandleTypeFlags)m_memoryHandleType
+               };
+               const VkBufferCreateInfo                                createInfo =
+               {
+                       VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+                       &externalInfo,
+                       0u,
+
+                       size,
+                       m_readOpSupport->getInResourceUsageFlags() | m_writeOpSupport->getOutResourceUsageFlags(),
+                       VK_SHARING_MODE_EXCLUSIVE,
+                       1u,
+                       &queueFamilyIndex
+               };
+               return vk::createBuffer(vkd, device, &createInfo);
+       }
+
        tcu::TestStatus iterate (void)
        {
                // We're using 2 devices to make sure we have 2 queues even on
@@ -657,24 +609,65 @@ public:
                std::vector<VkSemaphore>                                                        semaphoreHandlesB;
                std::vector<deUint64>                                                           timelineValuesA;
                std::vector<deUint64>                                                           timelineValuesB;
-               std::vector<QueueSubmitOrderSharedIteration>            iterations;
+               std::vector<QueueSubmitOrderSharedIteration>            iterations(12);
                std::vector<VkPipelineStageFlags>                                       stageBits;
 
                // Create a dozen of set of write/read operations.
-               iterations.resize(12);
                for (deUint32 iterIdx = 0; iterIdx < iterations.size(); iterIdx++)
                {
                        QueueSubmitOrderSharedIteration&        iter                            = iterations[iterIdx];
                        deUint32                                                        memoryTypeIndex;
                        NativeHandle                                            nativeMemoryHandle;
 
-                       iter.resourceA  = makeSharedPtr(createResource(vkA, deviceA,
-                                                                                                                  m_resourceDesc,
-                                                                                                                  universalQueueFamilyIndex,
-                                                                                                                  *m_readOpSupport,
-                                                                                                                  *m_writeOpSupport,
-                                                                                                                  m_memoryHandleType,
-                                                                                                                  memoryTypeIndex));
+                       if (m_resourceDesc.type == RESOURCE_TYPE_IMAGE)
+                       {
+                               const VkExtent3D                                extent =
+                               {
+                                       (deUint32)m_resourceDesc.size.x(),
+                                       de::max(1u, (deUint32)m_resourceDesc.size.y()),
+                                       de::max(1u, (deUint32)m_resourceDesc.size.z())
+                               };
+                               const VkImageSubresourceRange   subresourceRange =
+                               {
+                                       m_resourceDesc.imageAspect,
+                                       0u,
+                                       1u,
+                                       0u,
+                                       1u
+                               };
+                               const VkImageSubresourceLayers  subresourceLayers =
+                               {
+                                       m_resourceDesc.imageAspect,
+                                       0u,
+                                       0u,
+                                       1u
+                               };
+
+                               Move<VkImage>                                                   image                   = createImage(vkA, deviceA, extent, universalQueueFamilyIndex);
+                               const vk::VkMemoryRequirements                  requirements    = getMemoryRequirements(vkA, deviceA, *image);
+                                                                                                               memoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
+                               vk::Move<vk::VkDeviceMemory>                    memory                  = allocateExportableMemory(vkA, deviceA, requirements.size, memoryTypeIndex, m_memoryHandleType, *image);
+
+                               VK_CHECK(vkA.bindImageMemory(deviceA, *image, *memory, 0u));
+
+                               MovePtr<Allocation> allocation(new SimpleAllocation(vkA, deviceA, memory.disown()));
+                               iter.resourceA = makeSharedPtr(new Resource(image, allocation, extent, m_resourceDesc.imageType, m_resourceDesc.imageFormat, subresourceRange, subresourceLayers));
+                       }
+                       else
+                       {
+                               const VkDeviceSize                                              offset                  = 0u;
+                               const VkDeviceSize                                              size                    = static_cast<VkDeviceSize>(m_resourceDesc.size.x());
+                               Move<VkBuffer>                                                  buffer                  = createBuffer(vkA, deviceA, size, universalQueueFamilyIndex);
+                               const vk::VkMemoryRequirements                  requirements    = getMemoryRequirements(vkA, deviceA, *buffer);
+                                                                                                               memoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
+                               vk::Move<vk::VkDeviceMemory>                    memory                  = allocateExportableMemory(vkA, deviceA, requirements.size, memoryTypeIndex, m_memoryHandleType, *buffer);
+
+                               VK_CHECK(vkA.bindBufferMemory(deviceA, *buffer, *memory, 0u));
+
+                               MovePtr<Allocation> allocation(new SimpleAllocation(vkA, deviceA, memory.disown()));
+                               iter.resourceA = makeSharedPtr(new Resource(m_resourceDesc.type, buffer, allocation, offset, size));
+                       }
+
                        getMemoryNative(vkA, deviceA, iter.resourceA->getMemory(), m_memoryHandleType, nativeMemoryHandle);
                        iter.resourceB  = makeSharedPtr(importResource(vkB, *deviceB,
                                                                                                                   m_resourceDesc,
index 076dd8c..e7bf419 100644 (file)
@@ -52,19 +52,6 @@ namespace ExternalMemoryUtil
 {
 namespace
 {
-deUint32 chooseMemoryType (deUint32 bits)
-{
-       DE_ASSERT(bits != 0);
-
-       for (deUint32 memoryTypeIndex = 0; (1u << memoryTypeIndex) <= bits; memoryTypeIndex++)
-       {
-               if ((bits & (1u << memoryTypeIndex)) != 0)
-                       return memoryTypeIndex;
-       }
-
-       DE_FATAL("No supported memory types");
-       return -1;
-}
 
 } // anonymous
 
@@ -902,14 +889,42 @@ vk::Move<vk::VkSemaphore> createAndImportSemaphore (const vk::DeviceInterface&
        return semaphore;
 }
 
+deUint32 chooseMemoryType(deUint32 bits)
+{
+       DE_ASSERT(bits != 0);
+
+       for (deUint32 memoryTypeIndex = 0; (1u << memoryTypeIndex) <= bits; memoryTypeIndex++)
+       {
+               if ((bits & (1u << memoryTypeIndex)) != 0)
+                       return memoryTypeIndex;
+       }
+
+       DE_FATAL("No supported memory types");
+       return -1;
+}
+
+deUint32 chooseHostVisibleMemoryType (deUint32 bits, const vk::VkPhysicalDeviceMemoryProperties properties)
+{
+       DE_ASSERT(bits != 0);
+
+       for (deUint32 memoryTypeIndex = 0; (1u << memoryTypeIndex) <= bits; memoryTypeIndex++)
+       {
+               if (((bits & (1u << memoryTypeIndex)) != 0) &&
+                       ((properties.memoryTypes[memoryTypeIndex].propertyFlags & vk::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0))
+                       return memoryTypeIndex;
+       }
+
+       TCU_THROW(NotSupportedError, "No supported memory type found");
+       return -1;
+}
+
 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface&                                      vkd,
                                                                                                           vk::VkDevice                                                                 device,
-                                                                                                          const vk::VkMemoryRequirements&                              requirements,
+                                                                                                          vk::VkDeviceSize                                                             allocationSize,
+                                                                                                          deUint32                                                                             memoryTypeIndex,
                                                                                                           vk::VkExternalMemoryHandleTypeFlagBits               externalType,
-                                                                                                          vk::VkBuffer                                                                 buffer,
-                                                                                                          deUint32&                                                                    exportedMemoryTypeIndex)
+                                                                                                          vk::VkBuffer                                                                 buffer)
 {
-       exportedMemoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
        const vk::VkMemoryDedicatedAllocateInfo dedicatedInfo   =
        {
                vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
@@ -928,20 +943,19 @@ vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface
        {
                vk::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
                &exportInfo,
-               requirements.size,
-               exportedMemoryTypeIndex
+               allocationSize,
+               memoryTypeIndex
        };
        return vk::allocateMemory(vkd, device, &info);
 }
 
 vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface&                                      vkd,
                                                                                                           vk::VkDevice                                                                 device,
-                                                                                                          const vk::VkMemoryRequirements&                              requirements,
+                                                                                                          vk::VkDeviceSize                                                             allocationSize,
+                                                                                                          deUint32                                                                             memoryTypeIndex,
                                                                                                           vk::VkExternalMemoryHandleTypeFlagBits               externalType,
-                                                                                                          vk::VkImage                                                                  image,
-                                                                                                          deUint32&                                                                    exportedMemoryTypeIndex)
+                                                                                                          vk::VkImage                                                                  image)
 {
-       exportedMemoryTypeIndex = chooseMemoryType(requirements.memoryTypeBits);
        const vk::VkMemoryDedicatedAllocateInfo dedicatedInfo   =
        {
                vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
@@ -960,59 +974,12 @@ vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::DeviceInterface
        {
                vk::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
                &exportInfo,
-               requirements.size,
-               exportedMemoryTypeIndex
+               allocationSize,
+               memoryTypeIndex
        };
        return vk::allocateMemory(vkd, device, &info);
 }
 
-vk::Move<vk::VkDeviceMemory> allocateExportableMemory (const vk::InstanceInterface&                                    vki,
-                                                                                                          vk::VkPhysicalDevice                                                 physicalDevice,
-                                                                                                          const vk::DeviceInterface&                                   vkd,
-                                                                                                          vk::VkDevice                                                                 device,
-                                                                                                          const vk::VkMemoryRequirements&                              requirements,
-                                                                                                          vk::VkExternalMemoryHandleTypeFlagBits               externalType,
-                                                                                                          bool                                                                                 hostVisible,
-                                                                                                          vk::VkBuffer                                                                 buffer,
-                                                                                                          deUint32&                                                                    exportedMemoryTypeIndex)
-{
-       const vk::VkPhysicalDeviceMemoryProperties properties = vk::getPhysicalDeviceMemoryProperties(vki, physicalDevice);
-
-       for (deUint32 memoryTypeIndex = 0; (1u << memoryTypeIndex) <= requirements.memoryTypeBits; memoryTypeIndex++)
-       {
-               if (((requirements.memoryTypeBits & (1u << memoryTypeIndex)) != 0)
-                       && (((properties.memoryTypes[memoryTypeIndex].propertyFlags & vk::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0) == hostVisible))
-               {
-                       const vk::VkMemoryDedicatedAllocateInfo dedicatedInfo   =
-                       {
-                               vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
-                               DE_NULL,
-
-                               (vk::VkImage)0,
-                               buffer
-                       };
-                       const vk::VkExportMemoryAllocateInfo    exportInfo      =
-                       {
-                               vk::VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
-                               !!buffer ? &dedicatedInfo : DE_NULL,
-                               (vk::VkExternalMemoryHandleTypeFlags)externalType
-                       };
-                       const vk::VkMemoryAllocateInfo                  info            =
-                       {
-                               vk::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
-                               &exportInfo,
-                               requirements.size,
-                               memoryTypeIndex
-                       };
-
-                       exportedMemoryTypeIndex = memoryTypeIndex;
-                       return vk::allocateMemory(vkd, device, &info);
-               }
-       }
-
-       TCU_THROW(NotSupportedError, "No supported memory type found");
-}
-
 static vk::Move<vk::VkDeviceMemory> importMemory (const vk::DeviceInterface&                           vkd,
                                                                                                  vk::VkDevice                                                          device,
                                                                                                  vk::VkBuffer                                                          buffer,
index aa7c8f0..ab1654c 100644 (file)
@@ -257,7 +257,7 @@ void                                                        getFenceNative                                          (const vk::DeviceInterface&                                     vkd,
                                                                                                                                         vk::VkFence                                                            fence,
                                                                                                                                         vk::VkExternalFenceHandleTypeFlagBits          externalType,
                                                                                                                                         NativeHandle&                                                          nativeHandle,
-                                                                     bool expectFenceUnsignaled = true);
+                                                                                                                                        bool expectFenceUnsignaled = true);
 
 void                                                   importFence                                                     (const vk::DeviceInterface&                                     vkd,
                                                                                                                                         const vk::VkDevice                                                     device,
@@ -272,28 +272,28 @@ vk::Move<vk::VkFence>                     createAndImportFence                            (const vk::DeviceInterface&
                                                                                                                                         NativeHandle&                                                          handle,
                                                                                                                                         vk::VkFenceImportFlags                                         flags);
 
-vk::Move<vk::VkDeviceMemory>   allocateExportableMemory                        (const vk::DeviceInterface&                                     vkd,
-                                                                                                                                        vk::VkDevice                                                           device,
-                                                                                                                                        const vk::VkMemoryRequirements&                        requirements,
-                                                                                                                                        vk::VkExternalMemoryHandleTypeFlagBits         externalType,
-                                                                                                                                        deUint32&                                                                      exportedMemoryTypeIndex);
+deUint32                                               chooseMemoryType                                        (deUint32                                                                       bits);
+
+deUint32                                               chooseHostVisibleMemoryType                     (deUint32                                                                       bits,
+                                                                                                                                        const vk::VkPhysicalDeviceMemoryProperties     properties);
 
 // If buffer is not null use dedicated allocation
 vk::Move<vk::VkDeviceMemory>   allocateExportableMemory                        (const vk::DeviceInterface&                                     vkd,
                                                                                                                                         vk::VkDevice                                                           device,
-                                                                                                                                        const vk::VkMemoryRequirements&                        requirements,
+                                                                                                                                        vk::VkDeviceSize                                                       allocationSize,
+                                                                                                                                        deUint32                                                                       memoryTypeIndex,
                                                                                                                                         vk::VkExternalMemoryHandleTypeFlagBits         externalType,
-                                                                                                                                        vk::VkBuffer                                                           buffer,
-                                                                                                                                        deUint32&                                                                      exportedMemoryTypeIndex);
+                                                                                                                                        vk::VkBuffer                                                           buffer);
 
 // If image is not null use dedicated allocation
 vk::Move<vk::VkDeviceMemory>   allocateExportableMemory                        (const vk::DeviceInterface&                                     vkd,
                                                                                                                                         vk::VkDevice                                                           device,
-                                                                                                                                        const vk::VkMemoryRequirements&                        requirements,
+                                                                                                                                        vk::VkDeviceSize                                                       allocationSize,
+                                                                                                                                        deUint32                                                                       memoryTypeIndex,
                                                                                                                                         vk::VkExternalMemoryHandleTypeFlagBits         externalType,
-                                                                                                                                        vk::VkImage                                                            image,
-                                                                                                                                        deUint32&                                                                      exportedMemoryTypeIndex);
+                                                                                                                                        vk::VkImage                                                            image);
 
+/*
 // \note hostVisible argument is strict. Setting it to false will cause NotSupportedError to be thrown if non-host visible memory doesn't exist.
 // If buffer is not null use dedicated allocation
 vk::Move<vk::VkDeviceMemory>   allocateExportableMemory                        (const vk::InstanceInterface&                           vki,
@@ -305,6 +305,7 @@ vk::Move<vk::VkDeviceMemory>        allocateExportableMemory                        (const vk::InstanceInter
                                                                                                                                         bool                                                                           hostVisible,
                                                                                                                                         vk::VkBuffer                                                           buffer,
                                                                                                                                         deUint32&                                                                      exportedMemoryTypeIndex);
+*/
 
 vk::Move<vk::VkDeviceMemory>   importMemory                                            (const vk::DeviceInterface&                                     vkd,
                                                                                                                                         vk::VkDevice                                                           device,