Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Prune   Members   -  Reputation: 218

Like
0Likes
Like

Posted 19 May 2014 - 01:02 PM

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...


Edited by Prune, 19 May 2014 - 02:07 PM.

"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)

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 30385

Like
0Likes
Like

Posted 19 May 2014 - 04:39 PM

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.

#3 L. Spiro   Crossbones+   -  Reputation: 13591

Like
5Likes
Like

Posted 19 May 2014 - 06:09 PM

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

Edited by L. Spiro, 19 May 2014 - 06:10 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 Prune   Members   -  Reputation: 218

Like
0Likes
Like

Posted 19 May 2014 - 06:20 PM

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)

#5 L. Spiro   Crossbones+   -  Reputation: 13591

Like
0Likes
Like

Posted 19 May 2014 - 06:29 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 Prune   Members   -  Reputation: 218

Like
0Likes
Like

Posted 19 May 2014 - 06:48 PM

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)

#7 mark ds   Members   -  Reputation: 1259

Like
0Likes
Like

Posted 20 May 2014 - 05:39 AM

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?


Edited by mark ds, 20 May 2014 - 05:41 AM.


#8 L. Spiro   Crossbones+   -  Reputation: 13591

Like
0Likes
Like

Posted 20 May 2014 - 06:48 AM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 Prune   Members   -  Reputation: 218

Like
0Likes
Like

Posted 20 May 2014 - 05:14 PM

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.


Edited by Prune, 20 May 2014 - 07:10 PM.

"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)

#10 mark ds   Members   -  Reputation: 1259

Like
0Likes
Like

Posted 20 May 2014 - 07:55 PM

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.


Edited by mark ds, 20 May 2014 - 07:56 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS