Interface Blocks
Shader variables shared with the application or between stages can be, and sometimes must be, organized into blocks of variables. Uniform variables can be organized into uniform blocks, input and output variables into in and out blocks, and shader storage buffers into buffer blocks.
These all have a similar form. First, we use uniform to demonstrate, showing first an anonymous form and second a named form:
uniform b { // 'uniform' or 'in' or 'out' or 'buffer' vec4 v1; // list of variables bool v2; // ... }; // no name; access members as 'v1' and 'v2'
Or:
uniform b { // 'uniform' or 'in' or 'out' or 'buffer' vec4 v1; // list of variables bool v2; // ... } name; // named; access members as 'name.v1' and 'name.v2'
Specific interface block details are provided in the following sections. Generally, the block name at the beginning (b above) is used for interface matching or external identification, while the name at the end (name above) is used in the rest of the shader for accessing the members.
Uniform Blocks
As your shader programs become more complex, it’s likely that the number of uniform variables they use will increase. Often, the same uniform value is used within several shader programs. As uniform locations are generated when a shader is linked (i.e., when glLinkProgram() is called), the indices may change, even though (to you) the values of the uniform variables are identical. Uniform buffer objects provide a method to optimize both accessing uniform variables and enabling sharing of uniform values across shader programs.
As you might imagine, given that uniform variables can exist both in your application and in a shader, you’ll need to both modify your shaders and use OpenGL routines to set up uniform buffer objects.
Specifying Uniform Blocks in Shaders
To access a collection of uniform variables using routines such as glMapBuffer() (see Chapter 3, “Drawing with OpenGL” for more details), you need to slightly modify their declaration in your shader. Instead of declaring each uniform variable individually, you group them, just as you would do in a structure, in a uniform block. A uniform block is specified using the uniform keyword. You then enclose all the variables you want in that block within a pair of braces, as demonstrated in Example 2.3.
Example 2.3 Declaring a Uniform Block
uniform Matrices { mat4 ModelView; mat4 Projection; mat4 Color; };
Recall types are divided into two categories: opaque and transparent. The opaque types include samplers, images, and atomic counters. Only the transparent types are permitted to be within a uniform block. Additionally, uniform blocks must be declared at global scope.
Uniform Block Layout Control
A variety of qualifiers are available to specify how to lay out the variables within a uniform block. These qualifiers can be used for each individual uniform block or to specify how all subsequent uniform blocks are arranged (after specifying a layout declaration). The possible qualifiers are detailed in Table 2.12.
Table 2.12 Layout Qualifiers for Uniform
Layout Qualifier |
Description |
---|---|
binding = N |
Specify the buffer′s binding point, used by the OpenGL API. |
shared |
Specify that the uniform block is shared among multiple programs. (This is the default layout and is not to be confused with the shared storage qualifier.) |
packed |
Lay out the uniform block to minimize its memory use; however, this prevents sharing across programs. |
std140 |
Use a standard layout for uniform blocks or shader storage buffer blocks, described in Appendix H, “Buffer Object Layouts.” |
std430 |
Use a standard layout for buffer blocks, described in Appendix H, “Buffer Object Layouts.„ |
offset = N |
Explicitly force a member to be located at byte offset N in the buffer. |
align = N |
Explicitly force a member offset to round up to a multiple of N. |
row_major |
Cause matrices in the uniform block to be stored in a row-major element ordering. |
column_major |
Specify matrices should be stored in a column-major element ordering. (This is the default ordering.) |
For example, to specify that a single uniform block is shared and has row-major matrix storage, declare it in the following manner:
layout (shared, row_major) uniform { ... };
Multiple qualifying options must be separated by commas within the parentheses. To affect the layout of all subsequent uniform blocks, use the following construct:
layout (packed, column_major) uniform;
With this specification, all uniform blocks declared after that line will use that layout until the global layout is changed or unless they include a layout override specific to their declaration.
When you share a buffer between shaders and the application, both need to agree on what memory offsets are holding the members. Thus, an explicit layout is needed, and this is what std140 and std430 provide.
While std140 and std430 give a well-defined explicit layout of a buffer, you might want finer control over how the buffer is laid out. You can control exact locations of members using offset or align members at a coarser level using align. You only need to use these on some members, to keep layout in sync between the application and shader.
Subsequently unqualified members are automatically assigned offsets, as is standard for std140 or std430.
#version 440 layout (std140) uniform b { float size; // starts at byte 0, by default layout(offset=32) vec4 color; // starts at byte 32 layout(align=1024) vec4 a[12]; // starts at the next multiple // of 1024 vec4 b[12]; // assigned next offset after a[12] } buf;
In your application, set up the buffer’s structure to match, using language tools decorating a C/C++ struct or just directly writing to the buffer at the right offsets. The only catch is the offsets and alignments all have to be sensible. The members still go in order of increasing offsets and still must be aligned as required by the std140 and std430 rules. Generally, this is natural alignment of floats and doubles, for anything containing them, with std140 having the extra constraint of needing 16-byte alignment for things smaller than a vec4.
Note on N: Any time a GLSL layout qualifier has the form layout (ID = N), the value N must be a non-negative integer. Under #version is 430 or earlier, it must be a literal integer. However, starting with #version 440, N can be a constant integer expression.
Accessing Uniform Variables Declared in a Uniform Block
While uniform blocks are named, the uniform variables declared within them are not qualified by that name. That is, a uniform block doesn’t scope a uniform variable’s name, so declaring two variables of the same name within two uniform blocks of different names will cause an error. Using the block name is not necessary when accessing a uniform variable, however.
Accessing Uniform Blocks from Your Application
Because uniform variables form a bridge to share data between shaders and your application, you need to find the offsets of the various uniform variables inside the named uniform blocks in your shaders. Once you know the location of those variables, you can initialize them with data, just as you would any type of buffer object (using calls such as glNamedBufferSubData(), for example).
To start, let’s assume that you already know the names of the uniform blocks used inside the shaders in your application. The first step in initializing the uniform variables in your uniform block is to obtain the index of the block for a given program. Calling
glGetUniformBlockIndex() returns an essential piece of information required to complete the mapping of uniform variables into your application’s address space.
To initialize a buffer object to be associated with your uniform block, you’ll need to bind a buffer object to a GL_UNIFORM_BUFFER target using the glBindBuffer() routine. (Chapter 3, “Drawing with OpenGL,” will add more details.)
Once we have a buffer object initialized, we need to determine how large to make it to accommodate the variables in the named uniform block from our shader. To do so, we use the routine glGetActiveUniformBlockiv(), requesting the GL_UNIFORM_BLOCK_DATA_SIZE, which returns the size of the block as generated by the compiler. (The compiler may decide to eliminate uniform variables that aren’t used in the shader, depending on which uniform block layout you’ve selected.) glGetActiveUniformBlockiv() can be used to obtain other parameters associated with a named uniform block.
After obtaining the index of the uniform block, we need to associate a buffer object with that block. The most common method for doing so is to call either glBindBufferRange() or, if all the buffer storage is used for the uniform block, glBindBufferBase().
Once the association between a named uniform block and a buffer object is made, you can initialize or change values in that block by using any of the commands that affect a buffer’s values.
You may also want to specify the binding for a particular named uniform block to a buffer object, as compared to the process of allowing the linker to assign a block binding and then querying the value of that assignment after the fact. You might follow this approach if you have numerous shader programs that will share a uniform block. It avoids having the block be assigned a different index for each program. To explicitly control a uniform block’s binding, call glUniformBlockBinding() before calling glLinkProgram().
The layout of uniform variables in a named uniform block is controlled by the layout qualifier specified when the block was compiled and linked. If you used the default layout specification, you will need to determine the offset and date-store size of each variable in the uniform block. To do so, you will use a pair of calls: glGetUniformIndices(), to retrieve the index of a particular named uniform variable, and glGetActiveUniformsiv(), to get the offset and size for that particular index, as demonstrated in Example 2.4.
Example 2.4 Initializing Uniform Variables in a Named Uniform Block
// Vertex and fragment shaders that share a block of uniforms // named "Uniforms" const char* vShader = { "#version 330 core\n" "uniform Uniforms {" " vec3 translation;" " float scale;" " vec4 rotation;" " bool enabled;" "};" "in vec2 vPos;" "in vec3 vColor;" "out vec4 fColor;" "void main()" "{" " vec3 pos = vec3(vPos, 0.0);" " float angle = radians(rotation[0]);" " vec3 axis = normalize(rotation.yzw);" " mat3 I = mat3(1.0);" " mat3 S = mat3( 0, -axis.z, axis.y, " " axis.z, 0, -axis.x, " " -axis.y, axis.x, 0);" " mat3 uuT = outerProduct(axis, axis);" " mat3 rot = uuT + cos(angle)*(I - uuT) + sin(angle)*S;" " pos *= scale;" " pos *= rot;" " pos += translation;" " fColor = vec4(scale, scale, scale, 1);" " gl_Position = vec4(pos, 1);" "}" }; const char* fShader = { "#version 330 core\n" "uniform Uniforms {" " vec3 translation;" " float scale;" " vec4 rotation;" " bool enabled;" "};" "in vec4 fColor;" "out vec4 color;" "void main()" "{" " color = fColor;" "}" }; // Helper function to convert GLSL types to storage sizes size_t TypeSize(GLenum type) { size_t size; #define CASE(Enum, Count, Type) case Enum: size = Count * sizeof(Type); break switch (type) { CASE(GL_FLOAT, 1, GLfloat); CASE(GL_FLOAT_VEC2, 2, GLfloat); CASE(GL_FLOAT_VEC3, 3, GLfloat); CASE(GL_FLOAT_VEC4, 4, GLfloat); CASE(GL_INT, 1, GLint); CASE(GL_INT_VEC2, 2, GLint); CASE(GL_INT_VEC3, 3, GLint); CASE(GL_INT_VEC4, 4, GLint); CASE(GL_UNSIGNED_INT, 1, GLuint); CASE(GL_UNSIGNED_INT_VEC2, 2, GLuint); CASE(GL_UNSIGNED_INT_VEC3, 3, GLuint); CASE(GL_UNSIGNED_INT_VEC4, 4, GLuint); CASE(GL_BOOL, 1, GLboolean); CASE(GL_BOOL_VEC2, 2, GLboolean); CASE(GL_BOOL_VEC3, 3, GLboolean); CASE(GL_BOOL_VEC4, 4, GLboolean); CASE(GL_FLOAT_MAT2, 4, GLfloat); CASE(GL_FLOAT_MAT2x3, 6, GLfloat); CASE(GL_FLOAT_MAT2x4, 8, GLfloat); CASE(GL_FLOAT_MAT3, 9, GLfloat); CASE(GL_FLOAT_MAT3x2, 6, GLfloat); CASE(GL_FLOAT_MAT3x4, 12, GLfloat); CASE(GL_FLOAT_MAT4, 16, GLfloat); CASE(GL_FLOAT_MAT4x2, 8, GLfloat); CASE(GL_FLOAT_MAT4x3, 12, GLfloat); #undef CASE default: fprintf(stderr, "Unknown type: 0x%x\n", type); exit(EXIT_FAILURE); break; } return size; } void init() { GLuint program; glClearColor(1, 0, 0, 1); ShaderInfo shaders[] = { { GL_VERTEX_SHADER, vShader }, { GL_FRAGMENT_SHADER, fShader }, { GL_NONE, NULL } }; program = LoadShaders(shaders); glUseProgram(program); /* Initialize uniform values in uniform block "Uniforms" */ GLuint uboIndex; GLint uboSize; GLuint ubo; GLvoid *buffer; // Find the uniform buffer index for "Uniforms", and // determine the block's sizes uboIndex = glGetUniformBlockIndex(program, "Uniforms"); glGetActiveUniformBlockiv(program, uboIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize); buffer = malloc(uboSize); if (buffer == NULL) { fprintf(stderr, "Unable to allocate buffer\n"); exit(EXIT_FAILURE); } else { enum { Translation, Scale, Rotation, Enabled, NumUniforms }; /* Values to be stored in the buffer object */ GLfloat scale = 0.5; GLfloat translation[] = { 0.1, 0.1, 0.0 }; GLfloat rotation[] = { 90, 0.0, 0.0, 1.0 }; GLboolean enabled = GL_TRUE; /* Since we know the names of the uniforms ** in our block, make an array of those values */ const char* names[NumUniforms] = { "translation", "scale", "rotation", "enabled" }; /* Query the necessary attributes to determine ** where in the buffer we should write ** the values */ GLuint indices[NumUniforms]; GLint size[NumUniforms]; GLint offset[NumUniforms]; GLint type[NumUniforms]; glGetUniformIndices(program, NumUniforms, names, indices); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_OFFSET, offset); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_SIZE, size); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_TYPE, type); /* Copy the uniform values into the buffer */ memcpy(buffer + offset[Scale], &scale, size[Scale] * TypeSize(type[Scale])); memcpy(buffer + offset[Translation], &translation, size[Translation] * TypeSize(type[Translation])); memcpy(buffer + offset[Rotation], &rotation, size[Rotation] * TypeSize(type[Rotation])); memcpy(buffer + offset[Enabled], &enabled, size[Enabled] * TypeSize(type[Enabled])); /* Create the uniform buffer object, initialize ** its storage, and associated it with the shader ** program */ glGenBuffers(1, &ubo); glBindBuffer(GL_UNIFORM_BUFFER, ubo); glBufferData(GL_UNIFORM_BUFFER, uboSize, buffer, GL_STATIC_RAW); glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo); } ... }
Buffer Blocks
GLSL buffer blocks or, from the application’s perspective, shader storage buffer objects, operate quite similarly to uniform blocks. Two critical differences give these blocks great power, however. First, the shader can write to them, modifying their content as seen from other shader invocations or the application. Second, their size can be established just before rendering rather than at compile or link time. For example:
buffer BufferObject { // create a read-writable buffer int mode; // preamble members vec4 points[]; // last member can be unsized array };
If this array is not provided a size in the shader, its size can be established by the application before rendering, after compiling and linking. The shader can use the length() method to find the render-time size.
The shader may now both read and write the members of the buffer block. Writes modifying the shader storage buffer object will be visible to other shader invocations. This can be particularly valuable in a compute shader, especially when manipulating nongraphical memory rather than an image.
Memory qualifiers (e.g., coherent) and atomic operations apply to buffer blocks and are discussed in depth in Chapter 11, “Memory.”
You set up a shader storage buffer object similarly to how a uniform buffer was set up, except that glBindBuffer(), glBindBufferRange() and glBindBufferBase() take the target GL_SHADER_STORAGE_BUFFER. A more complete example is given in Chapter 11, “Memory,” in “Shader Storage Buffer Objects” on page 589.
If you don’t need to write to a buffer, use a uniform block, as your device might not have as many resources available for buffer blocks as it does for uniform blocks. Also, keep in mind that only buffer blocks can use the std430 layout, while uniform blocks can use either std140 or std430.
In/Out Blocks, Locations, and Components
Shader variables output from one stage and input into the next stage can also be organized into interface blocks. These logical groupings can make it easier to visually verify interface matches between stages, as well as to make linking separate programs together easier.
For example, a vertex shader might output
out Lighting { vec3 normal; vec2 bumpCoord; };
This would match a fragment shader input:
in Lighting { vec3 normal; vec2 bumpCoord; };
A vertex shader might output material and lighting information, each grouped into its own block.
Throughout this book, layout (location=N) is used on individual input or output variables. As of OpenGL Version 4.4, this can also be applied to members of input and output blocks, to explicitly assign a location:
#version 440 in Lighting { layout(location=1) vec3 normal; layout(location=2) vec2 bumpCoord; };
Whether in a block or not, each such location can hold the equivalent of a vec4. If you want to put multiple smaller objects into the same location, that can be done by further specifying a component:
#version 440 in Lighting { layout(location=1, component=0) vec2 offset; layout(location=1, component=2) vec2 bumpCoord; };
This is much better than trying to declare a vec4 combined and using combined.xy and combined.zw to simulate offset and bumpCoord. It can also be done outside of blocks.
The interfaces built into the OpenGL Shading Language are also organized into blocks, like gl_PerVertex, which contains the built-in variable gl_Position, among others. A complete list of these is available in Appendix C, “Built-in GLSL Variables and Functions.”