Permalink
Browse files

[Win] Use RelaxGraphicsMode to find optimal mode

  • Loading branch information...
thefiddler committed Jan 22, 2014
1 parent 615df92 commit 6ba475e23fd0c7ed0a7409b8a0ee55f9c97ea99f
Showing with 106 additions and 97 deletions.
  1. +106 −97 Source/OpenTK/Platform/Windows/WinGraphicsMode.cs
@@ -96,114 +96,123 @@ public WinGraphicsMode(IntPtr device)
// hardware acceleration (e.g. we are running in a VM or in a remote desktop
// connection), this method will return 0 formats and we will fall back to
// ChoosePixelFormatPFD.
- GraphicsMode ChoosePixelFormatARB(IntPtr device, GraphicsMode mode)
+ GraphicsMode ChoosePixelFormatARB(IntPtr device, GraphicsMode desired_mode)
{
GraphicsMode created_mode = null;
+ GraphicsMode mode = new GraphicsMode(desired_mode);
if (Wgl.SupportsExtension("WGL_ARB_pixel_format") &&
Wgl.SupportsFunction("wglChoosePixelFormatARB"))
{
+ int[] format = new int[1];
+ int count;
List<int> attributes = new List<int>();
- attributes.Add((int)WGL_ARB_pixel_format.AccelerationArb);
- attributes.Add((int)WGL_ARB_pixel_format.FullAccelerationArb);
-
- attributes.Add((int)WGL_ARB_pixel_format.DrawToWindowArb);
- attributes.Add(1);
+ bool retry = false;
- if (mode.ColorFormat.Red > 0)
+ do
{
- attributes.Add((int)WGL_ARB_pixel_format.RedBitsArb);
- attributes.Add(mode.ColorFormat.Red);
- }
-
- if (mode.ColorFormat.Green > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.GreenBitsArb);
- attributes.Add(mode.ColorFormat.Green);
- }
-
- if (mode.ColorFormat.Blue > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.BlueBitsArb);
- attributes.Add(mode.ColorFormat.Blue);
- }
-
- if (mode.ColorFormat.Alpha > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.AlphaBitsArb);
- attributes.Add(mode.ColorFormat.Alpha);
- }
-
- if (mode.Depth > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.DepthBitsArb);
- attributes.Add(mode.Depth);
- }
-
- if (mode.Stencil > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.StencilBitsArb);
- attributes.Add(mode.Stencil);
- }
-
- if (mode.AccumulatorFormat.Red > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.AccumRedBitsArb);
- attributes.Add(mode.AccumulatorFormat.Red);
- }
-
- if (mode.AccumulatorFormat.Green > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.AccumGreenBitsArb);
- attributes.Add(mode.AccumulatorFormat.Green);
- }
-
- if (mode.AccumulatorFormat.Blue > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.AccumBlueBitsArb);
- attributes.Add(mode.AccumulatorFormat.Blue);
- }
-
- if (mode.AccumulatorFormat.Alpha > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.AccumAlphaBitsArb);
- attributes.Add(mode.AccumulatorFormat.Alpha);
- }
-
- if (mode.Samples > 0 &&
- Wgl.SupportsExtension("WGL_ARB_multisample"))
- {
- attributes.Add((int)WGL_ARB_multisample.SampleBuffersArb);
- attributes.Add(1);
- attributes.Add((int)WGL_ARB_multisample.SamplesArb);
- attributes.Add(mode.Samples);
- }
-
- if (mode.Buffers > 0)
- {
- attributes.Add((int)WGL_ARB_pixel_format.DoubleBufferArb);
- attributes.Add(mode.Buffers > 1 ? 1 : 0);
- }
-
- if (mode.Stereo)
- {
- attributes.Add((int)WGL_ARB_pixel_format.StereoArb);
+ attributes.Clear();
+ attributes.Add((int)WGL_ARB_pixel_format.AccelerationArb);
+ attributes.Add((int)WGL_ARB_pixel_format.FullAccelerationArb);
+ attributes.Add((int)WGL_ARB_pixel_format.DrawToWindowArb);
attributes.Add(1);
- }
-
- attributes.Add(0);
- attributes.Add(0);
- int[] format = new int[1];
- int count;
- if (Wgl.Arb.ChoosePixelFormat(device, attributes.ToArray(), null, format.Length, format, out count)
- && count > 0)
- {
- created_mode = DescribePixelFormatARB(device, format[0]);
- }
- else
- {
- Debug.Print("[WGL] ChoosePixelFormatARB failed with {0}", Marshal.GetLastWin32Error());
+ if (mode.ColorFormat.Red > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.RedBitsArb);
+ attributes.Add(mode.ColorFormat.Red);
+ }
+
+ if (mode.ColorFormat.Green > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.GreenBitsArb);
+ attributes.Add(mode.ColorFormat.Green);
+ }
+
+ if (mode.ColorFormat.Blue > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.BlueBitsArb);
+ attributes.Add(mode.ColorFormat.Blue);
+ }
+
+ if (mode.ColorFormat.Alpha > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.AlphaBitsArb);
+ attributes.Add(mode.ColorFormat.Alpha);
+ }
+
+ if (mode.Depth > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.DepthBitsArb);
+ attributes.Add(mode.Depth);
+ }
+
+ if (mode.Stencil > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.StencilBitsArb);
+ attributes.Add(mode.Stencil);
+ }
+
+ if (mode.AccumulatorFormat.Red > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.AccumRedBitsArb);
+ attributes.Add(mode.AccumulatorFormat.Red);
+ }
+
+ if (mode.AccumulatorFormat.Green > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.AccumGreenBitsArb);
+ attributes.Add(mode.AccumulatorFormat.Green);
+ }
+
+ if (mode.AccumulatorFormat.Blue > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.AccumBlueBitsArb);
+ attributes.Add(mode.AccumulatorFormat.Blue);
+ }
+
+ if (mode.AccumulatorFormat.Alpha > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.AccumAlphaBitsArb);
+ attributes.Add(mode.AccumulatorFormat.Alpha);
+ }
+
+ if (mode.Samples > 0 &&
+ Wgl.SupportsExtension("WGL_ARB_multisample"))
+ {
+ attributes.Add((int)WGL_ARB_multisample.SampleBuffersArb);
+ attributes.Add(1);
+ attributes.Add((int)WGL_ARB_multisample.SamplesArb);
+ attributes.Add(mode.Samples);
+ }
+
+ if (mode.Buffers > 0)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.DoubleBufferArb);
+ attributes.Add(mode.Buffers > 1 ? 1 : 0);
+ }
+
+ if (mode.Stereo)
+ {
+ attributes.Add((int)WGL_ARB_pixel_format.StereoArb);
+ attributes.Add(1);
+ }
+
+ attributes.Add(0);
+ attributes.Add(0);
+
+ if (Wgl.Arb.ChoosePixelFormat(device, attributes.ToArray(), null, format.Length, format, out count)
+ && count > 0)
+ {
+ created_mode = DescribePixelFormatARB(device, format[0]);
+ retry = false;
+ }
+ else
+ {
+ Debug.Print("[WGL] ChoosePixelFormatARB failed with {0}", Marshal.GetLastWin32Error());
+ retry = Utilities.RelaxGraphicsMode(ref mode);
+ }
}
+ while (retry);
}
else
{

0 comments on commit 6ba475e

Please sign in to comment.