[Max] Use RelaxGraphicsMode to find optimal mode

This commit is contained in:
thefiddler 2014-01-22 11:03:40 +01:00
parent 6ba475e23f
commit 0575e63a27
2 changed files with 55 additions and 44 deletions

View file

@ -52,8 +52,6 @@ namespace OpenTK.Platform.MacOS
DisplayDevice device; DisplayDevice device;
bool mIsFullscreen = false; bool mIsFullscreen = false;
readonly MacOSGraphicsMode ModeSelector = new MacOSGraphicsMode();
public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext) public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext)
{ {
Debug.Print("Context Type: {0}", shareContext); Debug.Print("Context Type: {0}", shareContext);
@ -103,6 +101,7 @@ namespace OpenTK.Platform.MacOS
aglAttributes.Add((int)pixelFormatAttribute); aglAttributes.Add((int)pixelFormatAttribute);
aglAttributes.Add(value); aglAttributes.Add(value);
} }
void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool fullscreen) void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool fullscreen)
{ {
Debug.Print("AGL pixel format attributes:"); Debug.Print("AGL pixel format attributes:");
@ -110,51 +109,30 @@ namespace OpenTK.Platform.MacOS
AGLPixelFormat myAGLPixelFormat; AGLPixelFormat myAGLPixelFormat;
// Choose a pixel format with the attributes we specified. // Choose a pixel format with the attributes we specified.
if (fullscreen) IntPtr gdevice;
{ IntPtr cgdevice = GetQuartzDevice(carbonWindow);
IntPtr gdevice;
IntPtr cgdevice = GetQuartzDevice(carbonWindow);
if (cgdevice == IntPtr.Zero) if (cgdevice == IntPtr.Zero)
cgdevice = (IntPtr)DisplayDevice.Default.Id; cgdevice = (IntPtr)DisplayDevice.Default.Id;
OSStatus status = Carbon.API.DMGetGDeviceByDisplayID(cgdevice, out gdevice, false); OSStatus status = Carbon.API.DMGetGDeviceByDisplayID(cgdevice, out gdevice, false);
if (status != OSStatus.NoError) if (status != OSStatus.NoError)
throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); throw new MacOSException(status, "DMGetGDeviceByDisplayID failed.");
myAGLPixelFormat = ModeSelector.SelectPixelFormat( IGraphicsMode selector = new MacOSGraphicsMode(gdevice);
mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, Mode = selector.SelectGraphicsMode(
mode.AccumulatorFormat, mode.Buffers, mode.Stereo, mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples,
true, gdevice); mode.AccumulatorFormat, mode.Buffers, mode.Stereo);
MyAGLReportError("aglChoosePixelFormat");
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");
}
Debug.Print("Creating AGL context. Sharing with {0}", shareContextRef); Debug.Print("Creating AGL context. Sharing with {0}", shareContextRef);
myAGLPixelFormat = Mode.Index.Value;
// create the context and share it with the share reference. // create the context and share it with the share reference.
Handle = new ContextHandle(Agl.aglCreateContext(myAGLPixelFormat, shareContextRef)); Handle = new ContextHandle(Agl.aglCreateContext(myAGLPixelFormat, shareContextRef));
MyAGLReportError("aglCreateContext"); MyAGLReportError("aglCreateContext");
Mode = ModeSelector.GetGraphicsModeFromPixelFormat(myAGLPixelFormat);
// Free the pixel format from memory. // Free the pixel format from memory.
Agl.aglDestroyPixelFormat(myAGLPixelFormat); Agl.aglDestroyPixelFormat(myAGLPixelFormat);
MyAGLReportError("aglDestroyPixelFormat"); MyAGLReportError("aglDestroyPixelFormat");

View file

@ -37,14 +37,47 @@ namespace OpenTK.Platform.MacOS
class MacOSGraphicsMode : IGraphicsMode class MacOSGraphicsMode : IGraphicsMode
{ {
readonly IntPtr Device;
public MacOSGraphicsMode(IntPtr device)
{
Device = device;
}
#region IGraphicsMode Members #region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil, public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil,
int samples, ColorFormat accum, int buffers, bool stereo) int samples, ColorFormat accum, int buffers, bool stereo)
{ {
IntPtr pixelformat = SelectPixelFormat( IntPtr pixelformat;
color, depth, stencil, samples, accum, buffers, stereo, do
false, IntPtr.Zero); {
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); return GetGraphicsModeFromPixelFormat(pixelformat);
} }
@ -52,7 +85,7 @@ namespace OpenTK.Platform.MacOS
#region Internal Members #region Internal Members
internal GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat) GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat)
{ {
int r, g, b, a; int r, g, b, a;
Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_RED_SIZE, out r); Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_RED_SIZE, out r);
@ -75,7 +108,7 @@ namespace OpenTK.Platform.MacOS
depth, stencil, samples, new ColorFormat(ar, ag, ab, aa), buffers + 1, stereo != 0); 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) ColorFormat accum, int buffers, bool stereo, bool fullscreen, IntPtr device)
{ {
List<int> attribs = new List<int>(); List<int> attribs = new List<int>();