Exemple #1
0
        /// <summary>
        /// Create the device context and set the pixel format.
        /// </summary>
        protected void CreateDeviceContext()
        {
            // Create device context
            _DeviceContext = DeviceContextFactory.Create(this);
            _DeviceContext.IncRef();

            // Set pixel format
            DevicePixelFormatCollection pixelFormats     = _DeviceContext.PixelsFormats;
            DevicePixelFormat           controlReqFormat = new DevicePixelFormat();

            controlReqFormat.ColorBits       = (int)ColorBits;
            controlReqFormat.DepthBits       = (int)DepthBits;
            controlReqFormat.StencilBits     = (int)StencilBits;
            controlReqFormat.MultisampleBits = (int)MultisampleBits;
            controlReqFormat.DoubleBuffer    = DoubleBuffer;

            List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException("unable to find a suitable pixel format");
            }

            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);
        }
Exemple #2
0
        /// <summary>
        /// Create the device context and set the pixel format.
        /// </summary>
        private void CreateDeviceContext(DevicePixelFormat controlReqFormat)
        {
            #region Support ES API

            if (_ProfileType == ProfileType.Embedded)
            {
                DeviceContext.DefaultApi = KhronosVersion.ApiGles2;
            }

            #endregion

            #region Create device context

            _DeviceContext = DeviceContext.Create(GetDisplay(), this.Handle);
            _DeviceContext.IncRef();

            #endregion

            #region Set pixel format

            DevicePixelFormatCollection pixelFormats         = _DeviceContext.PixelsFormats;
            List <DevicePixelFormat>    matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if ((matchingPixelFormats.Count == 0) && controlReqFormat.DoubleBuffer)
            {
                // Try single buffered pixel formats
                controlReqFormat.DoubleBuffer = false;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
                if (matchingPixelFormats.Count == 0)
                {
                    throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
                }
            }
            else if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
            }

            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            #endregion

            #region Set V-Sync

            if (Gl.PlatformExtensions.SwapControl)
            {
                int swapInterval = SwapInterval;

                // Mask value in case it is not supported
                if (!Gl.PlatformExtensions.SwapControlTear && swapInterval == -1)
                {
                    swapInterval = 1;
                }

                _DeviceContext.SwapInterval(swapInterval);
            }

            #endregion
        }
        /// <summary>
        /// Copy this DevicePixelFormatCollection.
        /// </summary>
        /// <returns>
        /// It returns a <see cref="DevicePixelFormatCollection"/> equivalent to this DevicePixelFormatCollection.
        /// </returns>
        public DevicePixelFormatCollection Copy()
        {
            DevicePixelFormatCollection pixelFormats = new DevicePixelFormatCollection();

            pixelFormats.AddRange(this.Select(devicePixelFormat => devicePixelFormat.Copy()));

            return(pixelFormats);
        }
        /// <summary>
        /// Copy this DevicePixelFormatCollection.
        /// </summary>
        /// <returns>
        /// It returns a <see cref="DevicePixelFormatCollection"/> equivalent to this DevicePixelFormatCollection.
        /// </returns>
        public DevicePixelFormatCollection Copy()
        {
            DevicePixelFormatCollection pixelFormats = new DevicePixelFormatCollection();

            foreach (DevicePixelFormat devicePixelFormat in this)
            {
                pixelFormats.Add(devicePixelFormat.Copy());
            }

            return(pixelFormats);
        }
Exemple #5
0
        /// <summary>
        /// This is called immediately after the surface is first created.
        /// </summary>
        /// <param name="holder">
        /// The SurfaceHolder whose surface is being created.
        /// </param>
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            // Problem with static constructors? Ensure manual initialization
            Egl.Initialize(); Gl.Initialize();

            // Get actual native window handle
            _NativeWindowHandle = ANativeWindow_fromSurface(JNIEnv.Handle, holder.Surface.Handle);

            // Create device context
            _DeviceContext = DeviceContext.Create(IntPtr.Zero, _NativeWindowHandle);
            _DeviceContext.IncRef();

            // Set pixel format
            DevicePixelFormatCollection pixelFormats     = _DeviceContext.PixelsFormats;
            DevicePixelFormat           controlReqFormat = new DevicePixelFormat();

            controlReqFormat.RgbaUnsigned = true;
            controlReqFormat.RenderWindow = true;

            controlReqFormat.ColorBits = 24;
            //controlReqFormat.DepthBits = (int)DepthBits;
            //controlReqFormat.StencilBits = (int)StencilBits;
            //controlReqFormat.MultisampleBits = (int)MultisampleBits;
            //controlReqFormat.DoubleBuffer = true;

            List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException("unable to find a suitable pixel format");
            }

            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            // Create OpenGL context using compatibility profile
            if ((_RenderContext = _DeviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
            {
                throw new InvalidOperationException("unable to create render context");
            }
            // Make context current
            if (_DeviceContext.MakeCurrent(_RenderContext) == false)
            {
                throw new InvalidOperationException("unable to make context current");
            }
            // Raise relative event
            OnContextCreated();

            StartRendering(30.0f);
        }
		private DevicePixelFormatCollection GetPixelFormats_Win32()
		{
			DevicePixelFormatCollection pixelFormats = new DevicePixelFormatCollection();
			Wgl.PIXELFORMATDESCRIPTOR pixelDescr = new Wgl.PIXELFORMATDESCRIPTOR();
			int pixelFormatsCount = Wgl.DescribePixelFormat(_DeviceContext, 0, 0, ref pixelDescr);

			for (int i = 1; i <= pixelFormatsCount; i++) {
				Wgl.DescribePixelFormat(_DeviceContext, i, (uint)Marshal.SizeOf(typeof(Wgl.PIXELFORMATDESCRIPTOR)), ref pixelDescr);

				if ((pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.SupportOpenGL) == 0)
					continue;

				DevicePixelFormat pixelFormat = new DevicePixelFormat();

				pixelFormat.FormatIndex = i;

				pixelFormat.RgbaUnsigned = true;
				pixelFormat.RgbaFloat = false;

				pixelFormat.RenderWindow = true;
				pixelFormat.RenderBuffer = false;

				pixelFormat.DoubleBuffer = (pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.Doublebuffer) != 0;
				pixelFormat.SwapMethod = 0;
				pixelFormat.StereoBuffer = (pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.Stereo) != 0;

				pixelFormat.ColorBits = pixelDescr.cColorBits;
				pixelFormat.DepthBits = pixelDescr.cDepthBits;
				pixelFormat.StencilBits = pixelDescr.cStencilBits;

				pixelFormat.MultisampleBits = 0;

				pixelFormat.RenderPBuffer = false;

				pixelFormat.SRGBCapable = false;

				pixelFormats.Add(pixelFormat);
			}

			return (pixelFormats);
		}
Exemple #7
0
        /// <summary>
        /// Create the device context and set the pixel format.
        /// </summary>
        private void CreateDeviceContext(DevicePixelFormat controlReqFormat)
        {
            #region Support ES/SC API

            switch (_ProfileType)
            {
            case ProfileType.Embedded:
                DeviceContext.DefaultAPI = KhronosVersion.ApiGles2;
                break;

            case ProfileType.SecurityCritical2:
                DeviceContext.DefaultAPI = KhronosVersion.ApiGlsc2;
                break;
            }

            #endregion

            #region Create device context

            _DeviceContext = DeviceContext.Create(GetDisplay(), this.Handle);
            _DeviceContext.IncRef();

            #endregion

            #region Set pixel format

            DevicePixelFormatCollection pixelFormats         = _DeviceContext.PixelsFormats;
            List <DevicePixelFormat>    matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if ((matchingPixelFormats.Count == 0) && controlReqFormat.MultisampleBits > 0)
            {
                // Try to select the maximum multisample configuration
                int multisampleBits = 0;

                pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); });

                controlReqFormat.MultisampleBits = multisampleBits;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
            }

            if ((matchingPixelFormats.Count == 0) && controlReqFormat.DoubleBuffer)
            {
                // Try single buffered pixel formats
                controlReqFormat.DoubleBuffer = false;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
                if (matchingPixelFormats.Count == 0)
                {
                    throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
                }
            }
            else if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
            }

            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            #endregion

            #region Set V-Sync

            if (Gl.PlatformExtensions.SwapControl)
            {
                int swapInterval = SwapInterval;

                // Mask value in case it is not supported
                if (!Gl.PlatformExtensions.SwapControlTear && swapInterval == -1)
                {
                    swapInterval = 1;
                }

                _DeviceContext.SwapInterval(swapInterval);
            }

            #endregion
        }
		private DevicePixelFormatCollection GetPixelFormats_ARB_pixel_format(Wgl.Extensions wglExtensions)
		{
			// Get the number of pixel formats
			int[] countFormatAttribsCodes = new int[] { Wgl.NUMBER_PIXEL_FORMATS_ARB };
			int[] countFormatAttribsValues = new int[countFormatAttribsCodes.Length];

			Wgl.GetPixelFormatAttribARB(_DeviceContext, 1, 0, (uint)countFormatAttribsCodes.Length, countFormatAttribsCodes, countFormatAttribsValues);

			// Request configurations
			List<int> pixelFormatAttribsCodes = new List<int>(12);

			// Minimum requirements
			pixelFormatAttribsCodes.Add(Wgl.SUPPORT_OPENGL_ARB);      // Required to be Gl.TRUE
			pixelFormatAttribsCodes.Add(Wgl.ACCELERATION_ARB);        // Required to be Wgl.FULL_ACCELERATION or Wgl.ACCELERATION_ARB
			pixelFormatAttribsCodes.Add(Wgl.PIXEL_TYPE_ARB);
			// Buffer destination
			pixelFormatAttribsCodes.Add(Wgl.DRAW_TO_WINDOW_ARB);
			pixelFormatAttribsCodes.Add(Wgl.DRAW_TO_BITMAP_ARB);
			// Multiple buffers
			pixelFormatAttribsCodes.Add(Wgl.DOUBLE_BUFFER_ARB);
			pixelFormatAttribsCodes.Add(Wgl.SWAP_METHOD_ARB);
			pixelFormatAttribsCodes.Add(Wgl.STEREO_ARB);
			// Pixel description
			pixelFormatAttribsCodes.Add(Wgl.COLOR_BITS_ARB);
			pixelFormatAttribsCodes.Add(Wgl.DEPTH_BITS_ARB);
			pixelFormatAttribsCodes.Add(Wgl.STENCIL_BITS_ARB);

#if SUPPORT_MULTISAMPLE
			// Multisample extension
			if (wglExtensions.Multisample_ARB || wglExtensions.Multisample_EXT) {
				pixelFormatAttribsCodes.Add(Wgl.SAMPLE_BUFFERS_ARB);
				pixelFormatAttribsCodes.Add(Wgl.SAMPLES_ARB);
			}
			int pixelFormatAttribMultisampleIndex = pixelFormatAttribsCodes.Count - 1;
#endif

#if SUPPORT_PBUFFER
			if (wglExtensions.Pbuffer_ARB || wglExtensions.Pbuffer_EXT) {
				pixelFormatAttribsCodes.Add(Wgl.DRAW_TO_PBUFFER_ARB);
			}
			int pixelFormatAttribPBufferIndex = pixelFormatAttribsCodes.Count - 1;
#endif

#if SUPPORT_FRAMEBUFFER_SRGB
			// Framebuffer sRGB extension
			if (wglExtensions.FramebufferSRGB_ARB || wglExtensions.FramebufferSRGB_EXT)
				pixelFormatAttribsCodes.Add(Wgl.FRAMEBUFFER_SRGB_CAPABLE_ARB);
			int pixelFormatAttribFramebufferSrgbIndex = pixelFormatAttribsCodes.Count - 1;
#endif

			// Create pixel format collection
			DevicePixelFormatCollection pixelFormats = new DevicePixelFormatCollection();

			// Retrieve information about available pixel formats
			int[] pixelFormatAttribValues = new int[pixelFormatAttribsCodes.Count];

			for (int pixelFormatIndex = 1; pixelFormatIndex < countFormatAttribsValues[0]; pixelFormatIndex++) {
				DevicePixelFormat pixelFormat = new DevicePixelFormat();

				Wgl.GetPixelFormatAttribARB(_DeviceContext, pixelFormatIndex, 0, (uint)pixelFormatAttribsCodes.Count, pixelFormatAttribsCodes.ToArray(), pixelFormatAttribValues);

				// Check minimum requirements
				if (pixelFormatAttribValues[0] != Gl.TRUE)
					continue;       // No OpenGL support
				if (pixelFormatAttribValues[1] != Wgl.FULL_ACCELERATION_ARB)
					continue;       // No hardware acceleration

				switch (pixelFormatAttribValues[2]) {
					case Wgl.TYPE_RGBA_ARB:
#if SUPPORT_PIXEL_FORMAT_FLOAT
					case Wgl.TYPE_RGBA_FLOAT_ARB:
#endif
#if SUPPORT_PIXEL_FORMAT_PACKED_FLOAT
					case Wgl.TYPE_RGBA_UNSIGNED_FLOAT_EXT:
#endif
						break;
					default:
						continue;       // Ignored pixel type
				}

				// Collect pixel format attributes
				pixelFormat.FormatIndex = pixelFormatIndex;

				switch (pixelFormatAttribValues[2]) {
					case Wgl.TYPE_RGBA_ARB:
						pixelFormat.RgbaUnsigned = true;
						break;
					case Wgl.TYPE_RGBA_FLOAT_ARB:
						pixelFormat.RgbaFloat = true;
						break;
					case Wgl.TYPE_RGBA_UNSIGNED_FLOAT_EXT:
						pixelFormat.RgbaFloat = pixelFormat.RgbaUnsigned = true;
						break;
				}

				pixelFormat.RenderWindow = pixelFormatAttribValues[3] == Gl.TRUE;
				pixelFormat.RenderBuffer = pixelFormatAttribValues[4] == Gl.TRUE;

				pixelFormat.DoubleBuffer = pixelFormatAttribValues[5] == Gl.TRUE;
				pixelFormat.SwapMethod = pixelFormatAttribValues[6];
				pixelFormat.StereoBuffer = pixelFormatAttribValues[7] == Gl.TRUE;

				pixelFormat.ColorBits = pixelFormatAttribValues[8];
				pixelFormat.DepthBits = pixelFormatAttribValues[9];
				pixelFormat.StencilBits = pixelFormatAttribValues[10];

#if SUPPORT_MULTISAMPLE
				if (wglExtensions.Multisample_ARB || wglExtensions.Multisample_EXT) {
					Debug.Assert(pixelFormatAttribMultisampleIndex >= 0);
					pixelFormat.MultisampleBits = pixelFormatAttribValues[pixelFormatAttribMultisampleIndex];
				}
#endif

#if SUPPORT_PBUFFER
				if (wglExtensions.Pbuffer_ARB || wglExtensions.Pbuffer_EXT) {
					Debug.Assert(pixelFormatAttribPBufferIndex >= 0);
					pixelFormat.RenderPBuffer = pixelFormatAttribValues[pixelFormatAttribPBufferIndex] == Gl.TRUE;
				}
#endif

#if SUPPORT_FRAMEBUFFER_SRGB
				if (wglExtensions.FramebufferSRGB_ARB || wglExtensions.FramebufferSRGB_EXT) {
					Debug.Assert(pixelFormatAttribFramebufferSrgbIndex >= 0);
					pixelFormat.SRGBCapable = pixelFormatAttribValues[pixelFormatAttribFramebufferSrgbIndex] != 0;
				}
#endif

				pixelFormats.Add(pixelFormat);
			}

			return (pixelFormats);
		}
Exemple #9
0
        /// <summary>
        /// Obtain best macthing surface configuration supported by actual implementation.
        /// </summary>
        /// <param name="pixelFormats">
        /// A <see cref="List{DevicePixelFormat}"/> specifying the actual available device formats.
        /// </param>
        /// <param name="formatFilter">
        /// Delegate used for filtering pixel formats.
        /// </param>
        /// <returns>
        /// <para>
        /// It returns the closest macthing pixel format to this Surface configuration.
        /// </para>
        /// </returns>
        /// <exception cref="Exception">
        /// This exception is thrown when no pixel format was found for matching surface buffer
        /// configuration using the specified buffer policy.
        /// </exception>
        /// <remarks>
        /// Each system offer a limited number of possible configuration, which has to be choosen to
        /// allocated correctly a system Surface.
        /// These pixel formats are fetched during the static constructor of <see cref="GraphicsContext"/>,
        /// and this routine selects one of the available pixel format.
        /// </remarks>
        private DevicePixelFormat ChoosePixelFormat(DevicePixelFormatCollection pixelFormats, ValidBuffersFormatDelegate formatFilter)
        {
            if (pixelFormats == null)
            {
                throw new ArgumentNullException("pFormats");
            }

            // Custom filtering
            if (formatFilter != null)
            {
                pixelFormats.RemoveAll(delegate(DevicePixelFormat match) { return(formatFilter(match) == false); });
            }

            // Filter
            pixelFormats.RemoveAll(delegate(DevicePixelFormat match) {
                if ((RequiredUnsignedPixel && !match.RgbaUnsigned) || (RequiredUnsignedPixel && !match.RgbaUnsigned))
                {
                    return(true);
                }
                if (match.RedBits < _ColorBits.x || match.GreenBits < _ColorBits.y || match.BlueBits < _ColorBits.z || match.AlphaBits < _ColorBits.w)
                {
                    return(true);
                }
                if (((BuffersMask & GraphicsBufferType.Double) != 0) && !match.DoubleBuffer)
                {
                    return(true);
                }
                if (match.DepthBits < _DepthBits)
                {
                    return(true);
                }
                if (match.StencilBits < _StencilBits)
                {
                    return(true);
                }
                if (match.MultisampleBits < _MultisampleBits)
                {
                    return(true);
                }

                return(false);
            });

            if (pixelFormats.Count > 0)
            {
#if false
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    pFormats.RemoveAll(delegate(DevicePixelFormat match) {
                        return(match.DoubleBuffer == false);
                    });
                }
#endif
                pixelFormats.Sort(delegate(DevicePixelFormat a, DevicePixelFormat b) {
                    int comp;

                    if ((comp = a.DoubleBuffer.CompareTo(b.DoubleBuffer)) != 0)
                    {
                        return(comp);
                    }
                    if ((comp = a.ColorBits.CompareTo(b.ColorBits)) != 0)
                    {
                        return(comp);
                    }
                    if ((comp = a.DepthBits.CompareTo(b.DepthBits)) != 0)
                    {
                        return(comp);
                    }
                    if ((comp = a.MultisampleBits.CompareTo(b.MultisampleBits)) != 0)
                    {
                        return(comp);
                    }
                    if ((comp = a.StencilBits.CompareTo(b.StencilBits)) != 0)
                    {
                        return(comp);
                    }

                    return(a.FormatIndex.CompareTo(b.FormatIndex));
                });

                return(pixelFormats[0]);
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("unable to find any suitable window pixel format with ");
                if ((BuffersMask & GraphicsBufferType.Color) != 0)
                {
                    sb.AppendFormat("Color={0},", ColorType);
                }
                if ((BuffersMask & GraphicsBufferType.ColorSRGB) != 0)
                {
                    sb.AppendFormat("sRGB Color={0},", ColorType);
                }
                if (DepthBits > 0)
                {
                    sb.AppendFormat("Depth={0},", DepthBits);
                }
                if (StencilBits > 0)
                {
                    sb.AppendFormat("Stencil={0},", StencilBits);
                }
                if (MultisampleBits > 0)
                {
                    sb.AppendFormat("Multisample={0},", MultisampleBits);
                }
                if ((BuffersMask & GraphicsBufferType.Double) != 0)
                {
                    sb.AppendFormat("DoubleBuffer,");
                }
                sb.Remove(sb.Length - 1, 1);

                throw new InvalidOperationException(sb.ToString());
            }
        }