forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-builder.js
More file actions
161 lines (152 loc) · 4.88 KB
/
function-builder.js
File metadata and controls
161 lines (152 loc) · 4.88 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
'use strict';
const FunctionBuilderBase = require('../function-builder-base');
const WebGLFunctionNode = require('./function-node');
const utils = require('../../core/utils');
/**
* @class WebGLFunctionBuilder
*
* @extends FunctionBuilderBase
*
* @desc Builds webGl functions (shaders) from JavaScript function Strings
*
*/
module.exports = class WebGLFunctionBuilder extends FunctionBuilderBase {
addFunction(functionName, jsFunction, paramTypes, returnType) {
this.addFunctionNode(
new WebGLFunctionNode(functionName, jsFunction, paramTypes, returnType)
.setAddFunction(this.addFunction.bind(this))
);
}
/**
* @memberOf WebGLFunctionBuilder#
* @function
* @name getStringFromFunctionNames
*
* @desc Get the webGl string from function names
*
* @param {String[]} functionList - List of function to build the webgl string.
*
* @returns {String} The full webgl string, of all the various functions. Trace optimized if functionName given
*
*/
getStringFromFunctionNames(functionList) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const node = this.nodeMap[functionList[i]];
if (node) {
ret.push(this.nodeMap[functionList[i]].getFunctionString());
}
}
return ret.join('\n');
}
/**
* @memberOf WebGLFunctionBuilder#
* @function
* @name getPrototypeStringFromFunctionNames
*
* @desc Return webgl String of all functions converted to webgl shader form
*
* @param {String[]} functionList - List of function names to build the webgl string.
* @param {Object} opt - Settings object passed to functionNode. See functionNode for more details.
*
* @returns {String} Prototype String of all functions converted to webgl shader form
*
*/
getPrototypeStringFromFunctionNames(functionList, opt) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const node = this.nodeMap[functionList[i]];
if (node) {
ret.push(node.getFunctionPrototypeString(opt));
}
}
return ret.join('\n');
}
/**
* @memberOf WebGLFunctionBuilder#
* @function
* @name getString
*
* Get webGl string for a particular function name
*
* @param {String} functionName - 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
*
*/
getString(functionName, opt) {
if (opt === undefined) {
opt = {};
}
if (functionName) {
return this.getStringFromFunctionNames(this.traceFunctionCalls(functionName, [], opt).reverse(), opt);
}
return this.getStringFromFunctionNames(Object.keys(this.nodeMap), opt);
}
/**
* @memberOf WebGLFunctionBuilder#
* @name getPrototypeString
* @function
*
* @desc Return the webgl string for a function converted to glsl (webgl shaders)
*
* @param {String} functionName - 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
*
*/
getPrototypeString(functionName) {
this.rootKernel.generate();
if (functionName) {
return this.getPrototypeStringFromFunctionNames(this.traceFunctionCalls(functionName, []).reverse());
}
return this.getPrototypeStringFromFunctionNames(Object.keys(this.nodeMap));
}
/**
* @memberOf WebGLFunctionBuilder#
* @function
* @name addKernel
*
* @desc Add a new kernel to this instance
*
* @param {String} fnString - Kernel function as a String
* @param {Object} options - Settings object to set constants, debug mode, etc.
* @param {Array} paramNames - Parameters of the kernel
* @param {Array} paramTypes - Types of the parameters
*
*
* @returns {Object} The inserted kernel as a Kernel Node
*
*/
addKernel(fnString, options, paramNames, paramTypes) {
const kernelNode = new WebGLFunctionNode('kernel', fnString, options, paramTypes);
kernelNode.setAddFunction(this.addFunction.bind(this));
kernelNode.paramNames = paramNames;
kernelNode.paramTypes = paramTypes;
kernelNode.isRootKernel = true;
this.addFunctionNode(kernelNode);
return kernelNode;
}
/**
* @memberOf WebGLFunctionBuilder#
* @function
* @name addSubKernel
*
* @desc Add a new sub-kernel to the current kernel instance
*
* @param {Function} jsFunction - Sub-kernel function (JavaScript)
* @param {Object} options - Settings object to set constants, debug mode, etc.
* @param {Array} paramNames - Parameters of the sub-kernel
* @param {Array} returnType - Return type of the subKernel
*
* @returns {Object} The inserted sub-kernel as a Kernel Node
*
*/
addSubKernel(jsFunction, options, paramTypes, returnType) {
const kernelNode = new WebGLFunctionNode(null, jsFunction, options, paramTypes, returnType);
kernelNode.setAddFunction(this.addFunction.bind(this));
kernelNode.isSubKernel = true;
this.addFunctionNode(kernelNode);
return kernelNode;
}
};