1 | var GLType = ((GLType2) => {
|
2 | GLType2[GLType2["I8"] = 5120] = "I8";
|
3 | GLType2[GLType2["U8"] = 5121] = "U8";
|
4 | GLType2[GLType2["I16"] = 5122] = "I16";
|
5 | GLType2[GLType2["U16"] = 5123] = "U16";
|
6 | GLType2[GLType2["I32"] = 5124] = "I32";
|
7 | GLType2[GLType2["U32"] = 5125] = "U32";
|
8 | GLType2[GLType2["F32"] = 5126] = "F32";
|
9 | return GLType2;
|
10 | })(GLType || {});
|
11 | const GL2TYPE = {
|
12 | [5120 ]: "i8",
|
13 | [5121 ]: "u8",
|
14 | [5122 ]: "i16",
|
15 | [5123 ]: "u16",
|
16 | [5124 ]: "i32",
|
17 | [5125 ]: "u32",
|
18 | [5126 ]: "f32"
|
19 | };
|
20 | const TYPE2GL = {
|
21 | i8: 5120 ,
|
22 | u8: 5121 ,
|
23 | u8c: 5121 ,
|
24 | i16: 5122 ,
|
25 | u16: 5123 ,
|
26 | i32: 5124 ,
|
27 | u32: 5125 ,
|
28 | f32: 5126 ,
|
29 | f64: void 0
|
30 | };
|
31 | const SIZEOF = {
|
32 | u8: 1,
|
33 | u8c: 1,
|
34 | i8: 1,
|
35 | u16: 2,
|
36 | i16: 2,
|
37 | u32: 4,
|
38 | i32: 4,
|
39 | i64: 8,
|
40 | u64: 8,
|
41 | f32: 4,
|
42 | f64: 8
|
43 | };
|
44 | const BIT_SHIFTS = {
|
45 | i8: 0,
|
46 | u8: 0,
|
47 | u8c: 0,
|
48 | i16: 1,
|
49 | u16: 1,
|
50 | i32: 2,
|
51 | u32: 2,
|
52 | i64: 3,
|
53 | u64: 3,
|
54 | f32: 2,
|
55 | f64: 3
|
56 | };
|
57 | const FLOAT_ARRAY_CTORS = {
|
58 | f32: Float32Array,
|
59 | f64: Float64Array
|
60 | };
|
61 | const INT_ARRAY_CTORS = {
|
62 | i8: Int8Array,
|
63 | i16: Int16Array,
|
64 | i32: Int32Array
|
65 | };
|
66 | const UINT_ARRAY_CTORS = {
|
67 | u8: Uint8Array,
|
68 | u8c: Uint8ClampedArray,
|
69 | u16: Uint16Array,
|
70 | u32: Uint32Array
|
71 | };
|
72 | const BIGINT_ARRAY_CTORS = {
|
73 | i64: BigInt64Array,
|
74 | u64: BigUint64Array
|
75 | };
|
76 | const TYPEDARRAY_CTORS = {
|
77 | ...FLOAT_ARRAY_CTORS,
|
78 | ...INT_ARRAY_CTORS,
|
79 | ...UINT_ARRAY_CTORS
|
80 | };
|
81 | const asNativeType = (type) => {
|
82 | const t = GL2TYPE[type];
|
83 | return t !== void 0 ? t : type;
|
84 | };
|
85 | const asGLType = (type) => {
|
86 | const t = TYPE2GL[type];
|
87 | return t !== void 0 ? t : type;
|
88 | };
|
89 | const asInt = (...args) => args.map((x) => x | 0);
|
90 | const sizeOf = (type) => SIZEOF[type] || SIZEOF[asNativeType(type)];
|
91 | function typedArray(type, ...args) {
|
92 | const ctor = BIGINT_ARRAY_CTORS[type];
|
93 | return new (ctor || TYPEDARRAY_CTORS[asNativeType(type)])(...args);
|
94 | }
|
95 | function typedArrayOfVec(type, data, stride) {
|
96 | const $data = Array.isArray(data) ? data : [...data];
|
97 | if (stride === void 0) stride = $data[0].length;
|
98 | const num = $data.length;
|
99 | const res = typedArray(type, num * stride);
|
100 | for (let i = 0, j = 0; i < num; i++, j += stride) {
|
101 | res.set($data[i], j);
|
102 | }
|
103 | return res;
|
104 | }
|
105 | const typedArrayType = (x) => {
|
106 | if (Array.isArray(x)) return "f64";
|
107 | for (let id in TYPEDARRAY_CTORS) {
|
108 | if (x instanceof TYPEDARRAY_CTORS[id]) return id;
|
109 | }
|
110 | return "f64";
|
111 | };
|
112 | const uintTypeForSize = (x) => x <= 256 ? "u8" : x <= 65536 ? "u16" : "u32";
|
113 | const intTypeForSize = (x) => x >= -128 && x < 128 ? "i8" : x >= -32768 && x < 32768 ? "i16" : "i32";
|
114 | const uintTypeForBits = (x) => x > 16 ? "u32" : x > 8 ? "u16" : "u8";
|
115 | const intTypeForBits = (x) => x > 16 ? "i32" : x > 8 ? "i16" : "i8";
|
116 | const narrowInt = (t) => t === "i64" ? "i32" : t === "i32" ? "i16" : t === "i16" ? "i8" : "i8";
|
117 | const widenInt = (t) => t === "i8" ? "i16" : t === "i16" ? "i32" : t === "i32" ? "i64" : "i64";
|
118 | const narrowUint = (t) => t === "u64" ? "u32" : t === "u32" ? "u16" : t === "u16" ? "u8" : "u8";
|
119 | const widenUint = (t) => t === "u8" || t === "u8c" ? "u16" : t === "u16" ? "u32" : t === "u32" ? "u64" : "u64";
|
120 | const narrowFloat = (t) => t === "f64" ? "f32" : "f32";
|
121 | const widenFloat = (t) => t === "f32" ? "f64" : "f64";
|
122 | const narrowType = (t) => t[0] === "i" ? narrowInt(t) : t[0] === "u" ? narrowUint(t) : narrowFloat(t);
|
123 | const widenType = (t) => t[0] === "i" ? widenInt(t) : t[0] === "u" ? widenUint(t) : widenFloat(t);
|
124 | export {
|
125 | BIGINT_ARRAY_CTORS,
|
126 | BIT_SHIFTS,
|
127 | FLOAT_ARRAY_CTORS,
|
128 | GL2TYPE,
|
129 | GLType,
|
130 | INT_ARRAY_CTORS,
|
131 | SIZEOF,
|
132 | TYPE2GL,
|
133 | TYPEDARRAY_CTORS,
|
134 | UINT_ARRAY_CTORS,
|
135 | asGLType,
|
136 | asInt,
|
137 | asNativeType,
|
138 | intTypeForBits,
|
139 | intTypeForSize,
|
140 | narrowFloat,
|
141 | narrowInt,
|
142 | narrowType,
|
143 | narrowUint,
|
144 | sizeOf,
|
145 | typedArray,
|
146 | typedArrayOfVec,
|
147 | typedArrayType,
|
148 | uintTypeForBits,
|
149 | uintTypeForSize,
|
150 | widenFloat,
|
151 | widenInt,
|
152 | widenType,
|
153 | widenUint
|
154 | };
|