Debian Codegraph @

debian/missing-sources/glam-0.22.0/ARCHITECTURE.md

raw symbols SCIP
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 ### Component access via Deref
74
75 The `Deref` trait is used to provide direct access to SIMD vector components
76 like `.x`, `.y` and so on. The `Deref` implementation will return `XYZ<T>`
77 structure on which the vector components are accessible. Unfortunately if users
78 dereference the public types they will see confusing errors messages about
79 `XYZ` types but this on balance seemed preferable to needing to setter and
80 getting methods to read and write component values.
81
82 ## Code generation
83
84 See [codegen/README.md] for information on `glam`'s code generation process.