Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yknishidate/9c63688e393c81a00fd5090190fc03a4 to your computer and use it in GitHub Desktop.
Save yknishidate/9c63688e393c81a00fd5090190fc03a4 to your computer and use it in GitHub Desktop.
// create images
std::vector<vk::Image> images = ...;
// gather information
uint32_t memoryTypeBits = ~0; // 0b11111111...
vk::DeviceSize totalSize = 0;
std::vector<vk::DeviceSize> offsets;
for (auto&& image : images) {
offsets.push_back(totalSize);
auto requirements = device->getImageMemoryRequirements(image);
memoryTypeBits &= requirements.memoryTypeBits;
totalSize += requirements.size;
}
// find memory type index
uint32_t memoryTypeIndex = findMemoryTypeIndex(physicalDevice, memoryTypeBits, vk::MemoryPropertyFlagBits::eDeviceLocal);
// allocate memory
vk::MemoryAllocateInfo memoryAllocateInfo;
memoryAllocateInfo.setAllocationSize(totalSize);
memoryAllocateInfo.setMemoryTypeIndex(memoryTypeIndex);
vk::DeviceMemory memory = device->allocateMemory(memoryAllocateInfo);
// bind memory
for (int index = 0; index < images.size(); index++) {
device.bindImageMemory(images[index], memory, offsets[index]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment