Article: Crash Course in HLSL
The XNA Effect Template Explained
Now that the main theoretical aspects are in place, you should look at the default template for effect files provided by XNA Game Studio 3.1. To see this template, create a new XNA project, and add a new Effect File to your Content project. You should see something similar to the code below.
float4x4 World;
float4x4 View;
float4x4 Projection;
// TODO: add effect parameters here.
struct VertexShaderInput
{
float4 Position : POSITION0;
// TODO: add input channels such as texture
// coordinates and vertex colors here.
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
// TODO: add vertex shader outputs such as colors and texture
// coordinates here. These values will automatically be interpolated
// over the triangle, and provided as input to your pixel shader.
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
// TODO: add your vertex shader code here.
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
// TODO: add your pixel shader code here.
return float4(1, 0, 0, 1);
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_1_1 VertexShaderFunction();
PixelShader = compile ps_1_1 PixelShaderFunction();
}
}
At the very beginning, the effect contains definitions for three parameters. These parameters are matrices (float4x4) and represent the world, view and projection matrices. Under them, two structure data types are definded: one that defines the input to a vertex shader, VertexShaderInput, and one that defined the output of the vertex shader, VertexShaderOutput. Each of these structures contain a channel named Position, linked to the semantic POSITION0, which lets the GPU know the meaning of this data.
Next, a function is defined, which will fill the place of a vertex shader. As expected, its input is a VertexShaderInput structure, and it returns a VertexShaderOutput structure. The position given as an input is multiplied by the three matrices specified earlier, and the resulting position is written as the output. This sequence of multiplications is the most common one that happens in almost all vertex shaders.
The function destined to become a pixel shader returns a float4 as a result, and binds it to the COLOR0 semantic. The function uses a VertexShaderInput structure as input. This can be written in lots of different ways, but organizing the data like this yields in shader code which is cleaner and easier to maintain. The default XNA code simply returns the color red as the result.
And lastly, there is a technique defined. This technique only contains one pass, which uses the two functions defined above as a vertex shader and a pixel shader.
You can use this template as a starting point for your effect files.
Ending Words
I hope this short introduction was helpful for those of you looking to learn HLSL and shader programming. If you have any questions, feel free to post them in the comments below.
March 25th, 2010 - 07:42
Very interesting and well written article! I always has a bit of trouble getting the hang of shaders, but this has helped me out! Perhaps I should start looking into XNA again, with the new 4.0 release and all
March 25th, 2010 - 18:23
I’m always glad when a tutorial of mine helps someone
Shaders are really nice and learning to use them will help you a lot. Just remember that if you target the Windows Phone, XNA 4.0 has no support for shaders on that platform.
September 8th, 2010 - 13:09
Oh I wish I new about the NoInterpolation keyword much sooner… thanks Catalin; as always, a very nice read.
January 1st, 2011 - 20:06
Very nice articles, thank you.
It would be very useful if it were possible to get all the cash course in HLSL articles in one PDF file.
Then it could all be printed out and added to reference documents.
Thanks again for the articles and all the work involved in mamking this available to us folks.
Steve
January 4th, 2011 - 20:50
That’s a good idea. I’ll try to find some time in the next days to do this.