Switch.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Ui;
  10. using Ryujinx.Memory;
  11. using System;
  12. namespace Ryujinx.HLE
  13. {
  14. public class Switch : IDisposable
  15. {
  16. public HLEConfiguration Configuration { get; }
  17. public IHardwareDeviceDriver AudioDeviceDriver { get; }
  18. public MemoryBlock Memory { get; }
  19. public GpuContext Gpu { get; }
  20. public VirtualFileSystem FileSystem { get; }
  21. public Horizon System { get; }
  22. public ApplicationLoader Application { get; }
  23. public PerformanceStatistics Statistics { get; }
  24. public Hid Hid { get; }
  25. public TamperMachine TamperMachine { get; }
  26. public IHostUiHandler UiHandler { get; }
  27. public bool EnableDeviceVsync { get; set; } = true;
  28. public bool IsFrameAvailable => Gpu.Window.IsFrameAvailable;
  29. public Switch(HLEConfiguration configuration)
  30. {
  31. if (configuration.GpuRenderer == null)
  32. {
  33. throw new ArgumentNullException(nameof(configuration.GpuRenderer));
  34. }
  35. if (configuration.AudioDeviceDriver == null)
  36. {
  37. throw new ArgumentNullException(nameof(configuration.AudioDeviceDriver));
  38. }
  39. if (configuration.UserChannelPersistence == null)
  40. {
  41. throw new ArgumentNullException(nameof(configuration.UserChannelPersistence));
  42. }
  43. Configuration = configuration;
  44. FileSystem = Configuration.VirtualFileSystem;
  45. UiHandler = Configuration.HostUiHandler;
  46. MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
  47. ? MemoryAllocationFlags.Reserve
  48. : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
  49. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  50. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  51. Gpu = new GpuContext(Configuration.GpuRenderer);
  52. System = new Horizon(this);
  53. Statistics = new PerformanceStatistics();
  54. Hid = new Hid(this, System.HidStorage);
  55. Application = new ApplicationLoader(this);
  56. TamperMachine = new TamperMachine();
  57. System.State.SetLanguage(Configuration.SystemLanguage);
  58. System.State.SetRegion(Configuration.Region);
  59. EnableDeviceVsync = Configuration.EnableVsync;
  60. System.State.DockedMode = Configuration.EnableDockedMode;
  61. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  62. System.EnablePtc = Configuration.EnablePtc;
  63. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  64. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  65. }
  66. public void LoadCart(string exeFsDir, string romFsFile = null)
  67. {
  68. Application.LoadCart(exeFsDir, romFsFile);
  69. }
  70. public void LoadXci(string xciFile)
  71. {
  72. Application.LoadXci(xciFile);
  73. }
  74. public void LoadNca(string ncaFile)
  75. {
  76. Application.LoadNca(ncaFile);
  77. }
  78. public void LoadNsp(string nspFile)
  79. {
  80. Application.LoadNsp(nspFile);
  81. }
  82. public void LoadProgram(string fileName)
  83. {
  84. Application.LoadProgram(fileName);
  85. }
  86. public bool WaitFifo()
  87. {
  88. return Gpu.GPFifo.WaitForCommands();
  89. }
  90. public void ProcessFrame()
  91. {
  92. Gpu.ProcessShaderCacheQueue();
  93. Gpu.Renderer.PreFrame();
  94. Gpu.GPFifo.DispatchCalls();
  95. }
  96. public bool ConsumeFrameAvailable()
  97. {
  98. return Gpu.Window.ConsumeFrameAvailable();
  99. }
  100. public void PresentFrame(Action swapBuffersCallback)
  101. {
  102. Gpu.Window.Present(swapBuffersCallback);
  103. }
  104. public void SetVolume(float volume)
  105. {
  106. System.SetVolume(Math.Clamp(volume, 0, 1));
  107. }
  108. public float GetVolume()
  109. {
  110. return System.GetVolume();
  111. }
  112. public void EnableCheats()
  113. {
  114. FileSystem.ModLoader.EnableCheats(Application.TitleId, TamperMachine);
  115. }
  116. public bool IsAudioMuted()
  117. {
  118. return System.GetVolume() == 0;
  119. }
  120. public void DisposeGpu()
  121. {
  122. Gpu.Dispose();
  123. }
  124. public void Dispose()
  125. {
  126. Dispose(true);
  127. }
  128. protected virtual void Dispose(bool disposing)
  129. {
  130. if (disposing)
  131. {
  132. System.Dispose();
  133. AudioDeviceDriver.Dispose();
  134. FileSystem.Dispose();
  135. Memory.Dispose();
  136. }
  137. }
  138. }
  139. }