diff --git a/packages/core/src/shader/ShaderSystem.js b/packages/core/src/shader/ShaderSystem.js index 40b8af8..cfcb6a2 100644 --- a/packages/core/src/shader/ShaderSystem.js +++ b/packages/core/src/shader/ShaderSystem.js @@ -32,6 +32,13 @@ this.shader = null; this.program = null; + /** + * Cache to holds the generated functions. Stored against UniformObjects unique signiture + * @type {Object} + * @private + */ + this.cache = {}; + this.id = UID++; } @@ -99,12 +106,48 @@ createSyncGroups(group) { - group.syncUniforms[this.shader.program.id] = generateUniformsSync(group, this.shader.program.uniformData); + const id = this.getSignature(group, this.shader.program.uniformData); + + if (!this.cache[id]) + { + this.cache[id] = generateUniformsSync(group, this.shader.program.uniformData); + } + + group.syncUniforms[this.shader.program.id] = this.cache[id]; return group.syncUniforms[this.shader.program.id]; } /** + * Takes a uniform group and data and generates a unique signiture for them. + * + * @param {PIXI.UniformGroup} group the uniform group to get signiture of + * @param {Object} uniformData uniform information generated by the shader + * @returns {String} Unique signiture of the uniform group + * @private + */ + getSignature(group, uniformData) + { + const uniforms = group.uniforms; + + let sig = ''; + + for (const i in uniforms) + { + if (uniformData[i]) + { + sig += i + uniformData[i].type; + } + else + { + sig += i; + } + } + + return sig; + } + + /** * Returns the underlying GLShade rof the currently bound shader. * This can be handy for when you to have a little more control over the setting of your uniforms. *