Graphics Reference
In-Depth Information
• Arrays of scalars, vectors, and matrices are stored in memory by
element order, with array member zero placed at the lowest offset.
The offset between each pair of elements in the array is constant and
referred to as the array stride (
GL_UNIFORM_ARRAY_STRIDE
) and can be
queried in a linked program using
glGetActiveUniformsiv
.
Unless you use the
std140
uniform block layout (the default),
you will need to query the program object for the byte offsets and
strides to set uniform data in the uniform buffer object. The
std140
layout guarantees a specific packing behavior with an explicit layout
specification defined by the OpenGL ES 3.0 specification. Thus using
std140
layout allows you to share the uniform block between different
OpenGL ES 3.0 implementations. Other packing formats (see Table 5-4)
may allow some OpenGL ES 3.0 implementations to pack the data more
tightly together than the
std140
layout.
The following is an example of a named uniform block
LightBlock
using
the
std140
layout:
layout (std140) uniform LightBlock
{
vec3 lightDirection;
vec4 lightPosition;
};
The
std140
layout is specified as follows (adapted from the OpenGL
ES 3.0 specification). When the uniform block contains the following
member:
1.
A scalar variable—The base alignment is the size of the scalar. For
example,
sizeof(GLint)
.
2.
A two-component vector—The base alignment is twice the size of the
underlying component type size.
3.
A three-component or four-component vector—The base alignment
is four times the size of the underlying component type size.
4.
An array of scalars or vectors—The base alignment and array stride
are set to match the base alignment of a single element array. The
entire array is padded to a multiple of the size of a
vec4
.
5.
A column-major matrix with C columns and R rows—Stored as an
array of C vectors with R components according to rule 4.
6.
An array of M column-major matrices with C columns and R rows—
Stored as M × C vectors with R components according to rule 4.