Uniform Buffer Objects and std140 question

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?

Your problem is very simple: 3 vec3s are not the same as a vec3[3] array.

From the GL 3.3 specification:

Emphasis added.

Hello Alfonse,

thanks for your reply. I was aware of the fact that three floats are not handled the same way as an array of three floats. What I didn’t know was whether the padding at the end of each element was ‘taboo’ for other elements in both cases. The ‘may’ in ‘The array may have padding at the end’ wasn’t clear to me. But the ‘the base offset of the member following the array is rounded up to the next multiple of the base alignment’ makes it clear, thanks for pointing that out, I must have overlooked it.

Thanks.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.