Vertex shader (2)

Source: Internet
Author: User

Only one vertex shader is active at a time. You can have multiple vertex shader, if an object has a special transform or light, you can choose the appropriate vertex shader to accomplish this task.

You may want to use vertex shaders on each object or on each grid, for example, 10 meshes, you can use 10 different vertex shader, but that might harm your game's run.

Each vertex Shader-driven program must run through the following steps:

1. Check to see if your device supports vertex shader. (no longer needed after direct X9)

2. Use the D3DVSD_* macro to declare the vertex shader for vertex buffering (vertex buffer streams) mapping (map) to the input register.

3. Sets the constant register of the vertex shader with SetVertexShaderConstant ().

4. Compile the pre-written vertex shader with d3dxassembleshader* ().

5. Creates a vertex shader handle with CreateVertexShader ().

6. Use SetVertexShader () to set the vertex shader for an object that you want to effect.

7. Delete the vertex shader with DeleteVertexShader ().

Since the first step is not needed after DirectX 9, it is omitted here.

The following implementations correspond to the above steps.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------

2. Declares a vertex shader.

It must be declared before a vertex shader is used. This declaration can be referred to as a static external interface.

1 floatc[4] = {0.0f,0.5f,1.0f,2.0f};2DWORD dwdesc10[] = {3D3dvsd_stream (0),4D3dvsd_reg (0, D3DVSDT_FLOAT3),//Input Register V05D3dvsd_reg (5, D3dvsdt_d3dcolor),//Input Register v56 7 //set a few constants8D3dvsd_const (0,1),9* (dword*) &c[0],Ten* (dword*) &c[1], One* (dword*) &c[2], A* (dword*) &c[3], - d3dvsd_end () -};

This vertex shader declaration sets data flow 0 with D3dvsd_stream (0). After that, by using this Declaration, SetStreamSource () binds a vertex buffer to the device data stream.

You must declare which input vertex property or input vertex data is mapped to which entry register. D3dvsd_reg binds a single vertex register to a vertex element from the vertex stream. In our example of this declaration, the D3DVSD_FLOAT3 value should be placed in the first input register, and the D3dvsd_d3dcolor color value should be placed in the sixth input register. For example, the location data can be processed by input Register 0 (V0) with D3dvsd_reg (0,D3DVSDT_FLOAT3), and the normal data can be processed by the input Register 3 (v3) with D3dvsd_reg (3,D3DVSDT_FLOAT3).

Note that unless you want to use n-patches, which requires positional data in V0, the normal data is in V3, otherwise the developer can freely define the mapping of the Register to which the vertex data is entered.

In contrast, in a fixed pipeline, the mapping of vertex data input to a specific register is fixed.

D3dvsd_const loads a constant value into the constant register of the vertex shader. The first parameter is the starting position where the constant array begins to populate the data, and the possible range of values is 0-95 (in the case of the RADEON 8500, from 0-191.) we start at 0. The second parameter is the number of constant vectors to load, and if you want to load a 4x4 matrix (that is, 4 vectors), you can load it as follows:

1 floatc[ -] = (0.0f,0.5f,1.0f,2.0f,2 0.0f,0.5f,1.0f,2.0f,3 0.0f,0.5f,1.0f,2.0f,4 0.0f,0.5f,1.0f,2.0f);5D3dvsd_const (0,4), * (dword*) &c[0],* (dword*) &c[1],* (dword*) &c[2],* (dword*) &c[3],6* (dword*) &c[4],* (dword*) &c[5],* (dword*) &c[6],* (dword*) &c[7],7* (dword*) &c[8],* (dword*) &c[9],* (dword*) &c[Ten],* (dword*) &c[ One],8* (dword*) &c[ A],* (dword*) &c[ -],* (dword*) &c[ -],* (dword*) &c[ the],

D3dvsd_end generates an end that flags the end of the vertex shader.

1 d3dvsd_end ()

3. SetVertexShaderConstant () Set vertex shader constant register

You can populate the vertex shader constant register with SetVertexShaderConstant () and get the values in these registers with GetVertexShaderConstant ().

SetVertexShaderConstant () declares the following:

void* pconstantdata,dword constantcount);

Set vertex shader constants :

M_pd3ddevice->setvertexshaderconstant (0, &vzero,1); M_pd3ddevice->setvertexshaderconstant (1, &vone,1); M_pd3ddevice->setvertexshaderconstant (2, &vweight,1); M_pd3ddevice->setvertexshaderconstant (4, &mattranspose,4); M_pd3ddevice->setvertexshaderconstant (8, &matcameratranspose,4); M_pd3ddevice->setvertexshaderconstant ( A, &matviewtranspose,4); M_pd3ddevice->setvertexshaderconstant ( -, &flight,1); M_pd3ddevice->setvertexshaderconstant ( +, &fdiffuse,1); M_pd3ddevice->setvertexshaderconstant ( A, &fambient,1); M_pd3ddevice->setvertexshaderconstant ( at, &ffog,1); M_pd3ddevice->setvertexshaderconstant ( -, &fcaustics,1); M_pd3ddevice->setvertexshaderconstant ( -, &matprojtranspose,4);
As mentioned earlier, there are at least 96 constant registers that can be populated. SetVertexShaderConstant () The first parameter is the index of the Register, and the last parameter is the number of constants. For example, Matviewtranspose will be loaded into
Register 12, load number is 4, register 16-19 is not used, flight is loaded into register 20, load number is 1.


so , what is the difference between D3dvsd_const and SetVertexShaderConstant () used in shader declarations?
D3dvsd_const can only be used only once, and SetVertexShaderConstant () may be called before each drawprimitive* ().

Cond.....

Vertex shader (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.