Multiple Program Uniforms

How can I set multiple uniform variables for different shader programs?

The problem arises when I try to set a uniform for the shader which is not in use, but I have to since I need to keep uniform variables for all programs synchronous.

If I set the program current before setting its uniforms, the other’s uniforms are lost, correct?

Any idea how to do this nicely using OpenGL 2.1 only?

Thanks!

Uniform buffers may help:
http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt

Another idea is to ‘abuse’ the built-in GL uniform state. Among others, there’s: 8 light and 1 material parameter sets (each has several vec4 uniforms) and a lot of texture matrix uniforms (mat4).
Watch out when using gl_LightSource[x].position - it is subject to the current modelview matrix at the time you set it.

Thanks.

But if the driver does not support the uniform buffer extension, would it be possible to just glUseProgram before setting its uniforms and then switch to another program…would that cause the set uniform values to be lost? How expensive switching programs?

Uniform values are part of the program object’s state. So changing them while one program is in use does not affect any other program.

You can wrap the glUniform calls to take an extra param for global state or not, save out all uniforms that you mark as for global use, then wrap glUseProgram and check for a program change: if it changes resend the ones you’ve marked as global. Or just wrap glUseProgram and call a big dirty “SetGlobalUniforms” function every time the current program changes.

Thanks! All replies were helpful. :slight_smile: