I'm wondering what would be the preferred way to draw a 2d ui overlay on top of a 3d game. Some ideas I've seen online or am considering are:
1) draw the 3d frame, set projection/view uniform matrices to Identity, draw UI
2) draw 3d frame, set some uniform boolean value that tell shaders to skip the matrix transformations, draw ui, reset the boolean
3) draw 3d frame, switch to another shaderProgram, draw UI, switch back to 3d shaderProgram.
I'm not sure how expensive switching shader programs (3), but
https://www.opengl.org/discussion_boards/showthread.php/173818-Best-way-to-handle-multiple-shaders suggests it shouldn't be a problem. Likewise, not sure how expensive checking a uniform boolean value for every single vertex (2) would be?
I did some
tests about that:
Per second, I hit 105k framebuffer, 700k Program, 3.1M Texture Bindings, 3.4M Vertex Formats, 4.6M UBO Bindings, 6.3M Vertex Bindings and 15.2M Uniform Updates.
So by a performances point of view, option 1 is the best.
But practically it doesn't matter unless very specific condition. I'd go with the 3. Code clearer and more readable