Sign in to follow this  

OpenGL Detecting if-in-VSync

Recommended Posts

I don't know if anybody can help me, but I am having an unusual "slug" in my game using Mingw & SDL w/ OpenGL. Basically what happens is that at various seemingly chaotic times, the time it takes to render a frame will jump horribly from like it's normal 10-20 milliseconds on my (crappy) computer up to like 50 milliseconds and as a result (since in order to maintain 60fps, you must complete your logic and rendering within ~16 milliseconds each frame, 1000/60=16.) the whole game bogs down. This usually happens when there are a large number of objects on the screen or if you try to start a big war with a lot of people shooting at a lot of other people. However although it occurs frequently it does not occur reliably every time you do this. The specifics of this bug are complex and somewhat irrevalent because I already am pretty sure I know what the problem is, but I will explain a little bit more background before I get to my question so you can understand better. If you don't like reading long stuff just skip down to the bottom where I ask the question. I know that it is not just bogging down because there is a lot of stuff going on. Here are the reasons I know that, and some interestingly relavent points: 1) Lots of times, I can render just as many objects as I am rendering when it bogs down, and there is only marginal increase in render time. 2) My "logic time", the game logic, is fully decoupled from the "blit time", the renderer, it is as if they run in seperate threads, the logic running at a constant 100 fps and the renderer taking a "snapshot" whenever it can afford to (they do not literally run in seperate threads but it is programmed such that it is the same conceptually). My logic time does not increase significantly during the bog down. Therefore it is definetely the rendering which is slowing things down and not my game logic. 3) *MOST OF ALL*, once it is bogged down, it has a tendency to stay bogged down, even if I stop the war and everybody just sits there on the screen motionless executing no logic--THIS IS THE EXACT SAME SCENE as I could previously render in 10ms, yet after this bug has occurred it takes upwards of 50ms to render. 4) If it is bogged down and I push a debug key which disables the rendering, then push it again to turn rendering back on, it is no longer bogged down for a few seconds, then it bogs down again. 5) PERHAPS MOST IMPORTANTLY OF ALL I have, up till I got around to looking into this, had my renderer capped so that it would not blit faster than 20fps. The reason is very interesting--if I blit no faster than ~20fps, it takes about 10ms to render each frame. But if I let it blit too much faster than that, even though I have about 50% of my CPU time left over just sitting there getting burned off, it takes longer and longer to render the frames, and if I let it blit anytime it can, it takes a incredibly sluggish 50ms and bogs everything down. Now point number 5 makes no sense. The higher a frame rate you have, the more CPU speed you have to have. However each frame should take the same amount of time to render whether you are rendering at 100fps or 10fps. It requires more speed only because you are rendering 10 times as many per second. The solution What I think is happening is that when I call SDL_GL_SwapBuffers, it is waiting until v-sync before it blits the frame. What this means is that if you only blit frames at like 20fps, then the total amount of time wasted per second inside GL_SwapBuffers is small and not noticable. However, if instead you try to push frames out as fast as possible, then even if your computer can render 300 fps, each frame will block until the next vsync, and so you are burning off much more time blocked inside SwapBuffers, and so the framerate you can support becomes lower. This theory is supported by the fact that my measurements indicate that calling the renderer alone with no game logic as fast as possible, it takes longer to render each frame than calling the renderer 20 times per second. However if SDL_GL_SwapBuffers is commented out, it makes no difference how often you call it on the time the function takes to complete. And now to the real question So when my game happens to go over 16 ms for a few frames in a row, the block-until-vsync caused by SDL_GL_SwapBuffers means it starts taking longer to render each frame. This causes it to bog down more, and more, and more, and stay stuck that way even after whatever originally caused the brief lag is gone. What I want to do is: * Call some function which will return whether or not I am in v-sync. That way I can still start my renders in v-sync to avoid tearing, but I will have control over the wait state and so my entire program will not be suspended-- I can be cleverer and only try to swap the buffers when I know there will be no waiting. * Preferably, I would also like to disable SDL's/OpenGL's blocking until v-sync entirely, and just depend on my own logic to only blit when it's a good time. I am NOT running this application fullscreen. I am running it in a window. I know for a fact that when it is fullscreen I am stuck at 60fps. However it appears to be still syncing to something, although at about 110fps, while in a window.

Share this post

Link to post
Share on other sites
under windows

The current swap interval for the window
associated with the current context can be obtained by calling

the same function can be used to set the swap interval

Share this post

Link to post
Share on other sites
Yes, I know about that function but it does not help because all it does is tell you whether vertical syncing is enabled or not. It does not tell you whether your card is currently in a vsync cycle. Therefore you have no way of timing your buffer swaps to sync up with the vsync and so much valuble time gets wasted while opengl suspends your program waiting for vsync. Moreover, my computer does not even have the wgl extensions so they do not work for me anyway.

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  

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now