-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeometry-util.js
164 lines (149 loc) · 4.1 KB
/
geometry-util.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {align, getClosestPowerOf2} from './physics-util.js';
export class Allocator {
constructor(moduleInstance) {
this.moduleInstance = moduleInstance;
this.offsets = []
}
alloc(constructor, size) {
if (size > 0) {
const offset = this.moduleInstance._malloc(
size * constructor.BYTES_PER_ELEMENT
)
const b = new constructor(
this.moduleInstance.HEAP8.buffer,
this.moduleInstance.HEAP8.byteOffset + offset,
size
)
b.offset = offset
this.offsets.push(offset)
return b
} else {
return new constructor(this.moduleInstance.HEAP8.buffer, 0, 0)
}
}
freeAll() {
for (let i = 0; i < this.offsets.length; i++) {
this.moduleInstance._doFree(this.offsets[i])
}
this.offsets.length = 0
}
}
//
export class ScratchStack {
constructor(moduleInstance, size) {
this.ptr = moduleInstance._malloc(size)
this.u8 = new Uint8Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size
)
this.u16 = new Uint16Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 2
)
this.u32 = new Uint32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
this.i8 = new Int8Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size
)
this.i16 = new Int16Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 2
)
this.i32 = new Int32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
this.f32 = new Float32Array(
moduleInstance.HEAP8.buffer,
this.ptr,
size / 4
)
}
}
//
// circular index buffer
const maxSlotEntries = 4096;
export class FreeListArray {
constructor(slotSize, parent) {
this.slotSize = slotSize;
this.parent = parent;
this.startIndex = 0;
this.endIndex = 0;
this.entries = new Int32Array(maxSlotEntries);
this.allocatedEntries = 0;
}
alloc() {
if (this.allocatedEntries < maxSlotEntries) {
if (this.startIndex === this.endIndex) {
this.entries[this.endIndex] = this.parent.allocIndex(this.slotSize);
this.endIndex = (this.endIndex + 1) % maxSlotEntries;
}
const index = this.entries[this.startIndex];
this.startIndex = (this.startIndex + 1) % maxSlotEntries;
this.allocatedEntries++;
return index;
} else {
throw new Error('out of slots to allocate');
}
}
free(index) {
this.entries[this.endIndex] = index;
this.endIndex = (this.endIndex + 1) % maxSlotEntries;
this.allocatedEntries--;
}
}
export class FreeList {
constructor(size, alignment = 1) {
this.freeStart = 0;
this.freeEnd = size;
this.alignment = alignment;
this.slots = new Map(); // Map<slotSize, FreeListArray>
this.slotSizes = new Map(); // Map<index, slotSize>
}
allocIndex(slotSize) {
const allocSize = 1 << slotSize;
let newFreeStart = this.freeStart + allocSize;
newFreeStart = align(newFreeStart, this.alignment);
if (newFreeStart <= this.freeEnd) {
const index = this.freeStart;
this.freeStart = newFreeStart;
return index;
} else {
throw new Error('out of memory to allocate to slot');
}
}
alloc(size) {
const slotSize = getClosestPowerOf2(size);
let freeListArray = this.slots.get(slotSize);
if (freeListArray === undefined) {
freeListArray = new FreeListArray(slotSize, this);
this.slots.set(slotSize, freeListArray);
}
const index = freeListArray.alloc();
this.slotSizes.set(index, slotSize);
return index;
}
free(index) {
const slotSize = this.slotSizes.get(index);
if (slotSize !== undefined) {
const freeListArray = this.slots.get(slotSize);
if (freeListArray !== undefined) {
freeListArray.free(index);
this.slotSizes.delete(index);
} else {
throw new Error('invalid free slot');
}
} else {
throw new Error('invalid free index');
}
}
}