Minimum variable size for uniform buffer variables


Is there a minimum size for the variables in a uniform buffer.

I am looking to pass

1:vec2, 2:float, 3: vec2 to the uniform buffer

What is happening is that the vec2.x of the third component is assigned to the “ghost y” of the second float

Everything works as expected when i have a vec2 vec2 the uniform buffer.

Is this a value that needs to be checked e.g. like minUniformBufferOffsetAlignment?


RenderDoc mentions 3 variables, needed 24 bytes. Why does it need 24 bytes for 3 variables?

Any help would be appreciated.

Best regards.
Nihal Kenkre.

I am almost tempted to thank Alphonso in advance :confused:

Because those are the rules of std140 layout. The alignment of a vec2 under these rules is 8 bytes. Therefore, the second vec2 must be at an 8-byte alignment within the layout of the uniform block. So the compiler inserts padding between the end of the float and the start of the second vec2.


1 Like

What i found out from spending all of yesterday on this

If there are 3 floats in the uniform block. All require 1 float. And work fine. 3 variables 12 bytes required (RDoc)

If there are float, vec2, float… Only the third variable is padded up to vec2. 3 variables 20 bytes required (RDoc)

The padding seems occur only up to the data type of the previous variable.

Mentioning one of the posts in the link mentions vec4 padding for all variables

You didn’t need to spend a day on it; you could just look up the layout rules for std140 in the standard. Since it’s a PDF, I can’t link you directly there, but searching for “std140” would get you to them easily enough.

haha sure.

the findings were before i posted the thread. and had already spent a major part of the day on it by

i only got to know that something like std140 even existed only after your first reply.

pardon my ignorance.