luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with disabling V-Sync on Forms

Kirdow opened this issue · comments

I'm using this library using a WinForms control, and I have everything working, except framerate seem to be locked at 60fps which I'm assuming is V-Sync.

I checked the control options, and noticed AnimationTime should be set to 0 if it should run continously, which it currently is set to, additionally I saw SwapInterval should in that case be set to 0 to ensure V-Sync is disabled which it also is currently set to, but I still don't seem to have V-Sync disabled.

This is also the code for the RenderContext as created by the Visual Studio WinForms designer

            this.RenderContext.Animation = true;
            this.RenderContext.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            this.RenderContext.ColorBits = ((uint)(24u));
            this.RenderContext.DepthBits = ((uint)(24u));
            this.RenderContext.Dock = System.Windows.Forms.DockStyle.Fill;
            this.RenderContext.Location = new System.Drawing.Point(0, 0);
            this.RenderContext.MultisampleBits = ((uint)(0u));
            this.RenderContext.Name = "RenderContext";
            this.RenderContext.Size = new System.Drawing.Size(800, 450);
            this.RenderContext.StencilBits = ((uint)(0u));
            this.RenderContext.SwapInterval = 0;
            this.RenderContext.TabIndex = 0;
            this.RenderContext.ContextCreated += new System.EventHandler<OpenGL.GlControlEventArgs>(this.RenderContext_ContextCreated);
            this.RenderContext.Render += new System.EventHandler<OpenGL.GlControlEventArgs>(this.RenderContext_Render);
            this.RenderContext.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
            this.RenderContext.KeyUp += new System.Windows.Forms.KeyEventHandler(this.OnKeyUp);
            this.RenderContext.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
            this.RenderContext.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);
            this.RenderContext.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);

As a side reference the code to check the fps is as follows

private DateTime? lastSecond = null;
private int frameCounter = 0;
private int fps = 0;

private void GameDrawProxy() // Called by the Render event in RenderContext
{
    DateTime timeNow = DateTime.UtcNow;
    if (lastSecond == null)
        lastSecond = timeNow;

    TimeSpan elapsed = timeNow.Subtract(lastSecond.Value);
    if (elapsed.TotalMilliseconds >= 1000.0)
    {
        lastSecond = timeNow;
        fps = frameCounter;
        frameCounter = 0;

#if DEBUG
        System.Diagnostics.Debug.WriteLine($"FPS: {fps}");
#endif
    }

    GameDraw(); // The method doing the actual rendering
    frameCounter++;
}

Lastly the scene being rendered is fairly basic, so I'm pretty sure my GeForce GTX 1060 6GB could handle way more fps than this.

I've observed the same behavior. I identified the culprit in the System.Windows.Forms implementation, which don't let you to overcome the V-Sync signal (infact the UserControl.DoubleBuffer property must be set to true, otherwise no image output will be displayed on screen).

I left the SwapInterval designer property to allow users to set it to 2, in order to limit the frame rates at the GPU driver level (instead of sleeping manually).

I suspect you need to manage manually the windows creation and the relative message pump to allow the application to disable the V-Sync.