Skip to content

Commit 6df34b3

Browse files
fix: Don't call beforeMutate if immutable and unit test
1 parent b1af9b3 commit 6df34b3

File tree

8 files changed

+104
-38
lines changed

8 files changed

+104
-38
lines changed

dist/gpu-browser-core.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.8.1
8-
* @date Thu Mar 12 2020 07:32:27 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.8.2
8+
* @date Mon Mar 16 2020 08:37:54 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -10304,12 +10304,15 @@ class WebGLKernel extends GLKernel {
1030410304
}
1030510305

1030610306
_setupOutputTexture() {
10307-
const { context: gl, texSize } = this;
10307+
const gl = this.context;
1030810308
if (this.texture) {
10309-
this.texture.beforeMutate();
10309+
if (this.immutable) {
10310+
this.texture.beforeMutate();
10311+
}
1031010312
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
1031110313
return;
1031210314
}
10315+
const texSize = this.texSize;
1031310316
const texture = this.createTexture();
1031410317
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
1031510318
gl.bindTexture(gl.TEXTURE_2D, texture);
@@ -10341,7 +10344,9 @@ class WebGLKernel extends GLKernel {
1034110344
if (this.mappedTextures && this.mappedTextures.length > 0) {
1034210345
for (let i = 0; i < this.mappedTextures.length; i++) {
1034310346
const mappedTexture = this.mappedTextures[i];
10344-
mappedTexture.beforeMutate();
10347+
if (this.immutable) {
10348+
mappedTexture.beforeMutate();
10349+
}
1034510350
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
1034610351
}
1034710352
return;
@@ -12605,14 +12610,16 @@ class WebGL2Kernel extends WebGLKernel {
1260512610
}
1260612611

1260712612
_setupOutputTexture() {
12608-
const { context: gl } = this;
12613+
const gl = this.context;
1260912614
if (this.texture) {
12610-
this.texture.beforeMutate();
12615+
if (this.immutable) {
12616+
this.texture.beforeMutate();
12617+
}
1261112618
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
1261212619
return;
1261312620
}
1261412621
const texture = gl.createTexture();
12615-
const { texSize } = this;
12622+
const texSize = this.texSize;
1261612623
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
1261712624
gl.bindTexture(gl.TEXTURE_2D, texture);
1261812625
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
@@ -12639,16 +12646,18 @@ class WebGL2Kernel extends WebGLKernel {
1263912646
}
1264012647

1264112648
_setupSubOutputTextures() {
12642-
const { context: gl } = this;
12649+
const gl = this.context;
1264312650
if (this.mappedTextures && this.mappedTextures.length > 0) {
1264412651
for (let i = 0; i < this.mappedTextures.length; i++) {
1264512652
const mappedTexture = this.mappedTextures[i];
12646-
mappedTexture.beforeMutate();
12653+
if (this.immutable) {
12654+
mappedTexture.beforeMutate();
12655+
}
1264712656
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
1264812657
}
1264912658
return;
1265012659
}
12651-
const { texSize } = this;
12660+
const texSize = this.texSize;
1265212661
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
1265312662
this.mappedTextures = [];
1265412663
for (let i = 0; i < this.subKernels.length; i++) {

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gpu-browser.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.8.1
8-
* @date Thu Mar 12 2020 07:32:26 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.8.2
8+
* @date Mon Mar 16 2020 08:37:54 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -14757,12 +14757,15 @@ class WebGLKernel extends GLKernel {
1475714757
}
1475814758

1475914759
_setupOutputTexture() {
14760-
const { context: gl, texSize } = this;
14760+
const gl = this.context;
1476114761
if (this.texture) {
14762-
this.texture.beforeMutate();
14762+
if (this.immutable) {
14763+
this.texture.beforeMutate();
14764+
}
1476314765
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
1476414766
return;
1476514767
}
14768+
const texSize = this.texSize;
1476614769
const texture = this.createTexture();
1476714770
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
1476814771
gl.bindTexture(gl.TEXTURE_2D, texture);
@@ -14794,7 +14797,9 @@ class WebGLKernel extends GLKernel {
1479414797
if (this.mappedTextures && this.mappedTextures.length > 0) {
1479514798
for (let i = 0; i < this.mappedTextures.length; i++) {
1479614799
const mappedTexture = this.mappedTextures[i];
14797-
mappedTexture.beforeMutate();
14800+
if (this.immutable) {
14801+
mappedTexture.beforeMutate();
14802+
}
1479814803
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
1479914804
}
1480014805
return;
@@ -17058,14 +17063,16 @@ class WebGL2Kernel extends WebGLKernel {
1705817063
}
1705917064

1706017065
_setupOutputTexture() {
17061-
const { context: gl } = this;
17066+
const gl = this.context;
1706217067
if (this.texture) {
17063-
this.texture.beforeMutate();
17068+
if (this.immutable) {
17069+
this.texture.beforeMutate();
17070+
}
1706417071
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
1706517072
return;
1706617073
}
1706717074
const texture = gl.createTexture();
17068-
const { texSize } = this;
17075+
const texSize = this.texSize;
1706917076
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
1707017077
gl.bindTexture(gl.TEXTURE_2D, texture);
1707117078
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
@@ -17092,16 +17099,18 @@ class WebGL2Kernel extends WebGLKernel {
1709217099
}
1709317100

1709417101
_setupSubOutputTextures() {
17095-
const { context: gl } = this;
17102+
const gl = this.context;
1709617103
if (this.mappedTextures && this.mappedTextures.length > 0) {
1709717104
for (let i = 0; i < this.mappedTextures.length; i++) {
1709817105
const mappedTexture = this.mappedTextures[i];
17099-
mappedTexture.beforeMutate();
17106+
if (this.immutable) {
17107+
mappedTexture.beforeMutate();
17108+
}
1710017109
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
1710117110
}
1710217111
return;
1710317112
}
17104-
const { texSize } = this;
17113+
const texSize = this.texSize;
1710517114
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
1710617115
this.mappedTextures = [];
1710717116
for (let i = 0; i < this.subKernels.length; i++) {

dist/gpu-browser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.8.1",
3+
"version": "2.8.2",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/backend/web-gl/kernel.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,15 @@ class WebGLKernel extends GLKernel {
679679
* @desc Setup and replace output texture
680680
*/
681681
_setupOutputTexture() {
682-
const { context: gl, texSize } = this;
682+
const gl = this.context;
683683
if (this.texture) {
684-
this.texture.beforeMutate();
684+
if (this.immutable) {
685+
this.texture.beforeMutate();
686+
}
685687
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
686688
return;
687689
}
690+
const texSize = this.texSize;
688691
const texture = this.createTexture();
689692
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
690693
gl.bindTexture(gl.TEXTURE_2D, texture);
@@ -719,7 +722,9 @@ class WebGLKernel extends GLKernel {
719722
if (this.mappedTextures && this.mappedTextures.length > 0) {
720723
for (let i = 0; i < this.mappedTextures.length; i++) {
721724
const mappedTexture = this.mappedTextures[i];
722-
mappedTexture.beforeMutate();
725+
if (this.immutable) {
726+
mappedTexture.beforeMutate();
727+
}
723728
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
724729
}
725730
return;

src/backend/web-gl2/kernel.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,16 @@ class WebGL2Kernel extends WebGLKernel {
279279
}
280280

281281
_setupOutputTexture() {
282-
const { context: gl } = this;
282+
const gl = this.context;
283283
if (this.texture) {
284-
this.texture.beforeMutate();
284+
if (this.immutable) {
285+
this.texture.beforeMutate();
286+
}
285287
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
286288
return;
287289
}
288290
const texture = gl.createTexture();
289-
const { texSize } = this;
291+
const texSize = this.texSize;
290292
gl.activeTexture(gl.TEXTURE0 + this.constantTextureCount + this.argumentTextureCount);
291293
gl.bindTexture(gl.TEXTURE_2D, texture);
292294
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
@@ -313,16 +315,18 @@ class WebGL2Kernel extends WebGLKernel {
313315
}
314316

315317
_setupSubOutputTextures() {
316-
const { context: gl } = this;
318+
const gl = this.context;
317319
if (this.mappedTextures && this.mappedTextures.length > 0) {
318320
for (let i = 0; i < this.mappedTextures.length; i++) {
319321
const mappedTexture = this.mappedTextures[i];
320-
mappedTexture.beforeMutate();
322+
if (this.immutable) {
323+
mappedTexture.beforeMutate();
324+
}
321325
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i + 1, gl.TEXTURE_2D, mappedTexture.texture, 0);
322326
}
323327
return;
324328
}
325-
const { texSize } = this;
329+
const texSize = this.texSize;
326330
this.drawBuffersMap = [gl.COLOR_ATTACHMENT0];
327331
this.mappedTextures = [];
328332
for (let i = 0; i < this.subKernels.length; i++) {

test/internal/texture-recycling.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,42 @@ function testCloning(mode) {
415415
(GPU.isHeadlessGLSupported ? test : skip)('cloning sets up framebuffer with correct size headlessgl', () => {
416416
testCloning('headlessgl');
417417
});
418+
419+
function testMutableLeak(mode) {
420+
const gpu = new GPU({ mode });
421+
const kernel = gpu.createKernel(function() {
422+
return 1;
423+
}, {
424+
output: [1],
425+
pipeline: true
426+
});
427+
kernel.build();
428+
const cloneTextureSpy = sinon.spy(kernel.texture.constructor.prototype, 'beforeMutate');
429+
const texture1 = kernel();
430+
const texture2 = kernel();
431+
assert.equal(cloneTextureSpy.callCount, 0);
432+
assert.equal(texture1.texture._refs, 1);
433+
assert.ok(texture1 === texture2);
434+
cloneTextureSpy.restore();
435+
gpu.destroy();
436+
}
437+
438+
test('test mutable leak auto', () => {
439+
testMutableLeak();
440+
});
441+
442+
test('test mutable leak gpu', () => {
443+
testMutableLeak('gpu');
444+
});
445+
446+
(GPU.isWebGLSupported ? test : skip)('test mutable leak webgl', () => {
447+
testMutableLeak('webgl');
448+
});
449+
450+
(GPU.isWebGL2Supported ? test : skip)('test mutable leak webgl2', () => {
451+
testMutableLeak('webgl2');
452+
});
453+
454+
(GPU.isHeadlessGLSupported ? test : skip)('test mutable leak headlessgl', () => {
455+
testMutableLeak('headlessgl');
456+
});

0 commit comments

Comments
 (0)