3D Programming: Directxmath

Source: Internet
Author: User

Directxmath

Directxmath Microsoft provides a mathematical library for graphics applications that supports SIMD, C + + forms of APIs. SIMD is the abbreviation for single instruction multiple data, which supports using only one CPU instruction and processing multiple data at the same time. For example, the SIMD instructions in the Windows platform can handle 4 32-bit floating-point numbers at the same time, resulting in significant performance improvements. For example, two vectors are added, each vector has 4 components, and usually requires 4 additions. Using SIMD, only one instruction is required to complete these 4 additions. Directxmath is published as part of the Windows SDK (chap. III, "Tools of the Trade", which will discuss the Windows SDK). To use the Directxmath library, simply include the DIRECTXMATH.H header file in the application, all the implementation files are already contained in the header file, and no other libraries need to be linked.

Vectors

Vectors is implemented by the Xmvector data type, Xmvector is a 16-bit aligned data type, corresponding to the SIMD registers. For best performance, all vector operations (such as addition and subtraction) are performed through Xmvector instances. However, Xmvector is a closed data type, so a single component in a vector cannot be accessed as a member of a struct. In addition, because this data type is 16-bit aligned, it is not suitable for storing class-type members. Instead, it is recommended that you use XMFLOAT2,XMFLOAT3, and XMFLOAT4 to store class types. Each of these three types supports access to two, three, and four internal components (X,Y,Z,W).

Loading and storing

In order to fully utilize the SIMD instructions, you need to copy the data from the xmfloat* instance to the Xmvector object. You can use the following load method:

Xmvector XMLoadFloat2 (const xmfloat2* psource);
Xmvector XMLOADFLOAT3 (const xmfloat3* psource);
Xmvector XMLoadFloat4 (const xmfloat4* psource);
Conversely, the following store method can be used to copy data from a Xmvector instance to a xmfloat* object:
void XMStoreFloat2 (xmfloat2* pdestination, Fxmvector V);
void XMStoreFloat3 (xmfloat3* pdestination, Fxmvector V);
void XMStoreFloat4 (xmfloat4* pdestination, Fxmvector V);

Therefore, the vector operation in the Directxmath, using the xmfloat* instance to store class type members, to use the SIMD vector operation on the data load into the Xmvector object, if you need to save the vector operation output results, The result of the Xmvector object is then returned to the Xmfloat* class member.

Attention:

Directxmath supports vectors of many other data types, including byte,int,short,half-float (16-bit floating-point numbers). They all follow the same exceptions and the way they are invoked. For example, the XMBYTE4 store 8-bit a signed number, and the corresponding load and store methods are XMLoadByte4 () and XMStoreByte4 ().

Calling conventions (calling convention)

Note the Fxmvector type in the parameter list in the store function. This is one of the four aliases of Xmvector, and the other three are gxmvector,hxmvector, and Cxmvector respectively. These aliases are primarily used to optimize the portability of the previous platforms for data layout. On the x86 and Xbox 860 platforms, these are xmvector types. For the x64 platform, is the Xmvector reference type. The rules for passing xmvector parameters are as follows:

Input parameters for the first three xmvector types use Fxmvector.

The input parameters of the fourth Xmvector type use Gxmvector.

The input parameters for the fifth and sixth Xmvector types use Hxmvector.

If there are additional xmvector type input parameters, use Cxmvector.

All output uses xmvector* or xmvector& type parameters.

More about the Directxmath calling convention can view online documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/hh437833 (v=vs.85). aspx.

Accessors and Mutators

Although the Xmvector object is closed, it is not necessary to read and write a single component without the store or load the object. You can use the following access functions:

Float Xmvectorgetx (xmvector V);
Float xmvectorgety (xmvector V);
Float Xmvectorgetz (xmvector V);
Float XMVECTORGETW (xmvector V);
void Xmvectorsetx (Xmvector V, float X);
void Xmvectorsety (Xmvector V, float Y);
void Xmvectorsetz (Xmvector V, float Z);
void Xmvectorsetw (Xmvector V, float W);

Initialization functions

There are a number of ways to initialize Xmvector objects. Table 2.2 lists some of the commonly used initialization functions.

Table 2.2 Common Directxmath Vector initialization functions

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.