NvGpuFifo.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Ryujinx.HLE.Gpu.Memory;
  2. using System.Collections.Concurrent;
  3. using System.Threading;
  4. namespace Ryujinx.HLE.Gpu.Engines
  5. {
  6. 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. foreach (NvGpuPBEntry PBEntry in Buffer)
  51. {
  52. BufferQueue.Enqueue((Vmm, PBEntry));
  53. }
  54. Event.Set();
  55. }
  56. public void DispatchCalls()
  57. {
  58. while (Step());
  59. }
  60. public bool Step()
  61. {
  62. if (BufferQueue.TryDequeue(out (NvGpuVmm Vmm, NvGpuPBEntry PBEntry) Tuple))
  63. {
  64. CallMethod(Tuple.Vmm, Tuple.PBEntry);
  65. return true;
  66. }
  67. return false;
  68. }
  69. private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  70. {
  71. if (PBEntry.Method < 0x80)
  72. {
  73. switch ((NvGpuFifoMeth)PBEntry.Method)
  74. {
  75. case NvGpuFifoMeth.BindChannel:
  76. {
  77. NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0];
  78. SubChannels[PBEntry.SubChannel] = Engine;
  79. break;
  80. }
  81. case NvGpuFifoMeth.SetMacroUploadAddress:
  82. {
  83. CurrMacroPosition = PBEntry.Arguments[0];
  84. break;
  85. }
  86. case NvGpuFifoMeth.SendMacroCodeData:
  87. {
  88. foreach (int Arg in PBEntry.Arguments)
  89. {
  90. Mme[CurrMacroPosition++] = Arg;
  91. }
  92. break;
  93. }
  94. case NvGpuFifoMeth.SetMacroBindingIndex:
  95. {
  96. CurrMacroBindIndex = PBEntry.Arguments[0];
  97. break;
  98. }
  99. case NvGpuFifoMeth.BindMacro:
  100. {
  101. int Position = PBEntry.Arguments[0];
  102. Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position);
  103. break;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. switch (SubChannels[PBEntry.SubChannel])
  110. {
  111. case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break;
  112. case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break;
  113. case NvGpuEngine.Dma: CallDmaMethod(Vmm, PBEntry); break;
  114. }
  115. }
  116. }
  117. private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  118. {
  119. Gpu.Engine2d.CallMethod(Vmm, PBEntry);
  120. }
  121. private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  122. {
  123. if (PBEntry.Method < 0xe00)
  124. {
  125. Gpu.Engine3d.CallMethod(Vmm, PBEntry);
  126. }
  127. else
  128. {
  129. int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask;
  130. if ((PBEntry.Method & 1) != 0)
  131. {
  132. foreach (int Arg in PBEntry.Arguments)
  133. {
  134. Macros[MacroIndex].PushParam(Arg);
  135. }
  136. }
  137. else
  138. {
  139. Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]);
  140. }
  141. }
  142. }
  143. private void CallDmaMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  144. {
  145. Gpu.EngineDma.CallMethod(Vmm, PBEntry);
  146. }
  147. }
  148. }