To people commenting about “why don’t they just template/why don’t they use arrays/etc”:
Look at the functions that are being called inside. Each one has a different name depending on the type and the number of inputs. Those are the functions in the OpenGL api. What you see above is pretty much the only way to make the interface usable.
Does it look disgusting and repetitive? Yes. Is it poorly written code? Not really. It’s a cleaner C++ wrapper around a C interface (C doesn’t allow function overloading, so they all needed different names in the API).
Edit: Also, the functions are const because the object represents a shader program on the GPU, and setting uniforms sends values to the GPU. It doesn’t actually change any values in this object. You could argue that semantically it shouldn’t be const, because it modifies something the object refers to. Honestly it’s up to the user.
This is called "method overloading" and isn't actually bad code as long as each method has different arguments and different results based on those args.
As someone who knows graphics programming and OpenGL, there is nothing wrong with this, the parameter names are a little dubious, but that's what the API refers to them as as well.
This is basically a 1-1 mapping of OpenGL functions except with overloading.
There is no better way to do this without introducing overhead, which you really don't want to do in this context (the functions will probably get inlined, so an extra function call doesn't add overhead), uniform setters are one of the most used functions in a graphics API, and you only have a budget of (assuming 60FPS target) 1/60th of a second to push out your entire render pipeline.
Edit: technically you could generate these, but IMO, that's even uglier
What's the issue here? The only thing I can see is setters being marked const, but that depends on what class this is in so I can't even mark that as bad. If you're saying use arrays, then you'd have to do a length check and call one of those functions anyways, and presumably this is in at least once per frame code so there's no reason to throw additional overhead at it.
Don’t talk shit about templates dawg. Easily the best feature in C++.
I’m unfamiliar with the library this is going to, but I would think that the packed list can be dispatched during compilation. But I haven’t worked with templates packs very much.
That library is very clearly a C library, even if you didn't know anything about Opengl, there's 0 way to pass a template pack to a C function, because the functions are completely different names and signatures based on the argument count.
That's using a C++17 feature to make the code even less readable, and make the type errors if you pass something that's not a float or a s/uint harder to read.
Well this seems good to me. Some people might suggest an array of parameters, but then you lose type safety. Others might suggest just exposing the underlying function, but that would add possible dependencies. Overloading gives the programmer a huge amount of control over the available interface, which I think is preferrable over short code.
Guys! That code is not bad code, it's a practice commonly used in languages like Java called "method overloading". This is used to add optional parameters to functions, and is not actually bad code although it may look that way. I have used it, and if you put this code into an IDE it won't give you warnings for repeating anything, because it's fairly common, especially in APIs.
imo the dev should just have called directly into the GL functions. His wrapper is not THAT much better, it just saves a couple of characters.
If he really wants these kinds of wrappers for some reason, he should generate them procedurally (bc knowing OpenGL he has a lot of wrappers to write lmao), or use variadic templates if he's hacky (constexpr if for each number and type of arg, or mess with macros and concepts to try every function)
6
u/AutomaticPotatoe Aug 07 '21
I think I've just committed suicide by ignorance. Guess the real bad in this is me thinking that this is bad code. Very sorry.
95
u/sleurk Aug 06 '21
To people commenting about “why don’t they just template/why don’t they use arrays/etc”:
Look at the functions that are being called inside. Each one has a different name depending on the type and the number of inputs. Those are the functions in the OpenGL api. What you see above is pretty much the only way to make the interface usable.
Does it look disgusting and repetitive? Yes. Is it poorly written code? Not really. It’s a cleaner C++ wrapper around a C interface (C doesn’t allow function overloading, so they all needed different names in the API).
Edit: Also, the functions are const because the object represents a shader program on the GPU, and setting uniforms sends values to the GPU. It doesn’t actually change any values in this object. You could argue that semantically it shouldn’t be const, because it modifies something the object refers to. Honestly it’s up to the user.
24
u/DatRedditGiy Aug 06 '21
I spent 3 minutes staring at this wondering what was wrong since I assumed they just wanted functions that conformed to their style guidelines.
Then it hit me and I was overloaded with emotions
7
u/fuj1n sadistic Aug 07 '21
As someone who knows graphics programming and OpenGL, there is nothing wrong with this, the parameter names are a little dubious, but that's what the API refers to them as as well.
This is basically a 1-1 mapping of OpenGL functions except with overloading.
There is no better way to do this without introducing overhead, which you really don't want to do in this context (the functions will probably get inlined, so an extra function call doesn't add overhead), uniform setters are one of the most used functions in a graphics API, and you only have a budget of (assuming 60FPS target) 1/60th of a second to push out your entire render pipeline.
Edit: technically you could generate these, but IMO, that's even uglier
11
u/joonatoona Aug 06 '21
What's the issue here? The only thing I can see is setters being marked const, but that depends on what class this is in so I can't even mark that as bad. If you're saying use arrays, then you'd have to do a length check and call one of those functions anyways, and presumably this is in at least once per frame code so there's no reason to throw additional overhead at it.
9
u/Basacally Aug 06 '21
Well this seems good to me. Some people might suggest an array of parameters, but then you lose type safety. Others might suggest just exposing the underlying function, but that would add possible dependencies. Overloading gives the programmer a huge amount of control over the available interface, which I think is preferrable over short code.
6
u/TheUnlocked Aug 06 '21
What's the issue with this?
2
u/Tomik080 Aug 07 '21
It's literally the only way to do it.
2
u/jaundicedeye Aug 07 '21
This is where multiple dispatch languages really shine, they can make very generic functions.
2
u/inventord Aug 07 '21
Guys! That code is not bad code, it's a practice commonly used in languages like Java called "method overloading". This is used to add optional parameters to functions, and is not actually bad code although it may look that way. I have used it, and if you put this code into an IDE it won't give you warnings for repeating anything, because it's fairly common, especially in APIs.
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 06 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/Rafaelmonte Aug 07 '21
Just a noob question (never worked w/ C++). Should varargs works better in this case?
1
u/RedRikudo Aug 10 '21
imo the dev should just have called directly into the GL functions. His wrapper is not THAT much better, it just saves a couple of characters.
If he really wants these kinds of wrappers for some reason, he should generate them procedurally (bc knowing OpenGL he has a lot of wrappers to write lmao), or use variadic templates if he's hacky (constexpr if for each number and type of arg, or mess with macros and concepts to try every function)
1
u/Voltra_Neo Aug 11 '21
Can't escape the C apis
EDIT: Why the fuck are the setters const? Is this part of a class? What's going on?!
1
u/Melon_Chief Aug 16 '21
That's ok code. Not pretty. Gets the job done. Valid. Better than calling functions where the type of the arguments is in the name.
1
u/pumpkinplight Aug 07 '21
Your understanding of overloading eclipses all others!
0
u/[deleted] Aug 06 '21
Could be helped with some variadic parameter handling, or just like, an array of values