forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-builder-base.js
More file actions
117 lines (104 loc) · 3.1 KB
/
function-builder-base.js
File metadata and controls
117 lines (104 loc) · 3.1 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
///
/// Class: BaseFunctionBuilder
///
/// [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] = new FunctionNode;
///
module.exports = class FunctionBuilderBase {
///
/// Function: functionBuilder
///
/// [Constructor] Blank constructor, which initializes the properties
///
constructor(gpu) {
this.nodeMap = {};
this.gpu = gpu;
this.rootKernel = null;
}
///
/// Function: addFunction
///
/// Instantiates a FunctionNode, and add it to the nodeMap
///
/// Parameters:
/// gpu - {GPU} The GPU instance
/// functionName - {String} Function name to assume, if its null, it attempts to extract from the function
/// jsFunction - {JS Function} JS Function to do conversion
/// paramTypes - {[String,...]|{variableName: Type,...}} Parameter type array, assumes all parameters are 'float' if null
/// returnType - {String} The return type, assumes 'float' if null
///
addFunction(functionName, jsFunction, paramTypes, returnType) {
throw new Error('addFunction not supported on base');
}
///
/// Function: addFunctionNode
///
/// Add the funciton node directly
///
/// Parameters:
/// inNode - {functionNode} functionNode to add
///
addFunctionNode(inNode) {
this.nodeMap[inNode.functionName] = inNode;
if (inNode.isRootKernel) {
this.rootKernel = inNode;
}
}
///
/// Function: traceFunctionCalls
///
/// Trace all the depending functions being called, from a single function
///
/// This allow for 'unneeded' 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.
traceFunctionCalls(functionName, retList, parent) {
functionName = functionName || 'kernel';
retList = retList || [];
const 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.parent = parent;
fNode.getFunctionString(); //ensure JS trace is done
for (let i = 0; i < fNode.calledFunctions.length; ++i) {
this.traceFunctionCalls(fNode.calledFunctions[i], retList, fNode);
}
}
}
return retList;
}
//---------------------------------------------------------
//
// Polyfill stuff
//
//---------------------------------------------------------
// Round function used in polyfill
static round(a) {
return round(a);
}
///
/// Function: polyfillStandardFunctions
///
/// Polyfill in the missing Math functions (round)
///
polyfillStandardFunctions() {
this.addFunction('round', round);
}
};
function round(a) {
return Math.floor(a + 0.5);
}