-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[StoreApps/XAML] Run Game Loop and Input on a seperate Thread #5520
[StoreApps/XAML] Run Game Loop and Input on a seperate Thread #5520
Conversation
-Process Input on a working thread. -Copy mouse state in a single step. -Set mouse ButtonX1 & ButtonX2 state.
-Add method UAPGameWindow.ProcessWindowEvents() -Add method MetroGameWindow.ProcessWindowEvents()
Key events come from the Main thread and write to Keyboard._nextKeyboardState. Game loop thread performs a one-time copy of _nextKeyboardState to _keyboardState before Tick(). KeyboardState is a bit array of Pressed key states and there re no invalid states in the way we Set/Clear bits. There is no need to perform more strict syncronization.
ApplicationView.GetForCurrentView() can only be called from UIThread.
@Danthekilla, @MartinZikmund, @VitezslavImrysek @jakepoz, |
Hey nkast, will do a check here ASAP. We are also working on our own solution with @roponator so we will compare notes. @roponator is making a fuzz tester that will run some live games overnight with very intense workloads. We want to use that we want to use as a way to verify that the multi-threading is reliable. We are hoping to get a fix for this issue into 3.6. I feel it's a decent time to ask developers to adjust their code to Dispatch UI thread calls. Also, publishing a UWP app without this fix is just asking for lots of user complaints as we have unfortunately found out the hard way. |
Probably will have to be for 3.6.1 as 3.6 will be released this week. |
@Jjagg Got it, I didn't know it was coming so soon. Either way, we are testing this fix ASAP. @nkast Just gave this a quick test run and it seems to be working. We are going to build some of our games against this PR and run them overnight in a fuzz tester. It has been really good finding low frequency deadlocks and similar threading problems. |
@jakepoz - We should look at your solution as well... is it similar to this? We will get this issue solved and shipped in a 3.6.1 release. |
Yeah, it is similar. Instead of saving the state of each event that comes in off the main thread, and then "replaying" it like this PR does, @roponator was pausing the game loop, letting the event occur, and then resuming the game loop. @nkast solution looks more robust, we are doing some overnight fuzz testing and will know the results shortly. |
I think @nkast 's solution is better than mine since it doesn't lock the main loop since it stores just the last window event. I thought that may be a problem (eg if old window events would be discarded) but the ~8 hour overnight fuzz testing (all possible window change events like minimize, maximize, resize, screen rotation and then game input simulation) worked 100% fine on nkasts version so his should be used instead :) |
Sweet, we will do a live deployment on a small game of ours and see what the Health reports look like after a week. That should make good use of the time between now and getting this into 3.6.1 :) |
Ok, we have 2 live games updated with this PR. Will report back in a few days on the results. |
Thanks for the testing, Jake. It is very helpful.
|
I am going to check tomorrow and report back the results :). |
Thanks everyone for testing this.
That sounds interesting! +1 |
Hey, Everything looks great on the Windows 10 desktop side. But there is a problem on Windows Phone 10 where it will crash on the first button you press:
That happens on the following line in InputEvents.cs
|
Thanks @jakepoz |
Move DIP factor stuff outside of each event handler
@jakepoz |
@nkast Kk, just confirmed the health reports and everything is good. We have this deployed to a few thousand active users without issue. Can we merge this in now? We also have this bounty open on this issue that we can finally close out: |
I tested this PR and the input lag seems to be fixed. I haven't noticed any problems so far. |
…d from a different thread
Fixing the Media Player dispatcher
Thanks for all the work on this. Great to have a large test userbase. I'm happy to merge this now. |
@nkast - Done! |
…me#5520) * Input thread -Process Input on a working thread. -Copy mouse state in a single step. -Set mouse ButtonX1 & ButtonX2 state. * -Rename _windowEvents -> _inputEvents -Add method UAPGameWindow.ProcessWindowEvents() -Add method MetroGameWindow.ProcessWindowEvents() * Sync Keyboard events Key events come from the Main thread and write to Keyboard._nextKeyboardState. Game loop thread performs a one-time copy of _nextKeyboardState to _keyboardState before Tick(). KeyboardState is a bit array of Pressed key states and there re no invalid states in the way we Set/Clear bits. There is no need to perform more strict syncronization. * [UAP] Sync TextInput events * [UAP] Get ApplicationView from GameWindow ApplicationView.GetForCurrentView() can only be called from UIThread. * [UAP] Dispatch SwapChainPanel.CompositionScale * [UAP] Dispatch CoreWindow.PointerCursor * [UAP] Update window Size before Tick() * [UAP] Update Orientation before Tick() * [UAP] Game loop thread * [W8.1/WP8.1] Update window size before Tick() * [W8.1/WP8.1] Update Orientation before Tick() * [UAP] Update Focus before Tick() * [W8.1/WP8.1] Dispatch CoreWindow.PointerCursor * [W8.1/]WP8.1] Game loop thread * [W8.1/WP8.1] Update Focus before Tick() * Fix SwapChainBackgroundPanel size/scale bug * Move DIP factor stuff outside of each event handler * Move DIPs comments * The MediaPlayer needs to get a proper dispatcher now that it is called from a different thread
Fixes mouse, keyboard lag [UAP] touch, rotation lag & freeze (WP8.1).
#4897, #5300, #3948, #3589, #3316, #4105(?).
This probably still has a ton of bugs. It's hard to tell unless you review every line of MG codebase and run extensive tests. Unfortunetly, that's the nature of thread syncronization. I'd prefer to wait until after 3.6 (and a possible 3.6.1 service update) for this PR.
On the other hand #5352 is more predictable and solves 99% of the issues with just a couple of lines.