alignmentbuffervulkanstaging

Vulkan StagingBuffer Alignment


Lets say that I have different types of data like vertices, indices... and I want to transfer them by using just one staging buffer. Does buffer alignment cause problem, do I have to check whether their alignment requirements match or is it just fine to allocate total size of their memory and mapping them?

For example;

const s3DVertex vertices[4] = {
    {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
    {{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
    {{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
    {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}}
};

const uint16_t indices[6] = { 0, 1, 2, 2, 3, 0 };

void* data;
vkMapMemory(logicalDevice, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
memcpy(data, vertices, (size_t)(sizeof(s3DVertex) * 4));
memcpy(reinterpret_cast<void*>(reinterpret_cast<s3DVertex*>(data) + 4), indices, (size_t)(sizeof(uint16_t) * 6));
vkUnmapMemory(logicalDevice, stagingBufferMemory);

In this example, it works fine but I'm not sure if it works in all conditions?


Solution

  • The various queues you use to perform transfers between device memory may have specific alignment restrictions for such transfer operations. So you need to make sure to check for those on the queue family you're using.

    Beyond that, the memory alignment for the source of a device buffer copy operation doesn't matter. So just make sure that you're following the queue's alignment requirements.