From d3a56a15efaed9d073c30fa36fd5418efd63ef46 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Wed, 7 Oct 2009 10:44:45 +0000 Subject: [PATCH] * Graphics/ES10/Helper.cs: * Graphics/ES11/Helper.cs: * Graphics/ES20/Helper.cs: * Graphics/BindingsBase.cs: * Platform/X11/GlxHelper.cs: * Graphics/OpenGL/GLHelper.cs: * Platform/X11/X11GLContext.cs: Modified BindingsBase to define abstract GetAddress method (reason: removes OpenTK.Graphics-specific code from BindingsBase and allows it to be used in different bindings). Implemented GraphicsBindingsBase and modified the OpenGL and OpenGL|ES bindings to use this. Modified the GLX bindings to inherit from BindingsBase (reason: reduces code duplication for extension loading). --- Source/OpenTK/Graphics/BindingsBase.cs | 34 ++++++++++++++++------ Source/OpenTK/Graphics/ES10/Helper.cs | 2 +- Source/OpenTK/Graphics/ES11/Helper.cs | 2 +- Source/OpenTK/Graphics/ES20/Helper.cs | 2 +- Source/OpenTK/Graphics/OpenGL/GLHelper.cs | 2 +- Source/OpenTK/Platform/X11/GlxHelper.cs | 9 +++++- Source/OpenTK/Platform/X11/X11GLContext.cs | 4 +-- 7 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Source/OpenTK/Graphics/BindingsBase.cs b/Source/OpenTK/Graphics/BindingsBase.cs index a9f97b7d..9d166f8e 100644 --- a/Source/OpenTK/Graphics/BindingsBase.cs +++ b/Source/OpenTK/Graphics/BindingsBase.cs @@ -32,10 +32,10 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Diagnostics; -namespace OpenTK.Graphics +namespace OpenTK { /// - /// Provides a common foundation for all flat API classes. + /// Provides a common foundation for all flat API bindings and implements the extension loading interface. /// public abstract class BindingsBase { @@ -70,11 +70,14 @@ namespace OpenTK.Graphics DelegatesClass = this.GetType().GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); CoreClass = this.GetType().GetNestedType("Core", BindingFlags.Static | BindingFlags.NonPublic); - MethodInfo[] methods = CoreClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic); - CoreFunctionMap = new SortedList(methods.Length); - foreach (MethodInfo m in methods) + if (CoreClass != null) { - CoreFunctionMap.Add(m.Name, m); + MethodInfo[] methods = CoreClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic); + CoreFunctionMap = new SortedList(methods.Length); // Avoid resizing + foreach (MethodInfo m in methods) + { + CoreFunctionMap.Add(m.Name, m); + } } } @@ -91,6 +94,8 @@ namespace OpenTK.Graphics set { rebuildExtensionList = value; } } + protected abstract IntPtr GetAddress(string funcname); + #endregion #region Internal Members @@ -176,10 +181,10 @@ namespace OpenTK.Graphics #region GetExtensionDelegate // Creates a System.Delegate that can be used to call a dynamically exported OpenGL function. - internal static Delegate GetExtensionDelegate(string name, Type signature) + internal Delegate GetExtensionDelegate(string name, Type signature) { - IntPtr address = (GraphicsContext.CurrentContext as IGraphicsContextInternal).GetAddress(name); - + IntPtr address = GetAddress(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. @@ -197,3 +202,14 @@ namespace OpenTK.Graphics #endregion } } + +namespace OpenTK.Graphics +{ + public class GraphicsBindingsBase : BindingsBase + { + protected override IntPtr GetAddress(string funcname) + { + return (GraphicsContext.CurrentContext as IGraphicsContextInternal).GetAddress(funcname); + } + } +} diff --git a/Source/OpenTK/Graphics/ES10/Helper.cs b/Source/OpenTK/Graphics/ES10/Helper.cs index a59a983a..da2d4947 100644 --- a/Source/OpenTK/Graphics/ES10/Helper.cs +++ b/Source/OpenTK/Graphics/ES10/Helper.cs @@ -7,7 +7,7 @@ namespace OpenTK.Graphics.ES10 /// /// Provides access to OpenGL ES 1.0 methods. /// - public sealed partial class GL : BindingsBase + public sealed partial class GL : GraphicsBindingsBase { const string Library = "libGLES.dll"; } diff --git a/Source/OpenTK/Graphics/ES11/Helper.cs b/Source/OpenTK/Graphics/ES11/Helper.cs index f02ad274..eab6d123 100644 --- a/Source/OpenTK/Graphics/ES11/Helper.cs +++ b/Source/OpenTK/Graphics/ES11/Helper.cs @@ -9,7 +9,7 @@ namespace OpenTK.Graphics.ES11 /// /// Provides access to OpenGL ES 1.1 methods. /// - public sealed partial class GL : BindingsBase + public sealed partial class GL : GraphicsBindingsBase { const string Library = "libGLES.dll"; } diff --git a/Source/OpenTK/Graphics/ES20/Helper.cs b/Source/OpenTK/Graphics/ES20/Helper.cs index 6b91813e..7c62aac9 100644 --- a/Source/OpenTK/Graphics/ES20/Helper.cs +++ b/Source/OpenTK/Graphics/ES20/Helper.cs @@ -7,7 +7,7 @@ namespace OpenTK.Graphics.ES20 /// /// Provides access to OpenGL ES 2.0 methods. /// - public sealed partial class GL : BindingsBase + public sealed partial class GL : GraphicsBindingsBase { const string Library = "libGLESv2.dll"; } diff --git a/Source/OpenTK/Graphics/OpenGL/GLHelper.cs b/Source/OpenTK/Graphics/OpenGL/GLHelper.cs index a7a8ecb6..5fdc8ccb 100644 --- a/Source/OpenTK/Graphics/OpenGL/GLHelper.cs +++ b/Source/OpenTK/Graphics/OpenGL/GLHelper.cs @@ -46,7 +46,7 @@ namespace OpenTK.Graphics.OpenGL /// /// /// - public sealed partial class GL : BindingsBase + public sealed partial class GL : GraphicsBindingsBase { #region --- Fields --- diff --git a/Source/OpenTK/Platform/X11/GlxHelper.cs b/Source/OpenTK/Platform/X11/GlxHelper.cs index b2de144d..2094509b 100644 --- a/Source/OpenTK/Platform/X11/GlxHelper.cs +++ b/Source/OpenTK/Platform/X11/GlxHelper.cs @@ -15,13 +15,19 @@ using OpenTK.Graphics; namespace OpenTK.Platform.X11 { - static partial class Glx + partial class Glx : BindingsBase { const string Library = "libGL.so.1"; // Disable BeforeFieldInit optimization. static Glx() { } + protected override IntPtr GetAddress (string funcname) + { + return Glx.GetProcAddress(funcname); + } + +#if false #region static Delegate LoadDelegate(string name, Type signature) /// @@ -83,5 +89,6 @@ namespace OpenTK.Platform.X11 } #endregion +#endif } } diff --git a/Source/OpenTK/Platform/X11/X11GLContext.cs b/Source/OpenTK/Platform/X11/X11GLContext.cs index 714b6de4..7c3fec27 100644 --- a/Source/OpenTK/Platform/X11/X11GLContext.cs +++ b/Source/OpenTK/Platform/X11/X11GLContext.cs @@ -59,7 +59,7 @@ namespace OpenTK.Platform.X11 if (ctx != IntPtr.Zero) { - Glx.LoadAll(); + new Glx().LoadAll(); Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero); //Glx.DestroyContext(currentWindow.Display, ctx); glx_loaded = true; @@ -271,7 +271,7 @@ namespace OpenTK.Platform.X11 public override void LoadAll() { new OpenTK.Graphics.OpenGL.GL().LoadAll(); - Glx.LoadAll(); + new Glx().LoadAll(); vsync_supported = this.GetAddress("glXSwapIntervalSGI") != IntPtr.Zero; Debug.Print("Context supports vsync: {0}.", vsync_supported); }