forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionNode.js
More file actions
225 lines (200 loc) · 6.84 KB
/
functionNode.js
File metadata and controls
225 lines (200 loc) · 6.84 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
///
/// Class: functionNode
///
/// [INTERNAL] Represents a single function, inside JS, webGL, or openGL.
///
/// This handles all the raw state, converted state, etc. Of a single function.
///
/// Properties:
/// functionName - {String} Name of the function
/// jsFunction - {JS Function} The JS Function the node represents
/// jsFunctionString - {String} jsFunction.toString()
/// paramNames - {[String,...]} Parameter names of the function
/// paramType - {[String,...]} Shader land parameter type assumption
/// isRootKernel - {Boolean} Special indicator, for kernel function
/// webglFunctionString - {String} webgl converted function string
/// openglFunctionString - {String} opengl converted function string
/// calledFunctions - {[String,...]} List of all the functions called
/// initVariables - {[String,...]} List of variables initialized in the function
/// readVariables - {[String,...]} List of variables read operations occur
/// writeVariables - {[String,...]} List of variables write operations occur
///
var functionNode = (function() {
//
// Constructor
//----------------------------------------------------------------------------------------------------
///
/// Function: functionNode
///
/// [Constructor] Builds the function with the given JS function, and argument type array.
///
/// 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 / String} 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 functionNode( gpu, functionName, jsFunction, paramTypeArray, returnType ) {
this.gpu = gpu;
//
// Internal vars setup
//
this.calledFunctions = [];
this.initVariables = [];
this.readVariables = [];
this.writeVariables = [];
//
// Missing jsFunction object exception
//
if( jsFunction == null ) {
throw "jsFunction, parameter is null";
}
//
// Setup jsFunction and its string property + validate them
//
this.jsFunctionString = jsFunction.toString();
if( !GPUUtils.isFunctionString(this.jsFunctionString) ) {
console.error("jsFunction, to string conversion check falied: not a function?", this.jsFunctionString);
throw "jsFunction, to string conversion check falied: not a function?";
}
if( !GPUUtils.isFunction(jsFunction) ) {
//throw "jsFunction, is not a valid JS Function";
this.jsFunction = null;
} else {
this.jsFunction = jsFunction;
}
//
// Setup the function name property
//
this.functionName = functionName ||
(jsFunction && jsFunction.name) ||
GPUUtils.getFunctionName_fromString(this.jsFunctionString);
if( !(this.functionName) ) {
throw "jsFunction, missing name argument or value";
}
//
// Extract parameter name, and its argument types
//
this.paramNames = GPUUtils.getParamNames_fromString(this.jsFunctionString);
if( paramTypeArray != null ) {
if( paramTypeArray.length != this.paramNames.length ) {
throw "Invalid argument type array length, against function length -> ("+
paramTypeArray.length+","+
this.paramNames.length+
")";
}
this.paramType = paramTypeArray;
} else {
this.paramType = [];
for(var a=0; a<this.paramNames.length; ++a) {
this.paramType.push("float");
}
}
//
// Return type handling
//
this.returnType = returnType || "float";
}
//
// Core function
//----------------------------------------------------------------------------------------------------
///
/// Function: getJSFunction
///
/// Gets and return the stored JS Function.
/// Note: that this internally eval the function, if only the string was provided on construction
///
/// Returns:
/// {JS Function} The function object
///
function getJSFunction() {
if( this.jsFunction ) {
return this.jsFunction;
}
if( this.jsFunctionString ) {
this.jsFunction = eval( this.jsFunctionString );
return this.jsFunction;
}
throw "Missin jsFunction, and jsFunctionString parameter";
}
functionNode.prototype.getJSFunction = getJSFunction;
///
/// Function: getJS_AST
///
/// Parses the class function JS, and returns its Abstract Syntax Tree object.
///
/// This is used internally to convert to shader code
///
/// Parameters:
/// inParser - {JISON Parser} Parser to use, assumes in scope "parser" if null
///
/// Returns:
/// {AST Object} The function AST Object, note that result is cached under this.jsFunctionAST;
///
function getJS_AST( inParser ) {
if( this.jsFunctionAST ) {
return this.jsFunctionAST;
}
inParser = inParser || parser;
if( inParser == null ) {
throw "Missing JS to AST parser";
}
var prasedObj = parser.parse( "var "+this.functionName+" = "+this.jsFunctionString+";" );
if( prasedObj === null ) {
throw "Failed to parse JS code via JISON";
}
// take out the function object, outside the var declarations
var funcAST = prasedObj.body[0].declarations[0].init;
this.jsFunctionAST = funcAST;
return funcAST;
}
functionNode.prototype.getJS_AST = getJS_AST;
///
/// Function: getWebglFunctionString
///
/// Returns the converted webgl shader function equivalent of the JS function
///
/// Returns:
/// {String} webgl function string, result is cached under this.webglFunctionString
///
function getWebglFunctionString(opt) {
if( this.webglFunctionString ) {
return this.webglFunctionString;
}
return this.webglFunctionString = functionNode_webgl(this, opt);
}
functionNode.prototype.getWebglFunctionString = getWebglFunctionString;
///
/// Function: getWebglFunctionPrototypeString
///
/// Returns the converted webgl shader function equivalent of the JS function
///
/// Returns:
/// {String} webgl function string, result is cached under this.getWebglFunctionPrototypeString
///
function getWebglFunctionPrototypeString(opt) {
opt = opt || {};
if( this.webglFunctionPrototypeString ) {
return this.webglFunctionPrototypeString;
}
return this.webglFunctionPrototypeString = functionNode_webgl(this, {
prototypeOnly:true,
isRootKernel: opt.isRootKernel
});
}
functionNode.prototype.getWebglFunctionPrototypeString = getWebglFunctionPrototypeString;
///
/// Function: setWebglString
///
/// Set the webglFunctionString value, overwriting it
///
/// Parameters:
/// shaderCode - {String} Shader code string, representing the function
///
function setWebglFunctionString(shaderCode) {
this.webglFunctionString = shaderCode;
}
functionNode.prototype.setWebglFunctionString = setWebglFunctionString;
return functionNode;
})();