# DX11 weird behaviour using XMMATRIX variables inside a c++ function

## Recommended Posts

Posted (edited)

Hello everybody, i'm learning DirectX11 and i can't understand why the following code doesn't work as expected. I have a function that takes 3 XMMATRIX and multiplies them to get the WVP matrix.

XMMATRIX projectionMatrix) {

WVP = worldMatrix * viewMatrix * projectionMatrix;

}

The 3 XMMATRIX are correctly read, but the resulting WVP matrix contains some absurd values. The function behaves as expected if i replace the offending line with the following:

XMMATRIX temp1 = projectionMatrix;
XMMATRIX temp2 = viewMatrix;
XMMATRIX temp3 = worldMatrix;

WVP = temp3 * temp2 * temp1;

But it doesn't make any sense to me, what's the difference? I'm sure i'm missing something veeeery obvious, but i'm not that experienced in the field.

This is a fully reproducible example of my problem:

//main.cpp

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{

XMMATRIX world = XMMatrixIdentity();
XMMATRIX view = XMMatrixIdentity();
XMMATRIX projection = XMMatrixIdentity();

return 0;
}

using namespace DirectX;

{
public:
XMMATRIX WVP;
void PrepareToDraw(XMMATRIX worldMatrix, XMMATRIX viewMatrix,
XMMATRIX projectionMatrix);
};

{
}

{
}

XMMATRIX projectionMatrix) {

WVP = worldMatrix * viewMatrix * projectionMatrix;

}

Edited by rekotc

##### Share on other sites
Quote

In addition to the type aliases, you must also use the XM_CALLCONV annotation to make sure the function uses the appropriate calling convention (__fastcall versus __vectorcall) based on your compiler and architecture. Because of limitations with __vectorcall, we recommend that you not use XM_CALLCONV for C++ constructors.

I don't know if it's actually the cause of your problem but it may be. For your function correct prototype would be

void XM_CALLCONV DummyShader::PrepareToDraw

##### Share on other sites

Thanks! It worked! Now i need to understand a bit better what i just did

Huge thanks!

## Create an account

Register a new account

• 17
• 19
• 23
• 10
• 10