Jump to content
  • Advertisement
Sign in to follow this  
Fran

Memory alignment with xnamath / Directx11

This topic is 2537 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

Hi,

Just a little problem that I got. Suppose I have.

class foo
{
float a;
short b;
char c;
Whatever whatever;
...
XMMATRIX matworld_;
XMMATRIX matProj_;
XMMATRIX matView_;
};

Now I allocate a foo object on the heap with new. When I try to use any operation on my matrices (e.g. XMMatrixIdentity()), I sometimes have a violation access crash. I'm pretty sure this is because the matrices are not 16 bit aligned, because when I use global variables it works. The only solution I have found to settle this problem (untested though;)) is to use pointers on the matrices or on a struct of all 3 of them and use align_malloc. Is there any better way to do this ?

Thank you,

Joedassin

Share this post


Link to post
Share on other sites
Advertisement
If you are going to use new, you need to tell the compiler to align the members correctly, via __declspec(align()) or __attribute__((align())), depending on your compiler.
its also good practice (see intels optimization manuals) to have the biggest variables first along with those that have specialized alignment requirements, this makes sure everything is aligned correctly, and it can also save you memory by removing useless padding (which makes things more cache friendly).

However, this doesn't fix the fact that the base allocation may be off (which is more than likely your problem), so I generally make sure the SIMD stuff is correctly aligned in the struct/class (be careful with vftables, they can throw it off) either with manual padding or alignment specifiers. Then use _mm_malloc along with an init method or placement new else one can use overloaded operator new/delete. alternatively you could alloc the matrices on their own using aligned_malloc/_mm_malloc, how this generally kills cache locality.

as an example:
__declspec(align(16)) class Tester
{
public:
char throwitoff;
__m128i pVec;

public:
virtual ~Tester()
{ }
};
This fixes internals alignment, but new might still give use poorly aligned pointers, so thats a bit useless, to fix this, we can (ab)use class specific new/delete:

__declspec(align(16)) class Tester
{
public:
char throwitoff;
__m128i pVec;

public:
virtual ~Tester()
{
}

void* operator new(size_t i)
{
return _mm_malloc(i,16);
}

void operator delete(void* p)
{
_mm_free(p);
}
};
now it passes alignment requirements on both members and the allocation.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!