When should I use STD140 in OpenGL?

glsl, opengl, opengl-3, opengl-4

Solution

std140 is most useful when you have a uniform block that you update all at once, for example a collection of matrix and lighting values for rendering a scene. Declare the block with std140 in your shader(s), and you can replicate the memory layout in C with a struct. Instead of having to query and save the offsets for every individual value within the block from C, you can just glBufferData(GL_UNIFORM_BUFFER, sizeof(my_struct), &my_struct, with one call.

You do need to be a little careful with alignment in C, for instance, a vec3 will take up 4 floats, not 3, but it is still much easier IMHO.

Problem

When do I use the STD140 for uniform blocks in OpenGL? Although I am not a 100% sure, I believe there is an alternative to it which can achieve the same thing, called "Shared". Is it just preference for the coder? Or are there reasons to use one over the other?

Original source