diff --git a/packages/core/src/geometry/GeometrySystem.js b/packages/core/src/geometry/GeometrySystem.js index 881674d..6c2188f 100644 --- a/packages/core/src/geometry/GeometrySystem.js +++ b/packages/core/src/geometry/GeometrySystem.js @@ -35,6 +35,13 @@ * @readonly */ this.hasInstance = true; + + /** + * A cache thats stores vaos linked to geometries. + * @member {Object} + * @private + */ + this.cache = {}; } /** @@ -228,6 +235,32 @@ } /** + * Takes a geometry and program and generates a unique signature for them. + * + * @param {PIXI.Geometry} geometry to get signature from + * @param {PIXI.Program} prgram to test geometry against + * @returns {String} Unique signature of the geometry and program + * @private + */ + getSignature(geometry, program) + { + const attribs = geometry.attributes; + const shaderAttributes = program.attributeData; + + const strings = [geometry.id]; + + for (const i in attribs) + { + if (shaderAttributes[i]) + { + strings.push(i); + } + } + + return strings.join('-'); + } + + /** * Creates a Vao with the same structure as the geometry and stores it on the geometry. * @private * @param {PIXI.Geometry} geometry - Instance of geometry to to generate Vao for @@ -239,9 +272,20 @@ const gl = this.gl; const CONTEXT_UID = this.CONTEXT_UID; + + const signature = this.getSignature(geometry, program); + + if (this.cache[signature]) + { + const vao = this.cache[signature]; + + geometry.glVertexArrayObjects[this.CONTEXT_UID][program.id] = vao; + + return vao; + } + const buffers = geometry.buffers; const attributes = geometry.attributes; - const tempStride = {}; const tempStart = {}; diff --git a/packages/core/src/geometry/GeometrySystem.js b/packages/core/src/geometry/GeometrySystem.js index 881674d..6c2188f 100644 --- a/packages/core/src/geometry/GeometrySystem.js +++ b/packages/core/src/geometry/GeometrySystem.js @@ -35,6 +35,13 @@ * @readonly */ this.hasInstance = true; + + /** + * A cache thats stores vaos linked to geometries. + * @member {Object} + * @private + */ + this.cache = {}; } /** @@ -228,6 +235,32 @@ } /** + * Takes a geometry and program and generates a unique signature for them. + * + * @param {PIXI.Geometry} geometry to get signature from + * @param {PIXI.Program} prgram to test geometry against + * @returns {String} Unique signature of the geometry and program + * @private + */ + getSignature(geometry, program) + { + const attribs = geometry.attributes; + const shaderAttributes = program.attributeData; + + const strings = [geometry.id]; + + for (const i in attribs) + { + if (shaderAttributes[i]) + { + strings.push(i); + } + } + + return strings.join('-'); + } + + /** * Creates a Vao with the same structure as the geometry and stores it on the geometry. * @private * @param {PIXI.Geometry} geometry - Instance of geometry to to generate Vao for @@ -239,9 +272,20 @@ const gl = this.gl; const CONTEXT_UID = this.CONTEXT_UID; + + const signature = this.getSignature(geometry, program); + + if (this.cache[signature]) + { + const vao = this.cache[signature]; + + geometry.glVertexArrayObjects[this.CONTEXT_UID][program.id] = vao; + + return vao; + } + const buffers = geometry.buffers; const attributes = geometry.attributes; - const tempStride = {}; const tempStart = {}; diff --git a/packages/core/src/shader/ShaderSystem.js b/packages/core/src/shader/ShaderSystem.js index 159e30e..e43685e 100644 --- a/packages/core/src/shader/ShaderSystem.js +++ b/packages/core/src/shader/ShaderSystem.js @@ -130,21 +130,19 @@ { const uniforms = group.uniforms; - let sig = ''; + const strings = []; for (const i in uniforms) { + strings.push(i); + if (uniformData[i]) { - sig += i + uniformData[i].type; - } - else - { - sig += i; + strings.push(uniformData[i].type); } } - return sig; + return strings.join('-'); } /**