forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-base.js
More file actions
180 lines (152 loc) · 3.5 KB
/
kernel-base.js
File metadata and controls
180 lines (152 loc) · 3.5 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
const utils = require('../utils');
module.exports = class BaseKernel {
constructor(fnString, settings) {
this.paramNames = utils.getParamNamesFromString(fnString);
this.fnString = fnString;
this.dimensions = [];
this.debug = false;
this.graphical = false;
this.loopMaxIterations = 0;
this.constants = null;
this.wraparound = null;
this.hardcodeConstants = null;
this.outputToTexture = null;
this.texSize = null;
this._canvas = null;
this._webGl = null;
this.threadDim = null;
this.floatTextures = null;
this.floatOutput = null;
this.floatOutputForce = null;
this.addFunction = null;
this.copyData = true;
this.subKernels = null;
this.subKernelProperties = null;
this.subKernelNames = null;
this.subKernelOutputVariableNames = null;
for (let p in settings) {
if (!settings.hasOwnProperty(p) || !this.hasOwnProperty(p)) continue;
this[p] = settings[p];
}
if (settings.hasOwnProperty('canvas')) {
this._canvas = settings.canvas;
}
if (!this._canvas) this._canvas = utils.initCanvas();
}
build() {
throw new Error('"build" not defined on Base');
}
setAddFunction(cb) {
this.addFunction = cb;
return this;
}
setDimensions(dimensions) {
this.dimensions = dimensions;
return this;
}
setDebug(flag) {
this.debug = flag;
return this;
}
setGraphical(flag) {
this.graphical = flag;
return this;
}
setLoopMaxIterations(max) {
this.loopMaxIterations = max;
return this;
}
setConstants(constants) {
this.constants = constants;
return this;
}
setWraparound(flag) {
console.warn('Wraparound mode is not supported and undocumented.');
this.wraparound = flag;
return this;
}
setHardcodeConstants(flag) {
this.hardcodeConstants = flag;
return this;
}
setOutputToTexture(flag) {
this.outputToTexture = flag;
return this;
}
setFloatTextures(flag) {
this.floatTextures = flag;
return this;
}
setFloatOutput(flag) {
this.floatOutput = flag;
return this;
}
setFloatOutputForce(flag) {
this.floatOutputForce = flag;
return this;
}
setCanvas(canvas) {
this._canvas = canvas;
return this;
}
setWebGl(webGl) {
this._webGl = webGl;
return this;
}
setCopyData(copyData) {
this.copyData = copyData;
return this;
}
get canvas() {
return this._canvas;
}
get webGl() {
return this._webGl;
}
validateOptions() {
throw new Error('validateOptions not defined');
}
exec() {
return this.execute.apply(this, arguments);
}
execute() {
//
// Prepare the required objects
//
const args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
//
// Setup and return the promise, and execute the function, in synchronous mode
//
return utils.newPromise((accept, reject) => {
try {
accept(this.run.apply(this, args));
} catch (e) {
//
// Error : throw rejection
//
reject(e);
}
});
}
addSubKernel(fnString) {
if (this.subKernels === null) {
this.subKernels = [];
this.subKernelNames = [];
}
this.subKernels.push(fnString);
this.subKernelNames.push(utils.getFunctionNameFromString(fnString));
return this;
}
addSubKernelProperty(property, fnString) {
if (this.subKernelProperties === null) {
this.subKernelProperties = {};
this.subKernelNames = [];
}
if (this.subKernelProperties.hasOwnProperty(property)) {
throw new Error(`cannot add sub kernel ${ property }, already defined`);
}
this.subKernelProperties[property] = fnString;
this.subKernelNames.push(utils.getFunctionNameFromString(fnString));
return this;
}
};