Appendix I. Built-In OpenGL Shading Language Variables and Functions

The OpenGL Shading Language, commonly called the “GLSL,” defines a number of variables for matching OpenGL state and large set of convenience functions. This appendix[1] provides a complete list of GLSL built-in variables and functions.


[1] This appendix is adapted from The OpenGL® Shading Language Specification (Versions 1.30 and 1.40) by John Kessenich.

The OpenGL Shading Language has been updated including deprecating and removing some features.

Version 1.30 of GLSL added numerous new functions and data types, while maintaining complete backward compatibility with previous versions of GLSL. No features were removed through deprecation, although various variables and state were deprecated.

Version 1.40, while aligning with the removed features of OpenGL, removed several features as well. Those removed features are made available again if the GL_ARB_compatibility extension is available with your implementation. Removed features (which are supported with the compatibility extension) are listed in the margin.

Variables

For OpenGL versions up to and including 3.0, much of the OpenGL state associated with the fixed-function pipeline that an application can configure is also available as variables from within a GLSL shader. The next sections describe all state variables available to shaders, and any restrictions on their use.

Vertex Shader Input Attribute Variables

Table I-1 lists the set of global attribute values reflecting the per-vertex input data that can be accessed exclusively from within a vertex shader. Almost all of these values were deprecated in Version 1.30.

Compatibility Extension

gl_Vertex

gl_Color

gl_ SecondaryColor

gl_Normal

gl_ MultiTexCoordn

Table I-1. Global Vertex Shader Attribute Variables

image

Vertex Shader Special Output Variables

The following variables are used in later stages of the vertex processing pipeline, and are only available in vertex shaders.

In particular, the gl_Position variable is the only required output of vertex shaders, and specifies the final, projected position of the input vertex. Generally, this is accomplished by directly multiplying the input vertex by the combined modelview and projection transformations, as in the following example:

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;


Table I-2 enumerates the special output variables of a vertex shader.

Table I-2. Special Vertex Shader Output Variables

image

Compatibility Extension

gl_ClipVertex

Vertex Shader Output Varying Variables

While user-defined varying variables are shared between vertex and fragment shaders, certain OpenGL-defined varying variables are accessible only in vertex shader, with their values being assigned to a unique set of (corresponding, but differently named) varying values that are available only in fragment shaders.

Table I-3 lists the variables that are only available in vertex shaders.

Table I-3. Varying Vertex Shader Output Variables

image

Compatibility Extension

gl_Front*Color

gl_Back*Color

gl_TexCoord

gl_FogFragCoord

Table I-4 lists the set of input varying variables available in fragment shaders.

Table I-4. Varying Fragment Shader Input Variables

image

Compatibility Extension

gl_*Color

gl_TexCoord

gl_FogFragCoord

Built-In Implementation Constants

Most implementation-dependent maximum values are reflected as constant integer variables accessible in both vertex and fragment shaders. The variables listed in Table I-5 represent the minimum capabilities of an OpenGL implementation, and all are represented as a single constant integer expression.

Compatibility Extension

gl_MaxClipPlanes

gl_MaxVarying Floats

Table I-5. Implementation Maximum Value Variables

image

Built-In Uniform State Variables

The following sets of variables are available in both vertex and fragment shaders, and reflect the OpenGL state settings specified by the application.

Note

All built-in uniform state, with the exception of gl_DepthRange, was deprecated in GLSL 1.30, and effectively removed. All of the functionality is available if your implementation includes the GL_ARB_compatibility extension.

Transformation Matrix State

The following variables represent the matrix transformation states of OpenGL. Various matrices are derived from the values set within an OpenGL application. For example, lighting normals (see “Define Normal Vectors for Each Vertex of Every Object” in Chapter 5) are transformed by the inverse-transpose of the upper-left 3 × 3 part of the modelview matrix, which is automatically computed for you when changes are made to the modelview matrix and presented using the gl_NormalMatrix variable.

Derived matrices (particularly those involving matrix inverses) may be undefined if the source matrix is ill-conditioned or singular.

Table I-6 lists the available matrices in GLSL.

Compatibility Extension

All fixed-function matrices

Table I-6. Transformation Matrix Variables

image

Lighting Normal Normalization State

Vertex normals can be normalized within a shader by using the normalize() function. However, if normals were uniformly scaled (all calls to glScale() had the same values for x, y, and z), they could be normalized by a simple scaling operation (thus saving the potentially expensive square root operation required by normalize()). The scaling factor is stored in:

uniform float gl_NormalScale;


and applied as

vec3 normal = gl_NormalScale * gl_Normal;


Depth Range State

Access to the depth range values (as set by glDepthRange()) can be accessed using:

image


Table I-7 provides descriptions for each of the depth range variables.

Table I-7. Depth Range Variables

image

User-Defined Clipping Planes

Access to the user-defined clipping planes (as specified by glClipPlane()) can be accessed in a shader through the array:

uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];


Compatibility Extension

gl_ClipPlanes

Point Parameter

Parameters controlling the size and shading of points are controlled by the state set by calling glPointParameter(). The following structure allows access to all point parameter settings:

image


Compatibility Extension

gl_Point

The variables associated with point sizes and parameters are listed in Table I-8.

Table I-8. Point Size and Attenuation Variables

image

Material State

Material state, as specified by calling glMaterial(), can be queried by utilizing the values in the following structure, which stores material properties for both the front- and back-material state:

image


Compatibility Extension

gl_FrontMaterial

gl_BackMaterial

Table I-9 lists the material variables and their descriptions.

Table I-9. Lighting Material Variables

image

Lighting State

The state associated with each light in OpenGL is stored in the following structure. For this section, the notation described in “The Mathematics of Lighting” on page 240 is used.

Compatibility Extension

gl_LightSource

gl_LightModel

gl_ FrontLightProduct

gl_ BackLightProduct

image


Table I-10 provides a list of the parameters associated with each light source available in a shader.

Table I-10. Light Source Variables

image

The following structure contains the values for the light-model parameters.

image


The values associated with the light model are described in Table I-11.

Table I-11. Light Model Variables

image

To help reduce the computation required for computing the colors based on OpenGL’s Phone lighting model, the non-light dependent terms of the lighting equation (the emission and ambient material terms) combined with the light model ambient value are cached in the following structure and are described in Table I-12.

image


Table I-12. Cached Light Model Value Variables

image

Similarly, various products of constant factors of the material and lights are cached in the following structure and described in Table I-13.

image


Table I-13. Cached Light Product Value Variables

image

Texture Environment State

The texture environment color (as specified by glTexEnv()) for each texture unit is available by accessing:

uniform vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits];


Texture Coordinate Generation State

The user-defined planes for texture coordinate generation are available by accessing the following sets of values, one for each set of texture coordinates:

image


Compatibility Extension

gl_EyePlane*

gl_ObjectPlane*

Fog

The state associated with fog and atmospheric effects (see “Fog” in Chapter 6) in OpenGL is stored in the following structure and described in Table I-14.

Compatibility Extension

gl_Fog

image


Table I-14. Fog Variables and Cached Values

image

Built-In Functions

Angle Conversion and Trigonometric Functions

Table I-15 describes the trigonometric functions available in GLSL shaders. Angles are measured in radians, and the following commands operate on each component separately. TYPE represents one of float, vec2, vec3, or vec4.

Table I-15. Angle Conversion and Trigonometric Functions

image

Transcendental Functions

Table I-16 lists the logarithmic and exponential functions available in GLSL. The commands operate on each component separately. TYPE represents one of float, vec2, vec3, or vec4.

Table I-16. Transcendental Functions

image

Basic Numerical Functions

Table I-17 lists the basic numerical functions that GLSL supports. Each command operates on each component separately. TYPE represents one of float, vec2, vec3, or vec4. iTYPE represents one of int, ivec2, ivec3, or ivec4. Similarly, uTYPE represents one of uint, uvec2, uvec3, or uvec4.

Table I-17. Basic Numerical Functions

image image

Vector-Operation Functions

Table I-18 describes the vector operations in GLSL. The following commands operate on their arguments as vectors. Where not explicitly specified, TYPE represents one of: float, vec2, vec3, or vec4.

Compatibility Extension

ftransform()

Table I-18. Vector-Operation Functions

image

Matrix Functions

As compared to standard matrix multiplication, which is defined using the * operator, the matrixCompMult() routine, described in Table I-19, produces a component-wise multiplication of two equally dimensioned matrices.

Table I-19. Matrix Functions

image

Vector-Component Relational Functions

The following functions produce a vector of Boolean values representing the results of the specified operation conducted on each pair of components from the input vectors. TYPE represents any of vec2, vec3, vec4, ivec2, ivec3, ivec4, uvec2, uvec3, or uvec4 in the prototypes below, or a Boolean vector when explicitly specified. The input vectors must be of the same type, and the Boolean vector returned, represented by bvec below, will have the same size at the input vectors.

Table I-20 describes the accepted vector functions.

Table I-20. Vector Component Operation Functions

image

Texture Lookup Functions

Texture lookup functions are available to both vertex and fragment shaders if supported by the underlying OpenGL implementation. All of these functions rely on the use of samplers, which provide a handle for accessing the texture map specified by the application using the OpenGL API. The different types of samplers are detailed in “Accessing Texture Maps in Shaders” in Chapter 15.

In the following tables, sampler types (e.g., floating point, signed, and unsigned integer) are generically represented by “SAMPLER” with the specific texture type appended (e.g., SAMPLER2DArray). Likewise, optional parameters are denoted in square brackets ([...]).

The functions in Tables I-29 through I-32 provide access to textures through samplers, as set up through the OpenGL API. Texture properties such as size, pixel format, number of dimensions, filtering method, number of mipmap levels, depth comparison, and so on are also defined by OpenGL API calls. Such properties are taken into account as the texture is accessed via the built-in functions defined below.

Texture Sizes Access Functions

The following routines will return the sizes (as a set of integers) for each dimension of a texture level (lod):

Table I-21. Texture Size Access Functions

image

Basic Texture Access Functions

The following functions use the provided texture coordinates to retrieve a texel from the texture map associated with sampler.

All forms are accepted by fragment shaders. Functions with the bias parameter are not accepted in vertex shaders.

The optional bias parameter is used to alter which mipmap is sampled (see “Calculating the Mipmap Level” in Chapter 9). If a bias value is provided while accessing a texture that does not contain mipmaps, the texture is accessed directly, and the value is ignored. The bias forms of the texture functions are available only in fragment shaders.

Table I-22. Basic Texture Access Functions

image

Texture Lookup Functions Using a Constant Coordinate Offset

Sample a texture using a constant offset that is applied to the texture coordinates provided. If a bias value is specified, it is added to the mipmap lod parameter before sampling. The bias form is not valid in vertex shaders.

Table I-23. Constant Texture-Coordinate Offset Texture Access Functions

image

Single Texel Fetch Functions

The following functions retrieve a single texel (without filtering) from a texture. The *Offset forms offset the supplied texture coordinates by an offset, similar to the textureOffset() functions.

Table I-24. Single Texel Texture Access Functions

image

Projected Texture Access Functions

The following functions retrieve a texel from the texture map associated with sampler. Before the texel is retrieved, all components of the input texture coordinate, coord, are divided by the last coordinate.

All forms are accepted by fragment shaders. Functions with the bias parameter are not accepted in vertex shaders.

Once again, the optional bias parameter is used to alter which mipmap is sampled (see “Calculating the Mipmap Level” in Chapter 9). If a bias value is provided while accessing a texture that does not contain mipmaps, the texture is accessed directly and the value is ignored. The bias forms of the texture functions are available only in fragment shaders.

The *Offset forms apply the specified offset, similar to the operation of the texelOffset() routines, to the provided coordinates before the projective divide and texture sampling.

Table I-25. Projected Texture Access Functions

image image

Explicit Texture LOD Access Functions

The following functions retrieve texels only from a single mipmap level explicitly specified by the lod parameter.

The *Offset forms apply the specified offset, similar to the operation of the texelOffset() routines, to the provided coordinates before texture sampling.

Table I-26. Explicit Texture LOD Access Functions

image

Explicit Gradient Texture Access Functions

The following functions sample a texture using the user-supplied gradients in the mipmap level determination. The *Offset forms apply the specified offset, similar to the operation of the texelOffset() routines, to the provided coordinates before texture sampling.

Table I-27. Explicit Gradient Texture Access Functions

image image

Combination Texture Access Functions

The following routines provide combinations of the other routines for texture access. For example, the textureProjLod() function does a projective texture sample on a single mipmap level. As always, the *Offset() variants apply a coordinate offset before sampling the texture, similar to textureOffset().

Table I-28. Projected, LOD, and Gradient Combined Texture Access Functions

image image image

Deprecated Texture Access Functions

The following set of functions were deprecated in GLSL Version 1.30, but had not been removed from the language as of the time of this book’s publication.

Projective Texture Access Functions

The following functions retrieve a texel from the texture map associated with sampler. Before the texel is retrieved, all components of the input texture coordinate, coord, are divided by the last coordinate.

Table I-29. Deprecated Basic Texture Access Functions

image

All forms are accepted by fragment shaders. Functions with the bias parameter are not accepted in vertex shaders.

Once again, The optional bias parameter is used to alter which mipmap is sampled (see “Calculating the Mipmap Level” in Chapter 9). If a bias value is provided while accessing a texture that does not contain mipmaps, the texture is accessed directly and the value is ignored. The bias forms of the texture functions are only available in fragment shaders.

Table I-30. Projective Texture Access Functions

image

Vertex Shader Texture Access Functions

Texel retrieval in vertex shaders does not include the automatic computation of a mipmap level, as happens in a fragment shader. The following functions allow for the sampling of mipmapped textures by vertex shaders by providing an mipmap level-of-detail, or lod value (see “Calculating the Mipmap Level” in Chapter 9 for how to compute the level-of-detail value).

The vertex shader versions of the texture access functions are available in non-projective and projective forms (those calls that include “Proj” in the name. The input texture coordinates for the projective forms are processed as described in “Projective Texture Access Functions” on page 33.

Table I-31. Vertex Shader Texture Access Functions

image

Cube-Map Texture Access Functions

Retrieve the texel at input texture coordinate, coord, in the cube map texture currently bound to sampler. The direction of coord is used to select which face to do a two-dimensional texture lookup in.

Table I-32. Cube-Map Texture Access Functions

image

Shadow-Map Texture Access Functions

If a non-shadow texture call is made to a sampler that represents a depth texture with depth comparisons turned on, then results are undefined. If a shadow texture call is made to a sampler that represents a depth texture with depth comparisons turned off, then results are undefined. If a shadow texture call is made to a sampler that does not represent a depth texture, then results are undefined.

Table I-33. Shadow-Map Texture Access Functions

image

Fragment Processing Functions

Table I-34 describes the functions for accessing a fragment’s derivative, which are computed as the fragments of the underlying geometric primitive are rasterized.

Table I-34. Fragment Derivative Functions

image

Noise Functions

The stochastic noise functions described in Table I-35 are available in both vertex and fragment shaders. The computed noise values returned are pseudo-random, but repeatable provided the same random number seed is used.

Table I-35. Random-Noise Generation Functions

image
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.255.24