267 : Physics Multithreading

matsomatso Master of Patches Join Date: 2002-11-05 Member: 7000Members, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Shadow, NS2 Community Developer
edited July 2014 in NS2 General Discussion
One very important part of the NS2 client is showing up the various models going through their animations. Internally, the model consists of bones to which the skin of the model (with its textures) are attached. The animation then controls how the bones move in relation to each other. When you move the bones, the skin (and hitboxes) follow.

Animations needs be blended together (ie, running forward while aiming the rifle requires blending several animations together), and there is quite a few bones to keep track of, so figuring just how the left little pinkie should be curved takes a fair bit of processing power.

Lategame, when the number of entities goes up, this tends to become a bottleneck when it comes time to produce new frames. Upto 30-40% of the frametime can be spent setting up the physics.

In 266, this is all controlled by Lua code, which goes through all the models one by one, collects the animation data, asks the engine to calculate the new settings and then finally applying the result to the collision world before continuing with the next model.

Adding multithreading is kinda difficult, because the Lua VM is strictly single-threaded. Also, the results of the physics calculations needs to be stored in the collision world, which is also single-threaded. However, the middle part - where the little toes and pinkies gets sorted out - is where most of the time is spent. This opens up for applying a bit of pipelining.

Pipelining basically means that instead of having one thread doing
for every entity do
     A (collect data),
     B (compute) 
     C (apply them)

we instead do
Thread 1: [ Pipeline stage 1 ]
for every entity do
    A (collect data)
    Store data in engine for use by the next stage

Thread 2-4: [ Pipeline stage 2 ]
Wait for data to be stored in engine:
    B (compute)
    Store result for use by the next stage

Thread 5:  [ Pipeline stage 3 ]
Wait for data:
   C (Apply it).

So we need to do the first and last part in a single-threaded fashion, but we can do all the core heavy lifting in the middle part, where we have 1-3 threads all working to keep things going as fast as possible (the balance between the first thread producing data for the rest of the threads to work with means that more than 3 isn't really going to help any).

Now, the engine is not really designed to be used this way, but its been playtested for about 6 weeks now and after a few initial (and fixed) bugs, it seems to be pretty stable (ie, no known bugs).

However, it is OFF by default, because ...well, shit happens. And multhreaded shit happens to be especially stinky. So its off until we feel confident it won't cause too many sticky problems. Yes, we want victi^H^H^H^H volunteers to try it out... if it crashes too much, it stays off by default ( and its off for linux users, because it causes the linux client to crash ).

That said, the effect can be quite dramatic for your worst case FPS. 20% better worst-case FPS seems to be pretty standard for quad-cores. Even dual-cores gain a bit, though not as much.


tl;dr - Multithreading will increase your worst-case FPS by 20% (for quad core+) _OR_ possibly crash your machine. Off by default, turn on at your peril (yea, as if that's going to stop anyone... :-) )


«13456

Comments

  • DC_DarklingDC_Darkling Join Date: 2003-07-10 Member: 18068Members, Constellation, Squad Five Blue, Squad Five Silver
    I will definately enable it.
    I shall also enjoy spamming the community devs with any crash logs it shall produce. :D
  • Racer1Racer1 Join Date: 2002-11-22 Member: 9615Members
    You mention client side, but I assume the hitboxes are also calculated server side. Will there be similar changes on the server at some point?
  • matsomatso Master of Patches Join Date: 2002-11-05 Member: 7000Members, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Shadow, NS2 Community Developer
    Racer1 wrote: »
    You mention client side, but I assume the hitboxes are also calculated server side. Will there be similar changes on the server at some point?

    The server updates the physics on an as-needed basis only. It's actually very cool; all models have a maximum extents box, which fits around all of model, no matter what animation (yea, the largest box isn't the hive - it's the whip due to its 7m ranged slap attack).

    All active models place their big boxes in a sort of pre-collision world. Whenever you check for collision (when you move the player or shot a gun or slash ...), you first trace through this pre-collision world.

    Any models whose big boxes are touched, gets to have their physics updated to the correct time. THEN you do a collision trace through the detailed world, knowing that the models are all correctly configured, claws curled just like that, perky tail in _just_ the right position.

    The reason the client can't do the same is that you are implicitly tracing through your whole view volume... everything has to be rendered anyhow.

    Before you say "render only in view volume" - sorry, the view volume which is going to be rendered is not known yet - we set the physics for what will be rendered the NEXT frame (multithreaded rendering - we are calculating the physics for the next frame while we use last frames physics to render the current frame).

    And we don't know where the player will be looking next frame. High sensitivity mouse can easily turn you 180 degrees in one frame. Well, not easily, but...
  • CarNagE1CarNagE1 Poland Join Date: 2003-05-14 Member: 16298Members, NS2 Playtester, Reinforced - Shadow, WC 2013 - Supporter, Subnautica Playtester
  • IeptBarakatIeptBarakat The most difficult name to speak ingame. Join Date: 2009-07-10 Member: 68107Members, Constellation, NS2 Playtester, Squad Five Blue, NS2 Map Tester, Reinforced - Diamond, Reinforced - Shadow
    My i5 is ready for this. :D
  • Dictator93Dictator93 Join Date: 2008-12-21 Member: 65833Members, Reinforced - Shadow
    Matso's post proves once again how awesome this stuff is. I eagerly await this amazing patch.

    Question, is the PhysX sdk in NS2 (post v3.1 of the Nvidia SDK I believe) one of the later cores which also allows for the physX calculations to be further multi-threaded? I remember this was a big deal a while back when they moved from x87.
  • alsteralster Join Date: 2003-08-06 Member: 19124Members
    Since most players have a Nvidia card I say please use or enable PhysX. Most triple A titles use it.
  • simbasimba Join Date: 2012-05-06 Member: 151628Members
    This game is going to need a balance change if we see too much performance increase. Suddenly everyone can aim...
  • Dictator93Dictator93 Join Date: 2008-12-21 Member: 65833Members, Reinforced - Shadow
    alster wrote: »
    Since most players have a Nvidia card I say please use or enable PhysX. Most triple A titles use it.

    The game already uses PhysX, in fac there was an "attempted " GPu-PhysX implemetnation that never seemed to work. IMO, using the non-GPU multiäthreaded physX is better for all users than attempting to use the proprietary GPU libraries.
  • IeptBarakatIeptBarakat The most difficult name to speak ingame. Join Date: 2009-07-10 Member: 68107Members, Constellation, NS2 Playtester, Squad Five Blue, NS2 Map Tester, Reinforced - Diamond, Reinforced - Shadow
  • METROIDMETROID Join Date: 2012-10-31 Member: 165171Members, Reinforced - Supporter
    edited July 2014
    Need more to Enable!!!!!
    crazy_face.jpg
  • RockyMarcRockyMarc Join Date: 2009-11-24 Member: 69519Members, Reinforced - Shadow
  • ZeframZefram Join Date: 2004-05-11 Member: 28611Members, Forum Moderators, Constellation, NS2 Playtester, Squad Five Gold, NS2 Map Tester, WC 2013 - Shadow, Subnautica Playtester, Pistachionauts, Retired Community Developer
    RockyMarc wrote: »
    My body is ready.

    Don't write checks that your body can't cash.
  • ezekelezekel Join Date: 2012-11-29 Member: 173589Members, NS2 Map Tester
    Every atom of me is ready.

    Performance boost
    Smoother/more fluid frames in this and more in next updates
    unlocked server rates

    it's the ns2 I've always dreamed of :D
  • NordicNordic Long term camping in Kodiak Join Date: 2012-05-13 Member: 151995Members, NS2 Playtester, NS2 Map Tester, Reinforced - Supporter, Reinforced - Silver, Reinforced - Shadow
    I doubt it, but could this help servers in anyway?
  • OnosFactoryOnosFactory New Zealand Join Date: 2008-07-16 Member: 64637Members
    "I doubt it, but could this help servers in anyway?"

    The faster you update to the server, the less the server has to remember stuff from ages ago?
  • RockyMarcRockyMarc Join Date: 2009-11-24 Member: 69519Members, Reinforced - Shadow
    It would be bloody fantastic if this helped servers :)
    Maybe some of those overclocked 8GHz servers can go down a bit? :P
  • MephillesMephilles Germany Join Date: 2013-08-07 Member: 186634Members, NS2 Map Tester, NS2 Community Developer
    since you said on quad core+. how does it effect performance on dual core?
  • VetinariVetinari Join Date: 2013-07-23 Member: 186325Members, Squad Five Blue, Reinforced - Shadow, WC 2013 - Silver
    Mephilles wrote: »
    since you said on quad core+. how does it effect performance on dual core?
    matso wrote: »
    That said, the effect can be quite dramatic for your worst case FPS. 20% better worst-case FPS seems to be pretty standard for quad-cores. Even dual-cores gain a bit, though not as much.

    >20%
  • Laosh'RaLaosh'Ra Join Date: 2011-12-09 Member: 137232Members
    4 cores á 2.3 GHz here... REALLY looking forward to this option :D

    if it were to crash, what would be the possible reasons? race conditions? deadlocks? so much awesomeness that it breaks the laws of physics?
  • AsranielAsraniel Join Date: 2002-06-03 Member: 724Members, Playtest Lead, Forum Moderators, NS2 Playtester, Squad Five Blue, Reinforced - Shadow, WC 2013 - Shadow, Subnautica Playtester, Retired Community Developer
    During playtests we did not have any crashes related to it, but some buggy behaviour (all fixed now) related to physics. For example, the weapon a marine holds was flickering between the position in his hands and at his feet. Another bug was that first person view models stopped updating.

    That has all been fixed, but that is the type of problems you would look out for. Animations etc that are not updated correctly or badly placed objects. Crashes where not an issue.
  • UncleCrunchUncleCrunch Mayonnaise land Join Date: 2005-02-16 Member: 41365Members, Reinforced - Onos
    Before getting in a disaster. One must be cautious.

    The client guy should be able to enable/disable it properly (procedure as the dev want) and at will. So nobody get stuck. You should produce some documentation for these vic.. very important testers.

    This means clients should be granted a FPS analyzer/profiler (better than what we have, saving datas, upload etc...). So it can help find bugs, and other optimization. It save so much time in debugging.

    Another thing would be evaluate the impact on other stuff it produce. Netcode behavior etc...

    Why do i say that ??... no clue ?
  • GhoulofGSG9GhoulofGSG9 Join Date: 2013-03-31 Member: 184566Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Reinforced - Supporter, WC 2013 - Supporter, Pistachionauts
    edited July 2014
    Before getting in a disaster. One must be cautious.

    The client guy should be able to enable/disable it properly (procedure as the dev want) and at will. So nobody get stuck. You should produce some documentation for these vic.. very important testers.

    This means clients should be granted a FPS analyzer/profiler (better than what we have, saving datas, upload etc...). So it can help find bugs, and other optimization. It save so much time in debugging.

    Another thing would be evaluate the impact on other stuff it produce. Netcode behavior etc...

    Why do i say that ??... no clue ?

    We allready have such a analyser which will get improved in various ways with 267 as it was used alot to track down those hitching issue.

    The analyzer is hidden in your ns2 install folder under utils/PerfAnalyzer. You need to install python to run it.

    Ingame console commands to start logging are p_log (basic logging) or p_logall (advandced logging). Logs ( .plog file format ) will be saved into the ns2 config folder (for windows %appdata%\Natural Selection 2\ )

    BTW: This works also for the server. ;)

    352ed053748bece220fa46b2096a51.jpg

    1: Python Console ( for text output )
    2. Analyser Main Gui
    3. Frame Detail Window
    4. Frame Pick Window

    The graphs show how long each frame needed to get rendered. You might have seen those at the cdt twitter lately

    You open 3 and 4 by clicking at the graph in the main gui.
  • RockyMarcRockyMarc Join Date: 2009-11-24 Member: 69519Members, Reinforced - Shadow
    Blinded by the graphs!
  • ezekelezekel Join Date: 2012-11-29 Member: 173589Members, NS2 Map Tester
    RockyMarc wrote: »
    Blinded by the graphs!

    digital vibrancy strikes me again (moans in agony)
  • MigeMige Join Date: 2005-03-19 Member: 45796Members, Reinforced - Supporter
    alster wrote: »
    Since most players have a Nvidia card I say please use or enable PhysX. Most triple A titles use it.

    Yes please enable that we get more stutter and fps fluctuations, great. Like Borderlands 2 is Stutterlands 2 with phyx on. Gimmick feature
  • NominousNominous Baltimore, MD Join Date: 2012-02-18 Member: 146518Members
    IronHorse wrote: »
    1: Phyton Console.

    He means "Python" as in the scripting language. ;)
    Or Phyton as in... Phytoplankton. B-)
  • ObraxisObraxis Subnautica Animator & Generalist, NS2 Person Join Date: 2004-07-24 Member: 30071Super Administrators, Forum Admins, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Silver, WC 2013 - Supporter, Subnautica Developer, Pistachionauts
  • ezekelezekel Join Date: 2012-11-29 Member: 173589Members, NS2 Map Tester
    Just to clear things up with this update

    We have a multi-thread option we can enable (but it's disabled by default) and we have a new vram option? and if we have 2gb of VRAM we should set it to 1.5GB or is it default to that. That's what I got out of all the posts so far
Sign In or Register to comment.