Skip to content

Commit 3fe63f6

Browse files
feat: Add texture.empty() support and tests
1 parent 20b456b commit 3fe63f6

File tree

13 files changed

+146
-51
lines changed

13 files changed

+146
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ worker.onmessage = function(e) {
891891
* for instances of `GPU` use the `destroy` method. Example: `gpu.destroy()`
892892
* for instances of `Kernel` use the `destroy` method. Example: `kernel.destroy()`
893893
* for instances of `Texture` use the `delete` method. Example: `texture.delete()`
894+
* for instances of `Texture` that you might want to reuse/reset to zeros, use the `empty` method. Example: `texture.empty()`
894895

895896
## Flattened typed array support
896897
To use the useful `x`, `y`, `z` `thread` lookup api inside of GPU.js, and yet use flattened arrays, there is the `Input` type.

dist/gpu-browser-core.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.6.11
8-
* @date Mon Mar 09 2020 05:48:54 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.7.0
8+
* @date Tue Mar 10 2020 15:46:55 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -4353,12 +4353,13 @@ function getToArrayString(kernelResult, textureName) {
43534353
throw new Error('unhandled fromObject');
43544354
}
43554355
},
4356-
thisLookup: (property) => {
4356+
thisLookup: (property, isDeclaration) => {
43574357
if (property === 'texture') {
43584358
return textureName;
43594359
}
43604360
if (property === 'context') {
4361-
return null;
4361+
if (isDeclaration) return null;
4362+
return 'gl';
43624363
}
43634364
if (property === '_framebuffer') {
43644365
return '_framebuffer';
@@ -5503,10 +5504,7 @@ class GLTextureFloat extends GLTexture {
55035504
}
55045505
renderRawOutput() {
55055506
const { context: gl, size } = this;
5506-
if (!this._framebuffer) {
5507-
this._framebuffer = gl.createFramebuffer();
5508-
}
5509-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
5507+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
55105508
gl.framebufferTexture2D(
55115509
gl.FRAMEBUFFER,
55125510
gl.COLOR_ATTACHMENT0,
@@ -5571,12 +5569,7 @@ class GLTexture extends Texture {
55715569
console.warn('cloning internal texture');
55725570
}
55735571
const existingFramebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
5574-
if (!this._framebuffer) {
5575-
this._framebuffer = gl.createFramebuffer();
5576-
}
5577-
this._framebuffer.width = size[0];
5578-
this._framebuffer.height = size[1];
5579-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
5572+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
55805573
selectTexture(gl, texture);
55815574
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
55825575
const target = gl.createTexture();
@@ -5590,13 +5583,32 @@ class GLTexture extends Texture {
55905583
}
55915584
}
55925585

5586+
clear() {
5587+
const { context: gl, size, texture } = this;
5588+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
5589+
gl.bindTexture(gl.TEXTURE_2D, texture);
5590+
selectTexture(gl, texture);
5591+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
5592+
gl.clearColor(0, 0, 0, 0);
5593+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
5594+
}
5595+
55935596
delete() {
55945597
super.delete();
55955598
if (this.texture._refs === 0 && this._framebuffer) {
55965599
this.context.deleteFramebuffer(this._framebuffer);
55975600
this._framebuffer = null;
55985601
}
55995602
}
5603+
5604+
framebuffer() {
5605+
if (!this._framebuffer) {
5606+
this._framebuffer = this.context.createFramebuffer();
5607+
}
5608+
this._framebuffer.width = this.size[0];
5609+
this._framebuffer.height = this.size[1];
5610+
return this._framebuffer;
5611+
}
56005612
}
56015613

56025614
function selectTexture(gl, texture) {
@@ -14254,7 +14266,7 @@ const utils = {
1425414266
}
1425514267
case 'VariableDeclarator':
1425614268
if (ast.init.object && ast.init.object.type === 'ThisExpression') {
14257-
const lookup = thisLookup(ast.init.property.name);
14269+
const lookup = thisLookup(ast.init.property.name, true);
1425814270
if (lookup) {
1425914271
return `${ast.id.name} = ${flatten(ast.init)}`;
1426014272
} else {

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: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.6.11
8-
* @date Mon Mar 09 2020 05:48:54 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.7.0
8+
* @date Tue Mar 10 2020 15:46:55 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -8806,12 +8806,13 @@ function getToArrayString(kernelResult, textureName) {
88068806
throw new Error('unhandled fromObject');
88078807
}
88088808
},
8809-
thisLookup: (property) => {
8809+
thisLookup: (property, isDeclaration) => {
88108810
if (property === 'texture') {
88118811
return textureName;
88128812
}
88138813
if (property === 'context') {
8814-
return null;
8814+
if (isDeclaration) return null;
8815+
return 'gl';
88158816
}
88168817
if (property === '_framebuffer') {
88178818
return '_framebuffer';
@@ -9956,10 +9957,7 @@ class GLTextureFloat extends GLTexture {
99569957
}
99579958
renderRawOutput() {
99589959
const { context: gl, size } = this;
9959-
if (!this._framebuffer) {
9960-
this._framebuffer = gl.createFramebuffer();
9961-
}
9962-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
9960+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
99639961
gl.framebufferTexture2D(
99649962
gl.FRAMEBUFFER,
99659963
gl.COLOR_ATTACHMENT0,
@@ -10024,12 +10022,7 @@ class GLTexture extends Texture {
1002410022
console.warn('cloning internal texture');
1002510023
}
1002610024
const existingFramebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
10027-
if (!this._framebuffer) {
10028-
this._framebuffer = gl.createFramebuffer();
10029-
}
10030-
this._framebuffer.width = size[0];
10031-
this._framebuffer.height = size[1];
10032-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
10025+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
1003310026
selectTexture(gl, texture);
1003410027
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
1003510028
const target = gl.createTexture();
@@ -10043,13 +10036,32 @@ class GLTexture extends Texture {
1004310036
}
1004410037
}
1004510038

10039+
clear() {
10040+
const { context: gl, size, texture } = this;
10041+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
10042+
gl.bindTexture(gl.TEXTURE_2D, texture);
10043+
selectTexture(gl, texture);
10044+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
10045+
gl.clearColor(0, 0, 0, 0);
10046+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
10047+
}
10048+
1004610049
delete() {
1004710050
super.delete();
1004810051
if (this.texture._refs === 0 && this._framebuffer) {
1004910052
this.context.deleteFramebuffer(this._framebuffer);
1005010053
this._framebuffer = null;
1005110054
}
1005210055
}
10056+
10057+
framebuffer() {
10058+
if (!this._framebuffer) {
10059+
this._framebuffer = this.context.createFramebuffer();
10060+
}
10061+
this._framebuffer.width = this.size[0];
10062+
this._framebuffer.height = this.size[1];
10063+
return this._framebuffer;
10064+
}
1005310065
}
1005410066

1005510067
function selectTexture(gl, texture) {
@@ -18707,7 +18719,7 @@ const utils = {
1870718719
}
1870818720
case 'VariableDeclarator':
1870918721
if (ast.init.object && ast.init.object.type === 'ThisExpression') {
18710-
const lookup = thisLookup(ast.init.property.name);
18722+
const lookup = thisLookup(ast.init.property.name, true);
1871118723
if (lookup) {
1871218724
return `${ast.id.name} = ${flatten(ast.init)}`;
1871318725
} else {

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.6.11",
3+
"version": "2.7.0",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/backend/gl/kernel-string.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,13 @@ function getToArrayString(kernelResult, textureName) {
292292
throw new Error('unhandled fromObject');
293293
}
294294
},
295-
thisLookup: (property) => {
295+
thisLookup: (property, isDeclaration) => {
296296
if (property === 'texture') {
297297
return textureName;
298298
}
299299
if (property === 'context') {
300-
return null;
300+
if (isDeclaration) return null;
301+
return 'gl';
301302
}
302303
if (property === '_framebuffer') {
303304
return '_framebuffer';

src/backend/gl/texture/float.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ class GLTextureFloat extends GLTexture {
1111
}
1212
renderRawOutput() {
1313
const { context: gl, size } = this;
14-
if (!this._framebuffer) {
15-
this._framebuffer = gl.createFramebuffer();
16-
}
17-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
14+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
1815
gl.framebufferTexture2D(
1916
gl.FRAMEBUFFER,
2017
gl.COLOR_ATTACHMENT0,

src/backend/gl/texture/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ class GLTexture extends Texture {
3434
console.warn('cloning internal texture');
3535
}
3636
const existingFramebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
37-
if (!this._framebuffer) {
38-
this._framebuffer = gl.createFramebuffer();
39-
}
40-
this._framebuffer.width = size[0];
41-
this._framebuffer.height = size[1];
42-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
37+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
4338
selectTexture(gl, texture);
4439
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
4540
const target = gl.createTexture();
@@ -53,13 +48,32 @@ class GLTexture extends Texture {
5348
}
5449
}
5550

51+
clear() {
52+
const { context: gl, size, texture } = this;
53+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer());
54+
gl.bindTexture(gl.TEXTURE_2D, texture);
55+
selectTexture(gl, texture);
56+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
57+
gl.clearColor(0, 0, 0, 0);
58+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
59+
}
60+
5661
delete() {
5762
super.delete();
5863
if (this.texture._refs === 0 && this._framebuffer) {
5964
this.context.deleteFramebuffer(this._framebuffer);
6065
this._framebuffer = null;
6166
}
6267
}
68+
69+
framebuffer() {
70+
if (!this._framebuffer) {
71+
this._framebuffer = this.context.createFramebuffer();
72+
}
73+
this._framebuffer.width = this.size[0];
74+
this._framebuffer.height = this.size[1];
75+
return this._framebuffer;
76+
}
6377
}
6478

6579
function selectTexture(gl, texture) {

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class GPU<ArgTypes extends ThreadKernelVariable[] = ThreadKernelVariable[
1717
addNativeFunction(name: string, source: string, settings?: IGPUFunctionSettings): this;
1818
combineKernels(...kernels: KernelFunction[]): IKernelRunShortcut;
1919
combineKernels<KF extends KernelFunction>(...kernels: KF[]):
20-
((...args: Parameters<KF>) =>
20+
((...args: Parameters<KF>) =>
2121
ReturnType<KF>[]
2222
| ReturnType<KF>[][]
2323
| ReturnType<KF>[][][]
@@ -562,6 +562,7 @@ export class Texture {
562562
toArray(): TextureArrayOutput;
563563
clone(): Texture;
564564
delete(): void;
565+
clear(): void;
565566
kernel: Kernel;
566567
}
567568

0 commit comments

Comments
 (0)