Pergunta

I have an odd problem with my Direct3D11 application that I'm trying to resolve for few hours already. The problem is that method:

void CameraClass::Render()
{
    XMFLOAT3 sUp, sLookAt, sRotationInRadians;

    sUp.x = 0.0f;
    sUp.y = 1.0f;
    sUp.z = 0.0f;

    sLookAt.x = 0.0f;
    sLookAt.y = 0.0f;
    sLookAt.z = 1.0f;

    sRotationInRadians.x = m_Rotation.x * 0.0174532925f;
    sRotationInRadians.y = m_Rotation.y * 0.0174532925f;
    sRotationInRadians.z = m_Rotation.z * 0.0174532925f;

    XMVECTOR vecLookAt = XMVectorSet( sLookAt.x, sLookAt.y, sLookAt.z, 0.0f );
    XMVECTOR vecUp = XMVectorSet( sUp.x, sUp.y, sUp.z, 0.0f );
    XMVECTOR vecPosition = XMVectorSet( m_Position.x , m_Position.y, m_Position.z, 0.0f );

    XMMATRIX RotationMatrix( XMMatrixRotationRollPitchYaw( sRotationInRadians.x, sRotationInRadians.y, sRotationInRadians.z ));

    vecLookAt = XMVector3TransformCoord( vecLookAt, RotationMatrix );
    vecUp = XMVector3TransformCoord( vecUp, RotationMatrix );

    vecLookAt += vecPosition;   

    m_ViewMatrix = XMMatrixLookAtLH( vecPosition, vecLookAt, vecUp );
}

Everything's fine until it reaches that line:

m_ViewMatrix = XMMatrixLookAtLH( vecPosition, vecLookAt, vecUp );

Somehow it causes application crash ( switches to not responding to be correct ).

And here is how actual calls look like:

XMMATRIX ViewMatrix;
XMMATRIX ProjectionMatrix;
XMMATRIX WorldMatrix;

m_D3D->BeginScene( 0.0f, 0.0f, 0.0f, 1.0f );

m_Camera->Render();

m_D3D->GetWorldMatrix( WorldMatrix );
m_D3D->GetProjectionMatrix( ProjectionMatrix );

In advance, Vertex and Pixel shaders compile just fine, so that's not a problem. Most probably I'm doing something wrong with xnamath ( I'm completely new with it ), but I have no idea what on Earth that could be. Thanks in advance. I'll provide more information if needed.

Edit@1: With dozens of changes I managed to get Projection and World matrices to work. Though I still can't set View matrix. I changed the code, so it matches the actual one and got rid of what's not important.

Edit@2: Breaking news from last minute: there isn't a problem with XMMatrixLookAtLH function, because I decided to save the result to local variable and it works, but if I want to assign result matrix to class member then I get the crash. That's most certainly interesting.

Foi útil?

Solução

After many hours spent attempting to solve this problem, I believe I have finally solved it.

XMMATRIX represents a 16-byte aligned matrix and the unaligned allocation of the XMMATRIX causes this error when you attempt to copy the value. This would explain why it doesn't always happen and why it works in different mode.

The best solution I have found to this issue is to use an XMFLOAT4X4 to store the value.

Here is a simple example:

#include <windows.h>
#include <xnamath.h>

class Camera
{
protected:
    XMFLOAT4X4  view_;
public:
    XMFLOAT4X4  const& Update()
    {
        XMStoreFloat4x4(&this->view_ , XMMatrixIdentity());
        return this->view_;
    }
};

int main()
{
    Camera* cam = new Camera;
    XMFLOAT4X4 const& mat = cam->Update();
    XMMATRIX x = XMLoadFloat4x4(&mat);
}

Outras dicas

Since the crash happens at assignment: m_ViewMatrix = XMMatrixLookAtLH(...

and the build limps along better in x64 than x86 I would assume the m_ViewMatrix or even its container m_Camera is mapped in a memory space that is shared with something else. Memory clobering. The shift to 64bit is another indicator towards this since may have moved memory around to hide the problem.

How are you allocating the m_Camera field? What is the main container for everything?

Post the full CameraClass header and where you are allocating it. Also, try sprinkling in some arrays around the fields and see if that helps. That would indicate memory clobbering too.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top