Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multithreading and WebGL

Discussion in 'Web' started by ChristianKoGaMa, Jan 29, 2020.

  1. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    419
    @stonstad Now that we finally have the new Emscripten in 2021.2 and are working through all the issues that came with the new incremental builder, mono, and Emscripten; making sure multithreading is working again is one of our high priorities.
     
    LogicFlow, my1259, G33RT and 12 others like this.
  2. Raphael-PTC

    Raphael-PTC

    Joined:
    Oct 15, 2017
    Posts:
    1
    Hi.

    Is there an update on the progress towards multi-threading ?

    I'm interested on the support for both C/C++ multithreading and C# multithreading.

    Thx.
     
    OceanX000 likes this.
  3. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    I'm personally interested in just ScheduleParallel working with WebGL :) This news is amazing!
     
    Last edited: Aug 21, 2021
  4. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    24
    Is Mono going to be employed to output WebGL builds? I thought IL2CPP was needed to get from C# to C++ and then Emscripten would take it from there and output webassembly. Can IL2CPP be bypassed and Mono be used instead for WebGL?
     
    deus0 likes this.
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    You are correct, for WebGL, IL2CPP is still used. By mono here, I thin that @brendanduncan_u3d means the Mono base class libraries, which are used by both the Mono and IL2CPP scripting backends.
     
    deus0, Chris9465 and De-Panther like this.
  6. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    419
    Oh, sorry, I didn't mean to imply Mono was part of the WebGL build output. I should have worded that better. Scrap my mention of Mono.
     
    deus0, De-Panther and Chris9465 like this.
  7. G33RT

    G33RT

    Joined:
    Dec 27, 2013
    Posts:
    52
    Any new insights/updates on when multithreading will be available for WebGL? Any chance that will be on the 2021.x.x versions? Or rather 2022.x.x?
     
    ErkanAkin, DrViJ, tmars and 4 others like this.
  8. unity_B180E0F3EF7502A517F1

    unity_B180E0F3EF7502A517F1

    Joined:
    Nov 6, 2021
    Posts:
    8
    I am new, what do you mean about thread support in webgl.. If I am using System.Threading (for calculations, not graphics) then I can create webgl builds, right?
     
  9. StefanStrategis

    StefanStrategis

    Joined:
    May 7, 2018
    Posts:
    10
    I'm pretty sure there is no production-ready or user friendly way to support multi threading in WebGL yet.
     
    Magistr_AVSH likes this.
  10. ninjapretzel

    ninjapretzel

    Joined:
    Jul 19, 2011
    Posts:
    27
    Any word on when the previously mentioned 2021 release that supposedly restores System.Threading in WebGL builds will be out?

    I have some MMO framework code I've been wanting to port to WebGL forever.

    (Edit. Strikethrough unsupported. What a shame.)
    ~~I can dev it in the editor, but unfortunately can't do a build so long as Threads/Tasks don't work.~~

    Ended up doing some ~~c~h~a~n~g~e~s~~ hacks just to make it work without threads, then found out
    TypedReference
    is unsupported in WebGL, which lead to heap corruption, but eventually lead me to finding the
    where T: unmanaged
    generic type constraint, which is supported, works in WebGL, and lets me do the stuff my netcode needs.
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.3/blittable

    So no while not necessary, having threads would be a nice benefit to not need special hacks to make the code work in WebGL land.
     
    Last edited: Dec 8, 2021
  11. chris_hellsten

    chris_hellsten

    Joined:
    May 5, 2014
    Posts:
    12
    System.Threading never worked in Unity WebGL, Unity are not talking about *restoring* that behaviour, hopefully one day that feature is added. Unity are talking about first restoring multi-threading support for Unity's internal unmanaged threads which you can only leverage in your code via the C# Job System / DOTS, not C# Tasks / System.Threading.

    Any update on the this from Unity would be greatly appreciated, bad news is way better than no news! Is this already restored in 2021.2, might it be added in 2021 LTS / 2022.1? I haven't seen anything about it in recent patch notes.
     
  12. matt_cauldron

    matt_cauldron

    Joined:
    Dec 17, 2021
    Posts:
    48
    To tag on a multithreading Q here, does this also mean graphics jobs are currently unsupported?

    "Enable this option to instruct Unity to offload graphics tasks (render loops) to worker threads running on other CPU cores"
     
  13. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    24
    Yes I think they were trying to support graphics jobs first before opening up the System.Threading namespace, but I'm thinking their direction may change with WebGPU becoming more prevalent...but I do not know enough about it yet. My expectation is that Unity may try to target WebGPU as a platform years from now, but will have to tweak their emscripten cross compiler to work.

    *I could be wrong about all of this*
     
    Last edited: Mar 2, 2022
    G33RT likes this.
  14. tmars

    tmars

    Joined:
    Jul 26, 2013
    Posts:
    42
    LogicFlow, ROHITH_NANTHAN and DrViJ like this.
  15. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,157
    In this page it was mentioned about threading and fmod something being the blocking issue for webgl audio and microphone. Was this still be the case after unity already have webgl threading implemented?

    https://docs.unity3d.com/Manual/webgl-audio.html
     
  16. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    658
    @brendanduncan_u3d and @jukka_j

    Do you have an update you can share regarding restoration of multithreading in WebGL builds? The last update in this thread from @jukka_j was 2/3/21 and from @brendanduncan_u3d 7/15/21, I believe. I had to cancel WebGL support for a desktop/Android/iOS product because of inconsistent threading support and I am hoping this is now resolved.

    Thanks,
    Shaun
     
    Last edited: Apr 7, 2022
  17. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    658
    Hey @brendanduncan_u3d and @jukka_j It has been 10 months since this thread was updated -- do you have any news to share regarding WebGL threading support? Thanks.
     
  18. le_duke

    le_duke

    Joined:
    Aug 30, 2011
    Posts:
    48
    Interested too !
     
  19. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    419
    I just asked about this. We've been dangling the multithreading carrot for a long time now :-/. I guess it took a long time to get all of the various libraries working (baselib, physics, etc), which are all ready now, but it seems that the main thing holding it up now is that it breaks exception handling, which is still being worked on. I wish I could give an updated timeframe, but I trust if it's taking this long, there's good reason and it's not from lack of concern.
     
  20. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,049
    Would this also enable DOTS tech like Jobs and Entities? I'm trying to figure out if DOTS is at all relevant if we want to target WebGL in the medium term.
     
  21. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Thank you for the update on the situation and very comforting to know that it's a high priority issue and still being moved forward by unity.

    Question: Will it be ready for unity 2022 first release?
     
    Gamemakers3D likes this.
  22. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,042
    Last edited: May 3, 2022
    LM-CML likes this.
  23. seenquev

    seenquev

    Joined:
    Nov 10, 2020
    Posts:
    1
    I've just looked over release notes for 2022.1a (https://unity3d.com/beta/2022.1a) and there is not a single word mentioned about adding multithreading support for WebGL.

    It's been a long time. Can anyone from Unity Team refresh the roadmap and let us know about: where you are with all of the pending items that must be accomplished in order to add multithreading support to WebGL?
     
  24. darklord849

    darklord849

    Joined:
    Jan 11, 2018
    Posts:
    6
    Hey guys,
    Multithreading with unity webgl is a must to make a space in today's space with threejs and babylon on edge
     
  25. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    836
  26. vb2s360

    vb2s360

    Joined:
    Oct 7, 2015
    Posts:
    34
    Would love an update about this @brendanduncan_u3d even if there's no concrete timeline. Thanks !
     
  27. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,042
  28. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    419
    @vb2s360 From what I understand, the technical issues keeping it from happening have been fixed now. i don't know what the timelines are, people are taking summer vacations so a number of people involved are out for a little bit.
     
    kevin_unity698, vb2s360 and Thaina like this.
  29. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    419
    @bdovaz I mostly do graphics programming, so I'm not sure what all the limitations of what's proposed there are. I know C# threading is a *huge* challenge, largely due to garbage collection, that Jukka has been thinking about for a long time.
     
    De-Panther and gtk2k like this.
  30. JesusGonzalezRodriguez

    JesusGonzalezRodriguez

    Joined:
    Feb 18, 2022
    Posts:
    14
    I also wonder if there is any possibility to do a background job in web gl: web api calls are somehow doing that, and that works in web gl, but any other attempt to do something like that fails...
     
  31. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    836
    Why not?
    In theory, reading https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html and this https://medium.com/techtrument/multithreading-javascript-46156179cf9a should give you enough info to get started.
    Learning some Javascript for a very specific task shouldn't take more than 1-2h, if you already know any other popular programming language, like C#.
    Please post back if you manage to do it. I'm really interested in finding this out, but since it's not an immediate need, I don't have the time to look it up.
     
  32. chefneil

    chefneil

    Joined:
    Aug 21, 2020
    Posts:
    29
    Hey folks (cc @brendanduncan_u3d ) Just looking for an update on this. I have an ECS-based webgl project. Does the job system support multithreading yet for webgl? If so - what are the steps needed (if any) to enable this behavior and verify that it's working.

    Appreciate you!
     
  33. OceanX000

    OceanX000

    Joined:
    Feb 24, 2021
    Posts:
    120
    @brendanduncan_u3d @jukka_j, When will it be possible to use multithreading in Unity WebGL's native module? We really want to use multi-threading to improve the performance of the game, especially things like animation and rendering, physics. Difficulty running performance-hungry games. Thank u!
     
    xucian likes this.
  34. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    264
    @OceanX000 so here's the state of multithreading support: we brought back C/C++ multithreading support in 2022.2 with Emscripten 3.1.8. C# multithreading is still unsupported but is being worked on, because like @brendanduncan_u3d said, there are a lot of obstacles in its way and it's pretty challenging to get done. Does this help?
     
  35. OceanX000

    OceanX000

    Joined:
    Feb 24, 2021
    Posts:
    120
    Thanks for reply. We hope to use C/C++ multithreading first, do you have a rough time expectation? Is there any difficulty without considering C# multithreading?
     
  36. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    229
    Really thank you! When you say C++ I assume you mean the engine itself, but does it also include IL2CPP code (I suppose not as that would be the C# unsupported code)? And Burst-compiled code?
     
    SolidAlloy likes this.
  37. OceanX000

    OceanX000

    Joined:
    Feb 24, 2021
    Posts:
    120
    As @unityruba said, there are still many difficulties in C# multithreading support, and it will be long-term. So I hope to know how long it takes for the modules of the engine itself to support multithreading. I think that if this is done, both the engine itself and 3rd party plugins using C/C++ with posix thread API can be supported.
     
    forestrf likes this.
  38. louis-simon_unity

    louis-simon_unity

    Joined:
    Aug 5, 2022
    Posts:
    2
    Hello,

    Any news on supporting this for the 2023 versions?
    For now even simple examples don't work on current versions and there is very little documentation online. The existing blog posts are outdatted.

    Is there any project that is successfully using webgl with multithreading or an online example?

    Thank you
     
    OceanX000, poprev-3d and Zarbuz like this.
  39. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    836
    I'd be interested in calling native JS code that executes some tasks on the JS part at least. Anyone from unity or a web developer here can confirm this is possible?
    Of course it'd require recreating your input data in js, but it's better than nothing, and for some small tasks it'd be enough
     
  40. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Multithreading in WebGL let’s GOOoOoOoOo!!!
     
    havokentity likes this.
  41. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    836
    is it implemented?
     
  42. nfynt-zap

    nfynt-zap

    Joined:
    Jun 3, 2021
    Posts:
    20
  43. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    24
    Sorry to bug you guys again, but just wondering what are the abstract obstacles still in the way? Last I recall there were a lot of issues with error handling and garbage collection. Is that front still being developed?
     
    Last edited: Feb 13, 2023
  44. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    Also following. =D
     
  45. nfynt-zap

    nfynt-zap

    Joined:
    Jun 3, 2021
    Posts:
    20

    Upon further testing... it seems this null function exception is raised consistently when switching between scenes. So I would guess that there's a missing translation for `SceneManager.LoadScene` on the JS side? Otherwise all the other systems like - skinned mesh, physics 2D/3D, navigation mesh, etc. seems to be working fine independently in the first loaded scene.
     
  46. OmarVector

    OmarVector

    Joined:
    Apr 18, 2018
    Posts:
    129
    I want to understand, Does Unity Webgl atm, support Render thread away from main thread or still everything run on main thread?
     
    xucian likes this.
  47. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,507
    It is single-threaded. I have a build online made with Unity 2022.2 and [Graphy] stats asset where you can see it is using/detecting only one core (bottom right corner):
    upload_2023-3-24_19-40-7.png
     
    OmarVector likes this.
  48. whitegreenstudios88

    whitegreenstudios88

    Joined:
    Dec 12, 2021
    Posts:
    10
    2022.2 onwards supported native multithread c++ for webgl, i am new to native plugin, can anyone show me the process how to implement it? do i need NativeRenderingPlugin ? tq
     
  49. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,079
    OnAudioFilterRead, for WebGL, please!
     
  50. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    Is it possible to just force any tasks posted on synchronization context to just be executed synchronously? That would at least give some kind of backwards support to existing async functionality I guess. Not sure of the specifics or level of effort, but just a thought.