From 3e422f020cff31044a0e50feaa440cefbe5da12e Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 9 May 2024 22:59:08 -0400 Subject: [PATCH] Hook up SDL2 and GTK --- src/Ryujinx.Gtk3/UI/MainWindow.cs | 2 +- src/Ryujinx.Gtk3/UI/OpenGLRenderer.cs | 13 ++++++++--- .../UI/OpenToolkitBindingsContext.cs | 20 ---------------- src/Ryujinx.Gtk3/UI/SPBOpenGLContext.cs | 10 ++++---- .../OpenGL/OpenGLWindow.cs | 23 ++++++++----------- src/Ryujinx.Headless.SDL2/Program.cs | 4 ++-- 6 files changed, 28 insertions(+), 44 deletions(-) delete mode 100644 src/Ryujinx.Gtk3/UI/OpenToolkitBindingsContext.cs diff --git a/src/Ryujinx.Gtk3/UI/MainWindow.cs b/src/Ryujinx.Gtk3/UI/MainWindow.cs index d1ca6ce6a..eec90ad2c 100644 --- a/src/Ryujinx.Gtk3/UI/MainWindow.cs +++ b/src/Ryujinx.Gtk3/UI/MainWindow.cs @@ -504,7 +504,7 @@ namespace Ryujinx.UI } else { - renderer = new Graphics.OpenGL.OpenGLRenderer(); + renderer = new Graphics.OpenGL.OpenGLRenderer(((OpenGLRenderer)RendererWidget).GetApi()); } BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading; diff --git a/src/Ryujinx.Gtk3/UI/OpenGLRenderer.cs b/src/Ryujinx.Gtk3/UI/OpenGLRenderer.cs index 02a9e0388..0ff3700c0 100644 --- a/src/Ryujinx.Gtk3/UI/OpenGLRenderer.cs +++ b/src/Ryujinx.Gtk3/UI/OpenGLRenderer.cs @@ -1,7 +1,7 @@ -using Silk.NET.OpenGL.Legacy; using Ryujinx.Common.Configuration; using Ryujinx.Common.Logging; using Ryujinx.Input.HLE; +using Silk.NET.OpenGL.Legacy; using SPB.Graphics; using SPB.Graphics.Exceptions; using SPB.Graphics.OpenGL; @@ -96,11 +96,18 @@ namespace Ryujinx.UI _openGLContext.MakeCurrent(_nativeWindow); - GL.ClearColor(0, 0, 0, 1.0f); - GL.Clear(ClearBufferMask.ColorBufferBit); + var api = GetApi(); + + api.ClearColor(0, 0, 0, 1.0f); + api.Clear(ClearBufferMask.ColorBufferBit); SwapBuffers(); } + public GL GetApi() + { + return GL.GetApi(_openGLContext.GetProcAddress); + } + public override void SwapBuffers() { _nativeWindow.SwapBuffers(); diff --git a/src/Ryujinx.Gtk3/UI/OpenToolkitBindingsContext.cs b/src/Ryujinx.Gtk3/UI/OpenToolkitBindingsContext.cs deleted file mode 100644 index 1224ccfe0..000000000 --- a/src/Ryujinx.Gtk3/UI/OpenToolkitBindingsContext.cs +++ /dev/null @@ -1,20 +0,0 @@ -using SPB.Graphics; -using System; - -namespace Ryujinx.UI -{ - public class OpenToolkitBindingsContext : OpenTK.IBindingsContext - { - private readonly IBindingsContext _bindingContext; - - public OpenToolkitBindingsContext(IBindingsContext bindingsContext) - { - _bindingContext = bindingsContext; - } - - public IntPtr GetProcAddress(string procName) - { - return _bindingContext.GetProcAddress(procName); - } - } -} diff --git a/src/Ryujinx.Gtk3/UI/SPBOpenGLContext.cs b/src/Ryujinx.Gtk3/UI/SPBOpenGLContext.cs index 5833411d3..1ecc64c67 100644 --- a/src/Ryujinx.Gtk3/UI/SPBOpenGLContext.cs +++ b/src/Ryujinx.Gtk3/UI/SPBOpenGLContext.cs @@ -1,5 +1,5 @@ -using Silk.NET.OpenGL.Legacy; using Ryujinx.Graphics.OpenGL; +using Silk.NET.OpenGL.Legacy; using SPB.Graphics; using SPB.Graphics.OpenGL; using SPB.Platform; @@ -9,11 +9,13 @@ namespace Ryujinx.UI { class SPBOpenGLContext : IOpenGLContext { + public readonly GL Api; private readonly OpenGLContextBase _context; private readonly NativeWindowBase _window; - private SPBOpenGLContext(OpenGLContextBase context, NativeWindowBase window) + private SPBOpenGLContext(GL api, OpenGLContextBase context, NativeWindowBase window) { + Api = api; _context = context; _window = window; } @@ -39,11 +41,11 @@ namespace Ryujinx.UI context.Initialize(window); context.MakeCurrent(window); - GL.LoadBindings(new OpenToolkitBindingsContext(context)); + GL api = GL.GetApi(context.GetProcAddress); context.MakeCurrent(null); - return new SPBOpenGLContext(context, window); + return new SPBOpenGLContext(api, context, window); } } } diff --git a/src/Ryujinx.Headless.SDL2/OpenGL/OpenGLWindow.cs b/src/Ryujinx.Headless.SDL2/OpenGL/OpenGLWindow.cs index bef71e5a6..c8456df2c 100644 --- a/src/Ryujinx.Headless.SDL2/OpenGL/OpenGLWindow.cs +++ b/src/Ryujinx.Headless.SDL2/OpenGL/OpenGLWindow.cs @@ -38,22 +38,16 @@ namespace Ryujinx.Headless.SDL2.OpenGL CheckResult(SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_STEREO, 0)); } - private class OpenToolkitBindingsContext : IBindingsContext - { - public IntPtr GetProcAddress(string procName) - { - return SDL_GL_GetProcAddress(procName); - } - } - private class SDL2OpenGLContext : IOpenGLContext { + public readonly GL Api; private readonly IntPtr _context; private readonly IntPtr _window; private readonly bool _shouldDisposeWindow; - public SDL2OpenGLContext(IntPtr context, IntPtr window, bool shouldDisposeWindow = true) + public SDL2OpenGLContext(GL api, IntPtr context, IntPtr window, bool shouldDisposeWindow = true) { + Api = api; _context = context; _window = window; _shouldDisposeWindow = shouldDisposeWindow; @@ -68,13 +62,13 @@ namespace Ryujinx.Headless.SDL2.OpenGL IntPtr windowHandle = SDL_CreateWindow("Ryujinx background context window", 0, 0, 1, 1, SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL_WindowFlags.SDL_WINDOW_HIDDEN); IntPtr context = SDL_GL_CreateContext(windowHandle); - GL.LoadBindings(new OpenToolkitBindingsContext()); + GL api = GL.GetApi((_ => context)); CheckResult(SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 0)); CheckResult(SDL_GL_MakeCurrent(windowHandle, IntPtr.Zero)); - return new SDL2OpenGLContext(context, windowHandle); + return new SDL2OpenGLContext(api, context, windowHandle); } public void MakeCurrent() @@ -111,6 +105,7 @@ namespace Ryujinx.Headless.SDL2.OpenGL private readonly GraphicsDebugLevel _glLogLevel; private SDL2OpenGLContext _openGLContext; + public GL Api => _openGLContext.Api; public OpenGLWindow( InputManager inputManager, @@ -142,15 +137,15 @@ namespace Ryujinx.Headless.SDL2.OpenGL } // NOTE: The window handle needs to be disposed by the thread that created it and is handled separately. - _openGLContext = new SDL2OpenGLContext(context, WindowHandle, false); + _openGLContext = new SDL2OpenGLContext(GL.GetApi((_ => context)), context, WindowHandle, false); // First take exclusivity on the OpenGL context. ((OpenGLRenderer)Renderer).InitializeBackgroundContext(SDL2OpenGLContext.CreateBackgroundContext(_openGLContext)); _openGLContext.MakeCurrent(); - GL.ClearColor(0, 0, 0, 1.0f); - GL.Clear(ClearBufferMask.ColorBufferBit); + _openGLContext.Api.ClearColor(0, 0, 0, 1.0f); + _openGLContext.Api.Clear(ClearBufferMask.ColorBufferBit); SwapBuffers(); if (IsExclusiveFullscreen) diff --git a/src/Ryujinx.Headless.SDL2/Program.cs b/src/Ryujinx.Headless.SDL2/Program.cs index 38faa6b8d..1e6371513 100644 --- a/src/Ryujinx.Headless.SDL2/Program.cs +++ b/src/Ryujinx.Headless.SDL2/Program.cs @@ -532,8 +532,8 @@ namespace Ryujinx.Headless.SDL2 preferredGpuId); } - var openGlWindow = (OpenGLWindow)window; - return new OpenGLRenderer(window.); + var openGlWindow = window as OpenGLWindow; + return new OpenGLRenderer(openGlWindow.Api); } private static Switch InitializeEmulationContext(WindowBase window, IRenderer renderer, Options options)