Debian Codegraph @

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