• Advertisement
Sign in to follow this  

Xna Math Performance

This topic is 2349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've done a little research to check performance of Xna math library. For test I changed a little code for counting normals:

DX Math:


for(int i=0;i<primitives*3;i++)
{
D3DXVECTOR3 nor;
D3DXVECTOR3 v1 = pos[rand()%primitives]-pos[rand()%primitives];
D3DXVECTOR3 v2 = pos[rand()%primitives]-pos[rand()%primitives];
D3DXVec3Cross(&nor,&v1,&v2);
D3DXVec3Normalize(&nor,&nor);

pos[rand()%primitives] += nor;
pos[rand()%primitives] +=nor;
pos[rand()%primitives] += nor;
}
for(int i=0;i<primitives;i++)
{
D3DXVec3Normalize(&pos,&pos);
}


Xna Math:



for(int i=0;i<primitives*3;i++)
{
XMVECTOR nor;
nor = XMVector3Cross(xmpos[rand()%primitives]-xmpos[rand()%primitives],xmpos[rand()%primitives]-xmpos[rand()%primitives]);
nor = XMVector3Normalize(nor);

xmpos[rand()%primitives] += nor;
xmpos[rand()%primitives] += nor;
xmpos[rand()%primitives] += nor;
}
for(int i=0;i<primitives;i++)
{
xmpos =XMVector3Normalize(xmpos);
}



Well, I run it for 10^6 primitives and my time results for this code parts:
D3DX Math : 1.31335
XNA Math : 2.04672

After reading a bit I found out that XMVECTOR should be 16 byte aligned on heap, so I changed new to (XMVECTOR*)_aligned_malloc(sizeof(XMVECTOR)*primitives,16);

New results:
D3DX Math : 1.32109
XNA Math : 2.05517

Visual studio instructions set: Streaming SIMD Extensions 2 (/arch:SSE2) (/arch:SSE2)..




Now my questions is: what have I done wrong? I did also test with storing data as XMFLOAT3 with loading it for computations, than storing, and it was 3 times slower than simple and convenient DX math.

Share this post


Link to post
Share on other sites
Advertisement

XNA math is optimized for the XBox while DirectX math is optimized for the PC.

Thats not true by the way, MS renamed the math library to XNA math when they released XNA the first time. Ms actually urge you to use XNA Math as DX11 doesn't contain the D3DX math library (http://msdn.microsoft.com/en-us/library/ff729728(v=vs.85).aspx). And subsequently XNA math has been renamed to DirectXMath now, and should support SSE2 instructions.
It might be that your array acces is causing conversions between SIMD registers and normal float registers, these cause severe LHS overhead.

Share this post


Link to post
Share on other sites
Having just switched to XNA math, I'm very interested to see what the problem is. I vaguely remember reading that it is recommended not to use the overloaded operators for XNA Math where performance is critical, and to use the function name equivalents instead. Does it help if you replace those for your subtractions and additions?

Share this post


Link to post
Share on other sites
CPU SIMD performance is very tricky. If you look through the general programming forums you'll find plenty of threads with in-depth discussions of the potential pitfalls.

Share this post


Link to post
Share on other sites

Does it help if you replace those for your subtractions and additions?




It helps. But still:

DX Math : 1.30931
Xna Math : 1.64447




I changed Normalize to NormalizeEst. Now it is:

DX Math : 1.30711
Xna Math : 1.36559

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement