Ejemplo n.º 1
0
            /// <summary>
            /// Construct a NativeWindow on an OS window.
            /// </summary>
            /// <param name="display">
            /// A <see cref="IntPtr"/> that specifies the display handle to be passed to <see cref="Egl.GetDisplay(IntPtr)"/>.
            /// </param>
            /// <param name="windowHandle">
            /// A <see cref="IntPtr"/> that specifies the handle of the OS window.
            /// </param>
            /// <param name="pixelFormat">
            /// A <see cref="DevicePixelFormat"/> used for choosing the NativeWindow pixel format configuration. It can
            /// be null; in this case the pixel format will be set elsewhere.
            /// </param>
            public NativeWindow(IntPtr display, IntPtr windowHandle, DevicePixelFormat pixelFormat)
                : base(display)
            {
                try {
                    // Hold the window handle in case pixel format will be set later
                    _WindowHandle = windowHandle;

                    // Choose appropriate pixel format
                    if (pixelFormat != null)
                    {
                        pixelFormat.RenderWindow = true;

                        IntPtr     configId = ChoosePixelFormat(_Display, _EglVersion, pixelFormat);
                        List <int> attribs  = new List <int>();

                        if (pixelFormat.DoubleBuffer)
                        {
                            attribs.AddRange(new[] { Egl.RENDER_BUFFER, Egl.BACK_BUFFER });
                        }
                        attribs.Add(Egl.NONE);

                        CreateHandle(configId, attribs.ToArray());
                    }
                } catch {
                    Dispose();
                    throw;
                }
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Construct a NativePBuffer with a specific pixel format and size.
            /// </summary>
            /// <param name="display">
            ///
            /// </param>
            /// <param name="pixelFormat">
            /// A <see cref="DevicePixelFormat"/> that specifies the pixel format and the ancillary buffers required.
            /// </param>
            /// <param name="width">
            /// A <see cref="UInt32"/> that specifies the width of the P-Buffer, in pixels.
            /// </param>
            /// <param name="height">
            /// A <see cref="UInt32"/> that specifies the height of the P-Buffer, in pixels.
            /// </param>
            public NativePBuffer(IntPtr display, DevicePixelFormat pixelFormat, uint width, uint height)
                : base(display)
            {
                if (pixelFormat == null)
                {
                    throw new ArgumentNullException(nameof(pixelFormat));
                }

                try {
                    // Choose appropriate pixel format
                    pixelFormat.RenderWindow  = false;                          // XXX
                    pixelFormat.RenderPBuffer = true;

                    IntPtr     configId = ChoosePixelFormat(_Display, _EglVersion, pixelFormat);
                    List <int> attribs  = new List <int>();

                    attribs.AddRange(new[] { Egl.WIDTH, (int)width });
                    attribs.AddRange(new[] { Egl.HEIGHT, (int)height });
                    attribs.Add(Egl.NONE);

                    CreateHandle(configId, attribs.ToArray());
                } catch {
                    Dispose();
                    throw;
                }
            }
Ejemplo n.º 3
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
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Set the device pixel format.
 /// </summary>
 /// <param name="pixelFormat">
 /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// Exception thrown if <paramref name="pixelFormat"/> is null.
 /// </exception>
 public override void SetPixelFormat(DevicePixelFormat pixelFormat)
 {
     if (pixelFormat == null)
     {
         throw new ArgumentNullException("pixelFormat");
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }
            if (_PixelFormatSet == true)
            {
                throw new InvalidOperationException("pixel format already set");
            }

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
            bool res;

            // Note (from MSDN): Setting the pixel format of a window more than once can lead to significant complications for the Window Manager
            // and for multithread applications, so it is not allowed. An application can only set the pixel format of a window one time. Once a
            // window's pixel format is set, it cannot be changed.

            res = Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor);
            Debug.Assert(res);

            // Set choosen pixel format
            res = Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor);
            Debug.Assert(res);

            // Unable to set pixel format again
            _PixelFormatSet = true;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Set pixel format by letting the driver choose the best pixel format following the criteria.
        /// </summary>
        /// <param name="pixelFormat">
        ///
        /// </param>
        private void SetDisplayablePixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            List <int>   attribIList = new List <int>();
            List <float> attribFList = new List <float>();

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
            uint[] countFormatAttribsValues       = new uint[1];
            int[]  choosenFormats = new int[4];

            // Let choose pixel formats
            if (!Wgl.ChoosePixelFormatARB(_DeviceContext, attribIList.ToArray(), attribFList.ToArray(), (uint)choosenFormats.Length, choosenFormats, countFormatAttribsValues))
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to choose pixel format: {0}", innerException.Message), innerException);
            }

            // Set choosen pixel format
            if (Wgl.SetPixelFormat(_DeviceContext, choosenFormats[0], ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to set pixel format {0}: {1}", pixelFormat.FormatIndex, innerException.Message), innerException);
            }
        }
Ejemplo n.º 7
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]);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }
            if (_PixelFormatSet == true)
            {
                throw new InvalidOperationException("pixel format already set");
            }

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

            // Note (from MSDN): Setting the pixel format of a window more than once can lead to significant complications for the Window Manager
            // and for multithread applications, so it is not allowed. An application can only set the pixel format of a window one time. Once a
            // window's pixel format is set, it cannot be changed.

            if (Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to describe pixel format, {0}", innerException.Message), innerException);
            }

            // Set choosen pixel format
            if (Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to set pixel format, {0}", innerException.Message), innerException);
            }

            // Unable to set pixel format again
            _PixelFormatSet = true;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            List <int> configAttribs = new List <int>();

            if (Version >= Egl.Version_120)
            {
                configAttribs.AddRange(new int[] { Egl.RENDERABLE_TYPE, Egl.OPENGL_ES2_BIT });
            }
            configAttribs.AddRange(new int[] {
                Egl.CONFIG_ID, pixelFormat.FormatIndex,
            });
            configAttribs.Add(Egl.NONE);

            int[]    configCount = new int[1];
            IntPtr[] configs     = new IntPtr[1];

            if (Egl.ChooseConfig(_Display, configAttribs.ToArray(), configs, 1, configCount) == false)
            {
                throw new InvalidOperationException("unable to choose configuration");
            }
            if (configCount[0] == 0)
            {
                throw new InvalidOperationException("no available configuration");
            }

            IntPtr config = configs[0];

            _Config = config;
        }
Ejemplo n.º 10
0
			/// <summary>
			/// Construct a NativePBuffer with a specific pixel format and size.
			/// </summary>
			/// <param name="pixelFormat">
			/// A <see cref="DevicePixelFormat"/> that specifies the pixel format and the ancillary buffers required.
			/// </param>
			/// <param name="width">
			/// A <see cref="UInt32"/> that specifies the width of the P-Buffer, in pixels.
			/// </param>
			/// <param name="height">
			/// A <see cref="UInt32"/> that specifies the height of the P-Buffer, in pixels.
			/// </param>
			public NativePBuffer(DevicePixelFormat pixelFormat, uint width, uint height)
			{
				if (pixelFormat == null)
					throw new ArgumentNullException("pixelFormat");

				if (!Wgl.CurrentExtensions.Pbuffer_ARB && !Wgl.CurrentExtensions.Pbuffer_EXT)
					throw new NotSupportedException("WGL_(ARB|EXT)_pbuffer not implemented");
				if (Gl._NativeWindow == null)
					throw new InvalidOperationException("no underlying native window", Gl._InitializationException);

				try {
					// Uses screen device context
					_DeviceContext = Wgl.GetDC(Gl._NativeWindow.Handle);

					// Choose appropriate pixel format
					pixelFormat.RenderWindow = false;	// XXX
					pixelFormat.RenderPBuffer = true;
					pixelFormat.DoubleBuffer = true;

					int pixelFormatIndex = ChoosePixelFormat(_DeviceContext, pixelFormat);

					if (Wgl.CurrentExtensions.Pbuffer_ARB)
						_Handle = Wgl.CreatePbufferARB(_DeviceContext, pixelFormatIndex, (int)width, (int)height, new int[] { 0 });
					else
						_Handle = Wgl.CreatePbufferEXT(_DeviceContext, pixelFormatIndex, (int)width, (int)height, new int[] { 0 });
					if (_Handle == IntPtr.Zero)
						throw new InvalidOperationException("unable to create P-Buffer", GetPlatformExceptionCore());
				} catch {
					Dispose();
					throw;
				}
			}
Ejemplo n.º 11
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");
			if (IsPixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

#if CHOOSE_PIXEL_FORMAT_FALLBACK
			try {
#endif
				Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

				// Note (from MSDN): Setting the pixel format of a window more than once can lead to significant complications for the Window Manager
				// and for multithread applications, so it is not allowed. An application can only set the pixel format of a window one time. Once a
				// window's pixel format is set, it cannot be changed.

				if (Wgl.DescribePixelFormat(_DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor) == 0)
					throw new InvalidOperationException(String.Format("unable to describe pixel format {0}", pixelFormat.FormatIndex), GetPlatformException());

				// Set choosen pixel format
				if (!Wgl.SetPixelFormat(_DeviceContext, pixelFormat.FormatIndex, ref pDescriptor))
					throw new InvalidOperationException(String.Format("unable to set pixel format {0}: {1}", pixelFormat.FormatIndex), GetPlatformException());

#if CHOOSE_PIXEL_FORMAT_FALLBACK
			} catch (InvalidOperationException) {
				// Try using default ChoosePixelFormat*
				SetDisplayablePixelFormat(pixelFormat);
			}
#endif

			IsPixelFormatSet = true;
		}
Ejemplo n.º 12
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        private static int ChoosePixelFormat(IntPtr deviceContext, DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            List <int>   attribIList = new List <int>();
            List <float> attribFList = new List <float>();

            uint[] countFormatAttribsValues = new uint[1];
            int[]  choosenFormats           = new int[4];

            attribIList.AddRange(new int[] { Wgl.SUPPORT_OPENGL_ARB, Gl.TRUE });
            if (pixelFormat.RenderWindow)
            {
                attribIList.AddRange(new int[] { Wgl.DRAW_TO_WINDOW_ARB, Gl.TRUE });
            }
            if (pixelFormat.RenderPBuffer)
            {
                attribIList.AddRange(new int[] { Wgl.DRAW_TO_PBUFFER_ARB, Gl.TRUE });
            }

            if (pixelFormat.RgbaUnsigned)
            {
                attribIList.AddRange(new int[] { Wgl.PIXEL_TYPE_ARB, Wgl.TYPE_RGBA_ARB });
            }
            if (pixelFormat.RgbaFloat)
            {
                attribIList.AddRange(new int[] { Wgl.PIXEL_TYPE_ARB, Wgl.TYPE_RGBA_FLOAT_ARB });
            }

            if (pixelFormat.ColorBits > 0)
            {
                attribIList.AddRange(new int[] { Wgl.COLOR_BITS_ARB, pixelFormat.ColorBits });
            }
            if (pixelFormat.DepthBits > 0)
            {
                attribIList.AddRange(new int[] { Wgl.DEPTH_BITS_ARB, pixelFormat.DepthBits });
            }
            if (pixelFormat.StencilBits > 0)
            {
                attribIList.AddRange(new int[] { Wgl.STENCIL_BITS_ARB, pixelFormat.StencilBits });
            }

            if (pixelFormat.DoubleBuffer)
            {
                attribIList.AddRange(new int[] { Wgl.DOUBLE_BUFFER_ARB, pixelFormat.StencilBits });
            }
            attribIList.Add(0);

            // Let choose pixel formats
            if (!Wgl.ChoosePixelFormatARB(deviceContext, attribIList.ToArray(), attribFList.ToArray(), (uint)choosenFormats.Length, choosenFormats, countFormatAttribsValues))
            {
                throw new InvalidOperationException("unable to choose pixel format", GetPlatformExceptionCore());
            }

            return(choosenFormats[0]);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Set the device pixel format.
 /// </summary>
 /// <param name="pixelFormat">
 /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
 /// </param>
 public override void ChoosePixelFormat(DevicePixelFormat pixelFormat)
 {
     if (_NativeSurface.Handle != IntPtr.Zero)
     {
         throw new InvalidOperationException("pixel format already set");
     }
     _Config = ChoosePixelFormat(Display, Version, pixelFormat);
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            _FBConfig    = pixelFormat.XFbConfig;
            _XVisualInfo = pixelFormat.XVisualInfo;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            // Note: GLX does not really need a SetPixelFormat operation

            // Framebuffer configuration
            FBConfig = pixelFormat.XFbConfig;
            // X Visual Information
            XVisualInfo = pixelFormat.XVisualInfo;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        public override void ChoosePixelFormat(DevicePixelFormat pixelFormat)
        {
            if (_NativeSurface == null)
            {
                return;                 // Support EGL_KHR_surfaceless_context
            }
            if (_NativeSurface.Handle != IntPtr.Zero)
            {
                throw new InvalidOperationException("pixel format already set");
            }
            _Config = ChoosePixelFormat(Display, Version, pixelFormat);

            IsPixelFormatSet = true;
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		public override void ChoosePixelFormat(DevicePixelFormat pixelFormat)
		{
			if (IsPixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

			// Choose pixel format
			int pixelFormatIndex = ChoosePixelFormat(_DeviceContext, pixelFormat);
			
			// Set choosen pixel format
			Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

			if (Wgl.SetPixelFormat(_DeviceContext, pixelFormatIndex, ref pDescriptor) == false)
				throw new InvalidOperationException("unable to set pixel format", GetPlatformException());

			IsPixelFormatSet = true;
		}
Ejemplo n.º 19
0
        void impl_ChoosePixelFormat(DevicePixelFormat pixelFormat)
        {
            // Create pixel format descriptor
            _pfd = new Win32.PIXELFORMATDESCRIPTOR()
            {
                nSize      = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(typeof(OpenGL.Win32.PIXELFORMATDESCRIPTOR)),
                nVersion   = 1,
                dwFlags    = OpenGL.Win32.PFD_DRAW_TO_WINDOW | OpenGL.Win32.PFD_SUPPORT_OPENGL | OpenGL.Win32.PFD_DOUBLEBUFFER,
                iPixelType = OpenGL.Win32.PFD_TYPE_RGBA,
                cColorBits = (byte)pixelFormat.ColorBits,
                cDepthBits = (byte)pixelFormat.DepthBits,
                iLayerType = OpenGL.Win32.PFD_PLANE_MAIN,
            };

            // Choose pixel format
            _pixelFormat = Win32.ChoosePixelFormat(_hdc, ref _pfd);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Create an off-screen buffer.
        /// </summary>
        /// <returns>
        /// It returns a <see cref="INativePBuffer"/> that implements a native P-Buffer on the underlying platform.
        /// </returns>
        /// <exception cref='NotSupportedException'>
        /// Exception thrown if the current platform is not supported.
        /// </exception>
        public static INativePBuffer CreatePBuffer(DevicePixelFormat pixelFormat, uint width, uint height)
        {
#if !MONODROID
            if (Egl.IsRequired == false)
            {
                switch (Platform.CurrentPlatformId)
                {
                case Platform.Id.WindowsNT:
                    return(new DeviceContextWGL.NativePBuffer(pixelFormat, width, height));

                default:
                    throw new NotSupportedException(String.Format("platform {0} not supported", Platform.CurrentPlatformId));
                }
            }
            else
#endif
            return(new DeviceContextEGL.NativePBuffer(pixelFormat, width, height));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException(nameof(pixelFormat));
            }
            if (_NativeSurface == null)
            {
                return;                 // Support EGL_KHR_surfaceless_context
            }
            if (_NativeSurface.Handle != IntPtr.Zero)
            {
                throw new InvalidOperationException("pixel format already set");
            }

            List <int> configAttribs = new List <int>();

            if (Version >= Egl.Version_120)
            {
                configAttribs.AddRange(new[] { Egl.RENDERABLE_TYPE, Egl.OPENGL_ES2_BIT });
            }
            configAttribs.AddRange(new[] {
                Egl.CONFIG_ID, pixelFormat.FormatIndex
            });
            configAttribs.Add(Egl.NONE);

            int[]    configCount = new int[1];
            IntPtr[] configs     = new IntPtr[1];

            if (Egl.ChooseConfig(Display, configAttribs.ToArray(), configs, 1, configCount) == false)
            {
                throw new InvalidOperationException("unable to choose configuration");
            }
            if (configCount[0] == 0)
            {
                throw new InvalidOperationException("no available configuration");
            }

            _Config = configs[0];

            IsPixelFormatSet = true;
        }
Ejemplo n.º 22
0
		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);
		}
Ejemplo n.º 23
0
        private int GetPixelFormatPriority(DevicePixelFormat pixelFormat)
        {
            int priority = 0;

            if (!IsRequiredBuffer(BufferType.Depth) && pixelFormat.DepthBits > 0)
            {
                priority -= 1;
            }
            if (!IsRequiredBuffer(BufferType.Stencil) && pixelFormat.StencilBits > 0)
            {
                priority -= 1;
            }
            if (!IsRequiredBuffer(BufferType.Multisample) && pixelFormat.MultisampleBits > 0)
            {
                priority -= 1;
            }

            if (pixelFormat.DoubleBuffer)
            {
                priority += 3;
            }

            return(priority);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        public override void ChoosePixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            int screen = Glx.XDefaultScreen(_Display);

            // Get basic visual
            Glx.XVisualInfo visual;
            IntPtr          config;

            List <int> attributes = new List <int>();

            if (pixelFormat.RenderWindow)
            {
                attributes.AddRange(new int[] { Glx.DRAWABLE_TYPE, (int)Glx.WINDOW_BIT });
            }
            if (pixelFormat.RgbaUnsigned)
            {
                attributes.AddRange(new int[] { Glx.RENDER_TYPE, (int)Glx.RGBA_BIT });
            }
            if (pixelFormat.RgbaFloat)
            {
                attributes.AddRange(new int[] { Glx.RENDER_TYPE, (int)Glx.RGBA_FLOAT_BIT_ARB });
            }
            if (pixelFormat.DoubleBuffer)
            {
                attributes.AddRange(new int[] { Glx.DOUBLEBUFFER, Gl.TRUE });
            }

            if (pixelFormat.ColorBits > 0)
            {
                switch (pixelFormat.ColorBits)
                {
                case 16:
                    attributes.AddRange(new int[] { Glx.RED_SIZE, 5, Glx.GREEN_SIZE, 6, Glx.BLUE_SIZE, 8, Glx.ALPHA_SIZE, 5 });
                    break;

                case 24:
                    attributes.AddRange(new int[] { Glx.RED_SIZE, 8, Glx.GREEN_SIZE, 8, Glx.BLUE_SIZE, 8 });
                    break;

                case 32:
                    attributes.AddRange(new int[] { Glx.RED_SIZE, 8, Glx.GREEN_SIZE, 8, Glx.BLUE_SIZE, 8, Glx.ALPHA_SIZE, 8 });
                    break;

                default:
                    if (pixelFormat.ColorBits < 16)
                    {
                        attributes.AddRange(new int[] { Glx.RED_SIZE, 1, Glx.GREEN_SIZE, 1, Glx.BLUE_SIZE, 1 });
                    }
                    else
                    {
                        int bits = pixelFormat.ColorBits / 4;
                        attributes.AddRange(new int[] { Glx.RED_SIZE, bits, Glx.GREEN_SIZE, bits, Glx.BLUE_SIZE, bits });
                    }
                    break;
                }
            }

            if (pixelFormat.DepthBits > 0)
            {
                attributes.AddRange(new int[] { Glx.DEPTH_SIZE, pixelFormat.DepthBits });
            }
            if (pixelFormat.StencilBits > 0)
            {
                attributes.AddRange(new int[] { Glx.STENCIL_SIZE, pixelFormat.StencilBits });
            }

            attributes.Add(0);

            unsafe {
                int[] choosenConfigCount = new int[1];

                IntPtr *choosenConfigs = Glx.ChooseFBConfig(_Display, screen, attributes.ToArray(), choosenConfigCount);
                if (choosenConfigCount[0] == 0)
                {
                    throw new InvalidOperationException("unable to choose visual");
                }
                config = *choosenConfigs;
                KhronosApi.LogComment("Choosen config is 0x{0}", config.ToString("X8"));

                visual = Glx.GetVisualFromFBConfig(_Display, config);
                KhronosApi.LogComment("Choosen visual is {0}", visual);

                Glx.XFree((IntPtr)choosenConfigs);
            }

            _FBConfig    = config;
            _XVisualInfo = visual;

            IsPixelFormatSet = true;
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Copy this DevicePixelFormat
        /// </summary>
        /// <returns>
        /// It returns a <see cref="DevicePixelFormat"/> equals to this DevicePixelFormat.
        /// </returns>
        public DevicePixelFormat Copy()
        {
            DevicePixelFormat copy = (DevicePixelFormat)MemberwiseClone();

            return(copy);
        }
Ejemplo n.º 26
0
		/// <summary>
		/// Callback method for filtering valid pixel formats for this GraphicsSurface.
		/// </summary>
		/// <param name="pFormat">
		/// A <see cref="DevicePixelFormat"/> to be validated by derived implementation.
		/// </param>
		/// <returns>
		/// It returns true in the case the pixel format is valid for usage in the derived implementation,
		/// otherwise it returns false.
		/// </returns>
		private static bool ValidPixelFormat(DevicePixelFormat pFormat)
		{
			// The pixel format must be able to render on a window
			return (pFormat.RenderWindow);
		}
Ejemplo n.º 27
0
 /// <summary>
 /// Construct a NativePBuffer with a specific pixel format and size.
 /// </summary>
 /// <param name="pixelFormat">
 /// A <see cref="DevicePixelFormat"/> that specifies the pixel format and the ancillary buffers required.
 /// </param>
 /// <param name="width">
 /// A <see cref="UInt32"/> that specifies the width of the P-Buffer, in pixels.
 /// </param>
 /// <param name="height">
 /// A <see cref="UInt32"/> that specifies the height of the P-Buffer, in pixels.
 /// </param>
 public NativePBuffer(DevicePixelFormat pixelFormat, uint width, uint height)
     : this(DefaultDisplay, pixelFormat, width, height)
 {
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        private static IntPtr ChoosePixelFormat(IntPtr display, KhronosVersion version, DevicePixelFormat pixelFormat)
        {
            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }
            if (pixelFormat == null)
            {
                throw new ArgumentNullException(nameof(pixelFormat));
            }

            List <int> configAttribs = new List <int>();

            int[]    configCount = new int[1];
            IntPtr[] configs     = new IntPtr[8];
            int      surfaceType = 0;

            if (version >= Egl.Version_120)
            {
                configAttribs.AddRange(new[] { Egl.RENDERABLE_TYPE, Egl.OPENGL_ES2_BIT });
            }

            if (pixelFormat.RenderWindow)
            {
                surfaceType |= Egl.WINDOW_BIT;
            }
            if (pixelFormat.RenderPBuffer)
            {
                surfaceType |= Egl.PBUFFER_BIT;
            }
            if (surfaceType != 0)
            {
                configAttribs.AddRange(new[] { Egl.SURFACE_TYPE, surfaceType });
            }

            switch (pixelFormat.ColorBits)
            {
            case 24:
                configAttribs.AddRange(new[] { Egl.RED_SIZE, 8, Egl.GREEN_SIZE, 8, Egl.BLUE_SIZE, 8 });
                break;

            case 32:
                configAttribs.AddRange(new[] { Egl.RED_SIZE, 8, Egl.GREEN_SIZE, 8, Egl.BLUE_SIZE, 8, Egl.ALPHA_SIZE, 8 });
                break;

            default:
                configAttribs.AddRange(new[] { Egl.BUFFER_SIZE, pixelFormat.ColorBits });
                break;
            }
            if (pixelFormat.DepthBits > 0)
            {
                configAttribs.AddRange(new[] { Egl.DEPTH_SIZE, pixelFormat.DepthBits });
            }
            if (pixelFormat.StencilBits > 0)
            {
                configAttribs.AddRange(new[] { Egl.STENCIL_SIZE, pixelFormat.StencilBits });
            }

            configAttribs.Add(Egl.NONE);

            if (Egl.ChooseConfig(display, configAttribs.ToArray(), configs, configs.Length, configCount) == false)
            {
                throw new InvalidOperationException("unable to choose configuration");
            }
            if (configCount[0] == 0)
            {
                throw new InvalidOperationException("no available configuration");
            }

            return(configs[0]);
        }
		private int GetPixelFormatPriority(DevicePixelFormat pixelFormat)
		{
			int priority = 0;

			if (!IsRequiredBuffer(BufferType.Depth) && pixelFormat.DepthBits > 0)
				priority -= 1;
			if (!IsRequiredBuffer(BufferType.Stencil) && pixelFormat.StencilBits > 0)
				priority -= 1;
			if (!IsRequiredBuffer(BufferType.Multisample) && pixelFormat.MultisampleBits > 0)
				priority -= 1;

			if (pixelFormat.DoubleBuffer)
				priority += 3;

			return (priority);
		}
Ejemplo n.º 30
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
        }
		/// <summary>
		/// Confirm pixel format assigned to this surface. XXX
		/// </summary>
		/// <param name="pFormat">
		/// A <see cref="DevicePixelFormat"/> defining the available surface buffers
		/// and their definitions.
		/// </param>
		/// <remarks>
		/// This routine shall be called after a successfull call to SetPixelFormat.
		/// </remarks>
		public void SetBufferConfiguration(DevicePixelFormat pFormat)
		{
			// Determine available buffers
			mSurfaceBuffers = 0;
			if (pFormat.ColorBits > 0) {
				if (pFormat.SRGBCapable == false)
					mSurfaceBuffers |= BufferType.Color;
				else
					mSurfaceBuffers |= BufferType.ColorSRGB;
			}
			if (pFormat.DepthBits > 0)
				mSurfaceBuffers |= BufferType.Depth;
			if (pFormat.StencilBits > 0)
				mSurfaceBuffers |= BufferType.Stencil;
			if (pFormat.MultisampleBits > 0)
				mSurfaceBuffers |= BufferType.Multisample;
			if (pFormat.DoubleBuffer == true)
				mSurfaceBuffers |= BufferType.Double;
			if (pFormat.StereoBuffer == true)
				mSurfaceBuffers |= BufferType.Stereo;

			// Store buffer definitions
			switch (pFormat.ColorBits) {
				case 8:
					mColorType = PixelLayout.RGB8;
					break;
				case 16:
					mColorType = PixelLayout.RGB16;
					break;
				case 24:
					mColorType = PixelLayout.RGB24;
					break;
				case 32:
					mColorType = PixelLayout.RGBA32;
					break;
				default:
					throw new InvalidOperationException("invalid pixel format composed by " + pFormat.ColorBits + " bits");
			}
			mDepthBits = (uint)pFormat.DepthBits;
			mStencilBits = (uint)pFormat.StencilBits;
			mMultisampleBits = (uint)pFormat.MultisampleBits;
			mDoubleBuffers = pFormat.DoubleBuffer;
			mStereoBuffers = pFormat.StereoBuffer;
		}
Ejemplo n.º 32
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		public abstract void SetPixelFormat(DevicePixelFormat pixelFormat);
Ejemplo n.º 33
0
 /// <summary>
 /// Set the device pixel format.
 /// </summary>
 /// <param name="pixelFormat">
 /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
 /// </param>
 public abstract void ChoosePixelFormat(DevicePixelFormat pixelFormat);
Ejemplo n.º 34
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");

			
		}
Ejemplo n.º 35
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");

			// Note: GLX does not really need a SetPixelFormat operation

			// Framebuffer configuration
			FBConfig = pixelFormat.XFbConfig;
			// X Visual Information
			XVisualInfo = pixelFormat.XVisualInfo;
		}
Ejemplo n.º 36
0
 /// <summary>
 /// Set the device pixel format.
 /// </summary>
 /// <param name="pixelFormat">
 /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
 /// </param>
 public abstract void SetPixelFormat(DevicePixelFormat pixelFormat);
Ejemplo n.º 37
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");
			if (_PixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

			Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
			bool res;

			// Note (from MSDN): Setting the pixel format of a window more than once can lead to significant complications for the Window Manager
			// and for multithread applications, so it is not allowed. An application can only set the pixel format of a window one time. Once a
			// window's pixel format is set, it cannot be changed.

			res = Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor);
			Debug.Assert(res);

			// Set choosen pixel format
			res = Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor);
			Debug.Assert(res);

			// Unable to set pixel format again
			_PixelFormatSet = true;
		}