300 points by fluidmaster 5 months ago flag hide 12 comments
tomcraven 5 months ago next
This is a really cool project! I'm curious, how did you approach the memory management for the WebAssembly module? Any tips for optimizing performance?
jakeisenberg 5 months ago next
Hey tomcraven, thanks for your question! I used the <a href='https://github.com/WebAssembly/memory' target='_blank'>WebAssembly Memory API</a> for the memory management, and I found that keeping the WebAssembly linear memory aligned with the stack memory helped reduce GC pressure. In terms of performance, using <a href='https://github.com/WebAssembly/bulk-memory' target='_blank'>bulk memory operations</a> and pre-allocating memory for arrays made a big difference. Cheers!
captn 5 months ago prev next
I've been playing around with this demo and it's amazing how well the WebAssembly version performs compared to the native version! What kind of performance improvements are you seeing in the browser with WebAssembly?
konrad 5 months ago next
Thanks captn! I'm using the <a href='https://github.com/WebAssembly/wasm-obj-c' target='_blank'>WebAssembly Objective-C toolchain</a> to compile my C code to WebAssembly, and I'm seeing speedups of up to 2x compared to native on some of the simpler simulations, and even outperforming native on some of the more complex simulations. The compiled code has a slight overhead, but the performance advantages of running in the browser (parallel execution, efficient memory management) outweigh that overhead. Also, there are a bunch of optimizations that I haven't even started to look into yet, so there's still a lot of performance left to be gained!
netternet 5 months ago prev next
This is brilliant! I'd love to see a tutorial or example on simulating turbulent flows, so that I can use this for my research. Any chance for a write-up on more advanced fluid dynamics simulations?
ultron 5 months ago next
Netternet, I'm working on a turbulence simulation as we speak! I'll be sure to write a tutorial and post it here once I'm done. It's a bit more advanced than the other simulations, so I'll need some time to work out the bugs and optimize the performance. But I'm sure it will be worth the wait! :)
fknucker 5 months ago prev next
This is an impressive implementation! I'm wondering, are there any plans for support for real-time multithreading with WebAssembly?
arti 5 months ago next
fknucker, there are a few barriers to implementing multithreading in WebAssembly, but I'm hoping that with the recent updates to the WebAssembly spec and the addition of <a href='https://github.com/WebAssembly/threads' target='_blank'>WebAssembly threads</a>, we'll see support for real-time multithreading for WebAssembly in the browser. It's still early days, but I'm really excited about the possibilities!
kfer 5 months ago prev next
I'm blown away by this implementation! Are there any postprocessing techniques you're using for visualization? I'd love to be able to adapt some of these for a game that I'm developing.
mert 5 months ago next
kfer, I'm using some basic postprocessing techniques (such as blurring and shading) to make the simulations look smoother and more fluid. I'm glad to hear that you're interested in using this for a game -- I'd love to see what you come up with! In terms of the shaders, I'm using <a href='https://www.shadertoy.com' target='_blank'>GLSL shaders</a> , which are compatible with both the WebAssembly and WebGL version. Let me know if you have any specific questions!
recursive 5 months ago prev next
As a follow-up to this fantastic project, I'm curious if you've heard of any developments in WebAssembly and GPU acceleration?
blink 5 months ago next
Recursive, there have been some interesting developments in this area recently! The WebAssembly spec has been updated with <a href='https://github.com/WebAssembly/spec/blob/master/design/validation.md#artefact-id241' target='_blank'>ARTEFACT ID WebAssembly GPU binary format spec</a>, which defines a low-level GPU binary format that WebAssembly can use. It's still in the early stages, but it shows promise for better GPU acceleration in WebAssembly. I'm excited to see where this goes!