Simple Vertex ShaderBy Pieter Germishuys, January 9 2006 |
HLSL - High Level Shader Language, this is the topic at hand today and will be lots of fun as you will soon find out.
What is HLSL?
Before we get to the good stuff we will need a good foundation as to what HLSL is. Let's differentiate Shaders from the FFP (Fixed Function Pipeline). Up to now we have used the FFP, the FFP is the part of the Direct3D API that provides us with a fixed way of setting transformations, lighting etc...
By making the vertex processing and the first part of the pixel processing programmable. Us game/graphics developers can do some pretty cool stuff. Examples of such effects are doing nice looking realistic water and normal mapping such as in the normal mapping tutorial on my site.
For further information on the Fixed Function Pipeline refer to the documentation.
What do I need to know before moving onto shaders (HLSL)?
I cannot express the point enough, you should be very familiar with your spaces. As a quick recap, let's look at what spaces we are familiar with.
Object space : This space is where you define your vertices. Remember myVertex = new CustomVertex.PositionColored(1.0f, 0.0f, 0.0f, Color.Blue.ToArgb());
World space : Transforming your vertices to a position in the world is done using the world transformation matrix.
View space : This space locates your camera in space and position your camera and relocates all your objects around your camera.
Clip space : Also sometimes known as the projection transformation. This transformation takes your view frustum and converts it to a cuboid. Giving the impression that you have perspective. e.g. Objects at the back of the view frustum get shrinked and the impression that they are far away (small).
The other important aspects that you must know for shaders you can pick up along the way.
So as a little introduction. Let's look at a simple vertex shader. Remember the triangle that we created in tutorial 1? We are going to do exactly the same using HLSL.
What does HLSL look like? (A simple shader)
float4x4 worldViewProj : WORLDVIEWPROJ; //our world view projection matrix //application to vertex structure |
This is a simple vertex shader that just transforms the vertices that you give the shader from object space to screen space.
Let's take a look at what we have here, First of all you might notice that we have a float4x4 which is a keyword for HLSL that declares a 4x4 matrix called worldViewProjection and as we know transforming vertices from object space to screen space we need a world view projection transformation matrix. The difference to really note here is the a item called a semantic noted by the colon ":" and WORLDVIEWPROJ.
What are semantics?
"Semantics attach shader input and output parameters with pipeline data, or other shader parameters."
What does this mean? This means that when we look at the POSITION0 semantic in our example. This semantic is used by the vertex shader to specify where the positional data from the vertex buffer should be linked. So think of it as storage locations. With that said you will realize that we have no color semantic set and this means that no color is shown in our example.
Next up we have a structure that defines information coming from our application such as our vertex data and going to the vertex shader. We also define another structure that gives our information from the vertex shader to the pixel processing pipeline. Our simple method takes the 2 structures as parameters and note the keywords "in" and "out". These are keywords that tell the the shader that we will have incoming and outgoing data. We then just simply multiply the position data with the worldviewprojection matrix and compile the vertex shader. Simple as that.
This brings us to the end of our first HLSL tutorial. I hope that this gives everyone some grounding for the next couple of tutorials.
Next we will be covering the coloring.
Files for this tutorial
Filename | Size |
? hlsl1_vs2003.rar | 77.7 KB |
? hlsl1_vs2005.rar | 107.4 KB |