Game Development BASICS (17th)

Source: Internet
Author: User

Chapter 2

Vertex shader is a program running on the graphics card GPU. It can replace the transformation and illumination steps in the fixed functional assembly line (of course, not absolute, when the hardware Department supports the vertex coloring tool, direct3d uses a software algorithm to simulate the vertex coloring tool ).

The Vertex coloring tool is actually a custom program written in HLSL language. In this way, great flexibility is achieved in the graphic effects that can be achieved. For example, with the help of the vertex coloring tool, you can use any illumination algorithm that can be implemented in the vertex coloring tool.

The use of vertex data structures is more flexible, and the vertex structure in the programmable pipeline can contain more data than the fixed functional pipeline

Vertex paintors are a relatively new feature that is not currently supported by many graphics cards, especially those newer vertex paintors released by DirectX 9.0, check vertexshaderversion, a member of the d3dcaps9 structure, and compare it with the macro d3dvs_version to check whether the graphics card supports a certain Vertex coloring er version.

Detection process:

// If the device's supported version is less than Version 2.0
If (caps. vertexshaderversion <d3dvs_version () // The primary and secondary versions
// Then vertex shader version 2.0 is not supported on this device
Currently, the d3dxcompileshaderfromfile function supports the following vertices: 1.1, 2.0, and 3.0.

Vertex declaration:
In a programmable assembly line, vertex structures can even contain data that exceeds the fvf description capability. Therefore, vertex declaration is generally used with more powerful and more powerful features)

In a programmable assembly line, if the vertex structure can be described by fvf, it can still be used. However, this is for convenience. In fact, in a programmable assembly line, fvf is finally converted to a vertex declaration.

The vertex declaration description is a d3dvertexelement9 structure array. Each element in the structure array describes a component of the vertex structure. Therefore, if the vertex structure has three components (such as location, regular vector, color), the corresponding vertex declaration level can be described by a structure array of the d3dvertexelement9 type with a dimension of 3.

Typedef struct d3dvertexelement9
{
Word stream; // specifies the data stream associated with the vertex component
Word offset; // The Byte offset from the vertex data start point to the data related to a specific data type.
Byte type; // specifies the data type. This parameter can be used to enumerate any member of the d3ddecltype, d3ddecltype_float1 floating point type, and d3ddecltype_float2 floating point 2D vector, d3ddecltype_float3 floating point type 3D vector, d3ddecltype_float4 floating point type 4D vector, d3ddecltype_d3dcolor A d3dcolor type extended to rgba floating point type color vector (R, G, B,, the color // each component of the vector is normalized to the interval [0, 1 ].
Byte method; // specify the gridded (tesselation) method. This chapter uses d3ddeclmethod_default by default to specify
Byte usage; // specifies the purpose of the vertex component. For example, if a component is used as a location vector, a normal vector, or a texture vector, the identifier is taken from the enumerated class/type d3ddeclusahe;
Byte usageindex; // identifies multiple vertex components with the same usage. Usage index is an integer within the range []. For example, if the usage of three vertex components is d3ddeclusage_normal, you can specify the usage indexes of these three vertex components as //, 2 in order. In this way, you can use indexes to represent each specific normal vector.
} D3dvertexelement9, * lpd3dvertexelement9;

Typedef Enum d3ddeclusage
{
D3ddeclusage_position = 0, // If a vertex Declaration has this identifier, it indicates that the vertex has been transformed, and instruct the graphics card not to // route the vertex to the vertex processing link (transformation and illumination processing)
D3ddeclusage_blendweight = 1,
D3ddeclusage_blendindices = 2,
D3ddeclusage_normal = 3,
D3ddeclusage_psize = 4, // used to specify the vertex size of a vertex. This type is mainly used for point Sprite, so that each size is // controlled.
D3ddeclusage_texcoord = 5,

D3ddeclusage_tangent = 6,
D3ddeclusage_binormal = 7,
D3ddeclusage_tessfactor = 8,
D3ddeclusage_positiont = 9,
D3ddeclusage_color = 10,
D3ddeclusage_fog = 11,
D3ddeclusage_depth = 12,
D3ddeclusage_sample = 13
} D3ddeclusage, * lpd3declusage;

In this way, you can use indexes to represent each specific normal vector.

For example, if the vertex format to be described contains a location vector and three normal vectors, the corresponding vertex declaration can be specified
D3dvertexelement9 Decl {} =
{
{0, 0, d3ddecltype_float3, d3ddeclmethod_default, d3ddeclusage_position, 0 },
{0, 12, d3ddecltype_float3, d3ddeclmethod_default, d3ddeclusage_normal, 0 },
{0, 24, d3ddecltype_float3, d3ddeclmethod_default, d3ddeclusage_normal, 1 },
{0, 36, d3ddecltype_float3, d3ddeclmethod_default, d3ddeclusage_normal, 2 },
D3ddecl_end () // initialize the last vertex element in the d3dvertexelement9 Array
};

Creation of vertex Declaration: describes a vertex declaration as an array of the d3dvertexelement9 type. You can use the following method to obtain the idirect3dvertexdeclaration9 pointer pointing to the interface:
Hresult createvertexdeclaration (
Const d3dvertexelement9 * pvertexelements, // point to a d3dvertexelement9 type structure array // describes the vertex declaration to be created
Idirect3dvertexdeclaration9 ** ppdecl // return the port pointer pointing to the created idirect3dvertexdeclaration9. //
);

Example:
Idirect3dvertexdeclaration9 * _ Decl = 0;
HR = _ device-> createvertexdeclaration (Decl, & _ Decl );

Flexible vertex format is a very useful feature. It will be converted into vertex declaration within the programmable pipeline. Therefore, when using Vertex declaration directly, you do not need to call:
Device-> setfvf (fvf );
But just call:
Device-> setvertexdeclaration (_ Decl); // _ Decl is a pointer to the idirect3dvertexdeclaration interface.

You need to define a ing between the elements in the vertex Declaration and the data members in the input structure of the vertex shader. In the input structure, you can specify a semantics for each data member (: usage-type [usage-Index]) to define this ing. This Semantics identifies each element in the vertex Declaration through the usage type and usage index, the vertex element identified by the semantics of the data member is mapped to the element of the data member.
For example, the input structure used for the vertex declaration is as follows:
Struct vs_input
{
Vector position: position;
Vector normal: normal0;
Vector facenormal1: normal1;
Vector facenormal2: normal2;
};

If the usage index is omitted, it means that the index is 0. For example, position has the same meaning as position0.

The vertex shader supports the following input operations:
# Position [N]
# Blendweights [N] blend Weights)
# Blendindices [N] integrated index (blend indices)
# Normal [N] Normal Vector
# Psize [N] vertex size
# Diffuse [N] diffuse color
# Specular [N] highlight color (specular color)
# Texcoord [N] texture coordinates
# Tangent [N] Tangent Vector)
# Binormal [N] binormal Vector)
# Tessfactor [N] tessellation factor)
N is an optional integer, but must be taken from the range [0, 15]

For the output structure, the purpose of each member must be specified. For example, the data member should be regarded as a location vector, color vector, or texture coordinate. The graphic card is unaware of the purpose of each data member, you need to specify the display, which is also implemented at the semantic level, for example:
Struct vs_output
{
Vector position: position;
Vector diffuse: color0;
Vector specular: color1;
};

The output usages supported by the vertex shader include:
# Position [N]
# Psize [N] vertex size
# Fog [N] Fog fusion value (fog blend value)
# Color [N] vertex color. Note that multiple vertex colors can be output. These colors are combined to generate the final color.
# Texcoord [N] vertex texture coordinates. Note that multiple vertex texture coordinates may be output.
N is an optional integer, but must be taken from the range [0, 15]

To use the vertex shader:
1. Write and compile the vertex coloring tool.
2. Create an idirect3dvertexshader9 interface object to represent the vertex shader Based on the compiled shader code
3. Use the idirect3ddevice9: setvertexshader method to enable Vertex coloring.

After the vertex shader is used, it must be destroyed.

This method obtains the idirect3dvertexshader9 interface pointer, which represents a vertex shader.
Hresult idirect3ddevice9: createvertexshader (
Const DWORD * pfunction, // point to the compiled code pointer,
Idirect3dvertexshader9 ** ppshader // pointer to the idirect3dvertexshader Interface
);

For example, if the shader variable is a pointer to the id3dxbuffer interface, the pointer to the idirect3dvertexshader9 interface is obtained:
Idirect3dvertexshader9 * toonshader = 0;
HR = device-> createvertexshader (
(DWORD *) shader-> getbufferpointer (),
& Toonshader );

The d3dxcompileshaderfromfile function can return compiled shader code (corresponding to the shader above)

This method enables Vertex coloring er
Hresult idirect3ddevice9: setvertexshader (idirect3dvertexshader9 * pshader );
This method receives a single parameter. You can assign a pointer to the vertex shader you want to enable to this parameter.
Example:
Device-> setvertexshader (toonshader );

After idirect3dvertexshader9 is used, you must call its own release to release resources.
Example:
D3d: Release <idirect3dvertexshader9 *> (toonshader );

Related Article

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.