OpenGL Shader Variable Types

From GPWiki
Jump to: navigation, search

OpenGL Shading Language Data Types

The OpenGL Shading Language (GLSL) defines many data types, 4 data type qualifiers, and 4 function parameter types. They are used to define data, how data can be accessed, and how a function can interact with data.

Data Types

GLSL provides many different data types to make mathematics and graphic processing easier. For basic numerical types such as float and int, the standard mathematical operators are implemented.

For vectors, addition, subtraction and scalar multiplication is defined. Also, multiplication with a matrix is also defined. These operations use the standard mathematical operators, +-*/.


Vectors are mathematical vectors. They represent spacial coordinates, color, and texture coordinates. For this reason, there are multiple ways to reference the components of a vector.

  • x,y,z,w - The mathematical vector
  • r,g,b,a - The color vector
  • s,t,p,q - The texture coordinate vector.

Using the "swizzle" operator ., you can create vectors on the fly. You cannot mix component types in one call of the swizzle operator. void main() {

   vec4 someVector(1.0f, 2.0f, 3.0f, 4.0f);; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.rgb; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.stp; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.xgp; //Not valid!
   someVector.stz; //Not valid!


Void Data Type

This type is used to designate the return type of a function only.

  • void - A null type, it represents nothing.

Floating Point Types

  • float - The basic floating point numerical type. Declared using the suffix f, as in 1.0f.
  • vec2 - A two dimensional vector of floats.
  • vec3 - A three dimensional vector of floats.
  • vec4 - A four dimensional vector of floats

float myFloat = 1.0f; vec2 myVec2(1.0f, 2.0f); vec3 myVec3(1.0f, 2.0f, 3.0f); vec4 myVec4(1.0f, 2.0f, 3.0f, 4.0f);

Integer Types

  • int - The basic Integer
  • ivec2 - A two dimensional vector of integers.
  • ivec3 - A three dimensional vector of integers.
  • ivec4 - A four dimensional vector of integers

int myInt = 1; ivec2 myVec2(1, 2); ivec3 myVec3(1, 2, 3); ivec4 myVec4(1, 2, 3, 4);


  • bool- The basic boolean. Can either be true or false.
  • bvec2 - A two dimensional vector of booleans.
  • bvec3 - A three dimensional vector of booleans.
  • bvec4 - A four dimensional vector of booleans

bool myBool = true; bvec2 myVec2(true, true) bvec3 myVec3(true, true, true); bvec4 myVec4(true, true, true, true);

The vector forms of a boolean have the functions any and all defined.

  • bool any( bvec ) - Returns true if any members of the vector is true.
  • bool all( bvec ) - Returns true if all members of the vector is true.

Matrix Types

  • mat2 - A 2 by 2 matrix of floats
  • mat3 - A 3 by 3 matrix of floats
  • mat4 - A 4 by 4 matrix of floats

These matrixies are constructed either with floats, or with vectors of the correct size.

Sampler Types

These types allow one to sample from a texture.

  • sampler1D - A one dimensional texture sampler.
  • sampler2D - A two dimensional texture sampler.
  • sampler3D - A three dimensional texture sampler.
  • samplerCube - A cube map sampler.
  • sampler1DShadow - A one dimensional shadow texture sampler.
  • sampler2DShadow - A two dimensional shadow texture sampler

Data Type Qualifiers

  • uniform - Input to vertex and fragment shader from OpenGL or an application. Read Only. This variable is not interpolated from the vertex shader to the fragment shader.
  • attribute - Input per vertex to vertex shader from OpenGL or an application. Read Only.
  • varying - Output from vertex shader, where it is readable and writable, interpolated, and then used as input to the fragment shader, where it is read only.
  • const - Compile time constant. Read Only.

Function Parameters

  • in - Value can be modified, and is only initalized on entry. Modifying this variable inside the function has no effect outside the function.
  • out - Value can be modified, is not initalized on function entry. Modifying this variable inside the function changes the variable passed to this function.
  • inout - A combination of in and out.
  • const - Value cannot be changed.

void doStuff(in float a, out float b, inout float c) {

   a = 0.0f;
   b = 0.0f;
   c = 0.0f;


void main() {

   float a = 1.0f;
   float b = 2.0f;
   float c = 3.0f;
   doStuff(a, b, c); //Inside doStuff, b can be any value, and c is 3.0f
   //Now, a == 1.0f; b == 0.0f; c == 0.0f