Skip to content

Commit 21377d9

Browse files
committed
Now building the shader code, via the functionBuilder !
1 parent 29404d4 commit 21377d9

10 files changed

Lines changed: 150 additions & 542 deletions

File tree

src/backend/functionBuilder.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var functionBuilder = (function() {
5454
/// Trace all the depending functions being called, from a single function
5555
///
5656
/// This allow for "uneeded" functions to be automatically optimized out.
57+
/// Note that the 0-index, is the starting function trace.
5758
///
5859
/// Parameters:
5960
/// functionName - {String} Function name to trace from, default to "kernel"
@@ -84,5 +85,40 @@ var functionBuilder = (function() {
8485
}
8586
functionBuilder.prototype.traceFunctionCalls = traceFunctionCalls;
8687

88+
///
89+
/// Function: webglString_fromFunctionNames
90+
///
91+
/// Parameters:
92+
/// functionList - {[String,...]} List of function to build the webgl string.
93+
///
94+
/// Returns:
95+
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
96+
///
97+
function webglString_fromFunctionNames(functionList) {
98+
var ret = [];
99+
for(var i=0; i<functionList.length; ++i) {
100+
ret.push( this.nodeMap[functionList[i]].getWebglFunctionString() );
101+
}
102+
return ret.join("\n");
103+
}
104+
functionBuilder.prototype.webglString_fromFunctionNames = webglString_fromFunctionNames;
105+
106+
///
107+
/// Function: webglString
108+
///
109+
/// Parameters:
110+
/// functionName - {String} Function name to trace from. If null, it returns the WHOLE builder stack
111+
///
112+
/// Returns:
113+
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
114+
///
115+
function webglString(functionName) {
116+
if(functionName) {
117+
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, []).reverse() );
118+
}
119+
return this.webglString_fromFunctionNames(Object.keys(this.nodeMap));
120+
}
121+
functionBuilder.prototype.webglString = webglString;
122+
87123
return functionBuilder;
88124
})();

src/backend/functionNode_webgl.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ var functionNode_webgl = (function() {
109109
function ast_FunctionExpression(ast, retArr, funcParam) {
110110

111111
// Setup function return type and name
112-
retArr.push(funcParam.returnType);
112+
if(funcParam.isRootKernal) {
113+
retArr.push("vec4");
114+
} else {
115+
retArr.push(funcParam.returnType);
116+
}
113117
retArr.push(" ");
114118
retArr.push(funcParam.functionName);
115119
retArr.push("(");
@@ -243,6 +247,8 @@ var functionNode_webgl = (function() {
243247
retArr.push('uOutputDim.y');
244248
} else if (idtNode.name == "gpu_dimensionsZ") {
245249
retArr.push('uOutputDim.z');
250+
} else if(funcParam.isRootKernal) {
251+
retArr.push("user_"+idtNode.name);
246252
} else {
247253
retArr.push(idtNode.name);
248254
}
@@ -415,9 +421,26 @@ var functionNode_webgl = (function() {
415421
ast_generic(mNode.property, retArr, funcParam);
416422
retArr.push(")");
417423
} else {
418-
ast_generic(mNode.object, retArr, funcParam);
419-
retArr.push(".");
420-
ast_generic(mNode.property, retArr, funcParam);
424+
425+
// Unroll the member expression
426+
var unrolled = ast_MemberExpression_unroll(mNode);
427+
var unrolled_lc = unrolled.toLowerCase()
428+
429+
if (unrolled_lc == "this.thread.x") {
430+
retArr.push('threadId.x');
431+
} else if (unrolled_lc == "this.thread.y") {
432+
retArr.push('threadId.y');
433+
} else if (unrolled_lc == "this.thread.z") {
434+
retArr.push('threadId.z');
435+
} else if (unrolled_lc == "this.dimensions.x") {
436+
retArr.push('uOutputDim.x');
437+
} else if (unrolled_lc == "this.dimensions.y") {
438+
retArr.push('uOutputDim.y');
439+
} else if (unrolled_lc == "this.dimensions.z") {
440+
retArr.push('uOutputDim.z');
441+
} else {
442+
retArr.push(unrolled);
443+
}
421444
}
422445
return retArr;
423446
}
@@ -439,17 +462,19 @@ var functionNode_webgl = (function() {
439462
/// @param ast the AST object to parse
440463
///
441464
/// @returns {String} the function namespace call, unrolled
442-
function ast_CallExpression_unroll(ast, funcParam) {
465+
function ast_MemberExpression_unroll(ast, funcParam) {
443466
if( ast.type == "Identifier" ) {
444467
return ast.name;
445-
}
468+
} else if( ast.type == "ThisExpression" ) {
469+
return "this";
470+
}
446471

447472
if( ast.type == "MemberExpression" ) {
448473
if( ast.object && ast.property ) {
449474
return (
450-
ast_CallExpression_unroll( ast.object, funcParam ) +
475+
ast_MemberExpression_unroll( ast.object, funcParam ) +
451476
"." +
452-
ast_CallExpression_unroll( ast.property, funcParam )
477+
ast_MemberExpression_unroll( ast.property, funcParam )
453478
);
454479
}
455480
}
@@ -474,7 +499,7 @@ var functionNode_webgl = (function() {
474499
function ast_CallExpression(ast, retArr, funcParam) {
475500
if( ast.callee ) {
476501
// Get the full function call, unrolled
477-
var funcName = ast_CallExpression_unroll(ast.callee);
502+
var funcName = ast_MemberExpression_unroll(ast.callee);
478503

479504
// Its a math operator, remove the prefix
480505
if( funcName.indexOf(jsMathPrefix) === 0 ) {

0 commit comments

Comments
 (0)