Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix importing AHB memory with rebind #1820

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

ziga-lunarg
Copy link
Contributor

VMA does not support external memory. If memory is allocated with AHardwareBuffer we must manually allocate it in rebind

@ci-tester-lunarg
Copy link

CI gfxreconstruct build queued with queue ID 282811.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5115 running.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build queued with queue ID 282831.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build queued with queue ID 282848.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5118 running.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5118 passed.

VMA does not support external memory. If memory is allocated with
AHardwareBuffer we must manually allocate it in rebind
@ci-tester-lunarg
Copy link

CI gfxreconstruct build queued with queue ID 283151.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5127 running.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5127 failed.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build queued with queue ID 283228.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5129 running.

@ci-tester-lunarg
Copy link

CI gfxreconstruct build # 5129 passed.

@bradgrantham-lunarg
Copy link
Contributor

@mikes-lunarg can you make a minute to look at this? Thank you!

allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocate_info.pNext = &dedicatedAllocateInfo;
allocate_info.allocationSize = memory_alloc_info->allocation_size;
allocate_info.memoryTypeIndex = memory_alloc_info->original_index;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to make an assumption that the memory type index won't change between capture and replay. Do we need a TODO or an issue to make this smarter? The rebind allocator is supposed to be the "most portable"

importAHBInfo.buffer = memory_alloc_info->ahb;

VkMemoryDedicatedAllocateInfo dedicatedAllocateInfo{};
dedicatedAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is dedicated allocation always required for AHB import? Or are you just giving the driver extra info since you know it is dedicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, dedicated allocation is mostly required with AHB import. There are some VUIDs for this, mainly VUID-VkMemoryAllocateInfo-pNext-02384

result = vmaAllocateMemoryForImage(allocator_, image, &create_info, &allocation, &allocation_info);
if (result >= 0)
{
assert(allocate_info.memoryTypeIndex < replay_memory_properties_.memoryTypeCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the memory index bounds to be checked before allocating the memory instead of after.

Comment on lines +841 to +863
VkImportAndroidHardwareBufferInfoANDROID importAHBInfo;
importAHBInfo.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID;
importAHBInfo.pNext = nullptr;
importAHBInfo.buffer = memory_alloc_info->ahb;

VkMemoryDedicatedAllocateInfo dedicatedAllocateInfo{};
dedicatedAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
dedicatedAllocateInfo.pNext = &importAHBInfo;
dedicatedAllocateInfo.image = image;

VkMemoryAllocateInfo allocate_info{};
allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocate_info.pNext = &dedicatedAllocateInfo;
allocate_info.allocationSize = memory_alloc_info->allocation_size;
allocate_info.memoryTypeIndex = memory_alloc_info->original_index;
result = functions_.allocate_memory(device_, &allocate_info, nullptr, &memory);

VmaAllocationInfo allocation_info;
result = vmaAllocateMemoryForImage(allocator_, image, &create_info, &allocation, &allocation_info);
if (result >= 0)
{
assert(allocate_info.memoryTypeIndex < replay_memory_properties_.memoryTypeCount);

if (result >= 0)
result = functions_.bind_image_memory(device_, image, memory, memory_offset);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block looks duplicated from BindImageMemory, does it make sense to put it in a helper?

auto memory_alloc_info = reinterpret_cast<MemoryAllocInfo*>(allocator_memory_data);
if (memory_alloc_info->ahb)
{
VkDeviceMemory memory = bind_infos[i].memory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why initialize memory from bind_infos? It will just get overwritten.

allocate_info.pNext = &dedicatedAllocateInfo;
allocate_info.allocationSize = memory_alloc_info->allocation_size;
allocate_info.memoryTypeIndex = memory_alloc_info->original_index;
result = functions_.allocate_memory(device_, &allocate_info, nullptr, &memory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer not to reuse the input value memory to hold the new memory handle. Also it seems like we are going to leak this handle and never free it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VkMemory is just kPlaceholderHandleId, since VulkanRebindAllocator::AllocateMemory doesn't allocate any memory. But you're right it is better to use a different variable for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants