UWP development details: DirectX: XMMATRIX, uwpxmmatrix

Source: Internet
Author: User

UWP development details: DirectX: XMMATRIX, uwpxmmatrix

The code written in the past two days may crash in the XMMatrixMultiply () function. XMMatrixMultiply () itself is an inline function and you can see the code at the crash:

vX = _mm_mul_ps(vX,M2.r[0]);

It is found that the _ mm_mul_ps Command requires 16-byte alignment of the memory address. The conjecture may be caused by non-alignment of bytes. print out the address of the matrix parameter. It is true that 16 bytes are not alignment in the case of a crash.

The XMMATRIX declaration specifies 16 bytes of alignment, as shown below:

__declspec(align(16)) struct XMMATRIX {    ...};

However, the results obviously fail. We can continue to find that the XMMATRIX variable is defined as a class member variable, and the instance of this class is new:

Class MyClass {XMMATRIX matrix _;}; MyClass * ptr = new MyClass (); // there should be a compilation warning

The new operator only ensures that the alignof (std: max_align_t) alignof is aligned to 8 bytes. As a result, the member variable matrix _ can only be aligned to 8 bytes. Solution: Assign the member variable martix _ to a local variable, and then call the XMMatrixMultiply () function through the local variable. Directly Using new will have a warning message that the object may not be aligned to 16 bytes, but because I use std: make_shared to directly generate a smart pointer, the compiler failed to give this warning-this is also a pitfall that eventually caused the previous crash.

Note the XMMatrixMultiply () Declaration. The first parameter is the value passing parameter, and the second parameter is the reference passing parameter. The problem is that the reference passing parameter:

1 typedef const XMMATRIX FXMMATRIX2 typedef const XMMATRIX & CXMMATRIX; 3 4 inline XMMATRIX XM_CALLCONV XMMatrixMultiply (FXMMATRIX M1, CXMMATRIX M2); // note that the second parameter is a reference parameter passing

The description of the XMMatrixMultiply function in MSDN is inconsistent with the actual code:

XMMATRIX XMMatrixMultiply(  [in] XMMATRIX M1,  [in] XMMATRIX M2);

If the two parameters are both value-passing parameters, there is no byte alignment problem.

Conclusion:The XMMATRIX struct requires 16-byte alignment and can only be used as a local or global variable. Allocating memory in a static storage area or stack ensures that align (16) statements are valid, never use new to allocate heap memory or use it as a class member variable. If you need to save the matrix in the member variable, you can use DirectX: XMFLOAT4X4 to convert it to XMMATRIX.

 

By the way, it is well-deserved that MSDN was previously called the best-written development document. The newly added documents (such as the UWP and DX11 documents) in the last two years are obviously perfunctory. The function description often goes through a sentence. There is no difference between reading the documents and directly declaring and guessing functions, this time, the function declaration is inconsistent.

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.