Game Development Reference
In-Depth Information
You can see that this function contains two blocks of code that are quite similar to
each other. The first one initializes our vertex shader. The downloadable code con-
tains a file called
Effects.fx
, which is simply a text file containing the code of our
basic shaders.
The first line creates a string variable named
vsCompileError
. This will receive
any errors that are raised by the next piece of code. As you can see, it is a
using
block that calls the
ShaderBytecode.CompileFromFile()
method to compile
our vertex shader. The returned byte code is the compiled form of our vertex shader.
The
CreateFromFile()
method takes a handful of parameters, and it also has
several overloads. An
overload
method is another version of the same function
with a different parameter list.
The first parameter of the
CompileFromFile()
method is the file containing the
code of the shader we want to compile. The second parameter is the name of the
method in the shader file that contains the code for this shader. The third paramet-
er is the shader model. In this case we used
"vs_4_0"
, which tells it that we want
to use shader model 4. The fourth parameter is the shader flags we use. We used
ShaderFlags.Debug
here. Again, you'd probably want to remove this flag when
your game is done since the debug code will slow down performance. The next two
parameters are a list of shader macros to define during shader compilation and an
interface for handling the
include
files. These two parameters are set to
null
as
they are beyond the scope of this chapter. And the final parameter is the
psCom-
pileError
variable we created above the
using
block. If there are any errors, they
will be put in this variable.
Inside of this
using
block we have two lines of code. The first gets the signature for
this shader. Remember that the signature of a shader is just a list of the input and/or
output parameters of that shader. The second line of code creates a
VertexShader
object to hold our vertex shader, and stores it in our
m_VertexShader
variable.
The second block of code in our
InitShaders()
method is very similar to the first
one. It does the same stuff but for the pixel shader. It compiles our pixel shader and
stores it in our
m_PixelShader
member variable. You may have noticed that it uses
the same shader file as the vertex shader code at the top of this method. You can
define multiple shaders in a single file, and we've done that here for simplicity.