This document provides an overview of shaders in XNA game development. It discusses what shaders are and how they allow developers to program graphics pipelines rather than relying on fixed function pipelines. It also covers HLSL (High Level Shader Language) and how to define vertex formats, vertex shaders, and techniques in HLSL code files to render 3D objects with customized shaders. Specific topics covered include declaring effects, loading shader files, using techniques and passes, defining a custom vertex format structure, and writing a simple vertex shader to render colored triangles.
4. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
5. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
6. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
7. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
8. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
9. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
10. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
11. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
12. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
13. What a shader is?!
• Fixed Function Pipeline (FFP)
– limits what game developers can do, but ease things off for them
• Programmable graphics pipelines
– You get to decide exactly how things should be performed
22. The effect file
• An effect is some code that instructs your hardware (the graphics card) how it
should display these triangles
• An effect file contains one or more “techniques”
• For example technique A and technique B. Drawing triangles using technique A
will for example draw them semi-transparent, while drawing them using technique
B will for example draw all objects using only blue-gray colors as seen in some
horror movies.
25. The effect file
• Declaring an effect
• Loading the effect file
Effect effect;
effect = Content.Load<Effect> ("effects");
26. The effect file
• Declaring an effect
• Loading the effect file
Effect effect;
effect = Content.Load<Effect> ("effects");
27. The effect file
• Declaring an effect
• Loading the effect file
Effect effect;
effect = Content.Load<Effect> ("effects");
28. The effect file
• Declaring an effect
• Loading the effect file
• Draw() method
Effect effect;
effect = Content.Load<Effect> ("effects");
device.Clear(Color.DarkSlateBlue);
29. The effect file
• Using a “User-Defined Technique”!
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
30. The effect file
• Using a “User-Defined Technique”!
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
31. The effect file
• Using a “User-Defined Technique”!
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
• A technique can be made up of multiple passes, so we need to iterate
through them. Add this code below the code you just entered:
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
}
32. The effect file
• Using a “User-Defined Technique”!
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
• A technique can be made up of multiple passes, so we need to iterate
through them. Add this code below the code you just entered:
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
// Drawing code that this technique applies its effect to!
}
33. The effect file
• Using a “User-Defined Technique”!
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
• A technique can be made up of multiple passes, so we need to iterate
through them. Add this code below the code you just entered:
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
// Drawing code that this technique applies its effect to!
}
38. HLSL – Vertex Format
• Remember VertexPositionColor?
39. HLSL – Vertex Format
• We’ll just design our own
• “VertexPositionColor”!
40. HLSL – Vertex Format
• We’ll just design our own
• “VertexPositionColor”!
41. HLSL – Vertex Format
• Let’s name it
• “MyOwnVertexFormat”
42. HLSL – Vertex Format
• What we need is
– A structure that can hold the necessary data for each vertex and
• What we need is
– A structure that can hold the necessary data for each vertex and
– A definition of the data, so the vertex shader knows which data is included with every vertex.
• A simple colored triangle through using our format “MyOwnVertexFormat”
• What should our vertex shader hold?!
• Just holding a position and a color!
43. HLSL – Vertex Format
struct MyOwnVertexFormat
{
private Vector3 position;
private Color color;
public MyOwnVertexFormat (Vector3 position, Color color)
{
this.position = position;
this.color = color;
}
}
44. struct MyOwnVertexFormat
{
private Vector3 position;
private Color color;
public MyOwnVertexFormat (Vector3 position, Color color)
{
this.position = position;
this.color = color;
}
}
HLSL – Vertex Format
45. HLSL – Vertex Format
• Now, since we are dealing with the graphics card ,
• the graphics card needs to be told explicitly which data it will receive.
46. HLSL – Vertex Format
• Adding the following code to our struct
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
47. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
48. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
49. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
50. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
51. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
52. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
53. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
54. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
55. public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(
0,
VertexElementFormat.Vector3,
VertexElementUsage.Position,
0),
new VertexElement(
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementUsage.Color,
0)
);
HLSL – Vertex Format
• Adding the following code to our struct
56. HLSL – Vertex Format
• Now, Implementing it in our XNA code!
private void SetUpVertices()
{
MyOwnVertexFormat[] vertices = new MyOwnVertexFormat[3];
vertices[0] = new MyOwnVertexFormat(new Vector3(-2, 2, 0), Color.Red);
vertices[1] = new MyOwnVertexFormat(new Vector3(2, -2, -2), Color.Green);
vertices[2] = new MyOwnVertexFormat(new Vector3(0, 0, 2), Color.Yellow);
vertexBuffer = new VertexBuffer(device, MyOwnVertexFormat.VertexDeclaration,
vertices.Length, BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
}
57. HLSL – Vertex Format
• Now, Implementing it in our XNA code!
private void SetUpVertices()
{
MyOwnVertexFormat[] vertices = new MyOwnVertexFormat[3];
vertices[0] = new MyOwnVertexFormat(new Vector3(-2, 2, 0), Color.Red);
vertices[1] = new MyOwnVertexFormat(new Vector3(2, -2, -2), Color.Green);
vertices[2] = new MyOwnVertexFormat(new Vector3(0, 0, 2), Color.Yellow);
vertexBuffer = new VertexBuffer(device, MyOwnVertexFormat.VertexDeclaration,
vertices.Length, BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
}
70. HLSL – Vertex Shader
• So put this code at the top of your.fx file:
struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
};
71. HLSL – Vertex Shader
• So put this code at the top of your.fx file:
struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
};
72. HLSL – Vertex Shader
• So put this code at the top of your.fx file:
• Now, Place this method between the structure definition and our technique
definition:
struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
};
VertexToPixel SimplestVertexShader(float4 inPos : POSITION)
{
VertexToPixel Output = (VertexToPixel)0;
Output.Position = mul(inPos, xViewProjection);
Output.Color = 1.0f;
return Output;
}
73. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
74. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
75. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
76. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
77. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
78. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
79. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
80. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
81. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
82. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
83. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
84. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
85. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
86. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
Output.Color.rba = 1.0f;
Output.Color.g = 0.0f;
87. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
88. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
89. HLSL – Vertex Shader
• The hole code for now will be
struct VertexToPixel
{ float4 Position : POSITION;
float4 Color : COLOR0;
};
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_2_0 SimplestVertexShader();
PixelShader = NULL;
}
}
99. Pixel Shader
• We have the our last lesson Vertex Shader up and running, we just need a Pixel
Shader now to get the job done and draw using our own custom way of
rendering!
• The pixel shader receives its input (position and color, in our case) from our vertex
shader, and needs to output only color
100. Pixel Shader
• So let’s define its output structure at the top of our.fx file “Pixel Format”
struct PixelToFrame
{
float4 Color : COLOR0;
};
101. Pixel Shader
• So let’s define its output structure at the top of our.fx file “Pixel Format”
• Our first pixel shader will be a very simple method, here it is “Pixel Shader”
struct PixelToFrame
{
float4 Color : COLOR0;
};
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIn.Color;
return Output;
}
102. Pixel Shader
• So let’s define its output structure at the top of our.fx file “Pixel Format”
• Our first pixel shader will be a very simple method, here it is “Pixel Shader”
struct PixelToFrame
{
float4 Color : COLOR0;
};
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIn.Color;
return Output;
}
103. Pixel Shader
• Now we still need to set this method as pixel shader for our technique, at the
bottom of the file:
PixelShader = compile ps_2_0 OurFirstPixelShader();
104. Pixel Shader
• Now we still need to set this method as pixel shader for our technique, at the
bottom of the file:
PixelShader = compile ps_2_0 OurFirstPixelShader();
technique Simplest
{
pass Pass0
{
VertexShader = compile vs_2_0 SimplestVertexShader();
PixelShader = compile ps_2_0 OurFirstPixelShader();
}
}