// Note: there is no relevant ARB function. internal static GraphicsMode SetGraphicsModePFD(WinGraphicsMode mode_selector, GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (!mode.Index.HasValue) { mode = mode_selector.SelectGraphicsMode( mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo); } PixelFormatDescriptor pfd = new PixelFormatDescriptor(); Functions.DescribePixelFormat( window.DeviceContext, (int)mode.Index.Value, API.PixelFormatDescriptorSize, ref pfd); Debug.WriteLine(mode.Index.ToString()); if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref pfd)) { throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } return(mode); }
// Note: there is no relevant ARB function. internal static void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); if (!mode.Index.HasValue) { throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); } if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } PixelFormatDescriptor pfd = new PixelFormatDescriptor(); Functions.DescribePixelFormat(window.DeviceContext, (int)mode.Index.Value, API.PixelFormatDescriptorSize, ref pfd); Debug.WriteLine(mode.Index.ToString()); if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref pfd)) { throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } }
private void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { if (!mode.Index.HasValue) { throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); } if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } PixelFormatDescriptor formatDescriptor = new PixelFormatDescriptor(); Functions.DescribePixelFormat(window.DeviceContext, (int)mode.Index.Value, (int)API.PixelFormatDescriptorSize, ref formatDescriptor); if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref formatDescriptor)) { throw new GraphicsContextException(string.Format("Requested GraphicsMode not available. SetPixelFormat error: {0}", (object)Marshal.GetLastWin32Error())); } }
static GraphicsMode DescribePixelFormatPFD(IntPtr device, ref PixelFormatDescriptor pfd, int pixelformat) { GraphicsMode created_mode = null; if (Functions.DescribePixelFormat(device, pixelformat, pfd.Size, ref pfd) > 0) { created_mode = new GraphicsMode( new IntPtr(pixelformat), new ColorFormat(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, 0, // MSAA not supported when using PixelFormatDescriptor new ColorFormat(pfd.AccumRedBits, pfd.AccumGreenBits, pfd.AccumBlueBits, pfd.AccumAlphaBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0); } return(created_mode); }
GraphicsMode ChoosePixelFormatPFD(IntPtr device, GraphicsMode mode, AccelerationType requested_acceleration_type) { PixelFormatDescriptor pfd = new PixelFormatDescriptor(); PixelFormatDescriptorFlags flags = 0; flags |= PixelFormatDescriptorFlags.DRAW_TO_WINDOW; flags |= PixelFormatDescriptorFlags.SUPPORT_OPENGL; if (mode.Stereo) { flags |= PixelFormatDescriptorFlags.STEREO; } if (System.Environment.OSVersion.Version.Major >= 6 && requested_acceleration_type != AccelerationType.None) { // Request a compositor-capable mode when running on // Vista+ and using hardware acceleration. Without this, // some modes will cause the compositor to turn off, // which is very annoying to the user. // Note: compositor-capable modes require hardware // acceleration. Don't set this flag when running // with software acceleration (e.g. over Remote Desktop // as described in bug https://github.com/opentk/opentk/issues/35) flags |= PixelFormatDescriptorFlags.SUPPORT_COMPOSITION; } int count = Functions.DescribePixelFormat(device, 1, API.PixelFormatDescriptorSize, ref pfd); int best = 0; int best_dist = int.MaxValue; for (int index = 1; index <= count; index++) { int dist = 0; bool valid = Functions.DescribePixelFormat(device, index, API.PixelFormatDescriptorSize, ref pfd) != 0; valid &= GetAccelerationType(ref pfd) == requested_acceleration_type; valid &= (pfd.Flags & flags) == flags; valid &= pfd.PixelType == PixelType.RGBA; // indexed modes not currently supported // heavily penalize single-buffered modes when the user requests double buffering if ((pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) == 0 && mode.Buffers > 1) { dist += 1000; } valid &= Compare(pfd.ColorBits, mode.ColorFormat.BitsPerPixel, ref dist); valid &= Compare(pfd.RedBits, mode.ColorFormat.Red, ref dist); valid &= Compare(pfd.GreenBits, mode.ColorFormat.Green, ref dist); valid &= Compare(pfd.BlueBits, mode.ColorFormat.Blue, ref dist); valid &= Compare(pfd.AlphaBits, mode.ColorFormat.Alpha, ref dist); valid &= Compare(pfd.AccumBits, mode.AccumulatorFormat.BitsPerPixel, ref dist); valid &= Compare(pfd.AccumRedBits, mode.AccumulatorFormat.Red, ref dist); valid &= Compare(pfd.AccumGreenBits, mode.AccumulatorFormat.Green, ref dist); valid &= Compare(pfd.AccumBlueBits, mode.AccumulatorFormat.Blue, ref dist); valid &= Compare(pfd.AccumAlphaBits, mode.AccumulatorFormat.Alpha, ref dist); valid &= Compare(pfd.DepthBits, mode.Depth, ref dist); valid &= Compare(pfd.StencilBits, mode.Stencil, ref dist); if (valid && dist < best_dist) { best = index; best_dist = dist; } } return(DescribePixelFormatPFD(device, ref pfd, best)); }
GraphicsMode SelectGraphicsModePFD(ColorDepth color, int depth, int stencil, int samples, ColorDepth accum, int buffers, bool stereo) { using (Control native_window = new Control()) using (WinWindowInfo window = new WinWindowInfo(native_window.Handle, null)) { IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext; Debug.WriteLine(String.Format("Device context: {0}", deviceContext)); Debug.Write("Selecting pixel format... "); PixelFormatDescriptor pixelFormat = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; pixelFormat.Flags = PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW; pixelFormat.ColorBits = (byte)(color.Red + color.Green + color.Blue); pixelFormat.PixelType = color.IsIndexed ? PixelType.INDEXED : PixelType.RGBA; pixelFormat.RedBits = (byte)color.Red; pixelFormat.GreenBits = (byte)color.Green; pixelFormat.BlueBits = (byte)color.Blue; pixelFormat.AlphaBits = (byte)color.Alpha; if (accum.BitsPerPixel > 0) { pixelFormat.AccumBits = (byte)(accum.Red + accum.Green + accum.Blue); pixelFormat.AccumRedBits = (byte)accum.Red; pixelFormat.AccumGreenBits = (byte)accum.Green; pixelFormat.AccumBlueBits = (byte)accum.Blue; pixelFormat.AccumAlphaBits = (byte)accum.Alpha; } pixelFormat.DepthBits = (byte)depth; pixelFormat.StencilBits = (byte)stencil; if (depth <= 0) { pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE; } if (stereo) { pixelFormat.Flags |= PixelFormatDescriptorFlags.STEREO; } if (buffers > 1) { pixelFormat.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER; } int pixel = Functions.ChoosePixelFormat(deviceContext, ref pixelFormat); if (pixel == 0) { throw new GraphicsModeException("The requested GraphicsMode is not available."); } // Find out what we really got as a format: PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; Functions.DescribePixelFormat(deviceContext, pixel, API.PixelFormatDescriptorSize, ref pfd); GraphicsMode fmt = new GraphicsMode((IntPtr)pixel, new ColorDepth(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, 0, new ColorDepth(pfd.AccumBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0); return(fmt); } }