in RenderScriptMigrationSample/app/src/main/cpp/VulkanResources.cpp [113:149]
bool Image::createDeviceLocalImage(VkImageUsageFlags usage) {
// Create an image
const VkImageCreateInfo imageCreateInfo = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.extent = {mWidth, mHeight, 1},
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = 0,
.pQueueFamilyIndices = nullptr,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
CALL_VK(vkCreateImage, mContext->device(), &imageCreateInfo, nullptr, mImage.pHandle());
// Allocate device memory
VkMemoryRequirements memoryRequirements;
vkGetImageMemoryRequirements(mContext->device(), mImage.handle(), &memoryRequirements);
const auto memoryTypeIndex = mContext->findMemoryType(memoryRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
RET_CHECK(memoryTypeIndex.has_value());
const VkMemoryAllocateInfo allocateInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.pNext = nullptr,
.allocationSize = memoryRequirements.size,
.memoryTypeIndex = memoryTypeIndex.value(),
};
CALL_VK(vkAllocateMemory, mContext->device(), &allocateInfo, nullptr, mMemory.pHandle());
vkBindImageMemory(mContext->device(), mImage.handle(), mMemory.handle(), 0);
return true;
}