Reputation: 365
I have been reading about std140 Uniform block in OpenGL Superbible 6th edition, where in page 111 the following example is given :
layout(std140) uniform TransformBlock
{
//Member base alignment offset aligned offset
float scale ; // 4 0 0
vec3 translation; // 16 4 16
float rotation[3]; // 16 28 32 (rotation[0])
// 48 (rotation[1])
// 64 (rotation[2])
mat4 projection_matrix; // 16 80 80 (column 0)
// 96 (column 1)
// 112 (column 2)
// 128 (column 3)
}
I am having hard time understanding the alignments, even using the rules that have described earlier in that page, it still don't make sense.To be specific thing that confuses me is :
What is the Offset and what is aligned offset, why 2 different offset?
Why is the rotation[0] offset starts at 28, and why aligned offset is 32?
Hopefully someone can clear my confusions.I would highly appreciate. Thanks
Upvotes: 1
Views: 623
Reputation: 474376
What is the Offset
The byte offset that the variable would have had if not for its alignment requirements.
and what is aligned offset,
The byte offset it actually has.
why 2 different offset?
Because if the variable had different alignment requirements, then the actual offset would be different. For example, given the above code, translation
has the aligned offset of 16 because of its alignment requirements. However, if translation
were a float
, then it would have an aligned offset of 4. If it were a vec2
, it would have an aligned offset of 8.
Why is the rotation[0] offset starts at 28,
Because a vec3
(which is something you should never use) is 3 floats.
and why aligned offset is 32?
Because under std140 rules, the base alignment of any array, regardless of the type of its elements, is always rounded up to the alignment of a vec4
. AKA: 16 bytes.
Upvotes: 2