Switch.cs 6.1 KB

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