Confused about XMFLOAT2 and D3DXVECTOR2

Started by
4 comments, last by Brain 9 years, 2 months ago

Hi,

In some DirectX 9 code I see variables such as velocity are stored in a D3DXVECTOR2.

In DirectX 11 I see DirectXMath should be used, and their version of a D3DXVECTOR2 is an XMFLOAT2.

What I am confused about is the D3DXVECTOR2 has lots of overloaded operators, where as XMFLOAT2 only has an = operator. I need to add two XMFLOAT2 variables together using +=.

From my searching it seems if you want to do something with an XMFLOAT2 you first need to put it into an XMVECTOR, do the operation, and then put it back into an XMFLOAT2.

I am wondering why this is the way?

Secondly, can I just create my own XMFLOAT2 (myXMFLOAT2) from the existing XMFLOAT2 code in DIrectXMath, but add my own overloaded operators. Is there a noticeable performance difference using one or the other?

Thanks

Advertisement

The reason behind this change is that the DirectMath, XM* stuff, supports SIMD instructions which allow you to add 2 vector4 together in one operation. SIMD liberaries are constructed in such a way that they keep all of the data in XMM registers on the CPU. This means that the operations like +, -, * and / are implemented as standalone functions that return a XMVector and takes two XMVector arguments.

https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.arithmetic.xmvectoradd%28v=vs.85%29.aspx

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

As I remember there are a lot of functions starting with XM* to manipulate XMFLOAT* (and XMVECTOR*, XMMATRIX*). Pay attention at the use of the Load/Store functions, I remember some headaches because of not using them properly.

Take this while waiting for a more expert answer smile.png

EDIT: Ninja ph34r.png

Arka is right. You can use XMFLOAT types for example as class members, then in your (member) functions with local scope you load them into XMVECTOR type, apply stuff with the XM functions and then store them back into the XMFLOAT. That was using the instructions/ advantages Nightcreature mentioned. Same goes for XMFLOAT3, 4, 4x4 (matrix) etc

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Thanks guys, that clears up much of the confusion I am having. Especially about the Load and Store...

I also just found the DirectX TK's SimpleMath::Vector2 function which has some overloaded operators, so I will use that for now and worry about performance later.

Also watch out for alignment on the XM* types.

XMVECTOR and XMMATRIX etc must be 16-byte aligned. When you place the value into the stack, this is handled for you by the compiler, but if you allocate a struct or class containing an XMVECTOR or XMMATRIX then the alignment isn't done for you, and any attempt to access a mis-aligned XM* struct will cause an access violation and break.

You should probably use XMFLOAT4 etc to store the actual values in your classes and structures, and only load the XMMATRIX or XMVECTOR when neccessary within a methor or function.

This topic is closed to new replies.

Advertisement