diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 3425671..95b3eac 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -151,6 +151,9 @@ // map some webGL blend modes.. this._mapGlModes(); + // track textures in the renderer so we can no longer listen to them on destruction. + this._managedTextures = []; + /** * An array of render targets * @member {PIXI.RenderTarget[]} @@ -382,6 +385,7 @@ texture._glTextures[gl.id] = gl.createTexture(); texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); + this._managedTextures.push(texture); } @@ -422,7 +426,7 @@ * * @param texture {PIXI.BaseTexture|PIXI.Texture} the texture to destroy */ -WebGLRenderer.prototype.destroyTexture = function (texture) +WebGLRenderer.prototype.destroyTexture = function (texture, _skipRemove) { texture = texture.baseTexture || texture; @@ -434,6 +438,15 @@ if (texture._glTextures[this.gl.id]) { this.gl.deleteTexture(texture._glTextures[this.gl.id]); + delete texture._glTextures[this.gl.id]; + + if (!_skipRemove) + { + var i = this._managedTextures.indexOf(texture); + if (i !== -1) { + this._managedTextures.splice(i, 1); + } + } } }; @@ -457,9 +470,13 @@ this._initContext(); // empty all the old gl textures as they are useless now - for (var key in utils.BaseTextureCache) + for (var i = 0; i < this._managedTextures.length; ++i) { - utils.BaseTextureCache[key]._glTextures.length = 0; + var texture = this._managedTextures[i]; + if (texture._glTextures[this.gl.id]) + { + delete texture._glTextures[this.gl.id]; + } } }; @@ -476,8 +493,11 @@ this.view.removeEventListener('webglcontextlost', this.handleContextLost); this.view.removeEventListener('webglcontextrestored', this.handleContextRestored); - for (var key in utils.BaseTextureCache) { - var texture = utils.BaseTextureCache[key]; + // destroy managed textures + for (var i = 0; i < this._managedTextures.length; ++i) + { + var texture = this._managedTextures[i]; + this.destroyTexture(texture, true); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); } @@ -505,6 +525,8 @@ this._contextOptions = null; + this._managedTextures = null; + this.drawCount = 0; this.gl.useProgram(null); diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 3425671..95b3eac 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -151,6 +151,9 @@ // map some webGL blend modes.. this._mapGlModes(); + // track textures in the renderer so we can no longer listen to them on destruction. + this._managedTextures = []; + /** * An array of render targets * @member {PIXI.RenderTarget[]} @@ -382,6 +385,7 @@ texture._glTextures[gl.id] = gl.createTexture(); texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); + this._managedTextures.push(texture); } @@ -422,7 +426,7 @@ * * @param texture {PIXI.BaseTexture|PIXI.Texture} the texture to destroy */ -WebGLRenderer.prototype.destroyTexture = function (texture) +WebGLRenderer.prototype.destroyTexture = function (texture, _skipRemove) { texture = texture.baseTexture || texture; @@ -434,6 +438,15 @@ if (texture._glTextures[this.gl.id]) { this.gl.deleteTexture(texture._glTextures[this.gl.id]); + delete texture._glTextures[this.gl.id]; + + if (!_skipRemove) + { + var i = this._managedTextures.indexOf(texture); + if (i !== -1) { + this._managedTextures.splice(i, 1); + } + } } }; @@ -457,9 +470,13 @@ this._initContext(); // empty all the old gl textures as they are useless now - for (var key in utils.BaseTextureCache) + for (var i = 0; i < this._managedTextures.length; ++i) { - utils.BaseTextureCache[key]._glTextures.length = 0; + var texture = this._managedTextures[i]; + if (texture._glTextures[this.gl.id]) + { + delete texture._glTextures[this.gl.id]; + } } }; @@ -476,8 +493,11 @@ this.view.removeEventListener('webglcontextlost', this.handleContextLost); this.view.removeEventListener('webglcontextrestored', this.handleContextRestored); - for (var key in utils.BaseTextureCache) { - var texture = utils.BaseTextureCache[key]; + // destroy managed textures + for (var i = 0; i < this._managedTextures.length; ++i) + { + var texture = this._managedTextures[i]; + this.destroyTexture(texture, true); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); } @@ -505,6 +525,8 @@ this._contextOptions = null; + this._managedTextures = null; + this.drawCount = 0; this.gl.useProgram(null); diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index 3d6248d..78e5151 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -135,7 +135,7 @@ * @member {object} * @private */ - this._glTextures = []; + this._glTextures = {}; // if no source passed don't try to load if (source) @@ -346,7 +346,8 @@ { this.emit('dispose', this); - this._glTextures.length = 0; + // this should no longer be needed, the renderers should cleanup all the gl textures. + // this._glTextures = {}; }; /** diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 3425671..95b3eac 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -151,6 +151,9 @@ // map some webGL blend modes.. this._mapGlModes(); + // track textures in the renderer so we can no longer listen to them on destruction. + this._managedTextures = []; + /** * An array of render targets * @member {PIXI.RenderTarget[]} @@ -382,6 +385,7 @@ texture._glTextures[gl.id] = gl.createTexture(); texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); + this._managedTextures.push(texture); } @@ -422,7 +426,7 @@ * * @param texture {PIXI.BaseTexture|PIXI.Texture} the texture to destroy */ -WebGLRenderer.prototype.destroyTexture = function (texture) +WebGLRenderer.prototype.destroyTexture = function (texture, _skipRemove) { texture = texture.baseTexture || texture; @@ -434,6 +438,15 @@ if (texture._glTextures[this.gl.id]) { this.gl.deleteTexture(texture._glTextures[this.gl.id]); + delete texture._glTextures[this.gl.id]; + + if (!_skipRemove) + { + var i = this._managedTextures.indexOf(texture); + if (i !== -1) { + this._managedTextures.splice(i, 1); + } + } } }; @@ -457,9 +470,13 @@ this._initContext(); // empty all the old gl textures as they are useless now - for (var key in utils.BaseTextureCache) + for (var i = 0; i < this._managedTextures.length; ++i) { - utils.BaseTextureCache[key]._glTextures.length = 0; + var texture = this._managedTextures[i]; + if (texture._glTextures[this.gl.id]) + { + delete texture._glTextures[this.gl.id]; + } } }; @@ -476,8 +493,11 @@ this.view.removeEventListener('webglcontextlost', this.handleContextLost); this.view.removeEventListener('webglcontextrestored', this.handleContextRestored); - for (var key in utils.BaseTextureCache) { - var texture = utils.BaseTextureCache[key]; + // destroy managed textures + for (var i = 0; i < this._managedTextures.length; ++i) + { + var texture = this._managedTextures[i]; + this.destroyTexture(texture, true); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); } @@ -505,6 +525,8 @@ this._contextOptions = null; + this._managedTextures = null; + this.drawCount = 0; this.gl.useProgram(null); diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index 3d6248d..78e5151 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -135,7 +135,7 @@ * @member {object} * @private */ - this._glTextures = []; + this._glTextures = {}; // if no source passed don't try to load if (source) @@ -346,7 +346,8 @@ { this.emit('dispose', this); - this._glTextures.length = 0; + // this should no longer be needed, the renderers should cleanup all the gl textures. + // this._glTextures = {}; }; /** diff --git a/src/core/textures/RenderTexture.js b/src/core/textures/RenderTexture.js index e538dae..5d4915a 100644 --- a/src/core/textures/RenderTexture.js +++ b/src/core/textures/RenderTexture.js @@ -131,7 +131,7 @@ var gl = this.renderer.gl; this.textureBuffer = new RenderTarget(gl, this.width, this.height, baseTexture.scaleMode, this.resolution);//, this.baseTexture.scaleMode); - this.baseTexture._glTextures[gl.id] = this.textureBuffer.texture; + this.baseTexture._glTextures[gl.id] = this.textureBuffer.texture; //TODO refactor filter manager.. as really its no longer a manager if we use it here.. this.filterManager = new FilterManager(this.renderer); @@ -322,7 +322,7 @@ this.textureBuffer.clear(); } - + // this.textureBuffer. var context = this.textureBuffer.context;