Jump to content
  • Advertisement
Sign in to follow this  
rekotc

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.

void DummyShader::PrepareToDraw(XMMATRIX worldMatrix, XMMATRIX viewMatrix,
    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>
#include "DummyShader.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
    DummyShader* dummyShader = new DummyShader();  

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

    dummyShader->PrepareToDraw(world,view,projection);

    return 0;
}

//DummyShader.h

using namespace DirectX;

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

//DummyShader.cpp

#include "DummyShader.h"

 

DummyShader::DummyShader()
{
}


DummyShader::~DummyShader()
{
}

void DummyShader::PrepareToDraw(XMMATRIX worldMatrix, XMMATRIX viewMatrix,
    XMMATRIX projectionMatrix) {

    WVP = worldMatrix * viewMatrix * projectionMatrix;

}

Any hint? Thanks in advance

 

 

 

 

Edited by rekotc

Share this post


Link to post
Share on other sites
Advertisement

From MSDN:
 

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 this post


Link to post
Share on other sites

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

Huge thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!