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> /// </summary>
internal sealed class WinGLContext : DesktopGraphicsContext 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 IntPtr opengl32Handle;
static bool wgl_loaded; static bool wgl_loaded;
@ -39,18 +40,7 @@ namespace OpenTK.Platform.Windows
static WinGLContext() static WinGLContext()
{ {
lock (SyncRoot) Init();
{
// 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));
}
}
} }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext,
@ -68,58 +58,60 @@ namespace OpenTK.Platform.Windows
Mode = format; Mode = format;
Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle); Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.WindowHandle,
Debug.Write("Setting pixel format... "); System.Threading.Thread.CurrentThread.ManagedThreadId);
this.SetGraphicsModePFD(format, (WinWindowInfo)window); this.SetGraphicsModePFD(format, (WinWindowInfo)window);
if (!wgl_loaded) lock (LoadLock)
{ {
// We need to create a temp context in order to load wgl extensions (e.g. for multisampling or GL3). if (!wgl_loaded)
// 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_loaded = true;
}
if (Wgl.Delegates.wglCreateContextAttribsARB != null)
{
try
{ {
Debug.Write("Using WGL_ARB_create_context... "); // 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().
List<int> attributes = new List<int>(); Debug.Print("Creating temporary context for wgl extensions.");
attributes.Add((int)ArbCreateContext.MajorVersion); ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
attributes.Add(major); Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle);
attributes.Add((int)ArbCreateContext.MinorVersion); Wgl.LoadAll();
attributes.Add(minor); Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
if (flags != 0) Wgl.Imports.DeleteContext(temp_context.Handle);
{ wgl_loaded = true;
attributes.Add((int)ArbCreateContext.Flags); }
#warning "This is not entirely correct: Embedded is not a valid flag! We need to add a GetARBContextFlags(GraphicsContextFlags) method."
attributes.Add((int)flags); if (Wgl.Delegates.wglCreateContextAttribsARB != null)
} {
// According to the docs, " <attribList> specifies a list of attributes for the context. try
// The list consists of a sequence of <name,value> pairs terminated by the {
// value 0. [...]" Debug.Write("Using WGL_ARB_create_context... ");
// Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case).
attributes.Add(0); List<int> attributes = new List<int>();
attributes.Add(0); attributes.Add((int)ArbCreateContext.MajorVersion);
attributes.Add(major);
Handle = new ContextHandle( attributes.Add((int)ArbCreateContext.MinorVersion);
Wgl.Arb.CreateContextAttribs( attributes.Add(minor);
window.DeviceContext, if (flags != 0)
sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, {
attributes.ToArray())); attributes.Add((int)ArbCreateContext.Flags);
if (Handle == ContextHandle.Zero) #warning "This is not entirely correct: Embedded is not a valid flag! We need to add a GetARBContextFlags(GraphicsContextFlags) method."
Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); attributes.Add((int)flags);
}
// 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());
}
catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); }
catch (NullReferenceException e) { Debug.Print(e.ToString()); }
} }
catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); }
catch (NullReferenceException e) { Debug.Print(e.ToString()); }
} }
if (Handle == ContextHandle.Zero) if (Handle == ContextHandle.Zero)
@ -140,7 +132,7 @@ namespace OpenTK.Platform.Windows
if (sharedContext != null) if (sharedContext != null)
{ {
Marshal.GetLastWin32Error(); 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); bool result = Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle);
Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error()); Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error());
} }
@ -160,13 +152,13 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
#region --- IGraphicsContext Members --- #region IGraphicsContext Members
#region SwapBuffers #region SwapBuffers
public override void SwapBuffers() public override void SwapBuffers()
{ {
if (!Functions.SwapBuffers(Wgl.GetCurrentDC())) if (!Functions.SwapBuffers(DeviceContext))
throw new GraphicsContextException(String.Format( throw new GraphicsContextException(String.Format(
"Failed to swap buffers for context {0} current. Error: {1}", this, Marshal.GetLastWin32Error())); "Failed to swap buffers for context {0} current. Error: {1}", this, Marshal.GetLastWin32Error()));
} }
@ -177,30 +169,36 @@ namespace OpenTK.Platform.Windows
public override void MakeCurrent(IWindowInfo window) public override void MakeCurrent(IWindowInfo window)
{ {
bool success; lock (SyncRoot)
lock (LoadLock)
if (window != null)
{ {
if (((WinWindowInfo)window).WindowHandle == IntPtr.Zero) bool success;
throw new ArgumentException("window", "Must point to a valid window.");
success = Wgl.Imports.MakeCurrent(((WinWindowInfo)window).DeviceContext, Handle.Handle); if (window != null)
{
if (((WinWindowInfo)window).WindowHandle == IntPtr.Zero)
throw new ArgumentException("window", "Must point to a valid window.");
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()));
} }
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 #endregion
#region IsCurrent #region IsCurrent
public override bool IsCurrent public override bool IsCurrent
{ {
get { return Wgl.GetCurrentContext() == Handle.Handle; } get { return Wgl.Imports.GetCurrentContext() == Handle.Handle; }
} }
#endregion #endregion
@ -214,12 +212,18 @@ namespace OpenTK.Platform.Windows
{ {
get get
{ {
return vsync_supported && Wgl.Ext.GetSwapInterval() != 0; lock (LoadLock)
{
return vsync_supported && Wgl.Ext.GetSwapInterval() != 0;
}
} }
set set
{ {
if (vsync_supported) lock (LoadLock)
Wgl.Ext.SwapInterval(value ? 1 : 0); {
if (vsync_supported)
Wgl.Ext.SwapInterval(value ? 1 : 0);
}
} }
} }
@ -229,9 +233,12 @@ namespace OpenTK.Platform.Windows
public override void LoadAll() public override void LoadAll()
{ {
Wgl.LoadAll(); lock (LoadLock)
vsync_supported = Wgl.Arb.SupportsExtension(this, "WGL_EXT_swap_control") && {
Wgl.Load("wglGetSwapIntervalEXT") && Wgl.Load("wglSwapIntervalEXT"); Wgl.LoadAll();
vsync_supported = Wgl.Arb.SupportsExtension(this, "WGL_EXT_swap_control") &&
Wgl.Load("wglGetSwapIntervalEXT") && Wgl.Load("wglSwapIntervalEXT");
}
base.LoadAll(); base.LoadAll();
} }
@ -240,7 +247,7 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
#region --- IGLContextInternal Members --- #region IGLContextInternal Members
#region IWindowInfo IGLContextInternal.Info #region IWindowInfo IGLContextInternal.Info
/* /*
@ -262,12 +269,15 @@ namespace OpenTK.Platform.Windows
#endregion #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) void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window)
{ {
Debug.Write("Setting pixel format... ");
if (!mode.Index.HasValue) if (!mode.Index.HasValue)
throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
@ -281,20 +291,12 @@ namespace OpenTK.Platform.Windows
throw new GraphicsContextException(String.Format( throw new GraphicsContextException(String.Format(
"Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); "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
#endregion #endregion
#region --- Internal Methods --- #region Internal Methods
#region internal IntPtr DeviceContext #region internal IntPtr DeviceContext
@ -302,16 +304,32 @@ namespace OpenTK.Platform.Windows
{ {
get get
{ {
return Wgl.GetCurrentDC(); return Wgl.Imports.GetCurrentDC();
} }
} }
#endregion #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 #endregion
#region --- Overrides --- #region Overrides
/// <summary>Returns a System.String describing this OpenGL context.</summary> /// <summary>Returns a System.String describing this OpenGL context.</summary>
/// <returns>A System.String describing this OpenGL context.</returns> /// <returns>A System.String describing this OpenGL context.</returns>
@ -322,7 +340,7 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
#region --- IDisposable Members --- #region IDisposable Members
public override void Dispose() public override void Dispose()
{ {