title: Is loading uniform variables in OpenGL expensive?
body: <p>Typically I load all uniform variables (whether I need to or not) just before I draw a mesh. For example (in WebGL):</p>
  
  <pre><code>gl.uniformMatrix4fv(program.ModelView, false, ModelView.array);
  var MVP = Projection.mult(ModelView);
  gl.uniformMatrix4fv(program.ModelViewProjection, false, MVP.array);
  var NormalMatrix = ModelView.normal();
  gl.uniformMatrix3fv(program.NormalMatrix, false, NormalMatrix);
  gl.uniform3fv(program.materalDiffuse, materialDiffuse);
  gl.uniform3fv(program.materalAmbient, [1.0, 1.0, 1.0]);
  // ...etc...
  // ... draw mesh via gl.drawArrays or gl.drawElements
  </code></pre>
  
  <p>Some of these uniforms may not have changed since the previous calls to <code>gl.draw(Arrays|Elements)</code> so loading them is often redundant. Should I worry about this overhead?</p>
  
  <p>I could add a <code>dirty</code> bit to the client variables and only load uniform variables that need to be changed; e.g.:</p>
  
  <pre><code>if (ModelView.dirty || Projection.dirty) {
     gl.uniformMatrix4fv(program.ModelView, false, ModelView.array);
     var MVP = Projection.mult(ModelView);
     gl.uniformMatrix4fv(program.ModelViewProjection, false, MVP.array);
     ModelView.dirty = Projection.dirty = false;
  }
  </code></pre>
  
  <p>I would then set the respective <code>dirty</code> bits whenever <code>ModelView</code> or <code>Projection</code> is mutated. </p>
  
  <p>Is it worth the effort to try and limit uniform variables loads?</p>