OpenTK-based game stutters intermittently
I'm currently developing a game using OpenTK for rendering; C#, .Net 4.0, OpenGL 2.
Running maximized (not full screen, but taking up all the available screen space) on 1280x1024 resolution, I'm seeing about 400 FPS average. On 1680x1050 resolution, I'm seeing about 315 FPS average. Despite this high frame-rate, I'm experiencing intermittent stuttering.
B开发者_运维问答asically, for about 3 full seconds, the rendering will visibly stutter, then it is perfectly smooth for about 0.5 to 1 seconds. This cycle repeats forever.
There is no frame-rate drop showing on my FPS counter in accordance with the stuttering. I'm calculating the frame rate by counting the frames until I reach a desired update frequency, then calculating the frame-rate using the number of ticks in a second, the number of ticks in the update frequency, and the number of frames that were counted. With this method, I only see an update once every split second, but I've never seen the frame-rate drop far below where I would expect it to be.
The issue is exacerbated significantly if I drag the game window to my second display.
Running the application in a Release build did not resolve the issue.
I performance profiled the application using ANTS by redgate. This pointed out a good few issues that I've since fixed.
Any suggestions here?
This kind of stuttering is generally caused by the garbage collector.
Monitor your collection count and check whether they correspond to stuttering periods (call GC.CollectionCount() with 0, 1 and 2 as parameters). If this is indeed the cause, you will need to profile and reduce memory allocations (object pools can help significantly here, as can structures in place of short-lived classes).
As a rule of thumb, you don't want any gen-1 or gen-2 collections during regular gameplay.
Edit: disable the soft frame-limiter (i.e. call Run() or Run(60) but not Run(60, 60)) and enable vsync. This might help reduce stutter.
Additionally, make sure both monitors are synced to the same rate. Even slight differences will introduce stutter when dragging a window from the primary to a secondary monitor (I've seen this happen in differences as small as 60Hz vs 59.9Hz).
精彩评论