forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
90 lines (82 loc) · 2.44 KB
/
array.js
File metadata and controls
90 lines (82 loc) · 2.44 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
const { WebGLKernelValue } = require('./index');
const { Input } = require('../../../input');
/**
* @abstract
*/
class WebGLKernelArray extends WebGLKernelValue {
/**
*
* @param {number} width
* @param {number} height
*/
checkSize(width, height) {
if (!this.kernel.validate) return;
const { maxTextureSize } = this.kernel.constructor.features;
if (width > maxTextureSize || height > maxTextureSize) {
if (width > height) {
throw new Error(`Argument texture width of ${width} larger than maximum size of ${maxTextureSize} for your GPU`);
} else if (width < height) {
throw new Error(`Argument texture height of ${height} larger than maximum size of ${maxTextureSize} for your GPU`);
} else {
throw new Error(`Argument texture height and width of ${height} larger than maximum size of ${maxTextureSize} for your GPU`);
}
}
}
setup() {
this.requestTexture();
this.setupTexture();
this.defineTexture();
}
requestTexture() {
this.texture = this.onRequestTexture();
}
defineTexture() {
const { context: gl } = this;
gl.activeTexture(this.contextHandle);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
setupTexture() {
this.contextHandle = this.onRequestContextHandle();
this.index = this.onRequestIndex();
this.dimensionsId = this.id + 'Dim';
this.sizeId = this.id + 'Size';
}
/**
* bit storage ratio of source to target 'buffer', i.e. if 8bit array -> 32bit tex = 4
* @param value
* @returns {number}
*/
getBitRatio(value) {
if (Array.isArray(value[0])) {
return this.getBitRatio(value[0]);
} else if (value.constructor === Input) {
return this.getBitRatio(value.value);
}
switch (value.constructor) {
case Uint8ClampedArray:
case Uint8Array:
case Int8Array:
return 1;
case Uint16Array:
case Int16Array:
return 2;
case Float32Array:
case Int32Array:
default:
return 4;
}
}
destroy() {
if (this.prevArg) {
this.prevArg.delete();
}
this.context.deleteTexture(this.texture);
}
}
module.exports = {
WebGLKernelArray
};