Best way to transfer Uniform Buffer Objects to the Shaders?

I have some large uniform buffer objects, that I want to transfer to my (vertex) shader. But I get always problems with the alignment (vec3 takes same space like vec4, …) on the GPU, but not in my host memory

For example I want to submit this:


struct Matrix {
    glm::mat4 MVProjMatrix;
    glm::mat3 NormalMatrix; // takes same space like 3x vec4 (alignas(48) doesn't work)
};

via VkBuffer and VkDeviceMemory to the shader


vkMapMemory(...);
memcpy(dest, &data, dataSize);
vkUnmapMemory(...);

But when I submit this to the Shader, the values are kind of mixed up. Is there a better way to achieve this?

Yes: stop using types that don’t match the the UBO layout requirements. Don’t use vec3 or mat3. Instead use vec4 and mat4x3.