You need to ensure that the XMVECTOR
& XMMATRIX
types from DXMath are correctly aligned to 16-bytes (see the docs here), for stack allocations, this is automatic, but for heap allocations, you need to use _aligned_malloc
to get correctly aligned memory. Alternatively you can use the XMFLOATn
and XMFLOATnxn
for store and use the unaligned vector loads when you actually work with them.
For classes or structs that contain XM types you can overcome this by overloading the new
operator to do explicitly align allocations; the easiest is by deriving from a template such as this (note: this isn't complete, the are a few types of operator new
& delete
):
template <size_t align> class AlignedAllocPolicy
{
public:
void* operator new(std::size_t size)
{
return _aligned_malloc(size,align);
}
void operator delete(void* mem)
{
_aligned_free(mem);
}
}