GLSL Predefined Variables By Version

From OpenGL.org
Jump to: navigation, search

The OpenGL Shading Language defines a number of special variables for the various shader stages. These predefined variables (or built-in variables) have special properties. They are usually for communicating with certain fixed-functionality. By convention, all predefined variables start with "gl_"; no user-defined variables may start with this.

This page lists the variables identified by which version of OpenGL they correspond to.

Vertex shader inputs

Vertex shaders have the following predefined inputs.

/* no inputs prior to GLSL 1.30 */
 
in int gl_VertexID; /* GLSL ≥ 1.30 */
in int gl_InstanceID; /* GLSL ≥ 1.40 */

The gl_VertexID is the index of the current vertex being processed. For array rendering, this value is the index of the current vertex. For indexed rendering, this is the index fetched from the element buffer for this vertex.

The gl_InstanceID specifies which instance is being rendered, when using instanced rendering.

Vertex shader attributes

attribute vec4 gl_Color; /* GLSL < 1.40 */
attribute vec4 gl_SecondaryColor; /* GLSL < 1.40 */
attribute vec3 gl_Normal; /* GLSL < 1.40 */
attribute vec4 gl_Vertex; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord0; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord1; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord2; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord3; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord4; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord5; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord6; /* GLSL < 1.40 */
attribute vec4 gl_MultiTexCoord7; /* GLSL < 1.40 */
attribute float gl_FogCoord; /* GLSL < 1.40 */

These are all deprecated in GLSL 1.30, and removed in GLSL 1.40 and later.

Vertex shader outputs

Vertex shaders have the following predefined outputs.

out vec4 gl_Position; /* GLSL < 1.50 */
out float gl_PointSize; /* GLSL < 1.50 */
out vec4 gl_ClipVertex; /* GLSL < 1.40 */
out float gl_ClipDistance[]; /* GLSL ≥ 1.30 < 1.50 */
 
out gl_PerVertex /* GLSL ≥ 1.50 */
{
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
}

gl_PerVertex defines an interface block for outputs. The block is defined so that prefixing the names is not required.

gl_Position is the output position of the current vertex (in clip-space, if there is no geometry shader). It is not necessary to write to this value in a vertex shader, though if you do not, don't be surprised if the primitives you get are not reasonable. You can reasonably omit writing to this variable if you are using transform feedback and shutting off rasterization, or if you are using a geometry shader that will use user-defined outputs to generate positions.

gl_PointSize is the pixel width/height of the point being rasterized. It is only necessary to write to this when rendering points.

gl_ClipDistance allows the shader to set the distance from a vertex to each clip plane. A positive distance means that the vertex is inside/behind the clip plane, and a negative distance means it is outside/in front of the clip plane.

In order to use this variable, the user must manually redeclare it with a size.

Geometry shader inputs

/* no geometry shaders prior to GLSL 1.50 */
 
in gl_PerVertex /* GLSL ≥ 1.50 */
{
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
} gl_in[];
 
in int gl_PrimitiveIDIn; /* GLSL ≥ 1.50 */

The gl_PerVertex variables contain the values passed from the previous vertex shader stages. Note that in the geometry shader, they must be prefixed by gl_in, which is an interface block array. The size of this array is based on the layout qualifier used for inputs for this geometry shader. You do not have to redeclare this block with a specific size; the system does this for you. As with any array, you can get its length with the length() function.

gl_PrimitiveIDIn is the number of the current primitive being worked on during this rendering call. That is, the number of primitives previously processed by this glDraw* call.

GLSL 4.00 and above also have the following definitions:

in int gl_InvocationID; /* GLSL ≥ 4.00 */

Geometry shaders can be invoked multiple times; this value specifies the current invocation.

Geometry shader outputs

/* no geometry shaders prior to GLSL 1.50 */
 
out gl_PerVertex /* GLSL ≥ 1.50 */
{
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
};
 
out int gl_PrimitiveID; /* GLSL ≥ 1.50 */
out int gl_Layer; /* GLSL ≥ 1.50 */

The gl_PerVertex has the same meaning as from the vertex shader stage.

gl_PrimitiveID is a user-defined identifier for the primitive. It is passed directly to the fragment shader (see below). If the fragment shader uses its corresponding input, the value is undefined if the geometry shader does not write to it (OpenGL will automatically fill it in if there is no geometry shader).

The geometry shader should write to this value for the provoking vertex of the output primitive.

gl_Layer is used for rendering to layered framebuffer objects. It specifies the layer that the particular primitive is rendered to.

The gl_Layer variable is assigned to each vertex. If different vertices of a primitive get a different layer value, then OpenGL states that which layer is rendered to is undefined. Therefore, you should set it for each vertex emitted, and the value for each vertex of the same primitive should be the same.

Note that all of the output variables are cleared when EmitVertex() is called from the geometry shader.

GLSL 4.10 defines the following extra outputs:

out int gl_ViewportIndex; /* GLSL ≥ 4.10 */

The gl_ViewportIndex represents the viewport transform and scissor tests that this primitive will be used against. You should use this variable similarly to gl_Layer: always write to it and always write the same value to it for each primitive.

Tessellation control shader inputs

/* no tessellation control shaders prior to GLSL 4.00 */
 
in gl_PerVertex { /* GLSL ≥ 4.00 */
    vec4 gl_Position;
    float gl_PointSize;
    float gl_ClipDistance[];
} gl_in[gl_MaxPatchVertices];
in int gl_PatchVerticesIn; /* GLSL ≥ 4.00 */
in int gl_PrimitiveID; /* GLSL ≥ 4.00 */
in int gl_InvocationID; /* GLSL ≥ 4.00 */

Tessellation control shader outputs

/* no tessellation control shaders prior to GLSL 4.00 */
 
out gl_PerVertex { /* GLSL ≥ 4.00 */
    vec4 gl_Position;
    float gl_PointSize;
    float gl_ClipDistance[];
} gl_out[];
patch out float gl_TessLevelOuter[4]; /* GLSL ≥ 4.00 */
patch out float gl_TessLevelInner[2]; /* GLSL ≥ 4.00 */

Tessellation evaluation shader inputs

/* no tessellation evaluation shaders prior to GLSL 4.00 */
 
in gl_PerVertex { /* GLSL ≥ 4.00 */
    vec4 gl_Position;
    float gl_PointSize;
    float gl_ClipDistance[];
} gl_in[gl_MaxPatchVertices];
in int gl_PatchVerticesIn; /* GLSL ≥ 4.00 */
in int gl_PrimitiveID; /* GLSL ≥ 4.00 */
in vec3 gl_TessCoord; /* GLSL ≥ 4.00 */
patch in float gl_TessLevelOuter[4]; /* GLSL ≥ 4.00 */
patch in float gl_TessLevelInner[2]; /* GLSL ≥ 4.00 */

Tessellation evaluation shader outputs

/* no tessellation evaluation shaders prior to GLSL 4.00 */
 
out gl_PerVertex { /* GLSL ≥ 4.00 */
    vec4 gl_Position;
    float gl_PointSize;
    float gl_ClipDistance[];
};

Fragment shader inputs

in vec4 gl_FragCoord; /* GLSL all versions */
in bool gl_FrontFacing; /* GLSL all versions */
in float gl_ClipDistance[]; /* GLSL all versions */
in vec2 gl_PointCoord; /* GLSL ≥ 1.50 */
in int gl_PrimitiveID; /* GLSL ≥ 1.50 */

gl_FragCoord contains the window-space position of the current sample that this fragment represents. The Z component is the value that will be written to the depth buffer if the user does not override this (see below). The W component is special; it is 1/Wclip. That is, it is 1 divided by the W component of gl_Position output from the vertex or geometry shader.

gl_FrontFacing is true if the primitive is seen from the front, and false if it is the back.

gl_ClipDistance contains the values output from the vertex shader, linearly interpolated across the primitive. As before, it must be sized explicitly.

gl_PointCoord is the location within the area of a point that specifies this fragment's location. This is a normalized value, on the range [0, 1]. The (0,0) origin depends on the point coordinate origin set by OpenGL; the default is the lower-left corner.

gl_PrimitiveID is the value output by the geometry shader, or by OpenGL if no geometry shader was used. It represents the index of the primitive that is being rasterized.

GLSL 4.00 adds the following inputs:

in int gl_SampleID; /* GLSL ≥ 4.00 */
in vec2 gl_SamplePosition; /* GLSL ≥ 4.00 */
in int gl_SampleMaskIn[]; /* GLSL ≥ 4.00 */

gl_SampleID is the ID for the current sample being rasterized within the area of the pixel.

gl_SamplePosition is the location of the current sample within the pixel area being rendered. These values are on the range [0, 1].

Note: Using the gl_SamplePosition variable in any way will cause the fragment shader to be evaluated per-sample. Since the whole point of multisampling is to avoid that, it's probably not a good idea to do so unless you really need it.

gl_SampleMaskIn represents the sample coverage mask for the currently rasterized fragment. The user must redeclare the size of the sample mask to the implementation-dependent maximum number of samples, divided by 32, rounded up (to get the number of 32-bit integers).

GLSL 4.30 adds these inputs:

in int gl_Layer; /* GLSL ≥ 4.30 */
in int gl_ViewportIndex; /* GLSL ≥ 4.30 */

Both of these have whatever values are fed in from the geometry shader (above).

Fragment shader uniforms

The fragment shader defines some uniform built-in values for the sake of convenience:

struct gl_DepthRangeParameters /* GLSL all versions */
{
    float near;
    float far;
    float diff;
};
uniform gl_DepthRangeParameters gl_DepthRange; /* GLSL all versions */

This struct provides access to the glDepthRange near and far values. The diff value is the far value minus the near value.

Fragment shader outputs

out vec4 gl_FragColor; /* GLSL < 1.3 */
out vec4 gl_FragData[gl_MaxDrawBuffers]; /* GLSL < 1.3 */

The gl_FragColor value is what will be written to the colour buffer, representing (this contribution to) the output pixel. Alternatively, gl_FragData is a whole output array of values to be fed to the rest of the pipeline; the fragment shader writes to either gl_FragColor or glFragData, but not both.

Note: these are deprecated as of GLSL 1.3. You are supposed to define your own out variables instead.

out float gl_FragDepth; /* GLSL all versions */

The gl_FragDepth value is the value that will be written to the depth buffer. It will also be used for the depth test. The fragment shader does not have to write to this variable; it will automatically be filled with with gl_FragCoord.z. However, if the fragment shader writes to this variable anywhere, then it must ensure that the value is always written to, no matter what path is taken through the shader (unless discard is used in the other paths).

GLSL 4.00 adds the following output:

out int gl_SampleMask[]; /* GLSL ≥ 4.00 */

gl_SampleMask is a bitmask that represents the samples, when using multisample rendering, that will be written to. The user must redeclare the size of the sample mask to the implementation-dependent maximum number of samples, divided by 32, rounded up (to get the number of 32-bit integers).

The mask bits will be logically AND'd with the coverage mask computed normally.