Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekucera committed Dec 16, 2024
1 parent 6b94597 commit b1bb1da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
25 changes: 16 additions & 9 deletions src/extensions/renderer/canvas/webgl/atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -175,7 +175,7 @@ export class Atlas {
}

this.keyToLocation.set(key, locations);
this.buffered = false;
this.needsBuffer = true;
return locations;
}

Expand All @@ -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;
}
}

Expand Down
33 changes: 18 additions & 15 deletions src/extensions/renderer/canvas/webgl/webgl-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b1bb1da

Please sign in to comment.