DirectXMath
DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps
Top Related Projects
OpenGL Mathematics (GLM)
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
The C# math library used in Unity providing vector types and math functions with a shader like syntax
Godot Engine – Multi-platform 2D and 3D game engine
C++ math library developed primarily for games focused on simplicity and efficiency.
A modern formatting library
Quick Overview
DirectXMath is a C++ library developed by Microsoft for high-performance math operations commonly used in game development and graphics programming. It provides SIMD-optimized implementations of vector, matrix, and quaternion operations, leveraging SSE and AVX instructions on x86 platforms and NEON on ARM.
Pros
- Highly optimized for performance, utilizing SIMD instructions
- Seamless integration with DirectX and other Microsoft graphics technologies
- Extensive documentation and support from Microsoft
- Cross-platform support for Windows, Xbox, and UWP applications
Cons
- Steep learning curve for developers not familiar with SIMD programming
- Limited portability to non-Microsoft platforms
- May require specific compiler settings or intrinsics support
- Some operations may not be as intuitive as scalar math libraries
Code Examples
Vector addition:
#include <DirectXMath.h>
using namespace DirectX;
XMVECTOR v1 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR v2 = XMVectorSet(4.0f, 5.0f, 6.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
Matrix multiplication:
XMMATRIX m1 = XMMatrixRotationY(XMConvertToRadians(45.0f));
XMMATRIX m2 = XMMatrixTranslation(1.0f, 2.0f, 3.0f);
XMMATRIX result = XMMatrixMultiply(m1, m2);
Quaternion slerp:
XMVECTOR q1 = XMQuaternionRotationRollPitchYaw(0.0f, XM_PIDIV2, 0.0f);
XMVECTOR q2 = XMQuaternionIdentity();
float t = 0.5f;
XMVECTOR result = XMQuaternionSlerp(q1, q2, t);
Getting Started
-
Include DirectXMath in your project:
- For Visual Studio, it's included in the Windows SDK
- For other platforms, download from the GitHub repository
-
Add the necessary include:
#include <DirectXMath.h>
-
Use the DirectX namespace:
using namespace DirectX;
-
Start using DirectXMath types and functions:
XMVECTOR position = XMVectorSet(0.0f, 1.0f, 2.0f, 1.0f); XMMATRIX worldMatrix = XMMatrixIdentity();
Remember to compile with appropriate SIMD instruction set support (e.g., /arch:SSE2 for Visual Studio).
Competitor Comparisons
OpenGL Mathematics (GLM)
Pros of GLM
- Cross-platform compatibility (Windows, macOS, Linux)
- Extensive GLSL-style syntax support
- Wider range of mathematical functions and types
Cons of GLM
- Potentially slower performance due to template-heavy implementation
- Larger codebase and longer compilation times
- Less optimized for DirectX-specific scenarios
Code Comparison
GLM:
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
glm::mat4 model = glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::vec4 vector = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
glm::vec4 transformed = model * vector;
DirectXMath:
#include <DirectXMath.h>
XMMATRIX model = XMMatrixRotationY(XMConvertToRadians(45.0f));
XMVECTOR vector = XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f);
XMVECTOR transformed = XMVector4Transform(vector, model);
Both libraries provide similar functionality for 3D math operations, but GLM offers a more GLSL-like syntax and broader platform support. DirectXMath, on the other hand, is optimized for DirectX development and may offer better performance in Windows environments. The choice between the two depends on the specific project requirements, target platforms, and personal preference for syntax style.
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
Pros of Bullet3
- Comprehensive physics simulation library with collision detection, rigid body dynamics, and more
- Cross-platform support for various operating systems and devices
- Active community and ongoing development
Cons of Bullet3
- Steeper learning curve due to its broader scope and complexity
- Potentially higher performance overhead for simple math operations
Code Comparison
DirectXMath:
XMVECTOR v1 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR v2 = XMVectorSet(4.0f, 5.0f, 6.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
Bullet3:
btVector3 v1(1.0f, 2.0f, 3.0f);
btVector3 v2(4.0f, 5.0f, 6.0f);
btVector3 result = v1 + v2;
Summary
DirectXMath is a lightweight, SIMD-optimized math library specifically designed for DirectX applications, offering efficient vector and matrix operations. Bullet3, on the other hand, is a full-featured physics engine that includes its own math library along with extensive simulation capabilities. While Bullet3 provides a more comprehensive solution for physics-based applications, DirectXMath may be more suitable for projects primarily focused on efficient math operations in a DirectX environment.
The C# math library used in Unity providing vector types and math functions with a shader like syntax
Pros of Unity.Mathematics
- Cross-platform compatibility, supporting multiple platforms beyond Windows
- Tighter integration with Unity game engine and ecosystem
- More extensive support for quaternions and transformations
Cons of Unity.Mathematics
- Less optimized for DirectX-specific operations
- Smaller community and fewer resources compared to DirectXMath
- May have a steeper learning curve for developers not familiar with Unity
Code Comparison
DirectXMath:
XMVECTOR v1 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR v2 = XMVectorSet(4.0f, 5.0f, 6.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
Unity.Mathematics:
float4 v1 = new float4(1.0f, 2.0f, 3.0f, 0.0f);
float4 v2 = new float4(4.0f, 5.0f, 6.0f, 0.0f);
float4 result = v1 + v2;
Both libraries provide efficient vector math operations, but Unity.Mathematics offers a more intuitive syntax with operator overloading. DirectXMath is tailored for DirectX development, while Unity.Mathematics is designed for broader use within the Unity engine and beyond.
Godot Engine – Multi-platform 2D and 3D game engine
Pros of Godot
- Complete game engine with a wide range of features, including graphics, physics, and scripting
- Cross-platform support for multiple desktop and mobile operating systems
- Open-source with an active community and extensive documentation
Cons of Godot
- Steeper learning curve due to its comprehensive nature
- May be overkill for projects only requiring math operations
- Performance might be lower for specific math-intensive tasks
Code Comparison
DirectXMath:
XMVECTOR v1 = XMLoadFloat3(&float3);
XMVECTOR v2 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
Godot:
var v1 = Vector3(1, 2, 3)
var v2 = Vector3(4, 5, 6)
var result = v1 + v2
Summary
DirectXMath is a specialized math library for DirectX applications, while Godot is a full-featured game engine. DirectXMath offers optimized math operations for graphics programming, whereas Godot provides a complete development environment for creating games and interactive applications. The choice between them depends on the project's scope and requirements.
C++ math library developed primarily for games focused on simplicity and efficiency.
Pros of MathFu
- Cross-platform support (Windows, macOS, Linux, Android, iOS)
- SIMD optimizations for multiple architectures (SSE, NEON)
- Smaller codebase, potentially easier to integrate
Cons of MathFu
- Less comprehensive documentation compared to DirectXMath
- Fewer utility functions and specialized operations
- Less active development and community support
Code Comparison
DirectXMath:
XMVECTOR v1 = XMLoadFloat3(&vector1);
XMVECTOR v2 = XMLoadFloat3(&vector2);
XMVECTOR result = XMVector3Cross(v1, v2);
XMStoreFloat3(&crossProduct, result);
MathFu:
mathfu::Vector<float, 3> v1(vector1);
mathfu::Vector<float, 3> v2(vector2);
mathfu::Vector<float, 3> result = mathfu::Vector<float, 3>::CrossProduct(v1, v2);
Summary
DirectXMath is a more mature and comprehensive library, specifically tailored for DirectX development on Windows platforms. It offers extensive documentation and a wide range of utility functions.
MathFu, on the other hand, provides a more portable solution with cross-platform support and SIMD optimizations for various architectures. It has a simpler API and a smaller codebase, which can be advantageous for certain projects.
The choice between the two libraries depends on the specific requirements of your project, such as target platforms, performance needs, and desired level of abstraction.
A modern formatting library
Pros of fmt
- Cross-platform compatibility (Windows, macOS, Linux)
- Extensive formatting options and type-safe interface
- Active development and community support
Cons of fmt
- Primarily focused on string formatting, not math operations
- May have a steeper learning curve for basic usage
Code Comparison
fmt:
#include <fmt/core.h>
std::string result = fmt::format("Hello, {}!", "world");
fmt::print("The answer is {}.", 42);
DirectXMath:
#include <DirectXMath.h>
XMVECTOR v1 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR v2 = XMVectorSet(4.0f, 5.0f, 6.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
Key Differences
- Purpose: fmt is for string formatting, while DirectXMath is for 3D math operations
- Scope: fmt is more general-purpose, DirectXMath is specialized for graphics programming
- Performance: DirectXMath is optimized for SIMD operations, fmt focuses on formatting efficiency
Use Cases
- fmt: General string formatting, logging, text output
- DirectXMath: 3D graphics, game development, DirectX applications
Community and Support
Both projects have active communities, but fmt has a broader user base due to its general-purpose nature.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
DirectXMath
https://github.com/Microsoft/DirectXMath
Copyright (c) Microsoft Corporation.
February 2024
This package contains the DirectXMath library, an all inline SIMD C++ linear algebra library for use in games and graphics apps.
This code is designed to build with Visual Studio 2019 (16.11), Visual Studio 2022, or clang/LLVM for Windows. It is recommended that you make use of the latest updates.
These components are designed to work without requiring any content from the legacy DirectX SDK. For details, see Where is the DirectX SDK?.
Directory Layout
-
Inc\
-
DirectXMath Files (in the DirectX C++ namespace)
- DirectXMath.h - Core library
- DirectXPackedVector.h - Load/Store functions and types for working with various compressed GPU formats
- DirectXColors.h - .NET-style Color defines in sRGB and linear color space
- DirectXCollision.h - Bounding volume collision library
-
-
Extentions\
-
Advanced instruction set variants for guarded codepaths
- DirectXMathSSE3.h - SSE3
- DirectXMathBE.h - Supplemental SSE3 (SSSE3)
- DirectXMathSSE4.h - SSE4.1
- DirectXMathAVX.h - Advanced Vector Extensions (AVX)
- DirectXMathAVX2.h - Advanced Vector Extensions 2 (AVX2)
- DirectXMathF16C.h - Half-precision conversions (F16C)
- DirectXMathFMA3.h - Fused multiply-accumulate (FMA3)
- DirectXMathFMA4.h - Fused multiply-accumulate (FMA4)
-
-
SHMath\
-
Spherical Harmonics math functions
- DirectXSH.h - Header for SHMath functions
- DirectXSH.cpp, DirectXSHD3D11.cpp, DirectXSHD3D12.cpp - Implementation
-
-
XDSP\
- XDSP.h - Digital Signal Processing helper functions
-
build\
- Contains YAML files for the build pipelines along with some miscellaneous build files and scripts.
Documentation
Documentation is available on the Microsoft Docs. Additional information can be found on the project wiki.
Compiler support
Officially the library is supported with Microsoft Visual C++ 2019 or later, clang/LLVM v12 or later, and GCC 9 or later. It should also compile with the Intel C++ and MinGW compilers.
When building with clang/LLVM or other GNU C compilers, the _XM_NO_XMVECTOR_OVERLOADS_
control define is set because these compilers do not support creating operator overloads for the XMVECTOR
type. You can choose to enable this preprocessor define explicitly to do the same thing with Visual C++ for improved portability.
To build for non-Windows platforms, you need to provide a sal.h
header in your include path. You can obtain an open source version from GitHub.
With GCC, the SAL annotation preprocessor symbols can conflict with the GNU implementation of the Standard C++ Library. The workaround is to include the system headers before including DirectXMath:
#include <algorithm>
#include <utility>
#include <DirectXMath.h>
Notices
All content and source code for this package are subject to the terms of the MIT License.
For the latest version of DirectXMath, bug reports, etc. please visit the project site on GitHub.
Release Notes
FOR SECURITY ADVISORIES, see GitHub.
For a full change history, see CHANGELOG.md.
- The clang/LLVM toolset currently does not respect the
float_control
pragma for SSE instrinsics. Therefore, the use of/fp:fast
is not recommended on clang/LLVM until this issue is fixed. See 55713.
Support
For questions, consider using Stack Overflow with the directxmath tag, or the DirectX Discord Server in the dx12-developers or dx9-dx11-developers channel.
For bug reports and feature requests, please use GitHub issues for this project.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
Tests for new features should also be submitted as a PR to the Test Suite repository.
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Trademarks
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.
Credits
The xboxmath library was originated by Matt Bronder with contributions from Sakphong Chanbai and David Hefner for the Xbox 360.
The xnamath library for the DirectX SDK and Xbox XDK was the work of Chuck Walbourn and Becky Heineman based on xboxmath, with contributions from Jeremy Gup, Dan Haffner, Matt Lee, Casey Meekhof, Rich Sauer, Jason Strayer, and Xiaoyue Zheng.
The DirectXMath library for the Windows SDK and Xbox One XDK is the work of Chuck Walbourn based on xnamath, with contributions from Darren Anderson, Matt Lee, Aaron Rodriguez Hernandez, Yuichi Ito, Reza Nourai, Rich Sauer, and Jason Strayer.
Thanks to Dave Eberly for his contributions particularly in improving the transcendental functions.
Thanks to Bruce Dawson for his help with the rounding functions.
Thanks to Andrew Farrier for the fixes to XMVerifyCPUSupport
to properly support clang.
Thanks to Scott Matloff for his help in getting the library updated to use Intel SVML for VS 2019.
Top Related Projects
OpenGL Mathematics (GLM)
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
The C# math library used in Unity providing vector types and math functions with a shader like syntax
Godot Engine – Multi-platform 2D and 3D game engine
C++ math library developed primarily for games focused on simplicity and efficiency.
A modern formatting library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot