Skip to content

Commit

Permalink
fix: #590 add Kernel.onActivate
Browse files Browse the repository at this point in the history
- which is called after gpu.js switches kernels based off need
fix: Consistent error message for values that are not defined
fix: Add recompiled kernels to gpu.js kernels property and test
fix: Move texture deleting to within the GLKernel Texture implementation
fix: Add Texture.clear as an abstract method on the base Texture
fix: Bump and build
  • Loading branch information
robertleeplummerjr committed Apr 1, 2020
1 parent 7e62639 commit 4d2f27e
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 92 deletions.
73 changes: 46 additions & 27 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.9.2
* @date Mon Mar 30 2020 08:17:24 GMT-0400 (Eastern Daylight Time)
* @version 2.9.3
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -2952,11 +2952,7 @@ class FunctionNode {
if (this.isAstVariable(ast)) {
const signature = this.getVariableSignature(ast);
if (signature === 'value') {
const type = this.getVariableType(ast);
if (!type) {
throw this.astErrorOutput(`Unable to find identifier valueType`, ast);
}
return type;
return this.getCheckVariableType(ast);
}
}
const origin = this.findIdentifierOrigin(ast);
Expand All @@ -2982,13 +2978,13 @@ class FunctionNode {
const variableSignature = this.getVariableSignature(ast);
switch (variableSignature) {
case 'value[]':
return this.getLookupType(this.getVariableType(ast.object));
return this.getLookupType(this.getCheckVariableType(ast.object));
case 'value[][]':
return this.getLookupType(this.getVariableType(ast.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object));
case 'value[][][]':
return this.getLookupType(this.getVariableType(ast.object.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object.object));
case 'value[][][][]':
return this.getLookupType(this.getVariableType(ast.object.object.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object.object.object));
case 'value.thread.value':
case 'this.thread.value':
return 'Integer';
Expand All @@ -3005,9 +3001,7 @@ class FunctionNode {
case 'this.constants.value[][][][]':
return this.getLookupType(this.getConstantType(ast.object.object.object.object.property.name));
case 'fn()[]':
return this.getLookupType(this.getType(ast.object));
case 'fn()[][]':
return this.getLookupType(this.getType(ast.object));
case 'fn()[][][]':
return this.getLookupType(this.getType(ast.object));
case 'value.value':
Expand All @@ -3016,13 +3010,10 @@ class FunctionNode {
}
switch (ast.property.name) {
case 'r':
return this.getLookupType(this.getVariableType(ast.object));
case 'g':
return this.getLookupType(this.getVariableType(ast.object));
case 'b':
return this.getLookupType(this.getVariableType(ast.object));
case 'a':
return this.getLookupType(this.getVariableType(ast.object));
return this.getLookupType(this.getCheckVariableType(ast.object));
}
case '[][]':
return 'Number';
Expand All @@ -3048,6 +3039,14 @@ class FunctionNode {
}
}

getCheckVariableType(ast) {
const type = this.getVariableType(ast);
if (!type) {
throw this.astErrorOutput(`${ast.type} is not defined`, ast);
}
return type;
}

inferArgumentTypesIfNeeded(functionName, args) {
for (let i = 0; i < args.length; i++) {
if (!this.needsArgumentType(functionName, i)) continue;
Expand Down Expand Up @@ -5456,6 +5455,17 @@ class GLKernel extends Kernel {
}
}

onActivate(previousKernel) {
this._textureSwitched = true;
this.texture = previousKernel.texture;
if (this.mappedTextures) {
for (let i = 0; i < this.mappedTextures.length; i++) {
this._mappedTextureSwitched[i] = true;
}
this.mappedTextures = previousKernel.mappedTextures;
}
}

initCanvas() {}
}

Expand Down Expand Up @@ -5784,7 +5794,13 @@ class GLTexture extends Texture {
}

delete() {
super.delete();
if (this._deleted) return;
this._deleted = true;
if (this.texture._refs) {
this.texture._refs--;
if (this.texture._refs) return;
}
this.context.deleteTexture(this.texture);
if (this.texture._refs === 0 && this._framebuffer) {
this.context.deleteFramebuffer(this._framebuffer);
this._framebuffer = null;
Expand Down Expand Up @@ -6722,6 +6738,8 @@ class Kernel {
returnType: settings.returnType || null,
};
}

onActivate(previousKernel) {}
}

function splitArgumentTypes(argumentTypesObject) {
Expand Down Expand Up @@ -13504,9 +13522,10 @@ class GPU {
throw new Error('source parameter not a function');
}

const kernels = this.kernels;
if (this.mode === 'dev') {
const devKernel = gpuMock(source, upgradeDeprecatedCreateKernelSettings(settings));
this.kernels.push(devKernel);
kernels.push(devKernel);
return devKernel;
}

Expand Down Expand Up @@ -13568,6 +13587,7 @@ class GPU {
const signature = Constructor.getSignature(_kernel, argumentTypes);
const existingKernel = switchableKernels[signature];
if (existingKernel) {
existingKernel.onActivate(_kernel);
return existingKernel;
}

Expand Down Expand Up @@ -13607,6 +13627,7 @@ class GPU {
});
newKernel.build.apply(newKernel, args);
kernelRun.replaceKernel(newKernel);
kernels.push(newKernel);
return newKernel;
}
const mergedSettings = Object.assign({
Expand Down Expand Up @@ -13634,7 +13655,7 @@ class GPU {
this.context = kernel.context;
}

this.kernels.push(kernel);
kernels.push(kernel);

return kernelRun;
}
Expand Down Expand Up @@ -14077,13 +14098,11 @@ class Texture {
}

delete() {
if (this._deleted) return;
this._deleted = true;
if (this.texture._refs) {
this.texture._refs--;
if (this.texture._refs) return;
}
return this.context.deleteTexture(this.texture);
throw new Error(`Not implemented on ${this.constructor.name}`);
}

clear() {
throw new Error(`Not implemented on ${this.constructor.name}`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

73 changes: 46 additions & 27 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.9.2
* @date Mon Mar 30 2020 08:17:24 GMT-0400 (Eastern Daylight Time)
* @version 2.9.3
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -7405,11 +7405,7 @@ class FunctionNode {
if (this.isAstVariable(ast)) {
const signature = this.getVariableSignature(ast);
if (signature === 'value') {
const type = this.getVariableType(ast);
if (!type) {
throw this.astErrorOutput(`Unable to find identifier valueType`, ast);
}
return type;
return this.getCheckVariableType(ast);
}
}
const origin = this.findIdentifierOrigin(ast);
Expand All @@ -7435,13 +7431,13 @@ class FunctionNode {
const variableSignature = this.getVariableSignature(ast);
switch (variableSignature) {
case 'value[]':
return this.getLookupType(this.getVariableType(ast.object));
return this.getLookupType(this.getCheckVariableType(ast.object));
case 'value[][]':
return this.getLookupType(this.getVariableType(ast.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object));
case 'value[][][]':
return this.getLookupType(this.getVariableType(ast.object.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object.object));
case 'value[][][][]':
return this.getLookupType(this.getVariableType(ast.object.object.object.object));
return this.getLookupType(this.getCheckVariableType(ast.object.object.object.object));
case 'value.thread.value':
case 'this.thread.value':
return 'Integer';
Expand All @@ -7458,9 +7454,7 @@ class FunctionNode {
case 'this.constants.value[][][][]':
return this.getLookupType(this.getConstantType(ast.object.object.object.object.property.name));
case 'fn()[]':
return this.getLookupType(this.getType(ast.object));
case 'fn()[][]':
return this.getLookupType(this.getType(ast.object));
case 'fn()[][][]':
return this.getLookupType(this.getType(ast.object));
case 'value.value':
Expand All @@ -7469,13 +7463,10 @@ class FunctionNode {
}
switch (ast.property.name) {
case 'r':
return this.getLookupType(this.getVariableType(ast.object));
case 'g':
return this.getLookupType(this.getVariableType(ast.object));
case 'b':
return this.getLookupType(this.getVariableType(ast.object));
case 'a':
return this.getLookupType(this.getVariableType(ast.object));
return this.getLookupType(this.getCheckVariableType(ast.object));
}
case '[][]':
return 'Number';
Expand All @@ -7501,6 +7492,14 @@ class FunctionNode {
}
}

getCheckVariableType(ast) {
const type = this.getVariableType(ast);
if (!type) {
throw this.astErrorOutput(`${ast.type} is not defined`, ast);
}
return type;
}

inferArgumentTypesIfNeeded(functionName, args) {
for (let i = 0; i < args.length; i++) {
if (!this.needsArgumentType(functionName, i)) continue;
Expand Down Expand Up @@ -9909,6 +9908,17 @@ class GLKernel extends Kernel {
}
}

onActivate(previousKernel) {
this._textureSwitched = true;
this.texture = previousKernel.texture;
if (this.mappedTextures) {
for (let i = 0; i < this.mappedTextures.length; i++) {
this._mappedTextureSwitched[i] = true;
}
this.mappedTextures = previousKernel.mappedTextures;
}
}

initCanvas() {}
}

Expand Down Expand Up @@ -10237,7 +10247,13 @@ class GLTexture extends Texture {
}

delete() {
super.delete();
if (this._deleted) return;
this._deleted = true;
if (this.texture._refs) {
this.texture._refs--;
if (this.texture._refs) return;
}
this.context.deleteTexture(this.texture);
if (this.texture._refs === 0 && this._framebuffer) {
this.context.deleteFramebuffer(this._framebuffer);
this._framebuffer = null;
Expand Down Expand Up @@ -11175,6 +11191,8 @@ class Kernel {
returnType: settings.returnType || null,
};
}

onActivate(previousKernel) {}
}

function splitArgumentTypes(argumentTypesObject) {
Expand Down Expand Up @@ -17957,9 +17975,10 @@ class GPU {
throw new Error('source parameter not a function');
}

const kernels = this.kernels;
if (this.mode === 'dev') {
const devKernel = gpuMock(source, upgradeDeprecatedCreateKernelSettings(settings));
this.kernels.push(devKernel);
kernels.push(devKernel);
return devKernel;
}

Expand Down Expand Up @@ -18021,6 +18040,7 @@ class GPU {
const signature = Constructor.getSignature(_kernel, argumentTypes);
const existingKernel = switchableKernels[signature];
if (existingKernel) {
existingKernel.onActivate(_kernel);
return existingKernel;
}

Expand Down Expand Up @@ -18060,6 +18080,7 @@ class GPU {
});
newKernel.build.apply(newKernel, args);
kernelRun.replaceKernel(newKernel);
kernels.push(newKernel);
return newKernel;
}
const mergedSettings = Object.assign({
Expand Down Expand Up @@ -18087,7 +18108,7 @@ class GPU {
this.context = kernel.context;
}

this.kernels.push(kernel);
kernels.push(kernel);

return kernelRun;
}
Expand Down Expand Up @@ -18530,13 +18551,11 @@ class Texture {
}

delete() {
if (this._deleted) return;
this._deleted = true;
if (this.texture._refs) {
this.texture._refs--;
if (this.texture._refs) return;
}
return this.context.deleteTexture(this.texture);
throw new Error(`Not implemented on ${this.constructor.name}`);
}

clear() {
throw new Error(`Not implemented on ${this.constructor.name}`);
}
}

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.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.9.2",
"version": "2.9.3",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down Expand Up @@ -44,8 +44,8 @@
"vinyl-source-stream": "^2.0.0"
},
"scripts": {
"test": "qunit",
"coverage": "c8 qunit",
"test": "qunit test/issues test/internal test/features",
"coverage": "c8 qunit test/issues test/internal test/features",
"setup": "npm i -g gulp-cli",
"make": "gulp make",
"build": "gulp build",
Expand Down
Loading

0 comments on commit 4d2f27e

Please sign in to comment.