DirectX Math

Overview

The DirectXMath library provides a high‑performance SIMD‑oriented API for common 2‑D/3‑D math operations used in graphics programming. It includes types and functions for vectors, matrices, quaternions, planes, and more.

Key features:

  • Header‑only, no binary dependencies
  • Optimized for SSE, AVX, and ARM NEON
  • Fully compatible with HLSL data layouts
  • Easy integration with Direct3D 11/12 pipelines
Vector Types

DirectXMath defines several vector types. The most common is XMVECTOR, a 128‑bit SIMD register that can hold four floats.

#include <DirectXMath.h>
using namespace DirectX;

XMVECTOR v = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
float len = XMVectorGetX(XMVector3Length(v));

Utility functions provide component access, arithmetic, dot/cross products, and more.

Matrix Types

Matrix types are built on top of XMVECTOR. The primary type is XMMATRIX, a 4×4 matrix stored in row‑major order.

XMMATRIX view = XMMatrixLookAtLH(
    XMVectorSet(0.0f, 2.0f, -5.0f, 1.0f), // eye
    XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),  // at
    XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f)   // up
);

Common functions include XMMatrixIdentity, XMMatrixTranspose, XMMatrixMultiply, and projection helpers.

Quaternions

Quaternions are used for representing rotations without gimbal lock. DirectXMath provides XMVECTOR based quaternion operations.

XMVECTOR q = XMQuaternionRotationRollPitchYaw(
    XMConvertToRadians(30.0f),
    XMConvertToRadians(45.0f),
    XMConvertToRadians(60.0f)
);
XMVECTOR rotated = XMVector3Rotate(v, q);
Live Demo – Rotating Cube