-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port to asm.js/webassembly, is it worth? #3248
Comments
The thing we thought about would be to only convert our hot path in wasm. Basically Matrix and Vector Maths as well as adding a Resources store to ensure they are all stored in the same buffer. |
Yes, math should be the first thing to convert.Anyway, It is also possible to convert the whole library in C++ then converting It back to JS using emscripten, but I am not sure about the performance increase... |
The main problem I am facing is that the engine is very slow when you got thousands of nodes (And meshes) |
The thing is with thousands of node and mesh wasm will still not be sufficient as after the issue would be around binding the info to webgl |
I already worked on some workarounds about that, If we can gain some perfs, that is good anyway.I am just wondering if I should give It a go. |
I was planning to do it soon :-) but not Before Jan I guess. |
If you need help, no probs, I am experienced with C/C++/JS and others. |
Give it a try if you wish I would be able to focus on other issues instead :-) |
@sebavan, As Babylon exists in TypeScript, I think that It is going to be easy to port It. There is a TS to Haxe converter, then Haxe to C++, do you think that we can give It a go?! |
you can for test pupose this might help bootstrapping but if it proves working we ll need to carrefully craft a blazing fast math lib :-) |
I know I know, I will create a repo for that this week end.I will keep you updated. |
@sebavan 3MB of JS to convert by hand to C/C++ is a pain :D |
Could you not only convert the Math part ??? (might be easier) |
@sebavan Which parts you want to be converted first? |
I was thinking in Babylon.math.ts: the Matrix, Vector and quaternion classes. This is our cpu hit on large scene (deep and wide hierarchy of nodes). Having a large native array fixed size and considering any of the Matrix/Vector... pointers to the array would prevent the expensive go and back from the wasm context and still allow all the contribution in TS/Javascript. We would like to collect data around this part to evaluate the gain before going further. Does it sound reasonable ? |
Right,let's try to put in place some benchmarks first.
Le 1 déc. 2017 09:16, "Sebastien Vandenberghe" <[email protected]> a
écrit :
… I was thinking in Babylon.math.ts: the Matrix, Vector and quaternion
classes. This is our cpu hit on large scene (deep and wide hierarchy of
nodes).
Having a large native array fixed size and considering any of the
Matrix/Vector... pointers to the array would prevent the expensive go and
back from the wasm context and still allow all the contribution in
TS/Javascript.
We would like to collect data around this part to evaluate the gain before
going further.
Does it sound reasonable ?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3248 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGr5r6RR9_QDHkCQbb6YyIHhmx_suCt2ks5s77X1gaJpZM4Qvaf2>
.
|
May I let here my feedback about asm/wasm regarding BJS as I made some studies and experiments ? First of all, is asm/wasm worth it ? It's really really faster than JS, especially when you have to deal with huge amount of data. That said, we have to know how asm/wasm works in the browser. Other issue : Now, let's imagine we have achieved to implement such a generic way to run asm/wasm with the right heap size, all the methods exposed to JS, the JS orchestration part rightly decoupled. Ok ? Imho, the best way to get real gain with asm/wasm would be that :
So the user would code, say, in TS (when compilable to wasm) or Haxe or whatever in the same language than the framework. I guess that's the way Unity exports the code to wasm. Another approach would be to implement then only some parts of the BJS current code, like some maths computations for instance. Not that simple in every case ... So there's ever this duality :
|
Anyway Simply because the JS JIT compiler can't now get far more faster, neither the CPU. |
Yup, no worries I will create an ugly scene full of cube all parented to a deep hierarchy with a cheap shader to really measure the isolate the cpu impact. I am really curious on the gain. My first experiments on maths vector in wasm sharing the buffer and only indexing on it were actually not too bad. |
I did a asm test (not published for now) about a turboSPS. What I would like to achieve is to migrate the 40K iterations in the asm code instead of calling it 40K times, because I did some very basic big loop tests and they are always fast faster in asm : Note : I used asm instead of wasm because it can be easily written by hand (no need for C, then intermediate compilation) and performances in FF are for now quite comparable to wasm. But Wasm keeps the way to go imho. |
BTW the project AssemblyScript / Next looks promising, but I'm afraid that the lack of contributors prevents it to get mature : current discussion : AssemblyScript/assemblyscript#1 tl;dr, a language the closest possible to TS and a compiler emitting directly wasm bytecode. |
Right, been playing around.The ts to hx option is not what we want at all, It is painful and not reliable. |
The math tools is having a really low code churn in BJS and is pretty stable so up to you for the templating or not. Color is almost never use in Math for operations cpu. float vs double is an interesting one :-) might be cool to at least alias the type and test both perf ? |
From my experience (as GIS engineer), double is a pain.As an obvious
example,how can you consider zero? Generally speaking zero is 10e-6... but
that is a point of view.
Le 2 déc. 2017 20:24, "sebavan" <[email protected]> a écrit :
… The math tools is having a really low code churn in BJS and is pretty
stable so up to you for the templating or not. Color is almost never use in
Math for operations cpu.
float vs double is an interesting one :-) might be cool to at least alias
the type and test both perf ?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3248 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGr5r9pVWt0AjknJBUBF4oZt1gnXvbqIks5s8aP1gaJpZM4Qvaf2>
.
|
Can't agree more. We should define a list like collisions or solid particle system |
https://hacks.mozilla.org/2018/10/calls-between-javascript-and-webassembly-are-finally-fast-🎉/ [X] spidermonkey Just need chrome & edge to follow suit and we could swap out even kinda small parts |
This starts to LOOK REALLY GOOD :) |
yep... things are goinf the right way now :-D |
as well, its a matter of time before assemblyscript is mature enough to more easily compile babylonjs, the more contributors +/ sponsors on that project, the better! |
Long writeup coming through. Sorry if it seemed like I dropped off. I'm pretty busy with my own BJS project :) @jbousquie I'm using assemblyscript itself to make the wasm. It's math only, and I'm focusing on the functions that I can figure out are called often/generate lotsa garbage. So I dropped what I did last time and went with a WASM "scratchpad" approach. Basically the idea was to compile a list of methods in WASM that does all the math and call those methods from JS. WASM will take in a batch of arguments, do the math and store it in WASM-JS-shared memory. Since JS is basically singlethreaded it's technically possible to just have JS tell WASM to crunch all the numbers and then copy off WASM's "scratchpad" where it stored all the answers. A lot of functions do relatively similar math. for example I have a function that just adds 3 pairs of numbers to each other. like this:
which can be used to add stuff for Color3s and Vector3s, like so:
becomes
Now it's sorta too early to celebrate. I wrote some tests testing this idea by running add using JS and add using WASM 10000000 times and the results are unsurprisingly disappointing cause we're using WASM to do simple stuff like a + b.
This is on Chrome: I don't know what the guys at Mozilla fed Firefox, but it must be really good. We need more of it! And that's for SIMPLE a+b!
vs
The numbers start getting closer: Now Firefox is just showing off. |
I also wrote a function that should be able to scan all the methods of a given class in Math and test which ones are the slowest by running each of them 10000000 times. This is a rough sneak peak for Color3, only functions that don't return a new Color3() since I'm pretty sure BJS doesn't do that; it generates new objects once and then adds/mults/maths InPlace, right? toString: 2111.466064453125ms I'll need to write a custom one for each class so it'll take some time to do properly. I have a pretty major project using babylonjs so I'm pretty vested in it becoming as fast as possible. Once we get this and AmmoJS in then the real fun begins. |
@jbousquie it looks like you found a similar if not better way than how I was doing it. I was doing:
and then just straight up reading values off readWasmMemAsF64 by index. Do you know how that compares to your way of using WebAssembly.instantiateStreaming? |
@vtange nothing to be sorry about, long but interesting, keep up the good work :) |
Interesting. I think you could get more benefits if switch from |
That's my experiment for porting mapbox's earcut triangulation algorithm to wasm and compare it with Rust version: mapbox/mapbox-gl-js#4835 (comment) |
Hey guys, any updates? |
Exciting stuff. This would solve some of the problems I've been having. Looking forward to seeing where the projects goes regarding wasm |
what kind of problems would it solve?
…On Thu, 18 Jul 2019 at 12:15, Captain Justin ***@***.***> wrote:
Exciting stuff. This would solve some of the problems I've been having.
Looking forward to seeing where the projects goes regarding wasm
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3248?email_source=notifications&email_token=AETXIDI6XFEVD6V5PNZXS33QAAGRJA5CNFSM4EF5U73KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2HPZGY#issuecomment-512687259>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AETXIDKBNSSKBJKDGRAJUXDQAAGRJANCNFSM4EF5U73A>
.
|
If I may add some comments about WASM. Here is what I have learned: a) Unless things have changed, you don't want big WASM code modules. They need to get compiled when the page loads. b) The interface between JavaScript and WASM has overhead. So, you want to do enough functionality in WASM to pay for it. c) You wanna pick functions that are unlikely to change much This means the best approach is a modular function with a lot of loops. I always started by rigorously separating the functionality so that the WASM call could be a drop-in for the JS call. This allows a gain comparison and a handy retreat point. My biggest winner was a function that did a lot of proximity calculations on a very large three-dimensional matrix, represented by a typed array. I got about 2.5x performance improvement. That was well worth it but the complexity price was high. Having done that, I had no interest in converting general purpose script to WASM. |
Sorry, I do have a different view to WASM, a more optimistic: a) I would not call it "compiled". No source code is downloaded, not even something readable like ASM.js but binary optimized virtual assembler code. It gets "transcoded" to the actual CPU assembly. And now, this is done while the download is ongoing. So almost no time is wasted. b) Yes , the interface is slow, yet, the next version will be remarkable faster. Still, you should not calc a square root by wasm, but do complex things. One candidate I could image is the physic engine: It needs the start condition once, may be some updates and cyclically it will "move" the objects only. And the actual Typescript code is not needed to rewritten in C++ because there should be a C++ version already. The next versions of WASM will have direct access to the DOM. So the whole Babylon could be WASM and only the Interface to Javascript. There will be garbage collection and a Typescript compiler too. So the Babylon code will be usable. At last I would like to have a full WASM version of Babylon including the API. So I could use WASM-Babylon with my C++ or Rust code in the browser. Is anyone working with WASM and Babylon? I may join, with my limited knowledge and time. p.s.: The Form may be a better place to discuss this topic. |
Is there still any interest to port Babylon from TypeScript to AssemblyScript? Three.js for instance is written in JavaScript, but glas (WebGL in AssemblyScript) is a port of |
We still have a big interest but we are also looking for an automated solution |
It is possible to port the code to AssemblyScript and to keep the project working in typescript |
What i mean is that we want to keep the root source code in TS |
But AssemblyScript is a TS dialect |
Yes but a lot of people rely on using TS and all its ecosystem. It has to be TS->assemblyscript-> webasm |
I did not try it, but assemblyscript is an even stronger typescript i64 instead of number And there are some odds! |
There is also the question of the support itself |
Closing for now. Will reopen if it becomes relevant |
Hello,
I am currently doing some experiments with asm.js/webassembly and I have to say that the perfs potential is great!
As I love babylon.js (Thanks guys!), I was thinking about porting It to asm.js/webassembly (Unity is actually generating some low level code for asm.js for optimization).
I am working on a viewer to display and interact with quite big models (Via GLTF/GLB).
Anyway, my question is : Do you think that It is worth the work?I am pretty sure that some of you guys thought about the same, and I am curious to know what do you think.
Looking forward to reading your messages.
Regards,
The text was updated successfully, but these errors were encountered: