PDA

View Full Version : Multiple Program Uniforms



glfreak
08-30-2011, 01:49 PM
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!

skynet
08-30-2011, 03:08 PM
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.

glfreak
08-30-2011, 03:15 PM
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?

Alfonse Reinheart
08-30-2011, 04:26 PM
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.

mhagain
08-30-2011, 05:40 PM
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.

glfreak
08-30-2011, 08:45 PM
Thanks! All replies were helpful. :)