Skip to content

Commit

Permalink
fix: Clearing cloned textures produces new internal copy when multi r…
Browse files Browse the repository at this point in the history
…eferences
  • Loading branch information
robertleeplummerjr committed Mar 16, 2020
1 parent 0a072fe commit 8cfbf0c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 15 deletions.
15 changes: 12 additions & 3 deletions dist/gpu-browser-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.8.3
* @date Mon Mar 16 2020 08:42:55 GMT-0400 (Eastern Daylight Time)
* @version 2.8.4
* @date Mon Mar 16 2020 10:58:11 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -5591,7 +5591,16 @@ class GLTexture extends Texture {
}

clear() {
const { context: gl, size, texture } = this;
if (this.texture._refs) {
this.texture._refs--;
const gl = this.context;
const target = this.texture = gl.createTexture();
selectTexture(gl, target);
const size = this.size;
target._refs = 1;
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
}
const { context: gl, texture } = this;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
gl.bindTexture(gl.TEXTURE_2D, texture);
selectTexture(gl, texture);
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions dist/gpu-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.8.3
* @date Mon Mar 16 2020 08:42:55 GMT-0400 (Eastern Daylight Time)
* @version 2.8.4
* @date Mon Mar 16 2020 10:58:11 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -10044,7 +10044,16 @@ class GLTexture extends Texture {
}

clear() {
const { context: gl, size, texture } = this;
if (this.texture._refs) {
this.texture._refs--;
const gl = this.context;
const target = this.texture = gl.createTexture();
selectTexture(gl, target);
const size = this.size;
target._refs = 1;
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
}
const { context: gl, texture } = this;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
gl.bindTexture(gl.TEXTURE_2D, texture);
selectTexture(gl, texture);
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.8.3",
"version": "2.8.4",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
11 changes: 10 additions & 1 deletion src/backend/gl/texture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ class GLTexture extends Texture {
}

clear() {
const { context: gl, size, texture } = this;
if (this.texture._refs) {
this.texture._refs--;
const gl = this.context;
const target = this.texture = gl.createTexture();
selectTexture(gl, target);
const size = this.size;
target._refs = 1;
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
}
const { context: gl, texture } = this;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
gl.bindTexture(gl.TEXTURE_2D, texture);
selectTexture(gl, texture);
Expand Down
38 changes: 38 additions & 0 deletions test/features/clear-textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,41 @@ test('unsigned precision auto', () => {
(GPU.isSinglePrecisionSupported && GPU.isHeadlessGLSupported ? test : skip)('single precision headlessgl', () => {
clearTexture('single', 'headlessgl');
});


function clearClonedTexture(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function() {
return 1;
}, { output: [1], pipeline: true, immutable: true });
const result = kernel();
assert.equal(result.toArray()[0], 1);
const result2 = result.clone();
const result3 = result2.clone();
assert.equal(result2.toArray()[0], 1);
assert.equal(result3.toArray()[0], 1);
result2.clear();
assert.equal(result2.toArray()[0], 0);
assert.equal(result3.toArray()[0], 1);
gpu.destroy();
}

test('clear cloned texture auto', () => {
clearClonedTexture();
});

test('clear cloned texture gpu', () => {
clearClonedTexture('gpu');
});

(GPU.isWebGLSupported ? test : skip)('clear cloned texture webgl', () => {
clearClonedTexture('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('clear cloned texture webgl2', () => {
clearClonedTexture('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('clear cloned texture headlessgl', () => {
clearClonedTexture('headlessgl');
});

0 comments on commit 8cfbf0c

Please sign in to comment.