Shader Variable

From OpenGL Wiki
Revision as of 12:01, 4 August 2012 by Alfonse (talk | contribs) (Needed to be marked cleanup, not inaccurate.)
Jump to navigation Jump to search

Shader Variables in GLSL shader programs are not just the usual local and global variables as found in most programming languages; there are additional special types of them that are used to pass information between shaders. Some such variables are predefined by the language, but the programmer can also define their own. These variables can be of the following types:

  • Uniforms are set once by the client program before drawing a group of vertices with the shader. Their values are thus shared among all vertices in that group, which might comprise a single object, or a single part of an object with common material settings, or whatever. Uniforms can be read by all shaders, not just the vertex shader.
  • Attributes are defined on a per-vertex basis, and are passed by the client program in vertex arrays to the vertex shader. These may specify information such as the position of each vertex, the normal at the vertex, colour/lighting info, texture coordinates etc—their interpretation is entirely up to the shader program.
  • Interpolators (commonly called varying variables in OpenGL terminology) are outputs from the vertex shader and inputs to the fragment shader. The vertex shader defines a single value at each vertex, which is automatically linearly interpolated across all fragments which are being rendered between that vertex and its neighbours before being fed to the fragment shader. In GLSL versions prior to 1.30 (pre-OpenGL-3.0), they were defined with the varying type qualifier in both the vertex and fragment shaders; in later versions, they are simply declared with the out qualifier in the vertex shader, and the in qualifier in the fragment shader.