Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented IGraphicsContext.SwapInterval property that supersedes IG…

…raphicsContext.VSync. Fixes issue [#2671]: "Expose SwapInterval to GameWindow".

git-svn-id: https://opentk.svn.sourceforge.net/svnroot/opentk/trunk@3081 ebc5dd9b-fb1d-0410-b6f8-d24c324e9604
  • Loading branch information...
commit a63c05615642d99824fcb3ab03044985363c6474 1 parent fb4f71f
the_fiddler authored
View
4 Source/GLControl/GLControl.cs
@@ -164,7 +164,7 @@ protected override void OnHandleCreated(EventArgs e)
// Deferred setting of vsync mode. See VSync property for more information.
if (initial_vsync_value.HasValue)
{
- Context.VSync = initial_vsync_value.Value;
+ Context.SwapInterval = initial_vsync_value.Value ? 1 : 0;
initial_vsync_value = null;
}
@@ -331,7 +331,7 @@ public float AspectRatio
/// <summary>
/// Gets or sets a value indicating whether vsync is active for this GLControl.
/// </summary>
- [Description("Indicates whether GLControl updates are synced to the monitor's refresh.")]
+ [Description("Indicates whether GLControl updates are synced to the monitor's refresh rate.")]
public bool VSync
{
get
View
4 Source/OpenTK/GameWindow.cs
@@ -518,9 +518,9 @@ void RaiseRenderFrame(Stopwatch render_watch, ref double next_render, FrameEvent
{
// Check if we have enough time for a vsync
if (RenderTime > 2.0 * TargetRenderPeriod)
- Context.VSync = false;
+ Context.SwapInterval = 0;
else
- Context.VSync = true;
+ Context.SwapInterval = 1;
}
render_period = render_args.Time = time;
View
24 Source/OpenTK/Graphics/GraphicsContext.cs
@@ -461,8 +461,16 @@ public bool IsDisposed
}
/// <summary>
- /// Gets or sets a value indicating whether VSync is enabled.
+ /// [obsolete] Use SwapInterval property instead.
+ /// Gets or sets a value indicating whether VSync is enabled. When VSync is
+ /// enabled, <see cref="SwapBuffers()"/> calls will be synced to the refresh
+ /// rate of the <see cref="DisplayDevice"/> that contains improving visual
+ /// quality and reducing CPU usage. However, systems that cannot maintain
+ /// the requested rendering rate will suffer from large jumps in performance.
+ /// This can be counteracted by increasing the <see cref="SwapInterval"/>
+ /// value.
/// </summary>
+ [Obsolete("Use SwapInterval property instead.")]
public bool VSync
{
get { return implementation.VSync; }
@@ -470,6 +478,20 @@ public bool VSync
}
/// <summary>
+ /// Gets or sets a positive integer in the range [1, n), indicating the number of
+ /// <see cref="DisplayDevice"/> refreshes between consecutive
+ /// <see cref="SwapBuffers()"/> calls. The maximum value for n is
+ /// implementation-dependent. The default value is 1.
+ /// This value will only affect instances where <see cref="VSync"/> is enabled.
+ /// Invalid values will be clamped to the valid range.
+ /// </summary>
+ public int SwapInterval
+ {
+ get { return implementation.SwapInterval; }
+ set { implementation.SwapInterval = value; }
+ }
+
+ /// <summary>
/// Updates the graphics context. This must be called when the render target
/// is resized for proper behavior on Mac OS X.
/// </summary>
View
16 Source/OpenTK/Graphics/GraphicsContextBase.cs
@@ -57,8 +57,20 @@ public bool IsDisposed
get { return disposed; }
protected set { disposed = value; }
}
-
- public abstract bool VSync { get; set; }
+
+ public bool VSync
+ {
+ get { return SwapInterval > 0; }
+ set
+ {
+ if (value && SwapInterval <= 0)
+ SwapInterval = 1;
+ else if (!value && SwapInterval > 0)
+ SwapInterval = 0;
+ }
+ }
+
+ public abstract int SwapInterval { get; set; }
public virtual void Update(IWindowInfo window) { }
View
19 Source/OpenTK/Graphics/IGraphicsContext.cs
@@ -42,11 +42,28 @@ public interface IGraphicsContext : IDisposable
bool IsDisposed { get; }
/// <summary>
- /// Gets or sets a value indicating whether VSyncing is enabled.
+ /// Gets or sets a value indicating whether VSync is enabled. When VSync is
+ /// enabled, <see cref="SwapBuffers()"/> calls will be synced to the refresh
+ /// rate of the <see cref="DisplayDevice"/> that contains improving visual
+ /// quality and reducing CPU usage. However, systems that cannot maintain
+ /// the requested rendering rate will suffer from large jumps in performance.
+ /// This can be counteracted by increasing the <see cref="SwapInterval"/>
+ /// value.
/// </summary>
+ [Obsolete("Use SwapInterval property instead.")]
bool VSync { get; set; }
/// <summary>
+ /// Gets or sets a positive integer in the range [1, n), indicating the number of
+ /// <see cref="DisplayDevice"/> refreshes between consecutive
+ /// <see cref="SwapBuffers()"/> calls. The maximum value for n is
+ /// implementation-dependent. The default value is 1.
+ /// This value will only affect instances where <see cref="VSync"/> is enabled.
+ /// Invalid values will be clamped to the valid range.
+ /// </summary>
+ int SwapInterval { get; set; }
+
+ /// <summary>
/// Updates the graphics context. This must be called when the region the graphics context
/// is drawn to is resized.
/// </summary>
View
13 Source/OpenTK/Platform/Dummy/DummyGLContext.cs
@@ -24,6 +24,7 @@ internal sealed class DummyGLContext : DesktopGraphicsContext
{
// This mode is not real. To receive a real mode we'd have to create a temporary context, which is not desirable!
bool vsync;
+ int swap_interval;
static int handle_count;
Thread current_thread;
@@ -82,7 +83,17 @@ public override bool IsCurrent
public override IntPtr GetAddress(string function) { return IntPtr.Zero; }
- public override bool VSync { get { return vsync; } set { vsync = value; } }
+ public override int SwapInterval
+ {
+ get
+ {
+ return swap_interval;
+ }
+ set
+ {
+ swap_interval = value;
+ }
+ }
public override void Update(IWindowInfo window)
{ }
View
10 Source/OpenTK/Platform/Egl/EglContext.cs
@@ -39,7 +39,7 @@ class EglContext : EmbeddedGraphicsContext
EglWindowInfo WindowInfo;
IntPtr HandleAsEGLContext { get { return Handle.Handle; } set { Handle = new ContextHandle(value); } }
- bool vsync = true; // Default vsync value is defined as 1 (true) in EGL.
+ int swap_interval = 1; // Default interval is defined as 1 in EGL.
#endregion
@@ -117,18 +117,18 @@ public override bool IsCurrent
get { return Egl.GetCurrentContext() == HandleAsEGLContext; }
}
- public override bool VSync
+ public override int SwapInterval
{
get
{
// Egl.GetSwapInterval does not exist, so store and return the current interval.
// The default interval is defined as 1 (true).
- return vsync;
+ return swap_interval;
}
set
{
- if (Egl.SwapInterval(WindowInfo.Display, value ? 1 : 0))
- vsync = value;
+ if (Egl.SwapInterval(WindowInfo.Display, value))
+ swap_interval = value;
else
Debug.Print("[Warning] Egl.SwapInterval({0}, {1}) failed.", WindowInfo.Display, value);
}
View
23 Source/OpenTK/Platform/MacOS/AglContext.cs
@@ -44,7 +44,6 @@ namespace OpenTK.Platform.MacOS
class AglContext : DesktopGraphicsContext
{
- bool mVSync = false;
// Todo: keep track of which display adapter was specified when the context was created.
// IntPtr displayID;
@@ -427,16 +426,24 @@ public override bool IsCurrent
get { return (Handle.Handle == Agl.aglGetCurrentContext()); }
}
- public override bool VSync
+ public override int SwapInterval
{
- get { return mVSync; }
+ get {
+ int swap_interval = 0;
+ if (Agl.aglGetInteger(Handle.Handle, Agl.ParameterNames.AGL_SWAP_INTERVAL, out swap_interval))
+ {
+ return swap_interval;
+ }
+ else
+ {
+ MyAGLReportError("aglGetInteger");
+ return 0;
+ }
+ }
set
{
- int intVal = value ? 1 : 0;
-
- Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_SWAP_INTERVAL, ref intVal);
-
- mVSync = value;
+ if (!Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_SWAP_INTERVAL, ref value))
+ MyAGLReportError("aglSetInteger");
}
}
View
17 Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs
@@ -425,15 +425,20 @@ internal static void aglSetFullScreen(AGLContext ctx, int width, int height, int
/*
** Per context options
*/
- [DllImport(agl)] internal static extern byte aglEnable(AGLContext ctx, ParameterNames pname);
- [DllImport(agl)] internal static extern byte aglDisable(AGLContext ctx, ParameterNames pname);
- [DllImport(agl)] static extern byte aglIsEnabled(AGLContext ctx, GLenum pname);
[DllImport(agl)]
- internal static extern byte aglSetInteger(AGLContext ctx, ParameterNames pname, ref int @params);
+ internal static extern bool aglEnable(AGLContext ctx, ParameterNames pname);
[DllImport(agl)]
- internal static extern byte aglSetInteger(AGLContext ctx, ParameterNames pname, int []@params);
+ internal static extern bool aglDisable(AGLContext ctx, ParameterNames pname);
[DllImport(agl)]
- static extern byte aglGetInteger(AGLContext ctx, GLenum pname, int* @params);
+ static extern bool aglIsEnabled(AGLContext ctx, GLenum pname);
+ [DllImport(agl)]
+ internal static extern bool aglSetInteger(AGLContext ctx, ParameterNames pname, ref int param);
+ [DllImport(agl)]
+ internal static extern bool aglSetInteger(AGLContext ctx, ParameterNames pname, int[] @params);
+ //[DllImport(agl)]
+ //static extern bool aglGetInteger(AGLContext ctx, ParameterNames pname, int* @params);
+ [DllImport(agl)]
+ internal static extern bool aglGetInteger(AGLContext ctx, ParameterNames pname, out int param);
/*
** Font function
View
14 Source/OpenTK/Platform/Windows/WinGLContext.cs
@@ -203,18 +203,18 @@ public override bool IsCurrent
#endregion
- #region public bool VSync
+ #region SwapInterval
- /// <summary>
- /// Gets or sets a System.Boolean indicating whether SwapBuffer calls are synced to the screen refresh rate.
- /// </summary>
- public override bool VSync
+ public override int SwapInterval
{
get
{
lock (LoadLock)
{
- return vsync_supported && Wgl.Ext.GetSwapInterval() != 0;
+ if (vsync_supported)
+ return Wgl.Ext.GetSwapInterval();
+ else
+ return 0;
}
}
set
@@ -222,7 +222,7 @@ public override bool VSync
lock (LoadLock)
{
if (vsync_supported)
- Wgl.Ext.SwapInterval(value ? 1 : 0);
+ Wgl.Ext.SwapInterval(value);
}
}
}
View
21 Source/OpenTK/Platform/X11/X11GLContext.cs
@@ -30,7 +30,7 @@ internal sealed class X11GLContext : DesktopGraphicsContext
IntPtr display;
X11WindowInfo currentWindow;
bool vsync_supported;
- int vsync_interval;
+ int swap_interval = 1; // As defined in GLX_SGI_swap_control
bool glx_loaded;
#endregion
@@ -348,13 +348,16 @@ public override bool IsCurrent
#endregion
- #region VSync
+ #region SwapInterval
- public override bool VSync
+ public override int SwapInterval
{
get
{
- return vsync_supported && vsync_interval != 0;
+ if (vsync_supported)
+ return swap_interval;
+ else
+ return 0;
}
set
{
@@ -362,12 +365,12 @@ public override bool VSync
{
ErrorCode error_code = 0;
using (new XLock(Display))
- {
- error_code = Glx.Sgi.SwapInterval(value ? 1 : 0);
- }
- if (error_code != X11.ErrorCode.NO_ERROR)
+ error_code = Glx.Sgi.SwapInterval(value);
+
+ if (error_code == X11.ErrorCode.NO_ERROR)
+ swap_interval = value;
+ else
Debug.Print("VSync = {0} failed, error code: {1}.", value, error_code);
- vsync_interval = value ? 1 : 0;
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.