Added SupportsFunction methods. Corrected SupportsExtension methods. Added locking.

This commit is contained in:
the_fiddler 2007-11-06 13:33:56 +00:00
parent 020a31bc81
commit b3258229bb

View file

@ -68,6 +68,9 @@ namespace OpenTK.OpenGL
/// </remarks> /// </remarks>
public static partial class GL public static partial class GL
{ {
static StringBuilder sb = new StringBuilder();
static object gl_lock = new object();
static GL() static GL()
{ {
assembly = Assembly.GetExecutingAssembly();//Assembly.Load("OpenTK.OpenGL"); assembly = Assembly.GetExecutingAssembly();//Assembly.Load("OpenTK.OpenGL");
@ -117,19 +120,18 @@ namespace OpenTK.OpenGL
public static bool SupportsExtension(string name) public static bool SupportsExtension(string name)
{ {
if (rebuildExtensionList) if (rebuildExtensionList)
{
BuildExtensionList(); BuildExtensionList();
}
// Search the cache for the string. Note that the cache substitutes lock (gl_lock)
// strings "1.0" to "2.1" with "GL_VERSION_1_0" to "GL_VERSION_2_1"
if (AvailableExtensions.ContainsKey(name))
{ {
//return AvailableExtensions[name]; sb.Remove(0, sb.Length);
return true; if (!name.StartsWith("GL_"))
} sb.Append("gl_");
sb.Append(name.ToLower());
return false; // Search the cache for the string.
return AvailableExtensions.ContainsKey(sb.ToString());
}
} }
#endregion #endregion
@ -153,53 +155,57 @@ namespace OpenTK.OpenGL
if (version.StartsWith("1.2")) if (version.StartsWith("1.2"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("gl_version_1_2", true);
} }
else if (version.StartsWith("1.3")) else if (version.StartsWith("1.3"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("VERSION_1_3", true); AvailableExtensions.Add("gl_version_1_2", true);
AvailableExtensions.Add("gl_version_1_3", true);
} }
else if (version.StartsWith("1.4")) else if (version.StartsWith("1.4"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("VERSION_1_3", true); AvailableExtensions.Add("gl_version_1_2", true);
AvailableExtensions.Add("VERSION_1_4", true); AvailableExtensions.Add("gl_version_1_3", true);
AvailableExtensions.Add("gl_version_1_4", true);
} }
else if (version.StartsWith("1.5")) else if (version.StartsWith("1.5"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("VERSION_1_3", true); AvailableExtensions.Add("gl_version_1_2", true);
AvailableExtensions.Add("VERSION_1_4", true); AvailableExtensions.Add("gl_version_1_3", true);
AvailableExtensions.Add("VERSION_1_5", true); AvailableExtensions.Add("gl_version_1_4", true);
AvailableExtensions.Add("gl_version_1_5", true);
} }
else if (version.StartsWith("2.0")) else if (version.StartsWith("2.0"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("VERSION_1_3", true); AvailableExtensions.Add("gl_version_1_2", true);
AvailableExtensions.Add("VERSION_1_4", true); AvailableExtensions.Add("gl_version_1_3", true);
AvailableExtensions.Add("VERSION_1_5", true); AvailableExtensions.Add("gl_version_1_4", true);
AvailableExtensions.Add("VERSION_2_0", true); AvailableExtensions.Add("gl_version_1_5", true);
AvailableExtensions.Add("gl_version_2_0", true);
} }
else if (version.StartsWith("2.1")) else if (version.StartsWith("2.1"))
{ {
AvailableExtensions.Add("VERSION_1_2", true); AvailableExtensions.Add("gl_version_1_1", true);
AvailableExtensions.Add("VERSION_1_3", true); AvailableExtensions.Add("gl_version_1_2", true);
AvailableExtensions.Add("VERSION_1_4", true); AvailableExtensions.Add("gl_version_1_3", true);
AvailableExtensions.Add("VERSION_1_5", true); AvailableExtensions.Add("gl_version_1_4", true);
AvailableExtensions.Add("VERSION_2_0", true); AvailableExtensions.Add("gl_version_1_5", true);
AvailableExtensions.Add("VERSION_2_1", true); AvailableExtensions.Add("gl_version_2_0", true);
AvailableExtensions.Add("gl_version_2_1", true);
} }
string extension_string = GL.GetString(Enums.StringName.Extensions); string extension_string = GL.GetString(Enums.StringName.Extensions);
if (String.IsNullOrEmpty(extension_string)) if (String.IsNullOrEmpty(extension_string))
return; // no extensions are available return; // no extensions are available
string[] extensions = extension_string.Split(' '); string[] extensions = extension_string.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string ext in extensions) foreach (string ext in extensions)
{ AvailableExtensions.Add(ext.ToLower(), true);
AvailableExtensions.Add(ext, true);
}
rebuildExtensionList = false; rebuildExtensionList = false;
} }
@ -329,6 +335,103 @@ namespace OpenTK.OpenGL
#endregion #endregion
#region public static bool SupportsFunction(string function)
/// <summary>
/// Checks if a given OpenGL function is supported by the current context
/// </summary>
/// <param name="function">The name of the OpenGL function (i.e. glShaderSource)</param>
/// <returns>True if the function is supported, false otherwise</returns>
public static bool SupportsFunction(string function)
{
lock (gl_lock)
{
if (function == null)
throw new ArgumentNullException("function");
sb.Remove(0, sb.Length);
if (!function.StartsWith("gl"))
sb.Append("gl");
sb.Append(function);
FieldInfo f = delegatesClass.GetField(sb.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
if (f == null)
return false;
return f.GetValue(null) != null;
}
}
#endregion
#region public static bool SupportsFunction(string function, string extension)
/// <summary>
/// Checks if a given OpenGL function is supported by the current context
/// </summary>
/// <param name="function">The name of the OpenGL function (e.g. glShaderSource)</param>
/// <param name="extension">The name of the extension catagory (e.g. ARB, EXT, ATI, ...)</param>
/// <returns>True if the function is supported, false otherwise</returns>
public static bool SupportsFunction(string function, string extension)
{
lock (gl_lock)
{
if (function == null)
throw new ArgumentNullException("function");
if (extension == null)
throw new ArgumentNullException("extension");
sb.Remove(0, sb.Length);
if (!function.StartsWith("gl"))
sb.Append("gl");
sb.Append(function);
if (!function.EndsWith(extension))
sb.Append(extension);
FieldInfo f = delegatesClass.GetField(sb.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
if (f == null)
return false;
return f.GetValue(null) != null;
}
}
#endregion
#region public static bool SupportsFunction(Type function)
/// <summary>
/// Checks if a given OpenGL function is supported by the current context
/// </summary>
/// <param name="function">The name of the OpenGL function (e.g. glShaderSource)</param>
/// <param name="extension">The name of the extension catagory (e.g. ARB, EXT, ATI, ...)</param>
/// <returns>True if the function is supported, false otherwise</returns>
public static bool SupportsFunction(MethodInfo method)
{
lock (gl_lock)
{
/*
if (function == null)
throw new ArgumentNullException("function");
sb.Remove(0, sb.Length);
if (!function.Name.StartsWith("gl"))
sb.Append("gl");
sb.Append(function);
//if (!function.EndsWith(extension))
// sb.Append(extension);
FieldInfo f = delegatesClass.GetField(sb.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
if (f == null)
return false;
return f.GetValue(null) != null;
*/
return false;
}
}
#endregion
#region --- GetProcAddress --- #region --- GetProcAddress ---
private static IGetProcAddress getProcAddress; private static IGetProcAddress getProcAddress;