Home > Mobile >  Vulkan Image export handle to Win32 or fd. How to reverse win32 handle to obtain image information?
Vulkan Image export handle to Win32 or fd. How to reverse win32 handle to obtain image information?

Time:06-27

When I export a Win32 handle from the Vulkan image. Vulkan for another process. At this time, how to find the information of Vulkan Image according to win32 Handle reversely?

I created a Handle with Vulkan in a process and created OpenGL's texture according to the Handle. It is then shared with another process. So there is one of the above problems.

I don't know what the information of image is. How can I create an image and then import it into shared memory? I don't know what the image information is, so how can I create an OpenGL texture object? Therefore, I want to obtain the image information contained in Handle.

// Get the Vulkan texture and create the OpenGL equivalent using the memory allocated in Vulkan
inline void createTextureGL(nvvk::ResourceAllocator& alloc, Texture2DVkGL& texGl, int format, int minFilter, int magFilter, int wraps, int wrapt)
{
    vk::Device                  device = alloc.getDevice();
    nvvk::MemAllocator::MemInfo info = alloc.getMemoryAllocator()->getMemoryInfo(texGl.texVk.memHandle);

    texGl.handle = device.getMemoryWin32HandleKHR({ info.memory, vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32 });

    auto req = device.getImageMemoryRequirements(texGl.texVk.image);

    glCreateMemoryObjectsEXT(1, &texGl.memoryObject);

    glImportMemoryWin32HandleEXT(texGl.memoryObject, req.size, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, texGl.handle);

    glCreateTextures(GL_TEXTURE_2D, 1, &texGl.oglId);
    glTextureStorageMem2DEXT(texGl.oglId, texGl.mipLevels, format, texGl.imgSize.width, texGl.imgSize.height, texGl.memoryObject, info.offset);

goolge-image-import project:

    ::VkImage image = *images_[0];

    VkMemoryRequirements requirements;
    device_->vkGetImageMemoryRequirements(device_, image, &requirements);

    aligned_data_size_ =
        vulkan::RoundUp(requirements.size, requirements.alignment);

    uint32_t memory_index =
        vulkan::GetMemoryIndex(&device, log, requirements.memoryTypeBits,
                               VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

#ifdef _WIN32
    VkImportMemoryWin32HandleInfoKHR import_allocate_info{
        VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR, nullptr,
        VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT, handle, nullptr};
#elif __linux__
    VkImportMemoryFdInfoKHR import_allocate_info{
        VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, nullptr,
        VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, fd};
#endif

    VkMemoryAllocateInfo allocate_info{
        VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,  // sType
        &import_allocate_info,                   // pNext
        aligned_data_size_ * num_images,         // allocationSize
        memory_index};

    VkDeviceMemory device_memory;

    LOG_ASSERT(==, log, VK_SUCCESS,
               device->vkAllocateMemory(device, &allocate_info, nullptr,
                                        &device_memory));

CodePudding user response:

I don't know what the information of image is.

Yes, you do. You created it in Vulkan. You know its size. You know its format. You know everything about the image.

If you can pass a handle to this function to create an OpenGL texture, then you can pass the other information too.

There is no API to retrieve any information about the window. The driver may not even be keeping such information around, since it's information you already have and is therefore redundant.

  • Related