2007-09-09 16:07:39 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Platform;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents and provides methods to manipulate an OpenGL render context.
|
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
public sealed class GLContext : IGLContext, IGLContextInternal, IGLContextCreationHack
|
2007-09-09 16:07:39 +00:00
|
|
|
|
{
|
2007-12-09 18:15:51 +00:00
|
|
|
|
IGLContext implementation; // The actual render context implementation for the underlying platform.
|
|
|
|
|
List<IDisposable> dispose_queue = new List<IDisposable>();
|
2008-01-11 20:31:59 +00:00
|
|
|
|
bool disposed;
|
2008-01-22 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
static bool share_contexts = true;
|
|
|
|
|
static object context_lock = new object();
|
2008-01-11 20:31:59 +00:00
|
|
|
|
static Dictionary<ContextHandle, WeakReference> available_contexts =
|
|
|
|
|
new Dictionary<ContextHandle, WeakReference>(); // Contains all available OpenGL contexts.
|
2007-12-09 18:15:51 +00:00
|
|
|
|
|
|
|
|
|
#region public GLContext(DisplayMode mode, IWindowInfo window)
|
2007-09-09 16:07:39 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new GLContext with the specified DisplayMode, and bound to the specified IWindowInfo.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mode"></param>
|
|
|
|
|
/// <param name="window"></param>
|
|
|
|
|
public GLContext(DisplayMode mode, IWindowInfo window)
|
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
//if (available_contexts.Count == 0)
|
|
|
|
|
// available_contexts.Add(IntPtr.Zero, new WeakReference(null));
|
2007-12-09 18:15:51 +00:00
|
|
|
|
|
2007-09-09 16:07:39 +00:00
|
|
|
|
switch (Environment.OSVersion.Platform)
|
|
|
|
|
{
|
|
|
|
|
case PlatformID.Unix:
|
|
|
|
|
case (PlatformID)128:
|
2008-01-11 20:31:59 +00:00
|
|
|
|
implementation = new OpenTK.Platform.X11.X11GLContext();
|
2007-09-09 16:07:39 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
|
case PlatformID.Win32Windows:
|
|
|
|
|
case PlatformID.WinCE:
|
2008-01-11 20:31:59 +00:00
|
|
|
|
implementation = new OpenTK.Platform.Windows.WinGLContext();
|
2007-09-09 16:07:39 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2008-01-11 20:31:59 +00:00
|
|
|
|
throw new PlatformNotSupportedException("Your platform is not supported currently. Please, refer to http://www.opentk.com for more information.");
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
2008-01-14 22:38:09 +00:00
|
|
|
|
|
|
|
|
|
(this as IGLContextCreationHack).SetWindowHandle(window.Handle);
|
|
|
|
|
(this as IGLContextCreationHack).SelectDisplayMode(mode, window);
|
2008-01-22 14:53:44 +00:00
|
|
|
|
if (GLContext.ShareContexts)
|
|
|
|
|
{
|
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
// A small hack to create a shared context with the first available context.
|
|
|
|
|
foreach (WeakReference r in GLContext.available_contexts.Values)
|
|
|
|
|
{
|
|
|
|
|
this.CreateContext(true, (GLContext)r.Target);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Only reached if a shared context was not created above, or if this is the first
|
|
|
|
|
// context ever constructed.
|
2008-01-15 12:25:22 +00:00
|
|
|
|
this.CreateContext(true, null);
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#region void ContextDestroyed(IGLContext context, EventArgs e)
|
2007-12-09 18:15:51 +00:00
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the Destroy event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">The OpenTK.Platform.IGLContext that was destroyed.</param>
|
|
|
|
|
/// <param name="e">Not used.</param>
|
|
|
|
|
void ContextDestroyed(IGLContext context, EventArgs e)
|
2007-12-09 18:15:51 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
this.Destroy -= ContextDestroyed;
|
|
|
|
|
available_contexts.Remove(((IGLContextInternal)this).Context);
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#region --- Public Members ---
|
2007-09-09 16:07:39 +00:00
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#region public static IGLContext CurrentContext
|
2007-09-09 16:07:39 +00:00
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
internal delegate ContextHandle GetCurrentContextDelegate();
|
|
|
|
|
internal static GetCurrentContextDelegate GetCurrentContext;
|
2007-09-09 16:07:39 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2008-01-31 13:15:17 +00:00
|
|
|
|
/// Gets or sets the current GraphicsContext in the calling thread.
|
2007-09-09 16:07:39 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
public static GLContext CurrentContext
|
2007-09-09 16:07:39 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (available_contexts.Count > 0)
|
|
|
|
|
return (GLContext)available_contexts[GetCurrentContext()].Target;
|
|
|
|
|
//return (GLContext)available_contexts[((IGLContextInternal)available_contexts[IntPtr.Zero].Target).GetCurrentContext()].Target;
|
|
|
|
|
return null;
|
|
|
|
|
//return (GLContext)available_contexts[StaticGetCurrentContext().ToInt64()].Target;
|
|
|
|
|
}
|
2008-01-31 13:15:17 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
value.MakeCurrent();
|
|
|
|
|
else if (CurrentContext != null)
|
|
|
|
|
CurrentContext.IsCurrent = false;
|
|
|
|
|
}
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-22 14:53:44 +00:00
|
|
|
|
#region public static bool ShareContexts
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets a System.Boolean, indicating whether GLContexts are shared</summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If ShareContexts is true, new GLContexts will share resources. If this value is
|
|
|
|
|
/// false, new GLContexts will not share resources.</para>
|
|
|
|
|
/// <para>Changing this value will not affect already created GLContexts.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static bool ShareContexts { get { return share_contexts; } set { share_contexts = value; } }
|
|
|
|
|
|
2008-01-23 12:42:47 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public static AvailableDisplayFormats
|
|
|
|
|
|
|
|
|
|
|
2008-01-22 14:53:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGLContext Members ---
|
|
|
|
|
|
2007-09-09 16:07:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an OpenGL context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CreateContext()
|
|
|
|
|
{
|
2007-12-09 18:15:51 +00:00
|
|
|
|
CreateContext(true, null);
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an OpenGL context with a direct or indirect rendering mode. This parameter is ignored
|
|
|
|
|
/// on Windows platforms (direct mode only).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="direct">Set to true for direct rendering or false otherwise.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Direct rendering is the default rendering mode for OpenTK, since it can provide higher performance
|
|
|
|
|
/// in some circumastances.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The 'direct' parameter is a hint, and will ignored if the specified mode is not supported (e.g. setting
|
|
|
|
|
/// indirect rendering on Windows platforms).
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void CreateContext(bool direct)
|
|
|
|
|
{
|
2007-12-09 18:15:51 +00:00
|
|
|
|
CreateContext(direct, null);
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an OpenGL context with the specified direct/indirect rendering mode and sharing state with the
|
|
|
|
|
/// specified IGLContext.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="direct">Set to true for direct rendering or false otherwise.</param>
|
|
|
|
|
/// <param name="source">The source IGLContext to share state from.</param>.
|
|
|
|
|
/// <seealso cref="CreateContext(bool)"/>
|
|
|
|
|
public void CreateContext(bool direct, IGLContext source)
|
|
|
|
|
{
|
|
|
|
|
implementation.CreateContext(direct, source);
|
2008-01-11 20:31:59 +00:00
|
|
|
|
this.Destroy += ContextDestroyed;
|
2007-12-09 18:15:51 +00:00
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
available_contexts.Add((this as IGLContextInternal).Context, new WeakReference(this));
|
|
|
|
|
//if (StaticGetCurrentContext == null)
|
|
|
|
|
// StaticGetCurrentContext = implementation.GetCurrentContext;
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Swaps buffers on a context. This presents the rendered scene to the user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
|
|
|
|
implementation.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Makes this context the current rendering target.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void MakeCurrent()
|
|
|
|
|
{
|
|
|
|
|
implementation.MakeCurrent();
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a System.Boolean indicating whether this Context is current in the calling thread.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsCurrent
|
|
|
|
|
{
|
|
|
|
|
get { return implementation.IsCurrent; }
|
2008-01-31 13:15:17 +00:00
|
|
|
|
set { implementation.IsCurrent = value; }
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// Raised when a Context is destroyed.
|
2007-12-09 18:15:51 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
public event DestroyEvent<IGLContext> Destroy
|
2007-12-09 18:15:51 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
add { implementation.Destroy += value; }
|
|
|
|
|
remove { implementation.Destroy -= value; }
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// Gets or sets a value indicating whether VSync is enabled.
|
2007-12-09 18:15:51 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
public bool VSync
|
2007-12-09 18:15:51 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
get { return implementation.VSync; }
|
|
|
|
|
set { implementation.VSync = value; }
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGLContextInternal Members ---
|
|
|
|
|
|
2007-09-09 16:07:39 +00:00
|
|
|
|
/// <summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// Gets a handle to the OpenGL rendering context.
|
2007-09-09 16:07:39 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
ContextHandle IGLContextInternal.Context
|
2007-09-09 16:07:39 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
get { return ((IGLContextInternal)implementation).Context; }
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// Gets the IWindowInfo describing the window associated with this context.
|
2007-09-09 16:07:39 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
IWindowInfo IGLContextInternal.Info
|
2007-09-09 16:07:39 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
get { return (implementation as IGLContextInternal).Info; }
|
|
|
|
|
//internal set { (implementation as IGLContextInternal).Info = value; }
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-29 15:24:55 +00:00
|
|
|
|
/// <summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// Gets the DisplayMode of the context.
|
2007-09-29 15:24:55 +00:00
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
DisplayMode IGLContextInternal.Mode
|
2007-09-29 15:24:55 +00:00
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
get { return (implementation as IGLContextInternal).Mode; }
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-31 13:15:17 +00:00
|
|
|
|
///// <summary>
|
|
|
|
|
///// Gets a System.IntPtr containing the handle to the OpenGL context which is current in the
|
|
|
|
|
///// calling thread, or IntPtr.Zero if no OpenGL context is current.
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <returns>A System.IntPtr that holds the handle to the current OpenGL context.</returns>
|
|
|
|
|
//ContextHandle IGLContextInternal.GetCurrentContext()
|
|
|
|
|
//{
|
|
|
|
|
// return (implementation as IGLContextInternal).GetCurrentContext();
|
|
|
|
|
//}
|
2007-09-29 15:24:55 +00:00
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an OpenGL resource for disposal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="resource">The OpenGL resource to dispose.</param>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
void IGLContextInternal.RegisterForDisposal(IDisposable resource)
|
2007-12-09 18:15:51 +00:00
|
|
|
|
{
|
|
|
|
|
GC.KeepAlive(resource);
|
|
|
|
|
dispose_queue.Add(resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all registered OpenGL resources.
|
|
|
|
|
/// </summary>
|
2008-01-11 20:31:59 +00:00
|
|
|
|
void IGLContextInternal.DisposeResources()
|
2007-12-09 18:15:51 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (IDisposable resource in dispose_queue)
|
|
|
|
|
resource.Dispose();
|
2008-01-11 20:31:59 +00:00
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
dispose_queue.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the display modes supported by the current opengl context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>An IEnumerable containing all supported display modes.</returns>
|
|
|
|
|
IEnumerable<DisplayMode> IGLContextInternal.GetDisplayModes()
|
|
|
|
|
{
|
|
|
|
|
return (implementation as IGLContextInternal).GetDisplayModes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the address of an OpenGL extension function.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="function">The name of the OpenGL function (e.g. "glGetString")</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// A pointer to the specified function or IntPtr.Zero if the function isn't
|
|
|
|
|
/// available in the current opengl context.
|
|
|
|
|
/// </returns>
|
|
|
|
|
/// <see cref="Marshal.GetDelegateForFunctionPointer"/>
|
|
|
|
|
IntPtr IGLContextInternal.GetAddress(string function)
|
|
|
|
|
{
|
|
|
|
|
return (implementation as IGLContextInternal).GetAddress(function);
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-09 16:07:39 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-11 20:31:59 +00:00
|
|
|
|
#region --- IGLContextCreationHack Members ---
|
|
|
|
|
|
|
|
|
|
bool IGLContextCreationHack.SelectDisplayMode(DisplayMode mode, IWindowInfo info)
|
|
|
|
|
{
|
|
|
|
|
return (implementation as IGLContextCreationHack).SelectDisplayMode(mode, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IGLContextCreationHack.SetWindowHandle(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
(implementation as IGLContextCreationHack).SetWindowHandle(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
2007-09-09 16:07:39 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes of the GLContext.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2008-01-11 20:31:59 +00:00
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Check if this is safe
|
|
|
|
|
//if (manual)
|
|
|
|
|
{
|
|
|
|
|
implementation.Dispose();
|
|
|
|
|
}
|
2008-01-20 19:00:03 +00:00
|
|
|
|
disposed = true;
|
2008-01-11 20:31:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~GLContext()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
2007-09-09 16:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2008-01-23 00:20:02 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Represents errors related to Graphics operations.</summary>
|
|
|
|
|
public class GraphicsException : Exception
|
|
|
|
|
{
|
|
|
|
|
public GraphicsException() : base() { }
|
|
|
|
|
public GraphicsException(string message) : base(message) { }
|
|
|
|
|
}
|
2008-01-18 14:15:23 +00:00
|
|
|
|
}
|