Sign in to follow this  
surogue

OpenGL Detecting if-in-VSync

Recommended Posts

surogue    122
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
zedz    291
under windows

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

the same function can be used to set the swap interval

Share this post


Link to post
Share on other sites
surogue    122
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  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now