Hello,
I’m trying to understand the std140 layout for uniform buffers.
layout(std140) uniform bufferName
{
mat3 foo;
};
Is 48 bytes large according to GL which makes sense as a matrix gets treated like an array, so it should be equivalent to:
layout(std140) uniform bufferName
{
vec3 foo[3];
};
which it is. As the base alignment of a vec3 is 4N, even this buffer is 48 bytes in total:
layout(std140) uniform bufferName
{
vec3 foo0;
vec3 foo1;
vec3 foo2;
};
So far so good. But now I add one float at the end. In the case of the last example the size of the buffer does noch change as the float has a base alignment of N:
layout(std140) uniform bufferName
{
vec3 foo0;
vec3 foo1;
vec3 foo2;
float bar; // does not change the size of the buffer
};
BUT:
layout(std140) uniform bufferName
{
mat3 foo;
float bar; // does change the size of the buffer
};
is 64 bytes (the total size of the buffer seems always to be a multiple of 16 on NVidia).
So I have a question around this observations:
What in the specs (if anything) guarantees that the padding of a matrix at the end will not get used for the next scalar value? Formulated differently: Can I add the full padding to a matrix on the CPU side and upload that amount of memory without risking side effects?