1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export var GLType;
|
9 | (function (GLType) {
|
10 | GLType[GLType["I8"] = 5120] = "I8";
|
11 | GLType[GLType["U8"] = 5121] = "U8";
|
12 | GLType[GLType["I16"] = 5122] = "I16";
|
13 | GLType[GLType["U16"] = 5123] = "U16";
|
14 | GLType[GLType["I32"] = 5124] = "I32";
|
15 | GLType[GLType["U32"] = 5125] = "U32";
|
16 | GLType[GLType["F32"] = 5126] = "F32";
|
17 | })(GLType || (GLType = {}));
|
18 |
|
19 |
|
20 |
|
21 | export const GL2TYPE = {
|
22 | [GLType.I8]: "i8",
|
23 | [GLType.U8]: "u8",
|
24 | [GLType.I16]: "i16",
|
25 | [GLType.U16]: "u16",
|
26 | [GLType.I32]: "i32",
|
27 | [GLType.U32]: "u32",
|
28 | [GLType.F32]: "f32",
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export const TYPE2GL = {
|
39 | i8: GLType.I8,
|
40 | u8: GLType.U8,
|
41 | u8c: GLType.U8,
|
42 | i16: GLType.I16,
|
43 | u16: GLType.U16,
|
44 | i32: GLType.I32,
|
45 | u32: GLType.U32,
|
46 | f32: GLType.F32,
|
47 | f64: undefined,
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export const SIZEOF = {
|
54 | u8: 1,
|
55 | u8c: 1,
|
56 | i8: 1,
|
57 | u16: 2,
|
58 | i16: 2,
|
59 | u32: 4,
|
60 | i32: 4,
|
61 | i64: 8,
|
62 | u64: 8,
|
63 | f32: 4,
|
64 | f64: 8,
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export const BIT_SHIFTS = {
|
71 | i8: 0,
|
72 | u8: 0,
|
73 | u8c: 0,
|
74 | i16: 1,
|
75 | u16: 1,
|
76 | i32: 2,
|
77 | u32: 2,
|
78 | i64: 3,
|
79 | u64: 3,
|
80 | f32: 2,
|
81 | f64: 3,
|
82 | };
|
83 | export const FLOAT_ARRAY_CTORS = {
|
84 | f32: Float32Array,
|
85 | f64: Float64Array,
|
86 | };
|
87 | export const INT_ARRAY_CTORS = {
|
88 | i8: Int8Array,
|
89 | i16: Int16Array,
|
90 | i32: Int32Array,
|
91 | };
|
92 | export const UINT_ARRAY_CTORS = {
|
93 | u8: Uint8Array,
|
94 | u8c: Uint8ClampedArray,
|
95 | u16: Uint16Array,
|
96 | u32: Uint32Array,
|
97 | };
|
98 | export const BIGINT_ARRAY_CTORS = {
|
99 | i64: BigInt64Array,
|
100 | u64: BigUint64Array,
|
101 | };
|
102 | export const TYPEDARRAY_CTORS = {
|
103 | ...FLOAT_ARRAY_CTORS,
|
104 | ...INT_ARRAY_CTORS,
|
105 | ...UINT_ARRAY_CTORS,
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | export const asNativeType = (type) => {
|
120 | const t = GL2TYPE[type];
|
121 | return t !== undefined ? t : type;
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | export const asGLType = (type) => {
|
135 | const t = TYPE2GL[type];
|
136 | return t !== undefined ? t : type;
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 | export const asInt = (...args) => args.map((x) => x | 0);
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | export const sizeOf = (type) => SIZEOF[type] || SIZEOF[asNativeType(type)];
|
148 | export function typedArray(type, ...xs) {
|
149 | const ctor = BIGINT_ARRAY_CTORS[type];
|
150 | return new (ctor || TYPEDARRAY_CTORS[asNativeType(type)])(...xs);
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | export const typedArrayType = (x) => {
|
159 | if (Array.isArray(x))
|
160 | return "f64";
|
161 | for (let id in TYPEDARRAY_CTORS) {
|
162 | if (x instanceof TYPEDARRAY_CTORS[id])
|
163 | return id;
|
164 | }
|
165 | return "f64";
|
166 | };
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | export const uintTypeForSize = (x) => x <= 0x100 ? "u8" : x <= 0x10000 ? "u16" : "u32";
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | export const intTypeForSize = (x) => x >= -0x80 && x < 0x80 ? "i8" : x >= -0x8000 && x < 0x8000 ? "i16" : "i32";
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export const uintTypeForBits = (x) => x > 16 ? "u32" : x > 8 ? "u16" : "u8";
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | export const intTypeForBits = (x) => x > 16 ? "i32" : x > 8 ? "i16" : "i8";
|