JinxRyu/Ryujinx/Ui/GLRenderer.cs
Mary 6cb22c9d38
Miria: The Death of OpenTK 3 (#2194)
* openal: Update to OpenTK 4

* Ryujinx.Graphics.OpenGL: Update to OpenTK 4

* Entirely removed OpenTK 3, still wip

* Use SPB for context creation and handling

Still need to test on GLX and readd input support

* Start implementing a new input system

So far only gamepad are supported, no configuration possible via UI but detected via hotplug/removal

Button mapping backend is implemented

TODO: front end, configuration handling and configuration migration
TODO: keyboard support

* Enforce RGB only framebuffer on the GLWidget

Fix possible transparent window

* Implement UI gamepad frontend

Also fix bad mapping of minus button and ensure gamepad config is updated in real time

* Handle controller being disconnected and reconnected again

* Revert "Enforce RGB only framebuffer on the GLWidget"

This reverts commit 0949715d1a03ec793e35e37f7b610cbff2d63965.

* Fix first color clear

* Filter SDL2 events a bit

* Start working on the keyboard detail

- Rework configuration classes a bit to be more clean.
- Integrate fully the keyboard configuration to the front end (TODO: assigner)
- Start skeleton for the GTK3 keyboard driver

* Add KeyboardStateSnapshot and its integration

* Implement keyboard assigner and GTK3 key mapping

TODO: controller configuration mapping and IGamepad implementation for keyboard

* Add missing SR and SL definitions

* Fix copy pasta mistake on config for previous commit

* Implement IGamepad interface for GTK3 keyboard

* Fix some implementation still being commented in the controller ui for keyboard

* Port screen handle code

* Remove all configuration management code and move HidNew to Hid

* Rename InputConfigNew to InputConfig

* Add a version field to the input config

* Prepare serialization and deserialization of new input config and migrate profile loading and saving

* Support input configuration saving to config and bump config version to 23.

* Clean up in ConfigurationState

* Reference SPB via a nuget package

* Move new input system to Ryujinx.Input project and SDL2 detail to Ryujinx.Input.SDL2

* move GTK3 input to the right directory

* Fix triggers on SDL2

* Update to SDL2 2.0.14 via our own fork

* Update buttons definition for SDL2 2.0.14 and report gamepad features

* Implement motion support again with SDL2

TODO: cemu hooks integration

* Switch to latest of nightly SDL2

* SDL2: Fix bugs in gamepad id matching allowing different gamepad to match on the same device index

* Ensure values are set in UI when the gamepad get hot plugged

* Avoid trying to add controllers in the Update method and don't open SDL2 gamepad instance before checking ids

This fixes permanent rumble of pro controller in some hotplug scenario

* Fix more UI bugs

* Move legcay motion code around before reintegration

* gamecontroller UI tweaks here and there

* Hide Motion on non motion configurations

* Update the TODO grave

Some TODO were fixed long time ago or are quite oudated...

* Integrate cemu hooks motion configuration

* Integrate cemu hooks configuration options to the UI again

* cemuhooks => cemuhooks

* Add cemu hook support again

* Fix regression on normal motion and fix some very nasty bugs around

* Fix for XCB multithreads issue on Linux

* Enable motion by default

* Block inputs in the main view when in the controller configuration window

* Some fixes for the controller ui again

* Add joycon support and fixes other hints

* Bug fixes and clean up

- Invert default mapping if not a Nintendo controller
- Keep alive the controller being selected on the controller window (allow to avoid big delay for controller needing time to init when doing button assignment)
- Clean up hints in use
- Remove debug logs around
- Fixes potential double free with SDL2Gamepad

* Move the button assigner and motion logic to the Ryujinx.Input project

* Reimplement raw keyboard hle input

Also move out the logic of the hotkeys

* Move all remaining Input manager stuffs to the Ryujinx.Input project

* Increment configuration version yet again because of master changes

* Ensure input config isn't null when not present

* Fixes for VS not being nice

* Fix broken gamepad caching logic causing crashes on ui

* Ensure the background context is destroyed

* Update dependencies

* Readd retrocompat with old format of the config to avoid parsing and crashes on those versions

Also updated the debug Config.json

* Document new input APIs

* Isolate SDL2Driver to the project and remove external export of it

* Add support for external gamepad db mappings on SDL2

* Last clean up before PR

* Addresses first part of comments

* Address gdkchan's comments

* Do not use JsonException

* Last comment fixes
2021-04-14 12:28:43 +02:00

629 lines
20 KiB
C#

using ARMeilleure.Translation;
using ARMeilleure.Translation.PTC;
using Gdk;
using OpenTK.Graphics.OpenGL;
using Ryujinx.Common;
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Logging;
using Ryujinx.Configuration;
using Ryujinx.Graphics.OpenGL;
using Ryujinx.HLE.HOS.Services.Hid;
using Ryujinx.Input;
using Ryujinx.Input.HLE;
using Ryujinx.Ui.Widgets;
using SPB.Graphics;
using SPB.Graphics.OpenGL;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Key = Ryujinx.Input.Key;
namespace Ryujinx.Ui
{
using Switch = HLE.Switch;
public class GlRenderer : GLWidget
{
private const int SwitchPanelWidth = 1280;
private const int SwitchPanelHeight = 720;
private const int TargetFps = 60;
public ManualResetEvent WaitEvent { get; set; }
public NpadManager NpadManager { get; }
public static event EventHandler<StatusUpdatedEventArgs> StatusUpdatedEvent;
private bool _isActive;
private bool _isStopped;
private bool _isFocused;
private double _mouseX;
private double _mouseY;
private bool _mousePressed;
private bool _toggleFullscreen;
private bool _toggleDockedMode;
private readonly long _ticksPerFrame;
private long _ticks = 0;
private readonly Stopwatch _chrono;
private readonly Switch _device;
private Renderer _renderer;
private KeyboardHotkeyState _prevHotkeyState;
private GraphicsDebugLevel _glLogLevel;
private readonly ManualResetEvent _exitEvent;
// Hide Cursor
const int CursorHideIdleTime = 8; // seconds
private static readonly Cursor _invisibleCursor = new Cursor(Display.Default, CursorType.BlankCursor);
private long _lastCursorMoveTime;
private bool _hideCursorOnIdle;
private InputManager _inputManager;
private IKeyboard _keyboardInterface;
public GlRenderer(Switch device, InputManager inputManager, GraphicsDebugLevel glLogLevel)
: base (GetGraphicsMode(),
3, 3,
glLogLevel == GraphicsDebugLevel.None
? OpenGLContextFlags.Compat
: OpenGLContextFlags.Compat | OpenGLContextFlags.Debug)
{
_inputManager = inputManager;
NpadManager = _inputManager.CreateNpadManager();
_keyboardInterface = (IKeyboard)_inputManager.KeyboardDriver.GetGamepad("0");
NpadManager.ReloadConfiguration(ConfigurationState.Instance.Hid.InputConfig.Value.ToList());
WaitEvent = new ManualResetEvent(false);
_device = device;
Initialized += GLRenderer_Initialized;
Destroyed += GLRenderer_Destroyed;
ShuttingDown += GLRenderer_ShuttingDown;
Initialize();
_chrono = new Stopwatch();
_ticksPerFrame = Stopwatch.Frequency / TargetFps;
AddEvents((int)(EventMask.ButtonPressMask
| EventMask.ButtonReleaseMask
| EventMask.PointerMotionMask
| EventMask.KeyPressMask
| EventMask.KeyReleaseMask));
Shown += Renderer_Shown;
_glLogLevel = glLogLevel;
_exitEvent = new ManualResetEvent(false);
_hideCursorOnIdle = ConfigurationState.Instance.HideCursorOnIdle;
_lastCursorMoveTime = Stopwatch.GetTimestamp();
ConfigurationState.Instance.HideCursorOnIdle.Event += HideCursorStateChanged;
}
private void HideCursorStateChanged(object sender, ReactiveEventArgs<bool> state)
{
Gtk.Application.Invoke(delegate
{
_hideCursorOnIdle = state.NewValue;
if (_hideCursorOnIdle)
{
_lastCursorMoveTime = Stopwatch.GetTimestamp();
}
else
{
Window.Cursor = null;
}
});
}
private static FramebufferFormat GetGraphicsMode()
{
return Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default;
}
private void GLRenderer_ShuttingDown(object sender, EventArgs args)
{
_device.DisposeGpu();
NpadManager.Dispose();
}
private void Parent_FocusOutEvent(object o, Gtk.FocusOutEventArgs args)
{
_isFocused = false;
}
private void Parent_FocusInEvent(object o, Gtk.FocusInEventArgs args)
{
_isFocused = true;
}
private void GLRenderer_Destroyed(object sender, EventArgs e)
{
ConfigurationState.Instance.HideCursorOnIdle.Event -= HideCursorStateChanged;
NpadManager.Dispose();
Dispose();
}
protected void Renderer_Shown(object sender, EventArgs e)
{
_isFocused = this.ParentWindow.State.HasFlag(Gdk.WindowState.Focused);
}
public void HandleScreenState(KeyboardStateSnapshot keyboard)
{
bool toggleFullscreen = keyboard.IsPressed(Key.F11)
|| ((keyboard.IsPressed(Key.AltLeft)
|| keyboard.IsPressed(Key.AltRight))
&& keyboard.IsPressed(Key.Enter))
|| keyboard.IsPressed(Key.Escape);
bool fullScreenToggled = ParentWindow.State.HasFlag(Gdk.WindowState.Fullscreen);
if (toggleFullscreen != _toggleFullscreen)
{
if (toggleFullscreen)
{
if (fullScreenToggled)
{
ParentWindow.Unfullscreen();
(Toplevel as MainWindow)?.ToggleExtraWidgets(true);
}
else
{
if (keyboard.IsPressed(Key.Escape))
{
if (!ConfigurationState.Instance.ShowConfirmExit || GtkDialog.CreateExitDialog())
{
Exit();
}
}
else
{
ParentWindow.Fullscreen();
(Toplevel as MainWindow)?.ToggleExtraWidgets(false);
}
}
}
}
_toggleFullscreen = toggleFullscreen;
bool toggleDockedMode = keyboard.IsPressed(Key.F9);
if (toggleDockedMode != _toggleDockedMode)
{
if (toggleDockedMode)
{
ConfigurationState.Instance.System.EnableDockedMode.Value =
!ConfigurationState.Instance.System.EnableDockedMode.Value;
}
}
_toggleDockedMode = toggleDockedMode;
if (_hideCursorOnIdle)
{
long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
Window.Cursor = (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency) ? _invisibleCursor : null;
}
}
private void GLRenderer_Initialized(object sender, EventArgs e)
{
// Release the GL exclusivity that SPB gave us as we aren't going to use it in GTK Thread.
OpenGLContext.MakeCurrent(null);
WaitEvent.Set();
}
protected override bool OnConfigureEvent(EventConfigure evnt)
{
bool result = base.OnConfigureEvent(evnt);
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
_renderer.Window.SetSize(evnt.Width * monitor.ScaleFactor, evnt.Height * monitor.ScaleFactor);
return result;
}
public void Start()
{
_chrono.Restart();
_isActive = true;
Gtk.Window parent = this.Toplevel as Gtk.Window;
parent.FocusInEvent += Parent_FocusInEvent;
parent.FocusOutEvent += Parent_FocusOutEvent;
Gtk.Application.Invoke(delegate
{
parent.Present();
string titleNameSection = string.IsNullOrWhiteSpace(_device.Application.TitleName) ? string.Empty
: $" - {_device.Application.TitleName}";
string titleVersionSection = string.IsNullOrWhiteSpace(_device.Application.DisplayVersion) ? string.Empty
: $" v{_device.Application.DisplayVersion}";
string titleIdSection = string.IsNullOrWhiteSpace(_device.Application.TitleIdText) ? string.Empty
: $" ({_device.Application.TitleIdText.ToUpper()})";
string titleArchSection = _device.Application.TitleIs64Bit ? " (64-bit)" : " (32-bit)";
parent.Title = $"Ryujinx {Program.Version}{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}";
});
Thread renderLoopThread = new Thread(Render)
{
Name = "GUI.RenderLoop"
};
renderLoopThread.Start();
Thread nvStutterWorkaround = new Thread(NVStutterWorkaround)
{
Name = "GUI.NVStutterWorkaround"
};
nvStutterWorkaround.Start();
MainLoop();
renderLoopThread.Join();
nvStutterWorkaround.Join();
Exit();
}
private void NVStutterWorkaround()
{
while (_isActive)
{
// When NVIDIA Threaded Optimization is on, the driver will snapshot all threads in the system whenever the application creates any new ones.
// The ThreadPool has something called a "GateThread" which terminates itself after some inactivity.
// However, it immediately starts up again, since the rules regarding when to terminate and when to start differ.
// This creates a new thread every second or so.
// The main problem with this is that the thread snapshot can take 70ms, is on the OpenGL thread and will delay rendering any graphics.
// This is a little over budget on a frame time of 16ms, so creates a large stutter.
// The solution is to keep the ThreadPool active so that it never has a reason to terminate the GateThread.
// TODO: This should be removed when the issue with the GateThread is resolved.
ThreadPool.QueueUserWorkItem((state) => { });
Thread.Sleep(300);
}
}
protected override bool OnButtonPressEvent(EventButton evnt)
{
_mouseX = evnt.X;
_mouseY = evnt.Y;
if (evnt.Button == 1)
{
_mousePressed = true;
}
return false;
}
protected override bool OnButtonReleaseEvent(EventButton evnt)
{
if (evnt.Button == 1)
{
_mousePressed = false;
}
return false;
}
protected override bool OnMotionNotifyEvent(EventMotion evnt)
{
if (evnt.Device.InputSource == InputSource.Mouse)
{
_mouseX = evnt.X;
_mouseY = evnt.Y;
}
if (_hideCursorOnIdle)
{
_lastCursorMoveTime = Stopwatch.GetTimestamp();
}
return false;
}
protected override void OnGetPreferredHeight(out int minimumHeight, out int naturalHeight)
{
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
// If the monitor is at least 1080p, use the Switch panel size as minimal size.
if (monitor.Geometry.Height >= 1080)
{
minimumHeight = SwitchPanelHeight;
}
// Otherwise, we default minimal size to 480p 16:9.
else
{
minimumHeight = 480;
}
naturalHeight = minimumHeight;
}
protected override void OnGetPreferredWidth(out int minimumWidth, out int naturalWidth)
{
Gdk.Monitor monitor = Display.GetMonitorAtWindow(Window);
// If the monitor is at least 1080p, use the Switch panel size as minimal size.
if (monitor.Geometry.Height >= 1080)
{
minimumWidth = SwitchPanelWidth;
}
// Otherwise, we default minimal size to 480p 16:9.
else
{
minimumWidth = 854;
}
naturalWidth = minimumWidth;
}
public void Exit()
{
NpadManager?.Dispose();
if (_isStopped)
{
return;
}
_isStopped = true;
_isActive = false;
_exitEvent.WaitOne();
_exitEvent.Dispose();
}
public void Initialize()
{
if (!(_device.Gpu.Renderer is Renderer))
{
throw new NotSupportedException($"GPU renderer must be an OpenGL renderer when using {typeof(Renderer).Name}!");
}
_renderer = (Renderer)_device.Gpu.Renderer;
}
public void Render()
{
// First take exclusivity on the OpenGL context.
_renderer.InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(OpenGLContext));
Gtk.Window parent = Toplevel as Gtk.Window;
parent.Present();
OpenGLContext.MakeCurrent(NativeWindow);
_device.Gpu.Renderer.Initialize(_glLogLevel);
// Make sure the first frame is not transparent.
GL.ClearColor(0, 0, 0, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
SwapBuffers();
_device.Gpu.InitializeShaderCache();
Translator.IsReadyForTranslation.Set();
while (_isActive)
{
if (_isStopped)
{
return;
}
_ticks += _chrono.ElapsedTicks;
_chrono.Restart();
if (_device.WaitFifo())
{
_device.Statistics.RecordFifoStart();
_device.ProcessFrame();
_device.Statistics.RecordFifoEnd();
}
while (_device.ConsumeFrameAvailable())
{
_device.PresentFrame(SwapBuffers);
}
if (_ticks >= _ticksPerFrame)
{
string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? "Docked" : "Handheld";
float scale = Graphics.Gpu.GraphicsConfig.ResScale;
if (scale != 1)
{
dockedMode += $" ({scale}x)";
}
StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
_device.EnableDeviceVsync,
dockedMode,
ConfigurationState.Instance.Graphics.AspectRatio.Value.ToText(),
$"Game: {_device.Statistics.GetGameFrameRate():00.00} FPS",
$"FIFO: {_device.Statistics.GetFifoPercent():0.00} %",
$"GPU: {_renderer.GpuVendor}"));
_ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
}
}
}
public void SwapBuffers()
{
NativeWindow.SwapBuffers();
}
public void MainLoop()
{
while (_isActive)
{
UpdateFrame();
// Polling becomes expensive if it's not slept
Thread.Sleep(1);
}
_exitEvent.Set();
}
private bool UpdateFrame()
{
if (!_isActive)
{
return true;
}
if (_isStopped)
{
return false;
}
if (_isFocused)
{
Gtk.Application.Invoke(delegate
{
KeyboardStateSnapshot keyboard = _keyboardInterface.GetKeyboardStateSnapshot();
HandleScreenState(keyboard);
if (keyboard.IsPressed(Key.Delete))
{
if (!ParentWindow.State.HasFlag(WindowState.Fullscreen))
{
Ptc.Continue();
}
}
});
}
NpadManager.Update(_device.Hid, _device.TamperMachine);
if(_isFocused)
{
KeyboardHotkeyState currentHotkeyState = GetHotkeyState();
if (currentHotkeyState.HasFlag(KeyboardHotkeyState.ToggleVSync) &&
!_prevHotkeyState.HasFlag(KeyboardHotkeyState.ToggleVSync))
{
_device.EnableDeviceVsync = !_device.EnableDeviceVsync;
}
_prevHotkeyState = currentHotkeyState;
}
//Touchscreen
bool hasTouch = false;
// Get screen touch position from left mouse click
// OpenTK always captures mouse events, even if out of focus, so check if window is focused.
if (_isFocused && _mousePressed)
{
float aspectWidth = SwitchPanelHeight * ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat();
int screenWidth = AllocatedWidth;
int screenHeight = AllocatedHeight;
if (AllocatedWidth > AllocatedHeight * aspectWidth / SwitchPanelHeight)
{
screenWidth = (int)(AllocatedHeight * aspectWidth) / SwitchPanelHeight;
}
else
{
screenHeight = (AllocatedWidth * SwitchPanelHeight) / (int)aspectWidth;
}
int startX = (AllocatedWidth - screenWidth) >> 1;
int startY = (AllocatedHeight - screenHeight) >> 1;
int endX = startX + screenWidth;
int endY = startY + screenHeight;
if (_mouseX >= startX &&
_mouseY >= startY &&
_mouseX < endX &&
_mouseY < endY)
{
int screenMouseX = (int)_mouseX - startX;
int screenMouseY = (int)_mouseY - startY;
int mX = (screenMouseX * (int)aspectWidth) / screenWidth;
int mY = (screenMouseY * SwitchPanelHeight) / screenHeight;
TouchPoint currentPoint = new TouchPoint
{
X = (uint)mX,
Y = (uint)mY,
// Placeholder values till more data is acquired
DiameterX = 10,
DiameterY = 10,
Angle = 90
};
hasTouch = true;
_device.Hid.Touchscreen.Update(currentPoint);
}
}
if (!hasTouch)
{
_device.Hid.Touchscreen.Update();
}
_device.Hid.DebugPad.Update();
return true;
}
[Flags]
private enum KeyboardHotkeyState
{
None,
ToggleVSync
}
private KeyboardHotkeyState GetHotkeyState()
{
KeyboardHotkeyState state = KeyboardHotkeyState.None;
if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleVsync))
{
state |= KeyboardHotkeyState.ToggleVSync;
}
return state;
}
}
}