| 1 | # Architecture |
| 2 | |
| 3 | This document describes the high-level architecture of `glam`. While `glam` is |
| 4 | not a large library there are some complexities to its implementation. The |
| 5 | rational and explanation of these follows. |
| 6 | |
| 7 | ## Design goals |
| 8 | |
| 9 | There overarching design goals of glam are: |
| 10 | |
| 11 | * Good out of the box performance using SIMD when available |
| 12 | * Has a simple public interface |
| 13 | * Is fast to compile |
| 14 | * Follow Rust [standard library] conventions and [API guidelines] where possible |
| 15 | * High quality [rustdoc] generated document |
| 16 | |
| 17 | [standard library]: https://doc.rust-lang.org/std/index.html |
| 18 | [API guidelines]: https://rust-lang.github.io/api-guidelines |
| 19 | [rustdoc]: https://doc.rust-lang.org/rustdoc/index.html |
| 20 | |
| 21 | ### SIMD |
| 22 | |
| 23 | One of the core premises of `glam` was that using SSE2 instructions on `x86` and |
| 24 | `x86_64` architectures gave better performance than using Rust's built in `f32` |
| 25 | type. For more on this finding see [Optimising path tracing with SIMD]. |
| 26 | |
| 27 | I also wanted to have a `f32` fallback when SIMD was not available. |
| 28 | |
| 29 | [Optimising path tracing with SIMD]: https://bitshifter.github.io/2018/06/04/simd-path-tracing/#converting-vec3-to-sse2. |
| 30 | |
| 31 | ### No generics |
| 32 | |
| 33 | Because internally storage could be a SIMD vector intrinsic like `__m128` on |
| 34 | `x86` or say an array of `f32` if SSE2 was not available, a simple generic |
| 35 | parameter like `Vec4<T>` could not be used. The `T` would specify the public |
| 36 | facing type, but not storage. Perhaps this could be achieved with a second |
| 37 | generic parameter for storage, e.g. `Vec4<f32, __m128>` or `Vec4<f32, [f32; 4]>` |
| 38 | but I felt that such a design would introduce a lot of complexity that end users |
| 39 | would ultimately be burdened with, so it's not something that was pursued. |
| 40 | |
| 41 | Generics can also increase compile time and code size which is something glam |
| 42 | wants to avoid. |
| 43 | |
| 44 | ### No traits |
| 45 | |
| 46 | `glam` also mostly avoids using traits in the public interface. Primarily |
| 47 | because there wasn't a good reason to. A `Vec3` is not an interface, it is a |
| 48 | concrete type. The secondary reason is traits fragment documentation. If the |
| 49 | functionality of a `Vec3` is implemented across a number of different traits |
| 50 | then the documentation of all of the `Vec3` methods will be on the individual |
| 51 | traits, not the `Vec3` itself. This makes it harder for users to find what |
| 52 | methods a struct actually implements as the documentation is not in one place. |
| 53 | |
| 54 | Conversely `glam` does use traits for swizzle methods so that the documentation |
| 55 | for these methods is on the trait and not the `Vec2`, `Vec3`, `Vec4` and so on |
| 56 | structs. There are many swizzle methods which would clutter the documentation, |
| 57 | making them a trait means they won't pollute documentation. |
| 58 | |
| 59 | ### Support common primitives |
| 60 | |
| 61 | Initially `glam` only supported `f32` which kept the internal implementation |
| 62 | relatively simple. However users also wanted support for other primitives types |
| 63 | like `f64`, `i32` and `u32`. Because `glam` avoids using `generics` adding |
| 64 | support for other primitive types without a lot of code duplication required |
| 65 | some additional complexity in implementation. |
| 66 | |
| 67 | ## High level structure |
| 68 | |
| 69 | `glam` supports a number of permutations of vector, quaternion and matrix types |
| 70 | for `f32`, `f64`, `i32` and `u32` primitives, with SSE2 or wasm32 for some `f32` |
| 71 | types and scalar fallbacks if SIMD is not available. |
| 72 | |
| 73 | This is done with a combination of Rust macros for generating the public facing |
| 74 | types and documentation, e.g. `Vec4` and inner storage types which have a number |
| 75 | of traits implemented for different kinds of storage. |
| 76 | |
| 77 | ### Inner types and traits |
| 78 | |
| 79 | Many `glam` types may use SIMD storage where available, e.g. `Vec4` might use |
| 80 | `__m128` for storage if available or an inner storage struct `XYZW<f32>` for |
| 81 | the scalar implementation. |
| 82 | |
| 83 | There are a number of internal traits defined in `core::traits` for scalar, |
| 84 | vector, matrix and quaternion functionality that glam needs. These traits are |
| 85 | implemented for the different storage types, e.g. |
| 86 | `core::traits::vector::Vector4` has an implementations for `__m128`, `simd128` |
| 87 | and the `XYZW` struct. |
| 88 | |
| 89 | The traits will provide default definitions where possible so not every trait |
| 90 | method needs to be implemented for every storage type. |
| 91 | |
| 92 | ### Component access via Deref |
| 93 | |
| 94 | Because `glam` uses an inner storage type which could be a simple struct or a |
| 95 | SIMD vector it is not possible to provide direct access to the vector's |
| 96 | component values (e.g. `.x`, `.y`, and `.z`). The `Deref` trait is used to work |
| 97 | around this and provide direct access to vector components like `.x`, `.y` and |
| 98 | so on. The `Deref` implementation will return `XYZ<T>` structure on which the |
| 99 | vector components are accessible. Unfortunately if users dereference the public |
| 100 | types they will see confusing errors messages about `XYZ` types but this on |
| 101 | balance seemed preferable to needing to setter and getting methods to read and |
| 102 | write component values. |
| 103 | |
| 104 | ### Public types and macros |
| 105 | |
| 106 | Macros are used to generate the public types to reuse common implementation |
| 107 | details where possible. |
| 108 | |
| 109 | Generally the public type `struct` is declared then it's methods are populated |
| 110 | with multiple macros within a single `impl`. Methods are all declared within a |
| 111 | single `impl` definition for documentation purposes. While it is possible to |
| 112 | have multiple `impl` blocks for the same type this splits the method |
| 113 | documentation generated by `rustdoc`. |
| 114 | |
| 115 | A lot of the motivation for removing duplication is documentation. It is |
| 116 | reasonably easy to find discrepancies in duplicated code through unit testing |
| 117 | but documentation is a lot harder to keep in sync. |
| 118 | |
| 119 | ## A walkthrough of 3D vectors |
| 120 | |
| 121 | 3D vectors are the most complicated case in `glam`. There are 5 different 3D |
| 122 | vector types, including `Vec3`, `Vec3A` (16 byte aligned SIMD), `DVec3`, `IVec3` |
| 123 | and `UVec3`. There is some common code used by all of these types and some that |
| 124 | is specific to the primitives and storage that they implement. Note that there |
| 125 | is also a `BVec3` but as that type is used as a mask it is quite separate. |
| 126 | |
| 127 | ### Storage |
| 128 | |
| 129 | Store for a 3D vector may use `XYZ<T>` where `T` is one of `f32`, `f64`, `i32`, |
| 130 | or `u32` for the scalar case and `__m128` or `simd128` for the SIMD case for |
| 131 | SSE2 or wasm32 respectively. There is also `XYZF32A16` which is used as storage |
| 132 | for `Vec3A` when SIMD is not available. |
| 133 | |
| 134 | ### Traits |
| 135 | |
| 136 | There are quite a few traits involved in implementing a 3D vector. To start with |
| 137 | there are scalar traits which set up some constants and expected operations for |
| 138 | scalar types in the `core::trait::scalar` module: |
| 139 | |
| 140 | * `NumConstsEx` - defines `ZERO` and `ONE` constants |
| 141 | * `FloatConstEx` - defines `NEG_ONE`, `TWO` and `HALF` constants |
| 142 | * `NumEx` - base number trait, implemented for `f32`, `f64`, `i32` and `u32` |
| 143 | * `SignedEx` - signed number trait, implemented for `i32`, `f32` and `f64` |
| 144 | * `FloatEx` - float number trait, implemented for `f32` and `f64` |
| 145 | |
| 146 | Then vector traits are defined in `core::trait::vector`: |
| 147 | |
| 148 | * `VectorConst` - defines `ZERO` and `ONE` constants for vectors |
| 149 | * `Vector3Const` - defines 3D vector constants, `X`, `Y`, and `Z` |
| 150 | * `Vector` - defines methods for any size of vector, typically these methods |
| 151 | can be implemented without needing to know the number of components, e.g. |
| 152 | `splat` |
| 153 | * `SignedVector` - defines methods for signed vectors of any size, e.g. `neg` |
| 154 | * `Vector3` - defines methods for 3D vectors, e.g. `dot` needs to know how many |
| 155 | components to operate on |
| 156 | * `SignedVector3` - defines methods for signed vector types with 3 components, |
| 157 | in this case the default implementation of some methods needs to know the |
| 158 | number of components, e.g. `abs` |
| 159 | * `FloatVector3` - defines methods for float vector types with 3 components, for |
| 160 | when the implementation needs to know the number of components, e.g. `length` |
| 161 | |
| 162 | Note that the `Vector<T>` trait also has an associated `Mask` type which is the |
| 163 | type used for returning from comparison operators. Different types are used for |
| 164 | scalar and SIMD types. |
| 165 | |
| 166 | ### Macros |
| 167 | |
| 168 | The different 3D vector types are declared in the `vec3` module and macros are |
| 169 | used to implement the majority of their methods. Macros specific to 3D vectors |
| 170 | are found in the `vec3` module, most macros call other macros. |
| 171 | |
| 172 | * `impl_f32_vec3` - implements methods and traits common to `Vec3` and `Vec3A` |
| 173 | * `impl_vec3_float_methods` - implements methods for 3D vectors of floats |
| 174 | * `impl_vec3_signed_methods` - implements methods for 3D vectors of signed types |
| 175 | * `impl_vec3_common_methods` - implements common methods for 3D vectors |
| 176 | * `impl_vec3_float_traits` - implements traits for 3D vector float operators |
| 177 | * `impl_vec3_common_traits` - implements traits for common 3D vector operators |
| 178 | |
| 179 | Macros that define functionality to vectors of any size are found in |
| 180 | `src/vec.rs`. These do not call other macros. |
| 181 | |
| 182 | * `impl_vecn_float_methods` - implements common methods for vectors of floats |
| 183 | * `impl_vecn_signed_methods` - implements common methods for vectors of signed |
| 184 | * `imp_vecn_common_methods` - implements common methods for all vector types |
| 185 | * `impl_vecn_signed_traits` - implements trait operators for signed vector types |
| 186 | * `impl_vecn_common_traits` - implements common trait operators for all vectors |
| 187 | |
| 188 | ### Summary |
| 189 | |
| 190 | Functionality is broken down to the point that there is very little duplicated |
| 191 | code. Macros are also used to avoid duplicating comments. Functionality is |
| 192 | implemented through traits operating on different storage types. Common |
| 193 | functionality is often implemented in default trait implementations. |
| 194 | |
| 195 | ## Limitations |
| 196 | |
| 197 | Adding support for types other than `f32` greatly increased the complexity of |
| 198 | the internal implementation of the crate. |
| 199 | |
| 200 | While the current approach works well for keeping the public interface and |
| 201 | documentation simple and clean complexity exists under the surface. The use of |
| 202 | macros unfortunately obfuscates the source code for anyone trying to read it. |
| 203 | The largest issues being when users navigate to glam code in an IDE they are |
| 204 | usually presented with a very high level macro and need to manually hunt down |
| 205 | the actual implementation. The same issue exists when attempting to view source |
| 206 | in the `rustdoc` generated documentation. |
| 207 | |
| 208 | One way to address this would be to use an offline code generator instead of a |
| 209 | compile time code generator (i.e. Rust macros) which would remove macros from |
| 210 | the code that users end up viewing. The main downside is it would take a while |
| 211 | to write this and it might be more confusing for contributors. Another option |
| 212 | might be to improve tooling so that IDEs and rustdoc navigate directly to the |
| 213 | code that was generated by the macro rather than the macro itself. |
| 214 | |
| 215 | The use of `Deref` does occasionally cause confusion. It would be good if it was |
| 216 | only necessary to implement `Deref` on the SIMD types but currently that is not |
| 217 | possible. |