I am creating a UBO.
glGenBuffers(1, &uboMatrices);
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(glm::mat4), NULL, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, uboMatrices, 0, 2 * sizeof(glm::mat4));
Updating the Data
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(projection));
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(view));
glBindBuffer(GL_UNIFORM_BUFFER, 0);
Is it Necessary to do this step ?
unsigned int uniformIndexMyShader1 = glGetUniformBlockIndex(MyShader1.ID, "Matrices");
glUniformBlockBinding(MyShader1.ID, uniformIndexMyShader1, 0);
Without this also all of the shaders are getting updated with the Projection and view matrices.
According to the OpenGL 4.6 standard, section 7.6.3:
Each of a program’s active uniform blocks has a corresponding uniform buffer object binding point. The binding is established when a program is linked or re- linked, and the initial value of the binding is specified by a layout qualifier (if present), or zero otherwise. The binding point can be assigned by calling void UniformBlockBinding( uint program, uint uniformBlockIndex, uint uniformBlockBinding );
So if you specify a binding in the shader (via the layout) then it seems you would not need to explicitly call glUniformBlockBinding. If you do not specify the binding in the shader, then the standard appears to be saying that all uniform blocks get associated with binding point zero. If that's what you expect, then the call to glUniformBlockBinding wouldn't hurt, but it wouldn't be changing anything either.
Note that you have to establish the binding before linking the program.