Skip to content

Commit 18f8974

Browse files
committed
Merge commit 'e175f8a1a32f55d5ff34aacc715df7a0511642df' into eugene-gpujs#4-async-api
2 parents cf3327c + e175f8a commit 18f8974

7 files changed

Lines changed: 49 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The created function is a regular JavaScript function, you can use it like regul
7070

7171
```js
7272
myFunc();
73-
// Result: [0, 1, 2, 3, ... 100]
73+
// Result: [0, 1, 2, 3, ... 99]
7474
```
7575

7676
Note: Instead of creating an object, you can use the chainable shortcut methods as a neater way of specificying options.
@@ -81,7 +81,7 @@ var myFunc = gpu.createKernel(function() {
8181
}).dimensions([100]);
8282

8383
myFunc();
84-
// Result: [0, 1, 2, 3, ... 100]
84+
// Result: [0, 1, 2, 3, ... 99]
8585
```
8686
### Accepting Input
8787

src/backend/fallback.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167
return ret;
168168
};
169169

170+
ret.loopMaxIterations = function(max) {
171+
opt.loopMaxIterations = max;
172+
return ret;
173+
};
174+
170175
ret.wraparound = function() {
171176
opt.wraparound = false;
172177
return ret;

src/backend/functionBuilder.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var functionBuilder = (function() {
6464
///
6565
/// Returns:
6666
/// {[String,...]} Returning list of function names that is traced. Including itself.
67-
function traceFunctionCalls( functionName, retList ) {
67+
function traceFunctionCalls( functionName, retList, opt ) {
6868
functionName = functionName || "kernel";
6969
retList = retList || [];
7070

@@ -76,9 +76,9 @@ var functionBuilder = (function() {
7676
} else {
7777
retList.push(functionName);
7878

79-
fNode.getWebglFunctionString(); //ensure JS trace is done
79+
fNode.getWebglFunctionString(opt); //ensure JS trace is done
8080
for(var i=0; i<fNode.calledFunctions.length; ++i) {
81-
this.traceFunctionCalls( fNode.calledFunctions[i], retList );
81+
this.traceFunctionCalls( fNode.calledFunctions[i], retList, opt );
8282
}
8383
}
8484
}
@@ -96,12 +96,12 @@ var functionBuilder = (function() {
9696
/// Returns:
9797
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
9898
///
99-
function webglString_fromFunctionNames(functionList) {
99+
function webglString_fromFunctionNames(functionList, opt) {
100100
var ret = [];
101101
for(var i=0; i<functionList.length; ++i) {
102102
var node = this.nodeMap[functionList[i]];
103103
if(node) {
104-
ret.push( this.nodeMap[functionList[i]].getWebglFunctionString() );
104+
ret.push( this.nodeMap[functionList[i]].getWebglFunctionString(opt) );
105105
}
106106
}
107107
return ret.join("\n");
@@ -117,11 +117,15 @@ var functionBuilder = (function() {
117117
/// Returns:
118118
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
119119
///
120-
function webglString(functionName) {
120+
function webglString(functionName, opt) {
121+
if (opt == undefined) {
122+
opt = {};
123+
}
124+
121125
if(functionName) {
122-
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, []).reverse() );
126+
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, [], opt).reverse(), opt );
123127
}
124-
return this.webglString_fromFunctionNames(Object.keys(this.nodeMap));
128+
return this.webglString_fromFunctionNames( Object.keys(this.nodeMap), opt );
125129
}
126130
functionBuilder.prototype.webglString = webglString;
127131

src/backend/functionNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ var functionNode = (function() {
179179
/// Returns:
180180
/// {String} webgl function string, result is cached under this.webglFunctionString
181181
///
182-
function getWebglFunctionString() {
182+
function getWebglFunctionString(opt) {
183183
if( this.webglFunctionString ) {
184184
return this.webglFunctionString;
185185
}
186186

187-
return this.webglFunctionString = functionNode_webgl(this);
187+
return this.webglFunctionString = functionNode_webgl(this, opt);
188188
}
189189
functionNode.prototype.getWebglFunctionString = getWebglFunctionString;
190190

src/backend/functionNode_webgl.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Closure capture for the ast function, prevent collision with existing AST functions
22
var functionNode_webgl = (function() {
33

4-
var gpu, jsFunctionString;
4+
var gpu, opt, jsFunctionString;
55

66
function isIdentifierKernelParam(paramName, ast, funcParam) {
77
return funcParam.paramNames.indexOf(paramName) != -1;
@@ -31,8 +31,9 @@ var functionNode_webgl = (function() {
3131
/// Returns:
3232
/// the converted webGL function string
3333
///
34-
function functionNode_webgl( inNode ) {
34+
function functionNode_webgl( inNode, _opt ) {
3535
gpu = inNode.gpu;
36+
opt = _opt;
3637
jsFunctionString = inNode.jsFunctionString;
3738
inNode.webglFunctionString_array = ast_generic( inNode.getJS_AST(), [], inNode );
3839
inNode.webglFunctionString = webgl_regex_optimize(
@@ -344,15 +345,25 @@ var functionNode_webgl = (function() {
344345
}
345346

346347
if (forNode.test && forNode.test.type == "BinaryExpression") {
347-
if (forNode.test.right.type != "Literal") {
348-
retArr.push("{\n");
349-
retArr.push("float ");
348+
if (forNode.test.right.type == "Identifier"
349+
&& forNode.test.operator == "<") {
350+
351+
if (opt.loopMaxIterations === undefined) {
352+
console.warn("Warning: loopMaxIterations is not set! Using default of 100 which may result in unintended behavior.");
353+
console.warn("Set loopMaxIterations or use a for loop of fixed length to silence this message.");
354+
}
355+
356+
retArr.push("for (float ");
350357
ast_generic(forNode.init, retArr, funcParam);
351-
retArr.push(";\n");
352-
retArr.push("for (float i=0.0; i<LOOP_MAX; i++, ");
358+
retArr.push(";");
359+
ast_generic(forNode.test.left, retArr, funcParam);
360+
retArr.push(forNode.test.operator);
361+
retArr.push("LOOP_MAX");
362+
retArr.push(";");
353363
ast_generic(forNode.update, retArr, funcParam);
354-
retArr.push(") {\n");
364+
retArr.push(")");
355365

366+
retArr.push("{\n");
356367
retArr.push("if (");
357368
ast_generic(forNode.test.left, retArr, funcParam);
358369
retArr.push(forNode.test.operator);
@@ -363,8 +374,6 @@ var functionNode_webgl = (function() {
363374
}
364375
retArr.push("} else {\n");
365376
retArr.push("break;\n");
366-
retArr.push("}\n");
367-
368377
retArr.push("}\n");
369378
retArr.push("}\n");
370379

src/backend/glsl.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
'precision highp float;',
243243
'precision highp int;',
244244
'',
245-
'#define LOOP_MAX 10000000.0',
245+
'#define LOOP_MAX '+ (opt.loopMaxIterations ? parseInt(opt.loopMaxIterations)+'.0' : '100.0'),
246246
'',
247247
opt.hardcodeConstants ? 'vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform vec3 uOutputDim;',
248248
opt.hardcodeConstants ? 'vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform vec2 uTexSize;',
@@ -315,7 +315,7 @@
315315
'}',
316316
'',
317317
paramStr,
318-
builder.webglString("kernel"),
318+
builder.webglString("kernel", opt),
319319
'',
320320
'void main(void) {',
321321
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
@@ -511,6 +511,11 @@
511511
opt.graphical = flag;
512512
return ret;
513513
};
514+
515+
ret.loopMaxIterations = function(max) {
516+
opt.loopMaxIterations = max;
517+
return ret;
518+
};
514519

515520
ret.wraparound = function(flag) {
516521
opt.wraparound = flag;

test/src/features/for_loop.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ QUnit.test( "for_loop (CPU)", function( assert ) {
5858
var evil_while_cpuRef = new GPU();
5959
var evil_while_cpuRef_f = evil_while_cpuRef.createKernel(evil_while_kernalFunction, {
6060
dimensions : [6],
61-
mode : "cpu"
61+
mode : "cpu",
62+
loopMaxIterations: 10000
6263
});
6364

6465
var evil_while_exp = evil_while_cpuRef_f(evil_while_a,evil_while_b);

0 commit comments

Comments
 (0)