1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*! # glam `glam` is a simple and fast linear algebra library for games and graphics. ## Features `glam` is built with SIMD in mind. Currently only SSE2 on x86/x86_64 is supported as this is what stable Rust supports. * Single precision float (`f32`) support only * SSE2 storage and optimization for many types, including `Mat2`, `Mat4`, `Quat`, `Vec3A` and `Vec4` * Scalar fallback implementations exist when SSE2 is not available * Most functionality includes unit tests and benchmarks ## Linear algebra conventions `glam` interprets vectors as column matrices (also known as "column vectors") meaning when transforming a vector with a matrix the matrix goes on the left. ``` use glam::{Mat3, Vec3}; let m = Mat3::identity(); let x = Vec3::unit_x(); let v = m * x; assert_eq!(v, x); ``` Matrices are stored in memory in column-major order. Rotations follow left-hand rule. The direction of the axis gives the direction of rotation: with the left thumb pointing in the positive direction of the axis the left fingers curl around the axis in the direction of the rotation. ``` use glam::{Mat3, Vec3}; // rotate +x 90 degrees clockwise around y giving -z let m = Mat3::from_rotation_y(90.0_f32.to_radians()); let v = m * Vec3::unit_x(); assert!(v.abs_diff_eq(-Vec3::unit_z(), core::f32::EPSILON)); ``` ## Size and alignment of types Some `glam` types use SIMD for storage meaning they are 16 byte aligned, these types include `Mat2`, `Mat4`, `Quat`, `Vec3A` and `Vec4`. `Vec3A` is a SIMD optimized version of the `Vec3` type, which due to 16 byte alignment results in `Vec3A` containing 4 bytes of padding making it 16 bytes in size in total. | Type | `f32` bytes | Align bytes | Padding | Size bytes | |:------|------------:|------------:|--------:|-----------:| |`Vec3` | 12| 4| 0| 12| |`Vec3A`| 12| 16| 4| 16| Despite this wasted space the SIMD version tends to outperform the `f32` implementation in [**mathbench**](https://github.com/bitshifter/mathbench-rs) benchmarks. When SSE2 is not available on the target architecture this type will still be 16 byte aligned, so object sizes and layouts will not change between architectures. SIMD support can be disabled entirely using the `scalar-math` feature. This feature will also disable SIMD alignment meaning most types will use native `f32` alignment of 4 bytes. All the main `glam` types are tagged with #[repr(C)], so they are possible to expose as struct members to C interfaces if desired. Be mindful of Vec3A's extra padding though. ## Accessing internal data The SIMD types that `glam` builds on are opaque and their contents are not directly accessible. Because of this all types use getter and setter methods instead of providing direct access, regardless of whether they are using scalar or SIMD storage. ``` use glam::Vec3A; let mut v = Vec3A::new(1.0, 2.0, 3.0); assert_eq!(v.y(), 2.0); v.set_z(1.0); assert_eq!(v.z(), 1.0); *v.x_mut() = 2.0; assert_eq!(v.x(), 2.0); ``` If you need to access multiple elements it is easier to convert the type to a tuple or array: ``` use glam::Vec3A; let v = Vec3A::new(1.0, 2.0, 3.0); let (x, y, z) = v.into(); assert_eq!((x, y, z), (1.0, 2.0, 3.0)); ``` ## SIMD and scalar consistency `glam` types implement `serde` `Serialize` and `Deserialize` traits to ensure that they will serialize and deserialize exactly the same whether or not SIMD support is being used. The SIMD versions implement the `core::fmt::Debug` and `core::fmt::Display` traits so they print the same as the scalar version. ``` use glam::Vec3A; let a = Vec3A::new(1.0, 2.0, 3.0); assert_eq!(format!("{}", a), "[1, 2, 3]"); ``` ## Feature gates All `glam` dependencies are optional, however some are required for tests and benchmarks. * `std` - the default feature, has no dependencies. * `rand` - used to generate random values. Used in benchmarks. * `serde` - used for serialization and deserialization of types. * `mint` - used for interoperating with other linear algebra libraries. * `scalar-math` - disables SIMD support and uses native alignment for all types. * `debug-glam-assert` - adds assertions in debug builds which check the validity of parameters passed to `glam` to help catch runtime errors. * `glam-assert` - adds assertions to all builds which check the validity of parameters passed to `glam` to help catch runtime errors. */ #![doc(html_root_url = "https://docs.rs/glam/0.9.4")] #[macro_use] mod macros; pub mod f32; pub use self::f32::{ mat2, mat3, mat4, quat, vec2, vec3, vec3a, vec4, Mat2, Mat3, Mat4, Quat, Vec2, Vec2Mask, Vec3, Vec3A, Vec3AMask, Vec3Mask, Vec4, Vec4Mask, }; #[cfg(feature = "transform-types")] pub use self::f32::{TransformRT, TransformSRT}; #[repr(align(16))] pub(crate) struct Align16<T>(T); impl<T> Align16<T> { #[allow(dead_code)] pub fn as_ptr(&self) -> *const T { &self.0 } #[allow(dead_code)] pub fn as_mut_ptr(&mut self) -> *mut T { &mut self.0 } } #[test] fn test_align16() { use core::{mem, ptr}; let mut a = Align16::<f32>(1.0); assert_eq!(mem::align_of_val(&a), 16); unsafe { assert_eq!(ptr::read(a.as_ptr()), 1.0); ptr::write(a.as_mut_ptr(), -1.0); } assert_eq!(a.0, -1.0); }