Skip to content

Commit d9ff0b4

Browse files
committed
Changes in TestWin32HandlesExport
Based on a patch proposed in #503 - thanks @Agrael1
1 parent e64d42b commit d9ff0b4

File tree

1 file changed

+30
-185
lines changed

1 file changed

+30
-185
lines changed

src/Tests.cpp

Lines changed: 30 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -8529,7 +8529,7 @@ static void TestWin32HandlesExport()
85298529
if((externalBufferProperties.externalMemoryProperties.externalMemoryFeatures &
85308530
VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) == 0)
85318531
{
8532-
wprintf(L"WARNING: External memory not exportable, skipping test.\n");
8532+
wprintf(L" WARNING: External memory not exportable, skipping test.\n");
85338533
return;
85348534
}
85358535
requiresDedicated = (externalBufferProperties.externalMemoryProperties.externalMemoryFeatures &
@@ -8587,180 +8587,6 @@ static void TestWin32HandlesImport()
85878587

85888588
wprintf(L"Test Win32 handles import\n");
85898589

8590-
constexpr VkExternalMemoryHandleTypeFlagBits handleType =
8591-
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
8592-
constexpr uint32_t dataValue = 0x72158510;
8593-
8594-
PFN_vkGetPhysicalDeviceExternalBufferProperties pfnGetPhysicalDeviceExternalBufferProperties =
8595-
(PFN_vkGetPhysicalDeviceExternalBufferProperties)
8596-
vkGetInstanceProcAddr(g_hVulkanInstance, "vkGetPhysicalDeviceExternalBufferProperties");
8597-
TEST(pfnGetPhysicalDeviceExternalBufferProperties != nullptr);
8598-
8599-
for(size_t testIndex = 0; testIndex < 4; ++testIndex)
8600-
{
8601-
const bool testImport = (testIndex & 1) != 0;
8602-
const bool testCreateBuffer = (testIndex & 2) != 0;
8603-
8604-
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
8605-
bufCreateInfo.size = 0x10000; // 64 KB
8606-
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
8607-
8608-
HANDLE sharedHandle = NULL;
8609-
if(testImport)
8610-
{
8611-
VkPhysicalDeviceExternalBufferInfo externalBufInfo = {
8612-
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_BUFFER_INFO };
8613-
externalBufInfo.flags = bufCreateInfo.flags;
8614-
externalBufInfo.usage = bufCreateInfo.usage;
8615-
externalBufInfo.handleType = handleType;
8616-
8617-
VkExternalBufferProperties externalBufProps = {
8618-
VK_STRUCTURE_TYPE_EXTERNAL_BUFFER_PROPERTIES };
8619-
8620-
pfnGetPhysicalDeviceExternalBufferProperties(g_hPhysicalDevice,
8621-
&externalBufInfo, &externalBufProps);
8622-
8623-
if((externalBufProps.externalMemoryProperties.externalMemoryFeatures &
8624-
VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT) == 0)
8625-
{
8626-
wprintf(L" WARNING: External memory not importable, skipping test.\n");
8627-
continue;
8628-
}
8629-
8630-
const wchar_t* mappingName = L"MySharedVulkanMemory";
8631-
sharedHandle = CreateFileMapping(
8632-
INVALID_HANDLE_VALUE, // hFile - only in memory, no file.
8633-
NULL, // lpFileMappingAttributes
8634-
PAGE_READWRITE,
8635-
0, // dwMaximumSizeHigh
8636-
(DWORD)bufCreateInfo.size, // dwMaximumSizeLow
8637-
mappingName);
8638-
TEST(sharedHandle != NULL);
8639-
8640-
// Map the memory temporarily and write the dataValue to it.
8641-
void* sharedMemoryPtr = MapViewOfFile(
8642-
sharedHandle,
8643-
FILE_MAP_ALL_ACCESS,
8644-
0, // dwFileOffsetHigh
8645-
0, // dwFileOffsetLow
8646-
bufCreateInfo.size);
8647-
TEST(sharedMemoryPtr != NULL);
8648-
memcpy(sharedMemoryPtr, &dataValue, sizeof(dataValue));
8649-
UnmapViewOfFile(sharedMemoryPtr);
8650-
}
8651-
8652-
VkImportMemoryWin32HandleInfoKHR importInfo = {
8653-
VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
8654-
VkExternalMemoryBufferCreateInfoKHR externalMemBufCreateInfo = {
8655-
VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR };
8656-
void* memoryAllocateNext = nullptr;
8657-
8658-
if(testImport)
8659-
{
8660-
externalMemBufCreateInfo.handleTypes = handleType;
8661-
bufCreateInfo.pNext = &externalMemBufCreateInfo;
8662-
8663-
importInfo.handleType = handleType;
8664-
importInfo.handle = sharedHandle;
8665-
memoryAllocateNext = &importInfo;
8666-
}
8667-
8668-
VmaAllocationCreateInfo allocCreateInfo = {};
8669-
// We would like read the data. We cannot use VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT
8670-
// as we don't use VMA_MEMORY_USAGE_AUTO.
8671-
allocCreateInfo.preferredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
8672-
VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
8673-
8674-
VkBuffer buf = VK_NULL_HANDLE;
8675-
VmaAllocation alloc = VK_NULL_HANDLE;
8676-
8677-
if (testCreateBuffer)
8678-
{
8679-
VkResult res = vmaCreateDedicatedBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
8680-
memoryAllocateNext, &buf, &alloc, nullptr);
8681-
if (res != VK_SUCCESS)
8682-
{
8683-
TEST(alloc == VK_NULL_HANDLE && buf == VK_NULL_HANDLE);
8684-
if (res == VK_ERROR_FEATURE_NOT_PRESENT)
8685-
{
8686-
wprintf(L" WARNING: Couldn't create dedicated buffer - returned VK_ERROR_FEATURE_NOT_PRESENT. Likely no eligible memory type found.\n");
8687-
}
8688-
else if (res == VK_ERROR_OUT_OF_DEVICE_MEMORY)
8689-
{
8690-
wprintf(L" WARNING: Couldn't create dedicated buffer - returned VK_ERROR_OUT_OF_DEVICE_MEMORY.\n");
8691-
}
8692-
else
8693-
{
8694-
wprintf(L" WARNING: Couldn't create dedicated buffer - returned other error %u.\n", res);
8695-
}
8696-
}
8697-
}
8698-
else
8699-
{
8700-
TEST(vkCreateBuffer(g_hDevice, &bufCreateInfo, g_Allocs, &buf) == VK_SUCCESS);
8701-
8702-
VkMemoryRequirements memReq = {};
8703-
vkGetBufferMemoryRequirements(g_hDevice, buf, &memReq);
8704-
8705-
VkResult res = vmaAllocateDedicatedMemory(g_hAllocator, &memReq,
8706-
&allocCreateInfo, memoryAllocateNext, &alloc, nullptr);
8707-
if(res != VK_SUCCESS)
8708-
{
8709-
TEST(alloc == VK_NULL_HANDLE);
8710-
if (res == VK_ERROR_FEATURE_NOT_PRESENT)
8711-
{
8712-
wprintf(L" WARNING: Couldn't allocate dedicated memory - returned VK_ERROR_FEATURE_NOT_PRESENT. Likely no eligible memory type found.\n");
8713-
}
8714-
else if(res == VK_ERROR_OUT_OF_DEVICE_MEMORY)
8715-
{
8716-
wprintf(L" WARNING: Couldn't allocate dedicated memory - returned VK_ERROR_OUT_OF_DEVICE_MEMORY.\n");
8717-
}
8718-
else
8719-
{
8720-
wprintf(L" WARNING: Couldn't allocate dedicated memory - returned other error %u.\n", res);
8721-
}
8722-
}
8723-
8724-
if(alloc)
8725-
{
8726-
TEST(vmaBindBufferMemory(g_hAllocator, alloc, buf) == VK_SUCCESS);
8727-
}
8728-
}
8729-
8730-
if(alloc)
8731-
{
8732-
VmaAllocationInfo2 allocInfo2 = {};
8733-
vmaGetAllocationInfo2(g_hAllocator, alloc, &allocInfo2);
8734-
TEST(allocInfo2.dedicatedMemory);
8735-
8736-
VkMemoryPropertyFlags memPropsFlags = 0;
8737-
vmaGetAllocationMemoryProperties(g_hAllocator, alloc, &memPropsFlags);
8738-
const bool memoryMappable = (memPropsFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
8739-
8740-
if(testImport)
8741-
{
8742-
if(memoryMappable)
8743-
{
8744-
uint32_t readValue = 0;
8745-
TEST(vmaCopyAllocationToMemory(g_hAllocator, alloc, 0, &readValue, sizeof readValue) == VK_SUCCESS);
8746-
TEST(readValue == dataValue);
8747-
}
8748-
else
8749-
{
8750-
wprintf(L" WARNING: Allocation ended up in a non-HOST_VISIBLE memory.\n");
8751-
}
8752-
}
8753-
}
8754-
8755-
vmaDestroyBuffer(g_hAllocator, buf, alloc);
8756-
8757-
if(testImport)
8758-
{
8759-
CloseHandle(sharedHandle);
8760-
}
8761-
}
8762-
8763-
#if 0
87648590
constexpr VkExternalMemoryHandleTypeFlagBits handleType =
87658591
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
87668592

@@ -8794,10 +8620,12 @@ static void TestWin32HandlesImport()
87948620

87958621
vkGetPhysicalDeviceExternalBufferProperties(g_hPhysicalDevice,
87968622
&externalBufferInfo, &externalBufferProperties);
8623+
constexpr VkExternalMemoryFeatureFlags expectedFlags =
8624+
VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
87978625
if((externalBufferProperties.externalMemoryProperties.externalMemoryFeatures &
8798-
VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) == 0)
8626+
expectedFlags) != expectedFlags)
87998627
{
8800-
wprintf(L"WARNING: External memory not exportable, skipping test.\n");
8628+
wprintf(L" WARNING: External memory not exportable and importable, skipping test.\n");
88018629
return;
88028630
}
88038631
requiresDedicated = (externalBufferProperties.externalMemoryProperties.externalMemoryFeatures &
@@ -8831,21 +8659,38 @@ static void TestWin32HandlesImport()
88318659
VmaAllocation alloc = VK_NULL_HANDLE;
88328660
TEST(vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, nullptr) == VK_SUCCESS);
88338661
HANDLE handle = NULL;
8834-
HANDLE handle2 = NULL;
88358662
TEST(vmaGetMemoryWin32Handle(g_hAllocator, alloc, nullptr, &handle) == VK_SUCCESS);
88368663
TEST(handle != nullptr);
8837-
TEST(vmaGetMemoryWin32Handle(g_hAllocator, alloc, nullptr, &handle2) == VK_SUCCESS);
8838-
TEST(handle2 != nullptr);
8839-
TEST(handle2 != handle);
88408664

8665+
// Import it into another allocation.
8666+
VkImportMemoryWin32HandleInfoKHR importMemHandleInfo = {
8667+
VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
8668+
importMemHandleInfo.handleType = handleType;
8669+
importMemHandleInfo.handle = handle;
8670+
importMemHandleInfo.name = nullptr;
8671+
VmaAllocationCreateInfo importAllocCreateInfo = {};
8672+
importAllocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
8673+
8674+
VkBuffer importedBuf = VK_NULL_HANDLE;
8675+
VmaAllocation importedAlloc = VK_NULL_HANDLE;
8676+
TEST(vmaCreateDedicatedBuffer(g_hAllocator, &bufCreateInfo, &importAllocCreateInfo,
8677+
&importMemHandleInfo, &importedBuf, &importedAlloc, nullptr) == VK_SUCCESS);
8678+
TEST(importedBuf != VK_NULL_HANDLE);
8679+
TEST(importedAlloc != VK_NULL_HANDLE);
8680+
8681+
VmaAllocationInfo2 allocInfo2 = {};
8682+
vmaGetAllocationInfo2(g_hAllocator, importedAlloc, &allocInfo2);
8683+
if (test == 1)
8684+
{
8685+
TEST(allocInfo2.dedicatedMemory != VK_FALSE);
8686+
}
8687+
8688+
vmaDestroyBuffer(g_hAllocator, importedBuf, importedAlloc);
88418689
vmaDestroyBuffer(g_hAllocator, buf, alloc);
88428690
TEST(CloseHandle(handle));
8843-
TEST(CloseHandle(handle2));
88448691
}
88458692

88468693
vmaDestroyPool(g_hAllocator, pool);
8847-
#endif // #if 0
8848-
88498694
#endif
88508695
}
88518696

@@ -8895,7 +8740,7 @@ void Test()
88958740
TestDeviceLocalMapped();
88968741
TestMaintenance5();
88978742
TestWin32HandlesExport();
8898-
//TestWin32HandlesImport(); // Commented out because failing on some GPUs with strange errors.
8743+
TestWin32HandlesImport();
88998744
TestMappingMultithreaded();
89008745
TestLinearAllocator();
89018746
ManuallyTestLinearAllocator();

0 commit comments

Comments
 (0)