/// <summary> /// Ensure <see cref="MemoryCopy"/> functionality. /// </summary> private static void EnsureMemoryCopy() { if (MemoryCopyPointer == null) { IntPtr memoryCopyPtr; switch (Environment.OSVersion.Platform) { case PlatformID.Win32Windows: case PlatformID.Win32NT: case PlatformID.Win32S: memoryCopyPtr = GetProcAddress.GetAddress("msvcrt.dll", "memcpy"); if (memoryCopyPtr != IntPtr.Zero) { MemoryCopyPointer = (MemoryCopyDelegate)Marshal.GetDelegateForFunctionPointer(memoryCopyPtr, typeof(MemoryCopyDelegate)); return; } throw new NotSupportedException("no suitable memcpy support"); case PlatformID.Unix: memoryCopyPtr = GetProcAddress.GetAddress("libc.so.6", "memcpy"); if (memoryCopyPtr != IntPtr.Zero) { MemoryCopyPointer = (MemoryCopyDelegate)Marshal.GetDelegateForFunctionPointer(memoryCopyPtr, typeof(MemoryCopyDelegate)); return; } throw new NotSupportedException("no suitable memcpy support"); default: throw new NotSupportedException("no suitable memcpy support"); } } }
/// <summary> /// Link delegates fields using import declarations. /// </summary> /// <param name="path"> /// A <see cref="System.String"/> that specifies the assembly file path containing the import functions. /// </param> /// <param name="type"> /// A <see cref="System.Type"/> that specifies the type used for detecting import declarations and delegates fields. /// </param> protected static void LinkLibraryProcImports(string path, Type type) { SortedList <string, MethodInfo> sImportMap; List <FieldInfo> sDelegates; LinkProcAddressImports(path, type, delegate(string libpath, string function) { return(GetProcAddress.GetAddress(libpath, function)); }, out sImportMap, out sDelegates); }
protected Plugin(string assemblyPath, string factoryFunction) { if (assemblyPath == null) { throw new ArgumentNullException("assemblyPath"); } if ((_CreatePluginFactoryPtr = GetProcAddress.GetAddress(assemblyPath, factoryFunction)) == IntPtr.Zero) { throw new ArgumentException("not a valid factory function", "factoryFunction"); } // Marshal.GetDelegateForFunctionPointer(createFactoryPtr, factoryFunction); }
/// <summary> /// Link delegates fields using import declarations, using platform standard method for determining procesures addresses. /// </summary> /// <param name="path"> /// A <see cref="String"/> that specifies the assembly file path containing the import functions. /// </param> /// <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="path"/>, <paramref name="imports"/> or <paramref name="delegates"/> is null. /// </exception> protected static void LoadProcDelegates(string path, ImportMap imports, DelegateList delegates) { LoadProcDelegates(path, imports, delegates, delegate(string libpath, string function) { return(GetProcAddress.GetAddress(libpath, function)); }); }
/// <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> protected static void LoadProcDelegates(ImportMap imports, DelegateList delegates) { LoadProcDelegates(String.Empty, imports, delegates, delegate(string libpath, string function) { return(GetProcAddress.GetOpenGLAddress(function)); }); }
/// <summary> /// Link delegates fields using import declarations. /// </summary> /// <param name="type"> /// A <see cref="System.Type"/> that specifies the type used for detecting import declarations and delegates fields. /// </param> /// <param name="sImportMap"> /// A <see cref="T:SortedList{String, MethodInfo}"/> mapping a <see cref="MethodInfo"/> with the relative function name. /// </param> /// <param name="sDelegates"> /// A <see cref="T:List{FieldInfo}"/> listing <see cref="FieldInfo"/> related to function delegates. /// </param> protected static void LinkOpenGLProcImports(Type type, out SortedList <string, MethodInfo> sImportMap, out List <FieldInfo> sDelegates) { LinkProcAddressImports(null, type, delegate(string libpath, string function) { return(GetProcAddress.GetOpenGLAddress(function)); }, out sImportMap, out sDelegates); }
/// <summary> /// /// </summary> /// <param name="imports"></param> /// <param name="delegateField"></param> /// <returns></returns> private static Delegate GetDelegate(ImportMap imports, FieldInfo delegateField) { if (imports == null) { throw new ArgumentNullException("imports"); } Delegate importDelegate; Attribute[] aliasOfAttributes = Attribute.GetCustomAttributes(delegateField, typeof(AliasOfAttribute)); string importName = delegateField.Name.Substring(1); IntPtr importAddress = IntPtr.Zero; #region Get Function Pointer if (aliasOfAttributes.Length > 0) { for (int i = 0; i < aliasOfAttributes.Length; i++) { if ((importAddress = GetProcAddress.GetOpenGLAddress(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName)) != IntPtr.Zero) { break; } } } else { importAddress = GetProcAddress.GetOpenGLAddress(importName); } #endregion // Is function implemented? if (importAddress == IntPtr.Zero) { return(null); } // Try to load external symbol if ((importDelegate = Marshal.GetDelegateForFunctionPointer(importAddress, delegateField.FieldType)) == null) { MethodInfo importMethod = null; if (aliasOfAttributes.Length > 0) { for (int i = 0; i < aliasOfAttributes.Length; i++) { if ((importAddress = GetProcAddress.GetOpenGLAddress(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName)) != IntPtr.Zero) { break; } if (imports.TryGetValue(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName, out importMethod)) { break; } } } else { imports.TryGetValue(importName, out importMethod); } return(importMethod != null ? Delegate.CreateDelegate(delegateField.FieldType, importMethod) : null); } else { return(importDelegate); } }
/// <summary> /// Load available SIMD extensions. /// </summary> public static void LoadSimdExtensions() { const string AssemblyPath = "OpenGL.Simd.dll"; IntPtr getCpuInformationPtr = IntPtr.Zero; if (File.Exists(AssemblyPath) == false) { return; } try { getCpuInformationPtr = GetProcAddress.GetAddress(AssemblyPath, "GetCpuInformation"); } catch { /* Ignore exception, leave 'getCpuInformationPtr' equals to 'IntPtr.Zero' */ } // No CPU information? Ahi ahi ahi if (getCpuInformationPtr == IntPtr.Zero) { return; } GetCpuInformation getCpuInformation = (GetCpuInformation)Marshal.GetDelegateForFunctionPointer(getCpuInformationPtr, typeof(GetCpuInformation)); CpuInformation cpuInfo = new CpuInformation(); getCpuInformation(ref cpuInfo); if (cpuInfo.SimdSupport != SimdTechnology.None) { FieldInfo[] fields = typeof(Memory).GetFields(BindingFlags.Static | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fields) { // Test for SSSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSSE3"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE3"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE2 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE2) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE2"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE support if ((cpuInfo.SimdSupport & SimdTechnology.SSE) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for MMX support if ((cpuInfo.SimdSupport & SimdTechnology.MMX) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "MMX"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Reset field fieldInfo.SetValue(null, null); } } }