xnamath byte alignment

Started by
0 comments, last by EWClay 11 years, 1 month ago

I've come across this problem while following Beginning DirectX 11 Game Programming. During the cube demo the view and projection matrix are being transposed. If I'm using local XMMatrix as the parameter it works fine, if I'm using the member variable however it crashes with the error:


Unhandled exception at 0x00D4F279 in DX11Project.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.

I've come upon a few answers where they state XMMatrix expects objects which are 16-byte aligned and some objects created would be 8-byte aligned which would cause the crash.

Has anyone else come across this sort of problem, and would there be any way to fix it without creating local variables?

The member variables are declared in Cube.h


class Cube : public DX11Base
{
//...
XMMATRIX	m_viewMatrix;
XMMATRIX	m_projMatrix;
//...
};

The code crashes when I'm using:


m_viewMatrix = XMMatrixIdentity();
m_projMatrix = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f/600.0f, 0.01f, 100.0f);

m_viewMatrix = XMMatrixTranspose(m_viewMatrix);
m_projMatrix = XMMatrixTranspose(m_projMatrix);

It works fine when I'm using:


XMMATRIX view(XMMatrixIdentity());
XMMATRIX proj(XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f/600.0f, 0.01f, 100.0f));

m_viewMatrix = XMMatrixTranspose(view);
m_projMatrix = XMMatrixTranspose(proj);

Edit: I just found that by defining _XM_NO_INTRINSICS_ in your preprocessor also makes it work, however this will disable the optimization:

_XM_NO_INTRINSICS_ defines that no intrinsic types are to be used and that the XNA Math library will only use standard floating-point precision when performing its operations. This can be seen as the behavior that will allow an application to use XNA Math with no special optimizations or functionality.

Advertisement

You can use local variables, or you can make sure that Cube is always 16 byte aligned by using a custom allocator.

If you can't guarantee the alignment, the safest way would be to use XMFLOAT4X4 in the class and explicitly load and store to XMMATRIX when needed.

This topic is closed to new replies.

Advertisement