XNA L10–Shaders Part 1

Post on 14-May-2015

270 views 2 download

Tags:

Transcript of XNA L10–Shaders Part 1

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL10 – Shaders Part 1

Working with Shaders in XNA

What is a shader?!

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

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

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

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

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

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

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

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

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

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

HLSLHigh Level Shader Language

HLSL

• HLSL is used not to improve the gameplay, but to enhance the quality of the final

image.

HLSL

• Every vertex that is drawn will pass through your vertex shader, and even every

pixel drawn will have passed through your pixel shader

HLSL

The effect file

• One of the main differences between DirectX 9 and XNA is that we need an effect

for everything we draw!

The effect file

• So, what exactly is an “effect”?

The effect file

• In 3D programming, all objects are represented using triangles. Even spheres!

The effect file

• An effect is…!

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.

The effect file

• Declaring an effect

Effect effect;

The effect file

• .FX Files

The effect file

• Declaring an effect

• Loading the effect file

Effect effect;

effect = Content.Load<Effect> ("effects");

The effect file

• Declaring an effect

• Loading the effect file

Effect effect;

effect = Content.Load<Effect> ("effects");

The effect file

• Declaring an effect

• Loading the effect file

Effect effect;

effect = Content.Load<Effect> ("effects");

The effect file

• Declaring an effect

• Loading the effect file

• Draw() method

Effect effect;

effect = Content.Load<Effect> ("effects");

device.Clear(Color.DarkSlateBlue);

The effect file

• Using a “User-Defined Technique”!

effect.CurrentTechnique = effect.Techniques["Pretransformed"];

The effect file

• Using a “User-Defined Technique”!

effect.CurrentTechnique = effect.Techniques["Pretransformed"];

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 iteratethrough them. Add this code below the code you just entered:

foreach (EffectPass pass in effect.CurrentTechnique.Passes){

pass.Apply();

}

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 iteratethrough 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!

}

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 iteratethrough 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!

}

The effect file

• Quite simple!

HLSL – Vertex Format

HLSLLet’s play with shaders a bit!

HLSL

• Let’s play with shaders a bit!

– Mo l3beh ha! :D

HLSL – Vertex Format

• Remember VertexPositionColor?

HLSL – Vertex Format

• We’ll just design our own

• “VertexPositionColor”!

HLSL – Vertex Format

• We’ll just design our own

• “VertexPositionColor”!

HLSL – Vertex Format

• Let’s name it

• “MyOwnVertexFormat”

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!

HLSL – Vertex Format

struct MyOwnVertexFormat{

private Vector3 position;private Color color;

public MyOwnVertexFormat (Vector3 position, Color color){

this.position = position;this.color = color;

}}

struct MyOwnVertexFormat{

private Vector3 position;private Color color;

public MyOwnVertexFormat (Vector3 position, Color color){

this.position = position;this.color = color;

}}

HLSL – Vertex Format

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.

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)

);

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

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

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

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

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

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

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

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

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

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);}

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);}

HLSL – Vertex Format

• “App1-VertexFormat”

HLSL – Vertex Shader

• Create a new empty effect file, name it(OurHLSLfile.fx)

HLSL – Vertex Shader

• Create a new empty effect file, name it(OurHLSLfile.fx)

• Delete everything in it!

HLSL – Vertex Shader

• Create a new empty effect file, name it(OurHLSLfile.fx)

• Delete everything in it!

HLSL – Vertex Shader

• Create a new empty effect file, name it(OurHLSLfile.fx)

• Delete everything in it!

HLSL – Vertex Shader

• Create a new empty effect file, name it(OurHLSLfile.fx)

• Delete everything in it!

HLSL – Vertex Shader

• Put this as your first HLSL code in your.fx file

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();PixelShader = NULL;

}}

HLSL – Vertex Shader

• Put this as your first HLSL code in your.fx file

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();PixelShader = NULL;

}}

HLSL – Vertex Shader

• Put this as your first HLSL code in your.fx file

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();PixelShader = NULL;

}}

HLSL – Vertex Shader

• Put this as your first HLSL code in your.fx file

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();PixelShader = NULL;

}}

HLSL – Vertex Shader

• Put this as your first HLSL code in your.fx file

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();PixelShader = NULL;

}}

HLSL – Vertex Shader

HLSL – Vertex Shader

• So put this code at the top of your.fx file:

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

HLSL – Vertex Shader

• So put this code at the top of your.fx file:

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

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;

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• The hole code for now will bestruct 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;

}

}

HLSL – Vertex Shader

• FINALLY!

HLSL – Vertex Shader

• float4x4 xViewProjection;

HLSL – Vertex Shader

• float4x4 xViewProjection;

HLSL – Vertex Shader

float4x4 xViewProjection;

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;

}

}

HLSL – Vertex Shader

• NICE!

• “App3-CompleteFirstShader(White)”

• For more info– http://www.riemers.net/eng/Tutorials/XNA/Csharp/series3.php

Pixel Format and Pixel Shader

Pixel Shader

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Pixel_shader.php

For more info visit,

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

Pixel Shader

• So let’s define its output structure at the top of our.fx file “Pixel Format”

struct PixelToFrame{

float4 Color : COLOR0;};

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;}

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;}

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();

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();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

The hole code in HLSL

file

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

VertexToPixel SimplestVertexShader(float4 inPos : POSITION)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = 1.0f;

return Output;

}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

VertexToPixel SimplestVertexShader(float4 inPos : POSITION)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = 1.0f;

return Output;

}

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

Pixel Shader

• Now, let’s use our own HLSL code in our XNA code!

Pixel Shader

• Loading our own effect file “OurHLSLfile”

effect = Content.Load<Effect> ("OurHLSLfile");

Pixel Shader

• Draw() methodprotected override void Draw(GameTime gameTime)

{

device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,

Color.DarkSlateBlue, 1.0f, 0);

effect.CurrentTechnique = effect.Techniques["Simplest"];

effect.Parameters["xViewProjection"].SetValue(viewMatrix * projectionMatrix);

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

device.SetVertexBuffer(vertexBuffer);

device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

}

base.Draw(gameTime);

}

Pixel Shader

• Draw() methodprotected override void Draw(GameTime gameTime)

{

device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,

Color.DarkSlateBlue, 1.0f, 0);

effect.CurrentTechnique = effect.Techniques["Simplest"];

effect.Parameters["xViewProjection"].SetValue(viewMatrix * projectionMatrix);

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

device.SetVertexBuffer(vertexBuffer);

device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

}

base.Draw(gameTime);

}

Pixel Shader

• Draw() methodprotected override void Draw(GameTime gameTime)

{

device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,

Color.DarkSlateBlue, 1.0f, 0);

effect.CurrentTechnique = effect.Techniques["Simplest"];

effect.Parameters["xViewProjection"].SetValue(viewMatrix * projectionMatrix);

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

device.SetVertexBuffer(vertexBuffer);

device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

}

base.Draw(gameTime);

}

Pixel Shader

• We did it out own!

• We told the GPU how to work!

• “App2-CompleteFirstShader(Colored)”

Pixel ShaderRemember the following?!

Pixel Shaderfloat4x4 xViewProjection;

struct VertexToPixel{

float4 Position : POSITION;float4 Color : COLOR0;

};

struct PixelToFrame{

float4 Color : COLOR0;};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0){

VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);Output.Color = inColor;

return Output;}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn){

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;}

technique Simplest{

pass Pass0{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}}

VertexToPixel SimplestVertexShader(float4 inPos : POSITION)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = 1.0f;

return Output;

}

Pixel Shader

• NICE!

• “App3-CompleteFirstShader(White)”