Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of uniforms after glUseProgram() and speed

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)
like image 614
WORLD_DYNAMIC_USER Avatar asked Mar 13 '12 11:03

WORLD_DYNAMIC_USER


People also ask

What does uniform mean in GLSL?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.

What is uniform buffer?

A Buffer Object that is used to store uniform data for a shader program is called a Uniform Buffer Object. They can be used to share uniforms between different programs, as well as quickly change between sets of uniforms for the same program object.

What is a uniform variable in WebGL?

uniform variables contain read-only data shared from WebGL/OpenGL environment to a vertex or fragment shader. The value is per primitive, so is useful for variables which remain constant along a primitive, frame or scene.


1 Answers

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.

like image 104
haffax Avatar answered Sep 20 '22 02:09

haffax