-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
261 lines (242 loc) · 6.54 KB
/
index.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function setupArguments(args) {
const newArguments = new Array(args.length);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.toArray) {
newArguments[i] = arg.toArray();
} else {
newArguments[i] = arg;
}
}
return newArguments;
}
function mock1D() {
const args = setupArguments(arguments);
const row = new Float32Array(this.output.x);
for (let x = 0; x < this.output.x; x++) {
this.thread.x = x;
this.thread.y = 0;
this.thread.z = 0;
row[x] = this._fn.apply(this, args);
}
return row;
}
function mock2D() {
const args = setupArguments(arguments);
const matrix = new Array(this.output.y);
for (let y = 0; y < this.output.y; y++) {
const row = new Float32Array(this.output.x);
for (let x = 0; x < this.output.x; x++) {
this.thread.x = x;
this.thread.y = y;
this.thread.z = 0;
row[x] = this._fn.apply(this, args);
}
matrix[y] = row;
}
return matrix;
}
function mock2DGraphical() {
const args = setupArguments(arguments);
for (let y = 0; y < this.output.y; y++) {
for (let x = 0; x < this.output.x; x++) {
this.thread.x = x;
this.thread.y = y;
this.thread.z = 0;
this._fn.apply(this, args);
}
}
}
function mock3D() {
const args = setupArguments(arguments);
const cube = new Array(this.output.z);
for (let z = 0; z < this.output.z; z++) {
const matrix = new Array(this.output.y);
for (let y = 0; y < this.output.y; y++) {
const row = new Float32Array(this.output.x);
for (let x = 0; x < this.output.x; x++) {
this.thread.x = x;
this.thread.y = y;
this.thread.z = z;
row[x] = this._fn.apply(this, args);
}
matrix[y] = row;
}
cube[z] = matrix;
}
return cube;
}
function apiDecorate(kernel) {
kernel.setOutput = (output) => {
kernel.output = setupOutput(output);
if (kernel.graphical) {
setupGraphical(kernel);
}
};
kernel.toJSON = () => {
throw new Error('Not usable with gpuMock');
};
kernel.setConstants = (flag) => {
kernel.constants = flag;
return kernel;
};
kernel.setGraphical = (flag) => {
kernel.graphical = flag;
return kernel;
};
kernel.setCanvas = (flag) => {
kernel.canvas = flag;
return kernel;
};
kernel.setContext = (flag) => {
kernel.context = flag;
return kernel;
};
kernel.destroy = () => {};
kernel.validateSettings = () => {};
if (kernel.graphical && kernel.output) {
setupGraphical(kernel);
}
kernel.exec = function() {
return new Promise((resolve, reject) => {
try {
resolve(kernel.apply(kernel, arguments));
} catch(e) {
reject(e);
}
});
};
kernel.getPixels = (flip) => {
const {x, y} = kernel.output;
// cpu is not flipped by default
return flip ? flipPixels(kernel._imageData.data, x, y) : kernel._imageData.data.slice(0);
};
kernel.color = function(r, g, b, a) {
if (typeof a === 'undefined') {
a = 1;
}
r = Math.floor(r * 255);
g = Math.floor(g * 255);
b = Math.floor(b * 255);
a = Math.floor(a * 255);
const width = kernel.output.x;
const height = kernel.output.y;
const x = kernel.thread.x;
const y = height - kernel.thread.y - 1;
const index = x + y * width;
kernel._colorData[index * 4 + 0] = r;
kernel._colorData[index * 4 + 1] = g;
kernel._colorData[index * 4 + 2] = b;
kernel._colorData[index * 4 + 3] = a;
};
// these are added for api compatibility, but have no affect
const mockMethod = () => kernel;
const methods = [
'setWarnVarUsage',
'setArgumentTypes',
'setTactic',
'setOptimizeFloatMemory',
'setDebug',
'setLoopMaxIterations',
'setConstantTypes',
'setFunctions',
'setNativeFunctions',
'setInjectedNative',
'setPipeline',
'setPrecision',
'setOutputToTexture',
'setImmutable',
'setStrictIntegers',
'setDynamicOutput',
'setHardcodeConstants',
'setDynamicArguments',
'setUseLegacyEncoder',
'setWarnVarUsage',
'addSubKernel',
];
for (let i = 0; i < methods.length; i++) {
kernel[methods[i]] = mockMethod;
}
return kernel;
}
function setupGraphical(kernel) {
const {x, y} = kernel.output;
if (kernel.context && kernel.context.createImageData) {
const data = new Uint8ClampedArray(x * y * 4);
kernel._imageData = kernel.context.createImageData(x, y);
kernel._colorData = data;
} else {
const data = new Uint8ClampedArray(x * y * 4);
kernel._imageData = { data };
kernel._colorData = data;
}
}
function setupOutput(output) {
let result = null;
if (output.length) {
if (output.length === 3) {
const [x,y,z] = output;
result = { x, y, z };
} else if (output.length === 2) {
const [x,y] = output;
result = { x, y };
} else {
const [x] = output;
result = { x };
}
} else {
result = output;
}
return result;
}
function gpuMock(fn, settings = {}) {
const output = settings.output ? setupOutput(settings.output) : null;
function kernel() {
if (kernel.output.z) {
return mock3D.apply(kernel, arguments);
} else if (kernel.output.y) {
if (kernel.graphical) {
return mock2DGraphical.apply(kernel, arguments);
}
return mock2D.apply(kernel, arguments);
} else {
return mock1D.apply(kernel, arguments);
}
}
kernel._fn = fn;
kernel.constants = settings.constants || null;
kernel.context = settings.context || null;
kernel.canvas = settings.canvas || null;
kernel.graphical = settings.graphical || false;
kernel._imageData = null;
kernel._colorData = null;
kernel.output = output;
kernel.thread = {
x: 0,
y: 0,
z: 0
};
return apiDecorate(kernel);
}
function flipPixels(pixels, width, height) {
// https://stackoverflow.com/a/41973289/1324039
const halfHeight = height / 2 | 0; // the | 0 keeps the result an int
const bytesPerRow = width * 4;
// make a temp buffer to hold one row
const temp = new Uint8ClampedArray(width * 4);
const result = pixels.slice(0);
for (let y = 0; y < halfHeight; ++y) {
const topOffset = y * bytesPerRow;
const bottomOffset = (height - y - 1) * bytesPerRow;
// make copy of a row on the top half
temp.set(result.subarray(topOffset, topOffset + bytesPerRow));
// copy a row from the bottom half to the top
result.copyWithin(topOffset, bottomOffset, bottomOffset + bytesPerRow);
// copy the copy of the top half row to the bottom half
result.set(temp, bottomOffset);
}
return result;
}
module.exports = {
gpuMock
};