Mice with high polling rate causes significant lag on Windows
There was already a ticket about a similar issue but it was closed https://github.com/libsdl-org/SDL/issues/4376
I have a Razer Viper 8Khz and when playing CS2, the game gets very laggy on Windows when moving the mouse. This does not happen when playing on the Linux version of CS2.
I think this is because SDL is using GetRawInputData, this is known to not work properly with mice that have a high polling rate. GetRawInputData is acquiring a lock on every call, and this causes significant performance loss because a high number of calls are needed when there is a high polling rate.
The way to fix this issue would be to use GetRawInputBuffer https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getrawinputbuffer which can retrieves multiple input events at once.
Valorant had the same issue (they were using GetRawInputData), and they later added a game setting called "Raw Input Buffer" to make use of GetRawInputBuffer, and it fixes this same issue. See here for more information https://www.reddit.com/r/VALORANT/comments/r5w10e/comment/hmplvwh/?utm_source=reddit&utm_medium=web2x&context=3 https://www.thespike.gg/valorant/tips/what-is-raw-input-buffer-in-valorant It has also became the default setting in Valorant.
EDIT: Also, the Unity game engine was affected by the same issue. More information here: https://forum.unity.com/threads/windows-mouse-raw-input-mouse-handling-broken-in-rewired.1172786/#post-7514816
By the way, a second optimization may be possible by passing RIDEV_NOLEGACY to RegisterRawInputDevices to disable legacy mouse events. This would reduce the number of messages that needs to be processed.
It seems like this would require more changes though as SDL seems to handle mouse clicks and other things through legacy messages rather than raw input.
@AnAkkk, can you try this out? You won't be able to drop this into CS2, but you can test it with the SDL test programs.
RIDEV_NOLEGACY is a possibility, but I'd like to try that as a separate change, in case it affects other non-client area mouse interactions with the window.
I'm not sure how to test this, it doesn't seem like any of the tests are using raw input. I tried to add SDL_SetRelativeMouseMode(SDL_TRUE); inside testrendertarget.c.
But when I move my mouse the app just crashes inside WIN_PollRawMouseInput.
The data->rawinput pointer sometimes point to invalid data.
Then NEXTRAWINPUTBLOCK will just fetch an invalid pointer because dwSize is not correct, and the app crashes here https://github.com/libsdl-org/SDL/commit/8fe4a45edf7fcba60b15e44132d701a5afd1e70c#diff-d27e7b8a1f0400e2c224b0fc18ee8fd30dfbf8f793cc85b90525a5913154b59bR667
It doesn't happen every time the function is called. I can move the mouse for a few seconds, and can see that it processes many events, but eventually crashes.
EDIT:
The problem appears to be linked to the logic in this block https://github.com/libsdl-org/SDL/commit/8fe4a45edf7fcba60b15e44132d701a5afd1e70c#diff-d27e7b8a1f0400e2c224b0fc18ee8fd30dfbf8f793cc85b90525a5913154b59bR641 and how the buffer is allocated. During the same call of the function it overwrites data->rawinput multiple times, and loses the previous data, which is why the buffer becomes invalid and leads to the crash.
It looks like there is also a memory leak since data->rawinput is not freed.
Doh, that was supposed to be a realloc(), fixed in 2b369a14a
data->rawinput is freed in CleanupWindowData().
BTW, I was testing this with testsprite, using Ctrl-R to toggle relative mode.
Thank you, I can confirm the crash is fixed. I have managed to test in testsprite with and without the fix, and it works much better now. Before the app was very slow while moving the mouse and FPS were dropping quite a lot. Now it works properly.
Would you have an idea when SDL will next be updated in CS2 to include this fix?
Would you have an idea when SDL will next be updated in CS2 to include this fix?
Soon! :)
FYI, this is a risky change for overall event processing, so the CS2 team is going to test it internally for a while before it goes public.
FYI, this is a risky change for overall event processing, so the CS2 team is going to test it internally for a while before it goes public.
Would this solution be available in SDL 2.3.x (or 2.x.x) at some time in the future? I just noticed the issue myself when not using SDL_SetRelativeMouseMode(SDL_TRUE); on a 4khz polling rate mouse.
I'm not planning to backport this, but you're welcome to use sdl2-compat with SDL3 to get this improvement for free.
FYI, this is a risky change for overall event processing, so the CS2 team is going to test it internally for a while before it goes public.
Any update on this? I do get some input skips on framespikes, will this help?
CS2 is now using this.
hello from russia I've been digging into this problem for over a week the polling rate is constantly exceeded by 5-15 Hz... due to which I encounter lags especially in old games like sc blacklist I managed to partially fix this problem... (it even seemed to me that in addition to the fact that fps drops went away, the sound quality of music also became better (smsl c200 pro))
but I noticed when we play music through my external DAC, and I check the polling rate of the mouse on the website, these polling rate exceedances occur MUCH MORE OFTEN! I'm not a programmer and I don't understand what you wrote above, but if you tell me something, I'll be very grateful to you!!!!!!!