diff --git a/Directory.Packages.props.orig b/Directory.Packages.props.orig
new file mode 100644
index 000000000..8f22ea60c
--- /dev/null
+++ b/Directory.Packages.props.orig
@@ -0,0 +1,67 @@
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+<<<<<<< HEAD
+
+
+
+=======
+
+
+
+>>>>>>> 45a6dffcf (Bump SharpMetal)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+<<<<<<< HEAD
+
+
+
+=======
+
+
+
+
+>>>>>>> 45a6dffcf (Bump SharpMetal)
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Ryujinx.Graphics.Metal/CounterEvent.cs b/src/Ryujinx.Graphics.Metal/CounterEvent.cs
index eec810991..1773b9b63 100644
--- a/src/Ryujinx.Graphics.Metal/CounterEvent.cs
+++ b/src/Ryujinx.Graphics.Metal/CounterEvent.cs
@@ -2,7 +2,7 @@ using Ryujinx.Graphics.GAL;
namespace Ryujinx.Graphics.Metal
{
- public class CounterEvent : ICounterEvent
+ class CounterEvent : ICounterEvent
{
public CounterEvent()
diff --git a/src/Ryujinx.Graphics.Metal/FormatCapabilities.cs b/src/Ryujinx.Graphics.Metal/FormatCapabilities.cs
deleted file mode 100644
index 6dc56c59b..000000000
--- a/src/Ryujinx.Graphics.Metal/FormatCapabilities.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using SharpMetal;
-
-namespace Ryujinx.Graphics.Metal
-{
- static class FormatCapabilities
- {
- public static MTLPixelFormat ConvertToMTLFormat(GAL.Format srcFormat)
- {
- var format = FormatTable.GetFormat(srcFormat);
-
- return format;
- }
- }
-}
\ No newline at end of file
diff --git a/src/Ryujinx.Graphics.Metal/HardwareInfo.cs b/src/Ryujinx.Graphics.Metal/HardwareInfo.cs
index 3ca7cdfca..13566dbd8 100644
--- a/src/Ryujinx.Graphics.Metal/HardwareInfo.cs
+++ b/src/Ryujinx.Graphics.Metal/HardwareInfo.cs
@@ -6,8 +6,8 @@ namespace Ryujinx.Graphics.Metal
static partial class HardwareInfoTools
{
- private readonly static IntPtr kCFAllocatorDefault = IntPtr.Zero;
- private readonly static UInt32 kCFStringEncodingASCII = 0x0600;
+ private readonly static IntPtr _kCFAllocatorDefault = IntPtr.Zero;
+ private readonly static UInt32 _kCFStringEncodingASCII = 0x0600;
private const string IOKit = "/System/Library/Frameworks/IOKit.framework/IOKit";
private const string CoreFoundation = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
@@ -47,8 +47,8 @@ namespace Ryujinx.Graphics.Metal
{
var serviceDict = IOServiceMatching("IOGPU");
var service = IOServiceGetMatchingService(IntPtr.Zero, serviceDict);
- var cfString = CFStringCreateWithCString(kCFAllocatorDefault, "vendor-id", kCFStringEncodingASCII);
- var cfProperty = IORegistryEntryCreateCFProperty(service, cfString, kCFAllocatorDefault, 0);
+ var cfString = CFStringCreateWithCString(_kCFAllocatorDefault, "vendor-id", _kCFStringEncodingASCII);
+ var cfProperty = IORegistryEntryCreateCFProperty(service, cfString, _kCFAllocatorDefault, 0);
byte[] buffer = new byte[4];
var bufferPtr = CFDataGetBytePtr(cfProperty);
@@ -63,13 +63,13 @@ namespace Ryujinx.Graphics.Metal
{
var serviceDict = IOServiceMatching("IOGPU");
var service = IOServiceGetMatchingService(IntPtr.Zero, serviceDict);
- var cfString = CFStringCreateWithCString(kCFAllocatorDefault, "model", kCFStringEncodingASCII);
- var cfProperty = IORegistryEntryCreateCFProperty(service, cfString, kCFAllocatorDefault, 0);
+ var cfString = CFStringCreateWithCString(_kCFAllocatorDefault, "model", _kCFStringEncodingASCII);
+ var cfProperty = IORegistryEntryCreateCFProperty(service, cfString, _kCFAllocatorDefault, 0);
char[] buffer = new char[64];
IntPtr bufferPtr = Marshal.AllocHGlobal(buffer.Length);
- if (CFStringGetCString(cfProperty, bufferPtr, buffer.Length, kCFStringEncodingASCII))
+ if (CFStringGetCString(cfProperty, bufferPtr, buffer.Length, _kCFStringEncodingASCII))
{
var model = Marshal.PtrToStringUTF8(bufferPtr);
Marshal.FreeHGlobal(bufferPtr);
diff --git a/src/Ryujinx.Graphics.Metal/HelperShaders.cs b/src/Ryujinx.Graphics.Metal/HelperShaders.cs
index 0864839fd..a4517b7ce 100644
--- a/src/Ryujinx.Graphics.Metal/HelperShaders.cs
+++ b/src/Ryujinx.Graphics.Metal/HelperShaders.cs
@@ -30,17 +30,18 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
- public struct HelperShader
+ public readonly struct HelperShader
{
- private MTLRenderPipelineState _pipelineState;
+ private readonly MTLRenderPipelineState _pipelineState;
public static implicit operator MTLRenderPipelineState(HelperShader shader) => shader._pipelineState;
public HelperShader(MTLDevice device, MTLLibrary library, string vertex, string fragment)
{
- var renderPipelineDescriptor = new MTLRenderPipelineDescriptor();
-
- renderPipelineDescriptor.VertexFunction = library.NewFunction(StringHelper.NSString(vertex));;
- renderPipelineDescriptor.FragmentFunction = library.NewFunction(StringHelper.NSString(fragment));
+ var renderPipelineDescriptor = new MTLRenderPipelineDescriptor
+ {
+ VertexFunction = library.NewFunction(StringHelper.NSString(vertex)),
+ FragmentFunction = library.NewFunction(StringHelper.NSString(fragment))
+ };
renderPipelineDescriptor.ColorAttachments.Object(0).SetBlendingEnabled(true);
renderPipelineDescriptor.ColorAttachments.Object(0).PixelFormat = MTLPixelFormat.BGRA8Unorm;
renderPipelineDescriptor.ColorAttachments.Object(0).SourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha;
diff --git a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs
index d3d73f551..21c8c49b4 100644
--- a/src/Ryujinx.Graphics.Metal/MetalRenderer.cs
+++ b/src/Ryujinx.Graphics.Metal/MetalRenderer.cs
@@ -38,10 +38,12 @@ namespace Ryujinx.Graphics.Metal
var layer = _getMetalLayer();
layer.Device = _device;
- var captureDescriptor = new MTLCaptureDescriptor();
- captureDescriptor.CaptureObject = _queue;
- captureDescriptor.Destination = MTLCaptureDestination.GPUTraceDocument;
- captureDescriptor.OutputURL = NSURL.FileURLWithPath(StringHelper.NSString("/Users/isaacmarovitz/Desktop/Trace.gputrace"));
+ var captureDescriptor = new MTLCaptureDescriptor
+ {
+ CaptureObject = _queue,
+ Destination = MTLCaptureDestination.GPUTraceDocument,
+ OutputURL = NSURL.FileURLWithPath(StringHelper.NSString("/Users/isaacmarovitz/Desktop/Trace.gputrace"))
+ };
var captureError = new NSError(IntPtr.Zero);
MTLCaptureManager.SharedCaptureManager().StartCapture(captureDescriptor, ref captureError);
if (captureError != IntPtr.Zero)
@@ -216,7 +218,7 @@ namespace Ryujinx.Graphics.Metal
{
MTLBuffer mtlBuffer = new(Unsafe.As(ref buffer));
var span = new Span(mtlBuffer.Contents.ToPointer(), (int)mtlBuffer.Length);
- data.CopyTo(span.Slice(offset));
+ data.CopyTo(span[offset..]);
mtlBuffer.DidModifyRange(new NSRange
{
location = (ulong)offset,
diff --git a/src/Ryujinx.Graphics.Metal/Pipeline.cs b/src/Ryujinx.Graphics.Metal/Pipeline.cs
index c8607c5be..6a3d9a801 100644
--- a/src/Ryujinx.Graphics.Metal/Pipeline.cs
+++ b/src/Ryujinx.Graphics.Metal/Pipeline.cs
@@ -11,7 +11,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
- public class Pipeline : IPipeline, IDisposable
+ class Pipeline : IPipeline, IDisposable
{
private readonly MTLDevice _device;
private readonly MTLCommandQueue _mtlCommandQueue;
@@ -88,8 +88,13 @@ namespace Ryujinx.Graphics.Metal
return computeCommandEncoder;
}
- public void Present(CAMetalDrawable drawable, Texture texture)
+ public void Present(CAMetalDrawable drawable, ITexture texture)
{
+ if (texture is not Texture tex)
+ {
+ return;
+ }
+
EndCurrentPass();
var descriptor = new MTLRenderPassDescriptor();
@@ -108,7 +113,7 @@ namespace Ryujinx.Graphics.Metal
MipFilter = MTLSamplerMipFilter.NotMipmapped
});
- renderCommandEncoder.SetFragmentTexture(texture.MTLTexture, 0);
+ renderCommandEncoder.SetFragmentTexture(tex.MTLTexture, 0);
renderCommandEncoder.SetFragmentSamplerState(sampler, 0);
renderCommandEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 6);
@@ -162,7 +167,7 @@ namespace Ryujinx.Graphics.Metal
public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
{
- _clearColor = new MTLClearColor { red = color.Red, green = color.Green, blue = color.Blue, alpha = color.Alpha};
+ _clearColor = new MTLClearColor { red = color.Red, green = color.Green, blue = color.Blue, alpha = color.Alpha };
}
public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue,
diff --git a/src/Ryujinx.Graphics.Metal/Program.cs b/src/Ryujinx.Graphics.Metal/Program.cs
index add16462f..0a748bfcf 100644
--- a/src/Ryujinx.Graphics.Metal/Program.cs
+++ b/src/Ryujinx.Graphics.Metal/Program.cs
@@ -2,13 +2,8 @@ using Ryujinx.Graphics.GAL;
namespace Ryujinx.Graphics.Metal
{
- public class Program : IProgram
+ class Program : IProgram
{
- public void Dispose()
- {
- return;
- }
-
public ProgramLinkStatus CheckProgramLink(bool blocking)
{
return ProgramLinkStatus.Failure;
@@ -16,7 +11,12 @@ namespace Ryujinx.Graphics.Metal
public byte[] GetBinary()
{
- return new byte[] {};
+ return ""u8.ToArray();
+ }
+
+ public void Dispose()
+ {
+ return;
}
}
}
diff --git a/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs b/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs
index db946d8b8..e24b49090 100644
--- a/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs
+++ b/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs
@@ -1,5 +1,6 @@
using Ryujinx.Graphics.GAL;
using SharpMetal.Metal;
+using System;
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
@@ -7,8 +8,9 @@ namespace Ryujinx.Graphics.Metal
[SupportedOSPlatform("macos")]
struct RenderEncoderState
{
- private MTLDevice _device;
-
+ private readonly MTLDevice _device;
+ // TODO: Work with more than one pipeline state
+ private readonly MTLRenderPipelineState _copyPipeline;
private MTLDepthStencilState _depthStencilState = null;
private MTLCompareFunction _depthCompareFunction = MTLCompareFunction.Always;
@@ -17,7 +19,6 @@ namespace Ryujinx.Graphics.Metal
private MTLStencilDescriptor _backFaceStencil = null;
private MTLStencilDescriptor _frontFaceStencil = null;
- public MTLRenderPipelineState CopyPipeline;
public PrimitiveTopology Topology = PrimitiveTopology.Triangles;
public MTLCullMode CullMode = MTLCullMode.None;
public MTLWinding Winding = MTLWinding.Clockwise;
@@ -25,15 +26,19 @@ namespace Ryujinx.Graphics.Metal
public RenderEncoderState(MTLRenderPipelineState copyPipeline, MTLDevice device)
{
_device = device;
- CopyPipeline = copyPipeline;
+ _copyPipeline = copyPipeline;
}
- public void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder)
+ public readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder)
{
- renderCommandEncoder.SetRenderPipelineState(CopyPipeline);
+ renderCommandEncoder.SetRenderPipelineState(_copyPipeline);
renderCommandEncoder.SetCullMode(CullMode);
renderCommandEncoder.SetFrontFacingWinding(Winding);
- // renderCommandEncoder.SetDepthStencilState(_depthStencilState);
+
+ if (_depthStencilState != null)
+ {
+ renderCommandEncoder.SetDepthStencilState(_depthStencilState);
+ }
}
public MTLDepthStencilState UpdateStencilState(MTLStencilDescriptor backFace, MTLStencilDescriptor frontFace)
diff --git a/src/Ryujinx.Graphics.Metal/Sampler.cs b/src/Ryujinx.Graphics.Metal/Sampler.cs
index a40040c5f..cc1923cc3 100644
--- a/src/Ryujinx.Graphics.Metal/Sampler.cs
+++ b/src/Ryujinx.Graphics.Metal/Sampler.cs
@@ -3,13 +3,13 @@ using SharpMetal.Metal;
namespace Ryujinx.Graphics.Metal
{
- public class Sampler : ISampler
+ class Sampler : ISampler
{
- private MTLSamplerState _mtlSamplerState;
+ // private readonly MTLSamplerState _mtlSamplerState;
public Sampler(MTLSamplerState mtlSamplerState)
{
- _mtlSamplerState = mtlSamplerState;
+ // _mtlSamplerState = mtlSamplerState;
}
public void Dispose()
diff --git a/src/Ryujinx.Graphics.Metal/Texture.cs b/src/Ryujinx.Graphics.Metal/Texture.cs
index 7074e7da3..ea3a8cba8 100644
--- a/src/Ryujinx.Graphics.Metal/Texture.cs
+++ b/src/Ryujinx.Graphics.Metal/Texture.cs
@@ -9,7 +9,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
- public class Texture : ITexture, IDisposable
+ class Texture : ITexture, IDisposable
{
private readonly TextureCreateInfo _info;
private readonly Pipeline _pipeline;
@@ -28,12 +28,14 @@ namespace Ryujinx.Graphics.Metal
_pipeline = pipeline;
_info = info;
- var descriptor = new MTLTextureDescriptor();
- descriptor.PixelFormat = FormatTable.GetFormat(Info.Format);
- descriptor.Usage = MTLTextureUsage.ShaderRead | MTLTextureUsage.ShaderWrite | MTLTextureUsage.RenderTarget;
- descriptor.Width = (ulong)Width;
- descriptor.Height = (ulong)Height;
- descriptor.Depth = (ulong)Depth;
+ var descriptor = new MTLTextureDescriptor
+ {
+ PixelFormat = FormatTable.GetFormat(Info.Format),
+ Usage = MTLTextureUsage.ShaderRead | MTLTextureUsage.ShaderWrite | MTLTextureUsage.RenderTarget,
+ Width = (ulong)Width,
+ Height = (ulong)Height,
+ Depth = (ulong)Depth
+ };
descriptor.Depth = (ulong)Info.Depth;
descriptor.SampleCount = (ulong)Info.Samples;
descriptor.MipmapLevelCount = (ulong)Info.Levels;
diff --git a/src/Ryujinx.Graphics.Metal/Window.cs b/src/Ryujinx.Graphics.Metal/Window.cs
index 563f888af..f8ddca3fe 100644
--- a/src/Ryujinx.Graphics.Metal/Window.cs
+++ b/src/Ryujinx.Graphics.Metal/Window.cs
@@ -8,7 +8,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
- public class Window : IWindow, IDisposable
+ class Window : IWindow, IDisposable
{
private readonly MetalRenderer _renderer;
private readonly CAMetalLayer _metalLayer;
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenHelper.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenHelper.cs
index 632ad6bf2..c8757ad35 100644
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenHelper.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenHelper.cs
@@ -106,26 +106,26 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
Add(Instruction.ShiftRightS32, InstType.OpBinary, ">>");
Add(Instruction.ShiftRightU32, InstType.OpBinary, ">>");
// TODO: Shuffle funcs
- Add(Instruction.Shuffle, 0);
- Add(Instruction.ShuffleDown, 0);
- Add(Instruction.ShuffleUp, 0);
- Add(Instruction.ShuffleXor, 0);
- Add(Instruction.Sine, InstType.CallUnary, "sin");
- Add(Instruction.SquareRoot, InstType.CallUnary, "sqrt");
- Add(Instruction.Store, InstType.Special);
- Add(Instruction.Subtract, InstType.OpBinary, "-");
+ Add(Instruction.Shuffle, 0);
+ Add(Instruction.ShuffleDown, 0);
+ Add(Instruction.ShuffleUp, 0);
+ Add(Instruction.ShuffleXor, 0);
+ Add(Instruction.Sine, InstType.CallUnary, "sin");
+ Add(Instruction.SquareRoot, InstType.CallUnary, "sqrt");
+ Add(Instruction.Store, InstType.Special);
+ Add(Instruction.Subtract, InstType.OpBinary, "-");
// TODO: Swizzle add
- Add(Instruction.SwizzleAdd, InstType.Special);
- Add(Instruction.TextureSample, InstType.Special);
- Add(Instruction.TextureSize, InstType.Special);
- Add(Instruction.Truncate, InstType.CallUnary, "trunc");
+ Add(Instruction.SwizzleAdd, InstType.Special);
+ Add(Instruction.TextureSample, InstType.Special);
+ Add(Instruction.TextureSize, InstType.Special);
+ Add(Instruction.Truncate, InstType.CallUnary, "trunc");
Add(Instruction.UnpackDouble2x32, 0); // MSL does not have a 64-bit FP
- Add(Instruction.UnpackHalf2x16, InstType.CallUnary, "unpack_unorm2x16_to_half");
+ Add(Instruction.UnpackHalf2x16, InstType.CallUnary, "unpack_unorm2x16_to_half");
Add(Instruction.VectorExtract, InstType.Special);
- Add(Instruction.VoteAll, InstType.CallUnary, "simd_all");
+ Add(Instruction.VoteAll, InstType.CallUnary, "simd_all");
// TODO: https://github.com/KhronosGroup/SPIRV-Cross/blob/bccaa94db814af33d8ef05c153e7c34d8bd4d685/reference/shaders-msl/comp/shader_group_vote.msl21.comp#L9
- Add(Instruction.VoteAllEqual, InstType.Special);
- Add(Instruction.VoteAny, InstType.CallUnary, "simd_any");
+ Add(Instruction.VoteAllEqual, InstType.Special);
+ Add(Instruction.VoteAny, InstType.CallUnary, "simd_any");
#pragma warning restore IDE0055
}
diff --git a/src/Ryujinx/AppHost.cs b/src/Ryujinx/AppHost.cs
index 9cd44addb..5747b450e 100644
--- a/src/Ryujinx/AppHost.cs
+++ b/src/Ryujinx/AppHost.cs
@@ -894,12 +894,23 @@ namespace Ryujinx.Ava
VirtualFileSystem.ReloadKeySet();
// Initialize Renderer.
- IRenderer renderer = ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.OpenGl
- ? new OpenGLRenderer()
- : VulkanRenderer.Create(
+ IRenderer renderer;
+
+ if (ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.Vulkan)
+ {
+ renderer = VulkanRenderer.Create(
ConfigurationState.Instance.Graphics.PreferredGpu,
(RendererHost.EmbeddedWindow as EmbeddedWindowVulkan)!.CreateSurface,
VulkanHelper.GetRequiredInstanceExtensions);
+ }
+ else if (ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.Metal && OperatingSystem.IsMacOS())
+ {
+ renderer = new MetalRenderer();
+ }
+ else
+ {
+ renderer = new OpenGLRenderer();
+ }
BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
diff --git a/src/Ryujinx/AppHost.cs.orig b/src/Ryujinx/AppHost.cs.orig
new file mode 100644
index 000000000..302ef914c
--- /dev/null
+++ b/src/Ryujinx/AppHost.cs.orig
@@ -0,0 +1,1226 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Input;
+using Avalonia.Threading;
+using LibHac.Tools.FsSystem;
+using Ryujinx.Audio.Backends.Dummy;
+using Ryujinx.Audio.Backends.OpenAL;
+using Ryujinx.Audio.Backends.SDL2;
+using Ryujinx.Audio.Backends.SoundIo;
+using Ryujinx.Audio.Integration;
+using Ryujinx.Ava.Common;
+using Ryujinx.Ava.Common.Locale;
+using Ryujinx.Ava.Input;
+using Ryujinx.Ava.UI.Helpers;
+using Ryujinx.Ava.UI.Models;
+using Ryujinx.Ava.UI.Renderer;
+using Ryujinx.Ava.UI.ViewModels;
+using Ryujinx.Ava.UI.Windows;
+using Ryujinx.Common;
+using Ryujinx.Common.Configuration;
+using Ryujinx.Common.Configuration.Multiplayer;
+using Ryujinx.Common.Logging;
+using Ryujinx.Common.SystemInterop;
+using Ryujinx.Common.Utilities;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.GAL.Multithreading;
+using Ryujinx.Graphics.Gpu;
+using Ryujinx.Graphics.OpenGL;
+using Ryujinx.Graphics.Vulkan;
+using Ryujinx.Graphics.Metal;
+using Ryujinx.HLE;
+using Ryujinx.HLE.FileSystem;
+using Ryujinx.HLE.HOS;
+using Ryujinx.HLE.HOS.Services.Account.Acc;
+using Ryujinx.HLE.HOS.SystemState;
+using Ryujinx.Input;
+using Ryujinx.Input.HLE;
+using Ryujinx.UI.App.Common;
+using Ryujinx.UI.Common;
+using Ryujinx.UI.Common.Configuration;
+using Ryujinx.UI.Common.Helper;
+using Silk.NET.Vulkan;
+using SixLabors.ImageSharp;
+using SixLabors.ImageSharp.Formats.Png;
+using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.Processing;
+using SPB.Graphics.Vulkan;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using static Ryujinx.Ava.UI.Helpers.Win32NativeInterop;
+using AntiAliasing = Ryujinx.Common.Configuration.AntiAliasing;
+using Image = SixLabors.ImageSharp.Image;
+using InputManager = Ryujinx.Input.HLE.InputManager;
+using IRenderer = Ryujinx.Graphics.GAL.IRenderer;
+using Key = Ryujinx.Input.Key;
+using MouseButton = Ryujinx.Input.MouseButton;
+using ScalingFilter = Ryujinx.Common.Configuration.ScalingFilter;
+using Size = Avalonia.Size;
+using Switch = Ryujinx.HLE.Switch;
+
+namespace Ryujinx.Ava
+{
+ internal class AppHost
+ {
+ private const int CursorHideIdleTime = 5; // Hide Cursor seconds.
+ private const float MaxResolutionScale = 4.0f; // Max resolution hotkeys can scale to before wrapping.
+ private const int TargetFps = 60;
+ private const float VolumeDelta = 0.05f;
+
+ private static readonly Cursor _invisibleCursor = new(StandardCursorType.None);
+ private readonly IntPtr _invisibleCursorWin;
+ private readonly IntPtr _defaultCursorWin;
+
+ private readonly long _ticksPerFrame;
+ private readonly Stopwatch _chrono;
+ private long _ticks;
+
+ private readonly AccountManager _accountManager;
+ private readonly UserChannelPersistence _userChannelPersistence;
+ private readonly InputManager _inputManager;
+
+ private readonly MainWindowViewModel _viewModel;
+ private readonly IKeyboard _keyboardInterface;
+ private readonly TopLevel _topLevel;
+ public RendererHost RendererHost;
+
+ private readonly GraphicsDebugLevel _glLogLevel;
+ private float _newVolume;
+ private KeyboardHotkeyState _prevHotkeyState;
+
+ private long _lastCursorMoveTime;
+ private bool _isCursorInRenderer = true;
+
+ private bool _isStopped;
+ private bool _isActive;
+ private bool _renderingStarted;
+
+ private readonly ManualResetEvent _gpuDoneEvent;
+
+ private IRenderer _renderer;
+ private readonly Thread _renderingThread;
+ private readonly CancellationTokenSource _gpuCancellationTokenSource;
+ private WindowsMultimediaTimerResolution _windowsMultimediaTimerResolution;
+
+ private bool _dialogShown;
+ private readonly bool _isFirmwareTitle;
+
+ private readonly object _lockObject = new();
+
+ public event EventHandler AppExit;
+ public event EventHandler StatusInitEvent;
+ public event EventHandler StatusUpdatedEvent;
+
+ public VirtualFileSystem VirtualFileSystem { get; }
+ public ContentManager ContentManager { get; }
+ public NpadManager NpadManager { get; }
+ public TouchScreenManager TouchScreenManager { get; }
+ public Switch Device { get; set; }
+
+ public int Width { get; private set; }
+ public int Height { get; private set; }
+ public string ApplicationPath { get; private set; }
+ public bool ScreenshotRequested { get; set; }
+
+ public AppHost(
+ RendererHost renderer,
+ InputManager inputManager,
+ string applicationPath,
+ VirtualFileSystem virtualFileSystem,
+ ContentManager contentManager,
+ AccountManager accountManager,
+ UserChannelPersistence userChannelPersistence,
+ MainWindowViewModel viewmodel,
+ TopLevel topLevel)
+ {
+ _viewModel = viewmodel;
+ _inputManager = inputManager;
+ _accountManager = accountManager;
+ _userChannelPersistence = userChannelPersistence;
+ _renderingThread = new Thread(RenderLoop) { Name = "GUI.RenderThread" };
+ _lastCursorMoveTime = Stopwatch.GetTimestamp();
+ _glLogLevel = ConfigurationState.Instance.Logger.GraphicsDebugLevel;
+ _topLevel = topLevel;
+
+ _inputManager.SetMouseDriver(new AvaloniaMouseDriver(_topLevel, renderer));
+
+ _keyboardInterface = (IKeyboard)_inputManager.KeyboardDriver.GetGamepad("0");
+
+ NpadManager = _inputManager.CreateNpadManager();
+ TouchScreenManager = _inputManager.CreateTouchScreenManager();
+ ApplicationPath = applicationPath;
+ VirtualFileSystem = virtualFileSystem;
+ ContentManager = contentManager;
+
+ RendererHost = renderer;
+
+ _chrono = new Stopwatch();
+ _ticksPerFrame = Stopwatch.Frequency / TargetFps;
+
+ if (ApplicationPath.StartsWith("@SystemContent"))
+ {
+ ApplicationPath = VirtualFileSystem.SwitchPathToSystemPath(ApplicationPath);
+
+ _isFirmwareTitle = true;
+ }
+
+ ConfigurationState.Instance.HideCursor.Event += HideCursorState_Changed;
+
+ _topLevel.PointerMoved += TopLevel_PointerEnteredOrMoved;
+ _topLevel.PointerEntered += TopLevel_PointerEnteredOrMoved;
+ _topLevel.PointerExited += TopLevel_PointerExited;
+
+ if (OperatingSystem.IsWindows())
+ {
+ _invisibleCursorWin = CreateEmptyCursor();
+ _defaultCursorWin = CreateArrowCursor();
+ }
+
+ ConfigurationState.Instance.System.IgnoreMissingServices.Event += UpdateIgnoreMissingServicesState;
+ ConfigurationState.Instance.Graphics.AspectRatio.Event += UpdateAspectRatioState;
+ ConfigurationState.Instance.System.EnableDockedMode.Event += UpdateDockedModeState;
+ ConfigurationState.Instance.System.AudioVolume.Event += UpdateAudioVolumeState;
+ ConfigurationState.Instance.System.EnableDockedMode.Event += UpdateDockedModeState;
+ ConfigurationState.Instance.System.AudioVolume.Event += UpdateAudioVolumeState;
+ ConfigurationState.Instance.Graphics.AntiAliasing.Event += UpdateAntiAliasing;
+ ConfigurationState.Instance.Graphics.ScalingFilter.Event += UpdateScalingFilter;
+ ConfigurationState.Instance.Graphics.ScalingFilterLevel.Event += UpdateScalingFilterLevel;
+ ConfigurationState.Instance.Graphics.EnableColorSpacePassthrough.Event += UpdateColorSpacePassthrough;
+
+ ConfigurationState.Instance.System.EnableInternetAccess.Event += UpdateEnableInternetAccessState;
+ ConfigurationState.Instance.Multiplayer.LanInterfaceId.Event += UpdateLanInterfaceIdState;
+ ConfigurationState.Instance.Multiplayer.Mode.Event += UpdateMultiplayerModeState;
+
+ _gpuCancellationTokenSource = new CancellationTokenSource();
+ _gpuDoneEvent = new ManualResetEvent(false);
+ }
+
+ private void TopLevel_PointerEnteredOrMoved(object sender, PointerEventArgs e)
+ {
+ if (sender is MainWindow window)
+ {
+ _lastCursorMoveTime = Stopwatch.GetTimestamp();
+
+ var point = e.GetCurrentPoint(window).Position;
+ var bounds = RendererHost.EmbeddedWindow.Bounds;
+
+ _isCursorInRenderer = point.X >= bounds.X &&
+ point.X <= bounds.Width + bounds.X &&
+ point.Y >= bounds.Y &&
+ point.Y <= bounds.Height + bounds.Y;
+ }
+ }
+
+ private void TopLevel_PointerExited(object sender, PointerEventArgs e)
+ {
+ _isCursorInRenderer = false;
+ }
+
+ private void UpdateScalingFilterLevel(object sender, ReactiveEventArgs e)
+ {
+ _renderer.Window?.SetScalingFilter((Graphics.GAL.ScalingFilter)ConfigurationState.Instance.Graphics.ScalingFilter.Value);
+ _renderer.Window?.SetScalingFilterLevel(ConfigurationState.Instance.Graphics.ScalingFilterLevel.Value);
+ }
+
+ private void UpdateScalingFilter(object sender, ReactiveEventArgs e)
+ {
+ _renderer.Window?.SetScalingFilter((Graphics.GAL.ScalingFilter)ConfigurationState.Instance.Graphics.ScalingFilter.Value);
+ _renderer.Window?.SetScalingFilterLevel(ConfigurationState.Instance.Graphics.ScalingFilterLevel.Value);
+ }
+
+ private void UpdateColorSpacePassthrough(object sender, ReactiveEventArgs e)
+ {
+ _renderer.Window?.SetColorSpacePassthrough((bool)ConfigurationState.Instance.Graphics.EnableColorSpacePassthrough.Value);
+ }
+
+ private void ShowCursor()
+ {
+ Dispatcher.UIThread.Post(() =>
+ {
+ _viewModel.Cursor = Cursor.Default;
+
+ if (OperatingSystem.IsWindows())
+ {
+ SetCursor(_defaultCursorWin);
+ }
+ });
+ }
+
+ private void HideCursor()
+ {
+ Dispatcher.UIThread.Post(() =>
+ {
+ _viewModel.Cursor = _invisibleCursor;
+
+ if (OperatingSystem.IsWindows())
+ {
+ SetCursor(_invisibleCursorWin);
+ }
+ });
+ }
+
+ private void SetRendererWindowSize(Size size)
+ {
+ if (_renderer != null)
+ {
+ double scale = _topLevel.RenderScaling;
+
+ _renderer.Window?.SetSize((int)(size.Width * scale), (int)(size.Height * scale));
+ }
+ }
+
+ private void Renderer_ScreenCaptured(object sender, ScreenCaptureImageInfo e)
+ {
+ if (e.Data.Length > 0 && e.Height > 0 && e.Width > 0)
+ {
+ Task.Run(() =>
+ {
+ lock (_lockObject)
+ {
+ string applicationName = Device.Processes.ActiveApplication.Name;
+ string sanitizedApplicationName = FileSystemUtils.SanitizeFileName(applicationName);
+ DateTime currentTime = DateTime.Now;
+
+ string filename = $"{sanitizedApplicationName}_{currentTime.Year}-{currentTime.Month:D2}-{currentTime.Day:D2}_{currentTime.Hour:D2}-{currentTime.Minute:D2}-{currentTime.Second:D2}.png";
+
+ string directory = AppDataManager.Mode switch
+ {
+ AppDataManager.LaunchMode.Portable or AppDataManager.LaunchMode.Custom => Path.Combine(AppDataManager.BaseDirPath, "screenshots"),
+ _ => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Ryujinx"),
+ };
+
+ string path = Path.Combine(directory, filename);
+
+ try
+ {
+ Directory.CreateDirectory(directory);
+ }
+ catch (Exception ex)
+ {
+ Logger.Error?.Print(LogClass.Application, $"Failed to create directory at path {directory}. Error : {ex.GetType().Name}", "Screenshot");
+
+ return;
+ }
+
+ Image image = e.IsBgra ? Image.LoadPixelData(e.Data, e.Width, e.Height)
+ : Image.LoadPixelData(e.Data, e.Width, e.Height);
+
+ if (e.FlipX)
+ {
+ image.Mutate(x => x.Flip(FlipMode.Horizontal));
+ }
+
+ if (e.FlipY)
+ {
+ image.Mutate(x => x.Flip(FlipMode.Vertical));
+ }
+
+ image.SaveAsPng(path, new PngEncoder
+ {
+ ColorType = PngColorType.Rgb,
+ });
+
+ image.Dispose();
+
+ Logger.Notice.Print(LogClass.Application, $"Screenshot saved to {path}", "Screenshot");
+ }
+ });
+ }
+ else
+ {
+ Logger.Error?.Print(LogClass.Application, $"Screenshot is empty. Size : {e.Data.Length} bytes. Resolution : {e.Width}x{e.Height}", "Screenshot");
+ }
+ }
+
+ public void Start()
+ {
+ if (OperatingSystem.IsWindows())
+ {
+ _windowsMultimediaTimerResolution = new WindowsMultimediaTimerResolution(1);
+ }
+
+ DisplaySleep.Prevent();
+
+ NpadManager.Initialize(Device, ConfigurationState.Instance.Hid.InputConfig, ConfigurationState.Instance.Hid.EnableKeyboard, ConfigurationState.Instance.Hid.EnableMouse);
+ TouchScreenManager.Initialize(Device);
+
+ _viewModel.IsGameRunning = true;
+
+ Dispatcher.UIThread.InvokeAsync(() =>
+ {
+ _viewModel.Title = TitleHelper.ActiveApplicationTitle(Device.Processes.ActiveApplication, Program.Version);
+ });
+
+ _viewModel.SetUiProgressHandlers(Device);
+
+ RendererHost.BoundsChanged += Window_BoundsChanged;
+
+ _isActive = true;
+
+ _renderingThread.Start();
+
+ _viewModel.Volume = ConfigurationState.Instance.System.AudioVolume.Value;
+
+ MainLoop();
+
+ Exit();
+ }
+
+ private void UpdateIgnoreMissingServicesState(object sender, ReactiveEventArgs args)
+ {
+ if (Device != null)
+ {
+ Device.Configuration.IgnoreMissingServices = args.NewValue;
+ }
+ }
+
+ private void UpdateAspectRatioState(object sender, ReactiveEventArgs args)
+ {
+ if (Device != null)
+ {
+ Device.Configuration.AspectRatio = args.NewValue;
+ }
+ }
+
+ private void UpdateAntiAliasing(object sender, ReactiveEventArgs e)
+ {
+ _renderer?.Window?.SetAntiAliasing((Graphics.GAL.AntiAliasing)e.NewValue);
+ }
+
+ private void UpdateDockedModeState(object sender, ReactiveEventArgs e)
+ {
+ Device?.System.ChangeDockedModeState(e.NewValue);
+ }
+
+ private void UpdateAudioVolumeState(object sender, ReactiveEventArgs e)
+ {
+ Device?.SetVolume(e.NewValue);
+
+ Dispatcher.UIThread.Post(() =>
+ {
+ _viewModel.Volume = e.NewValue;
+ });
+ }
+
+ private void UpdateEnableInternetAccessState(object sender, ReactiveEventArgs e)
+ {
+ Device.Configuration.EnableInternetAccess = e.NewValue;
+ }
+
+ private void UpdateLanInterfaceIdState(object sender, ReactiveEventArgs e)
+ {
+ Device.Configuration.MultiplayerLanInterfaceId = e.NewValue;
+ }
+
+ private void UpdateMultiplayerModeState(object sender, ReactiveEventArgs e)
+ {
+ Device.Configuration.MultiplayerMode = e.NewValue;
+ }
+
+ public void ToggleVSync()
+ {
+ Device.EnableDeviceVsync = !Device.EnableDeviceVsync;
+ _renderer.Window.ChangeVSyncMode(Device.EnableDeviceVsync);
+ }
+
+ public void Stop()
+ {
+ _isActive = false;
+ }
+
+ private void Exit()
+ {
+ (_keyboardInterface as AvaloniaKeyboard)?.Clear();
+
+ if (_isStopped)
+ {
+ return;
+ }
+
+ _isStopped = true;
+ _isActive = false;
+ }
+
+ public void DisposeContext()
+ {
+ Dispose();
+
+ _isActive = false;
+
+ // NOTE: The render loop is allowed to stay alive until the renderer itself is disposed, as it may handle resource dispose.
+ // We only need to wait for all commands submitted during the main gpu loop to be processed.
+ _gpuDoneEvent.WaitOne();
+ _gpuDoneEvent.Dispose();
+
+ DisplaySleep.Restore();
+
+ NpadManager.Dispose();
+ TouchScreenManager.Dispose();
+ Device.Dispose();
+
+ DisposeGpu();
+
+ AppExit?.Invoke(this, EventArgs.Empty);
+ }
+
+ private void Dispose()
+ {
+ if (Device.Processes != null)
+ {
+ MainWindowViewModel.UpdateGameMetadata(Device.Processes.ActiveApplication.ProgramIdText);
+ }
+
+ ConfigurationState.Instance.System.IgnoreMissingServices.Event -= UpdateIgnoreMissingServicesState;
+ ConfigurationState.Instance.Graphics.AspectRatio.Event -= UpdateAspectRatioState;
+ ConfigurationState.Instance.System.EnableDockedMode.Event -= UpdateDockedModeState;
+ ConfigurationState.Instance.System.AudioVolume.Event -= UpdateAudioVolumeState;
+ ConfigurationState.Instance.Graphics.ScalingFilter.Event -= UpdateScalingFilter;
+ ConfigurationState.Instance.Graphics.ScalingFilterLevel.Event -= UpdateScalingFilterLevel;
+ ConfigurationState.Instance.Graphics.AntiAliasing.Event -= UpdateAntiAliasing;
+ ConfigurationState.Instance.Graphics.EnableColorSpacePassthrough.Event -= UpdateColorSpacePassthrough;
+
+ _topLevel.PointerMoved -= TopLevel_PointerEnteredOrMoved;
+ _topLevel.PointerEntered -= TopLevel_PointerEnteredOrMoved;
+ _topLevel.PointerExited -= TopLevel_PointerExited;
+
+ _gpuCancellationTokenSource.Cancel();
+ _gpuCancellationTokenSource.Dispose();
+
+ _chrono.Stop();
+ }
+
+ public void DisposeGpu()
+ {
+ if (OperatingSystem.IsWindows())
+ {
+ _windowsMultimediaTimerResolution?.Dispose();
+ _windowsMultimediaTimerResolution = null;
+ }
+
+ if (RendererHost.EmbeddedWindow is EmbeddedWindowOpenGL openGlWindow)
+ {
+ // Try to bind the OpenGL context before calling the shutdown event.
+ openGlWindow.MakeCurrent(false, false);
+
+ Device.DisposeGpu();
+
+ // Unbind context and destroy everything.
+ openGlWindow.MakeCurrent(true, false);
+ }
+ else
+ {
+ Device.DisposeGpu();
+ }
+ }
+
+ private void HideCursorState_Changed(object sender, ReactiveEventArgs state)
+ {
+ if (state.NewValue == HideCursorMode.OnIdle)
+ {
+ _lastCursorMoveTime = Stopwatch.GetTimestamp();
+ }
+ }
+
+ public async Task LoadGuestApplication()
+ {
+ InitializeSwitchInstance();
+ MainWindow.UpdateGraphicsConfig();
+
+ SystemVersion firmwareVersion = ContentManager.GetCurrentFirmwareVersion();
+
+ if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+ {
+ if (!SetupValidator.CanStartApplication(ContentManager, ApplicationPath, out UserError userError))
+ {
+ {
+ if (SetupValidator.CanFixStartApplication(ContentManager, ApplicationPath, userError, out firmwareVersion))
+ {
+ if (userError == UserError.NoFirmware)
+ {
+ UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
+ LocaleManager.Instance[LocaleKeys.DialogFirmwareNoFirmwareInstalledMessage],
+ LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogFirmwareInstallEmbeddedMessage, firmwareVersion.VersionString),
+ LocaleManager.Instance[LocaleKeys.InputDialogYes],
+ LocaleManager.Instance[LocaleKeys.InputDialogNo],
+ "");
+
+ if (result != UserResult.Yes)
+ {
+ await UserErrorDialog.ShowUserErrorDialog(userError);
+ Device.Dispose();
+
+ return false;
+ }
+ }
+
+ if (!SetupValidator.TryFixStartApplication(ContentManager, ApplicationPath, userError, out _))
+ {
+ await UserErrorDialog.ShowUserErrorDialog(userError);
+ Device.Dispose();
+
+ return false;
+ }
+
+ // Tell the user that we installed a firmware for them.
+ if (userError == UserError.NoFirmware)
+ {
+ firmwareVersion = ContentManager.GetCurrentFirmwareVersion();
+
+ _viewModel.RefreshFirmwareStatus();
+
+ await ContentDialogHelper.CreateInfoDialog(
+ LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogFirmwareInstalledMessage, firmwareVersion.VersionString),
+ LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogFirmwareInstallEmbeddedSuccessMessage, firmwareVersion.VersionString),
+ LocaleManager.Instance[LocaleKeys.InputDialogOk],
+ "",
+ LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
+ }
+ }
+ else
+ {
+ await UserErrorDialog.ShowUserErrorDialog(userError);
+ Device.Dispose();
+
+ return false;
+ }
+ }
+ }
+ }
+
+ Logger.Notice.Print(LogClass.Application, $"Using Firmware Version: {firmwareVersion?.VersionString}");
+
+ if (_isFirmwareTitle)
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as Firmware Title (NCA).");
+
+ if (!Device.LoadNca(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+ }
+ else if (Directory.Exists(ApplicationPath))
+ {
+ string[] romFsFiles = Directory.GetFiles(ApplicationPath, "*.istorage");
+
+ if (romFsFiles.Length == 0)
+ {
+ romFsFiles = Directory.GetFiles(ApplicationPath, "*.romfs");
+ }
+
+ if (romFsFiles.Length > 0)
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as cart with RomFS.");
+
+ if (!Device.LoadCart(ApplicationPath, romFsFiles[0]))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+ }
+ else
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as cart WITHOUT RomFS.");
+
+ if (!Device.LoadCart(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+ }
+ }
+ else if (File.Exists(ApplicationPath))
+ {
+ switch (Path.GetExtension(ApplicationPath).ToLowerInvariant())
+ {
+ case ".xci":
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as XCI.");
+
+ if (!Device.LoadXci(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+
+ break;
+ }
+ case ".nca":
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as NCA.");
+
+ if (!Device.LoadNca(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+
+ break;
+ }
+ case ".nsp":
+ case ".pfs0":
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as NSP.");
+
+ if (!Device.LoadNsp(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+
+ break;
+ }
+ default:
+ {
+ Logger.Info?.Print(LogClass.Application, "Loading as homebrew.");
+
+ try
+ {
+ if (!Device.LoadProgram(ApplicationPath))
+ {
+ Device.Dispose();
+
+ return false;
+ }
+ }
+ catch (ArgumentOutOfRangeException)
+ {
+ Logger.Error?.Print(LogClass.Application, "The specified file is not supported by Ryujinx.");
+
+ Device.Dispose();
+
+ return false;
+ }
+
+ break;
+ }
+ }
+ }
+ else
+ {
+ Logger.Warning?.Print(LogClass.Application, "Please specify a valid XCI/NCA/NSP/PFS0/NRO file.");
+
+ Device.Dispose();
+
+ return false;
+ }
+
+ DiscordIntegrationModule.SwitchToPlayingState(Device.Processes.ActiveApplication.ProgramIdText, Device.Processes.ActiveApplication.Name);
+
+ ApplicationLibrary.LoadAndSaveMetaData(Device.Processes.ActiveApplication.ProgramIdText, appMetadata =>
+ {
+ appMetadata.UpdatePreGame();
+ });
+
+ return true;
+ }
+
+ internal void Resume()
+ {
+ Device?.System.TogglePauseEmulation(false);
+
+ _viewModel.IsPaused = false;
+ _viewModel.Title = TitleHelper.ActiveApplicationTitle(Device?.Processes.ActiveApplication, Program.Version);
+ Logger.Info?.Print(LogClass.Emulation, "Emulation was resumed");
+ }
+
+ internal void Pause()
+ {
+ Device?.System.TogglePauseEmulation(true);
+
+ _viewModel.IsPaused = true;
+ _viewModel.Title = TitleHelper.ActiveApplicationTitle(Device?.Processes.ActiveApplication, Program.Version, LocaleManager.Instance[LocaleKeys.Paused]);
+ Logger.Info?.Print(LogClass.Emulation, "Emulation was paused");
+ }
+
+ private void InitializeSwitchInstance()
+ {
+ // Initialize KeySet.
+ VirtualFileSystem.ReloadKeySet();
+
+ // Initialize Renderer.
+ IRenderer renderer;
+
+ if (ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.Vulkan)
+ {
+ renderer = new VulkanRenderer(
+ Vk.GetApi(),
+ (RendererHost.EmbeddedWindow as EmbeddedWindowVulkan).CreateSurface,
+ VulkanHelper.GetRequiredInstanceExtensions,
+ ConfigurationState.Instance.Graphics.PreferredGpu.Value);
+ }
+<<<<<<< HEAD
+=======
+ else if (ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.Metal && OperatingSystem.IsMacOS())
+ {
+ renderer = new MetalRenderer();
+ }
+>>>>>>> b83dc41f8 (Formatting)
+ else
+ {
+ renderer = new OpenGLRenderer();
+ }
+
+ BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
+
+ var isGALThreaded = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading);
+ if (isGALThreaded)
+ {
+ renderer = new ThreadedRenderer(renderer);
+ }
+
+ Logger.Info?.PrintMsg(LogClass.Gpu, $"Backend Threading ({threadingMode}): {isGALThreaded}");
+
+ // Initialize Configuration.
+ var memoryConfiguration = ConfigurationState.Instance.System.ExpandRam.Value ? MemoryConfiguration.MemoryConfiguration6GiB : MemoryConfiguration.MemoryConfiguration4GiB;
+
+ HLEConfiguration configuration = new(VirtualFileSystem,
+ _viewModel.LibHacHorizonManager,
+ ContentManager,
+ _accountManager,
+ _userChannelPersistence,
+ renderer,
+ InitializeAudio(),
+ memoryConfiguration,
+ _viewModel.UiHandler,
+ (SystemLanguage)ConfigurationState.Instance.System.Language.Value,
+ (RegionCode)ConfigurationState.Instance.System.Region.Value,
+ ConfigurationState.Instance.Graphics.EnableVsync,
+ ConfigurationState.Instance.System.EnableDockedMode,
+ ConfigurationState.Instance.System.EnablePtc,
+ ConfigurationState.Instance.System.EnableInternetAccess,
+ ConfigurationState.Instance.System.EnableFsIntegrityChecks ? IntegrityCheckLevel.ErrorOnInvalid : IntegrityCheckLevel.None,
+ ConfigurationState.Instance.System.FsGlobalAccessLogMode,
+ ConfigurationState.Instance.System.SystemTimeOffset,
+ ConfigurationState.Instance.System.TimeZone,
+ ConfigurationState.Instance.System.MemoryManagerMode,
+ ConfigurationState.Instance.System.IgnoreMissingServices,
+ ConfigurationState.Instance.Graphics.AspectRatio,
+ ConfigurationState.Instance.System.AudioVolume,
+ ConfigurationState.Instance.System.UseHypervisor,
+ ConfigurationState.Instance.Multiplayer.LanInterfaceId.Value,
+ ConfigurationState.Instance.Multiplayer.Mode);
+
+ Device = new Switch(configuration);
+ }
+
+ private static IHardwareDeviceDriver InitializeAudio()
+ {
+ var availableBackends = new List
+ {
+ AudioBackend.SDL2,
+ AudioBackend.SoundIo,
+ AudioBackend.OpenAl,
+ AudioBackend.Dummy,
+ };
+
+ AudioBackend preferredBackend = ConfigurationState.Instance.System.AudioBackend.Value;
+
+ for (int i = 0; i < availableBackends.Count; i++)
+ {
+ if (availableBackends[i] == preferredBackend)
+ {
+ availableBackends.RemoveAt(i);
+ availableBackends.Insert(0, preferredBackend);
+ break;
+ }
+ }
+
+ static IHardwareDeviceDriver InitializeAudioBackend(AudioBackend backend, AudioBackend nextBackend) where T : IHardwareDeviceDriver, new()
+ {
+ if (T.IsSupported)
+ {
+ return new T();
+ }
+
+ Logger.Warning?.Print(LogClass.Audio, $"{backend} is not supported, falling back to {nextBackend}.");
+
+ return null;
+ }
+
+ IHardwareDeviceDriver deviceDriver = null;
+
+ for (int i = 0; i < availableBackends.Count; i++)
+ {
+ AudioBackend currentBackend = availableBackends[i];
+ AudioBackend nextBackend = i + 1 < availableBackends.Count ? availableBackends[i + 1] : AudioBackend.Dummy;
+
+ deviceDriver = currentBackend switch
+ {
+ AudioBackend.SDL2 => InitializeAudioBackend(AudioBackend.SDL2, nextBackend),
+ AudioBackend.SoundIo => InitializeAudioBackend(AudioBackend.SoundIo, nextBackend),
+ AudioBackend.OpenAl => InitializeAudioBackend(AudioBackend.OpenAl, nextBackend),
+ _ => new DummyHardwareDeviceDriver(),
+ };
+
+ if (deviceDriver != null)
+ {
+ ConfigurationState.Instance.System.AudioBackend.Value = currentBackend;
+ break;
+ }
+ }
+
+ MainWindowViewModel.SaveConfig();
+
+ return deviceDriver;
+ }
+
+ private void Window_BoundsChanged(object sender, Size e)
+ {
+ Width = (int)e.Width;
+ Height = (int)e.Height;
+
+ SetRendererWindowSize(e);
+ }
+
+ private void MainLoop()
+ {
+ while (_isActive)
+ {
+ UpdateFrame();
+
+ // Polling becomes expensive if it's not slept.
+ Thread.Sleep(1);
+ }
+ }
+
+ private void RenderLoop()
+ {
+ Dispatcher.UIThread.InvokeAsync(() =>
+ {
+ if (_viewModel.StartGamesInFullscreen)
+ {
+ _viewModel.WindowState = WindowState.FullScreen;
+ }
+
+ if (_viewModel.WindowState == WindowState.FullScreen)
+ {
+ _viewModel.ShowMenuAndStatusBar = false;
+ }
+ });
+
+ _renderer = Device.Gpu.Renderer is ThreadedRenderer tr ? tr.BaseRenderer : Device.Gpu.Renderer;
+
+ _renderer.ScreenCaptured += Renderer_ScreenCaptured;
+
+ (RendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.InitializeBackgroundContext(_renderer);
+
+ Device.Gpu.Renderer.Initialize(_glLogLevel);
+
+ _renderer?.Window?.SetAntiAliasing((Graphics.GAL.AntiAliasing)ConfigurationState.Instance.Graphics.AntiAliasing.Value);
+ _renderer?.Window?.SetScalingFilter((Graphics.GAL.ScalingFilter)ConfigurationState.Instance.Graphics.ScalingFilter.Value);
+ _renderer?.Window?.SetScalingFilterLevel(ConfigurationState.Instance.Graphics.ScalingFilterLevel.Value);
+ _renderer?.Window?.SetColorSpacePassthrough(ConfigurationState.Instance.Graphics.EnableColorSpacePassthrough.Value);
+
+ Width = (int)RendererHost.Bounds.Width;
+ Height = (int)RendererHost.Bounds.Height;
+
+ _renderer.Window.SetSize((int)(Width * _topLevel.RenderScaling), (int)(Height * _topLevel.RenderScaling));
+
+ _chrono.Start();
+
+ Device.Gpu.Renderer.RunLoop(() =>
+ {
+ Device.Gpu.SetGpuThread();
+ Device.Gpu.InitializeShaderCache(_gpuCancellationTokenSource.Token);
+
+ _renderer.Window.ChangeVSyncMode(Device.EnableDeviceVsync);
+
+ while (_isActive)
+ {
+ _ticks += _chrono.ElapsedTicks;
+
+ _chrono.Restart();
+
+ if (Device.WaitFifo())
+ {
+ Device.Statistics.RecordFifoStart();
+ Device.ProcessFrame();
+ Device.Statistics.RecordFifoEnd();
+ }
+
+ while (Device.ConsumeFrameAvailable())
+ {
+ if (!_renderingStarted)
+ {
+ _renderingStarted = true;
+ _viewModel.SwitchToRenderer(false);
+ InitStatus();
+ }
+
+ Device.PresentFrame(() => (RendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.SwapBuffers());
+ }
+
+ if (_ticks >= _ticksPerFrame)
+ {
+ UpdateStatus();
+ }
+ }
+
+ // Make sure all commands in the run loop are fully executed before leaving the loop.
+ if (Device.Gpu.Renderer is ThreadedRenderer threaded)
+ {
+ threaded.FlushThreadedCommands();
+ }
+
+ _gpuDoneEvent.Set();
+ });
+
+ (RendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.MakeCurrent(true);
+ }
+
+ public void InitStatus()
+ {
+ StatusInitEvent?.Invoke(this, new StatusInitEventArgs(
+ ConfigurationState.Instance.Graphics.GraphicsBackend.Value switch
+ {
+ GraphicsBackend.Vulkan => "Vulkan",
+ GraphicsBackend.OpenGl => "OpenGL",
+ _ => throw new NotImplementedException()
+ },
+ $"GPU: {_renderer.GetHardwareInfo().GpuDriver}"));
+ }
+
+ public void UpdateStatus()
+ {
+ // Run a status update only when a frame is to be drawn. This prevents from updating the ui and wasting a render when no frame is queued.
+ string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? LocaleManager.Instance[LocaleKeys.Docked] : LocaleManager.Instance[LocaleKeys.Handheld];
+
+ if (GraphicsConfig.ResScale != 1)
+ {
+ dockedMode += $" ({GraphicsConfig.ResScale}x)";
+ }
+
+ StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
+ Device.EnableDeviceVsync,
+ LocaleManager.Instance[LocaleKeys.VolumeShort] + $": {(int)(Device.GetVolume() * 100)}%",
+ ConfigurationState.Instance.Graphics.GraphicsBackend.Value.ToText(),
+ dockedMode,
+ ConfigurationState.Instance.Graphics.AspectRatio.Value.ToText(),
+ LocaleManager.Instance[LocaleKeys.Game] + $": {Device.Statistics.GetGameFrameRate():00.00} FPS ({Device.Statistics.GetGameFrameTime():00.00} ms)",
+ $"FIFO: {Device.Statistics.GetFifoPercent():00.00} %",
+ $"GPU: {_renderer.GetHardwareInfo().GpuDriver}"));
+ }
+
+ public async Task ShowExitPrompt()
+ {
+ bool shouldExit = !ConfigurationState.Instance.ShowConfirmExit;
+ if (!shouldExit)
+ {
+ if (_dialogShown)
+ {
+ return;
+ }
+
+ _dialogShown = true;
+
+ shouldExit = await ContentDialogHelper.CreateStopEmulationDialog();
+
+ _dialogShown = false;
+ }
+
+ if (shouldExit)
+ {
+ Stop();
+ }
+ }
+
+ private bool UpdateFrame()
+ {
+ if (!_isActive)
+ {
+ return false;
+ }
+
+ NpadManager.Update(ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat());
+
+ if (_viewModel.IsActive)
+ {
+ if (_isCursorInRenderer)
+ {
+ if (ConfigurationState.Instance.Hid.EnableMouse)
+ {
+ HideCursor();
+ }
+ else
+ {
+ switch (ConfigurationState.Instance.HideCursor.Value)
+ {
+ case HideCursorMode.Never:
+ ShowCursor();
+ break;
+ case HideCursorMode.OnIdle:
+ if (Stopwatch.GetTimestamp() - _lastCursorMoveTime >= CursorHideIdleTime * Stopwatch.Frequency)
+ {
+ HideCursor();
+ }
+ else
+ {
+ ShowCursor();
+ }
+ break;
+ case HideCursorMode.Always:
+ HideCursor();
+ break;
+ }
+ }
+ }
+ else
+ {
+ ShowCursor();
+ }
+
+ Dispatcher.UIThread.Post(() =>
+ {
+ if (_keyboardInterface.GetKeyboardStateSnapshot().IsPressed(Key.Delete) && _viewModel.WindowState != WindowState.FullScreen)
+ {
+ Device.Processes.ActiveApplication.DiskCacheLoadState?.Cancel();
+ }
+ });
+
+ KeyboardHotkeyState currentHotkeyState = GetHotkeyState();
+
+ if (currentHotkeyState != _prevHotkeyState)
+ {
+ switch (currentHotkeyState)
+ {
+ case KeyboardHotkeyState.ToggleVSync:
+ ToggleVSync();
+ break;
+ case KeyboardHotkeyState.Screenshot:
+ ScreenshotRequested = true;
+ break;
+ case KeyboardHotkeyState.ShowUI:
+ _viewModel.ShowMenuAndStatusBar = !_viewModel.ShowMenuAndStatusBar;
+ break;
+ case KeyboardHotkeyState.Pause:
+ if (_viewModel.IsPaused)
+ {
+ Resume();
+ }
+ else
+ {
+ Pause();
+ }
+ break;
+ case KeyboardHotkeyState.ToggleMute:
+ if (Device.IsAudioMuted())
+ {
+ Device.SetVolume(_viewModel.VolumeBeforeMute);
+ }
+ else
+ {
+ _viewModel.VolumeBeforeMute = Device.GetVolume();
+ Device.SetVolume(0);
+ }
+
+ _viewModel.Volume = Device.GetVolume();
+ break;
+ case KeyboardHotkeyState.ResScaleUp:
+ GraphicsConfig.ResScale = GraphicsConfig.ResScale % MaxResolutionScale + 1;
+ break;
+ case KeyboardHotkeyState.ResScaleDown:
+ GraphicsConfig.ResScale =
+ (MaxResolutionScale + GraphicsConfig.ResScale - 2) % MaxResolutionScale + 1;
+ break;
+ case KeyboardHotkeyState.VolumeUp:
+ _newVolume = MathF.Round((Device.GetVolume() + VolumeDelta), 2);
+ Device.SetVolume(_newVolume);
+
+ _viewModel.Volume = Device.GetVolume();
+ break;
+ case KeyboardHotkeyState.VolumeDown:
+ _newVolume = MathF.Round((Device.GetVolume() - VolumeDelta), 2);
+ Device.SetVolume(_newVolume);
+
+ _viewModel.Volume = Device.GetVolume();
+ break;
+ case KeyboardHotkeyState.None:
+ (_keyboardInterface as AvaloniaKeyboard).Clear();
+ break;
+ }
+ }
+
+ _prevHotkeyState = currentHotkeyState;
+
+ if (ScreenshotRequested)
+ {
+ ScreenshotRequested = false;
+ _renderer.Screenshot();
+ }
+ }
+
+ // Touchscreen.
+ bool hasTouch = false;
+
+ if (_viewModel.IsActive && !ConfigurationState.Instance.Hid.EnableMouse)
+ {
+ hasTouch = TouchScreenManager.Update(true, (_inputManager.MouseDriver as AvaloniaMouseDriver).IsButtonPressed(MouseButton.Button1), ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat());
+ }
+
+ if (!hasTouch)
+ {
+ Device.Hid.Touchscreen.Update();
+ }
+
+ Device.Hid.DebugPad.Update();
+
+ return true;
+ }
+
+ private KeyboardHotkeyState GetHotkeyState()
+ {
+ KeyboardHotkeyState state = KeyboardHotkeyState.None;
+
+ if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleVsync))
+ {
+ state = KeyboardHotkeyState.ToggleVSync;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot))
+ {
+ state = KeyboardHotkeyState.Screenshot;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUI))
+ {
+ state = KeyboardHotkeyState.ShowUI;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause))
+ {
+ state = KeyboardHotkeyState.Pause;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleMute))
+ {
+ state = KeyboardHotkeyState.ToggleMute;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ResScaleUp))
+ {
+ state = KeyboardHotkeyState.ResScaleUp;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ResScaleDown))
+ {
+ state = KeyboardHotkeyState.ResScaleDown;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.VolumeUp))
+ {
+ state = KeyboardHotkeyState.VolumeUp;
+ }
+ else if (_keyboardInterface.IsPressed((Key)ConfigurationState.Instance.Hid.Hotkeys.Value.VolumeDown))
+ {
+ state = KeyboardHotkeyState.VolumeDown;
+ }
+
+ return state;
+ }
+ }
+}