2020-11-17 16:40:19 -05:00
|
|
|
|
using ARMeilleure.Translation;
|
|
|
|
|
using ARMeilleure.Translation.PTC;
|
2020-05-02 22:00:53 -04:00
|
|
|
|
using Gdk;
|
2021-05-04 12:19:04 -04:00
|
|
|
|
using Gtk;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2021-03-02 17:45:33 -05:00
|
|
|
|
using Ryujinx.Common;
|
2020-08-02 10:41:24 -04:00
|
|
|
|
using Ryujinx.Common.Configuration;
|
2020-12-15 21:19:07 -05:00
|
|
|
|
using Ryujinx.Configuration;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using Ryujinx.Graphics.OpenGL;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
2021-04-14 06:28:43 -04:00
|
|
|
|
using Ryujinx.Input;
|
|
|
|
|
using Ryujinx.Input.HLE;
|
2021-01-08 03:14:13 -05:00
|
|
|
|
using Ryujinx.Ui.Widgets;
|
2021-04-14 06:28:43 -04:00
|
|
|
|
using SPB.Graphics;
|
|
|
|
|
using SPB.Graphics.OpenGL;
|
2021-05-04 12:19:04 -04:00
|
|
|
|
using SPB.Platform;
|
|
|
|
|
using SPB.Platform.GLX;
|
|
|
|
|
using SPB.Platform.WGL;
|
|
|
|
|
using SPB.Windowing;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using System;
|
2021-03-02 17:45:33 -05:00
|
|
|
|
using System.Diagnostics;
|
2021-04-14 06:28:43 -04:00
|
|
|
|
using System.Linq;
|
2021-05-04 12:19:04 -04:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2021-04-14 06:28:43 -04:00
|
|
|
|
using Key = Ryujinx.Input.Key;
|
|
|
|
|
|
2020-02-11 19:56:19 -05:00
|
|
|
|
namespace Ryujinx.Ui
|
|
|
|
|
{
|
2021-03-02 17:45:33 -05:00
|
|
|
|
using Switch = HLE.Switch;
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
public class GlRenderer : RendererWidgetBase
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-08-02 10:41:24 -04:00
|
|
|
|
private GraphicsDebugLevel _glLogLevel;
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
private bool _initializedOpenGL;
|
2020-12-01 18:23:43 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
private OpenGLContextBase _openGLContext;
|
|
|
|
|
private SwappableNativeWindowBase _nativeWindow;
|
2020-08-02 10:41:24 -04:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
public GlRenderer(InputManager inputManager, GraphicsDebugLevel glLogLevel) : base(inputManager, glLogLevel)
|
|
|
|
|
{
|
2020-08-02 10:41:24 -04:00
|
|
|
|
_glLogLevel = glLogLevel;
|
2021-03-02 17:45:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
protected override bool OnDrawn(Cairo.Context cr)
|
2021-03-02 17:45:33 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
if (!_initializedOpenGL)
|
2021-03-02 17:45:33 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
IntializeOpenGL();
|
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
return true;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
private void IntializeOpenGL()
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_nativeWindow = RetrieveNativeWindow();
|
2020-02-13 12:43:29 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
Window.EnsureNative();
|
2020-11-18 18:15:44 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext = PlatformHelper.CreateOpenGLContext(GetGraphicsMode(), 3, 3, _glLogLevel == GraphicsDebugLevel.None ? OpenGLContextFlags.Compat : OpenGLContextFlags.Compat | OpenGLContextFlags.Debug);
|
|
|
|
|
_openGLContext.Initialize(_nativeWindow);
|
|
|
|
|
_openGLContext.MakeCurrent(_nativeWindow);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-04-14 06:28:43 -04:00
|
|
|
|
// Release the GL exclusivity that SPB gave us as we aren't going to use it in GTK Thread.
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext.MakeCurrent(null);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
|
|
|
|
WaitEvent.Set();
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_initializedOpenGL = true;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
private SwappableNativeWindowBase RetrieveNativeWindow()
|
2020-12-09 17:36:08 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2020-12-09 17:36:08 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
IntPtr windowHandle = gdk_win32_window_get_handle(Window.Handle);
|
2020-12-09 17:36:08 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
return new WGLWindow(new NativeHandle(windowHandle));
|
2020-12-09 17:36:08 -05:00
|
|
|
|
}
|
2021-05-04 12:19:04 -04:00
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
IntPtr displayHandle = gdk_x11_display_get_xdisplay(Display.Handle);
|
|
|
|
|
IntPtr windowHandle = gdk_x11_window_get_xid(Window.Handle);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
return new GLXWindow(new NativeHandle(displayHandle), new NativeHandle(windowHandle));
|
2020-02-18 06:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
throw new NotImplementedException();
|
2020-02-18 06:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
[DllImport("libgdk-3-0.dll")]
|
|
|
|
|
private static extern IntPtr gdk_win32_window_get_handle(IntPtr d);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
[DllImport("libgdk-3.so.0")]
|
|
|
|
|
private static extern IntPtr gdk_x11_display_get_xdisplay(IntPtr gdkDisplay);
|
2020-12-01 18:23:43 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
[DllImport("libgdk-3.so.0")]
|
|
|
|
|
private static extern IntPtr gdk_x11_window_get_xid(IntPtr gdkWindow);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
private static FramebufferFormat GetGraphicsMode()
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
return Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
public override void InitializeRenderer()
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2020-02-14 05:52:34 -05:00
|
|
|
|
// First take exclusivity on the OpenGL context.
|
2021-05-04 12:19:04 -04:00
|
|
|
|
((Renderer)Renderer).InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(_openGLContext));
|
2021-04-14 06:28:43 -04:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext.MakeCurrent(_nativeWindow);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-04-14 06:28:43 -04:00
|
|
|
|
GL.ClearColor(0, 0, 0, 1.0f);
|
2020-02-14 05:52:34 -05:00
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
SwapBuffers();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
public override void SwapBuffers()
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_nativeWindow.SwapBuffers();
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
public override string GetGpuVendorName()
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
return ((Renderer)Renderer).GpuVendor;
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
protected override void Dispose(bool disposing)
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
// Try to bind the OpenGL context before calling the shutdown event
|
|
|
|
|
try
|
2020-02-11 19:56:19 -05:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext?.MakeCurrent(_nativeWindow);
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
2021-05-04 12:19:04 -04:00
|
|
|
|
catch (Exception) { }
|
2020-02-11 19:56:19 -05:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
Device.DisposeGpu();
|
|
|
|
|
NpadManager.Dispose();
|
2021-04-14 06:28:43 -04:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
// Unbind context and destroy everything
|
|
|
|
|
try
|
2021-04-14 06:28:43 -04:00
|
|
|
|
{
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext?.MakeCurrent(null);
|
2021-04-14 06:28:43 -04:00
|
|
|
|
}
|
2021-05-04 12:19:04 -04:00
|
|
|
|
catch (Exception) { }
|
2021-04-14 06:28:43 -04:00
|
|
|
|
|
2021-05-04 12:19:04 -04:00
|
|
|
|
_openGLContext.Dispose();
|
2021-04-14 06:28:43 -04:00
|
|
|
|
}
|
2020-02-11 19:56:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|