DMA buf import into Vulkan

85 views Asked by At

I am trying to import a DMA buffer I created with the DMA-buf heaps protocol into Vulkan. The DMA-buf is located in /cma-uncached memory. The chip is an i.MX8M Plus. I can mmap() the DMA-buf and it seems to be functioning correctly. I have attached the file descriptor for the DMA-buf to the VkDeviceMemory using vkImportMemoryFdInfoKHR. I then used vkBindBufferMemory to attach a VkBuffer to the VkDeviceMemory. Vulkan validation layers are not indicating any problems but the VkBuffer does not hold the data from the DMA-buf when I check with vkMapMemory. The portion of the code which attempts to import the DMA-buf into Vulkan is shown below. Please let me know if there is something obvious I am leaving out.

    VkExternalMemoryBufferCreateInfo extMemBufInfo{};
    extMemBufInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO;
    extMemBufInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
    extMemBufInfo.pNext = NULL;

    int FD_width = widthVIV;
    int FD_height = heightVIV;
    int size = FD_width * FD_height * 2;

    VkBufferCreateInfo bufferInfo{};
    bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
    bufferInfo.size = size;
    bufferInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
    bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    bufferInfo.flags = 0;
    bufferInfo.pNext = &extMemBufInfo;

    if (vkCreateBuffer(device, &bufferInfo, nullptr, &fdBuffer) != VK_SUCCESS) {
        qDebug("failed to create buffer!");
    }

    VkMemoryRequirements memRequirements;
    vkGetBufferMemoryRequirements(device, fdBuffer, &memRequirements);

    VkMemoryDedicatedAllocateInfo memDedAllocInfo{};
    memset(&memDedAllocInfo, 0, sizeof(memDedAllocInfo));
    memDedAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
    memDedAllocInfo.buffer = fdBuffer;

    VkImportMemoryFdInfoKHR importInfo{};
    memset(&importInfo, 0, sizeof(importInfo));
    importInfo.fd = dmafd;//expbuf.fd;//buf.m.fd;//dmabuffd;//heap_data.fd;// dmafd[0];
    importInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;
    importInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;//VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;//
    importInfo.pNext = &memDedAllocInfo;

    VkMemoryAllocateInfo allocInfo{};
    allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    allocInfo.allocationSize = memRequirements.size;
    allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
    allocInfo.pNext = &importInfo;

    if (vkAllocateMemory(device, &allocInfo, nullptr, &fdBufferMemory) != VK_SUCCESS) {
        qDebug("failed to allocate image memory!");
    }

    if(vkBindBufferMemory(device, fdBuffer, fdBufferMemory, 0) != VK_SUCCESS) {
          qDebug() << "vkBindBufferMemory failed: " << strerror(errno);
}
0

There are 0 answers