From 417a6bedc16278dacb446d4b134a228afa947abd Mon Sep 17 00:00:00 2001 From: thefiddler Date: Wed, 22 Jan 2014 11:37:37 +0100 Subject: [PATCH] [SDL2] Use RelaxGraphicsMode to find optimal mode For SDL2, RelaxGraphicsMode requires us to clear context attributes between consecutive attempts. This is implemented by calling ClearGLAttributes(). --- .../Platform/SDL2/Sdl2GraphicsContext.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Source/OpenTK/Platform/SDL2/Sdl2GraphicsContext.cs b/Source/OpenTK/Platform/SDL2/Sdl2GraphicsContext.cs index c7714b58..75818496 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2GraphicsContext.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2GraphicsContext.cs @@ -63,8 +63,20 @@ namespace OpenTK.Platform.SDL2 { lock (SDL.Sync) { - SetGLAttributes(mode, shareContext, major, minor, flags); - SdlContext = new ContextHandle(SDL.GL.CreateContext(Window.Handle)); + bool retry = false; + do + { + SetGLAttributes(mode, shareContext, major, minor, flags); + SdlContext = new ContextHandle(SDL.GL.CreateContext(Window.Handle)); + + // If we failed to create a valid context, relax the GraphicsMode + // and try again. + retry = + SdlContext == ContextHandle.Zero && + Utilities.RelaxGraphicsMode(ref mode); + } + while (retry); + if (SdlContext == ContextHandle.Zero) { var error = SDL.GetError(); @@ -152,12 +164,37 @@ namespace OpenTK.Platform.SDL2 stereo != 0 ? true : false); } + static void ClearGLAttributes() + { + SDL.GL.SetAttribute(ContextAttribute.ACCUM_ALPHA_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.ACCUM_RED_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.ACCUM_GREEN_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.ACCUM_BLUE_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.DOUBLEBUFFER, 0); + SDL.GL.SetAttribute(ContextAttribute.ALPHA_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.RED_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.GREEN_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.BLUE_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.DEPTH_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.MULTISAMPLEBUFFERS, 0); + SDL.GL.SetAttribute(ContextAttribute.MULTISAMPLESAMPLES, 0); + SDL.GL.SetAttribute(ContextAttribute.STENCIL_SIZE, 0); + SDL.GL.SetAttribute(ContextAttribute.STEREO, 0); + SDL.GL.SetAttribute(ContextAttribute.CONTEXT_MAJOR_VERSION, 1); + SDL.GL.SetAttribute(ContextAttribute.CONTEXT_MINOR_VERSION, 0); + SDL.GL.SetAttribute(ContextAttribute.CONTEXT_FLAGS, 0); + SDL.GL.SetAttribute(ContextAttribute.CONTEXT_EGL, 0); + SDL.GL.SetAttribute(ContextAttribute.CONTEXT_PROFILE_MASK, 0); + SDL.GL.SetAttribute(ContextAttribute.SHARE_WITH_CURRENT_CONTEXT, 0); + } + static void SetGLAttributes(GraphicsMode mode, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags) { ContextProfileFlags cpflags = 0; + ClearGLAttributes(); if (mode.AccumulatorFormat.BitsPerPixel > 0) {