Consider something like the following for webgl (untested but should work):

const lookup = new Map();
const isEqual3fv = (v1, v2) => {
 return v1[0] === v2[0] && v1[1] === v2[1] && v1[2] === v2[2];

const setUniform3fv = (location, value) => {
  if(!lookup.has(location) || !isEqual3fv(lookup.get(location), value)) {
    gl.uniform3fv(location, value);
    lookup.set(location, value);

Would this be an optimization in most cases? Is caching the values and comparing for equality before setting the uniform generally a worthwhile optimization - or is the difference pretty negligible?

  • 1
    $\begingroup$ Hell, I sometimes tend to not even check the uniform locations and just let glUniform silently abort on -1. ;-) $\endgroup$ Mar 12, 2018 at 15:07
  • 1
    $\begingroup$ As with most things CG (and most things programming, even), it is probably a tradeoff, and should be evaluated on a case-by-case basis. Run some benchmarks. If the memory used by your cache is limiting something else in your program then it might become an issue, you will have to think very hard to decide if the extra speed is worth the memory overhead $\endgroup$ Mar 27, 2018 at 14:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.