forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.js
More file actions
77 lines (72 loc) · 1.63 KB
/
texture.js
File metadata and controls
77 lines (72 loc) · 1.63 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
/**
* @desc WebGl Texture implementation in JS
* @param {ITextureSettings} settings
*/
class Texture {
constructor(settings) {
const {
texture,
size,
dimensions,
output,
context,
gpu,
type = 'NumberTexture',
} = settings;
if (!output) throw new Error('settings property "output" required.');
if (!context) throw new Error('settings property "context" required.');
this.texture = texture;
this.size = size;
this.dimensions = dimensions;
this.output = output;
this.context = context;
this.gpu = gpu;
this.kernel = null;
this.type = type;
}
/**
* @desc Converts the Texture into a JavaScript Array.
* @param {GPU} [gpu]
* @returns {Number[]|Number[][]|Number[][][]}
*/
toArray(gpu) {
let {
kernel
} = this;
if (kernel) return kernel(this);
gpu = gpu || this.gpu;
if (!gpu) throw new Error('settings property "gpu" or argument required.');
kernel = gpu.createKernel(function(x) {
return x[this.thread.z][this.thread.y][this.thread.x];
}, {
output: this.output,
precision: this.getPrecision(),
optimizeFloatMemory: this.type === 'MemoryOptimizedNumberTexture',
});
this.kernel = kernel;
return kernel(this);
}
getPrecision() {
switch (this.type) {
case 'NumberTexture':
return 'unsigned';
case 'MemoryOptimizedNumberTexture':
case 'ArrayTexture(1)':
case 'ArrayTexture(2)':
case 'ArrayTexture(3)':
case 'ArrayTexture(4)':
return 'single';
default:
throw new Error('Unknown texture type');
}
}
/**
* @desc Deletes the Texture
*/
delete() {
return this.context.deleteTexture(this.texture);
}
}
module.exports = {
Texture
};