Switch.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Ryujinx.Audio.Backends.CompatLayer;
  2. using Ryujinx.Audio.Integration;
  3. using Ryujinx.Common.Configuration;
  4. using Ryujinx.Graphics.Gpu;
  5. using Ryujinx.HLE.FileSystem;
  6. using Ryujinx.HLE.HOS;
  7. using Ryujinx.HLE.HOS.Services.Apm;
  8. using Ryujinx.HLE.HOS.Services.Hid;
  9. using Ryujinx.HLE.Loaders.Processes;
  10. using Ryujinx.HLE.UI;
  11. using Ryujinx.Memory;
  12. using System;
  13. namespace Ryujinx.HLE
  14. {
  15. public class Switch : IDisposable
  16. {
  17. public HLEConfiguration Configuration { get; }
  18. public IHardwareDeviceDriver AudioDeviceDriver { get; }
  19. public MemoryBlock Memory { get; }
  20. public GpuContext Gpu { get; }
  21. public VirtualFileSystem FileSystem { get; }
  22. public HOS.Horizon System { get; }
  23. public ProcessLoader Processes { get; }
  24. public PerformanceStatistics Statistics { get; }
  25. public Hid Hid { get; }
  26. public TamperMachine TamperMachine { get; }
  27. public IHostUIHandler UIHandler { get; }
  28. public VSyncMode VSyncMode { get; set; } = VSyncMode.Switch;
  29. public bool CustomVSyncIntervalEnabled { get; set; } = false;
  30. public int CustomVSyncInterval { get; set; }
  31. public long TargetVSyncInterval { get; set; } = 60;
  32. public bool IsFrameAvailable => Gpu.Window.IsFrameAvailable;
  33. public Switch(HLEConfiguration configuration)
  34. {
  35. ArgumentNullException.ThrowIfNull(configuration.GpuRenderer);
  36. ArgumentNullException.ThrowIfNull(configuration.AudioDeviceDriver);
  37. ArgumentNullException.ThrowIfNull(configuration.UserChannelPersistence);
  38. Configuration = configuration;
  39. FileSystem = Configuration.VirtualFileSystem;
  40. UIHandler = Configuration.HostUIHandler;
  41. MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
  42. ? MemoryAllocationFlags.Reserve
  43. : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
  44. #pragma warning disable IDE0055 // Disable formatting
  45. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  46. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  47. Gpu = new GpuContext(Configuration.GpuRenderer);
  48. System = new HOS.Horizon(this);
  49. Statistics = new PerformanceStatistics();
  50. Hid = new Hid(this, System.HidStorage);
  51. Processes = new ProcessLoader(this);
  52. TamperMachine = new TamperMachine();
  53. System.InitializeServices();
  54. System.State.SetLanguage(Configuration.SystemLanguage);
  55. System.State.SetRegion(Configuration.Region);
  56. VSyncMode = Configuration.VSyncMode;
  57. CustomVSyncInterval = Configuration.CustomVSyncInterval;
  58. System.State.DockedMode = Configuration.EnableDockedMode;
  59. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  60. System.EnablePtc = Configuration.EnablePtc;
  61. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  62. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  63. UpdateVSyncInterval();
  64. #pragma warning restore IDE0055
  65. }
  66. public void ProcessFrame()
  67. {
  68. Gpu.ProcessShaderCacheQueue();
  69. Gpu.Renderer.PreFrame();
  70. Gpu.GPFifo.DispatchCalls();
  71. }
  72. public void IncrementCustomVSyncInterval()
  73. {
  74. CustomVSyncInterval += 1;
  75. UpdateVSyncInterval();
  76. }
  77. public void DecrementCustomVSyncInterval()
  78. {
  79. CustomVSyncInterval -= 1;
  80. UpdateVSyncInterval();
  81. }
  82. public void UpdateVSyncInterval()
  83. {
  84. switch (VSyncMode)
  85. {
  86. case VSyncMode.Custom:
  87. TargetVSyncInterval = CustomVSyncInterval;
  88. break;
  89. case VSyncMode.Switch:
  90. TargetVSyncInterval = 60;
  91. break;
  92. case VSyncMode.Unbounded:
  93. TargetVSyncInterval = 1;
  94. break;
  95. }
  96. }
  97. public bool LoadCart(string exeFsDir, string romFsFile = null) => Processes.LoadUnpackedNca(exeFsDir, romFsFile);
  98. public bool LoadXci(string xciFile, ulong applicationId = 0) => Processes.LoadXci(xciFile, applicationId);
  99. public bool LoadNca(string ncaFile) => Processes.LoadNca(ncaFile);
  100. public bool LoadNsp(string nspFile, ulong applicationId = 0) => Processes.LoadNsp(nspFile, applicationId);
  101. public bool LoadProgram(string fileName) => Processes.LoadNxo(fileName);
  102. public void SetVolume(float volume) => AudioDeviceDriver.Volume = Math.Clamp(volume, 0f, 1f);
  103. public float GetVolume() => AudioDeviceDriver.Volume;
  104. public bool IsAudioMuted() => AudioDeviceDriver.Volume == 0;
  105. public void EnableCheats() => ModLoader.EnableCheats(Processes.ActiveApplication.ProgramId, TamperMachine);
  106. public bool WaitFifo() => Gpu.GPFifo.WaitForCommands();
  107. public bool ConsumeFrameAvailable() => Gpu.Window.ConsumeFrameAvailable();
  108. public void PresentFrame(Action swapBuffersCallback) => Gpu.Window.Present(swapBuffersCallback);
  109. public void DisposeGpu() => Gpu.Dispose();
  110. public void Dispose()
  111. {
  112. GC.SuppressFinalize(this);
  113. Dispose(true);
  114. }
  115. protected virtual void Dispose(bool disposing)
  116. {
  117. if (disposing)
  118. {
  119. System.Dispose();
  120. AudioDeviceDriver.Dispose();
  121. FileSystem.Dispose();
  122. Memory.Dispose();
  123. }
  124. }
  125. }
  126. }