forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
54 lines (49 loc) · 1.43 KB
/
input.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
class Input {
constructor(value, size) {
this.value = value;
if (Array.isArray(size)) {
this.size = size;
} else {
this.size = new Int32Array(3);
if (size.z) {
this.size = new Int32Array([size.x, size.y, size.z]);
} else if (size.y) {
this.size = new Int32Array([size.x, size.y]);
} else {
this.size = new Int32Array([size.x]);
}
}
const [w, h, d] = this.size;
if (d) {
if (this.value.length !== (w * h * d)) {
throw new Error(`Input size ${this.value.length} does not match ${w} * ${h} * ${d} = ${(h * w * d)}`);
}
} else if (h) {
if (this.value.length !== (w * h)) {
throw new Error(`Input size ${this.value.length} does not match ${w} * ${h} = ${(h * w)}`);
}
} else {
if (this.value.length !== w) {
throw new Error(`Input size ${this.value.length} does not match ${w}`);
}
}
}
toArray() {
const { utils } = require('./utils');
const [w, h, d] = this.size;
if (d) {
return utils.erectMemoryOptimized3DFloat(this.value.subarray ? this.value : new Float32Array(this.value), w, h, d);
} else if (h) {
return utils.erectMemoryOptimized2DFloat(this.value.subarray ? this.value : new Float32Array(this.value), w, h);
} else {
return this.value;
}
}
}
function input(value, size) {
return new Input(value, size);
}
module.exports = {
Input,
input
};