diff --git a/Ryujinx.Ava/AppHost.cs b/Ryujinx.Ava/AppHost.cs index 16bd81e60718..ce9ca594070c 100644 --- a/Ryujinx.Ava/AppHost.cs +++ b/Ryujinx.Ava/AppHost.cs @@ -100,7 +100,6 @@ public class AppHost : IDisposable private ManualResetEventSlim _vsyncResetEvent; private AdjustableRenderTimer _renderTimer; private bool _frameRateUnlocked; - private bool _isGALthreaded; public event EventHandler AppExit; public event EventHandler StatusUpdatedEvent; @@ -646,14 +645,14 @@ private void InitializeSwitchInstance() BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading; - _isGALthreaded = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading); + var isGALthreaded = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading); - if (_isGALthreaded) + if (isGALthreaded) { renderer = new ThreadedRenderer(renderer); } - Logger.Info?.PrintMsg(LogClass.Gpu, $"Backend Threading ({threadingMode}): {_isGALthreaded}"); + Logger.Info?.PrintMsg(LogClass.Gpu, $"Backend Threading ({threadingMode}): {isGALthreaded}"); if (ConfigurationState.Instance.System.AudioBackend.Value == AudioBackend.SDL2) { @@ -881,18 +880,8 @@ private unsafe void RenderLoop() _renderer.Window.SetSize((int)(Width * Program.WindowScaleFactor), (int)(Height * Program.WindowScaleFactor)); - if (!_isGALthreaded) - { - Renderer.MakeCurrent(null); - } - Device.Gpu.Renderer.RunLoop(() => { - if (!_isGALthreaded) - { - Renderer.MakeCurrent(); - } - Device.Gpu.SetGpuThread(); Device.Gpu.InitializeShaderCache(); Translator.IsReadyForTranslation.Set(); @@ -903,25 +892,24 @@ private unsafe void RenderLoop() _renderTimer.Tick += RenderTimer_Tick; UpdateTimer(ConfigurationState.Instance.Graphics.HostRefreshRate.Value); - if (!_isGALthreaded) - { - Renderer.MakeCurrent(null); - } - - Renderer.IsThreaded = _isGALthreaded; - Renderer.Start(); - EventHandler renderPass = (object s, EventArgs e) => + Renderer.QueueRender(); + + while (_isActive) { - if (!Device.IsFrameAvailable && Device.WaitFifo()) + _ticks += _chrono.ElapsedTicks; + + _chrono.Restart(); + + if (Device.WaitFifo()) { Device.Statistics.RecordFifoStart(); Device.ProcessFrame(); Device.Statistics.RecordFifoEnd(); } - if (Device.ConsumeFrameAvailable()) + while (Device.ConsumeFrameAvailable()) { if (!_renderingStarted) { @@ -929,27 +917,8 @@ private unsafe void RenderLoop() _parent.SwitchToGameControl(); } - Device.PresentFrame(Renderer.Present); + Device.PresentFrame(Present); } - else - { - Renderer.Continue(); - } - }; - - Renderer.Rendered += renderPass; - - Renderer.QueueRender(); - - while (_isActive) - { - _vsyncResetEvent.Wait(); - - _vsyncResetEvent.Reset(); - - _ticks += _chrono.ElapsedTicks; - - _chrono.Restart(); if (_ticks >= _ticksPerFrame) { @@ -980,8 +949,6 @@ private unsafe void RenderLoop() Renderer.Stop(); - Renderer.Rendered -= renderPass; - _renderTimer.Tick -= RenderTimer_Tick; _renderTimer.Dispose(); _vsyncResetEvent.Set(); @@ -996,10 +963,28 @@ private unsafe void RenderLoop() private void RenderTimer_Tick(TimeSpan obj) { - Renderer.QueueRender(); _vsyncResetEvent.Set(); } + private bool Present(int image) + { + bool presented = Renderer.Present(image); + + try + { + if (_isActive && !_frameRateUnlocked) + { + _vsyncResetEvent.Wait(); + + } + + _vsyncResetEvent.Reset(); + } + catch (Exception) { } + + return presented; + } + public async Task ShowExitPrompt() { bool shouldExit = !ConfigurationState.Instance.ShowConfirmExit; diff --git a/Ryujinx.Ava/Ui/Controls/OpenGlRenderer.cs b/Ryujinx.Ava/Ui/Controls/OpenGlRenderer.cs deleted file mode 100644 index 5afa7adfb28e..000000000000 --- a/Ryujinx.Ava/Ui/Controls/OpenGlRenderer.cs +++ /dev/null @@ -1,110 +0,0 @@ -/*using Avalonia; -using Avalonia.Controls; -using Avalonia.OpenGL; -using Avalonia.OpenGL.Controls; -using Avalonia.Platform; -using Avalonia.Win32; -using OpenTK.Graphics.OpenGL; -using OpenTK.Mathematics; -using Ryujinx.Common.Configuration; -using SPB.Graphics; -using SPB.Graphics.OpenGL; -using SPB.Platform; -using SPB.Platform.Win32; -using SPB.Windowing; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace Ryujinx.Ava.Ui.Controls -{ - internal class OpenGlRenderer : RendererControl - { - public OpenGlRenderer(int major, int minor, GraphicsDebugLevel graphicsDebugLevel) - { - Major = major; - Minor = minor; - DebugLevel = graphicsDebugLevel; - } - - protected override void OnOpenGlInit(GlInterface gl, int fb) - { - base.OnOpenGlInit(gl, fb); - - OpenGLContextBase mainContext = PrimaryContext; - - CreateWindow(mainContext); - - Window.SwapInterval = 0; - - OnInitialized(gl); - - _framebuffer = GL.GenFramebuffer(); - } - - protected override void OnRender(GlInterface gl, int fb) - { - if (GameContext == null || !IsStarted) - { - return; - } - - int current_texture = GL.GetInteger(GetPName.Texture2D); - GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); - - if (!IsThreaded) - { - GetControlContext().AsOpenGLContextBase().MakeCurrent(null); - MakeCurrent(); - } - - CallRenderEvent(); - - if (!IsThreaded) - { - MakeCurrent(null); - } - - if (Image == 0) - { - return; - } - - var disposableContext = !IsThreaded ? GetControlContext().EnsureCurrent() : null; - - GL.WaitSync(Fence, WaitSyncFlags.None, ulong.MaxValue); - - GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer); - GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, Image, 0); - - GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _framebuffer); - GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fb); - GL.ReadBuffer(ReadBufferMode.ColorAttachment0); - GL.BlitFramebuffer(0, - 0, - (int)RenderSize.Width, - (int)RenderSize.Height, - 0, - (int)RenderSize.Height, - (int)RenderSize.Width, - 0, - ClearBufferMask.ColorBufferBit, - BlitFramebufferFilter.Linear); - - GL.BindFramebuffer(FramebufferTarget.Framebuffer, fb); - - disposableContext?.Dispose(); - } - - protected override void OnOpenGlDeinit(GlInterface gl, int fb) - { - base.OnOpenGlDeinit(gl, fb); - - Window.SwapInterval = 1; - } - } -} -*/ \ No newline at end of file diff --git a/Ryujinx.Ava/Ui/Controls/RendererControl.cs b/Ryujinx.Ava/Ui/Controls/RendererControl.cs index ca50083615f4..75b78167e4c2 100644 --- a/Ryujinx.Ava/Ui/Controls/RendererControl.cs +++ b/Ryujinx.Ava/Ui/Controls/RendererControl.cs @@ -38,7 +38,6 @@ public class RendererControl : Control public bool IsStarted { get; private set; } protected IntPtr Fence { get; set; } = IntPtr.Zero; - public bool IsThreaded { get; internal set; } public int Major { get; } public int Minor { get; } @@ -90,20 +89,6 @@ public override void Render(DrawingContext context) return; } - GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); - - if (!IsThreaded) - { - MakeCurrent(); - } - - CallRenderEvent(); - - if (!IsThreaded) - { - MakeCurrent(null); - } - if (Image == 0) { return; @@ -115,22 +100,12 @@ public override void Render(DrawingContext context) _postFrameResetEvent.Set(); } - protected void CallRenderEvent() - { - _preFrameResetEvent.Reset(); - Rendered?.Invoke(this, EventArgs.Empty); - _preFrameResetEvent.Wait(); - } - - public void Continue() - { - _preFrameResetEvent?.Set(); - } - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { - Continue(); - _preFrameResetEvent.Dispose(); + _preFrameResetEvent?.Set(); + _preFrameResetEvent?.Dispose(); + _postFrameResetEvent?.Set(); + _postFrameResetEvent?.Dispose(); Window.SwapInterval = 1; base.OnDetachedFromVisualTree(e); @@ -172,6 +147,8 @@ internal bool Present(int image) { Image = image; + QueueRender(); + if(Fence != IntPtr.Zero) { GL.DeleteSync(Fence); @@ -180,18 +157,9 @@ internal bool Present(int image) Fence = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None); - _postFrameResetEvent.Reset(); - GL.Finish(); - Continue(); - - if (IsThreaded) - { - _postFrameResetEvent.Wait(); - } - - return false; + return true; } internal void Start() @@ -202,7 +170,6 @@ internal void Start() internal void Stop() { - Continue(); IsStarted = false; }