Working with D3DXMath
D3DXMath is a math helper library for Direct3D applications. D3DXMath is long-standing, is included in D3DX 9 and D3DX 10, and dates back to older versions of DirectX as well.
Note
The D3DX utility library (D3DX 9, D3DX 10, and D3DX 11) is deprecated for Windows 8, so we highly recommended that you migrate to DirectXMath rather than using D3DXMath.
DirectXMath shares much of the same functionality in D3DXMath, and internally D3DXMath includes a number of processor-specific optimizations. The key difference is that D3DXMath is hosted in the D3DX9*.DLLs and D3DX10*.DLLs, and very few of the functions are inlined. The DirectXMath Library calling convention is explicitly SIMD friendly, whereas D3DXMath has to perform load and store conversions to implement SIMD optimization.
Mixing DirectXMath and D3DXMath
D3DX11 does not contain D3DXMath, and in general we recommend using DirectXMath instead. However, you are free to continue to link to D3DX9 and/or D3DX10 in your application, and therefore you could continue to use D3DXMath or use both D3DXMath and DirectXMath in your application at the same time.
It is in general safe to cast an XMVECTOR* to a function that takes D3DXVECTOR4* or to cast an XMMATRIX* to a function that takes D3DXMATRIX*. The inverse is, however, not generally safe because XMVECTOR and XMMATRIX must be 16-byte aligned, while D3DXVECTOR4 and D3DXMATRIX have no such requirement. Failure to adhere to this requirement can result in invalid alignment exceptions at runtime.
It is safe to cast an XMVECTOR* to a function that takes D3DXVECTOR2* or D3DXVECTOR3*, but not vice-versa. Both alignment concerns and the fact that D3DXVECTOR2 and D3DXVECTOR3 are smaller structures make this an unsafe operation.
Note
D3DX (and therefore D3DXMath) is considered legacy, and is not available to Windows Store apps that run on Windows 8 and is not included in the Windows 8 SDK for desktop apps.
Using DirectXMath with Direct3D
Both DirectXMath and D3DXMath are optional when working with Direct3D. Direct3D 9 defined D3DMATRIX and D3DCOLOR as part of the Direct3D API in support of the (now legacy) fixed-function pipeline. D3DXMath in D3DX9 extends these Direct3D 9 types with common graphics math operations. For Direct3D 10.x and Direct3D 11, the API uses only the programmable pipeline so there is no API-specific structure for either matrices or color values. When the newer APIs require a color value, they take an explicit array of float values or a generic buffer of constant data that is interpreted by the HLSL shader. HLSL itself can support either row-major or column-major matrix formats, so the layout is entirely up to you (for more info, see HLSL, Matrix Ordering; if you use column-major matrix formats in your shaders, you need to transpose the DirectXMath matrix data as you place it into your constant buffer structures). While optional, the DirectXMath and D3DXMath libraries both provide common graphics related functionality, and are therefore extremely convenient when doing Direct3D programming.
It is safe to cast an XMVECTOR* to a D3DVECTOR* or XMMATRIX* to D3DMATRIX* since Direct3D 9 makes no alignment assumptions about the incoming data structure. It is also safe to cast XMCOLOR to D3DCOLOR. You can convert a 4-float representation of color to XMCOLOR via XMStoreColor() to get the 8:8:8:8 32-bit DWORD that is equivalent to D3DCOLOR.
When working with Direct3D 10.x or Direct3D 11, you will typically use DirectXMath types to build a structure for each of your constant buffers, and in those cases it largely depends on your ability to control the alignment to make these efficient, or to use XMStore*() operations to convert XMVECTOR and XMMATRIX data to the correct data types. When calling Direct3D 10.x or Direct3D 11 APIs that require a float[4] array of color values, you can cast an XMVECTOR* or XMFLOAT4* containing the color data.
Porting from D3DXMath
D3DXMath Type | DirectXMath Equivalent |
---|---|
D3DXFLOAT16 | HALF |
D3DXMATRIX | XMFLOAT4X4 |
D3DXMATRIXA16 | XMMATRIX or XMFLOAT4X4A |
D3DXQUATERNION D3DXPLANE D3DXCOLOR |
XMVECTOR is used rather than having unique types, so you will likely need to use an XMFLOAT4 Note: **D3DXQUATERNION::operator *** calls the D3DXQuaternionMultiply function, which multiplies two quaternions. But, unless you explicitly use the XMQuaternionMultiply function, you get an incorrect answer when you use **XMVECTOR::operator *** on a quaternion. |
D3DXVECTOR2 | XMFLOAT2 |
D3DXVECTOR2_16F | XMHALF2 |
D3DXVECTOR3 | XMFLOAT3 |
D3DXVECTOR4 | XMFLOAT4(or if you can guarantee the data is 16-byte aligned, XMVECTOR or XMFLOAT4A ) |
D3DXVECTOR4_16F | XMHALF4 |
Note
There is no direct equivalent to D3DXVECTOR3_16F in XNAMath.
D3DXMath Macro | DirectXMath Equivalent |
---|---|
D3DX_PI | XM_PI |
D3DX_1BYPI | XM_1DIVPI |
D3DXToRadian | XMConvertToRadians |
D3DXToDegree | XMConvertToDegrees |
D3DXMath Function | DirectXMath Equivalent |
---|---|
D3DXBoxBoundProbe | BoundingBox::Intersects(XMVECTOR, XMVECTOR, float&) |
D3DXComputeBoundingBox | BoundingBox::CreateFromPoints |
D3DXComputeBoundingSphere | BoundingSphere::CreateFromPoints |
D3DXSphereBoundProbe | BoundingSphere::Intersects(XMVECTOR, XMVECTOR, float&) |
D3DXIntersectTriFunction | TriangleTests::Intersects |
D3DXFloat32To16Array | XMConvertFloatToHalfStream |
D3DXFloat16To32Array | XMConvertHalfToFloatStream |
D3DXVec2Length | XMVector2Length or XMVector2LengthEst |
D3DXVec2LengthSq | XMVector2LengthSq |
D3DXVec2Dot | XMVector2Dot |
D3DXVec2CCW | XMVector2Cross |
D3DXVec2Add | XMVectorAdd |
D3DXVec2Subtract | XMVectorSubtract |
D3DXVec2Minimize | XMVectorMin |
D3DXVec2Maximize | XMVectorMax |
D3DXVec2Scale | XMVectorScale |
D3DXVec2Lerp | XMVectorLerp or XMVectorLerpV |
D3DXVec2Normalize | XMVector2Normalize or XMVector2NormalizeEst |
D3DXVec2Hermite | XMVectorHermite or XMVectorHermiteV |
D3DXVec2CatmullRom | XMVectorCatmullRom or XMVectorCatmullRomV |
D3DXVec2BaryCentric | XMVectorBaryCentric or XMVectorBaryCentricV |
D3DXVec2Transform | XMVector2Transform |
D3DXVec2TransformCoord | XMVector2TransformCoord |
D3DXVec2TransformNormal | XMVector2TransformNormal |
D3DXVec2TransformArray | XMVector2TransformStream |
D3DXVec2TransformCoordArray | XMVector2TransformCoordStream |
D3DXVec2TransformNormalArray | XMVector2TransformNormalStream |
D3DXVec3Length | XMVector3Length or XMVector3LengthEst |
D3DXVec3LengthSq | XMVector3LengthSq |
D3DXVec3Dot | XMVector3Dot |
D3DXVec3Cross | XMVector3Cross |
D3DXVec3Add | XMVectorAdd |
D3DXVec3Subtract | XMVectorSubtract |
D3DXVec3Minimize | XMVectorMin |
D3DXVec3Maximize | XMVectorMax |
D3DXVec3Scale | XMVectorScale |
D3DXVec3Lerp | XMVectorLerp or XMVectorLerpV |
D3DXVec3Normalize | XMVector3Normalize or XMVector3NormalizeEst |
D3DXVec3Hermite | XMVectorHermite or XMVectorHermiteV |
D3DXVec3CatmullRom | XMVectorCatmullRom or XMVectorCatmullRomV |
D3DXVec3BaryCentric | XMVectorBaryCentric or XMVectorBaryCentricV |
D3DXVec3Transform | XMVector3Transform |
D3DXVec3TransformCoord | XMVector3TransformCoord |
D3DXVec3TransformNormal | XMVector3TransformNormal |
D3DXVec3TransformArray | XMVector3TransformStream |
D3DXVec3TransformCoordArray | XMVector3TransformCoordStream |
D3DXVec3TransformNormalArray | XMVector3TransformNormalStream |
D3DXVec3Project | XMVector3Project |
D3DXVec3Unproject | XMVector3Unproject |
D3DXVec3ProjectArray | XMVector3ProjectStream |
D3DXVec3UnprojectArray | XMVector3UnprojectStream |
D3DXVec4Length | XMVector4Length or XMVector4LengthEst |
D3DXVec4LengthSq | XMVector4LengthSq |
D3DXVec4Dot | XMVector4Dot |
D3DXVec4Add | XMVectorAdd |
D3DXVec4Subtract | XMVectorSubtract |
D3DXVec4Minimize | XMVectorMin |
D3DXVec4Maximize | XMVectorMax |
D3DXVec4Scale | XMVectorScale |
D3DXVec4Lerp | XMVectorLerp or XMVectorLerpV |
D3DXVec4Cross | XMVector4Cross |
D3DXVec4Normalize | XMVector4Normalize or XMVector4NormalizeEst |
D3DXVec4Hermite | XMVectorHermite or XMVectorHermiteV |
D3DXVec4CatmullRom | XMVectorCatmullRom or XMVectorCatmullRomV |
D3DXVec4BaryCentric | XMVectorBaryCentric or XMVectorBaryCentricV |
D3DXVec4Transform | XMVector4Transform |
D3DXVec4TransformArray | XMVector4TransformStream |
D3DXMatrixIdentity | XMMatrixIdentity |
D3DXMatrixDeterminant | XMMatrixDeterminant |
D3DXMatrixDecompose | XMMatrixDecompose |
D3DXMatrixTranspose | XMMatrixTranspose |
D3DXMatrixMultiply | XMMatrixMultiply |
D3DXMatrixMultiplyTranspose | XMMatrixMultiplyTranspose |
D3DXMatrixInverse | XMMatrixInverse |
D3DXMatrixScaling | XMMatrixScaling |
D3DXMatrixTranslation | XMMatrixTranslation |
D3DXMatrixRotationX | XMMatrixRotationX |
D3DXMatrixRotationY | XMMatrixRotationY |
D3DXMatrixRotationZ | XMMatrixRotationZ |
D3DXMatrixRotationAxis | XMMatrixRotationAxis |
D3DXMatrixRotationQuaternion | XMMatrixRotationQuaternion |
D3DXMatrixRotationYawPitchRoll | XMMatrixRotationRollPitchYaw (Note the order of parameters is different: D3DXMatrixRotationYawPitchRoll takes yaw, pitch, roll, XMMatrixRotationRollPitchYaw takes pitch, yaw, roll) |
D3DXMatrixTransformation | XMMatrixTransformation |
D3DXMatrixTransformation2D | XMMatrixTransformation2D |
D3DXMatrixAffineTransformation | XMMatrixAffineTransformation |
D3DXMatrixAffineTransformation2D | XMMatrixAffineTransformation2D |
D3DXMatrixLookAtRH | XMMatrixLookAtRH |
D3DXMatrixLookAtLH | XMMatrixLookAtLH |
D3DXMatrixPerspectiveRH | XMMatrixPerspectiveRH |
D3DXMatrixPerspectiveLH | XMMatrixPerspectiveLH |
D3DXMatrixPerspectiveFovRH | XMMatrixPerspectiveFovRH |
D3DXMatrixPerspectiveFovLH | XMMatrixPerspectiveFovLH |
D3DXMatrixPerspectiveOffCenterRH | XMMatrixPerspectiveOffCenterRH |
D3DXMatrixPerspectiveOffCenterLH | XMMatrixPerspectiveOffCenterLH |
D3DXMatrixOrthoRH | XMMatrixOrthographicRH |
D3DXMatrixOrthoLH | XMMatrixOrthographicLH |
D3DXMatrixOrthoOffCenterRH | XMMatrixOrthographicOffCenterRH |
D3DXMatrixOrthoOffCenterLH | XMMatrixOrthographicOffCenterLH |
D3DXMatrixShadow | XMMatrixShadow |
D3DXMatrixReflect | XMMatrixReflect |
D3DXQuaternionLength | XMQuaternionLength |
D3DXQuaternionLengthSq | XMQuaternionLengthSq |
D3DXQuaternionDot | XMQuaternionDot |
D3DXQuaternionIdentity | XMQuaternionIdentity |
D3DXQuaternionIsIdentity | XMQuaternionIsIdentity |
D3DXQuaternionConjugate | XMQuaternionConjugate |
D3DXQuaternionToAxisAngle | XMQuaternionToAxisAngle |
D3DXQuaternionRotationMatrix | XMQuaternionRotationMatrix |
D3DXQuaternionRotationAxis | XMQuaternionRotationAxis |
D3DXQuaternionRotationYawPitchRoll | XMQuaternionRotationRollPitchYaw (Note the order of parameters is different: D3DXQuaternionRotationYawPitchRoll takes yaw, pitch, roll, XMQuaternionRotationRollPitchYaw takes pitch, yaw, roll) |
D3DXQuaternionMultiply | XMQuaternionMultiply |
D3DXQuaternionNormalize | XMQuaternionNormalize or XMQuaternionNormalizeEst |
D3DXQuaternionInverse | XMQuaternionInverse |
D3DXQuaternionLn | XMQuaternionLn |
D3DXQuaternionExp | XMQuaternionExp |
D3DXQuaternionSlerp | XMQuaternionSlerp or XMQuaternionSlerpV |
D3DXQuaternionSquad | XMQuaternionSquad or XMQuaternionSquadV |
D3DXQuaternionSquadSetup | XMQuaternionSquadSetup |
D3DXQuaternionBaryCentric | XMQuaternionBaryCentric or XMQuaternionBaryCentricV |
D3DXPlaneDot | XMPlaneDot |
D3DXPlaneDotCoord | XMPlaneDotCoord |
D3DXPlaneDotNormal | XMPlaneDotNormal |
D3DXPlaneScale | XMVectorScale |
D3DXPlaneNormalize | XMPlaneNormalize or XMPlaneNormalizeEst |
D3DXPlaneIntersectLine | XMPlaneIntersectLine |
D3DXPlaneFromPointNormal | XMPlaneFromPointNormal |
D3DXPlaneFromPoints | XMPlaneFromPoints |
D3DXPlaneTransform | XMPlaneTransform |
D3DXPlaneTransformArray | XMPlaneTransformStream |
D3DXColorNegative | XMColorNegative |
D3DXColorAdd | XMVectorAdd |
D3DXColorSubtract | XMVectorSubtract |
D3DXColorScale | XMVectorScale |
D3DXColorModulate | XMColorModulate |
D3DXColorLerp | XMVectorLerp or XMVectorLerpV |
D3DXColorAdjustSaturation | XMColorAdjustSaturation |
D3DXColorAdjustContrast | XMColorAdjustContrast |
D3DXFresnelTerm | XMFresnelTerm |
Note
Spherical Harmonics functions for DirectXMath are available separately. A DirectXMath equivalent to ID3DXMatrixStack is also available.
Related topics