Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do uniform values remain in GLSL shader if unbound?

Tags:

c++

opengl

glsl

I am writing a program that uses two different shaders for different primitives. My question is: if I bind a program, send it uniform variables, then use another shader program and come back to the first one, will the passed uniform values remain? Here is some pseudocode:

glUseProgram(shader1);
glUniform(shader1,...);
//stuff

for(elements in a list) {
    if(element.type = 1) {
        glUseProgram(shader2);
        element.draw();
    } else {
        glUseProgram(shader1); //Here, do the uniforms from above remain, if shader2 was bound before?
        element.draw();
    }
}
like image 809
Sebastian Mendez Avatar asked Jun 01 '12 21:06

Sebastian Mendez


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.

Can uniform variables be declared in a fragment shader?

Fragment shader reads a value from the same variable, automatically interpolated to that fragment. No need to declare these in the Java program.

How do GLSL shaders work?

Shaders use GLSL (OpenGL Shading Language), a special OpenGL Shading Language with syntax similar to C. GLSL is executed directly by the graphics pipeline. There are several kinds of shaders, but two are commonly used to create graphics on the web: Vertex Shaders and Fragment (Pixel) Shaders.

How do I optimize GLSL shader?

One way to speed up GLSL code, is by marking some variables constant at compile-time. This way the compiler may optimize code (e.g. unroll loops) and remove unused code (e.g. if hard shadows are disabled). The drawback is that changing these constant variables requires that the GLSL code is compiled again.


1 Answers

Yes, uniforms are specific to a program, and will be persistent if you unbind and rebind it.

Also, if you want, you could easily verify this yourself in that sample with glGetUniform.

From the OpenGL 4.1 Specification:

2.11.7 Uniform Variables

... Uniforms are program object-specific state. They retain their values once loaded, and their values are restored whenever a program object is used, as long as the program object has not been re-linked. ...
like image 198
Tim Avatar answered Oct 06 '22 02:10

Tim