Fix mupen grabbing an OpenGL context based on whatever current version/flag attributes were present

This is global state that gets mucked by us since we also use SDL2 to handle OpenGL context management, Rice/Glide/Glidemk2 don't like core contexts due to them using old deprecated functionality
This commit is contained in:
CasualPokePlayer 2024-05-31 20:48:16 -07:00
parent e2ece89fd5
commit 0488bbd33a
8 changed files with 28 additions and 32 deletions

Binary file not shown.

View File

@ -636,6 +636,10 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
/* If we're in OpenGL mode, just create a stub surface and we're done! */
if (flags & SDL_OPENGL) {
// make sure to set version/flag attributes, those might have been changed from the defaults
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
if (!SDL_VideoContext) {
return NULL;

View File

@ -41,7 +41,7 @@ namespace BizHawk.Bizware.Graphics.Controls
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Context = new(Handle, 3, 2, true, false);
Context = new(Handle, 3, 2, true);
}
protected override void OnHandleDestroyed(EventArgs e)

View File

@ -30,17 +30,6 @@ namespace BizHawk.Bizware.Graphics
throw new($"Could not load default OpenGL library! SDL Error: {SDL_GetError()}");
}
// set some sensible defaults
SDL_GL_ResetAttributes();
if (SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_RED_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_GREEN_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_BLUE_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_ALPHA_SIZE, 0) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1) is not 0)
{
throw new($"Could not set GL attributes! SDL Error: {SDL_GetError()}");
}
// we will be turning a foreign window into an SDL window
// we need this so it knows that it is capable of using OpenGL functions
SDL_SetHint(SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL, "1");
@ -58,8 +47,19 @@ namespace BizHawk.Bizware.Graphics
private IntPtr _sdlWindow;
private IntPtr _glContext;
private void CreateContext(int majorVersion, int minorVersion, bool coreProfile, bool forwardCompatible)
private void CreateContext(int majorVersion, int minorVersion, bool coreProfile)
{
// set some sensible defaults
SDL_GL_ResetAttributes();
if (SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_RED_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_GREEN_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_BLUE_SIZE, 8) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_ALPHA_SIZE, 0) is not 0
|| SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1) is not 0)
{
throw new($"Could not set GL attributes! SDL Error: {SDL_GetError()}");
}
if (SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, majorVersion) != 0)
{
throw new($"Could not set GL Major Version! SDL Error: {SDL_GetError()}");
@ -70,13 +70,6 @@ namespace BizHawk.Bizware.Graphics
throw new($"Could not set GL Minor Version! SDL Error: {SDL_GetError()}");
}
// TODO: Debug flag / debug callback with DEBUG build
if (SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_FLAGS, forwardCompatible
? (int)SDL_GLcontext.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG : 0) != 0)
{
throw new($"Could not set GL Context Flags! SDL Error: {SDL_GetError()}");
}
#if DEBUG_OPENGL
if (SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_FLAGS, (int)SDL_GLcontext.SDL_GL_CONTEXT_DEBUG_FLAG) != 0)
{
@ -111,7 +104,7 @@ namespace BizHawk.Bizware.Graphics
#endif
}
public SDL2OpenGLContext(IntPtr nativeWindowhandle, int majorVersion, int minorVersion, bool coreProfile, bool forwardCompatible)
public SDL2OpenGLContext(IntPtr nativeWindowhandle, int majorVersion, int minorVersion, bool coreProfile)
{
_sdlWindow = SDL_CreateWindowFrom(nativeWindowhandle);
if (_sdlWindow == IntPtr.Zero)
@ -125,10 +118,10 @@ namespace BizHawk.Bizware.Graphics
throw new($"Could not set share context attribute! SDL Error: {SDL_GetError()}");
}
CreateContext(majorVersion, minorVersion, coreProfile, forwardCompatible);
CreateContext(majorVersion, minorVersion, coreProfile);
}
public SDL2OpenGLContext(int majorVersion, int minorVersion, bool coreProfile, bool forwardCompatible)
public SDL2OpenGLContext(int majorVersion, int minorVersion, bool coreProfile)
{
_sdlWindow = SDL_CreateWindow(null, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1, 1,
SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL_WindowFlags.SDL_WINDOW_HIDDEN);
@ -144,7 +137,7 @@ namespace BizHawk.Bizware.Graphics
throw new($"Could not set share context attribute! SDL Error: {SDL_GetError()}");
}
CreateContext(majorVersion, minorVersion, coreProfile, forwardCompatible);
CreateContext(majorVersion, minorVersion, coreProfile);
}
public void Dispose()

View File

@ -13,8 +13,8 @@ namespace BizHawk.Client.EmuHawk
public bool SupportsGLVersion(int major, int minor)
=> OpenGLVersion.SupportsVersion(major, minor);
public object RequestGLContext(int major, int minor, bool coreProfile, bool forwardCompatible)
=> new SDL2OpenGLContext(major, minor, coreProfile, forwardCompatible);
public object RequestGLContext(int major, int minor, bool coreProfile)
=> new SDL2OpenGLContext(major, minor, coreProfile);
public void ReleaseGLContext(object context)
=> ((SDL2OpenGLContext)context).Dispose();

View File

@ -14,13 +14,12 @@ namespace BizHawk.Emulation.Common
public bool SupportsGLVersion(int major, int minor);
/// <summary>
/// Requests an OpenGL context with specified major / minor
/// Requests an OpenGL context with specified major / minor version
/// The core profile can be requested (otherwise, the compatibility profile will be used)
/// The forward compatible bit can also be requested
/// The requested OpenGL context will be shared with the current context
/// Note: creating a context implicitly makes that created context current
/// </summary>
public object RequestGLContext(int major, int minor, bool coreProfile, bool forwardCompatible);
public object RequestGLContext(int major, int minor, bool coreProfile);
/// <summary>
/// Frees this OpenGL context

View File

@ -169,7 +169,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS
private IntPtr RequestGLContextCallback()
{
var context = _openGLProvider.RequestGLContext(4, 3, true, false);
var context = _openGLProvider.RequestGLContext(4, 3, true);
_glContexts.Add(context);
var handle = GCHandle.Alloc(context, GCHandleType.Weak);
return GCHandle.ToIntPtr(handle);

View File

@ -162,7 +162,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
}
else
{
_glContext = _openGLProvider.RequestGLContext(majorGlVersion, minorGlVersion, true, false);
_glContext = _openGLProvider.RequestGLContext(majorGlVersion, minorGlVersion, true);
}
}
@ -174,7 +174,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
}
else
{
_glContext = _openGLProvider.RequestGLContext(3, 1, true, false);
_glContext = _openGLProvider.RequestGLContext(3, 1, true);
}
}