Sign in to follow this  
SteveHatcher

Confused about XMFLOAT2 and D3DXVECTOR2

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this