Introduction to Shader Programming

Part I (workshops 1..4) by Taco Cohen
Part II (workshops 5..7) by jcl

Introduction

In recent years, more and more emphasis has been put on the graphical aspect of video games. This has led to the advent of programmable vertex and pixel shaders in 2001. Before that, rendering of 3D graphics was handled by a number of fixed graphics algorithms. collectively called the fixed function pipeline.

In the fixed function pipeline, the only way to influence the rendering of 3D objects is by setting a number of parameters. Vertex and pixel shaders, on the other hand, give the programmer full control over how the objects in the 3D world are rendered, making it possible to create a visually unique game. Some examples of popular shading effects are bump/normal mapping, reflective/refractive water surfaces, toon shading and several post-processing effects like depth of field and bloom.

Gamestudio already comes with a huge collection of pre-made shaders that you can attach to your objects or level walls just with a mouse click. But when you need some special effects that are not covered by the shader collection, you might want to write your own shaders. In this tutorial we will take a look at how vertex and pixel shaders work. We will examine the rendering pipeline and see where the vertex and pixel shaders come into play. After that we'll build a simple lighting model in Microsoft's high level shading language (HLSL). We will start with simple ambient lighting and later add diffuse light, specular highlights and finally, normal mapping. In the advanced workshops we'll learn how to use shaders for postprocessing, shadow mapping, and smooth shadows. Don't worry: All our shaders are kept simple and usually contain less than 10 lines of code. I will explain the syntax details as we go. Fortunately, HLSL uses C syntax and thus is similar to Gamestudio's lite-C language that we (hopefully) know already. On our way we'll also learn how to incorporate shaders in projects in Gamestudio.

This tutorial is geared towards total novices to shader programming. However, a basic knowledge of calculus and trigonometry is required. Knowledge of vectors is also required, but there is an overview of vectors, matrices and transformations in Appendix B. If you are not familiar with vector math or need to be refreshed, I strongly suggest you take a look at it first. Although I will explain the HLSL language peculiarities as we go, you will need some programming experience in any c-like language such as lite-C. Lastly, you need to be familiar with some of the terminology used in game development.

The files and scripts for the shader workshops are included in the download version available on the Gamestudio download page.

The shader workshops require the Commercial Edition of Gamestudio / A8. The free version won't suffice because it doesn't support shaders, but the Gamestudio 30 days trial version will do. You'll also need a 3D card that supports shader model 2.0 and - for the advanced workshops - R32F floating point textures.

Let's get started!

Next: The D3D Pipeline