Skip to content

Commit 8cfbf0c

Browse files
fix: Clearing cloned textures produces new internal copy when multi references
1 parent 0a072fe commit 8cfbf0c

File tree

8 files changed

+80
-15
lines changed

8 files changed

+80
-15
lines changed

dist/gpu-browser-core.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.8.3
8-
* @date Mon Mar 16 2020 08:42:55 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.8.4
8+
* @date Mon Mar 16 2020 10:58:11 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -5591,7 +5591,16 @@ class GLTexture extends Texture {
55915591
}
55925592

55935593
clear() {
5594-
const { context: gl, size, texture } = this;
5594+
if (this.texture._refs) {
5595+
this.texture._refs--;
5596+
const gl = this.context;
5597+
const target = this.texture = gl.createTexture();
5598+
selectTexture(gl, target);
5599+
const size = this.size;
5600+
target._refs = 1;
5601+
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
5602+
}
5603+
const { context: gl, texture } = this;
55955604
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
55965605
gl.bindTexture(gl.TEXTURE_2D, texture);
55975606
selectTexture(gl, texture);

dist/gpu-browser-core.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gpu-browser.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.8.3
8-
* @date Mon Mar 16 2020 08:42:55 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.8.4
8+
* @date Mon Mar 16 2020 10:58:11 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -10044,7 +10044,16 @@ class GLTexture extends Texture {
1004410044
}
1004510045

1004610046
clear() {
10047-
const { context: gl, size, texture } = this;
10047+
if (this.texture._refs) {
10048+
this.texture._refs--;
10049+
const gl = this.context;
10050+
const target = this.texture = gl.createTexture();
10051+
selectTexture(gl, target);
10052+
const size = this.size;
10053+
target._refs = 1;
10054+
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
10055+
}
10056+
const { context: gl, texture } = this;
1004810057
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
1004910058
gl.bindTexture(gl.TEXTURE_2D, texture);
1005010059
selectTexture(gl, texture);

dist/gpu-browser.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.8.3",
3+
"version": "2.8.4",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/backend/gl/texture/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ class GLTexture extends Texture {
4949
}
5050

5151
clear() {
52-
const { context: gl, size, texture } = this;
52+
if (this.texture._refs) {
53+
this.texture._refs--;
54+
const gl = this.context;
55+
const target = this.texture = gl.createTexture();
56+
selectTexture(gl, target);
57+
const size = this.size;
58+
target._refs = 1;
59+
gl.texImage2D(gl.TEXTURE_2D, 0, this.internalFormat, size[0], size[1], 0, this.textureFormat, this.textureType, null);
60+
}
61+
const { context: gl, texture } = this;
5362
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
5463
gl.bindTexture(gl.TEXTURE_2D, texture);
5564
selectTexture(gl, texture);

test/features/clear-textures.js

+38
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,41 @@ test('unsigned precision auto', () => {
5454
(GPU.isSinglePrecisionSupported && GPU.isHeadlessGLSupported ? test : skip)('single precision headlessgl', () => {
5555
clearTexture('single', 'headlessgl');
5656
});
57+
58+
59+
function clearClonedTexture(mode) {
60+
const gpu = new GPU({ mode });
61+
const kernel = gpu.createKernel(function() {
62+
return 1;
63+
}, { output: [1], pipeline: true, immutable: true });
64+
const result = kernel();
65+
assert.equal(result.toArray()[0], 1);
66+
const result2 = result.clone();
67+
const result3 = result2.clone();
68+
assert.equal(result2.toArray()[0], 1);
69+
assert.equal(result3.toArray()[0], 1);
70+
result2.clear();
71+
assert.equal(result2.toArray()[0], 0);
72+
assert.equal(result3.toArray()[0], 1);
73+
gpu.destroy();
74+
}
75+
76+
test('clear cloned texture auto', () => {
77+
clearClonedTexture();
78+
});
79+
80+
test('clear cloned texture gpu', () => {
81+
clearClonedTexture('gpu');
82+
});
83+
84+
(GPU.isWebGLSupported ? test : skip)('clear cloned texture webgl', () => {
85+
clearClonedTexture('webgl');
86+
});
87+
88+
(GPU.isWebGL2Supported ? test : skip)('clear cloned texture webgl2', () => {
89+
clearClonedTexture('webgl2');
90+
});
91+
92+
(GPU.isHeadlessGLSupported ? test : skip)('clear cloned texture headlessgl', () => {
93+
clearClonedTexture('headlessgl');
94+
});

0 commit comments

Comments
 (0)