From 86761b84b67f3976b10ff56a3fcd6695caeef2fa Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Thu, 25 Jun 2026 13:11:36 +0200 Subject: [PATCH] remove OpenGLProvider and add project reference instead --- .../GraphicsImplementations/OpenGLProvider.cs | 29 ------------ src/BizHawk.Client.EmuHawk/MainForm.cs | 3 +- src/BizHawk.Emulation.Common/CoreComms.cs | 9 +--- .../Interfaces/IOpenGLProvider.cs | 44 ------------------- .../BizHawk.Emulation.Cores.csproj | 1 + .../AmstradCPC/Media/Disk/FloppyDisk.cs | 7 ++- .../SinclairSpectrum/Media/Disk/FloppyDisk.cs | 7 ++- .../Consoles/Nintendo/3DS/Encore.IEmulator.cs | 2 +- .../Consoles/Nintendo/3DS/Encore.cs | 23 +++++----- .../Nintendo/NDS/MelonDS.ISettable.cs | 2 +- .../Consoles/Nintendo/NDS/MelonDS.cs | 34 +++++--------- 11 files changed, 33 insertions(+), 128 deletions(-) delete mode 100644 src/BizHawk.Client.EmuHawk/GraphicsImplementations/OpenGLProvider.cs delete mode 100644 src/BizHawk.Emulation.Common/Interfaces/IOpenGLProvider.cs diff --git a/src/BizHawk.Client.EmuHawk/GraphicsImplementations/OpenGLProvider.cs b/src/BizHawk.Client.EmuHawk/GraphicsImplementations/OpenGLProvider.cs deleted file mode 100644 index b4793c7e8d4..00000000000 --- a/src/BizHawk.Client.EmuHawk/GraphicsImplementations/OpenGLProvider.cs +++ /dev/null @@ -1,29 +0,0 @@ -using BizHawk.Bizware.Graphics; -using BizHawk.Emulation.Common; - -namespace BizHawk.Client.EmuHawk -{ - /// - /// Provides a way for a core to use OpenGL - /// - public class OpenGLProvider : IOpenGLProvider - { - public bool SupportsGLVersion(int major, int minor) - => OpenGLVersion.SupportsVersion(major, minor); - - public object RequestGLContext(int major, int minor, bool coreProfile) - => new SDL2OpenGLContext(major, minor, coreProfile); - - public void ReleaseGLContext(object context) - => ((SDL2OpenGLContext)context).Dispose(); - - public void ActivateGLContext(object context) - => ((SDL2OpenGLContext)context).MakeContextCurrent(); - - public void DeactivateGLContext() - => SDL2OpenGLContext.MakeNoneCurrent(); - - public IntPtr GetGLProcAddress(string proc) - => SDL2OpenGLContext.GetGLProcAddress(proc); - } -} diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 13b08dd0b01..65232e21d0f 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -322,8 +322,7 @@ public CoreComm CreateCoreComm() message => this.ModalMessageBox(message, "Warning", EMsgBoxIcon.Warning), AddOnScreenMessage, cfp, - prefs, - new OpenGLProvider()); + prefs); } private void SetImages() diff --git a/src/BizHawk.Emulation.Common/CoreComms.cs b/src/BizHawk.Emulation.Common/CoreComms.cs index 630e4228868..a7ce83cd453 100644 --- a/src/BizHawk.Emulation.Common/CoreComms.cs +++ b/src/BizHawk.Emulation.Common/CoreComms.cs @@ -18,15 +18,13 @@ public CoreComm( ModalMessageBoxCallback showMessage, AddOnScreenMessageCallback notifyMessage, ICoreFileProvider coreFileProvider, - CorePreferencesFlags prefs, - IOpenGLProvider oglProvider + CorePreferencesFlags prefs ) { ShowMessage = showMessage; Notify = notifyMessage; CoreFileProvider = coreFileProvider; CorePreferences = prefs; - OpenGLProvider = oglProvider; } public ICoreFileProvider CoreFileProvider { get; } @@ -53,10 +51,5 @@ public enum CorePreferencesFlags /// Yeah, I put more stuff in corecomm. If you don't like it, change the settings/syncsettings stuff to support multiple "settings sets" to act like ini file sections kind of, so that we can hand a generic settings object to cores instead of strictly ones defined by the cores /// public CorePreferencesFlags CorePreferences { get; } - - /// - /// Interface to provide OpenGL resources to the core - /// - public IOpenGLProvider OpenGLProvider { get; } } } diff --git a/src/BizHawk.Emulation.Common/Interfaces/IOpenGLProvider.cs b/src/BizHawk.Emulation.Common/Interfaces/IOpenGLProvider.cs deleted file mode 100644 index 8acd2d0cbd3..00000000000 --- a/src/BizHawk.Emulation.Common/Interfaces/IOpenGLProvider.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace BizHawk.Emulation.Common -{ - /// - /// Defines an interface for cores to obtain OpenGL contexts and functions - /// - public interface IOpenGLProvider - { - /// - /// Checks if specified OpenGL version is supported - /// The current context will be preserved - /// - bool SupportsGLVersion(int major, int minor); - - /// - /// Requests an OpenGL context with specified major / minor version - /// The core profile can be requested (otherwise, the compatibility profile will be used) - /// The requested OpenGL context will be shared with the current context - /// Note: creating a context implicitly makes that created context current - /// - object RequestGLContext(int major, int minor, bool coreProfile); - - /// - /// Frees this OpenGL context - /// - void ReleaseGLContext(object context); - - /// - /// Sets this OpenGL context to current - /// - void ActivateGLContext(object context); - - /// - /// Deactivates the current OpenGL context - /// No context will be current after this call - /// - void DeactivateGLContext(); - - /// - /// Gets an OpenGL function pointer - /// The user must make a context active before using this - /// - IntPtr GetGLProcAddress(string? proc); - } -} diff --git a/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj b/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj index 16f5e5e251c..d99bba5f879 100644 --- a/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj +++ b/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj @@ -13,6 +13,7 @@ + diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs index 581db2ef738..661e92972fd 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs @@ -1,11 +1,10 @@ -using BizHawk.Common; - -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; -using BizHawk.Common.CollectionExtensions; +using BizHawk.Common; using BizHawk.Common.StringExtensions; +using CollectionExtensions = BizHawk.Common.CollectionExtensions.CollectionExtensions; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs index cbe56f61e13..0aaa5e45896 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs @@ -1,11 +1,10 @@ -using BizHawk.Common; - -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; -using BizHawk.Common.CollectionExtensions; +using BizHawk.Common; using BizHawk.Common.StringExtensions; +using CollectionExtensions = BizHawk.Common.CollectionExtensions.CollectionExtensions; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.IEmulator.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.IEmulator.cs index 1ae176e10e3..118b6e20ce3 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.IEmulator.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.IEmulator.cs @@ -82,7 +82,7 @@ public void Dispose() foreach (var glContext in _glContexts) { - _openGLProvider.ReleaseGLContext(glContext); + glContext.Dispose(); } _glContexts.Clear(); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs index eecb6b7a93b..79a61cab174 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs @@ -5,6 +5,7 @@ using System.Text; using BizHawk.BizInvoke; +using BizHawk.Bizware.Graphics; using BizHawk.Common; using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; @@ -39,9 +40,8 @@ private static void ResetEncoreResolver() private static Encore CurrentCore; - private readonly IOpenGLProvider _openGLProvider; private readonly bool _supportsOpenGL43; - private readonly List _glContexts = new(); + private readonly List _glContexts = new(); private readonly LibEncore.ConfigCallbackInterface _configCallbackInterface; private readonly LibEncore.GLCallbackInterface _glCallbackInterface; private readonly LibEncore.InputCallbackInterface _inputCallbackInterface; @@ -100,8 +100,7 @@ public Encore(CoreLoadParameters lp) _configCallbackInterface.GetFloat = GetFloatSettingCallback; _configCallbackInterface.GetString = GetStringSettingCallback; - _openGLProvider = lp.Comm.OpenGLProvider; - _supportsOpenGL43 = _openGLProvider.SupportsGLVersion(4, 3); + _supportsOpenGL43 = OpenGLVersion.SupportsVersion(4, 3); if (!_supportsOpenGL43/* && _syncSettings.GraphicsApi == EncoreSyncSettings.EGraphicsApi.OpenGL*/) { throw new PlatformNotSupportedException("OpenGL 4.3 is required, but it is not supported on this machine"); @@ -111,7 +110,7 @@ public Encore(CoreLoadParameters lp) _glCallbackInterface.RequestGLContext = RequestGLContextCallback; _glCallbackInterface.ReleaseGLContext = ReleaseGLContextCallback; _glCallbackInterface.ActivateGLContext = ActivateGLContextCallback; - _glCallbackInterface.GetGLProcAddress = GetGLProcAddressCallback; + _glCallbackInterface.GetGLProcAddress = SDL2OpenGLContext.GetGLProcAddress; _inputCallbackInterface.GetButton = GetButtonCallback; _inputCallbackInterface.GetAxis = GetAxisCallback; @@ -209,7 +208,7 @@ public Encore(CoreLoadParameters lp) private IntPtr RequestGLContextCallback() { - var context = _openGLProvider.RequestGLContext(4, 3, true); + var context = new SDL2OpenGLContext(4, 3, true); _glContexts.Add(context); var handle = GCHandle.Alloc(context, GCHandleType.Weak); return GCHandle.ToIntPtr(handle); @@ -218,18 +217,16 @@ private IntPtr RequestGLContextCallback() private void ReleaseGLContextCallback(IntPtr context) { var handle = GCHandle.FromIntPtr(context); - _openGLProvider.ReleaseGLContext(handle.Target); - _glContexts.Remove(handle.Target); + var glContext = (SDL2OpenGLContext) handle.Target; + glContext.Dispose(); + _glContexts.Remove(glContext); handle.Free(); } private void ActivateGLContextCallback(IntPtr context) { - var handle = GCHandle.FromIntPtr(context); - _openGLProvider.ActivateGLContext(handle.Target); + var glContext = (SDL2OpenGLContext) GCHandle.FromIntPtr(context).Target; + glContext.MakeContextCurrent(); } - - private IntPtr GetGLProcAddressCallback(string proc) - => _openGLProvider.GetGLProcAddress(proc); } } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISettable.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISettable.cs index 8b622583dfa..c402a099be9 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISettable.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISettable.cs @@ -521,7 +521,7 @@ private void RefreshScreenSettings(NDSSettings settings) ScreenSwap = settings.ScreenInvert }; - _openGLProvider.ActivateGLContext(_glContext); // SetScreenSettings will re-present the frame, so needs OpenGL context active + _glContext.MakeContextCurrent(); // SetScreenSettings will re-present the frame, so needs OpenGL context active _core.SetScreenSettings(_console, ref screenSettings, out var w , out var h); BufferWidth = w; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs index 5338f08b95b..186607b9653 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs @@ -13,6 +13,7 @@ using System.Threading; using BizHawk.BizInvoke; +using BizHawk.Bizware.Graphics; using BizHawk.Common; using BizHawk.Common.IOExtensions; using BizHawk.Common.NumberExtensions; @@ -47,13 +48,9 @@ private void LogCallback(LibMelonDS.LogLevel level, string message) } private readonly MelonDSGLTextureProvider _glTextureProvider; - private readonly IOpenGLProvider _openGLProvider; // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable private readonly LibMelonDS.GetGLProcAddressCallback _getGLProcAddressCallback; - private object _glContext; - - private IntPtr GetGLProcAddressCallback(string proc) - => _openGLProvider.GetGLProcAddress(proc); + private SDL2OpenGLContext _glContext; // TODO: Probably can make these into an interface (ITouchScreen with UntransformPoint/TransformPoint methods?) // Which case the hackiness of the current screen controls wouldn't be as bad @@ -315,8 +312,7 @@ static void InitIv(Span iv, ReadOnlySpan nonce) _logCallback = LogCallback; - _openGLProvider = CoreComm.OpenGLProvider; - _getGLProcAddressCallback = GetGLProcAddressCallback; + _getGLProcAddressCallback = SDL2OpenGLContext.GetGLProcAddress; if (lp.DeterministicEmulationRequested) { @@ -333,14 +329,14 @@ static void InitIv(Span iv, ReadOnlySpan nonce) _ => throw new InvalidOperationException($"Invalid {nameof(NDSSyncSettings.ThreeDeeRenderer)}") }; - if (!_openGLProvider.SupportsGLVersion(majorGlVersion, minorGlVersion)) + if (!OpenGLVersion.SupportsVersion(majorGlVersion, minorGlVersion)) { lp.Comm.Notify($"OpenGL {majorGlVersion}.{minorGlVersion} is not supported on this machine, falling back to software renderer"); _activeSyncSettings.ThreeDeeRenderer = NDSSyncSettings.ThreeDeeRendererType.Software; } else { - _glContext = _openGLProvider.RequestGLContext(majorGlVersion, minorGlVersion, true); + _glContext = new SDL2OpenGLContext(majorGlVersion, minorGlVersion, true); // reallocate video buffer for scaling if (_activeSyncSettings.GLScaleFactor > 1) { @@ -353,13 +349,13 @@ static void InitIv(Span iv, ReadOnlySpan nonce) if (_activeSyncSettings.ThreeDeeRenderer == NDSSyncSettings.ThreeDeeRendererType.Software) { - if (!_openGLProvider.SupportsGLVersion(3, 1)) + if (!OpenGLVersion.SupportsVersion(3, 1)) { lp.Comm.Notify("OpenGL 3.1 is not supported on this machine, screen control options will not work."); } else { - _glContext = _openGLProvider.RequestGLContext(3, 1, true); + _glContext = new SDL2OpenGLContext(3, 1, true); } } @@ -626,7 +622,7 @@ static void InitIv(Span iv, ReadOnlySpan nonce) if (_glContext != null) { - _glTextureProvider = new(this, _core, () => _openGLProvider.ActivateGLContext(_glContext)); + _glTextureProvider = new(this, _core, () => _glContext.MakeContextCurrent()); _serviceProvider.Register(_glTextureProvider); RefreshScreenSettings(_settings); } @@ -788,10 +784,7 @@ private static LibMelonDS.Buttons GetButtons(IController c) protected override LibWaterboxCore.FrameInfo FrameAdvancePrep(IController controller, bool render, bool rendersound) { - if (_glContext != null) - { - _openGLProvider.ActivateGLContext(_glContext); - } + _glContext?.MakeContextCurrent(); var isTracing = Tracer?.IsEnabled() ?? false; _core.SetTraceCallback(isTracing ? _traceCallback : null, _settings.GetTraceMask()); @@ -840,11 +833,8 @@ public override void Dispose() _frameThreadStartEvent.Dispose(); _frameThreadEndEvent.Dispose(); - if (_glContext != null) - { - _openGLProvider.ReleaseGLContext(_glContext); - _glContext = null; - } + _glContext?.Dispose(); + _glContext = null; _memoryCallbacks.ActiveChanged -= SetMemoryCallbacks; @@ -906,7 +896,7 @@ protected override void LoadStateBinaryInternal(BinaryReader reader) // But at least width and height will be corrected (since base LoadStateBinary overrides them) if (_glTextureProvider != null) { - _openGLProvider.ActivateGLContext(_glContext); + _glContext.MakeContextCurrent(); _core.PresentGL(_console, out var w , out var h); BufferWidth = w; BufferHeight = h;