Skip to content

Commit 4aa08ca

Browse files
fix: Casting for all non-float arguments used with Math.fn()
fix: Greater than, less than evaluation when comparing an integer with a literal that is a float, try not to cast left, but rather cast left to right fix: Constants that do not have a type defined should fail fix: Don't prevent "random" as a native function name fix: Allow Texture constructor to inherit instance of GPU for easy .toArray() fix: Texture constructor as settings object for easier understanding of order input fix: Typescript corrections for Texture fix: Bump headless-gl version to that of Multiple Render Targets
1 parent fb9be63 commit 4aa08ca

File tree

17 files changed

+2244
-242
lines changed

17 files changed

+2244
-242
lines changed

bin/gpu-browser-core.js

Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.0.0-rc.3
8-
* @date Wed Mar 13 2019 12:42:17 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.0.0-rc.4
8+
* @date Sun Mar 17 2019 22:11:56 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -1622,7 +1622,7 @@ class FunctionNode {
16221622
return type;
16231623
}
16241624
}
1625-
return null;
1625+
throw new Error(`Type for constant "${ constantName }" not declared`);
16261626
}
16271627

16281628
getUserArgumentName(name) {
@@ -2842,6 +2842,8 @@ class Kernel {
28422842

28432843
this.context = null;
28442844

2845+
this.gpu = null;
2846+
28452847
this.functions = null;
28462848

28472849
this.nativeFunctions = null;
@@ -3472,6 +3474,18 @@ class WebGLFunctionNode extends FunctionNode {
34723474

34733475
case 'Integer & Float':
34743476
case 'Integer & Number':
3477+
if (ast.operator === '>' || ast.operator === '<' && ast.right.type === 'Literal') {
3478+
if (!Number.isInteger(ast.right.value)) {
3479+
this.pushState('casting-to-float');
3480+
retArr.push('float(');
3481+
this.astGeneric(ast.left, retArr);
3482+
retArr.push(')');
3483+
this.popState('casting-to-float');
3484+
retArr.push(operatorMap[ast.operator] || ast.operator);
3485+
this.astGeneric(ast.right, retArr);
3486+
break;
3487+
}
3488+
}
34753489
this.astGeneric(ast.left, retArr);
34763490
retArr.push(operatorMap[ast.operator] || ast.operator);
34773491
this.pushState('casting-to-integer');
@@ -3972,8 +3986,9 @@ class WebGLFunctionNode extends FunctionNode {
39723986
}
39733987

39743988
let funcName = this.astMemberExpressionUnroll(ast.callee);
3989+
const isMathFunction = funcName.indexOf(jsMathPrefix) === 0;
39753990

3976-
if (funcName.indexOf(jsMathPrefix) === 0) {
3991+
if (isMathFunction) {
39773992
funcName = funcName.slice(jsMathPrefix.length);
39783993
}
39793994

@@ -3995,15 +4010,15 @@ class WebGLFunctionNode extends FunctionNode {
39954010
const functionArguments = [];
39964011
this.calledFunctionsArguments[funcName].push(functionArguments);
39974012

3998-
if (funcName === 'random' && this.plugins) {
4013+
if (funcName === 'random' && this.plugins && this.plugins.length > 0) {
39994014
for (let i = 0; i < this.plugins.length; i++) {
40004015
const plugin = this.plugins[i];
40014016
if (plugin.functionMatch === 'Math.random()' && plugin.functionReplace) {
40024017
functionArguments.push(plugin.functionReturnType);
40034018
retArr.push(plugin.functionReplace);
4019+
return retArr;
40044020
}
40054021
}
4006-
return retArr;
40074022
}
40084023

40094024
retArr.push(funcName);
@@ -4069,6 +4084,29 @@ class WebGLFunctionNode extends FunctionNode {
40694084
}
40704085
throw new Error(`Unhandled argument combination of ${ argumentType } and ${ targetType }`);
40714086
}
4087+
} else if (isMathFunction) {
4088+
for (let i = 0; i < ast.arguments.length; ++i) {
4089+
const argument = ast.arguments[i];
4090+
const argumentType = this.getType(argument);
4091+
4092+
if (i > 0) {
4093+
retArr.push(', ');
4094+
}
4095+
4096+
switch (argumentType) {
4097+
case 'Integer':
4098+
this.pushState('casting-to-float');
4099+
retArr.push('float(');
4100+
this.astGeneric(argument, retArr);
4101+
retArr.push(')');
4102+
this.popState('casting-to-float');
4103+
break;
4104+
case 'LiteralInteger':
4105+
default:
4106+
this.astGeneric(argument, retArr);
4107+
break;
4108+
}
4109+
}
40724110
} else {
40734111
for (let i = 0; i < ast.arguments.length; ++i) {
40744112
const argument = ast.arguments[i];
@@ -4726,7 +4764,15 @@ class WebGLKernel extends GLKernel {
47264764
this._setupOutputTexture();
47274765
}
47284766
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
4729-
return new Texture(this.outputTexture, texSize, this.threadDim, this.output, this.context, 'ArrayTexture(4)');
4767+
return new Texture({
4768+
texture: this.outputTexture,
4769+
size: texSize,
4770+
dimensions: this.threadDim,
4771+
output: this.output,
4772+
context: this.context,
4773+
gpu: this.gpu,
4774+
type: 'ArrayTexture(4)'
4775+
});
47304776
}
47314777
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
47324778
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
@@ -4756,7 +4802,14 @@ class WebGLKernel extends GLKernel {
47564802
result: this.renderOutput(outputTexture),
47574803
};
47584804
for (let i = 0; i < this.subKernels.length; i++) {
4759-
output[this.subKernels[i].property] = new Texture(this.subKernelOutputTextures[i], texSize, this.threadDim, this.output, this.context);
4805+
output[this.subKernels[i].property] = new Texture({
4806+
texture: this.subKernelOutputTextures[i],
4807+
size: texSize,
4808+
dimensions: this.threadDim,
4809+
output: this.output,
4810+
context: this.context,
4811+
gpu: this.gpu,
4812+
});
47604813
}
47614814
return output;
47624815
}
@@ -4771,7 +4824,14 @@ class WebGLKernel extends GLKernel {
47714824
const threadDim = this.threadDim;
47724825
const output = this.output;
47734826
if (this.pipeline) {
4774-
return new Texture(outputTexture, texSize, this.threadDim, output, this.context);
4827+
return new Texture({
4828+
texture: outputTexture,
4829+
size: texSize,
4830+
dimensions: this.threadDim,
4831+
output,
4832+
context: this.context,
4833+
gpu: this.gpu,
4834+
});
47754835
} else {
47764836
let result;
47774837
if (this.floatOutput) {
@@ -6100,7 +6160,15 @@ class WebGL2Kernel extends WebGLKernel {
61006160
this._setupOutputTexture();
61016161
}
61026162
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
6103-
return new Texture(this.outputTexture, texSize, this.threadDim, this.output, this.context, 'ArrayTexture(4)');
6163+
return new Texture({
6164+
texture: this.outputTexture,
6165+
size: texSize,
6166+
dimensions: this.threadDim,
6167+
output: this.output,
6168+
context: this.context,
6169+
gpu: this.gpu,
6170+
type: 'ArrayTexture(4)'
6171+
});
61046172
}
61056173
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
61066174
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
@@ -6130,7 +6198,14 @@ class WebGL2Kernel extends WebGLKernel {
61306198
result: this.renderOutput(outputTexture)
61316199
};
61326200
for (let i = 0; i < this.subKernels.length; i++) {
6133-
output[this.subKernels[i].property] = new Texture(this.subKernelOutputTextures[i], texSize, this.threadDim, this.output, this.context);
6201+
output[this.subKernels[i].property] = new Texture({
6202+
texture: this.subKernelOutputTextures[i],
6203+
size: texSize,
6204+
dimensions: this.threadDim,
6205+
output: this.output,
6206+
context: this.context,
6207+
gpu: this.gpu,
6208+
});
61346209
}
61356210
return output;
61366211
}
@@ -6993,7 +7068,8 @@ class GPU {
69937068
context: this.context,
69947069
canvas: this.canvas,
69957070
functions: this.functions,
6996-
nativeFunctions: this.nativeFunctions
7071+
nativeFunctions: this.nativeFunctions,
7072+
gpu: this,
69977073
}, settings || {});
69987074

69997075
const kernel = kernelRunShortcut(new this.Kernel(source, mergedSettings));
@@ -7350,20 +7426,32 @@ module.exports = {
73507426
};
73517427
},{}],27:[function(require,module,exports){
73527428
class Texture {
7353-
constructor(texture, size, dimensions, output, context, type = 'NumberTexture') {
7429+
constructor(settings) {
7430+
const {
7431+
texture,
7432+
size,
7433+
dimensions,
7434+
output,
7435+
context,
7436+
gpu,
7437+
type = 'NumberTexture'
7438+
} = settings;
7439+
if (!output) throw new Error('settings property "output" required.');
7440+
if (!context) throw new Error('settings property "context" required.');
73547441
this.texture = texture;
73557442
this.size = size;
73567443
this.dimensions = dimensions;
73577444
this.output = output;
73587445
this.context = context;
7446+
this.gpu = gpu;
73597447
this.kernel = null;
73607448
this.type = type;
73617449
}
73627450

73637451
toArray(gpu) {
7364-
if (!gpu) throw new Error('You need to pass the GPU object for toArray to work.');
73657452
if (this.kernel) return this.kernel(this);
7366-
7453+
gpu = gpu || this.gpu;
7454+
if (!gpu) throw new Error('settings property "gpu" or argument required.');
73677455
this.kernel = gpu.createKernel(function(x) {
73687456
return x[this.thread.z][this.thread.y][this.thread.x];
73697457
}).setOutput(this.output);
@@ -7379,6 +7467,7 @@ class Texture {
73797467
module.exports = {
73807468
Texture
73817469
};
7470+
73827471
},{}],28:[function(require,module,exports){
73837472
const {
73847473
Input

0 commit comments

Comments
 (0)