Vertex Shader

Revision as of 18:48, 23 September 2012 by Alfonse (talk | contribs) (See also)

Jump to: navigation, search

Vertex Shaders are shaders that handle vertex processing. They receive a sequence of Vertex Attributes and produce an equal length sequence of vertex data. There must be a 1:1 mapping from input vertices to output vertices.


Vertex shaders are fed Vertex Attribute data, as specified from a vertex array object by a rendering command. A vertex shader receives a single vertex from the vertex stream and generates a single vertex to the output vertex stream.


Input values to the vertex shader provided by issuing a rendering command while an appropriate vertex array object is bound.

Vertex shader input variables are defined as normal for shader stages, using the in​ type qualifier. Vertex Shader inputs cannot be aggregated into Interface Blocks.

Each input variable is assigned one or more vertex attribute indices. These can be explicitly assigned in one of three ways. The methods for assigning these are listed in priority order; the first takes precedence over the later ones.

In-shader specification
The shader defines the attribute index. This is done using the {{{1}}}​ syntax:
layout(location = 2) vec4 a_vec;
This assigns the attribute a_vec​ the index 2.
Pre-link specification
Before linking, the user may tell OpenGL to assign a particular attribute to a partcular index. This is done with the following function:
 void glBindAttribLocation(GLuint program​, GLuint index​, const GLchar *name​);
index​ is the attribute index to assign. name​ is the name of the vertex shader input to assign the given index to.
Note that it is perfectly legal to assign names to indices that are not mentioned in the vertex shader. The linking process will only use the names that are actually mentioned in the vertex shader. Because of that, it is also perfectly legal to assign multiple names to the same index; this is only an error if you attempt to link a program that uses both names.
Automatic assignment
If neither of the prior two methods assign an input to an attribute index, then the index is automatically assigned by OpenGL when the program is linked. The index assigned is completely arbitrary and may be different for different programs that are linked, even if they use the exact same vertex shader code.

Note that like uniforms, vertex inputs can be "active" and non-active. Active inputs are those that the compiler/linker detects are actually in use. The vertex shader and GLSL program linking process can decide that some input are not in use and therefore they are not active.

The number of active input variables can queried with glGetProgram using GL_ACTIVE_ATTRIBUTES. The names of these inputs can be queried with glGetActiveAttrib, and the attribute index can be queried by providing the attribute name to glGetAttribLocation.

Multiple attributes

Vertex shader inputs may be arrays, matrices, or double-precision types (if GL 4.1/ARB_vertex_attrib_64bit is available). These types require that the input variable be assigned to multiple attribute indices.

Matrix inputs take up one attribute index for every column. Array inputs take one attribute index for every .

Double-precision input variables take up two attribute indices, but only if they have a vector length of 3 or 4. So a dvec2​ takes up only 1 attribute, but dvec3​ takes up two.

Double-precision matrices and/or arrays combine these. Matrices/arrays are broken down into individual vectors. So a dmat2x4​ is equivalent to two dvec4​s; thus, it requires 4 attribute indices. A dmat2x4[2]​ array is broken into four dvec4​s, and thus requires 8 attribute indices.

Each input is only ever given a single attribute index. If that input variable needs multiple attribute indices, the rest are assigned sequentially starting at the given index. Consider:

layout(location = 3) mat4 a_matrix;

a_matrix​ will be assigned attribute indices 3, 4, 5, and 6. This works regardless of what methods you use to assign vertex attribute indices to input variables.

Linking will fail if any input names collide when they are assigned a range of attribute indices. Thus, this will fail to link:

layout(location = 0) mat4 a_matrix;
layout(location = 3) vec4 a_vec;

Because a_matrix​ has four columns, it will take up the attribute indices on the range [0, 3]. That overlaps with a_vec​'s attributes, and thus a linking error will occur.

Other inputs

There are a few other input variables to the vertex shader stage. These are built-in inputs.

in int gl_VertexID;
in int gl_InstanceID;

gl_VertexID​ is the index of the vertex currently being processed. When using non-indexed rendering, it is the effective index of the current vertex (the number of vertices processed + the first​ value). For indexed rendering, it is the index used to fetch this vertex from the buffer.

Note: It is not entirely clear from the specification how base vertex applies to this. That is, whether gl_VertexID​ includes the base vertex or not.

gl_InstanceID​ is the index of the current instance when doing some form of instanced rendering. If instancing is not being used, this value is 0.


Output variables from the vertex shader are passed on to the Tessellation Evaluation Shader or Geometry Shader. If neither of those shaders is active, then they are passed on to the next stages in the rendering pipeline.

User-defined output variables can have interpolation qualifiers (though these only matter if the output is not bieng passed to a Tessellation or Geometry shader. Vertex shader outputs can also be aggregated into Interface Blocks.

There are several pre-defined vertex shader outputs, which have specialized semantics. These variables only have these special semantics if the vertex shader is passing its outputs directly to the fixed-function post-vertex processing stages. If there is an active tessellation or geometry shader, these variables can still be used, but they have only whatever meaning those later stages choose to give them.

out gl_PerVertex
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];

gl_Position​ is the clip-space vertex position. gl_PointSize​ is the size in pixels of this point (only useful when rendering GL_POINTS primitives). gl_ClipDistance​ is used for doing triangle clipping against user-defiend clip planes.

See also