Want to get input messages instantly while waiting for vsync-blocked SwapBuffers()

Started by
8 comments, last by mark ds 9 years, 11 months ago

Typical loop in the window thread is getting all available input messages with PeekMessage() and doing SwapBuffers() (I've also seen GetMessage() with a timer instead of PeekMessage()).

I'm doing all processing with a thread pool and work-stealing task-based system, so all my processing is asynchronous.

The problem is that I don't want to wait until SwapBuffers() is finished to process any input messages that have come in while SwapBuffers() began and blocked waiting for the vsync; I want to have those events immediately, so I can minimize latency by processing them and preparing the data for the next frame already while the render thread is waiting for the vsync to unblock SwapBuffers(). As my input is touch, latency is more of an issue than with keyboard/mouse input because you can visibly see the delta between your finger and the object position (and using position prediction in practice doesn't give good results).

What's especially frustrating is that this problem is analogous to the problem of waiting for both WinAPI events (the kernel object) and for input messages. Yet, while the former problem is solved by the API using MsgWaitForMultipleObjects(), there's no provision for a way to wait for either an input message or buffer swap. Optimally, they should have allowed one to wait on an event triggered by the buffer swap.

So, I'm looking for workarounds. Calling SwapBuffers() on another thread, according to what I've found online, worked on XP, but performs horribly on Vista and newer (I'm targeting Windows 8.x). According to https://www.opengl.org/discussion_boards/showthread.php/182226-SwapBuffers%28%29-in-another-thread it works if the render thread calls glFinish() first before signalling the other thread to swap, but this function in itself blocks so it's a subpar solution (it would be better if glFlush() was sufficient, but I doubt it).

It seems that I'd have to have two windows, one with the GL context doing SwapBuffers(), and the other that gets the input events. The question then becomes how the hell I can get all the inputs to go to the window that is not the one doing the displaying (and I need this to work in both windowed and fullscreen modes). Is there some way to create the input-getting window to be invisible yet always active and on top of the displaying one?

I also looked at wglDelayBeforeSwapNV() but that doens't seem like a great idea, because the OS might very well preempt the thread after that call but before SwapBuffers(), thus missing the swap on that vsync.

I looked at hooks, but there's no touch equivalent to LowLevelMouseProc() which hooks those messages as they're about to be posted on the thread's queue. Hooking can also be WH_GETMESSAGE, but the documentation specifies that hooks runs when the thread calls GetMessage()/PeekMessage(), not when the message is posted. That might have been a solution if it could be set as a global hook, but global can't be used since RegisterTouchWindow() has to be called with a specific window's handle...

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
Advertisement
Maybe check out the doom 3 and/or quake 3 sources -- AFAIK, they used GL and also used a background thread to receive inputs at high frequency.

Is there some way to create the input-getting window to be invisible yet always active and on top of the displaying one?

Since we are talking about Windows, yes. DirectInput does this.

It surely is nothing more than a matter of managing both windows in synchronization (sizes, positions, etc.) while either using flags for the overlay window or simply creating a custom control with no draw routine. I would go with the latter, as it gives you full control over the overlay window and allows it to pass some events to the real window if necessary.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Someone on Stackoverflow suggested that after creating the GL context, one can simply make it current on another thread. It should remain attached to the original window. Sounds too easy to work, but I'm crossing my fingers and will give it a try.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
From your post I was under the impression you had done that and it was not an option. I’ve been back and forth between here and the link you posted and may have gotten mixed up, but it seems he is saying that SwapBuffers() on a second thread is itself a problem because it is really a Windows driver issue, unrelated to OpenGL, so on which thread your context is shouldn’t matter (it would have to be on the second thread in order to call SwapBuffers() there though, so I assumed you had also tried this).

In any case, simply making a second thread, putting my context on it (after creating it on the main thread), and issuing all draw calls and swaps from there was my original plan for all of my platforms (and is already working on iOS), but now you have me worried about the Windows implementation.

Report your results please.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I will try it tomorrow and post here. For reference, this is the Stackoverflow thread: http://stackoverflow.com/questions/23746636/getmessage-while-the-thread-is-blocked-in-swapbuffers
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

This is a commonly used approach to rendering, with the render thread interpolating between 'snapshots' created on the main thread:

WinMain

{

CreateWindow

CreateGLContext

GetGLPointers

CreateRenderThread ---------------------------> TakeOwnershipOfGLContext

while( running ) while( running )

{ {

PeekMessage RenderFrame

HandleInput SwapBuffers

DoAI }

DoPhysics

}

}

If I'm understanding the opengl.org post correctly, then that approach is illegal (al least according to NVidia); although it works most of the time, there are corner cases when it fails. Technically SwapBuffers should be on the main thread, because it was the one that created the window, and therefore owns the DC.

Are other people reading this the same way?

For the record, the point is to improve input response time, and for that you need to move the game logic (AI and physics) to the second thread as well. The main thread should have nothing else to do but to wait for messages from the operating system.

And yes, I was basically reading it that way. Except that I believe the reason is tied to the fact that SwapBuffers() is a Windows® routine, not related to OpenGL specifically. Windows® itself is forcing a lot of restrictions on what you can only do from the main thread of a window, and internally there is probably some transfer of control to the main thread when SwapBuffers() is called from a separate thread, explaining why the other person has 22 FPS instead of his original 30.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Out of curiosity, what sort of tool can I use to examine what internal calls SwapBuffers() is making?

Also, I'm not sure now whether I should implement this method, or the one "MalcolmB" suggests in that thread and says works "in 99% of cases": "I'm doing SwapBuffers() in the window's thread, with no GL context used at all."

[Edit:] making the context current in the other thread does work (as in, it doesn't hang or crash, and I don't see any artifacts), but my testing is not comprehensive and I don't know if there's any performance impact. Windows 8.1 x64, GTX 780.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

Out of curiosity, what sort of tool can I use to examine what internal calls SwapBuffers() is making?

Also, I'm not sure now whether I should implement this method, or the one "MalcolmB" suggests in that thread and says works "in 99% of cases": "I'm doing SwapBuffers() in the window's thread, with no GL context used at all."

[Edit:] making the context current in the other thread does work (as in, it doesn't hang or crash, and I don't see any artifacts), but my testing is not comprehensive and I don't know if there's any performance impact. Windows 8.1 x64, GTX 780.

Honestly, the corner case where it failed (Quadro + Mosaic) is probably not relevant to what you want to do. I've spent half the day looking into this, and I'm comfortable that my approach above will work on everything I target. (the illustration was limited to showing a single extra render thread for the sake of simplicity - L.Spiro was right in pointing out that it was a bad way to organise a game this way).

Also, the MSDN docs state quite clearly that GDI is NOT thread safe (SwapBuffers being a GDI call). However, If you ONLY make draw calls on the render thread, without overlap, I can't see this being a problem, other than when you allow the main thread to handle resize events without informing the render thread (which is just about the limit when dealing with fullscreen games).

The opengl.org OP had a very specific problem related to very specific hardware. You're unlikely to encounter that.

This topic is closed to new replies.

Advertisement