From 83f54f70aac177ca2724319bd291f8f1e5316311 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Mon, 6 Jan 2014 01:57:54 +0100 Subject: [PATCH 01/10] [OpenTK] Simplify and improve timing calculations This patch modifies GameWindow.Run() to use a single stopwatch instead of two separate stopwatches for timing UpdateFrame and RenderFrame events. It improves timing accuracy for issue #20 (FrameEventArgs.Time Inconsistencies) --- Source/OpenTK/GameWindow.cs | 132 +++++++++++++++++------------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 792869f8..0f41a3f8 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -87,7 +87,6 @@ namespace OpenTK double update_time, render_time; VSyncMode vsync; - Stopwatch update_watch = new Stopwatch(), render_watch = new Stopwatch(); double next_render = 0.0, next_update = 0.0; FrameEventArgs update_args = new FrameEventArgs(); FrameEventArgs render_args = new FrameEventArgs(); @@ -403,9 +402,12 @@ namespace OpenTK //Move += DispatchUpdateAndRenderFrame; //Resize += DispatchUpdateAndRenderFrame; + Debug.Print("Calibrating Stopwatch to account for drift"); + CalibrateStopwatch(); + Debug.Print("Stopwatch overhead: {0}", stopwatch_overhead); + Debug.Print("Entering main loop."); - update_watch.Start(); - render_watch.Start(); + watch.Start(); while (true) { ProcessEvents(); @@ -429,100 +431,94 @@ namespace OpenTK } } - void DispatchUpdateAndRenderFrame(object sender, EventArgs e) + double stopwatch_overhead = 0; + void CalibrateStopwatch() { - RaiseUpdateFrame(update_watch, ref next_update, update_args); - RaiseRenderFrame(render_watch, ref next_render, render_args); + // Make sure everything is JITted + watch.Start(); + stopwatch_overhead = watch.Elapsed.TotalSeconds; + watch.Stop(); + watch.Reset(); + // Measure stopwatch overhead + const int count = 10; + for (int i = 0; i < count; i++) + { + watch.Start(); + double sample = watch.Elapsed.TotalSeconds; + if (sample < 0 || sample > 0.1) + { + // calculation failed, repeat + i--; + continue; + } + stopwatch_overhead += sample; + watch.Stop(); + watch.Reset(); + } + stopwatch_overhead /= 10; } - void RaiseUpdateFrame(Stopwatch update_watch, ref double next_update, FrameEventArgs update_args) + Stopwatch watch = new Stopwatch(); + double update_timestamp = 0; + double render_timestamp = 0; + double update_elapsed = 0; + double render_elapsed = 0; + void DispatchUpdateAndRenderFrame(object sender, EventArgs e) { - int num_updates = 0; - double total_update_time = 0; + const int max_frameskip = 10; + int frameskip = 0; + double timestamp = 0; - // Cap the maximum time drift to 1 second (e.g. when the process is suspended). - double time = update_watch.Elapsed.TotalSeconds; - if (time <= 0) + do { - // Protect against negative Stopwatch.Elapsed values. - // See http://connect.microsoft.com/VisualStudio/feedback/details/94083/stopwatch-returns-negative-elapsed-time - update_watch.Reset(); - update_watch.Start(); - return; - } - if (time > 1.0) - time = 1.0; + // Raise UpdateFrame events until we catch up with our target update rate. + timestamp = watch.Elapsed.TotalSeconds; + update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0); + update_timestamp = timestamp; + RaiseUpdateFrame(update_elapsed, ref next_update); + } while (next_update > 0 && ++frameskip < max_frameskip); + // Calculate statistics + //update_period = total_update_time / (double)num_updates; - // Raise UpdateFrame events until we catch up with our target update rate. - while (next_update - time <= 0 && time > 0) + timestamp = watch.Elapsed.TotalSeconds; + render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); + render_timestamp = timestamp; + RaiseRenderFrame(render_elapsed, ref next_render); + } + + void RaiseUpdateFrame(double time, ref double next_update) + { + double time_left = next_render - time; + if (time_left <= 0.0 && time > 0) { - next_update -= time; update_args.Time = time; OnUpdateFrameInternal(update_args); - time = update_time = Math.Max(update_watch.Elapsed.TotalSeconds, 0) - time; - // Stopwatches are not accurate over long time periods. - // We accumulate the total elapsed time into the time variable - // while reseting the Stopwatch frequently. - update_watch.Reset(); - update_watch.Start(); // Don't schedule a new update more than 1 second in the future. // Sometimes the hardware cannot keep up with updates // (e.g. when the update rate is too high, or the UpdateFrame processing // is too costly). This cap ensures we can catch up in a reasonable time // once the load becomes lighter. - next_update += TargetUpdatePeriod; + next_update = time_left + TargetUpdatePeriod; next_update = Math.Max(next_update, -1.0); - - total_update_time += update_time; - - // Allow up to 10 consecutive UpdateFrame events to prevent the - // application from "hanging" when the hardware cannot keep up - // with the requested update rate. - if (++num_updates >= 10 || TargetUpdateFrequency == 0.0) - break; - } - - // Calculate statistics - if (num_updates > 0) - { - update_period = total_update_time / (double)num_updates; } } - void RaiseRenderFrame(Stopwatch render_watch, ref double next_render, FrameEventArgs render_args) + void RaiseRenderFrame(double time, ref double next_render) { - // Cap the maximum time drift to 1 second (e.g. when the process is suspended). - double time = render_watch.Elapsed.TotalSeconds; - if (time <= 0) - { - // Protect against negative Stopwatch.Elapsed values. - // See http://connect.microsoft.com/VisualStudio/feedback/details/94083/stopwatch-returns-negative-elapsed-time - render_watch.Reset(); - render_watch.Start(); - return; - } - if (time > 1.0) - time = 1.0; double time_left = next_render - time; - if (time_left <= 0.0 && time > 0) { - // Schedule next render event. The 1 second cap ensures - // the process does not appear to hang. - next_render = time_left + TargetRenderPeriod; - if (next_render < -1.0) - next_render = -1.0; - - render_watch.Reset(); - render_watch.Start(); - if (time > 0) { render_period = render_args.Time = time; OnRenderFrameInternal(render_args); - render_time = render_watch.Elapsed.TotalSeconds; } + + // Schedule next render event. The 1 second cap ensures + // the process does not appear to hang. + next_render = time_left + TargetRenderPeriod; + next_update = Math.Max(next_update, -1.0); } } From d49dacb5b3b850ff59c8f634dae9db588264f6b6 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 00:48:09 +0100 Subject: [PATCH 02/10] [Examples] Cleaned up input device printing in GameWindowStates --- .../Examples/OpenTK/Test/GameWindowStates.cs | 179 ++++++++++-------- 1 file changed, 98 insertions(+), 81 deletions(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index b5bf17f1..4fdbee4c 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -25,8 +25,6 @@ namespace Examples.Tests int texture; bool mouse_in_window = false; bool viewport_changed = true; - MouseState mouse; - KeyboardState keyboard; public GameWindowStates() : base(800, 600, GraphicsMode.Default) @@ -102,80 +100,103 @@ namespace Examples.Tests return val > max ? max : val < min ? min : val; } - static void DrawString(Graphics gfx, string str, int line) + static float DrawString(Graphics gfx, string str, int line) { - gfx.DrawString(str, TextFont, Brushes.White, new PointF(0, line * TextFont.Height)); + return DrawString(gfx, str, line, 0); } - static void DrawString(Graphics gfx, string str, int line, float offset) + static float DrawString(Graphics gfx, string str, int line, float offset) { gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height)); + return offset + gfx.MeasureString(str, TextFont).Width; } - static void DrawKeyboard(Graphics gfx, KeyboardState keyboard, int line) + static int DrawKeyboards(Graphics gfx, int line) { - const string str = "Keys pressed:"; - float space = gfx.MeasureString(" ", TextFont).Width; - float offset = gfx.MeasureString(str, TextFont).Width + space; - DrawString(gfx, str, line); - for (int i = 0; i < (int)Key.LastKey; i++) + line++; + DrawString(gfx, "Keyboard:", line++); + for (int i = 0; i < 4; i++) { - Key k = (Key)i; - if (keyboard[k]) + var state = OpenTK.Input.Keyboard.GetState(i); + if (state.IsConnected) { - string key = k.ToString(); - DrawString(gfx, key, line, offset); - offset += gfx.MeasureString(key, TextFont).Width + space; + StringBuilder sb = new StringBuilder(); + sb.Append(i); + sb.Append(": "); + for (int key_index = 0; key_index < (int)Key.LastKey; key_index++) + { + Key k = (Key)key_index; + if (state[k]) + { + sb.Append(k); + sb.Append(" "); + } + } + DrawString(gfx, sb.ToString(), line++); } } + return line; } - static void DrawMouse(Graphics gfx, MouseState mouse, int line) + static int DrawMice(Graphics gfx, int line) { - const string str = "Buttons pressed:"; - float space = gfx.MeasureString(" ", TextFont).Width; - float offset = gfx.MeasureString(str, TextFont).Width + space; - DrawString(gfx, str, line); - for (int i = 0; i < (int)MouseButton.LastButton; i++) + line++; + DrawString(gfx, "Mouse:", line++); + for (int i = 0; i < 4; i++) { - MouseButton b = (MouseButton)i; - if (mouse[b]) + var state = OpenTK.Input.Mouse.GetState(i); + if (state.IsConnected) { - string button = b.ToString(); - DrawString(gfx, button, line, offset); - offset += gfx.MeasureString(button, TextFont).Width + space; + StringBuilder sb = new StringBuilder(); + Vector3 pos = new Vector3(state.X, state.Y, state.WheelPrecise); + sb.Append(i); + sb.Append(": "); + sb.Append(pos); + for (int button_index = 0; button_index < (int)MouseButton.LastButton; button_index++) + { + MouseButton b = (MouseButton)button_index; + if (state[b]) + { + sb.Append(b); + sb.Append(" "); + } + } + DrawString(gfx, sb.ToString(), line++); } } + return line; } - static int DrawJoysticks(Graphics gfx, IList joysticks, int line) + static int DrawLegacyJoysticks(Graphics gfx, IList joysticks, int line) { - float space = gfx.MeasureString(" ", TextFont).Width; + line++; + DrawString(gfx, "Legacy Joystick:", line++); + int joy_index = -1; foreach (var joy in joysticks) { - string str = String.Format("Joystick '{0}': ", joy.Description); - DrawString(gfx, str, line); - - float offset = 0; - line++; - for (int i = 0; i < joy.Axis.Count; i++) + joy_index++; + if (!String.IsNullOrEmpty(joy.Description)) { - string axis = joy.Axis[i].ToString(); - DrawString(gfx, axis, line, offset); - offset += gfx.MeasureString(axis, TextFont).Width + space; - } + StringBuilder sb = new StringBuilder(); + sb.Append(joy_index); + sb.Append(": '"); + sb.Append(joy.Description); + sb.Append("' "); - offset = 0; - line++; - for (int i = 0; i < joy.Button.Count; i++) - { - string button = joy.Button[i].ToString(); - DrawString(gfx, button, line, offset); - offset += gfx.MeasureString(button, TextFont).Width + space; - } + for (int i = 0; i < joy.Axis.Count; i++) + { + sb.Append(joy.Axis[i]); + sb.Append(" "); + } - line++; + for (int i = 0; i < joy.Button.Count; i++) + { + sb.Append(joy.Button[i]); + sb.Append(" "); + } + DrawString(gfx, sb.ToString(), line++); + } } return line; @@ -183,11 +204,6 @@ namespace Examples.Tests protected override void OnUpdateFrame(FrameEventArgs e) { - InputDriver.Poll(); - - mouse = OpenTK.Input.Mouse.GetState(); - keyboard = OpenTK.Input.Keyboard.GetState(); - using (Graphics gfx = Graphics.FromImage(TextBitmap)) { int line = 0; @@ -195,41 +211,34 @@ namespace Examples.Tests gfx.Clear(Color.Black); gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; - DrawString(gfx, GL.GetString(StringName.Vendor), line++); - DrawString(gfx, GL.GetString(StringName.Version), line++); DrawString(gfx, GL.GetString(StringName.Renderer), line++); + DrawString(gfx, GL.GetString(StringName.Version), line++); DrawString(gfx, Context.GraphicsMode.ToString(), line++); + line++; + DrawString(gfx, "GameWindow:", line++); DrawString(gfx, String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), line++); DrawString(gfx, String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++); - DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++); - DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++); - DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++); - DrawString(gfx, String.Format("Mouse position (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++); - DrawString(gfx, String.Format("Mouse position (relative): {0}", new Vector3(mouse.X, mouse.Y, mouse.WheelPrecise)), line++); - DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++); - DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++); - DrawString(gfx, String.Format("Window: {{X={0},Y={1},Width={2},Height={3}}}", X, Y, Width, Height), line++); - DrawString(gfx, String.Format("Window.ClientRectangle: {0}", ClientRectangle), line++); + DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.", + mouse_in_window ? "inside" : "outside", + CursorVisible ? "visible" : "hidden", + Focused ? "Focused" : "Not focused"), line++); + DrawString(gfx, String.Format("Mouse (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++); + DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++); + DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++); DrawString(gfx, TypedText.ToString(), line++); - DrawKeyboard(gfx, keyboard, line++); - DrawMouse(gfx, mouse, line++); - line = DrawJoysticks(gfx, Joysticks, line++); - line = DrawGamePads(gfx, line++); - } - System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits( - new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height), - System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra, - PixelType.UnsignedByte, data.Scan0); - TextBitmap.UnlockBits(data); + line = DrawKeyboards(gfx, line); + line = DrawMice(gfx, line); + line = DrawJoysticks(gfx, line); + line = DrawLegacyJoysticks(gfx, Joysticks, line); + } } - int DrawGamePads(Graphics gfx, int line) + int DrawJoysticks(Graphics gfx, int line) { line++; - DrawString(gfx, "GamePads:", line++); + DrawString(gfx, "GamePad:", line++); for (int i = 0; i < 4; i++) { GamePadCapabilities caps = GamePad.GetCapabilities(i); @@ -240,8 +249,9 @@ namespace Examples.Tests DrawString(gfx, state.ToString(), line++); } } + line++; - DrawString(gfx, "Joysticks:", line++); + DrawString(gfx, "Joystick:", line++); for (int i = 0; i < 4; i++) { JoystickCapabilities caps = Joystick.GetCapabilities(i); @@ -283,13 +293,20 @@ namespace Examples.Tests protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); - + + System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits( + new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height), + System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra, + PixelType.UnsignedByte, data.Scan0); + TextBitmap.UnlockBits(data); + if (viewport_changed) { viewport_changed = false; GL.Viewport(0, 0, Width, Height); - + Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref ortho_projection); @@ -297,7 +314,7 @@ namespace Examples.Tests GL.Clear(ClearBufferMask.ColorBufferBit); - GL.Begin(BeginMode.Quads); + GL.Begin(PrimitiveType.Quads); GL.TexCoord2(0, 0); GL.Vertex2(0, 0); GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0); From c5dcc8a93b64419208a0a824b06f151266e3124f Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 01:01:00 +0100 Subject: [PATCH 03/10] [Examples] Calculate timing information in GameWindowStates --- .../Examples/OpenTK/Test/GameWindowStates.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index 4fdbee4c..d2207219 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -25,6 +25,8 @@ namespace Examples.Tests int texture; bool mouse_in_window = false; bool viewport_changed = true; + Stopwatch watch = new Stopwatch(); + double update_time, render_time; public GameWindowStates() : base(800, 600, GraphicsMode.Default) @@ -204,6 +206,9 @@ namespace Examples.Tests protected override void OnUpdateFrame(FrameEventArgs e) { + double clock_time = watch.Elapsed.TotalSeconds; + update_time += e.Time; + using (Graphics gfx = Graphics.FromImage(TextBitmap)) { int line = 0; @@ -226,7 +231,14 @@ namespace Examples.Tests DrawString(gfx, String.Format("Mouse (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++); DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++); DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++); - DrawString(gfx, TypedText.ToString(), line++); + DrawString(gfx, String.Format("Vsync: {0}", VSync), line++); + DrawString(gfx, String.Format("Frequency: Update ({0:f1}/{1:f1}); Render ({2:f1}/{3:f1})", + UpdateFrequency, TargetUpdateFrequency, RenderFrequency, TargetRenderFrequency), line++); + DrawString(gfx, String.Format("Period: Update ({0:f1}/{1:f1}); Render ({2:f1}/{3:f1})", + UpdatePeriod, TargetUpdatePeriod, RenderPeriod, TargetRenderPeriod), line++); + DrawString(gfx, String.Format("Time drift: Clock {0:f4}; Update {1:f4}; Render {2:f4}", + clock_time, clock_time - update_time, clock_time - render_time), line++); + DrawString(gfx, String.Format("Text: {0}", TypedText.ToString()), line++); line = DrawKeyboards(gfx, line); line = DrawMice(gfx, line); @@ -268,8 +280,8 @@ namespace Examples.Tests protected override void OnLoad(EventArgs e) { - base.OnLoad(e); - + watch.Start(); + GL.ClearColor(Color.MidnightBlue); GL.Enable(EnableCap.Texture2D); @@ -292,7 +304,7 @@ namespace Examples.Tests protected override void OnRenderFrame(FrameEventArgs e) { - base.OnRenderFrame(e); + render_time += e.Time; System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits( new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height), From 3856fcd48e679e6122d675b1cf1f9f19def63d31 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 08:52:02 +0100 Subject: [PATCH 04/10] [OpenTK] More robust timing for UpdateFrame and RenderFrame FrameEventArgs.Time should no longer drift from clock time measured outside GameWindow. --- Source/OpenTK/GameWindow.cs | 145 +++++++++++++++--------------------- 1 file changed, 62 insertions(+), 83 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 0f41a3f8..048d5f64 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -1,4 +1,4 @@ -#region License +#region License // // The Open Toolkit Library License // @@ -75,7 +75,7 @@ namespace OpenTK { #region --- Fields --- - object exit_lock = new object(); + readonly Stopwatch watch = new Stopwatch(); IGraphicsContext glContext; @@ -83,6 +83,10 @@ namespace OpenTK double update_period, render_period; double target_update_period, target_render_period; + + double update_timestamp; // timestamp of last UpdateFrame event + double render_timestamp; // timestamp of last RenderFrame event + // TODO: Implement these: double update_time, render_time; VSyncMode vsync; @@ -402,10 +406,6 @@ namespace OpenTK //Move += DispatchUpdateAndRenderFrame; //Resize += DispatchUpdateAndRenderFrame; - Debug.Print("Calibrating Stopwatch to account for drift"); - CalibrateStopwatch(); - Debug.Print("Stopwatch overhead: {0}", stopwatch_overhead); - Debug.Print("Entering main loop."); watch.Start(); while (true) @@ -431,97 +431,76 @@ namespace OpenTK } } - double stopwatch_overhead = 0; - void CalibrateStopwatch() - { - // Make sure everything is JITted - watch.Start(); - stopwatch_overhead = watch.Elapsed.TotalSeconds; - watch.Stop(); - watch.Reset(); - // Measure stopwatch overhead - const int count = 10; - for (int i = 0; i < count; i++) - { - watch.Start(); - double sample = watch.Elapsed.TotalSeconds; - if (sample < 0 || sample > 0.1) - { - // calculation failed, repeat - i--; - continue; - } - stopwatch_overhead += sample; - watch.Stop(); - watch.Reset(); - } - stopwatch_overhead /= 10; - } - - Stopwatch watch = new Stopwatch(); - double update_timestamp = 0; - double render_timestamp = 0; - double update_elapsed = 0; - double render_elapsed = 0; void DispatchUpdateAndRenderFrame(object sender, EventArgs e) { const int max_frameskip = 10; int frameskip = 0; - double timestamp = 0; + double timestamp = watch.Elapsed.TotalSeconds; do { // Raise UpdateFrame events until we catch up with our target update rate. - timestamp = watch.Elapsed.TotalSeconds; - update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0); - update_timestamp = timestamp; - RaiseUpdateFrame(update_elapsed, ref next_update); - } while (next_update > 0 && ++frameskip < max_frameskip); - // Calculate statistics - //update_period = total_update_time / (double)num_updates; - - timestamp = watch.Elapsed.TotalSeconds; - render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); - render_timestamp = timestamp; - RaiseRenderFrame(render_elapsed, ref next_render); - } - - void RaiseUpdateFrame(double time, ref double next_update) - { - double time_left = next_render - time; - if (time_left <= 0.0 && time > 0) - { - update_args.Time = time; - OnUpdateFrameInternal(update_args); - - // Don't schedule a new update more than 1 second in the future. - // Sometimes the hardware cannot keep up with updates - // (e.g. when the update rate is too high, or the UpdateFrame processing - // is too costly). This cap ensures we can catch up in a reasonable time - // once the load becomes lighter. - next_update = time_left + TargetUpdatePeriod; - next_update = Math.Max(next_update, -1.0); - } - } - - void RaiseRenderFrame(double time, ref double next_render) - { - double time_left = next_render - time; - if (time_left <= 0.0 && time > 0) - { - if (time > 0) + double update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0); + if (RaiseUpdateFrame(update_elapsed, ref next_update)) { - render_period = render_args.Time = time; - OnRenderFrameInternal(render_args); + update_timestamp = timestamp; } + timestamp = watch.Elapsed.TotalSeconds; + } while (next_update <= 0 && ++frameskip < max_frameskip); - // Schedule next render event. The 1 second cap ensures - // the process does not appear to hang. - next_render = time_left + TargetRenderPeriod; - next_update = Math.Max(next_update, -1.0); + double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); + if (RaiseRenderFrame(render_elapsed, ref next_render)) + { + render_timestamp = timestamp; } } + bool RaiseUpdateFrame(double time, ref double next_update) + { + if (time > 0) + { + next_update -= time; + if (next_update <= 0.0) + { + update_args.Time = time; + OnUpdateFrameInternal(update_args); + + // Don't schedule a new update more than 1 second in the future. + // Sometimes the hardware cannot keep up with updates + // (e.g. when the update rate is too high, or the UpdateFrame processing + // is too costly). This cap ensures we can catch up in a reasonable time + // once the load becomes lighter. + next_update += TargetUpdatePeriod; + next_update = Math.Max(next_update, -1.0); + update_period = Math.Max(next_update, 0.0); + return true; + } + } + return false; + } + + + bool RaiseRenderFrame(double time, ref double next_render) + { + if (time > 0) + { + next_render -= time; + if (next_render <= 0.0) + { + render_args.Time = time; + OnRenderFrameInternal(render_args); + + // Schedule next render event. The 1 second cap ensures + // the process does not appear to hang. + next_render += TargetRenderPeriod; + next_render = Math.Max(next_render, -1.0); + render_period = Math.Max(next_render, 0.0); + return true; + } + } + return false; + } + #endregion #region SwapBuffers From 56a3dd91e5243a9d16ece6070cb94b5fe1564320 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 09:09:43 +0100 Subject: [PATCH 05/10] [OpenTK] Implemented GameWindow.UpdateTime and RenderTime properties --- Source/OpenTK/GameWindow.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 048d5f64..3669970f 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -446,6 +446,7 @@ namespace OpenTK update_timestamp = timestamp; } timestamp = watch.Elapsed.TotalSeconds; + update_time = timestamp - update_timestamp; } while (next_update <= 0 && ++frameskip < max_frameskip); double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); @@ -453,6 +454,8 @@ namespace OpenTK { render_timestamp = timestamp; } + timestamp = watch.Elapsed.TotalSeconds; + render_time = timestamp - render_timestamp; } bool RaiseUpdateFrame(double time, ref double next_update) From 99df27b6356a0d9b6f20ce48f90ba1258c0e3f8a Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 09:11:55 +0100 Subject: [PATCH 06/10] [OpenTK] Corrected GameWindow.Update/RenderFrequency information --- Source/OpenTK/GameWindow.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 3669970f..c3063ff5 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -443,6 +443,7 @@ namespace OpenTK double update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0); if (RaiseUpdateFrame(update_elapsed, ref next_update)) { + update_period = update_elapsed; update_timestamp = timestamp; } timestamp = watch.Elapsed.TotalSeconds; @@ -452,6 +453,7 @@ namespace OpenTK double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); if (RaiseRenderFrame(render_elapsed, ref next_render)) { + render_period = render_elapsed; render_timestamp = timestamp; } timestamp = watch.Elapsed.TotalSeconds; @@ -475,7 +477,6 @@ namespace OpenTK // once the load becomes lighter. next_update += TargetUpdatePeriod; next_update = Math.Max(next_update, -1.0); - update_period = Math.Max(next_update, 0.0); return true; } } @@ -497,7 +498,6 @@ namespace OpenTK // the process does not appear to hang. next_render += TargetRenderPeriod; next_render = Math.Max(next_render, -1.0); - render_period = Math.Max(next_render, 0.0); return true; } } From 251f5717ae7a218a70091d5f8938e04419d045ec Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 7 Jan 2014 09:12:35 +0100 Subject: [PATCH 07/10] [Examples] Improve timing information; add vsync toggle --- .../Examples/OpenTK/Test/GameWindowStates.cs | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index d2207219..05289eaa 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -82,6 +82,10 @@ namespace Examples.Tests case Key.KeypadMinus: case Key.Minus: Size -= new Size(16, 16); break; + + case Key.V: + VSync = VSync == VSyncMode.On ? VSyncMode.Off : VSyncMode.On; + break; } } @@ -216,30 +220,39 @@ namespace Examples.Tests gfx.Clear(Color.Black); gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; + // OpenGL information DrawString(gfx, GL.GetString(StringName.Renderer), line++); DrawString(gfx, GL.GetString(StringName.Version), line++); DrawString(gfx, Context.GraphicsMode.ToString(), line++); - line++; + // GameWindow information + line++; DrawString(gfx, "GameWindow:", line++); - DrawString(gfx, String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), line++); - DrawString(gfx, String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++); + DrawString(gfx, String.Format("[1 - 4]:[5 - 7]: WindowState.{0}:WindowBorder.{1}", + this.WindowState, this.WindowBorder), line++); + DrawString(gfx, String.Format("[V]: VSync.{0}.", VSync), line++); + DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++); + DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++); DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.", mouse_in_window ? "inside" : "outside", CursorVisible ? "visible" : "hidden", Focused ? "Focused" : "Not focused"), line++); - DrawString(gfx, String.Format("Mouse (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++); - DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++); - DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++); - DrawString(gfx, String.Format("Vsync: {0}", VSync), line++); - DrawString(gfx, String.Format("Frequency: Update ({0:f1}/{1:f1}); Render ({2:f1}/{3:f1})", + DrawString(gfx, String.Format("Mouse coordinates: {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++); + + // Timing information + line++; + DrawString(gfx, "Timing:", line++); + DrawString(gfx, String.Format("Frequency: update ({0:f2}/{1:f2}); render ({2:f2}/{3:f2})", UpdateFrequency, TargetUpdateFrequency, RenderFrequency, TargetRenderFrequency), line++); - DrawString(gfx, String.Format("Period: Update ({0:f1}/{1:f1}); Render ({2:f1}/{3:f1})", + DrawString(gfx, String.Format("Period: update ({0:f4}/{1:f4}); render ({2:f4}/{3:f4})", UpdatePeriod, TargetUpdatePeriod, RenderPeriod, TargetRenderPeriod), line++); - DrawString(gfx, String.Format("Time drift: Clock {0:f4}; Update {1:f4}; Render {2:f4}", + DrawString(gfx, String.Format("Time: update {0:f4}; render {1:f4}", + UpdateTime, RenderTime), line++); + DrawString(gfx, String.Format("Drift: clock {0:f4}; update {1:f4}; render {2:f4}", clock_time, clock_time - update_time, clock_time - render_time), line++); DrawString(gfx, String.Format("Text: {0}", TypedText.ToString()), line++); + // Input information line = DrawKeyboards(gfx, line); line = DrawMice(gfx, line); line = DrawJoysticks(gfx, line); @@ -343,7 +356,7 @@ namespace Examples.Tests using (GameWindowStates ex = new GameWindowStates()) { Utilities.SetWindowTitle(ex); - ex.Run(30.0); + ex.Run(30.0); } } } From 6e03d501aea7c4a614034b83c8e7d2deb87aa5c0 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 7 Jan 2014 15:55:11 +0100 Subject: [PATCH 08/10] [OpenTK] Fixed Update/RenderTime calculation These values should only be re-calculated when an Update/RenderFrame event is raised. Otherwise, they should retain their previous values. --- Source/OpenTK/GameWindow.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index c3063ff5..925733e9 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -445,9 +445,13 @@ namespace OpenTK { update_period = update_elapsed; update_timestamp = timestamp; + timestamp = watch.Elapsed.TotalSeconds; + update_time = timestamp - update_timestamp; + } + else + { + timestamp = watch.Elapsed.TotalSeconds; } - timestamp = watch.Elapsed.TotalSeconds; - update_time = timestamp - update_timestamp; } while (next_update <= 0 && ++frameskip < max_frameskip); double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); @@ -455,9 +459,9 @@ namespace OpenTK { render_period = render_elapsed; render_timestamp = timestamp; + timestamp = watch.Elapsed.TotalSeconds; + render_time = timestamp - render_timestamp; } - timestamp = watch.Elapsed.TotalSeconds; - render_time = timestamp - render_timestamp; } bool RaiseUpdateFrame(double time, ref double next_update) From b6a806a5687dc5d9436a4254bafe69cf29fd8aad Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 7 Jan 2014 22:09:02 +0100 Subject: [PATCH 09/10] [OpenTK] Improved timing stability OpenTK now directly calculates the elapsed time between UpdateFrame (RenderFrame) events and compares that directly to TargetUpdatePeriod (TargetRenderPeriod). This significantly simplifies the implementation and improves timing stability. --- Source/OpenTK/GameWindow.cs | 60 +++++++++++++------------------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 925733e9..e3429237 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -83,15 +83,15 @@ namespace OpenTK double update_period, render_period; double target_update_period, target_render_period; + + double update_time; // length of last UpdateFrame event + double render_time; // length of last RenderFrame event double update_timestamp; // timestamp of last UpdateFrame event double render_timestamp; // timestamp of last RenderFrame event - // TODO: Implement these: - double update_time, render_time; VSyncMode vsync; - double next_render = 0.0, next_update = 0.0; FrameEventArgs update_args = new FrameEventArgs(); FrameEventArgs render_args = new FrameEventArgs(); @@ -441,7 +441,7 @@ namespace OpenTK { // Raise UpdateFrame events until we catch up with our target update rate. double update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0); - if (RaiseUpdateFrame(update_elapsed, ref next_update)) + if (RaiseUpdateFrame(update_elapsed)) { update_period = update_elapsed; update_timestamp = timestamp; @@ -450,12 +450,15 @@ namespace OpenTK } else { - timestamp = watch.Elapsed.TotalSeconds; + // We have executed enough UpdateFrame events to catch up. + // Break and issue a RenderFrame event. + break; } - } while (next_update <= 0 && ++frameskip < max_frameskip); + } while (++frameskip < max_frameskip); + timestamp = watch.Elapsed.TotalSeconds; double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0); - if (RaiseRenderFrame(render_elapsed, ref next_render)) + if (RaiseRenderFrame(render_elapsed)) { render_period = render_elapsed; render_timestamp = timestamp; @@ -464,46 +467,25 @@ namespace OpenTK } } - bool RaiseUpdateFrame(double time, ref double next_update) + bool RaiseUpdateFrame(double time) { - if (time > 0) + if (time >= TargetUpdatePeriod) { - next_update -= time; - if (next_update <= 0.0) - { - update_args.Time = time; - OnUpdateFrameInternal(update_args); - - // Don't schedule a new update more than 1 second in the future. - // Sometimes the hardware cannot keep up with updates - // (e.g. when the update rate is too high, or the UpdateFrame processing - // is too costly). This cap ensures we can catch up in a reasonable time - // once the load becomes lighter. - next_update += TargetUpdatePeriod; - next_update = Math.Max(next_update, -1.0); - return true; - } + update_args.Time = time; + OnUpdateFrameInternal(update_args); + return true; } return false; } - bool RaiseRenderFrame(double time, ref double next_render) + bool RaiseRenderFrame(double time) { - if (time > 0) + if (time >= TargetRenderPeriod) { - next_render -= time; - if (next_render <= 0.0) - { - render_args.Time = time; - OnRenderFrameInternal(render_args); - - // Schedule next render event. The 1 second cap ensures - // the process does not appear to hang. - next_render += TargetRenderPeriod; - next_render = Math.Max(next_render, -1.0); - return true; - } + render_args.Time = time; + OnRenderFrameInternal(render_args); + return true; } return false; } @@ -1083,4 +1065,4 @@ namespace OpenTK } #endregion -} \ No newline at end of file +} From a961fb3db36f4001c1aa8f2b4deba9f180d5f4a9 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 7 Jan 2014 22:09:52 +0100 Subject: [PATCH 10/10] [Examples] Added keys to modify timing Use [ and ] to decrease and increase the UpdateFrame frequency. Use < and > to decrease and increase the RenderFrame frequency. --- Source/Examples/OpenTK/Test/GameWindowStates.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index 05289eaa..10ae3141 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -86,6 +86,11 @@ namespace Examples.Tests case Key.V: VSync = VSync == VSyncMode.On ? VSyncMode.Off : VSyncMode.On; break; + + case Key.BracketLeft: TargetUpdateFrequency--; break; + case Key.BracketRight: TargetUpdateFrequency++; break; + case Key.Comma: TargetRenderFrequency--; break; + case Key.Period: TargetRenderFrequency++; break; } } @@ -356,7 +361,7 @@ namespace Examples.Tests using (GameWindowStates ex = new GameWindowStates()) { Utilities.SetWindowTitle(ex); - ex.Run(30.0); + ex.Run(30.0); } } }