Some bug fixes

Changelog:

- Reapply some changes that got lost while rebasing from #904
- Make sure to guarantee exclusivity on the GL context (fixing multiple
possible race conditions on Windows)
- Avoid making GLRenderer disposed multiple time
This commit is contained in:
Thog 2020-02-06 14:36:12 +01:00
commit d54080c64e
3 changed files with 87 additions and 38 deletions

View file

@ -3,6 +3,7 @@ using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
using OpenTK.Input; using OpenTK.Input;
using OpenTK.Platform;
using Ryujinx.Configuration; using Ryujinx.Configuration;
using Ryujinx.Graphics.OpenGL; using Ryujinx.Graphics.OpenGL;
using Ryujinx.HLE; using Ryujinx.HLE;
@ -88,7 +89,9 @@ namespace Ryujinx.Ui
private void GLRenderer_Initialized(object sender, EventArgs e) private void GLRenderer_Initialized(object sender, EventArgs e)
{ {
// Release the GL exclusivity that OpenTK gave us.
GraphicsContext.MakeCurrent(null); GraphicsContext.MakeCurrent(null);
WaitEvent.Set(); WaitEvent.Set();
} }
@ -168,11 +171,19 @@ namespace Ryujinx.Ui
public void Exit() public void Exit()
{ {
_device.DisposeGpu(); if (IsStopped)
{
return;
}
IsStopped = true; IsStopped = true;
IsActive = false; IsActive = false;
using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
{
_device.DisposeGpu();
}
WaitEvent.Set(); WaitEvent.Set();
} }
@ -188,8 +199,10 @@ namespace Ryujinx.Ui
public void Render() public void Render()
{ {
GraphicsContext.MakeCurrent(WindowInfo); using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
{
_renderer.Initialize(); _renderer.Initialize();
}
while (IsActive) while (IsActive)
{ {
@ -198,8 +211,8 @@ namespace Ryujinx.Ui
return; return;
} }
GraphicsContext.MakeCurrent(WindowInfo); using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
{
GL.ClearColor(Color4.Black); GL.ClearColor(Color4.Black);
_ticks += _chrono.ElapsedTicks; _ticks += _chrono.ElapsedTicks;
@ -239,13 +252,13 @@ namespace Ryujinx.Ui
} }
} }
} }
}
public void SwapBuffers() public void SwapBuffers()
{ {
OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers(); OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
} }
public void MainLoop() public void MainLoop()
{ {
while (IsActive) while (IsActive)

View file

@ -433,14 +433,15 @@ namespace Ryujinx.Ui
DiscordIntegrationModule.SwitchToMainMenu(); DiscordIntegrationModule.SwitchToMainMenu();
_screenExitStatus.Set();
Application.Invoke(delegate Application.Invoke(delegate
{ {
_stopEmulation.Sensitive = false; _stopEmulation.Sensitive = false;
_firmwareInstallFile.Sensitive = true; _firmwareInstallFile.Sensitive = true;
_firmwareInstallDirectory.Sensitive = true; _firmwareInstallDirectory.Sensitive = true;
}); });
_screenExitStatus.Set();
} }
private static void UpdateGameMetadata(string titleId) private static void UpdateGameMetadata(string titleId)
@ -475,7 +476,11 @@ namespace Ryujinx.Ui
{ {
UpdateGameMetadata(device.System.TitleIdText); UpdateGameMetadata(device.System.TitleIdText);
_gLWigdet?.Exit(); if (_gLWigdet != null)
{
_gLWigdet.Exit();
_screenExitStatus.WaitOne();
}
} }
Dispose(); Dispose();
@ -627,15 +632,11 @@ namespace Ryujinx.Ui
private void Exit_Pressed(object sender, EventArgs args) private void Exit_Pressed(object sender, EventArgs args)
{ {
_gLWigdet?.Exit();
End(_emulationContext); End(_emulationContext);
} }
private void Window_Close(object sender, DeleteEventArgs args) private void Window_Close(object sender, DeleteEventArgs args)
{ {
_gLWigdet?.Exit();
End(_emulationContext); End(_emulationContext);
} }

View file

@ -0,0 +1,35 @@
using OpenTK.Graphics;
using OpenTK.Platform;
using System;
using System.Threading;
namespace Ryujinx.Ui
{
class ScopedGLContext : IDisposable
{
private IGraphicsContext _graphicsContext;
private static readonly object _lock = new object();
public ScopedGLContext(IWindowInfo windowInfo, IGraphicsContext graphicsContext)
{
_graphicsContext = graphicsContext;
Monitor.Enter(_lock);
MakeCurrent(windowInfo);
}
private void MakeCurrent(IWindowInfo windowInfo)
{
_graphicsContext.MakeCurrent(windowInfo);
}
public void Dispose()
{
MakeCurrent(null);
Monitor.Exit(_lock);
}
}
}