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