Core Header: DirectXMath.h
mainDirectXMath.h header is the primary entry point for the library. It defines fundamental SIMD types, storage types, and the core math function families used for vector and matrix operations.repository·main·Indexed 23 days ago
https://github.com/microsoft/directxmathAn all-inline SIMD C++ linear algebra library designed for games and graphics applications. It provides high-performance math primitives optimized for modern CPU instruction sets, including support for XMVECTOR and XMMATRIX fundamental types, storage types, and specialized components for collision testing (DirectXCollision.h), packed vectors (DirectXPackedVector.h), and colors (DirectXColors.h). Officially supported with MSVC 2019, clang/LLVM v12, and GCC 10.
DirectXMath.h header is the primary entry point for the library. It defines fundamental SIMD types, storage types, and the core math function families used for vector and matrix operations.DirectXMath includes auxiliary libraries for specialized math tasks:
SHMath/ contains .cpp files that must be compiled into your project.The library is organized into several functional areas located in the Inc/ directory and specialized extension directories:
Inc/)DirectXMath.h: The core library.DirectXPackedVector.h: Load/Store functions and types for compressed GPU formats.DirectXColors.h: .NET-style Color defines (sRGB and linear).DirectXCollision.h: Bounding volume collision library.Extentions/)Provides advanced instruction set variants for guarded codepaths:
DirectXMathSSE3.h, DirectXMathSSE4.hDirectXMathBE.h (SSSE3)DirectXMathAVX.h, DirectXMathAVX2.hDirectXMathF16C.h (Half-precision)DirectXMathFMA3.h, DirectXMathFMA4.h (Fused multiply-accumulate)SHMath/: Spherical Harmonics math functions (via DirectXSH.h).XDSP/: Digital Signal Processing helper functions (via XDSP.h).DirectXMath separates memory storage types from SIMD computation types. To use the library correctly, follow this three-step pattern:
XMFLOAT3 or XMFLOAT4) into an XMVECTOR or XMMATRIX using XMLoad* functions.XMVector*, XMMatrix*, XMQuaternion*, or XMColor* functions.XMStore* functions.#include <DirectXMath.h>
using namespace DirectX;
// Load data from storage type to SIMD register
XMFLOAT3 position{ 1.0f, 2.0f, 3.0f };
XMVECTOR v = XMLoadFloat3(&position);
// Perform computation in SIMD
v = XMVector3Normalize(v);
// Store result back to memory
XMFLOAT3 result;
XMStoreFloat3(&result, v);Functions accepting XMVECTOR or XMMATRIX use XM_CALLCONV and specific parameter typedefs (FXMVECTOR, GXMVECTOR, HXMVECTOR, CXMVECTOR, FXMMATRIX, and CXMMATRIX) to optimize register usage across different platforms.
XMVECTOR and XMMATRIX require 16-byte alignment.A suffix (e.g., XMFLOAT4A) are aligned.XMFLOAT4) are unaligned and safe for use in arbitrary data structures where 16-byte alignment cannot be guaranteed.DirectXMath distinguishes between Fundamental Types (used for high-performance SIMD computation) and Storage Types (used for memory-efficient storage in structures or buffers).
These are 16-byte aligned SIMD register types:
XMVECTOR: 128-bit SIMD register type for computation.XMMATRIX: 4x4 matrix consisting of four XMVECTOR rows.Use these types for non-SIMD storage. Note that some types have an A suffix (e.g., XMFLOAT4A) which indicates they are 16-byte aligned.
| Type | Description |
|---|---|
XMFLOAT2 / XMFLOAT2A | 2D float vector |
XMFLOAT3 / XMFLOAT3A | 3D float vector |
XMFLOAT4 / XMFLOAT4A | 4D float vector |
XMINT2 / XMINT3 / XMINT4 | 2/3/4D signed int32 vector |
XMUINT2 / XMUINT3 / XMUINT4 | 2/3/4D unsigned int32 vector |
XMFLOAT3X3 | 3x3 float matrix |
XMFLOAT3X4 / XMFLOAT3X4A | 3x4 float matrix |
XMFLOAT4X3 / XMFLOAT4X3A | 4x3 float matrix |
XMFLOAT4X4 / XMFLOAT4X4A | 4x4 float matrix |
To install DirectXMath using vcpkg in classic mode, run the install command and then configure your CMakeLists.txt to find and link the package.
Available features:
dx11: Spherical Harmonics math library for DirectX 11dx12: Spherical Harmonics math library for DirectX 12xdsp: Digital Signal Processing librarySupported triplets include x64-windows, x64-linux, arm64-windows, etc.
vcpkg install directxmathfind_package(directxmath CONFIG REQUIRED)
target_link_libraries(YourTarget PRIVATE Microsoft::DirectXMath)You can install DirectXMath using the following methods:
directxmath package from nuget.org.#include <DirectXMath.h>.The directxmath NuGet package is deprecated. The recommended way to integrate the latest DirectXMath into your C++ project is using vcpkg.
If you are using CMake, ensure you are using version 3.21 or later. Visual Studio 2019 users may need to install a standalone version of CMake 3.21 or later and add it to their PATH.
You can use GitHub CoPilot CLI to get assistance with DirectXMath by installing the specific skills via the GitHub CLI.
winget install GitHub.Copilot
winget install GitHub.cli
gh skill install microsoft/directxmath
copilot
/skills listDirectXMath provides extension headers in the Extensions/ directory that offer optimized overrides for specific instruction sets. To use them, you must include the extension header after the core DirectXMath.h header. Functions within these extensions are accessed via their specific namespaces rather than the default DirectX namespace.
Note: If you are compiling with /arch:AVX or /arch:AVX2 compiler flags, these extensions are not required as the core library automatically utilizes those instructions.
To integrate DirectXMath into your project using vcpkg in manifest mode (recommended), add directxmath to the dependencies array in your vcpkg.json file.
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"dependencies": [
"directxmath"
]
}