forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
421 lines (379 loc) · 11 KB
/
utils.js
File metadata and controls
421 lines (379 loc) · 11 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
///
/// Class: GPUUtils
///
/// Various utility functions / snippets of code that GPU.JS uses internally.\
/// This covers various snippets of code that is not entirely gpu.js specific (ie. may find uses elsewhere)
///
/// Note that all moethods in this class is "static" by nature `GPUUtils.functionName()`
///
var GPUUtils = (function() {
var GPUUtils = {};
//-----------------------------------------------------------------------------
//
// System values support (currently only endianness)
//
//-----------------------------------------------------------------------------
// systemEndianness closure based memoizer
var endianness = null;
///
/// Function: systemEndianness
///
/// Gets the system endianness, and cache it
///
/// Returns:
/// {String} "LE" or "BE" depending on system architecture
///
/// Credit: https://gist.github.com/TooTallNate/4750953
function systemEndianness() {
if( endianness !== null ) {
return endianness;
}
var b = new ArrayBuffer(4);
var a = new Uint32Array(b);
var c = new Uint8Array(b);
a[0] = 0xdeadbeef;
if (c[0] == 0xef) return endianness = 'LE';
if (c[0] == 0xde) return endianness = 'BE';
throw new Error('unknown endianness');
}
GPUUtils.systemEndianness = systemEndianness;
//-----------------------------------------------------------------------------
//
// Function and function string validations
//
//-----------------------------------------------------------------------------
///
/// Function: isFunction
///
/// Return TRUE, on a JS function
///
/// Parameters:
/// funcObj - {JS Function} Object to validate if its a function
///
/// Returns:
/// {Boolean} TRUE if the object is a JS function
///
function isFunction( funcObj ) {
return typeof(funcObj) === 'function';
}
GPUUtils.isFunction = isFunction;
///
/// Function: isFunctionString
///
/// Return TRUE, on a valid JS function string
///
/// Note: This does just a VERY simply sanity check. And may give false positives.
///
/// Parameters:
/// funcStr - {String} String of JS function to validate
///
/// Returns:
/// {Boolean} TRUE if the string passes basic validation
///
function isFunctionString( funcStr ) {
if( funcStr !== null ) {
return (funcStr.toString().slice(0, "function".length).toLowerCase() == "function");
}
return false;
}
GPUUtils.isFunctionString = isFunctionString;
// FUNCTION_NAME regex
var FUNCTION_NAME = /function ([^(]*)/;
///
/// Function: getFunctionName_fromString
///
/// Return the function name from a JS function string
///
/// Parameters:
/// funcStr - {String} String of JS function to validate
///
/// Returns:
/// {String} Function name string (if found)
///
function getFunctionName_fromString( funcStr ) {
return FUNCTION_NAME.exec(funcStr)[1];
}
GPUUtils.getFunctionName_fromString = getFunctionName_fromString;
// STRIP COMMENTS regex
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
// ARGUMENT NAMES regex
var ARGUMENT_NAMES = /([^\s,]+)/g;
///
/// Function: getParamNames_fromString
///
/// Return list of parameter names extracted from the JS function string
///
/// Parameters:
/// funcStr - {String} String of JS function to validate
///
/// Returns:
/// {[String, ...]} Array representing all the parameter names
///
function getParamNames_fromString(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
GPUUtils.getParamNames_fromString = getParamNames_fromString;
//-----------------------------------------------------------------------------
//
// Object / function cloning and manipulation
//
//-----------------------------------------------------------------------------
///
/// Function: clone
///
/// Returns a clone
///
/// Parameters:
/// obj - {Object} Object to clone
///
/// Returns:
/// {Object} Cloned object
///
function clone(obj) {
if(obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj)
return obj;
var temp = obj.constructor(); // changed
for(var key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
obj.isActiveClone = null;
temp[key] = clone(obj[key]);
delete obj.isActiveClone;
}
}
return temp;
}
GPUUtils.clone = clone;
///
/// Function: newPromise
///
/// Returns a `new Promise` object based on the underlying implmentation
///
/// Parameters:
/// executor - {function(resolve,reject)} Promise builder function
///
/// Returns:
/// {Promise} Promise object
///
function newPromise(executor) {
var imple = Promise || small_promise;
if(imple === null) {
throw TypeError("Browser is missing Promise implmentation. Consider adding small_promise.js polyfill");
}
return (new imple(executor));
}
GPUUtils.newPromise = newPromise;
///
/// Function: functionBinder
///
/// Limited implmentation of Function.bind, with fallback
///
/// Parameters:
/// inFunc - {JS Function} to setup bind on
/// thisObj - {Object} The this parameter to assume inside the binded function
///
/// Returns:
/// {JS Function} The binded function
///
function functionBinder( inFunc, thisObj ) {
if( inFunc.bind ) {
return inFunc.bind(thisObj);
}
return function() {
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
return inFunc.apply( thisObj, args );
}
}
GPUUtils.functionBinder = functionBinder;
///
/// Function: getArgumentType
///
/// Evaluate the argument type, to apply respective logic for it
///
/// Parameters:
/// arg - {Object} The argument object to evaluate type
///
/// Returns:
/// {String} Argument type Array/Number/Texture/Unknown
///
function getArgumentType(arg) {
if (Array.isArray(arg)) {
return 'Array';
} else if (typeof arg == "number") {
return 'Number';
} else if (arg instanceof GPUTexture) {
return 'Texture';
} else {
return 'Unknown';
}
}
GPUUtils.getArgumentType = getArgumentType;
//-----------------------------------------------------------------------------
//
// Canvas validation and support
//
//-----------------------------------------------------------------------------
///
/// Function: isCanvas
///
/// Return TRUE, on a valid DOM canvas object
///
/// Note: This does just a VERY simply sanity check. And may give false positives.
///
/// Parameters:
/// canvasObj - {Canvas DOM object} Object to validate
///
/// Returns:
/// {Boolean} TRUE if the object is a DOM canvas
///
function isCanvas( canvasObj ) {
return (
canvasObj != null &&
canvasObj.nodeName &&
canvasObj.getContext &&
canvasObj.nodeName.toUpperCase() === "CANVAS"
);
}
GPUUtils.isCanvas = isCanvas;
// browserSupport_canvas closure based memoizer
var browserSupport_canvas_memoizer = null;
///
/// Function: browserSupport_canvas
///
/// Return TRUE, if browser supports canvas
///
/// Returns:
/// {Boolean} TRUE if browser supports canvas
///
function browserSupport_canvas() {
if( browserSupport_canvas_memoizer !== null ) {
return browserSupport_canvas_memoizer;
}
return browserSupport_canvas_memoizer = isCanvas(document.createElement('canvas'));
}
GPUUtils.browserSupport_canvas = browserSupport_canvas;
///
/// Function: init_canvas
///
/// Initiate and returns a canvas, for usage in init_webgl.
/// Returns only if canvas is supported by browser.
///
/// Returns:
/// {Canvas DOM object} Canvas dom object if supported by browser, else null
///
function init_canvas() {
// Fail fast if browser previously detected no support
if( browserSupport_canvas_memoizer === false ) {
return null;
}
// Create a new canvas DOM
var canvas = document.createElement('canvas');
// First time setup, does the browser support check memoizer
if( browserSupport_canvas_memoizer === null ) {
browserSupport_canvas_memoizer = isCanvas(canvas);
if( browserSupport_canvas_memoizer === false ) {
return null;
}
}
// Default width and height, to fix webgl issue in safari
canvas.width = 2;
canvas.height = 2;
// Returns the canvas
return canvas;
}
GPUUtils.init_canvas = init_canvas;
//-----------------------------------------------------------------------------
//
// Webgl validation and support
//
//-----------------------------------------------------------------------------
///
/// Function: isWebgl
///
/// Return TRUE, on a valid webgl context object
///
/// Note: This does just a VERY simply sanity check. And may give false positives.
///
/// Parameters:
/// webglObj - {webgl context} Object to validate
///
/// Returns:
/// {Boolean} TRUE if the object is a webgl context object
///
function isWebgl( webglObj ) {
return (
webglObj != null &&
webglObj.getExtension
);
}
GPUUtils.isWebgl = isWebgl;
// browserSupport_canvas closure based memoizer
var browserSupport_webgl_memoizer = null;
///
/// Function: browserSupport_webgl
///
/// Return TRUE, if browser supports webgl
///
/// Returns:
/// {Boolean} TRUE if browser supports webgl
///
function browserSupport_webgl() {
if( browserSupport_webgl_memoizer !== null ) {
return browserSupport_webgl_memoizer;
}
return browserSupport_webgl_memoizer = isWebgl(init_webgl(init_canvas()));
}
GPUUtils.browserSupport_webgl = browserSupport_webgl;
// Default webgl options to use
var init_webgl_defaultOptions = {
alpha: false,
depth: false,
antialias: false
}
///
/// Function: init_webgl
///
/// Initiate and returns a webgl, from a canvas object
/// Returns only if webgl is supported by browser.
///
/// Parameters:
/// canvasObj - {Canvas DOM object} Object to validate
///
/// Returns:
/// {Canvas DOM object} Canvas dom object if supported by browser, else null
///
function init_webgl(canvasObj) {
// Fail fast for invalid canvas object
if( !isCanvas(canvasObj) ) {
throw new Error("Invalid canvas object - "+canvasObj);
}
// Fail fast if browser previously detected no support
if( browserSupport_canvas_memoizer === false || browserSupport_webgl_memoizer === false ) {
return null;
}
// Create a new canvas DOM
var webgl = (
canvasObj.getContext("experimental-webgl2", init_webgl_defaultOptions) ||
canvasObj.getContext("experimental-webgl", init_webgl_defaultOptions) ||
canvasObj.getContext("webgl", init_webgl_defaultOptions)
);
// First time setup, does the browser support check memoizer
if( browserSupport_webgl_memoizer === null ) {
browserSupport_webgl_memoizer = isWebgl(webgl);
if( browserSupport_webgl_memoizer === false ) {
return null;
}
}
// Get the extension that is needed
GPUUtils.OES_texture_float = webgl.getExtension('OES_texture_float');
GPUUtils.OES_texture_float_linear = webgl.getExtension('OES_texture_float_linear');
GPUUtils.OES_element_index_uint = webgl.getExtension('OES_element_index_uint');
// Returns the canvas
return webgl;
}
GPUUtils.init_webgl = init_webgl;
return GPUUtils;
})();