2009-02-22 11:43:35 +01:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
|
* See license.txt for licensing detailed licensing details.
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Platform;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Graphics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents and provides methods to manipulate an OpenGL render context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class GraphicsContext : IGraphicsContext, IGraphicsContextInternal
|
|
|
|
|
{
|
2009-03-07 11:36:51 +01:00
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
|
IGraphicsContext implementation; // The actual render context implementation for the underlying platform.
|
|
|
|
|
List<IDisposable> dispose_queue = new List<IDisposable>();
|
|
|
|
|
bool disposed;
|
|
|
|
|
// Indicates that this context was created through external means, e.g. Tao.Sdl or GLWidget#.
|
|
|
|
|
// In this case, We'll assume that the external program will manage the lifetime of this
|
|
|
|
|
// context - we'll not destroy it manually.
|
2009-03-29 23:41:30 +02:00
|
|
|
|
//bool is_external;
|
|
|
|
|
bool check_errors = true;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
|
|
static bool share_contexts = true;
|
|
|
|
|
static bool direct_rendering = true;
|
2009-03-07 11:36:51 +01:00
|
|
|
|
readonly static object context_lock = new object();
|
2009-02-22 11:43:35 +01:00
|
|
|
|
// Maps OS-specific context handles to GraphicsContext weak references.
|
2009-03-07 11:36:51 +01:00
|
|
|
|
readonly static Dictionary<ContextHandle, WeakReference> available_contexts = new Dictionary<ContextHandle, WeakReference>();
|
|
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2009-05-30 21:27:52 +02:00
|
|
|
|
static GraphicsContext()
|
|
|
|
|
{
|
|
|
|
|
GetCurrentContext = Factory.CreateGetCurrentGraphicsContext();
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
// Necessary to allow creation of dummy GraphicsContexts (see CreateDummyContext static method).
|
2009-05-30 21:27:52 +02:00
|
|
|
|
GraphicsContext(ContextHandle handle)
|
|
|
|
|
{
|
|
|
|
|
implementation = new OpenTK.Platform.Dummy.DummyGLContext(handle);
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-05-30 21:27:52 +02:00
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
available_contexts.Add((implementation as IGraphicsContextInternal).Context, new WeakReference(this));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// <summary>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
/// Constructs a new GraphicsContext with the specified GraphicsMode and attaches it to the specified window.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
2009-03-25 22:53:12 +01:00
|
|
|
|
/// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
|
|
|
|
|
public GraphicsContext(GraphicsMode mode, IWindowInfo window)
|
2009-03-07 11:36:51 +01:00
|
|
|
|
: this(mode, window, 1, 0, GraphicsContextFlags.Default)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags, and attaches it to the specified window.
|
|
|
|
|
/// </summary>
|
2009-03-25 22:53:12 +01:00
|
|
|
|
/// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
/// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
|
|
|
|
|
/// <param name="major">The major version of the new GraphicsContext.</param>
|
|
|
|
|
/// <param name="minor">The minor version of the new GraphicsContext.</param>
|
|
|
|
|
/// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public GraphicsContext(GraphicsMode mode, IWindowInfo window, int major, int minor, GraphicsContextFlags flags)
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
|
|
|
|
bool designMode = false;
|
|
|
|
|
if (mode == null && window == null)
|
|
|
|
|
designMode = true;
|
|
|
|
|
else if (mode == null) throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
|
|
|
|
|
else if (window == null) throw new ArgumentNullException("window", "Must point to a valid window.");
|
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
// Silently ignore invalid major and minor versions.
|
|
|
|
|
if (major <= 0)
|
|
|
|
|
major = 1;
|
|
|
|
|
if (minor < 0)
|
|
|
|
|
minor = 0;
|
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
|
Debug.Print("Creating GraphicsContext.");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Debug.Indent();
|
|
|
|
|
Debug.Print("GraphicsMode: {0}", mode);
|
|
|
|
|
Debug.Print("IWindowInfo: {0}", window);
|
|
|
|
|
|
|
|
|
|
IGraphicsContext shareContext = null;
|
|
|
|
|
if (GraphicsContext.ShareContexts)
|
|
|
|
|
{
|
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
// A small hack to create a shared context with the first available context.
|
|
|
|
|
foreach (WeakReference r in GraphicsContext.available_contexts.Values)
|
|
|
|
|
{
|
|
|
|
|
shareContext = (IGraphicsContext)r.Target;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
// Todo: Add a DummyFactory implementing IPlatformFactory.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
if (designMode)
|
2009-05-30 21:27:52 +02:00
|
|
|
|
implementation = new Platform.Dummy.DummyGLContext();
|
2009-02-22 11:43:35 +01:00
|
|
|
|
else
|
2009-03-07 11:36:51 +01:00
|
|
|
|
implementation = Factory.CreateGLContext(mode, window, shareContext, DirectRendering, major, minor, flags);
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
available_contexts.Add((this as IGraphicsContextInternal).Context, new WeakReference(this));
|
|
|
|
|
}
|
|
|
|
|
//(implementation as IGraphicsContextInternal).LoadAll();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
#region --- Static Members ---
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
#region public static GraphicsContext CreateDummyContext()
|
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// <summary>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
/// Creates a dummy GraphicsContext to allow OpenTK to work with contexts created by external libraries.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
/// <returns>A new, dummy GraphicsContext instance.</returns>
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// <remarks>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
/// <para>Instances created by this methodwill not be functional. Instance methods will have no effect.</para>
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </remarks>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
public static GraphicsContext CreateDummyContext()
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
2009-05-30 21:27:52 +02:00
|
|
|
|
ContextHandle handle = GetCurrentContext();
|
|
|
|
|
if (handle == ContextHandle.Zero)
|
|
|
|
|
throw new InvalidOperationException("No GraphicsContext is current on the calling thread.");
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-05-30 21:27:52 +02:00
|
|
|
|
return CreateDummyContext(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GraphicsContext CreateDummyContext(ContextHandle handle)
|
|
|
|
|
{
|
|
|
|
|
if (handle == ContextHandle.Zero)
|
|
|
|
|
throw new ArgumentOutOfRangeException("handle");
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-05-30 21:27:52 +02:00
|
|
|
|
return new GraphicsContext(handle);
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 11:36:51 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
#region public static void Assert()
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// Checks if a GraphicsContext exists in the calling thread and throws a GraphicsContextException if it doesn't.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// <exception cref="GraphicsContextMissingException">Generated when no GraphicsContext is current in the calling thread.</exception>
|
|
|
|
|
public static void Assert()
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
2009-03-25 18:55:37 +01:00
|
|
|
|
if (GraphicsContext.CurrentContext == null)
|
|
|
|
|
throw new GraphicsContextMissingException();
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public static IGraphicsContext CurrentContext
|
|
|
|
|
|
|
|
|
|
internal delegate ContextHandle GetCurrentContextDelegate();
|
|
|
|
|
internal static GetCurrentContextDelegate GetCurrentContext;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// Gets the GraphicsContext that is current in the calling thread.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static GraphicsContext CurrentContext
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
if (available_contexts.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
ContextHandle handle = GetCurrentContext();
|
|
|
|
|
if (handle.Handle != IntPtr.Zero)
|
|
|
|
|
return (GraphicsContext)available_contexts[handle].Target;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public static bool ShareContexts
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets a System.Boolean, indicating whether GraphicsContext resources 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; } }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public static bool DirectRendering
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets a System.Boolean, indicating whether GraphicsContexts will perform direct rendering.</summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If DirectRendering is true, new contexts will be constructed with direct rendering capabilities, if possible.
|
|
|
|
|
/// If DirectRendering is false, new contexts will be constructed with indirect rendering capabilities.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>This property does not affect existing GraphicsContexts, unless they are recreated.</para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// This property is ignored on Operating Systems without support for indirect rendering, like Windows and OS X.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static bool DirectRendering
|
|
|
|
|
{
|
|
|
|
|
get { return direct_rendering; }
|
|
|
|
|
set { direct_rendering = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Internal Members ---
|
|
|
|
|
|
|
|
|
|
bool inside_begin_region;
|
|
|
|
|
List<ErrorCode> error_list = new List<ErrorCode>();
|
|
|
|
|
|
|
|
|
|
// Indicates that we entered a GL.Begin() - GL.End() region.
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
internal void EnterBeginRegion()
|
|
|
|
|
{
|
|
|
|
|
inside_begin_region = true;
|
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
// Indicates that we left a GL.Begin() - GL.End() region.
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
internal void ExitBeginRegion()
|
|
|
|
|
{
|
|
|
|
|
inside_begin_region = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve all OpenGL errors to clear the error list.
|
|
|
|
|
// See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/geterror.html
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
internal void ResetErrors()
|
|
|
|
|
{
|
2009-03-29 23:41:30 +02:00
|
|
|
|
if (check_errors && !inside_begin_region)
|
2009-03-25 18:55:37 +01:00
|
|
|
|
{
|
|
|
|
|
while (GL.GetError() != ErrorCode.NoError)
|
|
|
|
|
{ }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve all OpenGL errors and throw an exception if anything other than NoError is returned.
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
internal void CheckErrors()
|
|
|
|
|
{
|
2009-03-29 23:41:30 +02:00
|
|
|
|
if (check_errors && !inside_begin_region)
|
2009-03-25 18:55:37 +01:00
|
|
|
|
{
|
|
|
|
|
error_list.Clear();
|
|
|
|
|
ErrorCode error;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
error = GL.GetError();
|
|
|
|
|
error_list.Add(error);
|
|
|
|
|
} while (error != ErrorCode.NoError);
|
|
|
|
|
|
|
|
|
|
if (error_list.Count != 1)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (ErrorCode e in error_list)
|
|
|
|
|
{
|
2009-03-29 23:41:30 +02:00
|
|
|
|
if (e != ErrorCode.NoError)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(e.ToString());
|
|
|
|
|
sb.Append(", ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
2009-03-25 18:55:37 +01:00
|
|
|
|
}
|
2009-03-29 23:41:30 +02:00
|
|
|
|
sb.Remove(sb.Length - 2, 2); // Remove the last comma
|
2009-03-25 18:55:37 +01:00
|
|
|
|
|
2009-04-28 19:38:08 +02:00
|
|
|
|
throw new OpenGLErrorException(sb.ToString());
|
2009-03-25 18:55:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
#region --- Private Members ---
|
|
|
|
|
|
|
|
|
|
#region void ContextDestroyed(IGraphicsContext context, EventArgs e)
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// Handles the Destroy event.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// <param name="context">The OpenTK.Platform.IGraphicsContext that was destroyed.</param>
|
|
|
|
|
/// <param name="e">Not used.</param>
|
|
|
|
|
void ContextDestroyed(IGraphicsContext context, EventArgs e)
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
2009-03-25 18:55:37 +01:00
|
|
|
|
this.Destroy -= ContextDestroyed;
|
|
|
|
|
//available_contexts.Remove(((IGraphicsContextInternal)this).Context);
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGraphicsContext Members ---
|
|
|
|
|
|
2009-03-29 23:41:30 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a System.Boolean, indicating whether automatic error checking should be performed.
|
|
|
|
|
/// Influences the debug version of OpenTK.dll, only.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Automatic error checking will clear the OpenGL error state. Set CheckErrors to false if you use
|
|
|
|
|
/// the OpenGL error state in your code flow (e.g. for checking supported texture formats).</remarks>
|
|
|
|
|
public bool ErrorChecking
|
|
|
|
|
{
|
|
|
|
|
get { return check_errors; }
|
|
|
|
|
set { check_errors = value; }
|
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an OpenGL context with the specified direct/indirect rendering mode and sharing state with the
|
|
|
|
|
/// specified IGraphicsContext.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="direct">Set to true for direct rendering or false otherwise.</param>
|
|
|
|
|
/// <param name="source">The source IGraphicsContext to share state from.</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>
|
|
|
|
|
void CreateContext(bool direct, IGraphicsContext source)
|
|
|
|
|
{
|
|
|
|
|
this.Destroy += ContextDestroyed;
|
|
|
|
|
|
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
available_contexts.Add((this as IGraphicsContextInternal).Context, new WeakReference(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//OpenTK.Graphics.OpenGL.GL.Clear(OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit);
|
|
|
|
|
//if (StaticGetCurrentContext == null)
|
|
|
|
|
// StaticGetCurrentContext = implementation.GetCurrentContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Swaps buffers on a context. This presents the rendered scene to the user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
|
|
|
|
implementation.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Makes the GraphicsContext the current rendering target.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">A System.Platform.IWindowInfo structure for the window this context is bound to.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// You can use this method to bind the GraphicsContext to a different window than the one it was created from.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void MakeCurrent(IWindowInfo info)
|
|
|
|
|
{
|
|
|
|
|
implementation.MakeCurrent(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a System.Boolean indicating whether this Context is current in the calling thread.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsCurrent
|
|
|
|
|
{
|
|
|
|
|
get { return implementation.IsCurrent; }
|
|
|
|
|
//set { implementation.IsCurrent = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raised when a Context is destroyed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event DestroyEvent<IGraphicsContext> Destroy
|
|
|
|
|
{
|
|
|
|
|
add { implementation.Destroy += value; }
|
|
|
|
|
remove { implementation.Destroy -= value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether VSync is enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool VSync
|
|
|
|
|
{
|
|
|
|
|
get { return implementation.VSync; }
|
|
|
|
|
set { implementation.VSync = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the graphics context. This must be called when the render target
|
|
|
|
|
/// is resized for proper behavior on Mac OS X.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="window"></param>
|
|
|
|
|
public void Update(IWindowInfo window)
|
|
|
|
|
{
|
|
|
|
|
implementation.Update(window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGraphicsContextInternal Members ---
|
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the platform-specific implementation of this IGraphicsContext.
|
|
|
|
|
/// </summary>
|
2009-03-07 11:36:51 +01:00
|
|
|
|
IGraphicsContext IGraphicsContextInternal.Implementation
|
|
|
|
|
{
|
|
|
|
|
get { return implementation; }
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads all OpenGL extensions.
|
|
|
|
|
/// </summary>
|
2009-02-22 11:43:35 +01:00
|
|
|
|
void IGraphicsContextInternal.LoadAll()
|
|
|
|
|
{
|
|
|
|
|
(implementation as IGraphicsContextInternal).LoadAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
/// Gets a handle to the OpenGL rendering context.
|
2009-02-22 11:43:35 +01:00
|
|
|
|
/// </summary>
|
2009-03-25 18:55:37 +01:00
|
|
|
|
ContextHandle IGraphicsContextInternal.Context
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
2009-03-25 18:55:37 +01:00
|
|
|
|
get { return ((IGraphicsContextInternal)implementation).Context; }
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the GraphicsMode of the context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GraphicsMode GraphicsMode
|
|
|
|
|
{
|
2009-03-25 19:05:52 +01:00
|
|
|
|
get { return (implementation as IGraphicsContext).GraphicsMode; }
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an OpenGL resource for disposal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="resource">The OpenGL resource to dispose.</param>
|
|
|
|
|
void IGraphicsContextInternal.RegisterForDisposal(IDisposable resource)
|
|
|
|
|
{
|
|
|
|
|
GC.KeepAlive(resource);
|
|
|
|
|
dispose_queue.Add(resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all registered OpenGL resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void IGraphicsContextInternal.DisposeResources()
|
|
|
|
|
{
|
|
|
|
|
foreach (IDisposable resource in dispose_queue)
|
|
|
|
|
resource.Dispose();
|
|
|
|
|
|
|
|
|
|
dispose_queue.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
IntPtr IGraphicsContextInternal.GetAddress(string function)
|
|
|
|
|
{
|
|
|
|
|
return (implementation as IGraphicsContextInternal).GetAddress(function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes of the GraphicsContext.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
2009-05-27 20:40:29 +02:00
|
|
|
|
Debug.Print("Disposing context {0}.", (this as IGraphicsContextInternal).Context.ToString());
|
2009-02-22 11:43:35 +01:00
|
|
|
|
lock (context_lock)
|
|
|
|
|
{
|
|
|
|
|
available_contexts.Remove((this as IGraphicsContextInternal).Context);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-29 23:41:30 +02:00
|
|
|
|
if (manual)
|
2009-02-22 11:43:35 +01:00
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
implementation.Dispose();
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~GraphicsContext()
|
|
|
|
|
//{
|
|
|
|
|
// this.Dispose(false);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region public class GraphicsException : Exception
|
|
|
|
|
|
|
|
|
|
/// <summary>Represents errors related to Graphics operations.</summary>
|
|
|
|
|
public class GraphicsException : Exception
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Constructs a new GraphicsException.</summary>
|
|
|
|
|
public GraphicsException() : base() { }
|
|
|
|
|
/// <summary>Constructs a new GraphicsException with the specified excpetion message.</summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
public GraphicsException(string message) : base(message) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2009-04-28 19:38:08 +02:00
|
|
|
|
|
|
|
|
|
#region class GraphicsErrorException : GraphicsException
|
|
|
|
|
|
|
|
|
|
// This is private by design. These exceptions are only thrown in debug builds of
|
|
|
|
|
// OpenTK and the user should *not* handle them.
|
|
|
|
|
// If some specific OpenGL error is generated by design, the user should
|
|
|
|
|
// turn off automatic error checking for that portion of the code, using
|
|
|
|
|
// 'GraphicsContext.ErrorChecking = false'.
|
|
|
|
|
class OpenGLErrorException : GraphicsException
|
|
|
|
|
{
|
|
|
|
|
public OpenGLErrorException(string message) : base(message) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
|
}
|