Computer graphics processing unit (GPU) has come a long way that now they are being used for molecular modeling, oil exploration, and machine learning. OpenGL shading language (GLSL) can harness that power using shaders. A shader is a program that runs on the graphics card. There are several different shaders with each having a specific task to accomplish on the pipeline. The shaders are linked together using an OpenGL program. The OpenGL program will send vertices and attributes down the pipeline. The shaders will use this information to create different effects from color and brightness to more complex effects like bump mapping, distortion, edge detection and motion detection.
GLSL has come a long way since it replaced OpenGL ARB Assembly Lanuage in OpenGL version 1.4. Newer graphics cards runs a higher version of GLSL that gives the ability to program different types of shaders in the pipeline These additional shaders will give you more flexibility. Today’s graphics cards also have unified shaders which allow for any type shaders to run on any computational unit on the GPU. There is also parallel processing on the GPU on which gives user rich graphics in real time.
Why use GLSL
At this time there are three main competing shading language: GLSL, Microsoft High Level Shading language built for DirectX and Cg made by Nvidia . These HLSL and GLSL are similar in that both languages uses C and have a very similar graphics pipeline. The biggest difference is that HLSL is compiled through a Microsoft compiler and GLSL is compiled through the graphics cards driver. Cg is same as HLSL except for the visual differences in graphics cards not made by Nvidia. Cg can compile GLSL code but does not include some of the recent features of GLSL.
Some general benefits of GLSL is that it is cross-platform compatibility with many operating system including: Windows, Mac OS X, Linux and gaming systems. OpenGL has faster draw calls than DirectX. Another benefit is that since the graphics card manufacturer creates the compiler then they can optimize it for their card plus has access to the newest features of the GPU. Some programmers also prefer that OpenGL is ran by a nonprofit organization and is open standard.
Overview of the Pipeline
Shaders are not stand alone programs but work with an outside program that reads the shader, links and create attributes for the vertices. These vertices attributes are then sent to the first stage of the pipeline which is vertex shader.
The first stage in the pipeline is to take the vertices attributes from the OpenGL program for example position, normal vector, texture coordinate, color or any other custom attributes . The vertex shader can only accept one vertex at a time. The shader will then transform the vertex with its attributes . The output can include the position of the current vertex, pixel width/height and the clip distance.
Primitive and Patch Assembly
This stage will accept the vertices from the vertex shader and connectivity information from the OpenGL program then assemble patches or primitives. Primitives can be points, lines, triangles or patches for the tessellation shader. These primitives will be sent to the geometry shader and the patches will be sent to the tessellation.
Tessellation takes patches as inputs and converts them to primitives. These patches are array of transformed vertices with user defined number of vertices per patch. A patch is either a triangle or a quad but the patch can have any number of vertices. A patch can carry several of either shapes at once. The Tessellation will then create smaller primitives out of the patches. This is accomplished in three stages: tessellation control, primitive generation, and tessellation evaluation.
The tessellation control takes in the array of vertices and attributes then computes a new patch with the new attributes. This shader can also can subdivide the patches. This stage is programmable. The next stage in the Tessellation is the primitive generation also known as tessellator. Tessellator is not programmable but can be configured. This stage creates new primitives. The new primitives are passed to the tessellation evaluation which creates new vertices. The tessellation evaluation is programmable.
The Geometry shader accepts primitives from either the Tessellation or the Primitive Assembly. These primitives are either points, lines or triangles. The Geometry shader has full knowledge of the vertices it is working including adjacency information. The output will be different types of primitives or zero. This stage is optional.
The rasterization determines which pixels are in the primitive that will be displayed on the screen using clipping. Interpolation will interpolates the vertices to compute the attributes of the pixels. This stage is not programmable.
The Fragment shader receives fragments from the previous stage either from the interpolation or from fragments from the Rasterization. A fragment has a (x, y, z) postion and any data from the interpolation. After this stage, the pixel color will be sent to the frame buffer memory. The Fragment shader is programmable.
The Compute Shader works outside of the pipeline and works independent. The shader is turned on and off from the OpenGL application. The shader does not have any predefined inputs or outputs. It has access to many of the same variables as the other shaders including textures, images, and buffers. The Compute Shader works on work groups.
If setting up an IDE for GLSL seems too much trouble then there is GLSL editors. These editors do come with some draw backs. They are typically limited to only vertex and fragment shaders, and limited control over the program environment.
Shader Maker was created at University of Bremen in Germany. It is cross-platform for Windows, Linux and Mac OS X. This editor allows for vertex, geometry and fragment shader editing. It features syntax highlighting, uniform editing plus comes with predefined shapes and an OBJ loader.
This is an IDE created by Typhoon Labs. Only vertex and fragment shading is possible. Features include real time rendering, syntax highlighting, plug-in support, multiple texture support, access to OpenGL states, and shader validation.
This is a GLSL editor created by AMD but is no longer supported. The latest version of OpenGL it supports is version 2.0. Easy to use plus has a preview window and a debugger.
This is a very unique one, it is a GLSL editor for the iPhone. Once you are done editing the vertex and fragment shader the phone camera can display the results. It has a polished editor, shader input management, vertex data generation, 3D mesh file import, texture management, uniform binding, and many other features.
Types of Variables
There are three types of variables that you might not be a custom to: uniform, attributes and varying.
- Uniform: They are global variables that all shaders have access like light position. They can be of any type and can not change values.
- Attributes: These are the attributes of the vertices that are sent to the vertex shader like position. Only the vertex has access to the attributes.
- Varying: These variables can be shared between shaders and can change between shaders. An example of this is the normal values calculated in the vertex shader using a light source.