memoryvulkantexture2d

Vulkan Texture Array Memory Size Difference


For some reasons, I wanted to divide my texture into a texture array. My textures size is 72x64 and I wanted to divide it 96 array of textures with 6x8 size. They consume same amount pixels but texture array occupies significantly more size of memory. I don't know why. Their memoryRequirements are like

BigTexture -> Size: 8196 Alignment: 1024 MemoryTypeBits: 3

TextureArray -> Size: 49152 Alignment: 1024 MemoryTypeBits: 3

    VkImageCreateInfo fontImageInfo{};
    fontImageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
    fontImageInfo.imageType = VK_IMAGE_TYPE_2D;
    fontImageInfo.format = VK_FORMAT_R8_UNORM;
    fontImageInfo.extent = { 6,  8,  1U };
    fontImageInfo.mipLevels = 1;
    fontImageInfo.arrayLayers = 12 * 8;
    fontImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
    fontImageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
    fontImageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
    fontImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    fontImageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;

Solution

  • Though array layers within an arrayed image are allocated in the same block of memory, each layer needs to function as a separate image, with its own mipmap chain and the like. Furthermore, you can create single-image views of an arrayed image. So the system needs to be able to access the array layers in an arrayed image independently of the rest, typically without even knowing that the image being accessed happens to be part of an array.

    As such, arrayed images are not allocated contiguously in the same way that pixels in an "equivalent" unarrayed image would be allocated. That is, the next layer does not start where the next pixel would have started in a bigger image. Each layer will start with its own alignment. Given the numbers you're getting, there's a good chance that your hardware allocates layers with a 512 byte alignment (49152 / 96 = 512).

    That's fine if each image in the array itself uses all of those 512 bytes. But with your small sub-image size (6x8), that's not possible. So a lot of space is being wasted on alignment requirements.

    Given that the sub-images that you want arrayed are so tiny, it's probably best to just use one large single image instead of using array layers.