Skip to content

Commit

Permalink
Merge commit 'e175f8a1a32f55d5ff34aacc715df7a0511642df' into eugene-#4-…
Browse files Browse the repository at this point in the history
…async-api
  • Loading branch information
PicoCreator committed Feb 28, 2016
2 parents cf3327c + e175f8a commit 18f8974
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The created function is a regular JavaScript function, you can use it like regul

```js
myFunc();
// Result: [0, 1, 2, 3, ... 100]
// Result: [0, 1, 2, 3, ... 99]
```

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

myFunc();
// Result: [0, 1, 2, 3, ... 100]
// Result: [0, 1, 2, 3, ... 99]
```
### Accepting Input

Expand Down
5 changes: 5 additions & 0 deletions src/backend/fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@
return ret;
};

ret.loopMaxIterations = function(max) {
opt.loopMaxIterations = max;
return ret;
};

ret.wraparound = function() {
opt.wraparound = false;
return ret;
Expand Down
20 changes: 12 additions & 8 deletions src/backend/functionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var functionBuilder = (function() {
///
/// Returns:
/// {[String,...]} Returning list of function names that is traced. Including itself.
function traceFunctionCalls( functionName, retList ) {
function traceFunctionCalls( functionName, retList, opt ) {
functionName = functionName || "kernel";
retList = retList || [];

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

fNode.getWebglFunctionString(); //ensure JS trace is done
fNode.getWebglFunctionString(opt); //ensure JS trace is done
for(var i=0; i<fNode.calledFunctions.length; ++i) {
this.traceFunctionCalls( fNode.calledFunctions[i], retList );
this.traceFunctionCalls( fNode.calledFunctions[i], retList, opt );
}
}
}
Expand All @@ -96,12 +96,12 @@ var functionBuilder = (function() {
/// Returns:
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
///
function webglString_fromFunctionNames(functionList) {
function webglString_fromFunctionNames(functionList, opt) {
var ret = [];
for(var i=0; i<functionList.length; ++i) {
var node = this.nodeMap[functionList[i]];
if(node) {
ret.push( this.nodeMap[functionList[i]].getWebglFunctionString() );
ret.push( this.nodeMap[functionList[i]].getWebglFunctionString(opt) );
}
}
return ret.join("\n");
Expand All @@ -117,11 +117,15 @@ var functionBuilder = (function() {
/// Returns:
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
///
function webglString(functionName) {
function webglString(functionName, opt) {
if (opt == undefined) {
opt = {};
}

if(functionName) {
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, []).reverse() );
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, [], opt).reverse(), opt );
}
return this.webglString_fromFunctionNames(Object.keys(this.nodeMap));
return this.webglString_fromFunctionNames( Object.keys(this.nodeMap), opt );
}
functionBuilder.prototype.webglString = webglString;

Expand Down
4 changes: 2 additions & 2 deletions src/backend/functionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ var functionNode = (function() {
/// Returns:
/// {String} webgl function string, result is cached under this.webglFunctionString
///
function getWebglFunctionString() {
function getWebglFunctionString(opt) {
if( this.webglFunctionString ) {
return this.webglFunctionString;
}

return this.webglFunctionString = functionNode_webgl(this);
return this.webglFunctionString = functionNode_webgl(this, opt);
}
functionNode.prototype.getWebglFunctionString = getWebglFunctionString;

Expand Down
29 changes: 19 additions & 10 deletions src/backend/functionNode_webgl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Closure capture for the ast function, prevent collision with existing AST functions
var functionNode_webgl = (function() {

var gpu, jsFunctionString;
var gpu, opt, jsFunctionString;

function isIdentifierKernelParam(paramName, ast, funcParam) {
return funcParam.paramNames.indexOf(paramName) != -1;
Expand Down Expand Up @@ -31,8 +31,9 @@ var functionNode_webgl = (function() {
/// Returns:
/// the converted webGL function string
///
function functionNode_webgl( inNode ) {
function functionNode_webgl( inNode, _opt ) {
gpu = inNode.gpu;
opt = _opt;
jsFunctionString = inNode.jsFunctionString;
inNode.webglFunctionString_array = ast_generic( inNode.getJS_AST(), [], inNode );
inNode.webglFunctionString = webgl_regex_optimize(
Expand Down Expand Up @@ -344,15 +345,25 @@ var functionNode_webgl = (function() {
}

if (forNode.test && forNode.test.type == "BinaryExpression") {
if (forNode.test.right.type != "Literal") {
retArr.push("{\n");
retArr.push("float ");
if (forNode.test.right.type == "Identifier"
&& forNode.test.operator == "<") {

if (opt.loopMaxIterations === undefined) {
console.warn("Warning: loopMaxIterations is not set! Using default of 100 which may result in unintended behavior.");
console.warn("Set loopMaxIterations or use a for loop of fixed length to silence this message.");
}

retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";\n");
retArr.push("for (float i=0.0; i<LOOP_MAX; i++, ");
retArr.push(";");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
retArr.push("LOOP_MAX");
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(") {\n");
retArr.push(")");

retArr.push("{\n");
retArr.push("if (");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
Expand All @@ -363,8 +374,6 @@ var functionNode_webgl = (function() {
}
retArr.push("} else {\n");
retArr.push("break;\n");
retArr.push("}\n");

retArr.push("}\n");
retArr.push("}\n");

Expand Down
9 changes: 7 additions & 2 deletions src/backend/glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
'precision highp float;',
'precision highp int;',
'',
'#define LOOP_MAX 10000000.0',
'#define LOOP_MAX '+ (opt.loopMaxIterations ? parseInt(opt.loopMaxIterations)+'.0' : '100.0'),
'',
opt.hardcodeConstants ? 'vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform vec3 uOutputDim;',
opt.hardcodeConstants ? 'vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform vec2 uTexSize;',
Expand Down Expand Up @@ -315,7 +315,7 @@
'}',
'',
paramStr,
builder.webglString("kernel"),
builder.webglString("kernel", opt),
'',
'void main(void) {',
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
Expand Down Expand Up @@ -511,6 +511,11 @@
opt.graphical = flag;
return ret;
};

ret.loopMaxIterations = function(max) {
opt.loopMaxIterations = max;
return ret;
};

ret.wraparound = function(flag) {
opt.wraparound = flag;
Expand Down
3 changes: 2 additions & 1 deletion test/src/features/for_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ QUnit.test( "for_loop (CPU)", function( assert ) {
var evil_while_cpuRef = new GPU();
var evil_while_cpuRef_f = evil_while_cpuRef.createKernel(evil_while_kernalFunction, {
dimensions : [6],
mode : "cpu"
mode : "cpu",
loopMaxIterations: 10000
});

var evil_while_exp = evil_while_cpuRef_f(evil_while_a,evil_while_b);
Expand Down

0 comments on commit 18f8974

Please sign in to comment.