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