Removed unimplement SetPixelFormatARB - no such function exists.

Added aggressive locking during context creation and delegate loading in order to avoid potential race conditions.
Now uses Wgl.Imports directly wherever possible in order to improve performance and avoid race conditions.
Slightly improved debugging messages.
This commit is contained in:
the_fiddler 2010-11-08 21:38:32 +00:00
parent 9150a99252
commit 045d3d73f6

View file

@ -27,7 +27,8 @@ namespace OpenTK.Platform.Windows
/// </summary>
internal sealed class WinGLContext : DesktopGraphicsContext
{
static object SyncRoot = new object();
static readonly object LoadLock = new object();
static readonly object SyncRoot = new object();
static IntPtr opengl32Handle;
static bool wgl_loaded;
@ -39,18 +40,7 @@ namespace OpenTK.Platform.Windows
static WinGLContext()
{
lock (SyncRoot)
{
// Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl.
if (opengl32Handle == IntPtr.Zero)
{
opengl32Handle = Functions.LoadLibrary(opengl32Name);
if (opengl32Handle == IntPtr.Zero)
throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}",
opengl32Name, Marshal.GetLastWin32Error()));
Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle));
}
}
Init();
}
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext,
@ -68,21 +58,22 @@ namespace OpenTK.Platform.Windows
Mode = format;
Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle);
Debug.Write("Setting pixel format... ");
Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.WindowHandle,
System.Threading.Thread.CurrentThread.ManagedThreadId);
this.SetGraphicsModePFD(format, (WinWindowInfo)window);
lock (LoadLock)
{
if (!wgl_loaded)
{
// We need to create a temp context in order to load wgl extensions (e.g. for multisampling or GL3).
// We cannot rely on OpenTK.Platform.Wgl until we create the context and call Wgl.LoadAll().
Debug.Print("Creating temporary context for wgl extensions.");
ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle);
Wgl.LoadAll();
Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
Wgl.DeleteContext(temp_context.Handle);
Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
Wgl.Imports.DeleteContext(temp_context.Handle);
wgl_loaded = true;
}
@ -121,6 +112,7 @@ namespace OpenTK.Platform.Windows
catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); }
catch (NullReferenceException e) { Debug.Print(e.ToString()); }
}
}
if (Handle == ContextHandle.Zero)
{
@ -140,7 +132,7 @@ namespace OpenTK.Platform.Windows
if (sharedContext != null)
{
Marshal.GetLastWin32Error();
Debug.Write("Sharing state with context {0}: ", sharedContext.ToString());
Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext));
bool result = Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle);
Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error());
}
@ -160,13 +152,13 @@ namespace OpenTK.Platform.Windows
#endregion
#region --- IGraphicsContext Members ---
#region IGraphicsContext Members
#region SwapBuffers
public override void SwapBuffers()
{
if (!Functions.SwapBuffers(Wgl.GetCurrentDC()))
if (!Functions.SwapBuffers(DeviceContext))
throw new GraphicsContextException(String.Format(
"Failed to swap buffers for context {0} current. Error: {1}", this, Marshal.GetLastWin32Error()));
}
@ -176,6 +168,9 @@ namespace OpenTK.Platform.Windows
#region MakeCurrent
public override void MakeCurrent(IWindowInfo window)
{
lock (SyncRoot)
lock (LoadLock)
{
bool success;
@ -187,20 +182,23 @@ namespace OpenTK.Platform.Windows
success = Wgl.Imports.MakeCurrent(((WinWindowInfo)window).DeviceContext, Handle.Handle);
}
else
{
success = Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
}
if (!success)
throw new GraphicsContextException(String.Format(
"Failed to make context {0} current. Error: {1}", this, Marshal.GetLastWin32Error()));
}
}
#endregion
#region IsCurrent
public override bool IsCurrent
{
get { return Wgl.GetCurrentContext() == Handle.Handle; }
get { return Wgl.Imports.GetCurrentContext() == Handle.Handle; }
}
#endregion
@ -213,25 +211,34 @@ namespace OpenTK.Platform.Windows
public override bool VSync
{
get
{
lock (LoadLock)
{
return vsync_supported && Wgl.Ext.GetSwapInterval() != 0;
}
}
set
{
lock (LoadLock)
{
if (vsync_supported)
Wgl.Ext.SwapInterval(value ? 1 : 0);
}
}
}
#endregion
#region void LoadAll()
public override void LoadAll()
{
lock (LoadLock)
{
Wgl.LoadAll();
vsync_supported = Wgl.Arb.SupportsExtension(this, "WGL_EXT_swap_control") &&
Wgl.Load("wglGetSwapIntervalEXT") && Wgl.Load("wglSwapIntervalEXT");
}
base.LoadAll();
}
@ -240,7 +247,7 @@ namespace OpenTK.Platform.Windows
#endregion
#region --- IGLContextInternal Members ---
#region IGLContextInternal Members
#region IWindowInfo IGLContextInternal.Info
/*
@ -262,12 +269,15 @@ namespace OpenTK.Platform.Windows
#endregion
#region --- Private Methods ---
#region Private Methods
#region void SetGraphicsModePFD(GraphicsMode format, WinWindowInfo window)
#region SetGraphicsModePFD
// Note: there is no relevant ARB function.
void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window)
{
Debug.Write("Setting pixel format... ");
if (!mode.Index.HasValue)
throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
@ -281,20 +291,12 @@ namespace OpenTK.Platform.Windows
throw new GraphicsContextException(String.Format(
"Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error()));
}
#endregion
#region void SetGraphicsModeARB(GraphicsMode format, IWindowInfo window)
void SetGraphicsModeARB(GraphicsMode format, IWindowInfo window)
{
throw new NotImplementedException();
}
#endregion
#endregion
#region --- Internal Methods ---
#region Internal Methods
#region internal IntPtr DeviceContext
@ -302,16 +304,32 @@ namespace OpenTK.Platform.Windows
{
get
{
return Wgl.GetCurrentDC();
return Wgl.Imports.GetCurrentDC();
}
}
#endregion
static internal void Init()
{
lock (SyncRoot)
{
// Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl.
if (opengl32Handle == IntPtr.Zero)
{
opengl32Handle = Functions.LoadLibrary(opengl32Name);
if (opengl32Handle == IntPtr.Zero)
throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}",
opengl32Name, Marshal.GetLastWin32Error()));
Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle));
}
}
}
#endregion
#region --- Overrides ---
#region Overrides
/// <summary>Returns a System.String describing this OpenGL context.</summary>
/// <returns>A System.String describing this OpenGL context.</returns>
@ -322,7 +340,7 @@ namespace OpenTK.Platform.Windows
#endregion
#region --- IDisposable Members ---
#region IDisposable Members
public override void Dispose()
{