Programmable Pipeline
State of the Art: GPUs
- Graphics Processing Unit = Super Computers on the Desktop
- Order of magnitude faster than CPU
- 2007: CPU's peak performance ~70 Giga FLOPs, but GPU's was ~1 Tera FLOPs
- Different model: highly parallel for processing data arrays
- CPU is organized for latency, GPU for throughput
- GPU provides higher level, but restricted commands
- You want your code running on the GPU!
Pipeline History
- Pipeline is mathematically elegant, but fixed
- Only provides two "unrealistic" shading models
- Needs flexibility for new effects, realism
- Shaders introduce simple programs into pipeline
- Introduced by Pixar in 1988, limited to pixel (color) operations
- Now also supports operations on
- vertices (position/normal)
- fragments (depth/occulsion)
- geometry (add/remove points)
- textures (images/combinations)
- Possible effects are limitless
- WebGL and some mobile phones no longer support fixed pipeline!
Pipeline Steps
- Perform lighting calculations to find vertex colors
- Transform vertices to screen coordinates
- Find all the pixels covered by the triangle
- Fill all unoccluded pixels with the interpolated vertex colors and depth
Shaders
- Applied uniformly to all elements (vertices, pixels, triangles, etc.)
- Allowed read only access to arbitrary data designated by the programmer
- Can apply different shaders to different geometry
- Can have more than one active at once, but only one
main
program
- Commonly programmed in the following languages: GLSL (OpenGL), Cg, HLSL, Assembly
- Vertex Shader
- Manipulates vertex's position, normal, color, or texture coordinate
- Applied before vertices are clipped
- Must output vertex position
- Pixel Shader
- Manipulates pixel's color and depth, interpolated across fragments
- Must output pixel color
- Depending on resolution, ~2 million pixels may need to be rendered, lit, shaded, and colored for each frame
Using Shaders in OpenGL
- Create Shaders: allocate handles to shaders
- Specify Shaders: load strings that contain shader source
- Compile Shaders: compile source (translate source and check for errors)
- Create Program Object: programs controls the shaders
- Attach Shaders to Programs: attach shaders to program object via handle
- Link Shaders to Program: link all programs together (verify variables match)
- Enable Program: bind shaders to be run for subsequent geometry
Notes
- Installed shader replaces ALL OpenGL
fixed pipeline functionality until you remove it
- You have to transform each vertex into viewing coordinates
- You have to light each vertex
- You have to apply the current interpolated color to each pixel
- Debugging logic errors is very difficult and even finding syntax errors can be a pain
- Common problems:
- Typos in shader source
- Assuming implicit type conversion
- Attempting to pass data to undeclared varying/uniform variables (note,
may have been optimized out)
Examples
Other Uses
References