1
votes

I have an OpenGL application that uses Shaders and Vertex Buffer Objects, with GLM for matrices and vectors. Because there are matrix transformations that occur every frame, I'm calling glUniformMatrix4fv() every frame. Below is a snippet of my code:

ViewMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(YRotation), glm::vec3(0.0f, 1.0f, 0.0f)) * ViewMatrix;
ViewMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(XRotation), glm::vec3(1.0f, 0.0f, 0.0f)) * ViewMatrix;
ViewMatrix = glm::translate(glm::mat4(1.0f), Translate) * ViewMatrix;

...

glUniformMatrix4fv(ShaderProgram->GetUniformLocation("View"), 1, GL_FALSE, glm::value_ptr(ViewMatrix));

As shown, I am calling GetUniformLocation() to get the location of the View matrix that I declared as a uniform in the shader. It is a member function of a CProgram class which simply calls glGetUniformLocation(). Because this is being called every frame, I was wondering whether it would be better to call glGetUniformLocation() only once, at the beginning of the program, and repeatedly use the value returned from that. Would there actually be any noticeable difference in frame rate?

2

2 Answers

6
votes

your uniform location will never change across frames because the location of shaders variable is assigned at "shader compile time". So if you store the values inside a variable, you'll skip a function call.... don't expect a big improvement by the way ;)

1
votes

Actually uniform locations will be assigned during shader linking time. And they will never change for life time of program object. If you want improvement try not to always update uniforms. In your rendering loop check If uniform variable value changes then only updates its value. Kind of dirty flag. This will definitely improve performance. If you want more optimization look into usage of uniform blocks or uniform buffer objects.