When should I use STD140 in OpenGL?

Dan Webster picture Dan Webster · Apr 29, 2013 · Viewed 12.1k times · Source

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?

Answer

Sergey K. picture Sergey K. · Jul 22, 2013

Uniform buffer objects are described in http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt

The data storage for a uniform block can be declared to use one of three layouts in memory: packed, shared, or std140.

  1. packed uniform blocks have an implementation-dependent data layout for efficiency, and unused uniforms may be eliminated by the compiler to save space.

  2. shared uniform blocks, the default layout, have an implementation-dependent data layout for efficiency, but the layout will be uniquely determined by the structure of the block, allowing data storage to be shared across programs.

  3. std140 uniform blocks have a standard cross-platform cross-vendor layout. Unused uniforms will not be eliminated.

The std140 uniform block layout, which guarantees specific packing behavior and does not require the application to query for offsets and strides. In this case, the minimum size may still be queried, even though it is determined in advance based only on the uniform block declaration. The offset of each uniform in a uniform block can be derived from the definition of the uniform block by applying the set of rules described in the OpenGL specification.