diff --git a/src/Tests.cpp b/src/Tests.cpp index 4703672..bb6650f 100644 --- a/src/Tests.cpp +++ b/src/Tests.cpp @@ -8258,15 +8258,50 @@ static void TestWin32Handles() { #if VMA_EXTERNAL_MEMORY_WIN32 wprintf(L"Test Win32 handles\n"); - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 1024; - bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; + constexpr static VkExportMemoryAllocateInfoKHR exportInfo{ + VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR, + nullptr, + VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT + }; + constexpr static VkExternalMemoryBufferCreateInfoKHR externalInfo{ + VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR, + nullptr, + VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT + }; + + VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + sampleBufCreateInfo.size = 0x1000; // Doesn't matter. + sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; + sampleBufCreateInfo.pNext = &externalInfo; + + VmaAllocationCreateInfo sampleAllocCreateInfo = {}; + sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; + sampleAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT; + + uint32_t memTypeIndex; + TEST(vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, + &sampleBufCreateInfo, &sampleAllocCreateInfo, &memTypeIndex) == VK_SUCCESS); + // Check res... + + + // Create a pool that can have at most 2 blocks, 128 MiB each. + VmaPoolCreateInfo poolCreateInfo = {}; + poolCreateInfo.memoryTypeIndex = memTypeIndex; + poolCreateInfo.blockSize = 128ull * 1024 * 1024; + poolCreateInfo.maxBlockCount = 2; + poolCreateInfo.pMemoryAllocateNext = (void*)&exportInfo; + + + VmaPool pool; + TEST(vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool) == VK_SUCCESS); + + + sampleAllocCreateInfo.pool = pool; + VkBuffer buf; VmaAllocation alloc; VmaAllocationInfo allocInfo; - TEST(vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo) == VK_SUCCESS); + TEST(vmaCreateBuffer(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &buf, &alloc, &allocInfo) == VK_SUCCESS); HANDLE handle; HANDLE handle2; TEST(vmaGetMemoryWin32HandleKHR(g_hAllocator, alloc, nullptr, &handle) == VK_SUCCESS); diff --git a/src/VulkanSample.cpp b/src/VulkanSample.cpp index 1a2ebf1..480b325 100644 --- a/src/VulkanSample.cpp +++ b/src/VulkanSample.cpp @@ -69,6 +69,7 @@ bool VK_KHR_buffer_device_address_enabled = false; bool VK_EXT_memory_priority_enabled = false; bool VK_EXT_debug_utils_enabled = false; bool VK_KHR_maintenance5_enabled = false; +bool VK_KHR_external_memory_win32_enabled = false; bool g_SparseBindingEnabled = false; // # Pointers to functions from extensions @@ -1449,6 +1450,7 @@ static void PrintEnabledFeatures() } wprintf(L"VK_EXT_memory_priority: %d\n", VK_EXT_memory_priority_enabled ? 1 : 0); wprintf(L"VK_KHR_maintenance5: %d\n", VK_KHR_maintenance5_enabled? 1 : 0); + wprintf(L"VK_KHR_external_memory_win32: %d\n", VK_KHR_external_memory_win32_enabled ? 1 : 0); } void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo) @@ -1494,6 +1496,11 @@ void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo) outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE5_BIT; } + if(VK_KHR_external_memory_win32_enabled) + { + outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32_BIT; + } + if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS) { outInfo.pAllocationCallbacks = &g_CpuAllocationCallbacks; @@ -1876,6 +1883,8 @@ static void InitializeApplication() VK_EXT_memory_priority_enabled = true; else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_MAINTENANCE_5_EXTENSION_NAME) == 0) VK_KHR_maintenance5_enabled = true; + else if (strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME) == 0) + VK_KHR_external_memory_win32_enabled = true; } if(GetVulkanApiVersion() >= VK_API_VERSION_1_2) @@ -2036,6 +2045,8 @@ static void InitializeApplication() enabledDeviceExtensions.push_back(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME); if(VK_KHR_maintenance5_enabled) enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); + if (VK_KHR_external_memory_win32_enabled) + enabledDeviceExtensions.push_back(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME); VkPhysicalDeviceFeatures2 deviceFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 }; deviceFeatures.features.samplerAnisotropy = VK_TRUE;