Game Development Reference
In-Depth Information
Using the HLSL compiler, we can compile our code to any available
shader version. Using the assembly language, we would have to
port the code for each desired version.
HLSL is also very similar to C and C++ syntax, thus there is a very
short learning curve.
Finally, you will need to switch to the REF device for the shader
samples if your graphics card does not support vertex and pixel
shaders. Using the REF device means the shader samples run very
slowly, but they still display the correct results, allowing us to verify
that our code is correct.
Note: Vertex shaders can be emulated in software with software
vertex processing — D3DCREATE_SOFTWARE_VERTEX-
PROCESSING .
Objectives
To learn how to write and compile an HLSL shader program
To learn how to communicate data from the application to the
shader program
To become familiar with the syntax, types, and built-in functions of
HLSL
16.1 Writing an HLSL Shader
We can write the code to our HLSL shaders directly into our application
source files as a long character string. However, it is more convenient
and modular to separate the shader code from the application code. For
this reason, we write our shaders in Notepad and save them as regular
ASCII text files. Then we use the D3DXCompileShaderFromFile
function (section 16.2.2) to compile our shaders.
As an introduction, the following is a simple vertex shader written
in HLSL that was saved to a text file generated in Notepad called
Transform.txt. The complete project can be found in the companion
files under the title Transform. This vertex shader transforms the ver-
tices by a combined view and projection matrix and sets the diffuse
color component of the vertex to blue.
Note: This sample uses a vertex shader as an example, but do not
worry about what a vertex shader is supposed to do yet, as they are
covered in the next chapter. For now, the objective is to familiarize
yourself with the syntax and format of an HLSL program.
Search WWH ::




Custom Search