/// <summary> /// Creates a new graphics context. /// </summary> private void _createGraphicsContext(DevicePixelFormat pixelFormat) { _graphicsContext = GraphicsManager.CreateGraphicsContext(_getWindowHandle()); #region Set Pixel Format DevicePixelFormatCollection pixelFormats = _graphicsContext.PixelsFormats; System.Collections.Generic.List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(pixelFormat); if ((matchingPixelFormats.Count == 0) && pixelFormat.MultisampleBits > 0) { // Try to select the maximum multisample configuration int multisampleBits = 0; pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); }); pixelFormat.MultisampleBits = multisampleBits; matchingPixelFormats = pixelFormats.Choose(pixelFormat); } if ((matchingPixelFormats.Count == 0) && pixelFormat.DoubleBuffer) { // Try single buffered pixel formats pixelFormat.DoubleBuffer = false; matchingPixelFormats = pixelFormats.Choose(pixelFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat))); } } else if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat))); } _graphicsContext.SetPixelFormat(matchingPixelFormats[0]); #endregion // TODO: Platform specific extension checker if (WGL.IsExtensionSupported(_graphicsContext.DeviceHandle.Handle, WGL.EXT.SwapControl)) { // TODO: Handle tear WGL.SwapIntervalEXT(SwapInterval); } }
/// <summary> /// Create the device context and set the pixel format. /// </summary> private void CreateDeviceContext(DevicePixelFormat controlReqFormat) { #region Create device context _DeviceContext = DeviceContext.Create(GetDisplay(), GetWindowHandle()); _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 }
public void DevicePixelFormat_GuessChooseError() { DevicePixelFormatCollection pixelFormatCollection = new DevicePixelFormatCollection { // Hostile pixel format new DevicePixelFormat() }; string error; // No unsigned pixel format error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RgbaUnsigned = true, RgbaFloat = false }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No unsigned+float pixel format error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RgbaUnsigned = true, RgbaFloat = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No float pixel format error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RgbaUnsigned = false, RgbaFloat = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No render to window error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RenderWindow = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No render to P-Buffer error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RenderPBuffer = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No render to native buffer error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RenderBuffer = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought color bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { ColorBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Note: does rgba bits are collected in all platforms? // Not enought (red) color bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { RedBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought (green) color bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { GreenBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought (blue) color bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { BlueBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought (alpha) color bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { AlphaBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought depth bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { DepthBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought stencil bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { StencilBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Not enought multisample bits error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { MultisampleBits = 1 }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No double buffer error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { DoubleBuffer = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // No sRGB buffer error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat { SRGBCapable = true }); Assert.IsNotNull(error); Assert.AreNotEqual("no error", error); // Handle special case pixelFormatCollection = new DevicePixelFormatCollection { // Basic pixel format new DevicePixelFormat(24) }; error = pixelFormatCollection.GuessChooseError(new DevicePixelFormat(24)); Assert.IsNotNull(error); Assert.AreEqual("no error", error); }
/// <summary> /// Create the device context and set the pixel format. /// </summary> private void CreateDeviceContext(IntPtr displayHandle, IntPtr windowHandle, 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(displayHandle, windowHandle); 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( $"unable to find a suitable pixel format: {pixelFormats.GuessChooseError(controlReqFormat)}"); } } else if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException( $"unable to find a suitable pixel format: {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 }
public void DevicePixelFormat_GuessChooseError_Exceptions() { DevicePixelFormatCollection pixelFormatCollection = new DevicePixelFormatCollection(); Assert.Throws <ArgumentNullException>(() => pixelFormatCollection.GuessChooseError(null)); }