forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionBuilder.js
More file actions
146 lines (133 loc) · 4.44 KB
/
functionBuilder.js
File metadata and controls
146 lines (133 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
///
/// Class: functionBuilder
///
/// [INTERNAL] A collection of functionNodes.
///
/// This handles all the raw state, converted state, etc. Of a single function.
///
/// Properties:
/// nodeMap - {Object} Object map, where nodeMap[function] = functionNode;
///
var functionBuilder = (function() {
///
/// Function: functionBuilder
///
/// [Constructor] Blank constructor, which initializes the properties
///
function functionBuilder() {
this.nodeMap = {};
}
///
/// Function: addFunction
///
/// Creates the functionNode, and add it to the nodeMap
///
/// Parameters:
/// functionName - {String} Function name to assume, if its null, it attempts to extract from the function
/// jsFunction - {JS Function} JS Function to do conversion
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
/// returnType - {String} The return type, assumes "float" if null
///
function addFunction( functionName, jsFunction, paramTypeArray, returnType ) {
this.addFunctionNode( new functionNode( functionName, jsFunction, paramTypeArray, returnType ) );
}
functionBuilder.prototype.addFunction = addFunction;
///
/// Function: addFunctionNode
///
/// Add the funciton node directly
///
/// Parameters:
/// inNode - {functionNode} functionNode to add
///
function addFunctionNode( inNode ) {
this.nodeMap[ inNode.functionName ] = inNode;
}
functionBuilder.prototype.addFunctionNode = addFunctionNode;
///
/// Function: traceFunctionCalls
///
/// Trace all the depending functions being called, from a single function
///
/// This allow for "uneeded" functions to be automatically optimized out.
/// Note that the 0-index, is the starting function trace.
///
/// Parameters:
/// functionName - {String} Function name to trace from, default to "kernel"
/// retList - {[String,...]} Returning list of function names that is traced. Including itself.
///
/// Returns:
/// {[String,...]} Returning list of function names that is traced. Including itself.
function traceFunctionCalls( functionName, retList ) {
functionName = functionName || "kernel";
retList = retList || [];
var fNode = this.nodeMap[functionName];
if( fNode ) {
// Check if function already exists
if( retList.indexOf(functionName) >= 0 ) {
// Does nothing if already traced
} else {
retList.push(functionName);
fNode.getWebglFunctionString(); //ensure JS trace is done
for(var i=0; i<fNode.calledFunctions.length; ++i) {
this.traceFunctionCalls( fNode.calledFunctions[i], retList );
}
}
}
return retList;
}
functionBuilder.prototype.traceFunctionCalls = traceFunctionCalls;
///
/// Function: webglString_fromFunctionNames
///
/// Parameters:
/// functionList - {[String,...]} List of function to build the webgl string.
///
/// Returns:
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
///
function webglString_fromFunctionNames(functionList) {
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() );
}
}
return ret.join("\n");
}
functionBuilder.prototype.webglString_fromFunctionNames = webglString_fromFunctionNames;
///
/// Function: webglString
///
/// Parameters:
/// functionName - {String} Function name to trace from. If null, it returns the WHOLE builder stack
///
/// Returns:
/// {String} The full webgl string, of all the various functions. Trace optimized if functionName given
///
function webglString(functionName) {
if(functionName) {
return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, []).reverse() );
}
return this.webglString_fromFunctionNames(Object.keys(this.nodeMap));
}
functionBuilder.prototype.webglString = webglString;
//---------------------------------------------------------
//
// Polyfill stuff
//
//---------------------------------------------------------
// Round function used in polyfill
function round(a) { return Math.floor( a + 0.5 ); }
///
/// Function: polyfillStandardFunctions
///
/// Polyfill in the missing Math funcitons (round)
///
function polyfillStandardFunctions() {
this.addFunction(null, round);
}
functionBuilder.prototype.polyfillStandardFunctions = polyfillStandardFunctions;
return functionBuilder;
})();