Why Mouse Lag

IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
<div class="IPBDescription">why is there mouse lag in ns2?</div>Hey.

I am here to discuss, and put some of my limited game development knowledge to work to try to find out why ns2 has mouse lag.

First, let us ask, what is mouse lag in games?

Mouse lag is where there is a noticable delay from when the player moves their mouse, to when the game begins updating the mouse look, and finally when the game finishes updating the mouse look.

There are some games that have it bad, fallout 3 main menu is a good place to go for an example on mouse lag, so is the skyrim main menu. Skyrim in general also has mouse lag that I would consider worse than ns2's mouse lag.
Note: I had a friend check out the mouse lag in the skyrim main menu, he couldn't notice it... but trust me, it was freaking there, and it was massive (500ms at least).

So what causes mouse lag?
is it the fps? No. why not? Because fps will affect how often the game updates, but is not necessarily accountable for a delay in mouse input.
note: It might be part of the bad mouse lag situations in ns2, but is in no way the cause.

My theory is it's down to the game loop. A game loop is a constantly looping block of code that will repeat until the engine is stopped. If the game loop is completely Linear, and there is only one game loop running for everything. Then low performance in one area of the game engine, will hold every thing back.

I don't know if uwe has a separate loop, just for input, but my guess is no.

Another thing that would also cause mouse lag is how the game engine interpolates mouse movement, also known as mouse smoothing(not acceleration). If the spark engine is using any form of mouse smoothing, then that will cause noticable mouse lag, especially if the frame rate gets low.

What is mouse smoothing? let's say that te game receives a mouse position update at 0 seconds, then it isn't until 1 second that it receives the next position, the mouse has changed quite a distance. Mouse smoothing will add in extra positions to make the transition between positions... smooth. Without mouse smoothing, the position of the character's look will simply jump to the next position, this may look a bit jerky, but is actually better, because of the fact that there is no mucking around with where the player is trying to look.

So in summary. here are my theories:

There is something funky going on in the game loop, possibly another game loop for mouse input needs to be added.
Mouse smoothing is in there, doing something to people's mouse input.

please correct me if I'm wrong, I do hope I'm not wrong, because if I am wrong, then the problem is much worse.

Comments

  • KoruyoKoruyo AUT Join Date: 2009-06-06 Member: 67724Members, Reinforced - Shadow
    edited December 2011
    The thing is, the input lag is not on every machine, or at least not as strong - also it isn't consistent, so while I(with my pc) usually don't have too big input lag on my server, its depending on server(i do have strong input lag on some servers), area, or time into a game... (dont know the exact cause)

    Yesterday we played a scrim on ns2_turtle (on my server, which is also the pc i'm playing on), and on alien side i could look around fine at first, but later on in certain places or situations(usually if i ran into marines) i suddently had such strong input lag that i could barley turn. (even tho framerate didnt drop) Same happened for one of my mates.(yuuki)

    And even tho the server was just freshly started for the match, and no unusuall errors in the server log, after a server restart it was ok again.

    Its strange.
  • IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
    but you have to remember, the mouse lag is ALWAYS there. even if it goes low, to like 20ms, it's still there.
  • MaxMax Technical Director, Unknown Worlds Entertainment Join Date: 2002-03-15 Member: 318Super Administrators, Retired Developer, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, Subnautica Developer, Pistachionauts, Future Perfect Developer
    edited December 2011
    There is up to 1 frame of lag caused by input (e.g. if you press the a key immediately after the keyboard input was processed in the main loop). There is also 1 frame of lag caused by the multi-threaded rendering system. Both of this are pretty normal in most games and I'm not too worried about them. Multi-threaded rendering can be turned off if you want to recover that frame, but most likely it will reduce your frame rate, so it may not actually reduce the lag (i.e. 1 frame of lag at 30 FPS = 2 frames of lag at 60 FPS).

    What we are looking into is additional frames of lag on top of this. I'm pretty sure this is being caused by the graphics card buffering multiple frames before they are displayed. Most likely this is some error in the way we are using Direct3D. The effect is pretty dependent on the graphics card and the speed of your CPU (if your CPU is slow relative your graphics card, it won't get ahead).

    Also, there is no mouse smoothing.
  • KalabalanaKalabalana Join Date: 2003-11-14 Member: 22859Members
    A shame, I had always imagined there was a stored memory location by the OS for hardware input peripherals that a game could poll in real time.
  • juicejuice Join Date: 2003-01-28 Member: 12886Members, Constellation
    I have noticed that because I have good hardware, I can change resolutions quite a bit and not see a dramatic difference in FPS.

    <b>However</b>, when I do so, I seem to notice greater "mouse lag" at higher resolutions + atmospherics + aa. Would this be from a limitation of the cpu versus the graphics card? Is this consistent with Max's statement about slow CPU plus fast GFX = buffered frame delays, because the CPU is slower relative to GFX <b>at higher resolutions</b>, or is it the opposite? Hmmmm.
  • ArgathorArgathor Join Date: 2011-07-18 Member: 110942Members, Squad Five Blue
    <!--quoteo(post=1889254:date=Dec 8 2011, 10:00 PM:name=Max)--><div class='quotetop'>QUOTE (Max @ Dec 8 2011, 10:00 PM) <a href="index.php?act=findpost&pid=1889254"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->The effect is pretty dependent on the graphics card and the speed of your CPU (if your CPU is slow relative your graphics card, it won't get ahead).<!--QuoteEnd--></div><!--QuoteEEnd-->
    Interesting...

    I have an Intel i7 920 @ 4.2ghz (currently) and an ATI 5850. The gfx card doesn't get taxed much, but in late game my CPU gets...abused. If I am understanding correctly, you are suggesting that my input lag would be worse in the early game when my CPU is further ahead than my GPU? That is the opposite of my experience to date.

    It would however match with <b>some</b> of my play experience, where despite having FPS showing as 60+ the game <b>feels</b> laggy but this is quite rare.

    (note: Seeing as this patch, even the best NS2 servers rubberband mid-end game, my first issue could largely be down to server performance.)

    Either way I am incredibly grateful you are looking into it, as performance is really the only barrier between us and super fun NS2 games at the moment. The game is stunningly fun when you can simply enjoy playing it!
  • MaxMax Technical Director, Unknown Worlds Entertainment Join Date: 2002-03-15 Member: 318Super Administrators, Retired Developer, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, Subnautica Developer, Pistachionauts, Future Perfect Developer
    <!--quoteo(post=1889261:date=Dec 8 2011, 03:18 PM:name=juice)--><div class='quotetop'>QUOTE (juice @ Dec 8 2011, 03:18 PM) <a href="index.php?act=findpost&pid=1889261"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec--><b>However</b>, when I do so, I seem to notice greater "mouse lag" at higher resolutions + atmospherics + aa. Would this be from a limitation of the cpu versus the graphics card? Is this consistent with Max's statement about slow CPU plus fast GFX = buffered frame delays, because the CPU is slower relative to GFX <b>at higher resolutions</b>, or is it the opposite? Hmmmm.<!--QuoteEnd--></div><!--QuoteEEnd-->
    It's consistent with what I said, but I said the opposite of what you wrote :) If the CPU is fast relative to the card (or say the CPU has little work, like when you're in the ready room), then the CPU will finish processing the frame before your GPU does. If the CPU is allowed to go on to processing the next frame while the GPU is still working, it will get ahead and you'll feel the lag.
  • YuukiYuuki Join Date: 2010-11-20 Member: 75079Members
    I tried to reproduce it on a local sever but didn't manage, I tried to add 20+ bots (seems heavy on the cpu) so that my fps drops around 25, sure it's a bit slow but I failed to perceive input lag. Then I went on tram and added a lots of models to make my fps drop in the same range, it wasn't like the lag I had playing online on Koruyo's server. I also tried to change the number of pre-rendered frames in nvidia's panel, but it didn't changed much.
  • ZeikkoZeikko Join Date: 2007-12-16 Member: 63179Members, Squad Five Blue, NS2 Map Tester
    In the couple of latest builds i have had input lag problems too. I'm having quite stable 50-60 FPS usually and the input lag seems to be totally independent from the FPS. Usually there's none or only very little barely noticeable input lag. Sometimes I have a feeling that the input lag gets constantly bigger but then i just restart my client and it feels good again. But the worst thing is that sometimes there are huge sudden spikes in the input lag so that even turning around a corner seems impossible.
  • TheLordTheLord Join Date: 2003-05-13 Member: 16258Members
    Well the interesting thing is the difference between build 187 and 188/189. The mouse input lag issue appeared - but I did not change my gfx drivers settings. It sounds like nothing should have changed regarding the mouse lag from 187 to 188/189. However it changed <b>quite </b>a bit.

    I commanded a lot of games in 187 and right when 188 came out I was like "what the....?" - night and day. It is especially noticable as a commander in new heliport on ns_summit. I do notice it more if my fps drops lower...

    I did try to lower the pre buffered frames to 1 instead of 3 - that did improve it a bit but its still far worse than in 187 with 3 buffered frames (triple buffering was and is off).

    I can imaginge this is a really hard to find bug since so much was changed in 188. But it is critical to squash it.

    My idea on what could be causing it is the position in the code where the game gets the current mouse location. Did it change if you compare 187 to 188/189? Is the position saved somewhere and reused later? I hope I'm not wasting your time. :)
  • juicejuice Join Date: 2003-01-28 Member: 12886Members, Constellation
    <!--quoteo(post=1889275:date=Dec 8 2011, 05:42 PM:name=Max)--><div class='quotetop'>QUOTE (Max @ Dec 8 2011, 05:42 PM) <a href="index.php?act=findpost&pid=1889275"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->It's consistent with what I said...<!--QuoteEnd--></div><!--QuoteEEnd-->
    Awesome, that means the effect I feel at different settings is probably due to the CPU getting ahead/GFX buffering too many slow frames thing...or something... which means it will be fixed! Thanks.
  • MaxMax Technical Director, Unknown Worlds Entertainment Join Date: 2002-03-15 Member: 318Super Administrators, Retired Developer, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, Subnautica Developer, Pistachionauts, Future Perfect Developer
    <!--quoteo(post=1889307:date=Dec 8 2011, 06:33 PM:name=TheLord)--><div class='quotetop'>QUOTE (TheLord @ Dec 8 2011, 06:33 PM) <a href="index.php?act=findpost&pid=1889307"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Well the interesting thing is the difference between build 187 and 188/189. The mouse input lag issue appeared - but I did not change my gfx drivers settings. It sounds like nothing should have changed regarding the mouse lag from 187 to 188/189. However it changed <b>quite </b>a bit.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Something did change between 187 and 188, which was the new occlusion culling system. Because the old occlusion culling system operated on both the CPU and the GPU, there was inherent synchronization between them. This prevented the GPU from buffering frames in the manner I mentioned in an earlier post. This is actually a good thing, because it means the CPU and GPU can be more fully utilized instead of waiting around for the other one to finish up what it was doing.

    However it does mean that they can get out of sync and the graphics card can fall behind causing lag. I've just read through the NVIDIA GPU Programming Guide and they recommend an approach similar to how I implemented the r_sync command, but to allow it to buffer at least one frame (per GPU), so I'm going to implement that and we'll see where we are with regards to mouse lag.
  • ZeikkoZeikko Join Date: 2007-12-16 Member: 63179Members, Squad Five Blue, NS2 Map Tester
    <!--quoteo(post=1889390:date=Dec 9 2011, 04:14 PM:name=Max)--><div class='quotetop'>QUOTE (Max @ Dec 9 2011, 04:14 PM) <a href="index.php?act=findpost&pid=1889390"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I'm going to implement that and we'll see where we are with regards to mouse lag.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Great! Let's hope it fixes the issue!
  • MuYeahMuYeah Join Date: 2006-12-26 Member: 59261Members
    Technical explanations like this make my inner nerd go wild. :D
  • TheLordTheLord Join Date: 2003-05-13 Member: 16258Members
    Interesting!
    I sure hope your concept fixes the issue. I have my doubts though, since the driver already allows fixing this to 1 prerendered frame - which does not fix it completely. It's still worse than in 187...

    I wonder if it would be possible to "move" the mouse movement into a different thread and draw it on top of the image output. This different thread could run much faster than the current one on another cpu core... So while the gpu could still pre-render frames the mouse itself in commander view / certain selection screens would not lag.

    This would not fix "slow" mouse actions when the mouse itself isn't visible though (turning around etc)...

    It would be interesting to know how other games circumvented this issue. The settlers 3 is an interesting example. I did play it all the time between 1998 and 2001. While the fps went down to 0.3-2 the mouse was somehow independent of that and could still be moved smoothly at <refresh-rate> fps...
  • MaxMax Technical Director, Unknown Worlds Entertainment Join Date: 2002-03-15 Member: 318Super Administrators, Retired Developer, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, Subnautica Developer, Pistachionauts, Future Perfect Developer
    <!--quoteo(post=1889401:date=Dec 9 2011, 08:58 AM:name=TheLord)--><div class='quotetop'>QUOTE (TheLord @ Dec 9 2011, 08:58 AM) <a href="index.php?act=findpost&pid=1889401"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Interesting!
    I sure hope your concept fixes the issue. I have my doubts though, since the driver already allows fixing this to 1 prerendered frame - which does not fix it completely. It's still worse than in 187...

    I wonder if it would be possible to "move" the mouse movement into a different thread and draw it on top of the image output. This different thread could run much faster than the current one on another cpu core... So while the gpu could still pre-render frames the mouse itself in commander view / certain selection screens would not lag.

    This would not fix "slow" mouse actions when the mouse itself isn't visible though (turning around etc)...

    It would be interesting to know how other games circumvented this issue. The settlers 3 is an interesting example. I did play it all the time between 1998 and 2001. While the fps went down to 0.3-2 the mouse was somehow independent of that and could still be moved smoothly at <refresh-rate> fps...<!--QuoteEnd--></div><!--QuoteEEnd-->
    Are you actually experiencing the cursor lagging in commander mode? We're using the hardware cursor (like I'm sure Settlers 3 does) which is updated in the hardware independent of the frame rate of the game.
  • sheena_yanaisheena_yanai Join Date: 2002-12-23 Member: 11426Members
    the only input lag i experience is the delay when firing a lmg in quick, short bursts, it just does not feel right, there is this squishy trigger delay when shooting, but other than that i dont feel any input lag when moving around
  • TheLordTheLord Join Date: 2003-05-13 Member: 16258Members
    edited December 2011
    <!--quoteo--><div class='quotetop'>QUOTE </div><div class='quotemain'><!--quotec-->Are you actually experiencing the cursor lagging in commander mode? We're using the hardware cursor (like I'm sure Settlers 3 does) which is updated in the hardware independent of the frame rate of the game.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Hey, I just tested it again really closely - here's what I did: start summit, activate cheats, join rines, enter cc.
    And what I found:
    1. while the mouse is not over a GUI element (such as the map or the build buttons) everything is fine, the mouse is 100% responsive
    2. while the mouse is over a GUI element and the fps are high (such as at the edges of the map) its a bit laggy but still okay
    3. while the mouse is over a GUI element and the fps are low you can really perfectly see the huge delay of the mouse (I'd guess about 50-150ms)
    Another observation: while the mouse is not over a GUI element it's small and blue - once it enters a GUI area it becomes large and orange and the lagging starts immediately.

    I think this is quite an interesting observation, looks like you do indeed use the hardware cursor - as long as the mouse is not over a GUI element.

    Maybe this applies to normal players' mouse delays as well? The new marine HUD was also introduced with 188....
    edit: Ok, turning off the marine hud didn't change anything. It might be the way you treat the mouse while it's not visible... If you can fix it for the commander mode GUI elements it might automatically be fixed for the rest as well?
  • IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
    would putting the mouse polling and position updating into a separate, cpu only thread help at all? and then when the graphics updates, it merely grabs the current location gained from the mouse.

    That probably won't entirely fix the problem at low fps, but it may prevent a buildup of mouse lag.
  • WheeeeWheeee Join Date: 2003-02-18 Member: 13713Members, Reinforced - Shadow
    input lag does seem to be pretty massive - it seems like a lot of the time the input will be queued up and the cpu lags behind it (i.e. you press strafe left for 0.5 seconds and the animation lasts for 0.8 seconds) feels a little bit like ice skating. did the friction values decrease in between builds? it really feels like movement is very imprecise right now.
Sign In or Register to comment.