GLSL Variables

OPENGL ES 2.0 SHADING LANGUAGE VARIABLES & CONSTANTS

This is a reference for the special built-in variables and constants of the OpenGL ES Shading Language that is described in the OpenGL ES Shading Language specification. For more information please visit: www.opengl.org/registry.


SPECIAL OUTPUT VARIABLES OF THE VERTEX SHADER


Vertex position

highp vec4 gl_Position;

The built-in variable gl_Position is used by the vertex shader to hand over the transformed vertex position to the OpenGL ES 2.0 pipeline. The variable is declared as shown above and can be used in the vertex shader for an assignment without prior declaration.

The values of the position vector are interpreted in clip coordinate space. The vertex shader is responsible to execute all vertex manipulations, e.g. the transformation from object coordinates to clip coordinates.

The assignment of values to this variable is mandatory for the vertex shader.


Point size

mediump float gl_PointSize;

The built-in variable gl_PointSize is used by the vertex shader to hand over the point size of a vertex to the OpenGL ES 2.0 pipeline. The variable is declared as shown above and can be used in the vertex shader for an assignment without prior declaration.

The value of the the point size is interpreted as the radius in pixels of the point sprite. The actual drawing geometry is a quad (a rectangular primitive) that is derived from the position and the radius of the point sprite.

Side note: The value assigned to this variable is only taken into account by the OpenGL ES 2.0 pipeline if the rendered primitives are points.


SPECIAL INPUT VARIABLES OF THE FRAGMENT SHADER


Fragment coordinates

mediump vec4 gl_FragCoord;

The built-in variable gl_FragCoord is used by the OpenGL ES 2.0 pipeline to hand over the coordinates of the fragment to the fragment shader. The variable is read-only and the value is assigned by the OpenGL ES 2.0 pipeline.

The values of the fragment coordinate vector are given in the window coordinate system.


Fragment orientation

bool gl_FrontFacing;

The built-in variable gl_FrontFacing is used by the OpenGL ES 2.0 pipeline to hand over the information to the fragment shader if the fragment is part of a front-facing primitive (triangle). The variable is read-only and the value is assigned by the OpenGL ES 2.0 pipeline.

The front-facing variable has a boolean value.


Point coordinates

mediump int gl_PointCoord;

The built-in variable gl_PointCoord is used by the OpenGL ES 2.0 pipeline to hand over the coordinates of a point sprite to the fragment shader. The variable is read-only and the value is calculated and assigned by the OpenGL ES 2.0 pipeline based on the position and radius of the point sprite..

Side note: A value for this variable is provided by the OpenGL ES 2.0 pipeline only if the rendered primitives are points.


SPECIAL OUTPUT VARIABLES OF THE FRAGMENT SHADER


Fragment color

mediump vec4 gl_FragColor;

The built-in variable gl_FragColor is used by the fragment shader to hand over the color of the fragment to the OpenGL ES 2.0 pipeline. The variable is pre-declared as shown above that way the variable can be used in the fragment shader for an assignment without prior declaration.

The values of the color vector are interpreted in the RGBA color space.

The assignment of values to this variable is mandatory for the fragment shader.


BUILT-IN CONSTANTS (VERTEX-SHADER)

The following constants can be read by the vertex shader to determine the limitations of the OpenGL ES 2.0 environment at runtime.


Number of vertex attributes

const mediump int gl_MaxVertexAttribs >= 8

The built-in constant gl_MaxVertexAttribs provides the maximum number of attributes that can be used by the vertex shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 8.


Number of vertex uniform vectors

const mediump int gl_MaxVertexUniformVectors >= 128

The built-in constant gl_MaxVertexUniformVectors provides the maximum number of uniform vectors that can be used by the vertex shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 128.

Number of varying vectors

const mediump int gl_MaxVaryingVectors >= 8

The built-in constant gl_MaxVaryingVectors provides the maximum number of varying vectors that can be used by the vertex shader to hand over data to the fragment shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 8.

Number of texture units

const mediump int gl_MaxVertexTextureImageUnits >= 0

The built-in constant gl_MaxVertexTextureImageUnits provides the maximum number of texture units that can be used by the vertex shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 0.

Side note: This number is actually 0 on all iOS devices. This is the reason why there is no access to textures data in the vertex shader.

Number of combined texture units

const mediump int gl_MaxCombinedTextureImageUnits >= 8

The built-in constant gl_MaxCombinedTextureImageUnits provides the maximum number of texture units that are available in the vertex shader or the fragment shader respectivley. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 8.


BUILT-IN CONSTANTS (FRAGMENT-SHADER)

The following constants can be read by the fragment shader to determine the limitations of the OpenGL ES 2.0 environment at runtime.


Number of texture units

const mediump int gl_MaxTextureImageUnits >= 8

The built-in constant gl_MaxTextureImageUnits provides the maximum number of texture units that can be used by the fragment shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 8.


Number of fragment uniform vectors

const mediump int gl_MaxFragmentUniformVectors >= 16

The built-in constant gl_MaxFragmentUniformVectors provides the maximum number of uniform vectors that can be used by the fragment shader. The value of this variable is dependent on the OpenGL ES 2.0 implementation but has to be at least 16.


Number of draw buffers

const mediump int gl_MaxDrawBuffers = 1

The built-in constant gl_MaxDrawBuffers provides the maximum number of the available draw buffers. The value of this variable is 1 for all OpenGL ES 2.0 implementations. This might change in future versions.