Switch.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Ryujinx.Audio.Backends.CompatLayer;
  2. using Ryujinx.Audio.Integration;
  3. using Ryujinx.Graphics.Gpu;
  4. using Ryujinx.Graphics.Host1x;
  5. using Ryujinx.Graphics.Nvdec;
  6. using Ryujinx.Graphics.Vic;
  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.HOS.Services.Nv.NvDrvServices;
  12. using Ryujinx.Memory;
  13. using System;
  14. namespace Ryujinx.HLE
  15. {
  16. public class Switch : IDisposable
  17. {
  18. public HLEConfiguration Configuration { get; }
  19. public IHardwareDeviceDriver AudioDeviceDriver { get; }
  20. internal MemoryBlock Memory { get; }
  21. public GpuContext Gpu { get; }
  22. internal NvMemoryAllocator MemoryAllocator { get; }
  23. internal Host1xDevice Host1x { get; }
  24. public VirtualFileSystem FileSystem => Configuration.VirtualFileSystem;
  25. public Horizon System { get; }
  26. public ApplicationLoader Application { get; }
  27. public PerformanceStatistics Statistics { get; }
  28. public Hid Hid { get; }
  29. public TamperMachine TamperMachine { get; }
  30. public IHostUiHandler UiHandler { get; }
  31. public bool EnableDeviceVsync { get; set; } = true;
  32. public Switch(HLEConfiguration configuration)
  33. {
  34. if (configuration.GpuRenderer == null)
  35. {
  36. throw new ArgumentNullException(nameof(configuration.GpuRenderer));
  37. }
  38. if (configuration.AudioDeviceDriver == null)
  39. {
  40. throw new ArgumentNullException(nameof(configuration.AudioDeviceDriver));
  41. }
  42. if (configuration.UserChannelPersistence== null)
  43. {
  44. throw new ArgumentNullException(nameof(configuration.UserChannelPersistence));
  45. }
  46. Configuration = configuration;
  47. UiHandler = configuration.HostUiHandler;
  48. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(configuration.AudioDeviceDriver);
  49. Memory = new MemoryBlock(configuration.MemoryConfiguration.ToDramSize(), MemoryAllocationFlags.Reserve);
  50. Gpu = new GpuContext(configuration.GpuRenderer);
  51. MemoryAllocator = new NvMemoryAllocator();
  52. Host1x = new Host1xDevice(Gpu.Synchronization);
  53. var nvdec = new NvdecDevice(Gpu.MemoryManager);
  54. var vic = new VicDevice(Gpu.MemoryManager);
  55. Host1x.RegisterDevice(ClassId.Nvdec, nvdec);
  56. Host1x.RegisterDevice(ClassId.Vic, vic);
  57. nvdec.FrameDecoded += (FrameDecodedEventArgs e) =>
  58. {
  59. // FIXME:
  60. // Figure out what is causing frame ordering issues on H264.
  61. // For now this is needed as workaround.
  62. if (e.CodecId == CodecId.H264)
  63. {
  64. vic.SetSurfaceOverride(e.LumaOffset, e.ChromaOffset, 0);
  65. }
  66. else
  67. {
  68. vic.DisableSurfaceOverride();
  69. }
  70. };
  71. System = new Horizon(this);
  72. System.InitializeServices();
  73. Statistics = new PerformanceStatistics();
  74. Hid = new Hid(this, System.HidStorage);
  75. Hid.InitDevices();
  76. Application = new ApplicationLoader(this);
  77. TamperMachine = new TamperMachine();
  78. Initialize();
  79. }
  80. private void Initialize()
  81. {
  82. System.State.SetLanguage(Configuration.SystemLanguage);
  83. System.State.SetRegion(Configuration.Region);
  84. EnableDeviceVsync = Configuration.EnableVsync;
  85. System.State.DockedMode = Configuration.EnableDockedMode;
  86. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  87. System.EnablePtc = Configuration.EnablePtc;
  88. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  89. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  90. }
  91. public void LoadCart(string exeFsDir, string romFsFile = null)
  92. {
  93. Application.LoadCart(exeFsDir, romFsFile);
  94. }
  95. public void LoadXci(string xciFile)
  96. {
  97. Application.LoadXci(xciFile);
  98. }
  99. public void LoadNca(string ncaFile)
  100. {
  101. Application.LoadNca(ncaFile);
  102. }
  103. public void LoadNsp(string nspFile)
  104. {
  105. Application.LoadNsp(nspFile);
  106. }
  107. public void LoadProgram(string fileName)
  108. {
  109. Application.LoadProgram(fileName);
  110. }
  111. public bool WaitFifo()
  112. {
  113. return Gpu.GPFifo.WaitForCommands();
  114. }
  115. public void ProcessFrame()
  116. {
  117. Gpu.Renderer.PreFrame();
  118. Gpu.GPFifo.DispatchCalls();
  119. }
  120. public bool ConsumeFrameAvailable()
  121. {
  122. return Gpu.Window.ConsumeFrameAvailable();
  123. }
  124. public void PresentFrame(Action swapBuffersCallback)
  125. {
  126. Gpu.Window.Present(swapBuffersCallback);
  127. }
  128. public void DisposeGpu()
  129. {
  130. Gpu.Dispose();
  131. }
  132. public void Dispose()
  133. {
  134. Dispose(true);
  135. }
  136. protected virtual void Dispose(bool disposing)
  137. {
  138. if (disposing)
  139. {
  140. System.Dispose();
  141. Host1x.Dispose();
  142. AudioDeviceDriver.Dispose();
  143. FileSystem.Unload();
  144. Memory.Dispose();
  145. }
  146. }
  147. }
  148. }