Matrices causing crash
Asked Answered
J

2

8

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.

Jacobine answered 11/1, 2013 at 23:35 Comment(7)
Try debugging the method, and see what's the value of the m_ViewMAtrix pointer?Carboloy
Okay so I'm getting this exact same error. The code just mysteriously crashes when it tries to calculate XMMatrixLookAtLH or any XNA function.Nation
After trying some things out, I finally got it work by setting my mode to 64x build. Still looking for an answer on why that happens though.Nation
@Nation It is not appropriate to edit someone else's question this drastically.Hypoblast
@AndrewBarber I have put a bounty on this question and the issue is that if I post a new question with my code it will get closed as duplicate. Yet I still need to show my code. How do you suggest I do it?Nation
@Nation if you have any question than ask as difference question please do not made radical change to the post and also edit feature is not for thatBehnken
@NullPointer It is the same question, I'm just clarifying it with my code. Also, I didn't edit the person question. Only added to it.Nation
N
4

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);
}
Nation answered 11/2, 2013 at 7:38 Comment(2)
Only just seen your question - i had the same problem a while ago and this was the answer too. And in fact this is pretty much what microsoft recommend anyway..Confessional
For reference msdn.microsoft.com/en-gb/library/windows/desktop/… (This is got directxmath rather than the older xnamath but it's much the same)Confessional
U
2

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.

Unhand answered 12/2, 2013 at 3:42 Comment(3)
Seems like I can't edit the post because I'm not the original poster. But if you want to take a look at my code review the revision or view it here on github github.com/mmajeed-Fanshawe/XNAErrorNation
I would like to ask, what processor and OS is this problem occurring on?Eliathan
@Eliathan Windows 7, please read my answer to see the soultionNation

© 2022 - 2024 — McMap. All rights reserved.