revier_glam/lib.rs
1/*!
2# glam
3
4`glam` is a simple and fast linear algebra library for games and graphics.
5
6## Features
7
8* [`f32`](mod@f32) types
9 * vectors: [`Vec2`], [`Vec3`], [`Vec3A`] and [`Vec4`]
10 * square matrices: [`Mat2`], [`Mat3`], [`Mat3A`] and [`Mat4`]
11 * a quaternion type: [`Quat`]
12 * affine transformation types: [`Affine2`] and [`Affine3A`]
13* [`f64`](mod@f64) types
14 * vectors: [`DVec2`], [`DVec3`] and [`DVec4`]
15 * square matrices: [`DMat2`], [`DMat3`] and [`DMat4`]
16 * a quaternion type: [`DQuat`]
17 * affine transformation types: [`DAffine2`] and [`DAffine3`]
18* [`i32`](mod@i32) types
19 * vectors: [`IVec2`], [`IVec3`] and [`IVec4`]
20* [`u32`](mod@u32) types
21 * vectors: [`UVec2`], [`UVec3`] and [`UVec4`]
22* [`i64`](mod@i64) types
23 * vectors: [`I64Vec2`], [`I64Vec3`] and [`I64Vec4`]
24* [`u64`](mod@u64) types
25 * vectors: [`U64Vec2`], [`U64Vec3`] and [`U64Vec4`]
26* [`bool`](mod@bool) types
27 * vectors: [`BVec2`], [`BVec3`] and [`BVec4`]
28
29## SIMD
30
31`glam` is built with SIMD in mind. Many `f32` types use 128-bit SIMD vector types for storage
32and/or implementation. The use of SIMD generally enables better performance than using primitive
33numeric types such as `f32`.
34
35Some `glam` types use SIMD for storage meaning they are 16 byte aligned, these types include
36`Mat2`, `Mat3A`, `Mat4`, `Quat`, `Vec3A`, `Vec4`, `Affine2` an `Affine3A`. Types
37with an `A` suffix are a SIMD alternative to a scalar type, e.g. `Vec3` uses `f32` storage and
38`Vec3A` uses SIMD storage.
39
40When SIMD is not available on the target the types will maintain 16 byte alignment and internal
41padding so that object sizes and layouts will not change between architectures. There are scalar
42math fallback implementations exist when SIMD is not available. It is intended to add support for
43other SIMD architectures once they appear in stable Rust.
44
45Currently only SSE2 on x86/x86_64 is supported as this is what stable Rust supports.
46
47## Vec3A and Mat3A
48
49`Vec3A` is a SIMD optimized version of the `Vec3` type, which due to 16 byte alignment results
50in `Vec3A` containing 4 bytes of padding making it 16 bytes in size in total. `Mat3A` is composed
51of three `Vec3A` columns.
52
53| Type | `f32` bytes | Align bytes | Size bytes | Padding |
54|:-----------|------------:|------------:|-----------:|--------:|
55|[`Vec3`] | 12| 4| 12| 0|
56|[`Vec3A`] | 12| 16| 16| 4|
57|[`Mat3`] | 36| 4| 36| 0|
58|[`Mat3A`] | 36| 16| 48| 12|
59
60Despite this wasted space the SIMD implementations tend to outperform `f32` implementations in
61[**mathbench**](https://github.com/bitshifter/mathbench-rs) benchmarks.
62
63`glam` treats [`Vec3`] as the default 3D vector type and [`Vec3A`] a special case for optimization.
64When methods need to return a 3D vector they will generally return [`Vec3`].
65
66There are [`From`] trait implementations for converting from [`Vec4`] to a [`Vec3A`] and between
67[`Vec3`] and [`Vec3A`] (and vice versa).
68
69```
70use glam::{Vec3, Vec3A, Vec4};
71
72let v4 = Vec4::new(1.0, 2.0, 3.0, 4.0);
73
74// Convert from `Vec4` to `Vec3A`, this is a no-op if SIMD is supported.
75let v3a = Vec3A::from(v4);
76assert_eq!(Vec3A::new(1.0, 2.0, 3.0), v3a);
77
78// Convert from `Vec3A` to `Vec3`.
79let v3 = Vec3::from(v3a);
80assert_eq!(Vec3::new(1.0, 2.0, 3.0), v3);
81
82// Convert from `Vec3` to `Vec3A`.
83let v3a = Vec3A::from(v3);
84assert_eq!(Vec3A::new(1.0, 2.0, 3.0), v3a);
85```
86
87## Affine2 and Affine3A
88
89`Affine2` and `Affine3A` are composed of a linear transform matrix and a vector translation. The
90represent 2D and 3D affine transformations which are commonly used in games.
91
92The table below shows the performance advantage of `Affine2` over `Mat3A` and `Mat3A` over `Mat3`.
93
94| operation | `Mat3` | `Mat3A` | `Affine2` |
95|--------------------|-------------|------------|------------|
96| inverse | 11.4±0.09ns | 7.1±0.09ns | 5.4±0.06ns |
97| mul self | 10.5±0.04ns | 5.2±0.05ns | 4.0±0.05ns |
98| transform point2 | 2.7±0.02ns | 2.7±0.03ns | 2.8±0.04ns |
99| transform vector2 | 2.6±0.01ns | 2.6±0.03ns | 2.3±0.02ns |
100
101Performance is much closer between `Mat4` and `Affine3A` with the affine type being faster to
102invert.
103
104| operation | `Mat4` | `Affine3A` |
105|--------------------|-------------|-------------|
106| inverse | 15.9±0.11ns | 10.8±0.06ns |
107| mul self | 7.3±0.05ns | 7.0±0.06ns |
108| transform point3 | 3.6±0.02ns | 4.3±0.04ns |
109| transform point3a | 3.0±0.02ns | 3.0±0.04ns |
110| transform vector3 | 4.1±0.02ns | 3.9±0.04ns |
111| transform vector3a | 2.8±0.02ns | 2.8±0.02ns |
112
113Benchmarks were taken on an Intel Core i7-4710HQ.
114
115## Linear algebra conventions
116
117`glam` interprets vectors as column matrices (also known as column vectors) meaning when
118transforming a vector with a matrix the matrix goes on the left.
119
120```
121use glam::{Mat3, Vec3};
122let m = Mat3::IDENTITY;
123let x = Vec3::X;
124let v = m * x;
125assert_eq!(v, x);
126```
127
128Matrices are stored in memory in column-major order.
129
130All angles are in radians. Rust provides the `f32::to_radians()` and `f64::to_radians()` methods to
131convert from degrees.
132
133## Direct element access
134
135Because some types may internally be implemented using SIMD types, direct access to vector elements
136is supported by implementing the [`Deref`] and [`DerefMut`] traits.
137
138```
139use glam::Vec3A;
140let mut v = Vec3A::new(1.0, 2.0, 3.0);
141assert_eq!(3.0, v.z);
142v.z += 1.0;
143assert_eq!(4.0, v.z);
144```
145
146[`Deref`]: https://doc.rust-lang.org/std/ops/trait.Deref.html
147[`DerefMut`]: https://doc.rust-lang.org/std/ops/trait.DerefMut.html
148
149## glam assertions
150
151`glam` does not enforce validity checks on method parameters at runtime. For example methods that
152require normalized vectors as input such as `Quat::from_axis_angle(axis, angle)` will not check
153that axis is a valid normalized vector. To help catch unintended misuse of `glam` the
154`debug-glam-assert` or `glam-assert` features can be enabled to add checks ensure that inputs to
155are valid.
156
157## Vector swizzles
158
159`glam` vector types have functions allowing elements of vectors to be reordered, this includes
160creating a vector of a different size from the vectors elements.
161
162The swizzle functions are implemented using traits to add them to each vector type. This is
163primarily because there are a lot of swizzle functions which can obfuscate the other vector
164functions in documentation and so on. The traits are [`Vec2Swizzles`], [`Vec3Swizzles`] and
165[`Vec4Swizzles`].
166
167Note that the [`Vec3Swizzles`] implementation for [`Vec3A`] will return a [`Vec3A`] for 3 element
168swizzles, all other implementations will return [`Vec3`].
169
170```
171use glam::{swizzles::*, Vec2, Vec3, Vec3A, Vec4};
172
173let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
174
175// Reverse elements of `v`, if SIMD is supported this will use a vector shuffle.
176let wzyx = v.wzyx();
177assert_eq!(Vec4::new(4.0, 3.0, 2.0, 1.0), wzyx);
178
179// Swizzle the yzw elements of `v` into a `Vec3`
180let yzw = v.yzw();
181assert_eq!(Vec3::new(2.0, 3.0, 4.0), yzw);
182
183// To swizzle a `Vec4` into a `Vec3A` swizzle the `Vec4` first then convert to
184// `Vec3A`. If SIMD is supported this will use a vector shuffle. The last
185// element of the shuffled `Vec4` is ignored by the `Vec3A`.
186let yzw = Vec3A::from(v.yzwx());
187assert_eq!(Vec3A::new(2.0, 3.0, 4.0), yzw);
188
189// You can swizzle from a `Vec4` to a `Vec2`
190let xy = v.xy();
191assert_eq!(Vec2::new(1.0, 2.0), xy);
192
193// And back again
194let yyxx = xy.yyxx();
195assert_eq!(Vec4::new(2.0, 2.0, 1.0, 1.0), yyxx);
196```
197
198## SIMD and scalar consistency
199
200`glam` types implement `serde` `Serialize` and `Deserialize` traits to ensure
201that they will serialize and deserialize exactly the same whether or not
202SIMD support is being used.
203
204The SIMD versions implement the `core::fmt::Debug` and `core::fmt::Display`
205traits so they print the same as the scalar version.
206
207```
208use glam::Vec4;
209let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
210assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
211```
212
213## Feature gates
214
215All `glam` dependencies are optional, however some are required for tests
216and benchmarks.
217
218* `std` - the default feature, has no dependencies.
219* `approx` - traits and macros for approximate float comparisons
220* `bytemuck` - for casting into slices of bytes
221* `libm` - uses `libm` math functions instead of `std`, required to compile with `no_std`
222* `mint` - for interoperating with other 3D math libraries
223* `rand` - implementations of `Distribution` trait for all `glam` types.
224* `rkyv` - implementations of `Archive`, `Serialize` and `Deserialize` for all
225 `glam` types. Note that serialization is not interoperable with and without the
226 `scalar-math` feature. It should work between all other builds of `glam`.
227 Endian conversion is currently not supported
228* `bytecheck` - to perform archive validation when using the `rkyv` feature
229* `serde` - implementations of `Serialize` and `Deserialize` for all `glam`
230 types. Note that serialization should work between builds of `glam` with and without SIMD enabled
231* `scalar-math` - disables SIMD support and uses native alignment for all types.
232* `debug-glam-assert` - adds assertions in debug builds which check the validity of parameters
233 passed to `glam` to help catch runtime errors.
234* `glam-assert` - adds assertions to all builds which check the validity of parameters passed to
235 `glam` to help catch runtime errors.
236* `cuda` - forces `glam` types to match expected cuda alignment
237* `fast-math` - By default, glam attempts to provide bit-for-bit identical
238 results on all platforms. Using this feature will enable platform specific
239 optimizations that may not be identical to other platforms. **Intermediate
240 libraries should not use this feature and defer the decision to the final
241 binary build**.
242* `core-simd` - enables SIMD support via the portable simd module. This is an
243 unstable feature which requires a nightly Rust toolchain and `std` support.
244
245## Minimum Supported Rust Version (MSRV)
246
247The minimum supported Rust version is `1.58.1`.
248
249*/
250#![doc(html_root_url = "https://docs.rs/glam/0.24.2")]
251#![cfg_attr(not(feature = "std"), no_std)]
252#![cfg_attr(target_arch = "spirv", feature(repr_simd))]
253#![deny(
254 rust_2018_compatibility,
255 rust_2018_idioms,
256 future_incompatible,
257 nonstandard_style
258)]
259// clippy doesn't like `to_array(&self)`
260#![allow(clippy::wrong_self_convention)]
261#![cfg_attr(
262 all(feature = "core-simd", not(feature = "scalar-math")),
263 feature(portable_simd)
264)]
265
266#[macro_use]
267mod macros;
268
269mod align16;
270mod deref;
271mod euler;
272mod features;
273
274#[cfg(target_arch = "spirv")]
275mod spirv;
276
277#[cfg(all(
278 target_feature = "sse2",
279 not(any(feature = "core-simd", feature = "scalar-math"))
280))]
281mod sse2;
282
283#[cfg(all(
284 target_feature = "simd128",
285 not(any(feature = "core-simd", feature = "scalar-math"))
286))]
287mod wasm32;
288
289#[cfg(all(feature = "core-simd", not(feature = "scalar-math")))]
290mod coresimd;
291
292#[cfg(all(
293 target_feature = "sse2",
294 not(any(feature = "core-simd", feature = "scalar-math"))
295))]
296use align16::Align16;
297
298/** `bool` vector mask types. */
299pub mod bool;
300pub use self::bool::*;
301
302/** `f32` vector, quaternion and matrix types. */
303pub mod f32;
304pub use self::f32::*;
305
306/** `f64` vector, quaternion and matrix types. */
307pub mod f64;
308pub use self::f64::*;
309
310/** `i32` vector types. */
311pub mod i32;
312pub use self::i32::*;
313
314/** `u32` vector types. */
315pub mod u32;
316pub use self::u32::*;
317
318/** `i64` vector types. */
319pub mod i64;
320pub use self::i64::*;
321
322/** `u64` vector types. */
323pub mod u64;
324pub use self::u64::*;
325
326/** Traits adding swizzle methods to all vector types. */
327pub mod swizzles;
328pub use self::swizzles::{Vec2Swizzles, Vec3Swizzles, Vec4Swizzles};
329
330/** Rotation Helper */
331pub use euler::EulerRot;
332
333/** A trait for extending [`prim@f32`] and [`prim@f64`] with extra methods. */
334mod float;
335pub use float::FloatExt;