tags:

views:

32

answers:

2

Using the D3DX library that is a part of directX, specifically directx9 in this case, I'm wondering if it's safe to use the same matrix (or vector etc) for input and ouput

D3DXMATRIX mat;
D3DXMatrixInverse(&mat, NULL, &mat);

I've been avoiding doing so, assuming that it would result in bad things happening when parts of the array got partly overwritten as results are calculated but I see an awful lot of code around that does exactly this.

A brief test indicates that it seems to work ok, so I'm assuming that the D3DX functions take a copy where necessary of the input data, or some other method to ensure that this works ok, but I can't find it documented anywhere so I'm reluctant to rely on it working.

Is there any official statement on using the functions like this?

+1  A: 

I'm pretty sure the answer is yes. I can't find anywhere where this is said for sure however ...

Edit: Flicking through D3DX9Math.inl it looks like care HAS been taken to make sure you can. For example if we look at the code for D3DXVec3Cross:

D3DXINLINE D3DXVECTOR3* D3DXVec3Cross
    ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
{
    D3DXVECTOR3 v;

#ifdef D3DX_DEBUG
    if(!pOut || !pV1 || !pV2)
        return NULL;
#endif

    v.x = pV1->y * pV2->z - pV1->z * pV2->y;
    v.y = pV1->z * pV2->x - pV1->x * pV2->z;
    v.z = pV1->x * pV2->y - pV1->y * pV2->x;

    *pOut = v;
    return pOut;
}

You can see that it performs the cross product into a temporary and THEN, in the final step, it copies it into the return. It would be nice if this was stated somewhere for sure.

Goz
+2  A: 

Yes, it is. From msdn:

Each of the functions can take the same object as the passed [in] and returned [out] parameters

Bahbar
Ah excellent. For some reason i was unable to find this.
John Burton
Accepted this as it has the definative link. the other answer is good too though so voted up
John Burton
Heh I even checked that link ... obviously didn't read it well enough ;)
Goz