@daneren2005/shared-memory-objects 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -4
- package/src/__tests__/memory-heap.spec.ts +92 -0
- package/src/__tests__/serialize-object.spec.ts +23 -0
- package/src/__tests__/shared-data.bench.ts +166 -0
- package/src/__tests__/shared-list.spec.ts +247 -0
- package/src/__tests__/shared-map.spec.ts +98 -0
- package/src/__tests__/shared-string.spec.ts +75 -0
- package/src/__tests__/shared-vector.spec.ts +163 -0
- package/src/allocated-memory.ts +84 -0
- package/src/interfaces/pow2.ts +3 -0
- package/src/interfaces/typed-array-constructor.ts +6 -0
- package/src/interfaces/typed-array.ts +1 -0
- package/src/lock/read-write-lock.ts +41 -0
- package/src/lock/simple-lock.ts +21 -0
- package/src/main.ts +35 -0
- package/src/memory-buffer.ts +666 -0
- package/src/memory-heap.ts +149 -0
- package/src/serialize-object.ts +95 -0
- package/src/shared-list.ts +274 -0
- package/src/shared-map.ts +252 -0
- package/src/shared-pointer-list.ts +80 -0
- package/src/shared-string.ts +140 -0
- package/src/shared-vector.ts +236 -0
- package/src/utils/16-from-32-array.ts +23 -0
- package/src/utils/16-from-64-array.ts +18 -0
- package/src/utils/__tests__/pointer.spec.ts +18 -0
- package/src/utils/float32-atomics.ts +24 -0
- package/src/utils/pointer.ts +40 -0
- package/src/utils/typedarray.d.ts +279 -0
- package/src/utils/typedarray.js +162 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const buffer = new ArrayBuffer(4);
|
|
2
|
+
const uint32Array = new Uint32Array(buffer);
|
|
3
|
+
const uint16Array = new Uint16Array(buffer);
|
|
4
|
+
|
|
5
|
+
export function load16From32(data: Uint32Array, index: number): [number, number] {
|
|
6
|
+
return convert32To16(Atomics.load(data, index));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function store16In32(data: Uint32Array, index: number, value1: number, value2: number) {
|
|
10
|
+
Atomics.store(data, index, convert16To32(value1, value2));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function convert32To16(value: number): [number, number] {
|
|
14
|
+
uint32Array[0] = value;
|
|
15
|
+
|
|
16
|
+
return [uint16Array[0], uint16Array[1]];
|
|
17
|
+
}
|
|
18
|
+
export function convert16To32(value1: number, value2: number): number {
|
|
19
|
+
uint16Array[0] = value1;
|
|
20
|
+
uint16Array[1] = value2;
|
|
21
|
+
|
|
22
|
+
return uint32Array[0];
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const buffer = new ArrayBuffer(8);
|
|
2
|
+
const uint64Array = new BigUint64Array(buffer);
|
|
3
|
+
const uint16Array = new Uint16Array(buffer);
|
|
4
|
+
|
|
5
|
+
export function load16From64(data: BigUint64Array, index: number): [number, number, number, number] {
|
|
6
|
+
uint64Array[0] = Atomics.load(data, index);
|
|
7
|
+
|
|
8
|
+
return [uint16Array[0], uint16Array[1], uint16Array[2], uint16Array[3]];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function store16In64(data: BigUint64Array, index: number, value1: number, value2: number, value3: number, value4: number = 0) {
|
|
12
|
+
uint16Array[0] = value1;
|
|
13
|
+
uint16Array[1] = value2;
|
|
14
|
+
uint16Array[2] = value3;
|
|
15
|
+
uint16Array[3] = value4;
|
|
16
|
+
|
|
17
|
+
Atomics.store(data, index, uint64Array[0]);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MAX_BYTE_OFFSET_LENGTH, MAX_POSITION_LENGTH, createPointer, getPointer } from '../pointer';
|
|
2
|
+
|
|
3
|
+
describe('pointer', () => {
|
|
4
|
+
it('check all numbers', () => {
|
|
5
|
+
const SKIP_POSITIONS = 10;
|
|
6
|
+
const SKIP_BYTE_OFFSETS = 10;
|
|
7
|
+
|
|
8
|
+
for(let position = 0; position < MAX_POSITION_LENGTH; position += SKIP_POSITIONS) {
|
|
9
|
+
for(let byteOffset = 1; byteOffset < MAX_BYTE_OFFSET_LENGTH; byteOffset *= SKIP_BYTE_OFFSETS) {
|
|
10
|
+
let pointer = createPointer(position, byteOffset);
|
|
11
|
+
|
|
12
|
+
let { bufferPosition, bufferByteOffset } = getPointer(pointer);
|
|
13
|
+
expect(bufferPosition).toEqual(position);
|
|
14
|
+
expect(bufferByteOffset).toEqual(byteOffset);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// For doing Atomic operations on floats in SharedArrayBuffers
|
|
2
|
+
const buffer = new ArrayBuffer(4);
|
|
3
|
+
const float32 = new Float32Array(buffer);
|
|
4
|
+
const int32 = new Int32Array(buffer);
|
|
5
|
+
|
|
6
|
+
function loadFloat32(data: Int32Array, index: number) {
|
|
7
|
+
return convertInt32ToFloat32(Atomics.load(data, index));
|
|
8
|
+
}
|
|
9
|
+
function storeFloat32(data: Int32Array, index: number, value: number) {
|
|
10
|
+
Atomics.store(data, index, convertFloat32ToInt32(value));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function convertInt32ToFloat32(value: number) {
|
|
14
|
+
int32[0] = value;
|
|
15
|
+
|
|
16
|
+
return float32[0];
|
|
17
|
+
}
|
|
18
|
+
function convertFloat32ToInt32(value: number) {
|
|
19
|
+
float32[0] = value;
|
|
20
|
+
|
|
21
|
+
return int32[0];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { loadFloat32, storeFloat32, convertInt32ToFloat32, convertFloat32ToInt32 };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// bottom 12 bits (4096) for bufferPosition
|
|
2
|
+
// top 20 bits (1MB) for bufferByteOffset
|
|
3
|
+
const BYTE_OFFSET_BIT_COUNT = 20;
|
|
4
|
+
const POSITION_BIT_COUNT = 32 - BYTE_OFFSET_BIT_COUNT;
|
|
5
|
+
const MAX_BYTE_OFFSET_LENGTH = Math.pow(2, BYTE_OFFSET_BIT_COUNT);
|
|
6
|
+
const MAX_POSITION_LENGTH = Math.pow(2, POSITION_BIT_COUNT);
|
|
7
|
+
|
|
8
|
+
export function loadPointer(data: Uint32Array, index: number = 0) {
|
|
9
|
+
return getPointer(Atomics.load(data, index));
|
|
10
|
+
}
|
|
11
|
+
export function loadRawPointer(data: Uint32Array, index: number = 0) {
|
|
12
|
+
return Atomics.load(data, index);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function storePointer(data: Uint32Array, index: number = 0, bufferPosition: number, bufferByteOffset: number) {
|
|
16
|
+
Atomics.store(data, index, createPointer(bufferPosition, bufferByteOffset));
|
|
17
|
+
}
|
|
18
|
+
export function storeRawPointer(data: Uint32Array, index: number = 0, pointer: number) {
|
|
19
|
+
Atomics.store(data, index, pointer);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function replacePointer(data: Uint32Array, index: number, newBufferPosition: number, newBufferByteOffset: number, oldBufferPosition: number, oldBufferByteOffset: number) {
|
|
23
|
+
let oldPointer = createPointer(oldBufferPosition, oldBufferByteOffset);
|
|
24
|
+
return Atomics.compareExchange(data, index, oldPointer, createPointer(newBufferPosition, newBufferByteOffset)) === oldPointer;
|
|
25
|
+
}
|
|
26
|
+
export function replaceRawPointer(data: Uint32Array, index: number, newPointer: number, oldPointer: number): boolean {
|
|
27
|
+
return Atomics.compareExchange(data, index, oldPointer, newPointer) === oldPointer;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getPointer(value: number) {
|
|
31
|
+
return {
|
|
32
|
+
bufferPosition: value & 0b00000000000000000000111111111111,
|
|
33
|
+
bufferByteOffset: value >>> POSITION_BIT_COUNT
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function createPointer(bufferPosition: number, bufferByteOffset: number) {
|
|
37
|
+
return bufferPosition + (bufferByteOffset << POSITION_BIT_COUNT);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { BYTE_OFFSET_BIT_COUNT, POSITION_BIT_COUNT, MAX_BYTE_OFFSET_LENGTH, MAX_POSITION_LENGTH };
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
export type ArrayLikeIterable<T> = ArrayLike<T> & Iterable<T>;
|
|
2
|
+
export type NumericArray = number[] | TypedArray;
|
|
3
|
+
export type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
|
|
4
|
+
export type BigTypedArray = BigInt64Array | BigUint64Array;
|
|
5
|
+
export type FloatArray = Float32Array | Float64Array;
|
|
6
|
+
export type IntArray = Int8Array | Int16Array | Int32Array;
|
|
7
|
+
export type UIntArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
|
|
8
|
+
export type FloatArrayConstructor = Float32ArrayConstructor | Float64ArrayConstructor;
|
|
9
|
+
export type IntArrayConstructor = Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor;
|
|
10
|
+
export type UIntArrayConstructor = Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
|
|
11
|
+
export type BigIntArrayConstructor = BigInt64ArrayConstructor | BigUint64ArrayConstructor;
|
|
12
|
+
export 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
|
+
*/
|
|
19
|
+
export type Type = 'u8' | 'u8c' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'f32' | 'f64';
|
|
20
|
+
export type BigType = 'i64' | 'u64';
|
|
21
|
+
export type UintType = 'u8' | 'u8c' | 'u16' | 'u32';
|
|
22
|
+
export type IntType = 'i8' | 'i16' | 'i32';
|
|
23
|
+
export 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
|
+
*/
|
|
31
|
+
export 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
|
+
*/
|
|
43
|
+
export 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
|
+
*/
|
|
52
|
+
export 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
|
+
*/
|
|
57
|
+
export 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
|
+
*/
|
|
74
|
+
export 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
|
+
};
|
|
87
|
+
export declare const FLOAT_ARRAY_CTORS: Record<FloatType, FloatArrayConstructor>;
|
|
88
|
+
export declare const INT_ARRAY_CTORS: Record<IntType, IntArrayConstructor>;
|
|
89
|
+
export declare const UINT_ARRAY_CTORS: Record<UintType, UIntArrayConstructor>;
|
|
90
|
+
export declare const BIGINT_ARRAY_CTORS: Record<BigType, BigIntArrayConstructor>;
|
|
91
|
+
export declare const TYPEDARRAY_CTORS: Record<Type, TypedArrayConstructor>;
|
|
92
|
+
export 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
|
+
}
|
|
110
|
+
export 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
|
+
*/
|
|
126
|
+
export 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
|
+
*/
|
|
138
|
+
export declare const asGLType: (type: GLType | Type) => GLType;
|
|
139
|
+
/**
|
|
140
|
+
* Coerces given numeric args to integer values.
|
|
141
|
+
*/
|
|
142
|
+
export 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
|
+
*/
|
|
148
|
+
export 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
|
+
*/
|
|
155
|
+
export declare function typedArray<T extends Type | GLType>(type: T, length: number): TypedArrayTypeMap[T];
|
|
156
|
+
export declare function typedArray<T extends Type | GLType>(type: T, src: ArrayLike<number> | ArrayBufferLike): TypedArrayTypeMap[T];
|
|
157
|
+
export declare function typedArray<T extends Type | GLType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): TypedArrayTypeMap[T];
|
|
158
|
+
export declare function typedArray<T extends BigType>(type: T, length: number): BigTypedArrayTypeMap[T];
|
|
159
|
+
export declare function typedArray<T extends BigType>(type: T, src: ArrayLike<bigint> | ArrayBufferLike): BigTypedArrayTypeMap[T];
|
|
160
|
+
export declare function typedArray<T extends BigType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): BigTypedArrayTypeMap[T];
|
|
161
|
+
/**
|
|
162
|
+
* Constructs a typed array for given `type` and populates it with given vector
|
|
163
|
+
* values.
|
|
164
|
+
*
|
|
165
|
+
* @remarks
|
|
166
|
+
* The size of the array will be `data.length * stride`, where `stride` is the
|
|
167
|
+
* number of elements per item and defaulting to the size of the first data
|
|
168
|
+
* item/vector given.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* // inferred stride=2 (2d vectors)
|
|
173
|
+
* typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]]);
|
|
174
|
+
* // Float32Array(6) [ 1, 2, 3, 4, -10, 20 ]
|
|
175
|
+
*
|
|
176
|
+
* // with custom stride=4
|
|
177
|
+
* typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]], 4);
|
|
178
|
+
* // Float32Array(12) [ 1, 2, 0, 0, 3,4, 0, 0, -10, 20, 0, 0 ]
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @param type
|
|
182
|
+
* @param data
|
|
183
|
+
* @param stride
|
|
184
|
+
*/
|
|
185
|
+
export declare function typedArrayOfVec<T extends Type | GLType>(type: T, data: Iterable<ArrayLike<number>>, stride?: number): TypedArrayTypeMap[T];
|
|
186
|
+
export declare function typedArrayOfVec<T extends BigType>(type: T, data: Iterable<ArrayLike<bigint>>, stride?: number): BigTypedArrayTypeMap[T];
|
|
187
|
+
/**
|
|
188
|
+
* Takes an {@link NumericArray} and returns its corresponding {@link Type} ID.
|
|
189
|
+
* Standard JS arrays will default to {@link "f64"}.
|
|
190
|
+
*
|
|
191
|
+
* @param x -
|
|
192
|
+
*/
|
|
193
|
+
export declare const typedArrayType: (x: NumericArray) => Type;
|
|
194
|
+
/**
|
|
195
|
+
* Returns the smallest possible *unsigned* int type enum for given `x`.
|
|
196
|
+
* E.g. if `x <= 256`, the function returns `"u8"`.
|
|
197
|
+
*
|
|
198
|
+
* @param x - value to classify
|
|
199
|
+
*/
|
|
200
|
+
export declare const uintTypeForSize: (x: number) => UintType;
|
|
201
|
+
/**
|
|
202
|
+
* Returns the smallest possible *signed* int type enum for given `x`.
|
|
203
|
+
* E.g. if `x >= -128 && x < 128`, the function returns `"i8"`.
|
|
204
|
+
*
|
|
205
|
+
* @param x - value to classify
|
|
206
|
+
*/
|
|
207
|
+
export declare const intTypeForSize: (x: number) => IntType;
|
|
208
|
+
/**
|
|
209
|
+
* Returns suitable {@link UintType} for given bit size (`[0,32]` range)
|
|
210
|
+
*
|
|
211
|
+
* @param x -
|
|
212
|
+
*/
|
|
213
|
+
export declare const uintTypeForBits: (x: number) => UintType;
|
|
214
|
+
/**
|
|
215
|
+
* Returns suitable {@link IntType} for given bit size (`[0,32]` range)
|
|
216
|
+
*
|
|
217
|
+
* @param x -
|
|
218
|
+
*/
|
|
219
|
+
export declare const intTypeForBits: (x: number) => IntType;
|
|
220
|
+
/**
|
|
221
|
+
* Returns the next smaller {@link IntType} for given type (or the same type if
|
|
222
|
+
* already the narrowest).
|
|
223
|
+
*
|
|
224
|
+
* @param t
|
|
225
|
+
*/
|
|
226
|
+
export declare const narrowInt: (t: IntType | 'i64') => 'i8' | 'i16' | 'i32';
|
|
227
|
+
/**
|
|
228
|
+
* Returns the next larger {@link IntType} for given type (or the same type if
|
|
229
|
+
* already the widest).
|
|
230
|
+
*
|
|
231
|
+
* @param t
|
|
232
|
+
*/
|
|
233
|
+
export declare const widenInt: (t: IntType) => 'i16' | 'i32' | 'i64';
|
|
234
|
+
/**
|
|
235
|
+
* Returns the next smaller {@link UintType} for given type (or the same type if
|
|
236
|
+
* already the narrowest).
|
|
237
|
+
*
|
|
238
|
+
* @remarks
|
|
239
|
+
* If type is `u8c`, returns `u8`.
|
|
240
|
+
*
|
|
241
|
+
* @param t
|
|
242
|
+
*/
|
|
243
|
+
export declare const narrowUint: (t: UintType | 'u64') => 'u8' | 'u16' | 'u32';
|
|
244
|
+
/**
|
|
245
|
+
* Returns the next larger {@link UintType} for given type (or the same type if
|
|
246
|
+
* already the widest).
|
|
247
|
+
*
|
|
248
|
+
* @param t
|
|
249
|
+
*/
|
|
250
|
+
export declare const widenUint: (t: UintType) => 'u16' | 'u32' | 'u64';
|
|
251
|
+
/**
|
|
252
|
+
* Returns the next smaller {@link FloatType} for given type (or the same type
|
|
253
|
+
* if already the narrowest).
|
|
254
|
+
*
|
|
255
|
+
* @param t
|
|
256
|
+
*/
|
|
257
|
+
export declare const narrowFloat: (t: FloatType) => string;
|
|
258
|
+
/**
|
|
259
|
+
* Returns the next larger {@link FloatType} for given type (or the same type if
|
|
260
|
+
* already the widest).
|
|
261
|
+
*
|
|
262
|
+
* @param t
|
|
263
|
+
*/
|
|
264
|
+
export declare const widenFloat: (t: FloatType) => string;
|
|
265
|
+
/**
|
|
266
|
+
* Returns the next smaller type (i.e. {@link IntType}, {@link UintType} or
|
|
267
|
+
* {@link FloatType}) for given type (or the same type if already the smallest).
|
|
268
|
+
*
|
|
269
|
+
* @param t
|
|
270
|
+
*/
|
|
271
|
+
export declare const narrowType: (t: Type | BigType) => string;
|
|
272
|
+
/**
|
|
273
|
+
* Returns the next larger type (i.e. {@link IntType}, {@link UintType} or
|
|
274
|
+
* {@link FloatType}) for given type (or the same type if already the widest).
|
|
275
|
+
*
|
|
276
|
+
* @param t
|
|
277
|
+
*/
|
|
278
|
+
export declare const widenType: (t: Type | BigType) => string;
|
|
279
|
+
//# sourceMappingURL=typedarray.d.ts.map
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// Copied from @thi.ng/api/typedarray
|
|
2
|
+
// TODO: rewrite without this but for now just forked in order to add Atomics to base library
|
|
3
|
+
|
|
4
|
+
var GLType = /* @__PURE__ */ ((GLType2) => {
|
|
5
|
+
GLType2[GLType2['I8'] = 5120] = 'I8';
|
|
6
|
+
GLType2[GLType2['U8'] = 5121] = 'U8';
|
|
7
|
+
GLType2[GLType2['I16'] = 5122] = 'I16';
|
|
8
|
+
GLType2[GLType2['U16'] = 5123] = 'U16';
|
|
9
|
+
GLType2[GLType2['I32'] = 5124] = 'I32';
|
|
10
|
+
GLType2[GLType2['U32'] = 5125] = 'U32';
|
|
11
|
+
GLType2[GLType2['F32'] = 5126] = 'F32';
|
|
12
|
+
return GLType2;
|
|
13
|
+
})(GLType || {});
|
|
14
|
+
const GL2TYPE = {
|
|
15
|
+
[5120 /* I8 */]: 'i8',
|
|
16
|
+
[5121 /* U8 */]: 'u8',
|
|
17
|
+
[5122 /* I16 */]: 'i16',
|
|
18
|
+
[5123 /* U16 */]: 'u16',
|
|
19
|
+
[5124 /* I32 */]: 'i32',
|
|
20
|
+
[5125 /* U32 */]: 'u32',
|
|
21
|
+
[5126 /* F32 */]: 'f32'
|
|
22
|
+
};
|
|
23
|
+
const TYPE2GL = {
|
|
24
|
+
i8: 5120 /* I8 */,
|
|
25
|
+
u8: 5121 /* U8 */,
|
|
26
|
+
u8c: 5121 /* U8 */,
|
|
27
|
+
i16: 5122 /* I16 */,
|
|
28
|
+
u16: 5123 /* U16 */,
|
|
29
|
+
i32: 5124 /* I32 */,
|
|
30
|
+
u32: 5125 /* U32 */,
|
|
31
|
+
f32: 5126 /* F32 */,
|
|
32
|
+
f64: void 0
|
|
33
|
+
};
|
|
34
|
+
const SIZEOF = {
|
|
35
|
+
u8: 1,
|
|
36
|
+
u8c: 1,
|
|
37
|
+
i8: 1,
|
|
38
|
+
u16: 2,
|
|
39
|
+
i16: 2,
|
|
40
|
+
u32: 4,
|
|
41
|
+
i32: 4,
|
|
42
|
+
i64: 8,
|
|
43
|
+
u64: 8,
|
|
44
|
+
f32: 4,
|
|
45
|
+
f64: 8
|
|
46
|
+
};
|
|
47
|
+
const BIT_SHIFTS = {
|
|
48
|
+
i8: 0,
|
|
49
|
+
u8: 0,
|
|
50
|
+
u8c: 0,
|
|
51
|
+
i16: 1,
|
|
52
|
+
u16: 1,
|
|
53
|
+
i32: 2,
|
|
54
|
+
u32: 2,
|
|
55
|
+
i64: 3,
|
|
56
|
+
u64: 3,
|
|
57
|
+
f32: 2,
|
|
58
|
+
f64: 3
|
|
59
|
+
};
|
|
60
|
+
const FLOAT_ARRAY_CTORS = {
|
|
61
|
+
f32: Float32Array,
|
|
62
|
+
f64: Float64Array
|
|
63
|
+
};
|
|
64
|
+
const INT_ARRAY_CTORS = {
|
|
65
|
+
i8: Int8Array,
|
|
66
|
+
i16: Int16Array,
|
|
67
|
+
i32: Int32Array
|
|
68
|
+
};
|
|
69
|
+
const UINT_ARRAY_CTORS = {
|
|
70
|
+
u8: Uint8Array,
|
|
71
|
+
u8c: Uint8ClampedArray,
|
|
72
|
+
u16: Uint16Array,
|
|
73
|
+
u32: Uint32Array
|
|
74
|
+
};
|
|
75
|
+
const BIGINT_ARRAY_CTORS = {
|
|
76
|
+
// eslint-disable-next-line
|
|
77
|
+
i64: BigInt64Array,
|
|
78
|
+
// eslint-disable-next-line
|
|
79
|
+
u64: BigUint64Array
|
|
80
|
+
};
|
|
81
|
+
const TYPEDARRAY_CTORS = {
|
|
82
|
+
...FLOAT_ARRAY_CTORS,
|
|
83
|
+
...INT_ARRAY_CTORS,
|
|
84
|
+
...UINT_ARRAY_CTORS
|
|
85
|
+
};
|
|
86
|
+
const asNativeType = (type) => {
|
|
87
|
+
const t = GL2TYPE[type];
|
|
88
|
+
return t !== void 0 ? t : type;
|
|
89
|
+
};
|
|
90
|
+
const asGLType = (type) => {
|
|
91
|
+
const t = TYPE2GL[type];
|
|
92
|
+
return t !== void 0 ? t : type;
|
|
93
|
+
};
|
|
94
|
+
const asInt = (...args) => args.map((x) => x | 0);
|
|
95
|
+
const sizeOf = (type) => SIZEOF[type] || SIZEOF[asNativeType(type)];
|
|
96
|
+
function typedArray(type, ...xs) {
|
|
97
|
+
const ctor = BIGINT_ARRAY_CTORS[type];
|
|
98
|
+
return new (ctor || TYPEDARRAY_CTORS[asNativeType(type)])(...xs);
|
|
99
|
+
}
|
|
100
|
+
function typedArrayOfVec(type, data, stride) {
|
|
101
|
+
const $data = Array.isArray(data) ? data : [...data];
|
|
102
|
+
if(stride === void 0)
|
|
103
|
+
stride = $data[0].length;
|
|
104
|
+
const num = $data.length;
|
|
105
|
+
const res = typedArray(type, num * stride);
|
|
106
|
+
for(let i = 0, j = 0; i < num; i++, j += stride) {
|
|
107
|
+
res.set($data[i], j);
|
|
108
|
+
}
|
|
109
|
+
return res;
|
|
110
|
+
}
|
|
111
|
+
const typedArrayType = (x) => {
|
|
112
|
+
if(Array.isArray(x))
|
|
113
|
+
return 'f64';
|
|
114
|
+
for(let id in TYPEDARRAY_CTORS) {
|
|
115
|
+
if(x instanceof TYPEDARRAY_CTORS[id])
|
|
116
|
+
return id;
|
|
117
|
+
}
|
|
118
|
+
return 'f64';
|
|
119
|
+
};
|
|
120
|
+
const uintTypeForSize = (x) => x <= 256 ? 'u8' : x <= 65536 ? 'u16' : 'u32';
|
|
121
|
+
const intTypeForSize = (x) => x >= -128 && x < 128 ? 'i8' : x >= -32768 && x < 32768 ? 'i16' : 'i32';
|
|
122
|
+
const uintTypeForBits = (x) => x > 16 ? 'u32' : x > 8 ? 'u16' : 'u8';
|
|
123
|
+
const intTypeForBits = (x) => x > 16 ? 'i32' : x > 8 ? 'i16' : 'i8';
|
|
124
|
+
const narrowInt = (t) => t === 'i64' ? 'i32' : t === 'i32' ? 'i16' : t === 'i16' ? 'i8' : 'i8';
|
|
125
|
+
const widenInt = (t) => t === 'i8' ? 'i16' : t === 'i16' ? 'i32' : t === 'i32' ? 'i64' : 'i64';
|
|
126
|
+
const narrowUint = (t) => t === 'u64' ? 'u32' : t === 'u32' ? 'u16' : t === 'u16' ? 'u8' : 'u8';
|
|
127
|
+
const widenUint = (t) => t === 'u8' || t === 'u8c' ? 'u16' : t === 'u16' ? 'u32' : t === 'u32' ? 'u64' : 'u64';
|
|
128
|
+
const narrowFloat = (t) => t === 'f64' ? 'f32' : 'f32';
|
|
129
|
+
const widenFloat = (t) => t === 'f32' ? 'f64' : 'f64';
|
|
130
|
+
const narrowType = (t) => t[0] === 'i' ? narrowInt(t) : t[0] === 'u' ? narrowUint(t) : narrowFloat(t);
|
|
131
|
+
const widenType = (t) => t[0] === 'i' ? widenInt(t) : t[0] === 'u' ? widenUint(t) : widenFloat(t);
|
|
132
|
+
export {
|
|
133
|
+
BIGINT_ARRAY_CTORS,
|
|
134
|
+
BIT_SHIFTS,
|
|
135
|
+
FLOAT_ARRAY_CTORS,
|
|
136
|
+
GL2TYPE,
|
|
137
|
+
GLType,
|
|
138
|
+
INT_ARRAY_CTORS,
|
|
139
|
+
SIZEOF,
|
|
140
|
+
TYPE2GL,
|
|
141
|
+
TYPEDARRAY_CTORS,
|
|
142
|
+
UINT_ARRAY_CTORS,
|
|
143
|
+
asGLType,
|
|
144
|
+
asInt,
|
|
145
|
+
asNativeType,
|
|
146
|
+
intTypeForBits,
|
|
147
|
+
intTypeForSize,
|
|
148
|
+
narrowFloat,
|
|
149
|
+
narrowInt,
|
|
150
|
+
narrowType,
|
|
151
|
+
narrowUint,
|
|
152
|
+
sizeOf,
|
|
153
|
+
typedArray,
|
|
154
|
+
typedArrayOfVec,
|
|
155
|
+
typedArrayType,
|
|
156
|
+
uintTypeForBits,
|
|
157
|
+
uintTypeForSize,
|
|
158
|
+
widenFloat,
|
|
159
|
+
widenInt,
|
|
160
|
+
widenType,
|
|
161
|
+
widenUint
|
|
162
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|