| 1 | # glam |
| 2 | |
| 3 | [![Build Status]][github-ci] [![Coverage Status]][coveralls.io] |
| 4 | [![Latest Version]][crates.io] [![docs]][docs.rs] |
| 5 | [![Minimum Supported Rust Version]][Rust 1.68.2] |
| 6 | |
| 7 | A simple and fast 3D math library for games and graphics. |
| 8 | |
| 9 | ## Features |
| 10 | |
| 11 | * `f32` types |
| 12 | * vectors: `Vec2`, `Vec3`, `Vec3A` and `Vec4` |
| 13 | * square matrices: `Mat2`, `Mat3`, `Mat3A` and `Mat4` |
| 14 | * a quaternion type: `Quat` |
| 15 | * affine transformation types: `Affine2` and `Affine3A` |
| 16 | * `f64` types |
| 17 | * vectors: `DVec2`, `DVec3` and `DVec4` |
| 18 | * square matrices: `DMat2`, `DMat3` and `DMat4` |
| 19 | * a quaternion type: `DQuat` |
| 20 | * affine transformation types: `DAffine2` and `DAffine3` |
| 21 | * `i8` types |
| 22 | * vectors: `I8Vec2`, `I8Vec3` and `I8Vec4` |
| 23 | * `u8` types |
| 24 | * vectors: `U16Vec2`, `U16Vec3` and `U16Vec4` |
| 25 | * `i16` types |
| 26 | * vectors: `I16Vec2`, `I16Vec3` and `I16Vec4` |
| 27 | * `u16` types |
| 28 | * vectors: `U16Vec2`, `U16Vec3` and `U16Vec4` |
| 29 | * `i32` types |
| 30 | * vectors: `IVec2`, `IVec3` and `IVec4` |
| 31 | * `u32` types |
| 32 | * vectors: `UVec2`, `UVec3` and `UVec4` |
| 33 | * `i64` types |
| 34 | * vectors: `I64Vec2`, `I64Vec3` and `I64Vec4` |
| 35 | * `u64` types |
| 36 | * vectors: `U64Vec2`, `U64Vec3` and `U64Vec4` |
| 37 | * `usize` types |
| 38 | * vectors: `USizeVec2`, `USizeVec3` and `USizeVec4` |
| 39 | * `bool` types |
| 40 | * vectors: `BVec2`, `BVec3` and `BVec4` |
| 41 | |
| 42 | ### SIMD |
| 43 | |
| 44 | The `Vec3A`, `Vec4`, `Quat`, `Mat2`, `Mat3A`, `Mat4`, `Affine2` and `Affine3A` |
| 45 | types use 128-bit wide SIMD vector types for storage on `x86`, `x86_64` and |
| 46 | `wasm32` architectures. As a result, these types are all 16 byte aligned and |
| 47 | depending on the size of the type or the type's members, they may contain |
| 48 | internal padding. This results in some wasted space in the cases of `Vec3A`, |
| 49 | `Mat3A`, `Affine2` and `Affine3A`. However, the use of SIMD generally results |
| 50 | in better performance than scalar math. |
| 51 | |
| 52 | `glam` outperforms similar Rust libraries for common operations as tested by the |
| 53 | [`mathbench`][mathbench] project. |
| 54 | |
| 55 | [mathbench]: https://github.com/bitshifter/mathbench-rs |
| 56 | |
| 57 | ### Enabling SIMD |
| 58 | |
| 59 | SIMD is supported on `x86`, `x86_64` and `wasm32` targets. |
| 60 | |
| 61 | * `SSE2` is enabled by default on `x86_64` targets. |
| 62 | * To enable `SSE2` on `x86` targets add `-C target-feature=+sse2` to |
| 63 | `RUSTCFLAGS`. |
| 64 | * `NEON` is enabled by default on `aarch64` targets. |
| 65 | * To enable `NEON` on `aarch64` targets add `-C target-feature=+neon` to |
| 66 | `RUSTFLAGS`. |
| 67 | * To enable `simd128` on `wasm32` targets add `-C target-feature=+simd128` to |
| 68 | `RUSTFLAGS`. |
| 69 | * Experimental [portable simd] support can be enabled with the `core-simd` |
| 70 | feature. This requires the nightly compiler as it is still unstable in Rust. |
| 71 | |
| 72 | Note that SIMD on `wasm32` passes tests but has not been benchmarked, |
| 73 | performance may or may not be better than scalar math. |
| 74 | |
| 75 | [portable simd]: https://doc.rust-lang.org/core/simd/index.html |
| 76 | |
| 77 | ### `no_std` support |
| 78 | |
| 79 | `no_std` support can be enabled by compiling with `--no-default-features` to |
| 80 | disable `std` support and `--features libm` for math functions that are only |
| 81 | defined in `std`. For example: |
| 82 | |
| 83 | ```toml |
| 84 | [dependencies] |
| 85 | glam = { version = "0.30.10", default-features = false, features = ["libm"] } |
| 86 | ``` |
| 87 | |
| 88 | To support both `std` and `no_std` builds in project, you can use the following |
| 89 | in your `Cargo.toml`: |
| 90 | |
| 91 | ```toml |
| 92 | [features] |
| 93 | default = ["std"] |
| 94 | |
| 95 | std = ["glam/std"] |
| 96 | libm = ["glam/libm"] |
| 97 | |
| 98 | [dependencies] |
| 99 | glam = { version = "0.30.10", default-features = false } |
| 100 | ``` |
| 101 | |
| 102 | Alternatively, you can use the `nostd-libm` feature. This will always include a |
| 103 | `libm` dependency, but allows the user to still override it with `std` if they |
| 104 | prefer. This will allow your crate to compile with default features disabled, |
| 105 | instead of forcing the user to enable either `std` or `libm`. |
| 106 | |
| 107 | ```toml |
| 108 | [features] |
| 109 | default = ["std"] |
| 110 | |
| 111 | std = ["glam/std"] |
| 112 | libm = ["glam/libm"] |
| 113 | |
| 114 | [dependencies] |
| 115 | glam = { version = "0.30.10", default-features = false, features = ["nostd-libm"] } |
| 116 | ``` |
| 117 | |
| 118 | ### Optional features |
| 119 | |
| 120 | * [`approx`] - traits and macros for approximate float comparisons |
| 121 | * [`arbitrary`] - `arbitrary` trait implementations for `glam` types. |
| 122 | * [`bytemuck`] - for casting into slices of bytes |
| 123 | * [`encase`] - `encase` trait implementations for `glam` types. |
| 124 | * [`libm`] - uses `libm` math functions instead of `std` |
| 125 | * [`mint`] - for interoperating with other 3D math libraries |
| 126 | * [`rand`] - implementations of `Distribution` trait for all `glam` types. |
| 127 | * [`rkyv`] - implementations of `Archive`, `Serialize` and `Deserialize` for all |
| 128 | `glam` types. Note that serialization is not interoperable with and without |
| 129 | the `scalar-math` feature. It should work between all other builds of `glam`. |
| 130 | Endian conversion is currently not supported |
| 131 | * [`bytecheck`] - to perform archive validation when using the `rkyv` feature |
| 132 | * [`serde`] - implementations of `Serialize` and `Deserialize` for all `glam` |
| 133 | types. Note that serialization should work between builds of `glam` with and |
| 134 | without SIMD enabled |
| 135 | * [`speedy`] - implementations of `speedy`'s `Readable` and `Writable` for all |
| 136 | `glam` types. |
| 137 | * [`zerocopy`] - implementations of zerocopy traits for safe transmutes. |
| 138 | |
| 139 | [`approx`]: https://docs.rs/approx |
| 140 | [`arbitrary`]: https://docs.rs/arbitrary |
| 141 | [`bytecheck`]: https://github.com/rkyv/bytecheck |
| 142 | [`bytemuck`]: https://docs.rs/bytemuck |
| 143 | [`encase`]: https://github.com/teoxoy/encase |
| 144 | [`libm`]: https://github.com/rust-lang/libm |
| 145 | [`mint`]: https://github.com/kvark/mint |
| 146 | [`rand`]: https://github.com/rust-random/rand |
| 147 | [`rkyv`]: https://github.com/rkyv/rkyv |
| 148 | [`serde`]: https://serde.rs |
| 149 | [`speedy`]: https://docs.rs/speedy |
| 150 | [`zerocopy`]: https://github.com/google/zerocopy |
| 151 | |
| 152 | ### Feature gates |
| 153 | |
| 154 | * `std` - the default feature, has no dependencies. |
| 155 | * `nostd-libm` - uses `libm` math functions if `std` is not available |
| 156 | * `scalar-math` - compiles with SIMD support disabled |
| 157 | * `debug-glam-assert` - adds assertions in debug builds which check the validity |
| 158 | of parameters passed to `glam` to help catch runtime errors |
| 159 | * `glam-assert` - adds validation assertions to all builds |
| 160 | * `cuda` - forces `glam` types to match expected [cuda alignment] |
| 161 | * `fast-math` - By default, glam attempts to provide bit-for-bit identical |
| 162 | results on all platforms. Using this feature will enable platform specific |
| 163 | optimizations that may not be identical to other platforms. **Intermediate |
| 164 | libraries should not use this feature and defer the decision to the final |
| 165 | binary build**. |
| 166 | * `core-simd` - enables SIMD support via the [portable simd] module. This is an |
| 167 | unstable feature which requires a nightly Rust toolchain and `std` support. |
| 168 | |
| 169 | [cuda alignment]: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#built-in-vector-types |
| 170 | |
| 171 | ### Minimum Supported Rust Version (MSRV) |
| 172 | |
| 173 | The minimum supported version of Rust for `glam` is `1.68.2`. |
| 174 | |
| 175 | ## Conventions |
| 176 | |
| 177 | ### Column vectors |
| 178 | |
| 179 | `glam` interprets vectors as column matrices (also known as "column vectors") |
| 180 | meaning when transforming a vector with a matrix the matrix goes on the left, |
| 181 | e.g. `v' = Mv`. DirectX uses row vectors, OpenGL uses column vectors. There |
| 182 | are pros and cons to both. |
| 183 | |
| 184 | ### Column-major order |
| 185 | |
| 186 | Matrices are stored in column major format. Each column vector is stored in |
| 187 | contiguous memory. |
| 188 | |
| 189 | ### Co-ordinate system |
| 190 | |
| 191 | `glam` is co-ordinate system agnostic and intends to support both right-handed |
| 192 | and left-handed conventions. |
| 193 | |
| 194 | ## Design Philosophy |
| 195 | |
| 196 | The design of this library is guided by a desire for simplicity and good |
| 197 | performance. |
| 198 | |
| 199 | * No generics and minimal traits in the public API for simplicity of usage |
| 200 | * All dependencies are optional (e.g. `mint`, `rand` and `serde`) |
| 201 | * Follows the [Rust API Guidelines] where possible |
| 202 | * Aiming for 100% test [coverage] |
| 203 | * Common functionality is benchmarked using [Criterion.rs] |
| 204 | |
| 205 | [Rust API Guidelines]: https://rust-lang-nursery.github.io/api-guidelines/ |
| 206 | [coverage]: coveralls.io |
| 207 | [Criterion.rs]: https://bheisler.github.io/criterion.rs/book/index.html |
| 208 | |
| 209 | ## Architecture |
| 210 | |
| 211 | See [ARCHITECTURE.md] for details on `glam`'s internals. |
| 212 | |
| 213 | [ARCHITECTURE.md]: ARCHITECTURE.md |
| 214 | |
| 215 | ## Inspirations |
| 216 | |
| 217 | There were many inspirations for the interface and internals of glam from the |
| 218 | Rust and C++ worlds. In particular: |
| 219 | |
| 220 | * [How to write a maths library in 2016] inspired the initial `Vec3A` |
| 221 | implementation |
| 222 | * [Realtime Math] - header only C++11 with SSE and NEON SIMD intrinsic support |
| 223 | * [DirectXMath] - header only SIMD C++ linear algebra library for use in games |
| 224 | and graphics apps |
| 225 | * `glam` is a play on the name of the popular C++ library [GLM] |
| 226 | |
| 227 | [How to write a maths library in 2016]: http://www.codersnotes.com/notes/maths-lib-2016/ |
| 228 | [Realtime Math]: https://github.com/nfrechette/rtm |
| 229 | [DirectXMath]: https://docs.microsoft.com/en-us/windows/desktop/dxmath/directxmath-portal |
| 230 | [GLM]: https://glm.g-truc.net |
| 231 | |
| 232 | ## License |
| 233 | |
| 234 | Licensed under either of |
| 235 | |
| 236 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) |
| 237 | or http://www.apache.org/licenses/LICENSE-2.0) |
| 238 | * MIT license ([LICENSE-MIT](LICENSE-MIT) |
| 239 | or http://opensource.org/licenses/MIT) |
| 240 | |
| 241 | at your option. |
| 242 | |
| 243 | ## Contribution |
| 244 | |
| 245 | Contributions in any form (issues, pull requests, etc.) to this project must |
| 246 | adhere to Rust's [Code of Conduct]. |
| 247 | |
| 248 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 249 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 250 | dual licensed as above, without any additional terms or conditions. |
| 251 | |
| 252 | If you are interested in contributing or have a request or suggestion |
| 253 | [start a discussion] on GitHub. See [CONTRIBUTING.md] for more information for |
| 254 | contributors. |
| 255 | |
| 256 | Most code in `glam` is generated, see the [codegen README] for details. |
| 257 | |
| 258 | Thank you to all of the `glam` [contributors]! |
| 259 | |
| 260 | [Code of Conduct]: https://www.rust-lang.org/en-US/conduct.html |
| 261 | [start a discussion]: https://github.com/bitshifter/glam-rs/discussions |
| 262 | [CONTRIBUTING.md]: CONTRIBUTING.md |
| 263 | [codegen README]: codegen/README.md |
| 264 | [contributors]: https://github.com/bitshifter/glam-rs/graphs/contributors |
| 265 | |
| 266 | ## Support |
| 267 | |
| 268 | The [Game Development in Rust Discord] and [Bevy Engine Discord] servers are |
| 269 | not official support channels but can be good places to ask for help with |
| 270 | `glam`. |
| 271 | |
| 272 | [Game Development in Rust Discord]: https://discord.gg/yNtPTb2 |
| 273 | [Bevy Engine Discord]: https://discord.gg/gMUk5Ph |
| 274 | |
| 275 | ## Attribution |
| 276 | |
| 277 | `glam` contains code ported from the following C++ libraries: |
| 278 | |
| 279 | * [DirectXMath] - MIT License - Copyright (c) 2011-2020 Microsoft Corp |
| 280 | * [Realtime Math] - MIT License - Copyright (c) 2018 Nicholas Frechette |
| 281 | * [GLM] - MIT License - Copyright (c) 2005 - G-Truc Creation |
| 282 | |
| 283 | See [ATTRIBUTION.md] for details. |
| 284 | |
| 285 | [ATTRIBUTION.md]: ATTRIBUTION.md |
| 286 | |
| 287 | [Build Status]: https://github.com/bitshifter/glam-rs/actions/workflows/ci.yml/badge.svg |
| 288 | [github-ci]: https://github.com/bitshifter/glam-rs/actions/workflows/ci.yml |
| 289 | [Coverage Status]: https://coveralls.io/repos/github/bitshifter/glam-rs/badge.svg?branch=main |
| 290 | [coveralls.io]: https://coveralls.io/github/bitshifter/glam-rs?branch=main |
| 291 | [Latest Version]: https://img.shields.io/crates/v/glam.svg |
| 292 | [crates.io]: https://crates.io/crates/glam/ |
| 293 | [docs]: https://docs.rs/glam/badge.svg |
| 294 | [docs.rs]: https://docs.rs/glam/ |
| 295 | [Minimum Supported Rust Version]: https://img.shields.io/badge/Rust-1.68.2-blue?color=fc8d62&logo=rust |
| 296 | [Rust 1.68.2]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1682-2023-03-28 |