What exactly causes V-sync to introduce input lag?

Talk to software developers and aspiring geeks. Programming tips. Improve motion fluidity. Reduce input lag. Come Present() yourself!
silikone
Posts: 57
Joined: 02 Aug 2014, 12:27

What exactly causes V-sync to introduce input lag?

Post by silikone » 26 Dec 2016, 00:00

Chief Blur Buster wrote:NOTE: Some useful low-lag VSYNC ON code programming techniques is in Page 3 of this thread.
I did a test on a G-sync monitor by recording the screen at high speed while moving the cursor around. I can conclude that in my case, G-sync right below the refresh rate has a 3D application that uses the Windows cursor update both on the same frame. The very first monitor refresh with the cursor in a new position caused by pushing the mouse also contains the updated game frame. As soon as the frame rate reaches the refresh rate, V-sync engages, and now the cursor can be seen updating four times without the game showing any signs of mouse input. It's only on the fifth update that it finally responds.
So with 60Hz, that's at least 66ms of extra input lag, but why? One would think that it would add 16.7ms at most, assuming that it simply needs to wait for the next refresh rather than switching the buffers right away.

Sparky
Posts: 682
Joined: 15 Jan 2014, 02:29

Re: What exactly causes V-sync to introduce input lag?

Post by Sparky » 26 Dec 2016, 06:01

Say you have an assembly line with four stations, each station can work on one frame at a time, and each station takes a different amount of time. Your framerate is determined only by the slowest station(bottleneck). If the bottleneck is the very first station, then the latency is equal to just the amount of time it takes to work on it, as the remaining stations have a bit of idle time between frames. If the bottleneck is the very last station, then the station before it will finish early, and wait until the last station finishes before it can hand off its frame. So now the second station has to wait on the third, and the first station has to wait on the second. Your latency is the amount of time it takes the last station to work on it times four. This is what happens with v-sync when the display becomes the bottleneck.

You can move the bottleneck back to the front with an in-game framerate cap.

User avatar
RealNC
Site Admin
Posts: 3740
Joined: 24 Dec 2013, 18:32
Contact:

Re: What exactly causes V-sync to introduce input lag?

Post by RealNC » 26 Dec 2016, 15:59

When the game submits the frame for output, it keeps working on new frames. It doesn't actually wait for the vblank itself. The OS/driver is doing the waiting for vblank, but the game doesn't actually care. The game can submit another frame later on, even though the previous one is still waiting for the vblank. So now you have two frames waiting to be displayed, containing 33.3ms old input data. Then a third frame comes in, and now we're on 50ms input lag.

Actually this can get so bad that it's sometimes possible to sit on 100ms worth of frames... 100ms input lag depending on how many buffers exist in the whole chain.

A frame limiter is needed to avoid this issue. What the frame limiter does is get between the game and the OS/driver, and intercept the frame submission. The game thinks it calls the OS function, but in reality it calls the frame limiter's replacement of that function. The new function doesn't just accept the new frame for output. It first looks how long it has been since the previous frame has been displayed. For a 60FPS cap, it looks if at least 16.7ms have passed since the last scanout. If not, it just waits and most importantly, it makes the game also wait since it doesn't yet accept the new frame.

Since the game is blocked until at least 16.7ms have passed since the previous frame, the game cannot compute new frames that are older than 16.7ms.
SteamGitHubStack Overflow
The views and opinions expressed in my posts are my own and do not necessarily reflect the official policy or position of Blur Busters.

Glide
Posts: 280
Joined: 24 Mar 2015, 20:33

Re: What exactly causes V-sync to introduce input lag?

Post by Glide » 27 Dec 2016, 10:39

I think the question is really "how does the game update the cursor instantly but not everything else?"
Which is something that I've wondered too.

User avatar
RealNC
Site Admin
Posts: 3740
Joined: 24 Dec 2013, 18:32
Contact:

Re: What exactly causes V-sync to introduce input lag?

Post by RealNC » 27 Dec 2016, 10:55

Glide wrote:I think the question is really "how does the game update the cursor instantly but not everything else?"
Which is something that I've wondered too.
It doesn't. The driver updates it by bypassing the rendering pipeline. It's called "hardware cursor", and some games have an option for it (Witcher 3 is one example.) When enabled, the driver injects the mouse cursor pixels directly into the framebuffer that is currently being scanned out. It doesn't care about vsync. It just draws the cursor over the current frame that is being scanned out by the monitor. Basically, the mouse cursor is drawn as an overlay on top of the game.

The Windows desktop also uses a hardware mouse cursor when you move the mouse, but switches to a rendered cursor when you drag things so that the object that is being dragged doesn't lag behind the cursor. Switching the hardware cursor off produces a flicker which you can observe when you hold down the left mouse button over something draggable (like a window title bar). After about 1 second of holding down the button (or immediately after you start dragging the window), the mouse cursor will flicker as Windows switches the hardware cursor off and draws it by software instead.

This is also the reason why many recording programs cannot capture the cursor. Since it's not rendered, there's nothing to record. Capture programs that do have cursor capture functionality will actually emulate the cursor (place it artificially on the recording by reading the current mouse position.)
SteamGitHubStack Overflow
The views and opinions expressed in my posts are my own and do not necessarily reflect the official policy or position of Blur Busters.

Glide
Posts: 280
Joined: 24 Mar 2015, 20:33

Re: What exactly causes V-sync to introduce input lag?

Post by Glide » 27 Dec 2016, 11:27

RealNC wrote:
Glide wrote:I think the question is really "how does the game update the cursor instantly but not everything else?"
Which is something that I've wondered too.
It doesn't. The driver updates it by bypassing the rendering pipeline. It's called "hardware cursor", and some games have an option for it (Witcher 3 is one example.) When enabled, the driver injects the mouse cursor pixels directly into the framebuffer that is currently being scanned out. It doesn't care about vsync. It just draws the cursor over the current frame that is being scanned out by the monitor. Basically, the mouse cursor is drawn as an overlay on top of the game.
I thought it must be something like that.
RealNC wrote:The Windows desktop also uses a hardware mouse cursor when you move the mouse, but switches to a rendered cursor when you drag things so that the object that is being dragged doesn't lag behind the cursor. Switching the hardware cursor off produces a flicker which you can observe when you hold down the left mouse button over something draggable (like a window title bar). After about 1 second of holding down the button (or immediately after you start dragging the window), the mouse cursor will flicker as Windows switches the hardware cursor off and draws it by software instead.
Huh, that's really interesting. If you use a program like f.lux, you will see the mouse cursor change color, since hardware cursor bypasses the GPU LUT.
Another thing which will do this is enabling the windows magnifier.

Haste
Posts: 326
Joined: 22 Dec 2013, 09:03

Re: What exactly causes V-sync to introduce input lag?

Post by Haste » 27 Dec 2016, 14:26

For some reason I don't get the cursor flicker with the google chrome titlebar. (I do get it with the explorer titlebar)

edit: nvm I do get it too. But only when I start dragging the window. For the explorer titlebar, just holding the left mouse button for 1 second triggers it.
Monitor: Gigabyte M27Q X

stirner
Posts: 74
Joined: 07 Aug 2014, 04:55

Re: What exactly causes V-sync to introduce input lag?

Post by stirner » 27 Dec 2016, 18:21

With something like f.lux enabled it's actually really easy to distinguish hardware cursor from "rendered" cursor - it's clear white usually and then gets tinted a warmer color temperature when clicking to drag a window.

Btw.: There's no cursor tearing because the Windows DWM uses frame "pacing" or compositing, right? I. e. adaptive, double-buffered VSync? Without a driver installed, tearing occurs but the frame of lag disappears.

User avatar
RealNC
Site Admin
Posts: 3740
Joined: 24 Dec 2013, 18:32
Contact:

Re: What exactly causes V-sync to introduce input lag?

Post by RealNC » 28 Dec 2016, 00:21

stirner wrote:Btw.: There's no cursor tearing because the Windows DWM uses frame "pacing" or compositing, right? I. e. adaptive, double-buffered VSync? Without a driver installed, tearing occurs but the frame of lag disappears.
There is cursor tearing when using a hardware cursor. That's the whole point of it: zero lag for the mouse cursor.

But best of luck actually noticing the tearing on something so small like a mouse cursor :lol: Most of the time, the cursor would either be above or below the tear line anyway. It's rare for the cursor to actually be on the tear line. And even if it's on the tear line, how would you even see the tearing on something so small. Tearing, at least for me, is only visible when a big part of the screen is moving. I can play a pinball game like Pinball FX in portrait mode (so there's no scrolling) with vsync off, and I never notice any tearing, even though the ball itself is even bigger than a mouse cursor...
SteamGitHubStack Overflow
The views and opinions expressed in my posts are my own and do not necessarily reflect the official policy or position of Blur Busters.

flood
Posts: 929
Joined: 21 Dec 2013, 01:25

Re: What exactly causes V-sync to introduce input lag?

Post by flood » 28 Dec 2016, 02:09

only zero lag at the top of the screen :P

Post Reply