forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.js
More file actions
150 lines (132 loc) · 4.23 KB
/
gpu.js
File metadata and controls
150 lines (132 loc) · 4.23 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
///
/// Class: GPU
///
/// GPU.JS core class =D
///
var GPU = (function() {
var gl, canvas;
// https://gist.github.com/TooTallNate/4750953
function endianness() {
var b = new ArrayBuffer(4);
var a = new Uint32Array(b);
var c = new Uint8Array(b);
a[0] = 0xdeadbeef;
if (c[0] == 0xef) return 'LE';
if (c[0] == 0xde) return 'BE';
throw new Error('unknown endianness');
}
function GPU(ctx) {
canvas = undefined;
gl = ctx;
if (gl === undefined) {
canvas = document.createElement('canvas');
canvas.width = 2;
canvas.height = 2;
var glOpt = {
depth: false,
antialias: false
};
gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt);
}
gl.getExtension('OES_texture_float');
gl.getExtension('OES_texture_float_linear');
gl.getExtension('OES_element_index_uint');
this.gl = gl;
this.canvas = canvas;
this.programCache = {};
this.endianness = endianness();
this.functionBuilder = new functionBuilder();
this.functionBuilder.polyfillStandardFunctions();
}
GPU.prototype.getGl = function() {
return this.gl;
};
GPU.prototype.getCanvas = function() {
return this.canvas;
};
///
/// Function: createKernel
///
/// The core GPU.js function
///
/// The parameter object contains the following sub parameters
///
/// |---------------|---------------|---------------------------------------------------------------------------|
/// | Name | Default value | Description |
/// |---------------|---------------|---------------------------------------------------------------------------|
/// | dimensions | [1024] | Thread dimension array |
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
/// | | | + "cpu" : Forces JS fallback mode only |
/// |---------------|---------------|---------------------------------------------------------------------------|
///
/// Parameters:
/// inputFunction {JS Function} The calling to perform the conversion
/// paramObj {Object} The parameter configuration object
///
/// Returns:
/// callable function to run
///
function createKernel(kernel, paramObj) {
//
// basic parameters safety checks
//
if( kernel === undefined ) {
throw "Missing kernel parameter";
}
if( !(kernel instanceof Function) ) {
throw "kernel parameter not a function";
}
if( paramObj === undefined ) {
paramObj = {};
}
//
// Get theconfig, fallbacks to default value if not set
//
paramObj.dimensions = paramObj.dimensions || [];
mode = paramObj.mode && paramObj.mode.toLowerCase();
if ( mode == "cpu" ) {
return this._backendFallback(kernel, paramObj);
}
//
// Attempts to do the glsl conversion
//
try {
return this._backendGLSL(kernel, paramObj);
} catch (e) {
if ( mode != "gpu") {
console.warning("Falling back to CPU!");
return this._backendFallback(kernel, paramObj);
} else {
throw e;
}
}
};
GPU.prototype.createKernel = createKernel;
///
/// Function: addFunction
///
/// Adds additional functions, that the kernel may call.
///
/// Parameters:
/// jsFunction - {JS Function} JS Function to do conversion
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
/// returnType - {String} The return type, assumes "float" if null
///
/// Retuns:
/// {GPU} returns itself
///
function addFunction( jsFunction, paramTypeArray, returnType ) {
this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType );
return this;
}
GPU.prototype.addFunction = addFunction;
GPU.prototype.textureToArray = function(texture) {
var copy = this.createKernel(function(x) {
return x[this.thread.z][this.thread.y][this.thread.x];
});
return copy(texture);
};
return GPU;
})();