forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-string.js
More file actions
70 lines (66 loc) · 2.11 KB
/
kernel-string.js
File metadata and controls
70 lines (66 loc) · 2.11 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
const {
utils
} = require('../../utils');
const {
kernelRunShortcut
} = require('../../kernel-run-shortcut');
function removeFnNoise(fn) {
if (/^function /.test(fn)) {
fn = fn.substring(9);
}
return fn.replace(/[_]typeof/g, 'typeof');
}
function removeNoise(str) {
return str
.replace(/^[A-Za-z]+/, 'function')
.replace(/[_]typeof/g, 'typeof');
}
function cpuKernelString(cpuKernel, name) {
return `() => {
${ kernelRunShortcut.toString() };
const utils = {
allPropertiesOf: ${ removeNoise(utils.allPropertiesOf.toString()) },
clone: ${ removeNoise(utils.clone.toString()) },
};
let Input = function() {};
class ${ name || 'Kernel' } {
constructor() {
this.argumentsLength = 0;
this.canvas = null;
this.context = null;
this.built = false;
this.program = null;
this.argumentNames = ${ JSON.stringify(cpuKernel.argumentNames) };
this.argumentTypes = ${ JSON.stringify(cpuKernel.argumentTypes) };
this.argumentSizes = ${ JSON.stringify(cpuKernel.argumentSizes) };
this.output = ${ JSON.stringify(cpuKernel.output) };
this._kernelString = \`${ cpuKernel._kernelString }\`;
this.output = ${ JSON.stringify(cpuKernel.output) };
this.run = function() {
this.run = null;
this.build(arguments);
return this.run.apply(this, arguments);
}.bind(this);
this.thread = {
x: 0,
y: 0,
z: 0
};
}
setCanvas(canvas) { this.canvas = canvas; return this; }
setContext(context) { this.context = context; return this; }
setInput(Type) { Input = Type; }
${ removeFnNoise(cpuKernel.build.toString()) }
setupArguments() {}
${ removeFnNoise(cpuKernel.setupConstants.toString()) }
run () { ${ cpuKernel.kernelString } }
getKernelString() { return this._kernelString; }
${ removeFnNoise(cpuKernel.validateSettings.toString()) }
${ removeFnNoise(cpuKernel.checkOutput.toString()) }
};
return kernelRunShortcut(new Kernel());
};`;
}
module.exports = {
cpuKernelString
};