Display image with qt & opengl, timing accuracy and vsync issues, c++

佐手、 提交于 2019-12-12 03:35:19

问题


I'm building a module that is supposed to display images at a certain rate (not pre defined, but not very high - 10Hz max for the swapping of images).

From my research I got to the conclusion that QGLWidget is the right tool for this task, after enabling vsync with openGL calls(SwapInterval family).

Yet, I am not sure how to actually implement the swapping mechanisem - should I use a timer? If I set a timer for 333.3 ms(3 Hz), when the refresh rate is 60 Hz (16.67 per cycle, thus the timer is 20 cycles), and I be sure that timing will be fine? And if the rate should be 9Hz, I need to set the timer for 100+16.67 because this is the best I can get? And if a timer is ok, should I just call paintGL() when it sends me the timeout event?

Thanks


回答1:


should I use a timer?

Yes, but not in a naive way. If you'd simply use a timer for pinpointing the presentation of the images your timer frequency will beat against the display V-Sync/refresh oscillator – program timers run from a different clock source than the display output.

This beating will result in missed swap intervals, which will be perceived as frame stuttering.

Instead you should do the following: Use a V-Synced buffer swap (SwapBuffers¹) as a reference point to start a high precision time measurement timer.

Then render the frame for the next presentation time in the future you aim for; take into account that frame intervals come in at the display refresh interval granularity – unless G-Sync or FreeSync are used. Use glFinish to force completion of the frame rendering process, then stop the timer and determine how long it took to render the frame. If the frame was finished earlier than the refresh period you did aim for add a (high resolution delay) that delays your program into the aimed for display period (aim for the middle of the period), followed by a SwapBuffers which will be the reference point for the next iteration.


¹: This will work reliably only for Nvidia and AMD cards and their proprietary drivers. The drivers for Intel GPUs have a different timing behaviour.



来源:https://stackoverflow.com/questions/41999427/display-image-with-qt-opengl-timing-accuracy-and-vsync-issues-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!