Built-in Variable (GLSL)

From OpenGL.org
Revision as of 21:13, 2 May 2011 by Alfonse (Talk | contribs) (Vertex shader outputs: linking)

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.

Vertex shader inputs

Vertex shaders have the following predefined inputs.

in int gl_VertexID;
in int gl_InstanceID;

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 outputs

Vertex shaders have the following predefined outputs.

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

This 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

in gl_PerVertex
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
} gl_in[];
in int gl_PrimitiveIDIn;

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;

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

Geometry shader outputs

out gl_PerVertex
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
out int gl_PrimitiveID;
out int gl_Layer;

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;

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.

Fragment shader inputs

in  vec4  gl_FragCoord;
in  bool  gl_FrontFacing;
in  float gl_ClipDistance[];
in  vec2  gl_PointCoord;
in  int   gl_PrimitiveID;

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;
in  vec2  gl_SamplePosition;

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.

Fragment shader uniforms

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

struct gl_DepthRangeParameters
    float near;
    float far;
    float diff;
uniform gl_DepthRangeParameters gl_DepthRange;

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

Fragment shader outputs

out float gl_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).

GLSL 4.00 adds the following outputs:

out int gl_SampleMask[];

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.