Skip to content

Commit

Permalink
fix: query GL_MAX_SAMPLES before setting multisampling hint
Browse files Browse the repository at this point in the history
- adds better located exceptions to catch things like couldnt find matching glx visual"
- creates and disposes of temporary gl-enabled window and gl context to grab GL_MAX_SAMPLES from
  • Loading branch information
lodicolo committed Oct 30, 2023
1 parent a91dcc7 commit c7dbee9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion MonoGame.Framework/Platform/Graphics/OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetIntegerDelegate> ("glGetIntegerv");

int maxSamples = 0;
GetIntegerv((int)GetPName.MaxSamples, &maxSamples);
return maxSamples;
}

internal static void LoadEntryPoints ()
{
LoadPlatformEntryPoints ();
Expand Down Expand Up @@ -1455,7 +1464,7 @@ internal static void LoadExtensions()
GL.LoadFrameBufferObjectEXTEntryPoints();
}
if (GL.RenderbufferStorageMultisample == null)
{
{
if (Extensions.Contains("GL_APPLE_framebuffer_multisample"))
{
GL.RenderbufferStorageMultisample = LoadFunction<GL.RenderbufferStorageMultisampleDelegate>("glRenderbufferStorageMultisampleAPPLE");
Expand Down
20 changes: 19 additions & 1 deletion MonoGame.Framework/Platform/GraphicsDeviceManager.SDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions MonoGame.Framework/Platform/SDL/SDLGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c7dbee9

Please sign in to comment.