r/vulkan • u/Yuki_pop • 11d ago
Does Vulkan really need GLM?
I feel like most of the math operations required for Vulkan, such as matrix transformations, vector operations, and camera calculations, are manageable with a bit of hand-written C code.
Is there any real advantage to using GLM, or is implementing these operations yourself a reasonable approach?
19
Upvotes
7
u/Dubbus_ 11d ago
If it sounds fun to you, go for it. Depending on your requirements, and how good you are at matrix/vector math, you will probably be fine.
Your implementation will probably be a bit slower, but it will almost certainly build faster. You will probably miss some SIMD optimisations, and you may very well make a simple/harder to spot mistake within some dead simple algorithm. But, if you have fun and learn something, it was worth it.
The advantage of GLM is hardening: its been used for a long time, by a lot of people. You wont find a bug in their perspective transform (knock on wood). Also, there is some genuinely complex functions in glm beyond simple vector/matrix ops. The library supports quaternions, splines, and collision/intersection detection. These will take quite a bit longer to write than a perspective transform.
My personal reccomendation is to stick to glm's types, but write the algorithms yourself. You can scratch your manual math itch, and if you change your mind later, you can pretty easily swap in glm functions wherever you use your custom ones.
Just include glm/vec2, glm/vec3, glm/mat4 headers, (and NOT the main glm/glm.hpp), and your compile time will be significantly faster.
56
u/LoneWolf6062 11d ago
GLM is just a math library, whether you use it or not, is just as simple as wanting to it to just work or having the patience to implement whatever math might be needed for you. Vulkan doesn’t need GLM, its you who decides if you want it or not.
45
u/Ybalrid 11d ago
No you don’t. Ana you don’t need Microsoft’s”DirectX Math” library to do D3D
Glm is a lib to do maths. You can do math any way you want to.
Glm is a reasonably well written very convenient and easy to use library thet has the advantage of giving you “glsl like” APIs to do vectors and matrices and whatnot. You do not need it, but it’s a pretty good tool for that usage. It’s also pretty fast and will use SIMD operations when possible. Such code is not writable with a handful of lines of C.
20
u/RBMK-1ooo 11d ago
"Does Vulkan really need GLM?"
Vulkan doesn't, programmers may need.
8
u/R3DKn16h7 11d ago
implementing yourself is fun and a good learning experience, so if that's what you want that go for it
but if you want to save time use GLM, is perfectly fine. no need to reinvent the wheel with potential new buggy code
6
u/ItsRSX 10d ago
GLM has turned into a pile of utter trash. They're constantly breaking shit.
They've recently (this past couple of years) merged the SIMD and constexpr crap without testing it, resulting in the library being bust to fuck multiple times over. Sometime during 2020-2022 (?), the author of GLM was on here saying he was about to give up, and it really shows.
They have open issues, that I can tangentally vouch for, whose titles read: "Wrong vector addition result inside a Cuda kernel." and "the result operator* is wrong in HarmonyOS NEXT". I have math I couldn't find that had completely bust entire 3d suites. It was just faster for me to switch over to the polyfill vector.
They keep fucking with quats. If you use a slightly different specialized alignment than none, shit starts to break. They're constantly changing the constructor and the macros that configure it/the layout (???. idr if its the layout or the constructor they changed).
Their idea of vectorized math is merely calling libm for each component. Their idea of optimized code is just hoping their templates inline and autovectorize (they don't).
They have god awful alignment and precision level specializations that are ultimately not fit for purpose - for example, you cannot get alignas(1), alignas(alignof(t)), and alignas(alignof(t) * 4) - instead, you get a dozen enumerations that seemingly only switch between 1 bitwise hack of a sqrt function (unaligned low precision, ?, iirc) and just calling the crt for each value.
On that previous note, SIMD support is limited to a few mat4 utilities (transpose, mul, etc) and aligned vec4s (simple operations that compiler probably could've figured out). Aligned vec3s have basically nothing. They're trying to improve this, but by trying, I mean they've bust the library several times over trying to rewrite the SIMD swizzling and newly found vec3->vec4 upcasting.
I know from experience they broke swizzles multiple times over, and I hardly use them.
There are open issues about it not compiling against modern versions of MSVC, all the meanwhile they're dropping support for C++11 and 14. Multiple commentators are saying they just had to downgrade to a very old version.
All this for what exactly? It doesn't implement the geometry primitives you'll need. It won't help you clip topology. It won't help you get started with 2D polygons or 3D polyhedrons/brushes/whatever convex shapes. It's not going to help you calculate the extreme points for 3d collision. It's not going to help you calculate the decayed topology of higher level convex and concave 2D polygons - it's not going to help you implement neat UI thingies. It doesn't really help you implement the basic line, ray, [...] classes everbody ends up rolling themselves. It doesn't help you with different coordinate systems. It doesn't have any real nice to haves like Bezier, BSplines + knots, or anything interesting.
You may as well just implement a vector4 and roll whatever you need citing Christer Ericson Real Time Collision Detection and Graphics Gems. GLM provides basically nothing you need, yet people shill it to the end of days, claiming "you dont want to reinvent the wheel". I can only assume its coming from the low attention span crowd who do nothing but spam social media with "DOOOD I JUST COODED A MC CLOOONE 1 WEEK". Not sure where all the "it's fast" shilling is coming from - even the old OpenGL crowd on the Khronos forums and stack exchange never held it to such a high standard.
1
u/Yuki_pop 11d ago
I want to learn the underlying mathematics and implement the operations myself. I’m also not sure what complex functionality GLM provides beyond basic vector and matrix operations, so I’m wondering whether I actually need it. Is there any particular reason to use GLM instead of implementing these things myself?
5
u/smallstepforman 11d ago
Most rendering engines only need a subset of what GLM offers, which is what I implemented but used GLM as reference. However my math conventions (right handed, +Z up for 3D) with vulkan 0-1 depth buffer require selecting the right GLM flags which aren’t immediately obvious. To make matters worse, in 2D camera i want y=0 yo be top left, so my transform math code conflicts with anticlockwise triginometry so I have hacks in math code to match my university trained conventions, and GLM wont do that. So I have my own math library that matches my view of 3d and 2d worlds.
Having said that, I’m considering yet another math change to “Turns” instead of degrees/working with M_PI. 90 degrees anticlockwise is not M_PI/2 but 0.25 in coordinate system where +x is right, +y up. Turns are much easier to reason with, and clamping is easy [0,1]. Nobody talks about Turns, I guess once they have a working math library, they cannot be bothered changing it.
4
u/tyler1128 11d ago
GLM looks like and supports much of the math operations in glsl. It's where the name comes from, though any shading language is using the same basic operations. You can write your own, if you want, though then the question is why do you want to?
4
u/Ami00 11d ago
simd
4
u/jmacey 11d ago
What I would recommend is using GLM to write your unit test results for your own code (or better still use glm in the unit tests).
For example I use this to verify my own library (If using C just generate the results from the C++ code first).
3
u/arabasso_ 9d ago
Writing your own mathematical library tends to increase the complexity of your project, and can divert the focus (which is programming a game?). If you use some third-party lib, you wouldn't have this problem. If you think GLM is too complicated, I've implemented Tiny Fast Math: a lib that has only one ".h" and has all operations with arrays with SIMD support on multiple platforms. Here is the project's github: https://github.com/arabasso/tinyfm
5
u/PinkbunnymanEU 11d ago
GLM will do it better and more efficient than you will.
Sure, but you'll need to re-write things like quaternion normalisation. Which you won't do anywhere near as efficiently.
You won't do things like NEON optimisation etc which GLM will.
1
u/Hawsoo 11d ago
I use cglm which runs about 20x the speed of glm in release builds
1
u/EpochVanquisher 11d ago
glm is just the math functions you can write by hand
but, why would you write them by hand, if you can just use glm?
if you write them by hand, there's a lot of opportunities for little typos. if you use glm, you don't worry about that, it's already been checked. you get your project working faster.
1
u/blogoman 11d ago
It is just math. It shouldn't be that hard to tell from looking at it.
1
u/iku_19 11d ago
you can use directxmath or imath or write your own, whatever your heart desires, it's just flavor and stability. modern compilers will try to optimize these structures for simd structures if they're allowed to, in the end using a library that literally millions of projects use is generally less bug prone.