2

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?

davidkomer
  • 121
  • 1
  • 1
    Hell, I sometimes tend to not even check the uniform locations and just let glUniform silently abort on -1. ;-) – Christian Rau Mar 12 '18 at 15:07
  • 1
    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 – Sebastián Mestre Mar 27 '18 at 14:03

0 Answers0