Sign in to follow this  

OpenGL Irregular Framerates

Recommended Posts

Hello out there, I am experiencing quite a disturbing effect in my opengl application. Every now and then (usually at around every say 20 frames), my frame time doubles for one frame and the next frame finishes at around 1 ms. As all my animation is time based, the motion appears quite jerky, though the app is running at a moderate frame rate (~ 30 fps @ ATI 9600) In plain numbers, this is an excerpt from what I log : ... Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.236902 Sun Oct 08 17:28:46 2006 DEBUG Render Time 40.363510 Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.301156 Sun Oct 08 17:28:46 2006 DEBUG Render Time 45.085339 Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.304229 Sun Oct 08 17:28:46 2006 DEBUG Render Time 48.914038 Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.300597 Sun Oct 08 17:28:46 2006 DEBUG Render Time 89.839630 // <-- DOUBLED ! Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.343619 Sun Oct 08 17:28:46 2006 DEBUG Render Time 1.100978 // BLAZING FAST ALL OF A SUDDEN ! Sun Oct 08 17:28:46 2006 DEBUG Update Time 0.240813 Sun Oct 08 17:28:46 2006 DEBUG Render Time 43.929606 ... Could this be in conjunction with the vertical sync ? I am using the wglSwapIntervalEXT extension though... Has anybody an idea where to start looking or what I could do about it? I might well interpolate between the update times, but that seems more like a fix then a real solution. Thanks in advance for any suggestions, Christian [Edited by - Novative on October 8, 2006 4:12:41 PM]

Share this post

Link to post
Share on other sites
The obvious suggestion would be ask your profiler where all the time is going.

Alternatively, it could be that the time is spent outside the game. Yes, that happens, if other processes need the CPU. (an extra 40 ms (if it is milliseconds you're measuring) sounds a bit excessive for that though)

Share this post

Link to post
Share on other sites
Thanks for the suggestions :) Unfortunately it does not seem to be the code as such that gives rise to this problem, instead what really stuns me, is that the following frame completes in about 1 ms, though it runs through all the same code that averagely needs 40 ms to complete otherwise. How can this be possible ?

The effect is repeatable (with varying time between the peaks) so I am tempted to rule out the influence of other processes...and am left rather puzzled about this rather annoying behaviour.

Thanks anyway :)

Share this post

Link to post
Share on other sites
Unfortunately not, this is why this problem troubles me so much. I can tell by the jerky movements that the problem still persists when I have the logging turned off.

Maybe I try a different machine any time soon, just to rule out some weird influences.

Is there any logical explanation why my code can finish rendering in 1 ms ? It seems as if two frames get batched up (thus taking roughly twice the time) and the opengl calls of the succeeding frame are ignored/delayed (thus the heavy time saving). But this is beyond my knowledge of opengl (windows?) synchronization.

P.S: The code is not multi-threaded, its a plain update/renderloop in double buffer mode under WinXP.

Share this post

Link to post
Share on other sites
In addition to posting the code, maybe upload a stripped down version of the app. (simple enough to duplicate the issue) for others to try.

If the problem is happening across multiple computers, focus on your code. If other computers are not affected, then the issue would seem to be with another process running in the background sucking up your resources (spyware?).

Also be sure to check for any Sleep(rand()%100); calls that might be mysteriously placed throughout your code [grin].

- Dan

Share this post

Link to post
Share on other sites
I had this problem once too, and i found that i had memory leaks. The framerate would go very low for a millisecond every few seconds. It was something to do with how the OS (mac osX in my case) was handling the page files; run your program, look in task manager and note down the amount of memory its using (virtual and physical) and come back an hour later and look.
My application was leaking one struct of about 24 bytes per frame, and managed to use up all 768mb of my physical memory within four hours of execution. look out for memory leaks when using malloc()!

Share this post

Link to post
Share on other sites
[edit]seeing as your probelm is to do with the time rendering is taking, my suggestions are probably wrong. However, how are you timing how long it takes to render?[/edit]

I had a simmilar problem when I was using the performance timer incorrectly.
My guess is you're losing precision in your timer code, which could have the effect of returning times that are shorter than, or longer than the actual elapsed time for one frame.
eg. once I corrected my timer code and compared it with the old version I found results like this (nb. values are from memory and are illustrative):
actual | returned
33ms | 16ms
31ms | 30ms
31ms | 30ms
31ms | 80ms
32ms | 16ms
36ms | 60ms

This caused the game to animate very jerky, appearing smooth for a few frames, then jumping, then stalling, the going smooth again...

A "quick fix" is to average the timer for the last few frames, but thats a real hack... Perhaps you could post your timer code?

Share this post

Link to post
Share on other sites
Thanks for all the valuable input :)

I checked my timer implementation (thanks @PhilMorton for the hint :)) against simple GetTickCount() difference(am using QueryPerformanceCounter otherwise) around my render-code.

The timings differ within their natural resolution, but the overall picture is the same (with the second value being the GetTickCount difference) :

Mon Oct 09 07:34:21 2006 DEBUG Render Time 34.136461 / 31
Mon Oct 09 07:34:21 2006 DEBUG Render Time 74.885775 / 78
Mon Oct 09 07:34:21 2006 DEBUG Render Time 0.895365 / 0 // !!!
Mon Oct 09 07:34:21 2006 DEBUG Render Time 34.011306 / 47
Mon Oct 09 07:34:21 2006 DEBUG Render Time 75.542283 / 63
Mon Oct 09 07:34:21 2006 DEBUG Render Time 0.910730 / 0 // !!!!

I also checked for memory leaks (thanks @ speciesUnknown) , but at least there I seem to have done my homework ;)

Tried it on my laptop just right now, its the same. I put together a little binary package that should contain all that is needed to run my little sample application. Please note however that the source is not thoroughly tested and that I do not assume that it will run on any hardware (unfortunately for now FBO and VBO support is hardcoded ...)

I would be really grateful if someone might take a look at it.

All you need to do is start the program, let it run for a few seconds (use arrow keys for basic orbiting around if you feel that the vast sample scene is a little too boring to look at;)) and then check the log file. I added a little marker, that shows up at the end of the line whenever the actual render time is 1.5 higher than the previous. This falsely marks a few spots where rendering time is higher due to changed viewing distance, but most of the time it hits the spots I am looking for.

Thanks for the support and in advance for everyone that risks having a look. here is the link to the binary :

Sample App [1.2 MB]

Please report, if any files are missing.

Share this post

Link to post
Share on other sites
Thanks a lot for trying :) You're right, the scene does not impose much load onto either GPU nor CPU, but I didnt want to waste network bandwidth with a huge scene to download.

I had a look at the logfile and though not as clear as my local values (i should be thankful for my slow computer I guess ;)) the trend is still there.

I finegrained my time measurements more and arrived at the conclusion that its all about the swapping time. It varies from ~1 ms to ~10 ms and thus turning my time based animation into sth. rather useless.

Does anyone have some recipe for keeping the swap times in a moderate range ? Or can give a detailed explanation of what is going wrong ? I assume its something about the vertical sync, but the effect should then be constant, shouldnt it ?

I wonder then how to setup a proper time based animation scheme that can handle this type of varying update times (as a result of my varying frame times).
But maybe I start a new post for this.

Thanks to everyone, I really appreciate your help !

Excerpts from recent loggings :

normal run :

Mon Oct 09 13:16:17 2006 DEBUG PreFrameTime 0.051403
Mon Oct 09 13:16:17 2006 DEBUG InFrameTime 0.830552
Mon Oct 09 13:16:17 2006 DEBUG Swap time: 0.421841
Mon Oct 09 13:16:17 2006 DEBUG Render Time 2.564851 / 0

a) 90% of all cases are caused by a buffer swap time which peeks every now and then and takes up 5-20 times longer than usually, suggesting that I run into some kind of vertical sync-miss. (somebody please correct me, if this is a wrong conclusion)

Mon Oct 09 13:16:17 2006 DEBUG PreFrameTime 0.049727
Mon Oct 09 13:16:17 2006 DEBUG InFrameTime 0.821333
Mon Oct 09 13:16:17 2006 DEBUG Swap time: 10.032839
Mon Oct 09 13:16:17 2006 DEBUG Render Time 12.226694 / 16

b) the rest is caused by some misterious and yet to be resolved time loss, suggesting that I am maybe scheduled away by the OS.

NOTE : Swap is the last thing I do before I return from the render function and measure the render time.

Mon Oct 09 13:16:17 2006 DEBUG PreFrameTime 0.050286
Mon Oct 09 13:16:17 2006 DEBUG InFrameTime 0.825524
Mon Oct 09 13:16:17 2006 DEBUG Swap time: 0.460952
Mon Oct 09 13:16:17 2006 DEBUG Render Time 8.515887 / 15

Share this post

Link to post
Share on other sites
So are you using the vsync to time your app? In so that you expect that each frame is N milliseconds long and advance by that each frame? Or are you taking the time-delta between last and current frame and use that for animation?

The latter approach should work with any framerates, no matter how variable they are.

And there's nothing wrong in interpolating the times. You could store last 4 or more states (just positions) and interpolate between them, based on how much time has gone between them, rather than discreetly updating each frame. This is actually a way you can improve physics modelling anyway, so in a way it is a "correct" solution.


Share this post

Link to post
Share on other sites
No, I am not using the vsync to time my app. I simply keep it going and take the delta time between the last update and the current to advance my scene.

Unfortunately the varying frame rates cause the motion to appear jerky every once in a while, because the objects would "jump" a little, when the last frame suddenly took twice as long as the one before (as the delta time would be much higher and thus the way objects move). This kills any smooth movements.

This is no problem if the framerates are high enough (as the delta times are rather small and even doubling them makes not much difference positionwise), but when I am around 30fps it gets noticable dependend on the speed of the object.

Maybe I should really go and interpolate update times to smooth out the sudden peaks. The result of interpolating is an incorrect state though (as objects at time t are not where they are supposed to be, but rather at some time t' =! t), which might annihilate, when t varies around t'). But this is just a theoretical thought.

Have you ever applied this in practice ?

Thanks for the hint :)

Share this post

Link to post
Share on other sites
Original post by Novative
Have you ever applied this in practice ?

No, I only read about it, and even those were using it for physics modeling. You might wanna look up midpoint-method or runge-kutta if you're out for specific algorithms, even though the midpoint is the brute force method that you'd think first too.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By test opty
      Hi all,
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
  • Popular Now