Does glUseProgram alter VAO and/or VBO state? - opengl

Is the currently-installed shader program part of the VAO state and/or part of the VBO state? Or is it separate state that is not affected when VAO and VBO are switched?
According to these docs the glUseProgram function
Installs a program object as part of current rendering state.
In other words, how is "rendering state" is defined?

No, active shader is not a part of VAO nor VBO state.
By "rendering state" they seem to mean the global state.

Related

OpenGL - Where is the state set by glDrawBuffers stored?

If I understand correctly, you can attach textures to an FBO and then use glDrawBuffers to link those attachment points to (fragment) shader output indices. Where is this latter state stored after a glDrawBuffers call? The currently bound shader, FBO, or is it a global OpenGL state?
glDrawBuffer(s) sets framebuffer object state.

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
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.

Process of setting up a VAO in OpenGL

Can I get a more overall/general description of this?
I've been trying to research these things all week, but I only come into super technical explanations or examples.
Could somebody explain the overall process or goal of these VAO's? Maybe outline a bit of a "step-by-step" flowchart for setting up a VAO in OpenGL?
I've never been someone who learns by examples... so all the things I've found online have been really unhelpful so far.
Your Vertex Buffer Objects (VBOs) contain the vertex data. You set up attribute pointers which specify how that data should be accessed and interpreted in order to draw a vertex - how many components are there to each vertex, how are they stored (GL_FLOAT for example) etc.
The Vertex Array Object (VAO) saves all these attributes. Their purpose is to allow you to set up these attributes once and then restore them with one state change whenever you need to use them.
The advantage of this is you can easily set up multiple states and restore them - if I want to draw a cube and a triangle these are two different states - I create a VAO for each and the VAO saves the state for each of them. When I want to draw the cube, I restore the state for the cube only.
Setup
Setup has four steps:
Create the VAO
Bind the VAO
Set up the state
Unbind the VAO
Creating a VAO is very simple:
GLuint vao_name;
glGenVertexArrays(1, &vao_name);
This will generate one vertex array, and store it's name in vao_name.
Once your VAO is created you need to bind it:
glBindVertexArray(vao_name);
Now, any changes you make to the attribute pointers or bindings to the element array buffer will be saved. This is a call to any of the following:
glVertexAttribPointer(...)
glEnableVertexAttribArray(...)
glDisableVertexAttribArray(...)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)
Whenever you make one of these calls, the corresponding state is saved in the currently bound VAO. And when you later bind the VAO again, the state is restored.
Once you have set up the state you can unbind the VAO. Do this by binding VAO 0:
glBindVertexArrays(0);
You could also bind a different VAO and set up a different state for that. When this happens it is safe to make changes to the attribute pointers without disrupting your previous state.
Drawing
When you want to draw something all you have to do is restore the VAO:
glBindVertexArray(cube_vao)
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(triangle_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); //Unbind the VAO
Edit:
These are some answers that helped me when I was trying to understand VAOs:
glVertexAttribPointer overwrite, glVertexAttribPointer clarification, When should glVertexAttribPointer be called?
Also, the OpenGL specification explains it well once you've got your head around the basics: https://www.opengl.org/registry/
The VAO holds all information about how to read the attributes from the VBOs.
This includes for each attribute, which VBO it is in; the offset, the stride, how it is encoded (int, float, normalized or not), how many values per attribute. In other words the parameters of glVertexAttribPointer (and the bound GL_ARRAY_BUFFER at the time of the call.
It also holds the element array buffer that holds the indexes.
For what VAO's are, see #ratchet-freak's answer.
As for the goal of VAO's, it's a matter of improving performance by allowing one single state change to setup all vertex attributes bindings, which not only means fewer function calls but also more opportunity for the OpenGL implementation to optimize for this state as a whole.
For VAO, an understanding of VBOs and Shader is a pre-requisite. VAOs provide a way to “pre-define” vertex data and its attributes.
One way to understand is to draw with VBOs and then draw the same with VAOs. You will notice that it is similar to drawing with VBOs, except that it is first "saved" in a VAO for later use. Subsequently, the VAO is made active to actually draw the saved state.
See VBO, Shader, VAO for a fairly good example.

OpenGL Order of VBO binding

If I'm trying to do some OpenGL 3.3+ style VBO graphics, is it alright for me to first, enable attribute arrays and set vertex attribute pointers, then in an often refreshing VBO, load fresh data and bind a GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER then call drawELements? I have code that is crashing on drawElements, and I'm wondering if its because the order of my calls is messed up. I'll also mention that this is under the guise of Qt 5.
Whn you set your attribute pointers, it is crucial to have the correct GL_ARRAY_BUFFER bound. The current GL_ARRAY_BUFFER_BINDING at the time of the glVertexAttribPointer() call will become part of that attribute pointer state. It does not matter which GL_ARRAY_BUFFER is bound at draw time (in contrast to the GL_ELEMENT_ARRAY_BUFFER which is relevant for the glDrawElements() family of GL functions.

Must shader uniforms be uploaded once for every VAO?

Must shader uniforms (like the projection matrix that does not change very often) be uploaded once per vertex array object? Or does the bound variables have nothing to do with the active VAO, and the shader are only connected with the vertex object when drawn?
(E.g. does a shader stand for its own until vertex data is sent to it via a draw command, or is it somehow bound to the vao created?)
Uniforms have nothing to do with VAOs; they're completely different data. Uniforms are program object state. VAOs are entirely separate objects.
As with most state, uniform data set into a program will remain that way until you change it.