diff --git a/src/extensions/renderer/canvas/webgl/atlas.js b/src/extensions/renderer/canvas/webgl/atlas.js index 85e13ee8b..41738b8f3 100644 --- a/src/extensions/renderer/canvas/webgl/atlas.js +++ b/src/extensions/renderer/canvas/webgl/atlas.js @@ -22,7 +22,7 @@ export class Atlas { this.texture = null; this.canvas = null; - this.buffered = false; + this.needsBuffer = true; // a "location" is an object with a row and x fields this.freePointer = { x: 0, row: 0 }; @@ -175,7 +175,7 @@ export class Atlas { } this.keyToLocation.set(key, locations); - this.buffered = false; + this.needsBuffer = true; return locations; } @@ -197,13 +197,20 @@ export class Atlas { } bufferIfNeeded(gl) { - if(!this.buffered) { - if(this.texture) { - this.texture.deleteTexture(); - this.texture = null; - } - this.texture = util.bufferTexture(gl, this.canvas, this.debugID); - this.buffered = true; + if(!this.texture) { + this.texture = util.createTexture(gl, this.debugID); + } + if(this.needsBuffer) { + this.texture.buffer(this.canvas); + this.needsBuffer = false; + } + } + + dispose() { + if(this.texture) { + this.texture.deleteTexture(); + this.texture = null; + this.needsBuffer = true; } } diff --git a/src/extensions/renderer/canvas/webgl/webgl-util.js b/src/extensions/renderer/canvas/webgl/webgl-util.js index 193a8e19d..db4a7ec3c 100644 --- a/src/extensions/renderer/canvas/webgl/webgl-util.js +++ b/src/extensions/renderer/canvas/webgl/webgl-util.js @@ -13,7 +13,7 @@ export function compileShader(gl, type, source) { if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { throw new Error(gl.getShaderInfoLog(shader)); } - console.log(gl.getShaderInfoLog(shader)); + // console.log(gl.getShaderInfoLog(shader)); return shader; } @@ -113,22 +113,25 @@ export function vec4ToIndex(vec4) { ); } -export function bufferTexture(gl, textureCanvas, debugID) { +export function createTexture(gl, debugID) { const texture = gl.createTexture(); - gl.bindTexture(gl.TEXTURE_2D, texture); - - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); - - // very important, this tells webgl to premultiply colors by the alpha channel - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); - - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureCanvas); - gl.generateMipmap(gl.TEXTURE_2D); - gl.bindTexture(gl.TEXTURE_2D, null); + texture.buffer = (offscreenCanvas) => { + gl.bindTexture(gl.TEXTURE_2D, texture); + + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); + + // very important, this tells webgl to premultiply colors by the alpha channel + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); + + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, offscreenCanvas); + gl.generateMipmap(gl.TEXTURE_2D); + + gl.bindTexture(gl.TEXTURE_2D, null); + }; texture.deleteTexture = () => { gl.deleteTexture(texture);