I’m able to load Havok WASM on the main thread using await HavokPhysics(). But, this throws an error inside a Web Worker:
RuntimeError: Aborted(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f @+0). Build with -sASSERTIONS for more info.
Could anyone please share if they were able to run Havok inside a Web Worker?
For additional context, I’m able to load Ammo WASM successfully on both the main thread and in a Web Worker
There’s a couple of difficulties if you’re trying to use Havok from multiple web workers simultaneously, but I think it should be possible to use from just one (though, I haven’t tried.)
The error you’re seeing is because WASM modules expect to begin with a special identifier (\0asm) which does not appear in yours - instead, it has <!DO which looks like you’re trying to instantiate a WASM with an HTML page, rather than the module data?
@eoin Does running importScripts(<PATH_TO_HavokPhysics_umd.js>) and await HavokPhysics() inside a Web Worker script mean that I’m trying to instantiate a WASM with an HTML page, rather than the module data? Could you please share more details on how I can instantiate a WASM with the module data instead?
my two cents - it is trying to download the wasm file, but it is 404-ing, so you get an html page. When initializing the havok module you can pass the URL from which the wasm will be downloaded. might be the way to go here
@regna I’m curious what your experience is with putting Havok into a web worker? I see Havok performance issues when generating large physics meshes, but presumably sending mesh info to a web worker will have some overhead. The querying, on the other hand, runs very performantly for me.
So I’m curious, how much benefit have you seen from running Havok in a web worker and what exactly were you optimizing for?
@jvanderdoes I’ve been slow on getting to this and will update back here after experimenting. I’m planning to first create a comparison of falling blocks with and without Web Worker using Ammo and Havok
I don’t know yet if a Web Worker will boost performance significantly for Havok, though I remember it did for Ammo
I also haven’t gotten to a point yet where I’ve tried generating large physics meshes. Have you seen if a SharedArrayBuffer can help here?
If OBS Studio is not recording, the FPS increases by 2-3X, and the physics step computation times decreases by a couple X too
In the Worker, physics step computations seem slightly slower, but the FPS improvement is worth it
Delta time appears to be struggling worse in the Worker, however this is not something to really worry about. It just so happens that the few additional ms per physics step computation in the Worker reached the threshold for the spiral of death. It’s like a Pythagoras cup where if you go just a bit over the edge, things fall behind and cannot recover. Per tick, if running behind on time, the physics is allowed to simulate a maximum of four steps (with timestep 16 ms each) in attempt to catch up to real time, but the Worker struggles to catch up due to a semi-spiral of death. If we add just a few more boxes, I’m sure we can get the Non-Worker to hit this spiral of death too