UNPKG

8.39 kBTypeScriptView Raw
1export type ArrayLikeIterable<T> = ArrayLike<T> & Iterable<T>;
2export type NumericArray = number[] | TypedArray;
3export type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
4export type BigTypedArray = BigInt64Array | BigUint64Array;
5export type FloatArray = Float32Array | Float64Array;
6export type IntArray = Int8Array | Int16Array | Int32Array;
7export type UIntArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
8export type FloatArrayConstructor = Float32ArrayConstructor | Float64ArrayConstructor;
9export type IntArrayConstructor = Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor;
10export type UIntArrayConstructor = Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
11export type BigIntArrayConstructor = BigInt64ArrayConstructor | BigUint64ArrayConstructor;
12export type TypedArrayConstructor = FloatArrayConstructor | IntArrayConstructor | UIntArrayConstructor;
13/**
14 * Type IDs for typed array backed buffers and generally describing binary data
15 * values.
16 *
17 * {@link GLType} {@link GL2TYPE} {@link TYPE2GL}
18 */
19export type Type = "u8" | "u8c" | "i8" | "u16" | "i16" | "u32" | "i32" | "f32" | "f64";
20export type BigType = "i64" | "u64";
21export type UintType = "u8" | "u8c" | "u16" | "u32";
22export type IntType = "i8" | "i16" | "i32";
23export type FloatType = "f32" | "f64";
24/**
25 * WebGL numeric type constants. Use {@link GL2TYPE} to convert, if needed.
26 *
27 * {@link Type}
28 * {@link GL2TYPE}
29 * {@link TYPE2GL}
30 */
31export declare enum GLType {
32 I8 = 5120,
33 U8 = 5121,
34 I16 = 5122,
35 U16 = 5123,
36 I32 = 5124,
37 U32 = 5125,
38 F32 = 5126
39}
40/**
41 * Conversion from {@link GLType} to {@link Type} enums.
42 */
43export declare const GL2TYPE: Record<GLType, Type>;
44/**
45 * Potentially lossy conversion from {@link Type} to {@link GLType} enums.
46 *
47 * Not all enums are mappable:
48 *
49 * - `F64` maps to `undefined`, since unsupported by WebGL
50 * - `U8C` maps to "u8"
51 */
52export declare const TYPE2GL: Record<Type, GLType | undefined>;
53/**
54 * Size information (in bytes) for {@link Type} and {@link BigType}. Also see
55 * {@link sizeOf}.
56 */
57export declare const SIZEOF: {
58 u8: number;
59 u8c: number;
60 i8: number;
61 u16: number;
62 i16: number;
63 u32: number;
64 i32: number;
65 i64: number;
66 u64: number;
67 f32: number;
68 f64: number;
69};
70/**
71 * Bit shift values to convert byte addresses into array indices for all
72 * {@link Type}s and {@link BigType}s.
73 */
74export declare const BIT_SHIFTS: {
75 i8: number;
76 u8: number;
77 u8c: number;
78 i16: number;
79 u16: number;
80 i32: number;
81 u32: number;
82 i64: number;
83 u64: number;
84 f32: number;
85 f64: number;
86};
87export declare const FLOAT_ARRAY_CTORS: Record<FloatType, FloatArrayConstructor>;
88export declare const INT_ARRAY_CTORS: Record<IntType, IntArrayConstructor>;
89export declare const UINT_ARRAY_CTORS: Record<UintType, UIntArrayConstructor>;
90export declare const BIGINT_ARRAY_CTORS: Record<BigType, BigIntArrayConstructor>;
91export declare const TYPEDARRAY_CTORS: Record<Type, TypedArrayConstructor>;
92export interface TypedArrayTypeMap extends Record<Type | GLType, TypedArray> {
93 u8: Uint8Array;
94 u8c: Uint8ClampedArray;
95 i8: Int8Array;
96 u16: Uint16Array;
97 i16: Int16Array;
98 u32: Uint32Array;
99 i32: Int32Array;
100 f32: Float32Array;
101 f64: Float64Array;
102 [GLType.U8]: Uint8Array;
103 [GLType.I8]: Int8Array;
104 [GLType.U16]: Uint16Array;
105 [GLType.I16]: Int16Array;
106 [GLType.U32]: Uint32Array;
107 [GLType.I32]: Int32Array;
108 [GLType.F32]: Float32Array;
109}
110export interface BigTypedArrayTypeMap extends Record<BigType, BigTypedArray> {
111 i64: BigInt64Array;
112 u64: BigUint64Array;
113}
114/**
115 * Returns canonical {@link Type} value of `type` by first
116 * attempting to resolve it as {@link GLType} enum.
117 *
118 * @example
119 * ```ts
120 * asNativeType(GLType.F32) => "f32"
121 * asNativeType("f32") => "f32"
122 * ```
123 *
124 * @param type -
125 */
126export declare const asNativeType: (type: GLType | Type) => Type;
127/**
128 * Returns suitable {@link GLType} enum of `type`.
129 *
130 * @example
131 * ```ts
132 * asGLType("f32") => GLType.F32
133 * asGLType(GLType.F32) => GLType.F32
134 * ```
135 *
136 * @param type -
137 */
138export declare const asGLType: (type: GLType | Type) => GLType;
139/**
140 * Coerces given numeric args to integer values.
141 */
142export declare const asInt: (...args: number[]) => number[];
143/**
144 * Returns byte size for given {@link Type} ID or {@link GLType} enum.
145 *
146 * @param type -
147 */
148export declare const sizeOf: (type: Type | BigType | GLType) => number;
149/**
150 * Constructs new typed array of given {@link Type}, {@link GLType} or
151 * {@link BigType}. Supports all arities of standard typed array ctors.
152 *
153 * @param type - array type enum
154 */
155export declare function typedArray<T extends Type | GLType>(type: T, length: number): TypedArrayTypeMap[T];
156export declare function typedArray<T extends Type | GLType>(type: T, src: ArrayLike<number> | ArrayBufferLike): TypedArrayTypeMap[T];
157export declare function typedArray<T extends Type | GLType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): TypedArrayTypeMap[T];
158export declare function typedArray<T extends BigType>(type: T, length: number): BigTypedArrayTypeMap[T];
159export declare function typedArray<T extends BigType>(type: T, src: ArrayLike<bigint> | ArrayBufferLike): BigTypedArrayTypeMap[T];
160export declare function typedArray<T extends BigType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): BigTypedArrayTypeMap[T];
161/**
162 * Takes an {@link NumericArray} and returns its corresponding {@link Type} ID.
163 * Standard JS arrays will default to {@link "f64"}.
164 *
165 * @param x -
166 */
167export declare const typedArrayType: (x: NumericArray) => Type;
168/**
169 * Returns the smallest possible *unsigned* int type enum for given `x`.
170 * E.g. if `x <= 256`, the function returns `"u8"`.
171 *
172 * @param x - value to classify
173 */
174export declare const uintTypeForSize: (x: number) => UintType;
175/**
176 * Returns the smallest possible *signed* int type enum for given `x`.
177 * E.g. if `x >= -128 && x < 128`, the function returns `"i8"`.
178 *
179 * @param x - value to classify
180 */
181export declare const intTypeForSize: (x: number) => IntType;
182/**
183 * Returns suitable {@link UintType} for given bit size (`[0,32]` range)
184 *
185 * @param x -
186 */
187export declare const uintTypeForBits: (x: number) => UintType;
188/**
189 * Returns suitable {@link IntType} for given bit size (`[0,32]` range)
190 *
191 * @param x -
192 */
193export declare const intTypeForBits: (x: number) => IntType;
194/**
195 * Returns the next smaller {@link IntType} for given type (or the same type if
196 * already the narrowest).
197 *
198 * @param t
199 */
200export declare const narrowInt: (t: IntType | "i64") => "i32" | "i8" | "i16";
201/**
202 * Returns the next larger {@link IntType} for given type (or the same type if
203 * already the widest).
204 *
205 * @param t
206 */
207export declare const widenInt: (t: IntType) => "i32" | "i64" | "i16";
208/**
209 * Returns the next smaller {@link UintType} for given type (or the same type if
210 * already the narrowest).
211 *
212 * @remarks
213 * If type is `u8c`, returns `u8`.
214 *
215 * @param t
216 */
217export declare const narrowUint: (t: UintType | "u64") => "u8" | "u16" | "u32";
218/**
219 * Returns the next larger {@link UintType} for given type (or the same type if
220 * already the widest).
221 *
222 * @param t
223 */
224export declare const widenUint: (t: UintType) => "u16" | "u32" | "u64";
225/**
226 * Returns the next smaller {@link FloatType} for given type (or the same type
227 * if already the narrowest).
228 *
229 * @param t
230 */
231export declare const narrowFloat: (t: FloatType) => string;
232/**
233 * Returns the next larger {@link FloatType} for given type (or the same type if
234 * already the widest).
235 *
236 * @param t
237 */
238export declare const widenFloat: (t: FloatType) => string;
239/**
240 * Returns the next smaller type (i.e. {@link IntType}, {@link UintType} or
241 * {@link FloatType}) for given type (or the same type if already the smallest).
242 *
243 * @param t
244 */
245export declare const narrowType: (t: Type | BigType) => string;
246/**
247 * Returns the next larger type (i.e. {@link IntType}, {@link UintType} or
248 * {@link FloatType}) for given type (or the same type if already the widest).
249 *
250 * @param t
251 */
252export declare const widenType: (t: Type | BigType) => string;
253//# sourceMappingURL=typedarray.d.ts.map
\No newline at end of file