NvGpuFifo.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Ryujinx.Graphics.Memory;
  2. using System.Collections.Concurrent;
  3. using System.Threading;
  4. namespace Ryujinx.Graphics
  5. {
  6. public class NvGpuFifo
  7. {
  8. private const int MacrosCount = 0x80;
  9. private const int MacroIndexMask = MacrosCount - 1;
  10. //Note: The size of the macro memory is unknown, we just make
  11. //a guess here and use 256kb as the size. Increase if needed.
  12. private const int MmeWords = 256 * 256;
  13. private NvGpu Gpu;
  14. private ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])> BufferQueue;
  15. private NvGpuEngine[] SubChannels;
  16. public AutoResetEvent Event { get; private set; }
  17. private struct CachedMacro
  18. {
  19. public int Position { get; private set; }
  20. private MacroInterpreter Interpreter;
  21. public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position)
  22. {
  23. this.Position = Position;
  24. Interpreter = new MacroInterpreter(PFifo, Engine);
  25. }
  26. public void PushParam(int Param)
  27. {
  28. Interpreter?.Fifo.Enqueue(Param);
  29. }
  30. public void Execute(NvGpuVmm Vmm, int[] Mme, int Param)
  31. {
  32. Interpreter?.Execute(Vmm, Mme, Position, Param);
  33. }
  34. }
  35. private int CurrMacroPosition;
  36. private int CurrMacroBindIndex;
  37. private CachedMacro[] Macros;
  38. private int[] Mme;
  39. public NvGpuFifo(NvGpu Gpu)
  40. {
  41. this.Gpu = Gpu;
  42. BufferQueue = new ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])>();
  43. SubChannels = new NvGpuEngine[8];
  44. Macros = new CachedMacro[MacrosCount];
  45. Mme = new int[MmeWords];
  46. Event = new AutoResetEvent(false);
  47. }
  48. public void PushBuffer(NvGpuVmm Vmm, NvGpuPBEntry[] Buffer)
  49. {
  50. BufferQueue.Enqueue((Vmm, Buffer));
  51. Event.Set();
  52. }
  53. public void DispatchCalls()
  54. {
  55. while (Step());
  56. }
  57. private (NvGpuVmm Vmm, NvGpuPBEntry[] Pb) Curr;
  58. private int CurrPbEntryIndex;
  59. public bool Step()
  60. {
  61. while (Curr.Pb == null || Curr.Pb.Length <= CurrPbEntryIndex)
  62. {
  63. if (!BufferQueue.TryDequeue(out Curr))
  64. {
  65. return false;
  66. }
  67. Gpu.Engine3d.ResetCache();
  68. Gpu.ResourceManager.ClearPbCache();
  69. CurrPbEntryIndex = 0;
  70. }
  71. CallMethod(Curr.Vmm, Curr.Pb[CurrPbEntryIndex++]);
  72. return true;
  73. }
  74. private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  75. {
  76. if ((NvGpuFifoMeth)PBEntry.Method == NvGpuFifoMeth.BindChannel)
  77. {
  78. NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0];
  79. SubChannels[PBEntry.SubChannel] = Engine;
  80. }
  81. else
  82. {
  83. switch (SubChannels[PBEntry.SubChannel])
  84. {
  85. case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break;
  86. case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break;
  87. case NvGpuEngine.P2mf: CallP2mfMethod(Vmm, PBEntry); break;
  88. case NvGpuEngine.M2mf: CallM2mfMethod(Vmm, PBEntry); break;
  89. }
  90. }
  91. }
  92. private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  93. {
  94. Gpu.Engine2d.CallMethod(Vmm, PBEntry);
  95. }
  96. private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  97. {
  98. if (PBEntry.Method < 0x80)
  99. {
  100. switch ((NvGpuFifoMeth)PBEntry.Method)
  101. {
  102. case NvGpuFifoMeth.SetMacroUploadAddress:
  103. {
  104. CurrMacroPosition = PBEntry.Arguments[0];
  105. break;
  106. }
  107. case NvGpuFifoMeth.SendMacroCodeData:
  108. {
  109. foreach (int Arg in PBEntry.Arguments)
  110. {
  111. Mme[CurrMacroPosition++] = Arg;
  112. }
  113. break;
  114. }
  115. case NvGpuFifoMeth.SetMacroBindingIndex:
  116. {
  117. CurrMacroBindIndex = PBEntry.Arguments[0];
  118. break;
  119. }
  120. case NvGpuFifoMeth.BindMacro:
  121. {
  122. int Position = PBEntry.Arguments[0];
  123. Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position);
  124. break;
  125. }
  126. }
  127. }
  128. else if (PBEntry.Method < 0xe00)
  129. {
  130. Gpu.Engine3d.CallMethod(Vmm, PBEntry);
  131. }
  132. else
  133. {
  134. int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask;
  135. if ((PBEntry.Method & 1) != 0)
  136. {
  137. foreach (int Arg in PBEntry.Arguments)
  138. {
  139. Macros[MacroIndex].PushParam(Arg);
  140. }
  141. }
  142. else
  143. {
  144. Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]);
  145. }
  146. }
  147. }
  148. private void CallP2mfMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  149. {
  150. Gpu.EngineP2mf.CallMethod(Vmm, PBEntry);
  151. }
  152. private void CallM2mfMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  153. {
  154. Gpu.EngineM2mf.CallMethod(Vmm, PBEntry);
  155. }
  156. }
  157. }