From GPWiki
Jump to: navigation, search

Introduction to shaders

The High Level Shader/Shading Language, or HLSL for short, is a proprietary shading language developed by Microsoft for use with Direct3D.

Shaders are run by the Graphics Processing Unit, or GPU for short, on the graphics card of the end users hardware. Since most modern graphics cards have an internal set of memory and processor specificly designed to deal with graphics related math, one can relief strain on the CPU and divert workload to the GPU when it comes to graphical tasks that require alot of calculating operations - such as;

  • Blending
  • Multi texturing
  • Per-pixel lighting (bump & gloss mapping)
  • Cell shading
  • Environmental mapping
  • Glow/Light mapping

H3 masterchief tiff jpgcopy.jpg

Seen above: screenshot from Halo 3, using various shaders

Shader types

HLSL programs come in three types:

  • Vertex shaders

Vertex shaders affect only a series of vertices and thus can only alter vertex properties like position, color, texture coordinate etc. The vertices computed by vertex shaders are typically passed to geometry shaders.

  • Geometry shaders

Geometry shaders can add to and remove vertices from a mesh. Geometry shaders can be used to procedurally generate geometry or to add volumetric detail to existing meshes that would be too costly to process on the CPU.

  • Fragment/Pixel shaders

Pixel shaders - known as "fragment" shaders in GL - calculate the color value of individual pixels when the polygons produced by the vertex & geometry shaders are rasterized. They are typically used for scene lighting and related effects such as bump mapping and color toning.

HLSL Language

HLSL is based on the C programming language and although they share the same syntax, some features of C were modified and new data types were added to make HLSL more suitable for programming graphics processing units.


struct VertexToPixel {

   float4 Position     : POSITION;
   float4 Color        : COLOR0;


float4x4 xViewProjection;

VertexToPixel SimplestVertexShader( float4 inPos : POSITION) {

   VertexToPixel Output = (VertexToPixel)0;
   Output.Position =mul(inPos, xViewProjection);
   Output.Color = 1.0f;    
   return Output;    


technique Simplest {

   pass Pass0
       VertexShader = compile vs_1_1 SimplestVertexShader();
       PixelShader = NULL;


What did we learn?

  • HLSL is an advanced programming topic - not very suitable for beginners
  • Shaders are code that are executed by the GPU
  • Microsoft HLSL is a C-style syntax language