diff --git a/MonoGame.Framework/Platform/Graphics/OpenGL.cs b/MonoGame.Framework/Platform/Graphics/OpenGL.cs index b3a0bd181fc..23e2c4726e1 100644 --- a/MonoGame.Framework/Platform/Graphics/OpenGL.cs +++ b/MonoGame.Framework/Platform/Graphics/OpenGL.cs @@ -1258,6 +1258,15 @@ static void DebugMessageCallbackHandler(int source, int type, int id, int severi internal static int SwapInterval { get; set; } + internal unsafe static int GetMaxSamples() + { + GetIntegerv = LoadFunction ("glGetIntegerv"); + + int maxSamples = 0; + GetIntegerv((int)GetPName.MaxSamples, &maxSamples); + return maxSamples; + } + internal static void LoadEntryPoints () { LoadPlatformEntryPoints (); @@ -1455,7 +1464,7 @@ internal static void LoadExtensions() GL.LoadFrameBufferObjectEXTEntryPoints(); } if (GL.RenderbufferStorageMultisample == null) - { + { if (Extensions.Contains("GL_APPLE_framebuffer_multisample")) { GL.RenderbufferStorageMultisample = LoadFunction("glRenderbufferStorageMultisampleAPPLE"); diff --git a/MonoGame.Framework/Platform/GraphicsDeviceManager.SDL.cs b/MonoGame.Framework/Platform/GraphicsDeviceManager.SDL.cs index 1045b4f7e22..12bc052a0cd 100644 --- a/MonoGame.Framework/Platform/GraphicsDeviceManager.SDL.cs +++ b/MonoGame.Framework/Platform/GraphicsDeviceManager.SDL.cs @@ -2,7 +2,9 @@ // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. +using System; using Microsoft.Xna.Framework.Graphics; +using MonoGame.OpenGL; namespace Microsoft.Xna.Framework { @@ -45,8 +47,24 @@ partial void PlatformInitialize(PresentationParameters presentationParameters) if (presentationParameters.MultiSampleCount > 0) { + var temporaryWindow = Sdl.Window.Create( + "glContextInfoWindow", + 0, + 0, + 0, + 0, + Sdl.Window.State.OpenGL | + Sdl.Window.State.Hidden | + Sdl.Window.State.InputFocus | + Sdl.Window.State.MouseFocus + ); + var temporaryGLContext = Sdl.GL.CreateContext(temporaryWindow); + var maxSamples = GL.GetMaxSamples(); + Sdl.GL.DeleteContext(temporaryGLContext); + Sdl.Window.Destroy(temporaryWindow); + Sdl.GL.SetAttribute(Sdl.GL.Attribute.MultiSampleBuffers, 1); - Sdl.GL.SetAttribute(Sdl.GL.Attribute.MultiSampleSamples, presentationParameters.MultiSampleCount); + Sdl.GL.SetAttribute(Sdl.GL.Attribute.MultiSampleSamples, Math.Min(maxSamples, presentationParameters.MultiSampleCount)); } ((SdlGameWindow)SdlGameWindow.Instance).CreateWindow(); diff --git a/MonoGame.Framework/Platform/SDL/SDLGameWindow.cs b/MonoGame.Framework/Platform/SDL/SDLGameWindow.cs index f5e5d1820fc..93d48cf25ec 100644 --- a/MonoGame.Framework/Platform/SDL/SDLGameWindow.cs +++ b/MonoGame.Framework/Platform/SDL/SDLGameWindow.cs @@ -3,11 +3,13 @@ // file 'LICENSE.txt', which is part of this source code package. using System; +using System.Diagnostics; using System.IO; using System.Reflection; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MonoGame.Framework.Utilities; +using MonoGame.OpenGL; namespace Microsoft.Xna.Framework { @@ -133,6 +135,12 @@ public SdlGameWindow(Game game) _handle = Sdl.Window.Create("", 0, 0, GraphicsDeviceManager.DefaultBackBufferWidth, GraphicsDeviceManager.DefaultBackBufferHeight, Sdl.Window.State.Hidden | Sdl.Window.State.FullscreenDesktop); + + if (_handle == default) + { + var sdlError = Sdl.GetError(); + throw new NoSuitableGraphicsDeviceException(sdlError); + } } internal void CreateWindow() @@ -164,6 +172,12 @@ internal void CreateWindow() winx, winy, _width, _height, initflags ); + if (_handle == default) + { + var sdlError = Sdl.GetError(); + throw new NoSuitableGraphicsDeviceException(sdlError); + } + Id = Sdl.Window.GetWindowId(_handle); if (_icon != IntPtr.Zero)