-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix4.html
200 lines (177 loc) · 7.02 KB
/
Matrix4.html
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
<script src="./tests/libs/test.js"></script>
<script src="./node_modules/gl-matrix-wasm/pkg/gl_matrix_wasm.js"></script>
<script src="./tests/libs/as-loader.js"></script>
<script type="module">
// import * as AsBind from './node_modules/as-bind/dist/as-bind.esm.js';
import { Matrix4 } from './node_modules/three/src/math/Matrix4.js';
const { test, benchmark, expect } = window.TEST;
const glMatrixWasm = window.glMatrixWasm;
loader
.instantiate(
fetch('./build/matrix.wasm').then(i => i.arrayBuffer()),
{ env: { memory: new WebAssembly.Memory({ initial: 1 }) } },
)
.then(res => {
console.log(res);
main(res.exports, res.instance);
});
function main(wasmExport, wasmInstance) {
const i32Arr = wasmExport.__getInt32ArrayView.bind(wasmExport);
const f32Arr = wasmExport.__getFloat32ArrayView.bind(wasmExport);
const u8Arr = wasmExport.__getUint8ArrayView.bind(wasmExport);
const sleep = t => new Promise(r => setTimeout(r, t));
const wasmInstanceExport = wasmInstance.exports;
console.log(wasmInstanceExport);
test('Matrix4_multiply', async () => {
await glMatrixWasm.init();
const m4LeftJs = new Matrix4();
const m4RightJs = new Matrix4();
const m4LeftWasm = new wasmExport.Matrix4();
const m4RightWasm = new wasmExport.Matrix4();
// prettier-ignore
const ms = [
glMatrixWasm.Matrix4.fromValues(7, 3, 6, 9, 2, 3, 2, 5, 1, 9, 5, 8, 3, 7, 2, 2),
glMatrixWasm.Matrix4.fromValues(2, 5, 7, 8, 4, 8, 3, 9, 2, 5, 4, 9, 5, 6, 3, 1),
glMatrixWasm.Matrix4.fromValues(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),
];
// prettier-ignore
const benchCfg = {
JS(i) {
// m4LeftJs.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
// m4RightJs.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4LeftJs.multiplyMatrices(m4LeftJs, m4RightJs);
},
WASM(i) {
// m4LeftWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
// m4RightWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4LeftWasm.multiplyMatrices_no_simd(m4LeftWasm, m4RightWasm);
},
WASM_SIMD(i) {
// m4LeftWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
// m4RightWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4LeftWasm.multiplyMatrices(m4LeftWasm, m4RightWasm);
},
WASM_SIMD_LOAD_F32() {
// m4LeftWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
// m4RightWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4LeftWasm.multiplyMatrices_load_f32(m4LeftWasm, m4RightWasm);
},
WASM_RUST() {
glMatrixWasm.Matrix4.multiply(ms[2], ms[0], ms[1])
},
// WASM_RAW_EXPORT() {
// wasmInstanceExport['Matrix4#multiply']()
// },
WASM_RAW_EXPORT_FN_SIMD() {
wasmInstanceExport.multiplyMatrices(m4LeftWasm.elements, m4RightWasm.elements, m4LeftWasm.elements,)
},
WASM_RAW_EXPORT_FN_NO_SIMD() {
wasmInstanceExport.multiplyMatrices_no_simd(m4LeftWasm.elements, m4RightWasm.elements, m4LeftWasm.elements,)
}
};
benchCfg.JS();
benchCfg.WASM();
expect(f32Arr(m4LeftWasm.elements)).toBe(m4LeftJs.elements);
benchCfg.WASM_SIMD();
expect(f32Arr(m4LeftWasm.elements)).toBe(m4LeftJs.elements);
// benchmark(benchCfg, 1);
// benchmark(benchCfg, 100);
// benchmark(benchCfg, 10000);
// benchmark(benchCfg, 5000);
// benchmark(benchCfg, 20000);
// benchmark(benchCfg, 27_000);
benchmark(benchCfg, 100_000);
// benchmark(benchCfg, 1000_000);
// for (let i = 0; i < 5; i++) {
// await sleep(500);
// benchmark(benchCfg, 10_000 / 5);
// }
});
test('Matrix4_determinant', async () => {
const m4LeftJs = new Matrix4();
const m4LeftWasm = new wasmExport.Matrix4();
const m4LeftWasmElements = f32Arr(m4LeftWasm.elements);
expect(m4LeftJs.determinant()).toBe(1);
expect(m4LeftWasm.determinant()).toBe(1);
expect(m4LeftWasm.determinant_opt()).toBe(1);
m4LeftJs.elements[0] = 2;
m4LeftWasmElements[0] = 2;
expect(m4LeftJs.determinant()).toBe(2);
expect(m4LeftWasm.determinant()).toBe(2);
expect(m4LeftWasm.determinant_opt()).toBe(2);
m4LeftJs.elements[0] = 0;
m4LeftWasmElements[0] = 0;
expect(m4LeftJs.determinant()).toBe(0);
expect(m4LeftWasm.determinant()).toBe(0);
expect(m4LeftWasm.determinant_opt()).toBe(0);
// prettier-ignore
m4LeftJs.set(2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12);
// prettier-ignore
m4LeftWasm.set(2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12);
expect(m4LeftJs.determinant()).toBe(76);
expect(m4LeftWasm.determinant()).toBe(76);
expect(m4LeftWasm.determinant_opt()).toBe(76);
// prettier-ignore
m4LeftJs.set(2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12);
// prettier-ignore
m4LeftWasm.set(2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12);
// prettier-ignore
m4LeftWasm.set(2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12);
const benchCfg = {
JS() {
m4LeftJs.determinant();
},
WASM() {
m4LeftWasm.determinant_no_simd();
},
WASM_OPT() {
m4LeftWasm.determinant_opt();
},
WASM_SIMD() {
m4LeftWasm.determinant();
},
};
// benchmark(benchCfg, 1);
// benchmark(benchCfg, 100);
// benchmark(benchCfg, 10000);
// benchmark(benchCfg, 5000);
// benchmark(benchCfg, 27_000);
benchmark(benchCfg, 100_000);
// benchmark(benchCfg, 200_000);
});
test('Matrix4_invert', async () => {
const m4LeftJs = new Matrix4();
const m4LeftWasm = new wasmExport.Matrix4();
const m4RightWasm = new wasmExport.Matrix4();
m4LeftJs.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4LeftWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
m4RightWasm.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
const benchCfg = {
JS() {
m4LeftJs.invert();
},
WASM() {
m4LeftWasm.invert_no_simd();
},
WASM_SIMD() {
m4LeftWasm.invert();
},
WASM_NO_LOCAL_VAR_CACHE() {
m4LeftWasm.invert_no_teN_cache();
},
};
m4LeftJs.invert();
m4LeftWasm.invert();
m4RightWasm.invert();
expect(f32Arr(m4LeftWasm.elements)).toBe(m4LeftJs.elements);
expect(f32Arr(m4RightWasm.elements)).toBe(m4LeftJs.elements);
// benchmark(benchCfg, 1);
// benchmark(benchCfg, 100);
// benchmark(benchCfg, 10000);
// benchmark(benchCfg, 5000);
// benchmark(benchCfg, 27_000);
benchmark(benchCfg, 100_000);
// benchmark(benchCfg, 1000_000);
});
}
</script>