The ''gl_PerVertex'' variables contain the values passed from the previous 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.

The ''gl_PerVertex'' variables contain the values passed from the previous 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.

+

''gl_PrimitiveIDIn'' is the number of the current primitive being worked on during this rendering call. It is the number of primitives previously processed by the current {{code|glDraw*}} rendering call.

GLSL 4.00 and above also have the following definitions:

GLSL 4.00 and above also have the following definitions:

Line 156:

Line 156:

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.

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.

+

Note that the value of all output variables are undefined when {{code|EmitVertex()}} is called from the geometry shader.

GLSL 4.10 defines the following extra outputs:

GLSL 4.10 defines the following extra outputs:

Line 233:

Line 233:

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).

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).

+

+

{{perf note|If ''gl_FragDepth'' is not updated manually, the driver can [[Early Depth Test|perform the whole depth test before executing the fragment shader]]. This will usually enhance performance.}}

GLSL 4.00 adds the following output:

GLSL 4.00 adds the following output:

Line 246:

Line 248:

== Compute shader inputs ==

== Compute shader inputs ==

<source lang="glsl">

<source lang="glsl">

−

in uvec3 gl_NumWorkGroups; /* GLSL ≥ 4.30 */

+

// Compute shaders require GL 4.3 or ARB_compute_shader

−

in uvec3 gl_WorkGroupID; /* GLSL ≥ 4.30 */

+

in uvec3 gl_NumWorkGroups;

−

in uvec3 gl_LocalInvocationID; /* GLSL ≥ 4.30 */

+

in uvec3 gl_WorkGroupID;

−

in uvec3 gl_GlobalInvocationID; /* GLSL ≥ 4.30 */

+

in uvec3 gl_LocalInvocationID;

−

in uint gl_LocalInvocationIndex; /* GLSL ≥ 4.30 */

+

in uvec3 gl_GlobalInvocationID;

+

in uint gl_LocalInvocationIndex;

</source>

</source>

Line 261:

Line 264:

The ''gl_GlobalInvocationID'' variable uniquely identifies this invocation across all other local and global work groups dispatched by the compute dispatch call. It is defined as:

The ''gl_GlobalInvocationID'' variable uniquely identifies this invocation across all other local and global work groups dispatched by the compute dispatch call. It is defined as:

The ''gl_LocalInvocationIndex'' contains a 1-dimensional representation of ''gl_LocalInvocationID''. It is defined as:

The ''gl_LocalInvocationIndex'' contains a 1-dimensional representation of ''gl_LocalInvocationID''. It is defined as:

+

<source lang="glsl">

gl_LocalInvocationIndex =

gl_LocalInvocationIndex =

gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +

gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +

gl_LocalInvocationID.y * gl_WorkGroupSize.x +

gl_LocalInvocationID.y * gl_WorkGroupSize.x +

gl_LocalInvocationID.x;

gl_LocalInvocationID.x;

+

</source>

== Compute shader other variables ==

== Compute shader other variables ==

<source lang="glsl">

<source lang="glsl">

+

const uvec3 gl_WorkGroupSize; // GLSL ≥ 4.30

</source>

</source>

+

The ''gl_WorkGroupSize'' variable is a constant that contains the local work-group size of the shader, in 3 dimensions. It is defined by the [[GLSL_Type_Qualifiers#Layout_qualifiers|layout qualifiers]] ''local_size_x/y/z''. This is a compile-time constant.

[[Category:OpenGL Shading Language]]

[[Category:OpenGL Shading Language]]

Revision as of 16:40, 13 February 2013

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.

Note: This page only describes the core OpenGL shading language pre-defined variables. Any variables that are from the compatibility profiles are not listed here.

Vertex shader inputs

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.

Vertex shader outputs

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.

Tessellation control shader inputs

/* no tessellation control shaders prior to GLSL 4.00 */ingl_PerVertex{vec4gl_Position;floatgl_PointSize;floatgl_ClipDistance[];}gl_in[gl_MaxPatchVertices];inintgl_PatchVerticesIn;inintgl_PrimitiveID;inintgl_InvocationID;

Tessellation control shader outputs

/* no tessellation control shaders prior to GLSL 4.00 */outgl_PerVertex{vec4gl_Position;floatgl_PointSize;floatgl_ClipDistance[];}gl_out[];patchoutfloatgl_TessLevelOuter[4];patchoutfloatgl_TessLevelInner[2];

Geometry shader inputs

The gl_PerVertex variables contain the values passed from the previous 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. It is the number of primitives previously processed by the current glDraw*​ rendering call.

GLSL 4.00 and above also have the following definitions:

inintgl_InvocationID;

Geometry shaders can be invoked multiple times over the same primitive with geometry shader instancing; this value specifies the current invocation instance.

Geometry shader outputs

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.

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 the value of all output variables are undefined when EmitVertex()​ is called from the geometry shader.

GLSL 4.10 defines the following extra outputs:

outintgl_ViewportIndex;

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 vertex of a primitive.

Fragment shader inputs

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:

inintgl_SampleID;invec2gl_SamplePosition;inintgl_SampleMaskIn[];

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 the following inputs:

inintgl_Layer;inintgl_ViewportIndex;

gl_Layer and gl_ViewportIndex both have the values passed from the geometry shader. If no geometry shader is used, they both have the value 0.

Fragment shader uniforms

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

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

outfloatgl_FragDepth;

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).

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.

Compute shader other variables

constuvec3gl_WorkGroupSize;// GLSL ≥ 4.30

The gl_WorkGroupSize variable is a constant that contains the local work-group size of the shader, in 3 dimensions. It is defined by the layout qualifierslocal_size_x/y/z. This is a compile-time constant.