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
215 lines (193 loc) · 6.4 KB
/
gpu.js
File metadata and controls
215 lines (193 loc) · 6.4 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
const utils = require('./utils');
const WebGLRunner = require('./backend/web-gl/runner');
const CPURunner = require('./backend/cpu/runner');
const WebGLValidatorKernel = require('./backend/web-gl/validator-kernel');
///
/// Class: GPU
///
/// Initialises the GPU.js library class which manages the WebGL context for the created functions.
///
module.exports = class GPU {
constructor(settings) {
settings = settings || {};
this._canvas = settings.canvas || null;
this._webGl = settings.webGl || null;
let mode = settings.mode || 'webgl';
if (!utils.isWebGlSupported) {
console.warn('Warning: gpu not supported, falling back to cpu support');
mode = 'cpu';
}
this.kernels = [];
const runnerSettings = {
canvas: this._canvas,
webGl: this._webGl
};
if (mode) {
switch (mode.toLowerCase()) {
case 'cpu':
this._runner = new CPURunner(runnerSettings);
break;
case 'gpu':
case 'webgl':
this._runner = new WebGLRunner(runnerSettings);
break;
case 'webgl-validator':
this._runner = new WebGLRunner(runnerSettings);
this._runner.Kernel = WebGLValidatorKernel;
break;
default:
throw new Error(`"${mode}" mode is not defined`);
}
}
}
///
/// 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
/// settings {Object} The parameter configuration object (see above)
///
/// Returns:
/// callable function to run
///
createKernel(fn, settings) {
//
// basic parameters safety checks
//
if (typeof fn === 'undefined') {
throw 'Missing fn parameter';
}
if (!utils.isFunction(fn)) {
throw 'fn parameter not a function';
}
const kernel = this._runner.buildKernel(fn, settings || {});
//if canvas didn't come from this, propagate from kernel
if (!this._canvas) {
this._canvas = kernel.canvas;
}
if (!this._runner.canvas) {
this._runner.canvas = kernel.canvas;
}
this.kernels.push(kernel);
return kernel;
}
createKernels() {
let fn;
let settings;
if (typeof arguments[arguments.length - 2] === 'function') {
fn = arguments[arguments.length - 2];
settings = arguments[arguments.length - 1];
} else {
fn = arguments[arguments.length - 1];
}
const kernel = this.createKernel(fn, settings);
if (Array.isArray(arguments[0])) {
const functions = arguments[0];
for (let i = 0; i < functions.length; i++) {
kernel.addSubKernel(functions[i]);
}
} else {
const functions = arguments[0];
for (let p in functions) {
if (!functions.hasOwnProperty(p)) continue;
kernel.addSubKernelProperty(p, functions[p]);
}
}
return kernel;
}
combineKernels() {
const lastKernel = arguments[arguments.length - 2];
const combinedKernel = arguments[arguments.length - 1];
if (this.mode === 'cpu') return combinedKernel;
const canvas = arguments[0].canvas;
let webGl = arguments[0].webGl;
for (let i = 0; i < arguments.length - 1; i++) {
arguments[i]
.setCanvas(canvas)
.setWebGl(webGl)
.setOutputToTexture(true);
}
return function() {
combinedKernel.apply(null, arguments);
const texSize = lastKernel.texSize;
const gl = lastKernel.webGl;
const threadDim = lastKernel.threadDim;
let result;
if (lastKernel.floatOutput) {
result = new Float32Array(texSize[0] * texSize[1] * 4);
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.FLOAT, result);
} else {
const bytes = new Uint8Array(texSize[0] * texSize[1] * 4);
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.UNSIGNED_BYTE, bytes);
result = Float32Array.prototype.slice.call(new Float32Array(bytes.buffer));
}
result = result.subarray(0, threadDim[0] * threadDim[1] * threadDim[2]);
if (lastKernel.dimensions.length === 1) {
return result;
} else if (lastKernel.dimensions.length === 2) {
return utils.splitArray(result, lastKernel.dimensions[0]);
} else if (lastKernel.dimensions.length === 3) {
const cube = utils.splitArray(result, lastKernel.dimensions[0] * lastKernel.dimensions[1]);
return cube.map(function(x) {
return utils.splitArray(x, lastKernel.dimensions[0]);
});
}
};
}
get mode() {
return this._runner.mode;
}
///
/// Function: addFunction
///
/// Adds additional functions, that the kernel may call.
///
/// Parameters:
/// fn - {Function|String} 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
///
/// Returns:
/// {GPU} returns itself
///
addFunction(fn, paramTypes, returnType) {
this._runner.functionBuilder.addFunction(null, fn, paramTypes, returnType);
return this;
}
///
/// Function: isWebGlSupported
///
/// Return TRUE, if browser supports WebGl AND Canvas
///
/// Note: This function can also be called directly `GPU.isWebGlSupported()`
///
/// Returns:
/// {Boolean} TRUE if browser supports webGl
///
static get isWebGlSupported() {
return utils.isWebGlSupported;
}
get isWebGlSupported() {
return utils.isWebGlSupported;
}
get canvas() {
return this._canvas;
}
get webGl() {
return this._webGl;
}
};