Behavior of uniforms after glUseProgram() and speed
Asked Answered
S

1

10

How fast is glUseProgram()? Is there anything better (faster)?:

Here are my thoughts:

  1. Use 1 universal shader program, but with many input settings and attributes (settings for each graphics class)
  2. Use more than 1 shader for each graphics class

What state are uniforms in after changing the shader program? Do they save values (for example, values of matrices)?

Here are what I consider the benefits of #1 to be:

  • Doesn't use glUseProgram()

And the benefits of #2:

  • No matrix changes (for example, if class Menu and class Scene3D have different Projection matrices)
Selfloading answered 13/3, 2012 at 11:26 Comment(0)
A
12

What of the two options is better largely depends on what those shaders do, how different they are and how many attributes/uniforms you set and how often they are changed. There is no one right answer for all cases.

That said: Keep in mind, there is not only the cost for state changes, but also a shader runtime cost and it is payed per vertex and per fragment. So keeping the complexity of the shader low is always a good idea and a universal shader is more complex than specialised ones.

Minimize state change. If you have objects A, C, E using Program X and B, D, F using Program Y then, all else being equal, render in order ACEBDF, not ABCDEF.

Regarding the last question: Programs retain their state, and thus the values of uniforms, over their lifetime, unless you relink them. But uniforms are per program state, which means that if you have two uniforms with the same name and type in different programs, values won't carry over from one program to another.

Aegis answered 13/3, 2012 at 13:6 Comment(3)
"What of the two options is better largely depends on what those shaders do" - Task is simple - rendering with texturing, without any effects (only some must have shader effects).Selfloading
If some are very simple and some have an effect, then I'd have a simple shader with just texturing (assuming openg gl 2.x or above) and one per effect class. But really it is hard to judge without knowing more. It probably won't matter much in the big picture. Unless you have performance problems and know it is because of state changes, just do what is more convenient.Aegis
Uniforms can be shared between programs using the Uniform Buffer Object (see eg. Shared Uniforms)Therrien

© 2022 - 2024 — McMap. All rights reserved.