forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.js
More file actions
227 lines (212 loc) · 6.46 KB
/
gpu.js
File metadata and controls
227 lines (212 loc) · 6.46 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
226
227
///
/// Class: GPU
///
/// Initialises the GPU.js library class which manages the WebGL context for the created functions.
///
var GPU = (function() {
var GPU = GPUCore;
///
/// Function: createKernel
///
/// This creates a callable function object to call the kernel function with the argument parameter set
///
/// The parameter object contains the following sub parameters
///
/// |---------------|---------------|---------------------------------------------------------------------------|
/// | Name | Default value | Description |
/// |---------------|---------------|---------------------------------------------------------------------------|
/// | dimensions | [1024] | Thread dimension array |
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
/// | | | + "cpu" : Forces JS fallback mode only |
/// |---------------|---------------|---------------------------------------------------------------------------|
///
/// Parameters:
/// inputFunction {JS Function} The calling to perform the conversion
/// paramObj {Object} The parameter configuration object (see above)
///
/// Returns:
/// callable function to run
///
function createKernel(kernel, paramObj) {
//
// basic parameters safety checks
//
if( kernel === undefined ) {
throw "Missing kernel parameter";
}
if( !GPUUtils.isFunction(kernel) ) {
throw "kernel parameter not a function";
}
if( paramObj === undefined ) {
paramObj = {};
}
//
// Replace the kernel function and param object
//
this._kernelFunction = kernel;
this._kernelParamObj = paramObj || this._kernelParamObj || {};
//
// Get the config, fallbacks to default value if not set
//
var mode = paramObj.mode && paramObj.mode.toLowerCase();
this.computeMode = mode || "auto";
//
// Get the Synchronous executor
//
var ret = this.getSynchronousModeExecutor();
// Allow class refence from function
ret.gpujs = this;
// Execute callback
ret.exec = ret.execute = GPUUtils.functionBinder( this.execute, this );
// The Synchronous kernel
this._kernelSynchronousExecutor = ret; //For exec to reference
return ret;
};
GPU.prototype.createKernel = createKernel;
///
/// Function: getKernelFunction
///
/// Get and returns the kernel function previously set by `createKernel`
///
/// Returns:
/// {JS Function} The calling input function
///
function getKernelFunction() {
return this._kernelFunction;
}
GPU.prototype.getKernelFunction = getKernelFunction;
///
/// Function: getKernelParamObj
///
/// Get and returns the kernel parameter object previously set by `createKernel`
///
/// Returns:
/// {JS Function} The calling input function
///
function getKernelParamObj() {
return this._kernelParamObj;
}
GPU.prototype.getKernelParamObj = getKernelParamObj;
///
/// Function: executeKernel
///
/// Executes the kernel previously set by setKernel
///
/// Parameters:
/// ..... {Arguments} Various argument arrays used by the kernel
///
/// Returns:
/// {Promise} returns the promise object for the result / failure
///
function execute() {
//
// Prepare the required objects
//
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
var self = this;
//
// Setup and return the promise, and execute the function, in synchronous mode
//
return GPUUtils.newPromise(function(accept,reject) {
try {
accept( self._kernelSynchronousExecutor.apply(self, args) );
} catch (e) {
//
// Error : throw rejection
//
reject(e);
return;
}
});
}
GPU.prototype.execute = execute;
GPU.prototype.exec = execute;
///
/// Function: addFunction
///
/// Adds additional functions, that the kernel may call.
///
/// Parameters:
/// 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
///
/// Retuns:
/// {GPU} returns itself
///
function addFunction( jsFunction, paramTypeArray, returnType ) {
this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType );
return this;
}
GPU.prototype.addFunction = addFunction;
///
/// Function: getWebgl
///
/// [DEPRECATED] Returns the internal gpu webgl instance only if it has been initiated
///
/// Retuns:
/// {WebGL object} that the instance use
///
function getWebgl() {
// if( this.webgl == null ) {
// this.webgl = GPUUtils.init_webgl( getCanvas("gpu") );
// }
if( this.webgl ) {
return this.webgl;
}
throw "only call getWebgl after createKernel(gpu)"
};
GPU.prototype.getWebgl = getWebgl;
///
/// Function: getCanvas
///
/// [DEPRECATED] Returns the internal canvas instance only if it has been initiated
///
/// Retuns:
/// {Canvas object} that the instance use
///
function getCanvas(mode) {
// if(mode == "gpu") {
// if(this._canvas_gpu == null) {
// this._canvas_gpu = GPUUtils.init_canvas();
// }
// return this._canvas_gpu;
// } else if(mode == "cpu") {
// if(this._canvas_cpu == null) {
// this._canvas_cpu = GPUUtils.init_canvas();
// }
// return this._canvas_cpu;
// } else {
// if( this._canvas_gpu || this._canvas_cpu ) {
// return (this._canvas_gpu || this._canvas_cpu );
// }
// // if( this._canvas_gpu || this._canvas_cpu ) {
// //
// // }
// throw "Missing valid mode parameter in getCanvas("+mode+")"
// }
if( this.canvas ) {
return this.canvas;
}
throw "only call getCanvas after createKernel()"
};
GPU.prototype.getCanvas = getCanvas;
///
/// Function: supportWebgl
///
/// Return TRUE, if browser supports webgl AND canvas
///
/// Note: This function can also be called directly `GPU.supportWebgl()`
///
/// Returns:
/// {Boolean} TRUE if browser supports webgl
///
function supportWebgl() {
return GPUUtils.browserSupport_webgl();
}
GPU.prototype.supportWebgl = supportWebgl;
GPU.supportWebgl = supportWebgl;
return GPU;
})();