-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresample-wrapper.js
211 lines (207 loc) · 8.02 KB
/
resample-wrapper.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Create js wrapper for simpler usage
*
* @param {import('./resample.d.ts').AnimationResampleInstance} instance
* @return {import('./resample.d.ts').AnimationResampleWrapper}
*/
export function makeWrapper(instance) {
// heapPtr is aligned with 16 bytes
const heapPtr = instance.exports.get_heap_ptr();
// Math.floor((memory.buffer.byteLength - heapPtr) / 4);
const availableSize = (instance.exports.memory.buffer.byteLength - heapPtr) >> 2;
const memory = new Float32Array(
instance.exports.memory.buffer,
heapPtr, availableSize
);
const epsilon = 1.1920928955078125e-07;
/**
* @param {number} offset
* @return {number}
*/
function wasmPtr(offset) {
return offset * 4 + heapPtr;
}
/**
*
* @param {import('./resample').TypedArray} frames
* @param {import('./resample').TypedArray} values
* @param {number} tolerance
* @param {number} elementSize
* @param {number?} normalize
* @param {import('./resample').ResampleFn} callWasm
*/
function resampleInternal(
frames, values,
tolerance, elementSize, normalize,
callWasm
) {
const chunkSize = (memory.length / (elementSize + 1)) | 0;
const valueChunk = elementSize * chunkSize;
const wasmFrameOffset = 0, wasmValueOffset = chunkSize;
const wasmFramePtr = wasmPtr(wasmFrameOffset),
wasmValuePtr = wasmPtr(wasmValueOffset);
let frameReadOffset = 0, frameWriteOffset = 0,
valueReadOffset = 0, valueWriteOffset = 0;
let lastWriteCount = 0;
let isFirstChunk = true;
while (frameReadOffset < frames.length) {
let currChunkSize = chunkSize;
let currValueChunkSize = valueChunk;
let offset = 0;
let currWasmFrameOffset = wasmFrameOffset;
let currWasmValueOffset = wasmValueOffset;
if (isFirstChunk) {
isFirstChunk = false;
} else {
offset = instance.exports.stream_continue(
wasmFramePtr, 1,
wasmValuePtr, elementSize, elementSize,
lastWriteCount
);
currChunkSize -= offset;
currValueChunkSize -= offset * elementSize;
currWasmFrameOffset += offset;
currWasmValueOffset += offset * elementSize;
frameWriteOffset -= offset;
valueWriteOffset -= offset * elementSize;
}
if (frameReadOffset + currChunkSize < frames.length) {
memory.set(
frames.subarray(
frameReadOffset,
frameReadOffset + currChunkSize),
currWasmFrameOffset);
memory.set(
values.subarray(
valueReadOffset,
valueReadOffset + currValueChunkSize),
currWasmValueOffset);
} else {
// last chunk
const lastChunkSize = frames.length - frameReadOffset;
memory.set(
frames.subarray(frameReadOffset, frameReadOffset + lastChunkSize),
currWasmFrameOffset);
memory.set(
values.subarray(
valueReadOffset,
valueReadOffset + (lastChunkSize * elementSize)),
currWasmValueOffset);
currChunkSize = lastChunkSize;
}
if (normalize && normalize !== 5126) {
instance.exports.denormalize(
wasmValuePtr,
elementSize,
elementSize,
currChunkSize + offset,
normalize
);
}
let writeCount = callWasm(
wasmFramePtr, 1,
wasmValuePtr, elementSize,
currChunkSize + offset, tolerance
);
if (frameWriteOffset !== frameReadOffset || writeCount !== currChunkSize) {
// copy only if needed
frames.set(
memory.subarray(
wasmFrameOffset,
wasmFrameOffset + writeCount),
frameWriteOffset);
}
if (normalize && normalize !== 5126) {
instance.exports.normalize(
wasmValuePtr,
elementSize,
elementSize,
currChunkSize + offset,
normalize
);
}
if (valueWriteOffset !== valueReadOffset || writeCount !== currChunkSize) {
// copy only if needed
values.set(
memory.subarray(
wasmValueOffset,
wasmValueOffset + (writeCount * elementSize)),
valueWriteOffset);
}
frameReadOffset += currChunkSize;
valueReadOffset += currValueChunkSize;
frameWriteOffset += writeCount;
valueWriteOffset += writeCount * elementSize;
lastWriteCount = writeCount;
}
return {
frames: frames.subarray(0, frameWriteOffset),
values: values.subarray(0, valueWriteOffset),
};
}
function resampleFunction(wasmFn, elementSize) {
/**
* @param {import('./resample').TypedArray} frames
* @param {import('./resample').TypedArray} values
* @param {number} tolerance
* @param {number?} normalize
* @return {{frames: import('./resample').TypedArray, values: import('./resample').TypedArray}}
*/
function resample(
frames, values,
tolerance, normalize
) {
if (!tolerance) tolerance = epsilon;
return resampleInternal(frames, values, tolerance, elementSize, normalize, (
frames, frame_stride,
values, value_stride,
count, tolerance
) => instance.exports[wasmFn](
frames, frame_stride,
values, value_stride,
count, tolerance
));
}
return resample;
}
function resampleUnknown(wasmFn) {
/**
* @param {Float32Array} frames
* @param {Float32Array} values
* @param {number} elementSize
* @param {number} tolerance
* @param {number?} normalize
* @return {{frames: Float32Array, values: Float32Array}}
*/
function resample(
frames, values,
elementSize, tolerance, normalize
) {
if (!tolerance) tolerance = epsilon;
return resampleInternal(frames, values, tolerance, elementSize, normalize, (
frames, frame_stride,
values, value_stride,
count, tolerance
) => instance.exports[wasmFn](
frames, frame_stride,
values, value_stride, value_stride,
count, tolerance
));
}
return resample;
}
return {
instance: instance,
lerp_unknown: resampleUnknown('lerp_unknown'),
slerp_quat: resampleFunction('slerp_quat', 4),
lerp_vec4: resampleFunction('lerp_vec4', 4),
lerp_vec3: resampleFunction('lerp_vec3', 3),
lerp_vec2: resampleFunction('lerp_vec2', 2),
lerp_scalar: resampleFunction('lerp_scalar', 1),
step_unknown: resampleUnknown('step_unknown'),
step_vec4: resampleFunction('step_vec4', 4),
step_vec3: resampleFunction('step_vec3', 3),
step_vec2: resampleFunction('step_vec2', 2),
step_scalar: resampleFunction('step_scalar', 1),
};
}