Opentk/Source/OpenTK/Platform/Windows/WglHelper.cs

190 lines
6.8 KiB
C#
Raw Normal View History

2007-08-20 14:25:48 +02:00
#region --- License ---
2007-08-20 12:46:37 +02:00
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*
* Date: 12/8/2007
2007-08-20 14:25:48 +02:00
* Time: 6:43 <EFBFBD><EFBFBD>
2007-08-20 12:46:37 +02:00
*/
#endregion
using System;
using System.Runtime.InteropServices;
using System.Reflection;
namespace OpenTK.Platform.Windows
{
2008-01-06 03:26:11 +01:00
internal partial class Wgl
2007-08-20 12:46:37 +02:00
{
#region --- Constructors ---
static Wgl()
{
assembly = Assembly.GetExecutingAssembly();
wglClass = assembly.GetType("OpenTK.Platform.Windows.Wgl");
delegatesClass = wglClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic);
importsClass = wglClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic);
//LoadAll();
}
#endregion
#region --- Fields ---
internal const string Library = "OPENGL32.DLL";
private static Assembly assembly;
private static Type wglClass;
private static Type delegatesClass;
private static Type importsClass;
private static bool rebuildExtensionList = true;
2007-08-20 12:46:37 +02:00
#endregion
#region static Delegate LoadDelegate(string name, Type signature)
2007-08-20 12:46:37 +02:00
/// <summary>
/// Creates a System.Delegate that can be used to call an OpenGL function, core or extension.
/// </summary>
/// <param name="name">The name of the Wgl function (eg. "wglNewList")</param>
/// <param name="signature">The signature of the OpenGL function.</param>
/// <returns>
/// 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.
/// </returns>
static Delegate LoadDelegate(string name, Type signature)
2007-08-20 12:46:37 +02:00
{
Delegate d;
string realName = name.StartsWith("wgl") ? name.Substring(3) : name;
if (importsClass.GetMethod(realName,
BindingFlags.NonPublic | BindingFlags.Static) != null)
2007-09-22 17:07:28 +02:00
d = GetExtensionDelegate(name, signature) ??
2007-08-20 12:46:37 +02:00
Delegate.CreateDelegate(signature, typeof(Imports), realName);
else
2007-09-22 17:07:28 +02:00
d = GetExtensionDelegate(name, signature);
2007-08-20 12:46:37 +02:00
return d;
}
#endregion
2007-09-22 17:07:28 +02:00
#region private static Delegate GetExtensionDelegate(string name, Type signature)
/// <summary>
/// Creates a System.Delegate that can be used to call a dynamically exported OpenGL function.
/// </summary>
/// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
/// <param name="signature">The signature of the OpenGL function.</param>
/// <returns>
/// 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.
/// </returns>
private static Delegate GetExtensionDelegate(string name, Type signature)
{
IntPtr address = Imports.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()
2007-08-20 12:46:37 +02:00
/// <summary>
/// Loads all Wgl entry points, core and extensions.
/// </summary>
public static void LoadAll()
{
OpenTK.Platform.Utilities.LoadExtensions(typeof(Wgl));
2007-08-20 12:46:37 +02:00
}
#endregion
#region public static bool Load(string function)
2007-08-20 12:46:37 +02:00
/// <summary>
/// Loads the given Wgl entry point.
/// </summary>
/// <param name="function">The name of the function to load.</param>
/// <returns></returns>
public static bool Load(string function)
{
return OpenTK.Platform.Utilities.TryLoadExtension(typeof(Wgl), function);
2007-08-20 12:46:37 +02:00
}
#endregion
2008-01-23 01:26:47 +01:00
#region public static partial class Arb
/// <summary>Contains ARB extensions for WGL.</summary>
2008-01-15 01:43:51 +01:00
public static partial class Arb
{
private static string[] extensions;
/// <summary>
/// Checks if a Wgl extension is supported by the given context.
/// </summary>
/// <param name="deviceContext">The device context.</param>
/// <param name="ext">The extension to check.</param>
/// <returns>True if the extension is supported by the given context, false otherwise</returns>
2008-01-23 01:19:42 +01:00
public static bool SupportsExtension(WinGLContext context, string ext)
{
if (Wgl.Delegates.wglGetExtensionsStringARB != null)
{
if (extensions == null || rebuildExtensionList)
{
2008-01-23 01:19:42 +01:00
extensions = Wgl.Arb.GetExtensionsString(context.Device).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Array.Sort(extensions);
rebuildExtensionList = false;
}
return Array.BinarySearch(extensions, ext) != -1;
}
return false;
}
2007-08-20 14:25:48 +02:00
}
2008-01-23 01:26:47 +01:00
#endregion
#region public static partial class Ext
/// <summary>Contains EXT extensions for WGL.</summary>
public static partial class Ext
{
private static string[] extensions;
/// <summary>
/// Checks if a Wgl extension is supported by the given context.
/// </summary>
/// <param name="ext">The extension to check.</param>
/// <returns>True if the extension is supported by the given context, false otherwise</returns>
2008-01-23 01:19:42 +01:00
public static bool SupportsExtension(string ext)
{
if (Wgl.Delegates.wglGetExtensionsStringEXT != null)
{
if (extensions == null || rebuildExtensionList)
{
extensions = Wgl.Ext.GetExtensionsString().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Array.Sort(extensions);
rebuildExtensionList = false;
}
return Array.BinarySearch(extensions, ext) != -1;
}
return false;
}
}
2008-01-23 01:26:47 +01:00
#endregion
}
2007-08-20 12:46:37 +02:00
}