Debian Codegraph @

debian/missing-sources/glam-0.24.2/src/lib.rs

raw symbols SCIP
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
32 and/or implementation. The use of SIMD generally enables better performance than using primitive
33 numeric types such as `f32`.
34
35 Some `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
37 with an `A` suffix are a SIMD alternative to a scalar type, e.g. `Vec3` uses `f32` storage and
38 `Vec3A` uses SIMD storage.
39
40 When SIMD is not available on the target the types will maintain 16 byte alignment and internal
41 padding so that object sizes and layouts will not change between architectures. There are scalar
42 math fallback implementations exist when SIMD is not available. It is intended to add support for
43 other SIMD architectures once they appear in stable Rust.
44
45 Currently 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
50 in `Vec3A` containing 4 bytes of padding making it 16 bytes in size in total. `Mat3A` is composed
51 of 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
60 Despite 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.
64 When methods need to return a 3D vector they will generally return [`Vec3`].
65
66 There are [`From`] trait implementations for converting from [`Vec4`] to a [`Vec3A`] and between
67 [`Vec3`] and [`Vec3A`] (and vice versa).
68
69 ```
70 use glam::{Vec3, Vec3A, Vec4};
71
72 let 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.
75 let v3a = Vec3A::from(v4);
76 assert_eq!(Vec3A::new(1.0, 2.0, 3.0), v3a);
77
78 // Convert from `Vec3A` to `Vec3`.
79 let v3 = Vec3::from(v3a);
80 assert_eq!(Vec3::new(1.0, 2.0, 3.0), v3);
81
82 // Convert from `Vec3` to `Vec3A`.
83 let v3a = Vec3A::from(v3);
84 assert_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
90 represent 2D and 3D affine transformations which are commonly used in games.
91
92 The 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
101 Performance is much closer between `Mat4` and `Affine3A` with the affine type being faster to
102 invert.
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
113 Benchmarks 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
118 transforming a vector with a matrix the matrix goes on the left.
119
120 ```
121 use glam::{Mat3, Vec3};
122 let m = Mat3::IDENTITY;
123 let x = Vec3::X;
124 let v = m * x;
125 assert_eq!(v, x);
126 ```
127
128 Matrices are stored in memory in column-major order.
129
130 All angles are in radians. Rust provides the `f32::to_radians()` and `f64::to_radians()` methods to
131 convert from degrees.
132
133 ## Direct element access
134
135 Because some types may internally be implemented using SIMD types, direct access to vector elements
136 is supported by implementing the [`Deref`] and [`DerefMut`] traits.
137
138 ```
139 use glam::Vec3A;
140 let mut v = Vec3A::new(1.0, 2.0, 3.0);
141 assert_eq!(3.0, v.z);
142 v.z += 1.0;
143 assert_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
152 require normalized vectors as input such as `Quat::from_axis_angle(axis, angle)` will not check
153 that 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
155 are valid.
156
157 ## Vector swizzles
158
159 `glam` vector types have functions allowing elements of vectors to be reordered, this includes
160 creating a vector of a different size from the vectors elements.
161
162 The swizzle functions are implemented using traits to add them to each vector type. This is
163 primarily because there are a lot of swizzle functions which can obfuscate the other vector
164 functions in documentation and so on. The traits are [`Vec2Swizzles`], [`Vec3Swizzles`] and
165 [`Vec4Swizzles`].
166
167 Note that the [`Vec3Swizzles`] implementation for [`Vec3A`] will return a [`Vec3A`] for 3 element
168 swizzles, all other implementations will return [`Vec3`].
169
170 ```
171 use glam::{swizzles::*, Vec2, Vec3, Vec3A, Vec4};
172
173 let 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.
176 let wzyx = v.wzyx();
177 assert_eq!(Vec4::new(4.0, 3.0, 2.0, 1.0), wzyx);
178
179 // Swizzle the yzw elements of `v` into a `Vec3`
180 let yzw = v.yzw();
181 assert_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`.
186 let yzw = Vec3A::from(v.yzwx());
187 assert_eq!(Vec3A::new(2.0, 3.0, 4.0), yzw);
188
189 // You can swizzle from a `Vec4` to a `Vec2`
190 let xy = v.xy();
191 assert_eq!(Vec2::new(1.0, 2.0), xy);
192
193 // And back again
194 let yyxx = xy.yyxx();
195 assert_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
201 that they will serialize and deserialize exactly the same whether or not
202 SIMD support is being used.
203
204 The SIMD versions implement the `core::fmt::Debug` and `core::fmt::Display`
205 traits so they print the same as the scalar version.
206
207 ```
208 use glam::Vec4;
209 let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
210 assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
211 ```
212
213 ## Feature gates
214
215 All `glam` dependencies are optional, however some are required for tests
216 and 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
247 The 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]
267 mod macros;
268
269 mod align16;
270 mod deref;
271 mod euler;
272 mod features;
273
274 #[cfg(target_arch = "spirv")]
275 mod spirv;
276
277 #[cfg(all(
278 target_feature = "sse2",
279 not(any(feature = "core-simd", feature = "scalar-math"))
280 ))]
281 mod sse2;
282
283 #[cfg(all(
284 target_feature = "simd128",
285 not(any(feature = "core-simd", feature = "scalar-math"))
286 ))]
287 mod wasm32;
288
289 #[cfg(all(feature = "core-simd", not(feature = "scalar-math")))]
290 mod coresimd;
291
292 #[cfg(all(
293 target_feature = "sse2",
294 not(any(feature = "core-simd", feature = "scalar-math"))
295 ))]
296 use align16::Align16;
297
298 /** `bool` vector mask types. */
299 pub mod bool;
300 pub use self::bool::*;
301
302 /** `f32` vector, quaternion and matrix types. */
303 pub mod f32;
304 pub use self::f32::*;
305
306 /** `f64` vector, quaternion and matrix types. */
307 pub mod f64;
308 pub use self::f64::*;
309
310 /** `i32` vector types. */
311 pub mod i32;
312 pub use self::i32::*;
313
314 /** `u32` vector types. */
315 pub mod u32;
316 pub use self::u32::*;
317
318 /** `i64` vector types. */
319 pub mod i64;
320 pub use self::i64::*;
321
322 /** `u64` vector types. */
323 pub mod u64;
324 pub use self::u64::*;
325
326 /** Traits adding swizzle methods to all vector types. */
327 pub mod swizzles;
328 pub use self::swizzles::{Vec2Swizzles, Vec3Swizzles, Vec4Swizzles};
329
330 /** Rotation Helper */
331 pub use euler::EulerRot;