2013-11-14 16:37:39 +00:00
|
|
|
#region --- License ---
|
2009-02-22 10:43:35 +00:00
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* Contributions from Erik Ylvisaker
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
using OpenTK.Graphics;
|
2009-08-14 13:13:28 +00:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
{
|
2010-03-11 22:53:11 +00:00
|
|
|
/// \internal
|
2009-02-22 10:43:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Provides methods to create and control an opengl context on the Windows platform.
|
|
|
|
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
|
|
|
|
/// </summary>
|
2009-08-17 10:23:16 +00:00
|
|
|
internal sealed class WinGLContext : DesktopGraphicsContext
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
static readonly object LoadLock = new object();
|
2009-11-03 23:39:03 +00:00
|
|
|
|
2013-12-18 13:51:00 +00:00
|
|
|
IntPtr device_context;
|
2009-02-22 10:43:35 +00:00
|
|
|
bool vsync_supported;
|
|
|
|
|
2013-11-14 07:05:08 +00:00
|
|
|
readonly WinGraphicsMode ModeSelector;
|
2013-11-08 09:46:27 +00:00
|
|
|
|
2013-12-18 13:29:06 +00:00
|
|
|
// We need to create a temp context in order to load
|
|
|
|
// wgl extensions (e.g. for multisampling or GL3).
|
|
|
|
// We cannot rely on any WGL extensions before
|
|
|
|
// we load them with the temporary context.
|
|
|
|
class TemporaryContext : IDisposable
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
public ContextHandle Context;
|
|
|
|
|
|
|
|
public TemporaryContext(INativeWindow native)
|
2013-11-08 09:46:27 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
Debug.WriteLine("[WGL] Creating temporary context to load extensions");
|
|
|
|
|
|
|
|
if (native == null)
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
|
|
// Create temporary context and load WGL entry points
|
|
|
|
// First, set a compatible pixel format to the device context
|
|
|
|
// of the temp window
|
|
|
|
WinWindowInfo window = native.WindowInfo as WinWindowInfo;
|
|
|
|
WinGraphicsMode selector = new WinGraphicsMode(window.DeviceContext);
|
|
|
|
WinGLContext.SetGraphicsModePFD(selector, GraphicsMode.Default, window);
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
// Then, construct a temporary context and load all wgl extensions
|
|
|
|
Context = new ContextHandle(Wgl.CreateContext(window.DeviceContext));
|
|
|
|
if (Context != ContextHandle.Zero)
|
2013-11-14 07:05:08 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
// Make the context current.
|
|
|
|
// Note: on some video cards and on some virtual machines, wglMakeCurrent
|
|
|
|
// may fail with an errorcode of 6 (INVALID_HANDLE). The suggested workaround
|
|
|
|
// is to call wglMakeCurrent in a loop until it succeeds.
|
|
|
|
// See https://www.opengl.org/discussion_boards/showthread.php/171058-nVidia-wglMakeCurrent()-multiple-threads
|
|
|
|
// Sigh...
|
|
|
|
for (int retry = 0; retry < 5 && !success; retry++)
|
2013-11-14 07:05:08 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
success = Wgl.MakeCurrent(window.DeviceContext, Context.Handle);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
Debug.Print("wglMakeCurrent failed with error: {0}. Retrying", Marshal.GetLastWin32Error());
|
|
|
|
System.Threading.Thread.Sleep(10);
|
|
|
|
}
|
2013-11-14 07:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 13:29:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("[WGL] CreateContext failed with error: {0}", Marshal.GetLastWin32Error());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
Debug.WriteLine("[WGL] Failed to create temporary context");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (Context != ContextHandle.Zero)
|
|
|
|
{
|
|
|
|
Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
|
|
|
|
Wgl.DeleteContext(Context.Handle);
|
|
|
|
}
|
2013-11-08 09:46:27 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 13:29:06 +00:00
|
|
|
#region --- Contructors ---
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext,
|
2009-03-07 10:36:51 +00:00
|
|
|
int major, int minor, GraphicsContextFlags flags)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-03 23:39:03 +00:00
|
|
|
// There are many ways this code can break when accessed by multiple threads. The biggest offender is
|
|
|
|
// the sharedContext stuff, which will only become valid *after* this constructor returns.
|
|
|
|
// The easiest solution is to serialize all context construction - hence the big lock, below.
|
2013-12-18 13:29:06 +00:00
|
|
|
lock (LoadLock)
|
2009-11-03 23:39:03 +00:00
|
|
|
{
|
|
|
|
if (window == null)
|
|
|
|
throw new ArgumentNullException("window", "Must point to a valid window.");
|
2013-09-30 18:47:29 +00:00
|
|
|
if (window.Handle == IntPtr.Zero)
|
2009-11-03 23:39:03 +00:00
|
|
|
throw new ArgumentException("window", "Must be a valid window.");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2013-12-18 13:29:06 +00:00
|
|
|
IntPtr current_context = Wgl.GetCurrentContext();
|
|
|
|
INativeWindow temp_window = null;
|
|
|
|
TemporaryContext temp_context = null;
|
|
|
|
try
|
2009-11-03 23:39:03 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
if (current_context == IntPtr.Zero)
|
|
|
|
{
|
|
|
|
// Create temporary context to load WGL extensions
|
|
|
|
temp_window = new NativeWindow();
|
|
|
|
temp_context = new TemporaryContext(temp_window);
|
|
|
|
current_context = Wgl.GetCurrentContext();
|
|
|
|
if (current_context != IntPtr.Zero && current_context == temp_context.Context.Handle)
|
|
|
|
{
|
|
|
|
Wgl.LoadAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.Handle,
|
|
|
|
System.Threading.Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
2013-11-14 07:05:08 +00:00
|
|
|
ModeSelector = new WinGraphicsMode(window.DeviceContext);
|
|
|
|
Mode = SetGraphicsModePFD(ModeSelector, format, (WinWindowInfo)window);
|
2009-02-28 19:56:08 +00:00
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
if (Wgl.Delegates.wglCreateContextAttribsARB != null)
|
2009-03-07 10:36:51 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
try
|
2009-11-03 23:39:03 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
Debug.Write("Using WGL_ARB_create_context... ");
|
|
|
|
|
|
|
|
List<int> attributes = new List<int>();
|
|
|
|
attributes.Add((int)ArbCreateContext.MajorVersion);
|
|
|
|
attributes.Add(major);
|
|
|
|
attributes.Add((int)ArbCreateContext.MinorVersion);
|
|
|
|
attributes.Add(minor);
|
|
|
|
if (flags != 0)
|
|
|
|
{
|
2013-11-14 07:30:11 +00:00
|
|
|
attributes.Add((int)ArbCreateContext.ContextFlags);
|
|
|
|
attributes.Add((int)GetARBContextFlags(flags));
|
|
|
|
attributes.Add((int)ArbCreateContext.ProfileMask);
|
|
|
|
attributes.Add((int)GetARBContextProfile(flags));
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
|
|
|
// According to the docs, " <attribList> specifies a list of attributes for the context.
|
|
|
|
// The list consists of a sequence of <name,value> pairs terminated by the
|
|
|
|
// value 0. [...]"
|
|
|
|
// Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case).
|
|
|
|
attributes.Add(0);
|
|
|
|
attributes.Add(0);
|
|
|
|
|
|
|
|
Handle = new ContextHandle(
|
|
|
|
Wgl.Arb.CreateContextAttribs(
|
|
|
|
window.DeviceContext,
|
|
|
|
sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero,
|
|
|
|
attributes.ToArray()));
|
|
|
|
if (Handle == ContextHandle.Zero)
|
|
|
|
Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error());
|
2009-11-03 23:39:03 +00:00
|
|
|
}
|
2013-12-18 13:29:06 +00:00
|
|
|
catch (Exception e) { Debug.Print(e.ToString()); }
|
2009-03-07 10:36:51 +00:00
|
|
|
}
|
2009-02-28 19:56:08 +00:00
|
|
|
|
2009-11-03 23:39:03 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
2013-12-18 13:29:06 +00:00
|
|
|
{
|
|
|
|
// Failed to create GL3-level context, fall back to GL2.
|
|
|
|
Debug.Write("Falling back to GL2... ");
|
2013-11-28 22:22:35 +00:00
|
|
|
Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext));
|
2013-12-18 13:29:06 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
|
|
|
Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext));
|
|
|
|
if (Handle == ContextHandle.Zero)
|
|
|
|
throw new GraphicsContextException(
|
|
|
|
String.Format("Context creation failed. Wgl.CreateContext() error: {0}.",
|
|
|
|
Marshal.GetLastWin32Error()));
|
|
|
|
}
|
2013-11-14 07:05:08 +00:00
|
|
|
|
2013-12-18 13:29:06 +00:00
|
|
|
Debug.WriteLine(String.Format("success! (id: {0})", Handle));
|
|
|
|
}
|
|
|
|
finally
|
2009-11-03 23:39:03 +00:00
|
|
|
{
|
2013-12-18 13:29:06 +00:00
|
|
|
if (temp_context != null)
|
|
|
|
{
|
|
|
|
temp_context.Dispose();
|
|
|
|
temp_context = null;
|
|
|
|
}
|
|
|
|
if (temp_window != null)
|
|
|
|
{
|
|
|
|
temp_window.Dispose();
|
|
|
|
temp_window = null;
|
|
|
|
}
|
2009-11-03 23:39:03 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2013-12-18 13:29:06 +00:00
|
|
|
|
|
|
|
// Todo: is this comment still true?
|
|
|
|
// On intel drivers, wgl entry points appear to change
|
|
|
|
// when creating multiple contexts. As a workaround,
|
|
|
|
// we reload Wgl entry points every time we create a
|
|
|
|
// new context - this solves the issue without any apparent
|
|
|
|
// side-effects (i.e. the old contexts can still be handled
|
|
|
|
// using the new entry points.)
|
|
|
|
// Sigh...
|
2013-12-18 13:51:00 +00:00
|
|
|
MakeCurrent(window);
|
2013-12-18 13:29:06 +00:00
|
|
|
Wgl.LoadAll();
|
|
|
|
|
|
|
|
if (sharedContext != null)
|
|
|
|
{
|
|
|
|
Marshal.GetLastWin32Error();
|
|
|
|
Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext));
|
|
|
|
bool result = Wgl.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle);
|
|
|
|
Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error());
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 07:30:11 +00:00
|
|
|
static ArbCreateContext GetARBContextFlags(GraphicsContextFlags flags)
|
|
|
|
{
|
|
|
|
ArbCreateContext result = 0;
|
|
|
|
result |= (flags & GraphicsContextFlags.ForwardCompatible) != 0 ?
|
|
|
|
ArbCreateContext.CoreProfileBit : ArbCreateContext.CompatibilityProfileBit;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArbCreateContext GetARBContextProfile(GraphicsContextFlags flags)
|
|
|
|
{
|
|
|
|
ArbCreateContext result = 0;
|
|
|
|
result |= (flags & GraphicsContextFlags.Debug) != 0 ? ArbCreateContext.DebugBit : 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-11-04 16:49:56 +00:00
|
|
|
public WinGLContext(ContextHandle handle, WinWindowInfo window, IGraphicsContext sharedContext,
|
|
|
|
int major, int minor, GraphicsContextFlags flags)
|
|
|
|
{
|
|
|
|
if (handle == ContextHandle.Zero)
|
|
|
|
throw new ArgumentException("handle");
|
|
|
|
if (window == null)
|
|
|
|
throw new ArgumentNullException("window");
|
|
|
|
|
|
|
|
Handle = handle;
|
|
|
|
}
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#endregion
|
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
#region IGraphicsContext Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region SwapBuffers
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void SwapBuffers()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
if (!Functions.SwapBuffers(DeviceContext))
|
2009-05-19 09:48:47 +00:00
|
|
|
throw new GraphicsContextException(String.Format(
|
|
|
|
"Failed to swap buffers for context {0} current. Error: {1}", this, Marshal.GetLastWin32Error()));
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region MakeCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void MakeCurrent(IWindowInfo window)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
lock (LoadLock)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
bool success;
|
2009-08-17 10:23:16 +00:00
|
|
|
|
2013-11-14 07:05:08 +00:00
|
|
|
WinWindowInfo wnd = window as WinWindowInfo;
|
|
|
|
if (wnd != null)
|
2010-11-08 21:38:32 +00:00
|
|
|
{
|
2013-11-14 07:05:08 +00:00
|
|
|
if (wnd.Handle == IntPtr.Zero)
|
2010-11-08 21:38:32 +00:00
|
|
|
throw new ArgumentException("window", "Must point to a valid window.");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2013-11-28 22:22:35 +00:00
|
|
|
success = Wgl.MakeCurrent(wnd.DeviceContext, Handle.Handle);
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-28 22:22:35 +00:00
|
|
|
success = Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2013-12-18 13:51:00 +00:00
|
|
|
device_context = wnd.DeviceContext;
|
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
if (!success)
|
2013-12-18 13:51:00 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
throw new GraphicsContextException(String.Format(
|
|
|
|
"Failed to make context {0} current. Error: {1}", this, Marshal.GetLastWin32Error()));
|
2013-12-18 13:51:00 +00:00
|
|
|
}
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2010-11-08 21:38:32 +00:00
|
|
|
|
2009-07-19 17:50:41 +00:00
|
|
|
#endregion
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region IsCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override bool IsCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2013-11-28 22:22:35 +00:00
|
|
|
get { return Wgl.GetCurrentContext() == Handle.Handle; }
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2011-09-06 12:06:30 +00:00
|
|
|
#region SwapInterval
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2011-09-06 12:06:30 +00:00
|
|
|
public override int SwapInterval
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
lock (LoadLock)
|
|
|
|
{
|
2011-09-06 12:06:30 +00:00
|
|
|
if (vsync_supported)
|
|
|
|
return Wgl.Ext.GetSwapInterval();
|
|
|
|
else
|
|
|
|
return 0;
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
lock (LoadLock)
|
|
|
|
{
|
|
|
|
if (vsync_supported)
|
2011-09-06 12:06:30 +00:00
|
|
|
Wgl.Ext.SwapInterval(value);
|
2010-11-08 21:38:32 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region void LoadAll()
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void LoadAll()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
lock (LoadLock)
|
|
|
|
{
|
|
|
|
Wgl.LoadAll();
|
|
|
|
vsync_supported = Wgl.Arb.SupportsExtension(this, "WGL_EXT_swap_control") &&
|
|
|
|
Wgl.Load("wglGetSwapIntervalEXT") && Wgl.Load("wglSwapIntervalEXT");
|
|
|
|
}
|
2009-10-21 10:16:32 +00:00
|
|
|
|
|
|
|
base.LoadAll();
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2010-10-02 18:52:34 +00:00
|
|
|
#endregion
|
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
#region IGLContextInternal Members
|
2010-10-02 18:52:34 +00:00
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#region IWindowInfo IGLContextInternal.Info
|
|
|
|
/*
|
|
|
|
IWindowInfo IGraphicsContextInternal.Info
|
|
|
|
{
|
|
|
|
get { return (IWindowInfo)windowInfo; }
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region GetAddress
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override IntPtr GetAddress(string function_string)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2013-11-28 22:22:35 +00:00
|
|
|
IntPtr address = Wgl.GetProcAddress(function_string);
|
|
|
|
if (!IsValid(address))
|
|
|
|
{
|
2013-12-17 20:24:25 +00:00
|
|
|
address = Functions.GetProcAddress(WinFactory.OpenGLHandle, function_string);
|
2013-11-28 22:22:35 +00:00
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override IntPtr GetAddress(IntPtr function_string)
|
|
|
|
{
|
|
|
|
IntPtr address = Wgl.GetProcAddress(function_string);
|
2013-11-24 23:26:20 +00:00
|
|
|
if (!IsValid(address))
|
|
|
|
{
|
2013-12-17 20:24:25 +00:00
|
|
|
address = Functions.GetProcAddress(WinFactory.OpenGLHandle, function_string);
|
2013-11-24 23:26:20 +00:00
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsValid(IntPtr address)
|
|
|
|
{
|
2013-11-27 18:36:42 +00:00
|
|
|
// See https://www.opengl.org/wiki/Load_OpenGL_Functions
|
|
|
|
long a = address.ToInt64();
|
2013-12-18 13:29:06 +00:00
|
|
|
bool is_valid = (a < -1) || (a > 3);
|
2013-11-27 18:36:42 +00:00
|
|
|
return is_valid;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-09-30 18:47:29 +00:00
|
|
|
#region Internal Methods
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
#region SetGraphicsModePFD
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
// Note: there is no relevant ARB function.
|
2013-11-14 07:05:08 +00:00
|
|
|
internal static GraphicsMode SetGraphicsModePFD(WinGraphicsMode mode_selector,
|
|
|
|
GraphicsMode mode, WinWindowInfo window)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-11-08 21:38:32 +00:00
|
|
|
Debug.Write("Setting pixel format... ");
|
2013-11-08 09:46:27 +00:00
|
|
|
if (window == null)
|
|
|
|
throw new ArgumentNullException("window", "Must point to a valid window.");
|
2010-11-08 21:38:32 +00:00
|
|
|
|
2009-08-15 16:44:43 +00:00
|
|
|
if (!mode.Index.HasValue)
|
2013-11-08 09:46:27 +00:00
|
|
|
{
|
2013-11-14 07:05:08 +00:00
|
|
|
mode = mode_selector.SelectGraphicsMode(
|
2013-11-08 09:46:27 +00:00
|
|
|
mode.ColorFormat, mode.Depth, mode.Stencil,
|
|
|
|
mode.Samples, mode.AccumulatorFormat,
|
|
|
|
mode.Buffers, mode.Stereo);
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
PixelFormatDescriptor pfd = new PixelFormatDescriptor();
|
2013-12-17 21:35:30 +00:00
|
|
|
Functions.DescribePixelFormat(
|
2013-11-08 09:46:27 +00:00
|
|
|
window.DeviceContext, (int)mode.Index.Value,
|
2009-02-22 10:43:35 +00:00
|
|
|
API.PixelFormatDescriptorSize, ref pfd);
|
2013-12-18 13:29:06 +00:00
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
Debug.WriteLine(mode.Index.ToString());
|
2013-12-17 21:35:30 +00:00
|
|
|
|
|
|
|
if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref pfd))
|
2013-11-08 09:46:27 +00:00
|
|
|
{
|
2009-02-22 10:43:35 +00:00
|
|
|
throw new GraphicsContextException(String.Format(
|
2013-11-08 09:46:27 +00:00
|
|
|
"Requested GraphicsMode not available. SetPixelFormat error: {0}",
|
|
|
|
Marshal.GetLastWin32Error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region internal IntPtr DeviceContext
|
|
|
|
|
|
|
|
internal IntPtr DeviceContext
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-12-18 13:51:00 +00:00
|
|
|
return device_context;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
#region Overrides
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
/// <summary>Returns a System.String describing this OpenGL context.</summary>
|
|
|
|
/// <returns>A System.String describing this OpenGL context.</returns>
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return (this as IGraphicsContextInternal).Context.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2010-11-08 21:38:32 +00:00
|
|
|
#region IDisposable Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void Dispose()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Dispose(bool calledManually)
|
|
|
|
{
|
2009-09-03 12:23:11 +00:00
|
|
|
if (!IsDisposed)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
if (calledManually)
|
|
|
|
{
|
2009-05-19 09:48:47 +00:00
|
|
|
DestroyContext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
Debug.Print("[Warning] OpenGL context {0} leaked. Did you forget to call IGraphicsContext.Dispose()?",
|
2009-08-17 10:23:16 +00:00
|
|
|
Handle.Handle);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-09-03 12:23:11 +00:00
|
|
|
IsDisposed = true;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~WinGLContext()
|
|
|
|
{
|
|
|
|
Dispose(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#region private void DestroyContext()
|
|
|
|
|
|
|
|
private void DestroyContext()
|
|
|
|
{
|
2009-08-17 10:23:16 +00:00
|
|
|
if (Handle != ContextHandle.Zero)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2009-05-19 09:48:47 +00:00
|
|
|
// This will fail if the user calls Dispose() on thread X when the context is current on thread Y.
|
2013-11-28 22:22:35 +00:00
|
|
|
if (!Wgl.DeleteContext(Handle.Handle))
|
2009-02-22 10:43:35 +00:00
|
|
|
Debug.Print("Failed to destroy OpenGL context {0}. Error: {1}",
|
2009-08-17 10:23:16 +00:00
|
|
|
Handle.ToString(), Marshal.GetLastWin32Error());
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
catch (AccessViolationException e)
|
|
|
|
{
|
|
|
|
Debug.WriteLine("An access violation occured while destroying the OpenGL context. Please report at http://www.opentk.com.");
|
|
|
|
Debug.Indent();
|
|
|
|
Debug.Print("Marshal.GetLastWin32Error(): {0}", Marshal.GetLastWin32Error().ToString());
|
|
|
|
Debug.WriteLine(e.ToString());
|
|
|
|
Debug.Unindent();
|
|
|
|
}
|
2009-08-17 10:23:16 +00:00
|
|
|
Handle = ContextHandle.Zero;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|