Skip to content

Commit 2cb67c4

Browse files
propose changes for fix how floating values are handled
1 parent 1b74546 commit 2cb67c4

12 files changed

Lines changed: 83 additions & 49 deletions

File tree

bin/gpu-core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* GPU Accelerated JavaScript
66
*
77
* @version 1.0.0-rc.10
8-
* @date Fri Dec 15 2017 20:55:34 GMT+0300 (MSK)
8+
* @date Mon Dec 18 2017 11:09:58 GMT-0500 (EST)
99
*
1010
* @license MIT
1111
* The MIT License

bin/gpu.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* GPU Accelerated JavaScript
66
*
77
* @version 1.0.0-rc.10
8-
* @date Fri Dec 15 2017 20:55:34 GMT+0300 (MSK)
8+
* @date Mon Dec 18 2017 11:09:59 GMT-0500 (EST)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -1522,8 +1522,8 @@ module.exports = function () {
15221522
this._canvas = null;
15231523
this._webGl = null;
15241524
this.threadDim = null;
1525-
this.floatTextures = null;
1526-
this.floatOutput = null;
1525+
this.floatTextures = true;
1526+
this.floatOutput = true;
15271527
this.floatOutputForce = null;
15281528
this.addFunction = null;
15291529
this.functions = null;
@@ -2735,11 +2735,11 @@ function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
27352735
var start = ast.loc.start;
27362736

27372737
if (!isIdentifierKernelParam(paramName, funcParam) && expectedType !== 'float') {
2738-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
2738+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
27392739
} else {
27402740
var actualType = funcParam.paramTypes[funcParam.paramNames.indexOf(paramName)];
27412741
if (actualType !== expectedType) {
2742-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
2742+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
27432743
}
27442744
}
27452745
}
@@ -2813,17 +2813,18 @@ module.exports = function (_KernelBase) {
28132813
value: function validateOptions() {
28142814
var isReadPixel = utils.isFloatReadPixelsSupported();
28152815
if (this.floatTextures === true && !utils.OES_texture_float) {
2816-
throw 'Float textures are not supported on this browser';
2816+
throw new Error('Float textures are not supported on this browser');
28172817
} else if (this.floatOutput === true && this.floatOutputForce !== true && !isReadPixel) {
2818-
throw 'Float texture outputs are not supported on this browser';
2818+
console.warn('Float texture outputs are not supported on this browser');
2819+
this.floatOutput = false;
28192820
} else if (this.floatTextures === null && !isReadPixel && !this.graphical) {
28202821
this.floatTextures = true;
28212822
this.floatOutput = false;
28222823
}
28232824

28242825
if (!this.output || this.output.length === 0) {
28252826
if (arguments.length !== 1) {
2826-
throw 'Auto output only supported for kernels with only one input';
2827+
throw new Error('Auto output only supported for kernels with only one input');
28272828
}
28282829

28292830
var argType = utils.getArgumentType(arguments[0]);
@@ -2832,7 +2833,7 @@ module.exports = function (_KernelBase) {
28322833
} else if (argType === 'Texture') {
28332834
this.output = arguments[0].output;
28342835
} else {
2835-
throw 'Auto output not supported for input type: ' + argType;
2836+
throw new Error('Auto output not supported for input type: ' + argType);
28362837
}
28372838
}
28382839

@@ -2843,11 +2844,12 @@ module.exports = function (_KernelBase) {
28432844

28442845
if (this.graphical) {
28452846
if (this.output.length !== 2) {
2846-
throw 'Output must have 2 dimensions on graphical mode';
2847+
throw new Error('Output must have 2 dimensions on graphical mode');
28472848
}
28482849

28492850
if (this.floatOutput) {
2850-
throw 'Cannot use graphical mode and float output at the same time';
2851+
this.floatOutput = false;
2852+
console.warn('Cannot use graphical mode and float output at the same time');
28512853
}
28522854

28532855
this.texSize = utils.clone(this.output);
@@ -2911,12 +2913,12 @@ module.exports = function (_KernelBase) {
29112913
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
29122914
console.log(compiledVertShaderString);
29132915
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(vertShader));
2914-
throw 'Error compiling vertex shader';
2916+
throw new Error('Error compiling vertex shader');
29152917
}
29162918
if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
29172919
console.log(compiledFragShaderString);
29182920
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(fragShader));
2919-
throw 'Error compiling fragment shader';
2921+
throw new Error('Error compiling fragment shader');
29202922
}
29212923

29222924
if (this.debug) {
@@ -3318,7 +3320,7 @@ module.exports = function (_KernelBase) {
33183320
break;
33193321
}
33203322
default:
3321-
throw 'Input type not supported (WebGL): ' + value;
3323+
throw new Error('Input type not supported (WebGL): ' + value);
33223324
}
33233325
this.argumentsLength++;
33243326
}
@@ -4254,6 +4256,15 @@ var _systemEndianness = function () {
42544256

42554257
var _isFloatReadPixelsSupported = null;
42564258

4259+
var _isMixedIdentifiersSupported = function () {
4260+
try {
4261+
new Function('let i = 1; const j = 1;')();
4262+
return true;
4263+
} catch (e) {
4264+
return false;
4265+
}
4266+
}();
4267+
42574268
var Utils = function (_UtilsCore) {
42584269
_inherits(Utils, _UtilsCore);
42594270

@@ -4409,6 +4420,11 @@ var Utils = function (_UtilsCore) {
44094420

44104421
return _isFloatReadPixelsSupported;
44114422
}
4423+
}, {
4424+
key: 'isMixedIdentifiersSupported',
4425+
value: function isMixedIdentifiersSupported() {
4426+
return _isMixedIdentifiersSupported;
4427+
}
44124428
}, {
44134429
key: 'dimToTexSize',
44144430
value: function dimToTexSize(opt, dimensions, output) {

dist/backend/kernel-base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ module.exports = function () {
4646
this._canvas = null;
4747
this._webGl = null;
4848
this.threadDim = null;
49-
this.floatTextures = null;
50-
this.floatOutput = null;
49+
this.floatTextures = true;
50+
this.floatOutput = true;
5151
this.floatOutputForce = null;
5252
this.addFunction = null;
5353
this.functions = null;

dist/backend/web-gl/function-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,11 @@ function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
11951195
var start = ast.loc.start;
11961196

11971197
if (!isIdentifierKernelParam(paramName, funcParam) && expectedType !== 'float') {
1198-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
1198+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
11991199
} else {
12001200
var actualType = funcParam.paramTypes[funcParam.paramNames.indexOf(paramName)];
12011201
if (actualType !== expectedType) {
1202-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
1202+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
12031203
}
12041204
}
12051205
}

dist/backend/web-gl/kernel.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ module.exports = function (_KernelBase) {
8787
value: function validateOptions() {
8888
var isReadPixel = utils.isFloatReadPixelsSupported();
8989
if (this.floatTextures === true && !utils.OES_texture_float) {
90-
throw 'Float textures are not supported on this browser';
90+
throw new Error('Float textures are not supported on this browser');
9191
} else if (this.floatOutput === true && this.floatOutputForce !== true && !isReadPixel) {
92-
throw 'Float texture outputs are not supported on this browser';
92+
console.warn('Float texture outputs are not supported on this browser');
93+
this.floatOutput = false;
9394
} else if (this.floatTextures === null && !isReadPixel && !this.graphical) {
9495
//NOTE: handle
9596
this.floatTextures = true;
@@ -98,7 +99,7 @@ module.exports = function (_KernelBase) {
9899

99100
if (!this.output || this.output.length === 0) {
100101
if (arguments.length !== 1) {
101-
throw 'Auto output only supported for kernels with only one input';
102+
throw new Error('Auto output only supported for kernels with only one input');
102103
}
103104

104105
var argType = utils.getArgumentType(arguments[0]);
@@ -107,7 +108,7 @@ module.exports = function (_KernelBase) {
107108
} else if (argType === 'Texture') {
108109
this.output = arguments[0].output;
109110
} else {
110-
throw 'Auto output not supported for input type: ' + argType;
111+
throw new Error('Auto output not supported for input type: ' + argType);
111112
}
112113
}
113114

@@ -118,11 +119,12 @@ module.exports = function (_KernelBase) {
118119

119120
if (this.graphical) {
120121
if (this.output.length !== 2) {
121-
throw 'Output must have 2 dimensions on graphical mode';
122+
throw new Error('Output must have 2 dimensions on graphical mode');
122123
}
123124

124125
if (this.floatOutput) {
125-
throw 'Cannot use graphical mode and float output at the same time';
126+
this.floatOutput = false;
127+
console.warn('Cannot use graphical mode and float output at the same time');
126128
}
127129

128130
this.texSize = utils.clone(this.output);
@@ -195,12 +197,12 @@ module.exports = function (_KernelBase) {
195197
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
196198
console.log(compiledVertShaderString);
197199
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(vertShader));
198-
throw 'Error compiling vertex shader';
200+
throw new Error('Error compiling vertex shader');
199201
}
200202
if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
201203
console.log(compiledFragShaderString);
202204
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(fragShader));
203-
throw 'Error compiling fragment shader';
205+
throw new Error('Error compiling fragment shader');
204206
}
205207

206208
if (this.debug) {
@@ -740,7 +742,7 @@ module.exports = function (_KernelBase) {
740742
break;
741743
}
742744
default:
743-
throw 'Input type not supported (WebGL): ' + value;
745+
throw new Error('Input type not supported (WebGL): ' + value);
744746
}
745747
this.argumentsLength++;
746748
}

dist/core/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ var _systemEndianness = function () {
4646

4747
var _isFloatReadPixelsSupported = null;
4848

49+
var _isMixedIdentifiersSupported = function () {
50+
try {
51+
new Function('let i = 1; const j = 1;')();
52+
return true;
53+
} catch (e) {
54+
return false;
55+
}
56+
}();
57+
4958
var Utils = function (_UtilsCore) {
5059
_inherits(Utils, _UtilsCore);
5160

@@ -364,6 +373,11 @@ var Utils = function (_UtilsCore) {
364373

365374
return _isFloatReadPixelsSupported;
366375
}
376+
}, {
377+
key: 'isMixedIdentifiersSupported',
378+
value: function isMixedIdentifiersSupported() {
379+
return _isMixedIdentifiersSupported;
380+
}
367381
}, {
368382
key: 'dimToTexSize',
369383
value: function dimToTexSize(opt, dimensions, output) {

src/backend/kernel-base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module.exports = class BaseKernel {
4040
this._canvas = null;
4141
this._webGl = null;
4242
this.threadDim = null;
43-
this.floatTextures = null;
44-
this.floatOutput = null;
43+
this.floatTextures = true;
44+
this.floatOutput = true;
4545
this.floatOutputForce = null;
4646
this.addFunction = null;
4747
this.functions = null;

src/backend/web-gl/function-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,11 +1114,11 @@ function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
11141114
const start = ast.loc.start;
11151115

11161116
if (!isIdentifierKernelParam(paramName, funcParam) && expectedType !== 'float') {
1117-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
1117+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
11181118
} else {
11191119
const actualType = funcParam.paramTypes[funcParam.paramNames.indexOf(paramName)];
11201120
if (actualType !== expectedType) {
1121-
throw 'Error unexpected identifier ' + paramName + ' on line ' + start.line;
1121+
throw new Error('Error unexpected identifier ' + paramName + ' on line ' + start.line);
11221122
}
11231123
}
11241124
}

src/backend/web-gl/kernel.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ module.exports = class WebGLKernel extends KernelBase {
6969
validateOptions() {
7070
const isReadPixel = utils.isFloatReadPixelsSupported();
7171
if (this.floatTextures === true && !utils.OES_texture_float) {
72-
throw 'Float textures are not supported on this browser';
72+
throw new Error('Float textures are not supported on this browser');
7373
} else if (this.floatOutput === true && this.floatOutputForce !== true && !isReadPixel) {
74-
throw 'Float texture outputs are not supported on this browser';
74+
console.warn('Float texture outputs are not supported on this browser');
75+
this.floatOutput = false;
7576
} else if (this.floatTextures === null && !isReadPixel && !this.graphical) {
7677
//NOTE: handle
7778
this.floatTextures = true;
@@ -80,7 +81,7 @@ module.exports = class WebGLKernel extends KernelBase {
8081

8182
if (!this.output || this.output.length === 0) {
8283
if (arguments.length !== 1) {
83-
throw 'Auto output only supported for kernels with only one input';
84+
throw new Error('Auto output only supported for kernels with only one input');
8485
}
8586

8687
const argType = utils.getArgumentType(arguments[0]);
@@ -89,7 +90,7 @@ module.exports = class WebGLKernel extends KernelBase {
8990
} else if (argType === 'Texture') {
9091
this.output = arguments[0].output;
9192
} else {
92-
throw 'Auto output not supported for input type: ' + argType;
93+
throw new Error('Auto output not supported for input type: ' + argType);
9394
}
9495
}
9596

@@ -100,11 +101,12 @@ module.exports = class WebGLKernel extends KernelBase {
100101

101102
if (this.graphical) {
102103
if (this.output.length !== 2) {
103-
throw 'Output must have 2 dimensions on graphical mode';
104+
throw new Error('Output must have 2 dimensions on graphical mode');
104105
}
105106

106107
if (this.floatOutput) {
107-
throw 'Cannot use graphical mode and float output at the same time';
108+
this.floatOutput = false;
109+
console.warn('Cannot use graphical mode and float output at the same time');
108110
}
109111

110112
this.texSize = utils.clone(this.output);
@@ -174,12 +176,12 @@ module.exports = class WebGLKernel extends KernelBase {
174176
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
175177
console.log(compiledVertShaderString);
176178
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(vertShader));
177-
throw 'Error compiling vertex shader';
179+
throw new Error('Error compiling vertex shader');
178180
}
179181
if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
180182
console.log(compiledFragShaderString);
181183
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(fragShader));
182-
throw 'Error compiling fragment shader';
184+
throw new Error('Error compiling fragment shader');
183185
}
184186

185187
if (this.debug) {
@@ -691,7 +693,7 @@ module.exports = class WebGLKernel extends KernelBase {
691693
break;
692694
}
693695
default:
694-
throw 'Input type not supported (WebGL): ' + value;
696+
throw new Error('Input type not supported (WebGL): ' + value);
695697
}
696698
this.argumentsLength++;
697699
}

test/src/features/add-custom-function.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function addCustomFunction_constantsWidth(mode) {
4242
var gpu = new GPU({ mode: mode });
4343

4444
function custom_adder(a, b) {
45-
let sum = 0;
46-
for (let i = 0; i < this.constants.width; i++) {
45+
var sum = 0;
46+
for (var i = 0; i < this.constants.width; i++) {
4747
sum += (a[this.thread.x] + b[this.thread.x]);
4848
}
4949
return sum;
@@ -86,8 +86,8 @@ function addCustomFunction_thisOutputX(mode) {
8686
var gpu = new GPU({ mode: mode });
8787

8888
function custom_adder(a, b) {
89-
let sum = 0;
90-
for (let i = 0; i < this.output.x; i++) {
89+
var sum = 0;
90+
for (var i = 0; i < this.output.x; i++) {
9191
sum += (a[this.thread.x] + b[this.thread.x]);
9292
}
9393
return sum;

0 commit comments

Comments
 (0)