Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Use vertex shader.doc
Скачиваний:
23
Добавлен:
02.05.2014
Размер:
252.93 Кб
Скачать
  • Vertexshader0vertex

  • VERTEXSHADER1VERTEX

  • VERTEXSHADER2VERTEX

  • VERTEXSHADER3VERTEX

They will correspond to four vertex shaders. The vertex definitions used here included a structure, an FVF code, and initialized vertices.

Shader Declarations

The declaration portion of a vertex shader defines the static external interface of the shader. For our purposes, a vertex shader declaration includes the following information:

  • Loading data in the constant memory at the time that a shader is set as the current shader.

  • Binding stream data to vertex shader input registers.

Each constant token specifies values for one or more contiguous 4-DWORDconstant registers. This enables the shader to update an arbitrary subset of the constant memory, overwriting the device state, which contains the current values of the constant memory. These values can be overwritten betweenIDirect3DDevice8::DrawPrimitivecalls when a specific shader is bound to a device by invoking theIDirect3DDevice8::SetVertexShaderConstant method. We will use this procedure for simple constants used by the shaders.

The stream binding information defines the type and vertex input register assignment of each element in each data stream. The type specifies the arithmetic data type and the dimensionality—one, two, three, or four values. Stream data elements that are less than four values are always expanded to four values with zero or more 0.0f values and one 1.0f value. For clarity, we'll have only one stream at this time.

Since this sample has four shaders, it must contain four shader declarations and they are included below:

//shader decl

float c[4] = {0.0f,0.5f,1.0f,2.0f};

DWORD dwDecl0[] =

{

D3DVSD_STREAM(0),

D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ), //D3DVSDE_POSITION,0

D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ), //D3DVSDE_DIFFUSE, 5

D3DVSD_CONST(0,1),*(DWORD*)&c[0],*(DWORD*)&c[1],*(DWORD*)&c[2],*(DWORD*)&c[3],

D3DVSD_END()

};

DWORD dwDecl1[] =

{

D3DVSD_STREAM(0),

D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ), //D3DVSDE_POSITION,0

D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ), //D3DVSDE_DIFFUSE, 5

D3DVSD_CONST(0,1),*(DWORD*)&c[0],*(DWORD*)&c[1],*(DWORD*)&c[2],*(DWORD*)&c[3],

D3DVSD_END()

};

DWORD dwDecl2[] =

{

D3DVSD_STREAM(0),

D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ), //D3DVSDE_POSITION, 0

D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ), //D3DVSDE_DIFFUSE, 5

D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2 ), //D3DVSDE_TEXCOORD0, 7

D3DVSD_CONST(0,1),*(DWORD*)&c[0],*(DWORD*)&c[1],*(DWORD*)&c[2],*(DWORD*)&c[3],

D3DVSD_END()

};

DWORD dwDecl3[] =

{

D3DVSD_CONST(0,1),*(DWORD*)&c[0],*(DWORD*)&c[1],*(DWORD*)&c[2],*(DWORD*)&c[3],

D3DVSD_STREAM(0),

D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ), //D3DVSDE_POSITION, 0

D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3 ), //D3DVSDE_NORMAL, 3

D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ), //D3DVSDE_DIFFUSE, 5

D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2 ), //D3DVSDE_TEXCOORD0, 7

D3DVSD_CONST(0,1),*(DWORD*)&c[0],*(DWORD*)&c[1],*(DWORD*)&c[2],*(DWORD*)&c[3],

D3DVSD_END()

};

All of the shader declarations we use here define constants. All of the shaders we use here set the shader to use data stream 0. When you use an explicit shader declaration, as is the case here, the D3DVSD_REGpreprocessor macros define the vertex register to vertex component mapping. In addition, the predefinedD3DVSDE_ macros are used to specify the registers since we are using FVF vertex buffers.

The first two shader declarations, dwDecl0anddwDecl1, then define position and diffuse color in register 0 and register 7 using theD3DVSD_REGmacro and explicit register assignment. Note how this matches theVERTEXSHADER0VERTEX, D3DFVF_VERTEXSHADER0VERTEX andVERTEXSHADER1VERTEX, D3DFVF_VERTEXSHADER1VERTEX vertex definitions for theg_VertexShaderVertices0 andg_VertexShaderVertices1 vertex initializations.

The next two shader declarations, dwDecl2anddwDecl3, are a bit more complex. ThedwDecl2declaration defines position, diffuse, and texture coordinates. This indicates we will be performing a texturing vertex shader. Note how this matches theVERTEXSHADER2VERTEX, D3DFVF_VERTEXSHADER2VERTEX vertex definition and theg_VertexShaderVertices2 vertex initialization. Finally, thedwDecl3declaration defines position, normal, diffuse, and texture coordinates. This indicates we will be performing a texturing vertex shader with lighting. Note how this matches theVERTEXSHADER3VERTEX, D3DFVF_VERTEXSHADER3VERTEX vertex definition and theg_VertexShaderVertices3 vertex initialization.

While we perform lighting in this final vertex shader, in the future most interesting lighting tasks will be performed in pixel shaders. Still, certain global, standard lighting operations (like ambient) could be performed in vertex shaders for efficiency.