Type Qualifier (GLSL)

From OpenGL.org
Revision as of 18:40, 19 March 2011 by Alfonse (Talk | contribs) (Interpolation qualifiers)

Jump to: navigation, search

The OpenGL Shading Language defines a number of type qualifiers, which are applied to global and locally defined variables. These qualifiers change particular aspects of the variable. They come in a number of different categories.

Storage qualifiers


If no storage qualifier is specified when declaring a global variable, then the variable is just a normal global variable. All global variables have a scope of the particular shader stage. So if you have two vertex shader objects that define int someValue;, when you link them into a program, they will both be referring to the same value.

If no storage qualifier is specified for a local variable, then the variable is a local variable that can be modified.

Constant qualifier

Global or local variables can be declared with the const qualifier. This means that the variable's value cannot be changed after it is initialized. This also means that the variable declaration must initialize the variable. This should be familiar to C++ users or users of C99.

The initializer for a const variable must be a constant expression. Constant expressions are expressions that only contain:

  • Literal values (3, 2.4, etc)
  • A previously defined global or local variable that is qualified as const.
  • A type constructor that is given only constant expressions as arguments.
  • A built-in function call who's arguments are all constant expressions. Exempt from this are the texture functions.
  • A operator who's arguments are constant expressions (5+4).

Shader stage inputs and outputs

Global variables declared with the in qualifier are shader stage input variables. These variables are given values by the previous stage (possibly via interpolation of multiple output values). These variables are not constant (in the sense of const), but they cannot be changed by user code.

Global variables declared with the out qualifier are shader stage output variables. These values are passed to the next stage of the pipeline (possibly via interpolation of several output values). The shader must set all output variables at some point in its execution (unless the fragment shader runs the discard command), except for those outputs that are not read by the next shader.

Neither qualifier can be used on local variables.

These variables can be of any non-sampler basic type. They cannot be of struct types, but they can be arrays. There are usually very strict limits on the number of input and outputs available to a shader stage.

These variables are how the shader communicates to the earlier and later parts of the pipeline. Each variable name and type will match with an equivalent variable name and type on the next or previous shader stage. So, for each vertex shader output, the fragment shader will have a matching input with the same name and type. Using the same name with a different type is a linker error.

Also, it is allowed to have output variables that are not read by the next stage. It is not allowed to have input variables not written by the previous stage.

There are some special cases with input and output variables for different stages.

Vertex shader inputs

Inputs for vertex shaders are attributes. They are passed from vertex arrays to the vertex shader.

Array inputs can be defined. Each array value takes up one attribute index, sequentially from the first. So this input: in float values[5] will take up 5 attributes.

Matrix inputs take up one attribute index for each row.

Geometry shader inputs

Fragment shader outputs

Fragment shader outputs cannot be matrices or booleans; they must be floating-point or integer vectors or scalars.

Each fragment shader output corresponds to a draw buffer, set with the glDrawBuffers command. The association between outptu values and draw buffers is made with glBindFragDataLocation before linking the program.

Interpolation qualifiers

Fragment shader inputs can have interpolation qualifiers. Interpolation qualifiers can also be used on the outputs from the final shader stage before the rasterizer. This traditionally has been the veretx shader, but it could be the geometry shader or the tessellation evaluation shader.

The interpolation qualifiers for corresponding values must match; failure to do so is a linker error.

Interpolation qualifiers control how interpolation happens across a triangle or other primitive. There are three basic interpolation qualifiers.

flat means that there is no interpolation. The value given to the fragment shader is based on the provoking vertex conventions.

noperspective means that there will be linear interpolation in window-space. This is usually not what you want, but it can have its uses.

smooth, the default, means to do perspective-correct interpolation.

The centroid qualifier only matters when multisampling. If this qualifier is not present, then the value is interpolated to the pixel's center, anywhere in the pixel, or to one of the pixel's samples. This sample may lie outside of the actual primitive being rendered, since a primitive can cover only part of a pixel's area. The centroid qualifier is used to prevent this; the interpolation point must fall within both the pixel's area and the primitive's area.

This is useful for parameters or computations that would have undefined values if they fell outside of the primitive's area. A square root is only defined for positive numbers, so if you are taking the square root of an interpolated value, you may need to use centroid interpolation.

Precision qualifiers

Invariance qualifiers

Built-in redeclaration

The GLSL defines a number of [GLSL_Predefined_Variables predefined variables] at the various shader stages. These variables are defined with a particular set of qualifiers, as stated in the above article. If you wish to use them with a different qualifier, you can redeclare them. You must use the same type, however. And some variables cannot be redeclared with a new qualifier; gl_Position in the vertex shader for example cannot use an interpolation qualifier at all.

Deprecated qualifiers

The attribute qualifier is effectively equivalent to an input qualifier in vertex shaders. It cannot be used anywhere else.

The varying qualifier is equivalent to the input of a fragment shader or the output of a vertex shader. It cannot be used anywhere else.