267: FPS quality, part 1: Texture streaming

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 focus for 267 has been in finding out why NS2 has such a low frame-time quality or why "90 fps on NS2 feels like 30".

Turns out that there are multiple parts to that problem ... and we'll take one part at a time.

Part 1: Texture streaming

First, a word about the NS2 texture streamer. It works by creating a very low-res version of every texture to be used which uses so little VRAM that it can be kept around forever. Then, when you want to load a texture, the low-res version is used until the background streamer has loaded the hi-res version from the filesystem.

Thus, no GPU stalling, and at worst a momentarily faded-out texture visible.

However, it turned out that there were a couple of problems.

First, the video cards these days reports "virtual video ram" - which is to say, they lie and pretend they have much more texture ram than they actually have. The driver will stream textures back and forth between the primary memory and the video card, thus making it look like they have more ram than they actually have.

The main problem here is that the GPU will usually stall while waiting for textures that are not in the vram. Which means you will get a spike in the frametime while the texture is streamed in to the card (and another texture also needs to be streamed out first to make room).

But if you have a graphics cards that lies about its VRAM (check r_stats; if it reports about 3500Mb of "virtual video memory free" on the starting screen you got a liar inside your computer), then the (stall-free) NS2 texture manager will never trigger.

To avoid this problem, we have added a Texture Management option to the Graphics Options. That will allow you to set the texture ram to something rather more like your actual video ram (and it also allows you to exercise the texture manager - the 0.5Gb option will force it to load/unload textures pretty much the whole time - use r_stats to track the activity).


The second problem was that the texture manager was using the device driver while the main render thread was trying to render. As the render thread is often the bottleneck, this meant that the texture manager was slowing down rendering while active.

Easy fix, just a matter of adding a flag so the render thread could tell the texture manager to step back and wait until the render was through. Does mean that the texture manager will not load textures as fast, so you will get to see more of those low-res textures sometimes, but ...


The third problem turned out to be a bit more serious. NS2 uses memory-mapped files to load files effectively. Works very well if the file you are looking for is already in the filesystem buffers (then reading a 5Mb files takes pretty much no time at all - just a matter of switching around some internal registers and presto - you have access to the file data).

The texture streamer basically takes the memory mapped file, finds out where the texture data starts and hands that address to the card device driver. Extremely fast and efficient - unless the file isn't in the filesystem buffers.

At which time you are suddenly going to be loading a texture about 10 000 times slower than expected - 100+ ms instead of 0.01ms. And you are locking up the graphics driver while doing so... so those 100+ms gets added to your frame time.

Not a good thing. Fortunately, the fix is easy - just step through the memory mapped area while you are in the background loader to ensure the whole area is loaded (as this is a background thread, it does not affect frametime), and THEN call the device driver.

In case you are wondering why this wasn't caught earlier, you will only notice it if the files you need are not in the filesystem buffers ... and if you are developing NS2 they will pretty much always be loaded.


To be continued...
«13

Comments

  • craZyfxcraZyfx Austria Join Date: 2014-01-20 Member: 193350Members, Reinforced - Shadow
    Great posts, looking forward to 267 ;)
  • meatmachinemeatmachine South England Join Date: 2013-01-06 Member: 177858Members, NS2 Playtester, NS2 Map Tester, Reinforced - Shadow, WC 2013 - Supporter
    edited July 2014
  • MuckyMcFlyMuckyMcFly Join Date: 2012-03-19 Member: 148982Members, Reinforced - Supporter, Reinforced - Shadow
    I hope that it wont run too smooth, It wont be NS2 then ;)
  • MigeMige Join Date: 2005-03-19 Member: 45796Members, Reinforced - Supporter
  • CmdrKeenCmdrKeen Join Date: 2013-05-21 Member: 185321Members, Reinforced - Shadow
    matso wrote: »
    In case you are wondering why this wasn't caught earlier, you will only notice it if the files you need are not in the filesystem buffers ... and if you are developing NS2 they will pretty much always be loaded.

    This is why the NS2 vets walk / skulk the whole map before the round begins ... gotta load dem textures

  • 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
    MuckyMcFly wrote: »
    I hope that it wont run too smooth, It wont be NS2 then ;)

    Dont worry, not every hitch will be fixed for 267, need something to fix in 268 :) but that will be easier now that the main reasons have been identified and how to adress them.
  • MigeMige Join Date: 2005-03-19 Member: 45796Members, Reinforced - Supporter
    CmdrKeen wrote: »
    matso wrote: »
    In case you are wondering why this wasn't caught earlier, you will only notice it if the files you need are not in the filesystem buffers ... and if you are developing NS2 they will pretty much always be loaded.

    This is why the NS2 vets walk / skulk the whole map before the round begins ... gotta load dem textures

    Yep and again if you alt tab :D
  • alsteralster Join Date: 2003-08-06 Member: 19124Members
    This seems to be similar to how the Unreal Engine works or used to work. For an example load up Chivalry and you can see the more detailed textures appear.
  • Cannon_FodderAUSCannon_FodderAUS Brisbane, AU Join Date: 2013-06-23 Member: 185664Members, Squad Five Blue, Squad Five Silver, Reinforced - Shadow
    matso wrote: »
    One focus for 267 has been in finding out why NS2 has such a low frame-time quality or why "90 fps on NS2 feels like 30".

    Turns out that there are multiple parts to that problem ... and we'll take one part at a time.

    Part 1: Texture streaming

    First, a word about the NS2 texture streamer. It works by creating a very low-res version of every texture to be used which uses so little VRAM that it can be kept around forever. Then, when you want to load a texture, the low-res version is used until the background streamer has loaded the hi-res version from the filesystem.

    Thus, no GPU stalling, and at worst a momentarily faded-out texture visible.

    However, it turned out that there were a couple of problems.

    First, the video cards these days reports "virtual video ram" - which is to say, they lie and pretend they have much more texture ram than they actually have. The driver will stream textures back and forth between the primary memory and the video card, thus making it look like they have more ram than they actually have.

    The main problem here is that the GPU will usually stall while waiting for textures that are not in the vram. Which means you will get a spike in the frametime while the texture is streamed in to the card (and another texture also needs to be streamed out first to make room).

    But if you have a graphics cards that lies about its VRAM (check r_stats; if it reports about 3500Mb of "virtual video memory free" on the starting screen you got a liar inside your computer), then the (stall-free) NS2 texture manager will never trigger.

    To avoid this problem, we have added a Texture Management option to the Graphics Options. That will allow you to set the texture ram to something rather more like your actual video ram (and it also allows you to exercise the texture manager - the 0.5Gb option will force it to load/unload textures pretty much the whole time - use r_stats to track the activity).


    The second problem was that the texture manager was using the device driver while the main render thread was trying to render. As the render thread is often the bottleneck, this meant that the texture manager was slowing down rendering while active.

    Easy fix, just a matter of adding a flag so the render thread could tell the texture manager to step back and wait until the render was through. Does mean that the texture manager will not load textures as fast, so you will get to see more of those low-res textures sometimes, but ...


    The third problem turned out to be a bit more serious. NS2 uses memory-mapped files to load files effectively. Works very well if the file you are looking for is already in the filesystem buffers (then reading a 5Mb files takes pretty much no time at all - just a matter of switching around some internal registers and presto - you have access to the file data).

    The texture streamer basically takes the memory mapped file, finds out where the texture data starts and hands that address to the card device driver. Extremely fast and efficient - unless the file isn't in the filesystem buffers.

    At which time you are suddenly going to be loading a texture about 10 000 times slower than expected - 100+ ms instead of 0.01ms. And you are locking up the graphics driver while doing so... so those 100+ms gets added to your frame time.

    Not a good thing. Fortunately, the fix is easy - just step through the memory mapped area while you are in the background loader to ensure the whole area is loaded (as this is a background thread, it does not affect frametime), and THEN call the device driver.

    In case you are wondering why this wasn't caught earlier, you will only notice it if the files you need are not in the filesystem buffers ... and if you are developing NS2 they will pretty much always be loaded.


    To be continued...

    I understood about half of that (I think). But doesn't mean I can't awesome you for such a detailed explanation!
    Thanks.

  • MrFangsMrFangs Join Date: 2013-03-27 Member: 184474Members, Reinforced - Shadow
    Thanks for the post, interesting read.

    One question about the texture streamer... it sounds like the low-res textures are created on-the-fly whenever a texture is loaded. I would imagine that these could be precomputed (at least the vanilla NS2 textures), and just loaded as a single blob on startup. Wouldn't this save CPU time, and ensure that all low-res textures are available right from the start, and with zero hitching?
  • Kouji_SanKouji_San Sr. Hινε Uρкεερεг - EUPT Deputy The Netherlands Join Date: 2003-05-13 Member: 16271Members, NS2 Playtester, Squad Five Blue
    Would probably gobble up memory if lots of them are precompiled o.o
  • lwflwf Join Date: 2006-11-03 Member: 58311Members, Constellation
    I enjoy these posts as much as the patches. Keep 'em coming! :)
  • _INTER__INTER_ Join Date: 2009-08-08 Member: 68392Members, NS2 Playtester, Reinforced - Shadow
    Kouji_San wrote: »
    Would probably gobble up memory if lots of them are precompiled o.o
    I don't think so:
    It works by creating a very low-res version of every texture to be used which uses so little VRAM that it can be kept around forever.
    But what if the low-res version additionally isn't compiled on startup, but loaded from the drive?
  • DC_DarklingDC_Darkling Join Date: 2003-07-10 Member: 18068Members, Constellation, Squad Five Blue, Squad Five Silver
    I was laughing the first time and I am still laughing when I read that.

    Nevertheless I understand some bugs are hard to near impossible to catch earlier on, so dont see any of it as me complaining. I am not!
    I would say, well done to all involved. :)
  • 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
    MrFangs wrote: »
    Thanks for the post, interesting read.

    One question about the texture streamer... it sounds like the low-res textures are created on-the-fly whenever a texture is loaded. I would imagine that these could be precomputed (at least the vanilla NS2 textures), and just loaded as a single blob on startup. Wouldn't this save CPU time, and ensure that all low-res textures are available right from the start, and with zero hitching?

    That's actually what the precaching phase is all about. See my next post ... in a day or two.

  • DC_DarklingDC_Darkling Join Date: 2003-07-10 Member: 18068Members, Constellation, Squad Five Blue, Squad Five Silver
    Hahahaha.. I should really start spamming you even more then @Ironhorse.
    So you even more ways to prove your right next time to uhm... speed it up.
  • 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
    Hopefully this means the hitching issues won't be as bad as before.
  • NessNess Join Date: 2002-12-17 Member: 10935Members, Reinforced - Onos
    Thanks for the informative post! I always wondered how things felt sluggish even with very high end video cards.
  • Dictator93Dictator93 Join Date: 2008-12-21 Member: 65833Members, Reinforced - Shadow
    Thanks for the great an informative posting Matso. I seriously never though this would be fixed after I had been mentioning it for what felt like years.

    Amazing to see that all forms of hitching are being looked into and from all areas. I greatly look forward to the smoother NS2 that wil be coming about in the near future.

    Also a nice public thanks to Ironhorse from bringing this matter up so often.
  • lwflwf Join Date: 2006-11-03 Member: 58311Members, Constellation
    IronHorse wrote: »
    matso wrote: »
    In case you are wondering why this wasn't caught earlier, .
    Cause everyone thought I was crazy or had failing hardware!
    9 months of living with these issues... I must love this game.

    You see, if you believe in matso, he believes in you. :D
    Thanks again for spending the past few weeks hunting down every single one of these

    Which graphics card do you have? I can add that the AMD Radeon HD 6950 does not "lie".
  • IronHorseIronHorse Developer, QA Manager, Technical Support & contributor Join Date: 2010-05-08 Member: 71669Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Subnautica Playtester, Subnautica PT Lead, Pistachionauts
    570 gtx and it definitely lies.

    But that was maybe 15% of the issue. The largest impact was the device driver having to tell the OS to access textures from the HD instead of memory.
    This is why I've had this problem since reinforced, when texture streaming was forced on for everyone.

    The rest of these hitches like file openings are icing on the cake and effects everyone (whereas device locking and other planned solutions mostly effect gpu bottle necked and low end gpu systems) not all of the hitches will be fixed in time for this next patch.. But considering that 90% of them have been addressed, it is definitely something to be excited for!
  • xen32xen32 Join Date: 2012-10-18 Member: 162676Members, Reinforced - Supporter
    Does this mean I will get 90fps on integrated laptop video? Please?
  • ezekelezekel Join Date: 2012-11-29 Member: 173589Members, NS2 Map Tester
    So this doesn't necessarily mean framerate gains, but more 'better feeling' while playing, correct?

    So if I'm in an area where I have 160 fps, it will feel much more like that how it feels to other engines? That'd be really nice. if I understood all of this correctly
  • 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
    Yes, no framerate gain for now. The goal was to improve the quality of the frames you already have. Not a finished process as of yet, but it is clearly much much better.
  • Racer1Racer1 Join Date: 2002-11-22 Member: 9615Members
    So if I have a 2 GB vid card, is there a way to determine how much is actually available? Is 2GB an appropriate amount? Or does the card and/or driver often reserve for itself X MB, meaning that 1.9 GB might be more accurate.
  • McGlaspieMcGlaspie www.team156.com Join Date: 2010-07-26 Member: 73044Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Onos, WC 2013 - Gold, Subnautica Playtester
    Racer1 wrote: »
    So if I have a 2 GB vid card, is there a way to determine how much is actually available? Is 2GB an appropriate amount? Or does the card and/or driver often reserve for itself X MB, meaning that 1.9 GB might be more accurate.
    The engine will still need memory for other effects in the game (cinematics, shaders, etc). Typically, that would be approximately 25-30% of your available video memory should be reserved as such. This means on a 2GB card, you would select the 1.5GB setting in the options menu. Generally speaking, you will always want to round-down when chosing the correct option based on how much memory your video card has onboard. It's worth noting, selecting the right option has nothing to do with how much System memory your computer has, and choosing the wrong option (I.e. too high, ex: 1.5GB on a 1280MB video card) will force Spark to use system memory for textures. This will result in the hitching behavior that Matso has described.
  • DefaultErrorDefaultError Minnesota Join Date: 2013-10-31 Member: 188917Members, Reinforced - Silver, Reinforced - Gold, Reinforced - Shadow, WC 2013 - Supporter
    Really looking forward to this new patch and glad to see all the hard work paying off :)

    The new option that you are talking about will it automatic detect how much vRAM you have a select the best option for you or is this something you have to find out yourself? I have a 7870 and I think it has 2GB of vRAM, so I'm guessing I would have to switch it to 1.5.

    Would you see any different between selecting 1 and 1.5 for this option as well?
  • 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
    Actually the 2gb option will not reserve 2 gb for the game but a little less. You will just have to select the option closest to your gpu. the option "none" will be unlimited, so people with over 2 gb cards can use that. right now all cards are handled like 2+ gb cards, leading to some problems
  • VetinariVetinari Join Date: 2013-07-23 Member: 186325Members, Squad Five Blue, Reinforced - Shadow, WC 2013 - Silver
    This is awesome. How much of an improvement can we expect concretely?
Sign In or Register to comment.