recp / cglm

📽 Highly Optimized 2D / 3D Graphics Math (glm) for C
MIT License
2.32k stars 230 forks source link

How to generate wasm file. #320

Open 823639792 opened 1 year ago

823639792 commented 1 year ago

When execute command on Windows:

mkdir build 
cd build
emcmake cmake .. -DCMAKE_EXE_LINKER_FLAGS="-s STANDALONE_WASM" -DCGLM_STATIC=ON
ninja -C .

only generate libcglm.a file, no wasm and js file, so, how to generate wasm file?

823639792 commented 1 year ago

I want use it to three.js to improve compute speed.

recp commented 1 year ago

Hi @823639792

Sorry for the delay,

I’ll investigate this asap, libcglm.a can be used link to existing wasm executable but it seems you need wasm to use with js, an option can be added to generate wasm instead of arhive file, @myfreeer any feedback?

Thanks

myfreeer commented 1 year ago

For better performance, it is recommanded to use inline functions, which should be faster compared to linking to a library, where compilers generates extra function call overhead. Taking keyframe-resample-c as an example, code in C or C++ for your calucation logic, export functions to js, and write js wrapper (js glue), call it from js. In this case cglm headers are directly included, and functions callable from js are defined in Makefile using -Wl,--export linker flag. It is recommanded to code the main logic in C and export simple interface to js, and should not make too many calls to wasm from js. Even in modern browsers with jit, there is always overhead of js-to-wasm calls, so less calls lead to better and more predictable performance.

recp commented 1 year ago

@myfreeer many thanks for the point and benchmark. It seems wasm and simd version are similar ( more or less ), but may not reflect all operations due to auto vectorization maybe. I'll take a closer look asap.

myfreeer commented 1 year ago

@myfreeer many thanks for the point and benchmark. It seems wasm and simd version are similar ( more or less ), but may not reflect all operations due to auto vectorization maybe. I'll take a closer look asap.

The keyframe-resample-c project contains a mixed workload of lerp and slerp, the major performance bottleneck seems to be trigonometric functions in slerp, which uses non-simd functions from musl libc for precision. This issue contains an incomplete list of vectorized impl of trigonometric functions.

recp commented 1 year ago

@myfreeer Wow, thanks! It would be nice to improve and optimize slerp and any other trig functions in cglm, implementing simd here (slerp) is also in TODOs now.

For this issue, are there any benefits ( even a little ) to allow generate .wasm by an build option instead of library for some cases e.g. if someone want to use cglm in JS via wasm like @823639792?

823639792 commented 1 year ago

I made a test that calculates matrix multiplication 5000000 times. Some computers will bring some improvement, but some older computers, the operation will become slower. one computer: WASM: 67ms JS: 94.80000001192093ms another: WASM: 141.09999990463257ms JS: 131.40000009536743ms

It may indeed be necessary to reduce js calls to wasm in order to improve performance.