directx-11directx-9directxmath

Convert CONST D3DMATRIX to XMMATRIX


I'm programming a wrapper for first time and working with lots of different header files, but reached a problem. I can't convert D3DMATRIX to XMMATRIX. Looking at https://learn.microsoft.com/en-us/windows/win32/dxmath/pg-xnamath-migration-d3dx it says how XMMATRIX needs to be 16bit aligned but D3DMATRIX doesn't have that requirement.

code:

XMMATRIX WVP1;
---
---
HRESULT m_IDirect3DDevice9Ex::SetTransform(D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX *pMatrix)
{
    WVP1 = WVP1 * *pMatrix;

    ---
    ---
}

In this code, everytime SetTransform is called I want to times the matrix thats passed through with an existing matrix.

The calling function always passes through a D3DMATRIX I think so I cant change that. I also need to work with XMMATRIX type.

I've tried some code to sort this but it hasn't helped for example

const_cast<XMMATRIX*>(&WVP1) = const_cast<XMMATRIX*>(&WVP1) * reinterpret_cast<CONST XMMATRIX*>(pMatrix);

I don't know how to go about fixing this problem.

Thanks


Solution

  • You should use XMFLOAT4X4 which is the 'memory type' for a 4x4 float matrix and it has no alignment requirement. It has the same structure layout as D3DMATRIX.

    XMMATRIX m2 = XMLoadFloat4x4(reinterpret_cast<const XMFLOAT4X4*>(pMatrix));
    
    WVP1 = WVP1 * m2;
    

    Per the Microsoft Docs Programming Guide, XMVECTOR and XMMATRIX should in most cases be thought of as proxies for SIMD registers, not as data types. For example, if you have a class and you make XMVECTOR or XMMATRIX a member of that class, then the whole class has to be 16-byte aligned. While this is generally true for x64 native code, it is NOT true for x86 code.

    If you are new to DirectXMath, take a look at SimpleMath in the DirectX Tool Kit.