I’ll be using this as a project thread for updates on Klein.
Klein takes a different approach from GAL in that it specializes in \mathbf{P}(\mathbb{R}^*_{3, 0, 1}) (with \mathbf{P}(\mathbb{R}^*_{4, 0, 0}) for perspectivities coming soon hopefully). This enables me to optimize the code even more aggressively and I don’t have to rely on compiler autovectorization (which is still not in a state I would consider suitable for applications that are real-time and demand it).
As a result, you can see from the perf analysis page that Klein already is ~twice as fast as GLM and faster or equivalent to other existing solutions (not all the analysis has been published on that page yet, it’s something I need to automate). Note that the cycle counts there were simulated for Jaguar (PS4 chip) but I expect similar gains on other x86_64 platforms. In addition, Klein benefits from the GA formulation to support a host of operations not even supported in other libraries. A motor (dual quaternion) constant velocity interpolation (implemented in Klein in terms of the exp/log map) doesn’t exist in GLM, RTM, MathFu, DirectXMath, etc. for example.
I’ve only briefly started to translate the code to shader source code (first GLSL, then using spirv-cross to translate GLSL to HLSL).
To test Klein, I wrote an miniature CAS that does symbolic manipulation to help verify a whole host of operations comprehensively. The testing is bidirectional and results for each operation must agree for both the shipping library and the CAS. Compilation of the CAS (klein_shell) is optional.
I have a working subset of C-bindings that I hope will make it easy to bind to other languages like Javascript/Python/etc that would also benefit from the performance boost. This is still a work in progress as there are a number of operations missing, and this is another situation that would benefit from automation.
Documentation-wise, I’ve started drafting a “workbook” style guide for people learning GA and want to get practice with actual calculations and worked examples. The documentation in the API section of the site is autogenerated from source and tracks changes pushed to the repository very closely.
It’s hard to say exactly what should come next, as there’s so much to do! I think the first order of business for me is to implement the remaining missing functionality from libraries such as GLM and DirectXMath so I can truly say that Klein can replace them wholesale. A priority list for me is something like the following:
- GLM/DirectXMath parity
- More demos for common/interesting problems (camera control, IK, etc)
- GLSL/HLSL port
- SSE2/3 fallback (currently, I’m using
_mm_dp_ps
and_mm_blend_ps
so this shouldn’t be too difficult. - QoL improvements (automatic LLVM-MCA analysis, automatic shader tests, etc).
- Stability flag to prevent possible precision loss in situations where an implied cancellation was used for performance
For now though, I’m posting this because I believe what’s here is already immediately usable, and I hope it takes the game/graphics/etc community a bit closer to a GA formulation! Another goal of mine is to demonstrate that GA isn’t just a “replacement” for libraries that are based on the quat/dual-quat formulation. It’s a material improvement that enables far more operations/optimizations.
Last note, even though this is a project thread, anyone is welcome to provide feedback, ask questions, or comment.
Cheers,
Jeremy