Jump to content

  • Log In with Google      Sign In   
  • Create Account

DirectXMath Calling Convention


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 panicq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 29 July 2014 - 06:46 AM

Hello,

 

I'm new to directX and I'm reading a book where the author talks about "Calling conventions", and it's quite obscure to me even with the doc. Could you explain to me why in functions like

 

void XMStoreFloat2(XMFLOAT* dest, FXMVECTOR V);

 

...we don't pass the XMVECTOR as a reference or a pointer ? And why are we using alias like FXMVECTOR, GXMVECTOR, or CXMMATRIX  ? And why is there an order of "input parameters", and why we always need to return a pointer and a reference of XMVECTOR ?

 

I understood that there is a link with the "Single Instruction Multiple Datas" way of computing instructions in the CPU but I dont cleary understand what the link is.

 

Thanks in advance.

 

 

 



Sponsor:

#2 cozzie   Members   -  Reputation: 1762

Like
0Likes
Like

Posted 29 July 2014 - 07:54 AM

Hi.

DirectXMath offers lots of opportunities in efficiently using SIMD instructions and different sorts of intrinsics.

It depends a bit on your skills/ knowledge how much of this you want to use, manual memory alignments etc. (__M128).

 

But.. I am however, not that skilled to user everything, so here's a way you can use it without too much hassle and with a good balance in needed time/ gained effort:

 

- for all vector and matrix members of classes, I use XMFLOAT (2/3/4X4 etc.), so you don't need to do manual memory alignment

- in all functions with their own scope, including member functions, I use XMVECTOR or XMMATRIX

- using the XMStoreFloat and XMLoadFloat functions, you can easily read out your 'members' and 'save' them back.

This way for the calculations you will get the avantage of the intrinsics, memory efficiency etc.

 

Two examples:

void CD3dmeshInst::Transform(const DirectX::XMFLOAT4X4 &pMatrix)
{
	XMMATRIX xmWorldOld = XMLoadFloat4x4(&mMatWorld);
	XMMATRIX xmTransform = XMLoadFloat4x4(&pMatrix);

	XMMATRIX xmWorldNew = XMMatrixMultiply(xmWorldOld, xmTransform);
	XMStoreFloat4x4(&mMatWorld, xmWorldNew);

	mTransformed = true;
}
// transforming a vector with a matrix

	XMVECTOR xmCenterOld = XMLoadFloat3(&mSphereCenterModel);
	XMMATRIX xmMatWorld = XMLoadFloat4x4(&mMatWorld);
	XMVECTOR xmCenterNew = XMVector3TransformCoord(xmCenterOld, xmMatWorld);
	XMStoreFloat3(&mBoundingSphere.WorldCenter, xmCenterNew);

Good luck.



#3 imoogiBG   Members   -  Reputation: 1247

Like
0Likes
Like

Posted 29 July 2014 - 09:13 AM

The FXMVECTOR,CXMVECTOR ect existance is explined by different function call types. there is one specal function call called __vectorcall. The special thing about it is that first 3(if i remember correcltly) arguments of the function are passed via xmm registers. however this is not avaiable for all platforms(for example when compiling on ARM ach).

 

EDIT: I forgot..... Passing __m128 (aka. XMVECTOR) has some special rules. For example reference arg wont work for __vectorcall functions. Correct me if Im wrong.

Personally I love the D3D API, but I detest DirectXMath (in terms of usage). It won't give you any performance just by using it. The library will only add complexity to your code. The real performance gains are when you oprate on a big chunk of coninious data that is highly vertor friendly(most operations are not vector friendly). Try to avoid that library if possible, use it only for really really *slow/frequent* and vertor friendly code, don't use it as a general 
purpose math library.

 

Why you should not use it as a general purpouse math lib:
 

1. The default alignment and the new operator(new operator is not aware of the alignment)

2. Becase of 1 you have to implement new by yourself for every aligned structure(ak. that has XMVECTOR/MATRIX as a member)

3. Additional structures are introduced for math computation and data storage.

 

you can use glm (just add your own perspective/view matrices) or watever else you can find. Im currently writing a math library but it is not ready yet.

http://msdn.microsoft.com/en-us/library/dn375768.aspx


Edited by imoogiBG, 29 July 2014 - 09:39 AM.


#4 panicq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 29 July 2014 - 09:28 AM

Thx for your answers,

 

But, it is still not as easy to understand for me. You told me that FXMVECTOR call a __vectorcall function, but FXMVECTOR is a typedef of XMVECTOR wich is a datatype, so when it calls a function ? huh.png . What I really dont understand is that XMVECTOR is a 16 bytes chunk of memory, which represent a mathematical vector, so why should we use FXMVECTOR on an other platform, and WHY (beacause it's heavy) we don't pass it as a reference in many functions such as:

 

XMVECTOR XMVector3Lenght(FMXVECTOR V);

 

I'm lost. And if I'm using DirectXMath is to advance in my book (Real-Time 3D Rendering with directX and HLSL) happy.png

 

Thx


Edited by panicq, 29 July 2014 - 09:29 AM.


#5 imoogiBG   Members   -  Reputation: 1247

Like
1Likes
Like

Posted 29 July 2014 - 09:36 AM

XMVECTOR DOES NOT represent a mathmeatical vector (it is a coincidence that is could be used to represent a 4D vector).
When you call 
XMVector3Lenght for example, the functions will assume that its argument is 3D vector(in terms of mathematics). In terms of program it is a special SIMD type.
 

FXMVECTOR is a typedef of XMVECTOR

On some platform. Open DIrectXMath.h and see for yourself

// Fix-up for (1st-3rd) XMVECTOR parameters that are pass-in-register for x86, ARM, and Xbox 360; by reference otherwise
#if ( defined(_M_IX86) || defined(_M_ARM) || defined(_XM_VMX128_INTRINSICS_) ) && !defined(_XM_NO_INTRINSICS_)
typedef const XMVECTOR FXMVECTOR;
#else
typedef const XMVECTOR& FXMVECTOR;
#endif

On platform A we should use XMVECTOR& because this is how its SIMD workflow works.

On platform B we should use XMVECTOR because this is how its SIMD workflow works(windows for example). There is a specific __vectorcall functions modifyer what will help you in that specific scenario.


Edited by imoogiBG, 29 July 2014 - 09:38 AM.


#6 panicq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 29 July 2014 - 09:50 AM

Okay so in fact when we pass a XMVECTOR as a parameter to a function it is passed directly through registers (but is it copied or passed as a reference ?) and we should use the corresponding typedef on the platform we use. And __vectorcall or __fastcall are functions that are automatically called when we argument is read, and this functions choose wich way the arguement should be send to register ?



#7 cozzie   Members   -  Reputation: 1762

Like
0Likes
Like

Posted 29 July 2014 - 09:52 AM

I believe so.
And honestly I don't believe you should stay away from the library, you can use it relatively easy (as explained above), without pretty much no cons and a big number of useful math functions.

#8 Quat   Members   -  Reputation: 414

Like
0Likes
Like

Posted 29 July 2014 - 10:35 AM

I also think the library is fine.  Although I did write some functions to do 3d math on the XMFLOAT variations when I just have to do a one line math operation and converting to XMVECTOR would be a little annoying.  The library probably does shine more if you have a lot of CPU calculations to do (collision detection) and software skinning, lots of ray/triangle intersections.


-----Quat

#9 imoogiBG   Members   -  Reputation: 1247

Like
0Likes
Like

Posted 29 July 2014 - 10:48 AM

MVECTOR as a parameter to a function it is passed directly through registers (but is it copied or passed as a reference ?) 

That means that the variables VALUES are already in xmm[N] registers and they are ready to be used.

 

I think you're not comfortable with calling conventions http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx

1)CPU calculations to do (collision detection). 
2) lots of ray/triangle intersections.

Can't agree. There are scalar operations that are not vector friendly. 

 

EDIT:

I've missed a line in your prev comments :
 

 And if I'm using DirectXMath is to advance in my book (Real-Time 3D Rendering with directX and HLSL) 

Well if you're following a book you should use the same tools.
 


Edited by imoogiBG, 29 July 2014 - 10:58 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS