Uniform blocks and shader storage blocks work in very similar ways, so this section will explain the features they have in common. Collectively, these are called "buffer-backed blocks."

+

Uniform blocks and shader storage blocks work in very similar ways, so this section will explain the features they have in common. Collectively, these are called "buffer-backed blocks" because the storage for their contents come from a [[Buffer Object]].

Note that shader storage blocks are a GL 4.3 feature, and thus are not available unless 4.3 or ARB_shader_storage_buffer_objects is defined.

Note that shader storage blocks are a GL 4.3 feature, and thus are not available unless 4.3 or ARB_shader_storage_buffer_objects is defined.

Contents

Syntax

Interface blocks have different semantics in different contexts, but they have the same syntax regardless of how they are used. Uniform blocks are defined as follows:

interface_qualifierblock_name{<definemembershere>}instance_name;

This looks like a struct definition, but it is not.

interface_qualifier​ can be one of in​, out​, uniform​, or buffer​ (the latter requires GL 4.3). This defines what kind of interface block is being created.

block_name​ is the true name for the interface block. When the block is referenced in OpenGL code or otherwise talked about in most contexts, this is the name that is used.

instance_name​ is a GLSL name for one or more instances of the block named block_name​. It is optional; if it is present, then all GLSL variables defined within the block must be scoped with the instance name. For example, this defines a uniform block:

uniformMatrixBlock{mat4projection;mat4modelview;}matrices;

To access the projection​ member of this block, you must use matrices.projection​.

If it were defined as follows:

uniformMatrixBlock{mat4projection;mat4modelview;};

You could simply use projection​ to refer to it. So the interface name acts as a namespace qualifier.

Because these names are defined globally, they can conflict with other names:

This creates 3 separate interface blocks: matrices[0]​, matrices[1]​, and matrices[2]​. These can have separate binding locations (see below), so they can come from different buffer objects.

For uniform and shader storage blocks, the array index must be a dynamically-uniform integral expression. For other kinds of blocks, they can be any arbitrary integral expression.

Note: The interface name is only used by GLSL. OpenGL always uses the actual block name. Thus, when querying information about the block, one would use "MatrixBlock.modelview" as the uniform name to query the offset of. The array subscript is only used when identifying a specific block to query parameters about it. So the second block would be "MatrixBlock[1]".

Linking between shader stages allows multiple shaders to use the same block. Interface blocks match with each other based on the block name and the member field definitions. So the same block in different shader stages can have different instance names.

Input and output

Input and output blocks are designed to compliment each other. Their primary utility is with geometry or tessellation shaders, as these shaders often work with arrays of inputs/outputs.

Data passed between shader stages can be grouped into blocks. If the input is in a block, then the output must also be within a block that uses the same block name and members (but not necessarily the same instance name). For example, a vertex shader can pass data to a geometry shader using these block definitions:

Notice that the geometry shader block is defined as an array in the geometry shader. It also uses a different instance name. These work perfectly fine; the GS will receive a number of vertices based on the primitive type it is designed to take.

Buffer backed

Uniform blocks and shader storage blocks work in very similar ways, so this section will explain the features they have in common. Collectively, these are called "buffer-backed blocks" because the storage for their contents come from a Buffer Object.

Note that shader storage blocks are a GL 4.3 feature, and thus are not available unless 4.3 or ARB_shader_storage_buffer_objects is defined.

Matrix storage order

Because the storage for these blocks comes from Buffer Objects, matrix ordering becomes important. Matrices can be stored in column or row-major ordering. Layout qualifiers are used to decide which is used on a per-variable basis.

Note that this does not change how GLSL works with them. GLSL matrices are always column-major. This specification only changes how GLSL fetches the data from the buffer.

Defaults can be set with this syntax:

layout(row_major) uniform;

From this point on, all matrices in uniform blocks are considered row-major. Shader storage blocks are not affected; they would need their own definition (layout(row_major) buffer;​).

MatrixBlock.projection​ is row-major, but MatrixBlock.modelview​ is column-major. The default is column-major.

Memory layout

The specific size of basic types used by members of buffer-backed blocks is defined by OpenGL. However, implementations are allowed some latitude when assigning padding between members, as well as reasonable freedom to optimize away unused members. How much freedom implementations are allowed for specific blocks can be changed.

There are four memory layout qualifiers: shared​, packed​, std140​, and std420​. Defaults can be set the same as for matrix ordering (eg: layout(packed) buffer;​ sets all shader storage buffer blocks to use packed​). The default is shared​.

The packed​ layout type means that the implementation determines everything about how the fields are laid out in the block. The OpenGL API can be used to query the layout for the members of a particular block. Each member of a block will have a particular byte offset, which you can use to determine how to upload its data. Also, members of a block can be optimized out if they are found by the implementation to not affect the result of the shader. Therefore, the active components of a block may not be all of the components it was defined with.

The last limitation makes it difficult to share buffers between different programs, even with identical block definitions. Therefore, the shared​ type exists. It works essentially the way packed​ does; you have to query offsets for each variable. However, shared​ has two major differences. First, it guarantees that all of the variables defined in the block are considered active. Second, it guarantees that the same buffer can be used between all programs that share this same block definition, so long as:

The different programs use the exact same block definition (ignoring differences in variable names).

All members of the block use explicit sizes for arrays.

Because of these guarantees, buffer-backed blocks declared shared​ can be used with any program that defines a block with the same elements in the same order. This even works across different types of buffer-backed blocks. You can use a buffer as a uniform buffer at one point, then use it as a shader storage buffer in another. OpenGL guarantees that all of the offsets and alignments will match between two shared​ blocks that define the same members in the same order. In short, it allows the user to share buffers between multiple programs.

In both of the previous layout types, the user must query the offset for each variable of interest within the block. If you want to have a layout that is know without having to query this information, you may use std140​ or std430​. The OpenGL Specification goes into great detail as to exactly how each element of these layout types are placed within a buffer. These layouts also has the same properties as shared, in that all variables will be available, and that programs can share variables of this layout with each other (if the above conditions are adhered to).

std140​ layout will generally not use the most optimal packing in terms of size. This is the only real downside to using it.

Note that std430​ layout cannot be used with uniform blocks. It can only be used with storage buffer blocks. It offers greater packing and smaller alignment requirements than std140​.

Block binding

Each uniform/shader storage block has a binding index. This index references one of a number of slots in the OpenGL context that say which buffer objects to use. When a buffer object is bound to the index reference by a block, then that block will get its data from that buffer.

If GL 4.2 or ARB_shading_language_420pack is defined, then the block binding indices can be set directly from the shader:

layout(binding=3)uniformMatrixBlock{mat4projection;mat4modelview;};

Uniform and shader storage blocks have a different set of indices. Uniform block binding indices refer to blocks bound to indices in the GL_UNIFORM_BUFFER indexed target with glBindBufferRange​. Shader storage block binding indices refer to blocks bound to indices in the GL_SHADER_STORAGE_BUFFER target.

For arrays of blocks, the binding syntax sequentially allocates indices. So this definition:

There will be 4 separate blocks, which use the binding indices 2, 3, 4, and 5.

Block bindings can also be set manually from OpenGL. The function to do this depends on whether it is a uniform block or a shader storage block. See their individual sections.

Once a binding is assigned, the storage can be bound to the OpenGL context with glBindBufferRange​ (or glBindBufferBase​ for the whole buffer). Each type of buffer-backed block has its own target. Uniform blocks use GL_UNIFORM_BUFFER, and shader storage blocks use GL_SHADER_STORAGE_BUFFER.

Uniform blocks

Uniform blocks cannot use std430​ layout.

To set the block binding from OpenGL, you must first get the index for that uniform block. To do that, you may use one of these two functions:

The first function is only available in GL version 4.3 or if ARB_program_interface_query is available. The latter is always available. If you use glGetProgramResourceIndex​, the programInterface​​ parameter should be GL_UNIFORM_BLOCK.

If name​ specifies a block that is inactive, or specifies a block that isn't defined in program​, then GL_INVALID_INDEX is returned.

Once the index is retrieved, it can be used to set the buffer binding with this function:

This causes the uniform block specified by uniformBlockIndex​​ in program​​ to use the uniform buffer binding location uniformBlockBinding​​.

There are only GL_MAX_UNIFORM_BUFFER_BINDINGS binding locations available. So uniformBlockBinding​​ must be less than this value.

Shader storage blocks

If the last member variable of a shader storage block is declared with an indeterminate array length (using []​), then the size of this array is determined at the time the shader is executed. The size is basically the rest of the buffer object range that was attached to that binding point.

The number of float​ variables in lotsOfFloats​ depends on the size of the buffer range that is attached to the binding point. matrix​ will be 64 bytes in size, so the number of elements in lotsOfFloats​ will be (size of the bound range minus 64) / 4. Thus, if we bind with this: