SETTING SHADER CONSTANTS

One of the significant differences between DirectX 8 and DirectX 9 vertex shaders is that DirectX 8 has only a single floating point constant array, whereas DirectX 9 contains three different constant arrays: a floating point, an integer array, and a binary array. Shader constants are constant across set shader calls in DirectX 8. Since def statements in pixel shaders actually load the constants, loading a pixel shader with defs in it will override any constants set previously. In DirectX 9, only the defs become local and only persist for the life of the pixel shader. The previous value of the constant returns when the shader is unloaded. Constant definitions in DirectX 9 vertex shaders work just like they do for pixel shaders.

Setting DirectX 8 Vertex Shader Constants

DirectX 8 has only a single array of constants for vertex registers. If you want to set the constants in the shader you have loaded, then you'd make a call to SetShaderConstant() with the appropriate arguments, and the values you pass in will get loaded to the shader constant registers.

Note 

def statements in vertex shaders don't actually do anything except pass back some tokens that you have to insert manually into the shader token stream. Nobody does it this way. Use SetShaderConstant() instead.

The first example is the DirectX 8 way to set vertex constant registers. Note that a "register" is a float[4] value, thus you'll need to have values for the .xyzw elements of the register. If you want to set more than one serial register, you can create an array of the required size and use the initial element value plus the number of float[4] elements to set the constants. The following example demonstrates how to set both a single register and an array of registers.

Note 

It's a common mistake to forget to count each float[4] as a single register element.

 // DirectX 8! // Assume we have a valid D3D pointer float singleRegister[4] = {0.0f, 0.5f, 1.0f, 2.0f}; float arrayValue[4*20] - {. . .};    //set a single register    g_pD3DDevice->SetVertexShaderConstant(        5,              // the vertex constant register number        singleRegister, // beginning address        1 );            // the number of 4*float registers //set 20 serial register values g_pD3DDevice->SetVertexShaderConstant(     6, // the vertex constant register number     arrayValues, // beginning address     20 ); // the number of 4*float registers 

Setting DirectX 9 Vertex Shader Constants

In DirectX 9, there are three types of shader constants, and you have to use a unique call to set each type. Other than the new constant types, setting the constants in DirectX 9 is similar to setting them in a DirectX 8 shader—you provide the constant number to set, the initial array address, and the total number to set, and that's it. Instead of the DirectX 8 SetVertexShaderConstant() call, there are now three calls to set the integer, boolean, and float values, having added a letter I, B, and F suffix, respectively to indicate these new functions. Although the integer constants are composed of four-element registers (and thus are set to a count of four-elements), the boolean registers are single-element registers and are set to one element per count. Constant declarations made in a shader are only for the life of the shader.

The following example demonstrates how to set the integer, boolean, and float arrays for the DirectX 9 style vertex shader.

 // DirectX 9! // Assume we have a valid D3D pointer // and we have some varying arrays in varying sizes int integerArray[4*10] = {. . .}; BOOL boolArray[11] = {. . .}; //note - NOT AN ARRAY float floatArray[4*12] = {. . .};    //set 10 integer arrays    g_pD3DDevice->SetVertexShaderConstantI(        0,            // the vertex constant register number        integerArray, // beginning address        10 );         // the number of 4 element registers    //set 11 boolean values    g_pD3DDevice->SetVertexShaderConstantB(        0,           // the vertex constant register number        boolArray,   // beginning address        11 );        // the number of boolean registers    //set 12 float arrays    g_pD3DDevice->SetVertexShaderConstantF(        0,           // the vertex constant register number        floatArray,  // beginning address        12 );        // the number of 4 element registers 

Querying the Number of Vertex Shader Constant Registers

If you query the device capability bits member MaxVertexShaderConst, you can get the maximum number of floating point vertex shader registers that are available to you. This number is at least 96 and for VS 2.0 can be 256. There are no integer or boolean registers prior to VS 2.0. For VS 2.0, there are at least 16 single-element boolean registers and 16 four-element integer registers.

Setting Pixel Shader Constants

Setting DirectX 8 and 9 pixel shader constants is almost exactly the same as the vertex shader constants, except that the name of the function that you call is SetPixelShaderConstants().

 // Assume we have a valid D3D pointer float singleValue[4] = {0.0f,0.5f,1.0f,2.0f};    //set a single register    g_pD3DDevice->SetPixelShaderConstant(        2,             // the pixel constant register number        singleValue,   // beginning address        1 );           // the number of 4*float registers 

Unlike vertex shaders, the def instruction in pixel shaders works without any other effort. However, in DirectX 9, using a def statement produces a change only for the life of that shader.



Real-Time Shader Programming(c) Covering Directx 9. 0
Real-Time Shader Programming (The Morgan Kaufmann Series in Computer Graphics)
ISBN: 1558608532
EAN: 2147483647
Year: 2005
Pages: 104
Authors: Ron Fosner

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