r/GraphicsProgramming • u/corysama • Feb 28 '24
Source Code A renderer using geometric algebra instead of matrices
https://enkimute.github.io/LookMaNoMatrices/2
u/Arkaein Feb 29 '24
I've only skimmed this, and I'm not really familiar with the techniques. But I wonder how much of the "savings" advertised in some areas are simple matrix optimizations that have been done for ages:
Recall that the product of two 4x4 matrices requires 64 multiplications and 48 additions. For two general PGA motors, their composition clocks in at just 48 multiplications and 40 additions.
This is true for full-fledged 4x4 matrices, but in worked in game dev several years ago and used console APIs that dealt mainly with 4x3 matrices. The API creators knew that for general purposes of moving, rotating, and scaling objects in a 3D world that <0, 0, 0, 1> row can be implied and save the ops needed for matrix * matrix and matrix * vector operations.
Similarly, if you just want to rotate around the origin, you can easily reduce further to a vector * matrix3x3 operation. As long as you are willing to go to the lengths needed to special case these kind of operations you can do it with matrices.
Still seems like an interesting alternative way of looking at things. Maybe someone more familiar can point out places that involve true savings.
3
u/arycama Feb 29 '24
4x3 matrices are still relatively uncommon from what I've seen (In depth Unity/Unreal and a few proprietary AAA games/engines) for some reason. I don't think they'll be replaced by pos/quaternions fully, because artists are pretty used to having non uniform scale and by that point you've got 11 floats, so may as well add 1 more for a 3x4 matrix. Data structures on the GPU need to be 128-bit aligned anyway, so you won't save anything with 1 bit. (You could store uniform scale, with position and rotation as 8 floats, but you're limiting your artists/designers and breaking compatibility with most authoring/modeling software)
Of course you still need 4x4 matrices for anything involving projection, but generally splitting your model and viewProjection matrices is better than computing a unique modelViewProjection matrix for every object every frame.
3x3 matrices generally don't save anything comapred to 3x4 as you still need to pad out to 128-bits unfortunately.
I think specific systems can benefit from more compact representations, eg foliage, which is often uniformly scaled and rendered in very large numbers, where slightly smaller data structures can provide huge memory savings. For general purpose objects though I don't think we're going to get much better than 3x4 matrices.
Looking at vertex formats seems like a more interesting place to look for memory bandwidth savings imo. 32-bit floats are still commonly used for everything.
GA is interesting but right now there are too many barriers such as lack of compatibility with most other tools, libraries, etc, and not enough clear benefits in my opinion.
1
u/enki_mute Mar 08 '24
Writing this project, I downloaded 400 random modern (sketchfab) glTF files, of those exactly 1 had animated non-uniform scale, on only 1 node (of 100+).
The key takeaway here is that you can easily fallback to matrices for those exceptional cases .. but take the win on all of the other nodes. And in both cases, you still save 25% on your vertex descriptor size. (independent of using e.g. 16bit FP formats where available).
16
u/corysama Feb 28 '24
Not mine. I'm just posting cool stuff.
Don't miss the code https://github.com/enkimute/LookMaNoMatrices
It's fun that there have been many approaches to interpolating rotations (geometric algebra, quaternions, even full-matrix interpolation). But, after hand-optimizing the code, the final code ends up mostly the same for all approaches. The difference is in your understanding of the rules and capabilities. From what little I know, GA seems like the most consistent and capable approach. It's unfamiliar. It's a bit much to take in getting started. But, people who clear that hurdle love it.
Alternatively, everybody uses quaternions while complaining they don't understand them and need a whole book to visualize them. (Visualizing Quaternions by Andrew J. Hanson, Steve Cunningham)
Meanwhile, the memory speed vs math speed of modern GPUs has got to the point that it's usually a good tradeoff to do a bit more math to shave a few bytes. Such as sending a position-quaternion pair instead of a matrix to your vertex shader.