Using HLSL


One of the nicest features of the DirectX 9 release was something called the effects system, which utilizes HLSL. You might have even noticed that the project you're working on now declares (and uses) a variable of type Effect. This is the major entry point to the effects system and the easiest way to use HLSL to write your programs.

The effects system is designed to integrate both the vertex and pixel shader programs along with the pipeline state to render your objects. Most times, you have a text file that you pass into the effects system, and that file is compiled into the correct shader code, either at runtime or at build time, depending on how you've set it up. The basic layout of one of these files is like this:

 <Variable Declarations> void SomeVertexFunction() { // Do vertex shader program } void SomePixelFunction() { // Do pixel shader program } technique RenderScene {   pass P0   {      VertexShader = compile vs_1_1 SomeVertexFunction();      PixelShader = compile ps_1_1 SomePixelFunction();   } } 

Don't try to put this code into your program; it's just pseudo-code so you can see the basic layout of the shader programs. You'll be writing your own shader programs in a short while, so don't get too anxious. The main thing for which you will use the effects system (at least in the short term) is to maintain and modify the preceding variables and then to compile and use the shader programs it allows you to access.

If you look at the code that already exists for this application, you'll notice that the effect variable is used for a number of things and that a file is already created for you to hold the shader code, KartRacers.fx. You can see the code it contains in Listing 20.1.

Listing 20.1. The Empty Shader File
 //----------------------------------------------------------------------------- // File: KartRacers.fx // // The effect file for the KartRacers sample. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- float appTime;  //App's time in seconds float4x4 worldMatrix;  // World matrix for object float4x4 worldViewProjection; // World * View * Projection matrix //----------------------------------------------------------------------------- // Techniques //----------------------------------------------------------------------------- technique RenderScene {     pass P0     {     } } 

As you can see here, this has the basic layout that the preceding example did, with the exception that there are no shader functions defined here. That's what you'll be doing later in this chapter. For now, the things to notice are where the effects variable is used within the program. Once in the OnCreateDevice method, the object is created within the resource cache:

 string path = Utility.FindMediaFile("KartRacers.fx"); effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device,     path, null, null, shaderFlags, null); 

Then later, within the OnFrameRender method, the values of the global variables are set:

 // Update the effect's variables.  Instead of using strings, it would // be more efficient to cache a handle to the parameter by calling // Effect.GetParameter effect.SetValue("worldViewProjection", camera.WorldMatrix *     camera.ViewMatrix * camera.ProjectionMatrix); effect.SetValue("worldMatrix", camera.WorldMatrix); effect.SetValue("appTime", (float)appTime); 

Because there is nothing going on in the current techniques, this approach is fine for now, but to use the programmable pipeline, you need to change this.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net