Skip to content

Commit

Permalink
fix: Don't call beforeMutate if immutable and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertleeplummerjr committed Mar 16, 2020
1 parent b1af9b3 commit 6df34b3
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 38 deletions.
31 changes: 20 additions & 11 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.1
* @date Thu Mar 12 2020 07:32:27 GMT-0400 (Eastern Daylight Time)
* @version 2.8.2
* @date Mon Mar 16 2020 08:37:54 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -10304,12 +10304,15 @@ class WebGLKernel extends GLKernel {
}

_setupOutputTexture() {
const { context: gl, texSize } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texSize = this.texSize;
const texture = this.createTexture();
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
Expand Down Expand Up @@ -10341,7 +10344,9 @@ class WebGLKernel extends GLKernel {
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
Expand Down Expand Up @@ -12605,14 +12610,16 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupOutputTexture() {
const { context: gl } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texture = gl.createTexture();
const { texSize } = this;
const texSize = this.texSize;
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
Expand All @@ -12639,16 +12646,18 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupSubOutputTextures() {
const { context: gl } = this;
const gl = this.context;
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
}
const { texSize } = this;
const texSize = this.texSize;
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
this.mappedTextures = [];
for (let i = 0; i < this.subKernels.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

31 changes: 20 additions & 11 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.1
* @date Thu Mar 12 2020 07:32:26 GMT-0400 (Eastern Daylight Time)
* @version 2.8.2
* @date Mon Mar 16 2020 08:37:54 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -14757,12 +14757,15 @@ class WebGLKernel extends GLKernel {
}

_setupOutputTexture() {
const { context: gl, texSize } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texSize = this.texSize;
const texture = this.createTexture();
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
Expand Down Expand Up @@ -14794,7 +14797,9 @@ class WebGLKernel extends GLKernel {
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
Expand Down Expand Up @@ -17058,14 +17063,16 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupOutputTexture() {
const { context: gl } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texture = gl.createTexture();
const { texSize } = this;
const texSize = this.texSize;
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
Expand All @@ -17092,16 +17099,18 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupSubOutputTextures() {
const { context: gl } = this;
const gl = this.context;
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
}
const { texSize } = this;
const texSize = this.texSize;
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
this.mappedTextures = [];
for (let i = 0; i < this.subKernels.length; i++) {
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.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.8.1",
"version": "2.8.2",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
11 changes: 8 additions & 3 deletions src/backend/web-gl/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,15 @@ class WebGLKernel extends GLKernel {
* @desc Setup and replace output texture
*/
_setupOutputTexture() {
const { context: gl, texSize } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texSize = this.texSize;
const texture = this.createTexture();
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
Expand Down Expand Up @@ -719,7 +722,9 @@ class WebGLKernel extends GLKernel {
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
Expand Down
16 changes: 10 additions & 6 deletions src/backend/web-gl2/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,16 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupOutputTexture() {
const { context: gl } = this;
const gl = this.context;
if (this.texture) {
this.texture.beforeMutate();
if (this.immutable) {
this.texture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
return;
}
const texture = gl.createTexture();
const { texSize } = this;
const texSize = this.texSize;
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
Expand All @@ -313,16 +315,18 @@ class WebGL2Kernel extends WebGLKernel {
}

_setupSubOutputTextures() {
const { context: gl } = this;
const gl = this.context;
if (this.mappedTextures && this.mappedTextures.length > 0) {
for (let i = 0; i < this.mappedTextures.length; i++) {
const mappedTexture = this.mappedTextures[i];
mappedTexture.beforeMutate();
if (this.immutable) {
mappedTexture.beforeMutate();
}
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
}
return;
}
const { texSize } = this;
const texSize = this.texSize;
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
this.mappedTextures = [];
for (let i = 0; i < this.subKernels.length; i++) {
Expand Down
39 changes: 39 additions & 0 deletions test/internal/texture-recycling.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,42 @@ function testCloning(mode) {
(GPU.isHeadlessGLSupported ? test : skip)('cloning sets up framebuffer with correct size headlessgl', () => {
testCloning('headlessgl');
});

function testMutableLeak(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function() {
return 1;
}, {
output: [1],
pipeline: true
});
kernel.build();
const cloneTextureSpy = sinon.spy(kernel.texture.constructor.prototype, 'beforeMutate');
const texture1 = kernel();
const texture2 = kernel();
assert.equal(cloneTextureSpy.callCount, 0);
assert.equal(texture1.texture._refs, 1);
assert.ok(texture1 === texture2);
cloneTextureSpy.restore();
gpu.destroy();
}

test('test mutable leak auto', () => {
testMutableLeak();
});

test('test mutable leak gpu', () => {
testMutableLeak('gpu');
});

(GPU.isWebGLSupported ? test : skip)('test mutable leak webgl', () => {
testMutableLeak('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('test mutable leak webgl2', () => {
testMutableLeak('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('test mutable leak headlessgl', () => {
testMutableLeak('headlessgl');
});

0 comments on commit 6df34b3

Please sign in to comment.