DirectXMath Library

repository·main·Indexed 23 days ago

https://github.com/microsoft/directxmath

An 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.

Tokens
4.7K
Snippets
6
Records
22
Agent score
82%

What's inside DirectXMath

  1. Overview of DirectXMath auxiliary libraries

    main

    DirectXMath includes auxiliary libraries for specialized math tasks:

    • SHMath (Spherical Harmonics): Provides spherical harmonic math functions for lighting computations. Note that SHMath/ contains .cpp files that must be compiled into your project.
    • XDSP (Digital Signal Processing): A header-only library providing FFT and DSP functions built on top of DirectXMath.
  2. Overview of DirectXMath library components

    main

    The library is organized into several functional areas located in the Inc/ directory and specialized extension directories:

    Core Library (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.

    Instruction Set Extensions (Extentions/)

    Provides advanced instruction set variants for guarded codepaths:

    • DirectXMathSSE3.h, DirectXMathSSE4.h
    • DirectXMathBE.h (SSSE3)
    • DirectXMathAVX.h, DirectXMathAVX2.h
    • DirectXMathF16C.h (Half-precision)
    • DirectXMathFMA3.h, DirectXMathFMA4.h (Fused multiply-accumulate)

    Specialized Math

    • SHMath/: Spherical Harmonics math functions (via DirectXSH.h).
    • XDSP/: Digital Signal Processing helper functions (via XDSP.h).
  3. Use the Load / Compute / Store pattern in DirectXMath

    main

    DirectXMath separates memory storage types from SIMD computation types. To use the library correctly, follow this three-step pattern:

    1. Load: Transfer data from a storage type (like XMFLOAT3 or XMFLOAT4) into an XMVECTOR or XMMATRIX using XMLoad* functions.
    2. Compute: Perform mathematical operations using XMVector*, XMMatrix*, XMQuaternion*, or XMColor* functions.
    3. Store: Save the results from the SIMD register back to a storage type using 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);
  4. Understand DirectXMath calling conventions and alignment

    main

    Calling Conventions

    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.

    Alignment Requirements

    • SIMD Types: XMVECTOR and XMMATRIX require 16-byte alignment.
    • Storage Types:
      • Types with an A suffix (e.g., XMFLOAT4A) are aligned.
      • Unsuffixed variants (e.g., XMFLOAT4) are unaligned and safe for use in arbitrary data structures where 16-byte alignment cannot be guaranteed.
  5. Fundamental and Storage Types in DirectXMath

    main

    DirectXMath distinguishes between Fundamental Types (used for high-performance SIMD computation) and Storage Types (used for memory-efficient storage in structures or buffers).

    Fundamental Types

    These are 16-byte aligned SIMD register types:

    • XMVECTOR: 128-bit SIMD register type for computation.
    • XMMATRIX: 4x4 matrix consisting of four XMVECTOR rows.

    Storage Types

    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.

    TypeDescription
    XMFLOAT2 / XMFLOAT2A2D float vector
    XMFLOAT3 / XMFLOAT3A3D float vector
    XMFLOAT4 / XMFLOAT4A4D float vector
    XMINT2 / XMINT3 / XMINT42/3/4D signed int32 vector
    XMUINT2 / XMUINT3 / XMUINT42/3/4D unsigned int32 vector
    XMFLOAT3X33x3 float matrix
    XMFLOAT3X4 / XMFLOAT3X4A3x4 float matrix
    XMFLOAT4X3 / XMFLOAT4X3A4x3 float matrix
    XMFLOAT4X4 / XMFLOAT4X4A4x4 float matrix
  6. Integrate DirectXMath using vcpkg (classic)

    main

    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 11
    • dx12: Spherical Harmonics math library for DirectX 12
    • xdsp: Digital Signal Processing library

    Supported triplets include x64-windows, x64-linux, arm64-windows, etc.

    vcpkg install directxmath
    find_package(directxmath CONFIG REQUIRED)
    target_link_libraries(YourTarget PRIVATE Microsoft::DirectXMath)
  7. Integrate DirectXMath into your C++ project

    main

    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.

  8. Use CoPilot CLI for DirectXMath assistance

    main

    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 list
  9. Use instruction-set-specific optimized extensions

    main

    DirectXMath 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.

  10. Integrate DirectXMath using vcpkg manifest-mode

    main

    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"
      ]
    }