Debian Codegraph @

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