Microsoft DirectX 8.0 (C++)

Vertex Shader Declaration

The declaration portion of a vertex shader defines the static external interface of the shader. A vertex shader declaration includes the following information.

Declaration arrays are single-dimensional arrays of DWORDs composed of multiple tokens, each of which is one or more DWORDs. The single-DWORD token value 0xFFFFFFFF is a special token used to indicate the end of the declaration array. The single DWORD token value 0x00000000 is a padding token that is ignored during the declaration parsing. This pad token enables more complex in-place editing of declarations. Note that 0x00000000 is a valid value for DWORDs following the first DWORD for multiple word tokens.

The following code example is an example of writing declarators. It shows how to define a vertex using a simple structure.

struct Vertex
{
    D3DXVECTOR3 Position;
    D3DXVECTOR3 Normal;
    D3DCOLOR    Diffuse;
    D3DXVECTOR2 TexCoord0;
};

Now consider the following code example, which shows how to define the same vertex structure above as a declarator.

DWORD dwDecl[] =
{
    D3DVSD_STREAM( 0 ),
    D3DVSD_REG( D3DVSDE_POSITION,  D3DVSDT_FLOAT3 ),
    D3DVSD_REG( D3DVSDE_NORMAL,    D3DVSDT_FLOAT3 ),
    D3DVSD_REG( D3DVSDE_DIFFUSE,   D3DVSDT_D3DCOLOR ),
    D3DVSD_REG( D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2 ),
    D3DVSD_END()
};

DWORD dwFvf = D3DFVF_POSITION | D3DFVF_NORMAL | D3DFVF_DIFFUSE |
              D3DFVF_TEX0 | D3DFVF_TEXCOORDSIZE2(0);

The declarator sets data stream 0, defines a position and normal vector composed of three float values, defines a diffuse component composed of a four component unsigned byte, and defines a pair of texture coordinates composed of two float values.

When you use the fixed-function vertex shader, the mapping of the vertex input registers is fixed so that specific vertex elements, such as position or normal, must be placed in specific register locations in the vertex input memory. These assignments are made automatically when passing an FVF code to IDirect3DDevice8::SetVertexShader. When you use an explicit shader declaration, the D3DVSDE preprocessor macros define the vertex input location into which specific elements must be loaded. See D3d8types.h for a definition of the macros used to generate the declaration token array.

For information on the shader declaration token types and bit definitions, see Vertex Shader Declarator Macros.

Notes  When you use fixed-function vertex shaders, the vertex stride set in IDirect3DDevice8::SetStreamSource and vertex size must be the same size. For vertex shaders that use declarations, the stream stride can be greater than the declaration stride.