/// <summary>
        /// Query OpenGL implementation extensions.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public static GraphicsCapabilities Query(GraphicsContext ctx, IDeviceContext deviceContext)
        {
            GraphicsCapabilities graphicsCapabilities = new GraphicsCapabilities();

            KhronosApi.LogComment("Query OpenGL extensions.");

            #region Platform Extension Reload

            // Since at this point there's a current OpenGL context, it's possible to use
            // {glx|wgl}GetExtensionsString to retrieve platform specific extensions

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
            case PlatformID.Win32Windows:
            case PlatformID.Win32S:
            case PlatformID.WinCE:
                // Wgl.SyncDelegates();
                break;
            }

            #endregion

            // OpenGL extensions
            graphicsCapabilities._GlExtensions.Query();
            // Windows OpenGL extensions
            WindowsDeviceContext windowsDeviceContext = deviceContext as WindowsDeviceContext;
            if (windowsDeviceContext != null)
            {
                graphicsCapabilities._WglExtensions.Query(windowsDeviceContext);
            }
            // GLX OpenGL extensions
            XServerDeviceContext xserverDeviceContext = deviceContext as XServerDeviceContext;
            if (xserverDeviceContext != null)
            {
                graphicsCapabilities._GlxExtensions.Query(xserverDeviceContext);
            }

            // Query implementation limits
            graphicsCapabilities._GraphicsLimits = GraphicsLimits.Query(graphicsCapabilities._GlExtensions);

            return(graphicsCapabilities);
        }
		public static GraphicsLimits Query(GraphicsContext ctx, IDeviceContext deviceContext)
		{
			GraphicsLimits graphicsLimits = new GraphicsLimits();
			FieldInfo[] graphicsLimitsFields = typeof(GraphicsLimits).GetFields(BindingFlags.Public | BindingFlags.Instance);

			foreach (FieldInfo field in graphicsLimitsFields) {
				GraphicsLimitAttribute graphicsLimitAttribute = (GraphicsLimitAttribute)Attribute.GetCustomAttribute(field, typeof(GraphicsLimitAttribute));
				Attribute[] graphicsExtensionAttributes = Attribute.GetCustomAttributes(field, typeof(GraphicsExtensionAttribute));
				MethodInfo getMethod;

				if (graphicsLimitAttribute == null)
					continue;

				// Check extension support
				if ((graphicsExtensionAttributes != null) && (graphicsExtensionAttributes.Length > 0)) {
					bool supported = Array.Exists(graphicsExtensionAttributes, delegate(Attribute item) {
						return ((GraphicsExtensionAttribute)item).IsSupported(ctx, deviceContext);
					});

					if (supported == false)
						continue;
				}

				// Determine which method is used to get the OpenGL limit
				if (field.FieldType != typeof(String)) {
					if (field.FieldType.IsArray == true)
						getMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32), field.FieldType }, null);
					else
						getMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32), field.FieldType.MakeByRefType() }, null);
				} else
					getMethod = typeof(Gl).GetMethod("GetString", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32) }, null);

				if (getMethod != null) {
					if (field.FieldType != typeof(String)) {
						object obj;

						if (field.FieldType.IsArray == false)
							obj = Activator.CreateInstance(field.FieldType);
						else
							obj = Array.CreateInstance(field.FieldType.GetElementType(), graphicsLimitAttribute.ArrayLenght);
						object[] @params = new object[] { graphicsLimitAttribute.EnumValue, obj };
						getMethod.Invoke(null, @params);

						field.SetValue(graphicsLimits, @params[1]);
					} else {
						string s = (string)getMethod.Invoke(null, new object[] { graphicsLimitAttribute.EnumValue });

						field.SetValue(graphicsLimits, s);
					}
				} else
					throw new InvalidOperationException("GraphicsLimits field " + field.Name + " doesn't have a OpenGL compatible type");

				string fieldValueString;
				object fieldValue = field.GetValue(graphicsLimits);

				if (fieldValue is Array) {
					StringBuilder sb = new StringBuilder();

					sb.Append("{ ");
					if (((Array)fieldValue).Length > 0) {
						foreach (object arrayItem in (Array)fieldValue)
							sb.AppendFormat("{0}, ", arrayItem);
						sb.Remove(sb.Length - 2, 2);
					}
					sb.Append(" }");

					fieldValueString = sb.ToString();
				} else
					fieldValueString = fieldValue.ToString();
			}

			return (graphicsLimits);
		}
Exemple #3
0
        /// <summary>
        /// Query the OpenGL implementation limits.
        /// </summary>
        /// <param name="glExtensions">
        /// A <see cref="Gl.Extensions"/> that specify the supported OpenGL extension by the current
        /// implementation.
        /// </param>
        /// <returns>
        /// It returns a <see cref="GraphicsLimits"/> that specify the current OpenGL implementation limits.
        /// </returns>
        /// <remarks>
        /// It is assumed to have a valid OpenGL context current on the calling thread.
        /// </remarks>
        public static GraphicsLimits Query(Gl.Extensions glExtensions)
        {
            if (glExtensions == null)
            {
                throw new ArgumentNullException("glExtensions");
            }

            KhronosApi.LogComment("Query OpenGL implementation imits.");

            GraphicsLimits graphicsLimits = new GraphicsLimits();

            FieldInfo[] graphicsLimitsFields = typeof(GraphicsLimits).GetFields(BindingFlags.Public | BindingFlags.Instance);

            foreach (FieldInfo field in graphicsLimitsFields)
            {
                GraphicsLimitAttribute graphicsLimitAttribute      = (GraphicsLimitAttribute)Attribute.GetCustomAttribute(field, typeof(GraphicsLimitAttribute));
                Attribute[]            graphicsExtensionAttributes = Attribute.GetCustomAttributes(field, typeof(KhronosApi.ExtensionAttribute));
                MethodInfo             getMethod;

                if (graphicsLimitAttribute == null)
                {
                    continue;
                }

                // Check extension support
                if ((graphicsExtensionAttributes != null) && (graphicsExtensionAttributes.Length > 0))
                {
                    bool supported = Array.Exists(graphicsExtensionAttributes, delegate(Attribute item) {
                        return(glExtensions.HasExtensions(((KhronosApi.ExtensionAttribute)item).ExtensionName));
                    });

                    if (supported == false)
                    {
                        continue;
                    }
                }

                // Determine which method is used to get the OpenGL limit
                if (field.FieldType != typeof(String))
                {
                    if (field.FieldType.IsArray == true)
                    {
                        getMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32), field.FieldType }, null);
                    }
                    else
                    {
                        getMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32), field.FieldType.MakeByRefType() }, null);
                    }
                }
                else
                {
                    getMethod = typeof(Gl).GetMethod("GetString", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Int32) }, null);
                }

                if (getMethod != null)
                {
                    if (field.FieldType != typeof(String))
                    {
                        object obj;

                        if (field.FieldType.IsArray == false)
                        {
                            obj = Activator.CreateInstance(field.FieldType);
                        }
                        else
                        {
                            obj = Array.CreateInstance(field.FieldType.GetElementType(), graphicsLimitAttribute.ArrayLenght);
                        }
                        object[] @params = new object[] { graphicsLimitAttribute.EnumValue, obj };

                        try {
                            getMethod.Invoke(null, @params);

                            field.SetValue(graphicsLimits, @params[1]);
                        } catch (Exception) {
                        }
                    }
                    else
                    {
                        try {
                            string s = (string)getMethod.Invoke(null, new object[] { graphicsLimitAttribute.EnumValue });

                            field.SetValue(graphicsLimits, s);
                        } catch (Exception) {
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("GraphicsLimits field " + field.Name + " doesn't have a OpenGL compatible type");
                }

                string fieldValueString;
                object fieldValue = field.GetValue(graphicsLimits);

                if (fieldValue is Array)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.Append("{ ");
                    if (((Array)fieldValue).Length > 0)
                    {
                        foreach (object arrayItem in (Array)fieldValue)
                        {
                            sb.AppendFormat("{0}, ", arrayItem);
                        }
                        sb.Remove(sb.Length - 2, 2);
                    }
                    sb.Append(" }");

                    fieldValueString = sb.ToString();
                }
                else
                {
                    fieldValueString = fieldValue.ToString();
                }
            }

            return(graphicsLimits);
        }