Pankaj Bansal
Pankaj Bansal

Reputation: 919

Does uniforms set and vertex attributes values remain when shader is unbound

I am want to know if the uniform and vertex attribute variable values remain if the shader program is unbound and then rebound

Basically I want to ask this question Do uniform values remain in GLSL shader if unbound?. But I want to know if this applies to both uniforms and attribure variables?

If I do this

    glUseProgram(shader1);
    // Now set uniforms.
    glUniform4fv(m_uniforms[COLOR_HANDLE], 1, color.AsFloat());
    glUniformMatrix4fv(m_uniforms[MVP_HANDLE], 1, false, matrix);

    glBindBuffer(GL_ARRAY_BUFFER, bufferIndex);
    glEnableVertexAttribArray(m_attributes[POSITION_HANDLE1]);
    glEnableVertexAttribArray(m_attributes[POSITION_HANDLE2]);

    glVertexAttribPointer(m_attributes[POSITION_HANDLE], 3, GL_FLOAT, false, 3 * sizeof(GLfloat), 0);

Now save the current program, vao, vbo binded. Then use second program

    glUseProgram(shader2);
    //bind some new vao, vbo, set some uniform, vertex attribute variable.
    element.draw();

Then again use the first shader program. Rebind the vbo, vao

    glUseProgram(shader1); //Here, do the uniforms and attributes set in first shader program remain?
    element.draw();

Does this mean that complete state is restored and draw calls will work. I think this should work if the uniforms and attribute values are retained. So when I restore the client program with glUseProgram, all uniforms and attributes set by client will be restored.

If not, then how can I save complete state. Onething is client has to set them again. but if that is not an option, what is other way around. How can I save the full state and restore it later. Is it even possible ?

PS: I need to do it for opengl 2.0, opengl 3.2+, opengl es 2.0, opengles 3.0

Upvotes: 2

Views: 1742

Answers (1)

BDL
BDL

Reputation: 22167

Uniforms

Uniforms are part of the shader program object. Thus they keep saved even when the program object is unbound. The OpenGL 4.5 Specification says to this:

7.6 Uniform Variables

Uniforms in the default uniform block, except for subroutine 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.

Attributes

Attribute bindings are part of the VAO state. When no VAO is bound then the default VAO is used (which is btw. not allowed in Core Profile). When using VAOs, restoring attribute bindings is quite simple since it is sufficient to rebind the VAO. In the other case, I would have a look at the "Associated Gets" section here.

Upvotes: 4

Related Questions