pass, which can be anything you like. Inside the rendering pass you
specify what functions are to be compiled for the vertex and pixel
shader types and which shader model to use. This is done by setting
the HLSL shader types VertexShader and PixelShader to equal the
compiled corresponding shader functions. Compilation is performed
by calling the compile keyword, which is followed by the shader ver
sion and the function it is to be compiled with. The shader version
is specified using the major version number followed by the minor.
For example, vs_3_0 is used for compiling a vertex shader using
Shader Model 3.0, ps_2_0 for a pixel shader using Shader Model
2.0, and so forth.
Shader Model 3.0 cannot be used with previous shader models.
For example, you can’t have a vertex shader using 2.0 and a pixel
shader using 3.0. The only exception is when using reference (soft
ware rendering) mode in Direct3D. Previous versions were able to
mix and match, but 3.0 and 4.0 cannot.
You can also set rendering states in the pass of a technique. For a
list of all the rendering states, you can check the XNA documenta-
tion under GraphicsDevice.RenderState.
Simple Shader Demo
In this section we will examine this chapter’s demo, SimpleShader.
In this demo you will learn how to load an HLSL effect from a file
and how to apply it to a Model object. The demo can be found in the
companion files in the Chapter 11 folder.
In this demo the HLSL shader will perform a texture mapping
effect. The demo will show how to take a loaded texture from a
loaded Model object so that it can be sent to the custom HLSL
shader and sampled by the pixel shader, and how to attach the
loaded shader to the Model object instead of using the BasicEffect
class. Although the texture mapping effect is nothing fancy, it does
show you how to write a correct shader, bind parameters to a shader
file, and apply the shader to a Model object. This is important to
grasp when writing other effects.
414 Chapter 11
The demo starts by specifying objects for the object that will be
rendered, the decal texture, the necessary matrices, and the effect
shader. This demo builds directly off of the Textured3DModel demo
from Chapter 7. If you are following along with this demo, you can
use the Textured3DModel demo as a start. The global objects from
the SimpleShader demo can be seen in Listing 11-1.
Listing 11-1
public class SimpleShaderDemo : Microsoft.Xna.Framework.Game
GraphicsDeviceManager graphics;
Model texturedBox;
Texture boxDecal;
Effect textureMapEffect;
Matrix projectionMatrix, worldMatrix;
The LoadContent() function from the demo is where the first modi-
fications occur. The HLSL file, which is named textureMap.fx and is
in the Shaders folder, is loaded by the content pipeline into an Effect
object. The Effect class is the class you use to load HLSL shaders
into XNA. While HLSL shaders can have multiple techniques, the
current technique the Effect object uses is set to the only technique
that happens to appear in the shader; it is called TextureMap. When
accessing techniques of a loaded shader you can reference them by
index (techniques are ordered based on the sequence in which they
appear in the shader) or by name. The current technique is the tech
nique the Effect object is set to use when it is applied for rendering.
When the Model object is initially loaded, all textures specified in
the X file are also loaded and the object’s effects are instances of the
BasicEffect class, unless the model has a shader specified within it.
Since the texture is loaded by the content pipeline and since the
original model has no effect within the X file, we can obtain the tex
ture by accessing the mesh’s Effects field and using the Texture
property. Alternatively we can load the texture if no texture or a dif
ferent texture is specified in the X model. Keep in mind that if you
do load a texture that is specified in the Model object, you’ll have to
obtain it like we are doing in the LoadContent() function or you’ll
have to include the texture separately into the project, which will
cause XNA to display a warning that the same resource is being
processed multiple times.
Shaders and Custom Effects 415

Get Essential XNA Game Studio 2.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.