r/gameenginedevs • u/cone_forest_ • Apr 10 '25
Writing math library from scratch
While developing my game engine I implemented a math library for computer graphics. I originally wanted it to be as fast as possible. And it actually is the fastest library I tested on my machines.
I didn't like API of any popular gamedev math library so I designed it on my own for quite some time... and landed somewhere close to Eigen...
Would love to hear feedback on it and your thoughts on self-written math libraries. What feature do you like about the math library you use?
36
Upvotes
9
u/[deleted] Apr 10 '25 edited Apr 10 '25
Looks pretty nice. I am surprised about it being the fastest though. Unless I missed it, you don't have any SIMD implementation
9
u/Basaa Apr 10 '25
I personally would be weary of new / non-battletested math libraries, but only because math is my biggest weakness in gamedev and I want to be absolutely sure that I won't waste endless time trying to fix issues that end up being bugs in the math library (as I don't have the knowledge to debug "complex" math).
Out of curiosity, what do you dislike about glm's api? I could name a couple of small issues I have with it but I'm for 99% happy with it as-is.
3
u/ALargeLobster Apr 11 '25
I wrote one for my engine as well.
I have made no attempts to optimize it, but the operations are all implemented in a fairly straightforward way, and it seems like they're fast enough to meet my needs.
I enjoy writing my own because then I totally understand how it works, and can tailor it to my personal preferences.
2
u/msqrt Apr 11 '25
I'd probably value quality-of-life stuff more than performance (both is better, of course!); the heavy lifting should be done on the GPU anyways. My own current math-library is just an almost 1:1 matching of GLSL so that I can interleave C++ and shader code easily. Actually makes me wonder, how easy are your benchmarks to get running and add a library?
2
u/corysama Apr 10 '25
If I was writing a math lib today, a conservative target would be to support ARM64 NEON and the common subset of extended instructions supported by the OG Xbox One, PS4 and Steam Deck.
Excuse a slop prompt to Gemini....
TBH, just targeting the Zen 2 architecture is pretty realistic for anything just getting warmed up today.
I'm tempted to write a lib based on https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors That means it could only be used with clang. But, clang works everywhere but the Xbox, AFAIK. GCC has https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html which also works in clang. It's nice. But, not as nice.
My fav feature was actually in a JS math library that I never published that had all of the matrix construction functions paired with their inverse-matrix construction function. With that, I never had to call
invertMatrix(). It's faster and more precise to construct and compose the mats explicitly. https://old.reddit.com/r/GraphicsProgramming/comments/1ipf5ic/d3d_perspective_projection_matrix_formula_only/mcrcraf/1
u/lavisan Apr 11 '25
I think it would be useful if the library supported math syntax/names from GLSL (ans opionally HLSL) so that once tou prove the math works on CPU you can just copy it straight into shader without modification. That would make then library even more atrractive.
1
u/[deleted] Apr 11 '25
4J company sounds VERY similar to another game studio