-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathtypedarrays.js
146 lines (136 loc) · 6.35 KB
/
typedarrays.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
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
/*
* Copyright 2019 Gregg Tavares
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* Low level shader typed array related functions
*
* You should generally not need to use these functions. They are provided
* for those cases where you're doing something out of the ordinary
* and you need lower level access.
*
* For backward compatibility they are available at both `twgl.typedArray` and `twgl`
* itself
*
* See {@link module:twgl} for core functions
*
* @module twgl/typedArray
*/
// make sure we don't see a global gl
const gl = undefined; /* eslint-disable-line */
/* DataType */
const BYTE = 0x1400;
const UNSIGNED_BYTE = 0x1401;
const SHORT = 0x1402;
const UNSIGNED_SHORT = 0x1403;
const INT = 0x1404;
const UNSIGNED_INT = 0x1405;
const FLOAT = 0x1406;
const UNSIGNED_SHORT_4_4_4_4 = 0x8033;
const UNSIGNED_SHORT_5_5_5_1 = 0x8034;
const UNSIGNED_SHORT_5_6_5 = 0x8363;
const HALF_FLOAT = 0x140B;
const UNSIGNED_INT_2_10_10_10_REV = 0x8368;
const UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B;
const UNSIGNED_INT_5_9_9_9_REV = 0x8C3E;
const FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD;
const UNSIGNED_INT_24_8 = 0x84FA;
const glTypeToTypedArray = {};
{
const tt = glTypeToTypedArray;
tt[BYTE] = Int8Array;
tt[UNSIGNED_BYTE] = Uint8Array;
tt[SHORT] = Int16Array;
tt[UNSIGNED_SHORT] = Uint16Array;
tt[INT] = Int32Array;
tt[UNSIGNED_INT] = Uint32Array;
tt[FLOAT] = Float32Array;
tt[UNSIGNED_SHORT_4_4_4_4] = Uint16Array;
tt[UNSIGNED_SHORT_5_5_5_1] = Uint16Array;
tt[UNSIGNED_SHORT_5_6_5] = Uint16Array;
tt[HALF_FLOAT] = Uint16Array;
tt[UNSIGNED_INT_2_10_10_10_REV] = Uint32Array;
tt[UNSIGNED_INT_10F_11F_11F_REV] = Uint32Array;
tt[UNSIGNED_INT_5_9_9_9_REV] = Uint32Array;
tt[FLOAT_32_UNSIGNED_INT_24_8_REV] = Uint32Array;
tt[UNSIGNED_INT_24_8] = Uint32Array;
}
/**
* Get the GL type for a typedArray
* @param {ArrayBufferView} typedArray a typedArray
* @return {number} the GL type for array. For example pass in an `Int8Array` and `gl.BYTE` will
* be returned. Pass in a `Uint32Array` and `gl.UNSIGNED_INT` will be returned
* @memberOf module:twgl/typedArray
*/
function getGLTypeForTypedArray(typedArray) {
if (typedArray instanceof Int8Array) { return BYTE; } // eslint-disable-line
if (typedArray instanceof Uint8Array) { return UNSIGNED_BYTE; } // eslint-disable-line
if (typedArray instanceof Uint8ClampedArray) { return UNSIGNED_BYTE; } // eslint-disable-line
if (typedArray instanceof Int16Array) { return SHORT; } // eslint-disable-line
if (typedArray instanceof Uint16Array) { return UNSIGNED_SHORT; } // eslint-disable-line
if (typedArray instanceof Int32Array) { return INT; } // eslint-disable-line
if (typedArray instanceof Uint32Array) { return UNSIGNED_INT; } // eslint-disable-line
if (typedArray instanceof Float32Array) { return FLOAT; } // eslint-disable-line
throw new Error('unsupported typed array type');
}
/**
* Get the GL type for a typedArray type
* @param {ArrayBufferView} typedArrayType a typedArray constructor
* @return {number} the GL type for type. For example pass in `Int8Array` and `gl.BYTE` will
* be returned. Pass in `Uint32Array` and `gl.UNSIGNED_INT` will be returned
* @memberOf module:twgl/typedArray
*/
function getGLTypeForTypedArrayType(typedArrayType) {
if (typedArrayType === Int8Array) { return BYTE; } // eslint-disable-line
if (typedArrayType === Uint8Array) { return UNSIGNED_BYTE; } // eslint-disable-line
if (typedArrayType === Uint8ClampedArray) { return UNSIGNED_BYTE; } // eslint-disable-line
if (typedArrayType === Int16Array) { return SHORT; } // eslint-disable-line
if (typedArrayType === Uint16Array) { return UNSIGNED_SHORT; } // eslint-disable-line
if (typedArrayType === Int32Array) { return INT; } // eslint-disable-line
if (typedArrayType === Uint32Array) { return UNSIGNED_INT; } // eslint-disable-line
if (typedArrayType === Float32Array) { return FLOAT; } // eslint-disable-line
throw new Error('unsupported typed array type');
}
/**
* Get the typed array constructor for a given GL type
* @param {number} type the GL type. (eg: `gl.UNSIGNED_INT`)
* @return {function} the constructor for a the corresponding typed array. (eg. `Uint32Array`).
* @memberOf module:twgl/typedArray
*/
function getTypedArrayTypeForGLType(type) {
const CTOR = glTypeToTypedArray[type];
if (!CTOR) {
throw new Error('unknown gl type');
}
return CTOR;
}
const isArrayBuffer = typeof SharedArrayBuffer !== 'undefined'
? function isArrayBufferOrSharedArrayBuffer(a) {
return a && a.buffer && (a.buffer instanceof ArrayBuffer || a.buffer instanceof SharedArrayBuffer);
}
: function isArrayBuffer(a) {
return a && a.buffer && a.buffer instanceof ArrayBuffer;
};
export {
getGLTypeForTypedArray,
getGLTypeForTypedArrayType,
getTypedArrayTypeForGLType,
isArrayBuffer,
};