Shader Programming

References

Fosner, R. Real-time Shader Programming. Morgan Kaufman. 2003

 

Fixed Function Pipeline

Traditionally, graphics hardware was designed to render a scene using a fairly standardised process. The programmer had very little control over how primitve were rendered other than by setting the rendering mode (e.g. changing from flat to Gouraud shading). The programmer had even less control over new rendering features (e.g. phong shading or bump mapping), having to wait (hopefully) until the next generation of harware implemented the required feature. This static model of processing is known as the fixed function pipleine.

 

Reading: Overview of GPU operation

Shaders give the programmer more control over how the GPU processes and renders the scene data. A shader is a program which executes on the GPU and replace parts of the existing fixed function pipeline.

Sample Shaders

Shaders are intended to give grater freedom to programmers to create a unique look and style for their application.

Currently there are three types of shaders supported by DirectX and OpenGL; Vertex,Pixel and Geometry Shaders

 

Vertex Shder

Vertex shaders replace the transformation and lighting stage of the pipeline. The vertex shader is called once for each vertex processed. The input to the vertex shader is one vertex (position in model sapce), texture coords, colour, etc), the output is a vertex transformed into clip space. The other vetex properties (colour, texture coords may also be changed by the vertex shader). The vertex shader is often used to implement;

The vertex shader cannot add or remove vertices, just modify a vertices attributes. A minimum vertex shader should at least output the transformed homogeneous vertex position.

Geometry Shader

Geometry shader programs are executed after vertex shaders. They take as input a whole primitive, possibly with adjacency information. For example, when operating on triangles, the three vertices are the geometry shader's input. The shader can then emit zero or more primitives, which are rasterized and their fragments ultimately passed to a pixel shader.

Typical uses of a geometry shader include point sprite generation, geometry tessellationshadow volume extrusion, and single pass rendering to a cube map. A typical real world example of the benefits of geometry shaders would be automatic mesh complexity modification. A series of line strips representing control points for a curve are passed to the geometry shader and depending on the complexity required the shader can automatically generate extra lines each of which provides a better approximation of a curve.

 

Pixel Shader

Pixel shaders are run for each pixel drawn, the output of a pixel shader is (normally) a single pixel colour. The inputs are colour, texture coordinates, scene lights and textures. The pixel shader is used to implment;

Shader languages

A shading language is a special programming language adapted to easily map on shader programming. Those kind of languages usually have special data types like color and normal. Programs(shaders) in these languages are compiled to the instruction set of the GPU and executed at the proper point n the pipeline.

Phong Shading New Metal Shading
Non Photo-realistic shading

Shader Models (HLSL) & Shader Versions(GLSL)

The cababilities of GPUs is normally defined by the shader model. A shader model is a standard specification of the feature set of a GPU. An application can query the the graphics hardware at runtime to ensure that it is compatible with the selected shaders.

(Vertex and Pixel) Shader Model 1.1

(Vertex and Pixel) Shader Model 2.0

(Vertex and Pixel) Shader Model 3.0

Shader Model 4.0

Shader Model 5.0