public virtual void Initialize(IGetProcAddress GetProcAddress = null) { if (GetProcAddress == null) GetProcAddress = sGetProcAddress; var methods = pDelegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); //if (methods == null) // throw new InvalidOperationException("No method fields found in class"); foreach (var f in methods) { var ptr = GetProcAddress.GetProcAddress(f.Name); if (ptr != IntPtr.Zero) { var d = Marshal.GetDelegateForFunctionPointer(ptr, f.FieldType); f.SetValue(null, d); } #if DEBUG else { Debug.WriteLine(string.Format( "{0} not found!", f.Name), "OpenGL Loader"); } #endif } }
public virtual void Initialize(IGetProcAddress GetProcAddress = null) { if (GetProcAddress == null) { GetProcAddress = sGetProcAddress; } var methods = pDelegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); //if (methods == null) // throw new InvalidOperationException("No method fields found in class"); foreach (var f in methods) { var ptr = GetProcAddress.GetProcAddress(f.Name); if (ptr != IntPtr.Zero) { var d = Marshal.GetDelegateForFunctionPointer(ptr, f.FieldType); f.SetValue(null, d); } #if DEBUG else { Debug.WriteLine(string.Format( "{0} not found!", f.Name), "OpenGL Loader"); } #endif } }
/// <summary> /// Query the version of the current OpenGL context. /// </summary> /// <returns> /// It returns the <see cref="KhronosVersion"/> specifying teh actual version of <paramref name="ctx"/>. /// </returns> internal static KhronosVersion QueryContextVersion() { IntPtr ctx = GetCurrentContext(); if (ctx == null) { throw new InvalidOperationException("no current context"); } // Load minimal Gl functions for querying information IGetProcAddress getProcAddress = GetProcAddress.GetProcAddressGL; if (Egl.IsRequired == false) { Gl.BindAPIFunction(Gl.Version_100, "glGetError", getProcAddress); Gl.BindAPIFunction(Gl.Version_100, "glGetString", getProcAddress); } else { Gl.BindAPIFunction(Gl.Version_320_ES, "glGetError", getProcAddress); Gl.BindAPIFunction(Gl.Version_320_ES, "glGetString", getProcAddress); } // Parse version string (effective for detecting Desktop and ES contextes) KhronosVersion glversion = KhronosVersion.Parse(Gl.GetString(StringName.Version)); // ATM do not support fancy context creation flags return(glversion); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="function">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// </remarks> private static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (Configuration.RunningOnWindows) { getProcAddress = new GetProcAddressWindows(); } else if (Configuration.RunningOnMacOS) { getProcAddress = new GetProcAddressOSX(); } else if (Configuration.RunningOnX11) { getProcAddress = new GetProcAddressX11(); } else { throw new PlatformNotSupportedException( "Extension loading is only supported under Mac OS X, X11 and Windows. We are sorry for the inconvience."); } } return(getProcAddress.GetProcAddress(function)); }
/// <summary> /// Query the version of the current OpenGL context. /// </summary> /// <returns> /// It returns the <see cref="KhronosVersion"/> specifying teh actual version of <paramref name="ctx"/>. /// </returns> private static KhronosVersion QueryContextVersion() { IntPtr ctx = DeviceContext.GetCurrentContext(); if (ctx == null) { throw new InvalidOperationException("no current context"); } // Load minimal Gl functions for querying information IGetProcAddress getProcAddress = GetProcAddress.GetProcAddressGL; if (Egl.IsRequired == false) { Gl.BindAPIFunction(Gl.Version_100, null, "glGetError", getProcAddress); Gl.BindAPIFunction(Gl.Version_100, null, "glGetString", getProcAddress); Gl.BindAPIFunction(Gl.Version_100, null, "glGetIntegerv", getProcAddress); } else { Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetError", getProcAddress); Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetString", getProcAddress); Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetIntegerv", getProcAddress); } // Parse version string (effective for detecting Desktop and ES contextes) KhronosVersion glversion = KhronosVersion.Parse(Gl.GetString(StringName.Version)); // Context profile if (glversion.Api == KhronosVersion.ApiGl && glversion >= Gl.Version_320) { string glProfile = null; int ctxProfile = 0; Gl.Get(Gl.CONTEXT_PROFILE_MASK, out ctxProfile); if ((ctxProfile & Gl.CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0) { glProfile = KhronosVersion.ProfileCompatibility; } else if ((ctxProfile & Gl.CONTEXT_CORE_PROFILE_BIT) != 0) { glProfile = KhronosVersion.ProfileCore; } else { glProfile = KhronosVersion.ProfileCompatibility; } return(new KhronosVersion(glversion, glProfile)); } else { return(new KhronosVersion(glversion, KhronosVersion.ProfileCompatibility)); } }
/// <summary> /// Bind the OpenGL delegates to a specific API. /// </summary> /// <param name="version"> /// A <see cref="KhronosVersion"/> that specifies the API to bind. /// </param> /// <param name="getProcAddress"> /// The <see cref="IGetProcAddress"/> used for loading function pointers. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="version"/> or <paramref name="getProcAddress"/> is null. /// </exception> private static void BindAPI(KhronosVersion version, IGetProcAddress getProcAddress) { if (version == null) { throw new ArgumentNullException("version"); } if (getProcAddress == null) { throw new ArgumentNullException("getProcAddress"); } BindAPI <Gl>(GetPlatformLibrary(version), getProcAddress); }
/// <summary> /// Link delegates field using import declaration, using platform specific method for determining procedures address. /// </summary> internal static void BindAPIFunction <T>(string path, string functionName, IGetProcAddress getProcAddress) { if (path == null) { throw new ArgumentNullException("path"); } if (functionName == null) { throw new ArgumentNullException("function"); } if (getProcAddress == null) { throw new ArgumentNullException("getAddress"); } FunctionContext functionContext = GetFunctionContext(typeof(T)); Debug.Assert(functionContext != null); if (functionContext == null) { throw new InvalidOperationException("unrecognized API type"); } Type delegatesClass = typeof(T).GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); Debug.Assert(delegatesClass != null); if (delegatesClass == null) { throw new NotImplementedException("missing Delegates class"); } FieldInfo functionField = delegatesClass.GetField("p" + functionName, BindingFlags.Static | BindingFlags.NonPublic); Debug.Assert(functionField != null); if (functionField == null) { throw new NotImplementedException(String.Format("unable to find function named {0}", functionName)); } BindAPIFunction(path, functionContext, functionField, delegate(string libpath, string function) { // Note: IGetProcAddress implementation may have GetOpenGLProcAddress equivalent to GetProcAddress IntPtr procAddress = getProcAddress.GetOpenGLProcAddress(function); if (procAddress == IntPtr.Zero) { return(GetProcAddress.GetProcAddressOS.GetProcAddress(libpath, function)); } return(procAddress); }); }
/// <summary> /// Link delegates fields using import declarations, using platform specific method for determining procedures addresses. /// </summary> /// <param name="imports"> /// A <see cref="ImportMap"/> mapping a <see cref="MethodInfo"/> with the relative function name. /// </param> /// <param name="delegates"> /// A <see cref="DelegateList"/> listing <see cref="FieldInfo"/> related to function delegates. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="imports"/> or <paramref name="delegates"/> is null. /// </exception> internal static void BindAPI <T>(string path, IGetProcAddress getProcAddress) { BindAPI <T>(path, delegate(string libpath, string function) { // Note: IGetProcAddress implementation may have GetOpenGLProcAddress equivalent to GetProcAddress IntPtr procAddress = getProcAddress.GetOpenGLProcAddress(function); if (procAddress == IntPtr.Zero) { return(GetProcAddress.GetProcAddressOS.GetProcAddress(libpath, function)); } return(procAddress); }); }
public override void Initialize(IGetProcAddress GetProcAddress = null) { if (GetProcAddress == null) { GetProcAddress = sGetProcAddress; } var methods = pDelegatesClass.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); //if(methods == null) // throw new InvalidOperationException("No method fields found in class"); foreach (var f in methods) { Delegate d = null; MethodInfo m; if (pDllImportMap.TryGetValue(f.Name, out m)) { d = Delegate.CreateDelegate(f.FieldType, m); } else { var ptr = GetProcAddress.GetProcAddress(f.Name); if (ptr != IntPtr.Zero) { d = Marshal.GetDelegateForFunctionPointer(ptr, f.FieldType); } #if DEBUG else { Debug.WriteLine(string.Format( "{0} not found!", f.Name), "OpenGL Loader"); } #endif } if (d != null) { f.SetValue(null, d); } } #if DEBUG Marshal.PrelinkAll(pDllImportClass); #endif }
/// <summary> /// Link delegates fields using import declarations, using platform specific method for determining procedures addresses. /// </summary> /// <param name="imports"> /// A <see cref="ImportMap"/> mapping a <see cref="MethodInfo"/> with the relative function name. /// </param> /// <param name="delegates"> /// A <see cref="DelegateList"/> listing <see cref="FieldInfo"/> related to function delegates. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="imports"/> or <paramref name="delegates"/> is null. /// </exception> internal static void BindAPI <T>(string path, IGetProcAddress getProcAddress, KhronosVersion version, ExtensionsCollection extensions) { BindAPI <T>(path, delegate(string libpath, string function) { // Note: IGetProcAddress implementation may have GetOpenGLProcAddress equivalent to GetProcAddress IntPtr procAddress = getProcAddress.GetOpenGLProcAddress(function); if (procAddress == IntPtr.Zero) { return(GetProcAddress.GetProcAddressOS.GetProcAddress(libpath, function)); } if (procAddress == IntPtr.Zero) { LogComment("Warning: no address for command {0}.", function); } return(procAddress); }, version, extensions); }
public override void Initialize(IGetProcAddress GetProcAddress = null) { if (GetProcAddress == null) GetProcAddress = sGetProcAddress; var methods = pDelegatesClass.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); //if(methods == null) // throw new InvalidOperationException("No method fields found in class"); foreach (var f in methods) { Delegate d = null; MethodInfo m; if (pDllImportMap.TryGetValue(f.Name, out m)) { d = Delegate.CreateDelegate(f.FieldType, m); } else { var ptr = GetProcAddress.GetProcAddress(f.Name); if (ptr != IntPtr.Zero) d = Marshal.GetDelegateForFunctionPointer(ptr, f.FieldType); #if DEBUG else { Debug.WriteLine(string.Format( "{0} not found!", f.Name), "OpenGL Loader"); } #endif } if (d != null) f.SetValue(null, d); } #if DEBUG Marshal.PrelinkAll(pDllImportClass); #endif }
/// <summary> /// Query the version of the current OpenGL context. /// </summary> /// <returns> /// It returns the <see cref="KhronosVersion"/> specifying teh actual version of <paramref name="ctx"/>. /// </returns> private static KhronosVersion QueryContextVersionCore() { // Load minimal Gl functions for querying information IGetProcAddress getProcAddress = GetProcAddress.GetProcAddressGL; if (Egl.IsRequired == false) { BindAPIFunction(Gl.Version_100, null, "glGetError", getProcAddress); BindAPIFunction(Gl.Version_100, null, "glGetString", getProcAddress); BindAPIFunction(Gl.Version_100, null, "glGetIntegerv", getProcAddress); } else { BindAPIFunction(Gl.Version_320_ES, null, "glGetError", getProcAddress); BindAPIFunction(Gl.Version_320_ES, null, "glGetString", getProcAddress); BindAPIFunction(Gl.Version_320_ES, null, "glGetIntegerv", getProcAddress); } return(QueryContextVersion()); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="function">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// <see cref="Marshal.GetDelegateForFunctionPointer"/> /// </remarks> public static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || System.Environment.OSVersion.Platform == PlatformID.Win32S || System.Environment.OSVersion.Platform == PlatformID.Win32Windows || System.Environment.OSVersion.Platform == PlatformID.WinCE) { getProcAddress = new GetProcAddressWindows(); } else if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == (PlatformID)4) { // Distinguish between Unix and Mac OS X kernels. switch (DetectUnixKernel()) { case "Unix": case "Linux": getProcAddress = new GetProcAddressX11(); break; case "Darwin": getProcAddress = new GetProcAddressOSX(); break; default: throw new PlatformNotSupportedException( DetectUnixKernel() + ": Unknown Unix platform - cannot load extensions. Please report a bug at http://taoframework.com"); } } else { throw new PlatformNotSupportedException( "Extension loading is only supported under Mac OS X, Unix/X11 and Windows. We are sorry for the inconvience."); } } return getProcAddress.GetProcAddress(function); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="function">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// <see cref="Marshal.GetDelegateForFunctionPointer"/> /// </remarks> public static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || System.Environment.OSVersion.Platform == PlatformID.Win32S || System.Environment.OSVersion.Platform == PlatformID.Win32Windows || System.Environment.OSVersion.Platform == PlatformID.WinCE) { getProcAddress = new GetProcAddressWindows(); } else if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == (PlatformID)4) { // Distinguish between Unix and Mac OS X kernels. switch (DetectUnixKernel()) { case "Unix": case "Linux": getProcAddress = new GetProcAddressX11(); break; case "Darwin": getProcAddress = new GetProcAddressOSX(); break; default: throw new PlatformNotSupportedException( DetectUnixKernel() + ": Unknown Unix platform - cannot load extensions. Please report a bug at http://taoframework.com"); } } else { throw new PlatformNotSupportedException( "Extension loading is only supported under Mac OS X, Unix/X11 and Windows. We are sorry for the inconvience."); } } return(getProcAddress.GetProcAddress(function)); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="name">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// <see cref="Marshal.GetDelegateForFunctionPointer"/> /// <seealso cref="Gl.GetDelegateForExtensionMethod"/> /// </remarks> private static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || System.Environment.OSVersion.Platform == PlatformID.Win32S || System.Environment.OSVersion.Platform == PlatformID.Win32Windows || System.Environment.OSVersion.Platform == PlatformID.WinCE) { getProcAddress = new GetProcAddressWindows(); } else if (System.Environment.OSVersion.Platform == PlatformID.Unix) { getProcAddress = new GetProcAddressX11(); } else { throw new PlatformNotSupportedException( "Extension loading is only supported under X11 and Windows. We are sorry for the inconvience."); } } return(getProcAddress.GetProcAddress(function)); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="name">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// <see cref="Marshal.GetDelegateForFunctionPointer"/> /// <seealso cref="Gl.GetDelegateForExtensionMethod"/> /// </remarks> private static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || System.Environment.OSVersion.Platform == PlatformID.Win32S || System.Environment.OSVersion.Platform == PlatformID.Win32Windows || System.Environment.OSVersion.Platform == PlatformID.WinCE) { getProcAddress = new GetProcAddressWindows(); } else if (System.Environment.OSVersion.Platform == PlatformID.Unix) { getProcAddress = new GetProcAddressX11(); } else { throw new PlatformNotSupportedException( "Extension loading is only supported under X11 and Windows. We are sorry for the inconvience."); } } return getProcAddress.GetProcAddress(function); }
/// <summary> /// Retrieves the entry point for a dynamically exported OpenGL function. /// </summary> /// <param name="function">The function string for the OpenGL function (eg. "glNewList")</param> /// <returns> /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified /// OpenGL function is not dynamically exported. /// </returns> /// <remarks> /// <para> /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value /// into a call-able delegate. /// </para> /// <para> /// This function is cross-platform. It determines the underlying platform and uses the /// correct wgl, glx or agl GetAddress function to retrieve the function pointer. /// </para> /// </remarks> private static IntPtr GetAddress(string function) { if (getProcAddress == null) { if (Configuration.RunningOnWindows) { getProcAddress = new GetProcAddressWindows(); } else if (Configuration.RunningOnMacOS) { getProcAddress = new GetProcAddressOSX(); } else if (Configuration.RunningOnX11) { getProcAddress = new GetProcAddressX11(); } else { throw new PlatformNotSupportedException( "Extension loading is only supported under Mac OS X, X11 and Windows. We are sorry for the inconvience."); } } return getProcAddress.GetProcAddress(function); }
/// <summary> /// Bind a single OpenGL delegates to a specific API. /// </summary> /// <param name="version"> /// A <see cref="KhronosVersion"/> that specifies the API to bind. /// </param> /// <param name="getProcAddress"> /// The <see cref="IGetProcAddress"/> used for loading function pointers. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="version"/>, <paramref name="functionName"/> or <paramref name="getProcAddress"/> is null. /// </exception> internal static void BindAPIFunction(KhronosVersion version, ExtensionsCollection extensions, string functionName, IGetProcAddress getProcAddress) { BindAPIFunction <Gl>(GetPlatformLibrary(version), functionName, getProcAddress, version, extensions); }
/// <summary> /// Link delegates fields using import declarations, using platform specific method for determining procedures addresses. /// </summary> /// <param name="imports"> /// A <see cref="ImportMap"/> mapping a <see cref="MethodInfo"/> with the relative function name. /// </param> /// <param name="delegates"> /// A <see cref="DelegateList"/> listing <see cref="FieldInfo"/> related to function delegates. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="imports"/> or <paramref name="delegates"/> is null. /// </exception> internal static void BindAPI <T>(string path, IGetProcAddress getProcAddress) { BindAPI <T>(path, getProcAddress, null, null); }