Expand description
A linear algebra library for games and graphics with generic SIMD types.
The library provides:
- Vectors:
Vec2<T>,Vec3<T>,Vec4<T>. - Square Matrices:
Mat2<T>,Mat3<T>,Mat4<T>. - Quaternions:
Quat<T>. - Affine Transforms:
Affine2<T>,Affine3<T>. - Masks:
Mask2<T>,Mask3<T>,Mask4<T>.
§SIMD
Appropriate types have increased memory alignment in order to take advantage
of SIMD instructions that improve performance. For example, Vec3<f32>,
Vec4<f32>, Mat3<f32> and Mat4<f32> are aligned to 16 bytes on
x86 targets in order to take advantage of the SSE instruction set.
Although SIMD alignment generally results better performance, it can also
result in wasted space. For example, due to 16-byte alignment, Vec3<f32>
has 4 bytes of padding, and consequently Mat3<f32> has 12 bytes of
padding. For scenarios where better performance is not worth wasted space,
math types have non-SIMD, unaligned variants:
- Vectors:
Vec2U<T>,Vec3U<T>,Vec4U<T>. - Square Matrices:
Mat2U<T>,Mat3U<T>,Mat4U<T>. - Quaternions:
QuatU<T>. - Affine Transforms:
Affine2U<T>,Affine3U<T>. - Masks:
Mask2U<T>,Mask3U<T>,Mask4U<T>.
Unaligned types are optimal in memory-critical scenarios, for example when storing 3D models. In all other cases, aligned types are optimal and result in better performance than unaligned types.
Integration with wide enables SoA (Structure of Arrays) SIMD, which
lets you perform operations concurrently on multiple values, for example
with Vec3<f32x4> which represents four values of Vec3<f32>. SoA
requires modeling algorithms in a very specific way, but can be much faster
than normal types.
§Generics
Because types are generic over T, they support non-primitive scalar types
(see Scalar). Integration with fixed enables support for fixed-point
numbers, and integration with wide enables support for SoA.
When Rust’s type system is powerful enough, integration with
num-primitive will enable writing math code that is generic over
primitive types, for example functions generic over T: PrimitiveFloat will
have access to float-vector functionality.
Types relative to each other (e.g., Vec2<T>, Vec3<T>, Vec4<T>
and unaligned variants) are not distinct types, instead they are all type
aliases to these const-generic structs:
Where:
Nis the length (2, 3, or 4).Tis the scalar type (must implementScalar).Ais eitherAlignedorUnaligned.
Const generics eliminate the need for macros, making it easier to implement
functionality for all lengths (and both alignments). For example, instead of
defining seperate Ray2 and Ray3 types, it is possible to define a single
Ray<N, T, A> type then define type aliases for it.
§Math conventions
The library is coordinate-system agnostic, and should work for both right-handed and left-handed coordinate systems.
Vectors are treated as column matrices, meaning when transforming a vector with a matrix, the matrix goes on the left.
Matrices are stored in column-major order, meaning each column is continuous in memory.
Angles are in radians, but can be converted to and from degrees using standard-library functions.
§Optional features
-
std(default feature): Usesstdas the backend for float functionality. -
assertions: Enables assertions in release mode. Assertions are panics that catch invalid input and are enabled by default in debug mode. -
no-assertions: Disables assertions in debug mode. Assertions should only be controlled by binary crates. Library crates should not set this flag directly. -
bytemuck: Implementsbytemucktraits forggmathtypes. -
fixed: ImplementsScalarfor fixed-point numbers. -
fixp: ImplementsScalarfor fixed-point numbers. -
libm: Useslibmas the backend for float functionality. This makes the crateno_stdeven if thestdfeature is not disabled. Withoutstdorlibm, the crate compiles but all float functionality that relies on a backend is disabled. -
mint: Implements conversions betweenggmathandminttypes. -
serde: ImplementsSerializeandDeserializeforggmathtypes. -
wide: ImplementsScalarfor SIMD types.
Modules§
- constants
- A module with traits for scalar constants like
ZERO,ONEandNAN.
Macros§
- mat2
Deprecated - Creates a 2x2 column-major matrix from the provided arguments.
- mat3
Deprecated - Creates a 3x3 column-major matrix from the provided arguments.
- mat4
Deprecated - Creates a 4x4 column-major matrix from the provided arguments.
- vec2
Deprecated - Creates a 2-dimensional vector from the provided arguments.
- vec3
Deprecated - Creates a 3-dimensional vector from the provided arguments.
- vec4
Deprecated - Creates a 4-dimensional vector from the provided arguments.
Structs§
- Affine
- An
N-dimensional affine transform which can represent translation, rotation, scaling and shear of typeT. - Aligned
- A marker type specifying SIMD alignment.
- Length
- A marker type to restrict
const N: usizeto2,3and4. - Mask
- An
N-element vector mask optimized for typeT. - Matrix
- An
NxNcolumn-major matrix of typeT. - Quaternion
- A quaternion representing an orientation of type
T. - Unaligned
- A marker type specifying lack of SIMD alignment.
- Vector
- An
N-dimensional vector of typeT.
Traits§
- Alignment
- A marker trait controlling SIMD alignment for types.
- Float
Ext - Extends floating-point primitives with extra functionality.
- Scalar
- A trait for elements of math types.
- Scalar
Backend - Controls the implementation of math functions.
- Supported
Length - A marker trait to restrict
const N: usizeto2,3and4.
Type Aliases§
- Affine2
- A 2D affine transform which can represent translation, rotation, scaling and shear.
- Affine3
- A 3D affine transform which can represent translation, rotation, scaling and shear.
- Affine2U
- A 2D affine transform which can represent translation, rotation, scaling and shear.
- Affine3U
- A 3D affine transform which can represent translation, rotation, scaling and shear.
- Mask2
- A 2-element vector mask.
- Mask3
- A 3-element vector mask.
- Mask4
- A 4-element vector mask.
- Mask2U
- A 2-element vector mask.
- Mask3U
- A 3-element vector mask.
- Mask4U
- A 4-element vector mask.
- Mat2
- A 2x2 column-major matrix.
- Mat3
- A 3x3 column-major matrix.
- Mat4
- A 4x4 column-major matrix.
- Mat2U
- A 2x2 column-major matrix.
- Mat3U
- A 3x3 column-major matrix.
- Mat4U
- A 4x4 column-major matrix.
- Quat
- A quaternion representing an orientation.
- QuatU
- A quaternion representing an orientation.
- Vec2
- A 2-dimensional vector.
- Vec3
- A 3-dimensional vector.
- Vec4
- A 4-dimensional vector.
- Vec2U
- A 2-dimensional vector.
- Vec3U
- A 3-dimensional vector.
- Vec4U
- A 4-dimensional vector.