Switch.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ArgumentNullException.ThrowIfNull(configuration.GpuRenderer);
  32. ArgumentNullException.ThrowIfNull(configuration.AudioDeviceDriver);
  33. ArgumentNullException.ThrowIfNull(configuration.UserChannelPersistence);
  34. Configuration = configuration;
  35. FileSystem = Configuration.VirtualFileSystem;
  36. UiHandler = Configuration.HostUiHandler;
  37. MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
  38. ? MemoryAllocationFlags.Reserve
  39. : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
  40. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  41. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  42. Gpu = new GpuContext(Configuration.GpuRenderer);
  43. System = new Horizon(this);
  44. Statistics = new PerformanceStatistics();
  45. Hid = new Hid(this, System.HidStorage);
  46. Application = new ApplicationLoader(this);
  47. TamperMachine = new TamperMachine();
  48. System.State.SetLanguage(Configuration.SystemLanguage);
  49. System.State.SetRegion(Configuration.Region);
  50. EnableDeviceVsync = Configuration.EnableVsync;
  51. System.State.DockedMode = Configuration.EnableDockedMode;
  52. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  53. System.EnablePtc = Configuration.EnablePtc;
  54. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  55. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  56. }
  57. public void LoadCart(string exeFsDir, string romFsFile = null)
  58. {
  59. Application.LoadCart(exeFsDir, romFsFile);
  60. }
  61. public void LoadXci(string xciFile)
  62. {
  63. Application.LoadXci(xciFile);
  64. }
  65. public void LoadNca(string ncaFile)
  66. {
  67. Application.LoadNca(ncaFile);
  68. }
  69. public void LoadNsp(string nspFile)
  70. {
  71. Application.LoadNsp(nspFile);
  72. }
  73. public void LoadProgram(string fileName)
  74. {
  75. Application.LoadProgram(fileName);
  76. }
  77. public bool WaitFifo()
  78. {
  79. return Gpu.GPFifo.WaitForCommands();
  80. }
  81. public void ProcessFrame()
  82. {
  83. Gpu.ProcessShaderCacheQueue();
  84. Gpu.Renderer.PreFrame();
  85. Gpu.GPFifo.DispatchCalls();
  86. }
  87. public bool ConsumeFrameAvailable()
  88. {
  89. return Gpu.Window.ConsumeFrameAvailable();
  90. }
  91. public void PresentFrame(Action swapBuffersCallback)
  92. {
  93. Gpu.Window.Present(swapBuffersCallback);
  94. }
  95. public void SetVolume(float volume)
  96. {
  97. System.SetVolume(Math.Clamp(volume, 0, 1));
  98. }
  99. public float GetVolume()
  100. {
  101. return System.GetVolume();
  102. }
  103. public void EnableCheats()
  104. {
  105. FileSystem.ModLoader.EnableCheats(Application.TitleId, TamperMachine);
  106. }
  107. public bool IsAudioMuted()
  108. {
  109. return System.GetVolume() == 0;
  110. }
  111. public void DisposeGpu()
  112. {
  113. Gpu.Dispose();
  114. }
  115. public void Dispose()
  116. {
  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. }