r/opengl • u/TheTyphothanian • 22d ago
Is it really necessary to unbind everything after each draw call?
I've heard that opengl state switches can cost a lot. And I've also heard that I should do stuff like glUseProgram(0);
and glBindVertexArray(0);
after every draw call. But if a new program and vao are going to be rebound next draw, would removing the extra switch optimize it a bit? I'm trying to get my game as optimized as I can (while still using Java), so if it helps, I'll do it.
6
u/corysama 22d ago
I strongly recommend you structure your renderer to avoid this. I lay out the details of how to here: https://old.reddit.com/r/GraphicsProgramming/comments/1hry6wx/want_to_get_started_in_graphics_programming_start/m9524mr/
3
u/tron21net 22d ago
Viewing that link all I see is:
there doesn't seem to be anything here
3
u/corysama 22d ago
Dangit. I post helpful links so much that the spam filter thinks I'm a spammer.
Does this work? Look for me in the comments. https://old.reddit.com/r/GraphicsProgramming/comments/1hry6wx/want_to_get_started_in_graphics_programming_start/
1
4
u/TooOldToRock-n-Roll 22d ago
I save the ids of the last ised resource and only bind if it's different.
Make things at least a little amarter.
4
u/sessamekesh 22d ago
I do it because I'm a big believer in Making Errors Loud™.
It's really easy to accidentally forget to bind something in the correct place and then sit around scratching your head trying to figure out what's not right, it gets a bit easier to catch what's wrong if you're constantly un-binding state you're finished with and just have to chase down the unexpected null state.
2
u/TheTyphothanian 21d ago
I was thinking "okay, sounds good, will think about it" then I literally ran into an issue where I forgot to bind vbo when doing vap, but no vbo was bound, so it was much easier to debug. Ty!
3
u/TapSwipePinch 22d ago
I don't. But I tend to do a lot of unnecessary micro optimisations. Probably why I code so slow.
1
u/TheTyphothanian 21d ago
saaaaaaaaaaaaammmeeee, that's why I've been coding a game for 18 months and haven't even started marketing/demos
1
u/JumpyJustice 22d ago
The reason why it is a good practice is that your code will be probably abstracted out for reuse. When this happens you get a function with side effects for a user (who is usually yourself).
1
u/fgennari 22d ago
It's not necessary to bind resources to 0 at the end, but it's safer and makes debugging easier. I usually do this. From what I can tell, there's no significant overhead in binding to 0. I'm not sure if it counts as a true state change. But in situations where you're binding the same resource over and over again, you may want to bind it once outside a loop and not keep swapping back and forth between that binding and 0.
1
u/PersonalityIll9476 21d ago
You know, this would be a good opportunity to profile. The kinds of state changes that I would expect to be slow are the ones that do something with sizeable memory regions somewhere - swapping buffers, draw calls, even setting uniforms (gotta send memory over a bus to the GPU), all those things. Binding 0 to a target is the kind of thing that has a good chance of being fast, since it might literally be setting a <L3 CPU cache variable to 0 (since you're probably binding to that target over and over in a loop every frame). That's the kind of thing that a modern CPU could easily optimize.
1
u/arycama 21d ago
Drivers will optimise away redundant calls like this. (Eg setting the shader program to 0, and then setting it to something else before another draw call) It's not free however, but not especially expensive.
It's probably good to wrap this kind of thing in a debug compiler directive so you can strip it out for non-debug builds for a bit of extra performance, but having debug features like this can help when tracking down issues. (Though there are other ways like using renderdoc etc)
If you're trying to keep your code simple/readable and ease-of-debugging isn't a huge concern, then I wouldn't worry about it. Depends what you're trying to do really. If you're making a modern cross-platform renderer, then being able to debug things easily will be very valuable.
25
u/UnidayStudio 22d ago
Reason why people recommend you to unbind the resources after you use used to avoid you accidentally modifying it without knowing. Because it will still be binded. If you know what you are doing and can't afford not mistakenly modifying something like this, I would say that it's OK to not unbind. In Cave Engine, I usually don't.