D3D11 XMVECTOR - Is it safe to always keep loaded?

Started by
5 comments, last by Zaoshi Kaba 10 years, 4 months ago

I'm making a math library to make doing the XMVECTOR and XMMATRIX math a bit simpler, I want it optimized as much as possible so I want to load the vector as few times as possible... unless it's fast enough I don't have to worry about it?

Is it safe to always have data loaded for SIMD instructions? If not, is it going to slow the process down if I Load the XMVECTOR every time I do an operation?

I haven't messed with SIMD instructions much..

Advertisement

XMVECTOR and XMMATRIX are already easy to use. Even if you make own library it won't differ much and most likely will be slower.

Is it safe to always have data loaded for SIMD instructions? If not, is it going to slow the process down if I Load the XMVECTOR every time I do an operation?

What do you mean by "always have data loaded"?

if i do this

XMVECTOR someVec = XMLoadFloat3( XMFLOAT3( 1,1,1 ) );

is it safe to store that as a data member?

It's safe to store it as a member as long as the thing it's a member of is allocated on a 16-byte boundary.

If it's heap allocated, that means you need to override global operator new and delete to get that alignment right. Unfortunately the standard Windows heap allocation functions only align to 8 bytes.

ok I see thanks for the help.

I wish I understood how aligning bytes worked better, I didn't learn much about it in school and it seems confusing as hell when I look online lol.

You just need to declare four new functions at global scope:


void* operator new (size_t size)
{
return _aligned_malloc(size, 16);
}

void operator delete (void *p)
{
_aligned_free(p);
}

void *operator new[](std::size_t size)
{
return _aligned_malloc(size, 16);
}

void operator delete[](void *p)
{
_aligned_free(p);
}

There's a slight complication that for completeness you'll probably want to overload the nothrow versions too, so you need 8 functions in total.

You may also want to make new throw appropriate exceptions, if you use them.


is it safe to store that as a data member?

Load and Save operation are pretty much same as the ones in games; unless you save value it'll never get changed.

These functions exist because of data alignment. As Adam_42 mentioned you can overload new/delete operators.

This problem doesn't exist on 64 bit programs because alignment is suitable there, therefore you don't need Load/Save operations.

This topic is closed to new replies.

Advertisement