Hi,
When creating a shared d3d11 texture with miplevels = 1 and import it into Vulkan with VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT usage bit, vkBindImageMemory returns VK_ERROR_UNKNOWN. The following code demonstrate the issue:
// D3D11 texture
CD3D11_TEXTURE2D_DESC desc2d(DXGI_FORMAT_B8G8R8A8_UNORM, 256, 256);
desc2d.MiscFlags = D3D10_RESOURCE_MISC_SHARED;
desc2d.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
desc2d.MipLevels = 1;
Microsoft::WRL::ComPtr<ID3D11Texture2D> tex;
checkHResult(dev->CreateTexture2D(&desc2d, nullptr, &tex));
Microsoft::WRL::ComPtr<IDXGIResource> resource;
checkHResult(tex.As(&resource));
HANDLE sharedHandle;
checkHResult(resource->GetSharedHandle(&sharedHandle));
// Vulkan image
auto handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT;
VkImage image;
VkExternalMemoryImageCreateInfo externalInfo = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, nullptr, static_cast<VkExternalMemoryHandleTypeFlags>(handleType)};
VkImageCreateInfo imageInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, &externalInfo};
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
imageInfo.extent = {256, 256, 1};
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
checkResult(vkCreateImage(g_Device, &imageInfo, nullptr, &image));
VkMemoryRequirements memReq;
vkGetImageMemoryRequirements(g_Device, image, &memReq);
// Vulkan memory import
VkMemoryWin32HandlePropertiesKHR handleProperties = {VK_STRUCTURE_TYPE_MEMORY_WIN32_HANDLE_PROPERTIES_KHR};
checkResult(g_vkGetMemoryWin32HandlePropertiesKHR(g_Device, handleType, sharedHandle, &handleProperties));
VkDeviceMemory memory;
VkImportMemoryWin32HandleInfoKHR importInfo = {VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR, nullptr, handleType, sharedHandle};
VkMemoryAllocateInfo allocInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &importInfo};
allocInfo.memoryTypeIndex = findMemoryType(handleProperties.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
allocInfo.allocationSize = memReq.size;
checkResult(vkAllocateMemory(g_Device, &allocInfo, nullptr, &memory));
checkResult(vkBindImageMemory(g_Device, image, memory, 0));
vkDestroyImage(g_Device, image, nullptr);
vkFreeMemory(g_Device, memory, nullptr);
It is kind of strange that two conditions have to be met in order to reproduce the error: 1. the d3d11 texture is created with MipLevels = 1 only; 2. the Vulkan image is created with color attachment usage bit.
Solved! Go to Solution.
I have solved the issue. It must check the VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT feature and add VkMemoryDedicatedAllocateInfo accordingly when importing the d3d11 texture.
Try posting your question at AMD Forum's Developers that has a Vulkan Forum. But first you must get whitelisted to join the Vulkan forum from here: https://community.amd.com/t5/newcomers-start-here/bd-p/newcomer-forum
The Moderator should move your thread to the OpenCL & Vulkan Forum: https://community.amd.com/t5/opengl-vulkan/bd-p/opengl-and-vulkan-discussions
Moved the post to the "AMD OpenGL & Vulkan" forum.
Thanks.
I have solved the issue. It must check the VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT feature and add VkMemoryDedicatedAllocateInfo accordingly when importing the d3d11 texture.