From 5379deaf63ade3545d9070eb6fcdfe9cdad74a37 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 21 Jan 2014 09:00:25 +0100 Subject: [PATCH] [Win] Implemented calli-based interop for WGL --- .../OpenTK/Platform/Windows/Bindings/Wgl.cs | 334 +++++++----------- Source/OpenTK/Platform/Windows/WglHelper.cs | 192 ++++------ 2 files changed, 202 insertions(+), 324 deletions(-) diff --git a/Source/OpenTK/Platform/Windows/Bindings/Wgl.cs b/Source/OpenTK/Platform/Windows/Bindings/Wgl.cs index e1bd92ad..2af62cc5 100644 --- a/Source/OpenTK/Platform/Windows/Bindings/Wgl.cs +++ b/Source/OpenTK/Platform/Windows/Bindings/Wgl.cs @@ -34,8 +34,34 @@ namespace OpenTK.Platform.Windows #pragma warning disable 3019 #pragma warning disable 1591 - static partial class Wgl + partial class Wgl { + static Wgl() + { + EntryPointNames = new string[] + { + "wglCreateContextAttribsARB", + "wglGetExtensionsStringARB", + "wglGetPixelFormatAttribivARB", + "wglGetPixelFormatAttribfvARB", + "wglChoosePixelFormatARB", + "wglMakeContextCurrentARB", + "wglGetCurrentReadDCARB", + "wglCreatePbufferARB", + "wglGetPbufferDCARB", + "wglReleasePbufferDCARB", + "wglDestroyPbufferARB", + "wglQueryPbufferARB", + "wglBindTexImageARB", + "wglReleaseTexImageARB", + "wglSetPbufferAttribARB", + "wglGetExtensionsStringEXT", + "wglSwapIntervalEXT", + "wglGetSwapIntervalEXT", + }; + EntryPoints = new IntPtr[EntryPointNames.Length]; + } + [SuppressUnmanagedCodeSecurity] [DllImport(Wgl.Library, EntryPoint = "wglCreateContext", ExactSpelling = true, SetLastError = true)] internal extern static IntPtr CreateContext(IntPtr hDc); @@ -78,330 +104,232 @@ namespace OpenTK.Platform.Windows public static partial class Arb { - [CLSCompliant(false)] - unsafe public static - IntPtr CreateContextAttribs(IntPtr hDC, IntPtr hShareContext, int* attribList) - { - return Delegates.wglCreateContextAttribsARB((IntPtr)hDC, (IntPtr)hShareContext, (int*)attribList); - } - - public static - IntPtr CreateContextAttribs(IntPtr hDC, IntPtr hShareContext, ref int attribList) - { - unsafe - { - fixed (int* attribList_ptr = &attribList) - { - return Delegates.wglCreateContextAttribsARB((IntPtr)hDC, (IntPtr)hShareContext, (int*)attribList_ptr); - } - } - } - + [AutoGenerated(EntryPoint = "wglCreateContextAttribsARB")] public static IntPtr CreateContextAttribs(IntPtr hDC, IntPtr hShareContext, int[] attribList) { - unsafe - { - fixed (int* attribList_ptr = attribList) - { - return Delegates.wglCreateContextAttribsARB((IntPtr)hDC, (IntPtr)hShareContext, (int*)attribList_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetExtensionsStringARB")] public static string GetExtensionsString(IntPtr hdc) { - unsafe - { - return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Delegates.wglGetExtensionsStringARB((IntPtr)hdc)); - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetPixelFormatAttribivARB")] public static Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, int[] piAttributes, [Out] int[] piValues) { - unsafe - { - fixed (int* piAttributes_ptr = piAttributes) - fixed (int* piValues_ptr = piValues) - { - return Delegates.wglGetPixelFormatAttribivARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes_ptr, (int*)piValues_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetPixelFormatAttribivARB")] public static Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, ref int piAttributes, [Out] out int piValues) { - unsafe - { - fixed (int* piAttributes_ptr = &piAttributes) - fixed (int* piValues_ptr = &piValues) - { - Boolean retval = Delegates.wglGetPixelFormatAttribivARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes_ptr, (int*)piValues_ptr); - piValues = *piValues_ptr; - return retval; - } - } - } - - public static - unsafe Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, int* piAttributes, [Out] int* piValues) - { - return Delegates.wglGetPixelFormatAttribivARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes, (int*)piValues); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetPixelFormatAttribfvARB")] [System.CLSCompliant(false)] public static - Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, UInt32 nAttributes, int[] piAttributes, [Out] Single[] pfValues) + Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, int nAttributes, int[] piAttributes, [Out] Single[] pfValues) { - unsafe - { - fixed (int* piAttributes_ptr = piAttributes) - fixed (Single* pfValues_ptr = pfValues) - { - return Delegates.wglGetPixelFormatAttribfvARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes_ptr, (Single*)pfValues_ptr); - } - } - } - - public static - Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, int[] piAttributes, [Out] Single[] pfValues) - { - unsafe - { - fixed (int* piAttributes_ptr = piAttributes) - fixed (Single* pfValues_ptr = pfValues) - { - return Delegates.wglGetPixelFormatAttribfvARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes_ptr, (Single*)pfValues_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetPixelFormatAttribfvARB")] public static Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, ref int piAttributes, [Out] out Single pfValues) { - unsafe - { - fixed (int* piAttributes_ptr = &piAttributes) - fixed (Single* pfValues_ptr = &pfValues) - { - Boolean retval = Delegates.wglGetPixelFormatAttribfvARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes_ptr, (Single*)pfValues_ptr); - pfValues = *pfValues_ptr; - return retval; - } - } - } - - [System.CLSCompliant(false)] - public static - unsafe Boolean GetPixelFormatAttrib(IntPtr hdc, int iPixelFormat, int iLayerPlane, Int32 nAttributes, int* piAttributes, [Out] Single* pfValues) - { - return Delegates.wglGetPixelFormatAttribfvARB((IntPtr)hdc, (int)iPixelFormat, (int)iLayerPlane, (UInt32)nAttributes, (int*)piAttributes, (Single*)pfValues); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglChoosePixelFormatARB")] public static Boolean ChoosePixelFormat(IntPtr hdc, int[] piAttribIList, Single[] pfAttribFList, Int32 nMaxFormats, [Out] int[] piFormats, out int nNumFormats) { - unsafe - { - fixed (int* piAttribIList_ptr = piAttribIList) - fixed (Single* pfAttribFList_ptr = pfAttribFList) - fixed (int* piFormats_ptr = piFormats) - fixed (Int32* nNumFormats_ptr = &nNumFormats) - { - return Delegates.wglChoosePixelFormatARB((IntPtr)hdc, (int*)piAttribIList_ptr, (Single*)pfAttribFList_ptr, (UInt32)nMaxFormats, (int*)piFormats_ptr, (UInt32*)nNumFormats_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglChoosePixelFormatARB")] public static Boolean ChoosePixelFormat(IntPtr hdc, ref int piAttribIList, ref Single pfAttribFList, Int32 nMaxFormats, [Out] out int piFormats, [Out] out Int32 nNumFormats) { - unsafe - { - fixed (int* piAttribIList_ptr = &piAttribIList) - fixed (Single* pfAttribFList_ptr = &pfAttribFList) - fixed (int* piFormats_ptr = &piFormats) - fixed (Int32* nNumFormats_ptr = &nNumFormats) - { - Boolean retval = Delegates.wglChoosePixelFormatARB((IntPtr)hdc, (int*)piAttribIList_ptr, (Single*)pfAttribFList_ptr, (UInt32)nMaxFormats, (int*)piFormats_ptr, (UInt32*)nNumFormats_ptr); - piFormats = *piFormats_ptr; - nNumFormats = *nNumFormats_ptr; - return retval; - } - } - } - - [System.CLSCompliant(false)] - public static - unsafe Boolean ChoosePixelFormat(IntPtr hdc, int* piAttribIList, Single* pfAttribFList, Int32 nMaxFormats, [Out] int* piFormats, [Out] Int32* nNumFormats) - { - return Delegates.wglChoosePixelFormatARB((IntPtr)hdc, (int*)piAttribIList, (Single*)pfAttribFList, (UInt32)nMaxFormats, (int*)piFormats, (UInt32*)nNumFormats); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglMakeContextCurrentARB")] public static Boolean MakeContextCurrent(IntPtr hDrawDC, IntPtr hReadDC, IntPtr hglrc) { - return Delegates.wglMakeContextCurrentARB((IntPtr)hDrawDC, (IntPtr)hReadDC, (IntPtr)hglrc); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetCurrentReadDCARB")] public static IntPtr GetCurrentReadDC() { - return Delegates.wglGetCurrentReadDCARB(); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglCreatePbufferARB")] public static IntPtr CreatePbuffer(IntPtr hDC, int iPixelFormat, int iWidth, int iHeight, int[] piAttribList) { - unsafe - { - fixed (int* piAttribList_ptr = piAttribList) - { - return Delegates.wglCreatePbufferARB((IntPtr)hDC, (int)iPixelFormat, (int)iWidth, (int)iHeight, (int*)piAttribList_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglCreatePbufferARB")] public static IntPtr CreatePbuffer(IntPtr hDC, int iPixelFormat, int iWidth, int iHeight, ref int piAttribList) { - unsafe - { - fixed (int* piAttribList_ptr = &piAttribList) - { - return Delegates.wglCreatePbufferARB((IntPtr)hDC, (int)iPixelFormat, (int)iWidth, (int)iHeight, (int*)piAttribList_ptr); - } - } - } - - [System.CLSCompliant(false)] - public static - unsafe IntPtr CreatePbuffer(IntPtr hDC, int iPixelFormat, int iWidth, int iHeight, int* piAttribList) - { - return Delegates.wglCreatePbufferARB((IntPtr)hDC, (int)iPixelFormat, (int)iWidth, (int)iHeight, (int*)piAttribList); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetPbufferDCARB")] public static IntPtr GetPbufferDC(IntPtr hPbuffer) { - return Delegates.wglGetPbufferDCARB((IntPtr)hPbuffer); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglReleasePbufferDCARB")] public static int ReleasePbufferDC(IntPtr hPbuffer, IntPtr hDC) { - return Delegates.wglReleasePbufferDCARB((IntPtr)hPbuffer, (IntPtr)hDC); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglDestroyPbufferARB")] public static Boolean DestroyPbuffer(IntPtr hPbuffer) { - return Delegates.wglDestroyPbufferARB((IntPtr)hPbuffer); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglQueryPbufferARB")] public static Boolean QueryPbuffer(IntPtr hPbuffer, int iAttribute, [Out] int[] piValue) { - unsafe - { - fixed (int* piValue_ptr = piValue) - { - return Delegates.wglQueryPbufferARB((IntPtr)hPbuffer, (int)iAttribute, (int*)piValue_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglQueryPbufferARB")] public static Boolean QueryPbuffer(IntPtr hPbuffer, int iAttribute, [Out] out int piValue) { - unsafe - { - fixed (int* piValue_ptr = &piValue) - { - Boolean retval = Delegates.wglQueryPbufferARB((IntPtr)hPbuffer, (int)iAttribute, (int*)piValue_ptr); - piValue = *piValue_ptr; - return retval; - } - } - } - - [System.CLSCompliant(false)] - public static - unsafe Boolean QueryPbuffer(IntPtr hPbuffer, int iAttribute, [Out] int* piValue) - { - return Delegates.wglQueryPbufferARB((IntPtr)hPbuffer, (int)iAttribute, (int*)piValue); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglBindTexImageARB")] public static Boolean BindTexImage(IntPtr hPbuffer, int iBuffer) { - return Delegates.wglBindTexImageARB((IntPtr)hPbuffer, (int)iBuffer); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglReleaseTexImageARB")] public static Boolean ReleaseTexImage(IntPtr hPbuffer, int iBuffer) { - return Delegates.wglReleaseTexImageARB((IntPtr)hPbuffer, (int)iBuffer); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglSetPbufferAttribARB")] public static Boolean SetPbufferAttrib(IntPtr hPbuffer, int[] piAttribList) { - unsafe - { - fixed (int* piAttribList_ptr = piAttribList) - { - return Delegates.wglSetPbufferAttribARB((IntPtr)hPbuffer, (int*)piAttribList_ptr); - } - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglSetPbufferAttribARB")] public static Boolean SetPbufferAttrib(IntPtr hPbuffer, ref int piAttribList) { - unsafe - { - fixed (int* piAttribList_ptr = &piAttribList) - { - return Delegates.wglSetPbufferAttribARB((IntPtr)hPbuffer, (int*)piAttribList_ptr); - } - } - } - - [System.CLSCompliant(false)] - public static - unsafe Boolean SetPbufferAttrib(IntPtr hPbuffer, int* piAttribList) - { - return Delegates.wglSetPbufferAttribARB((IntPtr)hPbuffer, (int*)piAttribList); + throw new NotImplementedException(); } } public static partial class Ext { + [AutoGenerated(EntryPoint = "wglGetExtensionsStringEXT")] public static string GetExtensionsString() { - unsafe - { - return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Delegates.wglGetExtensionsStringEXT()); - } + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglSwapIntervalEXT")] public static Boolean SwapInterval(int interval) { - return Delegates.wglSwapIntervalEXT((int)interval); + throw new NotImplementedException(); } + [AutoGenerated(EntryPoint = "wglGetSwapIntervalEXT")] public static int GetSwapInterval() { - return Delegates.wglGetSwapIntervalEXT(); + throw new NotImplementedException(); } - } + + [Slot(0)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern IntPtr wglCreateContextAttribsARB(IntPtr hDC, IntPtr hShareContext, int* attribList); + [Slot(1)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern IntPtr wglGetExtensionsStringARB(IntPtr hdc); + [Slot(2)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern Boolean wglGetPixelFormatAttribivARB(IntPtr hdc, int iPixelFormat, int iLayerPlane, UInt32 nAttributes, int* piAttributes, [Out] int* piValues); + [Slot(3)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern Boolean wglGetPixelFormatAttribfvARB(IntPtr hdc, int iPixelFormat, int iLayerPlane, UInt32 nAttributes, int* piAttributes, [Out] Single* pfValues); + [Slot(4)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern Boolean wglChoosePixelFormatARB(IntPtr hdc, int* piAttribIList, Single* pfAttribFList, UInt32 nMaxFormats, [Out] int* piFormats, [Out] UInt32* nNumFormats); + [Slot(5)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern Boolean wglMakeContextCurrentARB(IntPtr hDrawDC, IntPtr hReadDC, IntPtr hglrc); + [Slot(6)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern IntPtr wglGetCurrentReadDCARB(); + [Slot(7)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern IntPtr wglCreatePbufferARB(IntPtr hDC, int iPixelFormat, int iWidth, int iHeight, int* piAttribList); + [Slot(8)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern IntPtr wglGetPbufferDCARB(IntPtr hPbuffer); + [Slot(9)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern int wglReleasePbufferDCARB(IntPtr hPbuffer, IntPtr hDC); + [Slot(10)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern Boolean wglDestroyPbufferARB(IntPtr hPbuffer); + [Slot(11)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern Boolean wglQueryPbufferARB(IntPtr hPbuffer, int iAttribute, [Out] int* piValue); + [Slot(12)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern Boolean wglBindTexImageARB(IntPtr hPbuffer, int iBuffer); + [Slot(13)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern Boolean wglReleaseTexImageARB(IntPtr hPbuffer, int iBuffer); + [Slot(14)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal unsafe static extern Boolean wglSetPbufferAttribARB(IntPtr hPbuffer, int* piAttribList); + [Slot(15)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern IntPtr wglGetExtensionsStringEXT(); + [Slot(16)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern Boolean wglSwapIntervalEXT(int interval); + [Slot(17)] + [DllImport(Library, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + internal static extern int wglGetSwapIntervalEXT(); } } diff --git a/Source/OpenTK/Platform/Windows/WglHelper.cs b/Source/OpenTK/Platform/Windows/WglHelper.cs index 0e863213..1f49dd62 100644 --- a/Source/OpenTK/Platform/Windows/WglHelper.cs +++ b/Source/OpenTK/Platform/Windows/WglHelper.cs @@ -11,119 +11,29 @@ using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Reflection; +using OpenTK.Graphics; namespace OpenTK.Platform.Windows { - internal partial class Wgl + internal partial class Wgl : GraphicsBindingsBase { - #region --- Constructors --- - - static Wgl() - { - assembly = Assembly.GetExecutingAssembly(); - wglClass = assembly.GetType("OpenTK.Platform.Windows.Wgl"); - delegatesClass = wglClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); - - //// Ensure core entry points are ready prior to accessing any method. - //// Resolves bug [#993]: "Possible bug in GraphicsContext.CreateDummyContext()" - //LoadAll(); - } - - #endregion - - #region --- Fields --- + static IntPtr[] EntryPoints; + static string[] EntryPointNames; internal const string Library = "OPENGL32.DLL"; readonly static Dictionary extensions = new Dictionary(); - private static Assembly assembly; - private static Type wglClass; - private static Type delegatesClass; + static readonly object sync = new object(); - private static bool rebuildExtensionList = true; - - static readonly object SyncRoot = new object(); - - #endregion - - #region static Delegate LoadDelegate(string name, Type signature) - - /// - /// Creates a System.Delegate that can be used to call an OpenGL function, core or extension. - /// - /// The name of the Wgl function (eg. "wglNewList") - /// The signature of the OpenGL function. - /// - /// A System.Delegate that can be used to call this OpenGL function, or null if the specified - /// function name did not correspond to an OpenGL function. - /// - static Delegate LoadDelegate(string name, Type signature) + public Wgl() { - Delegate d = GetExtensionDelegate(name, signature); - return d; + EntryPointsInstance = EntryPoints; + EntryPointNamesInstance = EntryPointNames; } - #endregion - - #region private static Delegate GetExtensionDelegate(string name, Type signature) - - /// - /// Creates a System.Delegate that can be used to call a dynamically exported OpenGL function. - /// - /// The name of the OpenGL function (eg. "glNewList") - /// The signature of the OpenGL function. - /// - /// A System.Delegate that can be used to call this OpenGL function or null - /// if the function is not available in the current OpenGL context. - /// - private static Delegate GetExtensionDelegate(string name, Type signature) - { - IntPtr address = GetProcAddress(name); - - if (address == IntPtr.Zero || - address == new IntPtr(1) || // Workaround for buggy nvidia drivers which return - address == new IntPtr(2)) // 1 or 2 instead of IntPtr.Zero for some extensions. - { - return null; - } - else - { - return Marshal.GetDelegateForFunctionPointer(address, signature); - } - } - - #endregion - - #region public static void LoadAll() - - /// - /// Loads all Wgl entry points, core and extensions. - /// - public static void LoadAll() - { - lock (SyncRoot) - { - OpenTK.Platform.Utilities.LoadExtensions(typeof(Wgl)); - } - } - - #endregion - - #region public static bool Load(string function) - - /// - /// Loads the given Wgl entry point. - /// - /// The name of the function to load. - /// - public static bool Load(string function) - { - return OpenTK.Platform.Utilities.TryLoadExtension(typeof(Wgl), function); - } - - #endregion + #region Public Members public static bool SupportsExtension(string name) { @@ -145,22 +55,15 @@ namespace OpenTK.Platform.Windows // static WGL/GL classes. Fortunately, this issue is extremely unlikely to arise in practice, as you'd // have to create one accelerated and one non-accelerated context in the same application, with the // non-accelerated context coming second. - Wgl.Delegates.GetExtensionsStringARB get_arb = Wgl.Delegates.wglGetExtensionsStringARB; - Wgl.Delegates.GetExtensionsStringEXT get_ext = Wgl.Delegates.wglGetExtensionsStringEXT; - IntPtr str_ptr = - get_arb != null ? get_arb(dc) : - get_ext != null ? get_ext() : - IntPtr.Zero; + bool get_arb = SupportsFunction("wglGetExtensionsStringARB"); + bool get_ext = SupportsFunction("wglGetExtensionsStringEXT"); + string str = + get_arb ? Arb.GetExtensionsString(dc) : + get_ext ? Ext.GetExtensionsString() : + String.Empty; - if (str_ptr != IntPtr.Zero) + if (!String.IsNullOrEmpty(str)) { - string str; - - unsafe - { - str = new string((sbyte*)str_ptr); - } - foreach (string ext in str.Split(' ')) { extensions.Add(ext, true); @@ -175,20 +78,67 @@ namespace OpenTK.Platform.Windows return false; } - #region public static partial class Arb - - /// Contains ARB extensions for WGL. - public static partial class Arb + /// + /// Checks whether an extension function is supported. + /// Do not use with core WGL functions, as this function + /// will incorrectly return false. + /// + /// The extension function to check (e.g. "wglGetExtensionsStringARB" + /// True if the extension function is supported; otherwise, false. + public static bool SupportsFunction(string name) { + int index = Array.IndexOf(EntryPointNames, name); + if (index >= 0) + { + return EntryPoints[index] != IntPtr.Zero; + } + return false; } - #endregion + #endregion - #region public static partial class Ext + #region Protected Members - /// Contains EXT extensions for WGL. - public static partial class Ext + protected override object SyncRoot { + get { return sync; } + } + + protected override IntPtr GetAddress(string function_string) + { + IntPtr address = Wgl.GetProcAddress(function_string); + if (!IsValid(address)) + { + address = Functions.GetProcAddress(WinFactory.OpenGLHandle, function_string); + } + return address; + } + + #endregion + + #region Private Members + + static bool IsValid(IntPtr address) + { + // See https://www.opengl.org/wiki/Load_OpenGL_Functions + long a = address.ToInt64(); + bool is_valid = (a < -1) || (a > 3); + return is_valid; + } + + #endregion + + #region Internal Members + + internal override void LoadEntryPoints() + { + if (Wgl.GetCurrentContext() != IntPtr.Zero) + { + for (int i = 0; i < EntryPointsInstance.Length; i++) + { + EntryPointsInstance[i] = GetAddress(EntryPointNamesInstance[i]); + } + } } #endregion