Permalink
Browse files

[Max] Use RelaxGraphicsMode to find optimal mode

  • Loading branch information...
1 parent 6ba475e commit 0575e63a275aba44aa83882e6fbe8b813ff25ee9 @thefiddler thefiddler committed Jan 22, 2014
Showing with 55 additions and 44 deletions.
  1. +17 −39 Source/OpenTK/Platform/MacOS/AglContext.cs
  2. +38 −5 Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs
@@ -52,8 +52,6 @@ class AglContext : DesktopGraphicsContext
DisplayDevice device;
bool mIsFullscreen = false;
- readonly MacOSGraphicsMode ModeSelector = new MacOSGraphicsMode();
-
public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext)
{
Debug.Print("Context Type: {0}", shareContext);
@@ -103,58 +101,38 @@ private void AddPixelAttrib(List<int> aglAttributes, Agl.PixelFormatAttribute pi
aglAttributes.Add((int)pixelFormatAttribute);
aglAttributes.Add(value);
}
+
void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool fullscreen)
{
Debug.Print("AGL pixel format attributes:");
AGLPixelFormat myAGLPixelFormat;
// Choose a pixel format with the attributes we specified.
- if (fullscreen)
- {
- IntPtr gdevice;
- IntPtr cgdevice = GetQuartzDevice(carbonWindow);
-
- if (cgdevice == IntPtr.Zero)
- cgdevice = (IntPtr)DisplayDevice.Default.Id;
+ IntPtr gdevice;
+ IntPtr cgdevice = GetQuartzDevice(carbonWindow);
- OSStatus status = Carbon.API.DMGetGDeviceByDisplayID(cgdevice, out gdevice, false);
+ if (cgdevice == IntPtr.Zero)
+ cgdevice = (IntPtr)DisplayDevice.Default.Id;
- if (status != OSStatus.NoError)
- throw new MacOSException(status, "DMGetGDeviceByDisplayID failed.");
+ OSStatus status = Carbon.API.DMGetGDeviceByDisplayID(cgdevice, out gdevice, false);
- myAGLPixelFormat = ModeSelector.SelectPixelFormat(
- mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples,
- mode.AccumulatorFormat, mode.Buffers, mode.Stereo,
- true, gdevice);
-
- Agl.AglError err = Agl.GetError();
- if (myAGLPixelFormat == IntPtr.Zero || err == Agl.AglError.BadPixelFormat)
- {
- Debug.Print("Failed to create full screen pixel format.");
- Debug.Print("Trying again to create a non-fullscreen pixel format.");
-
- CreateContext(mode, carbonWindow, shareContextRef, false);
- return;
- }
- }
- else
- {
- myAGLPixelFormat = ModeSelector.SelectPixelFormat(
- mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples,
- mode.AccumulatorFormat, mode.Buffers, mode.Stereo,
- false, IntPtr.Zero);
- MyAGLReportError("aglChoosePixelFormat");
- }
-
+ if (status != OSStatus.NoError)
+ throw new MacOSException(status, "DMGetGDeviceByDisplayID failed.");
+
+ IGraphicsMode selector = new MacOSGraphicsMode(gdevice);
+ Mode = selector.SelectGraphicsMode(
+ mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples,
+ mode.AccumulatorFormat, mode.Buffers, mode.Stereo);
+ MyAGLReportError("aglChoosePixelFormat");
+
Debug.Print("Creating AGL context. Sharing with {0}", shareContextRef);
-
+ myAGLPixelFormat = Mode.Index.Value;
+
// create the context and share it with the share reference.
Handle = new ContextHandle(Agl.aglCreateContext(myAGLPixelFormat, shareContextRef));
MyAGLReportError("aglCreateContext");
- Mode = ModeSelector.GetGraphicsModeFromPixelFormat(myAGLPixelFormat);
-
// Free the pixel format from memory.
Agl.aglDestroyPixelFormat(myAGLPixelFormat);
MyAGLReportError("aglDestroyPixelFormat");
@@ -37,22 +37,55 @@ namespace OpenTK.Platform.MacOS
class MacOSGraphicsMode : IGraphicsMode
{
+ readonly IntPtr Device;
+
+ public MacOSGraphicsMode(IntPtr device)
+ {
+ Device = device;
+ }
+
#region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil,
int samples, ColorFormat accum, int buffers, bool stereo)
{
- IntPtr pixelformat = SelectPixelFormat(
- color, depth, stencil, samples, accum, buffers, stereo,
- false, IntPtr.Zero);
+ IntPtr pixelformat;
+ do
+ {
+ pixelformat = SelectPixelFormat(
+ color, depth, stencil, samples, accum, buffers, stereo,
+ true, Device);
+
+ Agl.AglError err = Agl.GetError();
+ if (pixelformat == IntPtr.Zero || err == Agl.AglError.BadPixelFormat)
+ {
+ Debug.Print("Failed to create full screen pixel format.");
+ Debug.Print("Trying again to create a non-fullscreen pixel format.");
+ pixelformat = SelectPixelFormat(
+ color, depth, stencil, samples, accum, buffers, stereo,
+ false, IntPtr.Zero);
+ }
+
+ if (pixelformat == IntPtr.Zero)
+ {
+ if (!Utilities.RelaxGraphicsMode(
+ ref color, ref depth, ref stencil, ref samples, ref accum,
+ ref buffers, ref stereo))
+ {
+ throw new GraphicsModeException("Requested GraphicsMode not available.");
+ }
+ }
+ }
+ while (pixelformat == IntPtr.Zero);
+
return GetGraphicsModeFromPixelFormat(pixelformat);
}
#endregion
#region Internal Members
- internal GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat)
+ GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat)
{
int r, g, b, a;
Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_RED_SIZE, out r);
@@ -75,7 +108,7 @@ internal GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat)
depth, stencil, samples, new ColorFormat(ar, ag, ab, aa), buffers + 1, stereo != 0);
}
- internal IntPtr SelectPixelFormat(ColorFormat color, int depth, int stencil, int samples,
+ IntPtr SelectPixelFormat(ColorFormat color, int depth, int stencil, int samples,
ColorFormat accum, int buffers, bool stereo, bool fullscreen, IntPtr device)
{
List<int> attribs = new List<int>();

0 comments on commit 0575e63

Please sign in to comment.