UI-Overlay on 3D scene

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?

The best thing you can do is try all 3 approaches (in a little test program) and see which approach you like the best.

I think the best bet is to just use 2 cameras, in your case a projection camera, and a ortho cam for the hud.

whatever effects are going on with the projection cam should not impact the overlay.

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

The likeliness that you can reuse the same shaders for 3D and 2D rendering is very low anyway.

Thank you all. For now, I’ll go with swapping shader programs for simplicity. If this becomes the bottle neck or gains a significant impact on performance, I’ll try out the other options. Though based of eject’s reply, it seems this shouldn’t ever really be the bottleneck.

You can do hundreds of shader switches per frame before you get even to a millisecond of overhead.

Premature optimization is bad :point: