c++windowssdlvulkan

`vkGetMemoryWin32HandleKHR` tries to dereference a null address


I'm trying to create a shared texture between Vulkan and OpenGL, and as I understand the process, it's split in two parts: exporting the memory during allocation and then getting a Windows HANDLE to duplicate and pass to another process.

Allocating the memory passes with no obvious issues:

    auto vulkanImage = ...;
    auto memoryRequirements = device.getImageMemoryRequirements(vulkanImage);
    vk::ExportMemoryAllocateInfo exportInfo{
            .handleTypes = vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32
    };
    vulkanImageMemory = device.allocateMemory({
        .pNext = &exportInfo,
        .allocationSize = memoryRequirements.size,
        .memoryTypeIndex = findMemoryType(physicalDevice, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eDeviceLocal),
        });
    device.bindImageMemory(vulkanImage, vulkanImageMemory, 0);

Trying to get the handle however fails with access violation trying to dereference a null pointer:

auto hTextureMem = (HANDLE)device.getMemoryWin32HandleKHR({
    .memory = vulkanImageMemory,
    .handleType = vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32
    });

Debugging notes:

Any ideas how to get my HANDLE object?


Solution

  • So as it turns out, I didn't explicitly request the external memory extensions. Even though the function pointers existed, internally they weren't bound correctly I suppose.

    The necessary instance extensions were:

    VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME
    VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME
    

    And the necessary device extensions were:

    VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME
    VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME
    VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME
    VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME